File indexing completed on 2025-01-19 04:56:45

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Mario Bensi <mbensi@ipsquad.net>
0003  SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 
0007 #include "monitorspy.h"
0008 #include <QTest>
0009 
0010 static int s_expirationDelay = 1000;
0011 
0012 int MonitorSpy::expirationDelay()
0013 {
0014     return s_expirationDelay;
0015 }
0016 
0017 void MonitorSpy::setExpirationDelay(int value)
0018 {
0019     s_expirationDelay = value;
0020 }
0021 
0022 MonitorSpy::MonitorSpy(Akonadi::MonitorInterface* monitor, QObject *parent)
0023     : QObject(parent),
0024       m_monitor(monitor),
0025       m_timer(new QTimer(this)),
0026       m_isFinished(false)
0027 {
0028     m_timer->setTimerType(Qt::PreciseTimer);
0029 
0030     connect(m_monitor, &Akonadi::MonitorInterface::collectionAdded, this, &MonitorSpy::restartTimer);
0031     connect(m_monitor, &Akonadi::MonitorInterface::collectionRemoved, this, &MonitorSpy::restartTimer);
0032     connect(m_monitor, &Akonadi::MonitorInterface::collectionChanged, this, &MonitorSpy::restartTimer);
0033     connect(m_monitor, &Akonadi::MonitorInterface::collectionSelectionChanged, this, &MonitorSpy::restartTimer);
0034 
0035     connect(m_monitor, &Akonadi::MonitorInterface::itemAdded, this, &MonitorSpy::restartTimer);
0036     connect(m_monitor, &Akonadi::MonitorInterface::itemRemoved, this, &MonitorSpy::restartTimer);
0037     connect(m_monitor, &Akonadi::MonitorInterface::itemChanged, this, &MonitorSpy::restartTimer);
0038     connect(m_monitor, &Akonadi::MonitorInterface::itemMoved, this, &MonitorSpy::restartTimer);
0039 
0040     connect(m_timer, &QTimer::timeout, this, &MonitorSpy::onDelayExpired);
0041 }
0042 
0043 void MonitorSpy::waitForStableState()
0044 {
0045     m_isFinished = false;
0046     m_timer->start(s_expirationDelay);
0047     while (!m_isFinished)
0048         QTest::qWait(20);
0049 }
0050 
0051 void MonitorSpy::restartTimer()
0052 {
0053     m_timer->start(s_expirationDelay);
0054 }
0055 
0056 void MonitorSpy::onDelayExpired()
0057 {
0058     m_isFinished = true;
0059 }
0060 
0061 #include "moc_monitorspy.cpp"