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

Applets

models.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2007 Robert Knight <robertknight@gmail.com>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 // Own
00021 #include "core/models.h"
00022 #include "core/leavemodel.h"
00023 
00024 // Qt
00025 #include <QFileInfo>
00026 #include <QStandardItem>
00027 #include <QDir>
00028 
00029 // KDE
00030 #include <KDebug>
00031 #include <KConfigGroup>
00032 #include <KDesktopFile>
00033 #include <KIcon>
00034 #include <KGlobal>
00035 #include <KMimeType>
00036 #include <KUrl>
00037 #include <Solid/Device>
00038 #include <Solid/StorageAccess>
00039 #include <Solid/StorageDrive>
00040 
00041 using namespace Kickoff;
00042 
00043 namespace Kickoff
00044 {
00045 
00046 Q_GLOBAL_STATIC_WITH_ARGS(KUrl, homeUrl, (QDir::homePath()))
00047 Q_GLOBAL_STATIC_WITH_ARGS(KUrl, remoteUrl, ("remote:/"))
00048 K_GLOBAL_STATIC(StandardItemFactoryData, factoryData)
00049 
00050 StandardItemFactoryData* deviceFactoryData()
00051 {
00052     return factoryData;
00053 }
00054 } // namespace Kickoff
00055 
00056 QStandardItem *StandardItemFactory::createItemForUrl(const QString& urlString)
00057 {
00058     KUrl url(urlString);
00059 
00060     QStandardItem *item = 0;
00061 
00062     if (url.isLocalFile() && urlString.endsWith(".desktop")) {
00063         // .desktop files may be services (type field == 'Application' or 'Service')
00064         // or they may be other types such as links.
00065         //
00066         // first look in the KDE service database to see if this file is a service,
00067         // otherwise represent it as a generic .desktop file
00068         KService::Ptr service = KService::serviceByDesktopPath(url.path());
00069         if (service) {
00070             return createItemForService(service);
00071         }
00072 
00073         item = new QStandardItem;
00074         KDesktopFile desktopFile(url.path());
00075         item->setText(QFileInfo(urlString.mid(0, urlString.lastIndexOf('.'))).completeBaseName());
00076         item->setIcon(KIcon(desktopFile.readIcon()));
00077 
00078         //FIXME: desktopUrl is a hack around borkage in KRecentDocuments which
00079         //       stores a path in the URL field!
00080         KUrl desktopUrl(desktopFile.desktopGroup().readPathEntry("URL", QString()));
00081         if (!desktopUrl.url().isEmpty()) {
00082             item->setData(desktopUrl.url(), Kickoff::UrlRole);
00083         } else {
00084             // desktopUrl.url() is empty if the file doesn't exist so set the
00085             // url role to that which was passed so that the item can still be
00086             // manually removed
00087             item->setData(urlString, Kickoff::UrlRole);
00088         }
00089 
00090         QString subTitle = desktopUrl.isLocalFile() ? desktopUrl.path() : desktopUrl.prettyUrl();
00091         item->setData(subTitle, Kickoff::SubTitleRole);
00092 
00093         setSpecialUrlProperties(desktopUrl, item);
00094     } else if (url.scheme() == "leave") {
00095         item = LeaveModel::createStandardItem(urlString);
00096     } else {
00097         item = new QStandardItem;
00098         const QString subTitle = url.isLocalFile() ? url.path() : url.prettyUrl();
00099         QString basename = QFileInfo(urlString).completeBaseName();
00100         if (basename.isNull())
00101             basename = subTitle;
00102         item->setText(basename);
00103         item->setIcon(KIcon(KMimeType::iconNameForUrl(url)));
00104         item->setData(url.url(), Kickoff::UrlRole);
00105         item->setData(subTitle, Kickoff::SubTitleRole);
00106 
00107         setSpecialUrlProperties(url, item);
00108     }
00109 
00110     return item;
00111 }
00112 
00113 void StandardItemFactory::setSpecialUrlProperties(const KUrl& url, QStandardItem *item)
00114 {
00115     // specially handled URLs
00116     if (homeUrl() && url == *homeUrl()) {
00117         item->setText(i18n("Home Folder"));
00118         item->setIcon(KIcon("user-home"));
00119     } else if (remoteUrl() && url == *remoteUrl()) {
00120         item->setText(i18n("Network Folders"));
00121     }
00122 }
00123 
00124 QStandardItem *StandardItemFactory::createItemForService(KService::Ptr service)
00125 {
00126     QStandardItem *appItem = new QStandardItem;
00127 
00128     QString genericName = service->genericName();
00129     QString appName = service->name();
00130 
00131     appItem->setText(genericName.isEmpty() ? appName : genericName);
00132     appItem->setIcon(KIcon(service->icon()));
00133     appItem->setData(service->entryPath(), Kickoff::UrlRole);
00134 
00135     if (!genericName.isEmpty()) {
00136         appItem->setData(service->name(), Kickoff::SubTitleRole);
00137     }
00138 
00139     return appItem;
00140 }
00141 
00142 bool Kickoff::isLaterVersion(KService::Ptr first , KService::Ptr second)
00143 {
00144     // a very crude heuristic using the .desktop path names
00145     // which only understands kde3 vs kde4
00146     bool firstIsKde4 = first->entryPath().contains("kde4");
00147     bool secondIsKde4 = second->entryPath().contains("kde4");
00148 
00149     return firstIsKde4 && !secondIsKde4;
00150 }
00151 
00152 QStringList Kickoff::systemApplicationList()
00153 {
00154     KConfigGroup appsGroup = componentData().config()->group("SystemApplications");
00155     QStringList apps;
00156     apps << "systemsettings";
00157     apps = appsGroup.readEntry("DesktopFiles", apps);
00158     return apps;
00159 }
00160 
00161 #if 0
00162 void Kickoff::swapModelIndexes(QModelIndex& first, QModelIndex& second)
00163 {
00164     Q_ASSERT(first.isValid());
00165     Q_ASSERT(second.isValid());
00166 
00167     QAbstractItemModel *firstModel = const_cast<QAbstractItemModel*>(first.model());
00168     QAbstractItemModel *secondModel = const_cast<QAbstractItemModel*>(second.model());
00169 
00170     Q_ASSERT(firstModel && secondModel);
00171 
00172     QMap<int, QVariant> firstIndexData = firstModel->itemData(first);
00173     QMap<int, QVariant> secondIndexData = secondModel->itemData(second);
00174 
00175     firstModel->setItemData(first, secondIndexData);
00176     secondModel->setItemData(second, firstIndexData);
00177 }
00178 #endif
00179 
00180 K_GLOBAL_STATIC_WITH_ARGS(KComponentData, kickoffComponent, ("kickoff", QByteArray(), KComponentData::SkipMainComponentRegistration))
00181 KComponentData Kickoff::componentData()
00182 {
00183     return *kickoffComponent;
00184 }
00185 
00186 
00187 

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