From 6bf77f302f189e375f5db080fa27cddf0d924626 Mon Sep 17 00:00:00 2001 From: zhangyuanyuan1 Date: Fri, 28 Apr 2023 09:23:43 +0800 Subject: [PATCH] =?UTF-8?q?bug=20164390(=E9=9C=80=E6=B1=82)=20=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E6=A0=8F=E6=89=98=E7=9B=98=E7=BD=91=E7=BB=9C=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E5=B7=A5=E5=85=B7=E6=8F=90=E7=A4=BA=E4=B8=8D=E5=87=86?= =?UTF-8?q?=E7=A1=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kylinnetworkdeviceresource.cpp | 36 +++++++++ .../kylinnetworkdeviceresource.h | 2 + src/frontend/mainwindow.cpp | 77 ++++++++++++++++++- src/frontend/mainwindow.h | 2 + src/frontend/tab-pages/lanpage.cpp | 32 ++++++++ src/frontend/tab-pages/lanpage.h | 1 + src/frontend/tab-pages/wlanpage.cpp | 32 ++++++++ src/frontend/tab-pages/wlanpage.h | 3 + src/translations/kylin-nm_bo_CN.ts | 32 ++++++++ src/translations/kylin-nm_zh_CN.ts | 32 ++++++++ 10 files changed, 247 insertions(+), 2 deletions(-) diff --git a/src/backend/dbus-interface/kylinnetworkdeviceresource.cpp b/src/backend/dbus-interface/kylinnetworkdeviceresource.cpp index 211e5f8c..12280e9b 100644 --- a/src/backend/dbus-interface/kylinnetworkdeviceresource.cpp +++ b/src/backend/dbus-interface/kylinnetworkdeviceresource.cpp @@ -29,6 +29,7 @@ KyNetworkDeviceResourse::KyNetworkDeviceResourse(QObject *parent) : QObject(pare { qRegisterMetaType("NetworkManager::Device::State"); qRegisterMetaType("NetworkManager::Device::StateChangeReason"); + qRegisterMetaType("NetworkManager::Connectivity"); m_networkResourceInstance = KyNetworkResourceManager::getInstance(); m_deviceMap.clear(); @@ -249,6 +250,41 @@ qulonglong KyNetworkDeviceResourse::getDeviceTxRefreshRate(QString deviceName) return 0; } +void KyNetworkDeviceResourse::getDeviceConnectivity(const QString &deviceName, NetworkManager::Connectivity &connectivity) +{ + connectivity = NetworkManager::Connectivity::UnknownConnectivity; + QString dbusPath; + NetworkManager::Device::Ptr connectDevice = + m_networkResourceInstance->findDeviceInterface(deviceName); + if (connectDevice->isValid()) { + dbusPath = connectDevice->uni(); + } else { + qWarning() << "[KyNetworkDeviceResourse] can not find device " << deviceName; + return; + } + + QDBusInterface *ip4ConnectivityDbus = new QDBusInterface("org.freedesktop.NetworkManager", + dbusPath, + "org.freedesktop.DBus.Properties", + QDBusConnection::systemBus()); + + if (ip4ConnectivityDbus == nullptr || !ip4ConnectivityDbus->isValid()) { + qWarning() << "[KyNetworkDeviceResourse] get device properties failed"; + return; + } + + QDBusReply reply = ip4ConnectivityDbus->call("Get", "org.freedesktop.NetworkManager.Device", "Ip4Connectivity"); + + if (reply.isValid()) { + connectivity = (NetworkManager::Connectivity) reply.value().toUInt(); + } else { + qWarning() << "[KyNetworkDeviceResourse] get device properties failed"; + } + + delete ip4ConnectivityDbus; + ip4ConnectivityDbus = nullptr; +} + bool KyNetworkDeviceResourse::getActiveConnectionInfo(const QString devName, int &signalStrength, QString &uni, QString &secuType) { signalStrength = 0; diff --git a/src/backend/dbus-interface/kylinnetworkdeviceresource.h b/src/backend/dbus-interface/kylinnetworkdeviceresource.h index e16df4cf..b8cc03c9 100644 --- a/src/backend/dbus-interface/kylinnetworkdeviceresource.h +++ b/src/backend/dbus-interface/kylinnetworkdeviceresource.h @@ -72,6 +72,8 @@ public: qulonglong getDeviceRxRefreshRate(QString deviceName); qulonglong getDeviceTxRefreshRate(QString deviceName); + void getDeviceConnectivity(const QString &deviceName, NetworkManager::Connectivity &connectivity); + private: KyWiredConnectOperation wiredOperation; KyNetworkResourceManager *m_networkResourceInstance = nullptr; diff --git a/src/frontend/mainwindow.cpp b/src/frontend/mainwindow.cpp index 7bb986bf..72f99391 100644 --- a/src/frontend/mainwindow.cpp +++ b/src/frontend/mainwindow.cpp @@ -329,7 +329,7 @@ void MainWindow::initTrayIcon() m_showMainwindowAction = new QAction(tr("Show MainWindow"),this); m_showSettingsAction = new QAction(tr("Settings"),this); - m_trayIcon->setToolTip(QString(tr("Network tool"))); +// m_trayIcon->setToolTip(QString(tr("Network tool"))); m_showSettingsAction->setIcon(QIcon::fromTheme("document-page-setup-symbolic", QIcon(":/res/x/setup.png")) ); // m_trayIconMenu->addAction(m_showMainwindowAction); m_trayIconMenu->addAction(m_showSettingsAction); @@ -376,6 +376,11 @@ void MainWindow::initDbusConnnect() connect(m_wlanWidget, &WlanPage::showMainWindow, this, &MainWindow::onShowMainWindow); connect(m_wlanWidget, &WlanPage::connectivityChanged, this, &MainWindow::onConnectivityChanged); + connect(m_lanWidget, &LanPage::lanConnectChanged, this, &MainWindow::onRefreshTrayIconTooltip); + connect(m_lanWidget, &LanPage::deviceStatusChanged, this, &MainWindow::onRefreshTrayIconTooltip); + connect(m_wlanWidget, &WlanPage::wlanConnectChanged, this, &MainWindow::onRefreshTrayIconTooltip); + connect(m_wlanWidget, &WlanPage::deviceStatusChanged, this, &MainWindow::onRefreshTrayIconTooltip); + //模式切换 QDBusConnection::sessionBus().connect(QString("com.kylin.statusmanager.interfacer"), QString("/"), @@ -552,7 +557,36 @@ void MainWindow::setCentralWidgetType(IconActiveType iconStatus) } } else { m_centralWidget->setCurrentIndex(LAN_PAGE_INDEX); - } + } +} + +/** + * @brief MainWindow::assembleTrayIconTooltip 整理托盘图标tooltip内容 + * @param map <网卡名,网络状态> + * @param tip tooltip + */ +void MainWindow::assembleTrayIconTooltip(QMap &map, QString &tip) +{ + if (map.isEmpty()) { + tip = QString(tr("Network tool")); + return; + } + QMap::iterator iter = map.begin(); + if (map.size() == 1) { + tip = map.value(iter.key()); + } else if (map.size() > 1) { + tip = ""; + int count = 0; + while (iter != map.end()) { + count += 1; + tip += QString(tr("Network Card")) + QString("%1").arg(count) + "\n" + map.value(iter.key()); + ++iter; + + if (iter != map.end()) { + tip += "\n"; + } + } + } } /** @@ -652,6 +686,7 @@ void MainWindow::onRefreshTrayIcon() m_trayIcon->setIcon(QIcon::fromTheme(NONE_SIGNAL_LIMIT_ICON)); } } + onRefreshTrayIconTooltip(); } void MainWindow::onSetTrayIconLoading() @@ -703,6 +738,44 @@ void MainWindow::onTabletModeChanged(bool mode) hideMainwindow(); } +/** + * @brief MainWindow::onRefreshTrayIconTooltip 根据托盘图标调整其tooltip + */ +void MainWindow::onRefreshTrayIconTooltip() +{ + if (!m_trayIcon) { + return; + } + + QString trayIconToolTip = ""; + QMap lanMap; + QMap wlanMap; + switch(iconStatus) { + case IconActiveType::NOT_CONNECTED: + trayIconToolTip = QString(tr("Not connected to the network")); + break; + + case LAN_CONNECTED: + case IconActiveType::LAN_CONNECTED_LIMITED: + m_lanWidget->getWiredDeviceConnectState(lanMap); + assembleTrayIconTooltip(lanMap, trayIconToolTip); + break; + + case IconActiveType::WLAN_CONNECTED: + case IconActiveType::WLAN_CONNECTED_LIMITED: + m_wlanWidget->getWirelssDeviceConnectState(wlanMap); + assembleTrayIconTooltip(wlanMap, trayIconToolTip); + break; + + case IconActiveType::ACTIVATING: + default: + trayIconToolTip = QString(tr("Network tool")); + break; + } + + m_trayIcon->setToolTip(trayIconToolTip); +} + void MainWindow::onShowMainWindow(int type) { if (type == LANPAGE || type == WLANPAGE) { diff --git a/src/frontend/mainwindow.h b/src/frontend/mainwindow.h index b7bcb591..9b77c79f 100644 --- a/src/frontend/mainwindow.h +++ b/src/frontend/mainwindow.h @@ -161,6 +161,7 @@ private: void showControlCenter(); void showByWaylandHelper(); void setCentralWidgetType(IconActiveType iconStatus); + void assembleTrayIconTooltip(QMap &map, QString &tip); double m_transparency=1.0; //透明度 QGSettings * m_transGsettings; //透明度配置文件 int currentIconIndex=0; @@ -215,6 +216,7 @@ private Q_SLOTS: void onWlanConnectStatusToChangeTrayIcon(int state); void onConnectivityChanged(NetworkManager::Connectivity connectivity); void onTabletModeChanged(bool mode); + void onRefreshTrayIconTooltip(); }; #endif // MAINWINDOW_H diff --git a/src/frontend/tab-pages/lanpage.cpp b/src/frontend/tab-pages/lanpage.cpp index 4146dabc..d016dd03 100644 --- a/src/frontend/tab-pages/lanpage.cpp +++ b/src/frontend/tab-pages/lanpage.cpp @@ -1303,6 +1303,38 @@ bool LanPage::lanIsConnected() } } +void LanPage::getWiredDeviceConnectState(QMap &map) +{ + map.clear(); + if (m_enableDeviceList.isEmpty()) { + return; + } + + for (const auto devname : m_enableDeviceList) { + NetworkManager::Connectivity state; + QList activedList; + m_deviceResource->getDeviceConnectivity(devname, state); + + if (state < NetworkManager::Connectivity::Full) { + m_activeResourse->getActiveConnectionList(devname, NetworkManager::ConnectionSettings::Wired, activedList); + if (!activedList.isEmpty()) { + map.insert(devname, QString(tr("Connected: ")) + activedList.at(0)->m_connectName + QString(tr("(Limited)"))); + } else { + map.insert(devname, tr("Not Connected")); + } + + } else if (state == NetworkManager::Connectivity::Full) { + m_activeResourse->getActiveConnectionList(devname, NetworkManager::ConnectionSettings::Wired, activedList); + if (!activedList.isEmpty()) { + map.insert(devname, QString(tr("Connected: ")) + activedList.at(0)->m_connectName); + } + + } else { + qDebug() << devname << " Network connectivity is unknown."; + } + } +} + void LanPage::showRate() { //定时获取网速 diff --git a/src/frontend/tab-pages/lanpage.h b/src/frontend/tab-pages/lanpage.h index d8ed6eba..bbb3c8b5 100644 --- a/src/frontend/tab-pages/lanpage.h +++ b/src/frontend/tab-pages/lanpage.h @@ -50,6 +50,7 @@ public: void setWiredDeviceEnable(const QString& devName, bool enable); bool lanIsConnected(); + void getWiredDeviceConnectState(QMap &map); protected: bool eventFilter(QObject *watched, QEvent *event); diff --git a/src/frontend/tab-pages/wlanpage.cpp b/src/frontend/tab-pages/wlanpage.cpp index 5321af0a..701c87fe 100644 --- a/src/frontend/tab-pages/wlanpage.cpp +++ b/src/frontend/tab-pages/wlanpage.cpp @@ -1657,3 +1657,35 @@ int WlanPage::getAcivateWifiSignal() { return m_activatedConnectResource->getAcivateWifiSignal(); } + +void WlanPage::getWirelssDeviceConnectState(QMap &map) +{ + map.clear(); + if (m_devList.isEmpty()) { + return; + } + + for (const auto devname : m_devList) { + NetworkManager::Connectivity state; + KyWirelessNetItem wirelessNetItem; + if (!m_netDeviceResource->getDeviceManaged(devname)) { + continue; + } + m_netDeviceResource->getDeviceConnectivity(devname, state); + if (state < NetworkManager::Connectivity::Full) { + if (m_wirelessNetResource->getActiveWirelessNetItem(devname, wirelessNetItem)) { + map.insert(devname, QString(tr("Connected: ")) + wirelessNetItem.m_connName + QString(tr("(Limited)"))); + } else { + map.insert(devname, tr("Not Connected")); + } + + } else if (state == NetworkManager::Connectivity::Full) { + if (m_wirelessNetResource->getActiveWirelessNetItem(devname, wirelessNetItem)) { + map.insert(devname, QString(tr("Connected: ")) + wirelessNetItem.m_connName); + } + + } else { + qDebug() << devname << " Network connectivity is unknown."; + } + } +} diff --git a/src/frontend/tab-pages/wlanpage.h b/src/frontend/tab-pages/wlanpage.h index d3606881..2d270306 100644 --- a/src/frontend/tab-pages/wlanpage.h +++ b/src/frontend/tab-pages/wlanpage.h @@ -81,6 +81,9 @@ public: int getAcivateWifiSignal(); + //无线网卡连通性 + void getWirelssDeviceConnectState(QMap &map); + Q_SIGNALS: void oneItemExpanded(const QString &ssid); void wlanAdd(QString devName, QStringList info); diff --git a/src/translations/kylin-nm_bo_CN.ts b/src/translations/kylin-nm_bo_CN.ts index c621c1a0..82b4cad6 100644 --- a/src/translations/kylin-nm_bo_CN.ts +++ b/src/translations/kylin-nm_bo_CN.ts @@ -761,6 +761,18 @@ LAN Connected Successfully སྐུད་ཡོད་དྲ་བ་སྦྲེལ་ཡོད། + + Connected: + འབྲེལ་མཐུད་བྱུང་ཡོད།: + + + Not Connected + འབྲེལ་མཐུད་མ་བྱས་པ། + + + (Limited) + (དྲ་བར་ཚོད་འཛིན་ཐེབས་པ་རེད།) + ListItem @@ -810,6 +822,14 @@ Network tool དྲ་རྒྱའི་ལག་ཆ་ + + Network Card + དྲ་བྱང་། + + + Not connected to the network + དྲ་རྒྱ་དང་སྦྲེལ་མཐུད་མ་བྱས་པ། + NetDetail @@ -1415,6 +1435,18 @@ WLAN Disconnected Successfully སྐུད་མེད་དྲ་རྒྱ་ཆད་སོང་། + + Connected: + འབྲེལ་མཐུད་བྱུང་ཡོད།: + + + Not Connected + འབྲེལ་མཐུད་མ་བྱས་པ། + + + (Limited) + (དྲ་བར་ཚོད་འཛིན་ཐེབས་པ་རེད།) + WpaWifiDialog diff --git a/src/translations/kylin-nm_zh_CN.ts b/src/translations/kylin-nm_zh_CN.ts index f2f10e9f..37da337d 100644 --- a/src/translations/kylin-nm_zh_CN.ts +++ b/src/translations/kylin-nm_zh_CN.ts @@ -761,6 +761,18 @@ LAN Connected Successfully 有线网络已连接 + + Connected: + 已连接: + + + Not Connected + 未连接 + + + (Limited) + (网络受限) + ListItem @@ -810,6 +822,14 @@ Network tool 网络工具 + + Network Card + 网卡 + + + Not connected to the network + 未连接网络 + NetDetail @@ -1407,6 +1427,18 @@ WLAN Disconnected Successfully 无线网络已断开 + + Connected: + 已连接: + + + Not Connected + 未连接 + + + (Limited) + (网络受限) + WpaWifiDialog