Warning, file /pim/mailcommon/src/folder/foldercollectionmonitor.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002   SPDX-FileCopyrightText: 2009-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "foldercollectionmonitor.h"
0008 #include "collectionpage/attributes/expirecollectionattribute.h"
0009 #include "foldersettings.h"
0010 #include "mailcommon_debug.h"
0011 #include "util/mailutil.h"
0012 #include <Akonadi/ChangeRecorder>
0013 #include <Akonadi/Collection>
0014 #include <Akonadi/CollectionFetchScope>
0015 #include <Akonadi/EntityAnnotationsAttribute>
0016 #include <Akonadi/EntityTreeModel>
0017 #include <Akonadi/Item>
0018 #include <Akonadi/ItemDeleteJob>
0019 #include <Akonadi/ItemFetchJob>
0020 #include <Akonadi/ItemFetchScope>
0021 #include <Akonadi/MessageParts>
0022 
0023 using namespace MailCommon;
0024 
0025 class MailCommon::FolderCollectionMonitorPrivate
0026 {
0027 public:
0028     Akonadi::ChangeRecorder *mMonitor = nullptr;
0029 };
0030 
0031 FolderCollectionMonitor::FolderCollectionMonitor(Akonadi::Session *session, QObject *parent)
0032     : QObject(parent)
0033     , d(new MailCommon::FolderCollectionMonitorPrivate)
0034 {
0035     // monitor collection changes
0036     d->mMonitor = new Akonadi::ChangeRecorder(this);
0037     d->mMonitor->setSession(session);
0038     d->mMonitor->setCollectionMonitored(Akonadi::Collection::root());
0039     d->mMonitor->fetchCollectionStatistics(true);
0040     d->mMonitor->collectionFetchScope().setIncludeStatistics(true);
0041     d->mMonitor->fetchCollection(true);
0042     d->mMonitor->setAllMonitored(true);
0043     d->mMonitor->setMimeTypeMonitored(KMime::Message::mimeType());
0044     d->mMonitor->setResourceMonitored("akonadi_search_resource", true);
0045     d->mMonitor->itemFetchScope().fetchPayloadPart(Akonadi::MessagePart::Envelope);
0046     d->mMonitor->itemFetchScope().setFetchModificationTime(false);
0047     d->mMonitor->itemFetchScope().setFetchRemoteIdentification(false);
0048     d->mMonitor->itemFetchScope().setFetchTags(true);
0049     d->mMonitor->itemFetchScope().fetchAttribute<Akonadi::EntityAnnotationsAttribute>(true);
0050 }
0051 
0052 FolderCollectionMonitor::~FolderCollectionMonitor() = default;
0053 
0054 Akonadi::ChangeRecorder *FolderCollectionMonitor::monitor() const
0055 {
0056     return d->mMonitor;
0057 }
0058 
0059 void FolderCollectionMonitor::expireAllFolders(bool immediate, QAbstractItemModel *collectionModel)
0060 {
0061     if (collectionModel) {
0062         expireAllCollection(collectionModel, immediate);
0063     }
0064 }
0065 
0066 void FolderCollectionMonitor::expireAllCollection(const QAbstractItemModel *model, bool immediate, const QModelIndex &parentIndex)
0067 {
0068     const int rowCount = model->rowCount(parentIndex);
0069     for (int row = 0; row < rowCount; ++row) {
0070         const QModelIndex index = model->index(row, 0, parentIndex);
0071         const auto collection = model->data(index, Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
0072 
0073         if (!collection.isValid() || Util::isVirtualCollection(collection)) {
0074             continue;
0075         }
0076 
0077         const auto attr = collection.attribute<MailCommon::ExpireCollectionAttribute>();
0078         if (attr) {
0079             if (attr->isAutoExpire()) {
0080                 MailCommon::Util::expireOldMessages(collection, immediate);
0081             }
0082         }
0083 
0084         if (model->rowCount(index) > 0) {
0085             expireAllCollection(model, immediate, index);
0086         }
0087     }
0088 }
0089 
0090 void FolderCollectionMonitor::expunge(const Akonadi::Collection &col, bool sync)
0091 {
0092     if (col.isValid()) {
0093         auto job = new Akonadi::ItemDeleteJob(col, this);
0094         connect(job, &Akonadi::ItemDeleteJob::result, this, &FolderCollectionMonitor::slotDeleteJob);
0095         if (sync) {
0096             job->exec();
0097         }
0098     } else {
0099         qCDebug(MAILCOMMON_LOG) << " Try to expunge an invalid collection :" << col;
0100     }
0101 }
0102 
0103 void FolderCollectionMonitor::slotDeleteJob(KJob *job)
0104 {
0105     Util::showJobErrorMessage(job);
0106 }
0107 
0108 #include "moc_foldercollectionmonitor.cpp"