1 /* This file is part of the KDE project
2 Copyright (C) 2004-2007 Matthias Kretz <kretz@kde.org>
4 This program 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) version 3.
9 This library 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 GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
21 #include "backendselection.h"
24 #include <kservicetypeprofile.h>
25 #include <kservicetypetrader.h>
27 #include <QtCore/QStringList>
28 #include <QtGui/QListWidget>
29 #include <kapplication.h>
31 #include <QtCore/QList>
32 #include <QtDBus/QtDBus>
33 #include <kcmoduleproxy.h>
35 BackendSelection::BackendSelection(QWidget
*parent
)
39 m_down
->setIcon(KIcon("go-down"));
40 m_up
->setIcon(KIcon("go-up"));
41 m_comment
->setWordWrap(true);
43 m_emptyPage
= stackedWidget
->addWidget(new QWidget());
45 connect(m_select
, SIGNAL(itemSelectionChanged()),
46 SLOT(selectionChanged()));
47 //connect(m_website, SIGNAL(leftClickedUrl(const QString &)),
48 //kapp, SLOT(invokeBrowser(const QString &)));
49 connect(m_up
, SIGNAL(clicked()), SLOT(up()));
50 connect(m_down
, SIGNAL(clicked()), SLOT(down()));
53 void BackendSelection::load()
55 const KService::List offers
= KServiceTypeTrader::self()->query("PhononBackend",
56 "Type == 'Service' and [X-KDE-PhononBackendInfo-InterfaceVersion] == 1");
57 // the offers are already sorted for preference
59 foreach (KCModuleProxy
*proxy
, m_kcms
) {
66 void BackendSelection::showBackendKcm(const KService::Ptr
&backendService
)
68 QString parentComponent
= backendService
->library();
69 if (!m_kcms
.contains(parentComponent
)) {
70 const KService::List offers
= KServiceTypeTrader::self()->query("KCModule",
71 QString("'%1' in [X-KDE-ParentComponents]").arg(parentComponent
));
72 if (offers
.isEmpty()) {
73 m_kcms
.insert(parentComponent
, 0);
75 KCModuleProxy
*proxy
= new KCModuleProxy(offers
.first());
76 connect(proxy
, SIGNAL(changed(bool)), SIGNAL(changed()));
77 m_kcms
.insert(parentComponent
, proxy
);
78 stackedWidget
->addWidget(proxy
);
81 QWidget
*w
= m_kcms
.value(parentComponent
);
83 stackedWidget
->setCurrentWidget(w
);
85 stackedWidget
->setCurrentIndex(m_emptyPage
);
89 void BackendSelection::loadServices(const KService::List
&offers
)
94 KService::List::const_iterator it
= offers
.begin();
95 const KService::List::const_iterator end
= offers
.end();
96 for (; it
!= end
; ++it
)
98 KService::Ptr service
= *it
;
99 m_select
->addItem(service
->name());
100 m_services
[service
->name()] = service
;
102 m_select
->setItemSelected(m_select
->item(0), true);
105 void BackendSelection::save()
107 // save embedded KCMs
108 foreach (KCModuleProxy
*proxy
, m_kcms
) {
114 // save to servicetype profile
115 KService::List services
;
116 unsigned int count
= m_select
->count();
117 for (unsigned int i
= 0; i
< count
; ++i
)
119 QListWidgetItem
*item
= m_select
->item(i
);
120 KService::Ptr service
= m_services
[item
->text()];
121 services
.append(service
);
124 // get the currently used list
125 const KService::List offers
= KServiceTypeTrader::self()->query("PhononBackend",
126 "Type == 'Service' and [X-KDE-PhononBackendInfo-InterfaceVersion] == 1");
128 // we have to compare the lists manually as KService::Ptr::operator== is not what we want for
130 if (offers
.size() == services
.size()) {
132 for (int i
= 0; i
< offers
.size(); ++i
) {
133 if (offers
[i
]->entryPath() != services
[i
]->entryPath()) {
143 // be very conservative with this signal as it interrupts all playback:
144 if (offers
!= services
) {
145 KServiceTypeProfile::writeServiceTypeProfile("PhononBackend", services
);
147 QDBusMessage signal
= QDBusMessage::createSignal("/", "org.kde.Phonon.Factory", "phononBackendChanged");
148 QDBusConnection::sessionBus().send(signal
);
152 void BackendSelection::defaults()
154 foreach (KCModuleProxy
*proxy
, m_kcms
) {
160 loadServices(KServiceTypeTrader::self()->defaultOffers("PhononBackend"));
163 void BackendSelection::selectionChanged()
165 KService::Ptr service
;
166 if (m_select
->selectedItems().isEmpty()) {
167 m_up
->setEnabled(false);
168 m_down
->setEnabled(false);
170 const QListWidgetItem
*const item
= m_select
->selectedItems().first();
171 m_up
->setEnabled(m_select
->row(item
) > 0);
172 m_down
->setEnabled(m_select
->row(item
) < m_select
->count() - 1);
173 service
= m_services
[item
->text()];
175 m_icon
->setPixmap(KIcon(service
->icon()).pixmap(128));
176 m_name
->setText(QString());//service->name());
177 m_comment
->setText(service
->comment());
178 const QString website
= service
->property("X-KDE-PhononBackendInfo-Website").toString();
179 m_website
->setText(QString("<a href=\"%1\">%1</a>").arg(website
));
180 connect(m_website
, SIGNAL(linkActivated(const QString
&)), SLOT(openWebsite(const QString
&)));
181 m_version
->setText(service
->property("X-KDE-PhononBackendInfo-Version").toString());
182 showBackendKcm(service
);
186 void BackendSelection::openWebsite(const QString
&url
)
188 new KRun(KUrl(url
), window());
191 void BackendSelection::up()
193 QList
<QListWidgetItem
*> selectedList
= m_select
->selectedItems();
194 foreach (QListWidgetItem
*selected
, selectedList
)
196 int row
= m_select
->row(selected
);
199 QListWidgetItem
*taken
= m_select
->takeItem(row
- 1);
200 m_select
->insertItem(row
, taken
);
207 void BackendSelection::down()
209 QList
<QListWidgetItem
*> selectedList
= m_select
->selectedItems();
210 foreach (QListWidgetItem
*selected
, selectedList
)
212 int row
= m_select
->row(selected
);
213 if (row
+ 1 < m_select
->count())
215 QListWidgetItem
*taken
= m_select
->takeItem(row
+ 1);
216 m_select
->insertItem(row
, taken
);
223 #include "backendselection.moc"