]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/contextmenu/contextmenusettingspage.cpp
2d6c9279afdfb3b1b3d2c12c432f4e51d4e05c36
[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 topLayout->addWidget(label);
88 topLayout->addWidget(m_searchLineEdit);
89 topLayout->addWidget(m_listView);
90
91 #ifndef Q_OS_WIN
92 auto *downloadButton = new KNS3::Button(i18nc("@action:button", "Download New Services..."),
93 QStringLiteral("servicemenu.knsrc"),
94 this);
95 connect(downloadButton, &KNS3::Button::dialogFinished, this, [this](const KNS3::Entry::List &changedEntries) {
96 if (!changedEntries.isEmpty()) {
97 m_serviceModel->clear();
98 loadServices();
99 }
100 });
101 topLayout->addWidget(downloadButton);
102 #endif
103
104 m_enabledVcsPlugins = VersionControlSettings::enabledPlugins();
105 std::sort(m_enabledVcsPlugins.begin(), m_enabledVcsPlugins.end());
106 }
107
108 ContextMenuSettingsPage::~ContextMenuSettingsPage() {
109 }
110
111 bool ContextMenuSettingsPage::entryVisible(const QString& id)
112 {
113 if (id == "add_to_places") {
114 return ContextMenuSettings::showAddToPlaces();
115 } else if (id == "sort") {
116 return ContextMenuSettings::showSortBy();
117 } else if (id == "view_mode") {
118 return ContextMenuSettings::showViewMode();
119 } else if (id == "open_in_new_tab") {
120 return ContextMenuSettings::showOpenInNewTab();
121 } else if (id == "open_in_new_window") {
122 return ContextMenuSettings::showOpenInNewWindow();
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 }
130 return false;
131 }
132
133 void ContextMenuSettingsPage::setEntryVisible(const QString& id, bool visible)
134 {
135 if (id == "add_to_places") {
136 ContextMenuSettings::setShowAddToPlaces(visible);
137 } else if (id == "sort") {
138 ContextMenuSettings::setShowSortBy(visible);
139 } else if (id == "view_mode") {
140 ContextMenuSettings::setShowViewMode(visible);
141 } else if (id == "open_in_new_tab") {
142 ContextMenuSettings::setShowOpenInNewTab(visible);
143 } else if (id == "open_in_new_window") {
144 ContextMenuSettings::setShowOpenInNewWindow(visible);
145 } else if (id == "copy_location") {
146 ContextMenuSettings::setShowCopyLocation(visible);
147 } else if (id == "duplicate") {
148 ContextMenuSettings::setShowDuplicateHere(visible);
149 } else if (id == "open_terminal_here") {
150 ContextMenuSettings::setShowOpenTerminal(visible);
151 }
152 }
153
154 void ContextMenuSettingsPage::applySettings()
155 {
156 if (!m_initialized) {
157 return;
158 }
159
160 KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
161 KConfigGroup showGroup = config.group("Show");
162
163 QStringList enabledPlugins;
164
165 const QAbstractItemModel *model = m_listView->model();
166 for (int i = 0; i < model->rowCount(); ++i) {
167 const QModelIndex index = model->index(i, 0);
168 const QString service = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
169 const bool checked = model->data(index, Qt::CheckStateRole).toBool();
170
171 if (service.startsWith(VersionControlServicePrefix)) {
172 if (checked) {
173 enabledPlugins.append(model->data(index, Qt::DisplayRole).toString());
174 }
175 } else if (service == QLatin1String(DeleteService)) {
176 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals);
177 KConfigGroup configGroup(globalConfig, "KDE");
178 configGroup.writeEntry("ShowDeleteCommand", checked);
179 configGroup.sync();
180 } else if (service == QLatin1String(CopyToMoveToService)) {
181 ContextMenuSettings::setShowCopyMoveMenu(checked);
182 ContextMenuSettings::self()->save();
183 } else if (m_actionIds.contains(service)) {
184 setEntryVisible(service, checked);
185 ContextMenuSettings::self()->save();
186 } else {
187 showGroup.writeEntry(service, checked);
188 }
189 }
190
191 showGroup.sync();
192
193 if (m_enabledVcsPlugins != enabledPlugins) {
194 VersionControlSettings::setEnabledPlugins(enabledPlugins);
195 VersionControlSettings::self()->save();
196
197 if (!laterSelected) {
198 KMessageBox::ButtonCode promptRestart = KMessageBox::questionYesNo(window(),
199 i18nc("@info", "Dolphin must be restarted to apply the "
200 "updated version control system settings."),
201 i18nc("@info", "Restart now?"),
202 KGuiItem(QApplication::translate("KStandardGuiItem", "&Restart"), QStringLiteral("dialog-restart")),
203 KGuiItem(QApplication::translate("KStandardGuiItem", "&Later"), QStringLiteral("dialog-later"))
204 );
205 if (promptRestart == KMessageBox::ButtonCode::Yes) {
206 Dolphin::openNewWindow();
207 qApp->quit();
208 } else {
209 laterSelected = true;
210 }
211 }
212 }
213 }
214
215 void ContextMenuSettingsPage::restoreDefaults()
216 {
217 QAbstractItemModel* model = m_listView->model();
218 for (int i = 0; i < model->rowCount(); ++i) {
219 const QModelIndex index = model->index(i, 0);
220 const QString service = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
221
222 const bool checked = !service.startsWith(VersionControlServicePrefix)
223 && service != QLatin1String(DeleteService)
224 && service != QLatin1String(CopyToMoveToService);
225 model->setData(index, checked, Qt::CheckStateRole);
226 }
227 }
228
229 void ContextMenuSettingsPage::showEvent(QShowEvent* event)
230 {
231 if (!event->spontaneous() && !m_initialized) {
232 loadServices();
233
234 loadVersionControlSystems();
235
236 // Add "Show 'Delete' command" as service
237 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals);
238 KConfigGroup configGroup(globalConfig, "KDE");
239 addRow(QStringLiteral("edit-delete"),
240 i18nc("@option:check", "Delete"),
241 DeleteService,
242 configGroup.readEntry("ShowDeleteCommand", ShowDeleteDefault));
243
244 // Add "Show 'Copy To' and 'Move To' commands" as service
245 addRow(QStringLiteral("edit-copy"),
246 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
247 CopyToMoveToService,
248 ContextMenuSettings::showCopyMoveMenu());
249
250 if (m_actions){
251 // Add other built-in actions
252 for (const QString& id : m_actionIds) {
253 const QAction* action = m_actions->action(id);
254 if (action) {
255 addRow(action->icon().name(), action->text(), id, entryVisible(id));
256 }
257 }
258 }
259
260 m_sortModel->sort(Qt::DisplayRole);
261
262 m_initialized = true;
263 }
264 SettingsPageBase::showEvent(event);
265 }
266
267 void ContextMenuSettingsPage::loadServices()
268 {
269 const KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
270 const KConfigGroup showGroup = config.group("Show");
271
272 // Load generic services
273 const auto locations = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("kio/servicemenus"), QStandardPaths::LocateDirectory);
274 QStringList files = KFileUtils::findAllUniqueFiles(locations);
275 const KService::List services = KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
276 for (const KService::Ptr &service : services) {
277 files << QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kservices5/" % service->entryPath());
278 }
279 for (const auto &file : qAsConst(files)) {
280 const QList<KServiceAction> serviceActions = KDesktopFileActions::userDefinedServices(KService(file), true);
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()
291 ? action.text()
292 : i18nc("@item:inmenu", "%1: %2", subMenuName, action.text());
293 const bool checked = showGroup.readEntry(serviceName, true);
294 addRow(action.icon(), itemName, serviceName, checked);
295 }
296 }
297 }
298
299 // Load service plugins, this is deprecated in KIO 5.82
300 #if KIO_VERSION < QT_VERSION_CHECK(6, 0, 0)
301 const KService::List pluginServices = KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
302 for (const KService::Ptr &service : pluginServices) {
303 const QString desktopEntryName = service->desktopEntryName();
304 if (!isInServicesList(desktopEntryName)) {
305 const bool checked = showGroup.readEntry(desktopEntryName, true);
306 addRow(service->icon(), service->name(), desktopEntryName, checked);
307 }
308 }
309 #endif
310
311 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
312 const auto jsonPlugins = KPluginMetaData::findPlugins(QStringLiteral("kf5/kfileitemaction"));
313
314 for (const auto &jsonMetadata : jsonPlugins) {
315 const QString desktopEntryName = jsonMetadata.pluginId();
316 if (!isInServicesList(desktopEntryName)) {
317 const bool checked = showGroup.readEntry(desktopEntryName, true);
318 addRow(jsonMetadata.iconName(), jsonMetadata.name(), desktopEntryName, checked);
319 }
320 }
321
322 m_sortModel->sort(Qt::DisplayRole);
323 m_searchLineEdit->setFocus(Qt::OtherFocusReason);
324 }
325
326 void ContextMenuSettingsPage::loadVersionControlSystems()
327 {
328 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
329
330 // Create a checkbox for each available version control plugin
331 QSet<QString> loadedPlugins;
332
333 const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("dolphin/vcs"));
334 for (const auto &plugin : plugins) {
335 const QString pluginName = plugin.name();
336 addRow(QStringLiteral("code-class"),
337 pluginName,
338 VersionControlServicePrefix + pluginName,
339 enabledPlugins.contains(pluginName));
340 loadedPlugins += pluginName;
341 }
342
343 m_sortModel->sort(Qt::DisplayRole);
344 }
345
346 bool ContextMenuSettingsPage::isInServicesList(const QString &service) const
347 {
348 for (int i = 0; i < m_serviceModel->rowCount(); ++i) {
349 const QModelIndex index = m_serviceModel->index(i, 0);
350 if (m_serviceModel->data(index, ServiceModel::DesktopEntryNameRole).toString() == service) {
351 return true;
352 }
353 }
354 return false;
355 }
356
357 void ContextMenuSettingsPage::addRow(const QString &icon,
358 const QString &text,
359 const QString &value,
360 bool checked)
361 {
362 m_serviceModel->insertRow(0);
363
364 const QModelIndex index = m_serviceModel->index(0, 0);
365 m_serviceModel->setData(index, icon, Qt::DecorationRole);
366 m_serviceModel->setData(index, text, Qt::DisplayRole);
367 m_serviceModel->setData(index, value, ServiceModel::DesktopEntryNameRole);
368 m_serviceModel->setData(index, checked, Qt::CheckStateRole);
369 }