]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/contextmenu/contextmenusettingspage.cpp
Merge branch 'release/21.12'
[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 #include "global.h"
15
16 #include <KDesktopFile>
17 #include <KDesktopFileActions>
18 #include <KFileUtils>
19 #include <KLocalizedString>
20 #include <KMessageBox>
21 #include <KNS3/Button>
22 #include <KPluginMetaData>
23 #include <KService>
24 #include <KServiceTypeTrader>
25
26 #include <kio_version.h>
27
28 #include <QGridLayout>
29 #include <QLabel>
30 #include <QListWidget>
31 #include <QScroller>
32 #include <QShowEvent>
33 #include <QSortFilterProxyModel>
34 #include <QLineEdit>
35 #include <QApplication>
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,
48 const KActionCollection* actions,
49 const QStringList& actionIds) :
50 SettingsPageBase(parent),
51 m_initialized(false),
52 m_serviceModel(nullptr),
53 m_sortModel(nullptr),
54 m_listView(nullptr),
55 m_enabledVcsPlugins(),
56 m_actions(actions),
57 m_actionIds(actionIds)
58 {
59 QVBoxLayout* topLayout = new QVBoxLayout(this);
60
61 QLabel* label = new QLabel(i18nc("@label:textbox",
62 "Select which services should "
63 "be shown in the context menu:"), this);
64 label->setWordWrap(true);
65 m_searchLineEdit = new QLineEdit(this);
66 m_searchLineEdit->setPlaceholderText(i18nc("@label:textbox", "Search..."));
67 connect(m_searchLineEdit, &QLineEdit::textChanged, this, [this](const QString &filter){
68 m_sortModel->setFilterFixedString(filter);
69 });
70
71 m_listView = new QListView(this);
72 QScroller::grabGesture(m_listView->viewport(), QScroller::TouchGesture);
73
74 auto *delegate = new ServiceItemDelegate(m_listView, m_listView);
75 m_serviceModel = new ServiceModel(this);
76 m_sortModel = new QSortFilterProxyModel(this);
77 m_sortModel->setSourceModel(m_serviceModel);
78 m_sortModel->setSortRole(Qt::DisplayRole);
79 m_sortModel->setSortLocaleAware(true);
80 m_sortModel->setFilterRole(Qt::DisplayRole);
81 m_sortModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
82 m_listView->setModel(m_sortModel);
83 m_listView->setItemDelegate(delegate);
84 m_listView->setVerticalScrollMode(QListView::ScrollPerPixel);
85 connect(m_listView, &QListView::clicked, this, &ContextMenuSettingsPage::changed);
86
87 #ifndef Q_OS_WIN
88 auto *downloadButton = new KNS3::Button(i18nc("@action:button", "Download New Services..."),
89 QStringLiteral("servicemenu.knsrc"),
90 this);
91 connect(downloadButton, &KNS3::Button::dialogFinished, this, [this](const KNS3::Entry::List &changedEntries) {
92 if (!changedEntries.isEmpty()) {
93 m_serviceModel->clear();
94 loadServices();
95 }
96 });
97
98 #endif
99
100 topLayout->addWidget(label);
101 topLayout->addWidget(m_searchLineEdit);
102 topLayout->addWidget(m_listView);
103 #ifndef Q_OS_WIN
104 topLayout->addWidget(downloadButton);
105 #endif
106
107 m_enabledVcsPlugins = VersionControlSettings::enabledPlugins();
108 std::sort(m_enabledVcsPlugins.begin(), m_enabledVcsPlugins.end());
109 }
110
111 ContextMenuSettingsPage::~ContextMenuSettingsPage() {
112 }
113
114 bool ContextMenuSettingsPage::entryVisible(const QString& id)
115 {
116 if (id == "add_to_places") {
117 return ContextMenuSettings::showAddToPlaces();
118 } else if (id == "sort") {
119 return ContextMenuSettings::showSortBy();
120 } else if (id == "view_mode") {
121 return ContextMenuSettings::showViewMode();
122 } else if (id == "open_in_new_tab") {
123 return ContextMenuSettings::showOpenInNewTab();
124 } else if (id == "open_in_new_window") {
125 return ContextMenuSettings::showOpenInNewWindow();
126 } else if (id == "copy_location") {
127 return ContextMenuSettings::showCopyLocation();
128 } else if (id == "duplicate") {
129 return ContextMenuSettings::showDuplicateHere();
130 } else if (id == "open_terminal") {
131 return ContextMenuSettings::showOpenTerminal();
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 == "copy_location") {
149 ContextMenuSettings::setShowCopyLocation(visible);
150 } else if (id == "duplicate") {
151 ContextMenuSettings::setShowDuplicateHere(visible);
152 } else if (id == "open_terminal") {
153 ContextMenuSettings::setShowOpenTerminal(visible);
154 }
155 }
156
157 void ContextMenuSettingsPage::applySettings()
158 {
159 if (!m_initialized) {
160 return;
161 }
162
163 KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
164 KConfigGroup showGroup = config.group("Show");
165
166 QStringList enabledPlugins;
167
168 const QAbstractItemModel *model = m_listView->model();
169 for (int i = 0; i < model->rowCount(); ++i) {
170 const QModelIndex index = model->index(i, 0);
171 const QString service = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
172 const bool checked = model->data(index, Qt::CheckStateRole).toBool();
173
174 if (service.startsWith(VersionControlServicePrefix)) {
175 if (checked) {
176 enabledPlugins.append(model->data(index, Qt::DisplayRole).toString());
177 }
178 } else if (service == QLatin1String(DeleteService)) {
179 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals);
180 KConfigGroup configGroup(globalConfig, "KDE");
181 configGroup.writeEntry("ShowDeleteCommand", checked);
182 configGroup.sync();
183 } else if (service == QLatin1String(CopyToMoveToService)) {
184 ContextMenuSettings::setShowCopyMoveMenu(checked);
185 ContextMenuSettings::self()->save();
186 } else if (m_actionIds.contains(service)) {
187 setEntryVisible(service, checked);
188 ContextMenuSettings::self()->save();
189 } else {
190 showGroup.writeEntry(service, checked);
191 }
192 }
193
194 showGroup.sync();
195
196 if (m_enabledVcsPlugins != enabledPlugins) {
197 VersionControlSettings::setEnabledPlugins(enabledPlugins);
198 VersionControlSettings::self()->save();
199
200 if (!laterSelected) {
201 KMessageBox::ButtonCode promptRestart = KMessageBox::questionYesNo(window(),
202 i18nc("@info", "Dolphin must be restarted to apply the "
203 "updated version control system settings."),
204 i18nc("@info", "Restart now?"),
205 KGuiItem(QApplication::translate("KStandardGuiItem", "&Restart"), QStringLiteral("dialog-restart")),
206 KGuiItem(QApplication::translate("KStandardGuiItem", "&Later"), QStringLiteral("dialog-later"))
207 );
208 if (promptRestart == KMessageBox::ButtonCode::Yes) {
209 Dolphin::openNewWindow();
210 qApp->quit();
211 } else {
212 laterSelected = true;
213 }
214 }
215 }
216 }
217
218 void ContextMenuSettingsPage::restoreDefaults()
219 {
220 QAbstractItemModel* model = m_listView->model();
221 for (int i = 0; i < model->rowCount(); ++i) {
222 const QModelIndex index = model->index(i, 0);
223 const QString service = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
224
225 const bool checked = !service.startsWith(VersionControlServicePrefix)
226 && service != QLatin1String(DeleteService)
227 && service != QLatin1String(CopyToMoveToService);
228 model->setData(index, checked, Qt::CheckStateRole);
229 }
230 }
231
232 void ContextMenuSettingsPage::showEvent(QShowEvent* event)
233 {
234 if (!event->spontaneous() && !m_initialized) {
235 loadServices();
236
237 loadVersionControlSystems();
238
239 // Add "Show 'Delete' command" as service
240 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals);
241 KConfigGroup configGroup(globalConfig, "KDE");
242 addRow(QStringLiteral("edit-delete"),
243 i18nc("@option:check", "Delete"),
244 DeleteService,
245 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(), 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 const KService::List services = KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
279 for (const KService::Ptr &service : services) {
280 files << QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kservices5/" % service->entryPath());
281 }
282 for (const auto &file : qAsConst(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()
294 ? action.text()
295 : i18nc("@item:inmenu", "%1: %2", subMenuName, action.text());
296 const bool checked = showGroup.readEntry(serviceName, true);
297 addRow(action.icon(), itemName, serviceName, checked);
298 }
299 }
300 }
301
302 // Load service plugins, this is deprecated in KIO 5.82
303 #if KIO_VERSION < QT_VERSION_CHECK(6, 0, 0)
304 const KService::List pluginServices = KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
305 for (const KService::Ptr &service : pluginServices) {
306 const QString desktopEntryName = service->desktopEntryName();
307 if (!isInServicesList(desktopEntryName)) {
308 const bool checked = showGroup.readEntry(desktopEntryName, true);
309 addRow(service->icon(), service->name(), desktopEntryName, checked);
310 }
311 }
312 #endif
313
314 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
315 const auto jsonPlugins = KPluginMetaData::findPlugins(QStringLiteral("kf5/kfileitemaction"));
316
317 for (const auto &jsonMetadata : jsonPlugins) {
318 const QString desktopEntryName = jsonMetadata.pluginId();
319 if (!isInServicesList(desktopEntryName)) {
320 const bool checked = showGroup.readEntry(desktopEntryName, true);
321 addRow(jsonMetadata.iconName(), jsonMetadata.name(), desktopEntryName, checked);
322 }
323 }
324
325 m_sortModel->sort(Qt::DisplayRole);
326 m_searchLineEdit->setFocus(Qt::OtherFocusReason);
327 }
328
329 void ContextMenuSettingsPage::loadVersionControlSystems()
330 {
331 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
332
333 // Create a checkbox for each available version control plugin
334 QSet<QString> loadedPlugins;
335
336 const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("dolphin/vcs"));
337 for (const auto &plugin : plugins) {
338 const QString pluginName = plugin.name();
339 addRow(QStringLiteral("code-class"),
340 pluginName,
341 VersionControlServicePrefix + pluginName,
342 enabledPlugins.contains(pluginName));
343 loadedPlugins += pluginName;
344 }
345
346 m_sortModel->sort(Qt::DisplayRole);
347 }
348
349 bool ContextMenuSettingsPage::isInServicesList(const QString &service) const
350 {
351 for (int i = 0; i < m_serviceModel->rowCount(); ++i) {
352 const QModelIndex index = m_serviceModel->index(i, 0);
353 if (m_serviceModel->data(index, ServiceModel::DesktopEntryNameRole).toString() == service) {
354 return true;
355 }
356 }
357 return false;
358 }
359
360 void ContextMenuSettingsPage::addRow(const QString &icon,
361 const QString &text,
362 const QString &value,
363 bool checked)
364 {
365 m_serviceModel->insertRow(0);
366
367 const QModelIndex index = m_serviceModel->index(0, 0);
368 m_serviceModel->setData(index, icon, Qt::DecorationRole);
369 m_serviceModel->setData(index, text, Qt::DisplayRole);
370 m_serviceModel->setData(index, value, ServiceModel::DesktopEntryNameRole);
371 m_serviceModel->setData(index, checked, Qt::CheckStateRole);
372 }