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

« back to all changes in this revision

Viewing changes to plasma/netbook/containments/sal/models/kservicemodel.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 2009 Ivan Cukic <ivan.cukic+kde@gmail.com>
 
3
    Copyright 2010 Marco Martin <notmart@gmail.com>
 
4
 
 
5
    This library is free software; you can redistribute it and/or
 
6
    modify it under the terms of the GNU Library General Public
 
7
    License as published by the Free Software Foundation; either
 
8
    version 2 of the License, or (at your option) any later version.
 
9
 
 
10
    This library is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
    Library General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU Library General Public License
 
16
    along with this library; see the file COPYING.LIB.  If not, write to
 
17
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
18
    Boston, MA 02110-1301, USA.
 
19
*/
 
20
 
 
21
// Own
 
22
#include "kservicemodel.h"
 
23
#include "commonmodel.h"
 
24
 
 
25
// Qt
 
26
#include <QMimeData>
 
27
 
 
28
// KDE
 
29
#include <KService>
 
30
#include <KIcon>
 
31
#include <KDebug>
 
32
#include <KRun>
 
33
#include <KServiceTypeTrader>
 
34
#include <KSycocaEntry>
 
35
 
 
36
//Plasma
 
37
#include <Plasma/AbstractRunner>
 
38
#include <Plasma/RunnerManager>
 
39
 
 
40
 
 
41
bool KServiceItemHandler::openUrl(const KUrl& url)
 
42
{
 
43
    QString urlString = url.path();
 
44
    KService::Ptr service = KService::serviceByDesktopPath(urlString);
 
45
 
 
46
    if (!service) {
 
47
        service = KService::serviceByDesktopName(urlString);
 
48
    }
 
49
 
 
50
    if (!service) {
 
51
        return false;
 
52
    }
 
53
 
 
54
    return KRun::run(*service, KUrl::List(), 0);
 
55
}
 
56
 
 
57
 
 
58
KServiceModel::KServiceModel(const KConfigGroup &group, QObject *parent)
 
59
        : QStandardItemModel(parent),
 
60
          m_config(group),
 
61
          m_path("/"),
 
62
          m_allRootEntriesModel(0)
 
63
{
 
64
    QHash<int, QByteArray> newRoleNames = roleNames();
 
65
    newRoleNames[CommonModel::Description] = "description";
 
66
    newRoleNames[CommonModel::Url] = "url";
 
67
    newRoleNames[CommonModel::Weight] = "weight";
 
68
    newRoleNames[CommonModel::ActionTypeRole] = "action";
 
69
 
 
70
    setRoleNames(newRoleNames);
 
71
 
 
72
    loadRootEntries(this);
 
73
}
 
74
 
 
75
KServiceModel::~KServiceModel()
 
76
{
 
77
}
 
78
 
 
79
QMimeData * KServiceModel::mimeData(const QModelIndexList &indexes) const
 
80
{
 
81
    KUrl::List urls;
 
82
 
 
83
    foreach (const QModelIndex & index, indexes) {
 
84
        QString urlString = data(index, CommonModel::Url).toString();
 
85
 
 
86
        KService::Ptr service = KService::serviceByDesktopPath(urlString);
 
87
 
 
88
        if (!service) {
 
89
            service = KService::serviceByDesktopName(urlString);
 
90
        }
 
91
 
 
92
        if (service) {
 
93
            urls << KUrl(service->entryPath());
 
94
        }
 
95
    }
 
96
 
 
97
    QMimeData *mimeData = new QMimeData();
 
98
 
 
99
    if (!urls.isEmpty()) {
 
100
        urls.populateMimeData(mimeData);
 
101
    }
 
102
 
 
103
    return mimeData;
 
104
 
 
105
}
 
106
 
 
107
void KServiceModel::setPath(const QString &path)
 
108
{
 
109
    clear();
 
110
 
 
111
    if (path == "/") {
 
112
        loadRootEntries(this);
 
113
    } else {
 
114
        loadServiceGroup(KServiceGroup::group(path));
 
115
        setSortRole(Qt::DisplayRole);
 
116
        sort(0, Qt::AscendingOrder);
 
117
    }
 
118
    m_path = path;
 
119
}
 
