2 * collectionsearch.cpp - Search Akonadi Collections
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>
33 #include "kalarm_debug.h"
35 using namespace Akonadi
;
37 /******************************************************************************
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
),
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
);
82 qCCritical(KALARM_LOG
) << "CollectionFetchJob" << job
->fetchScope().resource() << "error: " << j
->errorString();
85 const Collection::List collections
= job
->collections();
86 foreach (const Collection
& c
, collections
)
88 if (c
.contentMimeTypes().contains(mMimeType
))
94 // Search for all Items with the specified GID
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
115 /******************************************************************************
116 * Called when an ItemFetchJob has completed.
118 void CollectionSearch::itemFetchResult(KJob
* j
)
120 ItemFetchJob
* job
= static_cast<ItemFetchJob
*>(j
);
122 qCDebug(KALARM_LOG
) << "ItemFetchJob: collection" << mItemFetchJobs
[job
] << "GID" << mGid
<< "error: " << j
->errorString();
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
);
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
);
151 qCDebug(KALARM_LOG
) << "ItemDeleteJob: resource" << mItemDeleteJobs
[job
] << "GID" << mGid
<< "error: " << j
->errorString();
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()
166 Q_EMIT
deleted(mDeleteCount
);
167 else if (mGid
.isEmpty())
168 Q_EMIT
collections(mCollections
);
170 Q_EMIT
items(mItems
);