1 /***************************************************************************
2 * Copyright (C) 2009-2010 by Peter Penz <peter.penz@gmx.at> *
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.h>
26 #include <kdesktopfileactions.h>
29 #include <kmessagebox.h>
30 #include <knewstuff3/knewstuffbutton.h>
32 #include <kservicetypetrader.h>
33 #include <kstandarddirs.h>
37 #include <QGridLayout>
40 #include <QListWidget>
41 #include <QPushButton>
43 ServicesSettingsPage::ServicesSettingsPage(QWidget
* parent
) :
44 SettingsPageBase(parent
),
51 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
53 QLabel
* label
= new QLabel(i18nc("@label:textbox",
54 "Configure which services should "
55 "be shown in the context menu."), this);
56 label
->setWordWrap(true);
58 m_servicesList
= new QListWidget(this);
59 m_servicesList
->setSortingEnabled(true);
60 m_servicesList
->setSelectionMode(QAbstractItemView::NoSelection
);
61 connect(m_servicesList
, SIGNAL(itemClicked(QListWidgetItem
*)),
62 this, SIGNAL(changed()));
64 KNS3::Button
* downloadButton
= new KNS3::Button(i18nc("@action:button", "Download New Services..."),
67 connect(downloadButton
, SIGNAL(dialogFinished(KNS3::Entry::List
)), this, SLOT(loadServices()));
69 m_vcsGroupBox
= new QGroupBox(i18nc("@title:group", "Version Control Systems"), this);
70 // Only show the version control group box, if a version
71 // control system could be found (see loadVersionControlSystems())
72 m_vcsGroupBox
->hide();
74 topLayout
->addWidget(label
);
75 topLayout
->addWidget(m_servicesList
);
76 topLayout
->addWidget(downloadButton
);
77 topLayout
->addWidget(m_vcsGroupBox
);
79 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
82 ServicesSettingsPage::~ServicesSettingsPage()
86 void ServicesSettingsPage::applySettings()
88 // Apply service menu settings
89 KConfig
config("kservicemenurc", KConfig::NoGlobals
);
90 KConfigGroup showGroup
= config
.group("Show");
92 const int count
= m_servicesList
->count();
93 for (int i
= 0; i
< count
; ++i
) {
94 QListWidgetItem
* item
= m_servicesList
->item(i
);
95 const bool show
= (item
->checkState() == Qt::Checked
);
96 const QString service
= item
->data(Qt::UserRole
).toString();
97 showGroup
.writeEntry(service
, show
);
102 // Apply version control settings
103 QStringList enabledPlugins
;
104 QMap
<QString
, QCheckBox
*>::const_iterator it
= m_vcsPluginsMap
.constBegin();
105 while (it
!= m_vcsPluginsMap
.constEnd()) {
106 if (it
.value()->isChecked()) {
107 enabledPlugins
.append(it
.key());
112 if (m_enabledVcsPlugins
!= enabledPlugins
) {
113 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
114 VersionControlSettings::self()->writeConfig();
116 KMessageBox::information(window(),
117 i18nc("@info", "Dolphin must be restarted to apply the "
118 "updated version control systems settings."),
119 QString(), // default title
120 QLatin1String("ShowVcsRestartInformation"));
124 void ServicesSettingsPage::restoreDefaults()
126 const int count
= m_servicesList
->count();
127 for (int i
= 0; i
< count
; ++i
) {
128 QListWidgetItem
* item
= m_servicesList
->item(i
);
129 item
->setCheckState(Qt::Checked
);
133 bool ServicesSettingsPage::event(QEvent
* event
)
135 if ((event
->type() == QEvent::Polish
) && !m_initialized
) {
136 QMetaObject::invokeMethod(this, "loadServices", Qt::QueuedConnection
);
137 QMetaObject::invokeMethod(this, "loadVersionControlSystems", Qt::QueuedConnection
);
138 m_initialized
= true;
140 return SettingsPageBase::event(event
);
143 void ServicesSettingsPage::loadServices()
145 const KConfig
config("kservicemenurc", KConfig::NoGlobals
);
146 const KConfigGroup showGroup
= config
.group("Show");
148 const KService::List entries
= KServiceTypeTrader::self()->query("KonqPopupMenu/Plugin");
149 foreach (const KSharedPtr
<KService
>& service
, entries
) {
150 const QString file
= KStandardDirs::locate("services", service
->entryPath());
151 const QList
<KServiceAction
> serviceActions
=
152 KDesktopFileActions::userDefinedServices(file
, true);
154 foreach (const KServiceAction
& action
, serviceActions
) {
155 const QString service
= action
.name();
156 const bool addService
= !action
.noDisplay()
157 && !action
.isSeparator()
158 && !isInServicesList(service
);
161 QListWidgetItem
* item
= new QListWidgetItem(KIcon(action
.icon()),
164 item
->setData(Qt::UserRole
, service
);
165 const bool show
= showGroup
.readEntry(service
, true);
166 item
->setCheckState(show
? Qt::Checked
: Qt::Unchecked
);
172 void ServicesSettingsPage::loadVersionControlSystems()
174 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
176 // Create a checkbox for each available version control plugin
177 const KService::List pluginServices
= KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
178 for (KService::List::ConstIterator it
= pluginServices
.constBegin(); it
!= pluginServices
.constEnd(); ++it
) {
179 const QString pluginName
= (*it
)->name();
180 QCheckBox
* checkBox
= new QCheckBox(pluginName
, m_vcsGroupBox
);
181 checkBox
->setChecked(enabledPlugins
.contains(pluginName
));
182 connect(checkBox
, SIGNAL(clicked()), this, SIGNAL(changed()));
183 connect(checkBox
, SIGNAL(clicked()), this, SLOT(feffi()));
184 m_vcsPluginsMap
.insert(pluginName
, checkBox
);
187 // Add the checkboxes into a grid layout of 2 columns
188 QGridLayout
* layout
= new QGridLayout(m_vcsGroupBox
);
189 const int maxRows
= (m_vcsPluginsMap
.count() + 1) / 2;
192 QMap
<QString
, QCheckBox
*>::const_iterator it
= m_vcsPluginsMap
.constBegin();
193 while (it
!= m_vcsPluginsMap
.constEnd()) {
194 const int column
= index
/ maxRows
;
195 const int row
= index
% maxRows
;
196 layout
->addWidget(it
.value(), row
, column
);
201 m_vcsGroupBox
->setVisible(!m_vcsPluginsMap
.isEmpty());
204 bool ServicesSettingsPage::isInServicesList(const QString
& service
) const
206 const int count
= m_servicesList
->count();
207 for (int i
= 0; i
< count
; ++i
) {
208 QListWidgetItem
* item
= m_servicesList
->item(i
);
209 if (item
->data(Qt::UserRole
).toString() == service
) {
216 #include "servicessettingspage.moc"