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 "context.h"
12 #include "indexedpulseobject_p.h"
13 #include "port_p.h"
14 #include "profile_p.h"
15 
16 namespace PulseAudioQt
17 {
18 Card::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 
29 Card::~Card()
30 {
31  delete d;
32 }
33 
34 CardPrivate::CardPrivate(Card *q)
35  : q(q)
36 {
37 }
38 
39 CardPrivate::~CardPrivate()
40 {
41 }
42 
43 void 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 
68  for (Profile *profile : qAsConst(m_profiles)) {
69  if (!newProfiles.contains(profile->name())) {
70  m_profiles.removeOne(profile);
71  delete profile;
72  }
73  }
74 
75  for (Profile *profile : qAsConst(m_profiles)) {
76  if (info->active_profile2->name == profile->name()) {
77  m_activeProfileIndex = m_profiles.indexOf(profile);
78  }
79  }
80 
81  Q_EMIT q->profilesChanged();
82  Q_EMIT q->activeProfileIndexChanged();
83 
84  QStringList newPorts;
85  QStringList existingPorts;
86 
87  for (const Port *port : qAsConst(m_ports)) {
88  existingPorts << port->name();
89  }
90  for (auto **it = info->ports; it && *it != nullptr; ++it) {
91  const QString name = QString::fromUtf8((*it)->name);
92  newPorts << name;
93  CardPort *port = nullptr;
94  if (existingPorts.contains(name)) {
95  port = m_ports[existingPorts.indexOf(name)];
96  } else {
97  port = new CardPort(q);
98  m_ports << port;
99  }
100  port->d->setInfo(*it);
101  }
102 
103  for (CardPort *port : qAsConst(m_ports)) {
104  if (!newPorts.contains(port->name())) {
105  m_ports.removeOne(port);
106  delete port;
107  }
108  }
109 
110  Q_EMIT q->portsChanged();
111 }
112 
113 QList<Profile *> Card::profiles() const
114 {
115  return d->m_profiles;
116 }
117 
118 quint32 Card::activeProfileIndex() const
119 {
120  return d->m_activeProfileIndex;
121 }
122 
123 void Card::setActiveProfileIndex(quint32 profileIndex)
124 {
125  const Profile *profile = qobject_cast<Profile *>(profiles().at(profileIndex));
126  Context::instance()->setCardProfile(index(), profile->name());
127 }
128 
129 QList<CardPort *> Card::ports() const
130 {
131  return d->m_ports;
132 }
133 
134 QList<Sink *> Card::sinks() const
135 {
136  QList<Sink *> ret;
137 
138  const auto allSinks = Context::instance()->sinks();
139  for (Sink *sink : allSinks) {
140  if (sink->cardIndex() == IndexedPulseObject::d->m_index) {
141  ret << sink;
142  }
143  }
144 
145  return ret;
146 }
147 
148 QList<Source *> Card::sources() const
149 {
150  QList<Source *> ret;
151 
152  const auto allSources = Context::instance()->sources();
153  for (Source *source : allSources) {
154  if (source->cardIndex() == IndexedPulseObject::d->m_index) {
155  ret << source;
156  }
157  }
158 
159  return ret;
160 }
161 
162 } // PulseAudioQt
QString fromUtf8(const char *str, int size)
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
A PulseAudio profile.
Definition: profile.h:20
A Port associated with a Card.
Definition: cardport.h:19
The primary namespace of PulseAudioQt.
Definition: card.cpp:16
int indexOf(QStringView str, int from) const const
A PulseAudio source.
Definition: source.h:19
A PulseAudio port.
Definition: port.h:18
A PulseAudio sink.
Definition: sink.h:19
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Wed Jun 29 2022 04:15:07 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.