• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

NepomukDaemons

queryserviceclient.cpp

Go to the documentation of this file.
00001 /*
00002    Copyright (c) 2008 Sebastian Trueg <trueg@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include "queryserviceclient.h"
00020 #include "dbusoperators.h"
00021 #include "result.h"
00022 #include "query.h"
00023 #include "queryserviceinterface.h"
00024 #include "queryinterface.h"
00025 
00026 #include <QtDBus/QDBusInterface>
00027 #include <QtDBus/QDBusConnection>
00028 #include <QtDBus/QDBusConnectionInterface>
00029 #include <QtDBus/QDBusReply>
00030 
00031 #include <QtCore/QEventLoop>
00032 #include <QtCore/QTimer>
00033 
00034 #include <KDebug>
00035 #include <KGlobal>
00036 
00037 
00038 namespace {
00044     class QDBusConnectionPerThreadHelper
00045     {
00046     public:
00047         QDBusConnectionPerThreadHelper()
00048             : m_counter( 0 ) {
00049         }
00050 
00051         QDBusConnection newConnection() {
00052             QMutexLocker lock( &m_mutex );
00053             return QDBusConnection::connectToBus( QDBusConnection::SessionBus,
00054                                                   QString("NepomukQueryServiceConnection%1").arg(++m_counter) );
00055         }
00056 
00057     private:
00058         int m_counter;
00059         QMutex m_mutex;
00060     };
00061 
00062     K_GLOBAL_STATIC( QDBusConnectionPerThreadHelper, s_globalDBusConnectionPerThreadHelper )
00063 }
00064 
00065 
00066 class Nepomuk::Search::QueryServiceClient::Private
00067 {
00068 public:
00069     Private()
00070         : queryServiceInterface( 0 ),
00071           queryInterface( 0 ),
00072           dbusConnection( s_globalDBusConnectionPerThreadHelper->newConnection() ),
00073           loop( 0 ) {
00074     }
00075 
00076     void _k_entriesRemoved( const QStringList& );
00077     void _k_finishedListing();
00078     bool handleQueryReply( QDBusReply<QDBusObjectPath> reply );
00079 
00080     org::kde::nepomuk::QueryService* queryServiceInterface;
00081     org::kde::nepomuk::Query* queryInterface;
00082 
00083     QueryServiceClient* q;
00084 
00085     QDBusConnection dbusConnection;
00086 
00087     QEventLoop* loop;
00088 };
00089 
00090 
00091 void Nepomuk::Search::QueryServiceClient::Private::_k_entriesRemoved( const QStringList& uris )
00092 {
00093     QList<QUrl> ul;
00094     foreach( const QString& s, uris ) {
00095         ul.append( QUrl( s ) );
00096     }
00097     emit q->entriesRemoved( ul );
00098 }
00099 
00100 
00101 void Nepomuk::Search::QueryServiceClient::Private::_k_finishedListing()
00102 {
00103     emit q->finishedListing();
00104     if( loop ) {
00105         q->close();
00106     }
00107 }
00108 
00109 
00110 bool Nepomuk::Search::QueryServiceClient::Private::handleQueryReply( QDBusReply<QDBusObjectPath> r )
00111 {
00112     if ( r.isValid() ) {
00113         queryInterface = new org::kde::nepomuk::Query( queryServiceInterface->service(),
00114                                                        r.value().path(),
00115                                                        dbusConnection  );
00116         connect( queryInterface, SIGNAL( newEntries( QList<Nepomuk::Search::Result> ) ),
00117                  q, SIGNAL( newEntries( QList<Nepomuk::Search::Result> ) ) );
00118         connect( queryInterface, SIGNAL( entriesRemoved( QStringList ) ),
00119                  q, SLOT( _k_entriesRemoved( QStringList ) ) );
00120         connect( queryInterface, SIGNAL( finishedListing() ),
00121                  q, SLOT( _k_finishedListing() ) );
00122         // run the listing async in case the event loop below is the only one we have
00123         // and we need it to handle the signals and list returns results immediately
00124         QTimer::singleShot( 0, queryInterface, SLOT(list()) );
00125         return true;
00126     }
00127     else {
00128         kDebug() << "Query failed:" << r.error().message();
00129         return false;
00130     }
00131 }
00132 
00133 
00134 Nepomuk::Search::QueryServiceClient::QueryServiceClient( QObject* parent )
00135     : QObject( parent ),
00136       d( new Private() )
00137 {
00138     d->q = this;
00139 
00140     Nepomuk::Search::registerDBusTypes();
00141 
00142     // we use our own connection to be thread-safe
00143     d->queryServiceInterface = new org::kde::nepomuk::QueryService( "org.kde.nepomuk.services.nepomukqueryservice",
00144                                                                     "/nepomukqueryservice",
00145                                                                     d->dbusConnection );
00146 }
00147 
00148 
00149 Nepomuk::Search::QueryServiceClient::~QueryServiceClient()
00150 {
00151     close();
00152     delete d;
00153 }
00154 
00155 
00156 bool Nepomuk::Search::QueryServiceClient::query( const QString& query )
00157 {
00158     close();
00159 
00160     if ( d->queryServiceInterface->isValid() ) {
00161         return d->handleQueryReply( d->queryServiceInterface->query( query, QStringList() ) );
00162     }
00163     else {
00164         kDebug() << "Could not contact query service.";
00165         return false;
00166     }
00167 }
00168 
00169 
00170 bool Nepomuk::Search::QueryServiceClient::query( const Query& query )
00171 {
00172     close();
00173 
00174     if ( d->queryServiceInterface->isValid() ) {
00175         return d->handleQueryReply( d->queryServiceInterface->query( query ) );
00176     }
00177     else {
00178         kDebug() << "Could not contact query service.";
00179         return false;
00180     }
00181 }
00182 
00183 
00184 bool Nepomuk::Search::QueryServiceClient::blockingQuery( const QString& q )
00185 {
00186     if( query( q ) ) {
00187         QEventLoop loop;
00188         d->loop = &loop;
00189         loop.exec();
00190         d->loop = 0;
00191         return true;
00192     }
00193     else {
00194         return false;
00195     }
00196 }
00197 
00198 
00199 bool Nepomuk::Search::QueryServiceClient::blockingQuery( const Query& q )
00200 {
00201     if( query( q ) ) {
00202         QEventLoop loop;
00203         d->loop = &loop;
00204         loop.exec();
00205         d->loop = 0;
00206         return true;
00207     }
00208     else {
00209         return false;
00210     }
00211 }
00212 
00213 
00214 void Nepomuk::Search::QueryServiceClient::close()
00215 {
00216     if ( d->queryInterface ) {
00217         kDebug();
00218         d->queryInterface->close();
00219         delete d->queryInterface;
00220         d->queryInterface = 0;
00221         if( d->loop )
00222             d->loop->exit();
00223     }
00224 }
00225 
00226 
00227 bool Nepomuk::Search::QueryServiceClient::serviceAvailable()
00228 {
00229     return QDBusConnection::sessionBus().interface()->isServiceRegistered( "org.kde.nepomuk.services.nepomukqueryservice" );
00230 }
00231 
00232 #include "queryserviceclient.moc"

NepomukDaemons

Skip menu "NepomukDaemons"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KCMShell
  • KNotify
  • KStyles
  • Nepomuk Daemons
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal