swift
Loading...
Searching...
No Matches
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
12#include "misc/stringutils.h"
13#include "misc/verify.h"
14
15SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::audio, CAudioDeviceInfo)
16
17namespace swift::misc::audio
18{
20
22 : m_type(static_cast<int>(type)), m_deviceName(name)
23 {}
24
26 {
27 if (m_deviceName.isEmpty())
28 {
29 return false;
30 }
31 if (m_deviceName == "default")
32 {
33 return true;
34 }
35 if (this->isInputDevice() && m_deviceName == QMediaDevices::defaultAudioInput().description())
36 {
37 return true;
38 }
39 if (this->isOutputDevice() && m_deviceName == QMediaDevices::defaultAudioOutput().description())
40 {
41 return true;
42 }
43 return false;
44 }
45
47 {
48 return device.getType() == this->getType() &&
49 stringCompare(device.getName(), this->getName(), Qt::CaseInsensitive) &&
50 stringCompare(device.getMachineName(), this->getMachineName(), Qt::CaseInsensitive);
51 }
52
54 {
55 return device.getType() == this->getType() &&
56 device.getIdentifier().getProcessId() == this->getIdentifier().getProcessId() &&
57 stringCompare(device.getName(), this->getName(), Qt::CaseInsensitive) &&
58 stringCompare(device.getMachineName(), this->getMachineName(), Qt::CaseInsensitive);
59 }
60
62 {
63 if (QMediaDevices::defaultAudioOutput().isNull()) return { Unknown, "" };
64 return { OutputDevice, QMediaDevices::defaultAudioOutput().description() };
65 }
66
68 {
69 if (QMediaDevices::defaultAudioInput().isNull()) return { Unknown, "" };
70 return { InputDevice, QMediaDevices::defaultAudioInput().description() };
71 }
72
74 {
75 if (index.isMyself())
76 {
77 return QVariant::fromValue(*this);
78 }
79 switch (index.frontCasted<ColumnIndex>())
80 {
81 case IndexDeviceType: return QVariant::fromValue(this->getType());
82 case IndexDeviceTypeAsString: return QVariant::fromValue(this->getTypeAsString());
83 case IndexName: return QVariant::fromValue(this->getName());
84 case IndexIdentifier: return m_identifier.propertyByIndex(index.copyFrontRemoved());
85 default: break;
86 }
88 }
89
91 {
92 if (index.isMyself())
93 {
94 (*this) = variant.value<CAudioDeviceInfo>();
95 return;
96 }
97 switch (index.frontCasted<ColumnIndex>())
98 {
99 case IndexDeviceType: m_type = static_cast<DeviceType>(variant.toInt()); return;
100 case IndexName: m_deviceName = variant.toString(); return;
101 case IndexIdentifier: m_identifier.setPropertyByIndex(index.copyFrontRemoved(), variant); return;
102 default: break;
103 }
104 CValueObject::setPropertyByIndex(index, variant);
105 }
106
108 {
109 if (index.isMyself())
110 {
111 return m_deviceName.compare(compareValue.m_deviceName, Qt::CaseInsensitive);
112 }
113 switch (index.frontCasted<ColumnIndex>())
114 {
115 case IndexDeviceTypeAsString:
116 case IndexDeviceType: return Compare::compare(m_type, compareValue.m_type);
117 case IndexName: return m_deviceName.compare(compareValue.m_deviceName, Qt::CaseInsensitive);
118 case IndexIdentifier:
119 return m_identifier.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getIdentifier());
120 default: break;
121 }
122 SWIFT_VERIFY_X(false, Q_FUNC_INFO, qUtf8Printable("No comparison for index " + index.toQString()));
123 return 0;
124 }
125
127 {
128 static const QString i("input");
129 static const QString o("output");
130 static const QString u("unknown");
131
132 switch (t)
133 {
134 case InputDevice: return i;
135 case OutputDevice: return o;
136 default: break;
137 }
138 return u;
139 }
140
142 {
143 Q_UNUSED(i18n)
144 if (!m_identifier.hasName())
145 {
146 return m_deviceName;
147 }
148 return m_deviceName % u" [" % this->getMachineName() % u']';
149 }
150
151} // namespace swift::misc::audio
qint64 getProcessId() const
Get process id.
Definition identifier.h:115
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.
bool matchesNameTypeMachineProcess(const CAudioDeviceInfo &device) const
Matching name, type and machine and process.
static CAudioDeviceInfo getDefaultInputDevice()
Default input device.
bool isInputDevice() const
Input device.
int comparePropertyByIndex(CPropertyIndexRef index, const CAudioDeviceInfo &compareValue) const
Compare for index.
const QString & getMachineName() const
Machine name.
const QString & getTypeAsString() const
Type as string.
bool isDefault() const
Is this a default device?
const QString & getName() const
Get the device name.
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
bool isOutputDevice() const
Output device.
static CAudioDeviceInfo getDefaultOutputDevice()
Default output device.
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
const CIdentifier & getIdentifier() const
Identifier.
bool matchesNameTypeMachineName(const CAudioDeviceInfo &device) const
Matching name, type and machine.
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:167
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition mixinindex.h:180
int compare(T a, T b)
Compare arithmetic values.
SWIFT_MISC_EXPORT bool stringCompare(const QString &c1, const QString &c2, Qt::CaseSensitivity cs)
String compare.
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