Skip to content

Commit

Permalink
fix: crash because NetworkDevice has been deleted in network library
Browse files Browse the repository at this point in the history
https://tower.im/teams/9487/todos/225916/

Change-Id: I390dbcc44b8754d6ebe0d05269a6c84c243c6a8e
  • Loading branch information
listenerri committed Dec 25, 2018
1 parent 2b9aea1 commit 18f4f2d
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 14 deletions.
32 changes: 32 additions & 0 deletions plugins/network/item/applet/wirelesslist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ void WirelessList::APRemoved(const QJsonObject &apInfo)

void WirelessList::setDeviceInfo(const int index)
{
if (m_device.isNull()) {
return;
}

// set device enable state
m_controlPanel->setDeviceEnabled(m_device->enabled());

Expand All @@ -158,6 +162,10 @@ void WirelessList::setDeviceInfo(const int index)

void WirelessList::loadAPList()
{
if (m_device.isNull()) {
return;
}

for (auto item : m_device->apList()) {
AccessPoint ap(item.toObject());
const auto mIndex = m_apList.indexOf(ap);
Expand Down Expand Up @@ -191,6 +199,10 @@ void WirelessList::updateAPList()
{
Q_ASSERT(sender() == m_updateAPTimer);

if (m_device.isNull()) {
return;
}

int avaliableAPCount = 0;

//if (m_networkInter->IsDeviceEnabled(m_device.dbusPath()))
Expand Down Expand Up @@ -270,6 +282,10 @@ void WirelessList::updateAPList()

void WirelessList::onEnableButtonToggle(const bool enable)
{
if (m_device.isNull()) {
return;
}

Q_EMIT requestSetDeviceEnable(m_device->path(), enable);
m_updateAPTimer->start();
}
Expand All @@ -282,6 +298,10 @@ void WirelessList::onDeviceEnableChanged(const bool enable)

void WirelessList::activateAP(const QString &apPath, const QString &ssid)
{
if (m_device.isNull()) {
return;
}

QString uuid;

QList<QJsonObject> connections = m_device->connections();
Expand All @@ -301,6 +321,10 @@ void WirelessList::activateAP(const QString &apPath, const QString &ssid)

void WirelessList::deactiveAP()
{
if (m_device.isNull()) {
return;
}

Q_EMIT requestDeactiveAP(m_device->path());
}

Expand All @@ -320,6 +344,10 @@ void WirelessList::updateIndicatorPos()

void WirelessList::onActiveConnectionChanged()
{
if (m_device.isNull()) {
return;
}

// 在这个方法中需要通过m_device->activeApSsid()的信息设置m_activeAP的值
// m_activeAP的值应该从m_apList中拿到,但在程序第一次启动后,当后端扫描无线网的数据还没有发过来,
// 这时m_device中的ap list为空,导致本类初始化时调用loadAPList()后m_apList也是空的,
Expand All @@ -341,6 +369,10 @@ void WirelessList::onActiveConnectionChanged()

void WirelessList::onActivateApFailed(const QString &apPath, const QString &uuid)
{
if (m_device.isNull()) {
return;
}

if (m_currentClickAP.path() == apPath) {
qDebug() << "wireless connect failed and may require more configuration,"
<< "path:" << m_currentClickAP.path() << "ssid" << m_currentClickAP.ssid()
Expand Down
3 changes: 2 additions & 1 deletion plugins/network/item/applet/wirelesslist.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <QVBoxLayout>
#include <QList>
#include <QTimer>
#include <QPointer>

#include <dpicturesequenceview.h>
#include <WirelessDevice>
Expand Down Expand Up @@ -71,7 +72,7 @@ private slots:


private:
dde::network::WirelessDevice *m_device;
QPointer<dde::network::WirelessDevice> m_device;

AccessPoint m_activeAP;
QList<AccessPoint> m_apList;
Expand Down
8 changes: 8 additions & 0 deletions plugins/network/item/deviceitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ const QString DeviceItem::itemCommand() const

const QString DeviceItem::itemContextMenu()
{
if (m_device.isNull()) {
return QString();
}

QList<QVariant> items;
items.reserve(2);

Expand Down Expand Up @@ -79,6 +83,10 @@ QWidget *DeviceItem::itemTips()

void DeviceItem::invokeMenuItem(const QString &menuId)
{
if (m_device.isNull()) {
return;
}

if (menuId == "settings")
//QProcess::startDetached("dbus-send --print-reply --dest=com.deepin.dde.ControlCenter /com/deepin/dde/ControlCenter com.deepin.dde.ControlCenter.ShowModule \"string:network\"");
DDBusSender()
Expand Down
5 changes: 3 additions & 2 deletions plugins/network/item/deviceitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#define DEVICEITEM_H

#include <QWidget>
#include <QPointer>

#include <NetworkDevice>

Expand All @@ -35,7 +36,7 @@ class DeviceItem : public QWidget

const QString &path() const { return m_path; }

inline const dde::network::NetworkDevice * device() { return m_device; }
inline const QPointer<dde::network::NetworkDevice> device() { return m_device; }

virtual void refreshIcon() = 0;
virtual const QString itemCommand() const;
Expand All @@ -52,7 +53,7 @@ class DeviceItem : public QWidget
QSize sizeHint() const;

protected:
dde::network::NetworkDevice *m_device;
QPointer<dde::network::NetworkDevice> m_device;

private:
QString m_path;
Expand Down
14 changes: 11 additions & 3 deletions plugins/network/item/wireditem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ WiredItem::WiredItem(WiredDevice *device)

connect(m_delayTimer, &QTimer::timeout, this, &WiredItem::reloadIcon);
connect(m_device, static_cast<void (NetworkDevice::*)(NetworkDevice::DeviceStatus) const>(&NetworkDevice::statusChanged), this, &WiredItem::deviceStateChanged);
connect(static_cast<WiredDevice *>(m_device), &WiredDevice::connectionsChanged, this, &WiredItem::deviceStateChanged);
connect(static_cast<WiredDevice *>(m_device), &WiredDevice::activeConnectionChanged, this, &WiredItem::deviceStateChanged);
connect(static_cast<WiredDevice *>(m_device.data()), &WiredDevice::connectionsChanged, this, &WiredItem::deviceStateChanged);
connect(static_cast<WiredDevice *>(m_device.data()), &WiredDevice::activeConnectionChanged, this, &WiredItem::deviceStateChanged);

QTimer::singleShot(0, this, &WiredItem::refreshTips);
QTimer::singleShot(0, this, &WiredItem::refreshIcon);
Expand Down Expand Up @@ -113,6 +113,10 @@ void WiredItem::reloadIcon()
{
Q_ASSERT(sender() == m_delayTimer);

if (m_device.isNull()) {
return;
}

// const Dock::DisplayMode displayMode = qApp->property(PROP_DISPLAY_MODE).value<Dock::DisplayMode>();
const Dock::DisplayMode displayMode = Dock::DisplayMode::Efficient;
const auto ratio = qApp->devicePixelRatio();
Expand Down Expand Up @@ -190,14 +194,18 @@ void WiredItem::deviceStateChanged()

void WiredItem::refreshTips()
{
if (m_device.isNull()) {
return;
}

m_itemTips->setText(m_device->statusStringDetail());

if (NetworkPlugin::isConnectivity()) {
do {
if (m_device->status() != NetworkDevice::DeviceStatus::Activated) {
break;
}
const QJsonObject info = static_cast<WiredDevice *>(m_device)->activeConnection();
const QJsonObject info = static_cast<WiredDevice *>(m_device.data())->activeConnection();
if (!info.contains("Ip4"))
break;
const QJsonObject ipv4 = info.value("Ip4").toObject();
Expand Down
24 changes: 18 additions & 6 deletions plugins/network/item/wirelessitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ WirelessItem::WirelessItem(WirelessDevice *device)

connect(m_refreshTimer, &QTimer::timeout, this, &WirelessItem::onRefreshTimeout);
connect(m_device, static_cast<void (NetworkDevice::*) (const QString &statStr) const>(&NetworkDevice::statusChanged), this, &WirelessItem::deviceStateChanged);
connect(static_cast<WirelessDevice *>(m_device), &WirelessDevice::activeApInfoChanged, m_refreshTimer, static_cast<void (QTimer::*) ()>(&QTimer::start));
connect(static_cast<WirelessDevice *>(m_device), &WirelessDevice::activeConnectionChanged, m_refreshTimer, static_cast<void (QTimer::*) ()>(&QTimer::start));
connect(static_cast<WirelessDevice *>(m_device.data()), &WirelessDevice::activeApInfoChanged, m_refreshTimer, static_cast<void (QTimer::*) ()>(&QTimer::start));
connect(static_cast<WirelessDevice *>(m_device.data()), &WirelessDevice::activeConnectionChanged, m_refreshTimer, static_cast<void (QTimer::*) ()>(&QTimer::start));

//QMetaObject::invokeMethod(this, "init", Qt::QueuedConnection);
QTimer::singleShot(0, this, &WirelessItem::refreshTips);
Expand Down Expand Up @@ -142,6 +142,10 @@ void WirelessItem::mousePressEvent(QMouseEvent *e)

const QPixmap WirelessItem::iconPix(const Dock::DisplayMode displayMode, const int size)
{
if (m_device.isNull()) {
return QPixmap();
}

QString type;
const auto state = m_device->status();

Expand Down Expand Up @@ -169,7 +173,7 @@ const QPixmap WirelessItem::iconPix(const Dock::DisplayMode displayMode, const i
break;
}
case NetworkDevice::DeviceStatus::Activated: {
const auto &activeApInfo = static_cast<WirelessDevice *>(m_device)->activeApInfo();
const auto &activeApInfo = static_cast<WirelessDevice *>(m_device.data())->activeApInfo();
if (activeApInfo.isEmpty()) {
strength = 100;
m_refreshTimer->start();
Expand Down Expand Up @@ -229,7 +233,7 @@ const QPixmap WirelessItem::cachedPix(const QString &key, const int size)

void WirelessItem::init()
{
m_APList = new WirelessList(static_cast<WirelessDevice *>(m_device));
m_APList = new WirelessList(static_cast<WirelessDevice *>(m_device.data()));
m_APList->installEventFilter(this);
m_APList->setObjectName("wireless-" + m_device->path());

Expand Down Expand Up @@ -265,14 +269,18 @@ void WirelessItem::refreshIcon()

void WirelessItem::refreshTips()
{
if (m_device.isNull()) {
return;
}

m_wirelessTips->setText(m_device->statusStringDetail());

if (NetworkPlugin::isConnectivity()) {
do {
if (m_device->status() != NetworkDevice::DeviceStatus::Activated) {
break;
}
const QJsonObject info = static_cast<WirelessDevice *>(m_device)->activeConnectionInfo();
const QJsonObject info = static_cast<WirelessDevice *>(m_device.data())->activeConnectionInfo();
if (!info.contains("Ip4"))
break;
const QJsonObject ipv4 = info.value("Ip4").toObject();
Expand All @@ -294,7 +302,11 @@ void WirelessItem::onRefreshTimeout()
{
Q_ASSERT(sender() == m_refreshTimer);

WirelessDevice *dev = static_cast<WirelessDevice *>(m_device);
if (m_device.isNull()) {
return;
}

WirelessDevice *dev = static_cast<WirelessDevice *>(m_device.data());
// the status is Activated and activeApInfo is empty if the hotspot status of this wireless device is enabled
if (m_device->status() == NetworkDevice::Activated && dev->activeApInfo().isEmpty() && !dev->hotspotEnabled()) {
Q_EMIT queryActiveConnInfo();
Expand Down
4 changes: 2 additions & 2 deletions plugins/network/networkplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ void NetworkPlugin::refreshWiredItemVisible()
QList<WiredItem *> wiredItems;

for (auto item : m_itemsMap.values()) {
if (item->device()->type() == NetworkDevice::Wireless) {
if (!item->device().isNull() && item->device()->type() == NetworkDevice::Wireless) {
hasWireless = true;
} else {
wiredItems.append(static_cast<WiredItem *>(item));
Expand All @@ -280,7 +280,7 @@ void NetworkPlugin::refreshWiredItemVisible()
}

for (auto wiredItem : wiredItems) {
if (!wiredItem->device()->enabled()) {
if (!wiredItem->device().isNull() && !wiredItem->device()->enabled()) {
m_proxyInter->itemRemoved(this, wiredItem->path());
} else {
m_proxyInter->itemAdded(this, wiredItem->path());
Expand Down

0 comments on commit 18f4f2d

Please sign in to comment.