22 #include <kdeversion.h>
23 #if defined(USE_AKONADI) && KDE_IS_VERSION(4,12,4)
27 #include <akonadi/agentinstance.h>
28 #include <akonadi/agentmanager.h>
29 #include <akonadi/collectionfetchjob.h>
30 #include <akonadi/collectionfetchscope.h>
31 #include <akonadi/itemfetchjob.h>
32 #include <akonadi/itemdeletejob.h>
34 #include <QStringList>
37 using namespace Akonadi;
49 CollectionSearch::CollectionSearch(
const QString& mimeType,
const QString& gid,
bool remove)
50 : mMimeType(mimeType),
53 mDelete(remove && !mGid.isEmpty())
55 const AgentInstance::List agents = AgentManager::self()->instances();
56 foreach (
const AgentInstance& agent, agents)
58 if (agent.type().mimeTypes().contains(mimeType))
61 CollectionFetchJob* job =
new CollectionFetchJob(Collection::root(), CollectionFetchJob::FirstLevel);
62 job->fetchScope().setResource(agent.identifier());
63 mCollectionJobs << job;
64 connect(job, SIGNAL(result(
KJob*)), SLOT(collectionFetchResult(
KJob*)));
69 if (mCollectionJobs.isEmpty())
80 void CollectionSearch::collectionFetchResult(
KJob* j)
82 CollectionFetchJob* job =
static_cast<CollectionFetchJob*
>(j);
84 kError() <<
"CollectionFetchJob" << job->fetchScope().resource() <<
"error: " << j->errorString();
87 const Collection::List collections = job->collections();
88 foreach (
const Collection& c, collections)
90 if (c.contentMimeTypes().contains(mMimeType))
99 ItemFetchJob* ijob =
new ItemFetchJob(item,
this);
100 ijob->setCollection(c);
101 mItemFetchJobs[ijob] = c.id();
102 connect(ijob, SIGNAL(result(
KJob*)), SLOT(itemFetchResult(
KJob*)));
107 mCollectionJobs.removeAll(job);
109 if (mCollectionJobs.isEmpty())
120 void CollectionSearch::itemFetchResult(
KJob* j)
122 ItemFetchJob* job =
static_cast<ItemFetchJob*
>(j);
124 kDebug() <<
"ItemFetchJob: collection" << mItemFetchJobs[job] <<
"GID" << mGid <<
"error: " << j->errorString();
129 Item::List items = job->items();
130 foreach (
const Item& item, items)
132 ItemDeleteJob* djob =
new ItemDeleteJob(item,
this);
133 mItemDeleteJobs[djob] = mItemFetchJobs[job];
134 connect(djob, SIGNAL(result(
KJob*)), SLOT(itemDeleteResult(
KJob*)));
138 mItems << job->items();
140 mItemFetchJobs.remove(job);
142 if (mItemFetchJobs.isEmpty() && mItemDeleteJobs.isEmpty() && mCollectionJobs.isEmpty())
149 void CollectionSearch::itemDeleteResult(
KJob* j)
151 ItemDeleteJob* job =
static_cast<ItemDeleteJob*
>(j);
153 kDebug() <<
"ItemDeleteJob: resource" << mItemDeleteJobs[job] <<
"GID" << mGid <<
"error: " << j->errorString();
156 mItemDeleteJobs.remove(job);
158 if (mItemFetchJobs.isEmpty() && mItemDeleteJobs.isEmpty() && mCollectionJobs.isEmpty())
165 void CollectionSearch::finish()
168 emit deleted(mDeleteCount);
169 else if (mGid.isEmpty())
170 emit collections(mCollections);
176 #include "moc_collectionsearch.cpp"