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/serviceitemdelegate.h"
13 #include "settings/servicemodel.h"
15 #include <KDesktopFile>
16 #include <KDesktopFileActions>
18 #include <KLocalizedString>
19 #include <KMessageBox>
20 #include <KPluginMetaData>
22 #include <KServiceTypeTrader>
23 #include <kio_version.h>
24 #include <kiocore_export.h>
25 #include <kservice_export.h>
26 #include <kwidgetsaddons_version.h>
28 #include <KNSWidgets/Button>
31 #include <QApplication>
32 #include <QGridLayout>
35 #include <QListWidget>
38 #include <QSortFilterProxyModel>
42 const bool ShowDeleteDefault
= false;
43 const char VersionControlServicePrefix
[] = "_version_control_";
44 const char DeleteService
[] = "_delete";
45 const char CopyToMoveToService
[] = "_copy_to_move_to";
47 bool laterSelected
= false;
50 ContextMenuSettingsPage::ContextMenuSettingsPage(QWidget
*parent
, const KActionCollection
*actions
, const QStringList
&actionIds
)
51 : SettingsPageBase(parent
)
52 , m_initialized(false)
53 , m_serviceModel(nullptr)
54 , m_sortModel(nullptr)
56 , m_enabledVcsPlugins()
58 , m_actionIds(actionIds
)
60 QVBoxLayout
*topLayout
= new QVBoxLayout(this);
62 QLabel
*label
= new QLabel(i18nc("@label:textbox",
63 "Select which services should "
64 "be shown in the context menu:"),
66 label
->setWordWrap(true);
67 m_searchLineEdit
= new QLineEdit(this);
68 m_searchLineEdit
->setPlaceholderText(i18nc("@label:textbox", "Search…"));
69 connect(m_searchLineEdit
, &QLineEdit::textChanged
, this, [this](const QString
&filter
) {
70 m_sortModel
->setFilterFixedString(filter
);
73 m_listView
= new QListView(this);
74 QScroller::grabGesture(m_listView
->viewport(), QScroller::TouchGesture
);
76 auto *delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
77 m_serviceModel
= new ServiceModel(this);
78 m_sortModel
= new QSortFilterProxyModel(this);
79 m_sortModel
->setSourceModel(m_serviceModel
);
80 m_sortModel
->setSortRole(Qt::DisplayRole
);
81 m_sortModel
->setSortLocaleAware(true);
82 m_sortModel
->setFilterRole(Qt::DisplayRole
);
83 m_sortModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
84 m_listView
->setModel(m_sortModel
);
85 m_listView
->setItemDelegate(delegate
);
86 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
87 connect(m_listView
, &QListView::clicked
, this, &ContextMenuSettingsPage::changed
);
89 topLayout
->addWidget(label
);
90 topLayout
->addWidget(m_searchLineEdit
);
91 topLayout
->addWidget(m_listView
);
94 using NewStuffButton
= KNSWidgets::Button
;
95 auto *downloadButton
= new NewStuffButton(i18nc("@action:button", "Download New Services…"), QStringLiteral("servicemenu.knsrc"), this);
96 connect(downloadButton
, &NewStuffButton::dialogFinished
, this, [this](const auto &changedEntries
) {
97 if (!changedEntries
.isEmpty()) {
98 m_serviceModel
->clear();
102 topLayout
->addWidget(downloadButton
);
105 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
106 std::sort(m_enabledVcsPlugins
.begin(), m_enabledVcsPlugins
.end());
109 ContextMenuSettingsPage::~ContextMenuSettingsPage()
113 bool ContextMenuSettingsPage::entryVisible(const QString
&id
)
115 if (id
== "add_to_places") {
116 return ContextMenuSettings::showAddToPlaces();
117 } else if (id
== "sort") {
118 return ContextMenuSettings::showSortBy();
119 } else if (id
== "view_mode") {
120 return ContextMenuSettings::showViewMode();
121 } else if (id
== "open_in_new_tab") {
122 return ContextMenuSettings::showOpenInNewTab();
123 } else if (id
== "open_in_new_window") {
124 return ContextMenuSettings::showOpenInNewWindow();
125 } else if (id
== "copy_location") {
126 return ContextMenuSettings::showCopyLocation();
127 } else if (id
== "duplicate") {
128 return ContextMenuSettings::showDuplicateHere();
129 } else if (id
== "open_terminal_here") {
130 return ContextMenuSettings::showOpenTerminal();
131 } else if (id
== "copy_to_inactive_split_view") {
132 return ContextMenuSettings::showCopyToOtherSplitView();
133 } else if (id
== "move_to_inactive_split_view") {
134 return ContextMenuSettings::showMoveToOtherSplitView();
139 void ContextMenuSettingsPage::setEntryVisible(const QString
&id
, bool visible
)
141 if (id
== "add_to_places") {
142 ContextMenuSettings::setShowAddToPlaces(visible
);
143 } else if (id
== "sort") {
144 ContextMenuSettings::setShowSortBy(visible
);
145 } else if (id
== "view_mode") {
146 ContextMenuSettings::setShowViewMode(visible
);
147 } else if (id
== "open_in_new_tab") {
148 ContextMenuSettings::setShowOpenInNewTab(visible
);
149 } else if (id
== "open_in_new_window") {
150 ContextMenuSettings::setShowOpenInNewWindow(visible
);
151 } else if (id
== "copy_location") {
152 ContextMenuSettings::setShowCopyLocation(visible
);
153 } else if (id
== "duplicate") {
154 ContextMenuSettings::setShowDuplicateHere(visible
);
155 } else if (id
== "open_terminal_here") {
156 ContextMenuSettings::setShowOpenTerminal(visible
);
157 } else if (id
== "copy_to_inactive_split_view") {
158 ContextMenuSettings::setShowCopyToOtherSplitView(visible
);
159 } else if (id
== "move_to_inactive_split_view") {
160 ContextMenuSettings::setShowMoveToOtherSplitView(visible
);
164 void ContextMenuSettingsPage::applySettings()
166 if (!m_initialized
) {
170 KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
171 KConfigGroup showGroup
= config
.group("Show");
173 QStringList enabledPlugins
;
175 const QAbstractItemModel
*model
= m_listView
->model();
176 for (int i
= 0; i
< model
->rowCount(); ++i
) {
177 const QModelIndex index
= model
->index(i
, 0);
178 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
179 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
181 if (service
.startsWith(VersionControlServicePrefix
)) {
183 enabledPlugins
.append(model
->data(index
, Qt::DisplayRole
).toString());
185 } else if (service
== QLatin1String(DeleteService
)) {
186 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals
);
187 KConfigGroup
configGroup(globalConfig
, "KDE");
188 configGroup
.writeEntry("ShowDeleteCommand", checked
);
190 } else if (service
== QLatin1String(CopyToMoveToService
)) {
191 ContextMenuSettings::setShowCopyMoveMenu(checked
);
192 ContextMenuSettings::self()->save();
193 } else if (m_actionIds
.contains(service
)) {
194 setEntryVisible(service
, checked
);
195 ContextMenuSettings::self()->save();
197 showGroup
.writeEntry(service
, checked
);
203 if (m_enabledVcsPlugins
!= enabledPlugins
) {
204 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
205 VersionControlSettings::self()->save();
207 if (!laterSelected
) {
208 KMessageBox::ButtonCode promptRestart
=
209 KMessageBox::questionTwoActions(window(),
211 "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")));
216 if (promptRestart
== KMessageBox::ButtonCode::PrimaryAction
) {
217 Dolphin::openNewWindow();
220 laterSelected
= true;
226 void ContextMenuSettingsPage::restoreDefaults()
228 QAbstractItemModel
*model
= m_listView
->model();
229 for (int i
= 0; i
< model
->rowCount(); ++i
) {
230 const QModelIndex index
= model
->index(i
, 0);
231 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
234 !service
.startsWith(VersionControlServicePrefix
) && service
!= QLatin1String(DeleteService
) && service
!= QLatin1String(CopyToMoveToService
);
235 model
->setData(index
, checked
, Qt::CheckStateRole
);
239 void ContextMenuSettingsPage::showEvent(QShowEvent
*event
)
241 if (!event
->spontaneous() && !m_initialized
) {
244 loadVersionControlSystems();
246 // Add "Show 'Delete' command" as service
247 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals
);
248 KConfigGroup
configGroup(globalConfig
, "KDE");
249 addRow(QStringLiteral("edit-delete"), i18nc("@option:check", "Delete"), DeleteService
, configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
251 // Add "Show 'Copy To' and 'Move To' commands" as service
252 addRow(QStringLiteral("edit-copy"),
253 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
255 ContextMenuSettings::showCopyMoveMenu());
258 // Add other built-in actions
259 for (const QString
&id
: m_actionIds
) {
260 const QAction
*action
= m_actions
->action(id
);
262 addRow(action
->icon().name(), action
->text(), id
, entryVisible(id
));
267 m_sortModel
->sort(Qt::DisplayRole
);
269 m_initialized
= true;
271 SettingsPageBase::showEvent(event
);
274 void ContextMenuSettingsPage::loadServices()
276 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
277 const KConfigGroup showGroup
= config
.group("Show");
279 // Load generic services
280 const auto locations
= QStandardPaths::locateAll(QStandardPaths::GenericDataLocation
, QStringLiteral("kio/servicemenus"), QStandardPaths::LocateDirectory
);
281 QStringList files
= KFileUtils::findAllUniqueFiles(locations
);
283 #if KIOWIDGETS_BUILD_DEPRECATED_SINCE(5, 90)
284 const KService::List services
= KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
285 for (const KService::Ptr
&service
: services
) {
286 files
<< QStandardPaths::locate(QStandardPaths::GenericDataLocation
, "kservices5/" % service
->entryPath());
290 for (const auto &file
: qAsConst(files
)) {
291 const QList
<KServiceAction
> serviceActions
= KDesktopFileActions::userDefinedServices(KService(file
), true);
293 const KDesktopFile
desktopFile(file
);
294 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
296 for (const KServiceAction
&action
: serviceActions
) {
297 const QString serviceName
= action
.name();
298 const bool addService
= !action
.noDisplay() && !action
.isSeparator() && !isInServicesList(serviceName
);
301 const QString itemName
= subMenuName
.isEmpty() ? action
.text() : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
302 const bool checked
= showGroup
.readEntry(serviceName
, true);
303 addRow(action
.icon(), itemName
, serviceName
, checked
);
308 // Load service plugins, this is deprecated in KIO 5.82
309 #if KIOCORE_BUILD_DEPRECATED_SINCE(5, 82)
310 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
311 for (const KService::Ptr
&service
: pluginServices
) {
312 const QString desktopEntryName
= service
->desktopEntryName();
313 if (!isInServicesList(desktopEntryName
)) {
314 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
315 addRow(service
->icon(), service
->name(), desktopEntryName
, checked
);
320 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
321 const auto jsonPlugins
= KPluginMetaData::findPlugins(QStringLiteral("kf" QT_STRINGIFY(QT_VERSION_MAJOR
)) + QStringLiteral("/kfileitemaction"));
323 for (const auto &jsonMetadata
: jsonPlugins
) {
324 const QString desktopEntryName
= jsonMetadata
.pluginId();
325 if (!isInServicesList(desktopEntryName
)) {
326 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
327 addRow(jsonMetadata
.iconName(), jsonMetadata
.name(), desktopEntryName
, checked
);
331 m_sortModel
->sort(Qt::DisplayRole
);
332 m_searchLineEdit
->setFocus(Qt::OtherFocusReason
);
335 void ContextMenuSettingsPage::loadVersionControlSystems()
337 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
339 // Create a checkbox for each available version control plugin
340 QSet
<QString
> loadedPlugins
;
342 const QVector
<KPluginMetaData
> plugins
= KPluginMetaData::findPlugins(QStringLiteral("dolphin/vcs"));
343 for (const auto &plugin
: plugins
) {
344 const QString pluginName
= plugin
.name();
345 addRow(QStringLiteral("code-class"), pluginName
, VersionControlServicePrefix
+ pluginName
, enabledPlugins
.contains(pluginName
));
346 loadedPlugins
+= pluginName
;
349 m_sortModel
->sort(Qt::DisplayRole
);
352 bool ContextMenuSettingsPage::isInServicesList(const QString
&service
) const
354 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
355 const QModelIndex index
= m_serviceModel
->index(i
, 0);
356 if (m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
363 void ContextMenuSettingsPage::addRow(const QString
&icon
, const QString
&text
, const QString
&value
, bool checked
)
365 m_serviceModel
->insertRow(0);
367 const QModelIndex index
= m_serviceModel
->index(0, 0);
368 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
369 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
370 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
371 m_serviceModel
->setData(index
, checked
, Qt::CheckStateRole
);
374 #include "moc_contextmenusettingspage.cpp"