add more spacing
[personal-kdebase.git] / workspace / plasma / runners / nepomuksearch / queryclientwrapper.cpp
blob2f5d07b0f1819e9e8554ed2764594d6aab55948e
1 /* This file is part of the Nepomuk Project
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 "queryclientwrapper.h"
20 #include "nepomuksearchrunner.h"
21 #include "queryserviceclient.h"
22 #include "result.h"
23 #include "query.h"
24 #include "queryparser.h"
26 #include <Nepomuk/Resource>
27 #include <Nepomuk/Types/Class>
29 #include <Soprano/Vocabulary/Xesam>
31 #include <KIcon>
32 #include <KDebug>
33 #include <KMimeType>
35 #include <Plasma/QueryMatch>
36 #include <Plasma/RunnerContext>
37 #include <Plasma/AbstractRunner>
39 #include <QtCore/QTimer>
40 #include <QtCore/QMutex>
43 Q_DECLARE_METATYPE(Nepomuk::Resource)
45 static const int s_maxResults = 10;
47 Nepomuk::QueryClientWrapper::QueryClientWrapper( SearchRunner* runner, Plasma::RunnerContext* context )
48 : QObject(),
49 m_runner( runner ),
50 m_runnerContext( context )
52 // initialize the query client
53 m_queryServiceClient = new Nepomuk::Search::QueryServiceClient( this );
54 connect( m_queryServiceClient, SIGNAL(newEntries( const QList<Nepomuk::Search::Result>& )),
55 this, SLOT(slotNewEntries( const QList<Nepomuk::Search::Result>& )) );
59 Nepomuk::QueryClientWrapper::~QueryClientWrapper()
64 void Nepomuk::QueryClientWrapper::runQuery()
66 kDebug() << m_runnerContext->query();
68 // add a timeout in case something goes wrong (no user wants to wait more than 30 seconds)
69 QTimer::singleShot( 30000, m_queryServiceClient, SLOT(close()) );
71 Search::Query q = Search::QueryParser::parseQuery( m_runnerContext->query() );
72 q.setLimit( s_maxResults );
73 m_queryServiceClient->blockingQuery( q );
75 kDebug() << m_runnerContext->query() << "done";
79 namespace {
80 qreal normalizeScore( double score ) {
81 // no search result is ever a perfect match, NEVER. And mostly, when typing a URL
82 // the users wants to open that url instead of using the search result. Thus, all
83 // search results need to have a lower score than URLs which can drop to 0.5
84 // And in the end, for 10 results, the score is not that important at the moment.
85 // This can be improved in the future.
86 // We go the easy way here and simply cut the score at 0.4
87 return qMin( 0.4, score );
91 void Nepomuk::QueryClientWrapper::slotNewEntries( const QList<Nepomuk::Search::Result>& results )
93 foreach( const Search::Result& result, results ) {
94 Plasma::QueryMatch match( m_runner );
95 match.setType( Plasma::QueryMatch::PossibleMatch );
96 match.setRelevance( normalizeScore( result.score() ) );
98 Nepomuk::Resource res( result.resourceUri() );
100 // we need to protect KMimeType which is not thread-safe, Nepomuk::Resource::genericIcon() also uses it
101 Plasma::AbstractRunner::bigLock()->lock();
102 QString type;
103 if( res.hasType( Soprano::Vocabulary::Xesam::File() ) ||
104 res.resourceUri().scheme() == "file" ) {
105 type = KMimeType::findByUrl( res.resourceUri() )->comment();
107 else {
108 type = Nepomuk::Types::Class( res.resourceType() ).label();
111 match.setText( i18nc( "@action file/resource to be opened from KRunner. %1 is the name and %2 the type",
112 "Open %1 (%2)",
113 res.genericLabel(),
114 type ) );
115 QString s = res.genericIcon();
116 match.setIcon( KIcon( s.isEmpty() ? QString("nepomuk") : s ) );
117 Plasma::AbstractRunner::bigLock()->unlock();
119 match.setData( qVariantFromValue( res ) );
121 m_runnerContext->addMatch( m_runnerContext->query(), match );
125 #include "queryclientwrapper.moc"