]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/contextmenu/contextmenusettingspage.cpp
Replace qAsConst with std::as_const
[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/servicemodel.h"
14
15 #include <KDesktopFile>
16 #include <KDesktopFileActions>
17 #include <KFileUtils>
18 #include <KLocalizedString>
19 #include <KMessageBox>
20 #include <KPluginMetaData>
21 #include <KService>
22 #include <kiocore_export.h>
23 #include <kservice_export.h>
24 #include <kwidgetsaddons_version.h>
25
26 #include <KNSWidgets/Button>
27 #include <QtGlobal>
28
29 #include <QApplication>
30 #include <QGridLayout>
31 #include <QLabel>
32 #include <QLineEdit>
33 #include <QListWidget>
34 #include <QScroller>
35 #include <QShowEvent>
36 #include <QSortFilterProxyModel>
37
38 namespace
39 {
40 const bool ShowDeleteDefault = false;
41 const char VersionControlServicePrefix[] = "_version_control_";
42 const char DeleteService[] = "_delete";
43 const char CopyToMoveToService[] = "_copy_to_move_to";
44
45 bool laterSelected = false;
46 }
47
48 ContextMenuSettingsPage::ContextMenuSettingsPage(QWidget *parent, const KActionCollection *actions, 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:"),
63 this);
64 label->setWordWrap(true);
65 m_searchLineEdit = new QLineEdit(this);
66 m_searchLineEdit->setPlaceholderText(i18nc("@label:textbox", "Search…"));
67 connect(m_searchLineEdit, &QLineEdit::textChanged, this, [this](const QString &filter) {
68 m_sortModel->setFilterFixedString(filter);
69 });
70
71 m_listView = new QListView(this);
72 QScroller::grabGesture(m_listView->viewport(), QScroller::TouchGesture);
73
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->setVerticalScrollMode(QListView::ScrollPerPixel);
83 connect(m_listView, &QListView::clicked, this, &ContextMenuSettingsPage::changed);
84
85 topLayout->addWidget(label);
86 topLayout->addWidget(m_searchLineEdit);
87 topLayout->addWidget(m_listView);
88
89 #ifndef Q_OS_WIN
90 using NewStuffButton = KNSWidgets::Button;
91 auto *downloadButton = new NewStuffButton(i18nc("@action:button", "Download New Services…"), QStringLiteral("servicemenu.knsrc"), this);
92 connect(downloadButton, &NewStuffButton::dialogFinished, this, [this](const auto &changedEntries) {
93 if (!changedEntries.isEmpty()) {
94 m_serviceModel->clear();
95 loadServices();
96 }
97 });
98 topLayout->addWidget(downloadButton);
99 #endif // Q_OS_WIN
100
101 m_enabledVcsPlugins = VersionControlSettings::enabledPlugins();
102 std::sort(m_enabledVcsPlugins.begin(), m_enabledVcsPlugins.end());
103 }
104
105 ContextMenuSettingsPage::~ContextMenuSettingsPage()
106 {
107 }
108
109 bool ContextMenuSettingsPage::entryVisible(const QString &id)
110 {
111 if (id == "add_to_places") {
112 return ContextMenuSettings::showAddToPlaces();
113 } else if (id == "sort") {
114 return ContextMenuSettings::showSortBy();
115 } else if (id == "view_mode") {
116 return ContextMenuSettings::showViewMode();
117 } else if (id == "open_in_new_tab") {
118 return ContextMenuSettings::showOpenInNewTab();
119 } else if (id == "open_in_new_window") {
120 return ContextMenuSettings::showOpenInNewWindow();
121 } else if (id == "copy_location") {
122 return ContextMenuSettings::showCopyLocation();
123 } else if (id == "duplicate") {
124 return ContextMenuSettings::showDuplicateHere();
125 } else if (id == "open_terminal_here") {
126 return ContextMenuSettings::showOpenTerminal();
127 } else if (id == "copy_to_inactive_split_view") {
128 return ContextMenuSettings::showCopyToOtherSplitView();
129 } else if (id == "move_to_inactive_split_view") {
130 return ContextMenuSettings::showMoveToOtherSplitView();
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_here") {
152 ContextMenuSettings::setShowOpenTerminal(visible);
153 } else if (id == "copy_to_inactive_split_view") {
154 ContextMenuSettings::setShowCopyToOtherSplitView(visible);
155 } else if (id == "move_to_inactive_split_view") {
156 ContextMenuSettings::setShowMoveToOtherSplitView(visible);
157 }
158 }
159
160 void ContextMenuSettingsPage::applySettings()
161 {
162 if (!m_initialized) {
163 return;
164 }
165
166 KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
167 KConfigGroup showGroup = config.group("Show");
168
169 QStringList enabledPlugins;
170
171 const QAbstractItemModel *model = m_listView->model();
172 for (int i = 0; i < model->rowCount(); ++i) {
173 const QModelIndex index = model->index(i, 0);
174 const QString service = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
175 const bool checked = model->data(index, Qt::CheckStateRole).value<Qt::CheckState>() == Qt::Checked;
176
177 if (service.startsWith(VersionControlServicePrefix)) {
178 if (checked) {
179 enabledPlugins.append(model->data(index, Qt::DisplayRole).toString());
180 }
181 } else if (service == QLatin1String(DeleteService)) {
182 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals);
183 KConfigGroup configGroup(globalConfig, "KDE");
184 configGroup.writeEntry("ShowDeleteCommand", checked);
185 configGroup.sync();
186 } else if (service == QLatin1String(CopyToMoveToService)) {
187 ContextMenuSettings::setShowCopyMoveMenu(checked);
188 ContextMenuSettings::self()->save();
189 } else if (m_actionIds.contains(service)) {
190 setEntryVisible(service, checked);
191 ContextMenuSettings::self()->save();
192 } else {
193 showGroup.writeEntry(service, checked);
194 }
195 }
196
197 showGroup.sync();
198
199 if (m_enabledVcsPlugins != enabledPlugins) {
200 VersionControlSettings::setEnabledPlugins(enabledPlugins);
201 VersionControlSettings::self()->save();
202
203 if (!laterSelected) {
204 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
205 KMessageBox::ButtonCode promptRestart =
206 KMessageBox::questionTwoActions(window(),
207 #else
208 KMessageBox::ButtonCode promptRestart =
209 KMessageBox::questionYesNo(window(),
210 #endif
211 i18nc("@info",
212 "Dolphin must be restarted to apply the "
213 "updated version control system settings."),
214 i18nc("@info", "Restart now?"),
215 KGuiItem(QApplication::translate("KStandardGuiItem", "&Restart"), QStringLiteral("dialog-restart")),
216 KGuiItem(QApplication::translate("KStandardGuiItem", "&Later"), QStringLiteral("dialog-later")));
217 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
218 if (promptRestart == KMessageBox::ButtonCode::PrimaryAction) {
219 #else
220 if (promptRestart == KMessageBox::ButtonCode::Yes) {
221 #endif
222 Dolphin::openNewWindow();
223 qApp->quit();
224 } else {
225 laterSelected = true;
226 }
227 }
228 }
229 }
230
231 void ContextMenuSettingsPage::restoreDefaults()
232 {
233 QAbstractItemModel *model = m_listView->model();
234 for (int i = 0; i < model->rowCount(); ++i) {
235 const QModelIndex index = model->index(i, 0);
236 const QString service = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
237
238 const bool checked =
239 !service.startsWith(VersionControlServicePrefix) && service != QLatin1String(DeleteService) && service != QLatin1String(CopyToMoveToService);
240 model->setData(index, checked ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
241 }
242 }
243
244 void ContextMenuSettingsPage::showEvent(QShowEvent *event)
245 {
246 if (!event->spontaneous() && !m_initialized) {
247 loadServices();
248
249 loadVersionControlSystems();
250
251 // Add "Show 'Delete' command" as service
252 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals);
253 KConfigGroup configGroup(globalConfig, "KDE");
254 addRow(QStringLiteral("edit-delete"), i18nc("@option:check", "Delete"), DeleteService, 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(), KLocalizedString::removeAcceleratorMarker(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
288 for (const auto &file : std::as_const(files)) {
289 const QList<KServiceAction> serviceActions = KDesktopFileActions::userDefinedServices(KService(file), true);
290
291 const KDesktopFile desktopFile(file);
292 const QString subMenuName = desktopFile.desktopGroup().readEntry("X-KDE-Submenu");
293
294 for (const KServiceAction &action : serviceActions) {
295 const QString serviceName = action.name();
296 const bool addService = !action.noDisplay() && !action.isSeparator() && !isInServicesList(serviceName);
297
298 if (addService) {
299 const QString itemName = subMenuName.isEmpty() ? action.text() : i18nc("@item:inmenu", "%1: %2", subMenuName, action.text());
300 const bool checked = showGroup.readEntry(serviceName, true);
301 addRow(action.icon(), itemName, serviceName, checked);
302 }
303 }
304 }
305
306 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
307 const auto jsonPlugins = KPluginMetaData::findPlugins(QStringLiteral("kf6/kfileitemaction"));
308
309 for (const auto &jsonMetadata : jsonPlugins) {
310 const QString desktopEntryName = jsonMetadata.pluginId();
311 if (!isInServicesList(desktopEntryName)) {
312 const bool checked = showGroup.readEntry(desktopEntryName, true);
313 addRow(jsonMetadata.iconName(), jsonMetadata.name(), desktopEntryName, checked);
314 }
315 }
316
317 m_sortModel->sort(Qt::DisplayRole);
318 m_searchLineEdit->setFocus(Qt::OtherFocusReason);
319 }
320
321 void ContextMenuSettingsPage::loadVersionControlSystems()
322 {
323 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
324
325 // Create a checkbox for each available version control plugin
326 QSet<QString> loadedPlugins;
327
328 const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("dolphin/vcs"));
329 for (const auto &plugin : plugins) {
330 const QString pluginName = plugin.name();
331 addRow(QStringLiteral("code-class"), pluginName, VersionControlServicePrefix + pluginName, enabledPlugins.contains(pluginName));
332 loadedPlugins += pluginName;
333 }
334
335 m_sortModel->sort(Qt::DisplayRole);
336 }
337
338 bool ContextMenuSettingsPage::isInServicesList(const QString &service) const
339 {
340 for (int i = 0; i < m_serviceModel->rowCount(); ++i) {
341 const QModelIndex index = m_serviceModel->index(i, 0);
342 if (m_serviceModel->data(index, ServiceModel::DesktopEntryNameRole).toString() == service) {
343 return true;
344 }
345 }
346 return false;
347 }
348
349 void ContextMenuSettingsPage::addRow(const QString &icon, const QString &text, const QString &value, bool checked)
350 {
351 m_serviceModel->insertRow(0);
352
353 const QModelIndex index = m_serviceModel->index(0, 0);
354 m_serviceModel->setData(index, icon, Qt::DecorationRole);
355 m_serviceModel->setData(index, text, Qt::DisplayRole);
356 m_serviceModel->setData(index, value, ServiceModel::DesktopEntryNameRole);
357 m_serviceModel->setData(index, checked ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
358 }
359
360 #include "moc_contextmenusettingspage.cpp"