File: BackgroundSync.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 (309 lines) | stat: -rw-r--r-- 10,553 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * This file is part of buteo-syncfw package
 *
 * Copyright (C) 2014 Jolla Ltd.
 *
 * Contact: Valerio Valerio <valerio.valerio@jolla.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include "LogMacros.h"
#include "BackgroundSync.h"
#include <QStringList>
#include <keepalive/backgroundactivity.h>

// 24 hours
const int MAX_FREQUENCY = 1440;

BackgroundSync::BackgroundSync(QObject *aParent)
    :  QObject(aParent)
{
    FUNCTION_CALL_TRACE(lcButeoTrace);
}

BackgroundSync::~BackgroundSync()
{
    FUNCTION_CALL_TRACE(lcButeoTrace);

    removeAll();
}

void BackgroundSync::removeAll()
{
    FUNCTION_CALL_TRACE(lcButeoTrace);

    QStringList profNames;

    QMapIterator<QString, BActivityStruct> iter(iScheduledSyncs);
    while (iter.hasNext()) {
        iter.next();
        profNames.append(iter.key());
    }

    for (int i = 0; i < profNames.size(); i++) {
        remove(profNames[i]);
    }

    removeAllSwitches();
}

bool BackgroundSync::remove(const QString &aProfName)
{
    FUNCTION_CALL_TRACE(lcButeoTrace);

    removeSwitch(aProfName);

    if (iScheduledSyncs.contains(aProfName) == false)
        return false;

    BActivityStruct &tmp = iScheduledSyncs[aProfName];

    tmp.backgroundActivity->stop();
    delete tmp.backgroundActivity;

    iScheduledSyncs.remove(aProfName);
    return true;
}

bool BackgroundSync::set(const QString &aProfName, int seconds)
{
    FUNCTION_CALL_TRACE(lcButeoTrace);

    if (aProfName.isEmpty())
        return false;

    if (iScheduledSyncs.contains(aProfName) == true) {
        // Can't schedule sync for such long interval removing existent profile if it exists,
        // new background activity will be added below
        if ((seconds / 60 >  MAX_FREQUENCY)) {
            remove(aProfName);
        } else {

            BActivityStruct &newAct = iScheduledSyncs[aProfName];
            BackgroundActivity::Frequency frequency = frequencyFromSeconds(seconds);

            if (newAct.frequency != frequency) {
                newAct.backgroundActivity->stop();
                newAct.frequency = frequency;
                newAct.backgroundActivity->setWakeupFrequency(newAct.frequency);
                newAct.backgroundActivity->wait();
                qCDebug(lcButeoMsyncd) << "BackgroundSync::set() Rescheduling for" << aProfName << "with frequency" <<
                          (seconds / 60) << "minutes, waiting.";
                return true;
            } else {
                newAct.backgroundActivity->wait();
                qCDebug(lcButeoMsyncd) << "BackgroundSync::set() Frequency unchanged for" << aProfName << ", waiting.";
                return true; //returning 'true' - no immediate sync request to be sent.
            }
        }
    }

    BActivityStruct &newAct = iScheduledSyncs[aProfName];
    newAct.backgroundActivity = new BackgroundActivity(this);
    newAct.id = newAct.backgroundActivity->id();
    connect(newAct.backgroundActivity, SIGNAL(running()), this, SLOT(onBackgroundSyncStarted()));
    if (seconds / 60 >  MAX_FREQUENCY) {
        newAct.frequency = BackgroundActivity::Range; // 0
        newAct.backgroundActivity->wait(seconds);
        qCDebug(lcButeoMsyncd) << "BackgroundSync::set() profile name =" << aProfName << "without a valid frequency, waiting for" << seconds <<
                  "seconds.";
    } else {
        newAct.frequency = frequencyFromSeconds(seconds);
        newAct.backgroundActivity->setWakeupFrequency(newAct.frequency);
        newAct.backgroundActivity->wait();
        qCDebug(lcButeoMsyncd) << "BackgroundSync::set() profile name =" << aProfName << "with frequency " <<
                  (seconds / 60) << "minutes, waiting.";
    }
    return true;
}

void BackgroundSync::onBackgroundSyncStarted()
{
    FUNCTION_CALL_TRACE(lcButeoTrace);

    BackgroundActivity *tempAct = static_cast<BackgroundActivity *>(sender());

    QString profName = getProfNameFromId(tempAct->id());

    if (!profName.isEmpty()) {
        qCDebug(lcButeoMsyncd) << "BackgroundSync started, for profile = " << profName;
        emit onBackgroundSyncRunning(profName);
    } else {
        qCWarning(lcButeoMsyncd) << "BackgroundSync: Error: profile for background activity not found!  Stopping background activity.";
        tempAct->stop(); // but don't delete tempAct to avoid possible crash in later profile cleanup.
    }
}

void BackgroundSync::onBackgroundSyncCompleted(QString aProfName)
{
    FUNCTION_CALL_TRACE(lcButeoTrace);
    qCDebug(lcButeoMsyncd) << "BackgroundSync completed, removing activity, profile name = " << aProfName;
    remove(aProfName);
}

QString BackgroundSync::getProfNameFromId(const QString activityId) const
{
    FUNCTION_CALL_TRACE(lcButeoTrace);

    QMapIterator<QString, BActivityStruct> iter(iScheduledSyncs);

    while (iter.hasNext()) {
        iter.next();
        const BActivityStruct &tmp = iter.value();
        if (tmp.id == activityId) {
            return iter.key();
            break;
        }
    }

    return QString();
}

