From 6f523698e5109fcf932e0fcd2618862002268faf Mon Sep 17 00:00:00 2001 From: zhangjiaping Date: Wed, 25 Aug 2021 09:03:21 +0800 Subject: [PATCH] Feature: Add activated wlan item. --- .../dbus-interface/kywirelessnetitem.h | 1 + .../dbus-interface/kywirelessnetresource.cpp | 2 +- .../dbus-interface/kywirelessnetresource.h | 2 +- src/frontend/list-items/wlanlistitem.cpp | 2 +- src/frontend/tab-pages/wlanpage.cpp | 96 +++++++++++++++---- src/frontend/tab-pages/wlanpage.h | 4 +- 6 files changed, 84 insertions(+), 23 deletions(-) diff --git a/src/backend/dbus-interface/kywirelessnetitem.h b/src/backend/dbus-interface/kywirelessnetitem.h index b5232230..e02342c7 100644 --- a/src/backend/dbus-interface/kywirelessnetitem.h +++ b/src/backend/dbus-interface/kywirelessnetitem.h @@ -11,6 +11,7 @@ class KyWirelessNetItem { public: KyWirelessNetItem(NetworkManager::WirelessNetwork::Ptr net); + KyWirelessNetItem() {;} ~KyWirelessNetItem(); private: diff --git a/src/backend/dbus-interface/kywirelessnetresource.cpp b/src/backend/dbus-interface/kywirelessnetresource.cpp index d88f0c70..6d920703 100644 --- a/src/backend/dbus-interface/kywirelessnetresource.cpp +++ b/src/backend/dbus-interface/kywirelessnetresource.cpp @@ -50,7 +50,7 @@ bool KyWirelessNetResource::getDeviceWifiNetwork(QString devIfaceName, QList > &map); bool getDeviceWifiNetwork(QString devIfaceName, QList &wirelessNetResource); diff --git a/src/frontend/list-items/wlanlistitem.cpp b/src/frontend/list-items/wlanlistitem.cpp index 43d4d9c4..7af2bdba 100644 --- a/src/frontend/list-items/wlanlistitem.cpp +++ b/src/frontend/list-items/wlanlistitem.cpp @@ -36,7 +36,7 @@ void WlanListItem::initWlanUI() { m_hasPwd = (m_data->m_secuType.isEmpty() || m_data->m_secuType == "") ? false : true; //设置显示的Wlan名称 - this->setName(m_data->m_NetSsid); + this->setName((m_data->m_connName != "") ? m_data->m_connName : m_data->m_NetSsid); //刷新左侧按钮图标 refreshIcon(); } diff --git a/src/frontend/tab-pages/wlanpage.cpp b/src/frontend/tab-pages/wlanpage.cpp index 238da900..6945a775 100644 --- a/src/frontend/tab-pages/wlanpage.cpp +++ b/src/frontend/tab-pages/wlanpage.cpp @@ -10,6 +10,7 @@ WlanPage::WlanPage(QWidget *parent) : TabPage(parent) m_resource = new KyWirelessNetResource(this); initWlanUI(); initConnections(); + getActiveWlan(); getAllWlan(); } @@ -75,32 +76,71 @@ void WlanPage::initConnections() } /** - * @brief WlanPage::getAllWlan 初始化所有Wlan + * @brief WlanPage::getActiveWlan 获取所有已激活连接 + */ +void WlanPage::getActiveWlan() +{ + QMap actMap; + m_activatedNetListWidget->clear(); + m_resource->getWirelessActiveConnection(NetworkManager::ActiveConnection::State::Activated, actMap); + QMap::iterator iter = actMap.begin(); + int height = 0; + while (iter != actMap.end()) { + if (iter.key() == "wlp3s0" && !iter.value().isEmpty()) { + QString ssid = iter.value().at(0); + m_activatedWlanSSid = ssid; + + KyWirelessNetItem data; + if (!m_resource->getWifiNetwork("wlp3s0", ssid, data)) { + return; + } + KyWirelessNetItem *item_data = new KyWirelessNetItem(data); + WlanListItem *wlanItemWidget = new WlanListItem(m_resource, item_data); + qDebug() << "Activated wlan: ssid = " << item_data->m_NetSsid; + QListWidgetItem *wlanItem = new QListWidgetItem(m_activatedNetListWidget); + wlanItem->setSizeHint(QSize(m_activatedNetListWidget->width(), wlanItemWidget->height())); + m_activatedNetListWidget->addItem(wlanItem); + m_activatedNetListWidget->setItemWidget(wlanItem, wlanItemWidget); + height += wlanItemWidget->height(); + break; + } + iter ++; + } + if (height > 0) { + m_activatedNetListWidget->setFixedHeight(height); + } else { + //ZJP_TODO 未连接任何WiFi的情况 + m_activatedWlanSSid.clear(); + } +} + +/** + * @brief WlanPage::getAllWlan 获取所有Wlan //ZJP_TODO 需要针对网卡进行筛选 */ void WlanPage::getAllWlan() { qDebug() << "Started loading wlan list! time=" << QDateTime::currentDateTime().toString("hh:mm:ss.zzzz"); m_inactivatedNetListWidget->clear(); - QMap > map; - if (!m_resource->getAllDeviceWifiNetwork(map)) + m_itemsMap.clear(); + QList wlanList; +// if (!m_resource->getAllDeviceWifiNetwork(map)) + if (!m_resource->getDeviceWifiNetwork("wlp3s0", wlanList)) //ZJP_TODO 获取默认网卡并传入 { return; } - QMap >::iterator iter = map.begin(); int height = 0; - while (iter != map.end()) - { - foreach (auto itemData, iter.value()) { - KyWirelessNetItem *data = &itemData; - WlanListItem *wlanItemWidget = new WlanListItem(m_resource, data); - QListWidgetItem *wlanItem = new QListWidgetItem(m_inactivatedNetListWidget); - wlanItem->setSizeHint(QSize(m_inactivatedNetListWidget->width(), wlanItemWidget->height())); - m_inactivatedNetListWidget->addItem(wlanItem); - m_inactivatedNetListWidget->setItemWidget(wlanItem, wlanItemWidget); - if (height == 0) height += wlanItemWidget->height(); - height += wlanItemWidget->height() + NET_LIST_SPACING; - } - iter++; + foreach (auto itemData, wlanList) { + if (itemData.m_NetSsid == this->m_activatedWlanSSid) { continue; } + + KyWirelessNetItem *data = new KyWirelessNetItem(itemData); + WlanListItem *wlanItemWidget = new WlanListItem(m_resource, data); + QListWidgetItem *wlanItem = new QListWidgetItem(m_inactivatedNetListWidget); + m_itemsMap.insert(data->m_NetSsid, wlanItem); + wlanItem->setSizeHint(QSize(m_inactivatedNetListWidget->width(), wlanItemWidget->height())); + m_inactivatedNetListWidget->addItem(wlanItem); + m_inactivatedNetListWidget->setItemWidget(wlanItem, wlanItemWidget); + if (height == 0) height += wlanItemWidget->height(); + height += wlanItemWidget->height() + NET_LIST_SPACING; } m_inactivatedNetListWidget->setFixedHeight(height); m_inactivatedWlanListAreaCentralWidget->setFixedHeight(m_inactivatedNetListWidget->height() + m_hiddenWlanLabel->height()); @@ -110,16 +150,34 @@ void WlanPage::getAllWlan() void WlanPage::onWlanAdded(QString interface, KyWirelessNetItem &item) { qDebug() << "A Wlan Added! interface = " << interface << "; ssid = " << item.m_NetSsid << Q_FUNC_INFO <<__LINE__; - getAllWlan(); + + KyWirelessNetItem *data = new KyWirelessNetItem(item); + WlanListItem *wlanItemWidget = new WlanListItem(m_resource, data); + QListWidgetItem *wlanItem = new QListWidgetItem(m_inactivatedNetListWidget); + wlanItem->setSizeHint(QSize(m_inactivatedNetListWidget->width(), wlanItemWidget->height())); + m_inactivatedNetListWidget->setItemWidget(wlanItem, wlanItemWidget); +// m_inactivatedNetListWidget->insertItem(N, wlanItem); + m_inactivatedNetListWidget->addItem(wlanItem); //ZJP_TODO 目前会添加到列表尾部 + m_inactivatedNetListWidget->setFixedHeight(m_inactivatedNetListWidget->height() + wlanItemWidget->height() + NET_LIST_SPACING); + m_inactivatedWlanListAreaCentralWidget->setFixedHeight(m_inactivatedNetListWidget->height() + m_hiddenWlanLabel->height()); + + m_itemsMap.insert(data->m_NetSsid, wlanItem); } void WlanPage::onWlanRemoved(QString interface, QString ssid) { + if (!m_itemsMap.contains(ssid)) { return; } qDebug() << "A Wlan Removed! interface = " << interface << "; ssid = " << ssid << Q_FUNC_INFO <<__LINE__; - getAllWlan(); + m_inactivatedNetListWidget->removeItemWidget(m_itemsMap.value(ssid)); + m_inactivatedNetListWidget->setFixedHeight(m_inactivatedNetListWidget->height() - + m_inactivatedNetListWidget->itemWidget(m_itemsMap.value(ssid))->height() - + NET_LIST_SPACING); + m_inactivatedWlanListAreaCentralWidget->setFixedHeight(m_inactivatedNetListWidget->height() + m_hiddenWlanLabel->height()); + m_itemsMap.remove(ssid); } void WlanPage::onWlanUpdated() { + //ZJP_TODO 某些特定情况下不可重绘整个列表,此处代码需要修改 getAllWlan(); } diff --git a/src/frontend/tab-pages/wlanpage.h b/src/frontend/tab-pages/wlanpage.h index 70b5fe30..45e6eece 100644 --- a/src/frontend/tab-pages/wlanpage.h +++ b/src/frontend/tab-pages/wlanpage.h @@ -21,6 +21,7 @@ protected: private: void initWlanUI(); void initConnections(); + void getActiveWlan(); void getAllWlan(); QMap m_itemsMap; QFrame * m_inactivatedWlanListAreaCentralWidget = nullptr; @@ -28,7 +29,8 @@ private: QFrame * m_hiddenWlanWidget = nullptr; QHBoxLayout * m_hiddenWlanLayout = nullptr; QLabel * m_hiddenWlanLabel = nullptr; - QMap m_itemMap; + + QString m_activatedWlanSSid; KyWirelessNetResource *m_resource = nullptr;