]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/contextmenu/contextmenusettingspage.cpp
Port away from deprecated KDesktopFileActions::userDefinedServices overload
[dolphin.git] / src / settings / contextmenu / contextmenusettingspage.cpp
1 /*
2 * SPDX-FileCopyrightText: 2009-2010 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "contextmenusettingspage.h"
8
9 #include "dolphin_generalsettings.h"
10 #include "dolphin_versioncontrolsettings.h"
11 #include "dolphin_contextmenusettings.h"
12 #include "settings/serviceitemdelegate.h"
13 #include "settings/servicemodel.h"
14 #include "global.h"
15
16 #include <KDesktopFile>
17 #include <KLocalizedString>
18 #include <KMessageBox>
19 #include <KNS3/Button>
20 #include <KPluginMetaData>
21 #include <KService>
22 #include <KServiceTypeTrader>
23 #include <KDesktopFileActions>
24
25 #include <kio_version.h>
26
27 #include <QGridLayout>
28 #include <QLabel>
29 #include <QListWidget>
30 #include <QScroller>
31 #include <QShowEvent>
32 #include <QSortFilterProxyModel>
33 #include <QLineEdit>
34 #include <QApplication>
35
36 namespace
37 {
38 const bool ShowDeleteDefault = false;
39 const char VersionControlServicePrefix[] = "_version_control_";
40 const char DeleteService[] = "_delete";
41 const char CopyToMoveToService[] ="_copy_to_move_to";
42
43 bool laterSelected = false;
44 }
45
46 ContextMenuSettingsPage::ContextMenuSettingsPage(QWidget* parent,
47 const KActionCollection* actions,
48 const QStringList& actionIds) :
49 SettingsPageBase(parent),
50 m_initialized(false),
51 m_serviceModel(nullptr),
52 m_sortModel(nullptr),
53 m_listView(nullptr),
54 m_enabledVcsPlugins(),
55 m_actions(actions),
56 m_actionIds(actionIds)
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 m_searchLineEdit = new QLineEdit(this);
65 m_searchLineEdit->setPlaceholderText(i18nc("@label:textbox", "Search..."));
66 connect(m_searchLineEdit, &QLineEdit::textChanged, this, [this](const QString &filter){
67 m_sortModel->setFilterFixedString(filter);
68 });
69
70 m_listView = new QListView(this);
71 QScroller::grabGesture(m_listView->viewport(), QScroller::TouchGesture);
72
73 auto *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_sortModel->setSortLocaleAware(true);
79 m_sortModel->setFilterRole(Qt::DisplayRole);
80 m_sortModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
81 m_listView->setModel(m_sortModel);
82 m_listView->setItemDelegate(delegate);
83 m_listView->setVerticalScrollMode(QListView::ScrollPerPixel);
84 connect(m_listView, &QListView::clicked, this, &ContextMenuSettingsPage::changed);
85
86 #ifndef Q_OS_WIN
87 auto *downloadButton = new KNS3::Button(i18nc("@action:button", "Download New Services..."),
88 QStringLiteral("servicemenu.knsrc"),
89 this);
90 connect(downloadButton, &KNS3::Button::dialogFinished, this, [this](const KNS3::Entry::List &changedEntries) {
91 if (!changedEntries.isEmpty()) {
92 m_serviceModel->clear();
93 loadServices();
94 }
95 });
96
97 #endif
98
99 topLayout->addWidget(label);
100 topLayout->addWidget(m_searchLineEdit);
101 topLayout->addWidget(m_listView);
102 #ifndef Q_OS_WIN
103 topLayout->addWidget(downloadButton);
104 #endif
105
106 m_enabledVcsPlugins = VersionControlSettings::enabledPlugins();
107 std::sort(m_enabledVcsPlugins.begin(), m_enabledVcsPlugins.end());
108 }
109
110 ContextMenuSettingsPage::~ContextMenuSettingsPage() {
111 }
112
113 bool ContextMenuSettingsPage::entryVisible(const QString& id)
114 {
115 if (id == "add_to_places") {
116 return ContextMenuSettings::showAddToPlaces();
117 } else if (id == "sort") {
118 return ContextMenuSettings::showSortBy();
119 } else if (id == "view_mode") {
120 return ContextMenuSettings::showViewMode();
121 } else if (id == "open_in_new_tab") {
122 return ContextMenuSettings::showOpenInNewTab();
123 } else if (id == "open_in_new_window") {
124 return ContextMenuSettings::showOpenInNewWindow();
125 } else if (id == "copy_location") {
126 return ContextMenuSettings::showCopyLocation();
127 } else if (id == "duplicate") {
128 return ContextMenuSettings::showDuplicateHere();
129 } else if (id == "open_terminal") {
130 return ContextMenuSettings::showOpenTerminal();
131 }
132 return false;
133 }
134
135 void ContextMenuSettingsPage::setEntryVisible(const QString& id, bool visible)
136 {
137 if (id == "add_to_places") {
138 ContextMenuSettings::setShowAddToPlaces(visible);
139 } else if (id == "sort") {
140 ContextMenuSettings::setShowSortBy(visible);
141 } else if (id == "view_mode") {
142 ContextMenuSettings::setShowViewMode(visible);
143 } else if (id == "open_in_new_tab") {
144 ContextMenuSettings::setShowOpenInNewTab(visible);
145 } else if (id == "open_in_new_window") {
146 ContextMenuSettings::setShowOpenInNewWindow(visible);
147 } else if (id == "copy_location") {
148 ContextMenuSettings::setShowCopyLocation(visible);
149 } else if (id == "duplicate") {
150 ContextMenuSettings::setShowDuplicateHere(visible);
151 } else if (id == "open_terminal") {
152 ContextMenuSettings::setShowOpenTerminal(visible);
153 }
154 }
155
156 void ContextMenuSettingsPage::applySettings()
157 {
158 if (!m_initialized) {
159 return;
160 }
161
162 KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
163 KConfigGroup showGroup = config.group("Show");
164
165 QStringList enabledPlugins;
166
167 const QAbstractItemModel *model = m_listView->model();
168 for (int i = 0; i < model->rowCount(); ++i) {
169 const QModelIndex index = model->index(i, 0);
170 const QString service = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
171 const bool checked = model->data(index, Qt::CheckStateRole).toBool();
172
173 if (service.startsWith(VersionControlServicePrefix)) {
174 if (checked) {
175 enabledPlugins.append(model->data(index, Qt::DisplayRole).toString());
176 }
177 } else if (service == QLatin1String(DeleteService)) {
178 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals);
179 KConfigGroup configGroup(globalConfig, "KDE");
180 configGroup.writeEntry("ShowDeleteCommand", checked);
181 configGroup.sync();
182 } else if (service == QLatin1String(CopyToMoveToService)) {
183 ContextMenuSettings::setShowCopyMoveMenu(checked);
184 ContextMenuSettings::self()->save();
185 } else if (m_actionIds.contains(service)) {
186 setEntryVisible(service, checked);
187 ContextMenuSettings::self()->save();
188 } else {
189 showGroup.writeEntry(service, checked);
190 }
191 }
192
193 showGroup.sync();
194
195 if (m_enabledVcsPlugins != enabledPlugins) {
196 VersionControlSettings::setEnabledPlugins(enabledPlugins);
197 VersionControlSettings::self()->save();
198
199 if (!laterSelected) {
200 KMessageBox::ButtonCode promptRestart = KMessageBox::questionYesNo(window(),
201 i18nc("@info", "Dolphin must be restarted to apply the "
202 "updated version control system settings."),
203 i18nc("@info", "Restart now?"),
204 KGuiItem(QApplication::translate("KStandardGuiItem", "&Restart"), QStringLiteral("dialog-restart")),
205 KGuiItem(QApplication::translate("KStandardGuiItem", "&Later"), QStringLiteral("dialog-later"))
206 );
207 if (promptRestart == KMessageBox::ButtonCode::Yes) {
208 Dolphin::openNewWindow();
209 qApp->quit();
210 } else {
211 laterSelected = true;
212 }
213 }
214 }
215 }
216
217 void ContextMenuSettingsPage::restoreDefaults()
218 {
219 QAbstractItemModel* model = m_listView->model();
220 for (int i = 0; i < model->rowCount(); ++i) {
221 const QModelIndex index = model->index(i, 0);
222 const QString service = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
223
224 const bool checked = !service.startsWith(VersionControlServicePrefix)
225 && service != QLatin1String(DeleteService)
226 && service != QLatin1String(CopyToMoveToService);
227 model->setData(index, checked, Qt::CheckStateRole);
228 }
229 }
230
231 void ContextMenuSettingsPage::showEvent(QShowEvent* event)
232 {
233 if (!event->spontaneous() && !m_initialized) {
234 loadServices();
235
236 loadVersionControlSystems();
237
238 // Add "Show 'Delete' command" as service
239 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals);
240 KConfigGroup configGroup(globalConfig, "KDE");
241 addRow(QStringLiteral("edit-delete"),
242 i18nc("@option:check", "Delete"),
243 DeleteService,
244 configGroup.readEntry("ShowDeleteCommand", ShowDeleteDefault));
245
246 // Add "Show 'Copy To' and 'Move To' commands" as service
247 addRow(QStringLiteral("edit-copy"),
248 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
249 CopyToMoveToService,
250 ContextMenuSettings::showCopyMoveMenu());
251
252 if (m_actions){
253 // Add other built-in actions
254 for (const QString& id : m_actionIds) {
255 const QAction* action = m_actions->action(id);
256 if (action) {
257 addRow(action->icon().name(), action->text(), id, entryVisible(id));
258 }
259 }
260 }
261
262 m_sortModel->sort(Qt::DisplayRole);
263
264 m_initialized = true;
265 }
266 SettingsPageBase::showEvent(event);
267 }
268
269 void ContextMenuSettingsPage::loadServices()
270 {
271 const KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
272 const KConfigGroup showGroup = config.group("Show");
273
274 // Load generic services
275 const KService::List entries = KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
276 for (const KService::Ptr &service : entries) {
277 const QString file = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kservices5/" % service->entryPath());
278 const QList<KServiceAction> serviceActions = KDesktopFileActions::userDefinedServices(KService(file), true);
279
280 const KDesktopFile desktopFile(file);
281 const QString subMenuName = desktopFile.desktopGroup().readEntry("X-KDE-Submenu");
282
283 for (const KServiceAction &action : serviceActions) {
284 const QString serviceName = action.name();
285 const bool addService = !action.noDisplay() && !action.isSeparator() && !isInServicesList(serviceName);
286
287 if (addService) {
288 const QString itemName = subMenuName.isEmpty()
289 ? action.text()
290 : i18nc("@item:inmenu", "%1: %2", subMenuName, action.text());
291 const bool checked = showGroup.readEntry(serviceName, true);
292 addRow(action.icon(), itemName, serviceName, checked);
293 }
294 }
295 }
296
297 // Load service plugins, this is deprecated in KIO 5.82
298 #if KIO_VERSION < QT_VERSION_CHECK(6, 0, 0)
299 const KService::List pluginServices = KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
300 for (const KService::Ptr &service : pluginServices) {
301 const QString desktopEntryName = service->desktopEntryName();
302 if (!isInServicesList(desktopEntryName)) {
303 const bool checked = showGroup.readEntry(desktopEntryName, true);
304 addRow(service->icon(), service->name(), desktopEntryName, checked);
305 }
306 }
307 #endif
308
309 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
310 const auto jsonPlugins = KPluginMetaData::findPlugins(QStringLiteral("kf5/kfileitemaction"));
311
312 for (const auto &jsonMetadata : jsonPlugins) {
313 const QString desktopEntryName = jsonMetadata.pluginId();
314 if (!isInServicesList(desktopEntryName)) {
315 const bool checked = showGroup.readEntry(desktopEntryName, true);
316 addRow(jsonMetadata.iconName(), jsonMetadata.name(), desktopEntryName, checked);
317 }
318 }
319
320 m_sortModel->sort(Qt::DisplayRole);
321 m_searchLineEdit->setFocus(Qt::OtherFocusReason);
322 }
323
324 void ContextMenuSettingsPage::loadVersionControlSystems()
325 {
326 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
327
328 // Create a checkbox for each available version control plugin
329 QSet<QString> loadedPlugins;
330
331 const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("dolphin/vcs"));
332 for (const auto &plugin : plugins) {
333 const QString pluginName = plugin.name();
334 addRow(QStringLiteral("code-class"),
335 pluginName,
336 VersionControlServicePrefix + pluginName,
337 enabledPlugins.contains(pluginName));
338 loadedPlugins += pluginName;
339 }
340
341 const KService::List pluginServices = KServiceTypeTrader::self()->query(QStringLiteral("FileViewVersionControlPlugin"));
342 for (const auto &plugin : pluginServices) {
343 const QString pluginName = plugin->name();
344 if (loadedPlugins.contains(pluginName)) {
345 continue;
346 }
347 addRow(QStringLiteral("code-class"),
348 pluginName,
349 VersionControlServicePrefix + pluginName,
350 enabledPlugins.contains(pluginName));
351 }
352
353 m_sortModel->sort(Qt::DisplayRole);
354 }
355
356 bool ContextMenuSettingsPage::isInServicesList(const QString &service) const
357 {
358 for (int i = 0; i < m_serviceModel->rowCount(); ++i) {
359 const QModelIndex index = m_serviceModel->index(i, 0);
360 if (m_serviceModel->data(index, ServiceModel::DesktopEntryNameRole).toString() == service) {
361 return true;
362 }
363 }
364 return false;
365 }
366
367 void ContextMenuSettingsPage::addRow(const QString &icon,
368 const QString &text,
369 const QString &value,
370 bool checked)
371 {
372 m_serviceModel->insertRow(0);
373
374 const QModelIndex index = m_serviceModel->index(0, 0);
375 m_serviceModel->setData(index, icon, Qt::DecorationRole);
376 m_serviceModel->setData(index, text, Qt::DisplayRole);
377 m_serviceModel->setData(index, value, ServiceModel::DesktopEntryNameRole);
378 m_serviceModel->setData(index, checked, Qt::CheckStateRole);
379 }