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_generalsettings.h"
10 #include "dolphin_versioncontrolsettings.h"
11 #include "dolphin_contextmenusettings.h"
12 #include "settings/serviceitemdelegate.h"
13 #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>
29 #include <knewstuff_version.h>
30 #if KNEWSTUFF_VERSION >= QT_VERSION_CHECK(5, 91, 0)
31 #include <KNSWidgets/Button>
33 #include <KNS3/Button>
36 #include <QGridLayout>
38 #include <QListWidget>
41 #include <QSortFilterProxyModel>
43 #include <QApplication>
47 const bool ShowDeleteDefault
= false;
48 const char VersionControlServicePrefix
[] = "_version_control_";
49 const char DeleteService
[] = "_delete";
50 const char CopyToMoveToService
[] ="_copy_to_move_to";
52 bool laterSelected
= false;
55 ContextMenuSettingsPage::ContextMenuSettingsPage(QWidget
* parent
,
56 const KActionCollection
* actions
,
57 const QStringList
& actionIds
) :
58 SettingsPageBase(parent
),
60 m_serviceModel(nullptr),
63 m_enabledVcsPlugins(),
65 m_actionIds(actionIds
)
67 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
69 QLabel
* label
= new QLabel(i18nc("@label:textbox",
70 "Select which services should "
71 "be shown in the context menu:"), this);
72 label
->setWordWrap(true);
73 m_searchLineEdit
= new QLineEdit(this);
74 m_searchLineEdit
->setPlaceholderText(i18nc("@label:textbox", "Search..."));
75 connect(m_searchLineEdit
, &QLineEdit::textChanged
, this, [this](const QString
&filter
){
76 m_sortModel
->setFilterFixedString(filter
);
79 m_listView
= new QListView(this);
80 QScroller::grabGesture(m_listView
->viewport(), QScroller::TouchGesture
);
82 auto *delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
83 m_serviceModel
= new ServiceModel(this);
84 m_sortModel
= new QSortFilterProxyModel(this);
85 m_sortModel
->setSourceModel(m_serviceModel
);
86 m_sortModel
->setSortRole(Qt::DisplayRole
);
87 m_sortModel
->setSortLocaleAware(true);
88 m_sortModel
->setFilterRole(Qt::DisplayRole
);
89 m_sortModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
90 m_listView
->setModel(m_sortModel
);
91 m_listView
->setItemDelegate(delegate
);
92 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
93 connect(m_listView
, &QListView::clicked
, this, &ContextMenuSettingsPage::changed
);
95 topLayout
->addWidget(label
);
96 topLayout
->addWidget(m_searchLineEdit
);
97 topLayout
->addWidget(m_listView
);
100 #if KNEWSTUFF_VERSION >= QT_VERSION_CHECK(5, 91, 0)
101 using NewStuffButton
= KNSWidgets::Button
;
103 using NewStuffButton
= KNS3::Button
;
104 #endif // KNEWSTUFF_VERSION
105 auto *downloadButton
= new NewStuffButton(i18nc("@action:button", "Download New Services..."),
106 QStringLiteral("servicemenu.knsrc"),
108 connect(downloadButton
, &NewStuffButton::dialogFinished
, this, [this](const auto &changedEntries
) {
109 if (!changedEntries
.isEmpty()) {
110 m_serviceModel
->clear();
114 topLayout
->addWidget(downloadButton
);
117 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
118 std::sort(m_enabledVcsPlugins
.begin(), m_enabledVcsPlugins
.end());
121 ContextMenuSettingsPage::~ContextMenuSettingsPage() {
124 bool ContextMenuSettingsPage::entryVisible(const QString
& id
)
126 if (id
== "add_to_places") {
127 return ContextMenuSettings::showAddToPlaces();
128 } else if (id
== "sort") {
129 return ContextMenuSettings::showSortBy();
130 } else if (id
== "view_mode") {
131 return ContextMenuSettings::showViewMode();
132 } else if (id
== "open_in_new_tab") {
133 return ContextMenuSettings::showOpenInNewTab();
134 } else if (id
== "open_in_new_window") {
135 return ContextMenuSettings::showOpenInNewWindow();
136 } else if (id
== "copy_location") {
137 return ContextMenuSettings::showCopyLocation();
138 } else if (id
== "duplicate") {
139 return ContextMenuSettings::showDuplicateHere();
140 } else if (id
== "open_terminal_here") {
141 return ContextMenuSettings::showOpenTerminal();
146 void ContextMenuSettingsPage::setEntryVisible(const QString
& id
, bool visible
)
148 if (id
== "add_to_places") {
149 ContextMenuSettings::setShowAddToPlaces(visible
);
150 } else if (id
== "sort") {
151 ContextMenuSettings::setShowSortBy(visible
);
152 } else if (id
== "view_mode") {
153 ContextMenuSettings::setShowViewMode(visible
);
154 } else if (id
== "open_in_new_tab") {
155 ContextMenuSettings::setShowOpenInNewTab(visible
);
156 } else if (id
== "open_in_new_window") {
157 ContextMenuSettings::setShowOpenInNewWindow(visible
);
158 } else if (id
== "copy_location") {
159 ContextMenuSettings::setShowCopyLocation(visible
);
160 } else if (id
== "duplicate") {
161 ContextMenuSettings::setShowDuplicateHere(visible
);
162 } else if (id
== "open_terminal_here") {
163 ContextMenuSettings::setShowOpenTerminal(visible
);
167 void ContextMenuSettingsPage::applySettings()
169 if (!m_initialized
) {
173 KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
174 KConfigGroup showGroup
= config
.group("Show");
176 QStringList enabledPlugins
;
178 const QAbstractItemModel
*model
= m_listView
->model();
179 for (int i
= 0; i
< model
->rowCount(); ++i
) {
180 const QModelIndex index
= model
->index(i
, 0);
181 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
182 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
184 if (service
.startsWith(VersionControlServicePrefix
)) {
186 enabledPlugins
.append(model
->data(index
, Qt::DisplayRole
).toString());
188 } else if (service
== QLatin1String(DeleteService
)) {
189 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals
);
190 KConfigGroup
configGroup(globalConfig
, "KDE");
191 configGroup
.writeEntry("ShowDeleteCommand", checked
);
193 } else if (service
== QLatin1String(CopyToMoveToService
)) {
194 ContextMenuSettings::setShowCopyMoveMenu(checked
);
195 ContextMenuSettings::self()->save();
196 } else if (m_actionIds
.contains(service
)) {
197 setEntryVisible(service
, checked
);
198 ContextMenuSettings::self()->save();
200 showGroup
.writeEntry(service
, checked
);
206 if (m_enabledVcsPlugins
!= enabledPlugins
) {
207 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
208 VersionControlSettings::self()->save();
210 if (!laterSelected
) {
211 KMessageBox::ButtonCode promptRestart
= KMessageBox::questionYesNo(window(),
212 i18nc("@info", "Dolphin must be restarted to apply the "
213 "updated version control system settings."),
214 i18nc("@info", "Restart now?"),
215 KGuiItem(QApplication::translate("KStandardGuiItem", "&Restart"), QStringLiteral("dialog-restart")),
216 KGuiItem(QApplication::translate("KStandardGuiItem", "&Later"), QStringLiteral("dialog-later"))
218 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();
235 const bool checked
= !service
.startsWith(VersionControlServicePrefix
)
236 && service
!= QLatin1String(DeleteService
)
237 && service
!= QLatin1String(CopyToMoveToService
);
238 model
->setData(index
, checked
, Qt::CheckStateRole
);
242 void ContextMenuSettingsPage::showEvent(QShowEvent
* event
)
244 if (!event
->spontaneous() && !m_initialized
) {
247 loadVersionControlSystems();
249 // Add "Show 'Delete' command" as service
250 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals
);
251 KConfigGroup
configGroup(globalConfig
, "KDE");
252 addRow(QStringLiteral("edit-delete"),
253 i18nc("@option:check", "Delete"),
255 configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
257 // Add "Show 'Copy To' and 'Move To' commands" as service
258 addRow(QStringLiteral("edit-copy"),
259 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
261 ContextMenuSettings::showCopyMoveMenu());
264 // Add other built-in actions
265 for (const QString
& id
: m_actionIds
) {
266 const QAction
* action
= m_actions
->action(id
);
268 addRow(action
->icon().name(), action
->text(), id
, entryVisible(id
));
273 m_sortModel
->sort(Qt::DisplayRole
);
275 m_initialized
= true;
277 SettingsPageBase::showEvent(event
);
280 void ContextMenuSettingsPage::loadServices()
282 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
283 const KConfigGroup showGroup
= config
.group("Show");
285 // Load generic services
286 const auto locations
= QStandardPaths::locateAll(QStandardPaths::GenericDataLocation
, QStringLiteral("kio/servicemenus"), QStandardPaths::LocateDirectory
);
287 QStringList files
= KFileUtils::findAllUniqueFiles(locations
);
289 #if KIOWIDGETS_BUILD_DEPRECATED_SINCE(5, 90)
290 const KService::List services
= KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
291 for (const KService::Ptr
&service
: services
) {
292 files
<< QStandardPaths::locate(QStandardPaths::GenericDataLocation
, "kservices5/" % service
->entryPath());
296 for (const auto &file
: qAsConst(files
)) {
297 const QList
<KServiceAction
> serviceActions
= KDesktopFileActions::userDefinedServices(KService(file
), true);
299 const KDesktopFile
desktopFile(file
);
300 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
302 for (const KServiceAction
&action
: serviceActions
) {
303 const QString serviceName
= action
.name();
304 const bool addService
= !action
.noDisplay() && !action
.isSeparator() && !isInServicesList(serviceName
);
307 const QString itemName
= subMenuName
.isEmpty()
309 : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
310 const bool checked
= showGroup
.readEntry(serviceName
, true);
311 addRow(action
.icon(), itemName
, serviceName
, checked
);
316 // Load service plugins, this is deprecated in KIO 5.82
317 #if KIOCORE_BUILD_DEPRECATED_SINCE(5, 82)
318 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
319 for (const KService::Ptr
&service
: pluginServices
) {
320 const QString desktopEntryName
= service
->desktopEntryName();
321 if (!isInServicesList(desktopEntryName
)) {
322 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
323 addRow(service
->icon(), service
->name(), desktopEntryName
, checked
);
328 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
329 const auto jsonPlugins
= KPluginMetaData::findPlugins(QStringLiteral("kf5/kfileitemaction"));
331 for (const auto &jsonMetadata
: jsonPlugins
) {
332 const QString desktopEntryName
= jsonMetadata
.pluginId();
333 if (!isInServicesList(desktopEntryName
)) {
334 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
335 addRow(jsonMetadata
.iconName(), jsonMetadata
.name(), desktopEntryName
, checked
);
339 m_sortModel
->sort(Qt::DisplayRole
);
340 m_searchLineEdit
->setFocus(Qt::OtherFocusReason
);
343 void ContextMenuSettingsPage::loadVersionControlSystems()
345 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
347 // Create a checkbox for each available version control plugin
348 QSet
<QString
> loadedPlugins
;
350 const QVector
<KPluginMetaData
> plugins
= KPluginMetaData::findPlugins(QStringLiteral("dolphin/vcs"));
351 for (const auto &plugin
: plugins
) {
352 const QString pluginName
= plugin
.name();
353 addRow(QStringLiteral("code-class"),
355 VersionControlServicePrefix
+ pluginName
,
356 enabledPlugins
.contains(pluginName
));
357 loadedPlugins
+= pluginName
;
360 m_sortModel
->sort(Qt::DisplayRole
);
363 bool ContextMenuSettingsPage::isInServicesList(const QString
&service
) const
365 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
366 const QModelIndex index
= m_serviceModel
->index(i
, 0);
367 if (m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
374 void ContextMenuSettingsPage::addRow(const QString
&icon
,
376 const QString
&value
,
379 m_serviceModel
->insertRow(0);
381 const QModelIndex index
= m_serviceModel
->index(0, 0);
382 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
383 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
384 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
385 m_serviceModel
->setData(index
, checked
, Qt::CheckStateRole
);