From e245fccd110db03c9bb186bf094010c20101f90c Mon Sep 17 00:00:00 2001 From: iaom <18504285112@163.com> Date: Tue, 10 Aug 2021 17:50:50 +0800 Subject: [PATCH] Optimize the application search plug-in code. --- libsearch/appsearch/app-match.cpp | 149 ++++++++---------- libsearch/appsearch/app-match.h | 25 +-- libsearch/appsearch/app-search-plugin.cpp | 37 +---- libsearch/appsearch/app-search-plugin.h | 1 + libsearch/file-utils.cpp | 3 + libsearch/index/file-search-plugin.cpp | 4 +- .../settingsearch/settings-search-plugin.cpp | 2 +- 7 files changed, 92 insertions(+), 129 deletions(-) diff --git a/libsearch/appsearch/app-match.cpp b/libsearch/appsearch/app-match.cpp index 7d9d030..c2815b0 100644 --- a/libsearch/appsearch/app-match.cpp +++ b/libsearch/appsearch/app-match.cpp @@ -20,6 +20,7 @@ #include "app-match.h" #include #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 &installed, QMap &softwarereturn) { - m_sourceText = input; - getAppName(installed); - softWareCenterSearch(softwarereturn); - qDebug() << "match app is successful!"; +void AppMatch::startMatchApp(QString input, size_t uniqueSymbol, DataQueue *searchResult) { + appNameMatch(input, uniqueSymbol, searchResult); + slotDBusCallFinished(input, uniqueSymbol, searchResult); + qDebug() << "App match finished!"; } /** @@ -232,8 +232,8 @@ void AppMatch::getAppName(QMap &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 &installed) { * @param appname * 应用名字 */ -void AppMatch::appNameMatch(QString appname, QMap &installed) { - NameString name{appname}; - QStringList list; - QStringList pinyinlist; - pinyinlist = FileUtils::findMultiToneWords(appname); +void AppMatch::appNameMatch(QString keyWord, size_t uniqueSymbol, DataQueue *searchResult) { QMapIterator iter(m_installAppMap); while(iter.hasNext()) { iter.next(); - if(iter.key().app_name == appname) { - list = iter.value(); - 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 &installed) { - QStringList list; - NameString name; - QMapIterator 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); +// 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; + } +// installed.insert(name, list); continue; } @@ -295,15 +265,29 @@ void AppMatch::appNameMatch(QMap &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; + } +// installed.insert(name, list); break; } - if(m_sourceText.size() < 2) + 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 &softwareretur // qWarning() << "softWareCente Dbus is timeout !"; // return; // } - slotDBusCallFinished(softwarereturn); +// slotDBusCallFinished(softwarereturn); qDebug() << "softWareCenter match app is successful!"; } -void AppMatch::slotDBusCallFinished(QMap &softwarereturn) { - QDBusReply>> reply = m_interFace->call("get_search_result", m_sourceText); //阻塞,直到远程方法调用完成。 +void AppMatch::slotDBusCallFinished(QString keyWord, size_t uniqueSymbol, DataQueue *searchResult) { + QDBusReply>> reply = m_interFace->call("get_search_result", keyWord); //阻塞,直到远程方法调用完成。 // QDBusPendingReply>> 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> list, QMap &softwarereturn) { -// qWarning()<> list, size_t uniqueSymbol, DataQueue *searchResult) { + qDebug() << "Begin parseSoftWareCenterReturn"; QLocale locale; - QString pkgname; for(int i = 0; i < list.size(); i++) { -// qWarning()<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 &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() { diff --git a/libsearch/appsearch/app-match.h b/libsearch/appsearch/app-match.h index 6f51d15..8832ca1 100644 --- a/libsearch/appsearch/app-match.h +++ b/libsearch/appsearch/app-match.h @@ -27,8 +27,8 @@ #include #include #include -#include #include +#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 &installed, QMap &softwarereturn); + void startMatchApp(QString input, size_t uniqueSymbol, DataQueue *searchResult); + +protected: + void run() override; private: explicit AppMatch(QObject *parent = nullptr); @@ -64,17 +67,16 @@ private: void getDesktopFilePath(); void getAppName(QMap &installed); // void appNameMatch(QString appname,QString desktoppath,QString appicon); - void appNameMatch(QString appname, QMap &installed); - void appNameMatch(QMap &installed); - +// void appNameMatch(QString keyWord, QString appname, QMap &installed); + void appNameMatch(QString keyWord, size_t uniqueSymbol, DataQueue *searchResult); void softWareCenterSearch(QMap &softwarereturn); - - void parseSoftWareCenterReturn(QList> list, QMap &softwarereturn); - + void parseSoftWareCenterReturn(QList> list, size_t uniqueSymbol, DataQueue *searchResult); void getInstalledAppsVersion(QString appname); + void creatResultInfo(SearchPluginIface::ResultInfo &ri, QMapIterator &iter, bool isInstalled = true); -private: QString m_sourceText; + size_t m_uniqueSymbol; + DataQueue *m_search_result = nullptr; QStringList m_filePathList; QDBusInterface *m_interFace = nullptr; @@ -82,12 +84,11 @@ private: QMap m_installAppMap; private Q_SLOTS: - void slotDBusCallFinished(QMap &softwarereturn); + void slotDBusCallFinished(QString keyWord, size_t uniqueSymbol, DataQueue *searchResult); //Q_SIGNALS: -protected: - void run() override; + }; } diff --git a/libsearch/appsearch/app-search-plugin.cpp b/libsearch/appsearch/app-search-plugin.cpp index 815b8e2..67657f1 100644 --- a/libsearch/appsearch/app-search-plugin.cpp +++ b/libsearch/appsearch/app-search-plugin.cpp @@ -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("

%1

").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 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 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); } diff --git a/libsearch/appsearch/app-search-plugin.h b/libsearch/appsearch/app-search-plugin.h index f5ea35f..2415b6a 100644 --- a/libsearch/appsearch/app-search-plugin.h +++ b/libsearch/appsearch/app-search-plugin.h @@ -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); diff --git a/libsearch/file-utils.cpp b/libsearch/file-utils.cpp index 2913354..ecb5e6e 100644 --- a/libsearch/file-utils.cpp +++ b/libsearch/file-utils.cpp @@ -21,6 +21,7 @@ */ #include "file-utils.h" #include +#include 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 FileUtils::map_chinese2pinyin = QMap(); +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, diff --git a/libsearch/index/file-search-plugin.cpp b/libsearch/index/file-search-plugin.cpp index 5b25f28..0167870 100644 --- a/libsearch/index/file-search-plugin.cpp +++ b/libsearch/index/file-search-plugin.cpp @@ -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("

%1

").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("

%1

").arg(FileUtils::escapeHtml(showname))); if(QString::compare(showname, ri.name)) { m_nameLabel->setToolTip(ri.name); diff --git a/libsearch/settingsearch/settings-search-plugin.cpp b/libsearch/settingsearch/settings-search-plugin.cpp index 23c5539..27af944 100644 --- a/libsearch/settingsearch/settings-search-plugin.cpp +++ b/libsearch/settingsearch/settings-search-plugin.cpp @@ -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("

%1

").arg(FileUtils::escapeHtml(showname))); if(QString::compare(showname, ri.name)) { m_nameLabel->setToolTip(ri.name);