Akonadi

collectionmaintenancepage.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 "collectionmaintenancepage.h"
8 #include "agentmanager.h"
9 #include "akonadiwidgets_debug.h"
10 #include "cachepolicy.h"
11 #include "core/collectionstatistics.h"
12 #include "indexpolicyattribute.h"
13 #include "monitor.h"
14 #include "servermanager.h"
15 #include "ui_collectionmaintenancepage.h"
16 
17 #include <QDBusInterface>
18 #include <QDBusPendingCallWatcher>
19 #include <QDBusPendingReply>
20 
21 #include <KIO/Global>
22 
23 #include <KLocalizedString>
24 #include <QCheckBox>
25 #include <QPushButton>
26 
27 using namespace Akonadi;
28 
29 class Akonadi::CollectionMaintenancePagePrivate
30 {
31 public:
32  CollectionMaintenancePagePrivate()
33  {
34  }
35 
36  void slotReindexCollection()
37  {
38  if (currentCollection.isValid()) {
39  // Don't allow to reindex twice.
40  ui.reindexButton->setEnabled(false);
41 
42  const auto service = ServerManager::agentServiceName(ServerManager::Agent, QStringLiteral("akonadi_indexing_agent"));
43  QDBusInterface indexingAgentIface(service, QStringLiteral("/"), QStringLiteral("org.freedesktop.Akonadi.Indexer"));
44  if (indexingAgentIface.isValid()) {
45  indexingAgentIface.call(QStringLiteral("reindexCollection"), static_cast<qlonglong>(currentCollection.id()));
46  ui.indexedCountLbl->setText(i18n("Remember that indexing can take some minutes."));
47  } else {
48  qCWarning(AKONADIWIDGETS_LOG) << "indexer interface not valid";
49  }
50  }
51  }
52 
53  void updateLabel(qint64 nbMail, qint64 nbUnreadMail, qint64 size)
54  {
55  ui.itemsCountLbl->setText(QString::number(qMax(0LL, nbMail)));
56  ui.unreadItemsCountLbl->setText(QString::number(qMax(0LL, nbUnreadMail)));
57  ui.folderSizeLbl->setText(KIO::convertSize(qMax(0LL, size)));
58  }
59 
60  Akonadi::Collection currentCollection;
61  Akonadi::Monitor *monitor = nullptr;
62 
63  Ui::CollectionMaintenancePage ui;
64 };
65 
66 CollectionMaintenancePage::CollectionMaintenancePage(QWidget *parent)
67  : CollectionPropertiesPage(parent)
68  , d(new CollectionMaintenancePagePrivate)
69 {
70  setObjectName(QStringLiteral("Akonadi::CollectionMaintenancePage"));
71  setPageTitle(i18n("Maintenance"));
72 }
73 
74 CollectionMaintenancePage::~CollectionMaintenancePage() = default;
75 
76 void CollectionMaintenancePage::init(const Collection &col)
77 {
78  d->ui.setupUi(this);
79 
80  d->currentCollection = col;
81  d->monitor = new Monitor(this);
82  d->monitor->setObjectName(QStringLiteral("CollectionMaintenancePageMonitor"));
83  d->monitor->setCollectionMonitored(col, true);
84  d->monitor->fetchCollectionStatistics(true);
85  connect(d->monitor, &Monitor::collectionStatisticsChanged, this, [this](Collection::Id /*unused*/, const CollectionStatistics &stats) {
86  d->updateLabel(stats.count(), stats.unreadCount(), stats.size());
87  });
88 
89  if (!col.isVirtual()) {
90  const AgentInstance instance = Akonadi::AgentManager::self()->instance(col.resource());
91  d->ui.folderTypeLbl->setText(instance.type().name());
92  } else {
93  d->ui.folderTypeLbl->hide();
94  d->ui.verticalLayout->labelForField(d->ui.folderTypeLbl)->hide();
95  }
96 
97  connect(d->ui.reindexButton, &QPushButton::clicked, this, [this]() {
98  d->slotReindexCollection();
99  });
100 
101  // Check if the resource caches full payloads or at least has local storage
102  // (so that the indexer can retrieve the payloads on demand)
103  const auto resource = Akonadi::AgentManager::self()->instance(col.resource()).type();
104  if (!col.cachePolicy().localParts().contains(QLatin1String("RFC822"))
105  && resource.customProperties().value(QStringLiteral("HasLocalStorage"), QString()) != QLatin1String("true")) {
106  d->ui.indexingLabel->hide();
107  d->ui.enableIndexingChkBox->hide();
108  d->ui.indexedCountLbl->hide();
109  d->ui.reindexButton->hide();
110  }
111 }
112 
113 void CollectionMaintenancePage::load(const Collection &col)
114 {
115  init(col);
116  if (col.isValid()) {
117  d->updateLabel(col.statistics().count(), col.statistics().unreadCount(), col.statistics().size());
118  const auto attr = col.attribute<Akonadi::IndexPolicyAttribute>();
119  const bool indexingWasEnabled(!attr || attr->indexingEnabled());
120  d->ui.enableIndexingChkBox->setChecked(indexingWasEnabled);
121  if (indexingWasEnabled) {
122  const auto service = ServerManager::agentServiceName(ServerManager::Agent, QStringLiteral("akonadi_indexing_agent"));
123  QDBusInterface indexingAgentIface(service, QStringLiteral("/"), QStringLiteral("org.freedesktop.Akonadi.Indexer"));
124  if (indexingAgentIface.isValid()) {
125  auto reply = indexingAgentIface.asyncCall(QStringLiteral("indexedItems"), static_cast<qint64>(col.id()));
126  auto w = new QDBusPendingCallWatcher(reply, this);
127  connect(w, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *w) {
128  QDBusPendingReply<qint64> reply = *w;
129  if (reply.isError()) {
130  d->ui.indexedCountLbl->setText(i18n("Error while retrieving indexed items count"));
131  qCWarning(AKONADIWIDGETS_LOG) << "Failed to retrieve indexed items count:" << reply.error().message();
132  } else {
133  d->ui.indexedCountLbl->setText(i18np("Indexed %1 item in this folder", "Indexed %1 items in this folder", reply.argumentAt<0>()));
134  }
135  w->deleteLater();
136  });
137  d->ui.indexedCountLbl->setText(i18n("Calculating indexed items..."));
138  } else {
139  qCDebug(AKONADIWIDGETS_LOG) << "Failed to obtain Indexer interface";
140  d->ui.indexedCountLbl->hide();
141  }
142  } else {
143  d->ui.indexedCountLbl->hide();
144  }
145  }
146 }
147 
148 void CollectionMaintenancePage::save(Collection &collection)
149 {
150  if (!collection.hasAttribute<Akonadi::IndexPolicyAttribute>() && d->ui.enableIndexingChkBox->isChecked()) {
151  return;
152  }
153 
155  attr->setIndexingEnabled(d->ui.enableIndexingChkBox->isChecked());
156 }
Provides statistics information of a Collection.
void finished(QDBusPendingCallWatcher *self)
KIOCORE_EXPORT QString convertSize(KIO::filesize_t size)
QString number(int n, int base)
AgentType type() const
Returns the agent type of this instance.
QCA_EXPORT void init()
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
void clicked(bool checked)
Represents a collection of PIM items.
Definition: collection.h:61
bool isError() const const
qint64 size() const
Returns the total size of the items in this collection or -1 if this information is not available.
An attribute to specify how a collection should be indexed for searching.
Monitors an item or collection for changes.
Definition: monitor.h:71
static QString agentServiceName(ServiceAgentType agentType, const QString &identifier)
Returns the namespaced D-Bus service name for an agent of type agentType with agent identifier identi...
qint64 count() const
Returns the number of items in this collection or -1 if this information is not available.
Attribute * attribute(const QByteArray &name)
Returns the attribute of the given type name if available, 0 otherwise.
Definition: collection.cpp:176
AgentInstance instance(const QString &identifier) const
Returns the agent instance with the given identifier or an invalid agent instance if the identifier d...
void deleteLater()
QString i18n(const char *text, const TYPE &arg...)
CachePolicy cachePolicy() const
Returns the cache policy of the collection.
Definition: collection.cpp:336
qint64 unreadCount() const
Returns the number of unread items in this collection or -1 if this information is not available.
bool hasAttribute(const QByteArray &name) const
Returns true if the collection has an attribute of the given type name, false otherwise.
Definition: collection.cpp:161
QStringList localParts() const
Returns the parts to permanently cache locally.
Definition: cachepolicy.cpp:65
void setIndexingEnabled(bool enable)
Sets whether this collection should be indexed at all.
CollectionStatistics statistics() const
Returns the collection statistics of the collection.
Definition: collection.cpp:326
void collectionStatisticsChanged(Akonadi::Collection::Id id, const Akonadi::CollectionStatistics &statistics)
This signal is emitted if the statistics information of a monitored collection has changed.
static AgentManager * self()
Returns the global instance of the agent manager.
@ AddIfMissing
Creates the attribute if it is missing.
Definition: collection.h:281
A single page in a collection properties dialog.
A representation of an agent instance.
qint64 Id
Describes the unique id type.
Definition: collection.h:79
Helper integration between Akonadi and Qt.
QString name() const
Returns the i18n'ed name of the agent type.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jul 2 2022 06:41:47 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.