Merge pull request #160 from sunfengsheng/0305

Application Search Optimization
This commit is contained in:
iaom 2021-03-05 10:05:42 +08:00 committed by GitHub
commit c089012854
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 193 additions and 86 deletions

View File

@ -20,16 +20,25 @@
#include "app-match.h" #include "app-match.h"
#include <glib.h> #include <glib.h>
#include "file-utils.h" #include "file-utils.h"
AppMatch::AppMatch(QObject *parent) : QObject(parent)
static AppMatch *app_match_Class = nullptr;
AppMatch *AppMatch::getAppMatch()
{
if (!app_match_Class) {
app_match_Class = new AppMatch;
}
return app_match_Class;
}
AppMatch::AppMatch(QObject *parent) : QThread(parent)
// m_versionCommand(new QProcess(this)) // m_versionCommand(new QProcess(this))
{ {
this->getDesktopFilePath();
m_watchAppDir=new QFileSystemWatcher(this); m_watchAppDir=new QFileSystemWatcher(this);
m_watchAppDir->addPath("/usr/share/applications/"); m_watchAppDir->addPath("/usr/share/applications/");
//This part is not right!!!!!!!!!----iaom
connect(m_watchAppDir,&QFileSystemWatcher::directoryChanged,[this](){ connect(m_watchAppDir,&QFileSystemWatcher::directoryChanged,[this](){
this->getDesktopFilePath(); this->getDesktopFilePath();
this->getAllDesktopFilePath("/usr/share/applications/");
}); });
qDBusRegisterMetaType<QMap<QString,QString>>(); qDBusRegisterMetaType<QMap<QString,QString>>();
@ -41,36 +50,27 @@ AppMatch::AppMatch(QObject *parent) : QObject(parent)
{ {
qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message()); qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message());
} }
qDebug()<<"AppMatch is new";
} }
AppMatch::~AppMatch(){ AppMatch::~AppMatch(){
if(m_interFace){ if(m_interFace){
delete m_interFace; delete m_interFace;
m_interFace=NULL;
} }
m_interFace=NULL;
if(m_watchAppDir){ if(m_watchAppDir){
delete m_watchAppDir; delete m_watchAppDir;
m_watchAppDir=NULL;
} }
m_watchAppDir=NULL;
} }
//QStringList AppMatch::startMatchApp(QString input){ void AppMatch::startMatchApp(QString input,QMap<QString,QStringList> &installed,QMap<QString,QStringList> &softwarereturn){
// input.replace(" ",""); input.replace(" ","");
// m_sourceText=input; m_sourceText=input;
// m_softWareCenterMap.clear(); getAppName(installed);
// m_matchInstallAppMap.clear(); softWareCenterSearch(softwarereturn);
// m_returnResult1.clear(); qDebug()<<"match app is successful!";
// if(input.isEmpty()){ }
// return m_returnResult;
// }
// softWareCenterSearch();
// getAppName();
// returnAppMap();
// m_returnResult1=m_midResult;
// m_midResult.clear();
// qWarning()<<"m_returnResult :"<<m_returnResult1;
// return m_returnResult;
//}
/** /**
* @brief AppMatch::startMatchApp * @brief AppMatch::startMatchApp
@ -92,7 +92,6 @@ QMap<QString,QList<QString>> AppMatch::startMatchApp(QString input){
returnAppMap(); returnAppMap();
m_returnResult1=m_midResult; m_returnResult1=m_midResult;
m_midResult.clear(); m_midResult.clear();
qWarning()<<"m_returnResult :"<<m_returnResult1;
return m_returnResult1; return m_returnResult1;
} }
@ -190,9 +189,11 @@ void AppMatch::getAllDesktopFilePath(QString path){
} }
name=g_key_file_get_locale_string(keyfile,"Desktop Entry","Name", nullptr, nullptr); name=g_key_file_get_locale_string(keyfile,"Desktop Entry","Name", nullptr, nullptr);
icon=g_key_file_get_locale_string(keyfile,"Desktop Entry","Icon", nullptr, nullptr); icon=g_key_file_get_locale_string(keyfile,"Desktop Entry","Icon", nullptr, nullptr);
m_installAppMap.insert(QString::fromLocal8Bit(name),applist<<filePathStr<<QString::fromLocal8Bit(icon)); if(!m_filePathList.contains(filePathStr)){
m_installAppMap.insert(QString::fromLocal8Bit(name),applist<<filePathStr<<QString::fromLocal8Bit(icon)<<"");
applist.clear(); applist.clear();
m_filePathList.append(filePathStr); }
// m_filePathList.append(filePathStr);
} }
i++; i++;
@ -207,59 +208,59 @@ void AppMatch::getAllDesktopFilePath(QString path){
void AppMatch::getDesktopFilePath() void AppMatch::getDesktopFilePath()
{ {
m_filePathList.clear(); m_filePathList.clear();
getAllDesktopFilePath("/usr/share/applications/"); m_installAppMap.clear();
m_filePathList.removeAll("/usr/share/applications/software-properties-livepatch.desktop"); m_filePathList.append("/usr/share/applications/software-properties-livepatch.desktop");
m_filePathList.removeAll("/usr/share/applications/mate-color-select.desktop"); m_filePathList.append("/usr/share/applications/mate-color-select.desktop");
m_filePathList.removeAll("/usr/share/applications/blueman-adapters.desktop"); m_filePathList.append("/usr/share/applications/blueman-adapters.desktop");
m_filePathList.removeAll("/usr/share/applications/blueman-manager.desktop"); m_filePathList.append("/usr/share/applications/blueman-manager.desktop");
m_filePathList.removeAll("/usr/share/applications/mate-user-guide.desktop"); m_filePathList.append("/usr/share/applications/mate-user-guide.desktop");
m_filePathList.removeAll("/usr/share/applications/nm-connection-editor.desktop"); m_filePathList.append("/usr/share/applications/nm-connection-editor.desktop");
m_filePathList.removeAll("/usr/share/applications/debian-uxterm.desktop"); m_filePathList.append("/usr/share/applications/debian-uxterm.desktop");
m_filePathList.removeAll("/usr/share/applications/debian-xterm.desktop"); m_filePathList.append("/usr/share/applications/debian-xterm.desktop");
m_filePathList.removeAll("/usr/share/applications/im-config.desktop"); m_filePathList.append("/usr/share/applications/im-config.desktop");
m_filePathList.removeAll("/usr/share/applications/fcitx.desktop"); m_filePathList.append("/usr/share/applications/fcitx.desktop");
m_filePathList.removeAll("/usr/share/applications/fcitx-configtool.desktop"); m_filePathList.append("/usr/share/applications/fcitx-configtool.desktop");
m_filePathList.removeAll("/usr/share/applications/onboard-settings.desktop"); m_filePathList.append("/usr/share/applications/onboard-settings.desktop");
m_filePathList.removeAll("/usr/share/applications/info.desktop"); m_filePathList.append("/usr/share/applications/info.desktop");
m_filePathList.removeAll("/usr/share/applications/ukui-power-preferences.desktop"); m_filePathList.append("/usr/share/applications/ukui-power-preferences.desktop");
m_filePathList.removeAll("/usr/share/applications/ukui-power-statistics.desktop"); m_filePathList.append("/usr/share/applications/ukui-power-statistics.desktop");
m_filePathList.removeAll("/usr/share/applications/software-properties-drivers.desktop"); m_filePathList.append("/usr/share/applications/software-properties-drivers.desktop");
m_filePathList.removeAll("/usr/share/applications/software-properties-gtk.desktop"); m_filePathList.append("/usr/share/applications/software-properties-gtk.desktop");
m_filePathList.removeAll("/usr/share/applications/gnome-session-properties.desktop"); m_filePathList.append("/usr/share/applications/gnome-session-properties.desktop");
m_filePathList.removeAll("/usr/share/applications/org.gnome.font-viewer.desktop"); m_filePathList.append("/usr/share/applications/org.gnome.font-viewer.desktop");
m_filePathList.removeAll("/usr/share/applications/xdiagnose.desktop"); m_filePathList.append("/usr/share/applications/xdiagnose.desktop");
m_filePathList.removeAll("/usr/share/applications/gnome-language-selector.desktop"); m_filePathList.append("/usr/share/applications/gnome-language-selector.desktop");
m_filePathList.removeAll("/usr/share/applications/mate-notification-properties.desktop"); m_filePathList.append("/usr/share/applications/mate-notification-properties.desktop");
m_filePathList.removeAll("/usr/share/applications/transmission-gtk.desktop"); m_filePathList.append("/usr/share/applications/transmission-gtk.desktop");
m_filePathList.removeAll("/usr/share/applications/mpv.desktop"); m_filePathList.append("/usr/share/applications/mpv.desktop");
m_filePathList.removeAll("/usr/share/applications/system-config-printer.desktop"); m_filePathList.append("/usr/share/applications/system-config-printer.desktop");
m_filePathList.removeAll("/usr/share/applications/org.gnome.DejaDup.desktop"); m_filePathList.append("/usr/share/applications/org.gnome.DejaDup.desktop");
m_filePathList.removeAll("/usr/share/applications/yelp.desktop"); m_filePathList.append("/usr/share/applications/yelp.desktop");
m_filePathList.removeAll("/usr/share/applications/peony-computer.desktop"); m_filePathList.append("/usr/share/applications/peony-computer.desktop");
m_filePathList.removeAll("/usr/share/applications/peony-home.desktop"); m_filePathList.append("/usr/share/applications/peony-home.desktop");
m_filePathList.removeAll("/usr/share/applications/peony-trash.desktop"); m_filePathList.append("/usr/share/applications/peony-trash.desktop");
m_filePathList.removeAll("/usr/share/applications/peony.desktop"); m_filePathList.append("/usr/share/applications/peony.desktop");
//v10 //v10
m_filePathList.removeAll("/usr/share/applications/mate-about.desktop"); m_filePathList.append("/usr/share/applications/mate-about.desktop");
m_filePathList.removeAll("/usr/share/applications/time.desktop"); m_filePathList.append("/usr/share/applications/time.desktop");
m_filePathList.removeAll("/usr/share/applications/network.desktop"); m_filePathList.append("/usr/share/applications/network.desktop");
m_filePathList.removeAll("/usr/share/applications/shares.desktop"); m_filePathList.append("/usr/share/applications/shares.desktop");
m_filePathList.removeAll("/usr/share/applications/mate-power-statistics.desktop"); m_filePathList.append("/usr/share/applications/mate-power-statistics.desktop");
m_filePathList.removeAll("/usr/share/applications/display-im6.desktop"); m_filePathList.append("/usr/share/applications/display-im6.desktop");
m_filePathList.removeAll("/usr/share/applications/display-im6.q16.desktop"); m_filePathList.append("/usr/share/applications/display-im6.q16.desktop");
m_filePathList.removeAll("/usr/share/applications/openjdk-8-policytool.desktop"); m_filePathList.append("/usr/share/applications/openjdk-8-policytool.desktop");
m_filePathList.removeAll("/usr/share/applications/kylin-io-monitor.desktop"); m_filePathList.append("/usr/share/applications/kylin-io-monitor.desktop");
m_filePathList.removeAll("/usr/share/applications/wps-office-uninstall.desktop"); m_filePathList.append("/usr/share/applications/wps-office-uninstall.desktop");
QString desktop; // QString desktop;
QStringList applist; // QStringList applist;
QMap<QString, QList<QString>>::const_iterator i; // QMap<QString, QList<QString>>::const_iterator i;
for(i=m_installAppMap.constBegin();i!=m_installAppMap.constEnd();++i){ // for(i=m_installAppMap.constBegin();i!=m_installAppMap.constEnd();++i){
applist=i.value(); // applist=i.value();
if(m_filePathList.contains(applist.at(0))) // if(m_filePathList.contains(applist.at(0)))
m_filterInstallAppMap.insert(i.key(),applist); // m_filterInstallAppMap.insert(i.key(),applist);
} // }
} }
/** /**
@ -269,11 +270,20 @@ void AppMatch::getDesktopFilePath()
void AppMatch::getAppName() void AppMatch::getAppName()
{ {
QMap<QString, QList<QString>>::const_iterator i; QMap<QString, QList<QString>>::const_iterator i;
for(i=m_filterInstallAppMap.constBegin();i!=m_filterInstallAppMap.constEnd();++i){ for(i=m_installAppMap.constBegin();i!=m_installAppMap.constEnd();++i){
appNameMatch(i.key()); appNameMatch(i.key());
} }
} }
void AppMatch::getAppName(QMap<QString,QStringList> &installed)
{
QMap<QString, QList<QString>>::const_iterator i;
for(i=m_installAppMap.constBegin();i!=m_installAppMap.constEnd();++i){
appNameMatch(i.key(),installed);
}
qDebug()<<"installed app match is successful!";
}
/** /**
* @brief AppMatch::appNameMatch * @brief AppMatch::appNameMatch
* *
@ -284,31 +294,62 @@ void AppMatch::getAppName()
*/ */
void AppMatch::appNameMatch(QString appname){ void AppMatch::appNameMatch(QString appname){
if(appname.contains(m_sourceText,Qt::CaseInsensitive)){ if(appname.contains(m_sourceText,Qt::CaseInsensitive)){
m_matchInstallAppMap.insert(appname,m_filterInstallAppMap.value(appname)); m_matchInstallAppMap.insert(appname,m_installAppMap.value(appname));
return; return;
} }
QString shouzimu=FileUtils::findMultiToneWords(appname).at(1);// 中文转首字母 QString shouzimu=FileUtils::findMultiToneWords(appname).at(1);// 中文转首字母
if(shouzimu.contains(m_sourceText,Qt::CaseInsensitive)){ if(shouzimu.contains(m_sourceText,Qt::CaseInsensitive)){
m_matchInstallAppMap.insert(appname,m_filterInstallAppMap.value(appname)); m_matchInstallAppMap.insert(appname,m_installAppMap.value(appname));
return; return;
} }
if(m_sourceText.size()<2) if(m_sourceText.size()<2)
return; return;
QString pinyin=FileUtils::findMultiToneWords(appname).at(0);// 中文转拼音 QString pinyin=FileUtils::findMultiToneWords(appname).at(0);// 中文转拼音
if(pinyin.contains(m_sourceText,Qt::CaseInsensitive)){ if(pinyin.contains(m_sourceText,Qt::CaseInsensitive)){
m_matchInstallAppMap.insert(appname,m_filterInstallAppMap.value(appname)); m_matchInstallAppMap.insert(appname,m_installAppMap.value(appname));
} }
} }
void AppMatch::appNameMatch(QString appname,QMap<QString,QStringList> &installed){
if(appname.contains(m_sourceText,Qt::CaseInsensitive)){
installed.insert(appname,m_installAppMap.value(appname));
return;
}
QString shouzimu=FileUtils::findMultiToneWords(appname).at(1);// 中文转首字母
if(shouzimu.contains(m_sourceText,Qt::CaseInsensitive)){
installed.insert(appname,m_installAppMap.value(appname));
return;
}
if(m_sourceText.size()<2)
return;
QString pinyin=FileUtils::findMultiToneWords(appname).at(0);// 中文转拼音
if(pinyin.contains(m_sourceText,Qt::CaseInsensitive)){
installed.insert(appname,m_installAppMap.value(appname));
}
}
void AppMatch::softWareCenterSearch(){ void AppMatch::softWareCenterSearch(){
// 调用D-Bus接口的方法 // 调用D-Bus接口的方法
// QDBusPendingCall pcall = m_interFace->asyncCall("get_search_result", m_sourceText); // QDBusPendingCall pcall = m_interFace->asyncCall("get_search_result", m_sourceText);
// 设置等待异步消息的信号槽 // 设置等待异步消息的信号槽
// QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(pcall, nullptr); // QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(pcall, nullptr);
// QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, &AppMatch::slotDBusCallFinished); // QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, &AppMatch::slotDBusCallFinished);
if(m_interFace->timeout()!=-1)
return;
// qWarning()<<"this :"<<m_interFace->timeout();
slotDBusCallFinished(); slotDBusCallFinished();
} }
void AppMatch::softWareCenterSearch(QMap<QString,QStringList> &softwarereturn){
if(m_interFace->timeout()!=-1){
qWarning()<<"softWareCente Dbus is timeout !";
return;
}
slotDBusCallFinished(softwarereturn);
qDebug()<<"softWareCenter match app is successful!";
}
void AppMatch::slotDBusCallFinished() void AppMatch::slotDBusCallFinished()
{ {
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",m_sourceText); //阻塞,直到远程方法调用完成。
@ -323,17 +364,61 @@ void AppMatch::slotDBusCallFinished()
} }
// call->deleteLater(); // call->deleteLater();
} }
void AppMatch::slotDBusCallFinished(QMap<QString,QStringList> &softwarereturn){
QDBusReply<QList<QMap<QString,QString>>> reply = m_interFace->call("get_search_result",m_sourceText); //阻塞,直到远程方法调用完成。
// QDBusPendingReply<QList<QMap<QString,QString>>> reply = *call;
if (reply.isValid())
{
parseSoftWareCenterReturn(reply.value(),softwarereturn);
}
else
{
qWarning() << "value method called failed!";
}
// call->deleteLater();
}
void AppMatch::parseSoftWareCenterReturn(QList<QMap<QString,QString>> list,QMap<QString,QStringList> &softwarereturn){
// qWarning()<<list;
QString appname;
QString appicon;
QString appdiscription;
QStringList applist;
QLocale locale;
for(int i=0;i<list.size();i++){
// qWarning()<<list.at(i).keys();
if(locale.language()==QLocale::Chinese){
appname=list.at(i).value("displayname_cn");
}
if(locale.language()==QLocale::English){
appname=list.at(i).value("appname");
}
appdiscription=list.at(i).value("discription");
appicon=list.at(i).value("icon");
softwarereturn.insert(appname,applist<<""<<appicon<<appdiscription);
applist.clear();
}
}
void AppMatch::parseSoftWareCenterReturn(QList<QMap<QString,QString>> list){ void AppMatch::parseSoftWareCenterReturn(QList<QMap<QString,QString>> list){
// qWarning()<<list; // qWarning()<<list;
QString appname; QString appname;
QString appicon; QString appicon;
QString appdiscription;
QStringList applist; QStringList applist;
QLocale locale;
for(int i=0;i<list.size();i++){ for(int i=0;i<list.size();i++){
// qWarning()<<list.at(i).keys(); // qWarning()<<list.at(i).keys();
appname=list.at(i).value("appname"); if(locale.language()==QLocale::Chinese){
appname=list.at(i).value("displayname_cn");
}
if(locale.language()==QLocale::English){
appname=list.at(i).value("appname");
}
appdiscription=list.at(i).value("discription");
// qWarning()<<"discription"<<appdiscription;
appicon=list.at(i).value("icon"); appicon=list.at(i).value("icon");
m_softWareCenterMap.insert(appname,applist<<""<<appicon); m_softWareCenterMap.insert(appname,applist<<""<<appicon<<appdiscription);
applist.clear(); applist.clear();
} }
} }
@ -369,3 +454,8 @@ void AppMatch::returnAppMap(){
} }
} }
void AppMatch::run(){
qDebug()<<"AppMatch is run";
this->getDesktopFilePath();
this->getAllDesktopFilePath("/usr/share/applications/");
}

