Akonadi

monitor.cpp
1 /*
2  Copyright (c) 2006 - 2007 Volker Krause <[email protected]>
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 "monitor.h"
21 #include "monitor_p.h"
22 
23 #include "changemediator_p.h"
24 #include "collectionfetchscope.h"
25 #include "itemfetchjob.h"
26 #include "session.h"
27 
28 #include <shared/akranges.h>
29 
30 #include <QMetaMethod>
31 
32 using namespace Akonadi;
33 using namespace AkRanges;
34 
36  : QObject(parent)
37  , d_ptr(new MonitorPrivate(nullptr, this))
38 {
39  d_ptr->init();
40  d_ptr->connectToNotificationManager();
41 
42  ChangeMediator::registerMonitor(this);
43 }
44 
45 //@cond PRIVATE
46 Monitor::Monitor(MonitorPrivate *d, QObject *parent)
47  : QObject(parent)
48  , d_ptr(d)
49 {
50  d_ptr->init();
51  d_ptr->connectToNotificationManager();
52 
53  ChangeMediator::registerMonitor(this);
54 }
55 //@endcond
56 
58 {
59  ChangeMediator::unregisterMonitor(this);
60 
61  delete d_ptr;
62 }
63 
64 void Monitor::setCollectionMonitored(const Collection &collection, bool monitored)
65 {
66  Q_D(Monitor);
67  if (!d->collections.contains(collection) && monitored) {
68  d->collections << collection;
69  d->pendingModification.startMonitoringCollection(collection.id());
70  d->scheduleSubscriptionUpdate();
71  } else if (!monitored) {
72  if (d->collections.removeAll(collection)) {
73  d->pendingModification.stopMonitoringCollection(collection.id());
74  d->scheduleSubscriptionUpdate();
75  }
76  }
77 
78  Q_EMIT collectionMonitored(collection, monitored);
79 }
80 
81 void Monitor::setItemMonitored(const Item &item, bool monitored)
82 {
83  Q_D(Monitor);
84  if (!d->items.contains(item.id()) && monitored) {
85  d->items.insert(item.id());
86  d->pendingModification.startMonitoringItem(item.id());
87  d->scheduleSubscriptionUpdate();
88  } else if (!monitored) {
89  if (d->items.remove(item.id())) {
90  d->pendingModification.stopMonitoringItem(item.id());
91  d->scheduleSubscriptionUpdate();
92  }
93  }
94 
95  Q_EMIT itemMonitored(item, monitored);
96 }
97 
98 void Monitor::setResourceMonitored(const QByteArray &resource, bool monitored)
99 {
100  Q_D(Monitor);
101  if (!d->resources.contains(resource) && monitored) {
102  d->resources.insert(resource);
103  d->pendingModification.startMonitoringResource(resource);
104  d->scheduleSubscriptionUpdate();
105  } else if (!monitored) {
106  if (d->resources.remove(resource)) {
107  d->pendingModification.stopMonitoringResource(resource);
108  d->scheduleSubscriptionUpdate();
109  }
110  }
111 
112  Q_EMIT resourceMonitored(resource, monitored);
113 }
114 
115 void Monitor::setMimeTypeMonitored(const QString &mimetype, bool monitored)
116 {
117  Q_D(Monitor);
118  if (!d->mimetypes.contains(mimetype) && monitored) {
119  d->mimetypes.insert(mimetype);
120  d->pendingModification.startMonitoringMimeType(mimetype);
121  d->scheduleSubscriptionUpdate();
122  } else if (!monitored) {
123  if (d->mimetypes.remove(mimetype)) {
124  d->pendingModification.stopMonitoringMimeType(mimetype);
125  d->scheduleSubscriptionUpdate();
126  }
127  }
128 
129  Q_EMIT mimeTypeMonitored(mimetype, monitored);
130 }
131 
132 void Monitor::setTagMonitored(const Akonadi::Tag &tag, bool monitored)
133 {
134  Q_D(Monitor);
135  if (!d->tags.contains(tag.id()) && monitored) {
136  d->tags.insert(tag.id());
137  d->pendingModification.startMonitoringTag(tag.id());
138  d->scheduleSubscriptionUpdate();
139  } else if (!monitored) {
140  if (d->tags.remove(tag.id())) {
141  d->pendingModification.stopMonitoringTag(tag.id());
142  d->scheduleSubscriptionUpdate();
143  }
144  }
145 
146  Q_EMIT tagMonitored(tag, monitored);
147 }
148 
149 void Monitor::setTypeMonitored(Monitor::Type type, bool monitored)
150 {
151  Q_D(Monitor);
152  if (!d->types.contains(type) && monitored) {
153  d->types.insert(type);
154  d->pendingModification.startMonitoringType(MonitorPrivate::monitorTypeToProtocol(type));
155  d->scheduleSubscriptionUpdate();
156  } else if (!monitored) {
157  if (d->types.remove(type)) {
158  d->pendingModification.stopMonitoringType(MonitorPrivate::monitorTypeToProtocol(type));
159  d->scheduleSubscriptionUpdate();
160  }
161  }
162 
163  Q_EMIT typeMonitored(type, monitored);
164 }
165 
167 {
168  Q_D(Monitor);
169  if (d->monitorAll == monitored) {
170  return;
171  }
172 
173  d->monitorAll = monitored;
174 
175  d->pendingModification.setAllMonitored(monitored);
176  d->scheduleSubscriptionUpdate();
177 
178  Q_EMIT allMonitored(monitored);
179 }
180 
181 void Monitor::setExclusive(bool exclusive)
182 {
183  Q_D(Monitor);
184  d->exclusive = exclusive;
185  d->pendingModification.setIsExclusive(exclusive);
186  d->scheduleSubscriptionUpdate();
187 }
188 
189 bool Monitor::exclusive() const
190 {
191  Q_D(const Monitor);
192  return d->exclusive;
193 }
194 
196 {
197  Q_D(Monitor);
198 
199  if (!d->sessions.contains(session->sessionId())) {
200  d->sessions << session->sessionId();
201  connect(session, &Session::destroyed, this, [d](QObject *o) { d->slotSessionDestroyed(o); });
202  d->pendingModification.startIgnoringSession(session->sessionId());
203  d->scheduleSubscriptionUpdate();
204  }
205 }
206 
207 void Monitor::fetchCollection(bool enable)
208 {
209  Q_D(Monitor);
210  d->fetchCollection = enable;
211 }
212 
214 {
215  Q_D(Monitor);
216  d->fetchCollectionStatistics = enable;
217 }
218 
220 {
221  Q_D(Monitor);
222  d->mItemFetchScope = fetchScope;
223  d->pendingModificationChanges |= Protocol::ModifySubscriptionCommand::ItemFetchScope;
224  d->scheduleSubscriptionUpdate();
225 }
226 
228 {
229  Q_D(Monitor);
230  d->pendingModificationChanges |= Protocol::ModifySubscriptionCommand::ItemFetchScope;
231  d->scheduleSubscriptionUpdate();
232  return d->mItemFetchScope;
233 }
234 
235 void Monitor::fetchChangedOnly(bool enable)
236 {
237  Q_D(Monitor);
238  d->mFetchChangedOnly = enable;
239 }
240 
242 {
243  Q_D(Monitor);
244  d->mCollectionFetchScope = fetchScope;
245  d->pendingModificationChanges |= Protocol::ModifySubscriptionCommand::CollectionFetchScope;
246  d->scheduleSubscriptionUpdate();
247 }
248 
250 {
251  Q_D(Monitor);
252  d->pendingModificationChanges |= Protocol::ModifySubscriptionCommand::CollectionFetchScope;
253  d->scheduleSubscriptionUpdate();
254  return d->mCollectionFetchScope;
255 }
256 
258 {
259  Q_D(Monitor);
260  d->mTagFetchScope = fetchScope;
261  d->pendingModificationChanges |= Protocol::ModifySubscriptionCommand::TagFetchScope;
262  d->scheduleSubscriptionUpdate();
263 }
264 
266 {
267  Q_D(Monitor);
268  d->pendingModificationChanges |= Protocol::ModifySubscriptionCommand::TagFetchScope;
269  d->scheduleSubscriptionUpdate();
270  return d->mTagFetchScope;
271 }
272 
274 {
275  Q_D(const Monitor);
276  return d->collections;
277 }
278 
280 {
281  Q_D(const Monitor);
282  QVector<Item::Id> result;
283  result.reserve(d->items.size());
284  std::copy(d->items.begin(), d->items.end(), std::back_inserter(result));
285  return result;
286 }
287 
289 {
290  Q_D(const Monitor);
291  return d->items.size();
292 }
293 
295 {
296  Q_D(const Monitor);
297  QVector<Tag::Id> result;
298  result.reserve(d->tags.size());
299  std::copy(d->tags.begin(), d->tags.end(), std::back_inserter(result));
300  return result;
301 }
302 
304 {
305  Q_D(const Monitor);
306  QVector<Monitor::Type> result;
307  result.reserve(d->types.size());
308  std::copy(d->types.begin(), d->types.end(), std::back_inserter(result));
309  return result;
310 }
311 
313 {
314  Q_D(const Monitor);
315  return d->mimetypes | Actions::toQList;
316 }
317 
319 {
320  Q_D(const Monitor);
321  return d->mimetypes.count();
322 }
323 
325 {
326  Q_D(const Monitor);
327  return d->resources | Actions::toQList;
328 }
329 
331 {
332  Q_D(const Monitor);
333  return d->resources.count();
334 }
335 
337 {
338  Q_D(const Monitor);
339  return d->monitorAll;
340 }
341 
343 {
344  Q_D(Monitor);
345  if (session == d->session) {
346  return;
347  }
348 
349  if (!session) {
350  d->session = Session::defaultSession();
351  } else {
352  d->session = session;
353  }
354 
355  d->itemCache->setSession(d->session);
356  d->collectionCache->setSession(d->session);
357  d->tagCache->setSession(d->session);
358 
359  // Reconnect with a new session
360  d->connectToNotificationManager();
361 }
362 
364 {
365  Q_D(const Monitor);
366  return d->session;
367 }
368 
370 {
371  Q_D(Monitor);
372  d->collectionMoveTranslationEnabled = enabled;
373 }
374 
375 void Monitor::connectNotify(const QMetaMethod &signal)
376 {
377  Q_D(Monitor);
378  d->updateListeners(signal, MonitorPrivate::AddListener);
379 }
380 
381 void Monitor::disconnectNotify(const QMetaMethod &signal)
382 {
383  Q_D(Monitor);
384  d->updateListeners(signal, MonitorPrivate::RemoveListener);
385 }
386 
387 #include "moc_monitor.cpp"
void setAllMonitored(bool monitored=true)
Sets whether all items shall be monitored.
Definition: monitor.cpp:166
void fetchCollectionStatistics(bool enable)
Enables automatic fetching of changed collection statistics information from the Akonadi storage...
Definition: monitor.cpp:213
QStringList mimeTypesMonitored() const
Returns the set of mimetypes being monitored.
Definition: monitor.cpp:312
void setTagMonitored(const Tag &tag, bool monitored=true)
Sets whether the specified tag shall be monitored for changes.
Definition: monitor.cpp:132
TagFetchScope & tagFetchScope()
Returns the tag fetch scope.
Definition: monitor.cpp:265
QList< QByteArray > resourcesMonitored() const
Returns the set of identifiers for resources being monitored.
Definition: monitor.cpp:324
void allMonitored(bool monitored)
This signal is emitted if the Monitor starts or stops monitoring everything.
Session * session() const
Returns the Session used by the monitor to communicate with Akonadi.
Definition: monitor.cpp:363
void collectionMonitored(const Akonadi::Collection &collection, bool monitored)
This signal is emitted if the Monitor starts or stops monitoring collection explicitly.
void setTagFetchScope(const TagFetchScope &fetchScope)
Sets the tag fetch scope.
Definition: monitor.cpp:257
Specifies which parts of a collection should be fetched from the Akonadi storage. ...
void setSession(Akonadi::Session *session)
Sets the session used by the Monitor to communicate with the Akonadi server.
Definition: monitor.cpp:342
void setMimeTypeMonitored(const QString &mimetype, bool monitored=true)
Sets whether items of the specified mime type shall be monitored for changes.
Definition: monitor.cpp:115
void setCollectionMonitored(const Collection &collection, bool monitored=true)
Sets whether the specified collection shall be monitored for changes.
Definition: monitor.cpp:64
Specifies which parts of a tag should be fetched from the Akonadi storage.
Definition: tagfetchscope.h:35
Represents a collection of PIM items.
Definition: collection.h:76
void mimeTypeMonitored(const QString &mimeType, bool monitored)
This signal is emitted if the Monitor starts or stops monitoring mimeType explicitly.
void setItemFetchScope(const ItemFetchScope &fetchScope)
Sets the item fetch scope.
Definition: monitor.cpp:219
void ignoreSession(Session *session)
Ignores all change notifications caused by the given session.
Definition: monitor.cpp:195
Id id() const
Returns the unique identifier of the tag.
Definition: tag.cpp:152
static Session * defaultSession()
Returns the default session for this thread.
~Monitor() override
Destroys the monitor.
Definition: monitor.cpp:57
QByteArray sessionId() const
Returns the session identifier.
QVector< Tag::Id > tagsMonitored() const
Returns the set of tags being monitored.
Definition: monitor.cpp:294
ItemFetchScope & itemFetchScope()
Returns the item fetch scope.
Definition: monitor.cpp:227
Collection::List collectionsMonitored() const
Returns the list of collections being monitored.
Definition: monitor.cpp:273
Monitor(QObject *parent=nullptr)
Creates a new monitor.
Definition: monitor.cpp:35
void resourceMonitored(const QByteArray &identifier, bool monitored)
This signal is emitted if the Monitor starts or stops monitoring the resource with the identifier ide...
int numResourcesMonitored() const
Returns the number of resources being monitored.
Definition: monitor.cpp:330
CollectionFetchScope & collectionFetchScope()
Returns the collection fetch scope.
Definition: monitor.cpp:249
void setCollectionFetchScope(const CollectionFetchScope &fetchScope)
Sets the collection fetch scope.
Definition: monitor.cpp:241
A communication session with the Akonadi storage.
Definition: core/session.h:67
void fetchCollection(bool enable)
Enables automatic fetching of changed collections from the Akonadi storage.
Definition: monitor.cpp:207
bool isAllMonitored() const
Returns true if everything is being monitored.
Definition: monitor.cpp:336
void tagMonitored(const Akonadi::Tag &tag, bool monitored)
This signal is emitted if the Monitor starts or stops monitoring tag explicitly.
void setCollectionMoveTranslationEnabled(bool enabled)
Allows to enable/disable collection move translation.
Definition: monitor.cpp:369
virtual void connectNotify(const QMetaMethod &signal)
void reserve(int size)
Specifies which parts of an item should be fetched from the Akonadi storage.
int numItemsMonitored() const
Returns the number of items being monitored.
Definition: monitor.cpp:288
void setResourceMonitored(const QByteArray &resource, bool monitored=true)
Sets whether the specified resource shall be monitored for changes.
Definition: monitor.cpp:98
void setTypeMonitored(Type type, bool monitored=true)
Sets whether given type (Collection, Item, Tag should be monitored).
Definition: monitor.cpp:149
virtual void disconnectNotify(const QMetaMethod &signal)
Monitors an item or collection for changes.
Definition: monitor.h:84
void typeMonitored(const Akonadi::Monitor::Type type, bool monitored)
This signal is emitted if the Monitor starts or stops monitoring type explicitly. ...
QObject(QObject *parent)
Id id() const
Returns the unique identifier of the collection.
Definition: collection.cpp:112
Helper integration between Akonadi and Qt.
An Akonadi Tag.
Definition: tag.h:39
QVector< Item::Id > itemsMonitoredEx() const
Returns the set of items being monitored.
Definition: monitor.cpp:279
void itemMonitored(const Akonadi::Item &item, bool monitored)
This signal is emitted if the Monitor starts or stops monitoring item explicitly. ...
QVector< Type > typesMonitored() const
Returns the set of types being monitored.
Definition: monitor.cpp:303
int numMimeTypesMonitored() const
Returns the number of mimetypes being monitored.
Definition: monitor.cpp:318
void setItemMonitored(const Item &item, bool monitored=true)
Sets whether the specified item shall be monitored for changes.
Definition: monitor.cpp:81
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
void destroyed(QObject *obj)
Q_EMITQ_EMIT
void fetchChangedOnly(bool enable)
Instructs the monitor to fetch only those parts that were changed and were requested in the fetch sco...
Definition: monitor.cpp:235
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:08:55 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.