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