Mailcommon

foldercollectionmonitor.cpp
1/*
2 SPDX-FileCopyrightText: 2009-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "foldercollectionmonitor.h"
8#include "collectionpage/attributes/expirecollectionattribute.h"
9#include "foldersettings.h"
10#include "mailcommon_debug.h"
11#include "util/mailutil.h"
12#include <Akonadi/ChangeRecorder>
13#include <Akonadi/Collection>
14#include <Akonadi/CollectionFetchScope>
15#include <Akonadi/EntityAnnotationsAttribute>
16#include <Akonadi/EntityTreeModel>
17#include <Akonadi/Item>
18#include <Akonadi/ItemDeleteJob>
19#include <Akonadi/ItemFetchJob>
20#include <Akonadi/ItemFetchScope>
21#include <Akonadi/MessageParts>
22
23using namespace MailCommon;
24
25class MailCommon::FolderCollectionMonitorPrivate
26{
27public:
28 Akonadi::ChangeRecorder *mMonitor = nullptr;
29};
30
31FolderCollectionMonitor::FolderCollectionMonitor(Akonadi::Session *session, QObject *parent)
32 : QObject(parent)
33 , d(new MailCommon::FolderCollectionMonitorPrivate)
34{
35 // monitor collection changes
36 d->mMonitor = new Akonadi::ChangeRecorder(this);
37 d->mMonitor->setSession(session);
38 d->mMonitor->setCollectionMonitored(Akonadi::Collection::root());
39 d->mMonitor->fetchCollectionStatistics(true);
40 d->mMonitor->collectionFetchScope().setIncludeStatistics(true);
41 d->mMonitor->fetchCollection(true);
42 d->mMonitor->setAllMonitored(true);
43 d->mMonitor->setMimeTypeMonitored(KMime::Message::mimeType());
44 d->mMonitor->setResourceMonitored("akonadi_search_resource", true);
45 d->mMonitor->itemFetchScope().fetchPayloadPart(Akonadi::MessagePart::Envelope);
46 d->mMonitor->itemFetchScope().setFetchModificationTime(false);
47 d->mMonitor->itemFetchScope().setFetchRemoteIdentification(false);
48 d->mMonitor->itemFetchScope().setFetchTags(true);
49 d->mMonitor->itemFetchScope().fetchAttribute<Akonadi::EntityAnnotationsAttribute>(true);
50}
51
52FolderCollectionMonitor::~FolderCollectionMonitor() = default;
53
54Akonadi::ChangeRecorder *FolderCollectionMonitor::monitor() const
55{
56 return d->mMonitor;
57}
58
59void FolderCollectionMonitor::expireAllFolders(bool immediate, QAbstractItemModel *collectionModel)
60{
61 if (collectionModel) {
62 expireAllCollection(collectionModel, immediate);
63 }
64}
65
66void FolderCollectionMonitor::expireAllCollection(const QAbstractItemModel *model, bool immediate, const QModelIndex &parentIndex)
67{
68 const int rowCount = model->rowCount(parentIndex);
69 for (int row = 0; row < rowCount; ++row) {
70 const QModelIndex index = model->index(row, 0, parentIndex);
71 const auto collection = model->data(index, Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
72
73 if (!collection.isValid() || Util::isVirtualCollection(collection)) {
74 continue;
75 }
76
77 const auto attr = collection.attribute<MailCommon::ExpireCollectionAttribute>();
78 if (attr) {
79 if (attr->isAutoExpire()) {
80 MailCommon::Util::expireOldMessages(collection, immediate);
81 }
82 }
83
84 if (model->rowCount(index) > 0) {
85 expireAllCollection(model, immediate, index);
86 }
87 }
88}
89
90void FolderCollectionMonitor::expunge(const Akonadi::Collection &col, bool sync)
91{
92 if (col.isValid()) {
93 auto job = new Akonadi::ItemDeleteJob(col, this);
94 connect(job, &Akonadi::ItemDeleteJob::result, this, &FolderCollectionMonitor::slotDeleteJob);
95 if (sync) {
96 job->exec();
97 }
98 } else {
99 qCDebug(MAILCOMMON_LOG) << " Try to expunge an invalid collection :" << col;
100 }
101}
102
103void FolderCollectionMonitor::slotDeleteJob(KJob *job)
104{
105 Util::showJobErrorMessage(job);
106}
107
108#include "moc_foldercollectionmonitor.cpp"
bool isValid() const
static Collection root()
const T * attribute() const
void result(KJob *job)
static QString mimeType()
AKONADI_MIME_EXPORT const char Envelope[]
The filter dialog.
virtual QVariant data(const QModelIndex &index, int role) const const=0
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual int rowCount(const QModelIndex &parent) const const=0
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:58:43 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.