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}
32
33CardPrivate::CardPrivate(Card *q)
34 : q(q)
35{
36}
37
38CardPrivate::~CardPrivate()
39{
40}
41
42void CardPrivate::update(const pa_card_info *info)
43{
44 q->IndexedPulseObject::d->updatePulseObject(info);
45 q->PulseObject::d->updateProperties(info);
46
47 QStringList newProfiles;
48 QStringList existingProfiles;
49
50 for (const Profile *profile : std::as_const(m_profiles)) {
51 existingProfiles << profile->name();
52 }
53
54 for (auto **it = info->profiles2; it && *it != nullptr; ++it) {
55 const QString name = QString::fromUtf8((*it)->name);
56 newProfiles << name;
57 Profile *profile = nullptr;
58 if (existingProfiles.contains(name)) {
59 profile = m_profiles[existingProfiles.indexOf(name)];
60 } else {
61 profile = new Profile(q);
62 m_profiles << profile;
63 }
64 profile->d->setInfo(*it);
65 }
66
68 while (it.hasNext()) {
69 Profile *profile = it.next();
70
71 if (!newProfiles.contains(profile->name())) {
72 it.remove();
73 delete profile;
74 }
75 }
76
77 for (Profile *profile : std::as_const(m_profiles)) {
78 if (QString::fromUtf8(info->active_profile2->name) == profile->name()) {
79 m_activeProfileIndex = m_profiles.indexOf(profile);
80 }
81 }
82
83 Q_EMIT q->profilesChanged();
84 Q_EMIT q->activeProfileIndexChanged();
85
86 QStringList newPorts;
87 QStringList existingPorts;
88
89 for (const Port *port : std::as_const(m_ports)) {
90 existingPorts << port->name();
91 }
92 for (auto **it = info->ports; it && *it != nullptr; ++it) {
93 const QString name = QString::fromUtf8((*it)->name);
94 newPorts << name;
95 CardPort *port = nullptr;
96 if (existingPorts.contains(name)) {
97 port = m_ports[existingPorts.indexOf(name)];
98 } else {
99 port = new CardPort(q);
100 m_ports << port;
101 }
102 port->d->setInfo(*it);
103 }
104
105 QList<CardPort *> toDiscard;
106 for (auto port : std::as_const(m_ports)) {
107 if (!newPorts.contains(port->name())) {
108 toDiscard << port;
109 }
110 }
111 for (auto port : std::as_const(toDiscard)) {
112 m_ports.removeOne(port);
113 delete port;
114 }
115
116 Q_EMIT q->portsChanged();
117}
118
119QList<Profile *> Card::profiles() const
120{
121 return d->m_profiles;
122}
123
124quint32 Card::activeProfileIndex() const
125{
126 return d->m_activeProfileIndex;
127}
128
129void Card::setActiveProfileIndex(quint32 profileIndex)
130{
131 const Profile *profile = qobject_cast<Profile *>(profiles().at(profileIndex));
132 Context::instance()->setCardProfile(index(), profile->name());
133}
134
135QList<CardPort *> Card::ports() const
136{
137 return d->m_ports;
138}
139
140QList<Sink *> Card::sinks() const
141{
142 QList<Sink *> ret;
143
144 const auto allSinks = Context::instance()->sinks();
145 for (Sink *sink : allSinks) {
146 if (sink->cardIndex() == IndexedPulseObject::d->m_index) {
147 ret << sink;
148 }
149 }
150
151 return ret;
152}
153
154QList<Source *> Card::sources() const
155{
156 QList<Source *> ret;
157
158 const auto allSources = Context::instance()->sources();
159 for (Source *source : allSources) {
160 if (source->cardIndex() == IndexedPulseObject::d->m_index) {
161 ret << source;
162 }
163 }
164
165 return ret;
166}
167
168} // 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 Fri Dec 20 2024 11:50:53 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.