[AFV] Ref T739, device info improvements

* registration
* use identifier in device info list
* propertyByIndex/compareByIndex
* unRegisterDevices
This commit is contained in:
Klaus Basan
2019-10-29 23:56:50 +01:00
committed by Mat Sutcliffe
parent 7beed0225c
commit 134c805990
9 changed files with 151 additions and 25 deletions

View File

@@ -1175,8 +1175,8 @@ namespace BlackCore
const CAudioDeviceInfo o = m_output->device();
lock.unlock();
return i.matchesNameTypeHostName(inputDevice) &&
o.matchesNameTypeHostName(outputDevice);
return i.matchesNameTypeMachineName(inputDevice) &&
o.matchesNameTypeMachineName(outputDevice);
}
CAfvClient::ConnectionStatus CAfvClient::getConnectionStatus() const

View File

@@ -8,6 +8,8 @@
#include "blackmisc/audio/audiodeviceinfo.h"
#include "blackmisc/stringutils.h"
#include "blackmisc/comparefunctions.h"
#include "blackmisc/verify.h"
#include <QStringBuilder>
#include <QHostInfo>
@@ -19,13 +21,12 @@ namespace BlackMisc
namespace Audio
{
CAudioDeviceInfo::CAudioDeviceInfo() :
m_type(Unknown),
m_hostName(QHostInfo::localHostName())
m_type(Unknown)
{ }
CAudioDeviceInfo::CAudioDeviceInfo(DeviceType type, const QString &name) :
m_type(type),
m_deviceName(name), m_hostName(QHostInfo::localHostName())
m_deviceName(name)
{ }
bool CAudioDeviceInfo::isDefault() const
@@ -37,11 +38,11 @@ namespace BlackMisc
return false;
}
bool CAudioDeviceInfo::matchesNameTypeHostName(const CAudioDeviceInfo &device) const
bool CAudioDeviceInfo::matchesNameTypeMachineName(const CAudioDeviceInfo &device) const
{
return device.getType() == this->getType() &&
stringCompare(device.getName(), this->getName(), Qt::CaseInsensitive) &&
stringCompare(device.getHostName(), this->getHostName(), Qt::CaseInsensitive);
stringCompare(device.getMachineName(), this->getMachineName(), Qt::CaseInsensitive);
}
CAudioDeviceInfo::DeviceType CAudioDeviceInfo::fromQtMode(QAudio::Mode m)
@@ -65,11 +66,72 @@ namespace BlackMisc
return CAudioDeviceInfo(InputDevice, QAudioDeviceInfo::defaultInputDevice().deviceName());
}
CVariant CAudioDeviceInfo::propertyByIndex(const CPropertyIndex &index) const
{
if (index.isMyself()) { return CVariant::fromValue(*this); }
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexDeviceType: return CVariant::fromValue(this->getType());
case IndexDeviceTypeAsString: return CVariant::fromValue(this->getTypeAsString());
case IndexName: return CVariant::fromValue(this->getName());
case IndexIdentifier: return m_identifier.propertyByIndex(index.copyFrontRemoved());
default: break;
}
return CValueObject::propertyByIndex(index);
}
void CAudioDeviceInfo::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant)
{
if (index.isMyself()) { (*this) = variant.to<CAudioDeviceInfo>(); return; }
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexDeviceType: m_type = static_cast<DeviceType>(variant.toInt()); return;
case IndexName: m_deviceName = variant.toQString(); return;
case IndexIdentifier: m_identifier.setPropertyByIndex(index.copyFrontRemoved(), variant); return;
default: break;
}
CValueObject::setPropertyByIndex(index, variant);
}
int CAudioDeviceInfo::comparePropertyByIndex(const CPropertyIndex &index, const CAudioDeviceInfo &compareValue) const
{
if (index.isMyself()) { return m_deviceName.compare(compareValue.m_deviceName, Qt::CaseInsensitive); }
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexDeviceTypeAsString:
case IndexDeviceType: return Compare::compare(m_type, compareValue.m_type);
case IndexName: return m_deviceName.compare(compareValue.m_deviceName, Qt::CaseInsensitive);
case IndexIdentifier: return m_identifier.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getIdentifier());
default: break;
}
BLACK_VERIFY_X(false, Q_FUNC_INFO, qUtf8Printable("No comparison for index " + index.toQString()));
return 0;
}
const QString &CAudioDeviceInfo::deviceTypeToString(CAudioDeviceInfo::DeviceType t)
{
static const QString i("input");
static const QString o("output");
static const QString u("unknown");
switch (t)
{
case InputDevice: return i;
case OutputDevice: return o;
default: break;
}
return u;
}
QString CAudioDeviceInfo::convertToQString(bool i18n) const
{
Q_UNUSED(i18n)
if (m_hostName.isEmpty()) { return m_deviceName; }
return m_deviceName % u" [" % this->getHostName() % u']';
if (!m_identifier.hasName()) { return m_deviceName; }
return m_deviceName % u" [" % this->getMachineName() % u']';
}
} // ns
} // ns

