更改搜索接口

This commit is contained in:
gjq 2023-03-21 14:31:02 +08:00 committed by hewenfei
parent 335638349e
commit c77c00b4c9
4 changed files with 151 additions and 96 deletions

View File

@ -18,7 +18,7 @@
#include "app-data-manager.h"
#include <UkuiSearchAppInfoTable>
#include <application-info.h>
#define APP_ICON_PREFIX "image://appicon/"
@ -39,44 +39,59 @@ Q_SIGNALS:
private Q_SLOTS:
void initAppData();
void onAppAdded(const QVector<UkuiSearch::AppInfoResult> &infos);
void onAppUpdated(QVector<UkuiSearch::AppInfoResult> infos);
void onAppAdded(const QStringList &infos);
void onAppUpdated(const UkuiSearch::ApplicationInfoMap &infos);
void onAppUpdatedAll(const QStringList &infos);
void onAppDeleted(QStringList infos);
private:
void updateFavoriteApps();
void removeApps(QStringList& appIdList, QStringList &removedIdList);
void updateApps(QVector<UkuiSearch::AppInfoResult> &infos, QList<DataEntity> &apps);
void appendApps(const QVector<UkuiSearch::AppInfoResult> &infos, QList<DataEntity> &apps);
void updateApps(const UkuiSearch::ApplicationInfoMap &infos, QList<DataEntity> &apps);
void updateAppsAll(const QStringList &infos, QList<DataEntity> &apps);
void appendApps(const QStringList &infos, QList<DataEntity> &apps);
void addInfoToApp(const UkuiSearch::ApplicationPropertyMap &info, DataEntity &app);
private:
AppDataManager *m_appManager{nullptr};
UkuiSearch::AppInfoTable *m_appInfoTable{nullptr};
UkuiSearch::ApplicationInfo *m_applicationInfo{nullptr};
UkuiSearch::ApplicationProperties m_appProperties;
UkuiSearch::ApplicationPropertyMap m_appPropertyMap;
};
AppDataWorker::AppDataWorker(AppDataManager *appManager) : QObject(nullptr), m_appManager(appManager)
{
qRegisterMetaType<QList<DataEntity> >("QList<DataEntity>");
qRegisterMetaType<QVector<DataEntity> >("QVector<DataEntity>");
m_appInfoTable = new UkuiSearch::AppInfoTable(this);
if (!m_appInfoTable || !m_appManager) {
m_applicationInfo = new UkuiSearch::ApplicationInfo(this);
if (!m_applicationInfo || !m_appManager) {
return;
}
initAppData();
connect(m_appInfoTable, &UkuiSearch::AppInfoTable::appDBItems2BAdd, this, &AppDataWorker::onAppAdded);
connect(m_appInfoTable, &UkuiSearch::AppInfoTable::appDBItems2BUpdate, this, &AppDataWorker::onAppUpdated);
connect(m_appInfoTable, &UkuiSearch::AppInfoTable::appDBItems2BDelete, this, &AppDataWorker::onAppDeleted);
connect(m_appInfoTable, &UkuiSearch::AppInfoTable::DBOpenFailed, this, &AppDataWorker::appDataBaseOpenFailed);
connect(m_applicationInfo, &UkuiSearch::ApplicationInfo::appDBItems2BAdd, this, &AppDataWorker::onAppAdded);
connect(m_applicationInfo, &UkuiSearch::ApplicationInfo::appDBItems2BUpdate, this, &AppDataWorker::onAppUpdated);
connect(m_applicationInfo, &UkuiSearch::ApplicationInfo::appDBItems2BUpdateAll, this, &AppDataWorker::onAppUpdatedAll);
connect(m_applicationInfo, &UkuiSearch::ApplicationInfo::appDBItems2BDelete, this, &AppDataWorker::onAppDeleted);
connect(m_applicationInfo, &UkuiSearch::ApplicationInfo::DBOpenFailed, this, &AppDataWorker::appDataBaseOpenFailed);
}
void AppDataWorker::initAppData()
{
QVector<UkuiSearch::AppInfoResult> appInfos;
if (!m_appInfoTable->getAppInfoResults(appInfos)) {
return;
}
m_appProperties << UkuiSearch::ApplicationProperty::Property::Top
<< UkuiSearch::ApplicationProperty::Property::Lock
<< UkuiSearch::ApplicationProperty::Property::Favorites
<< UkuiSearch::ApplicationProperty::Property::LaunchTimes
<< UkuiSearch::ApplicationProperty::Property::DesktopFilePath
<< UkuiSearch::ApplicationProperty::Property::Icon
<< UkuiSearch::ApplicationProperty::Property::LocalName
<< UkuiSearch::ApplicationProperty::Property::Category
<< UkuiSearch::ApplicationProperty::Property::FirstLetterAll
<< UkuiSearch::ApplicationProperty::Property::DontDisplay;
m_appPropertyMap.insert(UkuiSearch::ApplicationProperty::Property::DontDisplay, 0);
UkuiSearch::ApplicationInfoMap appInfos = m_applicationInfo->getInfo(m_appProperties, m_appPropertyMap);
if (appInfos.isEmpty()) {
return;
@ -84,20 +99,9 @@ void AppDataWorker::initAppData()
for (const auto &info : appInfos) {
DataEntity app;
app.setTop(info.top);
app.setLock(info.lock == 1);
app.setFavorite(info.favorite);
app.setLaunchTimes(info.launchTimes);
app.setId(info.desktopPath);
app.setIcon(APP_ICON_PREFIX + info.iconName);
app.setName(info.appLocalName);
app.setCategory(info.category);
app.setFirstLetter(info.firstLetter);
m_appManager->m_normalApps.append(app);
addInfoToApp(info, app);
m_appManager->m_normalApps.insert(app.id(), app);
}
updateFavoriteApps();
}
@ -110,14 +114,12 @@ void AppDataWorker::updateFavoriteApps()
}
}
if (favoriteApps.isEmpty()) {
return;
}
if (!favoriteApps.isEmpty()) {
// 排序搜藏夹数据
std::sort(favoriteApps.begin(), favoriteApps.end(), [](const DataEntity& a, const DataEntity &b) {
return a.favorite() < b.favorite();
});
}
{
QMutexLocker locker(&m_appManager->m_mutex);
@ -127,7 +129,7 @@ void AppDataWorker::updateFavoriteApps()
Q_EMIT favoriteAppChanged();
}
void AppDataWorker::onAppAdded(const QVector<UkuiSearch::AppInfoResult> &infos)
void AppDataWorker::onAppAdded(const QStringList &infos)
{
if (infos.isEmpty()) {
return;
@ -135,31 +137,39 @@ void AppDataWorker::onAppAdded(const QVector<UkuiSearch::AppInfoResult> &infos)
QList<DataEntity> apps;
appendApps(infos, apps);
Q_EMIT appAdded(apps);
updateFavoriteApps();
}
void AppDataWorker::appendApps(const QVector<UkuiSearch::AppInfoResult> &infos, QList<DataEntity> &apps)
void AppDataWorker::appendApps(const QStringList &infos, QList<DataEntity> &apps)
{
QMutexLocker locker(&m_appManager->m_mutex);
for (const auto &info : infos) {
for (const QString &info : infos) {
UkuiSearch::ApplicationPropertyMap appinfo = m_applicationInfo->getInfo(info, m_appProperties);
if (appinfo.value(UkuiSearch::ApplicationProperty::Property::DontDisplay).toInt() == 0) {
DataEntity app;
app.setTop(info.top);
app.setLock(info.lock == 1);
app.setFavorite(info.favorite);
app.setLaunchTimes(info.launchTimes);
app.setId(info.desktopPath);
app.setIcon(APP_ICON_PREFIX + info.iconName);
app.setName(info.appLocalName);
app.setCategory(info.category);
app.setFirstLetter(info.firstLetter);
m_appManager->m_normalApps.append(app);
addInfoToApp(appinfo, app);
m_appManager->m_normalApps.insert(app.id(), app);
apps.append(app);
}
}
}
void AppDataWorker::onAppUpdated(QVector<UkuiSearch::AppInfoResult> infos)
void AppDataWorker::addInfoToApp(const UkuiSearch::ApplicationPropertyMap &info, DataEntity &app)
{
app.setTop(info.value(UkuiSearch::ApplicationProperty::Property::Top).toInt());
app.setLock(info.value(UkuiSearch::ApplicationProperty::Property::Lock).toInt() == 1);
app.setFavorite(info.value(UkuiSearch::ApplicationProperty::Property::Favorites).toInt());
app.setLaunchTimes(info.value(UkuiSearch::ApplicationProperty::Property::LaunchTimes).toInt());
app.setId(info.value(UkuiSearch::ApplicationProperty::Property::DesktopFilePath).toString());
app.setIcon(APP_ICON_PREFIX + info.value(UkuiSearch::ApplicationProperty::Property::Icon).toString());
app.setName(info.value(UkuiSearch::ApplicationProperty::Property::LocalName).toString());
app.setCategory(info.value(UkuiSearch::ApplicationProperty::Property::Category).toString());
app.setFirstLetter(info.value(UkuiSearch::ApplicationProperty::Property::FirstLetterAll).toString());
}
void AppDataWorker::onAppUpdated(const UkuiSearch::ApplicationInfoMap &infos)
{
if (infos.isEmpty()) {
return;
@ -172,31 +182,78 @@ void AppDataWorker::onAppUpdated(QVector<UkuiSearch::AppInfoResult> infos)
updateFavoriteApps();
}
void AppDataWorker::updateApps(QVector<UkuiSearch::AppInfoResult> &infos, QList<DataEntity> &apps)
void AppDataWorker::onAppUpdatedAll(const QStringList &infos)
{
if (infos.isEmpty()) {
return;
}
QList<DataEntity> apps;
updateAppsAll(infos, apps);
Q_EMIT appUpdated(apps);
updateFavoriteApps();
}
void AppDataWorker::updateApps(const UkuiSearch::ApplicationInfoMap &infos, QList<DataEntity> &apps)
{
QMutexLocker locker(&m_appManager->m_mutex);
for (auto &app : m_appManager->m_normalApps) {
if (infos.isEmpty()) {
return;
}
for (const QString &info : infos.keys()) {
if (m_appManager->m_normalApps.contains(info)) {
DataEntity &app = m_appManager->m_normalApps[info];
for (auto &appProperty : infos.value(info).keys()) {
switch (appProperty) {
case UkuiSearch::ApplicationProperty::Property::Top:
app.setTop(infos.value(info).value(UkuiSearch::ApplicationProperty::Property::Top).toInt());
break;
case UkuiSearch::ApplicationProperty::Property::Lock:
app.setLock(infos.value(info).value(UkuiSearch::ApplicationProperty::Property::Lock).toInt() == 1);
break;
case UkuiSearch::ApplicationProperty::Property::Favorites:
app.setFavorite(infos.value(info).value(UkuiSearch::ApplicationProperty::Property::Favorites).toInt());
break;
case UkuiSearch::ApplicationProperty::Property::LaunchTimes:
app.setLaunchTimes(infos.value(info).value(UkuiSearch::ApplicationProperty::Property::LaunchTimes).toInt());
break;
case UkuiSearch::ApplicationProperty::Property::DesktopFilePath:
app.setId(infos.value(info).value(UkuiSearch::ApplicationProperty::Property::DesktopFilePath).toString());
break;
case UkuiSearch::ApplicationProperty::Property::Icon:
app.setIcon(APP_ICON_PREFIX + infos.value(info).value(UkuiSearch::ApplicationProperty::Property::Icon).toString());
break;
case UkuiSearch::ApplicationProperty::Property::LocalName:
app.setName(infos.value(info).value(UkuiSearch::ApplicationProperty::Property::LocalName).toString());
break;
case UkuiSearch::ApplicationProperty::Property::Category:
app.setCategory(infos.value(info).value(UkuiSearch::ApplicationProperty::Property::Category).toString());
break;
case UkuiSearch::ApplicationProperty::Property::FirstLetterAll:
app.setFirstLetter(infos.value(info).value(UkuiSearch::ApplicationProperty::Property::FirstLetterAll).toString());
break;
default:
break;
}
for (int i = 0; i < infos.size(); ++i) {
const UkuiSearch::AppInfoResult &info = infos.at(i);
if (info.desktopPath == app.id()) {
app.setTop(info.top);
app.setLock(info.lock == 1);
app.setFavorite(info.favorite);
app.setLaunchTimes(info.launchTimes);
app.setIcon(APP_ICON_PREFIX + info.iconName);
app.setName(info.appLocalName);
app.setCategory(info.category);
app.setFirstLetter(info.firstLetter);
}
apps.append(app);
infos.remove(i);
break;
}
}
}
void AppDataWorker::updateAppsAll(const QStringList &infos, QList<DataEntity> &apps)
{
QMutexLocker locker(&m_appManager->m_mutex);
for (const auto &info : infos) {
if (m_appManager->m_normalApps.contains(info)) {
UkuiSearch::ApplicationPropertyMap appinfo = m_applicationInfo->getInfo(info, m_appProperties);
DataEntity &app = m_appManager->m_normalApps[info];
addInfoToApp(appinfo, app);
apps.append(app);
}
}
}
void AppDataWorker::onAppDeleted(QStringList infos)
@ -214,16 +271,11 @@ void AppDataWorker::onAppDeleted(QStringList infos)
void AppDataWorker::removeApps(QStringList &appIdList, QStringList &removedIdList)
{
QMutexLocker locker(&m_appManager->m_mutex);
QList<DataEntity> &normalApps = m_appManager->m_normalApps;
QList<DataEntity>::iterator iterator = normalApps.begin();
while (iterator != normalApps.end() && !appIdList.isEmpty()) {
if (appIdList.removeOne((*iterator).id())) {
removedIdList.append((*iterator).id());
iterator = normalApps.erase(iterator);
continue;
for (const QString &id : appIdList) {
if (m_appManager->m_normalApps.remove(id)) {
removedIdList.append(id);
}
++iterator;
}
}
@ -257,7 +309,7 @@ AppDataManager::~AppDataManager()
QList<DataEntity> AppDataManager::normalApps()
{
QMutexLocker locker(&m_mutex);
return m_normalApps;
return m_normalApps.values();
}
QVector<DataEntity> AppDataManager::favoriteApps()

View File

@ -60,7 +60,7 @@ private:
private:
QMutex m_mutex;
QThread m_workerThread;
QList<DataEntity> m_normalApps;
QMap <QString, DataEntity> m_normalApps;
QVector<DataEntity> m_favoriteApps;
};

View File

@ -50,10 +50,14 @@ AppSearchPluginPrivate::AppSearchPluginPrivate() : QThread(nullptr)
m_appSearchTask = new UkuiSearch::UkuiSearchTask(this);
m_dataQueue = m_appSearchTask->init();
m_appSearchTask->initSearchPlugin(UkuiSearch::SearchType::Application);
m_appSearchTask->initSearchPlugin(UkuiSearch::SearchProperty::SearchType::Application);
m_appSearchTask->setSearchOnlineApps(false);
m_appSearchTask->setResultDataType(UkuiSearch::SearchType::Application,
UkuiSearch::ApplicationDesktopPath | UkuiSearch::ApplicationLocalName | UkuiSearch::ApplicationIconName);
UkuiSearch::SearchResultProperties searchResultProperties;
searchResultProperties << UkuiSearch::SearchProperty::SearchResultProperty::ApplicationDesktopPath
<< UkuiSearch::SearchProperty::SearchResultProperty::ApplicationLocalName
<< UkuiSearch::SearchProperty::SearchResultProperty::ApplicationIconName;
m_appSearchTask->setResultProperties(UkuiSearch::SearchProperty::SearchType::Application, searchResultProperties);
m_timer = new QTimer;
m_timer->setInterval(3000);
@ -68,7 +72,7 @@ void AppSearchPluginPrivate::startSearch(QString &keyword)
m_appSearchTask->clearKeyWords();
m_appSearchTask->addKeyword(keyword);
m_searchId = m_appSearchTask->startSearch(UkuiSearch::SearchType::Application);
m_searchId = m_appSearchTask->startSearch(UkuiSearch::SearchProperty::SearchType::Application);
}
void AppSearchPluginPrivate::stopSearch()
@ -81,7 +85,7 @@ void AppSearchPluginPrivate::run()
{
while (!isInterruptionRequested()) {
UkuiSearch::ResultItem result = m_dataQueue->tryDequeue();
if(result.getSearchId() == 0 && result.getItemKey().isEmpty() && result.getExtral().isEmpty()) {
if(result.getSearchId() == 0 && result.getItemKey().isEmpty() && result.getAllValue().isEmpty()) {
if(!m_timer->isActive()) {
// 超时退出
m_timer->start();
@ -90,12 +94,11 @@ void AppSearchPluginPrivate::run()
} else {
m_timer->stop();
if (result.getSearchId() == m_searchId) {
QVariantList list = result.getExtral();
DataEntity app;
app.setType(DataType::Normal);
app.setId(list.at(0).toString());
app.setName(list.at(1).toString());
app.setIcon("image://appicon/" + list.at(2).toString());
app.setId(result.getValue(UkuiSearch::SearchProperty::ApplicationDesktopPath).toString());
app.setName(result.getValue(UkuiSearch::SearchProperty::ApplicationLocalName).toString());
app.setIcon("image://appicon/" + result.getValue(UkuiSearch::SearchProperty::ApplicationIconName).toString());
Q_EMIT this->searchedOne(app);
}

View File

@ -22,7 +22,7 @@
#include "commons.h"
#include <QDebug>
#include <QAbstractListModel>
#include <UkuiSearchAppInfoTable>
#include <application-info.h>
namespace UkuiMenu {
@ -48,7 +48,7 @@ public Q_SLOTS:
private:
QVector<DataEntity> m_favoriteAppsData;
UkuiSearch::AppInfoTable *m_appInfoTable = nullptr;
UkuiSearch::ApplicationInfo *m_appInfoTable = nullptr;
};
FavoriteExtension::FavoriteExtension(QObject *parent) : MenuExtensionIFace(parent)
@ -109,7 +109,7 @@ void FavoriteExtension::updateFavoriteData()
FavoriteAppsModel::FavoriteAppsModel(QObject *parent) : QAbstractListModel(parent)
{
m_appInfoTable = new UkuiSearch::AppInfoTable(this);
m_appInfoTable = new UkuiSearch::ApplicationInfo(this);
}
int FavoriteAppsModel::rowCount(const QModelIndex &parent) const