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, &ServicesSettingsPage::loadServices
);
79 topLayout
->addWidget(label
);
80 topLayout
->addWidget(m_searchLineEdit
);
81 topLayout
->addWidget(m_listView
);
83 topLayout
->addWidget(downloadButton
);
86 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
87 std::sort(m_enabledVcsPlugins
.begin(), m_enabledVcsPlugins
.end());
90 ServicesSettingsPage::~ServicesSettingsPage() = default;
92 void ServicesSettingsPage::applySettings()
98 KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
99 KConfigGroup showGroup
= config
.group("Show");
101 QStringList enabledPlugins
;
103 const QAbstractItemModel
*model
= m_listView
->model();
104 for (int i
= 0; i
< model
->rowCount(); ++i
) {
105 const QModelIndex index
= model
->index(i
, 0);
106 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
107 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
109 if (service
.startsWith(VersionControlServicePrefix
)) {
111 enabledPlugins
.append(model
->data(index
, Qt::DisplayRole
).toString());
113 } else if (service
== QLatin1String(DeleteService
)) {
114 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals
);
115 KConfigGroup
configGroup(globalConfig
, "KDE");
116 configGroup
.writeEntry("ShowDeleteCommand", checked
);
118 } else if (service
== QLatin1String(CopyToMoveToService
)) {
119 GeneralSettings::setShowCopyMoveMenu(checked
);
120 GeneralSettings::self()->save();
122 showGroup
.writeEntry(service
, checked
);
128 if (m_enabledVcsPlugins
!= enabledPlugins
) {
129 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
130 VersionControlSettings::self()->save();
132 KMessageBox::information(window(),
133 i18nc("@info", "Dolphin must be restarted to apply the "
134 "updated version control systems settings."),
135 QString(), // default title
136 QStringLiteral("ShowVcsRestartInformation"));
140 void ServicesSettingsPage::restoreDefaults()
142 QAbstractItemModel
* model
= m_listView
->model();
143 for (int i
= 0; i
< model
->rowCount(); ++i
) {
144 const QModelIndex index
= model
->index(i
, 0);
145 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
147 const bool checked
= !service
.startsWith(VersionControlServicePrefix
)
148 && service
!= QLatin1String(DeleteService
)
149 && service
!= QLatin1String(CopyToMoveToService
);
150 model
->setData(index
, checked
, Qt::CheckStateRole
);
154 void ServicesSettingsPage::showEvent(QShowEvent
* event
)
156 if (!event
->spontaneous() && !m_initialized
) {
159 loadVersionControlSystems();
161 // Add "Show 'Delete' command" as service
162 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals
);
163 KConfigGroup
configGroup(globalConfig
, "KDE");
164 addRow(QStringLiteral("edit-delete"),
165 i18nc("@option:check", "Delete"),
167 configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
169 // Add "Show 'Copy To' and 'Move To' commands" as service
170 addRow(QStringLiteral("edit-copy"),
171 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
173 GeneralSettings::showCopyMoveMenu());
175 m_sortModel
->sort(Qt::DisplayRole
);
177 m_initialized
= true;
179 SettingsPageBase::showEvent(event
);
182 void ServicesSettingsPage::loadServices()
184 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
185 const KConfigGroup showGroup
= config
.group("Show");
187 // Load generic services
188 const KService::List entries
= KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
189 for (const KService::Ptr
&service
: entries
) {
190 const QString file
= QStandardPaths::locate(QStandardPaths::GenericDataLocation
, "kservices5/" % service
->entryPath());
191 const QList
<KServiceAction
> serviceActions
= KDesktopFileActions::userDefinedServices(file
, true);
193 const KDesktopFile
desktopFile(file
);
194 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
196 for (const KServiceAction
&action
: serviceActions
) {
197 const QString serviceName
= action
.name();
198 const bool addService
= !action
.noDisplay() && !action
.isSeparator() && !isInServicesList(serviceName
);
201 const QString itemName
= subMenuName
.isEmpty()
203 : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
204 const bool checked
= showGroup
.readEntry(serviceName
, true);
205 addRow(action
.icon(), itemName
, serviceName
, checked
);
210 // Load service plugins that implement the KFileItemActionPlugin interface
211 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
212 for (const KService::Ptr
&service
: pluginServices
) {
213 const QString desktopEntryName
= service
->desktopEntryName();
214 if (!isInServicesList(desktopEntryName
)) {
215 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
216 addRow(service
->icon(), service
->name(), desktopEntryName
, checked
);
220 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
221 const auto jsonPlugins
= KPluginLoader::findPlugins(QStringLiteral("kf5/kfileitemaction"), [](const KPluginMetaData
& metaData
) {
222 return metaData
.serviceTypes().contains(QLatin1String("KFileItemAction/Plugin"));
225 for (const auto &jsonMetadata
: jsonPlugins
) {
226 const QString desktopEntryName
= jsonMetadata
.pluginId();
227 if (!isInServicesList(desktopEntryName
)) {
228 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
229 addRow(jsonMetadata
.iconName(), jsonMetadata
.name(), desktopEntryName
, checked
);
233 m_sortModel
->sort(Qt::DisplayRole
);
234 m_searchLineEdit
->setFocus(Qt::OtherFocusReason
);
237 void ServicesSettingsPage::loadVersionControlSystems()
239 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
241 // Create a checkbox for each available version control plugin
242 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("FileViewVersionControlPlugin"));
243 for (const auto &plugin
: pluginServices
) {
244 const QString pluginName
= plugin
->name();
245 addRow(QStringLiteral("code-class"),
247 VersionControlServicePrefix
+ pluginName
,
248 enabledPlugins
.contains(pluginName
));
251 m_sortModel
->sort(Qt::DisplayRole
);
254 bool ServicesSettingsPage::isInServicesList(const QString
&service
) const
256 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
257 const QModelIndex index
= m_serviceModel
->index(i
, 0);
258 if (m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
265 void ServicesSettingsPage::addRow(const QString
&icon
,
267 const QString
&value
,
270 m_serviceModel
->insertRow(0);
272 const QModelIndex index
= m_serviceModel
->index(0, 0);
273 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
274 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
275 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
276 m_serviceModel
->setData(index
, checked
, Qt::CheckStateRole
);