mirror of
https://github.com/linuxdeepin/dde-dock.git
synced 2025-06-04 17:33:05 +00:00

添加蓝牙插件 bug:9128 (cherry picked from commit 7412d26cda31a617318842cb919242558e135a69) (cherry picked from commit 1f62819e0fc3a97723c386b6cdf2fc68d1318827)
176 lines
4.4 KiB
C++
176 lines
4.4 KiB
C++
/*
|
|
* 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"
|
|
|
|
#include <QJsonObject>
|
|
#include <QJsonDocument>
|
|
#include <QJsonArray>
|
|
#include <QJsonValue>
|
|
|
|
Adapter::Adapter(QObject *parent)
|
|
: QObject(parent)
|
|
, m_id("")
|
|
, m_name("")
|
|
, m_powered(false)
|
|
, m_current(false)
|
|
{
|
|
}
|
|
|
|
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();
|
|
const QString name = deviceObj["Alias"].toString();
|
|
const bool paired = deviceObj["Paired"].toBool();
|
|
const Device::State state = Device::State(deviceObj["State"].toInt());
|
|
|
|
removeDevice(id);
|
|
|
|
auto device = new Device(this);
|
|
|
|
device->setId(id);
|
|
device->setName(name);
|
|
device->setPaired(paired);
|
|
device->setState(state);
|
|
device->setAdapterId(m_id);
|
|
|
|
m_devices[id] = device;
|
|
divideDevice(device);
|
|
|
|
emit deviceAdded(device);
|
|
}
|
|
|
|
void Adapter::removeDevice(const QString &deviceId)
|
|
{
|
|
auto constDevice = m_devices.value(deviceId);
|
|
auto device = const_cast<Device *>(constDevice);
|
|
if (device) {
|
|
m_devices.remove(deviceId);
|
|
m_paredDev.remove(deviceId);
|
|
emit deviceRemoved(device);
|
|
delete device;
|
|
}
|
|
}
|
|
|
|
void Adapter::updateDevice(const QJsonObject &json)
|
|
{
|
|
const QString id = json["Path"].toString();
|
|
const QString name = json["Alias"].toString();
|
|
const bool paired = json["Paired"].toBool();
|
|
const Device::State state = Device::State(json["State"].toInt());
|
|
|
|
auto constdevice = m_devices.value(id);
|
|
auto device = const_cast<Device *>(constdevice);
|
|
|
|
if (device) {
|
|
device->setId(id);
|
|
device->setName(name);
|
|
device->setPaired(paired);
|
|
device->setState(state);
|
|
}
|
|
}
|
|
|
|
void Adapter::removeAllDevices()
|
|
{
|
|
QMapIterator<QString, const Device *> iterator(m_devices);
|
|
while (iterator.hasNext()) {
|
|
iterator.next();
|
|
auto device = const_cast<Device *>(iterator.value());
|
|
if (device) {
|
|
m_devices.remove(device->id());
|
|
m_paredDev.remove(device->id());
|
|
delete device;
|
|
}
|
|
}
|
|
}
|
|
|
|
const QMap<QString, const Device *> &Adapter::paredDevices() const
|
|
{
|
|
return m_paredDev;
|
|
}
|
|
|
|
int Adapter::paredDevicesCount() const
|
|
{
|
|
return m_paredDev.size();
|
|
}
|
|
|
|
void Adapter::divideDevice(const Device *device)
|
|
{
|
|
if (device->paired()) {
|
|
m_paredDev[device->id()] = device;
|
|
}
|
|
}
|
|
|
|
void Adapter::setPowered(bool powered)
|
|
{
|
|
if (powered != m_powered) {
|
|
m_powered = powered;
|
|
Q_EMIT poweredChanged(powered);
|
|
}
|
|
}
|
|
|
|
void Adapter::initDevicesList(const QJsonDocument &doc)
|
|
{
|
|
auto arr = doc.array();
|
|
for (QJsonValue val : arr) {
|
|
auto deviceObj = val.toObject();
|
|
const QString adapterId = deviceObj["AdapterPath"].toString();
|
|
const QString id = deviceObj["Path"].toString();
|
|
const QString name = deviceObj["Alias"].toString();
|
|
const bool paired = deviceObj["Paired"].toBool();
|
|
const Device::State state = Device::State(deviceObj["State"].toInt());
|
|
|
|
auto device = new Device(this);
|
|
device->setId(id);
|
|
device->setName(name);
|
|
device->setPaired(paired);
|
|
device->setState(state);
|
|
device->setAdapterId(adapterId);
|
|
|
|
m_devices[id] = device;
|
|
divideDevice(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;
|
|
}
|