From 2fcd30a538df01ae8505cf5c1baf26d18b1d2cef Mon Sep 17 00:00:00 2001
From: luoqing <luoqing@kylinsec.com.cn>
Date: Mon, 18 Sep 2023 10:39:58 +0800
Subject: [PATCH] fix(network):Modify DNS Settings and display DNS details
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- 自动获取IP（DNS）后，网络详情显示自动获取到的DNS
  手动配置DNS，网络详情中DNS服务器仅显示手动配置的DNS

Close #14000
---
 .../connection-details-widget.cpp             |  51 +++++--
 .../details-page/connection-details-widget.ui |   6 +-
 .../src/plugin/setting-widget/ipv4-widget.cpp | 135 +++++++++---------
 .../src/plugin/setting-widget/ipv4-widget.ui  |   8 +-
 4 files changed, 116 insertions(+), 84 deletions(-)

diff --git a/plugins/network/src/plugin/details-page/connection-details-widget.cpp b/plugins/network/src/plugin/details-page/connection-details-widget.cpp
index d94b1bd..235c48d 100644
--- a/plugins/network/src/plugin/details-page/connection-details-widget.cpp
+++ b/plugins/network/src/plugin/details-page/connection-details-widget.cpp
@@ -67,7 +67,7 @@ void ConnectionDetailsWidget::init()
 void ConnectionDetailsWidget::initUI()
 {
     QList<KiranFrame *> widgets = {ui->securityTypeWidget, ui->frequencyBandWidget, ui->channelWidget, ui->InterfaceWidget,
-                                   ui->macWidget, ui->ipv4Widget, ui->gatewayWidget, ui->preferredDNSWidget, ui->subnetMaskWidget,
+                                   ui->macWidget, ui->ipv4Widget, ui->gatewayWidget, ui->DNSWidget, ui->subnetMaskWidget,
                                    ui->ipv6Widget, ui->ipv6GatewayWidget, ui->prefixWidget, ui->rateWidget};
 
     for (auto widget : widgets)
@@ -76,7 +76,7 @@ void ConnectionDetailsWidget::initUI()
     }
 
     QList<QLabel *> labels = {ui->securityType, ui->frequencyBand, ui->channel, ui->networkInterface,
-                              ui->mac, ui->ipv4, ui->ipv4Gateway, ui->preferredDNS, ui->subnetMask,
+                              ui->mac, ui->ipv4, ui->ipv4Gateway, ui->DNS, ui->subnetMask,
                               ui->ipv6, ui->ipv6Gateway, ui->prefix, ui->rate};
 
     for (auto label : labels)
@@ -151,25 +151,54 @@ void ConnectionDetailsWidget::setIpDetails()
     ui->subnetMask->setText(netmask);
     ui->ipv4Gateway->setText(gateway);
 
-    QString preferredDNS = "-";
     Dhcp4Config::Ptr dhcp = m_activeConnection->dhcp4Config();
     auto dhcpOptions = dhcp->options();
+    
+    QStringList tmpDNS;
+
+    QStringList dhcpDns;
     if (!dhcpOptions.isEmpty())
     {
         QVariant domainNameServers = dhcpOptions.value("domain_name_servers");
-        // 以空格为分隔
-        QStringList dns = domainNameServers.toString().split(" ");
-        preferredDNS = dns.value(0);
+        dhcpDns = domainNameServers.toString().split(" ");
     }
-    else
+
+    Ipv4Setting::Ptr ipv4Setting = m_connection->settings()->setting(Setting::Ipv4).dynamicCast<Ipv4Setting>();
+    QList<QHostAddress> ipv4SettingDNS = ipv4Setting->dns();
+
+    QStringList manualDNS;
+
+    for(auto &address : ipv4SettingDNS)
+    {
+        manualDNS << address.toString();
+    }
+    /**
+     * NOTE:
+     * 1、自动获取IP（DNS）后，网络详情显示自动获取到的DNS
+     * 2、手动配置DNS，网络详情中DNS服务器仅显示手动配置的DNS
+    */
+
+    QString detailsDNS;
+    manualDNS.isEmpty() ? tmpDNS = dhcpDns : tmpDNS = manualDNS;
+    if(!tmpDNS.isEmpty())
     {
-        Ipv4Setting::Ptr ipv4Setting = m_connection->settings()->setting(Setting::Ipv4).dynamicCast<Ipv4Setting>();
-        if (!ipv4Setting->dns().isEmpty())
+        // 以空格为分隔,删除最后一个;
+        for(auto &dns : tmpDNS)
         {
-            preferredDNS = ipv4Setting->dns().value(0).toString();
+            detailsDNS.append(dns);
+            detailsDNS.append(";");
         }
+        if(detailsDNS.endsWith(";"))
+        {
+            detailsDNS.remove(detailsDNS.size() - 1,1);
+        }
+    }
+    else
+    {
+        detailsDNS = "-";
     }
