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