Add a interface to set launched state.

Add a method to set value of field in database.
This commit is contained in:
JunjieBai 2023-04-14 13:45:05 +08:00
parent fbd391d110
commit 8f03c44ef4
10 changed files with 89 additions and 19 deletions

View File

@ -41,6 +41,7 @@ public:
void setAppFavoritesState(const QString &desktopfp, uint num); void setAppFavoritesState(const QString &desktopfp, uint num);
void setAppTopState(const QString &desktopfp); void setAppTopState(const QString &desktopfp);
void setAppTopState(const QString &desktopfp, uint num); void setAppTopState(const QString &desktopfp, uint num);
void setAppLaunchedState(const QString &desktopFilePath, bool launched);
//搜索接口 //搜索接口
bool searchInstallApp(QString &keyWord, QStringList &installAppInfoRes); bool searchInstallApp(QString &keyWord, QStringList &installAppInfoRes);

View File

@ -83,6 +83,15 @@ void AppInfoTablePrivate::setAppTopState(const QString &desktopfp, uint num)
m_appDBInterface->call("updateTopState", desktopfp, 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 AppInfoTablePrivate::searchInstallApp(QString &keyWord, QStringList &installAppInfoRes)
{ {
bool res(true); bool res(true);
@ -522,6 +531,11 @@ void AppInfoTable::setAppTopState(const QString &desktopfp, size_t num)
return d->setAppTopState(desktopfp, 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) void AppInfoTable::setAppTopState(const QString &desktopfp)
{ {
return d->setAppTopState(desktopfp); return d->setAppTopState(desktopfp);

View File

@ -75,6 +75,8 @@ public:
*/ */
void setAppTopState(const QString &desktopfp, size_t num); void setAppTopState(const QString &desktopfp, size_t num);
void setAppLaunchedState(const QString &desktopFilePath, bool launched);
bool searchInstallApp(QString &keyWord, QStringList &installAppInfoRes); bool searchInstallApp(QString &keyWord, QStringList &installAppInfoRes);
bool searchInstallApp(QStringList &keyWord, QStringList &installAppInfoRes); bool searchInstallApp(QStringList &keyWord, QStringList &installAppInfoRes);

View File

@ -87,6 +87,11 @@ void ApplicationInfo::setTopOfApp(const QString &desktopFilePath, size_t num)
AppInfoTable::self()->setAppTopState(desktopFilePath, 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) bool ApplicationInfo::tranPidToDesktopFp(int pid, QString &desktopFilePath)
{ {
return AppInfoTable::self()->tranPidToDesktopFp(pid, desktopFilePath); return AppInfoTable::self()->tranPidToDesktopFp(pid, desktopFilePath);

View File

@ -100,6 +100,8 @@ public:
*/ */
void setTopOfApp(const QString &desktopFilePath, size_t num); void setTopOfApp(const QString &desktopFilePath, size_t num);
void setAppLaunchedState(const QString &desktopFilePath, bool launched = true);
/** /**
* @brief ApplicationInfo::tranPid2DesktopFp * @brief ApplicationInfo::tranPid2DesktopFp
* find the desktop file path of the process which corresponds to the pid * find the desktop file path of the process which corresponds to the pid

View File

@ -60,4 +60,5 @@ typedef QMap<ApplicationProperty::Property, QVariant> ApplicationPropertyMap;
typedef QMap<QString, ApplicationPropertyMap> ApplicationInfoMap; // desktopFile->ApplicationPropertyMap typedef QMap<QString, ApplicationPropertyMap> ApplicationInfoMap; // desktopFile->ApplicationPropertyMap
} }
Q_DECLARE_METATYPE(UkuiSearch::ApplicationProperty::Property) Q_DECLARE_METATYPE(UkuiSearch::ApplicationProperty::Property)
Q_DECLARE_METATYPE(UkuiSearch::ApplicationInfoMap)
#endif // APPLICATIONPROPERTY_H #endif // APPLICATIONPROPERTY_H

View File

@ -19,6 +19,7 @@
#include "app-db-manager.h" #include "app-db-manager.h"
#include "file-utils.h" #include "file-utils.h"
#include "convert-winid-to-desktop.h" #include "convert-winid-to-desktop.h"
#include "application-property-helper.h"
#include <qt5xdg/XdgDesktopFile> #include <qt5xdg/XdgDesktopFile>
#include <QMutexLocker> #include <QMutexLocker>
@ -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 AppDBManager::tranPidToDesktopFp(int pid)
{ {
QString exePath = QFile::symLinkTarget("/proc/" + QString::number(pid) + "/exe"); 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); PendingAppInfoQueue::getAppInfoQueue().enqueue(item);
} }
void AppDBManager::udpateLockState(const QString &desktopFilePath, int num) void AppDBManager::setValue(const ApplicationInfoMap &infos2BSet)
{ {
PendingAppInfo item(desktopFilePath, PendingAppInfo::HandleType::UpdateTop); for (const QString &desktopFilePath : infos2BSet.keys()) {
item.setLock(num); PendingAppInfo item(desktopFilePath, PendingAppInfo::SetValue);
PendingAppInfoQueue::getAppInfoQueue().enqueue(item); item.setValue(infos2BSet);
PendingAppInfoQueue::getAppInfoQueue().enqueue(item);
}
} }

View File

@ -80,6 +80,8 @@ public:
bool handleLockStateUpdate(const QString &desktopFilePath, int num); bool handleLockStateUpdate(const QString &desktopFilePath, int num);
void handleDataBaseRefresh(const QStringList &appPaths, bool dbVersionNeedUpdate); void handleDataBaseRefresh(const QStringList &appPaths, bool dbVersionNeedUpdate);
bool handleValueSet(const ApplicationInfoMap appInfoMap);
public Q_SLOTS: public Q_SLOTS:
//通过pid查找对应的desktop文件 //通过pid查找对应的desktop文件
QString tranPidToDesktopFp(int pid); QString tranPidToDesktopFp(int pid);
@ -94,7 +96,8 @@ public Q_SLOTS:
void updateLaunchTimes(const QString &desktopFilePath); void updateLaunchTimes(const QString &desktopFilePath);
void updateFavoritesState(const QString &desktopFilePath, uint num = 1); void updateFavoritesState(const QString &desktopFilePath, uint num = 1);
void updateTopState(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: protected:
void run() override; void run() override;

View File

@ -50,6 +50,8 @@ void PendingAppInfoQueue::enqueue(const PendingAppInfo &appInfo)
if (appInfo.handleType() == PendingAppInfo::Insert) { if (appInfo.handleType() == PendingAppInfo::Insert) {
m_cache << appInfo; m_cache << appInfo;
} }
} else if (appInfo.handleType() == PendingAppInfo::SetValue) {
m_cache << appInfo;
} else if (m_cache[index].handleType() <= PendingAppInfo::UpdateLocaleData } else if (m_cache[index].handleType() <= PendingAppInfo::UpdateLocaleData
and appInfo.handleType() <= PendingAppInfo::UpdateLocaleData) { and appInfo.handleType() <= PendingAppInfo::UpdateLocaleData) {
//类型为insert, updateall, updatelocaledata时设置为优先级高的操作类型 //类型为insert, updateall, updatelocaledata时设置为优先级高的操作类型
@ -148,7 +150,7 @@ void PendingAppInfoQueue::processCache()
for (const PendingAppInfo &info : m_pendingAppInfos) { for (const PendingAppInfo &info : m_pendingAppInfos) {
PendingAppInfo::HandleTypes handleTypes = info.handleType(); PendingAppInfo::HandleTypes handleTypes = info.handleType();
if (handleTypes <= PendingAppInfo::UpdateLocaleData || handleTypes == PendingAppInfo::RefreshDataBase) { if (handleTypes <= PendingAppInfo::UpdateLocaleData || handleTypes == PendingAppInfo::RefreshDataBase || handleTypes == PendingAppInfo::SetValue) {
switch (handleTypes) { switch (handleTypes) {
case PendingAppInfo::Delete: case PendingAppInfo::Delete:
AppDBManager::getInstance()->handleDBItemDelete(info.path()); AppDBManager::getInstance()->handleDBItemDelete(info.path());
@ -165,6 +167,9 @@ void PendingAppInfoQueue::processCache()
case PendingAppInfo::RefreshDataBase: case PendingAppInfo::RefreshDataBase:
AppDBManager::getInstance()->handleDataBaseRefresh(info.pathsNeedRefreshData(), info.dbVersionNeedUpdate()); AppDBManager::getInstance()->handleDataBaseRefresh(info.pathsNeedRefreshData(), info.dbVersionNeedUpdate());
break; break;
case PendingAppInfo::SetValue:
AppDBManager::getInstance()->handleValueSet(info.value2BSet());
break;
default: default:
break; break;
} }
@ -187,9 +192,6 @@ void PendingAppInfoQueue::processCache()
if (handleTypes & PendingAppInfo::UpdateTop) { if (handleTypes & PendingAppInfo::UpdateTop) {
AppDBManager::getInstance()->handleTopStateUpdate(info.path(), info.topState()); AppDBManager::getInstance()->handleTopStateUpdate(info.path(), info.topState());
} }
if (handleTypes & PendingAppInfo::UpdateLock) {
AppDBManager::getInstance()->handleLockStateUpdate(info.path(), info.lockState());
}
} }
} }
Q_EMIT AppDBManager::getInstance()->finishHandleAppDB(); Q_EMIT AppDBManager::getInstance()->finishHandleAppDB();

View File

@ -23,6 +23,7 @@
#include <QString> #include <QString>
#include <QStringList> #include <QStringList>
#include <QFlags> #include <QFlags>
#include "application-property.h"
namespace UkuiSearch { namespace UkuiSearch {
@ -37,20 +38,19 @@ public:
UpdateLaunchTimes = 1u << 3, UpdateLaunchTimes = 1u << 3,
UpdateFavorites = 1u << 4, UpdateFavorites = 1u << 4,
UpdateTop = 1u << 5, UpdateTop = 1u << 5,
UpdateLock = 1u << 6, RefreshDataBase = 1u << 6,
RefreshDataBase = 1u << 7 SetValue = 1u << 7
}; };
Q_DECLARE_FLAGS(HandleTypes, HandleType) Q_DECLARE_FLAGS(HandleTypes, HandleType)
PendingAppInfo() = default; PendingAppInfo() = default;
PendingAppInfo(QString desktopfp, HandleTypes type, 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) bool addLaunch = false, int launchTimes = 0)
: m_desktopfp(desktopfp), : m_desktopfp(desktopfp),
m_handleType(type), m_handleType(type),
m_favoritesState(favorites), m_favoritesState(favorites),
m_topState(top), m_topState(top),
m_lockState(lock),
m_willAddLaunch(addLaunch), m_willAddLaunch(addLaunch),
m_launchTimes(launchTimes) {} m_launchTimes(launchTimes) {}
@ -59,21 +59,21 @@ public:
HandleTypes handleType() const {return m_handleType;} HandleTypes handleType() const {return m_handleType;}
int favoritesState() const {return m_favoritesState;} int favoritesState() const {return m_favoritesState;}
int topState() const {return m_topState;} int topState() const {return m_topState;}
int lockState() const {return m_lockState;}
int launchTimes() const {return m_launchTimes;} int launchTimes() const {return m_launchTimes;}
bool willAddLunchTimes() const {return m_willAddLaunch;} bool willAddLunchTimes() const {return m_willAddLaunch;}
bool dbVersionNeedUpdate() const {return m_dbVersionNeedUpdate;} bool dbVersionNeedUpdate() const {return m_dbVersionNeedUpdate;}
ApplicationInfoMap value2BSet() const {return m_appInfoMap;}
void setDesktopFp(const QString& desktopfp) {m_desktopfp = desktopfp;} void setDesktopFp(const QString& desktopfp) {m_desktopfp = desktopfp;}
void setHandleType(const PendingAppInfo& info) {m_handleType = info.handleType();} void setHandleType(const PendingAppInfo& info) {m_handleType = info.handleType();}
void setHandleType(HandleTypes type) {m_handleType = type;} void setHandleType(HandleTypes type) {m_handleType = type;}
void setFavorites(int favoritesState) {m_favoritesState = favoritesState;} void setFavorites(int favoritesState) {m_favoritesState = favoritesState;}
void setTop(int top) {m_topState = top;} void setTop(int top) {m_topState = top;}
void setLock(int lock) {m_lockState = lock;}
void setLaunchWillAdd(bool willAdd) {m_willAddLaunch = willAdd;} void setLaunchWillAdd(bool willAdd) {m_willAddLaunch = willAdd;}
void setLaunchTimes(int times) {m_launchTimes = times;} void setLaunchTimes(int times) {m_launchTimes = times;}
void setPathsNeedRefreshData (const QStringList& paths) {m_pathsNeedRefreshData = paths;} void setPathsNeedRefreshData (const QStringList& paths) {m_pathsNeedRefreshData = paths;}
void setDBUpdate(bool versionNeedUpdate) {m_dbVersionNeedUpdate = versionNeedUpdate;} void setDBUpdate(bool versionNeedUpdate) {m_dbVersionNeedUpdate = versionNeedUpdate;}
void setValue(const ApplicationInfoMap infoMap) {m_appInfoMap = infoMap;}
void merge(const PendingAppInfo& info) void merge(const PendingAppInfo& info)
{ {
m_handleType |= info.handleType(); m_handleType |= info.handleType();
@ -86,9 +86,6 @@ public:
m_topState = info.topState(); m_topState = info.topState();
} }
if (info.lockState() != -1) {
m_lockState = info.lockState();
}
if (info.willAddLunchTimes()) { if (info.willAddLunchTimes()) {
m_launchTimes++; m_launchTimes++;
} }
@ -102,11 +99,11 @@ private:
HandleTypes m_handleType; HandleTypes m_handleType;
int m_favoritesState = -1; int m_favoritesState = -1;
int m_topState = -1; int m_topState = -1;
int m_lockState = -1;
bool m_willAddLaunch = false; bool m_willAddLaunch = false;
int m_launchTimes = 0; int m_launchTimes = 0;
QStringList m_pathsNeedRefreshData; QStringList m_pathsNeedRefreshData;
bool m_dbVersionNeedUpdate = false; bool m_dbVersionNeedUpdate = false;
ApplicationInfoMap m_appInfoMap;
}; };
} }