Applets
urlitemlauncher.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "urlitemlauncher.h"
00022
00023
00024 #include <QFileInfo>
00025 #include <QHash>
00026 #include <QModelIndex>
00027
00028
00029 #include <KDebug>
00030 #include <KRun>
00031 #include <KUrl>
00032 #include <Solid/Device>
00033 #include <Solid/StorageAccess>
00034
00035
00036 #include "core/models.h"
00037 #include "core/urlitemlauncher.h"
00038
00039 using namespace Kickoff;
00040
00041 class HandlerInfo
00042 {
00043 public:
00044 HandlerInfo() : type(UrlItemLauncher::ProtocolHandler) , handler(0) {}
00045 UrlItemLauncher::HandlerType type;
00046 UrlItemHandler *handler;
00047 };
00048
00049 class GenericItemHandler : public UrlItemHandler
00050 {
00051 public:
00052 virtual bool openUrl(const KUrl& url) {
00053 new KRun(url, 0);
00054 return true;
00055 }
00056 };
00057
00058 class UrlItemLauncher::Private
00059 {
00060 public:
00061 static QHash<QString, HandlerInfo> globalHandlers;
00062 static GenericItemHandler genericHandler;
00063
00064 static bool openUrl(const QString &urlString) {
00065 kDebug() << "Opening item with URL" << urlString;
00066
00067 KUrl url(urlString);
00068 HandlerInfo protocolHandler = globalHandlers[url.scheme()];
00069 if (protocolHandler.type == ProtocolHandler && protocolHandler.handler != 0) {
00070 return protocolHandler.handler->openUrl(url);
00071 }
00072
00073 QString extension = QFileInfo(url.path()).suffix();
00074 HandlerInfo extensionHandler = globalHandlers[extension];
00075 if (extensionHandler.type == ExtensionHandler && extensionHandler.handler != 0) {
00076 return extensionHandler.handler->openUrl(url);
00077 }
00078
00079 return genericHandler.openUrl(url);
00080 }
00081 };
00082
00083 QHash<QString, HandlerInfo> UrlItemLauncher::Private::globalHandlers;
00084 GenericItemHandler UrlItemLauncher::Private::genericHandler;
00085
00086 UrlItemLauncher::UrlItemLauncher(QObject *parent)
00087 : QObject(parent)
00088 , d(new Private)
00089 {
00090 }
00091
00092 UrlItemLauncher::~UrlItemLauncher()
00093 {
00094 delete d;
00095 }
00096
00097 bool UrlItemLauncher::openItem(const QModelIndex& index)
00098 {
00099 QString urlString = index.data(UrlRole).value<QString>();
00100 if (urlString.isEmpty()) {
00101 QString udi = index.data(DeviceUdiRole).toString();
00102 if (!udi.isEmpty()) {
00103 Solid::Device device(udi);
00104 Solid::StorageAccess *access = device.as<Solid::StorageAccess>();
00105
00106 if (access && !access->isAccessible()) {
00107 connect(access, SIGNAL(setupDone(Solid::ErrorType, QVariant, QString)),
00108 this, SLOT(onSetupDone(Solid::ErrorType, QVariant, QString)));
00109 access->setup();
00110 return true;
00111 }
00112 }
00113
00114 kDebug() << "Item" << index.data(Qt::DisplayRole) << "has no URL to open.";
00115 return false;
00116 }
00117
00118 return Private::openUrl(urlString);
00119 }
00120
00121 bool UrlItemLauncher::openUrl(const QString& url)
00122 {
00123 return Private::openUrl(url);
00124 }
00125
00126 void UrlItemLauncher::onSetupDone(Solid::ErrorType error, QVariant errorData, const QString &udi)
00127 {
00128 Q_UNUSED(errorData);
00129
00130 if (error != Solid::NoError) {
00131 return;
00132 }
00133
00134 Solid::Device device(udi);
00135 Solid::StorageAccess *access = device.as<Solid::StorageAccess>();
00136
00137 Q_ASSERT(access);
00138
00139 QString urlString = "file://" + access->filePath();
00140 Private::openUrl(urlString);
00141 }
00142
00143
00144
00145 void UrlItemLauncher::addGlobalHandler(HandlerType type, const QString& name, UrlItemHandler *handler)
00146 {
00147 HandlerInfo info;
00148 info.type = type;
00149 info.handler = handler;
00150 Private::globalHandlers.insert(name, info);
00151 }
00152
00153
00154 #include "urlitemlauncher.moc"