2 * SPDX-FileCopyrightText: 2009-2010 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "servicessettingspage.h"
9 #include "dolphin_generalsettings.h"
10 #include "dolphin_versioncontrolsettings.h"
11 #include "settings/serviceitemdelegate.h"
12 #include "settings/servicemodel.h"
14 #include <KDesktopFile>
15 #include <KLocalizedString>
16 #include <KMessageBox>
17 #include <KNS3/Button>
18 #include <KPluginMetaData>
20 #include <KServiceTypeTrader>
21 #include <KDesktopFileActions>
23 #include <QGridLayout>
25 #include <QListWidget>
27 #include <QSortFilterProxyModel>
32 const bool ShowDeleteDefault
= false;
33 const char VersionControlServicePrefix
[] = "_version_control_";
34 const char DeleteService
[] = "_delete";
35 const char CopyToMoveToService
[] ="_copy_to_move_to";
38 ServicesSettingsPage::ServicesSettingsPage(QWidget
* parent
) :
39 SettingsPageBase(parent
),
41 m_serviceModel(nullptr),
46 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
48 QLabel
* label
= new QLabel(i18nc("@label:textbox",
49 "Select which services should "
50 "be shown in the context menu:"), this);
51 label
->setWordWrap(true);
52 m_searchLineEdit
= new QLineEdit(this);
53 m_searchLineEdit
->setPlaceholderText(i18nc("@label:textbox", "Search..."));
54 connect(m_searchLineEdit
, &QLineEdit::textChanged
, this, [this](const QString
&filter
){
55 m_sortModel
->setFilterFixedString(filter
);
58 m_listView
= new QListView(this);
59 auto *delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
60 m_serviceModel
= new ServiceModel(this);
61 m_sortModel
= new QSortFilterProxyModel(this);
62 m_sortModel
->setSourceModel(m_serviceModel
);
63 m_sortModel
->setSortRole(Qt::DisplayRole
);
64 m_sortModel
->setSortLocaleAware(true);
65 m_sortModel
->setFilterRole(Qt::DisplayRole
);
66 m_sortModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
67 m_listView
->setModel(m_sortModel
);
68 m_listView
->setItemDelegate(delegate
);
69 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
70 connect(m_listView
, &QListView::clicked
, this, &ServicesSettingsPage::changed
);
73 auto *downloadButton
= new KNS3::Button(i18nc("@action:button", "Download New Services..."),
74 QStringLiteral("servicemenu.knsrc"),
76 connect(downloadButton
, &KNS3::Button::dialogFinished
, this, [this](const KNS3::Entry::List
&changedEntries
) {
77 if (!changedEntries
.isEmpty()) {
78 m_serviceModel
->clear();
85 topLayout
->addWidget(label
);
86 topLayout
->addWidget(m_searchLineEdit
);
87 topLayout
->addWidget(m_listView
);
89 topLayout
->addWidget(downloadButton
);
92 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
93 std::sort(m_enabledVcsPlugins
.begin(), m_enabledVcsPlugins
.end());
96 ServicesSettingsPage::~ServicesSettingsPage() = default;
98 void ServicesSettingsPage::applySettings()
100 if (!m_initialized
) {
104 KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
105 KConfigGroup showGroup
= config
.group("Show");
107 QStringList enabledPlugins
;
109 const QAbstractItemModel
*model
= m_listView
->model();
110 for (int i
= 0; i
< model
->rowCount(); ++i
) {
111 const QModelIndex index
= model
->index(i
, 0);
112 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
113 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
115 if (service
.startsWith(VersionControlServicePrefix
)) {
117 enabledPlugins
.append(model
->data(index
, Qt::DisplayRole
).toString());
119 } else if (service
== QLatin1String(DeleteService
)) {
120 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals
);
121 KConfigGroup
configGroup(globalConfig
, "KDE");
122 configGroup
.writeEntry("ShowDeleteCommand", checked
);
124 } else if (service
== QLatin1String(CopyToMoveToService
)) {
125 GeneralSettings::setShowCopyMoveMenu(checked
);
126 GeneralSettings::self()->save();
128 showGroup
.writeEntry(service
, checked
);
134 if (m_enabledVcsPlugins
!= enabledPlugins
) {
135 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
136 VersionControlSettings::self()->save();
138 KMessageBox::information(window(),
139 i18nc("@info", "Dolphin must be restarted to apply the "
140 "updated version control systems settings."),
141 QString(), // default title
142 QStringLiteral("ShowVcsRestartInformation"));
146 void ServicesSettingsPage::restoreDefaults()
148 QAbstractItemModel
* model
= m_listView
->model();
149 for (int i
= 0; i
< model
->rowCount(); ++i
) {
150 const QModelIndex index
= model
->index(i
, 0);
151 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
153 const bool checked
= !service
.startsWith(VersionControlServicePrefix
)
154 && service
!= QLatin1String(DeleteService
)
155 && service
!= QLatin1String(CopyToMoveToService
);
156 model
->setData(index
, checked
, Qt::CheckStateRole
);
160 void ServicesSettingsPage::showEvent(QShowEvent
* event
)
162 if (!event
->spontaneous() && !m_initialized
) {
165 loadVersionControlSystems();
167 // Add "Show 'Delete' command" as service
168 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals
);
169 KConfigGroup
configGroup(globalConfig
, "KDE");
170 addRow(QStringLiteral("edit-delete"),
171 i18nc("@option:check", "Delete"),
173 configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
175 // Add "Show 'Copy To' and 'Move To' commands" as service
176 addRow(QStringLiteral("edit-copy"),
177 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
179 GeneralSettings::showCopyMoveMenu());
181 m_sortModel
->sort(Qt::DisplayRole
);
183 m_initialized
= true;
185 SettingsPageBase::showEvent(event
);
188 void ServicesSettingsPage::loadServices()
190 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
191 const KConfigGroup showGroup
= config
.group("Show");
193 // Load generic services
194 const KService::List entries
= KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
195 for (const KService::Ptr
&service
: entries
) {
196 const QString file
= QStandardPaths::locate(QStandardPaths::GenericDataLocation
, "kservices5/" % service
->entryPath());
197 const QList
<KServiceAction
> serviceActions
= KDesktopFileActions::userDefinedServices(file
, true);
199 const KDesktopFile
desktopFile(file
);
200 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
202 for (const KServiceAction
&action
: serviceActions
) {
203 const QString serviceName
= action
.name();
204 const bool addService
= !action
.noDisplay() && !action
.isSeparator() && !isInServicesList(serviceName
);
207 const QString itemName
= subMenuName
.isEmpty()
209 : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
210 const bool checked
= showGroup
.readEntry(serviceName
, true);
211 addRow(action
.icon(), itemName
, serviceName
, checked
);
216 // Load service plugins that implement the KFileItemActionPlugin interface
217 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
218 for (const KService::Ptr
&service
: pluginServices
) {
219 const QString desktopEntryName
= service
->desktopEntryName();
220 if (!isInServicesList(desktopEntryName
)) {
221 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
222 addRow(service
->icon(), service
->name(), desktopEntryName
, checked
);
226 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
227 const auto jsonPlugins
= KPluginLoader::findPlugins(QStringLiteral("kf5/kfileitemaction"), [](const KPluginMetaData
& metaData
) {
228 return metaData
.serviceTypes().contains(QLatin1String("KFileItemAction/Plugin"));
231 for (const auto &jsonMetadata
: jsonPlugins
) {
232 const QString desktopEntryName
= jsonMetadata
.pluginId();
233 if (!isInServicesList(desktopEntryName
)) {
234 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
235 addRow(jsonMetadata
.iconName(), jsonMetadata
.name(), desktopEntryName
, checked
);
239 m_sortModel
->sort(Qt::DisplayRole
);
240 m_searchLineEdit
->setFocus(Qt::OtherFocusReason
);
243 void ServicesSettingsPage::loadVersionControlSystems()
245 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
247 // Create a checkbox for each available version control plugin
248 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("FileViewVersionControlPlugin"));
249 for (const auto &plugin
: pluginServices
) {
250 const QString pluginName
= plugin
->name();
251 addRow(QStringLiteral("code-class"),
253 VersionControlServicePrefix
+ pluginName
,
254 enabledPlugins
.contains(pluginName
));
257 m_sortModel
->sort(Qt::DisplayRole
);
260 bool ServicesSettingsPage::isInServicesList(const QString
&service
) const
262 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
263 const QModelIndex index
= m_serviceModel
->index(i
, 0);
264 if (m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
271 void ServicesSettingsPage::addRow(const QString
&icon
,
273 const QString
&value
,
276 m_serviceModel
->insertRow(0);
278 const QModelIndex index
= m_serviceModel
->index(0, 0);
279 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
280 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
281 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
282 m_serviceModel
->setData(index
, checked
, Qt::CheckStateRole
);