Messagelib

searchcollectionindexingwarning.cpp
1/*
2 * SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 *
6 */
7
8#include "searchcollectionindexingwarning.h"
9#include "messagelist_debug.h"
10
11#include <Akonadi/CachePolicy>
12#include <Akonadi/CollectionFetchJob>
13#include <Akonadi/CollectionFetchScope>
14#include <Akonadi/CollectionStatistics>
15#include <Akonadi/EntityHiddenAttribute>
16#include <Akonadi/PersistentSearchAttribute>
17
18#include <PimCommon/PimUtil>
19
20#include <KLocalizedString>
21
22#if !FORCE_DISABLE_AKONADI_SEARCH
23#include <PIM/indexeditems.h>
24#endif
25
26using namespace MessageList::Core;
27
28SearchCollectionIndexingWarning::SearchCollectionIndexingWarning(QWidget *parent)
29 : KMessageWidget(parent)
30#if !FORCE_DISABLE_AKONADI_SEARCH
31 , mIndexedItems(new Akonadi::Search::PIM::IndexedItems(this))
32#endif
33{
34 setVisible(false);
35 setWordWrap(true);
36 setText(
37 i18n("Some of the search folders in this query are still being indexed "
38 "or are excluded from indexing completely. The results below may be incomplete."));
39 setCloseButtonVisible(true);
40 setMessageType(Information);
41}
42
43SearchCollectionIndexingWarning::~SearchCollectionIndexingWarning() = default;
44
45Akonadi::CollectionFetchJob *SearchCollectionIndexingWarning::fetchCollections(const Akonadi::Collection::List &cols, bool recursive)
46{
48 auto fetch = new Akonadi::CollectionFetchJob(cols, type, this);
49 fetch->fetchScope().setAncestorRetrieval(Akonadi::CollectionFetchScope::None);
50 fetch->fetchScope().setContentMimeTypes(QStringList() << Akonadi::Collection::mimeType() << QStringLiteral("message/rfc822"));
51 fetch->fetchScope().setIncludeStatistics(true);
52 return fetch;
53}
54
55void SearchCollectionIndexingWarning::setCollection(const Akonadi::Collection &collection)
56{
57 if (collection == mCollection) {
58 return;
59 }
60
62
63 mCollection = collection;
64 mCollections.clear();
65
66 // Not a search collection?
68 return;
69 }
70
71 const auto attr = collection.attribute<Akonadi::PersistentSearchAttribute>();
73 const QList<qint64> queryCols = attr->queryCollections();
74 cols.reserve(queryCols.count());
75 for (qint64 col : queryCols) {
76 cols.push_back(Akonadi::Collection(col));
77 }
78 if (cols.isEmpty()) {
79 return;
80 }
81
82 // First retrieve the top-level collections
83 Akonadi::CollectionFetchJob *fetch = fetchCollections(cols, false);
84 fetch->setProperty("recursiveQuery", attr->isRecursive());
85 connect(fetch, &Akonadi::CollectionFetchJob::finished, this, &SearchCollectionIndexingWarning::queryRootCollectionFetchFinished);
86}
87
88void SearchCollectionIndexingWarning::queryRootCollectionFetchFinished(KJob *job)
89{
90 if (job->error()) {
91 qCWarning(MESSAGELIST_LOG) << job->errorString();
92 return;
93 }
94
95 // Store the root collections
96 mCollections = qobject_cast<Akonadi::CollectionFetchJob *>(job)->collections();
97
98 if (job->property("recursiveQuery").toBool()) {
99 // Fetch all descendants, if necessary
100 Akonadi::CollectionFetchJob *fetch = fetchCollections(mCollections, true);
101 connect(fetch, &Akonadi::CollectionFetchJob::finished, this, &SearchCollectionIndexingWarning::queryCollectionFetchFinished);
102 } else {
103 queryIndexerStatus();
104 }
105}
106
107void SearchCollectionIndexingWarning::queryCollectionFetchFinished(KJob *job)
108{
109 if (job->error()) {
110 qCWarning(MESSAGELIST_LOG) << job->errorString();
111 return;
112 }
113
114 mCollections += qobject_cast<Akonadi::CollectionFetchJob *>(job)->collections();
115 queryIndexerStatus();
116}
117
118void SearchCollectionIndexingWarning::queryIndexerStatus()
119{
120#if !FORCE_DISABLE_AKONADI_SEARCH
121 bool allFullyIndexed = true;
122 for (const Akonadi::Collection &col : std::as_const(mCollections)) {
123 if (col.hasAttribute<Akonadi::EntityHiddenAttribute>()) {
124 continue;
125 }
126 if (PimCommon::Util::isImapResource(col.resource()) && !col.cachePolicy().localParts().contains(QLatin1StringView("RFC822"))) {
127 continue;
128 }
129 const qlonglong result = mIndexedItems->indexedItems(col.id());
130
131 qCDebug(MESSAGELIST_LOG) << "Collection:" << col.displayName() << "(" << col.id() << "), count:" << col.statistics().count() << ", index:" << result;
132 if (col.statistics().count() != result) {
133 allFullyIndexed = false;
134 break;
135 }
136 }
137 if (!allFullyIndexed) {
138 animatedShow();
139 }
140#endif
141}
142
143#include "moc_searchcollectionindexingwarning.cpp"
static QString mimeType()
const T * attribute() const
bool hasAttribute() const
virtual QString errorString() const
int error() const
void finished(KJob *job)
void animatedHide()
void animatedShow()
QString i18n(const char *text, const TYPE &arg...)
The implementation independent part of the MessageList library.
Definition aggregation.h:22
void clear()
qsizetype count() const const
void reserve(qsizetype size)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QVariant property(const char *name) const const
T qobject_cast(QObject *object)
bool setProperty(const char *name, QVariant &&value)
bool toBool() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.