Plasma
locationrunner.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 "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
00044 setObjectName("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
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\\-\\.]+\\.[a-zA-Z]{2,6}")))) {
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
00113 match.setText(i18n("Launch with %1", KProtocolInfo::exec(url.protocol())));
00114 } else {
00115
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.5);
00122 match.setType(Plasma::QueryMatch::PossibleMatch);
00123 } else {
00124 match.setId("opennetwork");
00125 match.setRelevance(0.7);
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 if (location.isEmpty()) {
00140 return;
00141 }
00142
00143
00144
00145
00146 KUrl urlToRun(location);
00147
00148 if ((type == Plasma::RunnerContext::NetworkLocation || type == Plasma::RunnerContext::UnknownType) &&
00149 data.startsWith("http://")) {
00150
00151
00152 processUrl(urlToRun, location);
00153 } else if (type != Plasma::RunnerContext::NetworkLocation) {
00154 QString path = QDir::cleanPath(KShell::tildeExpand(location));
00155
00156 if (path[0] != '/') {
00157 path.prepend('/').prepend(QDir::currentPath());
00158 }
00159
00160 urlToRun = path;
00161 }
00162
00163 new KRun(urlToRun, 0);
00164 }
00165
00166 #include "locationrunner.moc"