add more spacing
[personal-kdebase.git] / workspace / plasma / applets / kickoff / core / models.cpp
blob0d5ba811640bd11810f369fc2b6dd34ff53779a5
1 /*
2 Copyright 2007 Robert Knight <robertknight@gmail.com>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 // Own
21 #include "core/models.h"
22 #include "core/leavemodel.h"
24 // Qt
25 #include <QFileInfo>
26 #include <QStandardItem>
27 #include <QDir>
29 // KDE
30 #include <KDebug>
31 #include <KConfigGroup>
32 #include <KDesktopFile>
33 #include <KIcon>
34 #include <KGlobal>
35 #include <KMimeType>
36 #include <KUrl>
37 #include <Solid/Device>
38 #include <Solid/StorageAccess>
39 #include <Solid/StorageDrive>
41 using namespace Kickoff;
43 namespace Kickoff
46 Q_GLOBAL_STATIC_WITH_ARGS(KUrl, homeUrl, (QDir::homePath()))
47 Q_GLOBAL_STATIC_WITH_ARGS(KUrl, remoteUrl, ("remote:/"))
48 K_GLOBAL_STATIC(StandardItemFactoryData, factoryData)
50 StandardItemFactoryData* deviceFactoryData()
52 return factoryData;
54 } // namespace Kickoff
56 QStandardItem *StandardItemFactory::createItemForUrl(const QString& urlString)
58 KUrl url(urlString);
60 QStandardItem *item = 0;
62 if (url.isLocalFile() && urlString.endsWith(".desktop")) {
63 // .desktop files may be services (type field == 'Application' or 'Service')
64 // or they may be other types such as links.
66 // first look in the KDE service database to see if this file is a service,
67 // otherwise represent it as a generic .desktop file
68 KService::Ptr service = KService::serviceByDesktopPath(url.path());
69 if (service) {
70 return createItemForService(service);
73 item = new QStandardItem;
74 KDesktopFile desktopFile(url.path());
75 item->setText(QFileInfo(urlString.mid(0, urlString.lastIndexOf('.'))).completeBaseName());
76 item->setIcon(KIcon(desktopFile.readIcon()));
78 //FIXME: desktopUrl is a hack around borkage in KRecentDocuments which
79 // stores a path in the URL field!
80 KUrl desktopUrl(desktopFile.desktopGroup().readPathEntry("URL", QString()));
81 if (!desktopUrl.url().isEmpty()) {
82 item->setData(desktopUrl.url(), Kickoff::UrlRole);
83 } else {
84 // desktopUrl.url() is empty if the file doesn't exist so set the
85 // url role to that which was passed so that the item can still be
86 // manually removed
87 item->setData(urlString, Kickoff::UrlRole);
90 QString subTitle = desktopUrl.isLocalFile() ? desktopUrl.path() : desktopUrl.prettyUrl();
91 item->setData(subTitle, Kickoff::SubTitleRole);
93 setSpecialUrlProperties(desktopUrl, item);
94 } else if (url.scheme() == "leave") {
95 item = LeaveModel::createStandardItem(urlString);
96 } else {
97 item = new QStandardItem;
98 const QString subTitle = url.isLocalFile() ? url.path() : url.prettyUrl();
99 QString basename = QFileInfo(urlString).completeBaseName();
100 if (basename.isNull())
101 basename = subTitle;
102 item->setText(basename);
103 item->setIcon(KIcon(KMimeType::iconNameForUrl(url)));
104 item->setData(url.url(), Kickoff::UrlRole);
105 item->setData(subTitle, Kickoff::SubTitleRole);
107 setSpecialUrlProperties(url, item);
110 return item;
113 void StandardItemFactory::setSpecialUrlProperties(const KUrl& url, QStandardItem *item)
115 // specially handled URLs
116 if (homeUrl() && url == *homeUrl()) {
117 item->setText(i18n("Home Folder"));
118 item->setIcon(KIcon("user-home"));
119 } else if (remoteUrl() && url == *remoteUrl()) {
120 item->setText(i18n("Network Folders"));
124 QStandardItem *StandardItemFactory::createItemForService(KService::Ptr service)
126 QStandardItem *appItem = new QStandardItem;
128 QString genericName = service->genericName();
129 QString appName = service->name();
131 appItem->setText(genericName.isEmpty() ? appName : genericName);
132 appItem->setIcon(KIcon(service->icon()));
133 appItem->setData(service->entryPath(), Kickoff::UrlRole);
135 if (!genericName.isEmpty()) {
136 appItem->setData(service->name(), Kickoff::SubTitleRole);
139 return appItem;
142 bool Kickoff::isLaterVersion(KService::Ptr first , KService::Ptr second)
144 // a very crude heuristic using the .desktop path names
145 // which only understands kde3 vs kde4
146 bool firstIsKde4 = first->entryPath().contains("kde4");
147 bool secondIsKde4 = second->entryPath().contains("kde4");
149 return firstIsKde4 && !secondIsKde4;
152 QStringList Kickoff::systemApplicationList()
154 KConfigGroup appsGroup = componentData().config()->group("SystemApplications");
155 QStringList apps;
156 apps << "systemsettings";
157 apps = appsGroup.readEntry("DesktopFiles", apps);
158 return apps;
161 #if 0
162 void Kickoff::swapModelIndexes(QModelIndex& first, QModelIndex& second)
164 Q_ASSERT(first.isValid());
165 Q_ASSERT(second.isValid());
167 QAbstractItemModel *firstModel = const_cast<QAbstractItemModel*>(first.model());
168 QAbstractItemModel *secondModel = const_cast<QAbstractItemModel*>(second.model());
170 Q_ASSERT(firstModel && secondModel);
172 QMap<int, QVariant> firstIndexData = firstModel->itemData(first);
173 QMap<int, QVariant> secondIndexData = secondModel->itemData(second);
175 firstModel->setItemData(first, secondIndexData);
176 secondModel->setItemData(second, firstIndexData);
178 #endif
180 K_GLOBAL_STATIC_WITH_ARGS(KComponentData, kickoffComponent, ("kickoff", QByteArray(), KComponentData::SkipMainComponentRegistration))
181 KComponentData Kickoff::componentData()
183 return *kickoffComponent;