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