diff --git a/agents/followupreminderagent/followupreminderagent.cpp b/agents/followupreminderagent/followupreminderagent.cpp index 43c75942b..73b1d904c 100644 --- a/agents/followupreminderagent/followupreminderagent.cpp +++ b/agents/followupreminderagent/followupreminderagent.cpp @@ -1,123 +1,138 @@ /* Copyright (C) 2014-2020 Laurent Montel This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "followupreminderagent.h" #include "followupremindermanager.h" #include #include "followupreminderadaptor.h" #include "followupreminderagentsettings.h" +#include + #include #include #include #include #include #include #include #include #include "followupreminderagent_debug.h" #include FollowUpReminderAgent::FollowUpReminderAgent(const QString &id) : Akonadi::AgentBase(id) { Kdelibs4ConfigMigrator migrate(QStringLiteral("followupreminderagent")); migrate.setConfigFiles(QStringList() << QStringLiteral("akonadi_followupreminder_agentrc") << QStringLiteral("akonadi_followupreminder_agent.notifyrc")); migrate.migrate(); new FollowUpReminderAgentAdaptor(this); QDBusConnection::sessionBus().registerObject(QStringLiteral("/FollowUpReminder"), this, QDBusConnection::ExportAdaptors); const QString service = Akonadi::ServerManager::self()->agentServiceName(Akonadi::ServerManager::Agent, QStringLiteral("akonadi_followupreminder_agent")); QDBusConnection::sessionBus().registerService(service); mManager = new FollowUpReminderManager(this); setNeedsNetwork(true); changeRecorder()->setMimeTypeMonitored(KMime::Message::mimeType()); changeRecorder()->itemFetchScope().setCacheOnly(true); changeRecorder()->itemFetchScope().setFetchModificationTime(false); changeRecorder()->fetchCollection(true); changeRecorder()->setChangeRecordingEnabled(false); changeRecorder()->ignoreSession(Akonadi::Session::defaultSession()); changeRecorder()->collectionFetchScope().setAncestorRetrieval(Akonadi::CollectionFetchScope::All); changeRecorder()->setCollectionMonitored(Akonadi::Collection::root(), true); if (FollowUpReminderAgentSettings::enabled()) { mManager->load(); } mTimer = new QTimer(this); connect(mTimer, &QTimer::timeout, this, &FollowUpReminderAgent::reload); //Reload all each 24hours mTimer->start(24 * 60 * 60 * 1000); } FollowUpReminderAgent::~FollowUpReminderAgent() { } void FollowUpReminderAgent::setEnableAgent(bool enabled) { if (FollowUpReminderAgentSettings::self()->enabled() == enabled) { return; } FollowUpReminderAgentSettings::self()->setEnabled(enabled); FollowUpReminderAgentSettings::self()->save(); if (enabled) { mManager->load(); mTimer->start(); } else { mTimer->stop(); } } bool FollowUpReminderAgent::enabledAgent() const { return FollowUpReminderAgentSettings::self()->enabled(); } void FollowUpReminderAgent::itemAdded(const Akonadi::Item &item, const Akonadi::Collection &collection) { if (!enabledAgent()) { return; } if (item.mimeType() != KMime::Message::mimeType()) { qCDebug(FOLLOWUPREMINDERAGENT_LOG) << "FollowUpReminderAgent::itemAdded called for a non-message item!"; return; } mManager->checkFollowUp(item, collection); } void FollowUpReminderAgent::reload() { if (enabledAgent()) { mManager->load(true); mTimer->start(); } } +void FollowUpReminderAgent::addReminder(const QString &messageId, Akonadi::Item::Id messageItemId, const QString &to, const QString &subject, const QDate &followupDate, Akonadi::Item::Id todoId) +{ + auto info = new FollowUpReminder::FollowUpReminderInfo(); + info->setMessageId(messageId); + info->setOriginalMessageItemId(messageItemId); + info->setTo(to); + info->setSubject(subject); + info->setFollowUpReminderDate(followupDate); + info->setTodoId(todoId); + + mManager->addReminder(info); +} + QString FollowUpReminderAgent::printDebugInfo() const { return mManager->printDebugInfo(); } AKONADI_AGENT_MAIN(FollowUpReminderAgent) diff --git a/agents/followupreminderagent/followupreminderagent.h b/agents/followupreminderagent/followupreminderagent.h index df0704b92..b7d37865d 100644 --- a/agents/followupreminderagent/followupreminderagent.h +++ b/agents/followupreminderagent/followupreminderagent.h @@ -1,48 +1,49 @@ /* Copyright (C) 2014-2020 Laurent Montel This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef FOLLOWUPREMINDERAGENT_H #define FOLLOWUPREMINDERAGENT_H #include class FollowUpReminderManager; class FollowUpReminderAgent : public Akonadi::AgentBase, public Akonadi::AgentBase::ObserverV3 { Q_OBJECT public: explicit FollowUpReminderAgent(const QString &id); ~FollowUpReminderAgent() override; void setEnableAgent(bool b); Q_REQUIRED_RESULT bool enabledAgent() const; Q_REQUIRED_RESULT QString printDebugInfo() const; public Q_SLOTS: void reload(); + void addReminder(const QString &messageId, Akonadi::Item::Id messageItemId, const QString &to, const QString &subject, const QDate &followupDate, Akonadi::Item::Id todoId); protected: void itemAdded(const Akonadi::Item &item, const Akonadi::Collection &collection) override; private: FollowUpReminderManager *mManager = nullptr; QTimer *mTimer = nullptr; }; #endif // FOLLOWUPREMINDERAGENT_H diff --git a/agents/followupreminderagent/followupremindermanager.cpp b/agents/followupreminderagent/followupremindermanager.cpp index 7c7bad8e5..8974e387e 100644 --- a/agents/followupreminderagent/followupremindermanager.cpp +++ b/agents/followupreminderagent/followupremindermanager.cpp @@ -1,191 +1,200 @@ /* Copyright (C) 2014-2020 Laurent Montel This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "followupremindermanager.h" #include "followupreminderagent_debug.h" #include #include #include "followupremindernoanswerdialog.h" #include "jobs/followupreminderjob.h" #include "jobs/followupreminderfinishtaskjob.h" #include #include #include #include #include #include #include using namespace FollowUpReminder; FollowUpReminderManager::FollowUpReminderManager(QObject *parent) : QObject(parent) { mConfig = KSharedConfig::openConfig(); } FollowUpReminderManager::~FollowUpReminderManager() { qDeleteAll(mFollowUpReminderInfoList); mFollowUpReminderInfoList.clear(); } void FollowUpReminderManager::load(bool forceReloadConfig) { if (forceReloadConfig) { mConfig->reparseConfiguration(); } const QStringList itemList = mConfig->groupList().filter(QRegularExpression(QStringLiteral("FollowupReminderItem \\d+"))); const int numberOfItems = itemList.count(); QList noAnswerList; for (int i = 0; i < numberOfItems; ++i) { KConfigGroup group = mConfig->group(itemList.at(i)); FollowUpReminderInfo *info = new FollowUpReminderInfo(group); if (info->isValid()) { if (!info->answerWasReceived()) { mFollowUpReminderInfoList.append(info); if (!mInitialize) { FollowUpReminderInfo *noAnswerInfo = new FollowUpReminderInfo(*info); noAnswerList.append(noAnswerInfo); } else { delete info; } } else { delete info; } } else { delete info; } } if (!noAnswerList.isEmpty()) { mInitialize = true; if (!mNoAnswerDialog.data()) { mNoAnswerDialog = new FollowUpReminderNoAnswerDialog; connect(mNoAnswerDialog.data(), &FollowUpReminderNoAnswerDialog::needToReparseConfiguration, this, &FollowUpReminderManager::slotReparseConfiguration); } mNoAnswerDialog->setInfo(noAnswerList); mNoAnswerDialog->wakeUp(); } } +void FollowUpReminderManager::addReminder(FollowUpReminder::FollowUpReminderInfo *info) +{ + if (info->isValid()) { + FollowUpReminderUtil::writeFollowupReminderInfo(FollowUpReminderUtil::defaultConfig(), info, true); + } else { + delete info; + } +} + void FollowUpReminderManager::slotReparseConfiguration() { load(true); } void FollowUpReminderManager::checkFollowUp(const Akonadi::Item &item, const Akonadi::Collection &col) { if (mFollowUpReminderInfoList.isEmpty()) { return; } const Akonadi::SpecialMailCollections::Type type = Akonadi::SpecialMailCollections::self()->specialCollectionType(col); switch (type) { case Akonadi::SpecialMailCollections::Trash: case Akonadi::SpecialMailCollections::Outbox: case Akonadi::SpecialMailCollections::Drafts: case Akonadi::SpecialMailCollections::Templates: case Akonadi::SpecialMailCollections::SentMail: return; default: break; } FollowUpReminderJob *job = new FollowUpReminderJob(this); connect(job, &FollowUpReminderJob::finished, this, &FollowUpReminderManager::slotCheckFollowUpFinished); job->setItem(item); job->start(); } void FollowUpReminderManager::slotCheckFollowUpFinished(const QString &messageId, Akonadi::Item::Id id) { for (FollowUpReminderInfo *info : qAsConst(mFollowUpReminderInfoList)) { qCDebug(FOLLOWUPREMINDERAGENT_LOG) << "FollowUpReminderManager::slotCheckFollowUpFinished info:" << info; if (!info) { continue; } if (info->messageId() == messageId) { info->setAnswerMessageItemId(id); info->setAnswerWasReceived(true); answerReceived(info->to()); if (info->todoId() != -1) { FollowUpReminderFinishTaskJob *job = new FollowUpReminderFinishTaskJob(info->todoId(), this); connect(job, &FollowUpReminderFinishTaskJob::finishTaskDone, this, &FollowUpReminderManager::slotFinishTaskDone); connect(job, &FollowUpReminderFinishTaskJob::finishTaskFailed, this, &FollowUpReminderManager::slotFinishTaskFailed); job->start(); } //Save item FollowUpReminder::FollowUpReminderUtil::writeFollowupReminderInfo(FollowUpReminder::FollowUpReminderUtil::defaultConfig(), info, true); break; } } } void FollowUpReminderManager::slotFinishTaskDone() { qCDebug(FOLLOWUPREMINDERAGENT_LOG) << " Task Done"; //TODO } void FollowUpReminderManager::slotFinishTaskFailed() { qCDebug(FOLLOWUPREMINDERAGENT_LOG) << " Task Failed"; //TODO } void FollowUpReminderManager::answerReceived(const QString &from) { KNotification::event(QStringLiteral("mailreceived"), QString(), i18n("Answer from %1 received", from), QStringLiteral("kmail"), nullptr, KNotification::CloseOnTimeout, QStringLiteral("akonadi_followupreminder_agent")); } QString FollowUpReminderManager::printDebugInfo() const { QString infoStr; if (mFollowUpReminderInfoList.isEmpty()) { infoStr = QStringLiteral("No mail"); } else { for (FollowUpReminder::FollowUpReminderInfo *info : qAsConst(mFollowUpReminderInfoList)) { if (!infoStr.isEmpty()) { infoStr += QLatin1Char('\n'); } infoStr += infoToStr(info); } } return infoStr; } QString FollowUpReminderManager::infoToStr(FollowUpReminder::FollowUpReminderInfo *info) const { QString infoStr = QStringLiteral("****************************************"); infoStr += QStringLiteral("Akonadi Item id :%1\n").arg(info->originalMessageItemId()); infoStr += QStringLiteral("MessageId :%1\n").arg(info->messageId()); infoStr += QStringLiteral("Subject :%1\n").arg(info->subject()); infoStr += QStringLiteral("To :%1\n").arg(info->to()); infoStr += QStringLiteral("Dead Line :%1\n").arg(info->followUpReminderDate().toString()); infoStr += QStringLiteral("Answer received :%1\n").arg(info->answerWasReceived() ? QStringLiteral("true") : QStringLiteral("false")); infoStr += QStringLiteral("****************************************\n"); return infoStr; } diff --git a/agents/followupreminderagent/followupremindermanager.h b/agents/followupreminderagent/followupremindermanager.h index 934f6505b..a847e2a3d 100644 --- a/agents/followupreminderagent/followupremindermanager.h +++ b/agents/followupreminderagent/followupremindermanager.h @@ -1,58 +1,59 @@ /* Copyright (C) 2014-2020 Laurent Montel This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef FOLLOWUPREMINDERMANAGER_H #define FOLLOWUPREMINDERMANAGER_H #include #include #include #include namespace FollowUpReminder { class FollowUpReminderInfo; } class FollowUpReminderNoAnswerDialog; class FollowUpReminderManager : public QObject { Q_OBJECT public: explicit FollowUpReminderManager(QObject *parent = nullptr); ~FollowUpReminderManager(); void load(bool forceReloadConfig = false); + void addReminder(FollowUpReminder::FollowUpReminderInfo *reminder); // takes ownership void checkFollowUp(const Akonadi::Item &item, const Akonadi::Collection &col); Q_REQUIRED_RESULT QString printDebugInfo() const; private: Q_DISABLE_COPY(FollowUpReminderManager) void slotCheckFollowUpFinished(const QString &messageId, Akonadi::Item::Id id); void slotFinishTaskDone(); void slotFinishTaskFailed(); void slotReparseConfiguration(); void answerReceived(const QString &from); Q_REQUIRED_RESULT QString infoToStr(FollowUpReminder::FollowUpReminderInfo *info) const; KSharedConfig::Ptr mConfig; QList mFollowUpReminderInfoList; QPointer mNoAnswerDialog; bool mInitialize = false; }; #endif // FOLLOWUPREMINDERMANAGER_H diff --git a/agents/followupreminderagent/org.freedesktop.Akonadi.FollowUpReminder.xml b/agents/followupreminderagent/org.freedesktop.Akonadi.FollowUpReminder.xml index 435279db7..0fde523c1 100644 --- a/agents/followupreminderagent/org.freedesktop.Akonadi.FollowUpReminder.xml +++ b/agents/followupreminderagent/org.freedesktop.Akonadi.FollowUpReminder.xml @@ -1,15 +1,24 @@ + + + + + + + + +