From a8c400cdb0c62467bfae6ff19e7733851788769a Mon Sep 17 00:00:00 2001 From: JunjieBai Date: Wed, 23 Aug 2023 15:35:27 +0800 Subject: [PATCH] perf(app-database-service):optimize the logic of the method which is used to find desktop file. --- .../app-db-manager.cpp | 76 +++++++++++-------- 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/ukui-search-app-data-service/app-db-manager.cpp b/ukui-search-app-data-service/app-db-manager.cpp index 1ab977e..dbf247c 100644 --- a/ukui-search-app-data-service/app-db-manager.cpp +++ b/ukui-search-app-data-service/app-db-manager.cpp @@ -1426,10 +1426,18 @@ QString AppDBManager::tranPidToDesktopFp(uint pid) if (cmdlist.at(i).startsWith("-") || cmdlist.at(i).contains("%")) { continue; } + if (!QUrl(cmdlist.at(i)).isRelative()) { + continue; + } exePath = cmdlist.at(i); break; } + QString desktopFilePath; + if (exePath.isEmpty()) { + qWarning() << "Can not find the desktop file by pid:" << pid << "because of empty exePath."; + return desktopFilePath; + } QSqlQuery query(m_database); query.setForwardOnly(true); @@ -1535,43 +1543,45 @@ QString AppDBManager::tranWinIdToDesktopFilePath(const QDBusVariant &id) KWindowInfo info(id.variant().toULongLong(), NET::Properties(), NET::WM2AllProperties); QString desktopFilePath; if (info.valid()) { - desktopFilePath = this->tranPidToDesktopFp(info.pid()); - if (desktopFilePath.isEmpty()) { - QString classClass = info.windowClassClass(); //the 2nd part of WM_CLASS, specified by the application writer - QSqlQuery query(m_database); - query.setForwardOnly(true); - query.prepare("SELECT DESKTOP_FILE_PATH, START_UP_WMCLASS FROM APPINFO WHERE EXEC LIKE :classClass OR DESKTOP_FILE_PATH LIKE :classClass OR LOWER(START_UP_WMCLASS)=:windowClassClass"); - query.bindValue(":classClass", "%" + classClass + "%"); - query.bindValue(":windowClassClass", classClass.toLower()); + QString classClass = info.windowClassClass(); //the 2nd part of WM_CLASS, specified by the application writer + QSqlQuery query(m_database); + query.setForwardOnly(true); + query.prepare("SELECT DESKTOP_FILE_PATH, START_UP_WMCLASS FROM APPINFO WHERE EXEC LIKE :classClass OR DESKTOP_FILE_PATH LIKE :classClass " + "OR LOWER(START_UP_WMCLASS)=:windowClassClass OR LOWER(LOCAL_NAME)=:windowClassClass"); + query.bindValue(":classClass", "%" + classClass + "%"); + query.bindValue(":windowClassClass", classClass.toLower()); - if (query.exec()) { - QMap wmClassInfos; - while (query.next()) { - wmClassInfos[query.value("DESKTOP_FILE_PATH").toString()] = query.value("START_UP_WMCLASS").toString(); - desktopFilePath = query.value("DESKTOP_FILE_PATH").toString(); - } - //筛选后有多个结果时进一步过滤 - if (wmClassInfos.size() > 1) { - desktopFilePath.clear(); - for (auto it = wmClassInfos.constBegin(); it != wmClassInfos.constEnd(); it++) { - if (it.key().startsWith(AUTOSTART_APP_DESKTOP_PATH)) { - continue; - } - if (it.key().section("/", -1) == (classClass + ".desktop") || it.value().toLower() == classClass.toLower()) { - desktopFilePath = it.key(); - break; - } + if (query.exec()) { + QMap wmClassInfos; + while (query.next()) { + wmClassInfos[query.value("DESKTOP_FILE_PATH").toString()] = query.value("START_UP_WMCLASS").toString(); + desktopFilePath = query.value("DESKTOP_FILE_PATH").toString(); + } + //筛选后有多个结果时进一步过滤 + if (wmClassInfos.size() > 1) { + desktopFilePath.clear(); + for (auto it = wmClassInfos.constBegin(); it != wmClassInfos.constEnd(); it++) { + if (it.key().startsWith(AUTOSTART_APP_DESKTOP_PATH)) { + continue; + } + if (it.key().section("/", -1) == (classClass + ".desktop") || it.value().toLower() == classClass.toLower()) { + desktopFilePath = it.key(); + break; } } - - if (!desktopFilePath.isEmpty()) { - qDebug() << "WId:" << id.variant() << "Classclass of window:" << classClass << "Desktop file path:" << desktopFilePath; - } else { - qWarning() << "Can not find the desktop file by windowClassClass:" << classClass; - } - } else { - qWarning() << "Fail to exec cmd" << query.lastQuery(); } + + if (!desktopFilePath.isEmpty()) { + qDebug() << "WId:" << id.variant() << "Classclass of window:" << classClass << "Desktop file path:" << desktopFilePath; + } else { + qWarning() << "Can not find the desktop file by windowClassClass:" << classClass; + } + } else { + qWarning() << "Fail to exec cmd" << query.lastQuery(); + } + + if (desktopFilePath.isEmpty()) { + desktopFilePath = this->tranPidToDesktopFp(info.pid()); } } else { qWarning() << "Cannot find desktop flie by WinId:" << id.variant() << "it is invalid" << id.variant().toULongLong();