From 55049f4ee286633b486936c7f4d19d3d426453a0 Mon Sep 17 00:00:00 2001
From: liuxinhao <liuxinhao@kylinsec.com.cn>
Date: Mon, 15 Jan 2024 16:31:01 +0800
Subject: [PATCH 14/17] fix(shortcut): add custom app icon,Error prompt for
 modifying input shortcut keys
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- 修改录入快捷键错误提示，新增自定义应用图标显示

Closes #25199,#25198
---
 plugins/keybinding/shortcut.cpp           |  79 ++++++++++++----
 plugins/keybinding/shortcut.h             |  10 +-
 translations/kiran-control-panel.zh_CN.ts | 106 ++++++++++++----------
 3 files changed, 127 insertions(+), 68 deletions(-)

diff --git a/plugins/keybinding/shortcut.cpp b/plugins/keybinding/shortcut.cpp
index bab4ad1..84c0cba 100644
--- a/plugins/keybinding/shortcut.cpp
+++ b/plugins/keybinding/shortcut.cpp
@@ -31,12 +31,15 @@
 #include <QKeyEvent>
 #include <QtConcurrentRun>
 
-Q_DECLARE_METATYPE(QList<ShortcutInfoPtr>)
+#define DEFAULT_APP_ICON "application-script-blank"
+#define APP_ICON_WIDTH 20
 
+Q_DECLARE_METATYPE(QList<ShortcutInfoPtr>)
 using namespace Kiran;
 
-Shortcut::Shortcut(QWidget *parent) : QWidget(parent),
-                                      ui(new Ui::Shortcut)
+Shortcut::Shortcut(QWidget *parent)
+    : QWidget(parent),
+    ui(new Ui::Shortcut)
 {
     ui->setupUi(this);
     init();
@@ -100,16 +103,24 @@ void Shortcut::initUI()
     }
 
     QHBoxLayout *hLayoutCustomApp = new QHBoxLayout(ui->lineEdit_custom_app);
+    m_customAppIcon = new QLabel;
+    m_customAppIcon->setFixedSize(24, 24);
+    m_customAppIcon->setPixmap(QIcon::fromTheme(DEFAULT_APP_ICON).pixmap(20, 20));
+    hLayoutCustomApp->addWidget(m_customAppIcon, 0, Qt::AlignVCenter);
+
+    hLayoutCustomApp->addStretch();
+
     m_btnCustomApp = new QToolButton;
     m_btnCustomApp->setObjectName("btn_custom_app");
     m_btnCustomApp->setAccessibleName("ButtonAddCustomApp");
     m_btnCustomApp->setText(tr("Add"));
     m_btnCustomApp->setFixedSize(56, 30);
     m_btnCustomApp->setCursor(Qt::PointingHandCursor);
-    hLayoutCustomApp->addStretch();
     hLayoutCustomApp->addWidget(m_btnCustomApp);
-    ui->lineEdit_custom_app->setTextMargins(0, 0, m_btnCustomApp->width(), 0);
+
+    ui->lineEdit_custom_app->setTextMargins(25, 0, m_btnCustomApp->width(), 0);
     connect(m_btnCustomApp, &QToolButton::clicked, this, &Shortcut::openFileSys);
+    connect(ui->lineEdit_custom_app,&QLineEdit::textChanged,this,&Shortcut::handleCustomAppTextChanged);
 
     QHBoxLayout *hLayoutModifyApp = new QHBoxLayout(ui->lineEdit_modify_app);
     m_btnModifyApp = new QToolButton;
@@ -354,7 +365,7 @@ bool Shortcut::isValidKeycode(QList<int> keycodes)
     return !pureModifier;
 }
 
-bool Shortcut::getExecFromDesktop(QString fileName, QString &exec)
+bool Shortcut::extractDesktopInfo(const QString &fileName, QString &exec, QString &icon)
 {
     QSettings settings(fileName, QSettings::IniFormat);
     QString str = settings.value("Desktop Entry/Exec").toString();
@@ -368,6 +379,10 @@ bool Shortcut::getExecFromDesktop(QString fileName, QString &exec)
     str = str.replace("%u", "", Qt::CaseInsensitive);
 
     exec = str;
+
+    str = settings.value("Desktop Entry/Icon").toString();
+    icon = str.isEmpty() ? DEFAULT_APP_ICON : str;
+
     return true;
 }
 
@@ -376,23 +391,38 @@ void Shortcut::openFileSys()
     QToolButton *senderbtn = qobject_cast<QToolButton *>(sender());
     QLineEdit *lineEdit = qobject_cast<QLineEdit *>(senderbtn->parent());
 
