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>
27 #include <knewstuff_version.h>
28 #if KNEWSTUFF_VERSION >= QT_VERSION_CHECK(5, 91, 0)
29 #include <KNSWidgets/Button>
31 #include <KNS3/Button>
34 #include <QGridLayout>
36 #include <QListWidget>
39 #include <QSortFilterProxyModel>
41 #include <QApplication>
45 const bool ShowDeleteDefault
= false;
46 const char VersionControlServicePrefix
[] = "_version_control_";
47 const char DeleteService
[] = "_delete";
48 const char CopyToMoveToService
[] ="_copy_to_move_to";
50 bool laterSelected
= false;
53 ContextMenuSettingsPage::ContextMenuSettingsPage(QWidget
* parent
,
54 const KActionCollection
* actions
,
55 const QStringList
& actionIds
) :
56 SettingsPageBase(parent
),
58 m_serviceModel(nullptr),
61 m_enabledVcsPlugins(),
63 m_actionIds(actionIds
)
65 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
67 QLabel
* label
= new QLabel(i18nc("@label:textbox",
68 "Select which services should "
69 "be shown in the context menu:"), this);
70 label
->setWordWrap(true);
71 m_searchLineEdit
= new QLineEdit(this);
72 m_searchLineEdit
->setPlaceholderText(i18nc("@label:textbox", "Search..."));
73 connect(m_searchLineEdit
, &QLineEdit::textChanged
, this, [this](const QString
&filter
){
74 m_sortModel
->setFilterFixedString(filter
);
77 m_listView
= new QListView(this);
78 QScroller::grabGesture(m_listView
->viewport(), QScroller::TouchGesture
);
80 auto *delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
81 m_serviceModel
= new ServiceModel(this);
82 m_sortModel
= new QSortFilterProxyModel(this);
83 m_sortModel
->setSourceModel(m_serviceModel
);
84 m_sortModel
->setSortRole(Qt::DisplayRole
);
85 m_sortModel
->setSortLocaleAware(true);
86 m_sortModel
->setFilterRole(Qt::DisplayRole
);
87 m_sortModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
88 m_listView
->setModel(m_sortModel
);
89 m_listView
->setItemDelegate(delegate
);
90 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
91 connect(m_listView
, &QListView::clicked
, this, &ContextMenuSettingsPage::changed
);
93 topLayout
->addWidget(label
);
94 topLayout
->addWidget(m_searchLineEdit
);
95 topLayout
->addWidget(m_listView
);
98 #if KNEWSTUFF_VERSION >= QT_VERSION_CHECK(5, 91, 0)
99 using NewStuffButton
= KNSWidgets::Button
;
101 using NewStuffButton
= KNS3::Button
;
102 #endif // KNEWSTUFF_VERSION
103 auto *downloadButton
= new NewStuffButton(i18nc("@action:button", "Download New Services..."),
104 QStringLiteral("servicemenu.knsrc"),
106 connect(downloadButton
, &NewStuffButton::dialogFinished
, this, [this](const auto &changedEntries
) {
107 if (!changedEntries
.isEmpty()) {
108 m_serviceModel
->clear();
112 topLayout
->addWidget(downloadButton
);
115 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
116 std::sort(m_enabledVcsPlugins
.begin(), m_enabledVcsPlugins
.end());
119 ContextMenuSettingsPage::~ContextMenuSettingsPage() {
122 bool ContextMenuSettingsPage::entryVisible(const QString
& id
)
124 if (id
== "add_to_places") {
125 return ContextMenuSettings::showAddToPlaces();
126 } else if (id
== "sort") {
127 return ContextMenuSettings::showSortBy();
128 } else if (id
== "view_mode") {
129 return ContextMenuSettings::showViewMode();
130 } else if (id
== "open_in_new_tab") {
131 return ContextMenuSettings::showOpenInNewTab();
132 } else if (id
== "open_in_new_window") {
133 return ContextMenuSettings::showOpenInNewWindow();
134 } else if (id
== "copy_location") {
135 return ContextMenuSettings::showCopyLocation();
136 } else if (id
== "duplicate") {
137 return ContextMenuSettings::showDuplicateHere();
138 } else if (id
== "open_terminal_here") {
139 return ContextMenuSettings::showOpenTerminal();
144 void ContextMenuSettingsPage::setEntryVisible(const QString
& id
, bool visible
)
146 if (id
== "add_to_places") {
147 ContextMenuSettings::setShowAddToPlaces(visible
);
148 } else if (id
== "sort") {
149 ContextMenuSettings::setShowSortBy(visible
);
150 } else if (id
== "view_mode") {
151 ContextMenuSettings::setShowViewMode(visible
);
152 } else if (id
== "open_in_new_tab") {
153 ContextMenuSettings::setShowOpenInNewTab(visible
);
154 } else if (id
== "open_in_new_window") {
155 ContextMenuSettings::setShowOpenInNewWindow(visible
);
156 } else if (id
== "copy_location") {
157 ContextMenuSettings::setShowCopyLocation(visible
);
158 } else if (id
== "duplicate") {
159 ContextMenuSettings::setShowDuplicateHere(visible
);
160 } else if (id
== "open_terminal_here") {
161 ContextMenuSettings::setShowOpenTerminal(visible
);
165 void ContextMenuSettingsPage::applySettings()
167 if (!m_initialized
) {
171 KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
172 KConfigGroup showGroup
= config
.group("Show");
174 QStringList enabledPlugins
;
176 const QAbstractItemModel
*model
= m_listView
->model();
177 for (int i
= 0; i
< model
->rowCount(); ++i
) {
178 const QModelIndex index
= model
->index(i
, 0);
179 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
180 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
182 if (service
.startsWith(VersionControlServicePrefix
)) {
184 enabledPlugins
.append(model
->data(index
, Qt::DisplayRole
).toString());
186 } else if (service
== QLatin1String(DeleteService
)) {
187 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals
);
188 KConfigGroup
configGroup(globalConfig
, "KDE");
189 configGroup
.writeEntry("ShowDeleteCommand", checked
);
191 } else if (service
== QLatin1String(CopyToMoveToService
)) {
192 ContextMenuSettings::setShowCopyMoveMenu(checked
);
193 ContextMenuSettings::self()->save();
194 } else if (m_actionIds
.contains(service
)) {
195 setEntryVisible(service
, checked
);
196 ContextMenuSettings::self()->save();
198 showGroup
.writeEntry(service
, checked
);
204 if (m_enabledVcsPlugins
!= enabledPlugins
) {
205 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
206 VersionControlSettings::self()->save();
208 if (!laterSelected
) {
209 KMessageBox::ButtonCode promptRestart
= KMessageBox::questionYesNo(window(),
210 i18nc("@info", "Dolphin must be restarted to apply the "
211 "updated version control system settings."),
212 i18nc("@info", "Restart now?"),
213 KGuiItem(QApplication::translate("KStandardGuiItem", "&Restart"), QStringLiteral("dialog-restart")),
214 KGuiItem(QApplication::translate("KStandardGuiItem", "&Later"), QStringLiteral("dialog-later"))
216 if (promptRestart
== KMessageBox::ButtonCode::Yes
) {
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();
233 const bool checked
= !service
.startsWith(VersionControlServicePrefix
)
234 && service
!= QLatin1String(DeleteService
)
235 && service
!= QLatin1String(CopyToMoveToService
);
236 model
->setData(index
, checked
, Qt::CheckStateRole
);
240 void ContextMenuSettingsPage::showEvent(QShowEvent
* event
)
242 if (!event
->spontaneous() && !m_initialized
) {
245 loadVersionControlSystems();
247 // Add "Show 'Delete' command" as service
248 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals
);
249 KConfigGroup
configGroup(globalConfig
, "KDE");
250 addRow(QStringLiteral("edit-delete"),
251 i18nc("@option:check", "Delete"),
253 configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
255 // Add "Show 'Copy To' and 'Move To' commands" as service
256 addRow(QStringLiteral("edit-copy"),
257 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
259 ContextMenuSettings::showCopyMoveMenu());
262 // Add other built-in actions
263 for (const QString
& id
: m_actionIds
) {
264 const QAction
* action
= m_actions
->action(id
);
266 addRow(action
->icon().name(), action
->text(), id
, entryVisible(id
));
271 m_sortModel
->sort(Qt::DisplayRole
);
273 m_initialized
= true;
275 SettingsPageBase::showEvent(event
);
278 void ContextMenuSettingsPage::loadServices()
280 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
281 const KConfigGroup showGroup
= config
.group("Show");
283 // Load generic services
284 const auto locations
= QStandardPaths::locateAll(QStandardPaths::GenericDataLocation
, QStringLiteral("kio/servicemenus"), QStandardPaths::LocateDirectory
);
285 QStringList files
= KFileUtils::findAllUniqueFiles(locations
);
286 const KService::List services
= KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
287 for (const KService::Ptr
&service
: services
) {
288 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()
303 : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
304 const bool checked
= showGroup
.readEntry(serviceName
, true);
305 addRow(action
.icon(), itemName
, serviceName
, checked
);
310 // Load service plugins, this is deprecated in KIO 5.82
311 #if KIO_VERSION < QT_VERSION_CHECK(6, 0, 0)
312 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
313 for (const KService::Ptr
&service
: pluginServices
) {
314 const QString desktopEntryName
= service
->desktopEntryName();
315 if (!isInServicesList(desktopEntryName
)) {
316 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
317 addRow(service
->icon(), service
->name(), desktopEntryName
, checked
);
322 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
323 const auto jsonPlugins
= KPluginMetaData::findPlugins(QStringLiteral("kf5/kfileitemaction"));
325 for (const auto &jsonMetadata
: jsonPlugins
) {
326 const QString desktopEntryName
= jsonMetadata
.pluginId();
327 if (!isInServicesList(desktopEntryName
)) {
328 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
329 addRow(jsonMetadata
.iconName(), jsonMetadata
.name(), desktopEntryName
, checked
);
333 m_sortModel
->sort(Qt::DisplayRole
);
334 m_searchLineEdit
->setFocus(Qt::OtherFocusReason
);
337 void ContextMenuSettingsPage::loadVersionControlSystems()
339 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
341 // Create a checkbox for each available version control plugin
342 QSet
<QString
> loadedPlugins
;
344 const QVector
<KPluginMetaData
> plugins
= KPluginMetaData::findPlugins(QStringLiteral("dolphin/vcs"));
345 for (const auto &plugin
: plugins
) {
346 const QString pluginName
= plugin
.name();
347 addRow(QStringLiteral("code-class"),
349 VersionControlServicePrefix
+ pluginName
,
350 enabledPlugins
.contains(pluginName
));
351 loadedPlugins
+= pluginName
;
354 m_sortModel
->sort(Qt::DisplayRole
);
357 bool ContextMenuSettingsPage::isInServicesList(const QString
&service
) const
359 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
360 const QModelIndex index
= m_serviceModel
->index(i
, 0);
361 if (m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
368 void ContextMenuSettingsPage::addRow(const QString
&icon
,
370 const QString
&value
,
373 m_serviceModel
->insertRow(0);
375 const QModelIndex index
= m_serviceModel
->index(0, 0);
376 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
377 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
378 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
379 m_serviceModel
->setData(index
, checked
, Qt::CheckStateRole
);