swift
audiodeviceinfo.cpp
1 // SPDX-FileCopyrightText: Copyright (c) 2013 swift project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QAudioDevice>
7 #include <QHostInfo>
8 #include <QMediaDevices>
9 #include <QtGlobal>
10 
11 #include "misc/comparefunctions.h"
12 #include "misc/stringutils.h"
13 #include "misc/verify.h"
14 
15 SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::audio, CAudioDeviceInfo)
16 
17 namespace swift::misc::audio
18 {
19  CAudioDeviceInfo::CAudioDeviceInfo() : m_type(Unknown) {}
20 
22  : m_type(static_cast<int>(type)), m_deviceName(name)
23  {}
24 
26  {
27  if (m_deviceName.isEmpty()) { return false; }
28  if (m_deviceName == "default") { return true; }
29  if (this->isInputDevice() && m_deviceName == QMediaDevices::defaultAudioInput().description()) { return true; }
30  if (this->isOutputDevice() && m_deviceName == QMediaDevices::defaultAudioOutput().description())
31  {
32  return true;
33  }
34  return false;
35  }
36 
38  {
39  return device.getType() == this->getType() &&
40  stringCompare(device.getName(), this->getName(), Qt::CaseInsensitive) &&
41  stringCompare(device.getMachineName(), this->getMachineName(), Qt::CaseInsensitive);
42  }
43 
45  {
46  return device.getType() == this->getType() &&
47  device.getIdentifier().getProcessId() == this->getIdentifier().getProcessId() &&
48  stringCompare(device.getName(), this->getName(), Qt::CaseInsensitive) &&
49  stringCompare(device.getMachineName(), this->getMachineName(), Qt::CaseInsensitive);
50  }
51 
53  {
54  return { OutputDevice, QMediaDevices::defaultAudioOutput().description() };
55  }
56 
58  {
59  return { InputDevice, QMediaDevices::defaultAudioInput().description() };
60  }
61 
63  {
64  if (index.isMyself()) { return QVariant::fromValue(*this); }
65  switch (index.frontCasted<ColumnIndex>())
66  {
67  case IndexDeviceType: return QVariant::fromValue(this->getType());
68  case IndexDeviceTypeAsString: return QVariant::fromValue(this->getTypeAsString());
69  case IndexName: return QVariant::fromValue(this->getName());
70  case IndexIdentifier: return m_identifier.propertyByIndex(index.copyFrontRemoved());
71  default: break;
72  }
73  return CValueObject::propertyByIndex(index);
74  }
75 
76  void CAudioDeviceInfo::setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
77  {
78  if (index.isMyself())
79  {
80  (*this) = variant.value<CAudioDeviceInfo>();
81  return;
82  }
83  switch (index.frontCasted<ColumnIndex>())
84  {
85  case IndexDeviceType: m_type = static_cast<DeviceType>(variant.toInt()); return;
86  case IndexName: m_deviceName = variant.toString(); return;
87  case IndexIdentifier: m_identifier.setPropertyByIndex(index.copyFrontRemoved(), variant); return;
88  default: break;
89  }
90  CValueObject::setPropertyByIndex(index, variant);
91  }
92 
94  {
95  if (index.isMyself()) { return m_deviceName.compare(compareValue.m_deviceName, Qt::CaseInsensitive); }
96  switch (index.frontCasted<ColumnIndex>())
97  {
98  case IndexDeviceTypeAsString:
99  case IndexDeviceType: return Compare::compare(m_type, compareValue.m_type);
100  case IndexName: return m_deviceName.compare(compareValue.m_deviceName, Qt::CaseInsensitive);
101  case IndexIdentifier:
102  return m_identifier.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getIdentifier());
103  default: break;
104  }
105  SWIFT_VERIFY_X(false, Q_FUNC_INFO, qUtf8Printable("No comparison for index " + index.toQString()));
106  return 0;
107  }
108 
110  {
111  static const QString i("input");
112  static const QString o("output");
113  static const QString u("unknown");
114 
115  switch (t)
116  {
117  case InputDevice: return i;
118  case OutputDevice: return o;
119  default: break;
120  }
121  return u;
122  }
123 
124  QString CAudioDeviceInfo::convertToQString(bool i18n) const
125  {
126  Q_UNUSED(i18n)
127  if (!m_identifier.hasName()) { return m_deviceName; }
128  return m_deviceName % u" [" % this->getMachineName() % u']';
129  }
130 
131 } // namespace swift::misc::audio
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: identifier.cpp:258
qint64 getProcessId() const
Get process id.
Definition: identifier.h:115
bool hasName() const
Has name.
Definition: identifier.h:83
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
Definition: identifier.cpp:213
int comparePropertyByIndex(CPropertyIndexRef index, const CIdentifier &compareValue) const
Compare for index.
Definition: identifier.cpp:234
Non-owning reference to a CPropertyIndex with a subset of its features.
Q_REQUIRED_RESULT CPropertyIndexRef copyFrontRemoved() const
Copy with first element removed.
QString toQString(bool i18n=false) const
Cast as QString.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
Value object encapsulating information of a audio device.
bool matchesNameTypeMachineProcess(const CAudioDeviceInfo &device) const
Matching name, type and machine and process.
const QString & getMachineName() const
Machine name.
static CAudioDeviceInfo getDefaultInputDevice()
Default input device.
bool isInputDevice() const
Input device.
int comparePropertyByIndex(CPropertyIndexRef index, const CAudioDeviceInfo &compareValue) const
Compare for index.
const CIdentifier & getIdentifier() const
Identifier.
bool isDefault() const
Is this a default device?
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
const QString & getTypeAsString() const
Type as string.
bool isOutputDevice() const
Output device.
static CAudioDeviceInfo getDefaultOutputDevice()
Default output device.
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
bool matchesNameTypeMachineName(const CAudioDeviceInfo &device) const
Matching name, type and machine.
const QString & getName() const
Get the device name.
static const QString & deviceTypeToString(DeviceType t)
Device type as string.
QString convertToQString(bool i18n=false) const
Cast as QString.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: mixinindex.h:160
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: mixinindex.h:167
SWIFT_MISC_EXPORT bool stringCompare(const QString &c1, const QString &c2, Qt::CaseSensitivity cs)
String compare.
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition: valueobject.h:67
#define SWIFT_VERIFY_X(COND, WHERE, WHAT)
A weaker kind of assert.
Definition: verify.h:26