2 Copyright (c) 2013-2015 Montel Laurent <montel@kde.org>
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License, version 2, as
6 published by the Free Software Foundation.
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 General Public License for more details.
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "sendlaterutil.h"
19 #include "sendlaterinfo.h"
20 #include "sendlateragentsettings.h"
21 #include "libsendlater_debug.h"
22 #include <KConfigGroup>
24 #include <QDBusInterface>
25 #include <QStringList>
27 bool SendLater::SendLaterUtil::compareSendLaterInfo(SendLater::SendLaterInfo
*left
, SendLater::SendLaterInfo
*right
)
29 if (left
->dateTime() == right
->dateTime()) {
30 //Set no recursive first.
31 if (left
->isRecurrence()) {
35 return left
->dateTime() < right
->dateTime();
38 void SendLater::SendLaterUtil::changeRecurrentDate(SendLater::SendLaterInfo
*info
)
40 if (info
&& info
->isRecurrence()) {
41 qCDebug(LIBSENDLATER_LOG
) << "BEFORE SendLater::SendLaterUtil::changeRecurrentDate " << info
->dateTime().toString();
42 QDateTime newInfoDateTime
= info
->dateTime();
43 newInfoDateTime
= updateRecurence(info
, newInfoDateTime
);
44 qCDebug(LIBSENDLATER_LOG
) << " QDateTime::currentDateTime()" << QDateTime::currentDateTime().toString();
45 while (newInfoDateTime
<= QDateTime::currentDateTime()) {
46 newInfoDateTime
= updateRecurence(info
, newInfoDateTime
);
48 info
->setDateTime(newInfoDateTime
);
49 qCDebug(LIBSENDLATER_LOG
) << "AFTER SendLater::SendLaterUtil::changeRecurrentDate " << info
->dateTime().toString() << " info" << info
<< "New date" << newInfoDateTime
;
50 writeSendLaterInfo(defaultConfig(), info
, true);
54 QDateTime
SendLater::SendLaterUtil::updateRecurence(SendLater::SendLaterInfo
*info
, QDateTime dateTime
)
56 switch (info
->recurrenceUnit()) {
57 case SendLater::SendLaterInfo::Days
:
58 dateTime
= dateTime
.addDays(info
->recurrenceEachValue());
60 case SendLater::SendLaterInfo::Weeks
:
61 dateTime
= dateTime
.addDays(info
->recurrenceEachValue() * 7);
63 case SendLater::SendLaterInfo::Months
:
64 dateTime
= dateTime
.addMonths(info
->recurrenceEachValue());
66 case SendLater::SendLaterInfo::Years
:
67 dateTime
= dateTime
.addYears(info
->recurrenceEachValue());
73 KSharedConfig::Ptr
SendLater::SendLaterUtil::defaultConfig()
75 return KSharedConfig::openConfig(QStringLiteral("akonadi_sendlater_agentrc"), KConfig::SimpleConfig
);
78 void SendLater::SendLaterUtil::writeSendLaterInfo(KSharedConfig::Ptr config
, SendLater::SendLaterInfo
*info
, bool forceReload
)
80 if (!info
|| !info
->isValid()) {
84 const QString groupName
= SendLater::SendLaterUtil::sendLaterPattern
.arg(info
->itemId());
85 // first, delete all filter groups:
86 const QStringList filterGroups
= config
->groupList();
87 foreach (const QString
&group
, filterGroups
) {
88 if (group
== groupName
) {
89 config
->deleteGroup(group
);
92 KConfigGroup group
= config
->group(groupName
);
93 info
->writeConfig(group
);
95 config
->reparseConfiguration();
96 qCDebug(LIBSENDLATER_LOG
) << " reparse config";
102 bool SendLater::SendLaterUtil::sentLaterAgentWasRegistered()
104 QDBusInterface
interface(QStringLiteral("org.freedesktop.Akonadi.Agent.akonadi_sendlater_agent"), QStringLiteral("/SendLaterAgent"));
105 return interface
.isValid();
108 void SendLater::SendLaterUtil::forceReparseConfiguration()
110 SendLaterAgentSettings::self()->save();
111 SendLaterAgentSettings::self()->config()->reparseConfiguration();
114 bool SendLater::SendLaterUtil::sentLaterAgentEnabled()
116 return SendLaterAgentSettings::self()->enabled();
119 void SendLater::SendLaterUtil::reload()
121 qCDebug(LIBSENDLATER_LOG
) << " void SendLater::SendLaterUtil::reload()";
122 QDBusInterface
interface(QStringLiteral("org.freedesktop.Akonadi.Agent.akonadi_sendlater_agent"), QStringLiteral("/SendLaterAgent"));
123 if (interface
.isValid()) {
124 interface
.call(QStringLiteral("reload"));
126 qCDebug(LIBSENDLATER_LOG
) << " Can not reload list";