]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/services/servicessettingspage.cpp
Don't try to apply the services-settings, if they have not even been shown. This...
[dolphin.git] / src / settings / services / servicessettingspage.cpp
1 /***************************************************************************
2 * Copyright (C) 2009-2010 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 "dolphin_versioncontrolsettings.h"
23
24 #include <kconfig.h>
25 #include <kconfiggroup.h>
26 #include <kdesktopfile.h>
27 #include <kdesktopfileactions.h>
28 #include <kicon.h>
29 #include <klocale.h>
30 #include <kmessagebox.h>
31 #include <knewstuff3/knewstuffbutton.h>
32 #include <kservice.h>
33 #include <kservicetypetrader.h>
34 #include <kstandarddirs.h>
35
36 #include <QCheckBox>
37 #include <QGridLayout>
38 #include <QGroupBox>
39 #include <QLabel>
40 #include <QListWidget>
41 #include <QPushButton>
42 #include <QShowEvent>
43
44 ServicesSettingsPage::ServicesSettingsPage(QWidget* parent) :
45 SettingsPageBase(parent),
46 m_initialized(false),
47 m_servicesList(0),
48 m_vcsGroupBox(0),
49 m_vcsPluginsMap(),
50 m_enabledVcsPlugins()
51 {
52 QVBoxLayout* topLayout = new QVBoxLayout(this);
53
54 QLabel* label = new QLabel(i18nc("@label:textbox",
55 "Select which services should "
56 "be shown in the context menu:"), this);
57 label->setWordWrap(true);
58
59 m_servicesList = new QListWidget(this);
60 m_servicesList->setSortingEnabled(true);
61 m_servicesList->setSelectionMode(QAbstractItemView::NoSelection);
62 connect(m_servicesList, SIGNAL(itemClicked(QListWidgetItem*)),
63 this, SIGNAL(changed()));
64
65 KNS3::Button* downloadButton = new KNS3::Button(i18nc("@action:button", "Download New Services..."),
66 "servicemenu.knsrc",
67 this);
68 connect(downloadButton, SIGNAL(dialogFinished(KNS3::Entry::List)), this, SLOT(loadServices()));
69
70 m_vcsGroupBox = new QGroupBox(i18nc("@title:group", "Version Control Systems"), this);
71 // Only show the version control group box, if a version
72 // control system could be found (see loadVersionControlSystems())
73 m_vcsGroupBox->hide();
74
75 topLayout->addWidget(label);
76 topLayout->addWidget(m_servicesList);
77 topLayout->addWidget(downloadButton);
78 topLayout->addWidget(m_vcsGroupBox);
79
80 m_enabledVcsPlugins = VersionControlSettings::enabledPlugins();
81 }
82
83 ServicesSettingsPage::~ServicesSettingsPage()
84 {
85 }
86
87 void ServicesSettingsPage::applySettings()
88 {
89 if (!m_initialized) {
90 return;
91 }
92
93 // Apply service menu settingsentries
94 KConfig config("kservicemenurc", KConfig::NoGlobals);
95 KConfigGroup showGroup = config.group("Show");
96
97 const int count = m_servicesList->count();
98 for (int i = 0; i < count; ++i) {
99 QListWidgetItem* item = m_servicesList->item(i);
100 const bool show = (item->checkState() == Qt::Checked);
101 const QString service = item->data(Qt::UserRole).toString();
102 showGroup.writeEntry(service, show);
103 }
104
105 showGroup.sync();
106
107 // Apply version control settings
108 QStringList enabledPlugins;
109 QMap<QString, QCheckBox*>::const_iterator it = m_vcsPluginsMap.constBegin();
110 while (it != m_vcsPluginsMap.constEnd()) {
111 if (it.value()->isChecked()) {
112 enabledPlugins.append(it.key());
113 }
114 ++it;
115 }
116
117 if (m_enabledVcsPlugins != enabledPlugins) {
118 VersionControlSettings::setEnabledPlugins(enabledPlugins);
119 VersionControlSettings::self()->writeConfig();
120
121 KMessageBox::information(window(),
122 i18nc("@info", "Dolphin must be restarted to apply the "
123 "updated version control systems settings."),
124 QString(), // default title
125 QLatin1String("ShowVcsRestartInformation"));
126 }
127 }
128
129 void ServicesSettingsPage::restoreDefaults()
130 {
131 const int count = m_servicesList->count();
132 for (int i = 0; i < count; ++i) {
133 QListWidgetItem* item = m_servicesList->item(i);
134 item->setCheckState(Qt::Checked);
135 }
136 }
137
138 void ServicesSettingsPage::showEvent(QShowEvent* event)
139 {
140 if (!event->spontaneous() && !m_initialized) {
141 QMetaObject::invokeMethod(this, "loadServices", Qt::QueuedConnection);
142 QMetaObject::invokeMethod(this, "loadVersionControlSystems", Qt::QueuedConnection);
143 m_initialized = true;
144 }
145 SettingsPageBase::showEvent(event);
146 }
147
148 void ServicesSettingsPage::loadServices()
149 {
150 const KConfig config("kservicemenurc", KConfig::NoGlobals);
151 const KConfigGroup showGroup = config.group("Show");
152
153 // Load generic services
154 const KService::List entries = KServiceTypeTrader::self()->query("KonqPopupMenu/Plugin");
155 foreach (const KSharedPtr<KService>& service, entries) {
156 const QString file = KStandardDirs::locate("services", service->entryPath());
157 const QList<KServiceAction> serviceActions =
158 KDesktopFileActions::userDefinedServices(file, true);
159
160 KDesktopFile desktopFile(file);
161 const QString subMenuName = desktopFile.desktopGroup().readEntry("X-KDE-Submenu");
162
163 foreach (const KServiceAction& action, serviceActions) {
164 const QString serviceName = action.name();
165 const bool addService = !action.noDisplay()
166 && !action.isSeparator()
167 && !isInServicesList(serviceName);
168
169 if (addService) {
170 const QString itemName = subMenuName.isEmpty()
171 ? action.text()
172 : i18nc("@item:inmenu", "%1: %2", subMenuName, action.text());
173 QListWidgetItem* item = new QListWidgetItem(KIcon(action.icon()),
174 itemName,
175 m_servicesList);
176 item->setData(Qt::UserRole, serviceName);
177 const bool show = showGroup.readEntry(serviceName, true);
178 item->setCheckState(show ? Qt::Checked : Qt::Unchecked);
179 }
180 }
181 }
182
183 // Load service plugins that implement the KFileItemActionPlugin interface
184 const KService::List pluginServices = KServiceTypeTrader::self()->query("KFileItemAction/Plugin");
185 foreach (const KSharedPtr<KService>& service, pluginServices) {
186 const QString serviceName = service->desktopEntryName();
187 if (!isInServicesList(serviceName)) {
188 QListWidgetItem* item = new QListWidgetItem(KIcon(service->icon()),
189 service->name(),
190 m_servicesList);
191 item->setData(Qt::UserRole, serviceName);
192 const bool show = showGroup.readEntry(serviceName, true);
193 item->setCheckState(show ? Qt::Checked : Qt::Unchecked);
194 }
195 }
196 }
197
198 void ServicesSettingsPage::loadVersionControlSystems()
199 {
200 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
201
202 // Create a checkbox for each available version control plugin
203 const KService::List pluginServices = KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
204 for (KService::List::ConstIterator it = pluginServices.constBegin(); it != pluginServices.constEnd(); ++it) {
205 const QString pluginName = (*it)->name();
206 QCheckBox* checkBox = new QCheckBox(pluginName, m_vcsGroupBox);
207 checkBox->setChecked(enabledPlugins.contains(pluginName));
208 connect(checkBox, SIGNAL(clicked()), this, SIGNAL(changed()));
209 m_vcsPluginsMap.insert(pluginName, checkBox);
210 }
211
212 // Add the checkboxes into a grid layout of 2 columns
213 QGridLayout* layout = new QGridLayout(m_vcsGroupBox);
214 const int maxRows = (m_vcsPluginsMap.count() + 1) / 2;
215
216 int index = 0;
217 QMap<QString, QCheckBox*>::const_iterator it = m_vcsPluginsMap.constBegin();
218 while (it != m_vcsPluginsMap.constEnd()) {
219 const int column = index / maxRows;
220 const int row = index % maxRows;
221 layout->addWidget(it.value(), row, column);
222 ++it;
223 ++index;
224 }
225
226 m_vcsGroupBox->setVisible(!m_vcsPluginsMap.isEmpty());
227 }
228
229 bool ServicesSettingsPage::isInServicesList(const QString& service) const
230 {
231 const int count = m_servicesList->count();
232 for (int i = 0; i < count; ++i) {
233 QListWidgetItem* item = m_servicesList->item(i);
234 if (item->data(Qt::UserRole).toString() == service) {
235 return true;
236 }
237 }
238 return false;
239 }
240
241 #include "servicessettingspage.moc"