-    ui->preferredDNS->setText(preferredDNS);
+
+    ui->DNS->setText(detailsDNS);
 
     IpConfig ipV6Config = m_activeConnection->ipV6Config();
     IpAddress ipv6Address = ipV6Config.addresses().value(0);
diff --git a/plugins/network/src/plugin/details-page/connection-details-widget.ui b/plugins/network/src/plugin/details-page/connection-details-widget.ui
index c83af72..b781390 100644
--- a/plugins/network/src/plugin/details-page/connection-details-widget.ui
+++ b/plugins/network/src/plugin/details-page/connection-details-widget.ui
@@ -518,7 +518,7 @@
       </widget>
      </item>
      <item>
-      <widget class="KiranFrame" name="preferredDNSWidget" native="true">
+      <widget class="KiranFrame" name="DNSWidget" native="true">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
          <horstretch>0</horstretch>
@@ -556,7 +556,7 @@
         <item>
          <widget class="QLabel" name="label_15">
           <property name="text">
-           <string>Preferred DNS</string>
+           <string>DNS</string>
           </property>
          </widget>
         </item>
@@ -574,7 +574,7 @@
          </spacer>
         </item>
         <item>
-         <widget class="QLabel" name="preferredDNS">
+         <widget class="QLabel" name="DNS">
           <property name="text">
            <string>TextLabel</string>
           </property>
