Debug-- Remove includes
[kdepim.git] / kmail / agents / followupreminderagent / followupremindermanager.cpp
blobdde860a62a8832923e1fda2c78bce309b65c31ce
1 /*
2 Copyright (C) 2014-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 "followupremindermanager.h"
21 #include "followupreminderagent_debug.h"
22 #include "FollowupReminder/FollowUpReminderInfo"
23 #include "FollowupReminder/FollowUpReminderUtil"
24 #include "followupremindernoanswerdialog.h"
25 #include "jobs/followupreminderjob.h"
26 #include "jobs/followupreminderfinishtaskjob.h"
27 #include <Akonadi/KMime/SpecialMailCollections>
29 #include <QIcon>
31 #include <KConfigGroup>
32 #include <KConfig>
33 #include <KSharedConfig>
34 #include <knotification.h>
35 #include <KLocalizedString>
36 #include <KIconLoader>
37 #include <QRegularExpression>
38 using namespace FollowUpReminder;
40 FollowUpReminderManager::FollowUpReminderManager(QObject *parent)
41 : QObject(parent),
42 mInitialize(false)
44 mConfig = KSharedConfig::openConfig();
47 FollowUpReminderManager::~FollowUpReminderManager()
49 qDeleteAll(mFollowUpReminderInfoList);
50 mFollowUpReminderInfoList.clear();
53 void FollowUpReminderManager::load(bool forceReloadConfig)
55 if (forceReloadConfig) {
56 mConfig->reparseConfiguration();
58 const QStringList itemList = mConfig->groupList().filter(QRegularExpression(QStringLiteral("FollowupReminderItem \\d+")));
59 const int numberOfItems = itemList.count();
60 QList<FollowUpReminder::FollowUpReminderInfo *> noAnswerList;
61 for (int i = 0; i < numberOfItems; ++i) {
63 KConfigGroup group = mConfig->group(itemList.at(i));
65 FollowUpReminderInfo *info = new FollowUpReminderInfo(group);
66 if (info->isValid()) {
67 if (!info->answerWasReceived()) {
68 mFollowUpReminderInfoList.append(info);
69 if (!mInitialize) {
70 FollowUpReminderInfo *noAnswerInfo = new FollowUpReminderInfo(*info);
71 noAnswerList.append(noAnswerInfo);
72 } else {
73 delete info;
75 } else {
76 delete info;
78 } else {
79 delete info;
82 if (!noAnswerList.isEmpty()) {
83 mInitialize = true;
84 if (!mNoAnswerDialog.data()) {
85 mNoAnswerDialog = new FollowUpReminderNoAnswerDialog;
86 connect(mNoAnswerDialog.data(), &FollowUpReminderNoAnswerDialog::needToReparseConfiguration, this, &FollowUpReminderManager::slotReparseConfiguration);
88 mNoAnswerDialog->setInfo(noAnswerList);
89 mNoAnswerDialog->show();
93 void FollowUpReminderManager::slotReparseConfiguration()
95 load(true);
98 void FollowUpReminderManager::checkFollowUp(const Akonadi::Item &item, const Akonadi::Collection &col)
100 if (mFollowUpReminderInfoList.isEmpty()) {
101 return;
104 const Akonadi::SpecialMailCollections::Type type = Akonadi::SpecialMailCollections::self()->specialCollectionType(col);
105 switch (type) {
106 case Akonadi::SpecialMailCollections::Trash:
107 case Akonadi::SpecialMailCollections::Outbox:
108 case Akonadi::SpecialMailCollections::Drafts:
109 case Akonadi::SpecialMailCollections::Templates:
110 case Akonadi::SpecialMailCollections::SentMail:
111 return;
112 default:
113 break;
116 FollowUpReminderJob *job = new FollowUpReminderJob(this);
117 connect(job, &FollowUpReminderJob::finished, this, &FollowUpReminderManager::slotCheckFollowUpFinished);
118 job->setItem(item);
119 job->start();
122 void FollowUpReminderManager::slotCheckFollowUpFinished(const QString &messageId, Akonadi::Item::Id id)
124 Q_FOREACH (FollowUpReminderInfo *info, mFollowUpReminderInfoList) {
125 qCDebug(FOLLOWUPREMINDERAGENT_LOG) << "FollowUpReminderManager::slotCheckFollowUpFinished info:" << info;
126 if (!info) {
127 continue;
129 if (info->messageId() == messageId) {
130 info->setAnswerMessageItemId(id);
131 info->setAnswerWasReceived(true);
132 answerReceived(info->to());
133 if (info->todoId() != -1) {
134 FollowUpReminderFinishTaskJob *job = new FollowUpReminderFinishTaskJob(info->todoId(), this);
135 connect(job, &FollowUpReminderFinishTaskJob::finishTaskDone, this, &FollowUpReminderManager::slotFinishTaskDone);
136 connect(job, &FollowUpReminderFinishTaskJob::finishTaskFailed, this, &FollowUpReminderManager::slotFinishTaskFailed);
137 job->start();
139 //Save item
140 FollowUpReminder::FollowUpReminderUtil::writeFollowupReminderInfo(FollowUpReminder::FollowUpReminderUtil::defaultConfig(), info, true);
141 break;
146 void FollowUpReminderManager::slotFinishTaskDone()
148 //TODO
151 void FollowUpReminderManager::slotFinishTaskFailed()
153 //TODO
156 void FollowUpReminderManager::answerReceived(const QString &from)
158 const QPixmap pixmap = QIcon::fromTheme(QStringLiteral("kmail")).pixmap(KIconLoader::SizeSmall, KIconLoader::SizeSmall);
159 KNotification::event(QStringLiteral("mailreceived"),
160 i18n("Answer from %1 received", from),
161 pixmap,
162 Q_NULLPTR,
163 KNotification::CloseOnTimeout,
164 QStringLiteral("akonadi_followupreminder_agent"));
168 QString FollowUpReminderManager::printDebugInfo()
170 QString infoStr;
171 if (mFollowUpReminderInfoList.isEmpty()) {
172 infoStr = QStringLiteral("No mail");
173 } else {
174 Q_FOREACH (FollowUpReminder::FollowUpReminderInfo *info, mFollowUpReminderInfoList) {
175 if (!infoStr.isEmpty()) {
176 infoStr += QLatin1Char('\n');
178 infoStr += infoToStr(info);
181 return infoStr;
184 QString FollowUpReminderManager::infoToStr(FollowUpReminder::FollowUpReminderInfo *info)
186 QString infoStr;
187 infoStr = QStringLiteral("****************************************");
188 infoStr += QStringLiteral("Akonadi Item id :%1\n").arg(info->originalMessageItemId());
189 infoStr += QStringLiteral("MessageId :%1\n").arg(info->messageId());
190 infoStr += QStringLiteral("Subject :%1\n").arg(info->subject());
191 infoStr += QStringLiteral("To :%1\n").arg(info->to());
192 infoStr += QStringLiteral("Dead Line :%1\n").arg(info->followUpReminderDate().toString());
193 infoStr += QStringLiteral("Answer received :%1\n").arg(info->answerWasReceived() ? QStringLiteral("true") : QStringLiteral("false"));
194 infoStr += QStringLiteral("****************************************\n");
195 return infoStr;