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