]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/services/servicessettingspage.cpp
--warning
[dolphin.git] / src / settings / services / servicessettingspage.cpp
1 /***************************************************************************
2 * Copyright (C) 2009-2010 by Peter Penz <peter.penz19@gmail.com> *
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>
25 #include <KConfigGroup>
26 #include <KDesktopFile>
27 #include <kdesktopfileactions.h>
28 #include <KIcon>
29 #include <KLocale>
30 #include <KMessageBox>
31 #include <knewstuff3/knewstuffbutton.h>
32 #include <KService>
33 #include <KServiceTypeTrader>
34 #include <KStandardDirs>
35
36 #include <settings/serviceitemdelegate.h>
37 #include <settings/servicemodel.h>
38
39 #include <QCheckBox>
40 #include <QGridLayout>
41 #include <QGroupBox>
42 #include <QLabel>
43 #include <QListWidget>
44 #include <QPushButton>
45 #include <QSortFilterProxyModel>
46 #include <QShowEvent>
47
48 ServicesSettingsPage::ServicesSettingsPage(QWidget* parent) :
49 SettingsPageBase(parent),
50 m_initialized(false),
51 m_listView(0),
52 m_vcsGroupBox(0),
53 m_vcsPluginsMap(),
54 m_enabledVcsPlugins()
55 {
56 QVBoxLayout* topLayout = new QVBoxLayout(this);
57
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);
62
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()));
73
74 KNS3::Button* downloadButton = new KNS3::Button(i18nc("@action:button", "Download New Services..."),
75 "servicemenu.knsrc",
76 this);
77 connect(downloadButton, SIGNAL(dialogFinished(KNS3::Entry::List)), this, SLOT(loadServices()));
78
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();
83
84 topLayout->addWidget(label);
85 topLayout->addWidget(m_listView);
86 topLayout->addWidget(downloadButton);
87 topLayout->addWidget(m_vcsGroupBox);
88
89 m_enabledVcsPlugins = VersionControlSettings::enabledPlugins();
90 }
91
92 ServicesSettingsPage::~ServicesSettingsPage()
93 {
94 }
95
96 void ServicesSettingsPage::applySettings()
97 {
98 if (!m_initialized) {
99 return;
100 }
101
102 // Apply service menu settingsentries
103 KConfig config("kservicemenurc", KConfig::NoGlobals);
104 KConfigGroup showGroup = config.group("Show");
105
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);
112 }
113
114 showGroup.sync();
115
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());
122 }
123 ++it;
124 }
125
126 if (m_enabledVcsPlugins != enabledPlugins) {
127 VersionControlSettings::setEnabledPlugins(enabledPlugins);
128 VersionControlSettings::self()->writeConfig();
129
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"));
135 }
136 }
137
138 void ServicesSettingsPage::restoreDefaults()
139 {
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);
144 }
145 }
146
147 void ServicesSettingsPage::showEvent(QShowEvent* event)
148 {
149 if (!event->spontaneous() && !m_initialized) {
150 loadServices();
151 loadVersionControlSystems();
152 m_initialized = true;
153 }
154 SettingsPageBase::showEvent(event);
155 }
156
157 void ServicesSettingsPage::loadServices()
158 {
159 QAbstractItemModel* model = m_listView->model();
160
161 const KConfig config("kservicemenurc", KConfig::NoGlobals);
162 const KConfigGroup showGroup = config.group("Show");
163
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);
170
171 KDesktopFile desktopFile(file);
172 const QString subMenuName = desktopFile.desktopGroup().readEntry("X-KDE-Submenu");
173
174 foreach (const KServiceAction& action, serviceActions) {
175 const QString serviceName = action.name();
176 const bool addService = !action.noDisplay()
177 && !action.isSeparator()
178 && !isInServicesList(serviceName);
179
180 if (addService) {
181 const QString itemName = subMenuName.isEmpty()
182 ? action.text()
183 : i18nc("@item:inmenu", "%1: %2", subMenuName, action.text());
184 const bool show = showGroup.readEntry(serviceName, true);
185
186 model->insertRow(0);
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);
192 }
193 }
194 }
195
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);
202
203 model->insertRow(0);
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);
209 }
210 }
211
212 model->sort(Qt::DisplayRole);
213 }
214
215 void ServicesSettingsPage::loadVersionControlSystems()
216 {
217 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
218
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);
227 }
228
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;
232
233 int index = 0;
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);
239 ++it;
240 ++index;
241 }
242
243 m_vcsGroupBox->setVisible(!m_vcsPluginsMap.isEmpty());
244 }
245
246 bool ServicesSettingsPage::isInServicesList(const QString& service) const
247 {
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) {
252 return true;
253 }
254 }
255 return false;
256 }
257
258 #include "servicessettingspage.moc"