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