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  if (QMediaDevices::defaultAudioOutput().isNull()) return { Unknown, "" };
55  return { OutputDevice, QMediaDevices::defaultAudioOutput().description() };
56  }
57 
59  {
60  if (QMediaDevices::defaultAudioInput().isNull()) return { Unknown, "" };
61  return { InputDevice, QMediaDevices::defaultAudioInput().description() };
62  }
63 
65  {
66  if (index.isMyself()) { return QVariant::fromValue(*this); }
67  switch (index.frontCasted<ColumnIndex>())
68  {
69  case IndexDeviceType: return QVariant::fromValue(this->getType());
70  case IndexDeviceTypeAsString: return QVariant::fromValue(this->getTypeAsString());
71  case IndexName: return QVariant::fromValue(this->getName());
72  case IndexIdentifier: return m_identifier.propertyByIndex(index.copyFrontRemoved());
73  default: break;
74  }
75  return CValueObject::propertyByIndex(index);
76  }
77 
79  {
80  if (index.isMyself())
81  {
82  (*this) = variant.value<CAudioDeviceInfo>();
83  return;
84  }
85  switch (index.frontCasted<ColumnIndex>())
86  {
87  case IndexDeviceType: m_type = static_cast<DeviceType>(variant.toInt()); return;
88  case IndexName: m_deviceName = variant.toString(); return;
89  case IndexIdentifier: m_identifier.setPropertyByIndex(index.copyFrontRemoved(), variant); return;
90  default: break;
91  }
92  CValueObject::setPropertyByIndex(index, variant);
93  }
94 
96  {
97  if (index.isMyself()) { return m_deviceName.compare(compareValue.m_deviceName, Qt::CaseInsensitive); }
98  switch (index.frontCasted<ColumnIndex>())
99  {
100  case IndexDeviceTypeAsString:
101  case IndexDeviceType: return Compare::compare(m_type, compareValue.m_type);
102  case IndexName: return m_deviceName.compare(compareValue.m_deviceName, Qt::CaseInsensitive);
103  case IndexIdentifier:
104  return m_identifier.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getIdentifier());
105  default: break;
106  }
107  SWIFT_VERIFY_X(false, Q_FUNC_INFO, qUtf8Printable("No comparison for index " + index.toQString()));
108  return 0;
109  }
110 
112  {
113  static const QString i("input");
114  static const QString o("output");
115  static const QString u("unknown");
116 
117  switch (t)
118  {
119  case InputDevice: return i;
120  case OutputDevice: return o;
121  default: break;
122  }
123  return u;
124  }
125 
127  {
128  Q_UNUSED(i18n)
129  if (!m_identifier.hasName()) { return m_deviceName; }
130  return m_deviceName % u" [" % this->getMachineName() % u']';
131  }
132 
133 } // 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:158
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: mixinindex.h:165
SWIFT_MISC_EXPORT bool stringCompare(const QString &c1, const QString &c2, Qt::CaseSensitivity cs)
String compare.
int compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs)
bool isEmpty() const const
CaseInsensitive
QVariant fromValue(T &&value)
int toInt(bool *ok) const const
QString toString() const const
T value() const &const
#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