File indexing completed on 2024-12-22 05:00:50
0001 /* 0002 SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "archivemailagent.h" 0008 #include "archivemailagentadaptor.h" 0009 #include "archivemailagentsettings.h" 0010 #include "archivemailmanager.h" 0011 #include <Akonadi/ServerManager> 0012 0013 #include <Akonadi/CollectionFetchScope> 0014 #include <Akonadi/Monitor> 0015 #include <Akonadi/Session> 0016 #include <KMime/Message> 0017 #include <MailCommon/MailKernel> 0018 #include <QDBusConnection> 0019 #include <QTimer> 0020 #include <chrono> 0021 using namespace std::chrono_literals; 0022 // #define DEBUG_ARCHIVEMAILAGENT 1 0023 0024 ArchiveMailAgent::ArchiveMailAgent(const QString &id) 0025 : Akonadi::AgentBase(id) 0026 , mTimer(new QTimer(this)) 0027 , mArchiveManager(new ArchiveMailManager(this)) 0028 { 0029 connect(this, &Akonadi::AgentBase::reloadConfiguration, this, &ArchiveMailAgent::reload); 0030 0031 connect(mArchiveManager, &ArchiveMailManager::needUpdateConfigDialogBox, this, &ArchiveMailAgent::needUpdateConfigDialogBox); 0032 0033 auto collectionMonitor = new Akonadi::Monitor(this); 0034 collectionMonitor->setObjectName(QLatin1StringView("ArchiveMailCollectionMonitor")); 0035 collectionMonitor->fetchCollection(true); 0036 collectionMonitor->ignoreSession(Akonadi::Session::defaultSession()); 0037 collectionMonitor->collectionFetchScope().setAncestorRetrieval(Akonadi::CollectionFetchScope::All); 0038 collectionMonitor->setMimeTypeMonitored(KMime::Message::mimeType()); 0039 0040 new ArchiveMailAgentAdaptor(this); 0041 QDBusConnection::sessionBus().registerObject(QStringLiteral("/ArchiveMailAgent"), this, QDBusConnection::ExportAdaptors); 0042 0043 const QString service = Akonadi::ServerManager::self()->agentServiceName(Akonadi::ServerManager::Agent, identifier()); 0044 0045 QDBusConnection::sessionBus().registerService(service); 0046 connect(collectionMonitor, &Akonadi::Monitor::collectionRemoved, this, &ArchiveMailAgent::mailCollectionRemoved); 0047 0048 if (enabledAgent()) { 0049 #ifdef DEBUG_ARCHIVEMAILAGENT 0050 QTimer::singleShot(1s, mArchiveManager, &ArchiveMailManager::load); 0051 #else 0052 QTimer::singleShot(5min, mArchiveManager, &ArchiveMailManager::load); 0053 #endif 0054 } 0055 0056 connect(mTimer, &QTimer::timeout, this, &ArchiveMailAgent::reload); 0057 // Now we have range support we need to reload each hour. 0058 mTimer->start(1h); 0059 } 0060 0061 ArchiveMailAgent::~ArchiveMailAgent() = default; 0062 0063 void ArchiveMailAgent::setEnableAgent(bool enabled) 0064 { 0065 if (enabled != ArchiveMailAgentSettings::enabled()) { 0066 ArchiveMailAgentSettings::setEnabled(enabled); 0067 ArchiveMailAgentSettings::self()->save(); 0068 if (!enabled) { 0069 mTimer->stop(); 0070 pause(); 0071 } else { 0072 mTimer->start(); 0073 } 0074 } 0075 } 0076 0077 bool ArchiveMailAgent::enabledAgent() const 0078 { 0079 return ArchiveMailAgentSettings::enabled(); 0080 } 0081 0082 void ArchiveMailAgent::mailCollectionRemoved(const Akonadi::Collection &collection) 0083 { 0084 mArchiveManager->removeCollection(collection); 0085 } 0086 0087 void ArchiveMailAgent::doSetOnline(bool online) 0088 { 0089 if (online) { 0090 resume(); 0091 } else { 0092 pause(); 0093 } 0094 } 0095 0096 void ArchiveMailAgent::reload() 0097 { 0098 if (isOnline() && enabledAgent()) { 0099 mArchiveManager->load(); 0100 mTimer->start(); 0101 } 0102 } 0103 0104 void ArchiveMailAgent::pause() 0105 { 0106 if (isOnline() && enabledAgent()) { 0107 mArchiveManager->pause(); 0108 } 0109 } 0110 0111 void ArchiveMailAgent::resume() 0112 { 0113 if (isOnline() && enabledAgent()) { 0114 mArchiveManager->resume(); 0115 } 0116 } 0117 0118 QString ArchiveMailAgent::printArchiveListInfo() const 0119 { 0120 return mArchiveManager->printArchiveListInfo(); 0121 } 0122 0123 QString ArchiveMailAgent::printCurrentListInfo() const 0124 { 0125 return mArchiveManager->printCurrentListInfo(); 0126 } 0127 0128 void ArchiveMailAgent::archiveFolder(const QString &path, Akonadi::Collection::Id collectionId) 0129 { 0130 mArchiveManager->archiveFolder(path, collectionId); 0131 } 0132 0133 AKONADI_AGENT_MAIN(ArchiveMailAgent) 0134 0135 #include "moc_archivemailagent.cpp"