File indexing completed on 2024-12-22 05:01:13

0001 /*
0002    SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "checkindexingjob.h"
0008 #include "kmail_debug.h"
0009 #include <PIM/indexeditems.h>
0010 
0011 #include <Akonadi/CollectionFetchJob>
0012 #include <Akonadi/CollectionFetchScope>
0013 #include <Akonadi/CollectionStatistics>
0014 
0015 #include <PimCommon/PimUtil>
0016 
0017 CheckIndexingJob::CheckIndexingJob(Akonadi::Search::PIM::IndexedItems *indexedItems, QObject *parent)
0018     : QObject(parent)
0019     , mIndexedItems(indexedItems)
0020 {
0021 }
0022 
0023 CheckIndexingJob::~CheckIndexingJob() = default;
0024 
0025 void CheckIndexingJob::askForNextCheck(quint64 id, bool needToReindex)
0026 {
0027     Q_EMIT finished(id, needToReindex);
0028     deleteLater();
0029 }
0030 
0031 void CheckIndexingJob::setCollection(const Akonadi::Collection &col)
0032 {
0033     mCollection = col;
0034 }
0035 
0036 void CheckIndexingJob::start()
0037 {
0038     if (mCollection.isValid()) {
0039         auto fetch = new Akonadi::CollectionFetchJob(mCollection, Akonadi::CollectionFetchJob::Base);
0040         fetch->fetchScope().setIncludeStatistics(true);
0041         connect(fetch, &KJob::result, this, &CheckIndexingJob::slotCollectionPropertiesFinished);
0042     } else {
0043         qCWarning(KMAIL_LOG) << "Collection was not valid";
0044         askForNextCheck(-1);
0045     }
0046 }
0047 
0048 void CheckIndexingJob::slotCollectionPropertiesFinished(KJob *job)
0049 {
0050     auto fetch = qobject_cast<Akonadi::CollectionFetchJob *>(job);
0051     Q_ASSERT(fetch);
0052     if (fetch->collections().isEmpty()) {
0053         qCWarning(KMAIL_LOG) << "No collection fetched";
0054         askForNextCheck(-1);
0055         return;
0056     }
0057 
0058     mCollection = fetch->collections().constFirst();
0059     const qlonglong result = mIndexedItems->indexedItems(mCollection.id());
0060     bool needToReindex = false;
0061     qCDebug(KMAIL_LOG) << "name :" << mCollection.name() << " mCollection.statistics().count() " << mCollection.statistics().count()
0062                        << "stats.value(mCollection.id())" << result;
0063     if (mCollection.statistics().count() != result) {
0064         needToReindex = true;
0065         qCDebug(KMAIL_LOG) << "Reindex collection :"
0066                            << "name :" << mCollection.name();
0067     }
0068     askForNextCheck(mCollection.id(), needToReindex);
0069 }
0070 
0071 #include "moc_checkindexingjob.cpp"