Optimize the interfaces which help to get apps' data.

This commit is contained in:
JunjieBai 2023-03-14 14:01:28 +08:00
parent f9a638c05b
commit 0e267ac29c
6 changed files with 212 additions and 260 deletions

View File

@ -25,16 +25,6 @@ public:
bool changeFavoriteAppPos(const QString &desktopfp, int pos);
bool changeTopAppPos(const QString &desktopfp, int pos);
//获取所有应用信息
bool getAppInfoResults(QVector<AppInfoResult> &appInfoResults);
//获取单个应用的某个状态(锁定,置顶,打开状态,收藏)
bool getAppLockState(QString &desktopfp, size_t &num);
bool getAppTopState(QString &desktopfp, size_t &num);
bool getAppLaunchedState(QString &desktopfp, size_t &num);
bool getAppFavoriteState(QString &desktopfp, size_t &num);
bool getAppCategory(QString &desktopfp, QString &category);
//添加快捷方式
bool addAppShortcut2Desktop(QString &desktopfp);
bool addAppShortcut2Panel(QString &desktopfp);
@ -55,9 +45,6 @@ public:
bool updateAppLaunchTimes(QString &desktopfp);
bool setAppLockState(QString &desktopfp, size_t num);
bool getAllAppDesktopList(QStringList &list);
bool getFavoritesAppList(QStringList &list);
bool getTopAppList(QStringList &list);
bool getLaunchTimesAppList(QStringList &list);
private:
~AppInfoTablePrivate();
@ -73,9 +60,10 @@ private:
QString m_ConnectionName;
public Q_SLOTS:
void sendAppDBItemsUpdate(QVector<AppInfoResult> results);
void sendAppDBItemsAdd(QVector<AppInfoResult> results);
void sendAppDBItemsDelete(QStringList desktopfps);
void sendAppDBItemsUpdate(ApplicationInfoMap results);
void sendAppDBItemsUpdateAll(QStringList desktopFilePaths);
void sendAppDBItemsAdd(QStringList desktopFilePaths);
void sendAppDBItemsDelete(QStringList desktopFilePaths);
};

View File

