]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/contextmenu/contextmenusettingspage.cpp
Fix building KF6 with PackageKit-Qt
[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
28 #include <QtGlobal>
29 #include <knewstuff_version.h>
30 #if KNEWSTUFF_VERSION >= QT_VERSION_CHECK(5, 91, 0)
31 #include <KNSWidgets/Button>
32 #else
33 #include <KNS3/Button>
34 #endif
35
36 #include <QGridLayout>
37 #include <QLabel>
38 #include <QListWidget>
39 #include <QScroller>
40 #include <QShowEvent>
41 #include <QSortFilterProxyModel>
42 #include <QLineEdit>
43 #include <QApplication>
44
45 namespace
46 {
47 const bool ShowDeleteDefault = false;
48 const char VersionControlServicePrefix[] = "_version_control_";
49 const char DeleteService[] = "_delete";
50 const char CopyToMoveToService[] ="_copy_to_move_to";
51
52 bool laterSelected = false;
53 }
54
55 ContextMenuSettingsPage::ContextMenuSettingsPage(QWidget* parent,
56 const KActionCollection* actions,
57 const QStringList& actionIds) :
58 SettingsPageBase(parent),
59 m_initialized(false),
60 m_serviceModel(nullptr),
61 m_sortModel(nullptr),
62 m_listView(nullptr),
63 m_enabledVcsPlugins(),
64 m_actions(actions),
65 m_actionIds(actionIds)
66 {
67 QVBoxLayout* topLayout = new QVBoxLayout(this);
68
69 QLabel* label = new QLabel(i18nc("@label:textbox",
70 "Select which services should "
71 "be shown in the context menu:"), this);
72 label->setWordWrap(true);
73 m_searchLineEdit = new QLineEdit(this);
74 m_searchLineEdit->setPlaceholderText(i18nc("@label:textbox", "Search..."));
75 connect(m_searchLineEdit, &QLineEdit::textChanged, this, [this](const QString &filter){
76 m_sortModel->setFilterFixedString(filter);
77 });
78
79 m_listView = new QListView(this);
80 QScroller::grabGesture(m_listView->viewport(), QScroller::TouchGesture);
81
82 auto *delegate = new ServiceItemDelegate(m_listView, m_listView);
83 m_serviceModel = new ServiceModel(this);
84 m_sortModel = new QSortFilterProxyModel(this);
85 m_sortModel->setSourceModel(m_serviceModel);
86 m_sortModel->setSortRole(Qt::DisplayRole);
87 m_sortModel->setSortLocaleAware(true);
88 m_sortModel->setFilterRole(Qt::DisplayRole);
89 m_sortModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
90 m_listView->setModel(m_sortModel);
91 m_listView->setItemDelegate(delegate);
92 m_listView->setVerticalScrollMode(QListView::ScrollPerPixel);
93 connect(m_listView, &QListView::clicked, this, &ContextMenuSettingsPage::changed);
94
95 topLayout->addWidget(label);
96 topLayout->addWidget(m_searchLineEdit);
97 topLayout->addWidget(m_listView);
98
99 #ifndef Q_OS_WIN
100 #if KNEWSTUFF_VERSION >= QT_VERSION_CHECK(5, 91, 0)
101 using NewStuffButton = KNSWidgets::Button;
102 #else
103 using NewStuffButton = KNS3::Button;
104 #endif // KNEWSTUFF_VERSION
105 auto *downloadButton = new NewStuffButton(i18nc("@action:button", "Download New Services..."),
106 QStringLiteral("servicemenu.knsrc"),
107 this);
108 connect(downloadButton, &NewStuffButton::dialogFinished, this, [this](const auto &changedEntries) {
109 if (!changedEntries.isEmpty()) {
110 m_serviceModel->clear();
111 loadServices();
112 }
113 });
114 topLayout->addWidget(downloadButton);
115 #endif // Q_OS_WIN
116
117 m_enabledVcsPlugins = VersionControlSettings::enabledPlugins();
118 std::sort(m_enabledVcsPlugins.begin(), m_enabledVcsPlugins.end());
119 }
120
121 ContextMenuSettingsPage::~ContextMenuSettingsPage() {
122 }
123
124 bool ContextMenuSettingsPage::entryVisible(const QString& id)
125 {
126 if (id == "add_to_places") {
127 return ContextMenuSettings::showAddToPlaces();
128 } else if (id == "sort") {
129 return ContextMenuSettings::showSortBy();
130 } else if (id == "view_mode") {
131 return ContextMenuSettings::showViewMode();
132 } else if (id == "open_in_new_tab") {
133 return ContextMenuSettings::showOpenInNewTab();
134 } else if (id == "open_in_new_window") {
135 return ContextMenuSettings::showOpenInNewWindow();
136 } else if (id == "copy_location") {
137 return ContextMenuSettings::showCopyLocation();
138 } else if (id == "duplicate") {
139 return ContextMenuSettings::showDuplicateHere();
140 } else if (id == "open_terminal_here") {
141 return ContextMenuSettings::showOpenTerminal();
142 }
143 return false;
144 }
145
146 void ContextMenuSettingsPage::setEntryVisible(const QString& id, bool visible)
147 {
148 if (id == "add_to_places") {
149 ContextMenuSettings::setShowAddToPlaces(visible);
150 } else if (id == "sort") {
151 ContextMenuSettings::setShowSortBy(visible);
152 } else if (id == "view_mode") {
153 ContextMenuSettings::setShowViewMode(visible);
154 } else if (id == "open_in_new_tab") {
155 ContextMenuSettings::setShowOpenInNewTab(visible);
156 } else if (id == "open_in_new_window") {
157 ContextMenuSettings::setShowOpenInNewWindow(visible);
158 } else if (id == "copy_location") {
159 ContextMenuSettings::setShowCopyLocation(visible);
160 } else if (id == "duplicate") {
161 ContextMenuSettings::setShowDuplicateHere(visible);
162 } else if (id == "open_terminal_here") {
163 ContextMenuSettings::setShowOpenTerminal(visible);
164 }
165 }
166
167 void ContextMenuSettingsPage::applySettings()
168 {
169 if (!m_initialized) {
170 return;
171 }
172
173 KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
174 KConfigGroup showGroup = config.group("Show");
175
176 QStringList enabledPlugins;
177
178 const QAbstractItemModel *model = m_listView->model();
179 for (int i = 0; i < model->rowCount(); ++i) {
180 const QModelIndex index = model->index(i, 0);
181 const QString service = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
182 const bool checked = model->data(index, Qt::CheckStateRole).toBool();
183
184 if (service.startsWith(VersionControlServicePrefix)) {
185 if (checked) {
186 enabledPlugins.append(model->data(index, Qt::DisplayRole).toString());
187 }
188 } else if (service == QLatin1String(DeleteService)) {
189 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals);
190 KConfigGroup configGroup(globalConfig, "KDE");
191 configGroup.writeEntry("ShowDeleteCommand", checked);
192 configGroup.sync();
193 } else if (service == QLatin1String(CopyToMoveToService)) {
194 ContextMenuSettings::setShowCopyMoveMenu(checked);
195 ContextMenuSettings::self()->save();
196 } else if (m_actionIds.contains(service)) {
197 setEntryVisible(service, checked);
198 ContextMenuSettings::self()->save();
199 } else {
200 showGroup.writeEntry(service, checked);
201 }
202 }
203
204 showGroup.sync();
205
206 if (m_enabledVcsPlugins != enabledPlugins) {
207 VersionControlSettings::setEnabledPlugins(enabledPlugins);
208 VersionControlSettings::self()->save();
209
210 if (!laterSelected) {
211 KMessageBox::ButtonCode promptRestart = KMessageBox::questionYesNo(window(),
212 i18nc("@info", "Dolphin must be restarted to apply the "
213 "updated version control system settings."),
214 i18nc("@info", "Restart now?"),
215 KGuiItem(QApplication::translate("KStandardGuiItem", "&Restart"), QStringLiteral("dialog-restart")),
216 KGuiItem(QApplication::translate("KStandardGuiItem", "&Later"), QStringLiteral("dialog-later"))
217 );
218 if (promptRestart == KMessageBox::ButtonCode::Yes) {
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("kf5/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 }