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