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"
26 #include <KConfigGroup>
27 #include <KDesktopFile>
28 #include <kdesktopfileactions.h>
30 #include <KLocalizedString>
31 #include <KMessageBox>
32 #include <KNS3/Button>
34 #include <KServiceTypeTrader>
35 #include <QStandardPaths>
37 #include <settings/serviceitemdelegate.h>
38 #include <settings/servicemodel.h>
41 #include <QGridLayout>
44 #include <QListWidget>
45 #include <QPushButton>
46 #include <QSortFilterProxyModel>
51 const bool ShowDeleteDefault
= false;
52 const char VersionControlServicePrefix
[] = "_version_control_";
53 const char DeleteService
[] = "_delete";
54 const char CopyToMoveToService
[] ="_copy_to_move_to";
57 ServicesSettingsPage::ServicesSettingsPage(QWidget
* parent
) :
58 SettingsPageBase(parent
),
65 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
67 QLabel
* label
= new QLabel(i18nc("@label:textbox",
68 "Select which services should "
69 "be shown in the context menu:"), this);
70 label
->setWordWrap(true);
72 m_listView
= new QListView(this);
73 ServiceItemDelegate
* delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
74 m_serviceModel
= new ServiceModel(this);
75 m_sortModel
= new QSortFilterProxyModel(this);
76 m_sortModel
->setSourceModel(m_serviceModel
);
77 m_sortModel
->setSortRole(Qt::DisplayRole
);
78 m_listView
->setModel(m_sortModel
);
79 m_listView
->setItemDelegate(delegate
);
80 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
81 connect(m_listView
, &QListView::clicked
, this, &ServicesSettingsPage::changed
);
83 KNS3::Button
* downloadButton
= new KNS3::Button(i18nc("@action:button", "Download New Services..."),
86 connect(downloadButton
, &KNS3::Button::dialogFinished
, this, &ServicesSettingsPage::loadServices
);
88 topLayout
->addWidget(label
);
89 topLayout
->addWidget(m_listView
);
90 topLayout
->addWidget(downloadButton
);
92 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
93 qSort(m_enabledVcsPlugins
);
96 ServicesSettingsPage::~ServicesSettingsPage()
100 void ServicesSettingsPage::applySettings()
102 if (!m_initialized
) {
106 KConfig
config("kservicemenurc", KConfig::NoGlobals
);
107 KConfigGroup showGroup
= config
.group("Show");
109 QStringList enabledPlugins
;
111 const QAbstractItemModel
* model
= m_listView
->model();
112 for (int i
= 0; i
< model
->rowCount(); ++i
) {
113 const QModelIndex index
= model
->index(i
, 0);
114 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
115 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
117 if (service
.startsWith(VersionControlServicePrefix
)) {
119 enabledPlugins
.append(model
->data(index
, Qt::DisplayRole
).toString());
121 } else if (service
== QLatin1String(DeleteService
)) {
122 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::NoGlobals
);
123 KConfigGroup
configGroup(globalConfig
, "KDE");
124 configGroup
.writeEntry("ShowDeleteCommand", checked
);
126 } else if (service
== QLatin1String(CopyToMoveToService
)) {
127 GeneralSettings::setShowCopyMoveMenu(checked
);
128 GeneralSettings::self()->save();
130 showGroup
.writeEntry(service
, checked
);
136 if (m_enabledVcsPlugins
!= enabledPlugins
) {
137 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
138 VersionControlSettings::self()->save();
140 KMessageBox::information(window(),
141 i18nc("@info", "Dolphin must be restarted to apply the "
142 "updated version control systems settings."),
143 QString(), // default title
144 QLatin1String("ShowVcsRestartInformation"));
148 void ServicesSettingsPage::restoreDefaults()
150 QAbstractItemModel
* model
= m_listView
->model();
151 for (int i
= 0; i
< model
->rowCount(); ++i
) {
152 const QModelIndex index
= model
->index(i
, 0);
153 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
155 const bool checked
= !service
.startsWith(VersionControlServicePrefix
)
156 && service
!= QLatin1String(DeleteService
)
157 && service
!= QLatin1String(CopyToMoveToService
);
158 model
->setData(index
, checked
, Qt::CheckStateRole
);
162 void ServicesSettingsPage::showEvent(QShowEvent
* event
)
164 if (!event
->spontaneous() && !m_initialized
) {
167 loadVersionControlSystems();
169 // Add "Show 'Delete' command" as service
170 KSharedConfig::Ptr globalConfig
= KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals
);
171 KConfigGroup
configGroup(globalConfig
, "KDE");
172 addRow("edit-delete",
173 i18nc("@option:check", "Delete"),
175 configGroup
.readEntry("ShowDeleteCommand", ShowDeleteDefault
));
177 // Add "Show 'Copy To' and 'Move To' commands" as service
179 i18nc("@option:check", "'Copy To' and 'Move To' commands"),
181 GeneralSettings::showCopyMoveMenu());
183 m_sortModel
->sort(Qt::DisplayRole
);
185 m_initialized
= true;
187 SettingsPageBase::showEvent(event
);
190 void ServicesSettingsPage::loadServices()
192 const KConfig
config("kservicemenurc", KConfig::NoGlobals
);
193 const KConfigGroup showGroup
= config
.group("Show");
195 // Load generic services
196 const KService::List entries
= KServiceTypeTrader::self()->query("KonqPopupMenu/Plugin");
197 foreach (const KService::Ptr
& service
, entries
) {
198 const QString file
= QStandardPaths::locate(QStandardPaths::GenericDataLocation
, "kservices5/" % service
->entryPath());
199 const QList
<KServiceAction
> serviceActions
=
200 KDesktopFileActions::userDefinedServices(file
, true);
202 KDesktopFile
desktopFile(file
);
203 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
205 foreach (const KServiceAction
& action
, serviceActions
) {
206 const QString serviceName
= action
.name();
207 const bool addService
= !action
.noDisplay()
208 && !action
.isSeparator()
209 && !isInServicesList(serviceName
);
212 const QString itemName
= subMenuName
.isEmpty()
214 : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
215 const bool checked
= showGroup
.readEntry(serviceName
, true);
216 addRow(action
.icon(), itemName
, serviceName
, checked
);
221 // Load service plugins that implement the KFileItemActionPlugin interface
222 const KService::List pluginServices
= KServiceTypeTrader::self()->query("KFileItemAction/Plugin");
223 foreach (const KService::Ptr
& service
, pluginServices
) {
224 const QString desktopEntryName
= service
->desktopEntryName();
225 if (!isInServicesList(desktopEntryName
)) {
226 const bool checked
= showGroup
.readEntry(desktopEntryName
, true);
227 addRow(service
->icon(), service
->name(), desktopEntryName
, checked
);
231 m_sortModel
->sort(Qt::DisplayRole
);
234 void ServicesSettingsPage::loadVersionControlSystems()
236 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
238 // Create a checkbox for each available version control plugin
239 const KService::List pluginServices
= KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
240 for (KService::List::ConstIterator it
= pluginServices
.constBegin(); it
!= pluginServices
.constEnd(); ++it
) {
241 const QString pluginName
= (*it
)->name();
244 VersionControlServicePrefix
+ pluginName
,
245 enabledPlugins
.contains(pluginName
));
248 m_sortModel
->sort(Qt::DisplayRole
);
251 bool ServicesSettingsPage::isInServicesList(const QString
& service
) const
253 for (int i
= 0; i
< m_serviceModel
->rowCount(); ++i
) {
254 const QModelIndex index
= m_serviceModel
->index(i
, 0);
255 if (m_serviceModel
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
262 void ServicesSettingsPage::addRow(const QString
& icon
,
264 const QString
& value
,
267 m_serviceModel
->insertRow(0);
269 const QModelIndex index
= m_serviceModel
->index(0, 0);
270 m_serviceModel
->setData(index
, icon
, Qt::DecorationRole
);
271 m_serviceModel
->setData(index
, text
, Qt::DisplayRole
);
272 m_serviceModel
->setData(index
, value
, ServiceModel::DesktopEntryNameRole
);
273 m_serviceModel
->setData(index
, checked
, Qt::CheckStateRole
);