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

Plasma

placesrunner.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright 2008 David Edmundson <kde@davidedmundson.co.uk>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License as
00006  *   published by the Free Software Foundation; either version 2, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details
00013  *
00014  *   You should have received a copy of the GNU Library General Public
00015  *   License along with this program; if not, write to the
00016  *   Free Software Foundation, Inc.,
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
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             //if we have to mount it set the device udi instead of the URL, as we can't open it directly
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     //I don't just pass the model index because the list could change before the user clicks on it, which would make everything go wrong. Ideally we don't want things to go wrong.
00084     if (action.data().canConvert<KUrl>()) {
00085         new KRun(action.data().value<KUrl>().url(), 0);
00086     } else if (action.data().canConvert<QString>()) {
00087         //search our list for the device with the same udi, then set it up (mount it).
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 //if a device needed mounting, this slot gets called when it's finished.
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"

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