Optimize the application search plug-in code.

This commit is contained in:
iaom 2021-08-10 17:50:50 +08:00
parent 73ede2e42d
commit e245fccd11
7 changed files with 92 additions and 129 deletions

View File

@ -20,6 +20,7 @@
#include "app-match.h"
#include <glib.h>
#include "file-utils.h"
#include "app-search-plugin.h"
using namespace Zeeker;
static AppMatch *app_match_Class = nullptr;
@ -47,7 +48,7 @@ AppMatch::AppMatch(QObject *parent) : QThread(parent)
qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message());
}
m_interFace->setTimeout(200);
qDebug() << "AppMatch is new";
qDebug() << "AppMatch init finished.";
}
AppMatch::~AppMatch() {
@ -61,11 +62,10 @@ AppMatch::~AppMatch() {
m_watchAppDir = NULL;
}
void AppMatch::startMatchApp(QString input, QMap<NameString, QStringList> &installed, QMap<NameString, QStringList> &softwarereturn) {
m_sourceText = input;
getAppName(installed);
softWareCenterSearch(softwarereturn);
qDebug() << "match app is successful!";
void AppMatch::startMatchApp(QString input, size_t uniqueSymbol, DataQueue<SearchPluginIface::ResultInfo> *searchResult) {
appNameMatch(input, uniqueSymbol, searchResult);
slotDBusCallFinished(input, uniqueSymbol, searchResult);
qDebug() << "App match finished!";
}
/**
@ -232,8 +232,8 @@ void AppMatch::getAppName(QMap<NameString, QStringList> &installed) {
// for(i = m_installAppMap.constBegin(); i != m_installAppMap.constEnd(); ++i) {
// appNameMatch(i.key().app_name, installed);
// }
appNameMatch(installed);
qDebug() << "installed app match is successful!";
// appNameMatch(installed);
// qDebug() << "installed app match is successful!";
}
/**
@ -242,51 +242,21 @@ void AppMatch::getAppName(QMap<NameString, QStringList> &installed) {
* @param appname
*
*/
void AppMatch::appNameMatch(QString appname, QMap<NameString, QStringList> &installed) {
NameString name{appname};
QStringList list;
QStringList pinyinlist;
pinyinlist = FileUtils::findMultiToneWords(appname);
void AppMatch::appNameMatch(QString keyWord, size_t uniqueSymbol, DataQueue<SearchPluginIface::ResultInfo> *searchResult) {
QMapIterator<NameString, QStringList> iter(m_installAppMap);
while(iter.hasNext()) {
iter.next();
if(iter.key().app_name == appname) {
list = iter.value();
// list = iter.value();
// name.app_name = iter.key().app_name;
if(iter.key().app_name.contains(keyWord, Qt::CaseInsensitive)) {
SearchPluginIface::ResultInfo ri;
creatResultInfo(ri, iter, true);
if (uniqueSymbol == AppSearchPlugin::uniqueSymbol) {
searchResult->enqueue(ri);
} else {
break;
}
}
if(appname.contains(m_sourceText, Qt::CaseInsensitive)) {
// installed.insert(name,m_installAppMap.value(name));
installed.insert(name, list);
return;
}
for(int i = 0; i < pinyinlist.size() / 2; i++) {
QString shouzimu = pinyinlist.at(2 * i + 1); // 中文转首字母
if(shouzimu.contains(m_sourceText, Qt::CaseInsensitive)) {
// installed.insert(name,m_installAppMap.value(name));
installed.insert(name, list);
return;
}
if(m_sourceText.size() < 2)
return;
QString pinyin = pinyinlist.at(2 * i); // 中文转拼音
if(pinyin.contains(m_sourceText, Qt::CaseInsensitive)) {
// installed.insert(name,m_installAppMap.value(name));
installed.insert(name, list);
return;
}
}
}
void AppMatch::appNameMatch(QMap<NameString, QStringList> &installed) {
QStringList list;
NameString name;
QMapIterator<NameString, QStringList> iter(m_installAppMap);
while(iter.hasNext()) {
iter.next();
list = iter.value();
name.app_name = iter.key().app_name;
if(iter.key().app_name.contains(m_sourceText, Qt::CaseInsensitive)) {
installed.insert(name, list);
// installed.insert(name, list);
continue;
}
@ -295,15 +265,29 @@ void AppMatch::appNameMatch(QMap<NameString, QStringList> &installed) {
for(int i = 0; i < pinyinlist.size() / 2; i++) {
QString shouzimu = pinyinlist.at(2 * i + 1); // 中文转首字母
if(shouzimu.contains(m_sourceText, Qt::CaseInsensitive)) {
installed.insert(name, list);
if(shouzimu.contains(keyWord, Qt::CaseInsensitive)) {
SearchPluginIface::ResultInfo ri;
creatResultInfo(ri, iter, true);
if (uniqueSymbol == AppSearchPlugin::uniqueSymbol) {
searchResult->enqueue(ri);
} else {
break;
}
if(m_sourceText.size() < 2)
// installed.insert(name, list);
break;
}
if(keyWord.size() < 2)
break;
QString pinyin = pinyinlist.at(2 * i); // 中文转拼音
if(pinyin.contains(m_sourceText, Qt::CaseInsensitive)) {
installed.insert(name, list);
if(pinyin.contains(keyWord, Qt::CaseInsensitive)) {
SearchPluginIface::ResultInfo ri;
creatResultInfo(ri, iter, true);
if (uniqueSymbol == AppSearchPlugin::uniqueSymbol) {
searchResult->enqueue(ri);
} else {
break;
}
// installed.insert(name, list);
break;
}
}
@ -315,44 +299,43 @@ void AppMatch::softWareCenterSearch(QMap<NameString, QStringList> &softwareretur
// qWarning() << "softWareCente Dbus is timeout !";
// return;
// }
slotDBusCallFinished(softwarereturn);
// slotDBusCallFinished(softwarereturn);
qDebug() << "softWareCenter match app is successful!";
}
void AppMatch::slotDBusCallFinished(QMap<NameString, QStringList> &softwarereturn) {
QDBusReply<QList<QMap<QString, QString>>> reply = m_interFace->call("get_search_result", m_sourceText); //阻塞,直到远程方法调用完成。
void AppMatch::slotDBusCallFinished(QString keyWord, size_t uniqueSymbol, DataQueue<SearchPluginIface::ResultInfo> *searchResult) {
QDBusReply<QList<QMap<QString, QString>>> reply = m_interFace->call("get_search_result", keyWord); //阻塞,直到远程方法调用完成。
// QDBusPendingReply<QList<QMap<QString,QString>>> reply = *call;
if(reply.isValid()) {
parseSoftWareCenterReturn(reply.value(), softwarereturn);
parseSoftWareCenterReturn(reply.value(), uniqueSymbol, searchResult);
} else {
qWarning() << "value method called failed!";
qWarning() << "SoftWareCenter dbus called failed!";
}
// call->deleteLater();
}
void AppMatch::parseSoftWareCenterReturn(QList<QMap<QString, QString>> list, QMap<NameString, QStringList> &softwarereturn) {
// qWarning()<<list;
QString appname;
NameString name;
QString appicon;
QString appdiscription;
QStringList applist;
void AppMatch::parseSoftWareCenterReturn(QList<QMap<QString, QString>> list, size_t uniqueSymbol, DataQueue<SearchPluginIface::ResultInfo> *searchResult) {
qDebug() << "Begin parseSoftWareCenterReturn";
QLocale locale;
QString pkgname;
for(int i = 0; i < list.size(); i++) {
// qWarning()<<list.at(i).keys();
SearchPluginIface::ResultInfo ri;
if(locale.language() == QLocale::Chinese) {
appname = list.at(i).value("displayname_cn");
pkgname = list.at(i).value("appname");
ri.name = list.at(i).value("displayname_cn");
} else {
ri.name = list.at(i).value("appname");
}
if(locale.language() == QLocale::English) {
appname = list.at(i).value("appname");
ri.icon = !(QIcon(list.at(i).value("icon")).isNull()) ? QIcon(list.at(i).value("icon")) : QIcon(":/res/icons/desktop.png");
SearchPluginIface::DescriptionInfo di;
di.key = QString(tr("Application Description:"));
di.value = list.at(i).value("discription");
ri.description.append(di);
ri.actionKey = list.at(i).value("appname");
ri.type = 1; //1 means not installed apps.
if (uniqueSymbol == AppSearchPlugin::uniqueSymbol) {
searchResult->enqueue(ri);
} else {
break;
}
appdiscription = list.at(i).value("discription");
appicon = list.at(i).value("icon");
name.app_name = appname;
pkgname.isEmpty() ? softwarereturn.insert(name, applist << "" << appicon << "" << appdiscription) : softwarereturn.insert(name, applist << "" << appicon << pkgname << appdiscription);
applist.clear();
}
}
@ -373,7 +356,15 @@ void AppMatch::getInstalledAppsVersion(QString appname) {
// qWarning()<<"-----------------------------------------------";
// }
// });
// m_versionCommand->close();
// m_versionCommand->close();
}
void AppMatch::creatResultInfo(SearchPluginIface::ResultInfo &ri, QMapIterator<NameString, QStringList> &iter, bool isInstalled)
{
ri.icon = QIcon::fromTheme(iter.value().at(1), QIcon(":/res/icons/desktop.png"));
ri.name = iter.key().app_name;
ri.actionKey = iter.value().at(0);
ri.type = 0; //0 means installed apps.
}
void AppMatch::run() {

View File

@ -27,8 +27,8 @@
#include <QDBusInterface>
#include <QDBusReply>
#include <QtDBus>
#include <QElapsedTimer>
#include <QThread>
#include "search-plugin-iface.h"
namespace Zeeker {
class NameString {
public:
@ -55,7 +55,10 @@ class AppMatch : public QThread {
Q_OBJECT
public:
static AppMatch *getAppMatch();
void startMatchApp(QString input, QMap<NameString, QStringList> &installed, QMap<NameString, QStringList> &softwarereturn);
void startMatchApp(QString input, size_t uniqueSymbol, DataQueue<SearchPluginIface::ResultInfo> *searchResult);
protected:
void run() override;
private:
explicit AppMatch(QObject *parent = nullptr);
@ -64,17 +67,16 @@ private:
void getDesktopFilePath();
void getAppName(QMap<NameString, QStringList> &installed);
// void appNameMatch(QString appname,QString desktoppath,QString appicon);
void appNameMatch(QString appname, QMap<NameString, QStringList> &installed);
void appNameMatch(QMap<NameString, QStringList> &installed);
// void appNameMatch(QString keyWord, QString appname, QMap<NameString, QStringList> &installed);
void appNameMatch(QString keyWord, size_t uniqueSymbol, DataQueue<SearchPluginIface::ResultInfo> *searchResult);
void softWareCenterSearch(QMap<NameString, QStringList> &softwarereturn);
void parseSoftWareCenterReturn(QList<QMap<QString, QString>> list, QMap<NameString, QStringList> &softwarereturn);
void parseSoftWareCenterReturn(QList<QMap<QString, QString>> list, size_t uniqueSymbol, DataQueue<SearchPluginIface::ResultInfo> *searchResult);
void getInstalledAppsVersion(QString appname);
void creatResultInfo(SearchPluginIface::ResultInfo &ri, QMapIterator<NameString, QStringList> &iter, bool isInstalled = true);
private:
QString m_sourceText;
size_t m_uniqueSymbol;
DataQueue<SearchPluginIface::ResultInfo> *m_search_result = nullptr;
QStringList m_filePathList;
QDBusInterface *m_interFace = nullptr;
@ -82,12 +84,11 @@ private:
QMap<NameString, QStringList> m_installAppMap;
private Q_SLOTS:
void slotDBusCallFinished(QMap<NameString, QStringList> &softwarereturn);
void slotDBusCallFinished(QString keyWord, size_t uniqueSymbol, DataQueue<SearchPluginIface::ResultInfo> *searchResult);
//Q_SIGNALS:
protected:
void run() override;
};
}

View File

@ -98,7 +98,7 @@ QWidget *AppSearchPlugin::detailPage(const ResultInfo &ri)
m_currentActionKey = ri.actionKey;
m_iconLabel->setPixmap(ri.icon.pixmap(120, 120));
QFontMetrics fontMetrics = m_nameLabel->fontMetrics();
QString showname = fontMetrics.elidedText(ri.name, Qt::ElideRight, 274); //当字体长度超过215时显示为省略号
QString showname = fontMetrics.elidedText(ri.name, Qt::ElideRight, 215); //当字体长度超过215时显示为省略号
m_nameLabel->setText(QString("<h3 style=\"font-weight:normal;\">%1</h3>").arg(FileUtils::escapeHtml(showname)));
if(QString::compare(showname, ri.name)) {
m_nameLabel->setToolTip(ri.name);
@ -286,38 +286,5 @@ AppSearch::~AppSearch()
void AppSearch::run()
{
//These weird code is mean to be compatible with the old version UI.
AppMatch::getAppMatch()->startMatchApp(m_keyword, m_installed_apps, m_not_installed_apps);
QMapIterator<NameString, QStringList> i(m_installed_apps);
while (i.hasNext()) {
i.next();
SearchPluginIface::ResultInfo ri;
ri.icon = QIcon::fromTheme(i.value().at(1), QIcon(":/res/icons/desktop.png"));
ri.name = i.key().app_name;
ri.actionKey = i.value().at(0);
ri.type = 0; //0 means installed apps.
if (m_uniqueSymbol == AppSearchPlugin::uniqueSymbol) {
m_search_result->enqueue(ri);
} else {
break;
}
}
QMapIterator<NameString, QStringList> in(m_not_installed_apps);
while (in.hasNext()) {
in.next();
SearchPluginIface::ResultInfo ri;
ri.icon = QIcon::fromTheme(in.value().at(1), QIcon(":/res/icons/desktop.png"));
ri.name = in.key().app_name;
SearchPluginIface::DescriptionInfo di;
di.key = QString(tr("Application Description:"));
di.value = in.value().at(3);
ri.description.append(di);
ri.actionKey = in.value().at(2);
ri.type = 1; //1 means not installed apps.
if (m_uniqueSymbol == AppSearchPlugin::uniqueSymbol) {
m_search_result->enqueue(ri);
} else {
break;
}
}
AppMatch::getAppMatch()->startMatchApp(m_keyword, m_uniqueSymbol, m_search_result);
}

View File

@ -16,6 +16,7 @@ namespace Zeeker {
class LIBSEARCH_EXPORT AppSearchPlugin : public QObject, public SearchPluginIface
{
friend class AppSearch;
friend class AppMatch;
Q_OBJECT
public:
AppSearchPlugin(QObject *parent = nullptr);

View File

@ -21,6 +21,7 @@
*/
#include "file-utils.h"
#include <QXmlStreamReader>
#include <QMutexLocker>
using namespace Zeeker;
size_t FileUtils::_max_index_count = 0;
@ -28,6 +29,7 @@ size_t FileUtils::_current_index_count = 0;
unsigned short FileUtils::_index_status = 0;
FileUtils::SearchMethod FileUtils::searchMethod = FileUtils::SearchMethod::DIRECTSEARCH;
QMap<QString, QStringList> FileUtils::map_chinese2pinyin = QMap<QString, QStringList>();
static QMutex iconMutex;
FileUtils::FileUtils() {
}
@ -43,6 +45,7 @@ std::string FileUtils::makeDocUterm(QString path) {
* @return
*/
QIcon FileUtils::getFileIcon(const QString &uri, bool checkValid) {
QMutexLocker locker(&iconMutex);
auto file = wrapGFile(g_file_new_for_uri(uri.toUtf8().constData()));
auto info = wrapGFileInfo(g_file_query_info(file.get()->get(),
G_FILE_ATTRIBUTE_STANDARD_ICON,

View File

@ -74,7 +74,7 @@ QWidget *FileSearchPlugin::detailPage(const ResultInfo &ri)
m_currentActionKey = ri.actionKey;
m_iconLabel->setPixmap(ri.icon.pixmap(120, 120));
QFontMetrics fontMetrics = m_nameLabel->fontMetrics();
QString showname = fontMetrics.elidedText(ri.name, Qt::ElideRight, 274); //当字体长度超过215时显示为省略号
QString showname = fontMetrics.elidedText(ri.name, Qt::ElideRight, 215); //当字体长度超过215时显示为省略号
m_nameLabel->setText(QString("<h3 style=\"font-weight:normal;\">%1</h3>").arg(FileUtils::escapeHtml(showname)));
if(QString::compare(showname, ri.name)) {
m_nameLabel->setToolTip(ri.name);
@ -263,7 +263,7 @@ QWidget *DirSearchPlugin::detailPage(const ResultInfo &ri)
m_currentActionKey = ri.actionKey;
m_iconLabel->setPixmap(ri.icon.pixmap(120, 120));
QFontMetrics fontMetrics = m_nameLabel->fontMetrics();
QString showname = fontMetrics.elidedText(ri.name, Qt::ElideRight, 274); //当字体长度超过215时显示为省略号
QString showname = fontMetrics.elidedText(ri.name, Qt::ElideRight, 215); //当字体长度超过215时显示为省略号
m_nameLabel->setText(QString("<h3 style=\"font-weight:normal;\">%1</h3>").arg(FileUtils::escapeHtml(showname)));
if(QString::compare(showname, ri.name)) {
m_nameLabel->setToolTip(ri.name);

View File

@ -129,7 +129,7 @@ QWidget *SettingsSearchPlugin::detailPage(const ResultInfo &ri)
m_currentActionKey = ri.actionKey;
m_iconLabel->setPixmap(ri.icon.pixmap(120, 120));
QFontMetrics fontMetrics = m_nameLabel->fontMetrics();
QString showname = fontMetrics.elidedText(ri.name, Qt::ElideRight, 274); //当字体长度超过215时显示为省略号
QString showname = fontMetrics.elidedText(ri.name, Qt::ElideRight, 215); //当字体长度超过215时显示为省略号
m_nameLabel->setText(QString("<h3 style=\"font-weight:normal;\">%1</h3>").arg(FileUtils::escapeHtml(showname)));
if(QString::compare(showname, ri.name)) {
m_nameLabel->setToolTip(ri.name);