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 const QAbstractItemModel
*model
= m_listView
->model();
174 for (int i
= 0; i
< model
->rowCount(); ++i
) {
175 const QModelIndex index
= model
->index(i
, 0);
176 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
177 const bool checked
= model
->data(index
, Qt::CheckStateRole
).value
<Qt::CheckState
>() == Qt::Checked
;
179 if (service
.startsWith(VersionControlServicePrefix
)) {
181 enabledPlugins
.append(model
->data(index
, Qt::DisplayRole
).toString());
183 } else if (service
== QLatin1String(DeleteService
)) {
184 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals
);
185 KConfigGroup
configGroup(globalConfig
, "KDE");
186 configGroup
.writeEntry("ShowDeleteCommand", checked
);
188 } else if (service
== QLatin1String(CopyToMoveToService
)) {
189 ContextMenuSettings::setShowCopyMoveMenu(checked
);
190 ContextMenuSettings::self()->save();
191 } else if (m_actionIds
.contains(service
)) {
192 setEntryVisible(service
, checked
);
193 ContextMenuSettings::self()->save();
195 showGroup
.writeEntry(service
, checked
);
201 if (m_enabledVcsPlugins
!= enabledPlugins
) {
202 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
203 VersionControlSettings::self()->save();
205 if (!laterSelected
) {
206 KMessageBox::ButtonCode promptRestart
=
207 KMessageBox::questionTwoActions(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 (promptRestart
== KMessageBox::ButtonCode::PrimaryAction
) {
215 Dolphin::openNewWindow();
218 laterSelected
= true;
224 void ContextMenuSettingsPage::restoreDefaults()
226 QAbstractItemModel
*model
= m_listView
->model();
227 for (int i
= 0; i
< model
->rowCount(); ++i
) {
228 const QModelIndex index
= model
->index(i
, 0);
229 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
232 !service
.startsWith(VersionControlServicePrefix
) && service
!= QLatin1String(DeleteService
) && service
!= QLatin1String(CopyToMoveToService
);
233 model
->setData(index
, checked
? Qt::Checked
: Qt::Unchecked
, Qt::CheckStateRole
);
237 void ContextMenuSettingsPage::showEvent(QShowEvent
*event
)
239 if (!event
->spontaneous() && !m_initialized
) {
242 loadVersionControlSystems();
244 // Add "Show 'Delete' command" as service
245 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals
);
246 KConfigGroup
configGroup(globalConfig
, "KDE");
247 addRow(QStringLiteral("edit-delete"), i18nc("@option:check", "Delete"), DeleteService
, configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
249 // Add "Show 'Copy To' and 'Move To' commands" as service
250 addRow(QStringLiteral("edit-copy"),
251 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
253 ContextMenuSettings::showCopyMoveMenu());
256 // Add other built-in actions
257 for (const QString
&id
: m_actionIds
) {
258 const QAction
*action
= m_actions
->action(id
);
260 addRow(action
->icon().name(), KLocalizedString::removeAcceleratorMarker(action
->text()), id
, entryVisible(id
));
265 m_sortModel
->sort(Qt::DisplayRole
);
267 m_initialized
= true;
269 SettingsPageBase::showEvent(event
);
272 void ContextMenuSettingsPage::loadServices()
274 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
275 const KConfigGroup showGroup
= config
.group("Show");
277 // Load generic services
278 const auto locations
= QStandardPaths::locateAll(QStandardPaths::GenericDataLocation
, QStringLiteral("kio/servicemenus"), QStandardPaths::LocateDirectory
);
279 QStringList files
= KFileUtils::findAllUniqueFiles(locations
);
281 for (const auto &file
: std::as_const(files
)) {
282 const QList
<KServiceAction
> serviceActions
= KService(file
).actions();
284 const KDesktopFile
desktopFile(file
);
285 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
287 for (const KServiceAction
&action
: serviceActions
) {
288 const QString serviceName
= action
.name();
289 const bool addService
= !action
.noDisplay() && !action
.isSeparator() && !isInServicesList(serviceName
);
292 const QString itemName
= subMenuName
.isEmpty() ? action
.text() : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
293 const bool checked
= showGroup
.readEntry(serviceName
, true);
294 addRow(action
.icon(), itemName
, serviceName
, checked
);
299 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
300 const auto jsonPlugins
= KPluginMetaData::findPlugins(QStringLiteral("kf6/kfileitemaction"));
302 for (const auto &jsonMetadata
: jsonPlugins
) {
303 const QString desktopEntryName
= jsonMetadata
.pluginId();
304 if (!isInServicesList(desktopEntryName
)) {
305 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
306 addRow(jsonMetadata
.iconName(), jsonMetadata
.name(), desktopEntryName
, checked
);
310 m_sortModel
->sort(Qt::DisplayRole
);
311 m_searchLineEdit
->setFocus(Qt::OtherFocusReason
);
314 void ContextMenuSettingsPage::loadVersionControlSystems()
316 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
318 // Create a checkbox for each available version control plugin
319 QSet
<QString
> loadedPlugins
;
321 const QVector
<KPluginMetaData
> plugins
= KPluginMetaData::findPlugins(QStringLiteral("dolphin/vcs"));
322 for (const auto &plugin
: plugins
) {
323 const QString pluginName
= plugin
.name();
324 addRow(QStringLiteral("code-class"), pluginName
, VersionControlServicePrefix
+ pluginName
, enabledPlugins
.contains(pluginName
));
325 loadedPlugins
+= pluginName
;
328 m_sortModel
->sort(Qt::DisplayRole
);
331 bool ContextMenuSettingsPage::isInServicesList(const QString
&service
) const
333 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
334 const QModelIndex index
= m_serviceModel
->index(i
, 0);
335 if (m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
342 void ContextMenuSettingsPage::addRow(const QString
&icon
, const QString
&text
, const QString
&value
, bool checked
)
344 m_serviceModel
->insertRow(0);
346 const QModelIndex index
= m_serviceModel
->index(0, 0);
347 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
348 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
349 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
350 m_serviceModel
->setData(index
, checked
? Qt::Checked
: Qt::Unchecked
, Qt::CheckStateRole
);
353 #include "moc_contextmenusettingspage.cpp"