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"
15 #include <KDesktopFile>
16 #include <KLocalizedString>
17 #include <KMessageBox>
18 #include <KNS3/Button>
19 #include <KPluginMetaData>
21 #include <KServiceTypeTrader>
22 #include <KDesktopFileActions>
24 #include <kio_version.h>
26 #include <QGridLayout>
28 #include <QListWidget>
31 #include <QSortFilterProxyModel>
36 const bool ShowDeleteDefault
= false;
37 const char VersionControlServicePrefix
[] = "_version_control_";
38 const char DeleteService
[] = "_delete";
39 const char CopyToMoveToService
[] ="_copy_to_move_to";
42 ContextMenuSettingsPage::ContextMenuSettingsPage(QWidget
* parent
,
43 const KActionCollection
* actions
,
44 const QStringList
& actionIds
) :
45 SettingsPageBase(parent
),
47 m_serviceModel(nullptr),
50 m_enabledVcsPlugins(),
52 m_actionIds(actionIds
)
54 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
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
);
66 m_listView
= new QListView(this);
67 QScroller::grabGesture(m_listView
->viewport(), QScroller::TouchGesture
);
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
);
83 auto *downloadButton
= new KNS3::Button(i18nc("@action:button", "Download New Services..."),
84 QStringLiteral("servicemenu.knsrc"),
86 connect(downloadButton
, &KNS3::Button::dialogFinished
, this, [this](const KNS3::Entry::List
&changedEntries
) {
87 if (!changedEntries
.isEmpty()) {
88 m_serviceModel
->clear();
95 topLayout
->addWidget(label
);
96 topLayout
->addWidget(m_searchLineEdit
);
97 topLayout
->addWidget(m_listView
);
99 topLayout
->addWidget(downloadButton
);
102 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
103 std::sort(m_enabledVcsPlugins
.begin(), m_enabledVcsPlugins
.end());
106 ContextMenuSettingsPage::~ContextMenuSettingsPage() {
109 bool ContextMenuSettingsPage::entryVisible(const QString
& id
)
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 } else if (id
== "open_terminal") {
126 return ContextMenuSettings::showOpenTerminal();
131 void ContextMenuSettingsPage::setEntryVisible(const QString
& id
, bool visible
)
133 if (id
== "add_to_places") {
134 ContextMenuSettings::setShowAddToPlaces(visible
);
135 } else if (id
== "sort") {
136 ContextMenuSettings::setShowSortBy(visible
);
137 } else if (id
== "view_mode") {
138 ContextMenuSettings::setShowViewMode(visible
);
139 } else if (id
== "open_in_new_tab") {
140 ContextMenuSettings::setShowOpenInNewTab(visible
);
141 } else if (id
== "open_in_new_window") {
142 ContextMenuSettings::setShowOpenInNewWindow(visible
);
143 } else if (id
== "copy_location") {
144 ContextMenuSettings::setShowCopyLocation(visible
);
145 } else if (id
== "duplicate") {
146 ContextMenuSettings::setShowDuplicateHere(visible
);
147 } else if (id
== "open_terminal") {
148 ContextMenuSettings::setShowOpenTerminal(visible
);
152 void ContextMenuSettingsPage::applySettings()
154 if (!m_initialized
) {
158 KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
159 KConfigGroup showGroup
= config
.group("Show");
161 QStringList enabledPlugins
;
163 const QAbstractItemModel
*model
= m_listView
->model();
164 for (int i
= 0; i
< model
->rowCount(); ++i
) {
165 const QModelIndex index
= model
->index(i
, 0);
166 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
167 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
169 if (service
.startsWith(VersionControlServicePrefix
)) {
171 enabledPlugins
.append(model
->data(index
, Qt::DisplayRole
).toString());
173 } else if (service
== QLatin1String(DeleteService
)) {
174 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals
);
175 KConfigGroup
configGroup(globalConfig
, "KDE");
176 configGroup
.writeEntry("ShowDeleteCommand", checked
);
178 } else if (service
== QLatin1String(CopyToMoveToService
)) {
179 ContextMenuSettings::setShowCopyMoveMenu(checked
);
180 ContextMenuSettings::self()->save();
181 } else if (m_actionIds
.contains(service
)) {
182 setEntryVisible(service
, checked
);
183 ContextMenuSettings::self()->save();
185 showGroup
.writeEntry(service
, checked
);
191 if (m_enabledVcsPlugins
!= enabledPlugins
) {
192 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
193 VersionControlSettings::self()->save();
195 KMessageBox::information(window(),
196 i18nc("@info", "Dolphin must be restarted to apply the "
197 "updated version control systems settings."),
198 QString(), // default title
199 QStringLiteral("ShowVcsRestartInformation"));
203 void ContextMenuSettingsPage::restoreDefaults()
205 QAbstractItemModel
* model
= m_listView
->model();
206 for (int i
= 0; i
< model
->rowCount(); ++i
) {
207 const QModelIndex index
= model
->index(i
, 0);
208 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
210 const bool checked
= !service
.startsWith(VersionControlServicePrefix
)
211 && service
!= QLatin1String(DeleteService
)
212 && service
!= QLatin1String(CopyToMoveToService
);
213 model
->setData(index
, checked
, Qt::CheckStateRole
);
217 void ContextMenuSettingsPage::showEvent(QShowEvent
* event
)
219 if (!event
->spontaneous() && !m_initialized
) {
222 loadVersionControlSystems();
224 // Add "Show 'Delete' command" as service
225 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals
);
226 KConfigGroup
configGroup(globalConfig
, "KDE");
227 addRow(QStringLiteral("edit-delete"),
228 i18nc("@option:check", "Delete"),
230 configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
232 // Add "Show 'Copy To' and 'Move To' commands" as service
233 addRow(QStringLiteral("edit-copy"),
234 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
236 ContextMenuSettings::showCopyMoveMenu());
238 // Add other built-in actions
239 for (const QString
& id
: m_actionIds
) {
240 const QAction
* action
= m_actions
->action(id
);
242 addRow(action
->icon().name(), action
->text(), id
, entryVisible(id
));
246 m_sortModel
->sort(Qt::DisplayRole
);
248 m_initialized
= true;
250 SettingsPageBase::showEvent(event
);
253 void ContextMenuSettingsPage::loadServices()
255 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
256 const KConfigGroup showGroup
= config
.group("Show");
258 // Load generic services
259 const KService::List entries
= KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
260 for (const KService::Ptr
&service
: entries
) {
261 const QString file
= QStandardPaths::locate(QStandardPaths::GenericDataLocation
, "kservices5/" % service
->entryPath());
262 const QList
<KServiceAction
> serviceActions
= KDesktopFileActions::userDefinedServices(file
, true);
264 const KDesktopFile
desktopFile(file
);
265 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
267 for (const KServiceAction
&action
: serviceActions
) {
268 const QString serviceName
= action
.name();
269 const bool addService
= !action
.noDisplay() && !action
.isSeparator() && !isInServicesList(serviceName
);
272 const QString itemName
= subMenuName
.isEmpty()
274 : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
275 const bool checked
= showGroup
.readEntry(serviceName
, true);
276 addRow(action
.icon(), itemName
, serviceName
, checked
);
281 // Load service plugins, this is deprecated in KIO 5.82
282 #if KIO_VERSION < QT_VERSION_CHECK(6, 0, 0)
283 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
284 for (const KService::Ptr
&service
: pluginServices
) {
285 const QString desktopEntryName
= service
->desktopEntryName();
286 if (!isInServicesList(desktopEntryName
)) {
287 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
288 addRow(service
->icon(), service
->name(), desktopEntryName
, checked
);
293 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
294 const auto jsonPlugins
= KPluginLoader::findPlugins(QStringLiteral("kf5/kfileitemaction"));
296 for (const auto &jsonMetadata
: jsonPlugins
) {
297 const QString desktopEntryName
= jsonMetadata
.pluginId();
298 if (!isInServicesList(desktopEntryName
)) {
299 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
300 addRow(jsonMetadata
.iconName(), jsonMetadata
.name(), desktopEntryName
, checked
);
304 m_sortModel
->sort(Qt::DisplayRole
);
305 m_searchLineEdit
->setFocus(Qt::OtherFocusReason
);
308 void ContextMenuSettingsPage::loadVersionControlSystems()
310 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
312 // Create a checkbox for each available version control plugin
313 QSet
<QString
> loadedPlugins
;
315 const QVector
<KPluginMetaData
> plugins
= KPluginLoader::findPlugins(QStringLiteral("dolphin/vcs"));
316 for (const auto &plugin
: plugins
) {
317 const QString pluginName
= plugin
.name();
318 addRow(QStringLiteral("code-class"),
320 VersionControlServicePrefix
+ pluginName
,
321 enabledPlugins
.contains(pluginName
));
322 loadedPlugins
+= pluginName
;
325 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("FileViewVersionControlPlugin"));
326 for (const auto &plugin
: pluginServices
) {
327 const QString pluginName
= plugin
->name();
328 if (loadedPlugins
.contains(pluginName
)) {
331 addRow(QStringLiteral("code-class"),
333 VersionControlServicePrefix
+ pluginName
,
334 enabledPlugins
.contains(pluginName
));
337 m_sortModel
->sort(Qt::DisplayRole
);
340 bool ContextMenuSettingsPage::isInServicesList(const QString
&service
) const
342 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
343 const QModelIndex index
= m_serviceModel
->index(i
, 0);
344 if (m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
351 void ContextMenuSettingsPage::addRow(const QString
&icon
,
353 const QString
&value
,
356 m_serviceModel
->insertRow(0);
358 const QModelIndex index
= m_serviceModel
->index(0, 0);
359 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
360 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
361 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
362 m_serviceModel
->setData(index
, checked
, Qt::CheckStateRole
);