File: SyncOnChangeScheduler.cpp

package info (click to toggle)
buteo-syncfw 0.11.10-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,704 kB
  • sloc: cpp: 18,406; xml: 396; sh: 34; makefile: 20
file content (87 lines) | stat: -rw-r--r-- 2,555 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <QTimer>

#include "SyncOnChangeScheduler.h"
#include "SyncProfile.h"
#include "LogMacros.h"

using namespace Buteo;

SyncOnChangeScheduler::SyncOnChangeScheduler()
{
    FUNCTION_CALL_TRACE(lcButeoTrace);
}

SyncOnChangeScheduler::~SyncOnChangeScheduler()
{
    FUNCTION_CALL_TRACE(lcButeoTrace);
    foreach (QObject *o, iSOCTimers.values()) {
        delete o;
    }
    iSOCTimers.clear();
    iSOCProfileNames.clear();
}

bool SyncOnChangeScheduler::addProfile(const SyncProfile *aProfile)
{
    FUNCTION_CALL_TRACE(lcButeoTrace);
    bool scheduled = false;
    if (aProfile && !iSOCProfileNames.contains(aProfile->name())) {
        qint32 time = aProfile->syncOnChangeAfter();
        iSOCProfileNames << aProfile->name();
        SyncOnChangeTimer *SOCtimer = new SyncOnChangeTimer(aProfile, time);
        QObject::connect(SOCtimer, SIGNAL(timeout(const SyncProfile *)),
                         this, SLOT(sync(const SyncProfile *)),
                         Qt::QueuedConnection);
        SOCtimer->fire();
        scheduled = true;
        iSOCTimers.insert(aProfile->name(), SOCtimer);
        qCDebug(lcButeoMsyncd) << "Sync on change scheduled for profile" << aProfile->name();
    } else if (aProfile) {
        qCDebug(lcButeoMsyncd) << "Sync on change already scheduled for profile" << aProfile->name();
    }
    return scheduled;
}

void SyncOnChangeScheduler::removeProfile(const QString &aProfileName)
{
    FUNCTION_CALL_TRACE(lcButeoTrace);
    iSOCProfileNames.removeAll(aProfileName);
    // cancel timer
    delete iSOCTimers.take(aProfileName);
}

void SyncOnChangeScheduler::sync(const SyncProfile *aProfile)
{
    FUNCTION_CALL_TRACE(lcButeoTrace);
    iSOCProfileNames.removeAll(aProfile->name());
    iSOCTimers.remove(aProfile->name());
    SyncOnChangeTimer *SOCtimer = qobject_cast<SyncOnChangeTimer *>(sender());
    if (SOCtimer) {
        qCDebug(lcButeoMsyncd) << "Sync on change for profile" << aProfile->name();
        delete SOCtimer;
        emit syncNow(aProfile->name());
    }
}

SyncOnChangeTimer::SyncOnChangeTimer(const SyncProfile *profile, const quint32 &aTimeout) :
    iSyncProfile(profile), iTimeout(aTimeout)
{
    FUNCTION_CALL_TRACE(lcButeoTrace);
}

SyncOnChangeTimer::~SyncOnChangeTimer()
{
    FUNCTION_CALL_TRACE(lcButeoTrace);
}

void SyncOnChangeTimer::fire()
{
    FUNCTION_CALL_TRACE(lcButeoTrace);
    QTimer::singleShot(iTimeout * 1000, this, SLOT(onTimeout()));
}

void SyncOnChangeTimer::onTimeout()
{
    FUNCTION_CALL_TRACE(lcButeoTrace);
    emit timeout(iSyncProfile);
}