• Skip to content
  • Skip to link menu
KDE 4.1 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     {
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"

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
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
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