Add a method which can find the desktop file for the process by pid.

This commit is contained in:
JunjieBai 2023-02-17 15:55:10 +08:00 committed by iaom
parent 68a1fc4f43
commit 04a9cb487e
5 changed files with 92 additions and 3 deletions

View File

@ -48,6 +48,9 @@ public:
//数据库错误信息 //数据库错误信息
QString lastError(void) const; QString lastError(void) const;
//通过pid查找desktop文件
bool tranPidToDesktopFp(int pid, QString &desktopfp);
//下面的接口都不外放,暂时没啥用 //下面的接口都不外放,暂时没啥用
bool setAppLaunchTimes(QString &desktopfp, size_t num); bool setAppLaunchTimes(QString &desktopfp, size_t num);
bool updateAppLaunchTimes(QString &desktopfp); bool updateAppLaunchTimes(QString &desktopfp);

View File

@ -347,6 +347,18 @@ QString AppInfoTablePrivate::lastError() const
return m_database->lastError().text(); return m_database->lastError().text();
} }
bool AppInfoTablePrivate::tranPidToDesktopFp(int pid, QString &desktopfp)
{
QDBusReply<QString> reply = m_appDBInterface->call("tranPidToDesktopFp", pid);
if (reply.isValid()) {
desktopfp = reply.value();
return true;
} else {
qDebug() << m_appDBInterface->lastError();
return false;
}
}
bool AppInfoTablePrivate::setAppLaunchTimes(QString &desktopfp, size_t num) bool AppInfoTablePrivate::setAppLaunchTimes(QString &desktopfp, size_t num)
{ {
bool res(true); bool res(true);
@ -663,6 +675,11 @@ QString AppInfoTable::lastError() const
return d->lastError(); return d->lastError();
} }
bool AppInfoTable::tranPidToDesktopFp(int pid, QString &desktopfp)
{
return d->tranPidToDesktopFp(pid, desktopfp);
}
//下面接口暂时没啥用,不外放。 //下面接口暂时没啥用,不外放。
bool AppInfoTable::setAppLaunchTimes(QString &desktopfp, size_t num) bool AppInfoTable::setAppLaunchTimes(QString &desktopfp, size_t num)
{ {

View File

@ -77,6 +77,15 @@ public:
*/ */
QString lastError(void) const; QString lastError(void) const;
/**
* @brief AppInfoTable::tranPid2DesktopFp
* find the desktop file path of the process which corresponds to the pid
* @param pid: the pid of the process which need to get its desktop file path
* @param desktopfp: the desktop file path of the process corresponding to pid
* @return bool:true if success,else false
*/
bool tranPidToDesktopFp(int pid, QString &desktopfp);
private: private:
//暂不外放的接口 //暂不外放的接口
bool setAppLaunchTimes(QString &desktopfp, size_t num); bool setAppLaunchTimes(QString &desktopfp, size_t num);

View File

@ -227,9 +227,13 @@ AppDBManager::AppDBManager(QObject *parent) : QThread(parent), m_database(QSqlDa
//监控应用进程开启 //监控应用进程开启
connect(KWindowSystem::self(), &KWindowSystem::windowAdded, [ = ](WId id) { connect(KWindowSystem::self(), &KWindowSystem::windowAdded, [ = ](WId id) {
QString desktopfp = ConvertWinidToDesktop::getConverter().tranIdToDesktop(id); KWindowInfo info = KWindowSystem::windowInfo(id, 0, NET::WM2AllProperties);
if (!desktopfp.isEmpty()) { if (info.valid()) {
this->updateLaunchTimes(desktopfp); QString desktopfp;
desktopfp = this->tranPidToDesktopFp(info.pid());
if (!desktopfp.isEmpty()) {
this->updateLaunchTimes(desktopfp);
}
} }
}); });
} else { } else {
@ -1025,6 +1029,59 @@ bool AppDBManager::createAppInfoResult(const QString &desktopfp, AppInfoResult &
return res; return res;
} }
QString AppDBManager::tranPidToDesktopFp(int pid)
{
QString exePath = QFile::symLinkTarget("/proc/" + QString::number(pid) + "/exe");
QString desktopfp;
QSqlQuery sql(m_database);
QString cmd = QString("SELECT DESKTOP_FILE_PATH, EXEC FROM APPINFO WHERE EXEC LIKE '%%0%'")
.arg(exePath.section('/', -1));
if (sql.exec(cmd)) {
QMap<QString, QString> execInfos;
while (sql.next()) {
execInfos[sql.value("DESKTOP_FILE_PATH").toString()] = sql.value("EXEC").toString();
desktopfp = sql.value("DESKTOP_FILE_PATH").toString();
}
//筛选后有多个结果时进一步过滤
if (execInfos.size() > 1) {
desktopfp.clear();
for (const QString &path : execInfos.values()) {
QStringList execlist = path.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("/") && partOfExec == exePath) ||
(partOfExec == exePath.section("/", -1))) {
desktopfp = path;
break;
}
}
if (!desktopfp.isEmpty()) {
break;
}
}
}
if (!desktopfp.isEmpty()) {
qDebug() << "PID: " << pid << "Desktop file path: " << desktopfp;
} else {
qWarning() << "The desktop file can not be found by pid: " << pid;
}
} else {
qWarning() << "Fail to exec cmd" << cmd << m_database.lastError();
}
return desktopfp;
}
void AppDBManager::insertDBItem(const QString &desktopfd) void AppDBManager::insertDBItem(const QString &desktopfd)
{ {
PendingAppInfo item(desktopfd, PendingAppInfo::HandleType::Insert); PendingAppInfo item(desktopfd, PendingAppInfo::HandleType::Insert);

View File

@ -76,6 +76,9 @@ public:
bool createAppInfoResult(const QString &desktopfp, AppInfoResult &result); bool createAppInfoResult(const QString &desktopfp, AppInfoResult &result);
public Q_SLOTS: public Q_SLOTS:
//通过pid查找对应的desktop文件
QString tranPidToDesktopFp(int pid);
//对数据库单条所有信息进行增删改 //对数据库单条所有信息进行增删改
void insertDBItem(const QString &desktopfd); void insertDBItem(const QString &desktopfd);
void updateDBItem(const QString &desktopfd); void updateDBItem(const QString &desktopfd);