]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/services/servicessettingspage.cpp
Clear services model after KNS entries changed
[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, [this](const KNS3::Entry::List &changedEntries) {
77 if (!changedEntries.isEmpty()) {
78 m_serviceModel->clear();
79 loadServices();
80 }
81 });
82
83 #endif
84
85 topLayout->addWidget(label);
86 topLayout->addWidget(m_searchLineEdit);
87 topLayout->addWidget(m_listView);
88 #ifndef Q_OS_WIN
89 topLayout->addWidget(downloadButton);
90 #endif
91
92 m_enabledVcsPlugins = VersionControlSettings::enabledPlugins();
93 std::sort(m_enabledVcsPlugins.begin(), m_enabledVcsPlugins.end());
94 }
95
96 ServicesSettingsPage::~ServicesSettingsPage() = default;
97
98 void ServicesSettingsPage::applySettings()
99 {
100 if (!m_initialized) {
101 return;
102 }
103
104 KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
105 KConfigGroup showGroup = config.group("Show");
106
107 QStringList enabledPlugins;
108
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();
114
115 if (service.startsWith(VersionControlServicePrefix)) {
116 if (checked) {
117 enabledPlugins.append(model->data(index, Qt::DisplayRole).toString());
118 }
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);
123 configGroup.sync();
124 } else if (service == QLatin1String(CopyToMoveToService)) {
125 GeneralSettings::setShowCopyMoveMenu(checked);
126 GeneralSettings::self()->save();
127 } else {
128 showGroup.writeEntry(service, checked);
129 }
130 }
131
132 showGroup.sync();
133
134 if (m_enabledVcsPlugins != enabledPlugins) {
135 VersionControlSettings::setEnabledPlugins(enabledPlugins);
136 VersionControlSettings::self()->save();
137
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"));
143 }
144 }
145
146 void ServicesSettingsPage::restoreDefaults()
147 {
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();
152
153 const bool checked = !service.startsWith(VersionControlServicePrefix)
154 && service != QLatin1String(DeleteService)
155 && service != QLatin1String(CopyToMoveToService);
156 model->setData(index, checked, Qt::CheckStateRole);
157 }
158 }
159
160 void ServicesSettingsPage::showEvent(QShowEvent* event)
161 {
162 if (!event->spontaneous() && !m_initialized) {
163 loadServices();
164
165 loadVersionControlSystems();
166
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"),
172 DeleteService,
173 configGroup.readEntry("ShowDeleteCommand", ShowDeleteDefault));
174
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"),
178 CopyToMoveToService,
179 GeneralSettings::showCopyMoveMenu());
180
181 m_sortModel->sort(Qt::DisplayRole);
182
183 m_initialized = true;
184 }
185 SettingsPageBase::showEvent(event);
186 }
187
188 void ServicesSettingsPage::loadServices()
189 {
190 const KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
191 const KConfigGroup showGroup = config.group("Show");
192
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);
198
199 const KDesktopFile desktopFile(file);
200 const QString subMenuName = desktopFile.desktopGroup().readEntry("X-KDE-Submenu");
201
202 for (const KServiceAction &action : serviceActions) {
203 const QString serviceName = action.name();
204 const bool addService = !action.noDisplay() && !action.isSeparator() && !isInServicesList(serviceName);
205
206 if (addService) {
207 const QString itemName = subMenuName.isEmpty()
208 ? action.text()
209 : i18nc("@item:inmenu", "%1: %2", subMenuName, action.text());
210 const bool checked = showGroup.readEntry(serviceName, true);
211 addRow(action.icon(), itemName, serviceName, checked);
212 }
213 }
214 }
215
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);
223 }
224 }
225
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"));
229 });
230
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);
236 }
237 }
238
239 m_sortModel->sort(Qt::DisplayRole);
240 m_searchLineEdit->setFocus(Qt::OtherFocusReason);
241 }
242
243 void ServicesSettingsPage::loadVersionControlSystems()
244 {
245 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
246
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"),
252 pluginName,
253 VersionControlServicePrefix + pluginName,
254 enabledPlugins.contains(pluginName));
255 }
256
257 m_sortModel->sort(Qt::DisplayRole);
258 }
259
260 bool ServicesSettingsPage::isInServicesList(const QString &service) const
261 {
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) {
265 return true;
266 }
267 }
268 return false;
269 }
270
271 void ServicesSettingsPage::addRow(const QString &icon,
272 const QString &text,
273 const QString &value,
274 bool checked)
275 {
276 m_serviceModel->insertRow(0);
277
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);
283 }
284