• 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
  • query
queryserviceclient.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2008-2009 Sebastian Trueg <trueg@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) version 3, or any
8  later version accepted by the membership of KDE e.V. (or its
9  successor approved by the membership of KDE e.V.), which shall
10  act as a proxy defined in Section 6 of version 3 of the license.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "queryserviceclient.h"
22 #include "queryserviceclient_p.h"
23 #include "dbusoperators_p.h"
24 #include "result.h"
25 #include "query.h"
26 #include "queryserviceinterface.h"
27 #include "queryinterface.h"
28 #include "kdbusconnectionpool.h"
29 
30 #include <QtDBus/QDBusInterface>
31 #include <QtDBus/QDBusConnection>
32 #include <QtDBus/QDBusConnectionInterface>
33 #include <QtDBus/QDBusReply>
34 
35 #include <QtCore/QEventLoop>
36 #include <QtCore/QTimer>
37 
38 #include <kdebug.h>
39 #include <kglobal.h>
40 
41 
42 namespace {
44  QHash<QString, QString> encodeRequestProperties( const QList<Nepomuk2::Query::Query::RequestProperty>& rps )
45  {
46  QHash<QString, QString> encodedRps;
47  int i = 1;
48  foreach( const Nepomuk2::Query::Query::RequestProperty& rp, rps ) {
49  encodedRps.insert( QString( "reqProp%1" ).arg( i++ ), KUrl( rp.property().uri() ).url() );
50  }
51  return encodedRps;
52  }
53 
55  QHash<QString, QString> encodeRequestProperties( const QHash<QString, Nepomuk2::Types::Property>& rps )
56  {
57  QHash<QString, QString> encodedRps;
58  for( QHash<QString, Nepomuk2::Types::Property>::const_iterator it = rps.constBegin();
59  it != rps.constEnd(); ++it ) {
60  encodedRps.insert( it.key(), KUrl( it.value().uri() ).url() );
61  }
62  return encodedRps;
63  }
64 
65  NepomukResultListEventLoop::NepomukResultListEventLoop(Nepomuk2::Query::QueryServiceClient* parent)
66  : QEventLoop(parent)
67  {
68  connect(parent, SIGNAL(newEntries(QList<Nepomuk2::Query::Result>)),
69  this, SLOT(addEntries(QList<Nepomuk2::Query::Result>)));
70  }
71 
72  NepomukResultListEventLoop::~NepomukResultListEventLoop()
73  {
74  }
75 
76  void NepomukResultListEventLoop::addEntries(const QList< Nepomuk2::Query::Result >& entries)
77  {
78  m_result << entries;
79  }
80 
81  QList< Nepomuk2::Query::Result > NepomukResultListEventLoop::result() const
82  {
83  return m_result;
84  }
85 
86 }
87 
88 
89 class Nepomuk2::Query::QueryServiceClient::Private
90 {
91 public:
92  Private()
93  : queryServiceInterface( 0 ),
94  queryInterface( 0 ),
95  dbusConnection( KDBusConnectionPool::threadConnection() ),
96  m_queryActive( false ),
97  loop( 0 ) {
98  }
99 
100  void _k_entriesRemoved( const QStringList& );
101  void _k_finishedListing();
102  void _k_handleQueryReply(QDBusPendingCallWatcher*);
103  void _k_serviceRegistered( const QString& );
104  void _k_serviceUnregistered( const QString& );
105 
106  org::kde::nepomuk::QueryService* queryServiceInterface;
107  org::kde::nepomuk::Query* queryInterface;
108  QDBusServiceWatcher *queryServiceWatcher;
109 
110  QueryServiceClient* q;
111 
112  QPointer<QDBusPendingCallWatcher> m_pendingCallWatcher;
113 
114  QDBusConnection dbusConnection;
115 
116  bool m_queryActive;
117  QEventLoop* loop;
118  QString m_errorMessage;
119 };
120 
121 
122 void Nepomuk2::Query::QueryServiceClient::Private::_k_entriesRemoved( const QStringList& uris )
123 {
124  QList<QUrl> ul;
125  foreach( const QString& s, uris ) {
126  ul.append( QUrl( s ) );
127  }
128  emit q->entriesRemoved( ul );
129 }
130 
131 
132 void Nepomuk2::Query::QueryServiceClient::Private::_k_finishedListing()
133 {
134  m_queryActive = false;
135  emit q->finishedListing();
136  if( loop ) {
137  q->close();
138  }
139 }
140 
141 
142 void Nepomuk2::Query::QueryServiceClient::Private::_k_handleQueryReply(QDBusPendingCallWatcher* watcher)
143 {
144  QDBusPendingReply<QDBusObjectPath> reply = *watcher;
145  if(reply.isError()) {
146  kDebug() << reply.error();
147  m_errorMessage = reply.error().message();
148  m_queryActive = false;
149  emit q->error(m_errorMessage);
150  if( loop ) {
151  loop->exit();
152  }
153  }
154  else {
155  queryInterface = new org::kde::nepomuk::Query( queryServiceInterface->service(),
156  reply.value().path(),
157  dbusConnection );
158  connect( queryInterface, SIGNAL( newEntries( QList<Nepomuk2::Query::Result> ) ),
159  q, SIGNAL( newEntries( QList<Nepomuk2::Query::Result> ) ) );
160  connect( queryInterface, SIGNAL( resultCount(int) ),
161  q, SIGNAL( resultCount(int) ) );
162  connect( queryInterface, SIGNAL( entriesRemoved( QStringList ) ),
163  q, SLOT( _k_entriesRemoved( QStringList ) ) );
164  connect( queryInterface, SIGNAL( finishedListing() ),
165  q, SLOT( _k_finishedListing() ) );
166  // run the listing async in case the event loop below is the only one we have
167  // and we need it to handle the signals and list returns results immediately
168  QTimer::singleShot( 0, queryInterface, SLOT(list()) );
169  }
170 
171  delete watcher;
172 }
173 
174 
175 void Nepomuk2::Query::QueryServiceClient::Private::_k_serviceRegistered(const QString &service)
176 {
177  if (service == "org.kde.nepomuk.services.nepomukqueryservice") {
178  delete queryServiceInterface;
179  queryServiceInterface = new org::kde::nepomuk::QueryService( "org.kde.nepomuk.services.nepomukqueryservice",
180  "/nepomukqueryservice",
181  dbusConnection );
182  emit q->serviceAvailabilityChanged(true);
183  }
184 }
185 
186 
187 void Nepomuk2::Query::QueryServiceClient::Private::_k_serviceUnregistered(const QString &service)
188 {
189  if (service == "org.kde.nepomuk.services.nepomukqueryservice") {
190  q->close();
191  emit q->serviceAvailabilityChanged(false);
192  }
193 }
194 
195 
196 
197 
198 Nepomuk2::Query::QueryServiceClient::QueryServiceClient( QObject* parent )
199  : QObject( parent ),
200  d( new Private() )
201 {
202  d->q = this;
203 
204  Nepomuk2::Query::registerDBusTypes();
205 
206  // we use our own connection to be thread-safe
207  d->queryServiceInterface = new org::kde::nepomuk::QueryService( QLatin1String("org.kde.nepomuk.services.nepomukqueryservice"),
208  QLatin1String("/nepomukqueryservice"),
209  d->dbusConnection );
210  d->queryServiceWatcher = new QDBusServiceWatcher(QLatin1String("org.kde.nepomuk.services.nepomukqueryservice"),
211  QDBusConnection::sessionBus(),
212  QDBusServiceWatcher::WatchForOwnerChange,
213  this);
214  connect(d->queryServiceWatcher, SIGNAL(serviceRegistered(QString)), this, SLOT(_k_serviceRegistered(QString)));
215  connect(d->queryServiceWatcher, SIGNAL(serviceUnregistered(QString)), this, SLOT(_k_serviceUnregistered(QString)));
216 }
217 
218 
219 Nepomuk2::Query::QueryServiceClient::~QueryServiceClient()
220 {
221  close();
222  delete d->queryServiceInterface;
223  delete d;
224 }
225 
226 
227 bool Nepomuk2::Query::QueryServiceClient::query( const Query& query )
228 {
229  close();
230 
231  if ( d->queryServiceInterface->isValid() ) {
232  d->m_queryActive = true;
233  d->m_pendingCallWatcher = new QDBusPendingCallWatcher(d->queryServiceInterface->asyncCall(QLatin1String("query"),
234  query.toString()),
235  this);
236  connect(d->m_pendingCallWatcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
237  this, SLOT(_k_handleQueryReply(QDBusPendingCallWatcher*)));
238  return true;
239  }
240  else {
241  kDebug() << "Could not contact nepomuk query service.";
242  return false;
243  }
244 }
245 
246 
247 bool Nepomuk2::Query::QueryServiceClient::sparqlQuery( const QString& query, const QHash<QString, Nepomuk2::Types::Property>& requestPropertyMap )
248 {
249  close();
250 
251  if ( d->queryServiceInterface->isValid() ) {
252  d->m_queryActive = true;
253  d->m_pendingCallWatcher = new QDBusPendingCallWatcher(d->queryServiceInterface->asyncCall(QLatin1String("sparqlQuery"),
254  query,
255  QVariant::fromValue(encodeRequestProperties( requestPropertyMap ))),
256  this);
257  connect(d->m_pendingCallWatcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
258  this, SLOT(_k_handleQueryReply(QDBusPendingCallWatcher*)));
259  return true;
260  }
261  else {
262  kDebug() << "Could not contact nepomuk query service.";
263  return false;
264  }
265 }
266 
267 
268 bool Nepomuk2::Query::QueryServiceClient::desktopQuery( const QString& query )
269 {
270  close();
271 
272  if ( d->queryServiceInterface->isValid() ) {
273  d->m_queryActive = true;
274  d->m_pendingCallWatcher = new QDBusPendingCallWatcher(d->queryServiceInterface->asyncCall(QLatin1String("desktopQuery"),
275  query),
276  this);
277  connect(d->m_pendingCallWatcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
278  this, SLOT(_k_handleQueryReply(QDBusPendingCallWatcher*)));
279  return true;
280  }
281  else {
282  kDebug() << "Could not contact nepomuk query service.";
283  return false;
284  }
285 }
286 
287 
288 bool Nepomuk2::Query::QueryServiceClient::blockingQuery( const Query& q )
289 {
290  if( query( q ) ) {
291  QEventLoop loop;
292  d->loop = &loop;
293  loop.exec();
294  d->loop = 0;
295  close();
296  return true;
297  }
298  else {
299  return false;
300  }
301 }
302 
303 
304 QList< Nepomuk2::Query::Result > Nepomuk2::Query::QueryServiceClient::syncQuery(const Query& q, bool* ok)
305 {
306  QueryServiceClient qsc;
307  if( qsc.query( q ) ) {
308  NepomukResultListEventLoop loop(&qsc);
309  qsc.d->loop = &loop;
310  loop.exec();
311  qsc.d->loop = 0;
312  if (ok) {
313  *ok = !qsc.errorMessage().isEmpty();
314  }
315  return loop.result();
316  }
317  else {
318  if (ok) {
319  *ok = false;
320  }
321  return QList< Nepomuk2::Query::Result >();
322  }
323 }
324 
325 
326 bool Nepomuk2::Query::QueryServiceClient::blockingSparqlQuery( const QString& q, const QHash<QString, Nepomuk2::Types::Property>& requestPropertyMap )
327 {
328  if( sparqlQuery( q, requestPropertyMap ) ) {
329  QEventLoop loop;
330  d->loop = &loop;
331  loop.exec();
332  d->loop = 0;
333  close();
334  return true;
335  }
336  else {
337  return false;
338  }
339 }
340 
341 
342 QList< Nepomuk2::Query::Result > Nepomuk2::Query::QueryServiceClient::syncSparqlQuery(const QString& q,
343  const QHash<QString, Nepomuk2::Types::Property>& requestPropertyMap,
344  bool *ok)
345 {
346  QueryServiceClient qsc;
347  if( qsc.sparqlQuery( q, requestPropertyMap ) ) {
348  NepomukResultListEventLoop loop(&qsc);
349  qsc.d->loop = &loop;
350  loop.exec();
351  qsc.d->loop = 0;
352  if (ok) {
353  *ok = !qsc.errorMessage().isEmpty();
354  }
355  return loop.result();
356  }
357  else {
358  if (ok) {
359  *ok = false;
360  }
361  return QList< Nepomuk2::Query::Result >();
362  }
363 }
364 
365 
366 bool Nepomuk2::Query::QueryServiceClient::blockingDesktopQuery( const QString& q )
367 {
368  if( desktopQuery( q ) ) {
369  QEventLoop loop;
370  d->loop = &loop;
371  loop.exec();
372  d->loop = 0;
373  close();
374  return true;
375  }
376  else {
377  return false;
378  }
379 }
380 
381 
382 QList< Nepomuk2::Query::Result > Nepomuk2::Query::QueryServiceClient::syncDesktopQuery(const QString& q, bool* ok)
383 {
384  QueryServiceClient qsc;
385  if( qsc.desktopQuery( q ) ) {
386  NepomukResultListEventLoop loop(&qsc);
387  qsc.d->loop = &loop;
388  loop.exec();
389  qsc.d->loop = 0;
390  if (ok) {
391  *ok = !qsc.errorMessage().isEmpty();
392  }
393  return loop.result();
394  }
395  else {
396  if (ok) {
397  *ok = false;
398  }
399  return QList< Nepomuk2::Query::Result >();
400  }
401 }
402 
403 
404 void Nepomuk2::Query::QueryServiceClient::close()
405 {
406  // drop pending query calls
407 
408  // in case we fired a query but it did not return yet, cancel it
409  if (d->m_pendingCallWatcher && !d->queryInterface) {
410  QDBusPendingReply<QDBusObjectPath> reply = *(d->m_pendingCallWatcher);
411  OrgKdeNepomukQueryInterface interface( d->queryServiceInterface->service(),
412  reply.value().path(),
413  d->dbusConnection );
414  interface.close();
415  }
416  delete d->m_pendingCallWatcher;
417 
418  d->m_errorMessage.truncate(0);
419 
420  if ( d->queryInterface ) {
421  kDebug();
422  d->queryInterface->close();
423  delete d->queryInterface;
424  d->queryInterface = 0;
425  d->m_queryActive = false;
426  if( d->loop )
427  d->loop->exit();
428  }
429 }
430 
431 
432 bool Nepomuk2::Query::QueryServiceClient::isListingFinished() const
433 {
434  return !d->m_queryActive;
435 }
436 
437 
438 bool Nepomuk2::Query::QueryServiceClient::serviceAvailable()
439 {
440  return QDBusConnection::sessionBus().interface()->isServiceRegistered( QLatin1String("org.kde.nepomuk.services.nepomukqueryservice") );
441 }
442 
443 
444 QString Nepomuk2::Query::QueryServiceClient::errorMessage() const
445 {
446  return d->m_errorMessage;
447 }
448 
449 #include "queryserviceclient.moc"
450 #include "queryserviceclient_p.moc"
Nepomuk2::Query::QueryServiceClient::blockingDesktopQuery
bool blockingDesktopQuery(const QString &query)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: queryserviceclient.cpp:366
Nepomuk2::Types::Entity::uri
QUrl uri() const
The URI of the resource.
Definition: entity.cpp:175
Nepomuk2::Query::QueryServiceClient::blockingSparqlQuery
bool blockingSparqlQuery(const QString &query, const Nepomuk2::Query::RequestPropertyMap &requestPropertyMap=Nepomuk2::Query::RequestPropertyMap())
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: queryserviceclient.cpp:326
Nepomuk2::Query::QueryServiceClient::blockingQuery
bool blockingQuery(const Query &query)
Start a query using the Nepomuk query service.
Definition: queryserviceclient.cpp:288
QHash
QObject
query.h
Nepomuk2::Query::QueryServiceClient::sparqlQuery
bool sparqlQuery(const QString &query, const Nepomuk2::Query::RequestPropertyMap &requestPropertyMap=Nepomuk2::Query::RequestPropertyMap())
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: queryserviceclient.cpp:247
Nepomuk2::Query::QueryServiceClient::syncQuery
static QList< Nepomuk2::Query::Result > syncQuery(const Query &query, bool *ok=0)
Start a query using the Nepomuk query service.
Definition: queryserviceclient.cpp:304
result.h
Nepomuk2::Query::Query
A Nepomuk desktop query.
Definition: query.h:76
Nepomuk2::Query::Query::RequestProperty
A request property can be added to a Query to retrieve additional information about the results...
Definition: query.h:287
Nepomuk2::Query::QueryServiceClient::query
bool query(const Query &query)
Start a query using the Nepomuk query service.
Definition: queryserviceclient.cpp:227
Nepomuk2::Query::QueryServiceClient::~QueryServiceClient
~QueryServiceClient()
Desctructor.
Definition: queryserviceclient.cpp:219
Nepomuk2::Query::QueryServiceClient::serviceAvailable
static bool serviceAvailable()
Check if the Nepomuk query service is running.
Definition: queryserviceclient.cpp:438
Nepomuk2::Query::Query::RequestProperty::property
Nepomuk2::Types::Property property() const
Definition: query.cpp:221
Nepomuk2::Query::QueryServiceClient::isListingFinished
bool isListingFinished() const
Definition: queryserviceclient.cpp:432
Nepomuk2::Query::QueryServiceClient::syncDesktopQuery
static QList< Nepomuk2::Query::Result > syncDesktopQuery(const QString &query, bool *ok=0)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: queryserviceclient.cpp:382
Nepomuk2::DBus::registerDBusTypes
void registerDBusTypes()
Definition: dbustypes.cpp:112
Nepomuk2::Query::QueryServiceClient::desktopQuery
bool desktopQuery(const QString &query)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: queryserviceclient.cpp:268
Nepomuk2::Query::QueryServiceClient::close
void close()
Close the client, thus stop to monitor the query for changes.
Definition: queryserviceclient.cpp:404
Nepomuk2::Query::QueryServiceClient::QueryServiceClient
QueryServiceClient(QObject *parent=0)
Create a new QueryServiceClient instance.
Definition: queryserviceclient.cpp:198
Nepomuk2::Query::Query::toString
QString toString() const
Encode the Query in a string.
Definition: query.cpp:595
Nepomuk2::Query::QueryServiceClient::errorMessage
QString errorMessage() const
The last error message which has been emitted via error() or an empty string if there was no error...
Definition: queryserviceclient.cpp:444
Nepomuk2::Query::QueryServiceClient::syncSparqlQuery
static QList< Nepomuk2::Query::Result > syncSparqlQuery(const QString &query, const Nepomuk2::Query::RequestPropertyMap &requestPropertyMap=Nepomuk2::Query::RequestPropertyMap(), bool *ok=0)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: queryserviceclient.cpp:342
Nepomuk2::Query::QueryServiceClient
Convenience frontend to the Nepomuk Query DBus Service.
Definition: queryserviceclient.h:58
queryserviceclient.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:08 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