-    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "/usr/share/applications");
+    QString fileName = QFileDialog::getOpenFileName(this,
+                                                    tr("Open File"),
+                                                    "/usr/share/applications",
+                                                    tr("Desktop entries(*.desktop)"));
     if (fileName.isNull())
         return;
 
-    QString exec = fileName;
-    if (fileName.endsWith(".desktop"))
+    QString cmd;
+    QString icon = DEFAULT_APP_ICON;
+    if (!extractDesktopInfo(fileName, cmd, icon))
     {
-        QString tmp;
-        if (!getExecFromDesktop(fileName, tmp))
-        {
-            KLOG_ERROR(qLcKeybinding) << "cant't get Exec key from " << fileName;
-            return;
-        }
-        exec = tmp;
+        KLOG_ERROR(qLcKeybinding) << "cant't get Exec key from " << fileName;
+        KiranMessageBox::message(this, tr("Error"),
+                                    "Extracting the program to be executed from the application's desktop file failed",
+                                    KiranMessageBox::Ok);
+        return;
+    }
+
+    // 缓存该次添加的应用以及图标信息
+    // 后续启动命令修改的时候，应复位图标
+    m_lastIcon = icon;
+    m_lastIconExec = cmd;
+
+    QIcon appIcon = QIcon::fromTheme(icon);
+    if( appIcon.isNull() )
+    {
+        m_lastIcon = DEFAULT_APP_ICON;
+        appIcon = QIcon::fromTheme(DEFAULT_APP_ICON);
     }
 
-    lineEdit->setText(exec);
+    m_customAppIcon->setPixmap(appIcon.pixmap(QSize(APP_ICON_WIDTH,APP_ICON_WIDTH)));
+    lineEdit->setText(cmd);
 }
 
 void Shortcut::handleSearchTimerTimeout()
@@ -787,6 +817,16 @@ void Shortcut::handleResetClicked()
     }
 }
 
