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.
21
#include "core/models.h"
22
#include "core/leavemodel.h"
26
#include <QStandardItem>
31
#include <KConfigGroup>
32
#include <KDesktopFile>
36
#include <Solid/Device>
37
#include <Solid/StorageAccess>
38
#include <Solid/StorageDrive>
40
using namespace Kickoff;
45
Q_GLOBAL_STATIC_WITH_ARGS(KUrl, homeUrl, (QDir::homePath()))
46
Q_GLOBAL_STATIC_WITH_ARGS(KUrl, remoteUrl, ("remote:/"))
47
K_GLOBAL_STATIC(StandardItemFactoryData, factoryData)
49
StandardItemFactoryData* deviceFactoryData()
53
} // namespace Kickoff
55
QStandardItem *StandardItemFactory::createItemForUrl(const QString& urlString, DisplayOrder displayOrder)
59
QStandardItem *item = 0;
61
// Match files ending with ".desktop" and being local or having a relative
62
// path. For instance applications that still installs .desktop files at
63
// /usr/share/applnk, like KVirc 3
64
if (urlString.endsWith(QLatin1String(".desktop")) && (url.isLocalFile() || url.isRelative())) {
65
// .desktop files may be services (type field == 'Application' or 'Service')
66
// or they may be other types such as links.
68
// first look in the KDE service database to see if this file is a service,
69
// otherwise represent it as a generic .desktop file
70
KService::Ptr service = KService::serviceByDesktopPath(url.toLocalFile());
72
return createItemForService(service, displayOrder);
75
item = new QStandardItem;
76
KDesktopFile desktopFile(url.toLocalFile());
77
item->setText(QFileInfo(urlString.mid(0, urlString.lastIndexOf('.'))).completeBaseName());
78
item->setIcon(KIcon(desktopFile.readIcon()));
80
//FIXME: desktopUrl is a hack around borkage in KRecentDocuments which
81
// stores a path in the URL field!
82
KUrl desktopUrl(desktopFile.desktopGroup().readPathEntry("URL", QString()));
83
if (!desktopUrl.url().isEmpty()) {
84
item->setData(desktopUrl.url(), Kickoff::UrlRole);
86
// desktopUrl.url() is empty if the file doesn't exist so set the
87
// url role to that which was passed so that the item can still be
89
item->setData(urlString, Kickoff::UrlRole);
92
QString subTitle = desktopUrl.isLocalFile() ? desktopUrl.toLocalFile() : desktopUrl.prettyUrl();
93
item->setData(subTitle, Kickoff::SubTitleRole);
95
setSpecialUrlProperties(desktopUrl, item);
96
} else if (url.scheme() == "leave") {
97
item = LeaveModel::createStandardItem(urlString);
99
item = new QStandardItem;
100
const QString subTitle = url.isLocalFile() ? url.toLocalFile() : url.prettyUrl();
101
QString basename = QFileInfo(url.prettyUrl()).completeBaseName();
102
if (basename.isNull()) {
106
item->setText(basename);
107
item->setIcon(KIcon(KMimeType::iconNameForUrl(url)));
108
item->setData(url.url(), Kickoff::UrlRole);
109
item->setData(subTitle, Kickoff::SubTitleRole);
111
setSpecialUrlProperties(url, item);
117
void StandardItemFactory::setSpecialUrlProperties(const KUrl& url, QStandardItem *item)
119
// specially handled URLs
120
if (homeUrl() && url == *homeUrl()) {
121
item->setText(i18n("Home Folder"));
122
item->setIcon(KIcon("user-home"));
123
} else if (remoteUrl() && url == *remoteUrl()) {
124
item->setText(i18n("Network Folders"));
128
QStandardItem *StandardItemFactory::createItem(const QIcon & icon, const QString & title,
129
const QString & description, const QString & url)
131
QStandardItem *appItem = new QStandardItem;
133
appItem->setText(title);
134
appItem->setIcon(icon);
135
appItem->setData(description, Kickoff::SubTitleRole);
136
appItem->setData(url, Kickoff::UrlRole);
141
QStandardItem *StandardItemFactory::createItemForService(KService::Ptr service, DisplayOrder displayOrder)
143
QStandardItem *appItem = new QStandardItem;
145
QString genericName = service->genericName();
146
QString appName = service->name();
147
bool nameFirst = displayOrder == NameBeforeDescription;
148
appItem->setText(nameFirst || genericName.isEmpty() ? appName : genericName);
149
appItem->setIcon(KIcon(service->icon()));
150
appItem->setData(service->entryPath(), Kickoff::UrlRole);
153
if (!genericName.isEmpty()) {
154
appItem->setData(genericName, Kickoff::SubTitleRole);
156
} else if (!genericName.isEmpty()) {
157
// we only set the subtitle to appname if the generic name is empty because if
158
// the generic name IS empty, then the app name is used as the title role
159
// and we don't want it repeated twice.
160
appItem->setData(appName, Kickoff::SubTitleRole);
166
bool Kickoff::isLaterVersion(KService::Ptr first , KService::Ptr second)
168
// a very crude heuristic using the .desktop path names
169
// which only understands kde3 vs kde4
170
bool firstIsKde4 = first->entryPath().contains("kde4");
171
bool secondIsKde4 = second->entryPath().contains("kde4");
173
return firstIsKde4 && !secondIsKde4;
176
QStringList Kickoff::systemApplicationList()
178
KConfigGroup appsGroup = componentData().config()->group("SystemApplications");
180
apps << "systemsettings";
181
apps = appsGroup.readEntry("DesktopFiles", apps);
186
void Kickoff::swapModelIndexes(QModelIndex& first, QModelIndex& second)
188
Q_ASSERT(first.isValid());
189
Q_ASSERT(second.isValid());
191
QAbstractItemModel *firstModel = const_cast<QAbstractItemModel*>(first.model());
192
QAbstractItemModel *secondModel = const_cast<QAbstractItemModel*>(second.model());
194
Q_ASSERT(firstModel && secondModel);
196
QMap<int, QVariant> firstIndexData = firstModel->itemData(first);
197
QMap<int, QVariant> secondIndexData = secondModel->itemData(second);
199
firstModel->setItemData(first, secondIndexData);
200
secondModel->setItemData(second, firstIndexData);
204
K_GLOBAL_STATIC_WITH_ARGS(KComponentData, kickoffComponent, ("kickoff", QByteArray(), KComponentData::SkipMainComponentRegistration))
205
KComponentData Kickoff::componentData()
207
return *kickoffComponent;