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>
40 #include <QSortFilterProxyModel>
44 const bool ShowDeleteDefault
= false;
45 const char VersionControlServicePrefix
[] = "_version_control_";
46 const char DeleteService
[] = "_delete";
47 const char CopyToMoveToService
[] ="_copy_to_move_to";
50 ServicesSettingsPage::ServicesSettingsPage(QWidget
* parent
) :
51 SettingsPageBase(parent
),
53 m_serviceModel(nullptr),
58 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
60 QLabel
* label
= new QLabel(i18nc("@label:textbox",
61 "Select which services should "
62 "be shown in the context menu:"), this);
63 label
->setWordWrap(true);
65 m_listView
= new QListView(this);
66 ServiceItemDelegate
* delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
67 m_serviceModel
= new ServiceModel(this);
68 m_sortModel
= new QSortFilterProxyModel(this);
69 m_sortModel
->setSourceModel(m_serviceModel
);
70 m_sortModel
->setSortRole(Qt::DisplayRole
);
71 m_sortModel
->setSortLocaleAware(true);
72 m_listView
->setModel(m_sortModel
);
73 m_listView
->setItemDelegate(delegate
);
74 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
75 connect(m_listView
, &QListView::clicked
, this, &ServicesSettingsPage::changed
);
77 KNS3::Button
* downloadButton
= new KNS3::Button(i18nc("@action:button", "Download New Services..."),
78 QStringLiteral("servicemenu.knsrc"),
80 connect(downloadButton
, &KNS3::Button::dialogFinished
, this, &ServicesSettingsPage::loadServices
);
82 topLayout
->addWidget(label
);
83 topLayout
->addWidget(m_listView
);
84 topLayout
->addWidget(downloadButton
);
86 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
87 std::sort(m_enabledVcsPlugins
.begin(), m_enabledVcsPlugins
.end());
90 ServicesSettingsPage::~ServicesSettingsPage()
94 void ServicesSettingsPage::applySettings()
100 KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
101 KConfigGroup showGroup
= config
.group("Show");
103 QStringList enabledPlugins
;
105 const QAbstractItemModel
* model
= m_listView
->model();
106 for (int i
= 0; i
< model
->rowCount(); ++i
) {
107 const QModelIndex index
= model
->index(i
, 0);
108 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
109 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
111 if (service
.startsWith(VersionControlServicePrefix
)) {
113 enabledPlugins
.append(model
->data(index
, Qt::DisplayRole
).toString());
115 } else if (service
== QLatin1String(DeleteService
)) {
116 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals
);
117 KConfigGroup
configGroup(globalConfig
, "KDE");
118 configGroup
.writeEntry("ShowDeleteCommand", checked
);
120 } else if (service
== QLatin1String(CopyToMoveToService
)) {
121 GeneralSettings::setShowCopyMoveMenu(checked
);
122 GeneralSettings::self()->save();
124 showGroup
.writeEntry(service
, checked
);
130 if (m_enabledVcsPlugins
!= enabledPlugins
) {
131 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
132 VersionControlSettings::self()->save();
134 KMessageBox::information(window(),
135 i18nc("@info", "Dolphin must be restarted to apply the "
136 "updated version control systems settings."),
137 QString(), // default title
138 QStringLiteral("ShowVcsRestartInformation"));
142 void ServicesSettingsPage::restoreDefaults()
144 QAbstractItemModel
* model
= m_listView
->model();
145 for (int i
= 0; i
< model
->rowCount(); ++i
) {
146 const QModelIndex index
= model
->index(i
, 0);
147 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
149 const bool checked
= !service
.startsWith(VersionControlServicePrefix
)
150 && service
!= QLatin1String(DeleteService
)
151 && service
!= QLatin1String(CopyToMoveToService
);
152 model
->setData(index
, checked
, Qt::CheckStateRole
);
156 void ServicesSettingsPage::showEvent(QShowEvent
* event
)
158 if (!event
->spontaneous() && !m_initialized
) {
161 loadVersionControlSystems();
163 // Add "Show 'Delete' command" as service
164 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::IncludeGlobals
);
165 KConfigGroup
configGroup(globalConfig
, "KDE");
166 addRow(QStringLiteral("edit-delete"),
167 i18nc("@option:check", "Delete"),
169 configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
171 // Add "Show 'Copy To' and 'Move To' commands" as service
172 addRow(QStringLiteral("edit-copy"),
173 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
175 GeneralSettings::showCopyMoveMenu());
177 m_sortModel
->sort(Qt::DisplayRole
);
179 m_initialized
= true;
181 SettingsPageBase::showEvent(event
);
184 void ServicesSettingsPage::loadServices()
186 const KConfig
config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals
);
187 const KConfigGroup showGroup
= config
.group("Show");
189 // Load generic services
190 const KService::List entries
= KServiceTypeTrader::self()->query(QStringLiteral("KonqPopupMenu/Plugin"));
191 foreach (const KService::Ptr
& service
, entries
) {
192 const QString file
= QStandardPaths::locate(QStandardPaths::GenericDataLocation
, "kservices5/" % service
->entryPath());
193 const QList
<KServiceAction
> serviceActions
=
194 KDesktopFileActions::userDefinedServices(file
, true);
196 KDesktopFile
desktopFile(file
);
197 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
199 foreach (const KServiceAction
& action
, serviceActions
) {
200 const QString serviceName
= action
.name();
201 const bool addService
= !action
.noDisplay()
202 && !action
.isSeparator()
203 && !isInServicesList(serviceName
);
206 const QString itemName
= subMenuName
.isEmpty()
208 : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
209 const bool checked
= showGroup
.readEntry(serviceName
, true);
210 addRow(action
.icon(), itemName
, serviceName
, checked
);
215 // Load service plugins that implement the KFileItemActionPlugin interface
216 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("KFileItemAction/Plugin"));
217 foreach (const KService::Ptr
& service
, pluginServices
) {
218 const QString desktopEntryName
= service
->desktopEntryName();
219 if (!isInServicesList(desktopEntryName
)) {
220 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
221 addRow(service
->icon(), service
->name(), desktopEntryName
, checked
);
225 // Load JSON-based plugins that implement the KFileItemActionPlugin interface
226 const auto jsonPlugins
= KPluginLoader::findPlugins(QStringLiteral("kf5/kfileitemaction"), [](const KPluginMetaData
& metaData
) {
227 return metaData
.serviceTypes().contains(QLatin1String("KFileItemAction/Plugin"));
230 foreach (const auto& jsonMetadata
, jsonPlugins
) {
231 const QString desktopEntryName
= jsonMetadata
.pluginId();
232 if (!isInServicesList(desktopEntryName
)) {
233 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
234 addRow(jsonMetadata
.iconName(), jsonMetadata
.name(), desktopEntryName
, checked
);
238 m_sortModel
->sort(Qt::DisplayRole
);
241 void ServicesSettingsPage::loadVersionControlSystems()
243 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
245 // Create a checkbox for each available version control plugin
246 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("FileViewVersionControlPlugin"));
247 for (KService::List::ConstIterator it
= pluginServices
.constBegin(); it
!= pluginServices
.constEnd(); ++it
) {
248 const QString pluginName
= (*it
)->name();
249 addRow(QStringLiteral("code-class"),
251 VersionControlServicePrefix
+ pluginName
,
252 enabledPlugins
.contains(pluginName
));
255 m_sortModel
->sort(Qt::DisplayRole
);
258 bool ServicesSettingsPage::isInServicesList(const QString
& service
) const
260 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
261 const QModelIndex index
= m_serviceModel
->index(i
, 0);
262 if (m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
269 void ServicesSettingsPage::addRow(const QString
& icon
,
271 const QString
& value
,
274 m_serviceModel
->insertRow(0);
276 const QModelIndex index
= m_serviceModel
->index(0, 0);
277 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
278 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
279 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
280 m_serviceModel
->setData(index
, checked
, Qt::CheckStateRole
);