+void Shortcut::handleCustomAppTextChanged(const QString& text)
+{
+    // 直接编辑自定义应用输入框，修改命令
+    // 导致和desktop读取出来的不一致时清空图标
+    if( !text.isEmpty() && text != m_lastIconExec )
+    {
+        m_customAppIcon->setPixmap(QIcon::fromTheme(DEFAULT_APP_ICON).pixmap(20,20));
+    }
+}
+
 void Shortcut::handleInputKeycode(QList<int> keycodes)
 {
     CustomLineEdit *senderLineEdit = qobject_cast<CustomLineEdit *>(sender());
@@ -803,8 +843,9 @@ void Shortcut::handleInputKeycode(QList<int> keycodes)
         {
             KiranMessageBox::message(nullptr,
                                      tr("Failed"),
-                                     QString(tr("Cannot use shortcut \"%1\", Because you cannot enter with this key."
-                                                "Please try again using Ctrl, Alt, or Shift at the same time."))
+                                     QString(tr("Cannot use shortcut \"%1\","
+                                         "Please keep pressing the modifier keys such as Ctrl,"
+                                         "Alt, and Shift before pressing the last key of the shortcut key"))
                                          .arg(keyStr),
                                      KiranMessageBox::Ok);
             return;
diff --git a/plugins/keybinding/shortcut.h b/plugins/keybinding/shortcut.h
index fc078fb..3504b1b 100644
--- a/plugins/keybinding/shortcut.h
+++ b/plugins/keybinding/shortcut.h
@@ -35,6 +35,8 @@ class ShortcutItem;
 class KeyMap;
 class CustomLineEdit;
 class KeybindingBackEndProxy;
+class QLabel;
+
 class Shortcut : public QWidget
 {
     Q_OBJECT
@@ -59,7 +61,7 @@ private:
     ShortcutItem *createShortcutItem(QVBoxLayout *parent, ShortcutInfoPtr shortcutInfo, int type);
     bool isConflict(QString &originName, QString newKeyCombination);
     bool isValidKeycode(QList<int> keycodes);
-    bool getExecFromDesktop(QString fileName, QString &exec);
+    bool extractDesktopInfo(const QString& fileName, QString &exec, QString &icon);
     void updateShorcut(ShortcutInfoPtr newShortcut);
     void insertShortcut(ShortcutInfoPtr shortcutInfo);
     void clearFilterItems();
@@ -72,6 +74,7 @@ public slots:
     void handledShortcutDeleted(QString result);
     void handleShortcutChanged(QString result);
 
+    void handleCustomAppTextChanged(const QString& text);
     void handleInputKeycode(QList<int> keycodes);
 
     void handleItemDeleteClicked(QString uid);
@@ -96,8 +99,13 @@ private:
     QList<ShortcutItem *> m_shortcutItem;
     QList<ShortcutItem *> m_filterItem;
 
+    //记录上次通过应用打开的desktop文件记录的Exec和Icon
+    QString m_lastIconExec;
+    QString m_lastIcon;
+
     QToolButton *m_btnModifyApp;
     QToolButton *m_btnCustomApp;
+    QLabel *m_customAppIcon;
     CustomLineEdit *m_lECustomKey;
     CustomLineEdit *m_lEModifyKey;
 
diff --git a/translations/kiran-control-panel.zh_CN.ts b/translations/kiran-control-panel.zh_CN.ts
index 970c5a0..31fc01e 100644
--- a/translations/kiran-control-panel.zh_CN.ts
+++ b/translations/kiran-control-panel.zh_CN.ts
@@ -33,18 +33,18 @@
     <name>AccountWidget</name>
     <message>
         <location filename="../plugins/account/account-widget.cpp" line="100"/>
-        <location filename="../plugins/account/account-widget.cpp" line="420"/>
+        <location filename="../plugins/account/account-widget.cpp" line="418"/>
         <source>disable</source>
         <translation type="unfinished">禁用</translation>
     </message>
     <message>
         <location filename="../plugins/account/account-widget.cpp" line="100"/>
-        <location filename="../plugins/account/account-widget.cpp" line="420"/>
+        <location filename="../plugins/account/account-widget.cpp" line="418"/>
         <source>enable</source>
         <translation type="unfinished">启用</translation>
     </message>
     <message>
-        <location filename="../plugins/account/account-widget.cpp" line="225"/>
+        <location filename="../plugins/account/account-widget.cpp" line="223"/>
         <source>Create new user</source>
         <translation type="unfinished">创建新用户</translation>
     </message>
@@ -2983,22 +2983,22 @@
         <translation>请用分号分隔多个DNS</translation>
     </message>
     <message>
-        <location filename="../plugins/network/src/plugin/setting-widget/ipv6-widget.cpp" line="234"/>
+        <location filename="../plugins/network/src/plugin/setting-widget/ipv6-widget.cpp" line="236"/>
         <source>Ipv6 DNS invalid</source>
         <translation>无效的Ipv6 DNS</translation>
     </message>
     <message>
-        <location filename="../plugins/network/src/plugin/setting-widget/ipv6-widget.cpp" line="250"/>
+        <location filename="../plugins/network/src/plugin/setting-widget/ipv6-widget.cpp" line="252"/>
         <source>Ipv6 address can not be empty</source>
         <translation>Ipv6地址不能为空</translation>
     </message>
     <message>
-        <location filename="../plugins/network/src/plugin/setting-widget/ipv6-widget.cpp" line="261"/>
+        <location filename="../plugins/network/src/plugin/setting-widget/ipv6-widget.cpp" line="263"/>
         <source>Ipv6 address invalid</source>
         <translation>无效的Ipv6地址</translation>
     </message>
     <message>
-        <location filename="../plugins/network/src/plugin/setting-widget/ipv6-widget.cpp" line="274"/>
+        <location filename="../plugins/network/src/plugin/setting-widget/ipv6-widget.cpp" line="276"/>
         <source>Ipv6 Gateway invalid</source>
         <translation>无效的Ipv6网关</translation>
     </message>
@@ -4675,8 +4675,8 @@ This is line 50 of the test text</source>
     </message>
     <message>
         <location filename="../plugins/keybinding/shortcut.ui" line="210"/>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="163"/>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="550"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="174"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="580"/>
         <source>Edit</source>
         <translation>编辑</translation>
     </message>
@@ -4688,8 +4688,8 @@ This is line 50 of the test text</source>
     <message>
         <location filename="../plugins/keybinding/shortcut.ui" line="346"/>
         <location filename="../plugins/keybinding/shortcut.ui" line="551"/>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="106"/>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="118"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="116"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="129"/>
         <source>Add</source>
         <translation>添加</translation>
     </message>
@@ -4742,7 +4742,7 @@ This is line 50 of the test text</source>
     <message>
         <location filename="../plugins/keybinding/shortcut.ui" line="589"/>
         <source>Cancel</source>
-        <translation type="unfinished">取消</translation>
+        <translation>取消</translation>
     </message>
     <message>
         <location filename="../plugins/keybinding/shortcut.ui" line="635"/>
@@ -4772,7 +4772,7 @@ This is line 50 of the test text</source>
     <message>
         <location filename="../plugins/keybinding/shortcut.ui" line="770"/>
         <source>Save</source>
-        <translation type="unfinished">保存</translation>
+        <translation>保存</translation>
     </message>
     <message>
         <location filename="../plugins/keybinding/shortcut.ui" line="805"/>
@@ -4782,129 +4782,139 @@ This is line 50 of the test text</source>
     <message>
         <location filename="../plugins/keybinding/shortcut.ui" line="808"/>
         <source>return</source>
-        <translation type="unfinished">返回</translation>
+        <translation>返回</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="87"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="90"/>
         <source>Please enter a search keyword...</source>
         <translation>请输入搜索关键字...</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="99"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="102"/>
         <source>Required</source>
         <translation>必填</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="127"/>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="134"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="138"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="145"/>
         <source>Please press the new shortcut key</source>
         <translation>请输入新快捷键</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="161"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="172"/>
         <source>Finished</source>
         <translation>完成</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="215"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="226"/>
         <source>failed to load shortcut key data!</source>
         <translation>加载快捷键数据失败！</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="257"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="268"/>
         <source>List shortcut failed,error:%1</source>
         <translation>列出快捷键失败，错误:%1</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="300"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="311"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="406"/>
         <source>Error</source>
         <translation>错误</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="301"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="312"/>
         <source>Get shortcut failed,error:</source>
         <translation>获取快捷键失败，错误：</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="379"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="395"/>
         <source>Open File</source>
         <translation>打开文件</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="603"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="397"/>
+        <source>Desktop entries(*.desktop)</source>
+        <translation>桌面文件(*.desktop)</translation>
+    </message>
+    <message>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="633"/>
         <source>System</source>
         <translation>系统</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="605"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="635"/>
         <source>Sound</source>
         <translation>声音</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="655"/>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="705"/>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="726"/>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="761"/>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="782"/>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="805"/>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="825"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="685"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="735"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="756"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="791"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="812"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="845"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="866"/>
         <source>Failed</source>
         <translation>失败</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="656"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="686"/>
         <source>Delete shortcut failed,error:</source>
         <translation>删除快捷键失败，错误：</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="669"/>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="744"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="699"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="774"/>
         <source>Warning</source>
-        <translation type="unfinished">警告</translation>
+        <translation>警告</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="670"/>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="745"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="700"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="775"/>
         <source>Please complete the shortcut information!</source>
         <translation>请完善快捷键信息！</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="679"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="709"/>
         <source>Set shortcut</source>
         <translation>设置快捷键</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="680"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="710"/>
         <source>Are you sure you want to disable this shortcut?</source>
         <translation>是否确定要禁用此快捷键？</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="706"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="736"/>
         <source>Modify system shortcut failed,error:</source>
         <translation>修改系统快捷键失败，错误：</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="727"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="757"/>
         <source>Modify custom shortcut failed,error:</source>
         <translation>修改自定义快捷键失败，错误：</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="762"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="792"/>
         <source>Add custom shortcut failed,error:</source>
         <translation>添加自定义快捷键失败，错误：</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="783"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="813"/>
         <source>Reset shortcut failed,error:</source>
         <translation>重置快捷键失败，错误：</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="806"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="846"/>
+        <source>Cannot use shortcut &quot;%1&quot;,Please keep pressing the modifier keys such as Ctrl,Alt, and Shift before pressing the last key of the shortcut key</source>
+        <translation>无法使用快捷键&quot;%1&quot;, 请保持按压Ctrl、Alt、Shift等修饰键后，再按压快捷键的最后一个键</translation>
+    </message>
+    <message>
         <source>Cannot use shortcut &quot;%1&quot;, Because you cannot enter with this key.Please try again using Ctrl, Alt, or Shift at the same time.</source>
-        <translation>无法使用快捷键&quot;%1&quot;,因为使用此键将无法输入，请同时使用Ctrl，Alt，Shift再试一次。</translation>
+        <translation type="vanished">无法使用快捷键&quot;%1&quot;,因为使用此键将无法输入，请同时使用Ctrl，Alt，Shift再试一次。</translation>
     </message>
     <message>
-        <location filename="../plugins/keybinding/shortcut.cpp" line="826"/>
+        <location filename="../plugins/keybinding/shortcut.cpp" line="867"/>
         <source>Shortcut keys %1 are already used in %2,Please try again!</source>
         <translation>快捷键%1已用于%2，请再试一次！</translation>
     </message>
-- 
2.33.0

