File indexing completed on 2024-06-16 04:49:59

0001 /*
0002     SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "preprocessorbase_p.h"
0008 
0009 #include "preprocessoradaptor.h"
0010 #include "servermanager.h"
0011 #include <QDBusConnection>
0012 
0013 #include "akonadiagentbase_debug.h"
0014 #include "itemfetchjob.h"
0015 
0016 using namespace Akonadi;
0017 
0018 PreprocessorBasePrivate::PreprocessorBasePrivate(PreprocessorBase *parent)
0019     : AgentBasePrivate(parent)
0020 {
0021     Q_Q(PreprocessorBase);
0022 
0023     new Akonadi__PreprocessorAdaptor(this);
0024 
0025     if (!QDBusConnection::sessionBus().registerObject(QStringLiteral("/Preprocessor"), this, QDBusConnection::ExportAdaptors)) {
0026         Q_EMIT q->error(i18n("Unable to register object at dbus: %1", QDBusConnection::sessionBus().lastError().message()));
0027     }
0028 }
0029 
0030 void PreprocessorBasePrivate::delayedInit()
0031 {
0032     if (!QDBusConnection::sessionBus().registerService(ServerManager::agentServiceName(ServerManager::Preprocessor, mId))) {
0033         qCCritical(AKONADIAGENTBASE_LOG) << "Unable to register service at D-Bus: " << QDBusConnection::sessionBus().lastError().message();
0034     }
0035     AgentBasePrivate::delayedInit();
0036 }
0037 
0038 void PreprocessorBasePrivate::beginProcessItem(qlonglong itemId, qlonglong collectionId, const QString &mimeType)
0039 {
0040     qCDebug(AKONADIAGENTBASE_LOG) << "PreprocessorBase: about to process item " << itemId << " in collection " << collectionId << " with mimeType " << mimeType;
0041 
0042     auto fetchJob = new ItemFetchJob(Item(itemId), this);
0043     fetchJob->setFetchScope(mFetchScope);
0044     connect(fetchJob, &ItemFetchJob::result, this, &PreprocessorBasePrivate::itemFetched);
0045 }
0046 
0047 void PreprocessorBasePrivate::itemFetched(KJob *job)
0048 {
0049     Q_Q(PreprocessorBase);
0050 
0051     if (job->error()) {
0052         Q_EMIT itemProcessed(PreprocessorBase::ProcessingFailed);
0053         return;
0054     }
0055 
0056     auto fetchJob = qobject_cast<ItemFetchJob *>(job);
0057 
0058     if (fetchJob->items().isEmpty()) {
0059         Q_EMIT itemProcessed(PreprocessorBase::ProcessingFailed);
0060         return;
0061     }
0062 
0063     const Item item = fetchJob->items().at(0);
0064 
0065     switch (q->processItem(item)) {
0066     case PreprocessorBase::ProcessingFailed:
0067     case PreprocessorBase::ProcessingRefused:
0068     case PreprocessorBase::ProcessingCompleted:
0069         qCDebug(AKONADIAGENTBASE_LOG) << "PreprocessorBase: item processed, emitting signal (" << item.id() << ")";
0070 
0071         // TODO: Handle the different status codes appropriately
0072 
0073         Q_EMIT itemProcessed(item.id());
0074 
0075         qCDebug(AKONADIAGENTBASE_LOG) << "PreprocessorBase: item processed, signal emitted (" << item.id() << ")";
0076         break;
0077     case PreprocessorBase::ProcessingDelayed:
0078         qCDebug(AKONADIAGENTBASE_LOG) << "PreprocessorBase: item processing delayed (" << item.id() << ")";
0079 
0080         mInDelayedProcessing = true;
0081         mDelayedProcessingItemId = item.id();
0082         break;
0083     }
0084 }
0085 
0086 #include "moc_preprocessorbase_p.cpp"