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 <QGridLayout>
26 #include <QListWidget>
29 #include <QSortFilterProxyModel>
34 const bool ShowDeleteDefault
= false;
35 const char VersionControlServicePrefix
[] = "_version_control_";
36 const char DeleteService
[] = "_delete";
37 const char CopyToMoveToService
[] ="_copy_to_move_to";
40 ContextMenuSettingsPage::ContextMenuSettingsPage(QWidget
* parent
,
41 KActionCollection
* actions
,
42 QStringList actionIds
) :
43 SettingsPageBase(parent
),
45 m_serviceModel(nullptr),
48 m_enabledVcsPlugins(),
50 m_actionIds(actionIds
)
52 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
54 QLabel
* label
= new QLabel(i18nc("@label:textbox",
55 "Select which services should "
56 "be shown in the context menu:"), this);
57 label
->setWordWrap(true);
58 m_searchLineEdit
= new QLineEdit(this);
59 m_searchLineEdit
->setPlaceholderText(i18nc("@label:textbox", "Search..."));
60 connect(m_searchLineEdit
, &QLineEdit::textChanged
, this, [this](const QString
&filter
){
61 m_sortModel
->setFilterFixedString(filter
);
64 m_listView
= new QListView(this);
65 QScroller::grabGesture(m_listView
->viewport(), QScroller::TouchGesture
);
67 auto *delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
68 m_serviceModel
= new ServiceModel(this);
69 m_sortModel
= new QSortFilterProxyModel(this);
70 m_sortModel
->setSourceModel(m_serviceModel
);
71 m_sortModel
->setSortRole(Qt::DisplayRole
);
72 m_sortModel
->setSortLocaleAware(true);
73 m_sortModel
->setFilterRole(Qt::DisplayRole
);
74 m_sortModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
75 m_listView
->setModel(m_sortModel
);
76 m_listView
->setItemDelegate(delegate
);
77 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
78 connect(m_listView
, &QListView::clicked
, this, &ContextMenuSettingsPage::changed
);
81 auto *downloadButton
= new KNS3::Button(i18nc("@action:button", "Download New Services..."),
82 QStringLiteral("servicemenu.knsrc"),
84 connect(downloadButton
, &KNS3::Button::dialogFinished
, this, [this](const KNS3::Entry::List
&changedEntries
) {
85 if (!changedEntries
.isEmpty()) {
86 m_serviceModel
->clear();
93 topLayout
->addWidget(label
);
94 topLayout
->addWidget(m_searchLineEdit
);
95 topLayout
->addWidget(m_listView
);
97 topLayout
->addWidget(downloadButton
);
100 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
101 std::sort(m_enabledVcsPlugins
.begin(), m_enabledVcsPlugins
.end());
104 ContextMenuSettingsPage::~ContextMenuSettingsPage() {
107 bool ContextMenuSettingsPage::entryVisible(const QString
& id
)
109 if (id
== "add_to_places") {
110 return ContextMenuSettings::showAddToPlaces();
111 } else if (id
== "sort") {
112 return ContextMenuSettings::showSortBy();
113 } else if (id
== "view_mode") {
114 return ContextMenuSettings::showViewMode();
115 } else if (id
== "open_in_new_tab") {
116 return ContextMenuSettings::showOpenInNewTab();
117 } else if (id
== "open_in_new_window") {
118 return ContextMenuSettings::showOpenInNewWindow();
119 } else if (id
== "copy_location") {
120 return ContextMenuSettings::showCopyLocation();
121 } else if (id
== "duplicate") {
122 return ContextMenuSettings::showDuplicateHere();
127 void ContextMenuSettingsPage::setEntryVisible(const QString
& id
, bool visible
)
129 if (id
== "add_to_places") {
130 ContextMenuSettings::setShowAddToPlaces(visible
);
131 } else if (id
== "sort") {
132 ContextMenuSettings::setShowSortBy(visible
);
133 } else if (id
== "view_mode") {
134 ContextMenuSettings::setShowViewMode(visible
);
135 } else if (id
== "open_in_new_tab") {
136 ContextMenuSettings::setShowOpenInNewTab(visible
);
137 } else if (id
== "open_in_new_window") {
138 ContextMenuSettings::setShowOpenInNewWindow(visible
);
139 } else if (id
== "copy_location") {
140 ContextMenuSettings::setShowCopyLocation(visible
);
141 } else if (id
== "duplicate") {
142 ContextMenuSettings::setShowDuplicateHere(visible
);
146 void ContextMenuSettingsPage::applySettings()
148 if (!m_initialized
) {
152 KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
153 KConfigGroup showGroup
= config
.group("Show");
155 QStringList enabledPlugins
;
157 const QAbstractItemModel
*model
= m_listView
->model();
158 for (int i
= 0; i
< model
->rowCount(); ++i
) {
159 const QModelIndex index
= model
->index(i
, 0);
160 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
161 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
163 if (service
.startsWith(VersionControlServicePrefix
)) {
165 enabledPlugins
.append(model
->data(index
, Qt::DisplayRole
).toString());
167 } else if (service
== QLatin1String(DeleteService
)) {
168 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals
);
169 KConfigGroup
configGroup(globalConfig
, "KDE");
170 configGroup
.writeEntry("ShowDeleteCommand", checked
);
172 } else if (service
== QLatin1String(CopyToMoveToService
)) {
173 ContextMenuSettings::setShowCopyMoveMenu(checked
);
174 ContextMenuSettings::self()->save();
175 } else if (m_actionIds
.contains(service
)) {
176 setEntryVisible(service
, checked
);
177 ContextMenuSettings::self()->save();
179 showGroup
.writeEntry(service
, checked
);
185 if (m_enabledVcsPlugins
!= enabledPlugins
) {
186 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
187 VersionControlSettings::self()->save();
189 KMessageBox::information(window(),
190 i18nc("@info", "Dolphin must be restarted to apply the "
191 "updated version control systems settings."),
192 QString(), // default title
193 QStringLiteral("ShowVcsRestartInformation"));
197 void ContextMenuSettingsPage::restoreDefaults()
199 QAbstractItemModel
* model
= m_listView
->model();
200 for (int i
= 0; i
< model
->rowCount(); ++i
) {
201 const QModelIndex index
= model
->index(i
, 0);
202 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
204 const bool checked
= !service
.startsWith(VersionControlServicePrefix
)
205 && service
!= QLatin1String(DeleteService
)
206 && service
!= QLatin1String(CopyToMoveToService
);
207 model
->setData(index
, checked
, Qt::CheckStateRole
);
211 void ContextMenuSettingsPage::showEvent(QShowEvent
* event
)
213 if (!event
->spontaneous() && !m_initialized
) {
216 loadVersionControlSystems();
218 // Add "Show 'Delete' command" as service
219 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals
);
220 KConfigGroup
configGroup(globalConfig
, "KDE");
221 addRow(QStringLiteral("edit-delete"),
222 i18nc("@option:check", "Delete"),
224 configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
226 // Add "Show 'Copy To' and 'Move To' commands" as service
227 addRow(QStringLiteral("edit-copy"),
228 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
230 ContextMenuSettings::showCopyMoveMenu());
232 // Add other built-in actions
233 for (const QString
& id
: qAsConst(m_actionIds
)) {
234 QAction
* action
= m_actions
->action(id
);
236 addRow(action
->icon().name(), action
->text(), id
, entryVisible(id
));
240 m_sortModel
->sort(Qt::DisplayRole
);
242 m_initialized
= true;
244 SettingsPageBase::showEvent(event
);
247 void ContextMenuSettingsPage::loadServices()
249 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
250 const KConfigGroup showGroup
= config
.group("Show");
252 // Load generic services
253 const KService::List entries
= KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
254 for (const KService::Ptr
&service
: entries
) {
255 const QString file
= QStandardPaths::locate(QStandardPaths::GenericDataLocation
, "kservices5/" % service
->entryPath());
256 const QList
<KServiceAction
> serviceActions
= KDesktopFileActions::userDefinedServices(file
, true);
258 const KDesktopFile
desktopFile(file
);
259 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
261 for (const KServiceAction
&action
: serviceActions
) {
262 const QString serviceName
= action
.name();
263 const bool addService
= !action
.noDisplay() && !action
.isSeparator() && !isInServicesList(serviceName
);
266 const QString itemName
= subMenuName
.isEmpty()
268 : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
269 const bool checked
= showGroup
.readEntry(serviceName
, true);
270 addRow(action
.icon(), itemName
, serviceName
, checked
);
275 // Load service plugins that implement the KFileItemActionPlugin interface
276 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
277 for (const KService::Ptr
&service
: pluginServices
) {
278 const QString desktopEntryName
= service
->desktopEntryName();
279 if (!isInServicesList(desktopEntryName
)) {
280 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
281 addRow(service
->icon(), service
->name(), desktopEntryName
, checked
);
285 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
286 const auto jsonPlugins
= KPluginLoader::findPlugins(QStringLiteral("kf5/kfileitemaction"), [](const KPluginMetaData
& metaData
) {
287 return metaData
.serviceTypes().contains(QLatin1String("KFileItemAction/Plugin"));
290 for (const auto &jsonMetadata
: jsonPlugins
) {
291 const QString desktopEntryName
= jsonMetadata
.pluginId();
292 if (!isInServicesList(desktopEntryName
)) {
293 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
294 addRow(jsonMetadata
.iconName(), jsonMetadata
.name(), desktopEntryName
, checked
);
298 m_sortModel
->sort(Qt::DisplayRole
);
299 m_searchLineEdit
->setFocus(Qt::OtherFocusReason
);
302 void ContextMenuSettingsPage::loadVersionControlSystems()
304 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
306 // Create a checkbox for each available version control plugin
307 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("FileViewVersionControlPlugin"));
308 for (const auto &plugin
: pluginServices
) {
309 const QString pluginName
= plugin
->name();
310 addRow(QStringLiteral("code-class"),
312 VersionControlServicePrefix
+ pluginName
,
313 enabledPlugins
.contains(pluginName
));
316 m_sortModel
->sort(Qt::DisplayRole
);
319 bool ContextMenuSettingsPage::isInServicesList(const QString
&service
) const
321 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
322 const QModelIndex index
= m_serviceModel
->index(i
, 0);
323 if (m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
330 void ContextMenuSettingsPage::addRow(const QString
&icon
,
332 const QString
&value
,
335 m_serviceModel
->insertRow(0);
337 const QModelIndex index
= m_serviceModel
->index(0, 0);
338 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
339 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
340 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
341 m_serviceModel
->setData(index
, checked
, Qt::CheckStateRole
);