From bf22f0e150374dec879aad2d0d018671c3e6a2ef Mon Sep 17 00:00:00 2001 From: JunjieBai Date: Tue, 14 Mar 2023 16:06:37 +0800 Subject: [PATCH] Optimize the search app interface. --- libsearch/appdata/app-info-table-private.h | 15 +-- libsearch/appdata/app-info-table.cpp | 124 +++++++++++++++------ libsearch/appdata/app-info-table.h | 13 ++- libsearch/appdata/application-info.cpp | 15 ++- libsearch/appdata/application-info.h | 7 +- 5 files changed, 117 insertions(+), 57 deletions(-) diff --git a/libsearch/appdata/app-info-table-private.h b/libsearch/appdata/app-info-table-private.h index c709541..6617faf 100644 --- a/libsearch/appdata/app-info-table-private.h +++ b/libsearch/appdata/app-info-table-private.h @@ -18,17 +18,13 @@ public: AppInfoTablePrivate &operator =(const AppInfoTablePrivate &) = delete; //设置应用的置顶和收藏 - void setAppFavoritesState(QString &desktopfp, int num); - void setAppTopState(QString &desktopfp, int num); + void setAppFavoritesState(const QString &desktopfp, int num); + void setAppTopState(const QString &desktopfp, int num); //改变置顶和收藏应用位置 bool changeFavoriteAppPos(const QString &desktopfp, int pos); bool changeTopAppPos(const QString &desktopfp, int pos); - //添加快捷方式 - bool addAppShortcut2Desktop(QString &desktopfp); - bool addAppShortcut2Panel(QString &desktopfp); - //搜索接口 bool searchInstallApp(QString &keyWord, QStringList &installAppInfoRes); bool searchInstallApp(QStringList &keyWord, QStringList &installAppInfoRes); @@ -41,10 +37,9 @@ public: bool tranPidToDesktopFp(int pid, QString &desktopfp); //下面的接口都不外放,暂时没啥用 - bool setAppLaunchTimes(QString &desktopfp, size_t num); - bool updateAppLaunchTimes(QString &desktopfp); - bool setAppLockState(QString &desktopfp, size_t num); - bool getAllAppDesktopList(QStringList &list); + bool setAppLaunchTimes(const QString &desktopfp, size_t num); + bool updateAppLaunchTimes(const QString &desktopfp); + bool setAppLockState(const QString &desktopfp, size_t num); private: ~AppInfoTablePrivate(); diff --git a/libsearch/appdata/app-info-table.cpp b/libsearch/appdata/app-info-table.cpp index 0fc80fa..d24b9fa 100644 --- a/libsearch/appdata/app-info-table.cpp +++ b/libsearch/appdata/app-info-table.cpp @@ -63,12 +63,12 @@ AppInfoTablePrivate::AppInfoTablePrivate(AppInfoTable *parent) : QObject(parent) } } -void AppInfoTablePrivate::setAppFavoritesState(QString &desktopfp, int num) +void AppInfoTablePrivate::setAppFavoritesState(const QString &desktopfp, int num) { m_appDBInterface->call("updateFavoritesState", desktopfp, num); } -void AppInfoTablePrivate::setAppTopState(QString &desktopfp, int num) +void AppInfoTablePrivate::setAppTopState(const QString &desktopfp, int num) { m_appDBInterface->call("updateTopState", desktopfp, num); } @@ -191,7 +191,7 @@ bool AppInfoTablePrivate::tranPidToDesktopFp(int pid, QString &desktopfp) } } -bool AppInfoTablePrivate::setAppLaunchTimes(QString &desktopfp, size_t num) +bool AppInfoTablePrivate::setAppLaunchTimes(const QString &desktopfp, size_t num) { bool res(true); if (m_database->transaction()) { @@ -217,7 +217,7 @@ bool AppInfoTablePrivate::setAppLaunchTimes(QString &desktopfp, size_t num) return res; } -bool AppInfoTablePrivate::updateAppLaunchTimes(QString &desktopfp) +bool AppInfoTablePrivate::updateAppLaunchTimes(const QString &desktopfp) { bool res(true); if (m_database->transaction()) { @@ -254,7 +254,7 @@ bool AppInfoTablePrivate::updateAppLaunchTimes(QString &desktopfp) return res; } -bool AppInfoTablePrivate::setAppLockState(QString &desktopfp, size_t num) +bool AppInfoTablePrivate::setAppLockState(const QString &desktopfp, size_t num) { bool res(true); if (m_database->transaction()) { @@ -279,24 +279,6 @@ bool AppInfoTablePrivate::setAppLockState(QString &desktopfp, size_t num) return res; } -bool AppInfoTablePrivate::getAllAppDesktopList(QStringList &list) -{ - bool res(true); - QSqlQuery sql(*m_database); - QString cmd = QString("SELECT DESKTOP_FILE_PATH FROM APPINFO"); - - 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; -} - AppInfoTablePrivate::~AppInfoTablePrivate() { this->closeDataBase(); @@ -426,9 +408,9 @@ bool AppInfoTable::query(ApplicationInfoMap &infoMap, Properties properties, Pro QString condition; for (const ApplicationProperty::Property prop: restrictions.keys()) { - condition.append(ApplicationPropertyHelper(prop).dataBaseField() + "=? AND"); + condition.append(ApplicationPropertyHelper(prop).dataBaseField() + "=? AND "); } - condition = condition.left(condition.lastIndexOf(" AND")); + condition = condition.left(condition.lastIndexOf(" AND ")); QSqlQuery query(*d->m_database); query.setForwardOnly(true); @@ -454,24 +436,38 @@ bool AppInfoTable::query(ApplicationInfoMap &infoMap, Properties properties, Pro return true; } -bool AppInfoTable::query(ApplicationInfoMap &infoMap, Properties properties, const QString &keyword) +bool AppInfoTable::query(ApplicationInfoMap &infoMap, Properties properties, const QString &keyword, 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 "); + } + 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); + cmd = QString("SELECT %0 FROM APPINFO WHERE %1(LOCAL_NAME || NAME_EN || NAME_ZH || FIRST_LETTER_OF_PINYIN) LIKE ?") + .arg(field) + .arg(condition); } else { - cmd = QString("SELECT %0 FROM APPINFO WHERE (LOCAL_NAME || NAME_EN || NAME_ZH || PINYIN_NAME || FIRST_LETTER_OF_PINYIN) LIKE :keyword").arg(field); + cmd = QString("SELECT %0 FROM APPINFO WHERE %1(LOCAL_NAME || NAME_EN || NAME_ZH || PINYIN_NAME || FIRST_LETTER_OF_PINYIN) LIKE ?") + .arg(field) + .arg(condition); } QSqlQuery query(*d->m_database); query.setForwardOnly(true); query.prepare(cmd); - query.bindValue(":keyword", "%" + keyword + "%"); + int count = 0; + for (const QVariant &conditionValue : restrictions) { + query.bindValue(count, conditionValue); + count++; + } + query.bindValue(count, "%" + keyword + "%"); if (!query.exec()) { qWarning() << d->m_database->lastError() << query.lastError(); return false; @@ -487,12 +483,72 @@ bool AppInfoTable::query(ApplicationInfoMap &infoMap, Properties properties, con return true; } -void AppInfoTable::setAppFavoritesState(QString &desktopfp, size_t num) +bool AppInfoTable::query(ApplicationInfoMap &infoMap, Properties properties, const QStringList &keywords, 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 "); + } + + QString cmd; + if (keywords.at(0).size() < 2) { + cmd = QString("SELECT %0 FROM APPINFO WHERE %1(LOCAL_NAME || NAME_EN || NAME_ZH || FIRST_LETTER_OF_PINYIN) LIKE ?") + .arg(field) + .arg(condition); + } else { + cmd = QString("SELECT %0 FROM APPINFO WHERE %1(LOCAL_NAME || NAME_EN || NAME_ZH || PINYIN_NAME || FIRST_LETTER_OF_PINYIN) LIKE ?") + .arg(field) + .arg(condition); + } + for (int i = 1; i < keywords.size(); i++) { + if (keywords.at(i).size() < 2) { + cmd += " AND (LOCAL_NAME || NAME_EN || NAME_ZH || FIRST_LETTER_OF_PINYIN) LIKE ?"; + } else { + cmd += " AND (LOCAL_NAME || NAME_EN || NAME_ZH || FIRST_LETTER_OF_PINYIN || PINYIN_NAME) LIKE ?"; + } + } + cmd += QString(" ORDER BY LENGTH(LOCAL_NAME)"); + + QSqlQuery query(*d->m_database); + query.setForwardOnly(true); + query.prepare(cmd); + + int count = 0; + for (const QVariant &conditionValue : restrictions) { + query.bindValue(count, conditionValue); + count++; + } + + for (int i = 0; i < keywords.size(); i++) { + query.bindValue(count + i, "%" + keywords.at(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; +} + +void AppInfoTable::setAppFavoritesState(const QString &desktopfp, size_t num) { return d->setAppFavoritesState(desktopfp, num); } -void AppInfoTable::setAppTopState(QString &desktopfp, size_t num) +void AppInfoTable::setAppTopState(const QString &desktopfp, size_t num) { return d->setAppTopState(desktopfp, num); } @@ -528,17 +584,17 @@ bool AppInfoTable::tranPidToDesktopFp(int pid, QString &desktopfp) } //下面接口暂时没啥用,不外放。 -bool AppInfoTable::setAppLaunchTimes(QString &desktopfp, size_t num) +bool AppInfoTable::setAppLaunchTimes(const QString &desktopfp, size_t num) { return d->setAppLaunchTimes(desktopfp, num); } -bool AppInfoTable::updateAppLaunchTimes(QString &desktopfp) +bool AppInfoTable::updateAppLaunchTimes(const QString &desktopfp) { return d->updateAppLaunchTimes(desktopfp); } -bool AppInfoTable::setAppLockState(QString &desktopfp, size_t num) +bool AppInfoTable::setAppLockState(const QString &desktopfp, size_t num) { return d->setAppLockState(desktopfp, num); } diff --git a/libsearch/appdata/app-info-table.h b/libsearch/appdata/app-info-table.h index 66ba2ac..4062e17 100644 --- a/libsearch/appdata/app-info-table.h +++ b/libsearch/appdata/app-info-table.h @@ -23,7 +23,8 @@ public: 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); + bool query(ApplicationInfoMap &infoMap, Properties properties, const QString &keyword, PropertyMap restrictions); + bool query(ApplicationInfoMap &infoMap, Properties properties, const QStringList &keywords, PropertyMap restrictions); /** * @brief AppInfoTable::setAppFavoritesState @@ -31,7 +32,7 @@ public: * @param desktopfp: the desktop file path of app * @param num: the favorites app's order(from 1) */ - void setAppFavoritesState(QString &desktopfp, size_t num); + void setAppFavoritesState(const QString &desktopfp, size_t num); /** * @brief AppInfoTable::setAppTopState @@ -39,7 +40,7 @@ public: * @param desktopfp: the desktop file path of app * @param num: the top app's order(from 1) */ - void setAppTopState(QString &desktopfp, size_t num); + void setAppTopState(const QString &desktopfp, size_t num); /** * @brief AppInfoTable::changeFavoriteAppPos @@ -80,9 +81,9 @@ public: private: //暂不外放的接口 - bool setAppLaunchTimes(QString &desktopfp, size_t num); - bool setAppLockState(QString &desktopfp, size_t num); - bool updateAppLaunchTimes(QString &desktopfp); + bool setAppLaunchTimes(const QString &desktopfp, size_t num); + bool setAppLockState(const QString &desktopfp, size_t num); + bool updateAppLaunchTimes(const QString &desktopfp); private: explicit AppInfoTable(QObject *parent = nullptr); diff --git a/libsearch/appdata/application-info.cpp b/libsearch/appdata/application-info.cpp index 8a02318..7236f79 100644 --- a/libsearch/appdata/application-info.cpp +++ b/libsearch/appdata/application-info.cpp @@ -52,19 +52,26 @@ ApplicationInfoMap ApplicationInfo::getInfo(Properties properties, PropertyMap r return infoMap; } -ApplicationInfoMap ApplicationInfo::searchApp(Properties properties, QString &keyWord) +ApplicationInfoMap ApplicationInfo::searchApp(Properties properties, const QString &keyword, PropertyMap restrictions) { ApplicationInfoMap infoMap; - AppInfoTable::self()->query(infoMap, properties, keyWord); + AppInfoTable::self()->query(infoMap, properties, keyword, restrictions); return infoMap; } -void ApplicationInfo::setAppFavoritesState(QString &desktopFilePath, size_t num) +ApplicationInfoMap ApplicationInfo::searchApp(Properties properties, const QStringList &keywords, PropertyMap restrictions) +{ + ApplicationInfoMap infoMap; + AppInfoTable::self()->query(infoMap, properties, keywords, restrictions); + return infoMap; +} + +void ApplicationInfo::setAppFavoritesState(const QString &desktopFilePath, size_t num) { AppInfoTable::self()->setAppFavoritesState(desktopFilePath, num); } -void ApplicationInfo::setAppTopState(QString &desktopFilePath, size_t num) +void ApplicationInfo::setAppTopState(const QString &desktopFilePath, size_t num) { AppInfoTable::self()->setAppTopState(desktopFilePath, num); } diff --git a/libsearch/appdata/application-info.h b/libsearch/appdata/application-info.h index a0d3dc3..f5c4484 100644 --- a/libsearch/appdata/application-info.h +++ b/libsearch/appdata/application-info.h @@ -48,7 +48,8 @@ public: * @param installAppInfoRes: the search results of applications * @return ApplicationInfoMap: the search result */ - ApplicationInfoMap searchApp(Properties properties, QString &keyWord); + ApplicationInfoMap searchApp(Properties properties, const QString &keyword, PropertyMap restrictions); + ApplicationInfoMap searchApp(Properties properties, const QStringList &keywords, PropertyMap restrictions); /** * @brief ApplicationInfo::setAppFavoritesState @@ -56,7 +57,7 @@ public: * @param desktopFilePath: the desktop file path of app * @param num: the favorites app's order(from 1) */ - void setAppFavoritesState(QString &desktopFilePath, size_t num); + void setAppFavoritesState(const QString &desktopFilePath, size_t num); /** * @brief ApplicationInfo::setAppTopState @@ -64,7 +65,7 @@ public: * @param desktopFilePath: the desktop file path of app * @param num: the top app's order(from 1) */ - void setAppTopState(QString &desktopFilePath, size_t num); + void setAppTopState(const QString &desktopFilePath, size_t num); /** * @brief ApplicationInfo::changeFavoriteAppPos