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

Plasma

locationrunner.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright (C) 2007 Teemu Rytilahti <tpr@iki.fi>
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 version 2 as
00006  *   published by the Free Software Foundation
00007  *
00008  *   This program 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
00011  *   GNU General Public License for more details
00012  *
00013  *   You should have received a copy of the GNU Library General Public
00014  *   License along with this program; if not, write to the
00015  *   Free Software Foundation, Inc.,
00016  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00017  */
00018 
00019 #include "locationrunner.h"
00020 
00021 #include <QAction>
00022 #include <QDir>
00023 #include <QStringList>
00024 
00025 #include <KDebug>
00026 #include <KRun>
00027 #include <KLocale>
00028 #include <KMimeType>
00029 #include <KShell>
00030 #include <KToolInvocation>
00031 #include <KUrl>
00032 #include <KIcon>
00033 #include <KProtocolInfo>
00034 
00035 #include <kservicetypetrader.h>
00036 
00037 
00038 LocationsRunner::LocationsRunner(QObject *parent, const QVariantList& args)
00039     : Plasma::AbstractRunner(parent, args)
00040 {
00041     KGlobal::locale()->insertCatalog("krunner_locationsrunner");
00042     Q_UNUSED(args);
00043     // set the name shown after the result in krunner window
00044     setObjectName(i18n("Locations"));
00045     setIgnoredTypes(Plasma::RunnerContext::Executable | Plasma::RunnerContext::ShellCommand);
00046 }
00047 
00048 LocationsRunner::~LocationsRunner()
00049 {
00050 }
00051 
00052 static void processUrl(KUrl &url, const QString &term)
00053 {
00054     if (url.protocol().isEmpty()) {
00055         int idx = term.indexOf('/');
00056         url.clear();
00057         url.setHost(term.left(idx));
00058         if (idx != -1) {
00059             url.setPath(term.mid(idx));
00060         }
00061         url.setProtocol("http");
00062     }
00063 }
00064 
00065 void LocationsRunner::match(Plasma::RunnerContext &context)
00066 {
00067     QString term = context.query();
00068     Plasma::RunnerContext::Type type = context.type();
00069 
00070     if (type == Plasma::RunnerContext::Directory ||
00071         type == Plasma::RunnerContext::File) {
00072         Plasma::QueryMatch match(this);
00073         match.setType(Plasma::QueryMatch::ExactMatch);
00074         match.setText(i18n("Open %1", term));
00075         match.setIcon(KIcon("system-file-manager"));
00076         match.setRelevance(1);
00077         match.setType(Plasma::QueryMatch::ExactMatch);
00078 
00079         if (type == Plasma::RunnerContext::Directory) {
00080             match.setId("opendir");
00081         } else {
00082             match.setId("openfile");
00083         }
00084         context.addMatch(term, match);
00085     } else if (type == Plasma::RunnerContext::Help) {
00086         //kDebug() << "Locations matching because of" << type;
00087         Plasma::QueryMatch match(this);
00088         match.setType(Plasma::QueryMatch::ExactMatch);
00089         match.setText(i18n("Open %1", term));
00090         match.setIcon(KIcon("system-help"));
00091         match.setRelevance(1);
00092         match.setRelevance(1);
00093         match.setType(Plasma::QueryMatch::ExactMatch);
00094         match.setId("help");
00095         context.addMatch(term, match);
00096     } else if (type == Plasma::RunnerContext::NetworkLocation ||
00097                (type == Plasma::RunnerContext::UnknownType &&
00098                 term.contains(QRegExp("^[a-zA-Z0-9]+\\.")))) {
00099         KUrl url(term);
00100         processUrl(url, term);
00101         QMutexLocker lock(bigLock());
00102         if (!KProtocolInfo::isKnownProtocol(url.protocol())) {
00103             return;
00104         }
00105 
00106         Plasma::QueryMatch match(this);
00107         match.setText(i18n("Go to %1", url.prettyUrl()));
00108         match.setIcon(KIcon(KProtocolInfo::icon(url.protocol())));
00109         match.setData(url.url());
00110 
00111         if (KProtocolInfo::isHelperProtocol(url.protocol())) {
00112             //kDebug() << "helper protocol" << url.protocol() <<"call external application" ; 
00113             match.setText(i18n("Launch with %1", KProtocolInfo::exec(url.protocol())));
00114         } else {
00115             //kDebug() << "protocol managed by browser" << url.protocol();
00116             match.setText(i18n("Go to %1", url.prettyUrl()));
00117         }
00118 
00119         if (type == Plasma::RunnerContext::UnknownType) {
00120             match.setId("openunknown");
00121             match.setRelevance(0);
00122             match.setType(Plasma::QueryMatch::PossibleMatch);
00123         } else {
00124             match.setId("opennetwork");
00125             match.setRelevance(1);
00126             match.setType(Plasma::QueryMatch::ExactMatch);
00127         }
00128 
00129         context.addMatch(term, match);
00130     }
00131 }
00132 
00133 void LocationsRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
00134 {
00135     QString data = match.data().toString();
00136     const QString location = context.query();
00137     Plasma::RunnerContext::Type type = context.type();
00138 
00139     //kDebug() << "command: " << match.query();
00140     //kDebug() << "url: " << location << data;
00141 
00142     if ((type == Plasma::RunnerContext::NetworkLocation || type == Plasma::RunnerContext::UnknownType) &&
00143         data.startsWith("http://")) {
00144         // the text may have changed while we were running, so we have to refresh
00145         // our content
00146         KUrl url(location);
00147         processUrl(url, location);
00148         KToolInvocation::invokeBrowser(url.url());
00149     } else if (type == Plasma::RunnerContext::NetworkLocation) {
00150         KToolInvocation::invokeBrowser(location);
00151     } else {
00152         new KRun(QDir::cleanPath(KShell::tildeExpand(location)), 0);
00153     }
00154 }
00155 
00156 #include "locationrunner.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
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
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