]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/contextmenu/contextmenusettingspage.cpp
"Group by" exists, group sorting rule is separate from sorting rule. Very WIP and...
[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 == "group") {
114 return ContextMenuSettings::showGroupBy();
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 == "open_in_split_view") {
122 return ContextMenuSettings::showOpenInSplitView();
123 } else if (id == "copy_location") {
124 return ContextMenuSettings::showCopyLocation();
125 } else if (id == "duplicate") {
126 return ContextMenuSettings::showDuplicateHere();
127 } else if (id == "open_terminal_here") {
128 return ContextMenuSettings::showOpenTerminal();
129 } else if (id == "copy_to_inactive_split_view") {
130 return ContextMenuSettings::showCopyToOtherSplitView();
131 } else if (id == "move_to_inactive_split_view") {
132 return ContextMenuSettings::showMoveToOtherSplitView();
133 }
134 return false;
135 }
136
137 void ContextMenuSettingsPage::setEntryVisible(const QString &id, bool visible)
138 {
139 if (id == "add_to_places") {
140 ContextMenuSettings::setShowAddToPlaces(visible);
141 } else if (id == "sort") {
142 ContextMenuSettings::setShowSortBy(visible);
143 } else if (id == "group") {
144 ContextMenuSettings::setShowGroupBy(visible);
145 } else if (id == "view_mode") {
146 ContextMenuSettings::setShowViewMode(visible);
147 } else if (id == "open_in_new_tab") {
148 ContextMenuSettings::setShowOpenInNewTab(visible);
149 } else if (id == "open_in_new_window") {
150 ContextMenuSettings::setShowOpenInNewWindow(visible);
151 } else if (id == "open_in_split_view") {
152 return ContextMenuSettings::setShowOpenInSplitView(visible);
153 } else if (id == "copy_location") {
154 ContextMenuSettings::setShowCopyLocation(visible);
155 } else if (id == "duplicate") {
156 ContextMenuSettings::setShowDuplicateHere(visible);
157 } else if (id == "open_terminal_here") {
158 ContextMenuSettings::setShowOpenTerminal(visible);
159 } else if (id == "copy_to_inactive_split_view") {
160 ContextMenuSettings::setShowCopyToOtherSplitView(visible);
161 } else if (id == "move_to_inactive_split_view") {
162 ContextMenuSettings::setShowMoveToOtherSplitView(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(QStringLiteral("Show"));
174
175 QStringList enabledPlugins;
176
177 for (int i = 0; i < m_serviceModel->rowCount(); ++i) {
178 const QModelIndex index = m_serviceModel->index(i, 0);
179 const QString service = m_serviceModel->data(index, ServiceModel::DesktopEntryNameRole).toString();
180 const bool checked = m_serviceModel->data(index, Qt::CheckStateRole).value<Qt::CheckState>() == Qt::Checked;
181
182 if (service.startsWith(VersionControlServicePrefix)) {
183 if (checked) {
184 enabledPlugins.append(m_serviceModel->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, QStringLiteral("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 KMessageBox::ButtonCode promptRestart =
210 KMessageBox::questionTwoActions(window(),
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 (promptRestart == KMessageBox::ButtonCode::PrimaryAction) {
218 Dolphin::openNewWindow();
219 qApp->quit();
220 } else {
221 laterSelected = true;
222 }
223 }
224 }
225 }
226
227 void ContextMenuSettingsPage::restoreDefaults()
228 {
229 for (int i = 0; i < m_serviceModel->rowCount(); ++i) {
230 const QModelIndex index = m_serviceModel->index(i, 0);
231 const QString service = m_serviceModel->data(index, ServiceModel::DesktopEntryNameRole).toString();
232
233 const bool checked =
234 !service.startsWith(VersionControlServicePrefix) && service != QLatin1String(DeleteService) && service != QLatin1String(CopyToMoveToService);
235 m_serviceModel->setData(index, checked ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
236 }
237 }
238
239 void ContextMenuSettingsPage::showEvent(QShowEvent *event)
240 {
241 if (!event->spontaneous() && !m_initialized) {
242 loadServices();
243
244 loadVersionControlSystems();
245
246 // Add "Show 'Delete' command" as service
247 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals);
248 KConfigGroup configGroup(globalConfig, QStringLiteral("KDE"));
249 addRow(QStringLiteral("edit-delete"), i18nc("@option:check", "Delete"), DeleteService, configGroup.readEntry("ShowDeleteCommand", ShowDeleteDefault));
250
251 // Add "Show 'Copy To' and 'Move To' commands" as service
252 addRow(QStringLiteral("edit-copy"),
253 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
254 CopyToMoveToService,
255 ContextMenuSettings::showCopyMoveMenu());
256
257 if (m_actions) {
258 // Add other built-in actions
259 for (const QString &id : m_actionIds) {
260 const QAction *action = m_actions->action(id);
261 if (action) {
262 addRow(action->icon().name(), KLocalizedString::removeAcceleratorMarker(action->text()), id, entryVisible(id));
263 }
264 }
265 }
266
267 m_sortModel->sort(Qt::DisplayRole);
268
269 m_initialized = true;
270 }
271 SettingsPageBase::showEvent(event);
272 }
273
274 void ContextMenuSettingsPage::loadServices()
275 {
276 const KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
277 const KConfigGroup showGroup = config.group(QStringLiteral("Show"));
278
279 // Load generic services
280 const auto locations = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("kio/servicemenus"), QStandardPaths::LocateDirectory);
281 QStringList files = KFileUtils::findAllUniqueFiles(locations);
282
283 for (const auto &file : std::as_const(files)) {
284 const QList<KServiceAction> serviceActions = KService(file).actions();
285
286 const KDesktopFile desktopFile(file);
287 const QString subMenuName = desktopFile.desktopGroup().readEntry("X-KDE-Submenu");
288
289 for (const KServiceAction &action : serviceActions) {
290 const QString serviceName = action.name();
291 const bool addService = !action.noDisplay() && !action.isSeparator() && !isInServicesList(serviceName);
292
293 if (addService) {
294 const QString itemName = subMenuName.isEmpty() ? action.text() : i18nc("@item:inmenu", "%1: %2", subMenuName, action.text());
295 const bool checked = showGroup.readEntry(serviceName, true);
296 addRow(action.icon(), itemName, serviceName, checked);
297 }
298 }
299 }
300
301 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
302 const auto jsonPlugins = KPluginMetaData::findPlugins(QStringLiteral("kf6/kfileitemaction"));
303
304 for (const auto &jsonMetadata : jsonPlugins) {
305 const QString desktopEntryName = jsonMetadata.pluginId();
306 if (!isInServicesList(desktopEntryName)) {
307 const bool checked = showGroup.readEntry(desktopEntryName, true);
308 addRow(jsonMetadata.iconName(), jsonMetadata.name(), desktopEntryName, checked);
309 }
310 }
311
312 m_sortModel->sort(Qt::DisplayRole);
313 m_searchLineEdit->setFocus(Qt::OtherFocusReason);
314 }
315
316 void ContextMenuSettingsPage::loadVersionControlSystems()
317 {
318 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
319
320 // Create a checkbox for each available version control plugin
321 QSet<QString> loadedPlugins;
322
323 const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("dolphin/vcs"));
324 for (const auto &plugin : plugins) {
325 const QString pluginName = plugin.name();
326 addRow(QStringLiteral("code-class"), pluginName, VersionControlServicePrefix + pluginName, enabledPlugins.contains(pluginName));
327 loadedPlugins += pluginName;
328 }
329
330 m_sortModel->sort(Qt::DisplayRole);
331 }
332
333 bool ContextMenuSettingsPage::isInServicesList(const QString &service) const
334 {
335 for (int i = 0; i < m_serviceModel->rowCount(); ++i) {
336 const QModelIndex index = m_serviceModel->index(i, 0);
337 if (m_serviceModel->data(index, ServiceModel::DesktopEntryNameRole).toString() == service) {
338 return true;
339 }
340 }
341 return false;
342 }
343
344 void ContextMenuSettingsPage::addRow(const QString &icon, const QString &text, const QString &value, bool checked)
345 {
346 m_serviceModel->insertRow(0);
347
348 const QModelIndex index = m_serviceModel->index(0, 0);
349 m_serviceModel->setData(index, icon, Qt::DecorationRole);
350 m_serviceModel->setData(index, text, Qt::DisplayRole);
351 m_serviceModel->setData(index, value, ServiceModel::DesktopEntryNameRole);
352 m_serviceModel->setData(index, checked ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
353 }
354
355 #include "moc_contextmenusettingspage.cpp"