BackgroundActivity::Frequency BackgroundSync::frequencyFromSeconds(int seconds)
{
    // Don't allow frequencies smaller than 5 mins.
    // In rare cases is possible that seconds is 0
    int minutes = seconds / 60;

    if (minutes <= 5)
        return BackgroundActivity::FiveMinutes;
    else if (minutes <= 10)
        return BackgroundActivity::TenMinutes;
    else if (minutes <= 15)
        return BackgroundActivity::FifteenMinutes;
    else if (minutes <= 30)
        return BackgroundActivity::ThirtyMinutes;
    else if (minutes <= 60)
        return BackgroundActivity::OneHour;
    else if (minutes <= 2 * 60)
        return BackgroundActivity::TwoHours;
    else if (minutes <= 4 * 60)
        return BackgroundActivity::FourHours;
    else if (minutes <= 8 * 60)
        return BackgroundActivity::EightHours;
    else if (minutes <= 10 * 60)
        return BackgroundActivity::TenHours;
    else if (minutes <= 12 * 60)
        return BackgroundActivity::TwelveHours;
    else
        return BackgroundActivity::TwentyFourHours;
}

// sync switches

void BackgroundSync::removeAllSwitches()
{
    FUNCTION_CALL_TRACE(lcButeoTrace);

    QStringList profNames;
    QMapIterator<QString, BActivitySwitchStruct> iter(iScheduledSwitch);
    while (iter.hasNext()) {
        iter.next();
        profNames.append(iter.key());
    }

    for (int i = 0; i < profNames.size(); i++) {
        removeSwitch(profNames[i]);
    }
}

bool BackgroundSync::removeSwitch(const QString &aProfName)
{
    FUNCTION_CALL_TRACE(lcButeoTrace);

    if (iScheduledSwitch.contains(aProfName) == false)
        return false;

    BActivitySwitchStruct &tmp = iScheduledSwitch[aProfName];

    tmp.backgroundActivity->stop();
    delete tmp.backgroundActivity;

    iScheduledSwitch.remove(aProfName);
    return true;
}

bool BackgroundSync::setSwitch(const QString &aProfName, const QDateTime &aSwitchTime)
{
    FUNCTION_CALL_TRACE(lcButeoTrace);

    if (aProfName.isEmpty())
        return false;

    int switchSecs = QDateTime::currentDateTime().secsTo(aSwitchTime);
    if (iScheduledSwitch.contains(aProfName) == true) {
        BActivitySwitchStruct &newSwitch = iScheduledSwitch[aProfName];
        if (newSwitch.nextSwitch != aSwitchTime) {
            // If activity's state was already Waiting, the state doesn't change, nothing happens and
            // the existing background activity keeps running until the previously set time expires, so we have to stop it.
            newSwitch.backgroundActivity->stop();
            newSwitch.nextSwitch = aSwitchTime;
            newSwitch.backgroundActivity->wait(switchSecs);
            qCDebug(lcButeoMsyncd) << "BackgroundSync::setSwitch() Rescheduling switch for" << aProfName << "at" << aSwitchTime.toString() << "(" <<
                      switchSecs << "secs ) waiting.";
        } else {
            newSwitch.backgroundActivity->wait(switchSecs);
            qCDebug(lcButeoMsyncd) << "BackgroundSync::setSwitch() Profile" << aProfName << "already with the same switch timer, at" <<
                      aSwitchTime.toString() << "(" << switchSecs << "secs ) waiting.";
        }
    } else {
        BActivitySwitchStruct &newSwitch = iScheduledSwitch[aProfName];
        newSwitch.backgroundActivity = new BackgroundActivity(this);
        newSwitch.id = newSwitch.backgroundActivity->id();
        connect(newSwitch.backgroundActivity, SIGNAL(running()), this, SLOT(onBackgroundSwitchStarted()));
        newSwitch.nextSwitch = aSwitchTime;
        newSwitch.backgroundActivity->wait(switchSecs);
        qCDebug(lcButeoMsyncd) << "BackgroundSync::setSwitch() Set switch for profile name =" << aProfName << "at" << aSwitchTime.toString() <<
                  "(" << switchSecs << "secs ) waiting.";
    }
    return true;
}

void BackgroundSync::onBackgroundSwitchStarted()
{
    FUNCTION_CALL_TRACE(lcButeoTrace);

    BackgroundActivity *tempAct = static_cast<BackgroundActivity *>(sender());

    QString profName = getProfNameFromSwitchId(tempAct->id());

    if (!profName.isEmpty()) {
        qCDebug(lcButeoMsyncd) << "BackgroundSync: switch timer started, for profile = " << profName;
        emit onBackgroundSwitchRunning(profName);
    } else {
        qCWarning(lcButeoMsyncd) << "BackgroundSync: Error: profile for switch timer not found!  Stopping background activity.";
        tempAct->stop(); // but don't delete tempAct to avoid possible crash in later profile cleanup.
    }
}

QString BackgroundSync::getProfNameFromSwitchId(const QString activityId) const
{
    FUNCTION_CALL_TRACE(lcButeoTrace);

    QMapIterator<QString, BActivitySwitchStruct> iter(iScheduledSwitch);

    while (iter.hasNext()) {
        iter.next();
        const BActivitySwitchStruct &tmp = iter.value();
        if (tmp.id == activityId) {
            return iter.key();
            break;
        }
    }

    return QString();
}