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

View File

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

View File

@ -98,7 +98,7 @@ QWidget *AppSearchPlugin::detailPage(const ResultInfo &ri)
m_currentActionKey = ri.actionKey; m_currentActionKey = ri.actionKey;
m_iconLabel->setPixmap(ri.icon.pixmap(120, 120)); m_iconLabel->setPixmap(ri.icon.pixmap(120, 120));
QFontMetrics fontMetrics = m_nameLabel->fontMetrics(); 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))); m_nameLabel->setText(QString("<h3 style=\"font-weight:normal;\">%1</h3>").arg(FileUtils::escapeHtml(showname)));
if(QString::compare(showname, ri.name)) { if(QString::compare(showname, ri.name)) {
m_nameLabel->setToolTip(ri.name); m_nameLabel->setToolTip(ri.name);
@ -286,38 +286,5 @@ AppSearch::~AppSearch()
void AppSearch::run() void AppSearch::run()
{ {
//These weird code is mean to be compatible with the old version UI. AppMatch::getAppMatch()->startMatchApp(m_keyword, m_uniqueSymbol, m_search_result);
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;
}
}
} }

View File

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

View File

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

View File

@ -74,7 +74,7 @@ QWidget *FileSearchPlugin::detailPage(const ResultInfo &ri)
m_currentActionKey = ri.actionKey; m_currentActionKey = ri.actionKey;
m_iconLabel->setPixmap(ri.icon.pixmap(120, 120)); m_iconLabel->setPixmap(ri.icon.pixmap(120, 120));
QFontMetrics fontMetrics = m_nameLabel->fontMetrics(); 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))); m_nameLabel->setText(QString("<h3 style=\"font-weight:normal;\">%1</h3>").arg(FileUtils::escapeHtml(showname)));
if(QString::compare(showname, ri.name)) { if(QString::compare(showname, ri.name)) {
m_nameLabel->setToolTip(ri.name); m_nameLabel->setToolTip(ri.name);
@ -263,7 +263,7 @@ QWidget *DirSearchPlugin::detailPage(const ResultInfo &ri)
m_currentActionKey = ri.actionKey; m_currentActionKey = ri.actionKey;
m_iconLabel->setPixmap(ri.icon.pixmap(120, 120)); m_iconLabel->setPixmap(ri.icon.pixmap(120, 120));
QFontMetrics fontMetrics = m_nameLabel->fontMetrics(); 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))); m_nameLabel->setText(QString("<h3 style=\"font-weight:normal;\">%1</h3>").arg(FileUtils::escapeHtml(showname)));
if(QString::compare(showname, ri.name)) { if(QString::compare(showname, ri.name)) {
m_nameLabel->setToolTip(ri.name); m_nameLabel->setToolTip(ri.name);

View File

@ -129,7 +129,7 @@ QWidget *SettingsSearchPlugin::detailPage(const ResultInfo &ri)
m_currentActionKey = ri.actionKey; m_currentActionKey = ri.actionKey;
m_iconLabel->setPixmap(ri.icon.pixmap(120, 120)); m_iconLabel->setPixmap(ri.icon.pixmap(120, 120));
QFontMetrics fontMetrics = m_nameLabel->fontMetrics(); 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))); m_nameLabel->setText(QString("<h3 style=\"font-weight:normal;\">%1</h3>").arg(FileUtils::escapeHtml(showname)));
if(QString::compare(showname, ri.name)) { if(QString::compare(showname, ri.name)) {
m_nameLabel->setToolTip(ri.name); m_nameLabel->setToolTip(ri.name);