@ -25,12 +25,12 @@ static AppInfoTable *global_intance = nullptr;
AppInfoTablePrivate::AppInfoTablePrivate(AppInfoTable *parent) : QObject(parent), q(parent), m_database(new QSqlDatabase())
{
//dbus接收数据库信号
qRegisterMetaType<PropertyMap>("PropertyMap");
qDBusRegisterMetaType<PropertyMap>();
qRegisterMetaType<AppInfoResult>("AppInfoResult");
qRegisterMetaType<QVector<AppInfoResult>>("QVector<AppInfoResult>");
qRegisterMetaType<ApplicationInfoMap>("ApplicationInfoMap");
qDBusRegisterMetaType<ApplicationInfoMap>();
qDBusRegisterMetaType<AppInfoResult>();
qDBusRegisterMetaType<QVector<AppInfoResult>>();
m_signalTransInterface = new QDBusInterface("com.ukui.search.appdb.service",
"/org/ukui/search/appDataBase/signalTransformer",
"org.ukui.search.signalTransformer");
@ -39,8 +39,9 @@ AppInfoTablePrivate::AppInfoTablePrivate(AppInfoTable *parent) : QObject(parent)
qCritical() << "Create privateDirWatcher Interface Failed Because: " << QDBusConnection::sessionBus().lastError();
return;
} else {
connect(m_signalTransInterface, SIGNAL(appDBItemsAdd(QVector<AppInfoResult>)), this, SLOT(sendAppDBItemsAdd(QVector<AppInfoResult>)));
connect(m_signalTransInterface, SIGNAL(appDBItemsUpdate(QVector<AppInfoResult>)), this, SLOT(sendAppDBItemsUpdate(QVector<AppInfoResult>)));
connect(m_signalTransInterface, SIGNAL(appDBItemsUpdate(ApplicationInfoMap)), this, SLOT(sendAppDBItemsUpdate(ApplicationInfoMap)));
connect(m_signalTransInterface, SIGNAL(appDBItemsUpdateAll(QStringList)), this, SLOT(sendAppDBItemsUpdateAll(QStringList)));
connect(m_signalTransInterface, SIGNAL(appDBItemsAdd(QStringList)), this, SLOT(sendAppDBItemsAdd(QStringList)));
connect(m_signalTransInterface, SIGNAL(appDBItemsDelete(QStringList)), this, SLOT(sendAppDBItemsDelete(QStringList)));
}
@ -94,93 +95,6 @@ bool AppInfoTablePrivate::changeTopAppPos(const QString &desktopfp, int pos)
}
}
bool AppInfoTablePrivate::getAppInfoResults(QVector<AppInfoResult> &appInfoResults)
{
QDBusReply<QVector<AppInfoResult>> reply = m_appDBInterface->call("getAppInfoResults");
if (reply.isValid()) {
appInfoResults = reply.value();
return true;
} else {
return false;
}
}
bool AppInfoTablePrivate::getAppLockState(QString &desktopfp, size_t &num)
{
QDBusReply<int> reply = m_appDBInterface->call("getAppLockState", desktopfp);
if (!reply.isValid()) {
qWarning() << m_appDBInterface->lastError();
return false;
} else {
num = reply.value();
if (num == -1) {
qWarning() << "There's something wrong while using database";
return false;
}
return true;
}
}
bool AppInfoTablePrivate::getAppTopState(QString &desktopfp, size_t &num)
{
QDBusReply<int> reply = m_appDBInterface->call("getAppTopState", desktopfp);
if (!reply.isValid()) {
qWarning() << m_appDBInterface->lastError();
return false;
} else {
num = reply.value();
if (num == -1) {
qWarning() << "There's something wrong while using database";
return false;
}
return true;
}
}
bool AppInfoTablePrivate::getAppLaunchedState(QString &desktopfp, size_t &num)
{
QDBusReply<int> reply = m_appDBInterface->call("getAppLaunchedState", desktopfp);
if (!reply.isValid()) {
qWarning() << m_appDBInterface->lastError();
return false;
} else {
num = reply.value();
if (num == -1) {
qWarning() << "There's something wrong while using database";
return false;
}
return true;
}
}
bool AppInfoTablePrivate::getAppFavoriteState(QString &desktopfp, size_t &num)
{
QDBusReply<int> reply = m_appDBInterface->call("getAppFavoriteState", desktopfp);
if (!reply.isValid()) {
qWarning() << m_appDBInterface->lastError();
return false;
} else {
num = reply.value();
if (num == -1) {
qWarning() << "There's something wrong while using database";
return false;
}
return true;
}
}
bool AppInfoTablePrivate::getAppCategory(QString &desktopfp, QString &category)
{
QDBusReply<QString> reply = m_appDBInterface->call("getAppCategory", desktopfp);
if (reply.isValid()) {
category = reply.value();
return true;
} else {
qDebug() << m_appDBInterface->lastError();
return false;
}
}
bool AppInfoTablePrivate::searchInstallApp(QString &keyWord, QStringList &installAppInfoRes)
{
bool res(true);
@ -383,76 +297,6 @@ bool AppInfoTablePrivate::getAllAppDesktopList(QStringList &list)
return res;
}
bool AppInfoTablePrivate::getFavoritesAppList(QStringList &list)
{
bool res(true);
QSqlQuery sql(*m_database);
QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM APPINFO WHERE FAVORITES!=0 ORDER BY FAVORITES");
if (sql.exec(cmd)) {
while (sql.next()) {
list.append(sql.value(0).toString());
}
} else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError();
res = false;
}
return res;
}
bool AppInfoTablePrivate::getTopAppList(QStringList &list)
{
bool res(true);
QSqlQuery sql(*m_database);
QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM APPINFO WHERE TOP!=0 ORDER BY TOP");
if (sql.exec(cmd)) {
while (sql.next()) {
list.append(sql.value(0).toString());
}
} else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError();
res = false;
}
return res;
}
bool AppInfoTablePrivate::getLaunchTimesAppList(QStringList &list)
{
bool res(true);
if (m_database->transaction()) {
QSqlQuery sql(*m_database);
QSqlQuery sqlque(*m_database);
QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM APPINFO ORDER BY LAUNCH_TIMES");
int count = 0;
if (sql.exec(cmd)) {
while (sql.next()) {
list.append(sql.value(0).toString());
cmd = QString("UPDATE appInfo SET TOP=%1 WHERE DESKTOP_FILE_PATH='%2'")
.arg(++count)
.arg(sql.value(0).toString());
if (!sqlque.exec(cmd)) {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError();
res = false;
break;
}
}
} else {
qWarning() << QString("cmd %0 failed!").arg(cmd) << m_database->lastError();
res = false;
}
if (!m_database->commit()) {
qWarning() << "Failed to commit !" << cmd;
m_database->rollback();
res = false;
}
} else {
qWarning() << "Failed to start transaction mode!!!";
res = false;
}
return res;
}
AppInfoTablePrivate::~AppInfoTablePrivate()
{
this->closeDataBase();
@ -494,19 +338,24 @@ void AppInfoTablePrivate::closeDataBase()
QSqlDatabase::removeDatabase(m_ConnectionName);
}
void AppInfoTablePrivate::sendAppDBItemsUpdate(QVector<AppInfoResult> results)
void AppInfoTablePrivate::sendAppDBItemsUpdate(ApplicationInfoMap results)
{
Q_EMIT q->appDBItems2BUpdate(results);
}
void AppInfoTablePrivate::sendAppDBItemsAdd(QVector<AppInfoResult> results)
void AppInfoTablePrivate::sendAppDBItemsUpdateAll(QStringList desktopFilePaths)
{
Q_EMIT q->appDBItems2BAdd(results);
Q_EMIT q->appDBItems2BUpdateAll(desktopFilePaths);
}
void AppInfoTablePrivate::sendAppDBItemsDelete(QStringList desktopfps)
void AppInfoTablePrivate::sendAppDBItemsAdd(QStringList desktopFilePaths)
{
Q_EMIT q->appDBItems2BDelete(desktopfps);
Q_EMIT q->appDBItems2BAdd(desktopFilePaths);
}
void AppInfoTablePrivate::sendAppDBItemsDelete(QStringList desktopFilePaths)
{
Q_EMIT q->appDBItems2BDelete(desktopFilePaths);
}
AppInfoTable *AppInfoTable::self()
@ -560,8 +409,78 @@ bool AppInfoTable::query(ApplicationInfoMap &infoMap, Properties properties)
}
while (query.next()) {
PropertyMap propertyMap;
for(int i = 1; i< properties.size(); i++) {
propertyMap.insert(properties.at(i), query.value(i));
for(int i = 0; i< properties.size(); i++) {
propertyMap.insert(properties.at(i), query.value(i + 1));
}
infoMap.insert(query.value(0).toString(), propertyMap);
}
return true;
}
bool AppInfoTable::query(ApplicationInfoMap &infoMap, Properties properties, PropertyMap restrictions)
{
QString field = ApplicationPropertyHelper(ApplicationProperty::Property::DesktopFilePath).dataBaseField();
for(const ApplicationProperty::Property &pro : properties) {
field.append("," + ApplicationPropertyHelper(pro).dataBaseField());
}
QString condition;
for (const ApplicationProperty::Property prop: restrictions.keys()) {
condition.append(ApplicationPropertyHelper(prop).dataBaseField() + "=? AND");
}
condition = condition.left(condition.lastIndexOf(" AND"));
QSqlQuery query(*d->m_database);
query.setForwardOnly(true);
query.prepare(QString("SELECT %0 FROM APPINFO WHERE %1")
.arg(field)
.arg(condition));
int i = 0;
for (const QVariant &conditionValue : restrictions) {
query.bindValue(i, conditionValue);
i++;
}
if (!query.exec()) {
qWarning() << d->m_database->lastError() << query.lastError();
return false;
}
while (query.next()) {
PropertyMap propertyMap;
for(int i = 0; i< properties.size(); i++) {
propertyMap.insert(properties.at(i), query.value(i + 1));
}
infoMap.insert(query.value(0).toString(), propertyMap);
}
return true;
}
bool AppInfoTable::query(ApplicationInfoMap &infoMap, Properties properties, const QString &keyword)
{
QString field(ApplicationPropertyHelper(ApplicationProperty::Property::DesktopFilePath).dataBaseField());
for(const ApplicationProperty::Property &pro : properties) {
field.append("," + ApplicationPropertyHelper(pro).dataBaseField());
}
QString cmd;
if (keyword.size() < 2) {
cmd = QString("SELECT %0 FROM APPINFO WHERE (LOCAL_NAME || NAME_EN || NAME_ZH || FIRST_LETTER_OF_PINYIN) LIKE :keyword").arg(field);
} else {
cmd = QString("SELECT %0 FROM APPINFO WHERE (LOCAL_NAME || NAME_EN || NAME_ZH || PINYIN_NAME || FIRST_LETTER_OF_PINYIN) LIKE :keyword").arg(field);
}
QSqlQuery query(*d->m_database);
query.setForwardOnly(true);
query.prepare(cmd);
query.bindValue(":keyword", "%" + keyword + "%");
if (!query.exec()) {
qWarning() << d->m_database->lastError() << query.lastError();
return false;
}
while (query.next()) {
PropertyMap propertyMap;
for(int i = 0; i< properties.size(); i++) {
propertyMap.insert(properties.at(i), query.value(i + 1));
}
infoMap.insert(query.value(0).toString(), propertyMap);
}
@ -578,11 +497,6 @@ void AppInfoTable::setAppTopState(QString &desktopfp, size_t num)
return d->setAppTopState(desktopfp, num);
}
bool AppInfoTable::getAppCategory(QString &desktopfp, QString &category)
{
return d->getAppCategory(desktopfp, category);
}
bool AppInfoTable::changeFavoriteAppPos(const QString &desktopfp, size_t pos)
{
return d->changeFavoriteAppPos(desktopfp, pos);
@ -593,31 +507,6 @@ bool AppInfoTable::changeTopAppPos(const QString &desktopfp, size_t pos)
return d->changeTopAppPos(desktopfp, pos);
}
bool AppInfoTable::getAppInfoResults(QVector<AppInfoResult> &appInfoResults)
{
return d->getAppInfoResults(appInfoResults);
}
bool AppInfoTable::getAppLockState(QString &desktopfp, size_t &num)
{
return d->getAppLockState(desktopfp, num);
}
bool AppInfoTable::getAppTopState(QString &desktopfp, size_t &num)
{
return d->getAppTopState(desktopfp, num);
}
bool AppInfoTable::getAppLaunchedState(QString &desktopfp, size_t &num)
{
return d->getAppLaunchedState(desktopfp, num);
}
bool AppInfoTable::getAppFavoriteState(QString &desktopfp, size_t &num)
{
return d->getAppFavoriteState(desktopfp, num);
}
bool AppInfoTable::searchInstallApp(QString &keyWord, QStringList &installAppInfoRes)
{
return d->searchInstallApp(keyWord, installAppInfoRes);
@ -653,23 +542,3 @@ bool AppInfoTable::setAppLockState(QString &desktopfp, size_t num)
{
return d->setAppLockState(desktopfp, num);
}
bool AppInfoTable::getAllAppDesktopList(QStringList &list)
{
return d->getAllAppDesktopList(list);
}
bool AppInfoTable::getFavoritesAppList(QStringList &list)
{
return d->getFavoritesAppList(list);
}
bool AppInfoTable::getTopAppList(QStringList &list)
{
return d->getTopAppList(list);
}
bool AppInfoTable::getLaunchTimesAppList(QStringList &list)
{
return d->getLaunchTimesAppList(list);
}

View File

@ -3,11 +3,14 @@
#include <QObject>
#include <QSqlQuery>
#include "app-db-common.h"
#include <QDir>
#include "application-property.h"
namespace UkuiSearch {
const static QString APP_DATABASE_PATH = QDir::homePath()+"/.config/org.ukui/ukui-search/appdata/";
const static QString APP_DATABASE_NAME = "app-info.db";
class AppInfoTablePrivate;
class AppInfoTable : public QObject
{
@ -19,6 +22,8 @@ public:
AppInfoTable &operator =(const AppInfoTable &) = delete;
bool query(PropertyMap &propertyMap, const QString &desktopFile, Properties properties);
bool query(ApplicationInfoMap &infoMap, Properties properties);
bool query(ApplicationInfoMap &infoMap, Properties properties, PropertyMap restrictions);
bool query(ApplicationInfoMap &infoMap, Properties properties, const QString &keyword);
/**
* @brief AppInfoTable::setAppFavoritesState
@ -53,20 +58,6 @@ public:
*/
bool changeTopAppPos(const QString &desktopfp, size_t pos);
/**
* @brief AppInfoTable::getAppInfoResults
* Get all App infos by passing AppInforesult Vector.
* @param AppInfoResults: a struct which includes all infos of each application
* @return bool: true if success, else false
*/
bool getAppInfoResults(QVector<AppInfoResult> &appInfoResults);
bool getAppLockState(QString &desktopfp, size_t &num);
bool getAppTopState(QString &desktopfp, size_t &num);
bool getAppLaunchedState(QString &desktopfp, size_t &num);
bool getAppFavoriteState(QString &desktopfp, size_t &num);
bool getAppCategory(QString &desktopfp, QString &category);
bool searchInstallApp(QString &keyWord, QStringList &installAppInfoRes);
bool searchInstallApp(QStringList &keyWord, QStringList &installAppInfoRes);
@ -92,10 +83,6 @@ private:
bool setAppLaunchTimes(QString &desktopfp, size_t num);
bool setAppLockState(QString &desktopfp, size_t num);
bool updateAppLaunchTimes(QString &desktopfp);
bool getAllAppDesktopList(QStringList &list);
bool getFavoritesAppList(QStringList &list);
bool getTopAppList(QStringList &list);
bool getLaunchTimesAppList(QStringList &list);
private:
explicit AppInfoTable(QObject *parent = nullptr);
@ -103,8 +90,9 @@ private:
Q_SIGNALS:
void DBOpenFailed();
void appDBItems2BUpdate(QVector<AppInfoResult>);
void appDBItems2BAdd(QVector<AppInfoResult>);
void appDBItems2BUpdate(ApplicationInfoMap);
void appDBItems2BUpdateAll(QStringList);
void appDBItems2BAdd(QStringList);
void appDBItems2BDelete(QStringList);
};
}

View File

@ -10,6 +10,10 @@ class ApplicationInfoPrivate
ApplicationInfo::ApplicationInfo(QObject *parent)
: QObject(parent), d(new ApplicationInfoPrivate)
{
connect(AppInfoTable::self(), &AppInfoTable::appDBItems2BUpdate, this, &ApplicationInfo::appDBItems2BUpdate);
connect(AppInfoTable::self(), &AppInfoTable::appDBItems2BUpdateAll, this, &ApplicationInfo::appDBItems2BUpdateAll);
connect(AppInfoTable::self(), &AppInfoTable::appDBItems2BAdd, this, &ApplicationInfo::appDBItems2BAdd);
connect(AppInfoTable::self(), &AppInfoTable::appDBItems2BDelete, this, &ApplicationInfo::appDBItems2BDelete);
}
ApplicationInfo::~ApplicationInfo()
@ -24,7 +28,7 @@ QVariant UkuiSearch::ApplicationInfo::getInfo(const QString &desktopFile, Applic
{
PropertyMap map;
AppInfoTable::self()->query(map, desktopFile, Properties{property});
return map.value(property);;
return map.value(property);
}
PropertyMap ApplicationInfo::getInfo(const QString &desktopFile, Properties properties)
@ -40,3 +44,42 @@ ApplicationInfoMap ApplicationInfo::getInfo(Properties properties)
AppInfoTable::self()->query(infoMap, properties);
return infoMap;
}
ApplicationInfoMap ApplicationInfo::getInfo(Properties properties, PropertyMap restrictions)
{
ApplicationInfoMap infoMap;
AppInfoTable::self()->query(infoMap, properties, restrictions);
return infoMap;
}
ApplicationInfoMap ApplicationInfo::searchApp(Properties properties, QString &keyWord)
{
ApplicationInfoMap infoMap;
AppInfoTable::self()->query(infoMap, properties, keyWord);
return infoMap;
}
void ApplicationInfo::setAppFavoritesState(QString &desktopFilePath, size_t num)
{
AppInfoTable::self()->setAppFavoritesState(desktopFilePath, num);
}
void ApplicationInfo::setAppTopState(QString &desktopFilePath, size_t num)
{
AppInfoTable::self()->setAppTopState(desktopFilePath, num);
}
bool ApplicationInfo::changeFavoriteAppPos(const QString &desktopFilePath, size_t pos)
{
return AppInfoTable::self()->changeFavoriteAppPos(desktopFilePath, pos);
}
bool ApplicationInfo::changeTopAppPos(const QString &desktopFilePath, size_t pos)
{
return AppInfoTable::self()->changeTopAppPos(desktopFilePath, pos);
}
bool ApplicationInfo::tranPidToDesktopFp(int pid, QString &desktopFilePath)
{
return AppInfoTable::self()->tranPidToDesktopFp(pid, desktopFilePath);
}

View File

@ -33,7 +33,71 @@ public:
*/
ApplicationInfoMap getInfo(Properties properties);
//TODO 搜索接口,设置收藏和置顶接口,应用更新卸载信号
/**
* @brief ApplicationInfo::getInfo
* get the application info that meets the restrictions
* @param restrictions: The restrictions that the search results should meet(e.g. u want get the app infos whose top state is 0)
* @param properties: Each application's information should contain these properties
* @return ApplicationInfoMap: the search result
*/
ApplicationInfoMap getInfo(Properties properties, PropertyMap restrictions);
/**
* @brief ApplicationInfo::searchApp
* @param keyWord: the keyword of this search for applications
* @param installAppInfoRes: the search results of applications
* @return ApplicationInfoMap: the search result
*/
ApplicationInfoMap searchApp(Properties properties, QString &keyWord);
/**
* @brief ApplicationInfo::setAppFavoritesState
* set the favorites state of the app
* @param desktopFilePath: the desktop file path of app
* @param num: the favorites app's order(from 1)
*/
void setAppFavoritesState(QString &desktopFilePath, size_t num);
/**
* @brief ApplicationInfo::setAppTopState
* set the top state of the app
* @param desktopFilePath: the desktop file path of app
* @param num: the top app's order(from 1)
*/
void setAppTopState(QString &desktopFilePath, size_t num);
/**
* @brief ApplicationInfo::changeFavoriteAppPos
* change the position of the app which is one of the Favorites Apps
* @param desktopFilePath: desktop file path of app
* @param pos: the position which the app will be changed into
*/
bool changeFavoriteAppPos(const QString &desktopFilePath, size_t pos);
/**
* @brief ApplicationInfo::changeTopAppPos
* hange the position of the app which is one of the Top Apps
* @param desktopFilePath: desktop file path of app
* @param pos: the position which the app will be changed into
* @return bool: true if success, else false
*/
bool changeTopAppPos(const QString &desktopFilePath, size_t pos);
/**
* @brief ApplicationInfo::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 desktopFilePath: the desktop file path of the process corresponding to pid
* @return bool:true if success,else false
*/
bool tranPidToDesktopFp(int pid, QString &desktopFilePath);
Q_SIGNALS:
void DBOpenFailed();
void appDBItems2BUpdate(ApplicationInfoMap);
void appDBItems2BUpdateAll(QStringList);
void appDBItems2BAdd(QStringList);
void appDBItems2BDelete(QStringList);
private:
ApplicationInfoPrivate *d = nullptr;

View File

@ -153,7 +153,7 @@ QString ApplicationPropertyHelper::toDataBaseString(QVariant &value)
case QMetaType::Type::UnknownType:
return QString();
case QMetaType::Type::QString:
return value.toString().replace("'", "''");
return value.toString();
case QMetaType::Type::QDateTime:
return value.toDateTime().toString("yyyy-MM-dd hh:mm:ss");
default: