Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / plasma / applets / kickoff / core / models.cpp
blob6a4006d5d51336cca1684707bb4c83c7845c22e7
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);
95 else if (url.scheme() == "leave") {
96 item = LeaveModel::createStandardItem(urlString);
98 else {
99 item = new QStandardItem;
100 const QString subTitle = url.isLocalFile() ? url.path() : url.prettyUrl();
101 QString basename = QFileInfo(urlString).completeBaseName();
102 if (basename.isNull())
103 basename = subTitle;
104 item->setText(basename);
105 item->setIcon(KIcon(KMimeType::iconNameForUrl(url)));
106 item->setData(url.url(), Kickoff::UrlRole);
107 item->setData(subTitle, Kickoff::SubTitleRole);
109 setSpecialUrlProperties(url, item);
112 return item;
115 void StandardItemFactory::setSpecialUrlProperties(const KUrl& url,QStandardItem *item)
117 // specially handled URLs
118 if (homeUrl() && url == *homeUrl()) {
119 item->setText(i18n("Home Folder"));
120 item->setIcon(KIcon("user-home"));
121 } else if (remoteUrl() && url == *remoteUrl()) {
122 item->setText(i18n("Network Folders"));
126 QStandardItem *StandardItemFactory::createItemForService(KService::Ptr service)
128 QStandardItem *appItem = new QStandardItem;
130 QString genericName = service->genericName();
131 QString appName = service->name();
133 appItem->setText(genericName.isEmpty() ? appName : genericName);
134 appItem->setIcon(KIcon(service->icon()));
135 appItem->setData(service->entryPath(),Kickoff::UrlRole);
137 if (!genericName.isEmpty()) {
138 appItem->setData(service->name(),Kickoff::SubTitleRole);
141 return appItem;
144 bool Kickoff::isLaterVersion(KService::Ptr first , KService::Ptr second)
146 // a very crude heuristic using the .desktop path names
147 // which only understands kde3 vs kde4
148 bool firstIsKde4 = first->entryPath().contains("kde4");
149 bool secondIsKde4 = second->entryPath().contains("kde4");
151 return firstIsKde4 && !secondIsKde4;
154 #if 0
155 void Kickoff::swapModelIndexes(QModelIndex& first,QModelIndex& second)
157 Q_ASSERT(first.isValid());
158 Q_ASSERT(second.isValid());
160 QAbstractItemModel *firstModel = const_cast<QAbstractItemModel*>(first.model());
161 QAbstractItemModel *secondModel = const_cast<QAbstractItemModel*>(second.model());
163 Q_ASSERT(firstModel && secondModel);
165 QMap<int,QVariant> firstIndexData = firstModel->itemData(first);
166 QMap<int,QVariant> secondIndexData = secondModel->itemData(second);
168 firstModel->setItemData(first,secondIndexData);
169 secondModel->setItemData(second,firstIndexData);
171 #endif
173 K_GLOBAL_STATIC_WITH_ARGS(KComponentData,kickoffComponent,("kickoff",QByteArray(),KComponentData::SkipMainComponentRegistration))
174 KComponentData Kickoff::componentData()
176 return *kickoffComponent;