2
* This file is part of buteo-syncfw package
4
* Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
6
* Contact: Sateesh Kavuri <sateesh.kavuri@nokia.com>
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public License
10
* version 2.1 as published by the Free Software Foundation.
12
* This library is distributed in the hope that it will be useful, but
13
* WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25
#include "SyncSchedule.h"
26
#include "SyncSchedule_p.h"
27
#include "ProfileEngineDefs.h"
28
#include <QDomDocument>
29
#include <QStringList>
31
using namespace Buteo;
33
static const QString DAY_SEPARATOR = ",";
35
SyncSchedulePrivate::SyncSchedulePrivate()
36
: iInterval(0), iRushInterval(0), iRushEnabled(false)
40
SyncSchedulePrivate::SyncSchedulePrivate(const SyncSchedulePrivate &aSource)
41
: iDays(aSource.iDays),
43
iInterval(aSource.iInterval),
44
iRushDays(aSource.iRushDays),
45
iRushBegin(aSource.iRushBegin),
46
iRushEnd(aSource.iRushEnd),
47
iRushInterval(aSource.iRushInterval),
48
iRushEnabled(aSource.iRushEnabled)
52
SyncSchedule::SyncSchedule()
53
: d_ptr(new SyncSchedulePrivate())
57
SyncSchedule::SyncSchedule(const SyncSchedule &aSource)
58
: d_ptr(new SyncSchedulePrivate(*aSource.d_ptr))
62
SyncSchedule::SyncSchedule(const QDomElement &aRoot)
63
: d_ptr(new SyncSchedulePrivate())
65
d_ptr->iTime = QTime::fromString(aRoot.attribute(ATTR_TIME), Qt::ISODate);
66
d_ptr->iInterval = aRoot.attribute(ATTR_INTERVAL).toUInt();
67
d_ptr->iDays = d_ptr->parseDays(aRoot.attribute(ATTR_DAYS));
69
QDomElement rush = aRoot.firstChildElement(TAG_RUSH);
72
d_ptr->iRushEnabled = (rush.attribute(ATTR_ENABLED) == BOOLEAN_TRUE);
73
d_ptr->iRushInterval = rush.attribute(ATTR_INTERVAL).toUInt();
74
d_ptr->iRushBegin = QTime::fromString(rush.attribute(ATTR_BEGIN), Qt::ISODate);
75
d_ptr->iRushEnd = QTime::fromString(rush.attribute(ATTR_END), Qt::ISODate);
76
d_ptr->iRushDays = d_ptr->parseDays(rush.attribute(ATTR_DAYS));
80
d_ptr->iRushEnabled = false;
81
d_ptr->iRushInterval = 0;
85
SyncSchedule::~SyncSchedule()
91
SyncSchedule& SyncSchedule::operator=(const SyncSchedule &aRhs)
96
d_ptr = new SyncSchedulePrivate(*aRhs.d_ptr);
102
QDomElement SyncSchedule::toXml(QDomDocument &aDoc) const
104
QDomElement root = aDoc.createElement(TAG_SCHEDULE);
105
root.setAttribute(ATTR_TIME, d_ptr->iTime.toString(Qt::ISODate));
106
root.setAttribute(ATTR_INTERVAL, QString::number(d_ptr->iInterval));
107
root.setAttribute(ATTR_DAYS, d_ptr->createDays(d_ptr->iDays));
109
QDomElement rush = aDoc.createElement(TAG_RUSH);
110
rush.setAttribute(ATTR_ENABLED, d_ptr->iRushEnabled ? BOOLEAN_TRUE :
112
rush.setAttribute(ATTR_INTERVAL, QString::number(d_ptr->iRushInterval));
113
rush.setAttribute(ATTR_BEGIN, d_ptr->iRushBegin.toString(Qt::ISODate));
114
rush.setAttribute(ATTR_END, d_ptr->iRushEnd.toString(Qt::ISODate));
115
rush.setAttribute(ATTR_DAYS, d_ptr->createDays(d_ptr->iRushDays));
116
root.appendChild(rush);
121
DaySet SyncSchedule::days() const
126
void SyncSchedule::setDays(const DaySet &aDays)
128
d_ptr->iDays = aDays;
131
QTime SyncSchedule::time() const
136
void SyncSchedule::setTime(const QTime &aTime)
138
d_ptr->iTime = aTime;
141
unsigned SyncSchedule::interval() const
143
return d_ptr->iInterval;
146
void SyncSchedule::setInterval(unsigned aInterval)
148
d_ptr->iInterval = aInterval;
151
bool SyncSchedule::rushEnabled() const
153
return d_ptr->iRushEnabled;
156
void SyncSchedule::setRushEnabled(bool aEnabled)
158
d_ptr->iRushEnabled = aEnabled;
161
DaySet SyncSchedule::rushDays() const
163
return d_ptr->iRushDays;
166
void SyncSchedule::setRushDays(const DaySet &aDays)
168
d_ptr->iRushDays = aDays;
171
QTime SyncSchedule::rushBegin() const
173
return d_ptr->iRushBegin;
176
QTime SyncSchedule::rushEnd() const
178
return d_ptr->iRushEnd;
181
void SyncSchedule::setRushTime(const QTime &aBegin, const QTime &aEnd)
183
d_ptr->iRushBegin = aBegin;
184
d_ptr->iRushEnd = aEnd;
187
unsigned SyncSchedule::rushInterval() const
189
return d_ptr->iRushInterval;
192
void SyncSchedule::setRushInterval(unsigned aInterval)
194
d_ptr->iRushInterval = aInterval;
197
QDateTime SyncSchedule::nextSyncTime(const QDateTime &aPrevSync,
198
const QDateTime &aCurrentTime) const
202
if (d_ptr->iTime.isValid() && !d_ptr->iDays.isEmpty())
204
// Explicit sync time defined.
205
nextSync.setTime(d_ptr->iTime);
206
nextSync.setDate(aCurrentTime.date());
207
if (aCurrentTime.time() > d_ptr->iTime)
209
nextSync = nextSync.addDays(1);
211
d_ptr->adjustDate(nextSync, d_ptr->iDays);
213
else if (d_ptr->iInterval > 0)
215
// Sync interval defined.
216
if (!aPrevSync.isValid())
218
nextSync = aCurrentTime;
222
nextSync = aPrevSync.addSecs(d_ptr->iInterval * 60);
223
if (d_ptr->adjustDate(nextSync, d_ptr->iDays))
225
nextSync.setTime(QTime(0, 0, 0, 0));
227
if (nextSync.isValid() && nextSync < aCurrentTime)
229
nextSync = aCurrentTime;
234
if (d_ptr->iRushEnabled && d_ptr->iRushInterval > 0)
236
// Calculate next sync time with rush settings.
237
QDateTime nextSyncRush;
238
if (!aPrevSync.isValid())
240
nextSyncRush = aCurrentTime;
242
else if (d_ptr->isRush(aCurrentTime))
244
nextSyncRush = aPrevSync.addSecs(d_ptr->iRushInterval * 60);
245
if (nextSyncRush < aCurrentTime)
247
nextSyncRush = aCurrentTime;
249
else if (!d_ptr->isRush(nextSyncRush))
251
nextSyncRush.setTime(d_ptr->iRushBegin);
252
if (nextSyncRush < aPrevSync)
254
nextSyncRush = nextSyncRush.addDays(1);
256
d_ptr->adjustDate(nextSyncRush, d_ptr->iRushDays);
261
nextSyncRush.setTime(d_ptr->iRushBegin);
262
nextSyncRush.setDate(aCurrentTime.date());
263
if (aCurrentTime.time() > d_ptr->iRushBegin)
265
nextSyncRush = nextSyncRush.addDays(1);
267
d_ptr->adjustDate(nextSyncRush, d_ptr->iRushDays);
270
// Use next sync time calculated with rush settings is sooner than
271
// with normal settings, use the rush sync time.
272
if (nextSyncRush.isValid() &&
273
(!nextSync.isValid() || nextSyncRush < nextSync))
275
nextSync = nextSyncRush;
282
DaySet SyncSchedulePrivate::parseDays(const QString &aDays) const
287
QStringList dayList = aDays.split(DAY_SEPARATOR,
288
QString::SkipEmptyParts);
289
foreach (QString dayStr, dayList)
292
int dayNum = dayStr.toInt(&ok);
295
daySet.insert(dayNum);
303
QString SyncSchedulePrivate::createDays(const DaySet &aDays) const
307
foreach (int dayNum, aDays)
309
dayList.append(QString::number(dayNum));
312
return dayList.join(DAY_SEPARATOR);
315
bool SyncSchedulePrivate::adjustDate(QDateTime &aTime, const DaySet &aDays) const
323
bool newValidDay = false;
324
int startDay = aTime.date().dayOfWeek();
325
while (!aDays.contains(aTime.date().dayOfWeek()))
328
aTime = aTime.addDays(1);
329
// Safety check, avoid infinite loop if date set contains
330
// only invalid values.
331
if (aTime.date().dayOfWeek() == startDay)
333
// Clear next sync time.
343
bool SyncSchedulePrivate::isRush(const QDateTime &aTime) const
345
return (iRushDays.contains(aTime.date().dayOfWeek()) &&
346
aTime.time() >= iRushBegin && aTime.time() < iRushEnd);