120
 
 
121
QString KServiceModel::path() const
 
122
{
 
123
    return m_path;
 
124
}
 
125
 
 
126
void KServiceModel::saveConfig()
 
127
{
 
128
    if (!m_allRootEntriesModel) {
 
129
        return;
 
130
    }
 
131
 
 
132
    QStringList enabledEntries;
 
133
 
 
134
    for (int i = 0; i <= m_allRootEntriesModel->rowCount() - 1; i++) {
 
135
        QModelIndex index = m_allRootEntriesModel->index(i, 0, QModelIndex());
 
136
        QStandardItem *item = m_allRootEntriesModel->itemFromIndex(index);
 
137
        if (item && item->checkState() == Qt::Checked) {
 
138
            enabledEntries << index.data(CommonModel::Url).value<QString>();
 
139
        }
 
140
    }
 
141
 
 
142
    m_config.writeEntry("EnabledEntries", enabledEntries);
 
143
 
 
144
    //sync should be kinda safe here this function is very rarely called
 
145
    m_config.sync();
 
146
 
 
147
    setPath("/");
 
148
}
 
149
 
 
150
QStandardItemModel *KServiceModel::allRootEntriesModel()
 
151
{
 
152
    if (!m_allRootEntriesModel) {
 
153
        m_allRootEntriesModel = new QStandardItemModel(this);
 
154
        loadRootEntries(m_allRootEntriesModel);
 
155
    }
 
156
 
 
157
    return m_allRootEntriesModel;
 
158
}
 
159
 
 
160
void KServiceModel::loadRootEntries(QStandardItemModel *model)
 
