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 <KPluginMetaData>
23 #include <KServiceTypeTrader>
24 #include <kio_version.h>
25 #include <kiocore_export.h>
26 #include <kservice_export.h>
29 #include <KNSWidgets/Button>
31 #include <QGridLayout>
33 #include <QListWidget>
36 #include <QSortFilterProxyModel>
38 #include <QApplication>
42 const bool ShowDeleteDefault
= false;
43 const char VersionControlServicePrefix
[] = "_version_control_";
44 const char DeleteService
[] = "_delete";
45 const char CopyToMoveToService
[] ="_copy_to_move_to";
47 bool laterSelected
= false;
50 ContextMenuSettingsPage::ContextMenuSettingsPage(QWidget
* parent
,
51 const KActionCollection
* actions
,
52 const QStringList
& actionIds
) :
53 SettingsPageBase(parent
),
55 m_serviceModel(nullptr),
58 m_enabledVcsPlugins(),
60 m_actionIds(actionIds
)
62 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
64 QLabel
* label
= new QLabel(i18nc("@label:textbox",
65 "Select which services should "
66 "be shown in the context menu:"), this);
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..."),
97 QStringLiteral("servicemenu.knsrc"),
99 connect(downloadButton
, &NewStuffButton::dialogFinished
, this, [this](const auto &changedEntries
) {
100 if (!changedEntries
.isEmpty()) {
101 m_serviceModel
->clear();
105 topLayout
->addWidget(downloadButton
);
108 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
109 std::sort(m_enabledVcsPlugins
.begin(), m_enabledVcsPlugins
.end());
112 ContextMenuSettingsPage::~ContextMenuSettingsPage() {
115 bool ContextMenuSettingsPage::entryVisible(const QString
& id
)
117 if (id
== "add_to_places") {
118 return ContextMenuSettings::showAddToPlaces();
119 } else if (id
== "sort") {
120 return ContextMenuSettings::showSortBy();
121 } else if (id
== "view_mode") {
122 return ContextMenuSettings::showViewMode();
123 } else if (id
== "open_in_new_tab") {
124 return ContextMenuSettings::showOpenInNewTab();
125 } else if (id
== "open_in_new_window") {
126 return ContextMenuSettings::showOpenInNewWindow();
127 } else if (id
== "copy_location") {
128 return ContextMenuSettings::showCopyLocation();
129 } else if (id
== "duplicate") {
130 return ContextMenuSettings::showDuplicateHere();
131 } else if (id
== "open_terminal_here") {
132 return ContextMenuSettings::showOpenTerminal();
137 void ContextMenuSettingsPage::setEntryVisible(const QString
& id
, bool visible
)
139 if (id
== "add_to_places") {
140 ContextMenuSettings::setShowAddToPlaces(visible
);
141 } else if (id
== "sort") {
142 ContextMenuSettings::setShowSortBy(visible
);
143 } else if (id
== "view_mode") {
144 ContextMenuSettings::setShowViewMode(visible
);
145 } else if (id
== "open_in_new_tab") {
146 ContextMenuSettings::setShowOpenInNewTab(visible
);
147 } else if (id
== "open_in_new_window") {
148 ContextMenuSettings::setShowOpenInNewWindow(visible
);
149 } else if (id
== "copy_location") {
150 ContextMenuSettings::setShowCopyLocation(visible
);
151 } else if (id
== "duplicate") {
152 ContextMenuSettings::setShowDuplicateHere(visible
);
153 } else if (id
== "open_terminal_here") {
154 ContextMenuSettings::setShowOpenTerminal(visible
);
158 void ContextMenuSettingsPage::applySettings()
160 if (!m_initialized
) {
164 KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
165 KConfigGroup showGroup
= config
.group("Show");
167 QStringList enabledPlugins
;
169 const QAbstractItemModel
*model
= m_listView
->model();
170 for (int i
= 0; i
< model
->rowCount(); ++i
) {
171 const QModelIndex index
= model
->index(i
, 0);
172 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
173 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
175 if (service
.startsWith(VersionControlServicePrefix
)) {
177 enabledPlugins
.append(model
->data(index
, Qt::DisplayRole
).toString());
179 } else if (service
== QLatin1String(DeleteService
)) {
180 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals
);
181 KConfigGroup
configGroup(globalConfig
, "KDE");
182 configGroup
.writeEntry("ShowDeleteCommand", checked
);
184 } else if (service
== QLatin1String(CopyToMoveToService
)) {
185 ContextMenuSettings::setShowCopyMoveMenu(checked
);
186 ContextMenuSettings::self()->save();
187 } else if (m_actionIds
.contains(service
)) {
188 setEntryVisible(service
, checked
);
189 ContextMenuSettings::self()->save();
191 showGroup
.writeEntry(service
, checked
);
197 if (m_enabledVcsPlugins
!= enabledPlugins
) {
198 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
199 VersionControlSettings::self()->save();
201 if (!laterSelected
) {
202 KMessageBox::ButtonCode promptRestart
= KMessageBox::questionYesNo(window(),
203 i18nc("@info", "Dolphin must be restarted to apply the "
204 "updated version control system settings."),
205 i18nc("@info", "Restart now?"),
206 KGuiItem(QApplication::translate("KStandardGuiItem", "&Restart"), QStringLiteral("dialog-restart")),
207 KGuiItem(QApplication::translate("KStandardGuiItem", "&Later"), QStringLiteral("dialog-later"))
209 if (promptRestart
== KMessageBox::ButtonCode::Yes
) {
210 Dolphin::openNewWindow();
213 laterSelected
= true;
219 void ContextMenuSettingsPage::restoreDefaults()
221 QAbstractItemModel
* model
= m_listView
->model();
222 for (int i
= 0; i
< model
->rowCount(); ++i
) {
223 const QModelIndex index
= model
->index(i
, 0);
224 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
226 const bool checked
= !service
.startsWith(VersionControlServicePrefix
)
227 && service
!= QLatin1String(DeleteService
)
228 && service
!= QLatin1String(CopyToMoveToService
);
229 model
->setData(index
, checked
, Qt::CheckStateRole
);
233 void ContextMenuSettingsPage::showEvent(QShowEvent
* event
)
235 if (!event
->spontaneous() && !m_initialized
) {
238 loadVersionControlSystems();
240 // Add "Show 'Delete' command" as service
241 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals
);
242 KConfigGroup
configGroup(globalConfig
, "KDE");
243 addRow(QStringLiteral("edit-delete"),
244 i18nc("@option:check", "Delete"),
246 configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
248 // Add "Show 'Copy To' and 'Move To' commands" as service
249 addRow(QStringLiteral("edit-copy"),
250 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
252 ContextMenuSettings::showCopyMoveMenu());
255 // Add other built-in actions
256 for (const QString
& id
: m_actionIds
) {
257 const QAction
* action
= m_actions
->action(id
);
259 addRow(action
->icon().name(), action
->text(), id
, entryVisible(id
));
264 m_sortModel
->sort(Qt::DisplayRole
);
266 m_initialized
= true;
268 SettingsPageBase::showEvent(event
);
271 void ContextMenuSettingsPage::loadServices()
273 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
274 const KConfigGroup showGroup
= config
.group("Show");
276 // Load generic services
277 const auto locations
= QStandardPaths::locateAll(QStandardPaths::GenericDataLocation
, QStringLiteral("kio/servicemenus"), QStandardPaths::LocateDirectory
);
278 QStringList files
= KFileUtils::findAllUniqueFiles(locations
);
280 #if KIOWIDGETS_BUILD_DEPRECATED_SINCE(5, 90)
281 const KService::List services
= KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
282 for (const KService::Ptr
&service
: services
) {
283 files
<< QStandardPaths::locate(QStandardPaths::GenericDataLocation
, "kservices5/" % service
->entryPath());
287 for (const auto &file
: qAsConst(files
)) {
288 const QList
<KServiceAction
> serviceActions
= KDesktopFileActions::userDefinedServices(KService(file
), true);
290 const KDesktopFile
desktopFile(file
);
291 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
293 for (const KServiceAction
&action
: serviceActions
) {
294 const QString serviceName
= action
.name();
295 const bool addService
= !action
.noDisplay() && !action
.isSeparator() && !isInServicesList(serviceName
);
298 const QString itemName
= subMenuName
.isEmpty()
300 : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
301 const bool checked
= showGroup
.readEntry(serviceName
, true);
302 addRow(action
.icon(), itemName
, serviceName
, checked
);
307 // Load service plugins, this is deprecated in KIO 5.82
308 #if KIOCORE_BUILD_DEPRECATED_SINCE(5, 82)
309 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
310 for (const KService::Ptr
&service
: pluginServices
) {
311 const QString desktopEntryName
= service
->desktopEntryName();
312 if (!isInServicesList(desktopEntryName
)) {
313 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
314 addRow(service
->icon(), service
->name(), desktopEntryName
, checked
);
319 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
320 const auto jsonPlugins
= KPluginMetaData::findPlugins(QStringLiteral("kf" QT_STRINGIFY(QT_VERSION_MAJOR
)) + QStringLiteral("/kfileitemaction"));
322 for (const auto &jsonMetadata
: jsonPlugins
) {
323 const QString desktopEntryName
= jsonMetadata
.pluginId();
324 if (!isInServicesList(desktopEntryName
)) {
325 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
326 addRow(jsonMetadata
.iconName(), jsonMetadata
.name(), desktopEntryName
, checked
);
330 m_sortModel
->sort(Qt::DisplayRole
);
331 m_searchLineEdit
->setFocus(Qt::OtherFocusReason
);
334 void ContextMenuSettingsPage::loadVersionControlSystems()
336 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
338 // Create a checkbox for each available version control plugin
339 QSet
<QString
> loadedPlugins
;
341 const QVector
<KPluginMetaData
> plugins
= KPluginMetaData::findPlugins(QStringLiteral("dolphin/vcs"));
342 for (const auto &plugin
: plugins
) {
343 const QString pluginName
= plugin
.name();
344 addRow(QStringLiteral("code-class"),
346 VersionControlServicePrefix
+ pluginName
,
347 enabledPlugins
.contains(pluginName
));
348 loadedPlugins
+= pluginName
;
351 m_sortModel
->sort(Qt::DisplayRole
);
354 bool ContextMenuSettingsPage::isInServicesList(const QString
&service
) const
356 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
357 const QModelIndex index
= m_serviceModel
->index(i
, 0);
358 if (m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
365 void ContextMenuSettingsPage::addRow(const QString
&icon
,
367 const QString
&value
,
370 m_serviceModel
->insertRow(0);
372 const QModelIndex index
= m_serviceModel
->index(0, 0);
373 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
374 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
375 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
376 m_serviceModel
->setData(index
, checked
, Qt::CheckStateRole
);