]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/contextmenu/contextmenusettingspage.cpp
Apply Elvis suggestions
[dolphin.git] / src / settings / contextmenu / contextmenusettingspage.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 "contextmenusettingspage.h"
8
9 #include "dolphin_generalsettings.h"
10 #include "dolphin_versioncontrolsettings.h"
11 #include "dolphin_contextmenusettings.h"
12 #include "settings/serviceitemdelegate.h"
13 #include "settings/servicemodel.h"
14
15 #include <KDesktopFile>
16 #include <KLocalizedString>
17 #include <KMessageBox>
18 #include <KNS3/Button>
19 #include <KPluginMetaData>
20 #include <KService>
21 #include <KServiceTypeTrader>
22 #include <KDesktopFileActions>
23
24 #include <QGridLayout>
25 #include <QLabel>
26 #include <QListWidget>
27 #include <QScroller>
28 #include <QShowEvent>
29 #include <QSortFilterProxyModel>
30 #include <QLineEdit>
31
32 namespace
33 {
34 const bool ShowDeleteDefault = false;
35 const char VersionControlServicePrefix[] = "_version_control_";
36 const char DeleteService[] = "_delete";
37 const char CopyToMoveToService[] ="_copy_to_move_to";
38 }
39
40 ContextMenuSettingsPage::ContextMenuSettingsPage(QWidget* parent,
41 KActionCollection* actions,
42 QStringList actionIds) :
43 SettingsPageBase(parent),
44 m_initialized(false),
45 m_serviceModel(nullptr),
46 m_sortModel(nullptr),
47 m_listView(nullptr),
48 m_enabledVcsPlugins(),
49 m_actions(actions),
50 m_actionIds(actionIds)
51 {
52 QVBoxLayout* topLayout = new QVBoxLayout(this);
53
54 QLabel* label = new QLabel(i18nc("@label:textbox",
55 "Select which services should "
56 "be shown in the context menu:"), this);
57 label->setWordWrap(true);
58 m_searchLineEdit = new QLineEdit(this);
59 m_searchLineEdit->setPlaceholderText(i18nc("@label:textbox", "Search..."));
60 connect(m_searchLineEdit, &QLineEdit::textChanged, this, [this](const QString &filter){
61 m_sortModel->setFilterFixedString(filter);
62 });
63
64 m_listView = new QListView(this);
65 QScroller::grabGesture(m_listView->viewport(), QScroller::TouchGesture);
66
67 auto *delegate = new ServiceItemDelegate(m_listView, m_listView);
68 m_serviceModel = new ServiceModel(this);
69 m_sortModel = new QSortFilterProxyModel(this);
70 m_sortModel->setSourceModel(m_serviceModel);
71 m_sortModel->setSortRole(Qt::DisplayRole);
72 m_sortModel->setSortLocaleAware(true);
73 m_sortModel->setFilterRole(Qt::DisplayRole);
74 m_sortModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
75 m_listView->setModel(m_sortModel);
76 m_listView->setItemDelegate(delegate);
77 m_listView->setVerticalScrollMode(QListView::ScrollPerPixel);
78 connect(m_listView, &QListView::clicked, this, &ContextMenuSettingsPage::changed);
79
80 #ifndef Q_OS_WIN
81 auto *downloadButton = new KNS3::Button(i18nc("@action:button", "Download New Services..."),
82 QStringLiteral("servicemenu.knsrc"),
83 this);
84 connect(downloadButton, &KNS3::Button::dialogFinished, this, [this](const KNS3::Entry::List &changedEntries) {
85 if (!changedEntries.isEmpty()) {
86 m_serviceModel->clear();
87 loadServices();
88 }
89 });
90
91 #endif
92
93 topLayout->addWidget(label);
94 topLayout->addWidget(m_searchLineEdit);
95 topLayout->addWidget(m_listView);
96 #ifndef Q_OS_WIN
97 topLayout->addWidget(downloadButton);
98 #endif
99
100 m_enabledVcsPlugins = VersionControlSettings::enabledPlugins();
101 std::sort(m_enabledVcsPlugins.begin(), m_enabledVcsPlugins.end());
102 }
103
104 ContextMenuSettingsPage::~ContextMenuSettingsPage() {
105 }
106
107 bool ContextMenuSettingsPage::entryVisible(const QString& id)
108 {
109 if (id == "add_to_places") {
110 return ContextMenuSettings::showAddToPlaces();
111 } else if (id == "sort") {
112 return ContextMenuSettings::showSortBy();
113 } else if (id == "view_mode") {
114 return ContextMenuSettings::showViewMode();
115 } else if (id == "open_in_new_tab") {
116 return ContextMenuSettings::showOpenInNewTab();
117 } else if (id == "open_in_new_window") {
118 return ContextMenuSettings::showOpenInNewWindow();
119 } else if (id == "copy_location") {
120 return ContextMenuSettings::showCopyLocation();
121 } else if (id == "duplicate") {
122 return ContextMenuSettings::showDuplicateHere();
123 }
124 return false;
125 }
126
127 void ContextMenuSettingsPage::setEntryVisible(const QString& id, bool visible)
128 {
129 if (id == "add_to_places") {
130 ContextMenuSettings::setShowAddToPlaces(visible);
131 } else if (id == "sort") {
132 ContextMenuSettings::setShowSortBy(visible);
133 } else if (id == "view_mode") {
134 ContextMenuSettings::setShowViewMode(visible);
135 } else if (id == "open_in_new_tab") {
136 ContextMenuSettings::setShowOpenInNewTab(visible);
137 } else if (id == "open_in_new_window") {
138 ContextMenuSettings::setShowOpenInNewWindow(visible);
139 } else if (id == "copy_location") {
140 ContextMenuSettings::setShowCopyLocation(visible);
141 } else if (id == "duplicate") {
142 ContextMenuSettings::setShowDuplicateHere(visible);
143 }
144 }
145
146 void ContextMenuSettingsPage::applySettings()
147 {
148 if (!m_initialized) {
149 return;
150 }
151
152 KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
153 KConfigGroup showGroup = config.group("Show");
154
155 QStringList enabledPlugins;
156
157 const QAbstractItemModel *model = m_listView->model();
158 for (int i = 0; i < model->rowCount(); ++i) {
159 const QModelIndex index = model->index(i, 0);
160 const QString service = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
161 const bool checked = model->data(index, Qt::CheckStateRole).toBool();
162
163 if (service.startsWith(VersionControlServicePrefix)) {
164 if (checked) {
165 enabledPlugins.append(model->data(index, Qt::DisplayRole).toString());
166 }
167 } else if (service == QLatin1String(DeleteService)) {
168 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals);
169 KConfigGroup configGroup(globalConfig, "KDE");
170 configGroup.writeEntry("ShowDeleteCommand", checked);
171 configGroup.sync();
172 } else if (service == QLatin1String(CopyToMoveToService)) {
173 ContextMenuSettings::setShowCopyMoveMenu(checked);
174 ContextMenuSettings::self()->save();
175 } else if (m_actionIds.contains(service)) {
176 setEntryVisible(service, checked);
177 ContextMenuSettings::self()->save();
178 } else {
179 showGroup.writeEntry(service, checked);
180 }
181 }
182
183 showGroup.sync();
184
185 if (m_enabledVcsPlugins != enabledPlugins) {
186 VersionControlSettings::setEnabledPlugins(enabledPlugins);
187 VersionControlSettings::self()->save();
188
189 KMessageBox::information(window(),
190 i18nc("@info", "Dolphin must be restarted to apply the "
191 "updated version control systems settings."),
192 QString(), // default title
193 QStringLiteral("ShowVcsRestartInformation"));
194 }
195 }
196
197 void ContextMenuSettingsPage::restoreDefaults()
198 {
199 QAbstractItemModel* model = m_listView->model();
200 for (int i = 0; i < model->rowCount(); ++i) {
201 const QModelIndex index = model->index(i, 0);
202 const QString service = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
203
204 const bool checked = !service.startsWith(VersionControlServicePrefix)
205 && service != QLatin1String(DeleteService)
206 && service != QLatin1String(CopyToMoveToService);
207 model->setData(index, checked, Qt::CheckStateRole);
208 }
209 }
210
211 void ContextMenuSettingsPage::showEvent(QShowEvent* event)
212 {
213 if (!event->spontaneous() && !m_initialized) {
214 loadServices();
215
216 loadVersionControlSystems();
217
218 // Add "Show 'Delete' command" as service
219 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals);
220 KConfigGroup configGroup(globalConfig, "KDE");
221 addRow(QStringLiteral("edit-delete"),
222 i18nc("@option:check", "Delete"),
223 DeleteService,
224 configGroup.readEntry("ShowDeleteCommand", ShowDeleteDefault));
225
226 // Add "Show 'Copy To' and 'Move To' commands" as service
227 addRow(QStringLiteral("edit-copy"),
228 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
229 CopyToMoveToService,
230 ContextMenuSettings::showCopyMoveMenu());
231
232 // Add other built-in actions
233 for (const QString& id : qAsConst(m_actionIds)) {
234 QAction* action = m_actions->action(id);
235 if (action) {
236 addRow(action->icon().name(), action->text(), id, entryVisible(id));
237 }
238 }
239
240 m_sortModel->sort(Qt::DisplayRole);
241
242 m_initialized = true;
243 }
244 SettingsPageBase::showEvent(event);
245 }
246
247 void ContextMenuSettingsPage::loadServices()
248 {
249 const KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
250 const KConfigGroup showGroup = config.group("Show");
251
252 // Load generic services
253 const KService::List entries = KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
254 for (const KService::Ptr &service : entries) {
255 const QString file = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kservices5/" % service->entryPath());
256 const QList<KServiceAction> serviceActions = KDesktopFileActions::userDefinedServices(file, true);
257
258 const KDesktopFile desktopFile(file);
259 const QString subMenuName = desktopFile.desktopGroup().readEntry("X-KDE-Submenu");
260
261 for (const KServiceAction &action : serviceActions) {
262 const QString serviceName = action.name();
263 const bool addService = !action.noDisplay() && !action.isSeparator() && !isInServicesList(serviceName);
264
265 if (addService) {
266 const QString itemName = subMenuName.isEmpty()
267 ? action.text()
268 : i18nc("@item:inmenu", "%1: %2", subMenuName, action.text());
269 const bool checked = showGroup.readEntry(serviceName, true);
270 addRow(action.icon(), itemName, serviceName, checked);
271 }
272 }
273 }
274
275 // Load service plugins that implement the KFileItemActionPlugin interface
276 const KService::List pluginServices = KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
277 for (const KService::Ptr &service : pluginServices) {
278 const QString desktopEntryName = service->desktopEntryName();
279 if (!isInServicesList(desktopEntryName)) {
280 const bool checked = showGroup.readEntry(desktopEntryName, true);
281 addRow(service->icon(), service->name(), desktopEntryName, checked);
282 }
283 }
284
285 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
286 const auto jsonPlugins = KPluginLoader::findPlugins(QStringLiteral("kf5/kfileitemaction"), [](const KPluginMetaData& metaData) {
287 return metaData.serviceTypes().contains(QLatin1String("KFileItemAction/Plugin"));
288 });
289
290 for (const auto &jsonMetadata : jsonPlugins) {
291 const QString desktopEntryName = jsonMetadata.pluginId();
292 if (!isInServicesList(desktopEntryName)) {
293 const bool checked = showGroup.readEntry(desktopEntryName, true);
294 addRow(jsonMetadata.iconName(), jsonMetadata.name(), desktopEntryName, checked);
295 }
296 }
297
298 m_sortModel->sort(Qt::DisplayRole);
299 m_searchLineEdit->setFocus(Qt::OtherFocusReason);
300 }
301
302 void ContextMenuSettingsPage::loadVersionControlSystems()
303 {
304 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
305
306 // Create a checkbox for each available version control plugin
307 const KService::List pluginServices = KServiceTypeTrader::self()->query(QStringLiteral("FileViewVersionControlPlugin"));
308 for (const auto &plugin : pluginServices) {
309 const QString pluginName = plugin->name();
310 addRow(QStringLiteral("code-class"),
311 pluginName,
312 VersionControlServicePrefix + pluginName,
313 enabledPlugins.contains(pluginName));
314 }
315
316 m_sortModel->sort(Qt::DisplayRole);
317 }
318
319 bool ContextMenuSettingsPage::isInServicesList(const QString &service) const
320 {
321 for (int i = 0; i < m_serviceModel->rowCount(); ++i) {
322 const QModelIndex index = m_serviceModel->index(i, 0);
323 if (m_serviceModel->data(index, ServiceModel::DesktopEntryNameRole).toString() == service) {
324 return true;
325 }
326 }
327 return false;
328 }
329
330 void ContextMenuSettingsPage::addRow(const QString &icon,
331 const QString &text,
332 const QString &value,
333 bool checked)
334 {
335 m_serviceModel->insertRow(0);
336
337 const QModelIndex index = m_serviceModel->index(0, 0);
338 m_serviceModel->setData(index, icon, Qt::DecorationRole);
339 m_serviceModel->setData(index, text, Qt::DisplayRole);
340 m_serviceModel->setData(index, value, ServiceModel::DesktopEntryNameRole);
341 m_serviceModel->setData(index, checked, Qt::CheckStateRole);
342 }