From 8f03c44ef4c8a4610b490e4e9f3a8e36ec66209f Mon Sep 17 00:00:00 2001 From: JunjieBai Date: Fri, 14 Apr 2023 13:45:05 +0800 Subject: [PATCH] Add a interface to set launched state. Add a method to set value of field in database. --- libsearch/appdata/app-info-table-private.h | 1 + libsearch/appdata/app-info-table.cpp | 14 +++++ libsearch/appdata/app-info-table.h | 2 + libsearch/appdata/application-info.cpp | 5 ++ libsearch/appdata/application-info.h | 2 + libsearch/appdata/application-property.h | 1 + .../app-db-manager.cpp | 51 +++++++++++++++++-- ukui-search-app-data-service/app-db-manager.h | 5 +- .../pending-app-info-queue.cpp | 10 ++-- .../pending-app-info.h | 17 +++---- 10 files changed, 89 insertions(+), 19 deletions(-) diff --git a/libsearch/appdata/app-info-table-private.h b/libsearch/appdata/app-info-table-private.h index 813411e..ccc1706 100644 --- a/libsearch/appdata/app-info-table-private.h +++ b/libsearch/appdata/app-info-table-private.h @@ -41,6 +41,7 @@ public: void setAppFavoritesState(const QString &desktopfp, uint num); void setAppTopState(const QString &desktopfp); void setAppTopState(const QString &desktopfp, uint num); + void setAppLaunchedState(const QString &desktopFilePath, bool launched); //搜索接口 bool searchInstallApp(QString &keyWord, QStringList &installAppInfoRes); diff --git a/libsearch/appdata/app-info-table.cpp b/libsearch/appdata/app-info-table.cpp index eb2f19e..b20abd2 100644 --- a/libsearch/appdata/app-info-table.cpp +++ b/libsearch/appdata/app-info-table.cpp @@ -83,6 +83,15 @@ void AppInfoTablePrivate::setAppTopState(const QString &desktopfp, uint num) m_appDBInterface->call("updateTopState", desktopfp, num); } +void AppInfoTablePrivate::setAppLaunchedState(const QString &desktopFilePath, bool launched) +{ + ApplicationInfoMap infoMap; + infoMap[desktopFilePath].insert(ApplicationProperty::Launched, launched); + QVariant var; + var.setValue(infoMap); + m_appDBInterface->call("setValue", var); +} + bool AppInfoTablePrivate::searchInstallApp(QString &keyWord, QStringList &installAppInfoRes) { bool res(true); @@ -522,6 +531,11 @@ void AppInfoTable::setAppTopState(const QString &desktopfp, size_t num) return d->setAppTopState(desktopfp, num); } +void AppInfoTable::setAppLaunchedState(const QString &desktopFilePath, bool launched) +{ + return d->setAppLaunchedState(desktopFilePath, launched); +} + void AppInfoTable::setAppTopState(const QString &desktopfp) { return d->setAppTopState(desktopfp); diff --git a/libsearch/appdata/app-info-table.h b/libsearch/appdata/app-info-table.h index ebcae46..8444b3f 100644 --- a/libsearch/appdata/app-info-table.h +++ b/libsearch/appdata/app-info-table.h @@ -75,6 +75,8 @@ public: */ void setAppTopState(const QString &desktopfp, size_t num); + void setAppLaunchedState(const QString &desktopFilePath, bool launched); + bool searchInstallApp(QString &keyWord, QStringList &installAppInfoRes); bool searchInstallApp(QStringList &keyWord, QStringList &installAppInfoRes); diff --git a/libsearch/appdata/application-info.cpp b/libsearch/appdata/application-info.cpp index 7687bcb..94c0cbe 100644 --- a/libsearch/appdata/application-info.cpp +++ b/libsearch/appdata/application-info.cpp @@ -87,6 +87,11 @@ void ApplicationInfo::setTopOfApp(const QString &desktopFilePath, size_t num) AppInfoTable::self()->setAppTopState(desktopFilePath, num); } +void ApplicationInfo::setAppLaunchedState(const QString &desktopFilePath, bool launched) +{ + AppInfoTable::self()->setAppLaunchedState(desktopFilePath, launched); +} + bool ApplicationInfo::tranPidToDesktopFp(int pid, QString &desktopFilePath) { return AppInfoTable::self()->tranPidToDesktopFp(pid, desktopFilePath); diff --git a/libsearch/appdata/application-info.h b/libsearch/appdata/application-info.h index b0d9e22..d899564 100644 --- a/libsearch/appdata/application-info.h +++ b/libsearch/appdata/application-info.h @@ -100,6 +100,8 @@ public: */ void setTopOfApp(const QString &desktopFilePath, size_t num); + void setAppLaunchedState(const QString &desktopFilePath, bool launched = true); + /** * @brief ApplicationInfo::tranPid2DesktopFp * find the desktop file path of the process which corresponds to the pid diff --git a/libsearch/appdata/application-property.h b/libsearch/appdata/application-property.h index 0b8e349..c813b6c 100644 --- a/libsearch/appdata/application-property.h +++ b/libsearch/appdata/application-property.h @@ -60,4 +60,5 @@ typedef QMap ApplicationPropertyMap; typedef QMap ApplicationInfoMap; // desktopFile->ApplicationPropertyMap } Q_DECLARE_METATYPE(UkuiSearch::ApplicationProperty::Property) +Q_DECLARE_METATYPE(UkuiSearch::ApplicationInfoMap) #endif // APPLICATIONPROPERTY_H diff --git a/ukui-search-app-data-service/app-db-manager.cpp b/ukui-search-app-data-service/app-db-manager.cpp index 952a0a7..4c80c56 100644 --- a/ukui-search-app-data-service/app-db-manager.cpp +++ b/ukui-search-app-data-service/app-db-manager.cpp @@ -19,6 +19,7 @@ #include "app-db-manager.h" #include "file-utils.h" #include "convert-winid-to-desktop.h" +#include "application-property-helper.h" #include #include @@ -1355,6 +1356,46 @@ void AppDBManager::handleDataBaseRefresh(const QStringList &appPaths, bool dbVer } } +bool AppDBManager::handleValueSet(const ApplicationInfoMap appInfoMap) +{ + bool res(true); + QSqlQuery query(m_database); + + for (auto iter = appInfoMap.constBegin(); iter != appInfoMap.constEnd(); iter++) { + QString desktopFilePath = iter.key(); + ApplicationPropertyMap propMap = iter.value(); + for (auto propIter = propMap.constBegin(); propIter != propMap.constEnd(); ++propIter) { + QString field = ApplicationPropertyHelper(propIter.key()).dataBaseField(); + + query.prepare(QString("UPDATE appInfo SET MODIFYED_TIME='%0', %1=:value WHERE DESKTOP_FILE_PATH=:desktopFilePath") + .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")) + .arg(field)); + query.bindValue(":value", propIter.value()); + query.bindValue(":desktopFilePath", desktopFilePath); + + if (!this->startTransaction()) { + return false; + } + + if (!query.exec()) { + qWarning() << "Set state failed!" << query.lastError(); + res = false; + m_database.rollback(); + } else { + if (this->startCommit()) { + ApplicationInfoMap infos2BSend; + infos2BSend[desktopFilePath].insert(propIter.key(), propIter.value()); + Q_EMIT this->appDBItemUpdate(infos2BSend); + qDebug() << "app database update " << desktopFilePath << field << propIter.value() << "success!"; + } else { + res = false; + } + } + } + } + return res; +} + QString AppDBManager::tranPidToDesktopFp(int pid) { QString exePath = QFile::symLinkTarget("/proc/" + QString::number(pid) + "/exe"); @@ -1455,9 +1496,11 @@ void AppDBManager::updateTopState(const QString &desktopFilePath, uint num) PendingAppInfoQueue::getAppInfoQueue().enqueue(item); } -void AppDBManager::udpateLockState(const QString &desktopFilePath, int num) +void AppDBManager::setValue(const ApplicationInfoMap &infos2BSet) { - PendingAppInfo item(desktopFilePath, PendingAppInfo::HandleType::UpdateTop); - item.setLock(num); - PendingAppInfoQueue::getAppInfoQueue().enqueue(item); + for (const QString &desktopFilePath : infos2BSet.keys()) { + PendingAppInfo item(desktopFilePath, PendingAppInfo::SetValue); + item.setValue(infos2BSet); + PendingAppInfoQueue::getAppInfoQueue().enqueue(item); + } } diff --git a/ukui-search-app-data-service/app-db-manager.h b/ukui-search-app-data-service/app-db-manager.h index 544680c..62aaaa9 100644 --- a/ukui-search-app-data-service/app-db-manager.h +++ b/ukui-search-app-data-service/app-db-manager.h @@ -80,6 +80,8 @@ public: bool handleLockStateUpdate(const QString &desktopFilePath, int num); void handleDataBaseRefresh(const QStringList &appPaths, bool dbVersionNeedUpdate); + bool handleValueSet(const ApplicationInfoMap appInfoMap); + public Q_SLOTS: //通过pid查找对应的desktop文件 QString tranPidToDesktopFp(int pid); @@ -94,7 +96,8 @@ public Q_SLOTS: void updateLaunchTimes(const QString &desktopFilePath); void updateFavoritesState(const QString &desktopFilePath, uint num = 1); void updateTopState(const QString &desktopFilePath, uint num = 1); - void udpateLockState(const QString &desktopFilePath, int num); + + void setValue(const ApplicationInfoMap &infos2BSet); protected: void run() override; diff --git a/ukui-search-app-data-service/pending-app-info-queue.cpp b/ukui-search-app-data-service/pending-app-info-queue.cpp index 78eb404..87ee644 100644 --- a/ukui-search-app-data-service/pending-app-info-queue.cpp +++ b/ukui-search-app-data-service/pending-app-info-queue.cpp @@ -50,6 +50,8 @@ void PendingAppInfoQueue::enqueue(const PendingAppInfo &appInfo) if (appInfo.handleType() == PendingAppInfo::Insert) { m_cache << appInfo; } + } else if (appInfo.handleType() == PendingAppInfo::SetValue) { + m_cache << appInfo; } else if (m_cache[index].handleType() <= PendingAppInfo::UpdateLocaleData and appInfo.handleType() <= PendingAppInfo::UpdateLocaleData) { //类型为insert, updateall, updatelocaledata时,设置为优先级高的操作类型 @@ -148,7 +150,7 @@ void PendingAppInfoQueue::processCache() for (const PendingAppInfo &info : m_pendingAppInfos) { PendingAppInfo::HandleTypes handleTypes = info.handleType(); - if (handleTypes <= PendingAppInfo::UpdateLocaleData || handleTypes == PendingAppInfo::RefreshDataBase) { + if (handleTypes <= PendingAppInfo::UpdateLocaleData || handleTypes == PendingAppInfo::RefreshDataBase || handleTypes == PendingAppInfo::SetValue) { switch (handleTypes) { case PendingAppInfo::Delete: AppDBManager::getInstance()->handleDBItemDelete(info.path()); @@ -165,6 +167,9 @@ void PendingAppInfoQueue::processCache() case PendingAppInfo::RefreshDataBase: AppDBManager::getInstance()->handleDataBaseRefresh(info.pathsNeedRefreshData(), info.dbVersionNeedUpdate()); break; + case PendingAppInfo::SetValue: + AppDBManager::getInstance()->handleValueSet(info.value2BSet()); + break; default: break; } @@ -187,9 +192,6 @@ void PendingAppInfoQueue::processCache() if (handleTypes & PendingAppInfo::UpdateTop) { AppDBManager::getInstance()->handleTopStateUpdate(info.path(), info.topState()); } - if (handleTypes & PendingAppInfo::UpdateLock) { - AppDBManager::getInstance()->handleLockStateUpdate(info.path(), info.lockState()); - } } } Q_EMIT AppDBManager::getInstance()->finishHandleAppDB(); diff --git a/ukui-search-app-data-service/pending-app-info.h b/ukui-search-app-data-service/pending-app-info.h index f1c1e52..503c66f 100644 --- a/ukui-search-app-data-service/pending-app-info.h +++ b/ukui-search-app-data-service/pending-app-info.h @@ -23,6 +23,7 @@ #include #include #include +#include "application-property.h" namespace UkuiSearch { @@ -37,20 +38,19 @@ public: UpdateLaunchTimes = 1u << 3, UpdateFavorites = 1u << 4, UpdateTop = 1u << 5, - UpdateLock = 1u << 6, - RefreshDataBase = 1u << 7 + RefreshDataBase = 1u << 6, + SetValue = 1u << 7 }; Q_DECLARE_FLAGS(HandleTypes, HandleType) PendingAppInfo() = default; PendingAppInfo(QString desktopfp, HandleTypes type, - int favorites = -1, int top = -1, int lock = -1, + int favorites = -1, int top = -1, bool addLaunch = false, int launchTimes = 0) : m_desktopfp(desktopfp), m_handleType(type), m_favoritesState(favorites), m_topState(top), - m_lockState(lock), m_willAddLaunch(addLaunch), m_launchTimes(launchTimes) {} @@ -59,21 +59,21 @@ public: HandleTypes handleType() const {return m_handleType;} int favoritesState() const {return m_favoritesState;} int topState() const {return m_topState;} - int lockState() const {return m_lockState;} int launchTimes() const {return m_launchTimes;} bool willAddLunchTimes() const {return m_willAddLaunch;} bool dbVersionNeedUpdate() const {return m_dbVersionNeedUpdate;} + ApplicationInfoMap value2BSet() const {return m_appInfoMap;} void setDesktopFp(const QString& desktopfp) {m_desktopfp = desktopfp;} void setHandleType(const PendingAppInfo& info) {m_handleType = info.handleType();} void setHandleType(HandleTypes type) {m_handleType = type;} void setFavorites(int favoritesState) {m_favoritesState = favoritesState;} void setTop(int top) {m_topState = top;} - void setLock(int lock) {m_lockState = lock;} void setLaunchWillAdd(bool willAdd) {m_willAddLaunch = willAdd;} void setLaunchTimes(int times) {m_launchTimes = times;} void setPathsNeedRefreshData (const QStringList& paths) {m_pathsNeedRefreshData = paths;} void setDBUpdate(bool versionNeedUpdate) {m_dbVersionNeedUpdate = versionNeedUpdate;} + void setValue(const ApplicationInfoMap infoMap) {m_appInfoMap = infoMap;} void merge(const PendingAppInfo& info) { m_handleType |= info.handleType(); @@ -86,9 +86,6 @@ public: m_topState = info.topState(); } - if (info.lockState() != -1) { - m_lockState = info.lockState(); - } if (info.willAddLunchTimes()) { m_launchTimes++; } @@ -102,11 +99,11 @@ private: HandleTypes m_handleType; int m_favoritesState = -1; int m_topState = -1; - int m_lockState = -1; bool m_willAddLaunch = false; int m_launchTimes = 0; QStringList m_pathsNeedRefreshData; bool m_dbVersionNeedUpdate = false; + ApplicationInfoMap m_appInfoMap; }; }