diff --git a/plugins/network/src/plugin/setting-widget/ipv4-widget.cpp b/plugins/network/src/plugin/setting-widget/ipv4-widget.cpp
index 8c39794..8dc420f 100644
--- a/plugins/network/src/plugin/setting-widget/ipv4-widget.cpp
+++ b/plugins/network/src/plugin/setting-widget/ipv4-widget.cpp
@@ -74,65 +74,68 @@ void Ipv4Widget::setErrorTips(KiranTips *errorTips)
 
 void Ipv4Widget::saveSettings()
 {
-    if (m_ipv4Setting != nullptr)
+    if (m_ipv4Setting == nullptr)
     {
-        IpAddress ipv4Address;
-        Ipv4Setting::ConfigMethod method = ui->ipv4Method->currentData().value<NetworkManager::Ipv4Setting::ConfigMethod>();
-        if (method == Ipv4Setting::ConfigMethod::Automatic)
-        {
-            m_ipv4Setting->setMethod(method);
-            ipv4Address.setIp(QHostAddress(""));
-            ipv4Address.setNetmask(QHostAddress(""));
-            ipv4Address.setGateway(QHostAddress(""));
-            m_ipv4Setting->setAddresses(QList<NetworkManager::IpAddress>() << ipv4Address);
-        }
-        else if (method == Ipv4Setting::ConfigMethod::Manual)
-        {
-            m_ipv4Setting->setMethod(method);
+        return;
+    }
+
+    IpAddress ipv4Address;
+    Ipv4Setting::ConfigMethod method = ui->ipv4Method->currentData().value<NetworkManager::Ipv4Setting::ConfigMethod>();
+    if (method == Ipv4Setting::ConfigMethod::Automatic)
+    {
+        m_ipv4Setting->setMethod(method);
+        ipv4Address.setIp(QHostAddress(""));
+        ipv4Address.setNetmask(QHostAddress(""));
+        ipv4Address.setGateway(QHostAddress(""));
+        m_ipv4Setting->setAddresses(QList<NetworkManager::IpAddress>() << ipv4Address);
+    }
+    else if (method == Ipv4Setting::ConfigMethod::Manual)
+    {
+        m_ipv4Setting->setMethod(method);
 
-            ipv4Address.setIp(QHostAddress(ui->ipv4Address->text()));
-            QString netMask = ui->ipv4Netmask->text();
-            if (!netMask.contains("."))
+        ipv4Address.setIp(QHostAddress(ui->ipv4Address->text()));
+        QString netMask = ui->ipv4Netmask->text();
+        if (!netMask.contains("."))
+        {
+            int netPrefix = netMask.toInt();
+            KLOG_DEBUG() << "netMask.toInt():" << netMask.toInt();
+            if ((netPrefix > 0) & (netPrefix < 33))
             {
-                int netPrefix = netMask.toInt();
-                KLOG_DEBUG() << "netMask.toInt():" << netMask.toInt();
-                if ((netPrefix > 0) & (netPrefix < 33))
-                {
-                    ipv4Address.setPrefixLength(netPrefix);
-                }
-                else
-                {
-                    KLOG_DEBUG() << "Net prefix length error";
-                }
+                ipv4Address.setPrefixLength(netPrefix);
             }
             else
             {
-                ipv4Address.setNetmask(QHostAddress(netMask));
+                KLOG_DEBUG() << "Net prefix length error";
             }
-
-            ipv4Address.setGateway(QHostAddress(ui->ipv4Gateway->text()));
-            KLOG_DEBUG() << "ipv4Address.ip():" << ipv4Address.ip();
-            KLOG_DEBUG() << "ipv4Address.netmask():" << ipv4Address.netmask();
-            KLOG_DEBUG() << "ipv4Address.prefixLength():" << ipv4Address.prefixLength();
-            KLOG_DEBUG() << "ipv4Address.gateway():" << ipv4Address.gateway();
-
-            QList<IpAddress> ipv4AddresseList;
-            ipv4AddresseList << ipv4Address;
-            m_ipv4Setting->setAddresses(ipv4AddresseList);
-        }
-
-        QList<QHostAddress> ipv4DNS;
-        if (!ui->ipv4PreferredDNS->text().isEmpty())
-        {
-            ipv4DNS << QHostAddress(ui->ipv4PreferredDNS->text());
         }
-        if (!ui->ipv4AlternateDNS->text().isEmpty())
+        else
         {
-            ipv4DNS << QHostAddress(ui->ipv4AlternateDNS->text());
+            ipv4Address.setNetmask(QHostAddress(netMask));
         }
-        KLOG_DEBUG() << "ipv4DNS:" << ipv4DNS;
-        m_ipv4Setting->setDns(ipv4DNS);
+
+        ipv4Address.setGateway(QHostAddress(ui->ipv4Gateway->text()));
+        KLOG_DEBUG() << "ipv4Address.ip():" << ipv4Address.ip();
+        KLOG_DEBUG() << "ipv4Address.netmask():" << ipv4Address.netmask();
+        KLOG_DEBUG() << "ipv4Address.prefixLength():" << ipv4Address.prefixLength();
+        KLOG_DEBUG() << "ipv4Address.gateway():" << ipv4Address.gateway();
+
+        QList<IpAddress> ipv4AddresseList;
+        ipv4AddresseList << ipv4Address;
+        m_ipv4Setting->setAddresses(ipv4AddresseList);
+    }
+
+    QList<QHostAddress> ipv4DNS;
+    if (!ui->ipv4FirstDNS->text().isEmpty())
+    {
+        ipv4DNS << QHostAddress(ui->ipv4FirstDNS->text());
+    }
+    if (!ui->ipv4SecondDNS->text().isEmpty())
+    {
+        ipv4DNS << QHostAddress(ui->ipv4SecondDNS->text());
     }
+    KLOG_DEBUG() << "ipv4DNS:" << ipv4DNS;
+    m_ipv4Setting->setDns(ipv4DNS);
+    
 }
 
 void Ipv4Widget::showSettings()
@@ -171,20 +174,20 @@ void Ipv4Widget::showSettings()
                 ui->ipv4Gateway->clear();
             }
         }
-        QString preferredDNS = "";
-        QString alternateDNS = "";
+        QString firstDNS = "";
+        QString secondDNS = "";
         if (!m_ipv4Setting->dns().isEmpty())
         {
-            preferredDNS = m_ipv4Setting->dns().at(0).toString();
+            firstDNS = m_ipv4Setting->dns().at(0).toString();
             if (m_ipv4Setting->dns().count() >= 2)
             {
-                alternateDNS = m_ipv4Setting->dns().at(1).toString();
+                secondDNS = m_ipv4Setting->dns().at(1).toString();
             }
         }
