not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / runners / nepomuksearch / nepomuksearchrunner.cpp
blob1e39c3b0a1c67ad622193bf5a70d8fc8e502a132
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 "nepomuksearchrunner.h"
20 #include "queryclientwrapper.h"
22 #include "queryserviceclient.h"
24 #include <KRun>
25 #include <KDebug>
26 #include <KUrl>
28 #include <Nepomuk/Resource>
29 #include <Nepomuk/ResourceManager>
31 #include <Soprano/Vocabulary/NAO>
34 Q_DECLARE_METATYPE(Nepomuk::Resource)
36 namespace {
37 /**
38 * Milliseconds to wait before issuing the next query.
39 * This timeout is intended to prevent us from starting
40 * a query after each key press by the user. So it should
41 * roughly equal the time between key presses of an average user.
43 const int s_userActionTimeout = 400;
47 Nepomuk::SearchRunner::SearchRunner( QObject* parent, const QVariantList& args )
48 : Plasma::AbstractRunner( parent, args )
50 init();
54 Nepomuk::SearchRunner::SearchRunner( QObject* parent, const QString& serviceId )
55 : Plasma::AbstractRunner( parent, serviceId )
57 init();
61 void Nepomuk::SearchRunner::init()
63 Nepomuk::ResourceManager::instance()->init();
65 // we are pretty slow at times and use DBus calls
66 setSpeed( SlowSpeed );
68 // we are way less important than others, mostly because we are slow
69 setPriority( LowPriority );
71 m_matchCnt = 0;
75 Nepomuk::SearchRunner::~SearchRunner()
80 void Nepomuk::SearchRunner::match( Plasma::RunnerContext& context )
82 kDebug() << &context << context.query();
84 // This method needs to be thread-safe since KRunner does simply start new threads whenever
85 // the query term changes.
86 m_mutex.lock();
88 // we do not want to restart a query on each key-press. That would result
89 // in way too many queries for the rather sluggy Nepomuk query service
90 // Thus, we use a little timeout to make sure we do not query too often
92 // we remember the match count when starting to wait, so we do not start the query
93 // in case another call deprecated us (KRunner will simply rerun this method in another
94 // thread when the user input changes)
95 int c = ++m_matchCnt;
97 kDebug() << &context << "waiting for match cnt" << c;
99 m_waiter.wait( &m_mutex, s_userActionTimeout );
100 if( c != m_matchCnt ) {
101 kDebug() << &context << "deprecated match cnt" << c;
102 // we are no longer the latest call
103 m_mutex.unlock();
104 return;
107 kDebug() << &context << "running match cnt" << c;
109 m_mutex.unlock();
111 // no queries on very short strings
112 if( Search::QueryServiceClient::serviceAvailable() &&
113 context.query().count() >= 3 ) {
114 QueryClientWrapper queryWrapper( this, &context );
115 queryWrapper.runQuery();
116 m_waiter.wakeAll();
121 void Nepomuk::SearchRunner::run( const Plasma::RunnerContext&, const Plasma::QueryMatch& match )
123 Nepomuk::Resource res = match.data().value<Nepomuk::Resource>();
124 KUrl url;
126 if( res.hasType( Soprano::Vocabulary::NAO::Tag() ) ) {
127 url.setProtocol( "nepomuksearch" );
128 url.setPath( QString( "/hasTag:\"%1\"" ).arg( res.genericLabel() ) );
130 else {
131 url = res.resourceUri();
134 (void)new KRun( url, 0 );
137 K_EXPORT_PLASMA_RUNNER(nepomuksearchrunner, Nepomuk::SearchRunner)
139 #include "nepomuksearchrunner.moc"