~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/desktop/applets/kickoff/core/urlitemlauncher.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright 2007 Robert Knight <robertknight@gmail.com>
 
3
    Copyright 2007 Kevin Ottens <ervin@kde.org>
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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.
 
19
*/
 
20
 
 
21
#include "urlitemlauncher.h"
 
22
 
 
23
// Qt
 
24
#include <QFileInfo>
 
25
#include <QHash>
 
26
#include <QModelIndex>
 
27
 
 
28
// KDE
 
29
#include <KAuthorized>
 
30
#include <KDebug>
 
31
#include <KRun>
 
32
#include <KUrl>
 
33
#include <Solid/Device>
 
34
#include <Solid/StorageAccess>
 
35
 
 
36
// Local
 
37
#include "core/models.h"
 
38
#include "core/urlitemlauncher.h"
 
39
 
 
40
// DBus
 
41
#include "krunner_interface.h"
 
42
 
 
43
using namespace Kickoff;
 
44
 
 
45
class HandlerInfo
 
46
{
 
47
public:
 
48
    HandlerInfo() : type(UrlItemLauncher::ProtocolHandler) , handler(0) {}
 
49
    UrlItemLauncher::HandlerType type;
 
50
    UrlItemHandler *handler;
 
51
};
 
52
 
 
53
class GenericItemHandler : public UrlItemHandler
 
54
{
 
55
public:
 
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());
 
60
            krunner.display();
 
61
            return true;
 
62
        }
 
63
 
 
64
        new KRun(url, 0);
 
65
        return true;
 
66
    }
 
67
};
 
68
 
 
69
class UrlItemLauncher::Private
 
70
{
 
71
public:
 
72
    static QHash<QString, HandlerInfo> globalHandlers;
 
73
    static GenericItemHandler genericHandler;
 
74
 
 
75
    static bool openUrl(const QString &urlString) {
 
76
        kDebug() << "Opening item with URL" << urlString;
 
77
 
 
78
        KUrl url(urlString);
 
79
        HandlerInfo protocolHandler = globalHandlers[url.scheme()];
 
80
        if (protocolHandler.type == ProtocolHandler && protocolHandler.handler != 0) {
 
81
            return protocolHandler.handler->openUrl(url);
 
82
        }
 
83
 
 
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);
 
88
        }
 
89
 
 
90
        return genericHandler.openUrl(url);
 
91
    }
 
92
};
 
93
 
 
94
QHash<QString, HandlerInfo> UrlItemLauncher::Private::globalHandlers;
 
95
GenericItemHandler UrlItemLauncher::Private::genericHandler;
 
96
 
 
97
UrlItemLauncher::UrlItemLauncher(QObject *parent)
 
98
        : QObject(parent)
 
99
        , d(new Private)
 
100
{
 
101
}
 
102
 
 
103
UrlItemLauncher::~UrlItemLauncher()
 
104
{
 
105
    delete d;
 
106
}
 
107
 
 
108
bool UrlItemLauncher::openItem(const QModelIndex& index)
 
109
{
 
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>();
 
116
 
 
117
            if (access && !access->isAccessible()) {
 
118
                connect(access, SIGNAL(setupDone(Solid::ErrorType, QVariant, QString)),
 
119
                        this, SLOT(onSetupDone(Solid::ErrorType, QVariant, QString)));
 
120
                access->setup();
 
121
                return true;
 
122
            }
 
123
        }
 
124
 
 
125
        kDebug() << "Item" << index.data(Qt::DisplayRole) << "has no URL to open.";
 
126
        return false;
 
127
    }
 
128
 
 
129
    return Private::openUrl(urlString);
 
130
}
 
131
 
 
132
bool UrlItemLauncher::openUrl(const QString& url)
 
133
{
 
134
    return Private::openUrl(url);
 
135
}
 
136
 
 
137
void UrlItemLauncher::onSetupDone(Solid::ErrorType error, QVariant errorData, const QString &udi)
 
138
{
 
139
    Q_UNUSED(errorData);
 
140
 
 
141
    if (error != Solid::NoError) {
 
142
        return;
 
143
    }
 
144
 
 
145
    Solid::Device device(udi);
 
146
    Solid::StorageAccess *access = device.as<Solid::StorageAccess>();
 
147
 
 
148
    Q_ASSERT(access);
 
149
 
 
150
    QString urlString = "file://" + access->filePath();
 
151
    Private::openUrl(urlString);
 
152
}
 
153
 
 
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)
 
157
{
 
158
    HandlerInfo info;
 
159
    info.type = type;
 
160
    info.handler = handler;
 
161
    Private::globalHandlers.insert(name, info);
 
162
}
 
163
 
 
164
 
 
165
#include "urlitemlauncher.moc"