From b0f10f121931ba6fd3809e4b9fb6d0592700752c Mon Sep 17 00:00:00 2001 From: donghualin Date: Tue, 15 Nov 2022 01:34:52 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E6=A0=8F=E9=83=A8=E5=88=86=E5=BA=94=E7=94=A8=E5=9B=BE=E6=A0=87?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原因:系统通过Bamf服务获取到的应用的路径为空,导致显示图标异常 解决方案:从本地读取索引文件,如果读取到的应用路径错误,则通过本地来加载,找到正确的路径 Log: 修复部分应用图标显示异常的问题 Influence: 打开安全中心、企业微信、微信等应用,观察其在任务栏上的图标 Bug: https://pms.uniontech.com/bug-view-147569.html Change-Id: I3ba3dea9884808e5edc5559e48b5a272cde754ea --- src/lib/utils.cpp | 3 - src/modules/apps/desktopfilereader.cpp | 104 +++++++++++++++++++++++++ src/modules/apps/desktopfilereader.h | 51 ++++++++++++ src/modules/dock/windowidentify.cpp | 53 +++++++------ 4 files changed, 186 insertions(+), 25 deletions(-) create mode 100644 src/modules/apps/desktopfilereader.cpp create mode 100644 src/modules/apps/desktopfilereader.h diff --git a/src/lib/utils.cpp b/src/lib/utils.cpp index 32b9aa7..e75bb41 100644 --- a/src/lib/utils.cpp +++ b/src/lib/utils.cpp @@ -193,6 +193,3 @@ bool hasBeginWith(std::string const& fullString, std::string const& ending) return false; } } - - - diff --git a/src/modules/apps/desktopfilereader.cpp b/src/modules/apps/desktopfilereader.cpp new file mode 100644 index 0000000..b934b89 --- /dev/null +++ b/src/modules/apps/desktopfilereader.cpp @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2022 ~ 2022 Deepin Technology Co., Ltd. + * + * Author: donghualin + * + * Maintainer: donghualin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "desktopfilereader.h" +#include "basedir.h" + +#include + +#define BAMF_INDEX_NAME "bamf-2.index" + +DesktopFileReader *DesktopFileReader::instance() +{ + static DesktopFileReader instance; + return &instance; +} + +QString DesktopFileReader::fileName(const QString &instanceName) const +{ + for (const BamfData &lineData: m_bamfLineData) { + if (lineData.instanceName.toLower() == instanceName.toLower()) { + QString name = lineData.lineData.split("\t").first(); + return QString("%1%2").arg(lineData.directory).arg(name); + } + } + + // 如果根据instanceName没有找到,则根据空格来进行分隔 + for (const BamfData &lineData: m_bamfLineData) { + QStringList lines = lineData.lineData.split("\t"); + if (lines.size() < 2) + continue; + + QStringList cmds = lines[2].split(" "); + if (cmds.size() > 1 && cmds[1].toLower() == instanceName.toLower()) + return QString("%1%2").arg(lineData.directory).arg(lines.first()); + } + + return instanceName; +} + +DesktopFileReader::DesktopFileReader() +{ + loadDesktopFiles(); +} + +DesktopFileReader::~DesktopFileReader() +{ +} + +QStringList DesktopFileReader::applicationDirs() const +{ + std::vector appDirs = BaseDir::appDirs(); + QStringList directions; + for (std::string appDir : appDirs) + directions << appDir.c_str(); + + return directions; +} + +void DesktopFileReader::loadDesktopFiles() +{ + QStringList directions = applicationDirs(); + for (const QString &direction : directions) { + // 读取后缀名为 + QDir dir(direction); + dir.setNameFilters(QStringList() << BAMF_INDEX_NAME); + QFileInfoList fileList = dir.entryInfoList(); + if (fileList.size() == 0) + continue; + + QFileInfo fileInfo = fileList.at(0); + QFile file(fileInfo.absoluteFilePath()); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) + continue; + + QList> bamfLine; + while (!file.atEnd()) { + QString line = file.readLine(); + QStringList part = line.split("\t"); + BamfData bamf; + bamf.directory = direction; + if (part.size() > 2) + bamf.instanceName = part[2].trimmed(); + bamf.lineData = line; + m_bamfLineData << bamf; + } + } +} diff --git a/src/modules/apps/desktopfilereader.h b/src/modules/apps/desktopfilereader.h new file mode 100644 index 0000000..97a596a --- /dev/null +++ b/src/modules/apps/desktopfilereader.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2022 ~ 2022 Deepin Technology Co., Ltd. + * + * Author: donghualin + * + * Maintainer: donghualin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef DESKTOPFILEREADER_H +#define DESKTOPFILEREADER_H + +#include +#include + +struct BamfData { + QString directory; + QString instanceName; + QString lineData; +}; + +class DesktopFileReader +{ +public: + static DesktopFileReader *instance(); + QString fileName(const QString &instanceName) const; + +protected: + DesktopFileReader(); + ~DesktopFileReader(); + +private: + QStringList applicationDirs() const; + void loadDesktopFiles(); + +private: + QList m_bamfLineData; +}; + +#endif // DESKTOPFILEREADER_H diff --git a/src/modules/dock/windowidentify.cpp b/src/modules/dock/windowidentify.cpp index df7d34a..ccc7847 100644 --- a/src/modules/dock/windowidentify.cpp +++ b/src/modules/dock/windowidentify.cpp @@ -28,6 +28,7 @@ #include "basedir.h" #include "dfile.h" #include "xcbutils.h" +#include "../apps/desktopfilereader.h" #include #include @@ -474,34 +475,42 @@ AppInfo *WindowIdentify::identifyWindowByGtkAppId(Dock *_dock, WindowInfoX *winI AppInfo *WindowIdentify::identifyWindowByWmClass(Dock *_dock, WindowInfoX *winInfo, QString &innerId) { - AppInfo *ret = nullptr; WMClass wmClass = winInfo->getWMClass(); - do { - if (wmClass.instanceName.size() > 0) { - // example: - // WM_CLASS(STRING) = "Brackets", "Brackets" - // wm class instance is Brackets - // try app id org.deepin.flatdeb.brackets - ret = new AppInfo("org.deepin.flatdeb." + QString(wmClass.instanceName.c_str()).toLower()); - if (ret) - break; - - ret = new AppInfo(wmClass.instanceName.c_str()); - if (ret) - break; + if (wmClass.instanceName.size() > 0) { + // example: + // WM_CLASS(STRING) = "Brackets", "Brackets" + // wm class instance is Brackets + // try app id org.deepin.flatdeb.brackets + //ret = new AppInfo("org.deepin.flatdeb." + QString(wmClass.instanceName.c_str()).toLower()); + if (DesktopInfo("org.deepin.flatdeb." + QString(wmClass.instanceName.c_str()).toLower().toStdString()).isValidDesktop()) { + AppInfo *appInfo = new AppInfo("org.deepin.flatdeb." + QString(wmClass.instanceName.c_str()).toLower()); + innerId = appInfo->getInnerId(); + return appInfo; } - if (wmClass.className.size() > 0) { - ret = new AppInfo(wmClass.className.c_str()); - if (ret) - break; + if (DesktopInfo(wmClass.instanceName).isValidDesktop()) { + AppInfo *appInfo = new AppInfo(wmClass.instanceName.c_str()); + innerId = appInfo->getInnerId(); + return appInfo; } - } while (0); + } - if (ret) - innerId = ret->getInnerId(); + if (wmClass.className.size() > 0) { + std::string filename = wmClass.className; + bool isValid = DesktopInfo(filename).isValidDesktop(); + if (!isValid) { + filename = DesktopFileReader::instance()->fileName(wmClass.instanceName.c_str()).toStdString(); + isValid = DesktopInfo(filename).isValidDesktop(); + } - return ret; + if (isValid) { + AppInfo *appInfo = new AppInfo(filename.c_str()); + innerId = appInfo->getInnerId(); + return appInfo; + } + } + + return nullptr; } AppInfo *WindowIdentify::fixAutostartAppInfo(QString fileName)