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_versioncontrolsettings.h"
12 #include "settings/servicemodel.h"
14 #include <KDesktopFile>
15 #include <KDesktopFileActions>
17 #include <KLocalizedString>
18 #include <KMessageBox>
19 #include <KPluginMetaData>
21 #include <kiocore_export.h>
22 #include <kservice_export.h>
23 #include <kwidgetsaddons_version.h>
25 #include <KNSWidgets/Button>
28 #include <QApplication>
29 #include <QGridLayout>
32 #include <QListWidget>
35 #include <QSortFilterProxyModel>
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
, const KActionCollection
*actions
, const QStringList
&actionIds
)
48 : SettingsPageBase(parent
)
49 , m_initialized(false)
50 , m_serviceModel(nullptr)
51 , m_sortModel(nullptr)
53 , m_enabledVcsPlugins()
55 , m_actionIds(actionIds
)
57 QVBoxLayout
*topLayout
= new QVBoxLayout(this);
59 QLabel
*label
= new QLabel(i18nc("@label:textbox",
60 "Select which services should "
61 "be shown in the context menu:"),
63 label
->setWordWrap(true);
64 m_searchLineEdit
= new QLineEdit(this);
65 m_searchLineEdit
->setPlaceholderText(i18nc("@label:textbox", "Search…"));
66 connect(m_searchLineEdit
, &QLineEdit::textChanged
, this, [this](const QString
&filter
) {
67 m_sortModel
->setFilterFixedString(filter
);
70 m_listView
= new QListView(this);
71 QScroller::grabGesture(m_listView
->viewport(), QScroller::TouchGesture
);
73 m_serviceModel
= new ServiceModel(this);
74 m_sortModel
= new QSortFilterProxyModel(this);
75 m_sortModel
->setSourceModel(m_serviceModel
);
76 m_sortModel
->setSortRole(Qt::DisplayRole
);
77 m_sortModel
->setSortLocaleAware(true);
78 m_sortModel
->setFilterRole(Qt::DisplayRole
);
79 m_sortModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
80 m_listView
->setModel(m_sortModel
);
81 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
82 connect(m_listView
, &QListView::clicked
, this, &ContextMenuSettingsPage::changed
);
84 topLayout
->addWidget(label
);
85 topLayout
->addWidget(m_searchLineEdit
);
86 topLayout
->addWidget(m_listView
);
89 using NewStuffButton
= KNSWidgets::Button
;
90 auto *downloadButton
= new NewStuffButton(i18nc("@action:button", "Download New Services…"), QStringLiteral("servicemenu.knsrc"), this);
91 connect(downloadButton
, &NewStuffButton::dialogFinished
, this, [this](const auto &changedEntries
) {
92 if (!changedEntries
.isEmpty()) {
93 m_serviceModel
->clear();
97 topLayout
->addWidget(downloadButton
);
100 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
101 std::sort(m_enabledVcsPlugins
.begin(), m_enabledVcsPlugins
.end());
104 ContextMenuSettingsPage::~ContextMenuSettingsPage()
108 bool ContextMenuSettingsPage::entryVisible(const QString
&id
)
110 if (id
== "add_to_places") {
111 return ContextMenuSettings::showAddToPlaces();
112 } else if (id
== "sort") {
113 return ContextMenuSettings::showSortBy();
114 } else if (id
== "view_mode") {
115 return ContextMenuSettings::showViewMode();
116 } else if (id
== "open_in_new_tab") {
117 return ContextMenuSettings::showOpenInNewTab();
118 } else if (id
== "open_in_new_window") {
119 return ContextMenuSettings::showOpenInNewWindow();
120 } else if (id
== "open_in_split_view") {
121 return ContextMenuSettings::showOpenInSplitView();
122 } else if (id
== "copy_location") {
123 return ContextMenuSettings::showCopyLocation();
124 } else if (id
== "duplicate") {
125 return ContextMenuSettings::showDuplicateHere();
126 } else if (id
== "open_terminal_here") {
127 return ContextMenuSettings::showOpenTerminal();
128 } else if (id
== "copy_to_inactive_split_view") {
129 return ContextMenuSettings::showCopyToOtherSplitView();
130 } else if (id
== "move_to_inactive_split_view") {
131 return ContextMenuSettings::showMoveToOtherSplitView();
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
== "open_in_split_view") {
149 return ContextMenuSettings::setShowOpenInSplitView(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
).value
<Qt::CheckState
>() == Qt::Checked
;
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 KMessageBox::ButtonCode promptRestart
=
208 KMessageBox::questionTwoActions(window(),
210 "Dolphin must be restarted to apply the "
211 "updated version control system settings."),
212 i18nc("@info", "Restart now?"),
213 KGuiItem(QApplication::translate("KStandardGuiItem", "&Restart"), QStringLiteral("dialog-restart")),
214 KGuiItem(QApplication::translate("KStandardGuiItem", "&Later"), QStringLiteral("dialog-later")));
215 if (promptRestart
== KMessageBox::ButtonCode::PrimaryAction
) {
216 Dolphin::openNewWindow();
219 laterSelected
= true;
225 void ContextMenuSettingsPage::restoreDefaults()
227 QAbstractItemModel
*model
= m_listView
->model();
228 for (int i
= 0; i
< model
->rowCount(); ++i
) {
229 const QModelIndex index
= model
->index(i
, 0);
230 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
233 !service
.startsWith(VersionControlServicePrefix
) && service
!= QLatin1String(DeleteService
) && service
!= QLatin1String(CopyToMoveToService
);
234 model
->setData(index
, checked
? Qt::Checked
: Qt::Unchecked
, Qt::CheckStateRole
);
238 void ContextMenuSettingsPage::showEvent(QShowEvent
*event
)
240 if (!event
->spontaneous() && !m_initialized
) {
243 loadVersionControlSystems();
245 // Add "Show 'Delete' command" as service
246 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals
);
247 KConfigGroup
configGroup(globalConfig
, "KDE");
248 addRow(QStringLiteral("edit-delete"), i18nc("@option:check", "Delete"), DeleteService
, configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
250 // Add "Show 'Copy To' and 'Move To' commands" as service
251 addRow(QStringLiteral("edit-copy"),
252 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
254 ContextMenuSettings::showCopyMoveMenu());
257 // Add other built-in actions
258 for (const QString
&id
: m_actionIds
) {
259 const QAction
*action
= m_actions
->action(id
);
261 addRow(action
->icon().name(), KLocalizedString::removeAcceleratorMarker(action
->text()), id
, entryVisible(id
));
266 m_sortModel
->sort(Qt::DisplayRole
);
268 m_initialized
= true;
270 SettingsPageBase::showEvent(event
);
273 void ContextMenuSettingsPage::loadServices()
275 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
276 const KConfigGroup showGroup
= config
.group("Show");
278 // Load generic services
279 const auto locations
= QStandardPaths::locateAll(QStandardPaths::GenericDataLocation
, QStringLiteral("kio/servicemenus"), QStandardPaths::LocateDirectory
);
280 QStringList files
= KFileUtils::findAllUniqueFiles(locations
);
282 for (const auto &file
: std::as_const(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() ? action
.text() : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
294 const bool checked
= showGroup
.readEntry(serviceName
, true);
295 addRow(action
.icon(), itemName
, serviceName
, checked
);
300 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
301 const auto jsonPlugins
= KPluginMetaData::findPlugins(QStringLiteral("kf6/kfileitemaction"));
303 for (const auto &jsonMetadata
: jsonPlugins
) {
304 const QString desktopEntryName
= jsonMetadata
.pluginId();
305 if (!isInServicesList(desktopEntryName
)) {
306 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
307 addRow(jsonMetadata
.iconName(), jsonMetadata
.name(), desktopEntryName
, checked
);
311 m_sortModel
->sort(Qt::DisplayRole
);
312 m_searchLineEdit
->setFocus(Qt::OtherFocusReason
);
315 void ContextMenuSettingsPage::loadVersionControlSystems()
317 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
319 // Create a checkbox for each available version control plugin
320 QSet
<QString
> loadedPlugins
;
322 const QVector
<KPluginMetaData
> plugins
= KPluginMetaData::findPlugins(QStringLiteral("dolphin/vcs"));
323 for (const auto &plugin
: plugins
) {
324 const QString pluginName
= plugin
.name();
325 addRow(QStringLiteral("code-class"), pluginName
, VersionControlServicePrefix
+ pluginName
, enabledPlugins
.contains(pluginName
));
326 loadedPlugins
+= pluginName
;
329 m_sortModel
->sort(Qt::DisplayRole
);
332 bool ContextMenuSettingsPage::isInServicesList(const QString
&service
) const
334 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
335 const QModelIndex index
= m_serviceModel
->index(i
, 0);
336 if (m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
343 void ContextMenuSettingsPage::addRow(const QString
&icon
, const QString
&text
, const QString
&value
, bool checked
)
345 m_serviceModel
->insertRow(0);
347 const QModelIndex index
= m_serviceModel
->index(0, 0);
348 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
349 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
350 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
351 m_serviceModel
->setData(index
, checked
? Qt::Checked
: Qt::Unchecked
, Qt::CheckStateRole
);
354 #include "moc_contextmenusettingspage.cpp"