fix(app-database):can not find the currect desktop file by pid

use ProcessManager's dbus interface instead.
This commit is contained in:
JunjieBai 2024-02-05 17:30:36 +08:00
parent 806aa95930
commit 4f8899810d
2 changed files with 22 additions and 91 deletions

View File

@ -32,22 +32,22 @@ public:
explicit ApplicationInfo(QObject *parent = nullptr);
~ApplicationInfo();
/**
* @brief getInfo
* @param desktopFile
* @param property
* @brief ApplicationInfo::getInfo
* @param desktopFile: desktop文件路径
* @param property:
* @return
*/
QVariant getInfo(const QString &desktopFile, ApplicationProperty::Property property);
/**
* @brief getInfo
* @param desktopFile
* @param properties
* @brief ApplicationInfo::getInfo
* @param desktopFile: desktop文件路径
* @param properties:
* @return
*/
ApplicationPropertyMap getInfo(const QString &desktopFile, ApplicationProperties properties);
/**
* @brief getInfo
* @param property
* @brief ApplicationInfo::getInfo
* @param properties:
* @return
*/
ApplicationInfoMap getInfo(ApplicationProperties properties);
@ -63,8 +63,9 @@ public:
/**
* @brief ApplicationInfo::searchApp
* @param keyWord: the keyword of this search for applications
* @param installAppInfoRes: the search results of applications
* @param properties: Each application's information should contain these properties
* @param keyword: the keyword of this search for applications
* @param restrictions: The restrictions that the search results should meet(e.g. u want get the app infos whose top state is 0)
* @return ApplicationInfoMap: the search result
*/
ApplicationInfoMap searchApp(ApplicationProperties properties, const QString &keyword, ApplicationPropertyMap restrictions);
@ -73,14 +74,14 @@ public:
/**
* @brief AppInfoTable::setAppToFavorites
* set the app to favorites apps(default is at 1)
* @param desktopfp: the desktop file path of app
* @param desktopFilePath: the desktop file path of app
*/
void setAppToFavorites(const QString &desktopFilePath);
/**
* @brief AppInfoTable::setFavoritesTo
* set the favorites state of the app to num, you can also use to change the position of the app which is one of the Favorites Apps
* @param desktopfp: the desktop file path of app
* @param desktopFilePath: the desktop file path of app
* @param num: the favorites app's position(from 1). If num is 0, it will remove the app from the favorites apps
*/
void setFavoritesOfApp(const QString &desktopFilePath, size_t num);
@ -88,14 +89,14 @@ public:
/**
* @brief AppInfoTable::setAppToTop
* set the app to top apps(default is at 1)
* @param desktopfp: desktop file path of app
* @param desktopFilePath: desktop file path of app
*/
void setAppToTop(const QString &desktopFilePath);
/**
* @brief AppInfoTable::setAppTopTo
* set the top state of the app to num, you can also use to change the position of the app which is one of the Top Apps
* @param desktopfp: the desktop file path of app
* @param desktopFilePath: the desktop file path of app
* @param num: the top app's position(from 1). If num is 0, it will remove the app from the top apps
*/
void setTopOfApp(const QString &desktopFilePath, size_t num);
@ -125,7 +126,7 @@ public:
* @brief ApplicationInfo::tranWinIdToDesktopFilePath
* find the desktop file path of the process which corresponds to the winId.
* it will find through tranPidToDesktopFp method first, and then use the winId if the desktop file can not be found by pid;
* @param winId: the winId of the process which need to get its desktop file path
* @param winId: the winId of the process which need to get its desktop file path(Only for X)
* @param desktopFilePath: the desktop file path of the process corresponding to pid
* @return bool:true if success,else false
*/

View File

@ -1342,85 +1342,15 @@ bool AppDBManager::handleValueSet(const ApplicationInfoMap appInfoMap)
QString AppDBManager::tranPidToDesktopFp(uint pid)
{
QFile file("/proc/" + QString::number(pid) + "/cmdline");
file.open(QFile::ReadOnly);
QList<QByteArray> cmdlist = file.readAll().split('\0');
file.close();
cmdlist.removeAll("");
QString exePath;
for (int i = cmdlist.size() - 1; i >= 0; i--) {
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);
query.prepare("SELECT DESKTOP_FILE_PATH, EXEC FROM APPINFO WHERE EXEC LIKE :exePath");
query.bindValue(":exePath", "%" + exePath.section('/', -1) + "%");
if (query.exec()) {
QMap<QString, QString> execInfos;
while (query.next()) {
execInfos[query.value("DESKTOP_FILE_PATH").toString()] = query.value("EXEC").toString();
desktopFilePath = query.value("DESKTOP_FILE_PATH").toString();
}
//筛选后有多个结果时进一步过滤
if (execInfos.size() > 1) {
desktopFilePath.clear();
for (auto it = execInfos.constBegin(); it != execInfos.constEnd(); it++) {
if (it.key().startsWith(AUTOSTART_APP_DESKTOP_PATH + "/")) {
continue;
}
QStringList execlist = it.value().split(" ", QString::SkipEmptyParts);
for (QString &partOfExec : execlist) {
//remove the cmd option
if (partOfExec.contains("%")) {
continue;
}
//remove the " in cmd
if (partOfExec.contains("\"")) {
partOfExec.remove("\"");
}
//compare the binary path
if (partOfExec.contains("/") && exePath.contains("/")) {
if (partOfExec == exePath) {
desktopFilePath = it.key();
break;
}
} else {
if ((partOfExec == exePath.section("/", -1)) || (partOfExec.section("/", -1) == exePath)) {
desktopFilePath = it.key();
break;
}
}
}
if (!desktopFilePath.isEmpty()) {
break;
}
}
}
if (!desktopFilePath.isEmpty()) {
QDBusReply<QString> reply = m_processManagerInterface->call("GetDesktopFileByPid", pid);
if (reply.isValid()) {
desktopFilePath = reply.value();
qDebug() << "PID: " << pid << "Desktop file path: " << desktopFilePath;
} else {
qWarning() << "Can not find the desktop file of" << exePath << "by pid:" << pid;
}
} else {
qWarning() << "Fail to exec cmd" << query.lastQuery() << m_database.lastError();
qWarning() << "Cannot find desktop file by pid:" << pid << "because of " << reply.error();
}
return desktopFilePath;
}