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 <kdesktopfile.h>
27 #include <kdesktopfileactions.h>
30 #include <kmessagebox.h>
31 #include <knewstuff3/knewstuffbutton.h>
33 #include <kservicetypetrader.h>
34 #include <kstandarddirs.h>
37 #include <QGridLayout>
40 #include <QListWidget>
41 #include <QPushButton>
44 ServicesSettingsPage::ServicesSettingsPage(QWidget
* parent
) :
45 SettingsPageBase(parent
),
52 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
54 QLabel
* label
= new QLabel(i18nc("@label:textbox",
55 "Select which services should "
56 "be shown in the context menu."), this);
57 label
->setWordWrap(true);
59 m_servicesList
= new QListWidget(this);
60 m_servicesList
->setSortingEnabled(true);
61 m_servicesList
->setSelectionMode(QAbstractItemView::NoSelection
);
62 connect(m_servicesList
, SIGNAL(itemClicked(QListWidgetItem
*)),
63 this, SIGNAL(changed()));
65 KNS3::Button
* downloadButton
= new KNS3::Button(i18nc("@action:button", "Download New Services..."),
68 connect(downloadButton
, SIGNAL(dialogFinished(KNS3::Entry::List
)), this, SLOT(loadServices()));
70 m_vcsGroupBox
= new QGroupBox(i18nc("@title:group", "Version Control Systems"), this);
71 // Only show the version control group box, if a version
72 // control system could be found (see loadVersionControlSystems())
73 m_vcsGroupBox
->hide();
75 topLayout
->addWidget(label
);
76 topLayout
->addWidget(m_servicesList
);
77 topLayout
->addWidget(downloadButton
);
78 topLayout
->addWidget(m_vcsGroupBox
);
80 m_enabledVcsPlugins
= VersionControlSettings::enabledPlugins();
83 ServicesSettingsPage::~ServicesSettingsPage()
87 void ServicesSettingsPage::applySettings()
89 // Apply service menu settingsentries
90 KConfig
config("kservicemenurc", KConfig::NoGlobals
);
91 KConfigGroup showGroup
= config
.group("Show");
93 const int count
= m_servicesList
->count();
94 for (int i
= 0; i
< count
; ++i
) {
95 QListWidgetItem
* item
= m_servicesList
->item(i
);
96 const bool show
= (item
->checkState() == Qt::Checked
);
97 const QString service
= item
->data(Qt::UserRole
).toString();
98 showGroup
.writeEntry(service
, show
);
103 // Apply version control settings
104 QStringList enabledPlugins
;
105 QMap
<QString
, QCheckBox
*>::const_iterator it
= m_vcsPluginsMap
.constBegin();
106 while (it
!= m_vcsPluginsMap
.constEnd()) {
107 if (it
.value()->isChecked()) {
108 enabledPlugins
.append(it
.key());
113 if (m_enabledVcsPlugins
!= enabledPlugins
) {
114 VersionControlSettings::setEnabledPlugins(enabledPlugins
);
115 VersionControlSettings::self()->writeConfig();
117 KMessageBox::information(window(),
118 i18nc("@info", "Dolphin must be restarted to apply the "
119 "updated version control systems settings."),
120 QString(), // default title
121 QLatin1String("ShowVcsRestartInformation"));
125 void ServicesSettingsPage::restoreDefaults()
127 const int count
= m_servicesList
->count();
128 for (int i
= 0; i
< count
; ++i
) {
129 QListWidgetItem
* item
= m_servicesList
->item(i
);
130 item
->setCheckState(Qt::Checked
);
134 void ServicesSettingsPage::showEvent(QShowEvent
* event
)
136 if (!event
->spontaneous() && !m_initialized
) {
137 QMetaObject::invokeMethod(this, "loadServices", Qt::QueuedConnection
);
138 QMetaObject::invokeMethod(this, "loadVersionControlSystems", Qt::QueuedConnection
);
139 m_initialized
= true;
141 SettingsPageBase::showEvent(event
);
144 void ServicesSettingsPage::loadServices()
146 const KConfig
config("kservicemenurc", KConfig::NoGlobals
);
147 const KConfigGroup showGroup
= config
.group("Show");
149 // Load generic services
150 const KService::List entries
= KServiceTypeTrader::self()->query("KonqPopupMenu/Plugin");
151 foreach (const KSharedPtr
<KService
>& service
, entries
) {
152 const QString file
= KStandardDirs::locate("services", service
->entryPath());
153 const QList
<KServiceAction
> serviceActions
=
154 KDesktopFileActions::userDefinedServices(file
, true);
156 KDesktopFile
desktopFile(file
);
157 const QString subMenuName
= desktopFile
.desktopGroup().readEntry("X-KDE-Submenu");
159 foreach (const KServiceAction
& action
, serviceActions
) {
160 const QString serviceName
= action
.name();
161 const bool addService
= !action
.noDisplay()
162 && !action
.isSeparator()
163 && !isInServicesList(serviceName
);
166 const QString itemName
= subMenuName
.isEmpty()
168 : i18nc("@item:inmenu", "%1: %2", subMenuName
, action
.text());
169 QListWidgetItem
* item
= new QListWidgetItem(KIcon(action
.icon()),
172 item
->setData(Qt::UserRole
, serviceName
);
173 const bool show
= showGroup
.readEntry(serviceName
, true);
174 item
->setCheckState(show
? Qt::Checked
: Qt::Unchecked
);
179 // Load service plugins that implement the KFileItemActionPlugin interface
180 const KService::List pluginServices
= KServiceTypeTrader::self()->query("KFileItemAction/Plugin");
181 foreach (const KSharedPtr
<KService
>& service
, pluginServices
) {
182 const QString serviceName
= service
->desktopEntryName();
183 if (!isInServicesList(serviceName
)) {
184 QListWidgetItem
* item
= new QListWidgetItem(KIcon(service
->icon()),
187 item
->setData(Qt::UserRole
, serviceName
);
188 const bool show
= showGroup
.readEntry(serviceName
, true);
189 item
->setCheckState(show
? Qt::Checked
: Qt::Unchecked
);
194 void ServicesSettingsPage::loadVersionControlSystems()
196 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
198 // Create a checkbox for each available version control plugin
199 const KService::List pluginServices
= KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
200 for (KService::List::ConstIterator it
= pluginServices
.constBegin(); it
!= pluginServices
.constEnd(); ++it
) {
201 const QString pluginName
= (*it
)->name();
202 QCheckBox
* checkBox
= new QCheckBox(pluginName
, m_vcsGroupBox
);
203 checkBox
->setChecked(enabledPlugins
.contains(pluginName
));
204 connect(checkBox
, SIGNAL(clicked()), this, SIGNAL(changed()));
205 m_vcsPluginsMap
.insert(pluginName
, checkBox
);
208 // Add the checkboxes into a grid layout of 2 columns
209 QGridLayout
* layout
= new QGridLayout(m_vcsGroupBox
);
210 const int maxRows
= (m_vcsPluginsMap
.count() + 1) / 2;
213 QMap
<QString
, QCheckBox
*>::const_iterator it
= m_vcsPluginsMap
.constBegin();
214 while (it
!= m_vcsPluginsMap
.constEnd()) {
215 const int column
= index
/ maxRows
;
216 const int row
= index
% maxRows
;
217 layout
->addWidget(it
.value(), row
, column
);
222 m_vcsGroupBox
->setVisible(!m_vcsPluginsMap
.isEmpty());
225 bool ServicesSettingsPage::isInServicesList(const QString
& service
) const
227 const int count
= m_servicesList
->count();
228 for (int i
= 0; i
< count
; ++i
) {
229 QListWidgetItem
* item
= m_servicesList
->item(i
);
230 if (item
->data(Qt::UserRole
).toString() == service
) {
237 #include "servicessettingspage.moc"