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 <kiocore_export.h>
24 #include <kservice_export.h>
25 #include <kwidgetsaddons_version.h>
27 #include <KNSWidgets/Button>
30 #include <QApplication>
31 #include <QGridLayout>
34 #include <QListWidget>
37 #include <QSortFilterProxyModel>
41 const bool ShowDeleteDefault
= false;
42 const char VersionControlServicePrefix
[] = "_version_control_";
43 const char DeleteService
[] = "_delete";
44 const char CopyToMoveToService
[] = "_copy_to_move_to";
46 bool laterSelected
= false;
49 ContextMenuSettingsPage::ContextMenuSettingsPage(QWidget
*parent
, const KActionCollection
*actions
, const QStringList
&actionIds
)
50 : SettingsPageBase(parent
)
51 , m_initialized(false)
52 , m_serviceModel(nullptr)
53 , m_sortModel(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:"),
65 label
->setWordWrap(true);
66 m_searchLineEdit
= new QLineEdit(this);
67 m_searchLineEdit
->setPlaceholderText(i18nc("@label:textbox", "Search…"));
68 connect(m_searchLineEdit
, &QLineEdit::textChanged
, this, [this](const QString
&filter
) {
69 m_sortModel
->setFilterFixedString(filter
);
72 m_listView
= new QListView(this);
73 QScroller::grabGesture(m_listView
->viewport(), QScroller::TouchGesture
);
75 auto *delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
76 m_serviceModel
= new ServiceModel(this);
77 m_sortModel
= new QSortFilterProxyModel(this);
78 m_sortModel
->setSourceModel(m_serviceModel
);
79 m_sortModel
->setSortRole(Qt::DisplayRole
);
80 m_sortModel
->setSortLocaleAware(true);
81 m_sortModel
->setFilterRole(Qt::DisplayRole
);
82 m_sortModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
83 m_listView
->setModel(m_sortModel
);
84 m_listView
->setItemDelegate(delegate
);
85 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
86 connect(m_listView
, &QListView::clicked
, this, &ContextMenuSettingsPage::changed
);
88 topLayout
->addWidget(label
);
89 topLayout
->addWidget(m_searchLineEdit
);
90 topLayout
->addWidget(m_listView
);
93 using NewStuffButton
= KNSWidgets::Button
;
94 auto *downloadButton
= new NewStuffButton(i18nc("@action:button", "Download New Services…"), QStringLiteral("servicemenu.knsrc"), this);
95 connect(downloadButton
, &NewStuffButton::dialogFinished
, this, [this](const auto &changedEntries
) {
96 if (!changedEntries
.isEmpty()) {
97 m_serviceModel
->clear();
101 topLayout
->addWidget(downloadButton
);
104 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
105 std::sort(m_enabledVcsPlugins
.begin(), m_enabledVcsPlugins
.end());
108 ContextMenuSettingsPage::~ContextMenuSettingsPage()
112 bool ContextMenuSettingsPage::entryVisible(const QString
&id
)
114 if (id
== "add_to_places") {
115 return ContextMenuSettings::showAddToPlaces();
116 } else if (id
== "sort") {
117 return ContextMenuSettings::showSortBy();
118 } else if (id
== "view_mode") {
119 return ContextMenuSettings::showViewMode();
120 } else if (id
== "open_in_new_tab") {
121 return ContextMenuSettings::showOpenInNewTab();
122 } else if (id
== "open_in_new_window") {
123 return ContextMenuSettings::showOpenInNewWindow();
124 } else if (id
== "copy_location") {
125 return ContextMenuSettings::showCopyLocation();
126 } else if (id
== "duplicate") {
127 return ContextMenuSettings::showDuplicateHere();
128 } else if (id
== "open_terminal_here") {
129 return ContextMenuSettings::showOpenTerminal();
130 } else if (id
== "copy_to_inactive_split_view") {
131 return ContextMenuSettings::showCopyToOtherSplitView();
132 } else if (id
== "move_to_inactive_split_view") {
133 return ContextMenuSettings::showMoveToOtherSplitView();
138 void ContextMenuSettingsPage::setEntryVisible(const QString
&id
, bool visible
)
140 if (id
== "add_to_places") {
141 ContextMenuSettings::setShowAddToPlaces(visible
);
142 } else if (id
== "sort") {
143 ContextMenuSettings::setShowSortBy(visible
);
144 } else if (id
== "view_mode") {
145 ContextMenuSettings::setShowViewMode(visible
);
146 } else if (id
== "open_in_new_tab") {
147 ContextMenuSettings::setShowOpenInNewTab(visible
);
148 } else if (id
== "open_in_new_window") {
149 ContextMenuSettings::setShowOpenInNewWindow(visible
);
150 } else if (id
== "copy_location") {
151 ContextMenuSettings::setShowCopyLocation(visible
);
152 } else if (id
== "duplicate") {
153 ContextMenuSettings::setShowDuplicateHere(visible
);
154 } else if (id
== "open_terminal_here") {
155 ContextMenuSettings::setShowOpenTerminal(visible
);
156 } else if (id
== "copy_to_inactive_split_view") {
157 ContextMenuSettings::setShowCopyToOtherSplitView(visible
);
158 } else if (id
== "move_to_inactive_split_view") {
159 ContextMenuSettings::setShowMoveToOtherSplitView(visible
);
163 void ContextMenuSettingsPage::applySettings()
165 if (!m_initialized
) {
169 KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
170 KConfigGroup showGroup
= config
.group("Show");
172 QStringList enabledPlugins
;
174 const QAbstractItemModel
*model
= m_listView
->model();
175 for (int i
= 0; i
< model
->rowCount(); ++i
) {
176 const QModelIndex index
= model
->index(i
, 0);
177 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
178 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
180 if (service
.startsWith(VersionControlServicePrefix
)) {
182 enabledPlugins
.append(model
->data(index
, Qt::DisplayRole
).toString());
184 } else if (service
== QLatin1String(DeleteService
)) {
185 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals
);
186 KConfigGroup
configGroup(globalConfig
, "KDE");
187 configGroup
.writeEntry("ShowDeleteCommand", checked
);
189 } else if (service
== QLatin1String(CopyToMoveToService
)) {
190 ContextMenuSettings::setShowCopyMoveMenu(checked
);
191 ContextMenuSettings::self()->save();
192 } else if (m_actionIds
.contains(service
)) {
193 setEntryVisible(service
, checked
);
194 ContextMenuSettings::self()->save();
196 showGroup
.writeEntry(service
, checked
);
202 if (m_enabledVcsPlugins
!= enabledPlugins
) {
203 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
204 VersionControlSettings::self()->save();
206 if (!laterSelected
) {
207 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
208 KMessageBox::ButtonCode promptRestart
=
209 KMessageBox::questionTwoActions(window(),
211 KMessageBox::ButtonCode promptRestart
=
212 KMessageBox::questionYesNo(window(),
215 "Dolphin must be restarted to apply the "
216 "updated version control system settings."),
217 i18nc("@info", "Restart now?"),
218 KGuiItem(QApplication::translate("KStandardGuiItem", "&Restart"), QStringLiteral("dialog-restart")),
219 KGuiItem(QApplication::translate("KStandardGuiItem", "&Later"), QStringLiteral("dialog-later")));
220 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
221 if (promptRestart
== KMessageBox::ButtonCode::PrimaryAction
) {
223 if (promptRestart
== KMessageBox::ButtonCode::Yes
) {
225 Dolphin::openNewWindow();
228 laterSelected
= true;
234 void ContextMenuSettingsPage::restoreDefaults()
236 QAbstractItemModel
*model
= m_listView
->model();
237 for (int i
= 0; i
< model
->rowCount(); ++i
) {
238 const QModelIndex index
= model
->index(i
, 0);
239 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
242 !service
.startsWith(VersionControlServicePrefix
) && service
!= QLatin1String(DeleteService
) && service
!= QLatin1String(CopyToMoveToService
);
243 model
->setData(index
, checked
, Qt::CheckStateRole
);
247 void ContextMenuSettingsPage::showEvent(QShowEvent
*event
)
249 if (!event
->spontaneous() && !m_initialized
) {
252 loadVersionControlSystems();
254 // Add "Show 'Delete' command" as service
255 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals
);
256 KConfigGroup
configGroup(globalConfig
, "KDE");
257 addRow(QStringLiteral("edit-delete"), i18nc("@option:check", "Delete"), DeleteService
, configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
259 // Add "Show 'Copy To' and 'Move To' commands" as service
260 addRow(QStringLiteral("edit-copy"),
261 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
263 ContextMenuSettings::showCopyMoveMenu());
266 // Add other built-in actions
267 for (const QString
&id
: m_actionIds
) {
268 const QAction
*action
= m_actions
->action(id
);
270 addRow(action
->icon().name(), action
->text(), id
, entryVisible(id
));
275 m_sortModel
->sort(Qt::DisplayRole
);
277 m_initialized
= true;
279 SettingsPageBase::showEvent(event
);
282 void ContextMenuSettingsPage::loadServices()
284 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
285 const KConfigGroup showGroup
= config
.group("Show");
287 // Load generic services
288 const auto locations
= QStandardPaths::locateAll(QStandardPaths::GenericDataLocation
, QStringLiteral("kio/servicemenus"), QStandardPaths::LocateDirectory
);
289 QStringList files
= KFileUtils::findAllUniqueFiles(locations
);
291 for (const auto &file
: qAsConst(files
)) {
292 const QList
<KServiceAction
> serviceActions
= KDesktopFileActions::userDefinedServices(KService(file
), true);
294 const KDesktopFile
desktopFile(file
);
295 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
297 for (const KServiceAction
&action
: serviceActions
) {
298 const QString serviceName
= action
.name();
299 const bool addService
= !action
.noDisplay() && !action
.isSeparator() && !isInServicesList(serviceName
);
302 const QString itemName
= subMenuName
.isEmpty() ? action
.text() : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
303 const bool checked
= showGroup
.readEntry(serviceName
, true);
304 addRow(action
.icon(), itemName
, serviceName
, checked
);
309 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
310 const auto jsonPlugins
= KPluginMetaData::findPlugins(QStringLiteral("kf6/kfileitemaction"));
312 for (const auto &jsonMetadata
: jsonPlugins
) {
313 const QString desktopEntryName
= jsonMetadata
.pluginId();
314 if (!isInServicesList(desktopEntryName
)) {
315 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
316 addRow(jsonMetadata
.iconName(), jsonMetadata
.name(), desktopEntryName
, checked
);
320 m_sortModel
->sort(Qt::DisplayRole
);
321 m_searchLineEdit
->setFocus(Qt::OtherFocusReason
);
324 void ContextMenuSettingsPage::loadVersionControlSystems()
326 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
328 // Create a checkbox for each available version control plugin
329 QSet
<QString
> loadedPlugins
;
331 const QVector
<KPluginMetaData
> plugins
= KPluginMetaData::findPlugins(QStringLiteral("dolphin/vcs"));
332 for (const auto &plugin
: plugins
) {
333 const QString pluginName
= plugin
.name();
334 addRow(QStringLiteral("code-class"), pluginName
, VersionControlServicePrefix
+ pluginName
, enabledPlugins
.contains(pluginName
));
335 loadedPlugins
+= pluginName
;
338 m_sortModel
->sort(Qt::DisplayRole
);
341 bool ContextMenuSettingsPage::isInServicesList(const QString
&service
) const
343 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
344 const QModelIndex index
= m_serviceModel
->index(i
, 0);
345 if (m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
352 void ContextMenuSettingsPage::addRow(const QString
&icon
, const QString
&text
, const QString
&value
, bool checked
)
354 m_serviceModel
->insertRow(0);
356 const QModelIndex index
= m_serviceModel
->index(0, 0);
357 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
358 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
359 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
360 m_serviceModel
->setData(index
, checked
, Qt::CheckStateRole
);
363 #include "moc_contextmenusettingspage.cpp"