bug 164390(需求) 任务栏托盘网络连接工具提示不准确

This commit is contained in:
zhangyuanyuan1 2023-04-28 09:23:43 +08:00
parent 1af6a8b036
commit 6bf77f302f
10 changed files with 247 additions and 2 deletions

View File

@ -29,6 +29,7 @@ KyNetworkDeviceResourse::KyNetworkDeviceResourse(QObject *parent) : QObject(pare
{
qRegisterMetaType<NetworkManager::Device::State>("NetworkManager::Device::State");
qRegisterMetaType<NetworkManager::Device::StateChangeReason>("NetworkManager::Device::StateChangeReason");
qRegisterMetaType<NetworkManager::Connectivity>("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<QVariant> 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;

View File

@ -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;

View File

@ -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<QString, QString> &map, QString &tip)
{
if (map.isEmpty()) {
tip = QString(tr("Network tool"));
return;
}
QMap<QString, QString>::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<QString, QString> lanMap;
QMap<QString, QString> 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) {

View File

@ -161,6 +161,7 @@ private:
void showControlCenter();
void showByWaylandHelper();
void setCentralWidgetType(IconActiveType iconStatus);
void assembleTrayIconTooltip(QMap<QString, QString> &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

View File

@ -1303,6 +1303,38 @@ bool LanPage::lanIsConnected()
}
}
void LanPage::getWiredDeviceConnectState(QMap<QString, QString> &map)
{
map.clear();
if (m_enableDeviceList.isEmpty()) {
return;
}
for (const auto devname : m_enableDeviceList) {
NetworkManager::Connectivity state;
QList<KyConnectItem *> 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()
{
//定时获取网速

View File

@ -50,6 +50,7 @@ public:
void setWiredDeviceEnable(const QString& devName, bool enable);
bool lanIsConnected();
void getWiredDeviceConnectState(QMap<QString, QString> &map);
protected:
bool eventFilter(QObject *watched, QEvent *event);

View File

@ -1657,3 +1657,35 @@ int WlanPage::getAcivateWifiSignal()
{
return m_activatedConnectResource->getAcivateWifiSignal();
}
void WlanPage::getWirelssDeviceConnectState(QMap<QString, QString> &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.";
}
}
}

View File

@ -81,6 +81,9 @@ public:
int getAcivateWifiSignal();
//无线网卡连通性
void getWirelssDeviceConnectState(QMap<QString, QString> &map);
Q_SIGNALS:
void oneItemExpanded(const QString &ssid);
void wlanAdd(QString devName, QStringList info);

View File

@ -761,6 +761,18 @@
<source>LAN Connected Successfully</source>
<translation type="vanished"></translation>
</message>
<message>
<source>Connected: </source>
<translation>: </translation>
</message>
<message>
<source>Not Connected</source>
<translation></translation>
</message>
<message>
<source>(Limited)</source>
<translation>()</translation>
</message>
</context>
<context>
<name>ListItem</name>
@ -810,6 +822,14 @@
<source>Network tool</source>
<translation></translation>
</message>
<message>
<source>Network Card</source>
<translation></translation>
</message>
<message>
<source>Not connected to the network</source>
<translation></translation>
</message>
</context>
<context>
<name>NetDetail</name>
@ -1415,6 +1435,18 @@
<source>WLAN Disconnected Successfully</source>
<translation></translation>
</message>
<message>
<source>Connected: </source>
<translation>: </translation>
</message>
<message>
<source>Not Connected</source>
<translation></translation>
</message>
<message>
<source>(Limited)</source>
<translation>()</translation>
</message>
</context>
<context>
<name>WpaWifiDialog</name>

View File

@ -761,6 +761,18 @@
<source>LAN Connected Successfully</source>
<translation type="vanished">线</translation>
</message>
<message>
<source>Connected: </source>
<translation>: </translation>
</message>
<message>
<source>Not Connected</source>
<translation></translation>
</message>
<message>
<source>(Limited)</source>
<translation>()</translation>
</message>
</context>
<context>
<name>ListItem</name>
@ -810,6 +822,14 @@
<source>Network tool</source>
<translation></translation>
</message>
<message>
<source>Network Card</source>
<translation></translation>
</message>
<message>
<source>Not connected to the network</source>
<translation></translation>
</message>
</context>
<context>
<name>NetDetail</name>
@ -1407,6 +1427,18 @@
<source>WLAN Disconnected Successfully</source>
<translation>线</translation>
</message>
<message>
<source>Connected: </source>
<translation>: </translation>
</message>
<message>
<source>Not Connected</source>
<translation></translation>
</message>
<message>
<source>(Limited)</source>
<translation>()</translation>
</message>
</context>
<context>
<name>WpaWifiDialog</name>