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>
31 #include <KConfigGroup>
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
)
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
);
70 FollowUpReminderInfo
*noAnswerInfo
= new FollowUpReminderInfo(*info
);
71 noAnswerList
.append(noAnswerInfo
);
82 if (!noAnswerList
.isEmpty()) {
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()
98 void FollowUpReminderManager::checkFollowUp(const Akonadi::Item
&item
, const Akonadi::Collection
&col
)
100 if (mFollowUpReminderInfoList
.isEmpty()) {
104 const Akonadi::SpecialMailCollections::Type type
= Akonadi::SpecialMailCollections::self()->specialCollectionType(col
);
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
:
116 FollowUpReminderJob
*job
= new FollowUpReminderJob(this);
117 connect(job
, &FollowUpReminderJob::finished
, this, &FollowUpReminderManager::slotCheckFollowUpFinished
);
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
;
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
);
140 FollowUpReminder::FollowUpReminderUtil::writeFollowupReminderInfo(FollowUpReminder::FollowUpReminderUtil::defaultConfig(), info
, true);
146 void FollowUpReminderManager::slotFinishTaskDone()
151 void FollowUpReminderManager::slotFinishTaskFailed()
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
),
163 KNotification::CloseOnTimeout
,
164 QStringLiteral("akonadi_followupreminder_agent"));
168 QString
FollowUpReminderManager::printDebugInfo()
171 if (mFollowUpReminderInfoList
.isEmpty()) {
172 infoStr
= QStringLiteral("No mail");
174 Q_FOREACH (FollowUpReminder::FollowUpReminderInfo
*info
, mFollowUpReminderInfoList
) {
175 if (!infoStr
.isEmpty()) {
176 infoStr
+= QLatin1Char('\n');
178 infoStr
+= infoToStr(info
);
184 QString
FollowUpReminderManager::infoToStr(FollowUpReminder::FollowUpReminderInfo
*info
)
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");