PulseAudio Qt Bindings

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

KDE's Doxygen guidelines are available online.