]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/nepomuk/resourcewatcher.cpp
Remove kde-runtime dependency "nepomukdatamanagement"
[dolphin.git] / src / kitemviews / private / nepomuk / resourcewatcher.cpp
1 /*
2 This file is part of the Nepomuk KDE project.
3 Copyright (C) 2011 Vishesh Handa <handa.vish@gmail.com>
4 Copyright (C) 2011-2012 Sebastian Trueg <trueg@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library 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 GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21
22 #include "resourcewatcher.h"
23 #include "resourcewatcherconnectioninterface.h"
24 #include "resourcewatchermanagerinterface.h"
25
26 #include <QtDBus/QDBusObjectPath>
27
28 #include <nepomuk/resource.h>
29 #include <nepomuk/resourcemanager.h>
30
31 #include <KUrl>
32 #include <KDebug>
33
34 namespace {
35 QString convertUri(const QUrl& uri) {
36 return KUrl(uri).url();
37 }
38
39 QStringList convertUris(const QList<QUrl>& uris) {
40 QStringList cs;
41 foreach(const QUrl& uri, uris) {
42 cs << convertUri(uri);
43 }
44 return cs;
45 }
46
47 QList<QUrl> convertUris(const QStringList& uris) {
48 QList<QUrl> us;
49 foreach(const QString& uri, uris) {
50 us << KUrl(uri);
51 }
52 return us;
53 }
54 }
55
56 class Nepomuk::ResourceWatcher::Private {
57 public:
58 QList<QUrl> m_types;
59 QList<QUrl> m_resources;
60 QList<QUrl> m_properties;
61
62 org::kde::nepomuk::ResourceWatcherConnection * m_connectionInterface;
63 org::kde::nepomuk::ResourceWatcher * m_watchManagerInterface;
64 };
65
66 Nepomuk::ResourceWatcher::ResourceWatcher(QObject* parent)
67 : QObject(parent),
68 d(new Private)
69 {
70 d->m_watchManagerInterface
71 = new org::kde::nepomuk::ResourceWatcher( "org.kde.nepomuk.DataManagement",
72 "/resourcewatcher",
73 QDBusConnection::sessionBus() );
74 d->m_connectionInterface = 0;
75 }
76
77 Nepomuk::ResourceWatcher::~ResourceWatcher()
78 {
79 stop();
80 delete d;
81 }
82
83 bool Nepomuk::ResourceWatcher::start()
84 {
85 stop();
86
87 //
88 // Convert to list of strings
89 //
90 QList<QString> uris = convertUris(d->m_resources);
91 QList<QString> props = convertUris(d->m_properties);
92 QList<QString> types_ = convertUris(d->m_types);
93
94 //
95 // Watch for the RW service to (re-)appear and then re-connect to make sure we always get updates
96 // We create this watcher even if we fail to connect below. Thus, once the rw service comes up we
97 // can re-attach.
98 //
99 connect(ResourceManager::instance(), SIGNAL(nepomukSystemStarted()), this, SLOT(start()));
100
101 //
102 // Create the dbus object to watch
103 //
104 QDBusPendingReply<QDBusObjectPath> reply = d->m_watchManagerInterface->watch( uris, props, types_ );
105 QDBusObjectPath path = reply.value();
106
107 if(!path.path().isEmpty()) {
108 d->m_connectionInterface = new org::kde::nepomuk::ResourceWatcherConnection( "org.kde.nepomuk.DataManagement",
109 path.path(),
110 QDBusConnection::sessionBus() );
111 connect( d->m_connectionInterface, SIGNAL(propertyAdded(QString,QString,QVariantList)),
112 this, SLOT(slotPropertyAdded(QString,QString,QVariantList)) );
113 connect( d->m_connectionInterface, SIGNAL(propertyRemoved(QString,QString,QVariantList)),
114 this, SLOT(slotPropertyRemoved(QString,QString,QVariantList)) );
115 connect( d->m_connectionInterface, SIGNAL(resourceCreated(QString,QStringList)),
116 this, SLOT(slotResourceCreated(QString,QStringList)) );
117 connect( d->m_connectionInterface, SIGNAL(propertyChanged(QString,QString,QVariantList,QVariantList)),
118 this, SLOT(slotPropertyChanged(QString,QString,QVariantList,QVariantList)) );
119 connect( d->m_connectionInterface, SIGNAL(resourceRemoved(QString,QStringList)),
120 this, SLOT(slotResourceRemoved(QString,QStringList)) );
121 connect( d->m_connectionInterface, SIGNAL(resourceTypesAdded(QString,QStringList)),
122 this, SLOT(slotResourceTypesAdded(QString,QStringList)) );
123 connect( d->m_connectionInterface, SIGNAL(resourceTypesRemoved(QString,QStringList)),
124 this, SLOT(slotResourceTypesRemoved(QString,QStringList)) );
125
126 kDebug() << "Successfully connected to watch service";
127 return true;
128 }
129 else {
130 kDebug() << "Failed to connect to watch service" << reply.error().message();
131 return false;
132 }
133 }
134
135 void Nepomuk::ResourceWatcher::stop()
136 {
137 if (d->m_connectionInterface) {
138 d->m_connectionInterface->close();
139 delete d->m_connectionInterface;
140 d->m_connectionInterface = 0;
141 }
142
143 disconnect(ResourceManager::instance(), SIGNAL(nepomukSystemStarted()), this, SLOT(start()));
144 }
145
146 void Nepomuk::ResourceWatcher::addProperty(const Nepomuk::Types::Property& property)
147 {
148 d->m_properties << property.uri();
149 if(d->m_connectionInterface) {
150 d->m_connectionInterface->addProperty(convertUri(property.uri()));
151 }
152 }
153
154 void Nepomuk::ResourceWatcher::addResource(const Nepomuk::Resource& res)
155 {
156 d->m_resources << res.resourceUri();
157 if(d->m_connectionInterface) {
158 d->m_connectionInterface->addResource(convertUri(res.resourceUri()));
159 }
160 }
161
162 void Nepomuk::ResourceWatcher::addType(const Nepomuk::Types::Class& type)
163 {
164 d->m_types << type.uri();
165 if(d->m_connectionInterface) {
166 d->m_connectionInterface->addType(convertUri(type.uri()));
167 }
168 }
169
170 void Nepomuk::ResourceWatcher::removeProperty(const Nepomuk::Types::Property& property)
171 {
172 d->m_properties.removeAll(property.uri());
173 if(d->m_connectionInterface) {
174 d->m_connectionInterface->removeProperty(convertUri(property.uri()));
175 }
176 }
177
178 void Nepomuk::ResourceWatcher::removeResource(const Nepomuk::Resource& res)
179 {
180 d->m_resources.removeAll(res.resourceUri());
181 if(d->m_connectionInterface) {
182 d->m_connectionInterface->removeResource(convertUri(res.resourceUri()));
183 }
184 }
185
186 void Nepomuk::ResourceWatcher::removeType(const Nepomuk::Types::Class& type)
187 {
188 d->m_types.removeAll(type.uri());
189 if(d->m_connectionInterface) {
190 d->m_connectionInterface->removeType(convertUri(type.uri()));
191 }
192 }
193
194 QList< Nepomuk::Types::Property > Nepomuk::ResourceWatcher::properties() const
195 {
196 QList< Nepomuk::Types::Property > props;
197 foreach(const QUrl& uri, d->m_properties)
198 props << Types::Property(uri);
199 return props;
200 }
201
202 QList<Nepomuk::Resource> Nepomuk::ResourceWatcher::resources() const
203 {
204 QList<Nepomuk::Resource> resources;
205 foreach(const QUrl& uri, d->m_resources)
206 resources << Resource::fromResourceUri(uri);
207 return resources;
208 }
209
210 QList< Nepomuk::Types::Class > Nepomuk::ResourceWatcher::types() const
211 {
212 QList<Nepomuk::Types::Class> types;
213 foreach(const QUrl& uri, d->m_types)
214 types << Types::Class(uri);
215 return types;
216 }
217
218 void Nepomuk::ResourceWatcher::setProperties(const QList< Nepomuk::Types::Property >& properties_)
219 {
220 d->m_properties.clear();
221 foreach(const Nepomuk::Types::Property& p, properties_) {
222 d->m_properties << p.uri();
223 }
224
225 if(d->m_connectionInterface) {
226 d->m_connectionInterface->setProperties(convertUris(d->m_properties));
227 }
228 }
229
230 void Nepomuk::ResourceWatcher::setResources(const QList< Nepomuk::Resource >& resources_)
231 {
232 d->m_resources.clear();
233 foreach(const Nepomuk::Resource& res, resources_) {
234 d->m_resources << res.resourceUri();
235 }
236
237 if(d->m_connectionInterface) {
238 d->m_connectionInterface->setResources(convertUris(d->m_resources));
239 }
240 }
241
242 void Nepomuk::ResourceWatcher::setTypes(const QList< Nepomuk::Types::Class >& types_)
243 {
244 d->m_types.clear();
245 foreach(const Nepomuk::Types::Class& t, types_) {
246 d->m_types << t.uri();
247 }
248
249 if(d->m_connectionInterface) {
250 d->m_connectionInterface->setTypes(convertUris(d->m_types));
251 }
252 }
253
254 void Nepomuk::ResourceWatcher::slotResourceCreated(const QString &res, const QStringList &types)
255 {
256 emit resourceCreated(Nepomuk::Resource::fromResourceUri(KUrl(res)), convertUris(types));
257 }
258
259 void Nepomuk::ResourceWatcher::slotResourceRemoved(const QString &res, const QStringList &types)
260 {
261 emit resourceRemoved(KUrl(res), convertUris(types));
262 }
263
264 void Nepomuk::ResourceWatcher::slotResourceTypesAdded(const QString &res, const QStringList &types)
265 {
266 foreach(const QString& type, types) {
267 emit resourceTypeAdded(KUrl(res), KUrl(type));
268 }
269 }
270
271 void Nepomuk::ResourceWatcher::slotResourceTypesRemoved(const QString &res, const QStringList &types)
272 {
273 foreach(const QString& type, types) {
274 emit resourceTypeRemoved(KUrl(res), KUrl(type));
275 }
276 }
277
278 void Nepomuk::ResourceWatcher::slotPropertyAdded(const QString& res, const QString& prop, const QVariantList &objects)
279 {
280 foreach(const QVariant& v, objects) {
281 emit propertyAdded( Resource::fromResourceUri(KUrl(res)), Types::Property( KUrl(prop) ), v );
282 }
283 }
284
285 void Nepomuk::ResourceWatcher::slotPropertyRemoved(const QString& res, const QString& prop, const QVariantList &objects)
286 {
287 foreach(const QVariant& v, objects) {
288 emit propertyRemoved( Resource::fromResourceUri(KUrl(res)), Types::Property( KUrl(prop) ), v );
289 }
290 }
291
292 void Nepomuk::ResourceWatcher::slotPropertyChanged(const QString& res, const QString& prop, const QVariantList& oldObjs, const QVariantList& newObjs)
293 {
294 emit propertyChanged( Resource::fromResourceUri(KUrl(res)), Types::Property( KUrl(prop) ),
295 oldObjs, newObjs );
296 }
297
298 #include "resourcewatcher.moc"
299