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 <KNS3/Button>
22 #include <KPluginMetaData>
24 #include <KServiceTypeTrader>
26 #include <kio_version.h>
28 #include <QGridLayout>
30 #include <QListWidget>
33 #include <QSortFilterProxyModel>
35 #include <QApplication>
39 const bool ShowDeleteDefault
= false;
40 const char VersionControlServicePrefix
[] = "_version_control_";
41 const char DeleteService
[] = "_delete";
42 const char CopyToMoveToService
[] ="_copy_to_move_to";
44 bool laterSelected
= false;
47 ContextMenuSettingsPage::ContextMenuSettingsPage(QWidget
* parent
,
48 const KActionCollection
* actions
,
49 const QStringList
& actionIds
) :
50 SettingsPageBase(parent
),
52 m_serviceModel(nullptr),
55 m_enabledVcsPlugins(),
57 m_actionIds(actionIds
)
59 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
61 QLabel
* label
= new QLabel(i18nc("@label:textbox",
62 "Select which services should "
63 "be shown in the context menu:"), this);
64 label
->setWordWrap(true);
65 m_searchLineEdit
= new QLineEdit(this);
66 m_searchLineEdit
->setPlaceholderText(i18nc("@label:textbox", "Search..."));
67 connect(m_searchLineEdit
, &QLineEdit::textChanged
, this, [this](const QString
&filter
){
68 m_sortModel
->setFilterFixedString(filter
);
71 m_listView
= new QListView(this);
72 QScroller::grabGesture(m_listView
->viewport(), QScroller::TouchGesture
);
74 auto *delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
75 m_serviceModel
= new ServiceModel(this);
76 m_sortModel
= new QSortFilterProxyModel(this);
77 m_sortModel
->setSourceModel(m_serviceModel
);
78 m_sortModel
->setSortRole(Qt::DisplayRole
);
79 m_sortModel
->setSortLocaleAware(true);
80 m_sortModel
->setFilterRole(Qt::DisplayRole
);
81 m_sortModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
82 m_listView
->setModel(m_sortModel
);
83 m_listView
->setItemDelegate(delegate
);
84 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
85 connect(m_listView
, &QListView::clicked
, this, &ContextMenuSettingsPage::changed
);
88 auto *downloadButton
= new KNS3::Button(i18nc("@action:button", "Download New Services..."),
89 QStringLiteral("servicemenu.knsrc"),
91 connect(downloadButton
, &KNS3::Button::dialogFinished
, this, [this](const KNS3::Entry::List
&changedEntries
) {
92 if (!changedEntries
.isEmpty()) {
93 m_serviceModel
->clear();
100 topLayout
->addWidget(label
);
101 topLayout
->addWidget(m_searchLineEdit
);
102 topLayout
->addWidget(m_listView
);
104 topLayout
->addWidget(downloadButton
);
107 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
108 std::sort(m_enabledVcsPlugins
.begin(), m_enabledVcsPlugins
.end());
111 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") {
131 return ContextMenuSettings::showOpenTerminal();
136 void ContextMenuSettingsPage::setEntryVisible(const QString
& id
, bool visible
)
138 if (id
== "add_to_places") {
139 ContextMenuSettings::setShowAddToPlaces(visible
);
140 } else if (id
== "sort") {
141 ContextMenuSettings::setShowSortBy(visible
);
142 } else if (id
== "view_mode") {
143 ContextMenuSettings::setShowViewMode(visible
);
144 } else if (id
== "open_in_new_tab") {
145 ContextMenuSettings::setShowOpenInNewTab(visible
);
146 } else if (id
== "open_in_new_window") {
147 ContextMenuSettings::setShowOpenInNewWindow(visible
);
148 } else if (id
== "copy_location") {
149 ContextMenuSettings::setShowCopyLocation(visible
);
150 } else if (id
== "duplicate") {
151 ContextMenuSettings::setShowDuplicateHere(visible
);
152 } else if (id
== "open_terminal") {
153 ContextMenuSettings::setShowOpenTerminal(visible
);
157 void ContextMenuSettingsPage::applySettings()
159 if (!m_initialized
) {
163 KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
164 KConfigGroup showGroup
= config
.group("Show");
166 QStringList enabledPlugins
;
168 const QAbstractItemModel
*model
= m_listView
->model();
169 for (int i
= 0; i
< model
->rowCount(); ++i
) {
170 const QModelIndex index
= model
->index(i
, 0);
171 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
172 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
174 if (service
.startsWith(VersionControlServicePrefix
)) {
176 enabledPlugins
.append(model
->data(index
, Qt::DisplayRole
).toString());
178 } else if (service
== QLatin1String(DeleteService
)) {
179 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals
);
180 KConfigGroup
configGroup(globalConfig
, "KDE");
181 configGroup
.writeEntry("ShowDeleteCommand", checked
);
183 } else if (service
== QLatin1String(CopyToMoveToService
)) {
184 ContextMenuSettings::setShowCopyMoveMenu(checked
);
185 ContextMenuSettings::self()->save();
186 } else if (m_actionIds
.contains(service
)) {
187 setEntryVisible(service
, checked
);
188 ContextMenuSettings::self()->save();
190 showGroup
.writeEntry(service
, checked
);
196 if (m_enabledVcsPlugins
!= enabledPlugins
) {
197 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
198 VersionControlSettings::self()->save();
200 if (!laterSelected
) {
201 KMessageBox::ButtonCode promptRestart
= KMessageBox::questionYesNo(window(),
202 i18nc("@info", "Dolphin must be restarted to apply the "
203 "updated version control system settings."),
204 i18nc("@info", "Restart now?"),
205 KGuiItem(QApplication::translate("KStandardGuiItem", "&Restart"), QStringLiteral("dialog-restart")),
206 KGuiItem(QApplication::translate("KStandardGuiItem", "&Later"), QStringLiteral("dialog-later"))
208 if (promptRestart
== KMessageBox::ButtonCode::Yes
) {
209 Dolphin::openNewWindow();
212 laterSelected
= true;
218 void ContextMenuSettingsPage::restoreDefaults()
220 QAbstractItemModel
* model
= m_listView
->model();
221 for (int i
= 0; i
< model
->rowCount(); ++i
) {
222 const QModelIndex index
= model
->index(i
, 0);
223 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
225 const bool checked
= !service
.startsWith(VersionControlServicePrefix
)
226 && service
!= QLatin1String(DeleteService
)
227 && service
!= QLatin1String(CopyToMoveToService
);
228 model
->setData(index
, checked
, Qt::CheckStateRole
);
232 void ContextMenuSettingsPage::showEvent(QShowEvent
* event
)
234 if (!event
->spontaneous() && !m_initialized
) {
237 loadVersionControlSystems();
239 // Add "Show 'Delete' command" as service
240 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals
);
241 KConfigGroup
configGroup(globalConfig
, "KDE");
242 addRow(QStringLiteral("edit-delete"),
243 i18nc("@option:check", "Delete"),
245 configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
247 // Add "Show 'Copy To' and 'Move To' commands" as service
248 addRow(QStringLiteral("edit-copy"),
249 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
251 ContextMenuSettings::showCopyMoveMenu());
254 // Add other built-in actions
255 for (const QString
& id
: m_actionIds
) {
256 const QAction
* action
= m_actions
->action(id
);
258 addRow(action
->icon().name(), action
->text(), id
, entryVisible(id
));
263 m_sortModel
->sort(Qt::DisplayRole
);
265 m_initialized
= true;
267 SettingsPageBase::showEvent(event
);
270 void ContextMenuSettingsPage::loadServices()
272 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
273 const KConfigGroup showGroup
= config
.group("Show");
275 // Load generic services
276 const auto locations
= QStandardPaths::locateAll(QStandardPaths::GenericDataLocation
, QStringLiteral("kio/servicemenus"), QStandardPaths::LocateDirectory
);
277 QStringList files
= KFileUtils::findAllUniqueFiles(locations
);
278 const KService::List services
= KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
279 for (const KService::Ptr
&service
: services
) {
280 files
<< QStandardPaths::locate(QStandardPaths::GenericDataLocation
, "kservices5/" % service
->entryPath());
282 for (const auto &file
: qAsConst(files
)) {
283 const QList
<KServiceAction
> serviceActions
= KDesktopFileActions::userDefinedServices(KService(file
), true);
285 const KDesktopFile
desktopFile(file
);
286 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
288 for (const KServiceAction
&action
: serviceActions
) {
289 const QString serviceName
= action
.name();
290 const bool addService
= !action
.noDisplay() && !action
.isSeparator() && !isInServicesList(serviceName
);
293 const QString itemName
= subMenuName
.isEmpty()
295 : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
296 const bool checked
= showGroup
.readEntry(serviceName
, true);
297 addRow(action
.icon(), itemName
, serviceName
, checked
);
302 // Load service plugins, this is deprecated in KIO 5.82
303 #if KIO_VERSION < QT_VERSION_CHECK(6, 0, 0)
304 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
305 for (const KService::Ptr
&service
: pluginServices
) {
306 const QString desktopEntryName
= service
->desktopEntryName();
307 if (!isInServicesList(desktopEntryName
)) {
308 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
309 addRow(service
->icon(), service
->name(), desktopEntryName
, checked
);
314 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
315 const auto jsonPlugins
= KPluginMetaData::findPlugins(QStringLiteral("kf5/kfileitemaction"));
317 for (const auto &jsonMetadata
: jsonPlugins
) {
318 const QString desktopEntryName
= jsonMetadata
.pluginId();
319 if (!isInServicesList(desktopEntryName
)) {
320 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
321 addRow(jsonMetadata
.iconName(), jsonMetadata
.name(), desktopEntryName
, checked
);
325 m_sortModel
->sort(Qt::DisplayRole
);
326 m_searchLineEdit
->setFocus(Qt::OtherFocusReason
);
329 void ContextMenuSettingsPage::loadVersionControlSystems()
331 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
333 // Create a checkbox for each available version control plugin
334 QSet
<QString
> loadedPlugins
;
336 const QVector
<KPluginMetaData
> plugins
= KPluginMetaData::findPlugins(QStringLiteral("dolphin/vcs"));
337 for (const auto &plugin
: plugins
) {
338 const QString pluginName
= plugin
.name();
339 addRow(QStringLiteral("code-class"),
341 VersionControlServicePrefix
+ pluginName
,
342 enabledPlugins
.contains(pluginName
));
343 loadedPlugins
+= pluginName
;
346 m_sortModel
->sort(Qt::DisplayRole
);
349 bool ContextMenuSettingsPage::isInServicesList(const QString
&service
) const
351 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
352 const QModelIndex index
= m_serviceModel
->index(i
, 0);
353 if (m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
360 void ContextMenuSettingsPage::addRow(const QString
&icon
,
362 const QString
&value
,
365 m_serviceModel
->insertRow(0);
367 const QModelIndex index
= m_serviceModel
->index(0, 0);
368 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
369 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
370 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
371 m_serviceModel
->setData(index
, checked
, Qt::CheckStateRole
);