2 Copyright (C) 2013-2016 Montel Laurent <montel@kde.org>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "sendlateragent.h"
21 #include "sendlatermanager.h"
22 #include "sendlaterconfiguredialog.h"
23 #include "sendlaterinfo.h"
24 #include "sendlaterutil.h"
25 #include "sendlateragentadaptor.h"
26 #include "sendlateragentsettings.h"
27 #include "sendlaterremovemessagejob.h"
28 #include "sendlateragent_debug.h"
29 #include <Akonadi/KMime/SpecialMailCollections>
30 #include <AgentInstance>
31 #include <AgentManager>
32 #include <kdbusconnectionpool.h>
33 #include <changerecorder.h>
34 #include <itemfetchscope.h>
35 #include <AkonadiCore/session.h>
36 #include <AttributeFactory>
37 #include <CollectionFetchScope>
38 #include <KMime/Message>
40 #include <KWindowSystem>
41 #include <Kdelibs4ConfigMigrator>
45 //#define DEBUG_SENDLATERAGENT 1
47 SendLaterAgent::SendLaterAgent(const QString
&id
)
48 : Akonadi::AgentBase(id
),
49 mAgentInitialized(false)
51 Kdelibs4ConfigMigrator
migrate(QStringLiteral("sendlateragent"));
52 migrate
.setConfigFiles(QStringList() << QStringLiteral("akonadi_sendlater_agentrc") << QStringLiteral("akonadi_sendlater_agent.notifyrc"));
55 mManager
= new SendLaterManager(this);
56 connect(mManager
, &SendLaterManager::needUpdateConfigDialogBox
, this, &SendLaterAgent::needUpdateConfigDialogBox
);
57 new SendLaterAgentAdaptor(this);
58 KDBusConnectionPool::threadConnection().registerObject(QStringLiteral("/SendLaterAgent"), this, QDBusConnection::ExportAdaptors
);
59 KDBusConnectionPool::threadConnection().registerService(QStringLiteral("org.freedesktop.Akonadi.SendLaterAgent"));
61 changeRecorder()->setMimeTypeMonitored(KMime::Message::mimeType());
62 changeRecorder()->itemFetchScope().setCacheOnly(true);
63 changeRecorder()->itemFetchScope().setFetchModificationTime(false);
64 changeRecorder()->setChangeRecordingEnabled(false);
65 changeRecorder()->ignoreSession(Akonadi::Session::defaultSession());
66 setNeedsNetwork(true);
68 if (SendLaterAgentSettings::enabled()) {
69 #ifdef DEBUG_SENDLATERAGENT
70 QTimer::singleShot(1000, this, &SendLaterAgent::slotStartAgent
);
72 QTimer::singleShot(1000 * 60 * 4, this, &SendLaterAgent::slotStartAgent
);
75 // For extra safety, check list every hour, in case we didn't properly get
76 // notified about the network going up or down.
77 QTimer
*reloadListTimer
= new QTimer(this);
78 connect(reloadListTimer
, &QTimer::timeout
, this, &SendLaterAgent::reload
);
79 reloadListTimer
->start(1000 * 60 * 60); //1 hour
82 SendLaterAgent::~SendLaterAgent()
86 void SendLaterAgent::slotStartAgent()
88 mAgentInitialized
= true;
94 void SendLaterAgent::doSetOnline(bool online
)
96 if (mAgentInitialized
) {
105 void SendLaterAgent::reload()
107 qCDebug(SENDLATERAGENT_LOG
) << " void SendLaterAgent::reload()";
108 if (SendLaterAgentSettings::enabled()) {
109 mManager
->load(true);
113 void SendLaterAgent::setEnableAgent(bool enabled
)
115 if (SendLaterAgentSettings::enabled() == enabled
) {
119 SendLaterAgentSettings::setEnabled(enabled
);
120 SendLaterAgentSettings::self()->save();
128 bool SendLaterAgent::enabledAgent() const
130 return SendLaterAgentSettings::enabled();
133 void SendLaterAgent::configure(WId windowId
)
135 showConfigureDialog((qlonglong
)windowId
);
138 void SendLaterAgent::slotSendNow(Akonadi::Item::Id id
)
140 mManager
->sendNow(id
);
143 void SendLaterAgent::showConfigureDialog(qlonglong windowId
)
145 QPointer
<SendLaterConfigureDialog
> dialog
= new SendLaterConfigureDialog();
148 KWindowSystem::setMainWindow(dialog
, windowId
);
150 KWindowSystem::setMainWindow(dialog
, (HWND
)windowId
);
153 connect(this, &SendLaterAgent::needUpdateConfigDialogBox
, dialog
.data(), &SendLaterConfigureDialog::slotNeedToReloadConfig
);
154 connect(dialog
.data(), &SendLaterConfigureDialog::sendNow
, this, &SendLaterAgent::slotSendNow
);
155 if (dialog
->exec()) {
157 QList
<Akonadi::Item::Id
> listMessage
= dialog
->messagesToRemove();
158 if (!listMessage
.isEmpty()) {
159 //Will delete in specific job when done.
160 SendLaterRemoveMessageJob
*sendlaterremovejob
= new SendLaterRemoveMessageJob(listMessage
, this);
161 sendlaterremovejob
->start();
167 void SendLaterAgent::itemsRemoved(const Akonadi::Item::List
&items
)
169 Q_FOREACH (const Akonadi::Item
&item
, items
) {
170 mManager
->itemRemoved(item
.id());
174 void SendLaterAgent::itemsMoved(const Akonadi::Item::List
&items
, const Akonadi::Collection
&/*sourceCollection*/, const Akonadi::Collection
&destinationCollection
)
176 if (Akonadi::SpecialMailCollections::self()->specialCollectionType(destinationCollection
) != Akonadi::SpecialMailCollections::Trash
) {
182 QString
SendLaterAgent::printDebugInfo()
184 return mManager
->printDebugInfo();
187 AKONADI_AGENT_MAIN(SendLaterAgent
)