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

Plasma

nepomuksearchrunner.cpp

Go to the documentation of this file.
00001 /* This file is part of the Nepomuk Project
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 "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     // we are pretty slow at times and use DBus calls
00066     setSpeed( SlowSpeed );
00067 
00068     // we are way less important than others, mostly because we are slow
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     // This method needs to be thread-safe since KRunner does simply start new threads whenever
00085     // the query term changes.
00086     m_mutex.lock();
00087 
00088     // we do not want to restart a query on each key-press. That would result
00089     // in way too many queries for the rather sluggy Nepomuk query service
00090     // Thus, we use a little timeout to make sure we do not query too often
00091 
00092     // we remember the match count when starting to wait, so we do not start the query
00093     // in case another call deprecated us (KRunner will simply rerun this method in another
00094     // thread when the user input changes)
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         // we are no longer the latest call
00103         m_mutex.unlock();
00104         return;
00105     }
00106 
00107     kDebug() << &context << "running match cnt" << c;
00108 
00109     m_mutex.unlock();
00110 
00111     // no queries on very short strings
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"

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
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