File indexing completed on 2024-05-12 03:53:11

0001 /*
0002  * BluezQt - Asynchronous BlueZ wrapper library
0003  *
0004  * SPDX-FileCopyrightText: 2015 David Rosca <nowrep@gmail.com>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007  */
0008 
0009 #include "declarativedevice.h"
0010 #include "declarativeadapter.h"
0011 #include "declarativebattery.h"
0012 #include "declarativeinput.h"
0013 #include "declarativemediaplayer.h"
0014 
0015 #include <QStringList>
0016 
0017 DeclarativeDevice::DeclarativeDevice(BluezQt::DevicePtr device, DeclarativeAdapter *adapter)
0018     : QObject(adapter)
0019     , m_device(device)
0020     , m_adapter(adapter)
0021     , m_battery(nullptr)
0022     , m_input(nullptr)
0023     , m_mediaPlayer(nullptr)
0024 {
0025     connect(m_device.data(), &BluezQt::Device::nameChanged, this, &DeclarativeDevice::nameChanged);
0026     connect(m_device.data(), &BluezQt::Device::friendlyNameChanged, this, &DeclarativeDevice::friendlyNameChanged);
0027     connect(m_device.data(), &BluezQt::Device::remoteNameChanged, this, &DeclarativeDevice::remoteNameChanged);
0028     connect(m_device.data(), &BluezQt::Device::deviceClassChanged, this, &DeclarativeDevice::deviceClassChanged);
0029     connect(m_device.data(), &BluezQt::Device::typeChanged, this, &DeclarativeDevice::typeChanged);
0030     connect(m_device.data(), &BluezQt::Device::appearanceChanged, this, &DeclarativeDevice::appearanceChanged);
0031     connect(m_device.data(), &BluezQt::Device::iconChanged, this, &DeclarativeDevice::iconChanged);
0032     connect(m_device.data(), &BluezQt::Device::pairedChanged, this, &DeclarativeDevice::pairedChanged);
0033     connect(m_device.data(), &BluezQt::Device::trustedChanged, this, &DeclarativeDevice::trustedChanged);
0034     connect(m_device.data(), &BluezQt::Device::blockedChanged, this, &DeclarativeDevice::blockedChanged);
0035     connect(m_device.data(), &BluezQt::Device::legacyPairingChanged, this, &DeclarativeDevice::legacyPairingChanged);
0036     connect(m_device.data(), &BluezQt::Device::rssiChanged, this, &DeclarativeDevice::rssiChanged);
0037     connect(m_device.data(), &BluezQt::Device::connectedChanged, this, &DeclarativeDevice::connectedChanged);
0038     connect(m_device.data(), &BluezQt::Device::uuidsChanged, this, &DeclarativeDevice::uuidsChanged);
0039     connect(m_device.data(), &BluezQt::Device::modaliasChanged, this, &DeclarativeDevice::modaliasChanged);
0040     connect(m_device.data(), &BluezQt::Device::mediaPlayerChanged, this, &DeclarativeDevice::updateMediaPlayer);
0041     connect(m_device.data(), &BluezQt::Device::inputChanged, this, &DeclarativeDevice::updateInput);
0042     connect(m_device.data(), &BluezQt::Device::batteryChanged, this, &DeclarativeDevice::updateBattery);
0043 
0044     connect(m_device.data(), &BluezQt::Device::deviceRemoved, this, [this]() {
0045         Q_EMIT deviceRemoved(this);
0046     });
0047 
0048     connect(m_device.data(), &BluezQt::Device::deviceChanged, this, [this]() {
0049         Q_EMIT deviceChanged(this);
0050     });
0051 
0052     updateInput();
0053     updateMediaPlayer();
0054 }
0055 
0056 QString DeclarativeDevice::ubi() const
0057 {
0058     return m_device->ubi();
0059 }
0060 
0061 QString DeclarativeDevice::address() const
0062 {
0063     return m_device->address();
0064 }
0065 
0066 QString DeclarativeDevice::name() const
0067 {
0068     return m_device->name();
0069 }
0070 
0071 void DeclarativeDevice::setName(const QString &name)
0072 {
0073     m_device->setName(name);
0074 }
0075 
0076 QString DeclarativeDevice::friendlyName() const
0077 {
0078     return m_device->friendlyName();
0079 }
0080 
0081 QString DeclarativeDevice::remoteName() const
0082 {
0083     return m_device->remoteName();
0084 }
0085 
0086 quint32 DeclarativeDevice::deviceClass() const
0087 {
0088     return m_device->deviceClass();
0089 }
0090 
0091 BluezQt::Device::Type DeclarativeDevice::type() const
0092 {
0093     return m_device->type();
0094 }
0095 
0096 quint16 DeclarativeDevice::appearance() const
0097 {
0098     return m_device->appearance();
0099 }
0100 
0101 QString DeclarativeDevice::icon() const
0102 {
0103     return m_device->icon();
0104 }
0105 
0106 bool DeclarativeDevice::isPaired() const
0107 {
0108     return m_device->isPaired();
0109 }
0110 
0111 bool DeclarativeDevice::isTrusted() const
0112 {
0113     return m_device->isTrusted();
0114 }
0115 
0116 void DeclarativeDevice::setTrusted(bool trusted)
0117 {
0118     m_device->setTrusted(trusted);
0119 }
0120 
0121 bool DeclarativeDevice::isBlocked() const
0122 {
0123     return m_device->isBlocked();
0124 }
0125 
0126 void DeclarativeDevice::setBlocked(bool blocked)
0127 {
0128     m_device->setBlocked(blocked);
0129 }
0130 
0131 bool DeclarativeDevice::hasLegacyPairing() const
0132 {
0133     return m_device->hasLegacyPairing();
0134 }
0135 
0136 qint16 DeclarativeDevice::rssi() const
0137 {
0138     return m_device->rssi();
0139 }
0140 
0141 bool DeclarativeDevice::isConnected() const
0142 {
0143     return m_device->isConnected();
0144 }
0145 
0146 QStringList DeclarativeDevice::uuids() const
0147 {
0148     return m_device->uuids();
0149 }
0150 
0151 QString DeclarativeDevice::modalias() const
0152 {
0153     return m_device->modalias();
0154 }
0155 
0156 DeclarativeBattery *DeclarativeDevice::battery() const
0157 {
0158     return m_battery;
0159 }
0160 
0161 DeclarativeInput *DeclarativeDevice::input() const
0162 {
0163     return m_input;
0164 }
0165 
0166 DeclarativeMediaPlayer *DeclarativeDevice::mediaPlayer() const
0167 {
0168     return m_mediaPlayer;
0169 }
0170 
0171 DeclarativeAdapter *DeclarativeDevice::adapter() const
0172 {
0173     return m_adapter;
0174 }
0175 
0176 BluezQt::PendingCall *DeclarativeDevice::connectToDevice()
0177 {
0178     return m_device->connectToDevice();
0179 }
0180 
0181 BluezQt::PendingCall *DeclarativeDevice::disconnectFromDevice()
0182 {
0183     return m_device->disconnectFromDevice();
0184 }
0185 
0186 BluezQt::PendingCall *DeclarativeDevice::connectProfile(const QString &uuid)
0187 {
0188     return m_device->connectProfile(uuid);
0189 }
0190 
0191 BluezQt::PendingCall *DeclarativeDevice::disconnectProfile(const QString &uuid)
0192 {
0193     return m_device->disconnectProfile(uuid);
0194 }
0195 
0196 BluezQt::PendingCall *DeclarativeDevice::pair()
0197 {
0198     return m_device->pair();
0199 }
0200 
0201 BluezQt::PendingCall *DeclarativeDevice::cancelPairing()
0202 {
0203     return m_device->cancelPairing();
0204 }
0205 
0206 void DeclarativeDevice::updateBattery()
0207 {
0208     if (m_battery) {
0209         m_battery->deleteLater();
0210         m_battery = nullptr;
0211     }
0212 
0213     if (m_device->battery()) {
0214         m_battery = new DeclarativeBattery(m_device->battery(), this);
0215     }
0216 
0217     Q_EMIT batteryChanged(m_battery);
0218 }
0219 
0220 void DeclarativeDevice::updateInput()
0221 {
0222     if (m_input) {
0223         m_input->deleteLater();
0224         m_input = nullptr;
0225     }
0226 
0227     if (m_device->input()) {
0228         m_input = new DeclarativeInput(m_device->input(), this);
0229     }
0230 
0231     Q_EMIT inputChanged(m_input);
0232 }
0233 
0234 void DeclarativeDevice::updateMediaPlayer()
0235 {
0236     if (m_mediaPlayer) {
0237         m_mediaPlayer->deleteLater();
0238         m_mediaPlayer = nullptr;
0239     }
0240 
0241     if (m_device->mediaPlayer()) {
0242         m_mediaPlayer = new DeclarativeMediaPlayer(m_device->mediaPlayer(), this);
0243     }
0244 
0245     Q_EMIT mediaPlayerChanged(m_mediaPlayer);
0246 }
0247 
0248 #include "moc_declarativedevice.cpp"