• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • sources
  • kde-4.12
  • kdelibs
  • nepomuk-core
  • libnepomukcore
  • datamanagement
resourcewatcher.cpp
Go to the documentation of this file.
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 #include "resourcemanager.h"
26 
27 #include "resource.h"
28 #include "variant.h"
29 #include "property.h"
30 #include "literal.h"
31 
32 #include <QtDBus>
33 
34 #include <KUrl>
35 #include <KDebug>
36 #include <Soprano/Vocabulary/RDFS>
37 
38 using namespace Soprano::Vocabulary;
39 
40 namespace {
41  QString convertUri(const QUrl& uri) {
42  return KUrl(uri).url();
43  }
44 
45  QStringList convertUris(const QList<QUrl>& uris) {
46  QStringList cs;
47  foreach(const QUrl& uri, uris) {
48  cs << convertUri(uri);
49  }
50  return cs;
51  }
52 
53  QList<QUrl> convertUris(const QStringList& uris) {
54  QList<QUrl> us;
55  foreach(const QString& uri, uris) {
56  us << KUrl(uri);
57  }
58  return us;
59  }
60 }
61 
62 class Nepomuk2::ResourceWatcher::Private {
63 public:
64  QList<QUrl> m_types;
65  QList<QUrl> m_resources;
66  QList<QUrl> m_properties;
67 
68  org::kde::nepomuk::ResourceWatcherConnection * m_connectionInterface;
69  org::kde::nepomuk::ResourceWatcher * m_watchManagerInterface;
70 };
71 
72 Nepomuk2::ResourceWatcher::ResourceWatcher(QObject* parent)
73  : QObject(parent),
74  d(new Private)
75 {
76  d->m_watchManagerInterface
77  = new org::kde::nepomuk::ResourceWatcher( "org.kde.NepomukStorage",
78  "/resourcewatcher",
79  QDBusConnection::sessionBus() );
80  d->m_connectionInterface = 0;
81 }
82 
83 Nepomuk2::ResourceWatcher::~ResourceWatcher()
84 {
85  stop();
86  delete d;
87 }
88 
89 bool Nepomuk2::ResourceWatcher::start()
90 {
91  stop();
92 
93  //
94  // Convert to list of strings
95  //
96  QList<QString> uris = convertUris(d->m_resources);
97  QList<QString> props = convertUris(d->m_properties);
98  QList<QString> types_ = convertUris(d->m_types);
99 
100  //
101  // Watch for the RW service to (re-)appear and then re-connect to make sure we always get updates
102  // We create this watcher even if we fail to connect below. Thus, once the rw service comes up we
103  // can re-attach.
104  //
105  connect(ResourceManager::instance(), SIGNAL(nepomukSystemStarted()), this, SLOT(start()));
106 
107  //
108  // Create the dbus object to watch
109  //
110  QDBusPendingReply<QDBusObjectPath> reply = d->m_watchManagerInterface->watch( uris, props, types_ );
111  QDBusPendingCallWatcher* replyWatcher = new QDBusPendingCallWatcher( reply, this );
112  connect( replyWatcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
113  this, SLOT(slotWatchFinished(QDBusPendingCallWatcher*)) );
114 
115  // Always return true for now
116  // FIXME: Change the return value for frameworks 5
117  return true;
118 }
119 
120 void Nepomuk2::ResourceWatcher::slotWatchFinished(QDBusPendingCallWatcher* replyWatcher)
121 {
122  QDBusPendingReply<QDBusObjectPath> reply = *replyWatcher;
123  if( reply.isError() ) {
124  kDebug() << "Failed to connect to watch service" << reply.error().message();
125  return;
126  }
127 
128  QDBusObjectPath path = reply.value();
129 
130  if(!path.path().isEmpty()) {
131  d->m_connectionInterface = new org::kde::nepomuk::ResourceWatcherConnection( "org.kde.NepomukStorage",
132  path.path(),
133  QDBusConnection::sessionBus() );
134  connect( d->m_connectionInterface, SIGNAL(propertyChanged(QString,QString,QVariantList,QVariantList)),
135  this, SLOT(slotPropertyChanged(QString,QString,QVariantList,QVariantList)) );
136  connect( d->m_connectionInterface, SIGNAL(resourceCreated(QString,QStringList)),
137  this, SLOT(slotResourceCreated(QString,QStringList)) );
138  connect( d->m_connectionInterface, SIGNAL(resourceRemoved(QString,QStringList)),
139  this, SLOT(slotResourceRemoved(QString,QStringList)) );
140  connect( d->m_connectionInterface, SIGNAL(resourceTypesAdded(QString,QStringList)),
141  this, SLOT(slotResourceTypesAdded(QString,QStringList)) );
142  connect( d->m_connectionInterface, SIGNAL(resourceTypesRemoved(QString,QStringList)),
143  this, SLOT(slotResourceTypesRemoved(QString,QStringList)) );
144 
145  foreach(const QUrl& uri, d->m_resources) {
146  d->m_connectionInterface->addResource(convertUri(uri));
147  }
148 
149  foreach(const QUrl& uri, d->m_properties) {
150  d->m_connectionInterface->addProperty(convertUri(uri));
151  }
152 
153  foreach(const QUrl& uri, d->m_types) {
154  d->m_connectionInterface->addType(convertUri(uri));
155  }
156  }
157  else {
158  kDebug() << "Failed to connect to watch service" << reply.error().message();
159  }
160 }
161 
162 void Nepomuk2::ResourceWatcher::stop()
163 {
164  if (d->m_connectionInterface) {
165  d->m_connectionInterface->close();
166  delete d->m_connectionInterface;
167  d->m_connectionInterface = 0;
168  }
169 
170  // This is for the case when ~ResourceManager removes its ResourceWatcher,
171  // which in turn calls stop().
172  if( ResourceManager::instance() )
173  disconnect(ResourceManager::instance(), SIGNAL(nepomukSystemStarted()), this, SLOT(start()));
174 }
175 
176 void Nepomuk2::ResourceWatcher::addProperty(const Nepomuk2::Types::Property& property)
177 {
178  d->m_properties << property.uri();
179  if(d->m_connectionInterface) {
180  d->m_connectionInterface->addProperty(convertUri(property.uri()));
181  }
182 }
183 
184 void Nepomuk2::ResourceWatcher::addResource(const Nepomuk2::Resource& res)
185 {
186  addResource(res.uri());
187 }
188 
189 void Nepomuk2::ResourceWatcher::addResource(const QUrl& resUri)
190 {
191  d->m_resources << resUri;
192  if(d->m_connectionInterface) {
193  d->m_connectionInterface->addResource(convertUri(resUri));
194  }
195 }
196 
197 
198 void Nepomuk2::ResourceWatcher::addType(const Nepomuk2::Types::Class& type)
199 {
200  d->m_types << type.uri();
201  if(d->m_connectionInterface) {
202  d->m_connectionInterface->addType(convertUri(type.uri()));
203  }
204 }
205 
206 void Nepomuk2::ResourceWatcher::removeProperty(const Nepomuk2::Types::Property& property)
207 {
208  d->m_properties.removeAll(property.uri());
209  if(d->m_connectionInterface) {
210  d->m_connectionInterface->removeProperty(convertUri(property.uri()));
211  }
212 }
213 
214 void Nepomuk2::ResourceWatcher::removeResource(const Nepomuk2::Resource& res)
215 {
216  removeResource(res.uri());
217 }
218 
219 void Nepomuk2::ResourceWatcher::removeResource(const QUrl& resUri)
220 {
221  d->m_resources.removeAll(resUri);
222  if(d->m_connectionInterface) {
223  d->m_connectionInterface->removeResource(convertUri(resUri));
224  }
225 }
226 
227 void Nepomuk2::ResourceWatcher::removeType(const Nepomuk2::Types::Class& type)
228 {
229  d->m_types.removeAll(type.uri());
230  if(d->m_connectionInterface) {
231  d->m_connectionInterface->removeType(convertUri(type.uri()));
232  }
233 }
234 
235 QList< Nepomuk2::Types::Property > Nepomuk2::ResourceWatcher::properties() const
236 {
237  QList< Nepomuk2::Types::Property > props;
238  foreach(const QUrl& uri, d->m_properties)
239  props << Types::Property(uri);
240  return props;
241 }
242 
243 QList<Nepomuk2::Resource> Nepomuk2::ResourceWatcher::resources() const
244 {
245  QList<Nepomuk2::Resource> resources;
246  foreach(const QUrl& uri, d->m_resources)
247  resources << Resource::fromResourceUri(uri);
248  return resources;
249 }
250 
251 QList< Nepomuk2::Types::Class > Nepomuk2::ResourceWatcher::types() const
252 {
253  QList<Nepomuk2::Types::Class> types;
254  foreach(const QUrl& uri, d->m_types)
255  types << Types::Class(uri);
256  return types;
257 }
258 
259 int Nepomuk2::ResourceWatcher::propertyCount() const
260 {
261  return d->m_properties.size();
262 }
263 
264 int Nepomuk2::ResourceWatcher::resourceCount() const
265 {
266  return d->m_resources.size();
267 }
268 
269 int Nepomuk2::ResourceWatcher::typeCount() const
270 {
271  return d->m_types.size();
272 }
273 
274 
275 void Nepomuk2::ResourceWatcher::setProperties(const QList< Nepomuk2::Types::Property >& properties_)
276 {
277  d->m_properties.clear();
278  foreach(const Nepomuk2::Types::Property& p, properties_) {
279  d->m_properties << p.uri();
280  }
281 
282  if(d->m_connectionInterface) {
283  d->m_connectionInterface->setProperties(convertUris(d->m_properties));
284  }
285 }
286 
287 void Nepomuk2::ResourceWatcher::setResources(const QList< Nepomuk2::Resource >& resources_)
288 {
289  d->m_resources.clear();
290  foreach(const Nepomuk2::Resource& res, resources_) {
291  d->m_resources << res.uri();
292  }
293 
294  if(d->m_connectionInterface) {
295  d->m_connectionInterface->setResources(convertUris(d->m_resources));
296  }
297 }
298 
299 void Nepomuk2::ResourceWatcher::setTypes(const QList< Nepomuk2::Types::Class >& types_)
300 {
301  d->m_types.clear();
302  foreach(const Nepomuk2::Types::Class& t, types_) {
303  d->m_types << t.uri();
304  }
305 
306  if(d->m_connectionInterface) {
307  d->m_connectionInterface->setTypes(convertUris(d->m_types));
308  }
309 }
310 
311 void Nepomuk2::ResourceWatcher::slotResourceCreated(const QString &res, const QStringList &types)
312 {
313  emit resourceCreated(Nepomuk2::Resource::fromResourceUri(KUrl(res)), convertUris(types));
314 }
315 
316 void Nepomuk2::ResourceWatcher::slotResourceRemoved(const QString &res, const QStringList &types)
317 {
318  emit resourceRemoved(KUrl(res), convertUris(types));
319 }
320 
321 void Nepomuk2::ResourceWatcher::slotResourceTypesAdded(const QString &res, const QStringList &types)
322 {
323  foreach(const QString& type, types) {
324  emit resourceTypeAdded(KUrl(res), KUrl(type));
325  }
326 }
327 
328 void Nepomuk2::ResourceWatcher::slotResourceTypesRemoved(const QString &res, const QStringList &types)
329 {
330  foreach(const QString& type, types) {
331  emit resourceTypeRemoved(KUrl(res), KUrl(type));
332  }
333 }
334 
335 namespace {
336  QVariant convertType(const Nepomuk2::Types::Property& prop, const QVariant& v) {
337  QVariant var(v);
338  if( !prop.literalRangeType().isValid() && var.type() == QVariant::String ) {
339  var.setValue<QUrl>(QUrl(var.toString()));
340  }
341 
342  return var;
343  }
344 }
345 
346 void Nepomuk2::ResourceWatcher::slotPropertyChanged(const QString& res_, const QString& prop_, const QVariantList& addedObjs, const QVariantList& removedObjs)
347 {
348  const Resource res = Resource::fromResourceUri(KUrl(res_));
349  const Types::Property prop = KUrl(prop_);
350 
351  foreach( const QVariant& v, addedObjs ) {
352  emit propertyAdded( res, prop, convertType(prop, v) );
353  }
354 
355  foreach( const QVariant& v, removedObjs ) {
356  emit propertyRemoved( res, prop, convertType(prop, v) );
357  }
358 
359  emit propertyChanged( res, prop, addedObjs, removedObjs );
360 }
361 
362 #include "resourcewatcher.moc"
363 
Nepomuk2::Types::Literal::isValid
bool isValid() const
Is this a valid Literal, i.e.
Definition: literal.cpp:124
Nepomuk2::ResourceWatcher::resources
QList< Nepomuk2::Resource > resources() const
The resources that have been configured via addResource() and setResources().
Definition: resourcewatcher.cpp:243
Nepomuk2::ResourceWatcher::properties
QList< Types::Property > properties() const
The properties that have been configured via addProperty() and setProperties().
Definition: resourcewatcher.cpp:235
Nepomuk2::ResourceWatcher::setProperties
void setProperties(const QList< Types::Property > &properties_)
Set the properties to be watched.
Definition: resourcewatcher.cpp:275
Nepomuk2::ResourceWatcher::addType
void addType(const Types::Class &type)
Add a type to be watched.
Definition: resourcewatcher.cpp:198
Nepomuk2::Types::Entity::uri
QUrl uri() const
The URI of the resource.
Definition: entity.cpp:175
Nepomuk2::ResourceWatcher::removeResource
void removeResource(const Nepomuk2::Resource &res)
Remove a resource to be watched.
Definition: resourcewatcher.cpp:214
Nepomuk2::ResourceWatcher::setResources
void setResources(const QList< Nepomuk2::Resource > &resources_)
Set the resources to be watched.
Definition: resourcewatcher.cpp:287
Nepomuk2::ResourceWatcher::types
QList< Types::Class > types() const
The types that have been configured via addType() and setTypes().
Definition: resourcewatcher.cpp:251
Nepomuk2::ResourceWatcher::addProperty
void addProperty(const Types::Property &property)
Add a property to be watched.
Definition: resourcewatcher.cpp:176
QObject
Nepomuk2::Types::Property::literalRangeType
Literal literalRangeType()
If the rage of this property is a literal (i.e.
Definition: libnepomukcore/types/property.cpp:271
variant.h
Nepomuk2::ResourceWatcher::propertyCount
int propertyCount() const
Return the number of properties that are being watched.
Definition: resourcewatcher.cpp:259
Nepomuk2::ResourceWatcher::start
bool start()
Start the signalling of changes.
Definition: resourcewatcher.cpp:89
Nepomuk2::DBus::convertUri
QString convertUri(const QUrl &uri)
Definition: dbustypes.cpp:33
Nepomuk2::Types::Class
A Class is a resource of type rdf:Class.
Definition: class.h:49
resource.h
Nepomuk2::Types::Property
A property is a resource of type rdf:Property which relates a domain with a range.
Definition: libnepomukcore/types/property.h:52
Nepomuk2::ResourceManager::instance
static ResourceManager * instance()
Definition: resourcemanager.cpp:270
Nepomuk2::Resource::fromResourceUri
static Resource fromResourceUri(const KUrl &uri, const Nepomuk2::Types::Class &type=Nepomuk2::Types::Class())
Allows to quickly load a resource from its resource URI without any additional checks.
Definition: resource.cpp:743
resourcemanager.h
Nepomuk2::ResourceWatcher::~ResourceWatcher
virtual ~ResourceWatcher()
Destructor.
Definition: resourcewatcher.cpp:83
Nepomuk2::ResourceWatcher::removeProperty
void removeProperty(const Types::Property &property)
Remove a property to be watched.
Definition: resourcewatcher.cpp:206
Nepomuk2::ResourceWatcher::removeType
void removeType(const Types::Class &type)
Remove a type to be watched.
Definition: resourcewatcher.cpp:227
Nepomuk2::ResourceWatcher::addResource
void addResource(const Nepomuk2::Resource &res)
Add a resource to be watched.
Definition: resourcewatcher.cpp:184
Nepomuk2::Resource
Resource is the central object type in Nepomuk.
Definition: resource.h:93
resourcewatcher.h
Nepomuk2::ResourceWatcher::setTypes
void setTypes(const QList< Types::Class > &types_)
Set the types to be watched.
Definition: resourcewatcher.cpp:299
start
void start()
Definition: ontologydownloadjob.cpp:13
Nepomuk2::ResourceWatcher::stop
void stop()
Stop the signalling of changes.
Definition: resourcewatcher.cpp:162
Nepomuk2::ResourceWatcher::resourceCount
int resourceCount() const
Return the number of resources that are being watched.
Definition: resourcewatcher.cpp:264
Nepomuk2::Resource::uri
QUrl uri() const
The URI of the resource, uniquely identifying it.
Definition: resource.cpp:166
literal.h
Nepomuk2::ResourceWatcher::typeCount
int typeCount() const
Return the number of types that are being watched.
Definition: resourcewatcher.cpp:269
Nepomuk2::ResourceWatcher::ResourceWatcher
ResourceWatcher(QObject *parent=0)
Create a new ResourceWatcher instance.
Definition: resourcewatcher.cpp:72
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:09 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Nepomuk-Core

Skip menu "Nepomuk-Core"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal