2020-05-14 13:09:26 +08:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd.
|
|
|
|
|
*
|
|
|
|
|
* Author: zhaolong <zhaolong@uniontech.com>
|
|
|
|
|
*
|
|
|
|
|
* Maintainer: zhaolong <zhaolong@uniontech.com>
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "adapter.h"
|
2020-04-01 16:18:12 +08:00
|
|
|
|
#include "device.h"
|
2020-05-14 13:09:26 +08:00
|
|
|
|
|
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
#include <QJsonArray>
|
|
|
|
|
#include <QJsonValue>
|
|
|
|
|
|
|
|
|
|
Adapter::Adapter(QObject *parent)
|
|
|
|
|
: QObject(parent)
|
|
|
|
|
, m_id("")
|
|
|
|
|
, m_name("")
|
|
|
|
|
, m_powered(false)
|
|
|
|
|
, m_current(false)
|
2020-06-10 15:49:55 +08:00
|
|
|
|
, m_discover(false)
|
2020-05-14 13:09:26 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Adapter::setName(const QString &name)
|
|
|
|
|
{
|
|
|
|
|
if (name != m_name) {
|
|
|
|
|
m_name = name;
|
|
|
|
|
Q_EMIT nameChanged(name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Adapter::addDevice(const QJsonObject &deviceObj)
|
|
|
|
|
{
|
|
|
|
|
const QString id = deviceObj["Path"].toString();
|
2021-03-01 10:35:26 +08:00
|
|
|
|
const QString name = deviceObj["Name"].toString();
|
|
|
|
|
const QString alias = deviceObj["Alias"].toString();
|
2020-05-14 13:09:26 +08:00
|
|
|
|
const bool paired = deviceObj["Paired"].toBool();
|
2020-04-24 17:14:46 +08:00
|
|
|
|
const int rssi = deviceObj["RSSI"].toInt();
|
2020-05-14 13:09:26 +08:00
|
|
|
|
const Device::State state = Device::State(deviceObj["State"].toInt());
|
2020-08-20 19:06:32 +08:00
|
|
|
|
const bool connectState = deviceObj["ConnectState"].toBool();
|
2020-06-03 11:59:17 +08:00
|
|
|
|
const QString bluetoothDeviceType = deviceObj["Icon"].toString();
|
2020-05-14 13:09:26 +08:00
|
|
|
|
|
|
|
|
|
removeDevice(id);
|
|
|
|
|
|
|
|
|
|
auto device = new Device(this);
|
|
|
|
|
|
|
|
|
|
device->setId(id);
|
|
|
|
|
device->setName(name);
|
2021-03-01 10:35:26 +08:00
|
|
|
|
device->setAlias(alias);
|
2020-05-14 13:09:26 +08:00
|
|
|
|
device->setPaired(paired);
|
|
|
|
|
device->setState(state);
|
2020-08-20 19:06:32 +08:00
|
|
|
|
device->setConnectState(connectState);
|
2020-04-24 17:14:46 +08:00
|
|
|
|
device->setRssi(rssi);
|
2020-05-14 13:09:26 +08:00
|
|
|
|
device->setAdapterId(m_id);
|
2020-06-03 11:59:17 +08:00
|
|
|
|
device->setDeviceType(bluetoothDeviceType);
|
2020-05-14 13:09:26 +08:00
|
|
|
|
|
|
|
|
|
m_devices[id] = device;
|
|
|
|
|
|
|
|
|
|
emit deviceAdded(device);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Adapter::removeDevice(const QString &deviceId)
|
|
|
|
|
{
|
2020-05-14 13:13:25 +08:00
|
|
|
|
const Device *constDevice = m_devices.value(deviceId);
|
2020-05-14 13:09:26 +08:00
|
|
|
|
auto device = const_cast<Device *>(constDevice);
|
|
|
|
|
if (device) {
|
|
|
|
|
m_devices.remove(deviceId);
|
|
|
|
|
emit deviceRemoved(device);
|
|
|
|
|
delete device;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-14 13:13:25 +08:00
|
|
|
|
void Adapter::updateDevice(const QJsonObject &dviceJson)
|
2020-05-14 13:09:26 +08:00
|
|
|
|
{
|
2020-05-14 13:13:25 +08:00
|
|
|
|
const QString id = dviceJson["Path"].toString();
|
2021-03-01 10:35:26 +08:00
|
|
|
|
const QString name = dviceJson["Name"].toString();
|
|
|
|
|
const QString alias = dviceJson["Alias"].toString();
|
2020-05-14 13:13:25 +08:00
|
|
|
|
const bool paired = dviceJson["Paired"].toBool();
|
|
|
|
|
const int rssi = dviceJson["RSSI"].toInt();
|
|
|
|
|
const Device::State state = Device::State(dviceJson["State"].toInt());
|
2020-08-20 19:06:32 +08:00
|
|
|
|
const bool connectState = dviceJson["ConnectState"].toBool();
|
2020-06-03 11:59:17 +08:00
|
|
|
|
const QString bluetoothDeviceType = dviceJson["Icon"].toString();
|
2020-05-14 13:09:26 +08:00
|
|
|
|
|
2021-03-04 09:58:05 +08:00
|
|
|
|
// FIXME: Solve the problem that the device name in the Bluetooth list is blank
|
|
|
|
|
if (name.isEmpty() && alias.isEmpty())
|
|
|
|
|
return ;
|
|
|
|
|
|
2020-05-14 13:13:25 +08:00
|
|
|
|
const Device *constdevice = m_devices.value(id);
|
2020-05-14 13:09:26 +08:00
|
|
|
|
auto device = const_cast<Device *>(constdevice);
|
|
|
|
|
if (device) {
|
|
|
|
|
device->setId(id);
|
|
|
|
|
device->setName(name);
|
2021-03-01 10:35:26 +08:00
|
|
|
|
device->setAlias(alias);
|
2020-05-14 13:09:26 +08:00
|
|
|
|
device->setPaired(paired);
|
2020-04-24 17:14:46 +08:00
|
|
|
|
device->setRssi(rssi);
|
2020-12-01 17:20:30 +08:00
|
|
|
|
//setState放后面,是因为用到了connectState,fix bug 55245
|
2020-08-20 19:06:32 +08:00
|
|
|
|
device->setConnectState(connectState);
|
2020-12-01 17:20:30 +08:00
|
|
|
|
device->setState(state);
|
2020-06-03 11:59:17 +08:00
|
|
|
|
device->setDeviceType(bluetoothDeviceType);
|
2021-03-04 09:58:05 +08:00
|
|
|
|
emit deviceNameUpdated(device);
|
2020-05-14 13:09:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Adapter::setPowered(bool powered)
|
|
|
|
|
{
|
|
|
|
|
if (powered != m_powered) {
|
|
|
|
|
m_powered = powered;
|
|
|
|
|
Q_EMIT poweredChanged(powered);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Adapter::initDevicesList(const QJsonDocument &doc)
|
|
|
|
|
{
|
2020-05-14 13:13:25 +08:00
|
|
|
|
QJsonArray arr = doc.array();
|
2020-05-14 13:09:26 +08:00
|
|
|
|
for (QJsonValue val : arr) {
|
2020-05-14 13:13:25 +08:00
|
|
|
|
QJsonObject deviceObj = val.toObject();
|
2020-05-14 13:09:26 +08:00
|
|
|
|
const QString adapterId = deviceObj["AdapterPath"].toString();
|
|
|
|
|
const QString id = deviceObj["Path"].toString();
|
2021-03-01 10:35:26 +08:00
|
|
|
|
const QString name = deviceObj["Name"].toString();
|
|
|
|
|
const QString alias = deviceObj["Alias"].toString();
|
2020-05-14 13:09:26 +08:00
|
|
|
|
const bool paired = deviceObj["Paired"].toBool();
|
2020-04-24 17:14:46 +08:00
|
|
|
|
const int rssi = deviceObj["RSSI"].toInt();
|
2020-05-14 13:09:26 +08:00
|
|
|
|
const Device::State state = Device::State(deviceObj["State"].toInt());
|
2020-08-20 19:06:32 +08:00
|
|
|
|
const bool connectState = deviceObj["ConnectState"].toBool();
|
2020-06-03 11:59:17 +08:00
|
|
|
|
const QString bluetoothDeviceType = deviceObj["Icon"].toString();
|
2020-05-14 13:09:26 +08:00
|
|
|
|
|
|
|
|
|
auto device = new Device(this);
|
|
|
|
|
device->setId(id);
|
|
|
|
|
device->setName(name);
|
2021-03-01 10:35:26 +08:00
|
|
|
|
device->setAlias(alias);
|
2020-05-14 13:09:26 +08:00
|
|
|
|
device->setPaired(paired);
|
|
|
|
|
device->setState(state);
|
2020-08-20 19:06:32 +08:00
|
|
|
|
device->setConnectState(connectState);
|
2020-04-24 17:14:46 +08:00
|
|
|
|
device->setRssi(rssi);
|
2020-05-14 13:09:26 +08:00
|
|
|
|
device->setAdapterId(adapterId);
|
2020-06-03 11:59:17 +08:00
|
|
|
|
device->setDeviceType(bluetoothDeviceType);
|
2020-05-14 13:09:26 +08:00
|
|
|
|
|
|
|
|
|
m_devices[id] = device;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QMap<QString, const Device *> Adapter::devices() const
|
|
|
|
|
{
|
|
|
|
|
return m_devices;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Device *Adapter::deviceById(const QString &id) const
|
|
|
|
|
{
|
|
|
|
|
return m_devices.keys().contains(id) ? m_devices[id] : nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Adapter::setId(const QString &id)
|
|
|
|
|
{
|
|
|
|
|
m_id = id;
|
|
|
|
|
}
|
2020-06-10 15:49:55 +08:00
|
|
|
|
|
|
|
|
|
void Adapter::setDiscover(bool discover)
|
|
|
|
|
{
|
|
|
|
|
if (discover != m_discover) {
|
|
|
|
|
m_discover = discover;
|
|
|
|
|
Q_EMIT discoveringChanged(discover);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|