View File

@ -28,24 +28,33 @@
#include <QDBusReply> #include <QDBusReply>
#include <QtDBus> #include <QtDBus>
#include <QElapsedTimer> #include <QElapsedTimer>
#include <QThread>
class AppMatch : public QObject class AppMatch : public QThread
{ {
Q_OBJECT Q_OBJECT
public: public:
static AppMatch *getAppMatch();
explicit AppMatch(QObject *parent = nullptr); explicit AppMatch(QObject *parent = nullptr);
~AppMatch(); ~AppMatch();
// QStringList startMatchApp(QString input); void startMatchApp(QString input,QMap<QString,QStringList> &installed,QMap<QString,QStringList> &softwarereturn);
QMap<QString,QList<QString>> startMatchApp(QString input); QMap<QString,QList<QString>> startMatchApp(QString input);
private: private:
void getAllDesktopFilePath(QString path); void getAllDesktopFilePath(QString path);
void getDesktopFilePath(); void getDesktopFilePath();
void getAppName(); void getAppName();
void getAppName(QMap<QString,QStringList> &installed);
// void appNameMatch(QString appname,QString desktoppath,QString appicon); // void appNameMatch(QString appname,QString desktoppath,QString appicon);
void appNameMatch(QString appname); void appNameMatch(QString appname);
void appNameMatch(QString appname,QMap<QString,QStringList> &installed);
void softWareCenterSearch(); void softWareCenterSearch();
void softWareCenterSearch(QMap<QString,QStringList> &softwarereturn);
void parseSoftWareCenterReturn(QList<QMap<QString,QString>> list); void parseSoftWareCenterReturn(QList<QMap<QString,QString>> list);
void parseSoftWareCenterReturn(QList<QMap<QString,QString>> list,QMap<QString,QStringList> &softwarereturn);
void getInstalledAppsVersion(QString appname); void getInstalledAppsVersion(QString appname);
void returnAppMap(); void returnAppMap();
@ -58,7 +67,6 @@ private:
QFileSystemWatcher *m_watchAppDir=nullptr; QFileSystemWatcher *m_watchAppDir=nullptr;
QMap<QString,QList<QString>> m_softWareCenterMap; QMap<QString,QList<QString>> m_softWareCenterMap;
QMap<QString,QList<QString>> m_installAppMap; QMap<QString,QList<QString>> m_installAppMap;
QMap<QString,QList<QString>> m_filterInstallAppMap;
QMap<QString,QList<QString>> m_matchInstallAppMap; QMap<QString,QList<QString>> m_matchInstallAppMap;
QMap<QString,QList<QString>> m_returnResult1; QMap<QString,QList<QString>> m_returnResult1;
QMap<QString,QList<QString>> m_midResult; QMap<QString,QList<QString>> m_midResult;
@ -66,6 +74,12 @@ private:
private Q_SLOTS: private Q_SLOTS:
void slotDBusCallFinished(); void slotDBusCallFinished();
void slotDBusCallFinished(QMap<QString,QStringList> &softwarereturn);
//Q_SIGNALS:
protected:
void run() override;
}; };

View File

@ -237,7 +237,10 @@ int main(int argc, char *argv[])
// FirstIndex* fi = new FirstIndex("/home"); // FirstIndex* fi = new FirstIndex("/home");
// fi->start(); // fi->start();
qDebug() << "main start"; qDebug() << "main start";
AppMatch::getAppMatch()->start();
//wtf???
// AppMatch apm;
// apm.start();
QThreadPool::globalInstance()->setExpiryTimeout(5); QThreadPool::globalInstance()->setExpiryTimeout(5);
// QThreadPool::globalInstance()->clear(); // QThreadPool::globalInstance()->clear();
// setAutoDelete(true); // setAutoDelete(true);