-        KLOG_DEBUG() << "preferredDNS:" << preferredDNS;
-        KLOG_DEBUG() << "alternateDNS:" << alternateDNS;
-        ui->ipv4PreferredDNS->setText(preferredDNS);
-        ui->ipv4AlternateDNS->setText(alternateDNS);
+        KLOG_DEBUG() << "firstDNS:" << firstDNS;
+        KLOG_DEBUG() << "secondDNS:" << secondDNS;
+        ui->ipv4FirstDNS->setText(firstDNS);
+        ui->ipv4SecondDNS->setText(secondDNS);
     }
     else
         resetSettings();
@@ -197,8 +200,8 @@ void Ipv4Widget::resetSettings()
     ui->ipv4Address->clear();
     ui->ipv4Netmask->clear();
     ui->ipv4Gateway->clear();
-    ui->ipv4PreferredDNS->clear();
-    ui->ipv4AlternateDNS->clear();
+    ui->ipv4FirstDNS->clear();
+    ui->ipv4SecondDNS->clear();
 }
 
 void Ipv4Widget::clearPtr()
@@ -271,27 +274,27 @@ bool Ipv4Widget::isInputValid()
         }
     }
 
-    QString preferredDNS = ui->ipv4PreferredDNS->text();
-    if (!preferredDNS.isEmpty())
+    QString firstDNS = ui->ipv4FirstDNS->text();
+    if (!firstDNS.isEmpty())
     {
-        if (!isIpv4AddressValid(preferredDNS))
+        if (!isIpv4AddressValid(firstDNS))
         {
             QString error = QString(tr("Ipv4 Preferred DNS invalid"));
             m_errorTip->setText(error);
-            m_errorTip->showTipAroundWidget(ui->ipv4PreferredDNS);
+            m_errorTip->showTipAroundWidget(ui->ipv4FirstDNS);
             KLOG_DEBUG() << "Ipv4 Preferred DNS invalid";
             return false;
         }
     }
 
-    QString alternateDNS = ui->ipv4AlternateDNS->text();
-    if (!alternateDNS.isEmpty())
+    QString secondDNS = ui->ipv4SecondDNS->text();
+    if (!secondDNS.isEmpty())
     {
-        if (!isIpv4AddressValid(alternateDNS))
+        if (!isIpv4AddressValid(secondDNS))
         {
             QString error = QString(tr("Ipv4 Alternate DNS invalid"));
             m_errorTip->setText(error);
-            m_errorTip->showTipAroundWidget(ui->ipv4AlternateDNS);
+            m_errorTip->showTipAroundWidget(ui->ipv4SecondDNS);
             KLOG_DEBUG() << "Ipv4 Alternate DNS invalid";
             return false;
         }
diff --git a/plugins/network/src/plugin/setting-widget/ipv4-widget.ui b/plugins/network/src/plugin/setting-widget/ipv4-widget.ui
index 026e26d..93b2355 100644
--- a/plugins/network/src/plugin/setting-widget/ipv4-widget.ui
+++ b/plugins/network/src/plugin/setting-widget/ipv4-widget.ui
@@ -187,12 +187,12 @@
      <item>
       <widget class="QLabel" name="label_16">
        <property name="text">
-        <string>Preferred DNS</string>
+        <string>DNS 1</string>
        </property>
       </widget>
      </item>
      <item>
-      <widget class="QLineEdit" name="ipv4PreferredDNS">
+      <widget class="QLineEdit" name="ipv4FirstDNS">
        <property name="minimumSize">
         <size>
          <width>0</width>
@@ -220,12 +220,12 @@
      <item>
       <widget class="QLabel" name="label_14">
        <property name="text">
-        <string>Alternate DNS</string>
+        <string>DNS 2</string>
        </property>
       </widget>
      </item>
      <item>
-      <widget class="QLineEdit" name="ipv4AlternateDNS">
+      <widget class="QLineEdit" name="ipv4SecondDNS">
        <property name="minimumSize">
         <size>
          <width>0</width>
-- 
2.33.0

