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>
28 #include <knewstuff_version.h>
29 #if KNEWSTUFF_VERSION >= QT_VERSION_CHECK(5, 91, 0)
30 #include <KNSWidgets/Button>
32 #include <KNS3/Button>
35 #include <QGridLayout>
37 #include <QListWidget>
40 #include <QSortFilterProxyModel>
42 #include <QApplication>
46 const bool ShowDeleteDefault
= false;
47 const char VersionControlServicePrefix
[] = "_version_control_";
48 const char DeleteService
[] = "_delete";
49 const char CopyToMoveToService
[] ="_copy_to_move_to";
51 bool laterSelected
= false;
54 ContextMenuSettingsPage::ContextMenuSettingsPage(QWidget
* parent
,
55 const KActionCollection
* actions
,
56 const QStringList
& actionIds
) :
57 SettingsPageBase(parent
),
59 m_serviceModel(nullptr),
62 m_enabledVcsPlugins(),
64 m_actionIds(actionIds
)
66 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
68 QLabel
* label
= new QLabel(i18nc("@label:textbox",
69 "Select which services should "
70 "be shown in the context menu:"), this);
71 label
->setWordWrap(true);
72 m_searchLineEdit
= new QLineEdit(this);
73 m_searchLineEdit
->setPlaceholderText(i18nc("@label:textbox", "Search..."));
74 connect(m_searchLineEdit
, &QLineEdit::textChanged
, this, [this](const QString
&filter
){
75 m_sortModel
->setFilterFixedString(filter
);
78 m_listView
= new QListView(this);
79 QScroller::grabGesture(m_listView
->viewport(), QScroller::TouchGesture
);
81 auto *delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
82 m_serviceModel
= new ServiceModel(this);
83 m_sortModel
= new QSortFilterProxyModel(this);
84 m_sortModel
->setSourceModel(m_serviceModel
);
85 m_sortModel
->setSortRole(Qt::DisplayRole
);
86 m_sortModel
->setSortLocaleAware(true);
87 m_sortModel
->setFilterRole(Qt::DisplayRole
);
88 m_sortModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
89 m_listView
->setModel(m_sortModel
);
90 m_listView
->setItemDelegate(delegate
);
91 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
92 connect(m_listView
, &QListView::clicked
, this, &ContextMenuSettingsPage::changed
);
94 topLayout
->addWidget(label
);
95 topLayout
->addWidget(m_searchLineEdit
);
96 topLayout
->addWidget(m_listView
);
99 #if KNEWSTUFF_VERSION >= QT_VERSION_CHECK(5, 91, 0)
100 using NewStuffButton
= KNSWidgets::Button
;
102 using NewStuffButton
= KNS3::Button
;
103 #endif // KNEWSTUFF_VERSION
104 auto *downloadButton
= new NewStuffButton(i18nc("@action:button", "Download New Services..."),
105 QStringLiteral("servicemenu.knsrc"),
107 connect(downloadButton
, &NewStuffButton::dialogFinished
, this, [this](const auto &changedEntries
) {
108 if (!changedEntries
.isEmpty()) {
109 m_serviceModel
->clear();
113 topLayout
->addWidget(downloadButton
);
116 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
117 std::sort(m_enabledVcsPlugins
.begin(), m_enabledVcsPlugins
.end());
120 ContextMenuSettingsPage::~ContextMenuSettingsPage() {
123 bool ContextMenuSettingsPage::entryVisible(const QString
& id
)
125 if (id
== "add_to_places") {
126 return ContextMenuSettings::showAddToPlaces();
127 } else if (id
== "sort") {
128 return ContextMenuSettings::showSortBy();
129 } else if (id
== "view_mode") {
130 return ContextMenuSettings::showViewMode();
131 } else if (id
== "open_in_new_tab") {
132 return ContextMenuSettings::showOpenInNewTab();
133 } else if (id
== "open_in_new_window") {
134 return ContextMenuSettings::showOpenInNewWindow();
135 } else if (id
== "copy_location") {
136 return ContextMenuSettings::showCopyLocation();
137 } else if (id
== "duplicate") {
138 return ContextMenuSettings::showDuplicateHere();
139 } else if (id
== "open_terminal_here") {
140 return ContextMenuSettings::showOpenTerminal();
145 void ContextMenuSettingsPage::setEntryVisible(const QString
& id
, bool visible
)
147 if (id
== "add_to_places") {
148 ContextMenuSettings::setShowAddToPlaces(visible
);
149 } else if (id
== "sort") {
150 ContextMenuSettings::setShowSortBy(visible
);
151 } else if (id
== "view_mode") {
152 ContextMenuSettings::setShowViewMode(visible
);
153 } else if (id
== "open_in_new_tab") {
154 ContextMenuSettings::setShowOpenInNewTab(visible
);
155 } else if (id
== "open_in_new_window") {
156 ContextMenuSettings::setShowOpenInNewWindow(visible
);
157 } else if (id
== "copy_location") {
158 ContextMenuSettings::setShowCopyLocation(visible
);
159 } else if (id
== "duplicate") {
160 ContextMenuSettings::setShowDuplicateHere(visible
);
161 } else if (id
== "open_terminal_here") {
162 ContextMenuSettings::setShowOpenTerminal(visible
);
166 void ContextMenuSettingsPage::applySettings()
168 if (!m_initialized
) {
172 KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
173 KConfigGroup showGroup
= config
.group("Show");
175 QStringList enabledPlugins
;
177 const QAbstractItemModel
*model
= m_listView
->model();
178 for (int i
= 0; i
< model
->rowCount(); ++i
) {
179 const QModelIndex index
= model
->index(i
, 0);
180 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
181 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
183 if (service
.startsWith(VersionControlServicePrefix
)) {
185 enabledPlugins
.append(model
->data(index
, Qt::DisplayRole
).toString());
187 } else if (service
== QLatin1String(DeleteService
)) {
188 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals
);
189 KConfigGroup
configGroup(globalConfig
, "KDE");
190 configGroup
.writeEntry("ShowDeleteCommand", checked
);
192 } else if (service
== QLatin1String(CopyToMoveToService
)) {
193 ContextMenuSettings::setShowCopyMoveMenu(checked
);
194 ContextMenuSettings::self()->save();
195 } else if (m_actionIds
.contains(service
)) {
196 setEntryVisible(service
, checked
);
197 ContextMenuSettings::self()->save();
199 showGroup
.writeEntry(service
, checked
);
205 if (m_enabledVcsPlugins
!= enabledPlugins
) {
206 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
207 VersionControlSettings::self()->save();
209 if (!laterSelected
) {
210 KMessageBox::ButtonCode promptRestart
= KMessageBox::questionYesNo(window(),
211 i18nc("@info", "Dolphin must be restarted to apply the "
212 "updated version control system settings."),
213 i18nc("@info", "Restart now?"),
214 KGuiItem(QApplication::translate("KStandardGuiItem", "&Restart"), QStringLiteral("dialog-restart")),
215 KGuiItem(QApplication::translate("KStandardGuiItem", "&Later"), QStringLiteral("dialog-later"))
217 if (promptRestart
== KMessageBox::ButtonCode::Yes
) {
218 Dolphin::openNewWindow();
221 laterSelected
= true;
227 void ContextMenuSettingsPage::restoreDefaults()
229 QAbstractItemModel
* model
= m_listView
->model();
230 for (int i
= 0; i
< model
->rowCount(); ++i
) {
231 const QModelIndex index
= model
->index(i
, 0);
232 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
234 const bool checked
= !service
.startsWith(VersionControlServicePrefix
)
235 && service
!= QLatin1String(DeleteService
)
236 && service
!= QLatin1String(CopyToMoveToService
);
237 model
->setData(index
, checked
, Qt::CheckStateRole
);
241 void ContextMenuSettingsPage::showEvent(QShowEvent
* event
)
243 if (!event
->spontaneous() && !m_initialized
) {
246 loadVersionControlSystems();
248 // Add "Show 'Delete' command" as service
249 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals
);
250 KConfigGroup
configGroup(globalConfig
, "KDE");
251 addRow(QStringLiteral("edit-delete"),
252 i18nc("@option:check", "Delete"),
254 configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
256 // Add "Show 'Copy To' and 'Move To' commands" as service
257 addRow(QStringLiteral("edit-copy"),
258 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
260 ContextMenuSettings::showCopyMoveMenu());
263 // Add other built-in actions
264 for (const QString
& id
: m_actionIds
) {
265 const QAction
* action
= m_actions
->action(id
);
267 addRow(action
->icon().name(), action
->text(), id
, entryVisible(id
));
272 m_sortModel
->sort(Qt::DisplayRole
);
274 m_initialized
= true;
276 SettingsPageBase::showEvent(event
);
279 void ContextMenuSettingsPage::loadServices()
281 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
282 const KConfigGroup showGroup
= config
.group("Show");
284 // Load generic services
285 const auto locations
= QStandardPaths::locateAll(QStandardPaths::GenericDataLocation
, QStringLiteral("kio/servicemenus"), QStandardPaths::LocateDirectory
);
286 QStringList files
= KFileUtils::findAllUniqueFiles(locations
);
287 const KService::List services
= KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
288 for (const KService::Ptr
&service
: services
) {
289 files
<< QStandardPaths::locate(QStandardPaths::GenericDataLocation
, "kservices5/" % service
->entryPath());
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()
304 : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
305 const bool checked
= showGroup
.readEntry(serviceName
, true);
306 addRow(action
.icon(), itemName
, serviceName
, checked
);
311 // Load service plugins, this is deprecated in KIO 5.82
312 #if KIOCORE_BUILD_DEPRECATED_SINCE(5, 82)
313 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
314 for (const KService::Ptr
&service
: pluginServices
) {
315 const QString desktopEntryName
= service
->desktopEntryName();
316 if (!isInServicesList(desktopEntryName
)) {
317 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
318 addRow(service
->icon(), service
->name(), desktopEntryName
, checked
);
323 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
324 const auto jsonPlugins
= KPluginMetaData::findPlugins(QStringLiteral("kf5/kfileitemaction"));
326 for (const auto &jsonMetadata
: jsonPlugins
) {
327 const QString desktopEntryName
= jsonMetadata
.pluginId();
328 if (!isInServicesList(desktopEntryName
)) {
329 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
330 addRow(jsonMetadata
.iconName(), jsonMetadata
.name(), desktopEntryName
, checked
);
334 m_sortModel
->sort(Qt::DisplayRole
);
335 m_searchLineEdit
->setFocus(Qt::OtherFocusReason
);
338 void ContextMenuSettingsPage::loadVersionControlSystems()
340 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
342 // Create a checkbox for each available version control plugin
343 QSet
<QString
> loadedPlugins
;
345 const QVector
<KPluginMetaData
> plugins
= KPluginMetaData::findPlugins(QStringLiteral("dolphin/vcs"));
346 for (const auto &plugin
: plugins
) {
347 const QString pluginName
= plugin
.name();
348 addRow(QStringLiteral("code-class"),
350 VersionControlServicePrefix
+ pluginName
,
351 enabledPlugins
.contains(pluginName
));
352 loadedPlugins
+= pluginName
;
355 m_sortModel
->sort(Qt::DisplayRole
);
358 bool ContextMenuSettingsPage::isInServicesList(const QString
&service
) const
360 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
361 const QModelIndex index
= m_serviceModel
->index(i
, 0);
362 if (m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
369 void ContextMenuSettingsPage::addRow(const QString
&icon
,
371 const QString
&value
,
374 m_serviceModel
->insertRow(0);
376 const QModelIndex index
= m_serviceModel
->index(0, 0);
377 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
378 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
379 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
380 m_serviceModel
->setData(index
, checked
, Qt::CheckStateRole
);