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"
25
#include <QStandardItem>
28
#include <KDesktopFile>
34
using namespace Kickoff;
36
static KUrl homeUrl(getenv("HOME"));
37
static KUrl remoteUrl("remote:/");
39
QStandardItem *StandardItemFactory::createItemForUrl(const QString& urlString)
43
QStandardItem *item = 0;
45
if (url.isLocalFile() && QFileInfo(url.path()).suffix() == "desktop") {
47
// .desktop files may be services (type field == 'Application' or 'Service')
48
// or they may be other types such as links.
50
// first look in the KDE service database to see if this file is a service,
51
// otherwise represent it as a generic .desktop file
52
KService::Ptr service = KService::serviceByDesktopPath(url.path());
54
return createItemForService(service);
57
item = new QStandardItem;
58
KDesktopFile desktopFile(url.path());
59
item->setText(QFileInfo(urlString).baseName());
60
item->setIcon(KIcon(desktopFile.readIcon()));
62
KUrl desktopUrl(desktopFile.readUrl());
63
item->setData(desktopUrl.url(),Kickoff::UrlRole);
65
QString subTitle = desktopUrl.isLocalFile() ? desktopUrl.path() : desktopUrl.prettyUrl();
67
item->setData(subTitle,Kickoff::SubTitleRole);
69
setSpecialUrlProperties(desktopUrl,item);
72
item = new QStandardItem;
73
item->setText(QFileInfo(urlString).baseName());
74
item->setIcon(KIcon(KMimeType::iconNameForUrl(url)));
76
item->setData(url.url(),Kickoff::UrlRole);
77
QString subTitle = url.isLocalFile() ? url.path() : url.prettyUrl();
78
item->setData(subTitle,Kickoff::SubTitleRole);
80
setSpecialUrlProperties(url,item);
85
void StandardItemFactory::setSpecialUrlProperties(const KUrl& url,QStandardItem *item)
87
// specially handled URLs
89
item->setText(i18n("Home Folder"));
90
item->setIcon(KIcon("user-home"));
91
} else if (url == remoteUrl) {
92
item->setText(i18n("Network Folders"));
95
QStandardItem *StandardItemFactory::createItemForService(KService::Ptr service)
97
QStandardItem *appItem = new QStandardItem;
99
QString genericName = service->genericName();
100
QString appName = service->name();
102
appItem->setText(genericName.isEmpty() ? appName : genericName);
103
appItem->setIcon(KIcon(service->icon()));
104
appItem->setData(service->entryPath(),Kickoff::UrlRole);
106
if (!genericName.isEmpty()) {
107
appItem->setData(service->name(),Kickoff::SubTitleRole);
113
bool Kickoff::isLaterVersion(KService::Ptr first , KService::Ptr second)
115
// a very crude heuristic using the .desktop path names
116
// which only understands kde3 vs kde4
117
bool firstIsKde4 = first->entryPath().contains("kde4");
118
bool secondIsKde4 = second->entryPath().contains("kde4");
120
return firstIsKde4 && !secondIsKde4;
124
void Kickoff::swapModelIndexes(QModelIndex& first,QModelIndex& second)
126
Q_ASSERT(first.isValid());
127
Q_ASSERT(second.isValid());
129
QAbstractItemModel *firstModel = const_cast<QAbstractItemModel*>(first.model());
130
QAbstractItemModel *secondModel = const_cast<QAbstractItemModel*>(second.model());
132
Q_ASSERT(firstModel && secondModel);
134
QMap<int,QVariant> firstIndexData = firstModel->itemData(first);
135
QMap<int,QVariant> secondIndexData = secondModel->itemData(second);
137
firstModel->setItemData(first,secondIndexData);
138
secondModel->setItemData(second,firstIndexData);
142
K_GLOBAL_STATIC_WITH_ARGS(KComponentData,kickoffComponent,("kickoff",QByteArray(),KComponentData::SkipMainComponentRegistration))
143
KComponentData Kickoff::componentData()
145
return *kickoffComponent;