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