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