]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/servicessettingspage.cpp
Invert default behavior regarding loading of version control plugins: Only load plugi...
[dolphin.git] / src / settings / servicessettingspage.cpp
1 /***************************************************************************
2 * Copyright (C) 2009-2010 by Peter Penz <peter.penz@gmx.at> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "servicessettingspage.h"
21
22 #include "dolphin_versioncontrolsettings.h"
23
24 #include <kconfig.h>
25 #include <kconfiggroup.h>
26 #include <kdesktopfileactions.h>
27 #include <kicon.h>
28 #include <klocale.h>
29 #include <kmessagebox.h>
30 #include <knewstuff3/knewstuffbutton.h>
31 #include <kservice.h>
32 #include <kservicetypetrader.h>
33 #include <kstandarddirs.h>
34
35 #include <QCheckBox>
36 #include <QEvent>
37 #include <QGridLayout>
38 #include <QGroupBox>
39 #include <QLabel>
40 #include <QListWidget>
41 #include <QPushButton>
42
43 ServicesSettingsPage::ServicesSettingsPage(QWidget* parent) :
44 SettingsPageBase(parent),
45 m_initialized(false),
46 m_servicesList(0),
47 m_vcsGroupBox(0),
48 m_vcsPluginsMap(),
49 m_enabledVcsPlugins()
50 {
51 QVBoxLayout* topLayout = new QVBoxLayout(this);
52
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);
57
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()));
63
64 KNS3::Button* downloadButton = new KNS3::Button(i18nc("@action:button", "Download New Services..."),
65 "servicemenu.knsrc",
66 this);
67 connect(downloadButton, SIGNAL(dialogFinished(KNS3::Entry::List)), this, SLOT(loadServices()));
68
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();
73
74 topLayout->addWidget(label);
75 topLayout->addWidget(m_servicesList);
76 topLayout->addWidget(downloadButton);
77 topLayout->addWidget(m_vcsGroupBox);
78
79 m_enabledVcsPlugins = VersionControlSettings::enabledPlugins();
80 }
81
82 ServicesSettingsPage::~ServicesSettingsPage()
83 {
84 }
85
86 void ServicesSettingsPage::applySettings()
87 {
88 // Apply service menu settings
89 KConfig config("kservicemenurc", KConfig::NoGlobals);
90 KConfigGroup showGroup = config.group("Show");
91
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);
98 }
99
100 showGroup.sync();
101
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());
108 }
109 ++it;
110 }
111
112 if (m_enabledVcsPlugins != enabledPlugins) {
113 VersionControlSettings::setEnabledPlugins(enabledPlugins);
114 VersionControlSettings::self()->writeConfig();
115
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"));
121 }
122 }
123
124 void ServicesSettingsPage::restoreDefaults()
125 {
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);
130 }
131 }
132
133 bool ServicesSettingsPage::event(QEvent* event)
134 {
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;
139 }
140 return SettingsPageBase::event(event);
141 }
142
143 void ServicesSettingsPage::loadServices()
144 {
145 const KConfig config("kservicemenurc", KConfig::NoGlobals);
146 const KConfigGroup showGroup = config.group("Show");
147
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);
153
154 foreach (const KServiceAction& action, serviceActions) {
155 const QString service = action.name();
156 const bool addService = !action.noDisplay()
157 && !action.isSeparator()
158 && !isInServicesList(service);
159
160 if (addService) {
161 QListWidgetItem* item = new QListWidgetItem(KIcon(action.icon()),
162 action.text(),
163 m_servicesList);
164 item->setData(Qt::UserRole, service);
165 const bool show = showGroup.readEntry(service, true);
166 item->setCheckState(show ? Qt::Checked : Qt::Unchecked);
167 }
168 }
169 }
170 }
171
172 void ServicesSettingsPage::loadVersionControlSystems()
173 {
174 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
175
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);
185 }
186
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;
190
191 int index = 0;
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);
197 ++it;
198 ++index;
199 }
200
201 m_vcsGroupBox->setVisible(!m_vcsPluginsMap.isEmpty());
202 }
203
204 bool ServicesSettingsPage::isInServicesList(const QString& service) const
205 {
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) {
210 return true;
211 }
212 }
213 return false;
214 }
215
216 #include "servicessettingspage.moc"