add more spacing
[personal-kdebase.git] / workspace / plasma / applets / kickoff / core / urlitemlauncher.cpp
blobff46dc4bdcb71d42f0c117c217a438d360b8c1e5
1 /*
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"
23 // Qt
24 #include <QFileInfo>
25 #include <QHash>
26 #include <QModelIndex>
28 // KDE
29 #include <KDebug>
30 #include <KRun>
31 #include <KUrl>
32 #include <Solid/Device>
33 #include <Solid/StorageAccess>
35 // Local
36 #include "core/models.h"
37 #include "core/urlitemlauncher.h"
39 using namespace Kickoff;
41 class HandlerInfo
43 public:
44 HandlerInfo() : type(UrlItemLauncher::ProtocolHandler) , handler(0) {}
45 UrlItemLauncher::HandlerType type;
46 UrlItemHandler *handler;
49 class GenericItemHandler : public UrlItemHandler
51 public:
52 virtual bool openUrl(const KUrl& url) {
53 new KRun(url, 0);
54 return true;
58 class UrlItemLauncher::Private
60 public:
61 static QHash<QString, HandlerInfo> globalHandlers;
62 static GenericItemHandler genericHandler;
64 static bool openUrl(const QString &urlString) {
65 kDebug() << "Opening item with URL" << urlString;
67 KUrl url(urlString);
68 HandlerInfo protocolHandler = globalHandlers[url.scheme()];
69 if (protocolHandler.type == ProtocolHandler && protocolHandler.handler != 0) {
70 return protocolHandler.handler->openUrl(url);
73 QString extension = QFileInfo(url.path()).suffix();
74 HandlerInfo extensionHandler = globalHandlers[extension];
75 if (extensionHandler.type == ExtensionHandler && extensionHandler.handler != 0) {
76 return extensionHandler.handler->openUrl(url);
79 return genericHandler.openUrl(url);
83 QHash<QString, HandlerInfo> UrlItemLauncher::Private::globalHandlers;
84 GenericItemHandler UrlItemLauncher::Private::genericHandler;
86 UrlItemLauncher::UrlItemLauncher(QObject *parent)
87 : QObject(parent)
88 , d(new Private)
92 UrlItemLauncher::~UrlItemLauncher()
94 delete d;
97 bool UrlItemLauncher::openItem(const QModelIndex& index)
99 QString urlString = index.data(UrlRole).value<QString>();
100 if (urlString.isEmpty()) {
101 QString udi = index.data(DeviceUdiRole).toString();
102 if (!udi.isEmpty()) {
103 Solid::Device device(udi);
104 Solid::StorageAccess *access = device.as<Solid::StorageAccess>();
106 if (access && !access->isAccessible()) {
107 connect(access, SIGNAL(setupDone(Solid::ErrorType, QVariant, QString)),
108 this, SLOT(onSetupDone(Solid::ErrorType, QVariant, QString)));
109 access->setup();
110 return true;
114 kDebug() << "Item" << index.data(Qt::DisplayRole) << "has no URL to open.";
115 return false;
118 return Private::openUrl(urlString);
121 bool UrlItemLauncher::openUrl(const QString& url)
123 return Private::openUrl(url);
126 void UrlItemLauncher::onSetupDone(Solid::ErrorType error, QVariant errorData, const QString &udi)
128 Q_UNUSED(errorData);
130 if (error != Solid::NoError) {
131 return;
134 Solid::Device device(udi);
135 Solid::StorageAccess *access = device.as<Solid::StorageAccess>();
137 Q_ASSERT(access);
139 QString urlString = "file://" + access->filePath();
140 Private::openUrl(urlString);
143 // FIXME: the handlers are leaked, as they are added with each new Kickoff instance,
144 // but never deleted.
145 void UrlItemLauncher::addGlobalHandler(HandlerType type, const QString& name, UrlItemHandler *handler)
147 HandlerInfo info;
148 info.type = type;
149 info.handler = handler;
150 Private::globalHandlers.insert(name, info);
154 #include "urlitemlauncher.moc"