]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/services/servicessettingspage.cpp
Merge branch 'master' into frameworks
[dolphin.git] / src / settings / services / servicessettingspage.cpp
1 /***************************************************************************
2 * Copyright (C) 2009-2010 by Peter Penz <peter.penz19@gmail.com> *
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_generalsettings.h"
23 #include "dolphin_versioncontrolsettings.h"
24
25 #include <KConfig>
26 #include <KConfigGroup>
27 #include <KDesktopFile>
28 #include <kdesktopfileactions.h>
29 #include <QIcon>
30 #include <KLocalizedString>
31 #include <KMessageBox>
32 #include <KNS3/Button>
33 #include <KService>
34 #include <KServiceTypeTrader>
35 #include <QStandardPaths>
36
37 #include <settings/serviceitemdelegate.h>
38 #include <settings/servicemodel.h>
39
40 #include <QCheckBox>
41 #include <QGridLayout>
42 #include <QGroupBox>
43 #include <QLabel>
44 #include <QListWidget>
45 #include <QPushButton>
46 #include <QSortFilterProxyModel>
47 #include <QShowEvent>
48
49 namespace
50 {
51 const bool ShowDeleteDefault = false;
52 const char VersionControlServicePrefix[] = "_version_control_";
53 const char DeleteService[] = "_delete";
54 const char CopyToMoveToService[] ="_copy_to_move_to";
55 }
56
57 ServicesSettingsPage::ServicesSettingsPage(QWidget* parent) :
58 SettingsPageBase(parent),
59 m_initialized(false),
60 m_serviceModel(0),
61 m_sortModel(0),
62 m_listView(0),
63 m_enabledVcsPlugins()
64 {
65 QVBoxLayout* topLayout = new QVBoxLayout(this);
66
67 QLabel* label = new QLabel(i18nc("@label:textbox",
68 "Select which services should "
69 "be shown in the context menu:"), this);
70 label->setWordWrap(true);
71
72 m_listView = new QListView(this);
73 ServiceItemDelegate* delegate = new ServiceItemDelegate(m_listView, m_listView);
74 m_serviceModel = new ServiceModel(this);
75 m_sortModel = new QSortFilterProxyModel(this);
76 m_sortModel->setSourceModel(m_serviceModel);
77 m_sortModel->setSortRole(Qt::DisplayRole);
78 m_listView->setModel(m_sortModel);
79 m_listView->setItemDelegate(delegate);
80 m_listView->setVerticalScrollMode(QListView::ScrollPerPixel);
81 connect(m_listView, &QListView::clicked, this, &ServicesSettingsPage::changed);
82
83 KNS3::Button* downloadButton = new KNS3::Button(i18nc("@action:button", "Download New Services..."),
84 "servicemenu.knsrc",
85 this);
86 connect(downloadButton, &KNS3::Button::dialogFinished, this, &ServicesSettingsPage::loadServices);
87
88 topLayout->addWidget(label);
89 topLayout->addWidget(m_listView);
90 topLayout->addWidget(downloadButton);
91
92 m_enabledVcsPlugins = VersionControlSettings::enabledPlugins();
93 qSort(m_enabledVcsPlugins);
94 }
95
96 ServicesSettingsPage::~ServicesSettingsPage()
97 {
98 }
99
100 void ServicesSettingsPage::applySettings()
101 {
102 if (!m_initialized) {
103 return;
104 }
105
106 KConfig config("kservicemenurc", KConfig::NoGlobals);
107 KConfigGroup showGroup = config.group("Show");
108
109 QStringList enabledPlugins;
110
111 const QAbstractItemModel* model = m_listView->model();
112 for (int i = 0; i < model->rowCount(); ++i) {
113 const QModelIndex index = model->index(i, 0);
114 const QString service = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
115 const bool checked = model->data(index, Qt::CheckStateRole).toBool();
116
117 if (service.startsWith(VersionControlServicePrefix)) {
118 if (checked) {
119 enabledPlugins.append(model->data(index, Qt::DisplayRole).toString());
120 }
121 } else if (service == QLatin1String(DeleteService)) {
122 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::NoGlobals);
123 KConfigGroup configGroup(globalConfig, "KDE");
124 configGroup.writeEntry("ShowDeleteCommand", checked);
125 configGroup.sync();
126 } else if (service == QLatin1String(CopyToMoveToService)) {
127 GeneralSettings::setShowCopyMoveMenu(checked);
128 GeneralSettings::self()->save();
129 } else {
130 showGroup.writeEntry(service, checked);
131 }
132 }
133
134 showGroup.sync();
135
136 if (m_enabledVcsPlugins != enabledPlugins) {
137 VersionControlSettings::setEnabledPlugins(enabledPlugins);
138 VersionControlSettings::self()->save();
139
140 KMessageBox::information(window(),
141 i18nc("@info", "Dolphin must be restarted to apply the "
142 "updated version control systems settings."),
143 QString(), // default title
144 QLatin1String("ShowVcsRestartInformation"));
145 }
146 }
147
148 void ServicesSettingsPage::restoreDefaults()
149 {
150 QAbstractItemModel* model = m_listView->model();
151 for (int i = 0; i < model->rowCount(); ++i) {
152 const QModelIndex index = model->index(i, 0);
153 const QString service = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
154
155 const bool checked = !service.startsWith(VersionControlServicePrefix)
156 && service != QLatin1String(DeleteService)
157 && service != QLatin1String(CopyToMoveToService);
158 model->setData(index, checked, Qt::CheckStateRole);
159 }
160 }
161
162 void ServicesSettingsPage::showEvent(QShowEvent* event)
163 {
164 if (!event->spontaneous() && !m_initialized) {
165 loadServices();
166
167 loadVersionControlSystems();
168
169 // Add "Show 'Delete' command" as service
170 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals);
171 KConfigGroup configGroup(globalConfig, "KDE");
172 addRow("edit-delete",
173 i18nc("@option:check", "Delete"),
174 DeleteService,
175 configGroup.readEntry("ShowDeleteCommand", ShowDeleteDefault));
176
177 // Add "Show 'Copy To' and 'Move To' commands" as service
178 addRow("edit-copy",
179 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
180 CopyToMoveToService,
181 GeneralSettings::showCopyMoveMenu());
182
183 m_sortModel->sort(Qt::DisplayRole);
184
185 m_initialized = true;
186 }
187 SettingsPageBase::showEvent(event);
188 }
189
190 void ServicesSettingsPage::loadServices()
191 {
192 const KConfig config("kservicemenurc", KConfig::NoGlobals);
193 const KConfigGroup showGroup = config.group("Show");
194
195 // Load generic services
196 const KService::List entries = KServiceTypeTrader::self()->query("KonqPopupMenu/Plugin");
197 foreach (const KService::Ptr& service, entries) {
198 const QString file = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kservices5/" % service->entryPath());
199 const QList<KServiceAction> serviceActions =
200 KDesktopFileActions::userDefinedServices(file, true);
201
202 KDesktopFile desktopFile(file);
203 const QString subMenuName = desktopFile.desktopGroup().readEntry("X-KDE-Submenu");
204
205 foreach (const KServiceAction& action, serviceActions) {
206 const QString serviceName = action.name();
207 const bool addService = !action.noDisplay()
208 && !action.isSeparator()
209 && !isInServicesList(serviceName);
210
211 if (addService) {
212 const QString itemName = subMenuName.isEmpty()
213 ? action.text()
214 : i18nc("@item:inmenu", "%1: %2", subMenuName, action.text());
215 const bool checked = showGroup.readEntry(serviceName, true);
216 addRow(action.icon(), itemName, serviceName, checked);
217 }
218 }
219 }
220
221 // Load service plugins that implement the KFileItemActionPlugin interface
222 const KService::List pluginServices = KServiceTypeTrader::self()->query("KFileItemAction/Plugin");
223 foreach (const KService::Ptr& service, pluginServices) {
224 const QString desktopEntryName = service->desktopEntryName();
225 if (!isInServicesList(desktopEntryName)) {
226 const bool checked = showGroup.readEntry(desktopEntryName, true);
227 addRow(service->icon(), service->name(), desktopEntryName, checked);
228 }
229 }
230
231 m_sortModel->sort(Qt::DisplayRole);
232 }
233
234 void ServicesSettingsPage::loadVersionControlSystems()
235 {
236 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
237
238 // Create a checkbox for each available version control plugin
239 const KService::List pluginServices = KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
240 for (KService::List::ConstIterator it = pluginServices.constBegin(); it != pluginServices.constEnd(); ++it) {
241 const QString pluginName = (*it)->name();
242 addRow("code-class",
243 pluginName,
244 VersionControlServicePrefix + pluginName,
245 enabledPlugins.contains(pluginName));
246 }
247
248 m_sortModel->sort(Qt::DisplayRole);
249 }
250
251 bool ServicesSettingsPage::isInServicesList(const QString& service) const
252 {
253 for (int i = 0; i < m_serviceModel->rowCount(); ++i) {
254 const QModelIndex index = m_serviceModel->index(i, 0);
255 if (m_serviceModel->data(index, ServiceModel::DesktopEntryNameRole).toString() == service) {
256 return true;
257 }
258 }
259 return false;
260 }
261
262 void ServicesSettingsPage::addRow(const QString& icon,
263 const QString& text,
264 const QString& value,
265 bool checked)
266 {
267 m_serviceModel->insertRow(0);
268
269 const QModelIndex index = m_serviceModel->index(0, 0);
270 m_serviceModel->setData(index, icon, Qt::DecorationRole);
271 m_serviceModel->setData(index, text, Qt::DisplayRole);
272 m_serviceModel->setData(index, value, ServiceModel::DesktopEntryNameRole);
273 m_serviceModel->setData(index, checked, Qt::CheckStateRole);
274 }
275