Plasma
nepomuksearchrunner.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "nepomuksearchrunner.h"
00020 #include "queryclientwrapper.h"
00021
00022 #include "queryserviceclient.h"
00023
00024 #include <KRun>
00025 #include <KDebug>
00026 #include <KUrl>
00027
00028 #include <Nepomuk/Resource>
00029 #include <Nepomuk/ResourceManager>
00030
00031 #include <Soprano/Vocabulary/NAO>
00032
00033
00034 Q_DECLARE_METATYPE(Nepomuk::Resource)
00035
00036 namespace {
00043 const int s_userActionTimeout = 400;
00044 }
00045
00046
00047 Nepomuk::SearchRunner::SearchRunner( QObject* parent, const QVariantList& args )
00048 : Plasma::AbstractRunner( parent, args )
00049 {
00050 init();
00051 }
00052
00053
00054 Nepomuk::SearchRunner::SearchRunner( QObject* parent, const QString& serviceId )
00055 : Plasma::AbstractRunner( parent, serviceId )
00056 {
00057 init();
00058 }
00059
00060
00061 void Nepomuk::SearchRunner::init()
00062 {
00063 Nepomuk::ResourceManager::instance()->init();
00064
00065
00066 setSpeed( SlowSpeed );
00067
00068
00069 setPriority( LowPriority );
00070
00071 m_matchCnt = 0;
00072 }
00073
00074
00075 Nepomuk::SearchRunner::~SearchRunner()
00076 {
00077 }
00078
00079
00080 void Nepomuk::SearchRunner::match( Plasma::RunnerContext& context )
00081 {
00082 kDebug() << &context << context.query();
00083
00084
00085
00086 m_mutex.lock();
00087
00088
00089
00090
00091
00092
00093
00094
00095 int c = ++m_matchCnt;
00096
00097 kDebug() << &context << "waiting for match cnt" << c;
00098
00099 m_waiter.wait( &m_mutex, s_userActionTimeout );
00100 if( c != m_matchCnt ) {
00101 kDebug() << &context << "deprecated match cnt" << c;
00102
00103 m_mutex.unlock();
00104 return;
00105 }
00106
00107 kDebug() << &context << "running match cnt" << c;
00108
00109 m_mutex.unlock();
00110
00111
00112 if( Search::QueryServiceClient::serviceAvailable() &&
00113 context.query().count() >= 3 ) {
00114 QueryClientWrapper queryWrapper( this, &context );
00115 queryWrapper.runQuery();
00116 m_waiter.wakeAll();
00117 }
00118 }
00119
00120
00121 void Nepomuk::SearchRunner::run( const Plasma::RunnerContext&, const Plasma::QueryMatch& match )
00122 {
00123 Nepomuk::Resource res = match.data().value<Nepomuk::Resource>();
00124 KUrl url;
00125
00126 if( res.hasType( Soprano::Vocabulary::NAO::Tag() ) ) {
00127 url.setProtocol( "nepomuksearch" );
00128 url.setPath( QString( "/hasTag:\"%1\"" ).arg( res.genericLabel() ) );
00129 }
00130 else {
00131 url = res.resourceUri();
00132 }
00133
00134 (void)new KRun( url, 0 );
00135 }
00136
00137 K_EXPORT_PLASMA_RUNNER(nepomuksearchrunner, Nepomuk::SearchRunner)
00138
00139 #include "nepomuksearchrunner.moc"