]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/servicessettingspage.cpp
Let the user choose if folders are always shown first in the views of
[dolphin.git] / src / settings / servicessettingspage.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program 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 *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "servicessettingspage.h"
21
22 #include <kconfig.h>
23 #include <kconfiggroup.h>
24 #include <kdesktopfileactions.h>
25 #include <kicon.h>
26 #include <klocale.h>
27 #include <kservice.h>
28 #include <kservicetypetrader.h>
29 #include <kstandarddirs.h>
30
31 #include <QEvent>
32 #include <QLabel>
33 #include <QListWidget>
34 #include <QVBoxLayout>
35
36 ServicesSettingsPage::ServicesSettingsPage(QWidget* parent) :
37 SettingsPageBase(parent),
38 m_initialized(false),
39 m_servicesList(0)
40 {
41 QVBoxLayout* topLayout = new QVBoxLayout(this);
42
43 QLabel* label = new QLabel(i18nc("@label:textbox",
44 "Configure which services should "
45 "be shown in the context menu."), this);
46
47 m_servicesList = new QListWidget(this);
48 m_servicesList->setSortingEnabled(true);
49 m_servicesList->setSelectionMode(QAbstractItemView::NoSelection);
50 connect(m_servicesList, SIGNAL(itemClicked(QListWidgetItem*)),
51 this, SIGNAL(changed()));
52
53 topLayout->addWidget(label);
54 topLayout->addWidget(m_servicesList);
55 }
56
57 ServicesSettingsPage::~ServicesSettingsPage()
58 {
59 }
60
61 void ServicesSettingsPage::applySettings()
62 {
63 KConfig config("kservicemenurc", KConfig::NoGlobals);
64 KConfigGroup showGroup = config.group("Show");
65
66 const int count = m_servicesList->count();
67 for (int i = 0; i < count; ++i) {
68 QListWidgetItem* item = m_servicesList->item(i);
69 const bool show = (item->checkState() == Qt::Checked);
70 const QString service = item->data(Qt::UserRole).toString();
71 showGroup.writeEntry(service, show);
72 }
73
74 showGroup.sync();
75 }
76
77 void ServicesSettingsPage::restoreDefaults()
78 {
79 const int count = m_servicesList->count();
80 for (int i = 0; i < count; ++i) {
81 QListWidgetItem* item = m_servicesList->item(i);
82 item->setCheckState(Qt::Checked);
83 }
84 }
85
86 bool ServicesSettingsPage::event(QEvent* event)
87 {
88 if ((event->type() == QEvent::Polish) && !m_initialized) {
89 QMetaObject::invokeMethod(this, "loadServices", Qt::QueuedConnection);
90 m_initialized = true;
91 }
92 return SettingsPageBase::event(event);
93 }
94
95 void ServicesSettingsPage::loadServices()
96 {
97 const KConfig config("kservicemenurc", KConfig::NoGlobals);
98 const KConfigGroup showGroup = config.group("Show");
99
100 const KService::List entries = KServiceTypeTrader::self()->query("KonqPopupMenu/Plugin");
101 foreach (const KSharedPtr<KService>& service, entries) {
102 const QString file = KStandardDirs::locate("services", service->entryPath());
103 const QList<KServiceAction> serviceActions =
104 KDesktopFileActions::userDefinedServices(file, true);
105
106 foreach (const KServiceAction& action, serviceActions) {
107 const QString service = action.name();
108 const bool addService = !action.noDisplay()
109 && !action.isSeparator()
110 && !isInServicesList(service);
111
112 if (addService) {
113 QListWidgetItem* item = new QListWidgetItem(KIcon(action.icon()),
114 action.text(),
115 m_servicesList);
116 item->setData(Qt::UserRole, service);
117 const bool show = showGroup.readEntry(service, true);
118 item->setCheckState(show ? Qt::Checked : Qt::Unchecked);
119 }
120 }
121 }
122 }
123
124 bool ServicesSettingsPage::isInServicesList(const QString& service) const
125 {
126 const int count = m_servicesList->count();
127 for (int i = 0; i < count; ++i) {
128 QListWidgetItem* item = m_servicesList->item(i);
129 if (item->data(Qt::UserRole).toString() == service) {
130 return true;
131 }
132 }
133 return false;
134 }
135
136 #include "servicessettingspage.moc"