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