]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/servicessettingspage.cpp
add doc path to enable launching the documentation from konquerors settings dialog
[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 <knewstuff2/engine.h>
28 #include <kservice.h>
29 #include <kservicetypetrader.h>
30 #include <kstandarddirs.h>
31
32 #include <QEvent>
33 #include <QLabel>
34 #include <QListWidget>
35 #include <QPushButton>
36 #include <QVBoxLayout>
37
38 ServicesSettingsPage::ServicesSettingsPage(QWidget* parent) :
39 SettingsPageBase(parent),
40 m_initialized(false),
41 m_servicesList(0)
42 {
43 QVBoxLayout* topLayout = new QVBoxLayout(this);
44
45 QLabel* label = new QLabel(i18nc("@label:textbox",
46 "Configure which services should "
47 "be shown in the context menu."), this);
48 label->setWordWrap(true);
49
50 m_servicesList = new QListWidget(this);
51 m_servicesList->setSortingEnabled(true);
52 m_servicesList->setSelectionMode(QAbstractItemView::NoSelection);
53 connect(m_servicesList, SIGNAL(itemClicked(QListWidgetItem*)),
54 this, SIGNAL(changed()));
55
56 QPushButton* downloadButton = new QPushButton(i18nc("@action:button", "Download New Services..."));
57 downloadButton->setIcon(KIcon("get-hot-new-stuff"));
58 connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadNewServices()));
59
60 topLayout->addWidget(label);
61 topLayout->addWidget(m_servicesList);
62 topLayout->addWidget(downloadButton);
63 }
64
65 ServicesSettingsPage::~ServicesSettingsPage()
66 {
67 }
68
69 void ServicesSettingsPage::applySettings()
70 {
71 KConfig config("kservicemenurc", KConfig::NoGlobals);
72 KConfigGroup showGroup = config.group("Show");
73
74 const int count = m_servicesList->count();
75 for (int i = 0; i < count; ++i) {
76 QListWidgetItem* item = m_servicesList->item(i);
77 const bool show = (item->checkState() == Qt::Checked);
78 const QString service = item->data(Qt::UserRole).toString();
79 showGroup.writeEntry(service, show);
80 }
81
82 showGroup.sync();
83 }
84
85 void ServicesSettingsPage::restoreDefaults()
86 {
87 const int count = m_servicesList->count();
88 for (int i = 0; i < count; ++i) {
89 QListWidgetItem* item = m_servicesList->item(i);
90 item->setCheckState(Qt::Checked);
91 }
92 }
93
94 bool ServicesSettingsPage::event(QEvent* event)
95 {
96 if ((event->type() == QEvent::Polish) && !m_initialized) {
97 QMetaObject::invokeMethod(this, "loadServices", Qt::QueuedConnection);
98 m_initialized = true;
99 }
100 return SettingsPageBase::event(event);
101 }
102
103 void ServicesSettingsPage::loadServices()
104 {
105 const KConfig config("kservicemenurc", KConfig::NoGlobals);
106 const KConfigGroup showGroup = config.group("Show");
107
108 const KService::List entries = KServiceTypeTrader::self()->query("KonqPopupMenu/Plugin");
109 foreach (const KSharedPtr<KService>& service, entries) {
110 const QString file = KStandardDirs::locate("services", service->entryPath());
111 const QList<KServiceAction> serviceActions =
112 KDesktopFileActions::userDefinedServices(file, true);
113
114 foreach (const KServiceAction& action, serviceActions) {
115 const QString service = action.name();
116 const bool addService = !action.noDisplay()
117 && !action.isSeparator()
118 && !isInServicesList(service);
119
120 if (addService) {
121 QListWidgetItem* item = new QListWidgetItem(KIcon(action.icon()),
122 action.text(),
123 m_servicesList);
124 item->setData(Qt::UserRole, service);
125 const bool show = showGroup.readEntry(service, true);
126 item->setCheckState(show ? Qt::Checked : Qt::Unchecked);
127 }
128 }
129 }
130 }
131
132 void ServicesSettingsPage::downloadNewServices()
133 {
134 KNS::Engine khns(this);
135 khns.init("servicemenu.knsrc");
136 khns.downloadDialogModal(this);
137 loadServices();
138 }
139
140 bool ServicesSettingsPage::isInServicesList(const QString& service) const
141 {
142 const int count = m_servicesList->count();
143 for (int i = 0; i < count; ++i) {
144 QListWidgetItem* item = m_servicesList->item(i);
145 if (item->data(Qt::UserRole).toString() == service) {
146 return true;
147 }
148 }
149 return false;
150 }
151
152 #include "servicessettingspage.moc"