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