From 091bc92282fd07e989747ca23157f7af5113c723 Mon Sep 17 00:00:00 2001
From: luoqing <luoqing@kylinsec.com.cn>
Date: Fri, 8 Sep 2023 15:53:56 +0800
Subject: [PATCH] fix(audio):The Sink/Source device list displays only active
 ports. The port selection function is temporarily disabled
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- 输入输出设备列表只显示目前已激活的端口，暂时禁用选择端口的功能

Related #13261
---
 .../audio/src/dbus/audio-device-interface.cpp | 33 +++++++++
 .../audio/src/dbus/audio-device-interface.h   |  9 +++
 plugins/audio/src/plugin/input-page.cpp       | 69 +++++++------------
 plugins/audio/src/plugin/input-page.h         |  1 -
 plugins/audio/src/plugin/output-page.cpp      | 62 ++++++-----------
 plugins/audio/src/plugin/output-page.h        |  1 -
 6 files changed, 89 insertions(+), 86 deletions(-)

diff --git a/plugins/audio/src/dbus/audio-device-interface.cpp b/plugins/audio/src/dbus/audio-device-interface.cpp
index a3a750f..7ec201d 100644
--- a/plugins/audio/src/dbus/audio-device-interface.cpp
+++ b/plugins/audio/src/dbus/audio-device-interface.cpp
@@ -13,6 +13,7 @@
  */
 
 #include "audio-device-interface.h"
+#include <qt5-log-i.h>
 
 /*
  * Implementation of interface class AudioDeviceInterface
@@ -28,6 +29,38 @@ AudioDeviceInterface::~AudioDeviceInterface()
 {
 }
 
+QList<AudioPortInfo> AudioDeviceInterface::getPortsInfo()
+{
+    QDBusPendingReply<QString> getPorts = GetPorts();
+    KLOG_DEBUG() << "getPorts:" << getPorts;
+
+    //解析默认sink的端口信息
+    QJsonParseError jsonParseError;
+    QJsonDocument doc = QJsonDocument::fromJson(getPorts.value().toUtf8(), &jsonParseError);
+
+    if((doc.isNull()) || (jsonParseError.error != QJsonParseError::NoError))
+    {
+        return QList<AudioPortInfo>();
+    }
+
+    QList<AudioPortInfo> portInfoList;
+    if (doc.isArray() && jsonParseError.error == QJsonParseError::NoError)
+    {
+        QJsonArray array = doc.array();
+        for (int i = 0; i < array.count(); ++i)
+        {
+            QJsonObject object = array.at(i).toObject();
+            AudioPortInfo portInfo;
+            portInfo.description = object.value("description").toString();
+            portInfo.name = object.value("name").toString();
+            portInfo.priority = object.value("priority").toDouble();
+            portInfoList << portInfo;
+        }
+    }
+
+    return portInfoList;
+}
+
 void sendPropertyChangedDetailSignal(AudioDeviceInterface *ptr, const QString &propertyName, QVariant value)
 {
     if (propertyName == QStringLiteral("active_port"))
diff --git a/plugins/audio/src/dbus/audio-device-interface.h b/plugins/audio/src/dbus/audio-device-interface.h
index 29fbe6d..8bea01a 100644
--- a/plugins/audio/src/dbus/audio-device-interface.h
+++ b/plugins/audio/src/dbus/audio-device-interface.h
@@ -26,6 +26,13 @@
 #include <QtDBus/QtDBus>
 #include "audio-device-interface.h"
 
+struct AudioPortInfo
+{
+    QString description;
+    QString name;
+    double priority;
+};
+
 /*
  * Proxy class for interface com.kylinsec.Kiran.SessionDaemon.Audio.Device
  */
@@ -103,6 +110,8 @@ public:
         return qvariant_cast<double>(property("volume"));
     }
 
