1/*
2 Copyright (c) 2006 - 2007 Volker Krause <vkrause@kde.org>
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 <kdebug.h>
29
30#include <QtDBus/QDBusInterface>
31#include <QtDBus/QDBusConnection>
32
33#include <QtCore/QDebug>
34#include <QtCore/QTimer>
35#include <iterator>
36
37using namespace Akonadi;
38
39Monitor::Monitor(QObject *parent)
40 : QObject(parent)
41 , d_ptr(new MonitorPrivate(0, this))
42{
43 d_ptr->init();
44 d_ptr->connectToNotificationManager();
45}
46
47//@cond PRIVATE
48Monitor::Monitor(MonitorPrivate *d, QObject *parent)
49 : QObject(parent)
50 , d_ptr(d)
51{
52 d_ptr->init();
53 d_ptr->connectToNotificationManager();
54
55 ChangeMediator::registerMonitor(this);
56}
57//@endcond
58
59Monitor::~Monitor()
60{
61 ChangeMediator::unregisterMonitor(this);
62
63 delete d_ptr;
64}
65
66void Monitor::setCollectionMonitored(const Collection &collection, bool monitored)
67{
68 Q_D(Monitor);
69 if (!d->collections.contains(collection) && monitored) {
70 d->collections << collection;
71 if (d->notificationSource) {
72 d->notificationSource->setMonitoredCollection(collection.id(), true);
73 }
74 } else if (!monitored) {
75 if (d->collections.removeAll(collection)) {
76 d->cleanOldNotifications();
77 if (d->notificationSource) {
78 d->notificationSource->setMonitoredCollection(collection.id(), false);
79 }
80 }
81 }
82
83 emit collectionMonitored(collection, monitored);
84}
85
86void Monitor::setItemMonitored(const Item &item, bool monitored)
87{
88 Q_D(Monitor);
89 if (!d->items.contains(item.id()) && monitored) {
90 d->items.insert(item.id());
91 if (d->notificationSource) {
92 d->notificationSource->setMonitoredItem(item.id(), true);
93 }
94 } else if (!monitored) {
95 if (d->items.remove(item.id())) {
96 d->cleanOldNotifications();
97 if (d->notificationSource) {
98 d->notificationSource->setMonitoredItem(item.id(), false);
99 }
100 }
101 }
102
103 emit itemMonitored(item, monitored);
104}
105
106void Monitor::setResourceMonitored(const QByteArray &resource, bool monitored)
107{
108 Q_D(Monitor);
109 if (!d->resources.contains(resource) && monitored) {
110 d->resources.insert(resource);
111 if (d->notificationSource) {
112 d->notificationSource->setMonitoredResource(resource, true);
113 }
114 } else if (!monitored) {
115 if (d->resources.remove(resource)) {
116 d->cleanOldNotifications();
117 if (d->notificationSource) {
118 d->notificationSource->setMonitoredResource(resource, false);
119 }
120 }
121 }
122
123 emit resourceMonitored(resource, monitored);
124}
125
126void Monitor::setMimeTypeMonitored(const QString &mimetype, bool monitored)
127{
128 Q_D(Monitor);
129 if (!d->mimetypes.contains(mimetype) && monitored) {
130 d->mimetypes.insert(mimetype);
131 if (d->notificationSource) {
132 d->notificationSource->setMonitoredMimeType(mimetype, true);
133 }
134 } else if (!monitored) {
135 if (d->mimetypes.remove(mimetype)) {
136 d->cleanOldNotifications();
137 if (d->notificationSource) {
138 d->notificationSource->setMonitoredMimeType(mimetype, false);
139 }
140 }
141 }
142
143 emit mimeTypeMonitored(mimetype, monitored);
144}
145
146void Monitor::setTagMonitored(const Akonadi::Tag &tag, bool monitored)
147{
148 Q_D(Monitor);
149 if (!d->tags.contains(tag.id()) && monitored) {
150 d->tags.insert(tag.id());
151 if (d->notificationSource) {
152 d->notificationSource->setMonitoredTag(tag.id(), true);
153 }
154 } else if (!monitored) {
155 if (d->tags.remove(tag.id())) {
156 d->cleanOldNotifications();
157 if (d->notificationSource) {
158 d->notificationSource->setMonitoredTag(tag.id(), false);
159 }
160 }
161 }
162
163 emit tagMonitored(tag, monitored);
164}
165
166void Monitor::setTypeMonitored(Monitor::Type type, bool monitored)
167{
168 Q_D(Monitor);
169 if (!d->types.contains(type) && monitored) {
170 d->types.insert(type);
171 if (d->notificationSource) {
172 d->notificationSource->setMonitoredType(static_cast<NotificationMessageV2::Type>(type), true);
173 }
174 } else if (!monitored) {
175 if (d->types.remove(type)) {
176 d->cleanOldNotifications();
177 if (d->notificationSource) {
178 d->notificationSource->setMonitoredType(static_cast<NotificationMessageV2::Type>(type), false);
179 }
180 }
181 }
182
183 emit typeMonitored(type, monitored);
184}
185
186void Akonadi::Monitor::setAllMonitored(bool monitored)
187{
188 Q_D(Monitor);
189 if (d->monitorAll == monitored) {
190 return;
191 }
192
193 d->monitorAll = monitored;
194
195 if (!monitored) {
196 d->cleanOldNotifications();
197 }
198
199 if (d->notificationSource) {
200 d->notificationSource->setAllMonitored(monitored);
201 }
202
203 emit allMonitored(monitored);
204}
205
206void Monitor::ignoreSession(Session *session)
207{
208 Q_D(Monitor);
209
210 if (!d->sessions.contains(session->sessionId())) {
211 d->sessions << session->sessionId();
212 connect(session, SIGNAL(destroyed(QObject*)), this, SLOT(slotSessionDestroyed(QObject*)));
213 if (d->notificationSource) {
214 d->notificationSource->setIgnoredSession(session->sessionId(), true);
215 }
216 }
217}
218
219void Monitor::fetchCollection(bool enable)
220{
221 Q_D(Monitor);
222 d->fetchCollection = enable;
223}
224
225void Monitor::fetchCollectionStatistics(bool enable)
226{
227 Q_D(Monitor);
228 d->fetchCollectionStatistics = enable;
229}
230
231void Monitor::setItemFetchScope(const ItemFetchScope &fetchScope)
232{
233 Q_D(Monitor);
234 d->mItemFetchScope = fetchScope;
235}
236
237ItemFetchScope &Monitor::itemFetchScope()
238{
239 Q_D(Monitor);
240 return d->mItemFetchScope;
241}
242
243void Monitor::fetchChangedOnly(bool enable)
244{
245 Q_D(Monitor);
246 d->mFetchChangedOnly = enable;
247}
248
249void Monitor::setCollectionFetchScope(const CollectionFetchScope &fetchScope)
250{
251 Q_D(Monitor);
252 d->mCollectionFetchScope = fetchScope;
253}
254
255CollectionFetchScope &Monitor::collectionFetchScope()
256{
257 Q_D(Monitor);
258 return d->mCollectionFetchScope;
259}
260
261void Monitor::setTagFetchScope(const TagFetchScope &fetchScope)
262{
263 Q_D(Monitor);
264 d->mTagFetchScope = fetchScope;
265}
266
267TagFetchScope &Monitor::tagFetchScope()
268{
269 Q_D(Monitor);
270 return d->mTagFetchScope;
271}
272
273Akonadi::Collection::List Monitor::collectionsMonitored() const
274{
275 Q_D(const Monitor);
276 return d->collections;
277}
278
279QList<Item::Id> Monitor::itemsMonitored() const
280{
281 Q_D(const Monitor);
282 return d->items.toList();
283}
284
285QVector<Item::Id> Monitor::itemsMonitoredEx() const
286{
287 Q_D(const Monitor);
288 QVector<Item::Id> result;
289 result.reserve(d->items.size());
290 qCopy(d->items.begin(), d->items.end(), std::back_inserter(result));
291 return result;
292}
293
294QVector<Tag::Id> Monitor::tagsMonitored() const
295{
296 Q_D(const Monitor);
297 QVector<Tag::Id> result;
298 result.reserve(d->tags.size());
299 qCopy(d->tags.begin(), d->tags.end(), std::back_inserter(result));
300 return result;
301}
302
303QVector<Monitor::Type> Monitor::typesMonitored() const
304{
305 Q_D(const Monitor);
306 QVector<Monitor::Type> result;
307 result.reserve(d->types.size());
308 qCopy(d->types.begin(), d->types.end(), std::back_inserter(result));
309 return result;
310}
311
312QStringList Monitor::mimeTypesMonitored() const
313{
314 Q_D(const Monitor);
315 return d->mimetypes.toList();
316}
317
318QList<QByteArray> Monitor::resourcesMonitored() const
319{
320 Q_D(const Monitor);
321 return d->resources.toList();
322}
323
324bool Monitor::isAllMonitored() const
325{
326 Q_D(const Monitor);
327 return d->monitorAll;
328}
329
330void Monitor::setSession(Akonadi::Session *session)
331{
332 Q_D(Monitor);
333 if (session == d->session) {
334 return;
335 }
336
337 if (!session) {
338 d->session = Session::defaultSession();
339 } else {
340 d->session = session;
341 }
342
343 d->itemCache->setSession(d->session);
344 d->collectionCache->setSession(d->session);
345}
346
347Session *Monitor::session() const
348{
349 Q_D(const Monitor);
350 return d->session;
351}
352
353void Monitor::setCollectionMoveTranslationEnabled(bool enabled)
354{
355 Q_D(Monitor);
356 d->collectionMoveTranslationEnabled = enabled;
357}
358
359#include "moc_monitor.cpp"
360