PulseAudio Qt Bindings

card.cpp
1/*
2 SPDX-FileCopyrightText: 2014-2015 Harald Sitter <sitter@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "card.h"
8#include "card_p.h"
9#include "debug.h"
10
11#include "context.h"
12#include "indexedpulseobject_p.h"
13#include "port_p.h"
14#include "profile_p.h"
15
16namespace PulseAudioQt
17{
18Card::Card(QObject *parent)
19 : IndexedPulseObject(parent)
20 , d(new CardPrivate(this))
21{
22 connect(Context::instance(), &Context::sinkAdded, this, &Card::sinksChanged);
23 connect(Context::instance(), &Context::sinkRemoved, this, &Card::sinksChanged);
24
25 connect(Context::instance(), &Context::sourceAdded, this, &Card::sourcesChanged);
26 connect(Context::instance(), &Context::sourceRemoved, this, &Card::sourcesChanged);
27}
28
29Card::~Card()
30{
31 delete d;
32}
33
34CardPrivate::CardPrivate(Card *q)
35 : q(q)
36{
37}
38
39CardPrivate::~CardPrivate()
40{
41}
42
43void CardPrivate::update(const pa_card_info *info)
44{
45 q->IndexedPulseObject::d->updatePulseObject(info);
46 q->PulseObject::d->updateProperties(info);
47
48 QStringList newProfiles;
49 QStringList existingProfiles;
50
51 for (const Profile *profile : qAsConst(m_profiles)) {
52 existingProfiles << profile->name();
53 }
54
55 for (auto **it = info->profiles2; it && *it != nullptr; ++it) {
56 const QString name = QString::fromUtf8((*it)->name);
57 newProfiles << name;
58 Profile *profile = nullptr;
59 if (existingProfiles.contains(name)) {
60 profile = m_profiles[existingProfiles.indexOf(name)];
61 } else {
62 profile = new Profile(q);
63 m_profiles << profile;
64 }
65 profile->d->setInfo(*it);
66 }
67
69 while (it.hasNext()) {
70 Profile *profile = it.next();
71
72 if (!newProfiles.contains(profile->name())) {
73 it.remove();
74 delete profile;
75 }
76 }
77
78 for (Profile *profile : qAsConst(m_profiles)) {
79 if (info->active_profile2->name == profile->name()) {
80 m_activeProfileIndex = m_profiles.indexOf(profile);
81 }
82 }
83
84 Q_EMIT q->profilesChanged();
85 Q_EMIT q->activeProfileIndexChanged();
86
87 QStringList newPorts;
88 QStringList existingPorts;
89
90 for (const Port *port : qAsConst(m_ports)) {
91 existingPorts << port->name();
92 }
93 for (auto **it = info->ports; it && *it != nullptr; ++it) {
94 const QString name = QString::fromUtf8((*it)->name);
95 newPorts << name;
96 CardPort *port = nullptr;
97 if (existingPorts.contains(name)) {
98 port = m_ports[existingPorts.indexOf(name)];
99 } else {
100 port = new CardPort(q);
101 m_ports << port;
102 }
103 port->d->setInfo(*it);
104 }
105
106 for (CardPort *port : qAsConst(m_ports)) {
107 if (!newPorts.contains(port->name())) {
108 m_ports.removeOne(port);
109 delete port;
110 }
111 }
112
113 Q_EMIT q->portsChanged();
114}
115
116QList<Profile *> Card::profiles() const
117{
118 return d->m_profiles;
119}
120
121quint32 Card::activeProfileIndex() const
122{
123 return d->m_activeProfileIndex;
124}
125
126void Card::setActiveProfileIndex(quint32 profileIndex)
127{
128 const Profile *profile = qobject_cast<Profile *>(profiles().at(profileIndex));
129 Context::instance()->setCardProfile(index(), profile->name());
130}
131
132QList<CardPort *> Card::ports() const
133{
134 return d->m_ports;
135}
136
137QList<Sink *> Card::sinks() const
138{
140
141 const auto allSinks = Context::instance()->sinks();
142 for (Sink *sink : allSinks) {
143 if (sink->cardIndex() == IndexedPulseObject::d->m_index) {
144 ret << sink;
145 }
146 }
147
148 return ret;
149}
150
151QList<Source *> Card::sources() const
152{
154
155 const auto allSources = Context::instance()->sources();
156 for (Source *source : allSources) {
157 if (source->cardIndex() == IndexedPulseObject::d->m_index) {
158 ret << source;
159 }
160 }
161
162 return ret;
163}
164
165} // PulseAudioQt
A Port associated with a Card.
Definition cardport.h:20
A PulseAudio port.
Definition port.h:19
A PulseAudio profile.
Definition profile.h:21
A PulseAudio sink.
Definition sink.h:20
A PulseAudio source.
Definition source.h:20
The primary namespace of PulseAudioQt.
Definition card.cpp:17
bool hasNext() const const
T qobject_cast(QObject *object)
QString fromUtf8(QByteArrayView str)
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
qsizetype indexOf(const QRegularExpression &re, qsizetype from) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:07 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.