2 * SPDX-FileCopyrightText: 2009-2010 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "contextmenusettingspage.h"
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"
16 #include <KDesktopFile>
17 #include <KDesktopFileActions>
19 #include <KLocalizedString>
20 #include <KMessageBox>
21 #include <KPluginMetaData>
23 #include <KServiceTypeTrader>
24 #include <kio_version.h>
25 #include <kiocore_export.h>
26 #include <kservice_export.h>
27 #include <kwidgetsaddons_version.h>
30 #include <KNSWidgets/Button>
32 #include <QGridLayout>
34 #include <QListWidget>
37 #include <QSortFilterProxyModel>
39 #include <QApplication>
43 const bool ShowDeleteDefault
= false;
44 const char VersionControlServicePrefix
[] = "_version_control_";
45 const char DeleteService
[] = "_delete";
46 const char CopyToMoveToService
[] ="_copy_to_move_to";
48 bool laterSelected
= false;
51 ContextMenuSettingsPage::ContextMenuSettingsPage(QWidget
* parent
,
52 const KActionCollection
* actions
,
53 const QStringList
& actionIds
) :
54 SettingsPageBase(parent
),
56 m_serviceModel(nullptr),
59 m_enabledVcsPlugins(),
61 m_actionIds(actionIds
)
63 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
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
);
75 m_listView
= new QListView(this);
76 QScroller::grabGesture(m_listView
->viewport(), QScroller::TouchGesture
);
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
);
91 topLayout
->addWidget(label
);
92 topLayout
->addWidget(m_searchLineEdit
);
93 topLayout
->addWidget(m_listView
);
96 using NewStuffButton
= KNSWidgets::Button
;
97 auto *downloadButton
= new NewStuffButton(i18nc("@action:button", "Download New Services..."),
98 QStringLiteral("servicemenu.knsrc"),
100 connect(downloadButton
, &NewStuffButton::dialogFinished
, this, [this](const auto &changedEntries
) {
101 if (!changedEntries
.isEmpty()) {
102 m_serviceModel
->clear();
106 topLayout
->addWidget(downloadButton
);
109 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
110 std::sort(m_enabledVcsPlugins
.begin(), m_enabledVcsPlugins
.end());
113 ContextMenuSettingsPage::~ContextMenuSettingsPage() {
116 bool ContextMenuSettingsPage::entryVisible(const QString
& id
)
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();
138 void ContextMenuSettingsPage::setEntryVisible(const QString
& id
, bool visible
)
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
);
159 void ContextMenuSettingsPage::applySettings()
161 if (!m_initialized
) {
165 KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
166 KConfigGroup showGroup
= config
.group("Show");
168 QStringList enabledPlugins
;
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();
176 if (service
.startsWith(VersionControlServicePrefix
)) {
178 enabledPlugins
.append(model
->data(index
, Qt::DisplayRole
).toString());
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
);
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();
192 showGroup
.writeEntry(service
, checked
);
198 if (m_enabledVcsPlugins
!= enabledPlugins
) {
199 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
200 VersionControlSettings::self()->save();
202 if (!laterSelected
) {
203 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
204 KMessageBox::ButtonCode promptRestart
= KMessageBox::questionTwoActions(window(),
206 KMessageBox::ButtonCode promptRestart
= KMessageBox::questionYesNo(window(),
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"))
214 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
215 if (promptRestart
== KMessageBox::ButtonCode::PrimaryAction
) {
217 if (promptRestart
== KMessageBox::ButtonCode::Yes
) {
219 Dolphin::openNewWindow();
222 laterSelected
= true;
228 void ContextMenuSettingsPage::restoreDefaults()
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();
235 const bool checked
= !service
.startsWith(VersionControlServicePrefix
)
236 && service
!= QLatin1String(DeleteService
)
237 && service
!= QLatin1String(CopyToMoveToService
);
238 model
->setData(index
, checked
, Qt::CheckStateRole
);
242 void ContextMenuSettingsPage::showEvent(QShowEvent
* event
)
244 if (!event
->spontaneous() && !m_initialized
) {
247 loadVersionControlSystems();
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"),
255 configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
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"),
261 ContextMenuSettings::showCopyMoveMenu());
264 // Add other built-in actions
265 for (const QString
& id
: m_actionIds
) {
266 const QAction
* action
= m_actions
->action(id
);
268 addRow(action
->icon().name(), action
->text(), id
, entryVisible(id
));
273 m_sortModel
->sort(Qt::DisplayRole
);
275 m_initialized
= true;
277 SettingsPageBase::showEvent(event
);
280 void ContextMenuSettingsPage::loadServices()
282 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
283 const KConfigGroup showGroup
= config
.group("Show");
285 // Load generic services
286 const auto locations
= QStandardPaths::locateAll(QStandardPaths::GenericDataLocation
, QStringLiteral("kio/servicemenus"), QStandardPaths::LocateDirectory
);
287 QStringList files
= KFileUtils::findAllUniqueFiles(locations
);
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());
296 for (const auto &file
: qAsConst(files
)) {
297 const QList
<KServiceAction
> serviceActions
= KDesktopFileActions::userDefinedServices(KService(file
), true);
299 const KDesktopFile
desktopFile(file
);
300 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
302 for (const KServiceAction
&action
: serviceActions
) {
303 const QString serviceName
= action
.name();
304 const bool addService
= !action
.noDisplay() && !action
.isSeparator() && !isInServicesList(serviceName
);
307 const QString itemName
= subMenuName
.isEmpty()
309 : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
310 const bool checked
= showGroup
.readEntry(serviceName
, true);
311 addRow(action
.icon(), itemName
, serviceName
, checked
);
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
);
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"));
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
);
339 m_sortModel
->sort(Qt::DisplayRole
);
340 m_searchLineEdit
->setFocus(Qt::OtherFocusReason
);
343 void ContextMenuSettingsPage::loadVersionControlSystems()
345 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
347 // Create a checkbox for each available version control plugin
348 QSet
<QString
> loadedPlugins
;
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"),
355 VersionControlServicePrefix
+ pluginName
,
356 enabledPlugins
.contains(pluginName
));
357 loadedPlugins
+= pluginName
;
360 m_sortModel
->sort(Qt::DisplayRole
);
363 bool ContextMenuSettingsPage::isInServicesList(const QString
&service
) const
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
) {
374 void ContextMenuSettingsPage::addRow(const QString
&icon
,
376 const QString
&value
,
379 m_serviceModel
->insertRow(0);
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
);