Akonadi

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

KDE's Doxygen guidelines are available online.