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

NepomukDaemons

queryservice.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 "queryservice.h"
00020 #include "folder.h"
00021 #include "folderconnection.h"
00022 #include "query.h"
00023 #include "queryparser.h"
00024 #include "queryadaptor.h"
00025 #include "dbusoperators.h"
00026 
00027 #include <QtDBus/QDBusConnection>
00028 #include <QtDBus/QDBusConnectionInterface>
00029 #include <QtDBus/QDBusObjectPath>
00030 #include <QtDBus/QDBusMessage>
00031 
00032 #include <KPluginFactory>
00033 #include <KDebug>
00034 
00035 #include <Nepomuk/ResourceManager>
00036 
00037 
00038 NEPOMUK_EXPORT_SERVICE( Nepomuk::Search::QueryService, "nepomukqueryservice" )
00039 
00040 
00041 Nepomuk::Search::QueryService* Nepomuk::Search::QueryService::s_instance = 0;
00042 
00043 Nepomuk::Search::QueryService::QueryService( QObject* parent, const QVariantList& )
00044     : Service( parent ),
00045       m_folderConnectionCnt( 0 )
00046 {
00047     // only so ResourceManager won't open yet another connection to the nepomuk server
00048     ResourceManager::instance()->setOverrideMainModel( mainModel() );
00049 
00050     Nepomuk::Search::registerDBusTypes();
00051 
00052     s_instance = this;
00053 
00054     connect( QDBusConnection::sessionBus().interface(),
00055              SIGNAL( serviceOwnerChanged( const QString&, const QString&, const QString& ) ),
00056              this,
00057              SLOT( slotServiceOwnerChanged( const QString&, const QString&, const QString& ) ) );
00058 }
00059 
00060 
00061 Nepomuk::Search::QueryService::~QueryService()
00062 {
00063 }
00064 
00065 
00066 QDBusObjectPath Nepomuk::Search::QueryService::query( const QString& queryString, const QStringList& props, const QDBusMessage& msg )
00067 {
00068     kDebug() << "Query request:" << queryString;
00069 
00070     // create query folder + connection
00071     Query q = QueryParser::parseQuery( queryString );
00072     foreach( const QString& rp, props ) {
00073         q.addRequestProperty( QUrl( rp ) );
00074     }
00075     return query( q, msg );
00076 }
00077 
00078 
00079 QDBusObjectPath Nepomuk::Search::QueryService::query( const Nepomuk::Search::Query& q, const QDBusMessage& msg )
00080 {
00081     kDebug() << "Query request:" << q;
00082 
00083     Folder* folder = getFolder( q );
00084     FolderConnection* conn = new FolderConnection( folder );
00085     connect( conn, SIGNAL( destroyed( QObject* ) ),
00086              this, SLOT( slotFolderConnectionDestroyed( QObject* ) ) );
00087 
00088     // register the new connection with dbus
00089     ( void )new QueryAdaptor( conn );
00090     QString dbusObjectPath = QString( "/nepomukqueryservice/query%1" ).arg( ++m_folderConnectionCnt );
00091     QDBusConnection::sessionBus().registerObject( dbusObjectPath, conn );
00092 
00093     // remember the client for automatic cleanup
00094     QString dbusClient = msg.service();
00095     m_openConnections.insert( dbusClient, conn );
00096     m_connectionDBusServiceHash.insert( conn, dbusClient );
00097 
00098     return QDBusObjectPath( dbusObjectPath );
00099 }
00100 
00101 
00102 Soprano::Model* Nepomuk::Search::QueryService::mainModel()
00103 {
00104     return Nepomuk::Service::mainModel();
00105 }
00106 
00107 
00108 Nepomuk::Search::QueryService* Nepomuk::Search::QueryService::instance()
00109 {
00110     return s_instance;
00111 }
00112 
00113 
00114 Nepomuk::Search::Folder* Nepomuk::Search::QueryService::getFolder( const Query& query )
00115 {
00116     QHash<Query, Folder*>::iterator it = m_openFolders.find( query );
00117     if ( it != m_openFolders.end() ) {
00118         kDebug() << "Recycling folder" << *it;
00119         return *it;
00120     }
00121     else {
00122         kDebug() << "Creating new search folder for query:" << query;
00123         Folder* newFolder = new Folder( query );
00124         connect( newFolder, SIGNAL( destroyed( QObject* ) ),
00125                  this, SLOT( slotFolderDestroyed( QObject* ) ) );
00126         m_openFolders.insert( query, newFolder );
00127         m_folderQueryHash.insert( newFolder, query );
00128         return newFolder;
00129     }
00130 }
00131 
00132 
00133 void Nepomuk::Search::QueryService::slotFolderDestroyed( QObject* folder )
00134 {
00135     kDebug() << folder;
00136     QHash<Folder*, Query>::iterator it = m_folderQueryHash.find( ( Folder* )folder );
00137     if ( it != m_folderQueryHash.end() ) {
00138         m_openFolders.remove( *it );
00139         m_folderQueryHash.erase( it );
00140     }
00141 }
00142 
00143 
00144 void Nepomuk::Search::QueryService::slotFolderConnectionDestroyed( QObject* o )
00145 {
00146     kDebug() << o;
00147     FolderConnection* conn = ( FolderConnection* )o;
00148     QHash<FolderConnection*, QString>::iterator it = m_connectionDBusServiceHash.find( conn );
00149     if ( it != m_connectionDBusServiceHash.end() ) {
00150         m_openConnections.remove( *it, conn );
00151         m_connectionDBusServiceHash.erase( it );
00152     }
00153 }
00154 
00155 
00156 void Nepomuk::Search::QueryService::slotServiceOwnerChanged( const QString& serviceName,
00157                                                              const QString&,
00158                                                              const QString& newOwner )
00159 {
00160     if ( newOwner.isEmpty() ) {
00161         QList<FolderConnection*> conns = m_openConnections.values( serviceName );
00162         if ( !conns.isEmpty() ) {
00163             kDebug() << "Service" << serviceName << "went down. Removing connections";
00164             // hash cleanup will be triggered automatically
00165             qDeleteAll( conns );
00166         }
00167     }
00168 }
00169 
00170 #include "queryservice.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