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();
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_here") {
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 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
202 KMessageBox::ButtonCode promptRestart
=
203 KMessageBox::questionTwoActions(window(),
205 KMessageBox::ButtonCode promptRestart
=
206 KMessageBox::questionYesNo(window(),
209 "Dolphin must be restarted to apply the "
210 "updated version control system settings."),
211 i18nc("@info", "Restart now?"),
212 KGuiItem(QApplication::translate("KStandardGuiItem", "&Restart"), QStringLiteral("dialog-restart")),
213 KGuiItem(QApplication::translate("KStandardGuiItem", "&Later"), QStringLiteral("dialog-later")));
214 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
215 if (promptRestart
== KMessageBox::ButtonCode::PrimaryAction
) {
217 if (promptRestart
== KMessageBox::ButtonCode::Yes
) {
219 Dolphin::openNewWindow();
222 laterSelected
= true;
228 void ContextMenuSettingsPage::restoreDefaults()
230 QAbstractItemModel
*model
= m_listView
->model();
231 for (int i
= 0; i
< model
->rowCount(); ++i
) {
232 const QModelIndex index
= model
->index(i
, 0);
233 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
236 !service
.startsWith(VersionControlServicePrefix
) && service
!= QLatin1String(DeleteService
) && service
!= QLatin1String(CopyToMoveToService
);
237 model
->setData(index
, checked
, Qt::CheckStateRole
);
241 void ContextMenuSettingsPage::showEvent(QShowEvent
*event
)
243 if (!event
->spontaneous() && !m_initialized
) {
246 loadVersionControlSystems();
248 // Add "Show 'Delete' command" as service
249 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals
);
250 KConfigGroup
configGroup(globalConfig
, "KDE");
251 addRow(QStringLiteral("edit-delete"), i18nc("@option:check", "Delete"), DeleteService
, configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
253 // Add "Show 'Copy To' and 'Move To' commands" as service
254 addRow(QStringLiteral("edit-copy"),
255 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
257 ContextMenuSettings::showCopyMoveMenu());
260 // Add other built-in actions
261 for (const QString
&id
: m_actionIds
) {
262 const QAction
*action
= m_actions
->action(id
);
264 addRow(action
->icon().name(), action
->text(), id
, entryVisible(id
));
269 m_sortModel
->sort(Qt::DisplayRole
);
271 m_initialized
= true;
273 SettingsPageBase::showEvent(event
);
276 void ContextMenuSettingsPage::loadServices()
278 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
279 const KConfigGroup showGroup
= config
.group("Show");
281 // Load generic services
282 const auto locations
= QStandardPaths::locateAll(QStandardPaths::GenericDataLocation
, QStringLiteral("kio/servicemenus"), QStandardPaths::LocateDirectory
);
283 QStringList files
= KFileUtils::findAllUniqueFiles(locations
);
285 #if KIOWIDGETS_BUILD_DEPRECATED_SINCE(5, 90)
286 const KService::List services
= KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
287 for (const KService::Ptr
&service
: services
) {
288 files
<< QStandardPaths::locate(QStandardPaths::GenericDataLocation
, "kservices5/" % service
->entryPath());
292 for (const auto &file
: qAsConst(files
)) {
293 const QList
<KServiceAction
> serviceActions
= KDesktopFileActions::userDefinedServices(KService(file
), true);
295 const KDesktopFile
desktopFile(file
);
296 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
298 for (const KServiceAction
&action
: serviceActions
) {
299 const QString serviceName
= action
.name();
300 const bool addService
= !action
.noDisplay() && !action
.isSeparator() && !isInServicesList(serviceName
);
303 const QString itemName
= subMenuName
.isEmpty() ? action
.text() : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
304 const bool checked
= showGroup
.readEntry(serviceName
, true);
305 addRow(action
.icon(), itemName
, serviceName
, checked
);
310 // Load service plugins, this is deprecated in KIO 5.82
311 #if KIOCORE_BUILD_DEPRECATED_SINCE(5, 82)
312 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
313 for (const KService::Ptr
&service
: pluginServices
) {
314 const QString desktopEntryName
= service
->desktopEntryName();
315 if (!isInServicesList(desktopEntryName
)) {
316 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
317 addRow(service
->icon(), service
->name(), desktopEntryName
, checked
);
322 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
323 const auto jsonPlugins
= KPluginMetaData::findPlugins(QStringLiteral("kf" QT_STRINGIFY(QT_VERSION_MAJOR
)) + QStringLiteral("/kfileitemaction"));
325 for (const auto &jsonMetadata
: jsonPlugins
) {
326 const QString desktopEntryName
= jsonMetadata
.pluginId();
327 if (!isInServicesList(desktopEntryName
)) {
328 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
329 addRow(jsonMetadata
.iconName(), jsonMetadata
.name(), desktopEntryName
, checked
);
333 m_sortModel
->sort(Qt::DisplayRole
);
334 m_searchLineEdit
->setFocus(Qt::OtherFocusReason
);
337 void ContextMenuSettingsPage::loadVersionControlSystems()
339 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
341 // Create a checkbox for each available version control plugin
342 QSet
<QString
> loadedPlugins
;
344 const QVector
<KPluginMetaData
> plugins
= KPluginMetaData::findPlugins(QStringLiteral("dolphin/vcs"));
345 for (const auto &plugin
: plugins
) {
346 const QString pluginName
= plugin
.name();
347 addRow(QStringLiteral("code-class"), pluginName
, VersionControlServicePrefix
+ pluginName
, 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
, const QString
&text
, const QString
&value
, bool checked
)
367 m_serviceModel
->insertRow(0);
369 const QModelIndex index
= m_serviceModel
->index(0, 0);
370 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
371 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
372 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
373 m_serviceModel
->setData(index
, checked
, Qt::CheckStateRole
);