1 /***************************************************************************
2 * Copyright (C) 2009-2010 by Peter Penz <peter.penz19@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "servicessettingspage.h"
22 #include "dolphin_generalsettings.h"
23 #include "dolphin_versioncontrolsettings.h"
24 #include "settings/serviceitemdelegate.h"
25 #include "settings/servicemodel.h"
27 #include <KDesktopFile>
28 #include <KLocalizedString>
29 #include <KMessageBox>
30 #include <KNS3/Button>
31 #include <KPluginMetaData>
33 #include <KServiceTypeTrader>
34 #include <kdesktopfileactions.h>
36 #include <QGridLayout>
38 #include <QListWidget>
39 #include <QSortFilterProxyModel>
43 const bool ShowDeleteDefault
= false;
44 const char VersionControlServicePrefix
[] = "_version_control_";
45 const char DeleteService
[] = "_delete";
46 const char CopyToMoveToService
[] ="_copy_to_move_to";
49 ServicesSettingsPage::ServicesSettingsPage(QWidget
* parent
) :
50 SettingsPageBase(parent
),
52 m_serviceModel(nullptr),
57 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
59 QLabel
* label
= new QLabel(i18nc("@label:textbox",
60 "Select which services should "
61 "be shown in the context menu:"), this);
62 label
->setWordWrap(true);
64 m_listView
= new QListView(this);
65 ServiceItemDelegate
* delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
66 m_serviceModel
= new ServiceModel(this);
67 m_sortModel
= new QSortFilterProxyModel(this);
68 m_sortModel
->setSourceModel(m_serviceModel
);
69 m_sortModel
->setSortRole(Qt::DisplayRole
);
70 m_listView
->setModel(m_sortModel
);
71 m_listView
->setItemDelegate(delegate
);
72 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
73 connect(m_listView
, &QListView::clicked
, this, &ServicesSettingsPage::changed
);
75 KNS3::Button
* downloadButton
= new KNS3::Button(i18nc("@action:button", "Download New Services..."),
76 QStringLiteral("servicemenu.knsrc"),
78 connect(downloadButton
, &KNS3::Button::dialogFinished
, this, &ServicesSettingsPage::loadServices
);
80 topLayout
->addWidget(label
);
81 topLayout
->addWidget(m_listView
);
82 topLayout
->addWidget(downloadButton
);
84 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
85 qSort(m_enabledVcsPlugins
);
88 ServicesSettingsPage::~ServicesSettingsPage()
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 foreach (const KService::Ptr
& service
, entries
) {
190 const QString file
= QStandardPaths::locate(QStandardPaths::GenericDataLocation
, "kservices5/" % service
->entryPath());
191 const QList
<KServiceAction
> serviceActions
=
192 KDesktopFileActions::userDefinedServices(file
, true);
194 KDesktopFile
desktopFile(file
);
195 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
197 foreach (const KServiceAction
& action
, serviceActions
) {
198 const QString serviceName
= action
.name();
199 const bool addService
= !action
.noDisplay()
200 && !action
.isSeparator()
201 && !isInServicesList(serviceName
);
204 const QString itemName
= subMenuName
.isEmpty()
206 : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
207 const bool checked
= showGroup
.readEntry(serviceName
, true);
208 addRow(action
.icon(), itemName
, serviceName
, checked
);
213 // Load service plugins that implement the KFileItemActionPlugin interface
214 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
215 foreach (const KService::Ptr
& service
, pluginServices
) {
216 const QString desktopEntryName
= service
->desktopEntryName();
217 if (!isInServicesList(desktopEntryName
)) {
218 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
219 addRow(service
->icon(), service
->name(), desktopEntryName
, checked
);
223 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
224 const auto jsonPlugins
= KPluginLoader::findPlugins(QStringLiteral("kf5/kfileitemaction"), [](const KPluginMetaData
& metaData
) {
225 return metaData
.serviceTypes().contains(QStringLiteral("KFileItemAction/Plugin"));
228 foreach (const auto& jsonMetadata
, jsonPlugins
) {
229 const QString desktopEntryName
= jsonMetadata
.pluginId();
230 if (!isInServicesList(desktopEntryName
)) {
231 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
232 addRow(jsonMetadata
.iconName(), jsonMetadata
.name(), desktopEntryName
, checked
);
236 m_sortModel
->sort(Qt::DisplayRole
);
239 void ServicesSettingsPage::loadVersionControlSystems()
241 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
243 // Create a checkbox for each available version control plugin
244 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("FileViewVersionControlPlugin"));
245 for (KService::List::ConstIterator it
= pluginServices
.constBegin(); it
!= pluginServices
.constEnd(); ++it
) {
246 const QString pluginName
= (*it
)->name();
247 addRow(QStringLiteral("code-class"),
249 VersionControlServicePrefix
+ pluginName
,
250 enabledPlugins
.contains(pluginName
));
253 m_sortModel
->sort(Qt::DisplayRole
);
256 bool ServicesSettingsPage::isInServicesList(const QString
& service
) const
258 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
259 const QModelIndex index
= m_serviceModel
->index(i
, 0);
260 if (m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
267 void ServicesSettingsPage::addRow(const QString
& icon
,
269 const QString
& value
,
272 m_serviceModel
->insertRow(0);
274 const QModelIndex index
= m_serviceModel
->index(0, 0);
275 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
276 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
277 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
278 m_serviceModel
->setData(index
, checked
, Qt::CheckStateRole
);