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_contextmenusettings.h"
10 #include "dolphin_generalsettings.h"
11 #include "dolphin_versioncontrolsettings.h"
13 #include "settings/serviceitemdelegate.h"
14 #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>
29 #include <KNSWidgets/Button>
32 #include <QApplication>
33 #include <QGridLayout>
36 #include <QListWidget>
39 #include <QSortFilterProxyModel>
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
, const KActionCollection
*actions
, const QStringList
&actionIds
)
52 : SettingsPageBase(parent
)
53 , m_initialized(false)
54 , m_serviceModel(nullptr)
55 , m_sortModel(nullptr)
57 , m_enabledVcsPlugins()
59 , m_actionIds(actionIds
)
61 QVBoxLayout
*topLayout
= new QVBoxLayout(this);
63 QLabel
*label
= new QLabel(i18nc("@label:textbox",
64 "Select which services should "
65 "be shown in the context menu:"),
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
);
74 m_listView
= new QListView(this);
75 QScroller::grabGesture(m_listView
->viewport(), QScroller::TouchGesture
);
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
);
90 topLayout
->addWidget(label
);
91 topLayout
->addWidget(m_searchLineEdit
);
92 topLayout
->addWidget(m_listView
);
95 using NewStuffButton
= KNSWidgets::Button
;
96 auto *downloadButton
= new NewStuffButton(i18nc("@action:button", "Download New Services..."), QStringLiteral("servicemenu.knsrc"), this);
97 connect(downloadButton
, &NewStuffButton::dialogFinished
, this, [this](const auto &changedEntries
) {
98 if (!changedEntries
.isEmpty()) {
99 m_serviceModel
->clear();
103 topLayout
->addWidget(downloadButton
);
106 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
107 std::sort(m_enabledVcsPlugins
.begin(), m_enabledVcsPlugins
.end());
110 ContextMenuSettingsPage::~ContextMenuSettingsPage()
114 bool ContextMenuSettingsPage::entryVisible(const QString
&id
)
116 if (id
== "add_to_places") {
117 return ContextMenuSettings::showAddToPlaces();
118 } else if (id
== "sort") {
119 return ContextMenuSettings::showSortBy();
120 } else if (id
== "view_mode") {
121 return ContextMenuSettings::showViewMode();
122 } else if (id
== "open_in_new_tab") {
123 return ContextMenuSettings::showOpenInNewTab();
124 } else if (id
== "open_in_new_window") {
125 return ContextMenuSettings::showOpenInNewWindow();
126 } else if (id
== "copy_location") {
127 return ContextMenuSettings::showCopyLocation();
128 } else if (id
== "duplicate") {
129 return ContextMenuSettings::showDuplicateHere();
130 } else if (id
== "open_terminal_here") {
131 return ContextMenuSettings::showOpenTerminal();
132 } else if (id
== "copy_to_inactive_split_view") {
133 return ContextMenuSettings::showCopyToOtherSplitView();
134 } else if (id
== "move_to_inactive_split_view") {
135 return ContextMenuSettings::showMoveToOtherSplitView();
140 void ContextMenuSettingsPage::setEntryVisible(const QString
&id
, bool visible
)
142 if (id
== "add_to_places") {
143 ContextMenuSettings::setShowAddToPlaces(visible
);
144 } else if (id
== "sort") {
145 ContextMenuSettings::setShowSortBy(visible
);
146 } else if (id
== "view_mode") {
147 ContextMenuSettings::setShowViewMode(visible
);
148 } else if (id
== "open_in_new_tab") {
149 ContextMenuSettings::setShowOpenInNewTab(visible
);
150 } else if (id
== "open_in_new_window") {
151 ContextMenuSettings::setShowOpenInNewWindow(visible
);
152 } else if (id
== "copy_location") {
153 ContextMenuSettings::setShowCopyLocation(visible
);
154 } else if (id
== "duplicate") {
155 ContextMenuSettings::setShowDuplicateHere(visible
);
156 } else if (id
== "open_terminal_here") {
157 ContextMenuSettings::setShowOpenTerminal(visible
);
158 } else if (id
== "copy_to_inactive_split_view") {
159 ContextMenuSettings::setShowCopyToOtherSplitView(visible
);
160 } else if (id
== "move_to_inactive_split_view") {
161 ContextMenuSettings::setShowMoveToOtherSplitView(visible
);
165 void ContextMenuSettingsPage::applySettings()
167 if (!m_initialized
) {
171 KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
172 KConfigGroup showGroup
= config
.group("Show");
174 QStringList enabledPlugins
;
176 const QAbstractItemModel
*model
= m_listView
->model();
177 for (int i
= 0; i
< model
->rowCount(); ++i
) {
178 const QModelIndex index
= model
->index(i
, 0);
179 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
180 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
182 if (service
.startsWith(VersionControlServicePrefix
)) {
184 enabledPlugins
.append(model
->data(index
, Qt::DisplayRole
).toString());
186 } else if (service
== QLatin1String(DeleteService
)) {
187 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals
);
188 KConfigGroup
configGroup(globalConfig
, "KDE");
189 configGroup
.writeEntry("ShowDeleteCommand", checked
);
191 } else if (service
== QLatin1String(CopyToMoveToService
)) {
192 ContextMenuSettings::setShowCopyMoveMenu(checked
);
193 ContextMenuSettings::self()->save();
194 } else if (m_actionIds
.contains(service
)) {
195 setEntryVisible(service
, checked
);
196 ContextMenuSettings::self()->save();
198 showGroup
.writeEntry(service
, checked
);
204 if (m_enabledVcsPlugins
!= enabledPlugins
) {
205 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
206 VersionControlSettings::self()->save();
208 if (!laterSelected
) {
209 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
210 KMessageBox::ButtonCode promptRestart
=
211 KMessageBox::questionTwoActions(window(),
213 KMessageBox::ButtonCode promptRestart
=
214 KMessageBox::questionYesNo(window(),
217 "Dolphin must be restarted to apply the "
218 "updated version control system settings."),
219 i18nc("@info", "Restart now?"),
220 KGuiItem(QApplication::translate("KStandardGuiItem", "&Restart"), QStringLiteral("dialog-restart")),
221 KGuiItem(QApplication::translate("KStandardGuiItem", "&Later"), QStringLiteral("dialog-later")));
222 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
223 if (promptRestart
== KMessageBox::ButtonCode::PrimaryAction
) {
225 if (promptRestart
== KMessageBox::ButtonCode::Yes
) {
227 Dolphin::openNewWindow();
230 laterSelected
= true;
236 void ContextMenuSettingsPage::restoreDefaults()
238 QAbstractItemModel
*model
= m_listView
->model();
239 for (int i
= 0; i
< model
->rowCount(); ++i
) {
240 const QModelIndex index
= model
->index(i
, 0);
241 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
244 !service
.startsWith(VersionControlServicePrefix
) && service
!= QLatin1String(DeleteService
) && service
!= QLatin1String(CopyToMoveToService
);
245 model
->setData(index
, checked
, Qt::CheckStateRole
);
249 void ContextMenuSettingsPage::showEvent(QShowEvent
*event
)
251 if (!event
->spontaneous() && !m_initialized
) {
254 loadVersionControlSystems();
256 // Add "Show 'Delete' command" as service
257 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals
);
258 KConfigGroup
configGroup(globalConfig
, "KDE");
259 addRow(QStringLiteral("edit-delete"), i18nc("@option:check", "Delete"), DeleteService
, configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
261 // Add "Show 'Copy To' and 'Move To' commands" as service
262 addRow(QStringLiteral("edit-copy"),
263 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
265 ContextMenuSettings::showCopyMoveMenu());
268 // Add other built-in actions
269 for (const QString
&id
: m_actionIds
) {
270 const QAction
*action
= m_actions
->action(id
);
272 addRow(action
->icon().name(), action
->text(), id
, entryVisible(id
));
277 m_sortModel
->sort(Qt::DisplayRole
);
279 m_initialized
= true;
281 SettingsPageBase::showEvent(event
);
284 void ContextMenuSettingsPage::loadServices()
286 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
287 const KConfigGroup showGroup
= config
.group("Show");
289 // Load generic services
290 const auto locations
= QStandardPaths::locateAll(QStandardPaths::GenericDataLocation
, QStringLiteral("kio/servicemenus"), QStandardPaths::LocateDirectory
);
291 QStringList files
= KFileUtils::findAllUniqueFiles(locations
);
293 #if KIOWIDGETS_BUILD_DEPRECATED_SINCE(5, 90)
294 const KService::List services
= KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
295 for (const KService::Ptr
&service
: services
) {
296 files
<< QStandardPaths::locate(QStandardPaths::GenericDataLocation
, "kservices5/" % service
->entryPath());
300 for (const auto &file
: qAsConst(files
)) {
301 const QList
<KServiceAction
> serviceActions
= KDesktopFileActions::userDefinedServices(KService(file
), true);
303 const KDesktopFile
desktopFile(file
);
304 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
306 for (const KServiceAction
&action
: serviceActions
) {
307 const QString serviceName
= action
.name();
308 const bool addService
= !action
.noDisplay() && !action
.isSeparator() && !isInServicesList(serviceName
);
311 const QString itemName
= subMenuName
.isEmpty() ? action
.text() : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
312 const bool checked
= showGroup
.readEntry(serviceName
, true);
313 addRow(action
.icon(), itemName
, serviceName
, checked
);
318 // Load service plugins, this is deprecated in KIO 5.82
319 #if KIOCORE_BUILD_DEPRECATED_SINCE(5, 82)
320 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
321 for (const KService::Ptr
&service
: pluginServices
) {
322 const QString desktopEntryName
= service
->desktopEntryName();
323 if (!isInServicesList(desktopEntryName
)) {
324 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
325 addRow(service
->icon(), service
->name(), desktopEntryName
, checked
);
330 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
331 const auto jsonPlugins
= KPluginMetaData::findPlugins(QStringLiteral("kf" QT_STRINGIFY(QT_VERSION_MAJOR
)) + QStringLiteral("/kfileitemaction"));
333 for (const auto &jsonMetadata
: jsonPlugins
) {
334 const QString desktopEntryName
= jsonMetadata
.pluginId();
335 if (!isInServicesList(desktopEntryName
)) {
336 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
337 addRow(jsonMetadata
.iconName(), jsonMetadata
.name(), desktopEntryName
, checked
);
341 m_sortModel
->sort(Qt::DisplayRole
);
342 m_searchLineEdit
->setFocus(Qt::OtherFocusReason
);
345 void ContextMenuSettingsPage::loadVersionControlSystems()
347 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
349 // Create a checkbox for each available version control plugin
350 QSet
<QString
> loadedPlugins
;
352 const QVector
<KPluginMetaData
> plugins
= KPluginMetaData::findPlugins(QStringLiteral("dolphin/vcs"));
353 for (const auto &plugin
: plugins
) {
354 const QString pluginName
= plugin
.name();
355 addRow(QStringLiteral("code-class"), pluginName
, VersionControlServicePrefix
+ pluginName
, enabledPlugins
.contains(pluginName
));
356 loadedPlugins
+= pluginName
;
359 m_sortModel
->sort(Qt::DisplayRole
);
362 bool ContextMenuSettingsPage::isInServicesList(const QString
&service
) const
364 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
365 const QModelIndex index
= m_serviceModel
->index(i
, 0);
366 if (m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
373 void ContextMenuSettingsPage::addRow(const QString
&icon
, const QString
&text
, const QString
&value
, bool checked
)
375 m_serviceModel
->insertRow(0);
377 const QModelIndex index
= m_serviceModel
->index(0, 0);
378 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
379 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
380 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
381 m_serviceModel
->setData(index
, checked
, Qt::CheckStateRole
);