]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/contextmenu/contextmenusettingspage.cpp
Add explicit moc includes to sources for moc-covered headers
[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 } else if (id == "copy_to_inactive_split_view") {
133 return ContextMenuSettings::showCopyToOtherSplitView();
134 } else if (id == "move_to_inactive_split_view") {
135 return ContextMenuSettings::showMoveToOtherSplitView();
136 }
137 return false;
138 }
139
140 void ContextMenuSettingsPage::setEntryVisible(const QString &id, bool visible)
141 {
142 if (id == "add_to_places") {
143 ContextMenuSettings::setShowAddToPlaces(visible);
144 } else if (id == "sort") {
145 ContextMenuSettings::setShowSortBy(visible);
146 } else if (id == "view_mode") {
147 ContextMenuSettings::setShowViewMode(visible);
148 } else if (id == "open_in_new_tab") {
149 ContextMenuSettings::setShowOpenInNewTab(visible);
150 } else if (id == "open_in_new_window") {
151 ContextMenuSettings::setShowOpenInNewWindow(visible);
152 } else if (id == "copy_location") {
153 ContextMenuSettings::setShowCopyLocation(visible);
154 } else if (id == "duplicate") {
155 ContextMenuSettings::setShowDuplicateHere(visible);
156 } else if (id == "open_terminal_here") {
157 ContextMenuSettings::setShowOpenTerminal(visible);
158 } else if (id == "copy_to_inactive_split_view") {
159 ContextMenuSettings::setShowCopyToOtherSplitView(visible);
160 } else if (id == "move_to_inactive_split_view") {
161 ContextMenuSettings::setShowMoveToOtherSplitView(visible);
162 }
163 }
164
165 void ContextMenuSettingsPage::applySettings()
166 {
167 if (!m_initialized) {
168 return;
169 }
170
171 KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
172 KConfigGroup showGroup = config.group("Show");
173
174 QStringList enabledPlugins;
175
176 const QAbstractItemModel *model = m_listView->model();
177 for (int i = 0; i < model->rowCount(); ++i) {
178 const QModelIndex index = model->index(i, 0);
179 const QString service = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
180 const bool checked = model->data(index, Qt::CheckStateRole).toBool();
181
182 if (service.startsWith(VersionControlServicePrefix)) {
183 if (checked) {
184 enabledPlugins.append(model->data(index, Qt::DisplayRole).toString());
185 }
186 } else if (service == QLatin1String(DeleteService)) {
187 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals);
188 KConfigGroup configGroup(globalConfig, "KDE");
189 configGroup.writeEntry("ShowDeleteCommand", checked);
190 configGroup.sync();
191 } else if (service == QLatin1String(CopyToMoveToService)) {
192 ContextMenuSettings::setShowCopyMoveMenu(checked);
193 ContextMenuSettings::self()->save();
194 } else if (m_actionIds.contains(service)) {
195 setEntryVisible(service, checked);
196 ContextMenuSettings::self()->save();
197 } else {
198 showGroup.writeEntry(service, checked);
199 }
200 }
201
202 showGroup.sync();
203
204 if (m_enabledVcsPlugins != enabledPlugins) {
205 VersionControlSettings::setEnabledPlugins(enabledPlugins);
206 VersionControlSettings::self()->save();
207
208 if (!laterSelected) {
209 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
210 KMessageBox::ButtonCode promptRestart =
211 KMessageBox::questionTwoActions(window(),
212 #else
213 KMessageBox::ButtonCode promptRestart =
214 KMessageBox::questionYesNo(window(),
215 #endif
216 i18nc("@info",
217 "Dolphin must be restarted to apply the "
218 "updated version control system settings."),
219 i18nc("@info", "Restart now?"),
220 KGuiItem(QApplication::translate("KStandardGuiItem", "&Restart"), QStringLiteral("dialog-restart")),
221 KGuiItem(QApplication::translate("KStandardGuiItem", "&Later"), QStringLiteral("dialog-later")));
222 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
223 if (promptRestart == KMessageBox::ButtonCode::PrimaryAction) {
224 #else
225 if (promptRestart == KMessageBox::ButtonCode::Yes) {
226 #endif
227 Dolphin::openNewWindow();
228 qApp->quit();
229 } else {
230 laterSelected = true;
231 }
232 }
233 }
234 }
235
236 void ContextMenuSettingsPage::restoreDefaults()
237 {
238 QAbstractItemModel *model = m_listView->model();
239 for (int i = 0; i < model->rowCount(); ++i) {
240 const QModelIndex index = model->index(i, 0);
241 const QString service = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
242
243 const bool checked =
244 !service.startsWith(VersionControlServicePrefix) && service != QLatin1String(DeleteService) && service != QLatin1String(CopyToMoveToService);
245 model->setData(index, checked, Qt::CheckStateRole);
246 }
247 }
248
249 void ContextMenuSettingsPage::showEvent(QShowEvent *event)
250 {
251 if (!event->spontaneous() && !m_initialized) {
252 loadServices();
253
254 loadVersionControlSystems();
255
256 // Add "Show 'Delete' command" as service
257 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals);
258 KConfigGroup configGroup(globalConfig, "KDE");
259 addRow(QStringLiteral("edit-delete"), i18nc("@option:check", "Delete"), DeleteService, configGroup.readEntry("ShowDeleteCommand", ShowDeleteDefault));
260
261 // Add "Show 'Copy To' and 'Move To' commands" as service
262 addRow(QStringLiteral("edit-copy"),
263 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
264 CopyToMoveToService,
265 ContextMenuSettings::showCopyMoveMenu());
266
267 if (m_actions) {
268 // Add other built-in actions
269 for (const QString &id : m_actionIds) {
270 const QAction *action = m_actions->action(id);
271 if (action) {
272 addRow(action->icon().name(), action->text(), id, entryVisible(id));
273 }
274 }
275 }
276
277 m_sortModel->sort(Qt::DisplayRole);
278
279 m_initialized = true;
280 }
281 SettingsPageBase::showEvent(event);
282 }
283
284 void ContextMenuSettingsPage::loadServices()
285 {
286 const KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
287 const KConfigGroup showGroup = config.group("Show");
288
289 // Load generic services
290 const auto locations = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("kio/servicemenus"), QStandardPaths::LocateDirectory);
291 QStringList files = KFileUtils::findAllUniqueFiles(locations);
292
293 #if KIOWIDGETS_BUILD_DEPRECATED_SINCE(5, 90)
294 const KService::List services = KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
295 for (const KService::Ptr &service : services) {
296 files << QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kservices5/" % service->entryPath());
297 }
298 #endif
299
300 for (const auto &file : qAsConst(files)) {
301 const QList<KServiceAction> serviceActions = KDesktopFileActions::userDefinedServices(KService(file), true);
302
303 const KDesktopFile desktopFile(file);
304 const QString subMenuName = desktopFile.desktopGroup().readEntry("X-KDE-Submenu");
305
306 for (const KServiceAction &action : serviceActions) {
307 const QString serviceName = action.name();
308 const bool addService = !action.noDisplay() && !action.isSeparator() && !isInServicesList(serviceName);
309
310 if (addService) {
311 const QString itemName = subMenuName.isEmpty() ? action.text() : i18nc("@item:inmenu", "%1: %2", subMenuName, action.text());
312 const bool checked = showGroup.readEntry(serviceName, true);
313 addRow(action.icon(), itemName, serviceName, checked);
314 }
315 }
316 }
317
318 // Load service plugins, this is deprecated in KIO 5.82
319 #if KIOCORE_BUILD_DEPRECATED_SINCE(5, 82)
320 const KService::List pluginServices = KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
321 for (const KService::Ptr &service : pluginServices) {
322 const QString desktopEntryName = service->desktopEntryName();
323 if (!isInServicesList(desktopEntryName)) {
324 const bool checked = showGroup.readEntry(desktopEntryName, true);
325 addRow(service->icon(), service->name(), desktopEntryName, checked);
326 }
327 }
328 #endif
329
330 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
331 const auto jsonPlugins = KPluginMetaData::findPlugins(QStringLiteral("kf" QT_STRINGIFY(QT_VERSION_MAJOR)) + QStringLiteral("/kfileitemaction"));
332
333 for (const auto &jsonMetadata : jsonPlugins) {
334 const QString desktopEntryName = jsonMetadata.pluginId();
335 if (!isInServicesList(desktopEntryName)) {
336 const bool checked = showGroup.readEntry(desktopEntryName, true);
337 addRow(jsonMetadata.iconName(), jsonMetadata.name(), desktopEntryName, checked);
338 }
339 }
340
341 m_sortModel->sort(Qt::DisplayRole);
342 m_searchLineEdit->setFocus(Qt::OtherFocusReason);
343 }
344
345 void ContextMenuSettingsPage::loadVersionControlSystems()
346 {
347 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
348
349 // Create a checkbox for each available version control plugin
350 QSet<QString> loadedPlugins;
351
352 const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("dolphin/vcs"));
353 for (const auto &plugin : plugins) {
354 const QString pluginName = plugin.name();
355 addRow(QStringLiteral("code-class"), pluginName, VersionControlServicePrefix + pluginName, enabledPlugins.contains(pluginName));
356 loadedPlugins += pluginName;
357 }
358
359 m_sortModel->sort(Qt::DisplayRole);
360 }
361
362 bool ContextMenuSettingsPage::isInServicesList(const QString &service) const
363 {
364 for (int i = 0; i < m_serviceModel->rowCount(); ++i) {
365 const QModelIndex index = m_serviceModel->index(i, 0);
366 if (m_serviceModel->data(index, ServiceModel::DesktopEntryNameRole).toString() == service) {
367 return true;
368 }
369 }
370 return false;
371 }
372
373 void ContextMenuSettingsPage::addRow(const QString &icon, const QString &text, const QString &value, bool checked)
374 {
375 m_serviceModel->insertRow(0);
376
377 const QModelIndex index = m_serviceModel->index(0, 0);
378 m_serviceModel->setData(index, icon, Qt::DecorationRole);
379 m_serviceModel->setData(index, text, Qt::DisplayRole);
380 m_serviceModel->setData(index, value, ServiceModel::DesktopEntryNameRole);
381 m_serviceModel->setData(index, checked, Qt::CheckStateRole);
382 }
383
384 #include "moc_contextmenusettingspage.cpp"