Updated the Tools menu to reflect current build.
[kdepim.git] / sendlateragent / sendlaterutil.cpp
blobcbe6501be777ea123297465ea50728797858c7e7
1 /*
2 Copyright (c) 2013 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
19 #include "sendlaterutil.h"
20 #include "sendlaterinfo.h"
21 #include "sendlateragentsettings.h"
23 #include <KConfigGroup>
25 #include <QDBusInterface>
26 #include <QStringList>
28 bool SendLater::SendLaterUtil::compareSendLaterInfo(SendLater::SendLaterInfo *left, SendLater::SendLaterInfo *right)
30 if (left->dateTime() == right->dateTime()) {
31 //Set no recursive first.
32 if (left->isRecurrence()) {
33 return false;
36 return left->dateTime() < right->dateTime();
39 void SendLater::SendLaterUtil::changeRecurrentDate(SendLater::SendLaterInfo *info)
41 if (info && info->isRecurrence()) {
42 //qDebug()<<" SendLater::SendLaterUtil::changeRecurrentDate "<<info->dateTime().toString();
43 switch(info->recurrenceUnit()) {
44 case SendLater::SendLaterInfo::Days:
45 info->setDateTime(info->dateTime().addDays(info->recurrenceEachValue()));
46 break;
47 case SendLater::SendLaterInfo::Weeks:
48 info->setDateTime(info->dateTime().addDays(info->recurrenceEachValue()*7));
49 break;
50 case SendLater::SendLaterInfo::Months:
51 info->setDateTime(info->dateTime().addMonths(info->recurrenceEachValue()));
52 break;
53 case SendLater::SendLaterInfo::Years:
54 info->setDateTime(info->dateTime().addYears(info->recurrenceEachValue()));
55 break;
57 //qDebug()<<"AFTER SendLater::SendLaterUtil::changeRecurrentDate "<<info->dateTime().toString();
58 writeSendLaterInfo(info, false);
62 KSharedConfig::Ptr SendLater::SendLaterUtil::defaultConfig()
64 return KSharedConfig::openConfig( QLatin1String("akonadi_sendlater_agentrc") );
67 void SendLater::SendLaterUtil::writeSendLaterInfo(SendLater::SendLaterInfo *info, bool forceReload)
69 if (!info)
70 return;
72 KSharedConfig::Ptr config = SendLaterUtil::defaultConfig();
74 const QString groupName = SendLater::SendLaterUtil::sendLaterPattern.arg(info->itemId());
75 // first, delete all filter groups:
76 const QStringList filterGroups =config->groupList().filter( groupName );
77 foreach ( const QString &group, filterGroups ) {
78 config->deleteGroup( group );
80 KConfigGroup group = config->group(groupName);
81 info->writeConfig(group);
82 config->sync();
83 config->reparseConfiguration();
84 if (forceReload)
85 reload();
88 bool SendLater::SendLaterUtil::sentLaterAgentWasRegistered()
90 QDBusInterface interface( QLatin1String("org.freedesktop.Akonadi.Agent.akonadi_sendlater_agent"), QLatin1String("/SendLaterAgent") );
91 return interface.isValid();
94 void SendLater::SendLaterUtil::forceReparseConfiguration()
96 SendLaterAgentSettings::self()->writeConfig();
97 SendLaterAgentSettings::self()->config()->reparseConfiguration();
100 bool SendLater::SendLaterUtil::sentLaterAgentEnabled()
102 return SendLaterAgentSettings::self()->enabled();
105 void SendLater::SendLaterUtil::reload()
107 QDBusInterface interface( QLatin1String("org.freedesktop.Akonadi.Agent.akonadi_sendlater_agent"), QLatin1String("/SendLaterAgent") );
108 if (interface.isValid()) {
109 interface.call(QLatin1String("reload"));