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

Plasma

queryclientwrapper.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 "queryclientwrapper.h"
00020 #include "nepomuksearchrunner.h"
00021 #include "queryserviceclient.h"
00022 #include "result.h"
00023 #include "query.h"
00024 #include "queryparser.h"
00025 
00026 #include <Nepomuk/Resource>
00027 #include <Nepomuk/Types/Class>
00028 
00029 #include <Soprano/Vocabulary/Xesam>
00030 
00031 #include <KIcon>
00032 #include <KDebug>
00033 #include <KMimeType>
00034 
00035 #include <Plasma/QueryMatch>
00036 #include <Plasma/RunnerContext>
00037 #include <Plasma/AbstractRunner>
00038 
00039 #include <QtCore/QTimer>
00040 #include <QtCore/QMutex>
00041 
00042 
00043 Q_DECLARE_METATYPE(Nepomuk::Resource)
00044 
00045 static const int s_maxResults = 10;
00046 
00047 Nepomuk::QueryClientWrapper::QueryClientWrapper( SearchRunner* runner, Plasma::RunnerContext* context )
00048     : QObject(),
00049       m_runner( runner ),
00050       m_runnerContext( context )
00051 {
00052     // initialize the query client
00053     m_queryServiceClient = new Nepomuk::Search::QueryServiceClient( this );
00054     connect( m_queryServiceClient, SIGNAL(newEntries( const QList<Nepomuk::Search::Result>& )),
00055              this, SLOT(slotNewEntries( const QList<Nepomuk::Search::Result>& )) );
00056 }
00057 
00058 
00059 Nepomuk::QueryClientWrapper::~QueryClientWrapper()
00060 {
00061 }
00062 
00063 
00064 void Nepomuk::QueryClientWrapper::runQuery()
00065 {
00066     kDebug() << m_runnerContext->query();
00067 
00068     // add a timeout in case something goes wrong (no user wants to wait more than 30 seconds)
00069     QTimer::singleShot( 30000, m_queryServiceClient, SLOT(close()) );
00070 
00071     Search::Query q = Search::QueryParser::parseQuery( m_runnerContext->query() );
00072     q.setLimit( s_maxResults );
00073     m_queryServiceClient->blockingQuery( q );
00074 
00075     kDebug() << m_runnerContext->query() << "done";
00076 }
00077 
00078 
00079 namespace {
00080 qreal normalizeScore( double score ) {
00081     // no search result is ever a perfect match, NEVER. And mostly, when typing a URL
00082     // the users wants to open that url instead of using the search result. Thus, all
00083     // search results need to have a lower score than URLs which can drop to 0.5
00084     // And in the end, for 10 results, the score is not that important at the moment.
00085     // This can be improved in the future.
00086     // We go the easy way here and simply cut the score at 0.4
00087     return qMin( 0.4, score );
00088 }
00089 }
00090 
00091 void Nepomuk::QueryClientWrapper::slotNewEntries( const QList<Nepomuk::Search::Result>& results )
00092 {
00093     foreach( const Search::Result& result, results ) {
00094         Plasma::QueryMatch match( m_runner );
00095         match.setType( Plasma::QueryMatch::PossibleMatch );
00096         match.setRelevance( normalizeScore( result.score() ) );
00097 
00098         Nepomuk::Resource res( result.resourceUri() );
00099 
00100         // we need to protect KMimeType which is not thread-safe, Nepomuk::Resource::genericIcon() also uses it
00101         Plasma::AbstractRunner::bigLock()->lock();
00102         QString type;
00103         if( res.hasType( Soprano::Vocabulary::Xesam::File() ) ||
00104             res.resourceUri().scheme() == "file" ) {
00105             type = KMimeType::findByUrl( res.resourceUri() )->comment();
00106         }
00107         else {
00108             type = Nepomuk::Types::Class( res.resourceType() ).label();
00109         }
00110 
00111         match.setText( i18nc( "@action file/resource to be opened from KRunner. %1 is the name and %2 the type",
00112                               "Open %1 (%2)",
00113                               res.genericLabel(),
00114                               type ) );
00115         QString s = res.genericIcon();
00116         match.setIcon( KIcon( s.isEmpty() ? QString("nepomuk") : s ) );
00117         Plasma::AbstractRunner::bigLock()->unlock();
00118 
00119         match.setData( qVariantFromValue( res ) );
00120 
00121         m_runnerContext->addMatch( m_runnerContext->query(), match );
00122     }
00123 }
00124 
00125 #include "queryclientwrapper.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