diff --git a/libsearch/appdata/app-info-table-private.h b/libsearch/appdata/app-info-table-private.h index b842718..788a39e 100644 --- a/libsearch/appdata/app-info-table-private.h +++ b/libsearch/appdata/app-info-table-private.h @@ -48,6 +48,9 @@ public: //数据库错误信息 QString lastError(void) const; + //通过pid查找desktop文件 + bool tranPidToDesktopFp(int pid, QString &desktopfp); + //下面的接口都不外放,暂时没啥用 bool setAppLaunchTimes(QString &desktopfp, size_t num); bool updateAppLaunchTimes(QString &desktopfp); diff --git a/libsearch/appdata/app-info-table.cpp b/libsearch/appdata/app-info-table.cpp index 634f438..2a39646 100644 --- a/libsearch/appdata/app-info-table.cpp +++ b/libsearch/appdata/app-info-table.cpp @@ -347,6 +347,18 @@ QString AppInfoTablePrivate::lastError() const return m_database->lastError().text(); } +bool AppInfoTablePrivate::tranPidToDesktopFp(int pid, QString &desktopfp) +{ + QDBusReply 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 res(true); @@ -663,6 +675,11 @@ QString AppInfoTable::lastError() const return d->lastError(); } +bool AppInfoTable::tranPidToDesktopFp(int pid, QString &desktopfp) +{ + return d->tranPidToDesktopFp(pid, desktopfp); +} + //下面接口暂时没啥用,不外放。 bool AppInfoTable::setAppLaunchTimes(QString &desktopfp, size_t num) { diff --git a/libsearch/appdata/app-info-table.h b/libsearch/appdata/app-info-table.h index dbfb3ae..91cc4d2 100644 --- a/libsearch/appdata/app-info-table.h +++ b/libsearch/appdata/app-info-table.h @@ -77,6 +77,15 @@ public: */ 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: //暂不外放的接口 bool setAppLaunchTimes(QString &desktopfp, size_t num); diff --git a/ukui-search-app-data-service/app-db-manager.cpp b/ukui-search-app-data-service/app-db-manager.cpp index c11b7af..dc48f67 100644 --- a/ukui-search-app-data-service/app-db-manager.cpp +++ b/ukui-search-app-data-service/app-db-manager.cpp @@ -227,9 +227,13 @@ AppDBManager::AppDBManager(QObject *parent) : QThread(parent), m_database(QSqlDa //监控应用进程开启 connect(KWindowSystem::self(), &KWindowSystem::windowAdded, [ = ](WId id) { - QString desktopfp = ConvertWinidToDesktop::getConverter().tranIdToDesktop(id); - if (!desktopfp.isEmpty()) { - this->updateLaunchTimes(desktopfp); + KWindowInfo info = KWindowSystem::windowInfo(id, 0, NET::WM2AllProperties); + if (info.valid()) { + QString desktopfp; + desktopfp = this->tranPidToDesktopFp(info.pid()); + if (!desktopfp.isEmpty()) { + this->updateLaunchTimes(desktopfp); + } } }); } else { @@ -1025,6 +1029,59 @@ bool AppDBManager::createAppInfoResult(const QString &desktopfp, AppInfoResult & 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 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) { PendingAppInfo item(desktopfd, PendingAppInfo::HandleType::Insert); diff --git a/ukui-search-app-data-service/app-db-manager.h b/ukui-search-app-data-service/app-db-manager.h index 9eaad4b..c40f5e8 100644 --- a/ukui-search-app-data-service/app-db-manager.h +++ b/ukui-search-app-data-service/app-db-manager.h @@ -76,6 +76,9 @@ public: bool createAppInfoResult(const QString &desktopfp, AppInfoResult &result); public Q_SLOTS: + //通过pid查找对应的desktop文件 + QString tranPidToDesktopFp(int pid); + //对数据库单条所有信息进行增删改 void insertDBItem(const QString &desktopfd); void updateDBItem(const QString &desktopfd);