File indexing completed on 2024-12-22 05:00:54

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "mailmergeagent.h"
0008 #include "mailmergeagent_debug.h"
0009 #include "mailmergeagentadaptor.h"
0010 #include "mailmergeagentsettings.h"
0011 #include "mailmergeconfiguredialog.h"
0012 #include "mailmergemanager.h"
0013 #include <Akonadi/AgentInstance>
0014 #include <Akonadi/AgentManager>
0015 #include <Akonadi/AttributeFactory>
0016 #include <Akonadi/ChangeRecorder>
0017 #include <Akonadi/CollectionFetchScope>
0018 #include <Akonadi/ItemFetchScope>
0019 #include <Akonadi/ServerManager>
0020 #include <Akonadi/Session>
0021 #include <Akonadi/SpecialMailCollections>
0022 #include <KMime/Message>
0023 #include <QDBusConnection>
0024 
0025 #include <KWindowSystem>
0026 
0027 #include <QPointer>
0028 #include <QTimer>
0029 #include <chrono>
0030 
0031 using namespace std::chrono_literals;
0032 
0033 // #define DEBUG_MAILMERGEAGENT 1
0034 
0035 MailMergeAgent::MailMergeAgent(const QString &id)
0036     : Akonadi::AgentBase(id)
0037     , mManager(new MailMergeManager(this))
0038 {
0039     connect(mManager, &MailMergeManager::needUpdateConfigDialogBox, this, &MailMergeAgent::needUpdateConfigDialogBox);
0040     new MailMergeAgentAdaptor(this);
0041     QDBusConnection::sessionBus().registerObject(QStringLiteral("/MailMergeAgent"), this, QDBusConnection::ExportAdaptors);
0042 
0043     const QString service = Akonadi::ServerManager::self()->agentServiceName(Akonadi::ServerManager::Agent, QStringLiteral("akonadi_mergemail_agent"));
0044 
0045     QDBusConnection::sessionBus().registerService(service);
0046 
0047     changeRecorder()->setMimeTypeMonitored(KMime::Message::mimeType());
0048     changeRecorder()->itemFetchScope().setCacheOnly(true);
0049     changeRecorder()->itemFetchScope().setFetchModificationTime(false);
0050     changeRecorder()->setChangeRecordingEnabled(false);
0051     changeRecorder()->ignoreSession(Akonadi::Session::defaultSession());
0052     setNeedsNetwork(true);
0053 
0054     if (MailMergeAgentSettings::enabled()) {
0055 #ifdef DEBUG_MailMergeAgent
0056         QTimer::singleShot(1s, this, &MailMergeAgent::slotStartAgent);
0057 #else
0058         QTimer::singleShot(4min, this, &MailMergeAgent::slotStartAgent);
0059 #endif
0060     }
0061     // For extra safety, check list every hour, in case we didn't properly get
0062     // notified about the network going up or down.
0063     auto reloadListTimer = new QTimer(this);
0064     connect(reloadListTimer, &QTimer::timeout, this, &MailMergeAgent::reload);
0065     reloadListTimer->start(1h); // 1 hour
0066 }
0067 
0068 MailMergeAgent::~MailMergeAgent() = default;
0069 
0070 void MailMergeAgent::slotStartAgent()
0071 {
0072     mAgentInitialized = true;
0073     if (isOnline()) {
0074         mManager->load();
0075     }
0076 }
0077 
0078 void MailMergeAgent::doSetOnline(bool online)
0079 {
0080     if (mAgentInitialized) {
0081         if (online) {
0082             reload();
0083         } else {
0084             mManager->stopAll();
0085         }
0086     }
0087 }
0088 
0089 void MailMergeAgent::reload()
0090 {
0091     qCDebug(MAILMERGEAGENT_LOG) << " void MailMergeAgent::reload()";
0092     if (MailMergeAgentSettings::enabled()) {
0093         mManager->load(true);
0094     }
0095 }
0096 
0097 void MailMergeAgent::setEnableAgent(bool enabled)
0098 {
0099     if (MailMergeAgentSettings::enabled() == enabled) {
0100         return;
0101     }
0102 
0103     MailMergeAgentSettings::setEnabled(enabled);
0104     MailMergeAgentSettings::self()->save();
0105     if (enabled) {
0106         mManager->load();
0107     } else {
0108         mManager->stopAll();
0109     }
0110 }
0111 
0112 bool MailMergeAgent::enabledAgent() const
0113 {
0114     return MailMergeAgentSettings::enabled();
0115 }
0116 
0117 void MailMergeAgent::configure(WId windowId)
0118 {
0119     QPointer<MailMergeConfigureDialog> dialog = new MailMergeConfigureDialog();
0120     if (windowId) {
0121         dialog->setAttribute(Qt::WA_NativeWindow, true);
0122         KWindowSystem::setMainWindow(dialog->windowHandle(), windowId);
0123     }
0124     //    connect(this, &MailMergeAgent::needUpdateConfigDialogBox, dialog.data(), &MailMergeConfigureDialog::slotNeedToReloadConfig);
0125     //    connect(dialog.data(), &SendLaterConfigureDialog::sendNow, this, &MailMergeAgent::slotSendNow);
0126     if (dialog->exec()) {
0127         // TODO
0128         //        mManager->load();
0129         //        const QList<Akonadi::Item::Id> listMessage = dialog->messagesToRemove();
0130         //        if (!listMessage.isEmpty()) {
0131         //            // Will delete in specific job when done.
0132         //            auto sendlaterremovejob = new SendLaterRemoveMessageJob(listMessage, this);
0133         //            sendlaterremovejob->start();
0134         //        }
0135     }
0136     delete dialog;
0137 }
0138 
0139 void MailMergeAgent::removeItem(qint64 item)
0140 {
0141     if (mManager->itemRemoved(item)) {
0142         reload();
0143     }
0144 }
0145 
0146 void MailMergeAgent::addItem(qint64 timestamp,
0147                              bool recurrence,
0148                              int recurrenceValue,
0149                              int recurrenceUnit,
0150                              Akonadi::Item::Id id,
0151                              const QString &subject,
0152                              const QString &to)
0153 {
0154     //    auto info = new MessageComposer::SendLaterInfo;
0155     //    info->setDateTime(QDateTime::fromSecsSinceEpoch(timestamp));
0156     //    info->setRecurrence(recurrence);
0157     //    info->setRecurrenceEachValue(recurrenceValue);
0158     //    info->setRecurrenceUnit(static_cast<MessageComposer::SendLaterInfo::RecurrenceUnit>(recurrenceUnit));
0159     //    info->setItemId(id);
0160     //    info->setSubject(subject);
0161     //    info->setTo(to);
0162 
0163     //    SendLaterUtil::writeSendLaterInfo(SendLaterUtil::defaultConfig(), info);
0164     //    reload();
0165 }
0166 
0167 void MailMergeAgent::itemsRemoved(const Akonadi::Item::List &items)
0168 {
0169     bool needToReload = false;
0170     for (const Akonadi::Item &item : items) {
0171         if (mManager->itemRemoved(item.id())) {
0172             needToReload = true;
0173         }
0174     }
0175     if (needToReload) {
0176         reload();
0177     }
0178 }
0179 
0180 void MailMergeAgent::itemsMoved(const Akonadi::Item::List &items,
0181                                 const Akonadi::Collection & /*sourceCollection*/,
0182                                 const Akonadi::Collection &destinationCollection)
0183 {
0184     if (Akonadi::SpecialMailCollections::self()->specialCollectionType(destinationCollection) != Akonadi::SpecialMailCollections::Trash) {
0185         return;
0186     }
0187     itemsRemoved(items);
0188 }
0189 
0190 QString MailMergeAgent::printDebugInfo() const
0191 {
0192     return mManager->printDebugInfo();
0193 }
0194 
0195 AKONADI_AGENT_MAIN(MailMergeAgent)
0196 
0197 #include "moc_mailmergeagent.cpp"