1/*
2 Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#include "preprocessorbase_p.h"
21#include "preprocessorbase.h"
22
23#include "dbusconnectionpool.h"
24#include "preprocessoradaptor.h"
25#include "servermanager.h"
26
27#include <akonadi/itemfetchjob.h>
28
29using namespace Akonadi;
30
31PreprocessorBasePrivate::PreprocessorBasePrivate(PreprocessorBase *parent)
32 : AgentBasePrivate(parent)
33 , mInDelayedProcessing(false)
34 , mDelayedProcessingItemId(0)
35{
36 Q_Q(PreprocessorBase);
37
38 new Akonadi__PreprocessorAdaptor(this);
39
40 if (!DBusConnectionPool::threadConnection().registerObject(QLatin1String("/Preprocessor"), this, QDBusConnection::ExportAdaptors)) {
41 q->error(i18n("Unable to register object at dbus: %1", DBusConnectionPool::threadConnection().lastError().message()));
42 }
43
44}
45
46void PreprocessorBasePrivate::delayedInit()
47{
48 if (!DBusConnectionPool::threadConnection().registerService(ServerManager::agentServiceName(ServerManager::Preprocessor, mId))) {
49 kFatal() << "Unable to register service at D-Bus: " << DBusConnectionPool::threadConnection().lastError().message();
50 }
51 AgentBasePrivate::delayedInit();
52}
53
54void PreprocessorBasePrivate::beginProcessItem(qlonglong itemId, qlonglong collectionId, const QString &mimeType)
55{
56 kDebug() << "PreprocessorBase: about to process item " << itemId << " in collection " << collectionId << " with mimeType " << mimeType;
57
58 ItemFetchJob *fetchJob = new ItemFetchJob(Item(itemId), this);
59 fetchJob->setFetchScope(mFetchScope);
60 connect(fetchJob, SIGNAL(result(KJob*)), SLOT(itemFetched(KJob*)));
61}
62
63void PreprocessorBasePrivate::itemFetched(KJob *job)
64{
65 Q_Q(PreprocessorBase);
66
67 if (job->error()) {
68 emit itemProcessed(PreprocessorBase::ProcessingFailed);
69 return;
70 }
71
72 ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob *>(job);
73
74 if (fetchJob->items().isEmpty()) {
75 emit itemProcessed(PreprocessorBase::ProcessingFailed);
76 return;
77 }
78
79 const Item item = fetchJob->items().first();
80
81 switch (q->processItem(item)) {
82 case PreprocessorBase::ProcessingFailed:
83 case PreprocessorBase::ProcessingRefused:
84 case PreprocessorBase::ProcessingCompleted:
85 kDebug() << "PreprocessorBase: item processed, emitting signal (" << item.id() << ")";
86
87 // TODO: Handle the different status codes appropriately
88
89 emit itemProcessed(item.id());
90
91 kDebug() << "PreprocessorBase: item processed, signal emitted (" << item.id() << ")";
92 break;
93 case PreprocessorBase::ProcessingDelayed:
94 kDebug() << "PreprocessorBase: item processing delayed (" << item.id() << ")";
95
96 mInDelayedProcessing = true;
97 mDelayedProcessingItemId = item.id();
98 break;
99 }
100}
101