+    QList<AudioPortInfo> getPortsInfo();
+
 public Q_SLOTS:  // METHODS
     inline QDBusPendingReply<QString> GetPorts()
     {
diff --git a/plugins/audio/src/plugin/input-page.cpp b/plugins/audio/src/plugin/input-page.cpp
index 8b0ecaa..11275ec 100644
--- a/plugins/audio/src/plugin/input-page.cpp
+++ b/plugins/audio/src/plugin/input-page.cpp
@@ -198,48 +198,28 @@ void InputPage::initInputDevice()
 
 void InputPage::initActivedPort()
 {
-    QDBusPendingReply<QString> getPorts = m_defaultSource->GetPorts();
-    KLOG_DEBUG() << "getPorts:" << getPorts;
-    //解析默认source的端口信息
-    QJsonParseError jsonParseError;
-    QJsonDocument doc = QJsonDocument::fromJson(getPorts.value().toUtf8(), &jsonParseError);
-    if (!doc.isNull() && jsonParseError.error == QJsonParseError::NoError)
-    {
-        if (doc.isArray() && jsonParseError.error == QJsonParseError::NoError)
-        {
-            QJsonArray array = doc.array();
-            for (int i = 0; i < array.count(); ++i)
-            {
-                KLOG_DEBUG() << "array.at(i)" << array.at(i);
-                QJsonObject object = array.at(i).toObject();
-                QString description = object.value("description").toString();
-                QString name = object.value("name").toString();
-                double priority = object.value("priority").toDouble();
-                KLOG_DEBUG() << "description" << description;
-                KLOG_DEBUG() << "name" << name;
-                KLOG_DEBUG() << "priority" << priority;
-
-                ui->inputDevices->insertItem(i, description);
-                ui->inputDevices->setItemData(i, name, Qt::UserRole);  //激活端口所需信息
-                //获取已激活的端口在comobox中的index
-                if (m_defaultSource->active_port() == name)
-                {
-                    m_defaultDeviceIndex = i;
-                    KLOG_DEBUG() << "m_defaultDeviceIndex" << m_defaultDeviceIndex;
-                }
-            }
-        }
-        //默认选中已激活的端口
-        m_isValidPort = true;
-        ui->inputDevices->setCurrentIndex(m_defaultDeviceIndex);
-        ui->inputDevices->setEnabled(true);
-        ui->volumeSetting->setEnabled(true);
-    }
-    else
+    QList<AudioPortInfo> portsInfo = m_defaultSource->getPortsInfo();
+    
+    if(portsInfo.isEmpty())
     {
         KLOG_DEBUG() << "ports is null";
         disableSettings();
+        return;
     }
+
+    Q_FOREACH (auto portInfo, portsInfo)
+    {
+        if(m_defaultSource->active_port() == portInfo.name)
+        {
+            ui->inputDevices->addItem(portInfo.description,portInfo.name);
+            break;
+        }
+    }
+
+    //默认选中已激活的端口
+    m_isValidPort = true;
+    ui->inputDevices->setEnabled(true);
+    ui->volumeSetting->setEnabled(true);
 }
 
 void InputPage::initInputSettins()
@@ -304,13 +284,16 @@ void InputPage::disableSettings()
 void InputPage::handleActivePortChanged(const QString &value)
 {
     KLOG_DEBUG() << "handleActivePortChanged :" << value;
-    for (int i = 0; i < ui->inputDevices->count(); ++i)
+    
+    QList<AudioPortInfo> portsInfo = m_defaultSource->getPortsInfo();
+    
+    Q_FOREACH (auto portInfo, portsInfo)
     {
-        QString name = ui->inputDevices->itemData(i, Qt::UserRole).toString();
-        if (name == value)
+        if(m_defaultSource->active_port() == portInfo.name)
         {
-            ui->inputDevices->setCurrentIndex(i);
-            KLOG_DEBUG() << "handleActivePortChanged  setCurrentIndex " << i;
+            ui->inputDevices->clear();
+            ui->inputDevices->addItem(portInfo.description,portInfo.name);
+            break;
         }
     }
 }
diff --git a/plugins/audio/src/plugin/input-page.h b/plugins/audio/src/plugin/input-page.h
index 3b57daf..e9f6909 100644
--- a/plugins/audio/src/plugin/input-page.h
+++ b/plugins/audio/src/plugin/input-page.h
@@ -91,7 +91,6 @@ private:
     AudioDeviceInterface *m_activedSource;
     QMap<int, AudioDeviceInterface *> m_inputDevicesMap;
     int m_defaultSourceIndex;
-    int m_defaultDeviceIndex;
     bool m_isValidPort = false;
 
     QAudioInput *m_audioInput = nullptr;
diff --git a/plugins/audio/src/plugin/output-page.cpp b/plugins/audio/src/plugin/output-page.cpp
index 420cf98..1242b8a 100644
--- a/plugins/audio/src/plugin/output-page.cpp
+++ b/plugins/audio/src/plugin/output-page.cpp
@@ -93,47 +93,25 @@ void OutputPage::initOutputDevice()
 
 void OutputPage::initActivedPort()
 {
-    QDBusPendingReply<QString> getPorts = m_defaultSink->GetPorts();
-    KLOG_DEBUG() << "getPorts:" << getPorts;
-
-    //解析默认sink的端口信息
-    QJsonParseError jsonParseError;
-    QJsonDocument doc = QJsonDocument::fromJson(getPorts.value().toUtf8(), &jsonParseError);
-    if (!doc.isNull() && jsonParseError.error == QJsonParseError::NoError)
-    {
-        if (doc.isArray() && jsonParseError.error == QJsonParseError::NoError)
-        {
-            QJsonArray array = doc.array();
-            for (int i = 0; i < array.count(); ++i)
-            {
-                KLOG_DEBUG() << "array.at(i)" << array.at(i);
-                QJsonObject object = array.at(i).toObject();
-                QString description = object.value("description").toString();
-                QString name = object.value("name").toString();
-                double priority = object.value("priority").toDouble();
-                KLOG_DEBUG() << "description" << description;
-                KLOG_DEBUG() << "name" << name;
-                KLOG_DEBUG() << "priority" << priority;
-                ui->outputDevices->insertItem(i, description);
-                ui->outputDevices->setItemData(i, name, Qt::UserRole);  //激活端口所需信息
-
-                //获取已激活的端口在comobox中的index
-                if (m_defaultSink->active_port() == name)
-                {
-                    m_defaultDeviceIndex = i;
-                }
-            }
-        }
-        //默认选中已激活的端口
-        ui->outputDevices->setCurrentIndex(m_defaultDeviceIndex);
-        initOutputSettins();
-    }
-    else
+    QList<AudioPortInfo> portsInfo = m_defaultSink->getPortsInfo();
+    if(portsInfo.isEmpty())
     {
         //无激活端口则禁用音量设置和平衡
         KLOG_DEBUG() << "default sink ports is null";
         disableSettings();
+        return;
     }
+
+    Q_FOREACH (auto portInfo, portsInfo)
+    {
+        if(m_defaultSink->active_port() == portInfo.name)
+        {
+            ui->outputDevices->addItem(portInfo.description,portInfo.name);
+            break;
+        }
+    }
+
+    initOutputSettins();
 }
 
 void OutputPage::initOutputSettins()
@@ -202,13 +180,15 @@ void OutputPage::initConnect()
 void OutputPage::handleActivePortChanged(const QString &value)
 {
     KLOG_DEBUG() << "handleActivePortChanged :" << value;
-    for (int i = 0; i < ui->outputDevices->count(); ++i)
+    QList<AudioPortInfo> portsInfo = m_defaultSink->getPortsInfo();
+    
+    Q_FOREACH (auto portInfo, portsInfo)
     {
-        QString name = ui->outputDevices->itemData(i, Qt::UserRole).toString();
-        if (name == value)
+        if(m_defaultSink->active_port() == portInfo.name)
         {
-            ui->outputDevices->setCurrentIndex(i);
-            KLOG_DEBUG() << "handleActivePortChanged  setCurrentIndex " << i;
+            ui->outputDevices->clear();
+            ui->outputDevices->addItem(portInfo.description,portInfo.name);
+            break;
         }
     }
 }
diff --git a/plugins/audio/src/plugin/output-page.h b/plugins/audio/src/plugin/output-page.h
index abaf9a1..dea45e7 100644
--- a/plugins/audio/src/plugin/output-page.h
+++ b/plugins/audio/src/plugin/output-page.h
@@ -61,7 +61,6 @@ private:
     AudioInterface *m_audioInterface;
     QMap<int, AudioDeviceInterface *> m_outputDevicesMap;
     AudioDeviceInterface *m_defaultSink;
-    int m_defaultDeviceIndex;
     QDBusServiceWatcher *m_dbusServiceWatcher;
 };
 
-- 
2.33.0

