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