~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/desktop/applets/kickoff/core/models.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright 2007 Robert Knight <robertknight@gmail.com>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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.
 
18
*/
 
19
 
 
20
// Own
 
21
#include "core/models.h"
 
22
#include "core/leavemodel.h"
 
23
 
 
24
// Qt
 
25
#include <QFileInfo>
 
26
#include <QStandardItem>
 
27
#include <QDir>
 
28
 
 
29
// KDE
 
30
#include <KDebug>
 
31
#include <KConfigGroup>
 
32
#include <KDesktopFile>
 
33
#include <KIcon>
 
34
#include <KMimeType>
 
35
#include <KUrl>
 
36
#include <Solid/Device>
 
37
#include <Solid/StorageAccess>
 
38
#include <Solid/StorageDrive>
 
39
 
 
40
using namespace Kickoff;
 
41
 
 
42
namespace Kickoff
 
43
{
 
44
 
 
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)
 
48
 
 
49
StandardItemFactoryData* deviceFactoryData()
 
50
{
 
51
    return factoryData;
 
52
}
 
53
} // namespace Kickoff
 
54
 
 
55
QStandardItem *StandardItemFactory::createItemForUrl(const QString& urlString, DisplayOrder displayOrder)
 
56
{
 
57
    KUrl url(urlString);
 
58
 
 
59
    QStandardItem *item = 0;
 
60
 
 
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.
 
67
        //
 
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());
 
71
        if (service) {
 
72
            return createItemForService(service, displayOrder);
 
73
        }
 
74
 
 
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()));
 
79
 
 
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);
 
85
        } else {
 
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
 
88
            // manually removed
 
89
            item->setData(urlString, Kickoff::UrlRole);
 
90
        }
 
91
 
 
92
        QString subTitle = desktopUrl.isLocalFile() ? desktopUrl.toLocalFile() : desktopUrl.prettyUrl();
 
93
        item->setData(subTitle, Kickoff::SubTitleRole);
 
94
 
 
95
        setSpecialUrlProperties(desktopUrl, item);
 
96
    } else if (url.scheme() == "leave") {
 
97
        item = LeaveModel::createStandardItem(urlString);
 
98
    } else {
 
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()) {
 
103
            basename = subTitle;
 
104
        }
 
105
 
 
106
        item->setText(basename);
 
107
        item->setIcon(KIcon(KMimeType::iconNameForUrl(url)));
 
108
        item->setData(url.url(), Kickoff::UrlRole);
 
109
        item->setData(subTitle, Kickoff::SubTitleRole);
 
110
 
 
111
        setSpecialUrlProperties(url, item);
 
112
    }
 
113
 
 
114
    return item;
 
115
}
 
116
 
 
117
void StandardItemFactory::setSpecialUrlProperties(const KUrl& url, QStandardItem *item)
 
118
{
 
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"));
 
125
    }
 
126
}
 
127
 
 
128
QStandardItem *StandardItemFactory::createItem(const QIcon & icon, const QString & title,
 
129
        const QString & description, const QString & url)
 
130
{
 
131
    QStandardItem *appItem = new QStandardItem;
 
132
 
 
133
    appItem->setText(title);
 
134
    appItem->setIcon(icon);
 
135
    appItem->setData(description, Kickoff::SubTitleRole);
 
136
    appItem->setData(url, Kickoff::UrlRole);
 
137
 
 
138
    return appItem;
 
139
}
 
140
 
 
141
QStandardItem *StandardItemFactory::createItemForService(KService::Ptr service, DisplayOrder displayOrder)
 
142
{
 
143
    QStandardItem *appItem = new QStandardItem;
 
144
 
 
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);
 
151
 
 
152
    if (nameFirst) {
 
153
        if (!genericName.isEmpty()) {
 
154
            appItem->setData(genericName, Kickoff::SubTitleRole);
 
155
        }
 
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);
 
161
    }
 
162
 
 
163
    return appItem;
 
164
}
 
165
 
 
166
bool Kickoff::isLaterVersion(KService::Ptr first , KService::Ptr second)
 
167
{
 
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");
 
172
 
 
173
    return firstIsKde4 && !secondIsKde4;
 
174
}
 
175
 
 
176
QStringList Kickoff::systemApplicationList()
 
177
{
 
178
    KConfigGroup appsGroup = componentData().config()->group("SystemApplications");
 
179
    QStringList apps;
 
180
    apps << "systemsettings";
 
181
    apps = appsGroup.readEntry("DesktopFiles", apps);
 
182
    return apps;
 
183
}
 
184
 
 
185
#if 0
 
186
void Kickoff::swapModelIndexes(QModelIndex& first, QModelIndex& second)
 
187
{
 
188
    Q_ASSERT(first.isValid());
 
189
    Q_ASSERT(second.isValid());
 
190
 
 
191
    QAbstractItemModel *firstModel = const_cast<QAbstractItemModel*>(first.model());
 
192
    QAbstractItemModel *secondModel = const_cast<QAbstractItemModel*>(second.model());
 
193
 
 
194
    Q_ASSERT(firstModel && secondModel);
 
195
 
 
196
    QMap<int, QVariant> firstIndexData = firstModel->itemData(first);
 
197
    QMap<int, QVariant> secondIndexData = secondModel->itemData(second);
 
198
 
 
199
    firstModel->setItemData(first, secondIndexData);
 
200
    secondModel->setItemData(second, firstIndexData);
 
201
}
 
202
#endif
 
203
 
 
204
K_GLOBAL_STATIC_WITH_ARGS(KComponentData, kickoffComponent, ("kickoff", QByteArray(), KComponentData::SkipMainComponentRegistration))
 
205
KComponentData Kickoff::componentData()
 
206
{
 
207
    return *kickoffComponent;
 
208
}
 
209
 
 
210
 
 
211