]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/services/servicessettingspage.cpp
GIT_SILENT Update Appstream for new release
[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 #ifndef Q_OS_WIN
73 auto *downloadButton = new KNS3::Button(i18nc("@action:button", "Download New Services..."),
74 QStringLiteral("servicemenu.knsrc"),
75 this);
76 connect(downloadButton, &KNS3::Button::dialogFinished, this, &ServicesSettingsPage::loadServices);
77 #endif
78
79 topLayout->addWidget(label);
80 topLayout->addWidget(m_searchLineEdit);
81 topLayout->addWidget(m_listView);
82 #ifndef Q_OS_WIN
83 topLayout->addWidget(downloadButton);
84 #endif
85
86 m_enabledVcsPlugins = VersionControlSettings::enabledPlugins();
87 std::sort(m_enabledVcsPlugins.begin(), m_enabledVcsPlugins.end());
88 }
89
90 ServicesSettingsPage::~ServicesSettingsPage() = default;
91
92 void ServicesSettingsPage::applySettings()
93 {
94 if (!m_initialized) {
95 return;
96 }
97
98 KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
99 KConfigGroup showGroup = config.group("Show");
100
101 QStringList enabledPlugins;
102
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();
108
109 if (service.startsWith(VersionControlServicePrefix)) {
110 if (checked) {
111 enabledPlugins.append(model->data(index, Qt::DisplayRole).toString());
112 }
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);
117 configGroup.sync();
118 } else if (service == QLatin1String(CopyToMoveToService)) {
119 GeneralSettings::setShowCopyMoveMenu(checked);
120 GeneralSettings::self()->save();
121 } else {
122 showGroup.writeEntry(service, checked);
123 }
124 }
125
126 showGroup.sync();
127
128 if (m_enabledVcsPlugins != enabledPlugins) {
129 VersionControlSettings::setEnabledPlugins(enabledPlugins);
130 VersionControlSettings::self()->save();
131
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"));
137 }
138 }
139
140 void ServicesSettingsPage::restoreDefaults()
141 {
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();
146
147 const bool checked = !service.startsWith(VersionControlServicePrefix)
148 && service != QLatin1String(DeleteService)
149 && service != QLatin1String(CopyToMoveToService);
150 model->setData(index, checked, Qt::CheckStateRole);
151 }
152 }
153
154 void ServicesSettingsPage::showEvent(QShowEvent* event)
155 {
156 if (!event->spontaneous() && !m_initialized) {
157 loadServices();
158
159 loadVersionControlSystems();
160
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"),
166 DeleteService,
167 configGroup.readEntry("ShowDeleteCommand", ShowDeleteDefault));
168
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"),
172 CopyToMoveToService,
173 GeneralSettings::showCopyMoveMenu());
174
175 m_sortModel->sort(Qt::DisplayRole);
176
177 m_initialized = true;
178 }
179 SettingsPageBase::showEvent(event);
180 }
181
182 void ServicesSettingsPage::loadServices()
183 {
184 const KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
185 const KConfigGroup showGroup = config.group("Show");
186
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);
192
193 const KDesktopFile desktopFile(file);
194 const QString subMenuName = desktopFile.desktopGroup().readEntry("X-KDE-Submenu");
195
196 for (const KServiceAction &action : serviceActions) {
197 const QString serviceName = action.name();
198 const bool addService = !action.noDisplay() && !action.isSeparator() && !isInServicesList(serviceName);
199
200 if (addService) {
201 const QString itemName = subMenuName.isEmpty()
202 ? action.text()
203 : i18nc("@item:inmenu", "%1: %2", subMenuName, action.text());
204 const bool checked = showGroup.readEntry(serviceName, true);
205 addRow(action.icon(), itemName, serviceName, checked);
206 }
207 }
208 }
209
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);
217 }
218 }
219
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"));
223 });
224
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);
230 }
231 }
232
233 m_sortModel->sort(Qt::DisplayRole);
234 m_searchLineEdit->setFocus(Qt::OtherFocusReason);
235 }
236
237 void ServicesSettingsPage::loadVersionControlSystems()
238 {
239 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
240
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"),
246 pluginName,
247 VersionControlServicePrefix + pluginName,
248 enabledPlugins.contains(pluginName));
249 }
250
251 m_sortModel->sort(Qt::DisplayRole);
252 }
253
254 bool ServicesSettingsPage::isInServicesList(const QString &service) const
255 {
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) {
259 return true;
260 }
261 }
262 return false;
263 }
264
265 void ServicesSettingsPage::addRow(const QString &icon,
266 const QString &text,
267 const QString &value,
268 bool checked)
269 {
270 m_serviceModel->insertRow(0);
271
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);
277 }
278