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