SVN_SILENT made messages (.desktop file) - always resolve ours
[kdepim.git] / kalarm / collectionsearch.cpp
blob4fca0776ff72a1105b36ebe6c624e6c8abfa6e3e
1 /*
2 * collectionsearch.cpp - Search Akonadi Collections
3 * Program: kalarm
4 * Copyright © 2014 by David Jarvie <djarvie@kde.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <kdeversion.h>
23 #include "collectionsearch.h"
25 #include <AkonadiCore/agentinstance.h>
26 #include <AkonadiCore/agentmanager.h>
27 #include <AkonadiCore/collectionfetchjob.h>
28 #include <AkonadiCore/collectionfetchscope.h>
29 #include <AkonadiCore/itemfetchjob.h>
30 #include <AkonadiCore/itemdeletejob.h>
32 #include <QTimer>
33 #include "kalarm_debug.h"
35 using namespace Akonadi;
37 /******************************************************************************
38 * Constructor.
39 * Creates jobs to fetch all collections for resources containing the mime type.
40 * Its subsequent actions depend on the parameters:
41 * - If 'remove' is true, it will locate all Items with the specified 'gid' and
42 * delete them. The deleted() signal will be emitted.
43 * - Otherwise, if 'gid' is specified, it will Q_EMIT the signal items() to
44 * notify all Items with that GID.
45 * - Otherwise, it will Q_EMIT the signal collections() to notify all Collections.
47 CollectionSearch::CollectionSearch(const QString& mimeType, const QString& gid, bool remove)
48 : mMimeType(mimeType),
49 mGid(gid),
50 mDeleteCount(0),
51 mDelete(remove && !mGid.isEmpty())
53 const AgentInstance::List agents = AgentManager::self()->instances();
54 foreach (const AgentInstance& agent, agents)
56 if (agent.type().mimeTypes().contains(mimeType))
59 CollectionFetchJob* job = new CollectionFetchJob(Collection::root(), CollectionFetchJob::FirstLevel);
60 job->fetchScope().setResource(agent.identifier());
61 mCollectionJobs << job;
62 connect(job, &CollectionFetchJob::result, this, &CollectionSearch::collectionFetchResult);
67 if (mCollectionJobs.isEmpty())
69 // There are no resources containing the mime type, so ensure that a
70 // signal is emitted after construction.
71 QTimer::singleShot(0, this, &CollectionSearch::finish);
75 /******************************************************************************
76 * Called when a CollectionFetchJob has completed.
78 void CollectionSearch::collectionFetchResult(KJob* j)
80 CollectionFetchJob* job = static_cast<CollectionFetchJob*>(j);
81 if (j->error())
82 qCCritical(KALARM_LOG) << "CollectionFetchJob" << job->fetchScope().resource() << "error: " << j->errorString();
83 else
85 const Collection::List collections = job->collections();
86 foreach (const Collection& c, collections)
88 if (c.contentMimeTypes().contains(mMimeType))
90 if (mGid.isEmpty())
91 mCollections << c;
92 else
94 // Search for all Items with the specified GID
95 Item item;
96 item.setGid(mGid);
97 ItemFetchJob* ijob = new ItemFetchJob(item, this);
98 ijob->setCollection(c);
99 mItemFetchJobs[ijob] = c.id();
100 connect(ijob, &ItemFetchJob::result, this, &CollectionSearch::itemFetchResult);
105 mCollectionJobs.removeAll(job);
107 if (mCollectionJobs.isEmpty())
109 // All collections have now been fetched
110 if (mGid.isEmpty())
111 finish();
115 /******************************************************************************
116 * Called when an ItemFetchJob has completed.
118 void CollectionSearch::itemFetchResult(KJob* j)
120 ItemFetchJob* job = static_cast<ItemFetchJob*>(j);
121 if (j->error())
122 qCDebug(KALARM_LOG) << "ItemFetchJob: collection" << mItemFetchJobs[job] << "GID" << mGid << "error: " << j->errorString();
123 else
125 if (mDelete)
127 Item::List items = job->items();
128 foreach (const Item& item, items)
130 ItemDeleteJob* djob = new ItemDeleteJob(item, this);
131 mItemDeleteJobs[djob] = mItemFetchJobs[job];
132 connect(djob, &ItemDeleteJob::result, this, &CollectionSearch::itemDeleteResult);
135 else
136 mItems << job->items();
138 mItemFetchJobs.remove(job);
140 if (mItemFetchJobs.isEmpty() && mItemDeleteJobs.isEmpty() && mCollectionJobs.isEmpty())
141 finish(); // all Items have now been fetched or deleted, so notify the result
144 /******************************************************************************
145 * Called when an ItemDeleteJob has completed.
147 void CollectionSearch::itemDeleteResult(KJob* j)
149 ItemDeleteJob* job = static_cast<ItemDeleteJob*>(j);
150 if (j->error())
151 qCDebug(KALARM_LOG) << "ItemDeleteJob: resource" << mItemDeleteJobs[job] << "GID" << mGid << "error: " << j->errorString();
152 else
153 ++mDeleteCount;
154 mItemDeleteJobs.remove(job);
156 if (mItemFetchJobs.isEmpty() && mItemDeleteJobs.isEmpty() && mCollectionJobs.isEmpty())
157 finish(); // all Items have now been deleted, so notify the result
160 /******************************************************************************
161 * Notify the result of the search/delete operation, and delete this instance.
163 void CollectionSearch::finish()
165 if (mDelete)
166 Q_EMIT deleted(mDeleteCount);
167 else if (mGid.isEmpty())
168 Q_EMIT collections(mCollections);
169 else
170 Q_EMIT items(mItems);
171 deleteLater();
176 // vim: et sw=4: