Plasma
queryclientwrapper.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 "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
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
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
00082
00083
00084
00085
00086
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
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"