• Skip to content
  • Skip to link menu
KDE 4.1 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     }
00095     else if (url.scheme() == "leave") {
00096         item = LeaveModel::createStandardItem(urlString);
00097     }
00098     else {
00099         item = new QStandardItem;
00100         const QString subTitle = url.isLocalFile() ? url.path() : url.prettyUrl();
00101         QString basename = QFileInfo(urlString).completeBaseName();
00102         if (basename.isNull())
00103             basename = subTitle;
00104         item->setText(basename);
00105         item->setIcon(KIcon(KMimeType::iconNameForUrl(url)));
00106         item->setData(url.url(), Kickoff::UrlRole);
00107         item->setData(subTitle, Kickoff::SubTitleRole);
00108 
00109         setSpecialUrlProperties(url, item);
00110     }
00111 
00112     return item;
00113 }
00114 
00115 void StandardItemFactory::setSpecialUrlProperties(const KUrl& url,QStandardItem *item)
00116 {
00117     // specially handled URLs
00118     if (homeUrl() && url == *homeUrl()) {
00119         item->setText(i18n("Home Folder"));
00120         item->setIcon(KIcon("user-home"));
00121     } else if (remoteUrl() && url == *remoteUrl()) {
00122         item->setText(i18n("Network Folders"));
00123     }
00124 }
00125 
00126 QStandardItem *StandardItemFactory::createItemForService(KService::Ptr service)
00127 {
00128     QStandardItem *appItem = new QStandardItem;
00129 
00130     QString genericName = service->genericName();
00131     QString appName = service->name();
00132 
00133     appItem->setText(genericName.isEmpty() ? appName : genericName);
00134     appItem->setIcon(KIcon(service->icon()));
00135     appItem->setData(service->entryPath(),Kickoff::UrlRole);
00136 
00137     if (!genericName.isEmpty()) {
00138         appItem->setData(service->name(),Kickoff::SubTitleRole);
00139     }
00140 
00141     return appItem;
00142 }
00143 
00144 bool Kickoff::isLaterVersion(KService::Ptr first , KService::Ptr second)
00145 {
00146     // a very crude heuristic using the .desktop path names
00147     // which only understands kde3 vs kde4
00148     bool firstIsKde4 = first->entryPath().contains("kde4");
00149     bool secondIsKde4 = second->entryPath().contains("kde4");
00150 
00151     return firstIsKde4 && !secondIsKde4;
00152 }
00153 
00154 QStringList Kickoff::systemApplicationList()
00155 {
00156     KConfigGroup appsGroup = componentData().config()->group("SystemApplications");
00157     QStringList apps;
00158     apps << "systemsettings";
00159     apps = appsGroup.readEntry("DesktopFiles", apps);
00160     return apps;
00161 }
00162 
00163 #if 0
00164 void Kickoff::swapModelIndexes(QModelIndex& first,QModelIndex& second)
00165 {
00166     Q_ASSERT(first.isValid());
00167     Q_ASSERT(second.isValid());
00168 
00169     QAbstractItemModel *firstModel = const_cast<QAbstractItemModel*>(first.model());
00170     QAbstractItemModel *secondModel = const_cast<QAbstractItemModel*>(second.model());
00171 
00172     Q_ASSERT(firstModel && secondModel);
00173 
00174     QMap<int,QVariant> firstIndexData = firstModel->itemData(first);
00175     QMap<int,QVariant> secondIndexData = secondModel->itemData(second);
00176 
00177     firstModel->setItemData(first,secondIndexData);
00178     secondModel->setItemData(second,firstIndexData);
00179 }
00180 #endif
00181 
00182 K_GLOBAL_STATIC_WITH_ARGS(KComponentData,kickoffComponent,("kickoff",QByteArray(),KComponentData::SkipMainComponentRegistration))
00183 KComponentData Kickoff::componentData()
00184 {
00185     return *kickoffComponent;
00186 }
00187 
00188 
00189 

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