• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kalarm

  • sources
  • kde-4.14
  • kdepim
  • kalarm
collectionsearch.cpp
Go to the documentation of this file.
1 /*
2  * collectionsearch.cpp - Search Akonadi Collections
3  * Program: kalarm
4  * Copyright © 2014 by David Jarvie <djarvie@kde.org>
5  *
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.
10  *
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.
15  *
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.
19  */
20 
21 // kdepimlibs and kdepim-runtime 4.12.4 minimum are required
22 #include <kdeversion.h>
23 #if defined(USE_AKONADI) && KDE_IS_VERSION(4,12,4)
24 
25 #include "collectionsearch.h"
26 
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>
33 
34 #include <QStringList>
35 #include <QTimer>
36 
37 using namespace Akonadi;
38 
39 /******************************************************************************
40 * Constructor.
41 * Creates jobs to fetch all collections for resources containing the mime type.
42 * Its subsequent actions depend on the parameters:
43 * - If 'remove' is true, it will locate all Items with the specified 'gid' and
44 * delete them. The deleted() signal will be emitted.
45 * - Otherwise, if 'gid' is specified, it will emit the signal items() to
46 * notify all Items with that GID.
47 * - Otherwise, it will emit the signal collections() to notify all Collections.
48 */
49 CollectionSearch::CollectionSearch(const QString& mimeType, const QString& gid, bool remove)
50  : mMimeType(mimeType),
51  mGid(gid),
52  mDeleteCount(0),
53  mDelete(remove && !mGid.isEmpty())
54 {
55  const AgentInstance::List agents = AgentManager::self()->instances();
56  foreach (const AgentInstance& agent, agents)
57  {
58  if (agent.type().mimeTypes().contains(mimeType))
59  {
60  {
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*)));
65  }
66  }
67  }
68 
69  if (mCollectionJobs.isEmpty())
70  {
71  // There are no resources containing the mime type, so ensure that a
72  // signal is emitted after construction.
73  QTimer::singleShot(0, this, SLOT(finish()));
74  }
75 }
76 
77 /******************************************************************************
78 * Called when a CollectionFetchJob has completed.
79 */
80 void CollectionSearch::collectionFetchResult(KJob* j)
81 {
82  CollectionFetchJob* job = static_cast<CollectionFetchJob*>(j);
83  if (j->error())
84  kError() << "CollectionFetchJob" << job->fetchScope().resource() << "error: " << j->errorString();
85  else
86  {
87  const Collection::List collections = job->collections();
88  foreach (const Collection& c, collections)
89  {
90  if (c.contentMimeTypes().contains(mMimeType))
91  {
92  if (mGid.isEmpty())
93  mCollections << c;
94  else
95  {
96  // Search for all Items with the specified GID
97  Item item;
98  item.setGid(mGid);
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*)));
103  }
104  }
105  }
106  }
107  mCollectionJobs.removeAll(job);
108 
109  if (mCollectionJobs.isEmpty())
110  {
111  // All collections have now been fetched
112  if (mGid.isEmpty())
113  finish();
114  }
115 }
116 
117 /******************************************************************************
118 * Called when an ItemFetchJob has completed.
119 */
120 void CollectionSearch::itemFetchResult(KJob* j)
121 {
122  ItemFetchJob* job = static_cast<ItemFetchJob*>(j);
123  if (j->error())
124  kDebug() << "ItemFetchJob: collection" << mItemFetchJobs[job] << "GID" << mGid << "error: " << j->errorString();
125  else
126  {
127  if (mDelete)
128  {
129  Item::List items = job->items();
130  foreach (const Item& item, items)
131  {
132  ItemDeleteJob* djob = new ItemDeleteJob(item, this);
133  mItemDeleteJobs[djob] = mItemFetchJobs[job];
134  connect(djob, SIGNAL(result(KJob*)), SLOT(itemDeleteResult(KJob*)));
135  }
136  }
137  else
138  mItems << job->items();
139  }
140  mItemFetchJobs.remove(job);
141 
142  if (mItemFetchJobs.isEmpty() && mItemDeleteJobs.isEmpty() && mCollectionJobs.isEmpty())
143  finish(); // all Items have now been fetched or deleted, so notify the result
144 }
145 
146 /******************************************************************************
147 * Called when an ItemDeleteJob has completed.
148 */
149 void CollectionSearch::itemDeleteResult(KJob* j)
150 {
151  ItemDeleteJob* job = static_cast<ItemDeleteJob*>(j);
152  if (j->error())
153  kDebug() << "ItemDeleteJob: resource" << mItemDeleteJobs[job] << "GID" << mGid << "error: " << j->errorString();
154  else
155  ++mDeleteCount;
156  mItemDeleteJobs.remove(job);
157 
158  if (mItemFetchJobs.isEmpty() && mItemDeleteJobs.isEmpty() && mCollectionJobs.isEmpty())
159  finish(); // all Items have now been deleted, so notify the result
160 }
161 
162 /******************************************************************************
163 * Notify the result of the search/delete operation, and delete this instance.
164 */
165 void CollectionSearch::finish()
166 {
167  if (mDelete)
168  emit deleted(mDeleteCount);
169  else if (mGid.isEmpty())
170  emit collections(mCollections);
171  else
172  emit items(mItems);
173  deleteLater();
174 }
175 
176 #include "moc_collectionsearch.cpp"
177 #endif
178 
179 // vim: et sw=4:
QString
KJob
collectionsearch.h
QTimer::singleShot
singleShot
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:34:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kalarm

Skip menu "kalarm"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal