]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/services/servicessettingspage.cpp
Output of licensedigger + manual cleanup afterwards.
[dolphin.git] / src / settings / services / servicessettingspage.cpp
1 /*
2 * SPDX-FileCopyrightText: 2009-2010 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "servicessettingspage.h"
8
9 #include "dolphin_generalsettings.h"
10 #include "dolphin_versioncontrolsettings.h"
11 #include "settings/serviceitemdelegate.h"
12 #include "settings/servicemodel.h"
13
14 #include <KDesktopFile>
15 #include <KLocalizedString>
16 #include <KMessageBox>
17 #include <KNS3/Button>
18 #include <KPluginMetaData>
19 #include <KService>
20 #include <KServiceTypeTrader>
21 #include <KDesktopFileActions>
22
23 #include <QGridLayout>
24 #include <QLabel>
25 #include <QListWidget>
26 #include <QShowEvent>
27 #include <QSortFilterProxyModel>
28 #include <QLineEdit>
29
30 namespace
31 {
32 const bool ShowDeleteDefault = false;
33 const char VersionControlServicePrefix[] = "_version_control_";
34 const char DeleteService[] = "_delete";
35 const char CopyToMoveToService[] ="_copy_to_move_to";
36 }
37
38 ServicesSettingsPage::ServicesSettingsPage(QWidget* parent) :
39 SettingsPageBase(parent),
40 m_initialized(false),
41 m_serviceModel(nullptr),
42 m_sortModel(nullptr),
43 m_listView(nullptr),
44 m_enabledVcsPlugins()
45 {
46 QVBoxLayout* topLayout = new QVBoxLayout(this);
47
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);
56 });
57
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);
71
72 auto *downloadButton = new KNS3::Button(i18nc("@action:button", "Download New Services..."),
73 QStringLiteral("servicemenu.knsrc"),
74 this);
75 connect(downloadButton, &KNS3::Button::dialogFinished, this, &ServicesSettingsPage::loadServices);
76
77 topLayout->addWidget(label);
78 topLayout->addWidget(m_searchLineEdit);
79 topLayout->addWidget(m_listView);
80 topLayout->addWidget(downloadButton);
81
82 m_enabledVcsPlugins = VersionControlSettings::enabledPlugins();
83 std::sort(m_enabledVcsPlugins.begin(), m_enabledVcsPlugins.end());
84 }
85
86 ServicesSettingsPage::~ServicesSettingsPage() = default;
87
88 void ServicesSettingsPage::applySettings()
89 {
90 if (!m_initialized) {
91 return;
92 }
93
94 KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
95 KConfigGroup showGroup = config.group("Show");
96
97 QStringList enabledPlugins;
98
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();
104
105 if (service.startsWith(VersionControlServicePrefix)) {
106 if (checked) {
107 enabledPlugins.append(model->data(index, Qt::DisplayRole).toString());
108 }
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);
113 configGroup.sync();
114 } else if (service == QLatin1String(CopyToMoveToService)) {
115 GeneralSettings::setShowCopyMoveMenu(checked);
116 GeneralSettings::self()->save();
117 } else {
118 showGroup.writeEntry(service, checked);
119 }
120 }
121
122 showGroup.sync();
123
124 if (m_enabledVcsPlugins != enabledPlugins) {
125 VersionControlSettings::setEnabledPlugins(enabledPlugins);
126 VersionControlSettings::self()->save();
127
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"));
133 }
134 }
135
136 void ServicesSettingsPage::restoreDefaults()
137 {
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();
142
143 const bool checked = !service.startsWith(VersionControlServicePrefix)
144 && service != QLatin1String(DeleteService)
145 && service != QLatin1String(CopyToMoveToService);
146 model->setData(index, checked, Qt::CheckStateRole);
147 }
148 }
149
150 void ServicesSettingsPage::showEvent(QShowEvent* event)
151 {
152 if (!event->spontaneous() && !m_initialized) {
153 loadServices();
154
155 loadVersionControlSystems();
156
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"),
162 DeleteService,
163 configGroup.readEntry("ShowDeleteCommand", ShowDeleteDefault));
164
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"),
168 CopyToMoveToService,
169 GeneralSettings::showCopyMoveMenu());
170
171 m_sortModel->sort(Qt::DisplayRole);
172
173 m_initialized = true;
174 }
175 SettingsPageBase::showEvent(event);
176 }
177
178 void ServicesSettingsPage::loadServices()
179 {
180 const KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
181 const KConfigGroup showGroup = config.group("Show");
182
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);
188
189 const KDesktopFile desktopFile(file);
190 const QString subMenuName = desktopFile.desktopGroup().readEntry("X-KDE-Submenu");
191
192 for (const KServiceAction &action : serviceActions) {
193 const QString serviceName = action.name();
194 const bool addService = !action.noDisplay() && !action.isSeparator() && !isInServicesList(serviceName);
195
196 if (addService) {
197 const QString itemName = subMenuName.isEmpty()
198 ? action.text()
199 : i18nc("@item:inmenu", "%1: %2", subMenuName, action.text());
200 const bool checked = showGroup.readEntry(serviceName, true);
201 addRow(action.icon(), itemName, serviceName, checked);
202 }
203 }
204 }
205
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);
213 }
214 }
215
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"));
219 });
220
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);
226 }
227 }
228
229 m_sortModel->sort(Qt::DisplayRole);
230 m_searchLineEdit->setFocus(Qt::OtherFocusReason);
231 }
232
233 void ServicesSettingsPage::loadVersionControlSystems()
234 {
235 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
236
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"),
242 pluginName,
243 VersionControlServicePrefix + pluginName,
244 enabledPlugins.contains(pluginName));
245 }
246
247 m_sortModel->sort(Qt::DisplayRole);
248 }
249
250 bool ServicesSettingsPage::isInServicesList(const QString &service) const
251 {
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) {
255 return true;
256 }
257 }
258 return false;
259 }
260
261 void ServicesSettingsPage::addRow(const QString &icon,
262 const QString &text,
263 const QString &value,
264 bool checked)
265 {
266 m_serviceModel->insertRow(0);
267
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);
273 }
274