• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Applets

urlitemlauncher.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2007 Robert Knight <robertknight@gmail.com>
00003     Copyright 2007 Kevin Ottens <ervin@kde.org>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "urlitemlauncher.h"
00022 
00023 // Qt
00024 #include <QFileInfo>
00025 #include <QHash>
00026 #include <QModelIndex>
00027 
00028 // KDE
00029 #include <KDebug>
00030 #include <KRun>
00031 #include <KUrl>
00032 #include <Solid/Device>
00033 #include <Solid/StorageAccess>
00034 
00035 // Local
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 // FIXME: the handlers are leaked, as they are added with each new Kickoff instance,
00144 //        but never deleted.
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"

Applets

Skip menu "Applets"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal