2
Copyright 2007 Robert Knight <robertknight@gmail.com>
3
Copyright 2007 Kevin Ottens <ervin@kde.org>
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License as published by the Free Software Foundation; either
8
version 2 of the License, or (at your option) any later version.
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public License
16
along with this library; see the file COPYING.LIB. If not, write to
17
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
Boston, MA 02110-1301, USA.
21
#include "urlitemlauncher.h"
26
#include <QModelIndex>
29
#include <KAuthorized>
33
#include <Solid/Device>
34
#include <Solid/StorageAccess>
37
#include "core/models.h"
38
#include "core/urlitemlauncher.h"
41
#include "krunner_interface.h"
43
using namespace Kickoff;
48
HandlerInfo() : type(UrlItemLauncher::ProtocolHandler) , handler(0) {}
49
UrlItemLauncher::HandlerType type;
50
UrlItemHandler *handler;
53
class GenericItemHandler : public UrlItemHandler
56
virtual bool openUrl(const KUrl& url) {
57
if (url.protocol() == "run" && KAuthorized::authorize("run_command")) {
58
QString interface("org.kde.krunner");
59
org::kde::krunner::App krunner(interface, "/App", QDBusConnection::sessionBus());
69
class UrlItemLauncher::Private
72
static QHash<QString, HandlerInfo> globalHandlers;
73
static GenericItemHandler genericHandler;
75
static bool openUrl(const QString &urlString) {
76
kDebug() << "Opening item with URL" << urlString;
79
HandlerInfo protocolHandler = globalHandlers[url.scheme()];
80
if (protocolHandler.type == ProtocolHandler && protocolHandler.handler != 0) {
81
return protocolHandler.handler->openUrl(url);
84
QString extension = QFileInfo(url.path()).suffix();
85
HandlerInfo extensionHandler = globalHandlers[extension];
86
if (extensionHandler.type == ExtensionHandler && extensionHandler.handler != 0) {
87
return extensionHandler.handler->openUrl(url);
90
return genericHandler.openUrl(url);
94
QHash<QString, HandlerInfo> UrlItemLauncher::Private::globalHandlers;
95
GenericItemHandler UrlItemLauncher::Private::genericHandler;
97
UrlItemLauncher::UrlItemLauncher(QObject *parent)
103
UrlItemLauncher::~UrlItemLauncher()
108
bool UrlItemLauncher::openItem(const QModelIndex& index)
110
QString urlString = index.data(UrlRole).value<QString>();
111
if (urlString.isEmpty()) {
112
QString udi = index.data(DeviceUdiRole).toString();
113
if (!udi.isEmpty()) {
114
Solid::Device device(udi);
115
Solid::StorageAccess *access = device.as<Solid::StorageAccess>();
117
if (access && !access->isAccessible()) {
118
connect(access, SIGNAL(setupDone(Solid::ErrorType, QVariant, QString)),
119
this, SLOT(onSetupDone(Solid::ErrorType, QVariant, QString)));
125
kDebug() << "Item" << index.data(Qt::DisplayRole) << "has no URL to open.";
129
return Private::openUrl(urlString);
132
bool UrlItemLauncher::openUrl(const QString& url)
134
return Private::openUrl(url);
137
void UrlItemLauncher::onSetupDone(Solid::ErrorType error, QVariant errorData, const QString &udi)
141
if (error != Solid::NoError) {
145
Solid::Device device(udi);
146
Solid::StorageAccess *access = device.as<Solid::StorageAccess>();
150
QString urlString = "file://" + access->filePath();
151
Private::openUrl(urlString);
154
// FIXME: the handlers are leaked, as they are added with each new Kickoff instance,
155
// but never deleted.
156
void UrlItemLauncher::addGlobalHandler(HandlerType type, const QString& name, UrlItemHandler *handler)
160
info.handler = handler;
161
Private::globalHandlers.insert(name, info);
165
#include "urlitemlauncher.moc"