From a76ad7de22f16efcad20236ec95dfa666ab3a8fa Mon Sep 17 00:00:00 2001
From: liuxinhao <liuxinhao@kylinsec.com.cn>
Date: Tue, 6 Feb 2024 10:14:07 +0800
Subject: [PATCH 14/16] fix(virutalkeyboard): Add detection for whether to
 install a virtual keyboard
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- 新增对于是否安装虚拟键盘的检测
---
 lib/utils/virtual-keyboard.cpp   | 14 ++++++++++++++
 lib/utils/virtual-keyboard.h     |  5 +++--
 src/lightdm-greeter/frame.cpp    | 11 +++++++++--
 src/screensaver-dialog/frame.cpp |  9 ++++++++-
 4 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/lib/utils/virtual-keyboard.cpp b/lib/utils/virtual-keyboard.cpp
index 292740a..3e1e7dd 100644
--- a/lib/utils/virtual-keyboard.cpp
+++ b/lib/utils/virtual-keyboard.cpp
@@ -22,6 +22,7 @@
 #include <QMutex>
 #include <QScopedPointer>
 #include <QScreen>
+#include <QFileInfo>
 
 #define ONBOARD_LAYOUT "Compact"
 #define ONBOARD_THEME "Blackboard"
@@ -61,10 +62,18 @@ VirtualKeyboard::~VirtualKeyboard()
 
 bool VirtualKeyboard::init(QWidget *parent)
 {
+    if( !QFileInfo::exists("/usr/bin/onboard") )
+    {
+        m_isSupported = false;
+        return false;
+    }
+
     if (m_keyboardWidget != nullptr)
     {
         return false;
     }
+
+    m_isSupported = true;
     m_process = new QProcess(this);
     connect(m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
             this, &VirtualKeyboard::slot_finished);
@@ -98,6 +107,11 @@ bool VirtualKeyboard::init(QWidget *parent)
     return true;
 }
 
+bool VirtualKeyboard::isSupported()
+{
+    return m_isSupported;
+}
+
 bool VirtualKeyboard::isVisible()
 {
     if (m_keyboardWidget == nullptr)
diff --git a/lib/utils/virtual-keyboard.h b/lib/utils/virtual-keyboard.h
index 4670105..99bf405 100644
--- a/lib/utils/virtual-keyboard.h
+++ b/lib/utils/virtual-keyboard.h
@@ -29,8 +29,9 @@ public:
     ~VirtualKeyboard();
 
     static VirtualKeyboard *instance();
-
     bool init(QWidget *parent = nullptr);
+    bool isSupported();
+
     void hide();
     bool isVisible();
     void showAdjustSize(QWidget *parent = nullptr);
@@ -39,11 +40,11 @@ public:
 
 public slots:
     void slot_finished(int exitCode, QProcess::ExitStatus exitStatus);
-
 private slots:
     void slotReadyReadStandardOutput();
 
 private:
+    bool m_isSupported = false;
     QWidget *m_keyboardWidget = nullptr;
     QProcess *m_process = nullptr;
     QWidget *m_keyboardEmbed = nullptr;
diff --git a/src/lightdm-greeter/frame.cpp b/src/lightdm-greeter/frame.cpp
index e4afcb9..085139a 100644
--- a/src/lightdm-greeter/frame.cpp
+++ b/src/lightdm-greeter/frame.cpp
@@ -306,7 +306,14 @@ void Frame::initUI()
 
     rbBtnLayout->addItem(rbBtnLayoutItem);
     rbBtnLayout->addWidget(m_btnSession, 1);
-    rbBtnLayout->addWidget(m_btnKeyboard, 1);
+    if( VirtualKeyboard::instance()->isSupported() )
+    {
+        rbBtnLayout->addWidget(m_btnKeyboard, 1);
+    }
+    else
+    {
+        m_btnKeyboard->setVisible(false);
+    }
     rbBtnLayout->addWidget(m_btnPower, 1);
 
     setRightBottomWidget(rbBtnWidget);
@@ -316,7 +323,7 @@ void Frame::initAuth()
 {
     AuthLightdm* auth = new AuthLightdm(m_greeter);
     LoginFrame::initAuth(auth);
-    connect(m_greeter.data(),&QLightDM::Greeter::autologinTimerExpired,this,&Frame::onAutoLoginTimeout);
+    connect(m_greeter.data(), &QLightDM::Greeter::autologinTimerExpired, this, &Frame::onAutoLoginTimeout);
 }
 
 void Frame::initConnection()
diff --git a/src/screensaver-dialog/frame.cpp b/src/screensaver-dialog/frame.cpp
index 73f50dc..04f0f30 100644
--- a/src/screensaver-dialog/frame.cpp
+++ b/src/screensaver-dialog/frame.cpp
@@ -175,7 +175,14 @@ void Frame::initUI()
 
     rbBtnLayout->addItem(rbBtnLayoutItem);
     rbBtnLayout->addWidget(m_btnSwitchToGreeter, 1);
-    rbBtnLayout->addWidget(m_btnKeyboard, 1);
+    if( VirtualKeyboard::instance()->isSupported() )
+    {
+        rbBtnLayout->addWidget(m_btnKeyboard, 1);
+    }
+    else
+    {
+        m_btnKeyboard->setVisible(false);
+    }
     rbBtnLayout->addWidget(m_btnPower, 1);
     setRightBottomWidget(rbBtnWidget);
 
-- 
2.27.0

