not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / runners / locations / locationrunner.cpp
blobcd2151f8d333ff5bfa7be6443d1e2c2ee29e47bd
1 /*
2 * Copyright (C) 2007 Teemu Rytilahti <tpr@iki.fi>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License version 2 as
6 * published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details
13 * You should have received a copy of the GNU Library General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "locationrunner.h"
21 #include <QAction>
22 #include <QDir>
23 #include <QStringList>
25 #include <KDebug>
26 #include <KRun>
27 #include <KLocale>
28 #include <KMimeType>
29 #include <KShell>
30 #include <KToolInvocation>
31 #include <KUrl>
32 #include <KIcon>
33 #include <KProtocolInfo>
35 #include <kservicetypetrader.h>
38 LocationsRunner::LocationsRunner(QObject *parent, const QVariantList& args)
39 : Plasma::AbstractRunner(parent, args)
41 KGlobal::locale()->insertCatalog("krunner_locationsrunner");
42 Q_UNUSED(args);
43 // set the name shown after the result in krunner window
44 setObjectName("Locations");
45 setIgnoredTypes(Plasma::RunnerContext::Executable | Plasma::RunnerContext::ShellCommand);
48 LocationsRunner::~LocationsRunner()
52 static void processUrl(KUrl &url, const QString &term)
54 if (url.protocol().isEmpty()) {
55 int idx = term.indexOf('/');
56 url.clear();
57 url.setHost(term.left(idx));
58 if (idx != -1) {
59 url.setPath(term.mid(idx));
61 url.setProtocol("http");
65 void LocationsRunner::match(Plasma::RunnerContext &context)
67 QString term = context.query();
68 Plasma::RunnerContext::Type type = context.type();
70 if (type == Plasma::RunnerContext::Directory ||
71 type == Plasma::RunnerContext::File) {
72 Plasma::QueryMatch match(this);
73 match.setType(Plasma::QueryMatch::ExactMatch);
74 match.setText(i18n("Open %1", term));
75 match.setIcon(KIcon("system-file-manager"));
76 match.setRelevance(1);
77 match.setType(Plasma::QueryMatch::ExactMatch);
79 if (type == Plasma::RunnerContext::Directory) {
80 match.setId("opendir");
81 } else {
82 match.setId("openfile");
84 context.addMatch(term, match);
85 } else if (type == Plasma::RunnerContext::Help) {
86 //kDebug() << "Locations matching because of" << type;
87 Plasma::QueryMatch match(this);
88 match.setType(Plasma::QueryMatch::ExactMatch);
89 match.setText(i18n("Open %1", term));
90 match.setIcon(KIcon("system-help"));
91 match.setRelevance(1);
92 match.setRelevance(1);
93 match.setType(Plasma::QueryMatch::ExactMatch);
94 match.setId("help");
95 context.addMatch(term, match);
96 } else if (type == Plasma::RunnerContext::NetworkLocation ||
97 (type == Plasma::RunnerContext::UnknownType &&
98 term.contains(QRegExp("^[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,6}")))) {
99 KUrl url(term);
100 processUrl(url, term);
101 QMutexLocker lock(bigLock());
102 if (!KProtocolInfo::isKnownProtocol(url.protocol())) {
103 return;
106 Plasma::QueryMatch match(this);
107 match.setText(i18n("Go to %1", url.prettyUrl()));
108 match.setIcon(KIcon(KProtocolInfo::icon(url.protocol())));
109 match.setData(url.url());
111 if (KProtocolInfo::isHelperProtocol(url.protocol())) {
112 //kDebug() << "helper protocol" << url.protocol() <<"call external application" ;
113 match.setText(i18n("Launch with %1", KProtocolInfo::exec(url.protocol())));
114 } else {
115 //kDebug() << "protocol managed by browser" << url.protocol();
116 match.setText(i18n("Go to %1", url.prettyUrl()));
119 if (type == Plasma::RunnerContext::UnknownType) {
120 match.setId("openunknown");
121 match.setRelevance(0.5);
122 match.setType(Plasma::QueryMatch::PossibleMatch);
123 } else {
124 match.setId("opennetwork");
125 match.setRelevance(0.7);
126 match.setType(Plasma::QueryMatch::ExactMatch);
129 context.addMatch(term, match);
133 void LocationsRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
135 QString data = match.data().toString();
136 const QString location = context.query();
137 Plasma::RunnerContext::Type type = context.type();
139 if (location.isEmpty()) {
140 return;
143 //kDebug() << "command: " << context.query();
144 //kDebug() << "url: " << location << data;
146 KUrl urlToRun(location);
148 if ((type == Plasma::RunnerContext::NetworkLocation || type == Plasma::RunnerContext::UnknownType) &&
149 data.startsWith("http://")) {
150 // the text may have changed while we were running, so we have to refresh
151 // our content
152 processUrl(urlToRun, location);
153 } else if (type != Plasma::RunnerContext::NetworkLocation) {
154 QString path = QDir::cleanPath(KShell::tildeExpand(location));
156 if (path[0] != '/') {
157 path.prepend('/').prepend(QDir::currentPath());
160 urlToRun = path;
163 new KRun(urlToRun, 0);
166 #include "locationrunner.moc"