Plasma
placesrunner.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
00020 #include "placesrunner.h"
00021
00022 #include <KIcon>
00023 #include <KRun>
00024 #include <KUrl>
00025
00026 PlacesRunner::PlacesRunner(QObject* parent, const QVariantList &args)
00027 : Plasma::AbstractRunner(parent, args)
00028 {
00029 Q_UNUSED(args)
00030 setObjectName("Places");
00031 m_filePlaces = new KFilePlacesModel(this);
00032 connect(m_filePlaces, SIGNAL(setupDone(QModelIndex, bool)), SLOT(setupComplete(QModelIndex, bool)));
00033 }
00034
00035 PlacesRunner::~PlacesRunner()
00036 {
00037 }
00038
00039 void PlacesRunner::match(Plasma::RunnerContext &context)
00040 {
00041 const QString term = context.query();
00042 QList<Plasma::QueryMatch> matches;
00043
00044 if (term.length() < 3)
00045 return;
00046
00047 for (int i = 0; i <= m_filePlaces->rowCount();i++) {
00048 QModelIndex current_index = m_filePlaces->index(i, 0);
00049 Plasma::QueryMatch::Type type = Plasma::QueryMatch::NoMatch;
00050 qreal relevance = 0;
00051
00052 if (m_filePlaces->text(current_index).toLower() == term.toLower()) {
00053 type = Plasma::QueryMatch::ExactMatch;
00054 relevance = 1.0;
00055 } else if (m_filePlaces->text(current_index).contains(term, Qt::CaseInsensitive)) {
00056 type = Plasma::QueryMatch::PossibleMatch;
00057 relevance = 0.7;
00058 }
00059
00060 if (type != Plasma::QueryMatch::NoMatch) {
00061 Plasma::QueryMatch match(this);
00062 match.setType(type);
00063 match.setRelevance(relevance);
00064 match.setIcon(KIcon(m_filePlaces->icon(current_index)));
00065 match.setText(m_filePlaces->text(current_index));
00066
00067
00068 if (m_filePlaces->isDevice(current_index) && m_filePlaces->setupNeeded(current_index)) {
00069 match.setData(m_filePlaces->deviceForIndex(current_index).udi());
00070 } else {
00071 match.setData(m_filePlaces->url(current_index));
00072 }
00073 matches << match;
00074 }
00075 }
00076 context.addMatches(term, matches);
00077 }
00078
00079
00080 void PlacesRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &action)
00081 {
00082 Q_UNUSED(context);
00083
00084 if (action.data().canConvert<KUrl>()) {
00085 new KRun(action.data().value<KUrl>().url(), 0);
00086 } else if (action.data().canConvert<QString>()) {
00087
00088 QString deviceUdi = action.data().toString();
00089
00090 for (int i = 0; i <= m_filePlaces->rowCount();i++) {
00091 QModelIndex current_index = m_filePlaces->index(i, 0);
00092 if (m_filePlaces->isDevice(current_index) && m_filePlaces->deviceForIndex(current_index).udi() == deviceUdi) {
00093 m_filePlaces->requestSetup(current_index);
00094 break;
00095 }
00096 }
00097 }
00098 }
00099
00100
00101 void PlacesRunner::setupComplete(QModelIndex index, bool success)
00102 {
00103 if (success) {
00104 new KRun(m_filePlaces->url(index), 0);
00105 }
00106 }
00107
00108 #include "placesrunner.moc"