1 /* This file is part of the KDE project
2 Copyright (C) 2006-2008 Matthias Kretz <kretz@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 version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
20 #include "globalconfig_p.h"
22 #include "factory_p.h"
23 #include "phonon/objectdescription.h"
24 #include "phonondefs_p.h"
25 #include "phonon/platformplugin.h"
26 #include "phonon/backendinterface.h"
27 #include "qsettingsgroup_p.h"
28 #include "phononnamespace_p.h"
30 #include <QtCore/QList>
31 #include <QtCore/QVariant>
38 GlobalConfig::GlobalConfig(QObject
*parent
)
40 , m_config(QLatin1String("kde.org"), QLatin1String("libphonon"))
44 GlobalConfig::~GlobalConfig()
49 FilterAdvancedDevices
= 1,
50 FilterHardwareDevices
= 2
53 static void filter(ObjectDescriptionType type
, BackendInterface
*backendIface
, QList
<int> *list
, int whatToFilter
)
55 QMutableListIterator
<int> it(*list
);
56 while (it
.hasNext()) {
57 const QHash
<QByteArray
, QVariant
> properties
= backendIface
->objectDescriptionProperties(type
, it
.next());
59 if (whatToFilter
& FilterAdvancedDevices
) {
60 var
= properties
.value("isAdvanced");
61 if (var
.isValid() && var
.toBool()) {
66 if (whatToFilter
& FilterHardwareDevices
) {
67 var
= properties
.value("isHardwareDevice");
68 if (var
.isValid() && var
.toBool()) {
76 static QList
<int> listSortedByConfig(const QSettingsGroup
&backendConfig
, Phonon::Category category
, QList
<int> &defaultList
)
78 if (defaultList
.size() <= 1) {
82 // make entries unique
84 QMutableListIterator
<int> it(defaultList
);
85 while (it
.hasNext()) {
86 if (seen
.contains(it
.next())) {
89 seen
.insert(it
.value());
94 QString categoryKey
= QLatin1String("Category_") + QString::number(static_cast<int>(category
));
95 if (!backendConfig
.hasKey(categoryKey
)) {
96 // no list in config for the given category
97 categoryKey
= QLatin1String("Category_") + QString::number(static_cast<int>(Phonon::NoCategory
));
98 if (!backendConfig
.hasKey(categoryKey
)) {
99 // no list in config for NoCategory
104 //Now the list from m_config
105 QList
<int> deviceList
= backendConfig
.value(categoryKey
, QList
<int>());
107 //if there are devices in m_config that the backend doesn't report, remove them from the list
108 QMutableListIterator
<int> i(deviceList
);
109 while (i
.hasNext()) {
110 if (0 == defaultList
.removeAll(i
.next())) {
115 //if the backend reports more devices that are not in m_config append them to the list
116 deviceList
+= defaultList
;
121 QList
<int> GlobalConfig::audioOutputDeviceListFor(Phonon::Category category
, HideAdvancedDevicesOverride override
) const
123 //The devices need to be stored independently for every backend
124 const QSettingsGroup
backendConfig(&m_config
, QLatin1String("AudioOutputDevice")); // + Factory::identifier());
125 const QSettingsGroup
generalGroup(&m_config
, QLatin1String("General"));
126 const bool hideAdvancedDevices
= (override
== FromSettings
127 ? generalGroup
.value(QLatin1String("HideAdvancedDevices"), true)
128 : static_cast<bool>(override
));
130 PlatformPlugin
*platformPlugin
= Factory::platformPlugin();
131 BackendInterface
*backendIface
= qobject_cast
<BackendInterface
*>(Factory::backend());
133 QList
<int> defaultList
;
134 if (platformPlugin
) {
135 // the platform plugin lists the audio devices for the platform
136 // this list already is in default order (as defined by the platform plugin)
137 defaultList
= platformPlugin
->objectDescriptionIndexes(Phonon::AudioOutputDeviceType
);
138 if (hideAdvancedDevices
) {
139 QMutableListIterator
<int> it(defaultList
);
140 while (it
.hasNext()) {
141 AudioOutputDevice objDesc
= AudioOutputDevice::fromIndex(it
.next());
142 const QVariant var
= objDesc
.property("isAdvanced");
143 if (var
.isValid() && var
.toBool()) {
150 // lookup the available devices directly from the backend (mostly for virtual devices)
152 // this list already is in default order (as defined by the backend)
153 QList
<int> list
= backendIface
->objectDescriptionIndexes(Phonon::AudioOutputDeviceType
);
154 if (hideAdvancedDevices
|| !defaultList
.isEmpty()) {
155 filter(AudioOutputDeviceType
, backendIface
, &list
,
156 (hideAdvancedDevices
? FilterAdvancedDevices
: 0)
157 // the platform plugin already provided the hardware devices
158 | (defaultList
.isEmpty() ? 0 : FilterHardwareDevices
)
164 return listSortedByConfig(backendConfig
, category
, defaultList
);
167 int GlobalConfig::audioOutputDeviceFor(Phonon::Category category
) const
169 QList
<int> ret
= audioOutputDeviceListFor(category
);
175 QList
<int> GlobalConfig::audioCaptureDeviceListFor(Phonon::Category category
, HideAdvancedDevicesOverride override
) const
177 //The devices need to be stored independently for every backend
178 const QSettingsGroup
backendConfig(&m_config
, QLatin1String("AudioCaptureDevice")); // + Factory::identifier());
179 const QSettingsGroup
generalGroup(&m_config
, QLatin1String("General"));
180 const bool hideAdvancedDevices
= (override
== FromSettings
181 ? generalGroup
.value(QLatin1String("HideAdvancedDevices"), true)
182 : static_cast<bool>(override
));
184 PlatformPlugin
*platformPlugin
= Factory::platformPlugin();
185 BackendInterface
*backendIface
= qobject_cast
<BackendInterface
*>(Factory::backend());
187 QList
<int> defaultList
;
188 if (platformPlugin
) {
189 // the platform plugin lists the audio devices for the platform
190 // this list already is in default order (as defined by the platform plugin)
191 defaultList
= platformPlugin
->objectDescriptionIndexes(Phonon::AudioCaptureDeviceType
);
192 if (hideAdvancedDevices
) {
193 QMutableListIterator
<int> it(defaultList
);
194 while (it
.hasNext()) {
195 AudioCaptureDevice objDesc
= AudioCaptureDevice::fromIndex(it
.next());
196 const QVariant var
= objDesc
.property("isAdvanced");
197 if (var
.isValid() && var
.toBool()) {
204 // lookup the available devices directly from the backend (mostly for virtual devices)
206 // this list already is in default order (as defined by the backend)
207 QList
<int> list
= backendIface
->objectDescriptionIndexes(Phonon::AudioCaptureDeviceType
);
208 if (hideAdvancedDevices
|| !defaultList
.isEmpty()) {
209 filter(AudioCaptureDeviceType
, backendIface
, &list
,
210 (hideAdvancedDevices
? FilterAdvancedDevices
: 0)
211 // the platform plugin already provided the hardware devices
212 | (defaultList
.isEmpty() ? 0 : FilterHardwareDevices
)
218 return listSortedByConfig(backendConfig
, category
, defaultList
);
221 int GlobalConfig::audioCaptureDeviceFor(Phonon::Category category
) const
223 QList
<int> ret
= audioCaptureDeviceListFor(category
);
229 } // namespace Phonon
233 #include "moc_globalconfig_p.cpp"