Commit 482e3e29 authored by Laurent Montel's avatar Laurent Montel 😁

Create a manager to check if it's necessary to reindex some folder

parent 6f442c4c
......@@ -153,6 +153,11 @@ set(kmailprivate_editor_plugininterface_LIB_SRCS
editor/plugininterface/kmailplugineditorcheckbeforesendmanagerinterface.cpp
)
set(kmailprivate_checkindexing_LIB_SRCS
search/checkindexingmanager.cpp
search/checkindexingjob.cpp
)
set(kmail_common_SRCS)
ecm_qt_declare_logging_category(kmail_common_SRCS HEADER kmail_debug.h IDENTIFIER KMAIL_LOG CATEGORY_NAME log_kmail)
set(kmailprivate_LIB_SRCS
......@@ -191,6 +196,7 @@ set(kmailprivate_LIB_SRCS
${kmailprivate_editor_potentialphishingemail_SRCS}
${kmailprivate_plugininterface_LIB_SRCS}
${kmailprivate_editor_plugininterface_LIB_SRCS}
${kmailprivate_checkindexing_LIB_SRCS}
)
qt5_generate_dbus_interface(editor/kmcomposerwin.h org.kde.kmail.mailcomposer.xml OPTIONS -a)
......
/*
Copyright (C) 2016 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "checkindexingjob.h"
#include "kmail_debug.h"
#include <AkonadiCore/CollectionFetchJob>
#include <AkonadiCore/CollectionFetchScope>
#include <AkonadiCore/CollectionStatistics>
#include <PimCommon/CollectionIndexStatusJob>
#include <QDBusInterface>
#include <QDebug>
CheckIndexingJob::CheckIndexingJob(QObject *parent)
: QObject(parent)
{
}
CheckIndexingJob::~CheckIndexingJob()
{
}
void CheckIndexingJob::askForNextCheck()
{
deleteLater();
Q_EMIT finished();
}
void CheckIndexingJob::setCollection(const Akonadi::Collection &col)
{
mCollection = col;
}
void CheckIndexingJob::start()
{
if (mCollection.isValid()) {
Akonadi::CollectionFetchJob *fetch = new Akonadi::CollectionFetchJob(mCollection,
Akonadi::CollectionFetchJob::Base);
fetch->fetchScope().setIncludeStatistics(true);
connect(fetch, &KJob::result, this, &CheckIndexingJob::slotCollectionPropertiesFinished);
} else {
qCWarning(KMAIL_LOG) << "Collection was not valid";
askForNextCheck();
}
}
void CheckIndexingJob::slotCollectionPropertiesFinished(KJob *job)
{
Akonadi::CollectionFetchJob *fetch = qobject_cast<Akonadi::CollectionFetchJob *>(job);
Q_ASSERT(fetch);
if (fetch->collections().isEmpty()) {
qCWarning(KMAIL_LOG) << "No collection fetched";
askForNextCheck();
return;
}
mCollection = fetch->collections().first();
PimCommon::CollectionIndexStatusJob *indexerJob = new PimCommon::CollectionIndexStatusJob(Akonadi::Collection::List() << mCollection, this);
connect(indexerJob, &PimCommon::CollectionIndexStatusJob::finished, this, &CheckIndexingJob::indexerStatsFetchFinished);
indexerJob->start();
}
void CheckIndexingJob::indexerStatsFetchFinished(KJob* job)
{
if (job->error()) {
qCWarning(KMAIL_LOG) << "CheckIndexingJob::indexerStatsFetchFinished error :" << job->errorString();
askForNextCheck();
return;
}
QMap<qint64, qint64> stats = qobject_cast<PimCommon::CollectionIndexStatusJob*>(job)->resultStats();
qDebug()<<" stats "<< stats;
if (mCollection.statistics().count() != stats.value(mCollection.id())) {
QDBusInterface interfaceBalooIndexer(QStringLiteral("org.freedesktop.Akonadi.Agent.akonadi_indexing_agent"), QStringLiteral("/"), QStringLiteral("org.freedesktop.Akonadi.Indexer"));
if (interfaceBalooIndexer.isValid()) {
qCDebug(KMAIL_LOG) << "Reindex collection :"<< mCollection.id();
interfaceBalooIndexer.call(QStringLiteral("reindexCollection"), (qlonglong)mCollection.id());
}
}
askForNextCheck();
}
/*
Copyright (C) 2016 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef CHECKINDEXINGJOB_H
#define CHECKINDEXINGJOB_H
#include <QObject>
#include <AkonadiCore/Collection>
class KJob;
class CheckIndexingJob : public QObject
{
Q_OBJECT
public:
explicit CheckIndexingJob(QObject *parent = Q_NULLPTR);
~CheckIndexingJob();
void setCollection(const Akonadi::Collection &col);
void start();
Q_SIGNALS:
void finished();
private Q_SLOTS:
void slotCollectionPropertiesFinished(KJob *job);
void indexerStatsFetchFinished(KJob *job);
private:
void askForNextCheck();
Akonadi::Collection mCollection;
};
#endif // CHECKINDEXINGJOB_H
/*
Copyright (C) 2016 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "checkindexingmanager.h"
#include "kmail_debug.h"
#include "checkindexingjob.h"
#include <AkonadiCore/EntityTreeModel>
#include <AkonadiCore/CachePolicy>
#include <MailCommon/MailUtil>
#include <PimCommon/PimUtil>
#include <QTimer>
#include <AkonadiCore/entityhiddenattribute.h>
CheckIndexingManager::CheckIndexingManager(QObject *parent)
: QObject(parent),
mIndex(0),
mIsReady(true)
{
mTimer = new QTimer(this);
mTimer->setSingleShot(true);
mTimer->setInterval(5 * 1000); //5 secondes
connect(mTimer, &QTimer::timeout, this, &CheckIndexingManager::checkNextCollection);
}
CheckIndexingManager::~CheckIndexingManager()
{
}
void CheckIndexingManager::start(QAbstractItemModel *collectionModel)
{
if(mIsReady) {
mIndex = 0;
if (collectionModel) {
initializeCollectionList(collectionModel);
if (!mListCollection.isEmpty()) {
qCDebug(KMAIL_LOG) << "Number of collection to check " << mListCollection.count();
mIsReady = false;
mTimer->start();
}
}
}
}
void CheckIndexingManager::createJob()
{
CheckIndexingJob *job = new CheckIndexingJob(this);
job->setCollection(mListCollection.at(mIndex));
connect(job, &CheckIndexingJob::finished, this, &CheckIndexingManager::slotRestartTimer);
}
void CheckIndexingManager::checkNextCollection()
{
if (mIndex < mListCollection.count()) {
createJob();
}
}
void CheckIndexingManager::slotRestartTimer()
{
mIndex++;
if (mIndex < mListCollection.count()) {
mTimer->start();
} else {
mIsReady = true;
mIndex = 0;
mListCollection.clear();
}
}
bool CheckIndexingManager::isReady() const
{
return mIsReady;
}
void CheckIndexingManager::initializeCollectionList(QAbstractItemModel *model, const QModelIndex &parentIndex)
{
mListCollection.clear();
const int rowCount = model->rowCount(parentIndex);
for (int row = 0; row < rowCount; ++row) {
const QModelIndex index = model->index(row, 0, parentIndex);
const Akonadi::Collection collection =
model->data(
index, Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
if (!collection.isValid() || MailCommon::Util::isVirtualCollection(collection)) {
continue;
}
if (collection.hasAttribute<Akonadi::EntityHiddenAttribute>()) {
continue;
}
if (PimCommon::Util::isImapResource(collection.resource()) && !collection.cachePolicy().localParts().contains(QLatin1String("RFC822"))) {
continue;
}
mListCollection.append(collection);
if (model->rowCount(index) > 0) {
initializeCollectionList(model, index);
}
}
}
/*
Copyright (C) 2016 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef CHECKINDEXINGMANAGER_H
#define CHECKINDEXINGMANAGER_H
#include <QObject>
#include <AkonadiCore/Collection>
#include <QAbstractItemModel>
class QTimer;
class CheckIndexingManager : public QObject
{
Q_OBJECT
public:
explicit CheckIndexingManager(QObject *parent = Q_NULLPTR);
~CheckIndexingManager();
void start(QAbstractItemModel *collectionModel);
bool isReady() const;
private Q_SLOTS:
void checkNextCollection();
void slotRestartTimer();
private:
void initializeCollectionList(QAbstractItemModel *model, const QModelIndex &parentIndex = QModelIndex());
void createJob();
Akonadi::Collection::List mListCollection;
QTimer *mTimer;
int mIndex;
bool mIsReady;
};
#endif // CHECKINDEXINGMANAGER_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment