mirror of
https://github.com/linuxdeepin/dde-dock.git
synced 2025-06-04 17:33:05 +00:00
feat: 任务栏添加ip冲突提示功能
其他主机与本地ip冲突,任务栏网络图标变为offline状态,鼠标悬浮在网络图标上提示ip冲突. Log: 任务栏新增ip冲突提示功能 Task: https://pms.uniontech.com/zentao/task-view-78387.html Bug: https://pms.uniontech.com/zentao/bug-view-87420.html Change-Id: I3c0357b811b384c16cf1416e0f14009ab6e22534
This commit is contained in:
parent
85872693ed
commit
5b54c309d8
@ -14,6 +14,7 @@ find_package(Qt5Widgets REQUIRED)
|
||||
find_package(Qt5Svg REQUIRED)
|
||||
find_package(Qt5DBus REQUIRED)
|
||||
find_package(DtkWidget REQUIRED)
|
||||
find_package(Qt5Network REQUIRED)
|
||||
|
||||
pkg_check_modules(DDE-Network-Utils REQUIRED dde-network-utils)
|
||||
pkg_check_modules(DFrameworkDBus REQUIRED dframeworkdbus)
|
||||
@ -27,6 +28,7 @@ target_include_directories(${PLUGIN_NAME} PUBLIC ${DtkWidget_INCLUDE_DIRS}
|
||||
${DFrameworkDBus_INCLUDE_DIRS}
|
||||
${QGSettings_INCLUDE_DIRS}
|
||||
${DDE-Network-Utils_INCLUDE_DIRS}
|
||||
${Qt5Network_INCLUDE_DIRS}
|
||||
../../interfaces
|
||||
../../frame)
|
||||
target_link_libraries(${PLUGIN_NAME} PRIVATE
|
||||
@ -37,6 +39,7 @@ target_link_libraries(${PLUGIN_NAME} PRIVATE
|
||||
${QGSettings_LIBRARIES}
|
||||
${DDE-Network-Utils_LIBRARIES}
|
||||
${DFrameworkDBus_LIBRARIES}
|
||||
${Qt5Network_LIBRARIES}
|
||||
)
|
||||
|
||||
install(TARGETS ${PLUGIN_NAME} LIBRARY DESTINATION lib/dde-dock/plugins/system-trays)
|
||||
|
@ -13,6 +13,8 @@
|
||||
#include <QVBoxLayout>
|
||||
#include <QJsonDocument>
|
||||
#include <QGSettings>
|
||||
#include <QNetworkInterface>
|
||||
#include <QHostAddress>
|
||||
|
||||
extern const int ItemWidth;
|
||||
extern const int ItemMargin;
|
||||
@ -39,6 +41,10 @@ NetworkItem::NetworkItem(QWidget *parent)
|
||||
, m_firstSeparator(new HorizontalSeperator(this))
|
||||
, m_secondSeparator(new HorizontalSeperator(this))
|
||||
, m_thirdSeparator(new HorizontalSeperator(this))
|
||||
, m_networkInter(new DbusNetwork("com.deepin.daemon.Network", "/com/deepin/daemon/Network", QDBusConnection::sessionBus(), this))
|
||||
, m_detectTimer(new QTimer(this))
|
||||
, m_ipAddr(QString())
|
||||
, m_ipConflict(false)
|
||||
{
|
||||
refreshIconTimer->setInterval(100);
|
||||
|
||||
@ -68,6 +74,8 @@ NetworkItem::NetworkItem(QWidget *parent)
|
||||
m_loadingIndicator->setFrameShape(QFrame::NoFrame);
|
||||
m_loadingIndicator->installEventFilter(this);
|
||||
|
||||
this->installEventFilter(this);
|
||||
|
||||
m_wirelessLayout = new QVBoxLayout;
|
||||
m_wirelessLayout->setMargin(0);
|
||||
m_wirelessLayout->setSpacing(0);
|
||||
@ -152,6 +160,22 @@ NetworkItem::NetworkItem(QWidget *parent)
|
||||
connect(m_switchWirelessBtn, &DSwitchButton::toggled, this, &NetworkItem::wirelessEnable);
|
||||
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, &NetworkItem::onThemeTypeChanged);
|
||||
|
||||
connect(m_networkInter, &DbusNetwork::IPConflict, this, &NetworkItem::ipConfllict);
|
||||
connect(m_detectTimer, &QTimer::timeout, [ & ]() {
|
||||
// 检查所有网卡ip(有线,无线,虚拟)是否与外界ip冲突
|
||||
foreach(const QString ip, currentIpList()) {
|
||||
// 主动检测,如果冲突会触发IPConflict信号
|
||||
m_networkInter->RequestIPConflictCheck(ip, "");
|
||||
}
|
||||
|
||||
// 距离上一次冲突处理3秒内没有触发信号就视为ip冲突已处理
|
||||
if (m_timeElapse.elapsed() > 3000) {
|
||||
m_ipConflict = false;
|
||||
m_detectTimer->stop();
|
||||
updateSelf();
|
||||
}
|
||||
});
|
||||
|
||||
const QGSettings *gsetting = Utils::SettingsPtr("com.deepin.dde.dock", QByteArray(), this);
|
||||
if (gsetting)
|
||||
connect(gsetting, &QGSettings::changed, [&](const QString &key) {
|
||||
@ -332,6 +356,18 @@ void NetworkItem::refreshIcon()
|
||||
int iconSize = PLUGIN_ICON_MAX_SIZE;
|
||||
int strength = 0;
|
||||
|
||||
if (m_ipConflict) {
|
||||
stateString = "offline";
|
||||
iconString = QString("network-%1-symbolic").arg(stateString);
|
||||
if (height() <= PLUGIN_BACKGROUND_MIN_SIZE
|
||||
&& DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType)
|
||||
iconString.append(PLUGIN_MIN_ICON_NAME);
|
||||
|
||||
m_iconPixmap = ImageUtil::loadSvg(iconString, ":/", iconSize, ratio);
|
||||
update();
|
||||
return;
|
||||
}
|
||||
|
||||
switch (m_pluginState) {
|
||||
case Disabled:
|
||||
case Adisabled:
|
||||
@ -432,6 +468,7 @@ void NetworkItem::refreshIcon()
|
||||
case Failed: //无线连接失败改为 disconnect
|
||||
stateString = "disconnect";
|
||||
iconString = QString("wireless-%1").arg(stateString);
|
||||
break;
|
||||
}
|
||||
|
||||
refreshIconTimer->stop();
|
||||
@ -484,6 +521,24 @@ bool NetworkItem::eventFilter(QObject *obj, QEvent *event)
|
||||
wirelessScan();
|
||||
}
|
||||
}
|
||||
|
||||
// DbusNetwork::IPConflict信号不能保证ip冲突后一直发送信号出来,
|
||||
// 当网络冲突放置很长时间或者进程杀死又重新启动后没有收到ip冲突信号,
|
||||
// 用户会鼠标悬浮检查网络状态,此时发起检测,并更新网络状态
|
||||
if (obj == this) {
|
||||
if (event->type() == QEvent::Enter) {
|
||||
foreach(const QString ip, currentIpList()) {
|
||||
// 主动检测,如果冲突会触发IPConflict信号
|
||||
m_networkInter->RequestIPConflictCheck(ip, "");
|
||||
}
|
||||
|
||||
if (m_timeElapse.elapsed() > 3000) {
|
||||
m_ipConflict = false;
|
||||
m_detectTimer->stop();
|
||||
updateSelf();
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -542,6 +597,21 @@ void NetworkItem::onThemeTypeChanged(DGuiApplicationHelper::ColorType themeType)
|
||||
refreshIcon();
|
||||
}
|
||||
|
||||
void NetworkItem::ipConfllict(const QString &in0, const QString &in1)
|
||||
{
|
||||
Q_UNUSED(in0);
|
||||
Q_UNUSED(in1);
|
||||
|
||||
m_timeElapse.start();
|
||||
m_detectTimer->start(1000);
|
||||
|
||||
if (m_ipConflict)
|
||||
return;
|
||||
|
||||
m_ipConflict = true;
|
||||
updateSelf();
|
||||
}
|
||||
|
||||
void NetworkItem::getPluginState()
|
||||
{
|
||||
int wiredState = 0;
|
||||
@ -1191,6 +1261,11 @@ void NetworkItem::updateMasterControlSwitch()
|
||||
|
||||
void NetworkItem::refreshTips()
|
||||
{
|
||||
if (m_ipConflict) {
|
||||
m_tipsWidget->setText(tr("IP conflict"));
|
||||
return;
|
||||
}
|
||||
|
||||
switch (m_pluginState) {
|
||||
case Disabled:
|
||||
case Adisabled:
|
||||
@ -1354,6 +1429,28 @@ bool NetworkItem::isShowControlCenter()
|
||||
return false;
|
||||
}
|
||||
|
||||
const QStringList NetworkItem::currentIpList()
|
||||
{
|
||||
QStringList strIpAddress = QStringList();
|
||||
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
|
||||
// 获取第一个本主机的IPv4地址
|
||||
int nListSize = ipAddressesList.size();
|
||||
for (int i = 0; i < nListSize; ++i) {
|
||||
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
|
||||
ipAddressesList.at(i).toIPv4Address()) {
|
||||
if (strIpAddress.contains(ipAddressesList.at(i).toString()))
|
||||
continue;
|
||||
|
||||
strIpAddress.append(ipAddressesList.at(i).toString());
|
||||
}
|
||||
}
|
||||
|
||||
if (strIpAddress.isEmpty())
|
||||
strIpAddress.append(QHostAddress(QHostAddress::LocalHost).toString());
|
||||
|
||||
return strIpAddress;
|
||||
}
|
||||
|
||||
void NetworkItem::wirelessScan()
|
||||
{
|
||||
if (m_loadingIndicator->loading())
|
||||
|
@ -1,6 +1,8 @@
|
||||
#ifndef NETWORKITEM_H
|
||||
#define NETWORKITEM_H
|
||||
|
||||
#include "com_deepin_daemon_network.h"
|
||||
|
||||
#include <DGuiApplicationHelper>
|
||||
#include <DSwitchButton>
|
||||
#include <dloadingindicator.h>
|
||||
@ -20,6 +22,9 @@ class TipsWidget;
|
||||
class WiredItem;
|
||||
class WirelessItem;
|
||||
class HorizontalSeperator;
|
||||
|
||||
using DbusNetwork = com::deepin::daemon::Network;
|
||||
|
||||
class NetworkItem : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
@ -64,6 +69,8 @@ public:
|
||||
void refreshTips();
|
||||
bool isShowControlCenter();
|
||||
|
||||
const QStringList currentIpList();
|
||||
|
||||
public slots:
|
||||
void updateSelf();
|
||||
void refreshIcon();
|
||||
@ -79,6 +86,7 @@ private slots:
|
||||
void wiredsEnable(bool enable);
|
||||
void wirelessEnable(bool enable);
|
||||
void onThemeTypeChanged(DGuiApplicationHelper::ColorType themeType);
|
||||
void ipConfllict(const QString &in0, const QString &in1);
|
||||
|
||||
private:
|
||||
void getPluginState();
|
||||
@ -120,6 +128,12 @@ private:
|
||||
HorizontalSeperator *m_firstSeparator;
|
||||
HorizontalSeperator *m_secondSeparator;
|
||||
HorizontalSeperator *m_thirdSeparator;
|
||||
|
||||
DbusNetwork *m_networkInter;
|
||||
QTimer *m_detectTimer;
|
||||
QTime m_timeElapse;
|
||||
QString m_ipAddr;
|
||||
bool m_ipConflict;
|
||||
};
|
||||
|
||||
#endif // NETWORKITEM_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user