add more spacing
[personal-kdebase.git] / runtime / nepomuk / services / queryservice / queryservice.cpp
blob83ca9e19c39fd6260f090120e5fa3ebf6f49b845
1 /*
2 Copyright (c) 2008 Sebastian Trueg <trueg@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
19 #include "queryservice.h"
20 #include "folder.h"
21 #include "folderconnection.h"
22 #include "query.h"
23 #include "queryparser.h"
24 #include "queryadaptor.h"
25 #include "dbusoperators.h"
27 #include <QtDBus/QDBusConnection>
28 #include <QtDBus/QDBusConnectionInterface>
29 #include <QtDBus/QDBusObjectPath>
30 #include <QtDBus/QDBusMessage>
32 #include <KPluginFactory>
33 #include <KDebug>
35 #include <Nepomuk/ResourceManager>
38 NEPOMUK_EXPORT_SERVICE( Nepomuk::Search::QueryService, "nepomukqueryservice" )
41 Nepomuk::Search::QueryService* Nepomuk::Search::QueryService::s_instance = 0;
43 Nepomuk::Search::QueryService::QueryService( QObject* parent, const QVariantList& )
44 : Service( parent ),
45 m_folderConnectionCnt( 0 )
47 // only so ResourceManager won't open yet another connection to the nepomuk server
48 ResourceManager::instance()->setOverrideMainModel( mainModel() );
50 Nepomuk::Search::registerDBusTypes();
52 s_instance = this;
54 connect( QDBusConnection::sessionBus().interface(),
55 SIGNAL( serviceOwnerChanged( const QString&, const QString&, const QString& ) ),
56 this,
57 SLOT( slotServiceOwnerChanged( const QString&, const QString&, const QString& ) ) );
61 Nepomuk::Search::QueryService::~QueryService()
66 QDBusObjectPath Nepomuk::Search::QueryService::query( const QString& queryString, const QStringList& props, const QDBusMessage& msg )
68 kDebug() << "Query request:" << queryString;
70 // create query folder + connection
71 Query q = QueryParser::parseQuery( queryString );
72 foreach( const QString& rp, props ) {
73 q.addRequestProperty( QUrl( rp ) );
75 return query( q, msg );
79 QDBusObjectPath Nepomuk::Search::QueryService::query( const Nepomuk::Search::Query& q, const QDBusMessage& msg )
81 kDebug() << "Query request:" << q;
83 Folder* folder = getFolder( q );
84 FolderConnection* conn = new FolderConnection( folder );
85 connect( conn, SIGNAL( destroyed( QObject* ) ),
86 this, SLOT( slotFolderConnectionDestroyed( QObject* ) ) );
88 // register the new connection with dbus
89 ( void )new QueryAdaptor( conn );
90 QString dbusObjectPath = QString( "/nepomukqueryservice/query%1" ).arg( ++m_folderConnectionCnt );
91 QDBusConnection::sessionBus().registerObject( dbusObjectPath, conn );
93 // remember the client for automatic cleanup
94 QString dbusClient = msg.service();
95 m_openConnections.insert( dbusClient, conn );
96 m_connectionDBusServiceHash.insert( conn, dbusClient );
98 return QDBusObjectPath( dbusObjectPath );
102 Soprano::Model* Nepomuk::Search::QueryService::mainModel()
104 return Nepomuk::Service::mainModel();
108 Nepomuk::Search::QueryService* Nepomuk::Search::QueryService::instance()
110 return s_instance;
114 Nepomuk::Search::Folder* Nepomuk::Search::QueryService::getFolder( const Query& query )
116 QHash<Query, Folder*>::iterator it = m_openFolders.find( query );
117 if ( it != m_openFolders.end() ) {
118 kDebug() << "Recycling folder" << *it;
119 return *it;
121 else {
122 kDebug() << "Creating new search folder for query:" << query;
123 Folder* newFolder = new Folder( query );
124 connect( newFolder, SIGNAL( destroyed( QObject* ) ),
125 this, SLOT( slotFolderDestroyed( QObject* ) ) );
126 m_openFolders.insert( query, newFolder );
127 m_folderQueryHash.insert( newFolder, query );
128 return newFolder;
133 void Nepomuk::Search::QueryService::slotFolderDestroyed( QObject* folder )
135 kDebug() << folder;
136 QHash<Folder*, Query>::iterator it = m_folderQueryHash.find( ( Folder* )folder );
137 if ( it != m_folderQueryHash.end() ) {
138 m_openFolders.remove( *it );
139 m_folderQueryHash.erase( it );
144 void Nepomuk::Search::QueryService::slotFolderConnectionDestroyed( QObject* o )
146 kDebug() << o;
147 FolderConnection* conn = ( FolderConnection* )o;
148 QHash<FolderConnection*, QString>::iterator it = m_connectionDBusServiceHash.find( conn );
149 if ( it != m_connectionDBusServiceHash.end() ) {
150 m_openConnections.remove( *it, conn );
151 m_connectionDBusServiceHash.erase( it );
156 void Nepomuk::Search::QueryService::slotServiceOwnerChanged( const QString& serviceName,
157 const QString&,
158 const QString& newOwner )
160 if ( newOwner.isEmpty() ) {
161 QList<FolderConnection*> conns = m_openConnections.values( serviceName );
162 if ( !conns.isEmpty() ) {
163 kDebug() << "Service" << serviceName << "went down. Removing connections";
164 // hash cleanup will be triggered automatically
165 qDeleteAll( conns );
170 #include "queryservice.moc"