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_versioncontrolsettings.h"
25 #include <KConfigGroup>
26 #include <KDesktopFile>
27 #include <kdesktopfileactions.h>
30 #include <KMessageBox>
31 #include <knewstuff3/knewstuffbutton.h>
33 #include <KServiceTypeTrader>
34 #include <KStandardDirs>
36 #include <settings/serviceitemdelegate.h>
37 #include <settings/servicemodel.h>
40 #include <QGridLayout>
43 #include <QListWidget>
44 #include <QPushButton>
45 #include <QSortFilterProxyModel>
48 ServicesSettingsPage::ServicesSettingsPage(QWidget
* parent
) :
49 SettingsPageBase(parent
),
56 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
58 QLabel
* label
= new QLabel(i18nc("@label:textbox",
59 "Select which services should "
60 "be shown in the context menu:"), this);
61 label
->setWordWrap(true);
63 m_listView
= new QListView(this);
64 ServiceItemDelegate
* delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
65 ServiceModel
* serviceModel
= new ServiceModel(this);
66 QSortFilterProxyModel
* proxyModel
= new QSortFilterProxyModel(this);
67 proxyModel
->setSourceModel(serviceModel
);
68 proxyModel
->setSortRole(Qt::DisplayRole
);
69 m_listView
->setModel(proxyModel
);
70 m_listView
->setItemDelegate(delegate
);
71 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
72 connect(m_listView
, SIGNAL(clicked(QModelIndex
)), this, SIGNAL(changed()));
74 KNS3::Button
* downloadButton
= new KNS3::Button(i18nc("@action:button", "Download New Services..."),
77 connect(downloadButton
, SIGNAL(dialogFinished(KNS3::Entry::List
)), this, SLOT(loadServices()));
79 m_vcsGroupBox
= new QGroupBox(i18nc("@title:group", "Version Control Systems"), this);
80 // Only show the version control group box, if a version
81 // control system could be found (see loadVersionControlSystems())
82 m_vcsGroupBox
->hide();
84 topLayout
->addWidget(label
);
85 topLayout
->addWidget(m_listView
);
86 topLayout
->addWidget(downloadButton
);
87 topLayout
->addWidget(m_vcsGroupBox
);
89 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
92 ServicesSettingsPage::~ServicesSettingsPage()
96 void ServicesSettingsPage::applySettings()
102 // Apply service menu settingsentries
103 KConfig
config("kservicemenurc", KConfig::NoGlobals
);
104 KConfigGroup showGroup
= config
.group("Show");
106 const QAbstractItemModel
* model
= m_listView
->model();
107 for (int i
= 0; i
< model
->rowCount(); ++i
) {
108 const QModelIndex index
= model
->index(i
, 0);
109 const bool show
= model
->data(index
, Qt::CheckStateRole
).toBool();
110 const QString service
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
111 showGroup
.writeEntry(service
, show
);
116 // Apply version control settings
117 QStringList enabledPlugins
;
118 QMap
<QString
, QCheckBox
*>::const_iterator it
= m_vcsPluginsMap
.constBegin();
119 while (it
!= m_vcsPluginsMap
.constEnd()) {
120 if (it
.value()->isChecked()) {
121 enabledPlugins
.append(it
.key());
126 if (m_enabledVcsPlugins
!= enabledPlugins
) {
127 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
128 VersionControlSettings::self()->writeConfig();
130 KMessageBox::information(window(),
131 i18nc("@info", "Dolphin must be restarted to apply the "
132 "updated version control systems settings."),
133 QString(), // default title
134 QLatin1String("ShowVcsRestartInformation"));
138 void ServicesSettingsPage::restoreDefaults()
140 QAbstractItemModel
* model
= m_listView
->model();
141 for (int i
= 0; i
< model
->rowCount(); ++i
) {
142 const QModelIndex index
= model
->index(i
, 0);
143 model
->setData(index
, true, Qt::CheckStateRole
);
147 void ServicesSettingsPage::showEvent(QShowEvent
* event
)
149 if (!event
->spontaneous() && !m_initialized
) {
151 loadVersionControlSystems();
152 m_initialized
= true;
154 SettingsPageBase::showEvent(event
);
157 void ServicesSettingsPage::loadServices()
159 QAbstractItemModel
* model
= m_listView
->model();
161 const KConfig
config("kservicemenurc", KConfig::NoGlobals
);
162 const KConfigGroup showGroup
= config
.group("Show");
164 // Load generic services
165 const KService::List entries
= KServiceTypeTrader::self()->query("KonqPopupMenu/Plugin");
166 foreach (const KSharedPtr
<KService
>& service
, entries
) {
167 const QString file
= KStandardDirs::locate("services", service
->entryPath());
168 const QList
<KServiceAction
> serviceActions
=
169 KDesktopFileActions::userDefinedServices(file
, true);
171 KDesktopFile
desktopFile(file
);
172 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
174 foreach (const KServiceAction
& action
, serviceActions
) {
175 const QString serviceName
= action
.name();
176 const bool addService
= !action
.noDisplay()
177 && !action
.isSeparator()
178 && !isInServicesList(serviceName
);
181 const QString itemName
= subMenuName
.isEmpty()
183 : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
184 const bool show
= showGroup
.readEntry(serviceName
, true);
187 const QModelIndex index
= model
->index(0, 0);
188 model
->setData(index
, action
.icon(), Qt::DecorationRole
);
189 model
->setData(index
, show
, Qt::CheckStateRole
);
190 model
->setData(index
, itemName
, Qt::DisplayRole
);
191 model
->setData(index
, serviceName
, ServiceModel::DesktopEntryNameRole
);
196 // Load service plugins that implement the KFileItemActionPlugin interface
197 const KService::List pluginServices
= KServiceTypeTrader::self()->query("KFileItemAction/Plugin");
198 foreach (const KSharedPtr
<KService
>& service
, pluginServices
) {
199 const QString desktopEntryName
= service
->desktopEntryName();
200 if (!isInServicesList(desktopEntryName
)) {
201 const bool show
= showGroup
.readEntry(desktopEntryName
, true);
204 const QModelIndex index
= model
->index(0, 0);
205 model
->setData(index
, service
->icon(), Qt::DecorationRole
);
206 model
->setData(index
, show
, Qt::CheckStateRole
);
207 model
->setData(index
, service
->name(), Qt::DisplayRole
);
208 model
->setData(index
, desktopEntryName
, ServiceModel::DesktopEntryNameRole
);
212 model
->sort(Qt::DisplayRole
);
215 void ServicesSettingsPage::loadVersionControlSystems()
217 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
219 // Create a checkbox for each available version control plugin
220 const KService::List pluginServices
= KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
221 for (KService::List::ConstIterator it
= pluginServices
.constBegin(); it
!= pluginServices
.constEnd(); ++it
) {
222 const QString pluginName
= (*it
)->name();
223 QCheckBox
* checkBox
= new QCheckBox(pluginName
, m_vcsGroupBox
);
224 checkBox
->setChecked(enabledPlugins
.contains(pluginName
));
225 connect(checkBox
, SIGNAL(clicked()), this, SIGNAL(changed()));
226 m_vcsPluginsMap
.insert(pluginName
, checkBox
);
229 // Add the checkboxes into a grid layout of 2 columns
230 QGridLayout
* layout
= new QGridLayout(m_vcsGroupBox
);
231 const int maxRows
= (m_vcsPluginsMap
.count() + 1) / 2;
234 QMap
<QString
, QCheckBox
*>::const_iterator it
= m_vcsPluginsMap
.constBegin();
235 while (it
!= m_vcsPluginsMap
.constEnd()) {
236 const int column
= index
/ maxRows
;
237 const int row
= index
% maxRows
;
238 layout
->addWidget(it
.value(), row
, column
);
243 m_vcsGroupBox
->setVisible(!m_vcsPluginsMap
.isEmpty());
246 bool ServicesSettingsPage::isInServicesList(const QString
& service
) const
248 QAbstractItemModel
* model
= m_listView
->model();
249 for (int i
= 0; i
< model
->rowCount(); ++i
) {
250 const QModelIndex index
= model
->index(i
, 0);
251 if (model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString() == service
) {
258 #include "servicessettingspage.moc"