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