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
);
72 auto *downloadButton
= new KNS3::Button(i18nc("@action:button", "Download New Services..."),
73 QStringLiteral("servicemenu.knsrc"),
75 connect(downloadButton
, &KNS3::Button::dialogFinished
, this, &ServicesSettingsPage::loadServices
);
77 topLayout
->addWidget(label
);
78 topLayout
->addWidget(m_searchLineEdit
);
79 topLayout
->addWidget(m_listView
);
80 topLayout
->addWidget(downloadButton
);
82 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
83 std::sort(m_enabledVcsPlugins
.begin(), m_enabledVcsPlugins
.end());
86 ServicesSettingsPage::~ServicesSettingsPage() = default;
88 void ServicesSettingsPage::applySettings()
94 KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
95 KConfigGroup showGroup
= config
.group("Show");
97 QStringList enabledPlugins
;
99 const QAbstractItemModel
*model
= m_listView
->model();
100 for (int i
= 0; i
< model
->rowCount(); ++i
) {
101 const QModelIndex index
= model
->index(i
, 0);
102 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
103 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
105 if (service
.startsWith(VersionControlServicePrefix
)) {
107 enabledPlugins
.append(model
->data(index
, Qt::DisplayRole
).toString());
109 } else if (service
== QLatin1String(DeleteService
)) {
110 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals
);
111 KConfigGroup
configGroup(globalConfig
, "KDE");
112 configGroup
.writeEntry("ShowDeleteCommand", checked
);
114 } else if (service
== QLatin1String(CopyToMoveToService
)) {
115 GeneralSettings::setShowCopyMoveMenu(checked
);
116 GeneralSettings::self()->save();
118 showGroup
.writeEntry(service
, checked
);
124 if (m_enabledVcsPlugins
!= enabledPlugins
) {
125 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
126 VersionControlSettings::self()->save();
128 KMessageBox::information(window(),
129 i18nc("@info", "Dolphin must be restarted to apply the "
130 "updated version control systems settings."),
131 QString(), // default title
132 QStringLiteral("ShowVcsRestartInformation"));
136 void ServicesSettingsPage::restoreDefaults()
138 QAbstractItemModel
* model
= m_listView
->model();
139 for (int i
= 0; i
< model
->rowCount(); ++i
) {
140 const QModelIndex index
= model
->index(i
, 0);
141 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
143 const bool checked
= !service
.startsWith(VersionControlServicePrefix
)
144 && service
!= QLatin1String(DeleteService
)
145 && service
!= QLatin1String(CopyToMoveToService
);
146 model
->setData(index
, checked
, Qt::CheckStateRole
);
150 void ServicesSettingsPage::showEvent(QShowEvent
* event
)
152 if (!event
->spontaneous() && !m_initialized
) {
155 loadVersionControlSystems();
157 // Add "Show 'Delete' command" as service
158 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals
);
159 KConfigGroup
configGroup(globalConfig
, "KDE");
160 addRow(QStringLiteral("edit-delete"),
161 i18nc("@option:check", "Delete"),
163 configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
165 // Add "Show 'Copy To' and 'Move To' commands" as service
166 addRow(QStringLiteral("edit-copy"),
167 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
169 GeneralSettings::showCopyMoveMenu());
171 m_sortModel
->sort(Qt::DisplayRole
);
173 m_initialized
= true;
175 SettingsPageBase::showEvent(event
);
178 void ServicesSettingsPage::loadServices()
180 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
181 const KConfigGroup showGroup
= config
.group("Show");
183 // Load generic services
184 const KService::List entries
= KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
185 for (const KService::Ptr
&service
: entries
) {
186 const QString file
= QStandardPaths::locate(QStandardPaths::GenericDataLocation
, "kservices5/" % service
->entryPath());
187 const QList
<KServiceAction
> serviceActions
= KDesktopFileActions::userDefinedServices(file
, true);
189 const KDesktopFile
desktopFile(file
);
190 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
192 for (const KServiceAction
&action
: serviceActions
) {
193 const QString serviceName
= action
.name();
194 const bool addService
= !action
.noDisplay() && !action
.isSeparator() && !isInServicesList(serviceName
);
197 const QString itemName
= subMenuName
.isEmpty()
199 : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
200 const bool checked
= showGroup
.readEntry(serviceName
, true);
201 addRow(action
.icon(), itemName
, serviceName
, checked
);
206 // Load service plugins that implement the KFileItemActionPlugin interface
207 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
208 for (const KService::Ptr
&service
: pluginServices
) {
209 const QString desktopEntryName
= service
->desktopEntryName();
210 if (!isInServicesList(desktopEntryName
)) {
211 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
212 addRow(service
->icon(), service
->name(), desktopEntryName
, checked
);
216 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
217 const auto jsonPlugins
= KPluginLoader::findPlugins(QStringLiteral("kf5/kfileitemaction"), [](const KPluginMetaData
& metaData
) {
218 return metaData
.serviceTypes().contains(QLatin1String("KFileItemAction/Plugin"));
221 for (const auto &jsonMetadata
: jsonPlugins
) {
222 const QString desktopEntryName
= jsonMetadata
.pluginId();
223 if (!isInServicesList(desktopEntryName
)) {
224 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
225 addRow(jsonMetadata
.iconName(), jsonMetadata
.name(), desktopEntryName
, checked
);
229 m_sortModel
->sort(Qt::DisplayRole
);
230 m_searchLineEdit
->setFocus(Qt::OtherFocusReason
);
233 void ServicesSettingsPage::loadVersionControlSystems()
235 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
237 // Create a checkbox for each available version control plugin
238 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("FileViewVersionControlPlugin"));
239 for (const auto &plugin
: pluginServices
) {
240 const QString pluginName
= plugin
->name();
241 addRow(QStringLiteral("code-class"),
243 VersionControlServicePrefix
+ pluginName
,
244 enabledPlugins
.contains(pluginName
));
247 m_sortModel
->sort(Qt::DisplayRole
);
250 bool ServicesSettingsPage::isInServicesList(const QString
&service
) const
252 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
253 const QModelIndex index
= m_serviceModel
->index(i
, 0);
254 if (m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
261 void ServicesSettingsPage::addRow(const QString
&icon
,
263 const QString
&value
,
266 m_serviceModel
->insertRow(0);
268 const QModelIndex index
= m_serviceModel
->index(0, 0);
269 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
270 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
271 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
272 m_serviceModel
->setData(index
, checked
, Qt::CheckStateRole
);