View File

@@ -11,6 +11,7 @@
#ifndef BLACKMISC_AUDIO_AUDIODEVICE_H
#define BLACKMISC_AUDIO_AUDIODEVICE_H
#include "blackmisc/identifier.h"
#include "blackmisc/metaclass.h"
#include "blackmisc/valueobject.h"
#include "blackmisc/blackmiscexport.h"
@@ -31,6 +32,15 @@ namespace BlackMisc
class BLACKMISC_EXPORT CAudioDeviceInfo : public CValueObject<CAudioDeviceInfo>
{
public:
//! Properties by index
enum ColumnIndex
{
IndexName = CPropertyIndex::GlobalIndexCAudioDeviceInfo,
IndexDeviceType,
IndexDeviceTypeAsString,
IndexIdentifier
};
//! Type
enum DeviceType
{
@@ -39,7 +49,6 @@ namespace BlackMisc
Unknown
};
//!
//! Default constructor.
CAudioDeviceInfo();
@@ -49,12 +58,18 @@ namespace BlackMisc
//! Get the device name
const QString &getName() const { return m_deviceName; }
//! Host name
const QString &getHostName() const { return m_hostName; }
//! Machine name
const QString &getMachineName() const { return m_identifier.getMachineName(); }
//! Identifier
const CIdentifier &getIdentifier() const { return m_identifier; }
//! Type
DeviceType getType() const { return m_type; }
//! Type as string
const QString &getTypeAsString() const { return deviceTypeToString(this->getType()); }
//! Input device
bool isInputDevice() const { return this->getType() == InputDevice; }
@@ -67,8 +82,8 @@ namespace BlackMisc
//! Is this a default device?
bool isDefault() const;
//! Mathcing name, type and machine
bool matchesNameTypeHostName(const CAudioDeviceInfo &device) const;
//! Matching name, type and machine
bool matchesNameTypeMachineName(const CAudioDeviceInfo &device) const;
//! Convert the Qt type
static DeviceType fromQtMode(QAudio::Mode m);
@@ -79,19 +94,31 @@ namespace BlackMisc
//! Default input device
static CAudioDeviceInfo getDefaultInputDevice();
//! \copydoc BlackMisc::Mixin::Index::propertyByIndex
CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const;
//! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex
void setPropertyByIndex(const BlackMisc::CPropertyIndex &index, const CVariant &variant);
//! \copydoc BlackMisc::Mixin::String::toQString
QString convertToQString(bool i18n = false) const;
//! \copydoc BlackMisc::Mixin::Index::comparePropertyByIndex
int comparePropertyByIndex(const CPropertyIndex &index, const CAudioDeviceInfo &compareValue) const;
//! Device type as string
static const QString &deviceTypeToString(DeviceType t);
private:
DeviceType m_type = Unknown; //!< Device type, @see CAudioDeviceInfo::DeviceType
QString m_deviceName; //!< Device name
QString m_hostName; //!< We use a DBus based system. Hence an audio device can reside on a differen computers, this here is its name
CIdentifier m_identifier; //!< We use a DBus based system. Hence an audio device can reside on a different computers, this here is its name
BLACK_METACLASS(
CAudioDeviceInfo,
BLACK_METAMEMBER(type),
BLACK_METAMEMBER(deviceName),
BLACK_METAMEMBER(hostName)
BLACK_METAMEMBER(identifier)
);
};
} // namespace

View File

@@ -66,7 +66,7 @@ namespace BlackMisc
CAudioDeviceInfoList devices;
for (const CAudioDeviceInfo &d : *this)
{
if (stringCompare(hostName, d.getHostName(), Qt::CaseInsensitive))
if (stringCompare(hostName, d.getMachineName(), Qt::CaseInsensitive))
{
devices.push_back(d);
}
@@ -78,7 +78,7 @@ namespace BlackMisc
{
for (const CAudioDeviceInfo &d : *this)
{
if (device.matchesNameTypeHostName(d)) { return d; }
if (device.matchesNameTypeMachineName(d)) { return d; }
}
return {};
}
@@ -116,6 +116,11 @@ namespace BlackMisc
}
}
void CAudioDeviceInfoList::unRegisterDevices(const CIdentifier &identifier)
{
this->removeIf(&CAudioDeviceInfo::getIdentifier, identifier);
}
bool CAudioDeviceInfoList::isRegisteredDevice(const CAudioDeviceInfo &device) const
{
return this->findRegisteredDevice(device).isValid();

View File

@@ -12,6 +12,7 @@
#define BLACKMISC_AUDIO_AUDIODEVICELIST_H
#include "blackmisc/audio/audiodeviceinfo.h"
#include "blackmisc/identifier.h"
#include "blackmisc/collection.h"
#include "blackmisc/sequence.h"
#include "blackmisc/variant.h"
@@ -70,6 +71,9 @@ namespace BlackMisc
//! Un-register devices
void unRegisterDevices(const CAudioDeviceInfoList &devices);
//! Un-register devices
void unRegisterDevices(const CIdentifier &identifier);
//! Is that a registered device?
bool isRegisteredDevice(const CAudioDeviceInfo &device) const;

View File

@@ -27,6 +27,7 @@ namespace BlackMisc
qDBusRegisterMetaType<PTTCOM>();
qDBusRegisterMetaType<CNotificationSounds::PlayMode>();
qDBusRegisterMetaType<CNotificationSounds::NotificationFlag>();
qDBusRegisterMetaType<CAudioDeviceInfo::DeviceType>();
qRegisterMetaTypeStreamOperators<CNotificationSounds::PlayMode>();
qRegisterMetaTypeStreamOperators<CNotificationSounds::NotificationFlag>();
}

