1/*
2 Copyright (c) 2011 Tobias Koenig <tokoe@kde.org>
3 Copyright (c) 2011 Stephen Kelly <steveire@gmail.com>
4
5 This library is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or (at your
8 option) any later version.
9
10 This library is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301, USA.
19*/
20
21#include "changemediator_p.h"
22
23#include <QApplication>
24
25#include "changenotificationdependenciesfactory_p.h"
26#include "notificationsourceinterface.h"
27#include "job_p.h"
28#include "itemmovejob.h"
29#include "movejobimpl_p.h"
30#include "collection.h"
31#include "item.h"
32
33#include <kglobal.h>
34
35//static const char mediatorSessionId[] = "MediatorSession"; TODO: remove?
36
37using namespace Akonadi;
38
39K_GLOBAL_STATIC(ChangeMediator, s_globalChangeMediator)
40
41ChangeMediator *ChangeMediator::instance()
42{
43 if (s_globalChangeMediator.isDestroyed()) {
44 return 0;
45 } else {
46 return s_globalChangeMediator;
47 }
48}
49
50ChangeMediator::ChangeMediator(QObject *parent)
51 : QObject(parent)
52{
53 if (qApp) {
54 this->moveToThread(qApp->thread());
55 }
56}
57
58/* static */
59void ChangeMediator::registerMonitor(QObject *monitor)
60{
61 QMetaObject::invokeMethod(instance(), "do_registerMonitor", Q_ARG(QObject *, monitor));
62}
63
64/* static */
65void ChangeMediator::unregisterMonitor(QObject *monitor)
66{
67 QMetaObject::invokeMethod(instance(), "do_unregisterMonitor", Q_ARG(QObject *, monitor));
68}
69
70/* static */
71void ChangeMediator::invalidateCollection(const Akonadi::Collection &collection)
72{
73 QMetaObject::invokeMethod(instance(), "do_invalidateCollection", Q_ARG(Akonadi::Collection, collection));
74}
75
76/* static */
77void ChangeMediator::invalidateItem(const Akonadi::Item &item)
78{
79 QMetaObject::invokeMethod(instance(), "do_invalidateItem", Q_ARG(Akonadi::Item, item));
80}
81
82/* static */
83void ChangeMediator::invalidateTag(const Tag &tag)
84{
85 QMetaObject::invokeMethod(instance(), "do_invalidateTag", Q_ARG(Akonadi::Tag, tag));
86}
87
88void ChangeMediator::do_registerMonitor(QObject *monitor)
89{
90 m_monitors.append(monitor);
91}
92
93void ChangeMediator::do_unregisterMonitor(QObject *monitor)
94{
95 m_monitors.removeAll(monitor);
96}
97
98void ChangeMediator::do_invalidateCollection(const Akonadi::Collection &collection)
99{
100 foreach (QObject *monitor, m_monitors) {
101 QMetaObject::invokeMethod(monitor, "invalidateCollectionCache", Qt::AutoConnection, Q_ARG(qint64, collection.id()));
102 }
103}
104
105void ChangeMediator::do_invalidateItem(const Akonadi::Item &item)
106{
107 foreach (QObject *monitor, m_monitors) {
108 QMetaObject::invokeMethod(monitor, "invalidateItemCache", Qt::AutoConnection, Q_ARG(qint64, item.id()));
109 }
110}
111
112void ChangeMediator::do_invalidateTag(const Tag &tag)
113{
114 foreach (QObject *monitor, m_monitors) {
115 QMetaObject::invokeMethod(monitor, "invalidateTagCache", Qt::AutoConnection, Q_ARG(qint64, tag.id()));
116 }
117}
118
119/* static */
120void ChangeMediator::registerSession(const QByteArray &id)
121{
122 if (id != "MediatorSession") {
123 QMetaObject::invokeMethod(instance(), "do_registerSession", Qt::AutoConnection, Q_ARG(QByteArray, id));
124 }
125}
126
127/* static */
128void ChangeMediator::unregisterSession(const QByteArray &id)
129{
130 if (id != "MediatorSession") {
131 QMetaObject::invokeMethod(instance(), "do_unregisterSession", Qt::AutoConnection, Q_ARG(QByteArray, id));
132 }
133}
134
135/* static */
136void ChangeMediator::beginMoveItems(JobPrivate *movePrivate, const QByteArray &id)
137{
138 QMetaObject::invokeMethod(instance(), "do_beginMoveItems", Qt::AutoConnection, Q_ARG(JobPrivate *, movePrivate), Q_ARG(QByteArray, id));
139}
140
141/* static */
142void ChangeMediator::itemsMoved(const Item::List &items, const Collection &sourceParent, const QByteArray &id)
143{
144 QMetaObject::invokeMethod(instance(), "do_itemsMoved", Qt::AutoConnection, Q_ARG(Item::List, items), Q_ARG(Collection, sourceParent), Q_ARG(QByteArray, id));
145}
146
147void ChangeMediator::do_registerSession(const QByteArray &id)
148{
149 Q_ASSERT(!id.isEmpty());
150 m_sessions.insert(id);
151}
152
153void ChangeMediator::do_unregisterSession(const QByteArray &id)
154{
155 Q_ASSERT(!id.isEmpty());
156 m_sessions.remove(id);
157}
158
159void ChangeMediator::do_beginMoveItems(JobPrivate *movePrivate, const QByteArray &id)
160{
161 if (!m_sessions.contains(id)) {
162 return;
163 }
164
165 Q_ASSERT(movePrivate->q_ptr->inherits("Akonadi::ItemMoveJob"));
166 MoveJobImpl<Item, ItemMoveJob> *itemMoveJob = static_cast<MoveJobImpl<Item, ItemMoveJob>*>(movePrivate);
167
168 Item::List itemsDataAvailable;
169 Item::List itemsDataNotAvailable;
170
171 foreach (const Item &item, itemMoveJob->objectsToMove) {
172 if (item.parentCollection().isValid()) {
173 itemsDataAvailable.append(item);
174 } else {
175 itemsDataNotAvailable.append(item);
176 }
177 }
178
179 if (itemsDataNotAvailable.isEmpty()) {
180 return;
181 }
182
183// if (itemsDataAvailable.isEmpty())
184
185}
186
187void ChangeMediator::do_itemsMoved(const Item::List &items, const Collection &sourceParent, const QByteArray &id)
188{
189 qDebug() << "MOVED" << items.size() << sourceParent << "TO " << items.first().parentCollection() << id;
190}
191