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 KMessageBox::ButtonCode promptRestart
=
210 KMessageBox::questionTwoActions(window(),
212 "Dolphin must be restarted to apply the "
213 "updated version control system settings."),
214 i18nc("@info", "Restart now?"),
215 KGuiItem(QApplication::translate("KStandardGuiItem", "&Restart"), QStringLiteral("dialog-restart")),
216 KGuiItem(QApplication::translate("KStandardGuiItem", "&Later"), QStringLiteral("dialog-later")));
217 if (promptRestart
== KMessageBox::ButtonCode::PrimaryAction
) {
218 Dolphin::openNewWindow();
221 laterSelected
= true;
227 void ContextMenuSettingsPage::restoreDefaults()
229 QAbstractItemModel
*model
= m_listView
->model();
230 for (int i
= 0; i
< model
->rowCount(); ++i
) {
231 const QModelIndex index
= model
->index(i
, 0);
232 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
235 !service
.startsWith(VersionControlServicePrefix
) && service
!= QLatin1String(DeleteService
) && service
!= QLatin1String(CopyToMoveToService
);
236 model
->setData(index
, checked
, Qt::CheckStateRole
);
240 void ContextMenuSettingsPage::showEvent(QShowEvent
*event
)
242 if (!event
->spontaneous() && !m_initialized
) {
245 loadVersionControlSystems();
247 // Add "Show 'Delete' command" as service
248 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals
);
249 KConfigGroup
configGroup(globalConfig
, "KDE");
250 addRow(QStringLiteral("edit-delete"), i18nc("@option:check", "Delete"), DeleteService
, configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
252 // Add "Show 'Copy To' and 'Move To' commands" as service
253 addRow(QStringLiteral("edit-copy"),
254 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
256 ContextMenuSettings::showCopyMoveMenu());
259 // Add other built-in actions
260 for (const QString
&id
: m_actionIds
) {
261 const QAction
*action
= m_actions
->action(id
);
263 addRow(action
->icon().name(), action
->text(), id
, entryVisible(id
));
268 m_sortModel
->sort(Qt::DisplayRole
);
270 m_initialized
= true;
272 SettingsPageBase::showEvent(event
);
275 void ContextMenuSettingsPage::loadServices()
277 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
278 const KConfigGroup showGroup
= config
.group("Show");
280 // Load generic services
281 const auto locations
= QStandardPaths::locateAll(QStandardPaths::GenericDataLocation
, QStringLiteral("kio/servicemenus"), QStandardPaths::LocateDirectory
);
282 QStringList files
= KFileUtils::findAllUniqueFiles(locations
);
284 #if KIOWIDGETS_BUILD_DEPRECATED_SINCE(5, 90)
285 const KService::List services
= KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
286 for (const KService::Ptr
&service
: services
) {
287 files
<< QStandardPaths::locate(QStandardPaths::GenericDataLocation
, "kservices5/" % service
->entryPath());
291 for (const auto &file
: qAsConst(files
)) {
292 const QList
<KServiceAction
> serviceActions
= KDesktopFileActions::userDefinedServices(KService(file
), true);
294 const KDesktopFile
desktopFile(file
);
295 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
297 for (const KServiceAction
&action
: serviceActions
) {
298 const QString serviceName
= action
.name();
299 const bool addService
= !action
.noDisplay() && !action
.isSeparator() && !isInServicesList(serviceName
);
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
);
309 // Load service plugins, this is deprecated in KIO 5.82
310 #if KIOCORE_BUILD_DEPRECATED_SINCE(5, 82)
311 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
312 for (const KService::Ptr
&service
: pluginServices
) {
313 const QString desktopEntryName
= service
->desktopEntryName();
314 if (!isInServicesList(desktopEntryName
)) {
315 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
316 addRow(service
->icon(), service
->name(), desktopEntryName
, checked
);
321 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
322 const auto jsonPlugins
= KPluginMetaData::findPlugins(QStringLiteral("kf" QT_STRINGIFY(QT_VERSION_MAJOR
)) + QStringLiteral("/kfileitemaction"));
324 for (const auto &jsonMetadata
: jsonPlugins
) {
325 const QString desktopEntryName
= jsonMetadata
.pluginId();
326 if (!isInServicesList(desktopEntryName
)) {
327 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
328 addRow(jsonMetadata
.iconName(), jsonMetadata
.name(), desktopEntryName
, checked
);
332 m_sortModel
->sort(Qt::DisplayRole
);
333 m_searchLineEdit
->setFocus(Qt::OtherFocusReason
);
336 void ContextMenuSettingsPage::loadVersionControlSystems()
338 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
340 // Create a checkbox for each available version control plugin
341 QSet
<QString
> loadedPlugins
;
343 const QVector
<KPluginMetaData
> plugins
= KPluginMetaData::findPlugins(QStringLiteral("dolphin/vcs"));
344 for (const auto &plugin
: plugins
) {
345 const QString pluginName
= plugin
.name();
346 addRow(QStringLiteral("code-class"), pluginName
, VersionControlServicePrefix
+ pluginName
, enabledPlugins
.contains(pluginName
));
347 loadedPlugins
+= pluginName
;
350 m_sortModel
->sort(Qt::DisplayRole
);
353 bool ContextMenuSettingsPage::isInServicesList(const QString
&service
) const
355 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
356 const QModelIndex index
= m_serviceModel
->index(i
, 0);
357 if (m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
364 void ContextMenuSettingsPage::addRow(const QString
&icon
, const QString
&text
, const QString
&value
, bool checked
)
366 m_serviceModel
->insertRow(0);
368 const QModelIndex index
= m_serviceModel
->index(0, 0);
369 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
370 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
371 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
372 m_serviceModel
->setData(index
, checked
, Qt::CheckStateRole
);
375 #include "moc_contextmenusettingspage.cpp"