View File

@@ -7,6 +7,7 @@
*/
#include "blackmisc/identifier.h"
#include "blackmisc/comparefunctions.h"
#include <QCoreApplication>
#include <QHostInfo>
@@ -225,19 +226,41 @@ namespace BlackMisc
switch (i)
{
case IndexName: return CVariant::fromValue(m_name);
case IndexMachineIdBase64: return CVariant::fromValue(m_machineIdBase64);
case IndexMachineName: return CVariant::fromValue(getMachineName());
case IndexMachineId: return CVariant::fromValue(getMachineId());
case IndexProcessId: return CVariant::fromValue(m_processId);
case IndexProcessName: return CVariant::fromValue(m_processName);
case IndexName: return CVariant::fromValue(m_name);
case IndexMachineIdBase64: return CVariant::fromValue(m_machineIdBase64);
case IndexMachineName: return CVariant::fromValue(getMachineName());
case IndexMachineId: return CVariant::fromValue(getMachineId());
case IndexProcessId: return CVariant::fromValue(m_processId);
case IndexProcessName: return CVariant::fromValue(m_processName);
case IndexIsFromLocalMachine: return CVariant::fromValue(isFromLocalMachine());
case IndexIsFromSameProcess: return CVariant::fromValue(hasApplicationProcessId());
case IndexIsFromSameProcess: return CVariant::fromValue(hasApplicationProcessId());
case IndexIsFromSameProcessName: return CVariant::fromValue(hasApplicationProcessName());
default: return CValueObject::propertyByIndex(index);
}
}
int CIdentifier::comparePropertyByIndex(const CPropertyIndex &index, const CIdentifier &compareValue) const
{
if (index.isMyself()) { return Compare::compare(m_processId, compareValue.m_processId); }
const ColumnIndex i = index.frontCasted<ColumnIndex>();
if (ITimestampBased::canHandleIndex(index)) { return ITimestampBased::comparePropertyByIndex(index, compareValue); }
switch (i)
{
case IndexName: return m_name.compare(compareValue.m_name, Qt::CaseInsensitive);
case IndexMachineIdBase64: return m_machineIdBase64.compare(compareValue.m_machineIdBase64, Qt::CaseInsensitive);
case IndexMachineName: return m_machineName.compare(compareValue.m_machineName, Qt::CaseInsensitive);
case IndexMachineId: return m_machineName.compare(compareValue.m_machineName, Qt::CaseInsensitive);
case IndexProcessId: return Compare::compare(m_processId, compareValue.m_processId);
case IndexProcessName: return m_processName.compare(compareValue.m_processName, Qt::CaseInsensitive);
case IndexIsFromLocalMachine: return Compare::compare(this->isFromLocalMachine(), compareValue.isFromLocalMachine());
case IndexIsFromSameProcess: return Compare::compare(this->hasApplicationProcessId(), compareValue.hasApplicationProcessId());
case IndexIsFromSameProcessName: return Compare::compare(this->hasApplicationProcessName(), compareValue.hasApplicationProcessName());
default: return CValueObject::comparePropertyByIndex(index, compareValue);
}
}
void CIdentifier::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant)
{
CValueObject::setPropertyByIndex(index, variant);

View File

@@ -146,6 +146,9 @@ namespace BlackMisc
//! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex
void setPropertyByIndex(const BlackMisc::CPropertyIndex &index, const CVariant &variant);
//! \copydoc BlackMisc::Mixin::Index::comparePropertyByIndex
int comparePropertyByIndex(const CPropertyIndex &index, const CIdentifier &compareValue) const;
private:
//! Constructor.
CIdentifier(const QString &name, const QString &machineId, const QString &machineName,

View File

@@ -146,6 +146,7 @@ namespace BlackMisc
GlobalIndexCAircraftCfgEntries = 8800,
GlobalIndexCDistributor = 8900,
GlobalIndexCVPilotModelRule = 9000,
GlobalIndexCAudioDeviceInfo = 10000,
GlobalIndexCSettingKeyboardHotkey = 11000,
GlobalIndexCKeyboardKey = 11100,
GlobalIndexCJoystickButton = 11200,