Plasma
xesamrunner.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 "xesamrunner.h"
00020
00021 #include <QDBusInterface>
00022 #include <QDBusReply>
00023 #include <QDBusMetaType>
00024 #include <QStringList>
00025 #include <QVector>
00026
00027 #include <KDebug>
00028 #include <KMimeType>
00029 #include <KRun>
00030 #include <KUrl>
00031 #include <KIcon>
00032
00033 Q_DECLARE_METATYPE(QList<QList<QVariant> >)
00034 static int typeId = qDBusRegisterMetaType<QList<QList<QVariant> > >();
00035
00036 XesamRunner::XesamRunner(QObject *parent, const QVariantList& args)
00037 : Plasma::AbstractRunner(parent, args)
00038 {
00039 KGlobal::locale()->insertCatalog("krunner_xesam");
00040 Q_UNUSED(args);
00041
00042 setObjectName(i18n("Desktop Search"));
00043 setSpeed(AbstractRunner::SlowSpeed);
00044 }
00045
00046 XesamRunner::~XesamRunner()
00047 {
00048 }
00049
00050 void XesamRunner::match(Plasma::RunnerContext &context)
00051 {
00052 if (context.query().length()<3) return;
00053
00054 QDBusInterface xesam("org.freedesktop.xesam.searcher",
00055 "/org/freedesktop/xesam/searcher/main");
00056
00057 QDBusReply<QString> reply = xesam.call("NewSession");
00058 if (!reply.isValid()) return;
00059
00060 QString session = reply;
00061 xesam.call("SetProperty", session, "hit.fields",
00062 QStringList() << "uri" << "dc:title");
00063
00064 QString query =
00065 "<?xml version='1.0' encoding='UTF-8'?>"
00066 "<request xmlns='http://freedesktop.org/standards/xesam/1.0/query'>"
00067 "<userQuery>%1</userQuery>"
00068 "</request>";
00069 QString userQuery = context.query();
00070
00071 reply = xesam.call("NewSearch", session, query.arg(userQuery));
00072 if (!reply.isValid()) return;
00073
00074 QString search = reply;
00075
00076 QDBusReply<void> start_reply = xesam.call("StartSearch", search);
00077 if (!start_reply.isValid()) return;
00078
00079 xesam.call("GetHitCount", search);
00080 QDBusReply<QList<QList<QVariant> > > hits_reply
00081 = xesam.call("GetHits", search, (quint32)10);
00082 if (!hits_reply.isValid()) return;
00083
00084 QList<QList<QVariant> > hits = hits_reply;
00085
00086 xesam.call("CloseSession", session);
00087
00088 foreach (const QList<QVariant> &hit, hits) {
00089 if (hit.isEmpty()) continue;
00090
00091 QString url = hit[0].toString();
00092
00093 QString title;
00094 if (hit.size()>1) {
00095 title = hit[1].toString();
00096 } else {
00097 title = hit[0].toString();
00098 }
00099
00100 Plasma::QueryMatch match(this);
00101 match.setType(Plasma::QueryMatch::PossibleMatch);
00102 match.setIcon(KIcon("text-x-generic"));
00103 match.setData(url);
00104 match.setText(title);
00105 match.setRelevance(1);
00106 context.addMatch(userQuery, match);
00107 }
00108 }
00109
00110 void XesamRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
00111 {
00112 Q_UNUSED(context)
00113 QMutexLocker lock(bigLock());
00114 new KRun(KUrl(match.data().toString()), 0);
00115 }
00116
00117 #include "xesamrunner.moc"