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>
32 #include <Solid/Device>
33 #include <Solid/StorageAccess>
36 #include "core/models.h"
37 #include "core/urlitemlauncher.h"
39 using namespace Kickoff
;
44 HandlerInfo() : type(UrlItemLauncher::ProtocolHandler
) , handler(0) {}
45 UrlItemLauncher::HandlerType type
;
46 UrlItemHandler
*handler
;
49 class GenericItemHandler
: public UrlItemHandler
52 virtual bool openUrl(const KUrl
& url
) {
58 class UrlItemLauncher::Private
61 static QHash
<QString
, HandlerInfo
> globalHandlers
;
62 static GenericItemHandler genericHandler
;
64 static bool openUrl(const QString
&urlString
) {
65 kDebug() << "Opening item with 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
)
92 UrlItemLauncher::~UrlItemLauncher()
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
)));
114 kDebug() << "Item" << index
.data(Qt::DisplayRole
) << "has no URL to open.";
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
)
130 if (error
!= Solid::NoError
) {
134 Solid::Device
device(udi
);
135 Solid::StorageAccess
*access
= device
.as
<Solid::StorageAccess
>();
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
)
149 info
.handler
= handler
;
150 Private::globalHandlers
.insert(name
, info
);
154 #include "urlitemlauncher.moc"