monitor device change

Change-Id: Ib6064595c564c8b286aeffabb88c40a1178428b2
This commit is contained in:
石博文 2016-07-27 16:24:23 +08:00 committed by Hualet Wang
parent 9a605bbe24
commit 91eee633dd
9 changed files with 77 additions and 44 deletions

View File

@ -7,9 +7,10 @@
#define MAX_HEIGHT 200
#define ITEM_HEIGHT 30
WirelessApplet::WirelessApplet(const QString &devicePath, QWidget *parent)
WirelessApplet::WirelessApplet(const QSet<NetworkDevice>::const_iterator &deviceIter, QWidget *parent)
: QScrollArea(parent),
m_devicePath(devicePath),
m_device(*deviceIter),
m_updateAPTimer(new QTimer(this)),
@ -42,6 +43,7 @@ WirelessApplet::WirelessApplet(const QString &devicePath, QWidget *parent)
connect(m_networkInter, &DBusNetwork::AccessPointAdded, this, &WirelessApplet::APAdded);
connect(m_networkInter, &DBusNetwork::AccessPointRemoved, this, &WirelessApplet::APRemoved);
connect(m_networkInter, &DBusNetwork::AccessPointPropertiesChanged, this, &WirelessApplet::APPropertiesChanged);
connect(m_networkInter, &DBusNetwork::DevicesChanged, this, &WirelessApplet::deviceStateChanegd);
connect(m_controlPanel, &DeviceControlWidget::deviceEnableChanged, this, &WirelessApplet::deviceEnableChanged);
@ -56,7 +58,7 @@ void WirelessApplet::init()
void WirelessApplet::APAdded(const QString &devPath, const QString &info)
{
if (devPath != m_devicePath)
if (devPath != m_device.path())
return;
AccessPoint ap(info);
@ -69,7 +71,7 @@ void WirelessApplet::APAdded(const QString &devPath, const QString &info)
void WirelessApplet::APRemoved(const QString &devPath, const QString &info)
{
if (devPath != m_devicePath)
if (devPath != m_device.path())
return;
m_apList.removeOne(AccessPoint(info));
@ -79,35 +81,13 @@ void WirelessApplet::APRemoved(const QString &devPath, const QString &info)
void WirelessApplet::setDeviceInfo()
{
// set device enable state
m_controlPanel->setDeviceEnabled(m_networkInter->IsDeviceEnabled(QDBusObjectPath(m_devicePath)));
// set device name
const QJsonDocument doc = QJsonDocument::fromJson(m_networkInter->devices().toUtf8());
Q_ASSERT(doc.isObject());
const QJsonObject obj = doc.object();
for (auto infoList(obj.constBegin()); infoList != obj.constEnd(); ++infoList)
{
Q_ASSERT(infoList.value().isArray());
if (infoList.key() != "wireless")
continue;
for (auto wireless : infoList.value().toArray())
{
const QJsonObject info = wireless.toObject();
if (info.value("Path") == m_devicePath)
{
m_controlPanel->setDeviceName(info.value("Vendor").toString());
break;
}
}
}
m_controlPanel->setDeviceEnabled(m_networkInter->IsDeviceEnabled(m_device.dbusPath()));
m_controlPanel->setDeviceName(m_device.vendor());
}
void WirelessApplet::loadAPList()
{
const QString data = m_networkInter->GetAccessPoints(QDBusObjectPath(m_devicePath));
const QString data = m_networkInter->GetAccessPoints(m_device.dbusPath());
const QJsonDocument doc = QJsonDocument::fromJson(data.toUtf8());
Q_ASSERT(doc.isArray());
@ -125,7 +105,7 @@ void WirelessApplet::loadAPList()
void WirelessApplet::APPropertiesChanged(const QString &devPath, const QString &info)
{
if (devPath != m_devicePath)
if (devPath != m_device.path())
return;
QJsonDocument doc = QJsonDocument::fromJson(info.toUtf8());
@ -158,7 +138,7 @@ void WirelessApplet::updateAPList()
int avaliableAPCount = 0;
if (m_networkInter->IsDeviceEnabled(QDBusObjectPath(m_devicePath)))
if (m_networkInter->IsDeviceEnabled(m_device.dbusPath()))
{
// sort ap list by strength
std::sort(m_apList.begin(), m_apList.end(), std::greater<AccessPoint>());
@ -179,6 +159,39 @@ void WirelessApplet::updateAPList()
void WirelessApplet::deviceEnableChanged(const bool enable)
{
m_networkInter->EnableDevice(QDBusObjectPath(m_devicePath), enable);
m_networkInter->EnableDevice(m_device.dbusPath(), enable);
m_updateAPTimer->start();
}
void WirelessApplet::deviceStateChanegd()
{
const QJsonDocument doc = QJsonDocument::fromJson(m_networkInter->devices().toUtf8());
Q_ASSERT(doc.isObject());
const QJsonObject obj = doc.object();
for (auto infoList(obj.constBegin()); infoList != obj.constEnd(); ++infoList)
{
Q_ASSERT(infoList.value().isArray());
if (infoList.key() != "wireless")
continue;
for (auto wireless : infoList.value().toArray())
{
const QJsonObject info = wireless.toObject();
if (info.value("Path") == m_device.path())
{
NetworkDevice dev = NetworkDevice(NetworkDevice::Wireless, info);
if (dev.state() != m_device.state())
emit wirelessStateChanged(dev.state());
if (dev.activeAp() != m_device.activeAp())
emit activeAPChanged();
m_device = dev;
break;
}
}
}
}

View File

@ -3,6 +3,7 @@
#include "devicecontrolwidget.h"
#include "accesspoint.h"
#include "../../networkdevice.h"
#include "../../dbus/dbusnetwork.h"
#include <QScrollArea>
@ -15,7 +16,11 @@ class WirelessApplet : public QScrollArea
Q_OBJECT
public:
explicit WirelessApplet(const QString &devicePath, QWidget *parent = 0);
explicit WirelessApplet(const QSet<NetworkDevice>::const_iterator &deviceIter, QWidget *parent = 0);
signals:
void wirelessStateChanged(const NetworkDevice::NetworkState state) const;
void activeAPChanged() const;
private:
void setDeviceInfo();
@ -28,9 +33,10 @@ private slots:
void APPropertiesChanged(const QString &devPath, const QString &info);
void updateAPList();
void deviceEnableChanged(const bool enable);
void deviceStateChanegd();
private:
const QString m_devicePath;
NetworkDevice m_device;
QList<AccessPoint> m_apList;

View File

@ -30,7 +30,7 @@ QWidget *WiredItem::itemApplet()
break;
}
const QJsonObject info = m_networkManager->deviceInfo(m_deviceUuid);
const QJsonObject info = m_networkManager->deviceConnInfo(m_deviceUuid);
if (!info.contains("Ip4"))
break;
const QJsonObject ipv4 = info.value("Ip4").toObject();

View File

@ -65,8 +65,8 @@ const QPixmap WirelessItem::cachedPix(const QString &key, const int size)
void WirelessItem::init()
{
const QString devPath = m_networkManager->devicePath(m_deviceUuid);
const auto devInfo = m_networkManager->device(m_deviceUuid);
m_applet = new WirelessApplet(devPath, this);
m_applet = new WirelessApplet(devInfo, this);
m_applet->setVisible(false);
}

View File

@ -41,11 +41,26 @@ const QString NetworkDevice::path() const
return m_objectPath;
}
const QDBusObjectPath NetworkDevice::dbusPath() const
{
return QDBusObjectPath(m_objectPath);
}
const QString NetworkDevice::hwAddress() const
{
return std::move(m_infoObj.value("HwAddress").toString());
}
const QString NetworkDevice::vendor() const
{
return std::move(m_infoObj.value("Vendor").toString());
}
const QString NetworkDevice::activeAp() const
{
return std::move(m_infoObj.value("ActiveAp").toString());
}
NetworkDevice::NetworkType NetworkDevice::deviceType(const QString &type)
{
if (type == "bt")

View File

@ -46,7 +46,10 @@ public:
NetworkType type() const;
const QUuid uuid() const;
const QString path() const;
const QDBusObjectPath dbusPath() const;
const QString hwAddress() const;
const QString vendor() const;
const QString activeAp() const;
private:
NetworkType m_type;

View File

@ -64,7 +64,7 @@ const QString NetworkManager::devicePath(const QUuid &uuid) const
return item->path();
}
const QJsonObject NetworkManager::deviceInfo(const QUuid &uuid) const
const QJsonObject NetworkManager::deviceConnInfo(const QUuid &uuid) const
{
const QString addr = deviceHwAddr(uuid);
if (addr.isEmpty())

View File

@ -25,7 +25,8 @@ public:
NetworkDevice::NetworkState deviceState(const QUuid &uuid) const;
const QString deviceHwAddr(const QUuid &uuid) const;
const QString devicePath(const QUuid &uuid) const;
const QJsonObject deviceInfo(const QUuid &uuid) const;
const QJsonObject deviceConnInfo(const QUuid &uuid) const;
const QSet<NetworkDevice>::const_iterator device(const QUuid &uuid) const;
signals:
void deviceAdded(const NetworkDevice &device) const;
@ -37,7 +38,6 @@ signals:
private:
explicit NetworkManager(QObject *parent = 0);
const QSet<NetworkDevice>::const_iterator device(const QUuid &uuid) const;
private slots:
void reloadDevices();

View File

@ -48,8 +48,6 @@ QWidget *NetworkPlugin::itemPopupApplet(const QString &itemKey)
void NetworkPlugin::deviceAdded(const NetworkDevice &device)
{
qDebug() << "add: " << device.uuid();
DeviceItem *item = nullptr;
switch (device.type())
{
@ -67,8 +65,6 @@ void NetworkPlugin::deviceAdded(const NetworkDevice &device)
void NetworkPlugin::deviceRemoved(const NetworkDevice &device)
{
qDebug() << "remove: " << device.uuid();
const auto item = std::find_if(m_deviceItemList.begin(), m_deviceItemList.end(),
[&] (DeviceItem *dev) {return device == dev->uuid();});