Mailcommon

foldercollectionmonitor.cpp
1 /*
2  SPDX-FileCopyrightText: 2009-2022 Laurent Montel <[email protected]>
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 
23 using namespace MailCommon;
24 
25 class MailCommon::FolderCollectionMonitorPrivate
26 {
27 public:
28  Akonadi::ChangeRecorder *mMonitor = nullptr;
29 };
30 
31 FolderCollectionMonitor::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 
52 FolderCollectionMonitor::~FolderCollectionMonitor() = default;
53 
54 Akonadi::ChangeRecorder *FolderCollectionMonitor::monitor() const
55 {
56  return d->mMonitor;
57 }
58 
59 void FolderCollectionMonitor::expireAllFolders(bool immediate, QAbstractItemModel *collectionModel)
60 {
61  if (collectionModel) {
62  expireAllCollection(collectionModel, immediate);
63  }
64 }
65 
66 void 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 
90 void 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 
103 void FolderCollectionMonitor::slotDeleteJob(KJob *job)
104 {
105  Util::showJobErrorMessage(job);
106 }
virtual int rowCount(const QModelIndex &parent) const const=0
void result(KJob *job)
virtual QVariant data(const QModelIndex &index, int role) const const=0
T value() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
static QString mimeType()
const AKONADI_MIME_EXPORT char Envelope[]
const T * attribute() const
static Collection root()
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
bool isValid() const
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Oct 1 2022 04:00:53 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.