2
Copyright 2009 Ivan Cukic <ivan.cukic+kde@gmail.com>
3
Copyright 2010 Marco Martin <notmart@gmail.com>
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.
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.
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.
22
#include "kservicemodel.h"
23
#include "commonmodel.h"
33
#include <KServiceTypeTrader>
34
#include <KSycocaEntry>
37
#include <Plasma/AbstractRunner>
38
#include <Plasma/RunnerManager>
41
bool KServiceItemHandler::openUrl(const KUrl& url)
43
QString urlString = url.path();
44
KService::Ptr service = KService::serviceByDesktopPath(urlString);
47
service = KService::serviceByDesktopName(urlString);
54
return KRun::run(*service, KUrl::List(), 0);
58
KServiceModel::KServiceModel(const KConfigGroup &group, QObject *parent)
59
: QStandardItemModel(parent),
62
m_allRootEntriesModel(0)
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";
70
setRoleNames(newRoleNames);
72
loadRootEntries(this);
75
KServiceModel::~KServiceModel()
79
QMimeData * KServiceModel::mimeData(const QModelIndexList &indexes) const
83
foreach (const QModelIndex & index, indexes) {
84
QString urlString = data(index, CommonModel::Url).toString();
86
KService::Ptr service = KService::serviceByDesktopPath(urlString);
89
service = KService::serviceByDesktopName(urlString);
93
urls << KUrl(service->entryPath());
97
QMimeData *mimeData = new QMimeData();
99
if (!urls.isEmpty()) {
100
urls.populateMimeData(mimeData);
107
void KServiceModel::setPath(const QString &path)
112
loadRootEntries(this);
114
loadServiceGroup(KServiceGroup::group(path));
115
setSortRole(Qt::DisplayRole);
116
sort(0, Qt::AscendingOrder);
121
QString KServiceModel::path() const
126
void KServiceModel::saveConfig()
128
if (!m_allRootEntriesModel) {
132
QStringList enabledEntries;
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>();
142
m_config.writeEntry("EnabledEntries", enabledEntries);
144
//sync should be kinda safe here this function is very rarely called
150
QStandardItemModel *KServiceModel::allRootEntriesModel()
152
if (!m_allRootEntriesModel) {
153
m_allRootEntriesModel = new QStandardItemModel(this);
154
loadRootEntries(m_allRootEntriesModel);
157
return m_allRootEntriesModel;
160
void KServiceModel::loadRootEntries(QStandardItemModel *model)
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();
169
QHash<QString, KServiceGroup::Ptr> groupSet;
170
KServiceGroup::Ptr group = KServiceGroup::root();
171
KServiceGroup::List list = group->entries();
173
for( KServiceGroup::List::ConstIterator it = list.constBegin();
174
it != list.constEnd(); it++) {
175
const KSycocaEntry::Ptr p = (*it);
177
if (p->isType(KST_KServiceGroup)) {
178
KServiceGroup::Ptr subGroup = KServiceGroup::Ptr::staticCast(p);
180
if (!subGroup->noDisplay() && subGroup->childCount() > 0) {
181
groupSet.insert(subGroup->relPath(), subGroup);
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);
194
if ((model != m_allRootEntriesModel) && enabledEntries.contains(service->storageId()) &&
195
(url.scheme() != "kservicegroup" || groupSet.contains(groupName))) {
197
StandardItemFactory::createItem(
198
KIcon(service->icon()),
203
CommonModel::NoAction
206
} else if (model == m_allRootEntriesModel && (url.scheme() != "kservicegroup" || groupSet.contains(groupName))) {
207
QStandardItem * item = StandardItemFactory::createItem(
208
KIcon(service->icon()),
211
service->storageId(),
213
CommonModel::NoAction
215
item->setCheckable(true);
216
item->setCheckState(enabledEntries.contains(service->storageId())?Qt::Checked:Qt::Unchecked);
217
model->appendRow(item);
220
if (groupSet.contains(groupName)) {
221
groupSet.remove(groupName);
226
foreach (const KServiceGroup::Ptr group, groupSet) {
227
if ((model != m_allRootEntriesModel) && enabledEntries.contains(group->relPath())) {
229
StandardItemFactory::createItem(
230
KIcon(group->icon()),
233
QString("kserviceGroup://root/") + group->relPath(),
235
CommonModel::NoAction
238
} else if (model == m_allRootEntriesModel) {
239
QStandardItem *item = StandardItemFactory::createItem(
240
KIcon(group->icon()),
245
CommonModel::NoAction
247
item->setCheckable(true);
248
item->setCheckState(enabledEntries.contains(group->storageId())?Qt::Checked:Qt::Unchecked);
249
model->appendRow(item);
253
model->setSortRole(CommonModel::Weight);
254
model->sort(0, Qt::DescendingOrder);
257
void KServiceModel::loadServiceGroup(KServiceGroup::Ptr group)
259
if (group && group->isValid()) {
260
KServiceGroup::List list = group->entries();
262
for( KServiceGroup::List::ConstIterator it = list.constBegin();
263
it != list.constEnd(); it++) {
264
const KSycocaEntry::Ptr p = (*it);
266
if (p->isType(KST_KService)) {
267
const KService::Ptr service = KService::Ptr::staticCast(p);
269
if (!service->noDisplay()) {
270
QString genericName = service->genericName();
271
if (genericName.isNull()) {
272
genericName = service->comment();
275
StandardItemFactory::createItem(
276
KIcon(service->icon()),
279
service->entryPath(),
281
CommonModel::AddAction
286
} else if (p->isType(KST_KServiceGroup)) {
287
const KServiceGroup::Ptr subGroup = KServiceGroup::Ptr::staticCast(p);
289
if (!subGroup->noDisplay() && subGroup->childCount() > 0) {
290
loadServiceGroup(subGroup);
299
#include "kservicemodel.moc"