add more spacing
[personal-kdebase.git] / workspace / solid / kcm / backendchooser.cpp
blob9fc20d839103131912f45dd840075c37c1de6196
1 /* This file is part of the KDE project
2 Copyright (C) 2006 Kevin Ottens <ervin@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (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 Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
21 #include "backendchooser.h"
23 #include <kservicetypeprofile.h>
24 #include <kservicetype.h>
25 #include <kservicetypetrader.h>
26 #include <kconfig.h>
27 #include <QStringList>
28 #include <QListWidgetItem>
29 #include <kapplication.h>
30 #include <kicon.h>
31 #include <QList>
34 static bool equals(const KService::List &list1, const KService::List &list2)
36 if (list1.size()!=list2.size())
38 return false;
41 for (int i=0; i<list1.size(); ++i)
43 if (list1[i]->name()!=list2[i]->name())
45 return false;
49 return true;
53 BackendChooser::BackendChooser(QWidget *parent, const QString &backendType)
54 : QWidget(parent), m_backendType(backendType)
56 m_ui.setupUi(this);
58 connect(m_ui.listView, SIGNAL(itemSelectionChanged()),
59 this, SLOT(slotSelectionChanged()));
60 connect(m_ui.upButton, SIGNAL(clicked()),
61 this, SLOT(slotUpClicked()));
62 connect(m_ui.downButton, SIGNAL(clicked()),
63 this, SLOT(slotDownClicked()));
65 KServiceType::Ptr service_type = KServiceType::serviceType(backendType);
67 QString title = service_type->comment();
69 if (title.isEmpty())
71 title = backendType;
74 m_ui.groupBox->setTitle(title);
77 void BackendChooser::load()
79 m_initServices = KServiceTypeTrader::self()->query(m_backendType, "Type == 'Service'");
81 loadServicesInView(m_initServices);
84 void BackendChooser::loadServicesInView(const KService::List &services)
86 m_services.clear();
87 m_ui.listView->clear();
89 foreach (KService::Ptr service, services)
91 m_ui.listView->addItem(service->name());
92 m_services[service->name()] = service;
95 m_ui.listView->setItemSelected(m_ui.listView->item(0), true);
98 KService::List BackendChooser::servicesFromView()
100 KService::List services;
102 int count = m_ui.listView->count();
104 for (int i=0; i<count; ++i)
106 QListWidgetItem *item = m_ui.listView->item(i);
107 KService::Ptr service = m_services[item->text()];
108 services.append(service);
111 return services;
114 void BackendChooser::save()
116 if (!equals(m_initServices, servicesFromView()))
118 m_initServices = servicesFromView();
119 KServiceTypeProfile::writeServiceTypeProfile(m_backendType, m_initServices);
120 emit changed(false);
124 void BackendChooser::defaults()
126 KService::List currentServices = servicesFromView();
127 KService::List defaultServices = KServiceTypeTrader::self()->defaultOffers(m_backendType, "Type == 'Service'");
129 if (!equals(currentServices, defaultServices))
131 loadServicesInView(KServiceTypeTrader::self()->defaultOffers(m_backendType, "Type == 'Service'"));
132 emit changed(!equals(m_initServices, servicesFromView()));
136 void BackendChooser::slotSelectionChanged()
138 KService::Ptr service;
140 for (int i=0; i<m_ui.listView->count(); ++i)
142 QListWidgetItem *item = m_ui.listView->item(i);
144 if (m_ui.listView->isItemSelected(item))
146 service = m_services[item->text()];
147 break;
151 if (service)
153 m_ui.iconLabel->setPixmap(KIcon(service->icon()).pixmap(32));
154 m_ui.nameLabel->setText(service->name());
155 m_ui.commentLabel->setText(service->comment());
156 m_ui.versionLabel->setText(service->property("X-KDE-SolidBackendInfo-Version").toString());
160 void BackendChooser::slotUpClicked()
162 QList<QListWidgetItem *> selectedList = m_ui.listView->selectedItems();
164 foreach (QListWidgetItem *selected, selectedList)
166 int row = m_ui.listView->row(selected);
168 if (row > 0)
170 QListWidgetItem *taken = m_ui.listView->takeItem(row - 1);
171 m_ui.listView->insertItem(row, taken);
173 emit changed(!equals(m_initServices, servicesFromView()));
174 return;
179 void BackendChooser::slotDownClicked()
181 QList<QListWidgetItem *> selectedList = m_ui.listView->selectedItems();
183 foreach (QListWidgetItem *selected, selectedList)
185 int row = m_ui.listView->row(selected);
187 if (row + 1 < m_ui.listView->count())
189 QListWidgetItem *taken = m_ui.listView->takeItem(row + 1);
190 m_ui.listView->insertItem(row, taken);
192 emit changed(!equals(m_initServices, servicesFromView()));
193 return;
198 #include "backendchooser.moc"