161
{
 
162
    QStringList defaultEnabledEntries;
 
163
    defaultEnabledEntries << "plasma-sal-contacts.desktop" << "plasma-sal-bookmarks.desktop"
 
164
        << "plasma-sal-multimedia.desktop" << "plasma-sal-internet.desktop"
 
165
        << "plasma-sal-graphics.desktop" << "plasma-sal-education.desktop"
 
166
        << "plasma-sal-games.desktop" << "plasma-sal-office.desktop";
 
167
    QSet <QString> enabledEntries = m_config.readEntry("EnabledEntries", defaultEnabledEntries).toSet();
 
168
 
 
169
    QHash<QString, KServiceGroup::Ptr> groupSet;
 
170
    KServiceGroup::Ptr group = KServiceGroup::root();
 
171
    KServiceGroup::List list = group->entries();
 
172
 
 
173
    for( KServiceGroup::List::ConstIterator it = list.constBegin();
 
174
         it != list.constEnd(); it++) {
 
175
        const KSycocaEntry::Ptr p = (*it);
 
176
 
 
177
        if (p->isType(KST_KServiceGroup)) {
 
178
            KServiceGroup::Ptr subGroup = KServiceGroup::Ptr::staticCast(p);
 
179
 
 
180
            if (!subGroup->noDisplay() && subGroup->childCount() > 0) {
 
181
                groupSet.insert(subGroup->relPath(), subGroup);
 
182
            }
 
183
        }
 
184
 
 
185
    }
 
186
 
 
187
    KService::List services = KServiceTypeTrader::self()->query("Plasma/Sal/Menu");
 
188
    if (!services.isEmpty()) {
 
189
        foreach (const KService::Ptr &service, services) {
 
190
            const QUrl url = QUrl(service->property("X-Plasma-Sal-Url", QVariant::String).toString());
 
191
            const int relevance = service->property("X-Plasma-Sal-Relevance", QVariant::Int).toInt();
 
192
            const QString groupName = url.path().remove(0, 1);
 
193
 
 
194
            if ((model != m_allRootEntriesModel) && enabledEntries.contains(service->storageId()) &&
 
195
                (url.scheme() != "kservicegroup" || groupSet.contains(groupName))) {
 
196
                model->appendRow(
 
197
                        StandardItemFactory::createItem(
 
198
                            KIcon(service->icon()),
 
199
                            service->name(),
 
200
                            service->comment(),
 
201
                            url.toString(),
 
202
                            relevance,
 
203
                            CommonModel::NoAction
 
204
                            )
 
205
                        );
 
206
            } else if (model == m_allRootEntriesModel && (url.scheme() != "kservicegroup" || groupSet.contains(groupName))) {
 
207
                QStandardItem * item  = StandardItemFactory::createItem(
 
208
                                KIcon(service->icon()),
 
209
                                service->name(),
 
210
                                service->comment(),
 
211
                                service->storageId(),
 
212
                                relevance,
 
213
                                CommonModel::NoAction
 
214
                                );
 
215
                    item->setCheckable(true);
 
216
                    item->setCheckState(enabledEntries.contains(service->storageId())?Qt::Checked:Qt::Unchecked);
 
217
                    model->appendRow(item);
 
218
            }
 
219
 
 
220
            if (groupSet.contains(groupName)) {
 
221
                groupSet.remove(groupName);
 
222
            }
 
223
        }
 
224
    }
 
225
 
 
226
    foreach (const KServiceGroup::Ptr group, groupSet) {
 
227
        if ((model != m_allRootEntriesModel) && enabledEntries.contains(group->relPath())) {
 
228
            model->appendRow(
 
229
                    StandardItemFactory::createItem(
 
230
                        KIcon(group->icon()),
 
231
                        group->caption(),
 
232
                        group->comment(),
 
233
                        QString("kserviceGroup://root/") + group->relPath(),
 
234
                        0.1,
 
235
                        CommonModel::NoAction
 
236
                        )
 
237
                    );
 
238
        } else if (model == m_allRootEntriesModel) {
 
239
            QStandardItem *item = StandardItemFactory::createItem(
 
240
                        KIcon(group->icon()),
 
241
                        group->caption(),
 
242
                        group->comment(),
 
243
                        group->storageId(),
 
244
                        0.1,
 
245
                        CommonModel::NoAction
 
246
                        );
 
247
            item->setCheckable(true);
 
248
            item->setCheckState(enabledEntries.contains(group->storageId())?Qt::Checked:Qt::Unchecked);
 
249
            model->appendRow(item);
 
250
        }
 
251
    }
 
252
 
 
253
    model->setSortRole(CommonModel::Weight);
 
254
    model->sort(0, Qt::DescendingOrder);
 
255
}
 
256
 
 
257
void KServiceModel::loadServiceGroup(KServiceGroup::Ptr group)
 
258
{
 
259
    if (group && group->isValid()) {
 
260
        KServiceGroup::List list = group->entries();
 
261
 
 
262
        for( KServiceGroup::List::ConstIterator it = list.constBegin();
 
263
             it != list.constEnd(); it++) {
 
264
            const KSycocaEntry::Ptr p = (*it);
 
265
 
 
266
            if (p->isType(KST_KService)) {
 
267
                const KService::Ptr service = KService::Ptr::staticCast(p);
 
268
 
 
269
                if (!service->noDisplay()) {
 
270
                    QString genericName = service->genericName();
 
271
                    if (genericName.isNull()) {
 
272
                        genericName = service->comment();
 
273
                    }
 
274
                    appendRow(
 
275
                        StandardItemFactory::createItem(
 
276
                            KIcon(service->icon()),
 
277
                            service->name(),
 
278
                            genericName,
 
279
                            service->entryPath(),
 
280
                            0.5,
 
281
                            CommonModel::AddAction
 
282
                            )
 
283
                        );
 
284
                }
 
285
 
 
286
            } else if (p->isType(KST_KServiceGroup)) {
 
287
                const KServiceGroup::Ptr subGroup = KServiceGroup::Ptr::staticCast(p);
 
288
 
 
289
                if (!subGroup->noDisplay() && subGroup->childCount() > 0) {
 
290
                    loadServiceGroup(subGroup);
 
291
                }
 
292
            }
 
293
 
 
294
        }
 
295
 
 
296
    }
 
297
}
 
298
 
 
299
#include "kservicemodel.moc"