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 {
00054 new KRun(url,0);
00055 return true;
00056 }
00057 };
00058
00059 class UrlItemLauncher::Private
00060 {
00061 public:
00062 static QHash<QString,HandlerInfo> globalHandlers;
00063 static GenericItemHandler genericHandler;
00064
00065 static bool openUrl(const QString &urlString)
00066 {
00067 kDebug() << "Opening item with URL" << urlString;
00068
00069 KUrl url(urlString);
00070 HandlerInfo protocolHandler = globalHandlers[url.scheme()];
00071 if (protocolHandler.type == ProtocolHandler && protocolHandler.handler != 0) {
00072 return protocolHandler.handler->openUrl(url);
00073 }
00074
00075 QString extension = QFileInfo(url.path()).suffix();
00076 HandlerInfo extensionHandler = globalHandlers[extension];
00077 if (extensionHandler.type == ExtensionHandler && extensionHandler.handler != 0) {
00078 return extensionHandler.handler->openUrl(url);
00079 }
00080
00081 return genericHandler.openUrl(url);
00082 }
00083 };
00084
00085 QHash<QString,HandlerInfo> UrlItemLauncher::Private::globalHandlers;
00086 GenericItemHandler UrlItemLauncher::Private::genericHandler;
00087
00088 UrlItemLauncher::UrlItemLauncher(QObject *parent)
00089 : QObject(parent)
00090 , d(new Private)
00091 {
00092 }
00093
00094 UrlItemLauncher::~UrlItemLauncher()
00095 {
00096 delete d;
00097 }
00098
00099 bool UrlItemLauncher::openItem(const QModelIndex& index)
00100 {
00101 QString urlString = index.data(UrlRole).value<QString>();
00102 if (urlString.isEmpty()) {
00103 QString udi = index.data(DeviceUdiRole).toString();
00104 if (!udi.isEmpty()) {
00105 Solid::Device device(udi);
00106 Solid::StorageAccess *access = device.as<Solid::StorageAccess>();
00107
00108 if (access && !access->isAccessible()) {
00109 connect(access, SIGNAL(setupDone(Solid::ErrorType, QVariant, QString)),
00110 this, SLOT(onSetupDone(Solid::ErrorType, QVariant, QString)));
00111 access->setup();
00112 return true;
00113 }
00114 }
00115
00116 kDebug() << "Item" << index.data(Qt::DisplayRole) << "has no URL to open.";
00117 return false;
00118 }
00119
00120 return Private::openUrl(urlString);
00121 }
00122
00123 bool UrlItemLauncher::openUrl(const QString& url)
00124 {
00125 return Private::openUrl(url);
00126 }
00127
00128 void UrlItemLauncher::onSetupDone(Solid::ErrorType error, QVariant errorData, const QString &udi)
00129 {
00130 Q_UNUSED(errorData);
00131
00132 if (error!=Solid::NoError) {
00133 return;
00134 }
00135
00136 Solid::Device device(udi);
00137 Solid::StorageAccess *access = device.as<Solid::StorageAccess>();
00138
00139 Q_ASSERT(access);
00140
00141 QString urlString = "file://"+access->filePath();
00142 Private::openUrl(urlString);
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"