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>
16 #include <KLocalizedString>
17 #include <KMessageBox>
18 #include <KPluginMetaData>
20 #include <kiocore_export.h>
21 #include <kservice_export.h>
22 #include <kwidgetsaddons_version.h>
24 #include <KNSWidgets/Button>
27 #include <QApplication>
28 #include <QGridLayout>
31 #include <QListWidget>
34 #include <QSortFilterProxyModel>
38 const bool ShowDeleteDefault
= false;
39 const char VersionControlServicePrefix
[] = "_version_control_";
40 const char DeleteService
[] = "_delete";
41 const char CopyToMoveToService
[] = "_copy_to_move_to";
43 bool laterSelected
= false;
46 ContextMenuSettingsPage::ContextMenuSettingsPage(QWidget
*parent
, const KActionCollection
*actions
, const QStringList
&actionIds
)
47 : SettingsPageBase(parent
)
48 , m_initialized(false)
49 , m_serviceModel(nullptr)
50 , m_sortModel(nullptr)
52 , m_enabledVcsPlugins()
54 , m_actionIds(actionIds
)
56 QVBoxLayout
*topLayout
= new QVBoxLayout(this);
58 QLabel
*label
= new QLabel(i18nc("@label:textbox",
59 "Select which services should "
60 "be shown in the context menu:"),
62 label
->setWordWrap(true);
63 m_searchLineEdit
= new QLineEdit(this);
64 m_searchLineEdit
->setPlaceholderText(i18nc("@label:textbox", "Search…"));
65 connect(m_searchLineEdit
, &QLineEdit::textChanged
, this, [this](const QString
&filter
) {
66 m_sortModel
->setFilterFixedString(filter
);
69 m_listView
= new QListView(this);
70 QScroller::grabGesture(m_listView
->viewport(), QScroller::TouchGesture
);
72 m_serviceModel
= new ServiceModel(this);
73 m_sortModel
= new QSortFilterProxyModel(this);
74 m_sortModel
->setSourceModel(m_serviceModel
);
75 m_sortModel
->setSortRole(Qt::DisplayRole
);
76 m_sortModel
->setSortLocaleAware(true);
77 m_sortModel
->setFilterRole(Qt::DisplayRole
);
78 m_sortModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
79 m_listView
->setModel(m_sortModel
);
80 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
81 connect(m_listView
, &QListView::clicked
, this, &ContextMenuSettingsPage::changed
);
83 topLayout
->addWidget(label
);
84 topLayout
->addWidget(m_searchLineEdit
);
85 topLayout
->addWidget(m_listView
);
88 using NewStuffButton
= KNSWidgets::Button
;
89 auto *downloadButton
= new NewStuffButton(i18nc("@action:button", "Download New Services…"), QStringLiteral("servicemenu.knsrc"), this);
90 connect(downloadButton
, &NewStuffButton::dialogFinished
, this, [this](const auto &changedEntries
) {
91 if (!changedEntries
.isEmpty()) {
92 m_serviceModel
->clear();
96 topLayout
->addWidget(downloadButton
);
99 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
100 std::sort(m_enabledVcsPlugins
.begin(), m_enabledVcsPlugins
.end());
103 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
== "open_in_split_view") {
120 return ContextMenuSettings::showOpenInSplitView();
121 } else if (id
== "copy_location") {
122 return ContextMenuSettings::showCopyLocation();
123 } else if (id
== "duplicate") {
124 return ContextMenuSettings::showDuplicateHere();
125 } else if (id
== "open_terminal_here") {
126 return ContextMenuSettings::showOpenTerminal();
127 } else if (id
== "copy_to_inactive_split_view") {
128 return ContextMenuSettings::showCopyToOtherSplitView();
129 } else if (id
== "move_to_inactive_split_view") {
130 return ContextMenuSettings::showMoveToOtherSplitView();
135 void ContextMenuSettingsPage::setEntryVisible(const QString
&id
, bool visible
)
137 if (id
== "add_to_places") {
138 ContextMenuSettings::setShowAddToPlaces(visible
);
139 } else if (id
== "sort") {
140 ContextMenuSettings::setShowSortBy(visible
);
141 } else if (id
== "view_mode") {
142 ContextMenuSettings::setShowViewMode(visible
);
143 } else if (id
== "open_in_new_tab") {
144 ContextMenuSettings::setShowOpenInNewTab(visible
);
145 } else if (id
== "open_in_new_window") {
146 ContextMenuSettings::setShowOpenInNewWindow(visible
);
147 } else if (id
== "open_in_split_view") {
148 return ContextMenuSettings::setShowOpenInSplitView(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
);
155 } else if (id
== "copy_to_inactive_split_view") {
156 ContextMenuSettings::setShowCopyToOtherSplitView(visible
);
157 } else if (id
== "move_to_inactive_split_view") {
158 ContextMenuSettings::setShowMoveToOtherSplitView(visible
);
162 void ContextMenuSettingsPage::applySettings()
164 if (!m_initialized
) {
168 KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
169 KConfigGroup showGroup
= config
.group("Show");
171 QStringList enabledPlugins
;
173 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
174 const QModelIndex index
= m_serviceModel
->index(i
, 0);
175 const QString service
= m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
176 const bool checked
= m_serviceModel
->data(index
, Qt::CheckStateRole
).value
<Qt::CheckState
>() == Qt::Checked
;
178 if (service
.startsWith(VersionControlServicePrefix
)) {
180 enabledPlugins
.append(m_serviceModel
->data(index
, Qt::DisplayRole
).toString());
182 } else if (service
== QLatin1String(DeleteService
)) {
183 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals
);
184 KConfigGroup
configGroup(globalConfig
, "KDE");
185 configGroup
.writeEntry("ShowDeleteCommand", checked
);
187 } else if (service
== QLatin1String(CopyToMoveToService
)) {
188 ContextMenuSettings::setShowCopyMoveMenu(checked
);
189 ContextMenuSettings::self()->save();
190 } else if (m_actionIds
.contains(service
)) {
191 setEntryVisible(service
, checked
);
192 ContextMenuSettings::self()->save();
194 showGroup
.writeEntry(service
, checked
);
200 if (m_enabledVcsPlugins
!= enabledPlugins
) {
201 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
202 VersionControlSettings::self()->save();
204 if (!laterSelected
) {
205 KMessageBox::ButtonCode promptRestart
=
206 KMessageBox::questionTwoActions(window(),
208 "Dolphin must be restarted to apply the "
209 "updated version control system settings."),
210 i18nc("@info", "Restart now?"),
211 KGuiItem(QApplication::translate("KStandardGuiItem", "&Restart"), QStringLiteral("dialog-restart")),
212 KGuiItem(QApplication::translate("KStandardGuiItem", "&Later"), QStringLiteral("dialog-later")));
213 if (promptRestart
== KMessageBox::ButtonCode::PrimaryAction
) {
214 Dolphin::openNewWindow();
217 laterSelected
= true;
223 void ContextMenuSettingsPage::restoreDefaults()
225 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
226 const QModelIndex index
= m_serviceModel
->index(i
, 0);
227 const QString service
= m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
230 !service
.startsWith(VersionControlServicePrefix
) && service
!= QLatin1String(DeleteService
) && service
!= QLatin1String(CopyToMoveToService
);
231 m_serviceModel
->setData(index
, checked
? Qt::Checked
: Qt::Unchecked
, Qt::CheckStateRole
);
235 void ContextMenuSettingsPage::showEvent(QShowEvent
*event
)
237 if (!event
->spontaneous() && !m_initialized
) {
240 loadVersionControlSystems();
242 // Add "Show 'Delete' command" as service
243 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals
);
244 KConfigGroup
configGroup(globalConfig
, "KDE");
245 addRow(QStringLiteral("edit-delete"), i18nc("@option:check", "Delete"), DeleteService
, configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
247 // Add "Show 'Copy To' and 'Move To' commands" as service
248 addRow(QStringLiteral("edit-copy"),
249 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
251 ContextMenuSettings::showCopyMoveMenu());
254 // Add other built-in actions
255 for (const QString
&id
: m_actionIds
) {
256 const QAction
*action
= m_actions
->action(id
);
258 addRow(action
->icon().name(), KLocalizedString::removeAcceleratorMarker(action
->text()), id
, entryVisible(id
));
263 m_sortModel
->sort(Qt::DisplayRole
);
265 m_initialized
= true;
267 SettingsPageBase::showEvent(event
);
270 void ContextMenuSettingsPage::loadServices()
272 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
273 const KConfigGroup showGroup
= config
.group("Show");
275 // Load generic services
276 const auto locations
= QStandardPaths::locateAll(QStandardPaths::GenericDataLocation
, QStringLiteral("kio/servicemenus"), QStandardPaths::LocateDirectory
);
277 QStringList files
= KFileUtils::findAllUniqueFiles(locations
);
279 for (const auto &file
: std::as_const(files
)) {
280 const QList
<KServiceAction
> serviceActions
= KService(file
).actions();
282 const KDesktopFile
desktopFile(file
);
283 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
285 for (const KServiceAction
&action
: serviceActions
) {
286 const QString serviceName
= action
.name();
287 const bool addService
= !action
.noDisplay() && !action
.isSeparator() && !isInServicesList(serviceName
);
290 const QString itemName
= subMenuName
.isEmpty() ? action
.text() : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
291 const bool checked
= showGroup
.readEntry(serviceName
, true);
292 addRow(action
.icon(), itemName
, serviceName
, checked
);
297 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
298 const auto jsonPlugins
= KPluginMetaData::findPlugins(QStringLiteral("kf6/kfileitemaction"));
300 for (const auto &jsonMetadata
: jsonPlugins
) {
301 const QString desktopEntryName
= jsonMetadata
.pluginId();
302 if (!isInServicesList(desktopEntryName
)) {
303 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
304 addRow(jsonMetadata
.iconName(), jsonMetadata
.name(), desktopEntryName
, checked
);
308 m_sortModel
->sort(Qt::DisplayRole
);
309 m_searchLineEdit
->setFocus(Qt::OtherFocusReason
);
312 void ContextMenuSettingsPage::loadVersionControlSystems()
314 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
316 // Create a checkbox for each available version control plugin
317 QSet
<QString
> loadedPlugins
;
319 const QVector
<KPluginMetaData
> plugins
= KPluginMetaData::findPlugins(QStringLiteral("dolphin/vcs"));
320 for (const auto &plugin
: plugins
) {
321 const QString pluginName
= plugin
.name();
322 addRow(QStringLiteral("code-class"), pluginName
, VersionControlServicePrefix
+ pluginName
, enabledPlugins
.contains(pluginName
));
323 loadedPlugins
+= pluginName
;
326 m_sortModel
->sort(Qt::DisplayRole
);
329 bool ContextMenuSettingsPage::isInServicesList(const QString
&service
) const
331 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
332 const QModelIndex index
= m_serviceModel
->index(i
, 0);
333 if (m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
340 void ContextMenuSettingsPage::addRow(const QString
&icon
, const QString
&text
, const QString
&value
, bool checked
)
342 m_serviceModel
->insertRow(0);
344 const QModelIndex index
= m_serviceModel
->index(0, 0);
345 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
346 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
347 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
348 m_serviceModel
->setData(index
, checked
? Qt::Checked
: Qt::Unchecked
, Qt::CheckStateRole
);
351 #include "moc_contextmenusettingspage.cpp"