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>
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.
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.
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
22 #include "resourcewatcher.h"
23 #include "resourcewatcherconnectioninterface.h"
24 #include "resourcewatchermanagerinterface.h"
26 #include <QtDBus/QDBusObjectPath>
28 #include <nepomuk/resource.h>
29 #include <nepomuk/resourcemanager.h>
35 QString
convertUri(const QUrl
& uri
) {
36 return KUrl(uri
).url();
39 QStringList
convertUris(const QList
<QUrl
>& uris
) {
41 foreach(const QUrl
& uri
, uris
) {
42 cs
<< convertUri(uri
);
47 QList
<QUrl
> convertUris(const QStringList
& uris
) {
49 foreach(const QString
& uri
, uris
) {
56 class Nepomuk::ResourceWatcher::Private
{
59 QList
<QUrl
> m_resources
;
60 QList
<QUrl
> m_properties
;
62 org::kde::nepomuk::ResourceWatcherConnection
* m_connectionInterface
;
63 org::kde::nepomuk::ResourceWatcher
* m_watchManagerInterface
;
66 Nepomuk::ResourceWatcher::ResourceWatcher(QObject
* parent
)
70 d
->m_watchManagerInterface
71 = new org::kde::nepomuk::ResourceWatcher( "org.kde.nepomuk.DataManagement",
73 QDBusConnection::sessionBus() );
74 d
->m_connectionInterface
= 0;
77 Nepomuk::ResourceWatcher::~ResourceWatcher()
83 bool Nepomuk::ResourceWatcher::start()
88 // Convert to list of strings
90 QList
<QString
> uris
= convertUris(d
->m_resources
);
91 QList
<QString
> props
= convertUris(d
->m_properties
);
92 QList
<QString
> types_
= convertUris(d
->m_types
);
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
99 connect(ResourceManager::instance(), SIGNAL(nepomukSystemStarted()), this, SLOT(start()));
102 // Create the dbus object to watch
104 QDBusPendingReply
<QDBusObjectPath
> reply
= d
->m_watchManagerInterface
->watch( uris
, props
, types_
);
105 QDBusObjectPath path
= reply
.value();
107 if(!path
.path().isEmpty()) {
108 d
->m_connectionInterface
= new org::kde::nepomuk::ResourceWatcherConnection( "org.kde.nepomuk.DataManagement",
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
)) );
126 kDebug() << "Successfully connected to watch service";
130 kDebug() << "Failed to connect to watch service" << reply
.error().message();
135 void Nepomuk::ResourceWatcher::stop()
137 if (d
->m_connectionInterface
) {
138 d
->m_connectionInterface
->close();
139 delete d
->m_connectionInterface
;
140 d
->m_connectionInterface
= 0;
143 disconnect(ResourceManager::instance(), SIGNAL(nepomukSystemStarted()), this, SLOT(start()));
146 void Nepomuk::ResourceWatcher::addProperty(const Nepomuk::Types::Property
& property
)
148 d
->m_properties
<< property
.uri();
149 if(d
->m_connectionInterface
) {
150 d
->m_connectionInterface
->addProperty(convertUri(property
.uri()));
154 void Nepomuk::ResourceWatcher::addResource(const Nepomuk::Resource
& res
)
156 d
->m_resources
<< res
.resourceUri();
157 if(d
->m_connectionInterface
) {
158 d
->m_connectionInterface
->addResource(convertUri(res
.resourceUri()));
162 void Nepomuk::ResourceWatcher::addType(const Nepomuk::Types::Class
& type
)
164 d
->m_types
<< type
.uri();
165 if(d
->m_connectionInterface
) {
166 d
->m_connectionInterface
->addType(convertUri(type
.uri()));
170 void Nepomuk::ResourceWatcher::removeProperty(const Nepomuk::Types::Property
& property
)
172 d
->m_properties
.removeAll(property
.uri());
173 if(d
->m_connectionInterface
) {
174 d
->m_connectionInterface
->removeProperty(convertUri(property
.uri()));
178 void Nepomuk::ResourceWatcher::removeResource(const Nepomuk::Resource
& res
)
180 d
->m_resources
.removeAll(res
.resourceUri());
181 if(d
->m_connectionInterface
) {
182 d
->m_connectionInterface
->removeResource(convertUri(res
.resourceUri()));
186 void Nepomuk::ResourceWatcher::removeType(const Nepomuk::Types::Class
& type
)
188 d
->m_types
.removeAll(type
.uri());
189 if(d
->m_connectionInterface
) {
190 d
->m_connectionInterface
->removeType(convertUri(type
.uri()));
194 QList
< Nepomuk::Types::Property
> Nepomuk::ResourceWatcher::properties() const
196 QList
< Nepomuk::Types::Property
> props
;
197 foreach(const QUrl
& uri
, d
->m_properties
)
198 props
<< Types::Property(uri
);
202 QList
<Nepomuk::Resource
> Nepomuk::ResourceWatcher::resources() const
204 QList
<Nepomuk::Resource
> resources
;
205 foreach(const QUrl
& uri
, d
->m_resources
)
206 resources
<< Resource::fromResourceUri(uri
);
210 QList
< Nepomuk::Types::Class
> Nepomuk::ResourceWatcher::types() const
212 QList
<Nepomuk::Types::Class
> types
;
213 foreach(const QUrl
& uri
, d
->m_types
)
214 types
<< Types::Class(uri
);
218 void Nepomuk::ResourceWatcher::setProperties(const QList
< Nepomuk::Types::Property
>& properties_
)
220 d
->m_properties
.clear();
221 foreach(const Nepomuk::Types::Property
& p
, properties_
) {
222 d
->m_properties
<< p
.uri();
225 if(d
->m_connectionInterface
) {
226 d
->m_connectionInterface
->setProperties(convertUris(d
->m_properties
));
230 void Nepomuk::ResourceWatcher::setResources(const QList
< Nepomuk::Resource
>& resources_
)
232 d
->m_resources
.clear();
233 foreach(const Nepomuk::Resource
& res
, resources_
) {
234 d
->m_resources
<< res
.resourceUri();
237 if(d
->m_connectionInterface
) {
238 d
->m_connectionInterface
->setResources(convertUris(d
->m_resources
));
242 void Nepomuk::ResourceWatcher::setTypes(const QList
< Nepomuk::Types::Class
>& types_
)
245 foreach(const Nepomuk::Types::Class
& t
, types_
) {
246 d
->m_types
<< t
.uri();
249 if(d
->m_connectionInterface
) {
250 d
->m_connectionInterface
->setTypes(convertUris(d
->m_types
));
254 void Nepomuk::ResourceWatcher::slotResourceCreated(const QString
&res
, const QStringList
&types
)
256 emit
resourceCreated(Nepomuk::Resource::fromResourceUri(KUrl(res
)), convertUris(types
));
259 void Nepomuk::ResourceWatcher::slotResourceRemoved(const QString
&res
, const QStringList
&types
)
261 emit
resourceRemoved(KUrl(res
), convertUris(types
));
264 void Nepomuk::ResourceWatcher::slotResourceTypesAdded(const QString
&res
, const QStringList
&types
)
266 foreach(const QString
& type
, types
) {
267 emit
resourceTypeAdded(KUrl(res
), KUrl(type
));
271 void Nepomuk::ResourceWatcher::slotResourceTypesRemoved(const QString
&res
, const QStringList
&types
)
273 foreach(const QString
& type
, types
) {
274 emit
resourceTypeRemoved(KUrl(res
), KUrl(type
));
278 void Nepomuk::ResourceWatcher::slotPropertyAdded(const QString
& res
, const QString
& prop
, const QVariantList
&objects
)
280 foreach(const QVariant
& v
, objects
) {
281 emit
propertyAdded( Resource::fromResourceUri(KUrl(res
)), Types::Property( KUrl(prop
) ), v
);
285 void Nepomuk::ResourceWatcher::slotPropertyRemoved(const QString
& res
, const QString
& prop
, const QVariantList
&objects
)
287 foreach(const QVariant
& v
, objects
) {
288 emit
propertyRemoved( Resource::fromResourceUri(KUrl(res
)), Types::Property( KUrl(prop
) ), v
);
292 void Nepomuk::ResourceWatcher::slotPropertyChanged(const QString
& res
, const QString
& prop
, const QVariantList
& oldObjs
, const QVariantList
& newObjs
)
294 emit
propertyChanged( Resource::fromResourceUri(KUrl(res
)), Types::Property( KUrl(prop
) ),
298 #include "resourcewatcher.moc"