diff --git a/libsearch/appsearch/app-match.cpp b/libsearch/appsearch/app-match.cpp index 036de4a..5ae75da 100644 --- a/libsearch/appsearch/app-match.cpp +++ b/libsearch/appsearch/app-match.cpp @@ -20,16 +20,24 @@ #include "app-match.h" #include #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)) { - - this->getDesktopFilePath(); - m_watchAppDir=new QFileSystemWatcher(this); m_watchAppDir->addPath("/usr/share/applications/"); connect(m_watchAppDir,&QFileSystemWatcher::directoryChanged,[this](){ this->getDesktopFilePath(); + this->getAllDesktopFilePath("/usr/share/applications/"); }); qDBusRegisterMetaType>(); @@ -41,36 +49,27 @@ AppMatch::AppMatch(QObject *parent) : QObject(parent) { qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message()); } + qDebug()<<"AppMatch is new"; } AppMatch::~AppMatch(){ if(m_interFace){ delete m_interFace; - m_interFace=NULL; } + m_interFace=NULL; if(m_watchAppDir){ delete m_watchAppDir; - m_watchAppDir=NULL; } + m_watchAppDir=NULL; } -//QStringList AppMatch::startMatchApp(QString input){ -// input.replace(" ",""); -// m_sourceText=input; -// m_softWareCenterMap.clear(); -// m_matchInstallAppMap.clear(); -// m_returnResult1.clear(); -// if(input.isEmpty()){ -// return m_returnResult; -// } -// softWareCenterSearch(); -// getAppName(); -// returnAppMap(); -// m_returnResult1=m_midResult; -// m_midResult.clear(); -// qWarning()<<"m_returnResult :"< &installed,QMap &softwarereturn){ + input.replace(" ",""); + m_sourceText=input; + getAppName(installed); + softWareCenterSearch(softwarereturn); + qDebug()<<"match app is successful!"; +} /** * @brief AppMatch::startMatchApp 查询应用,含所有已安装与未安装 @@ -92,7 +91,6 @@ QMap> AppMatch::startMatchApp(QString input){ returnAppMap(); m_returnResult1=m_midResult; m_midResult.clear(); - qWarning()<<"m_returnResult :"<>::const_iterator i; - for(i=m_installAppMap.constBegin();i!=m_installAppMap.constEnd();++i){ - applist=i.value(); - if(m_filePathList.contains(applist.at(0))) - m_filterInstallAppMap.insert(i.key(),applist); - } +// QString desktop; +// QStringList applist; +// QMap>::const_iterator i; +// for(i=m_installAppMap.constBegin();i!=m_installAppMap.constEnd();++i){ +// applist=i.value(); +// if(m_filePathList.contains(applist.at(0))) +// m_filterInstallAppMap.insert(i.key(),applist); +// } } /** @@ -269,11 +269,20 @@ void AppMatch::getDesktopFilePath() void AppMatch::getAppName() { QMap>::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()); } } +void AppMatch::getAppName(QMap &installed) +{ + QMap>::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 * 进行匹配 @@ -284,31 +293,62 @@ void AppMatch::getAppName() */ void AppMatch::appNameMatch(QString appname){ if(appname.contains(m_sourceText,Qt::CaseInsensitive)){ - m_matchInstallAppMap.insert(appname,m_filterInstallAppMap.value(appname)); + m_matchInstallAppMap.insert(appname,m_installAppMap.value(appname)); return; } QString shouzimu=FileUtils::findMultiToneWords(appname).at(1);// 中文转首字母 if(shouzimu.contains(m_sourceText,Qt::CaseInsensitive)){ - m_matchInstallAppMap.insert(appname,m_filterInstallAppMap.value(appname)); + m_matchInstallAppMap.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)){ - m_matchInstallAppMap.insert(appname,m_filterInstallAppMap.value(appname)); + m_matchInstallAppMap.insert(appname,m_installAppMap.value(appname)); } } +void AppMatch::appNameMatch(QString appname,QMap &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(){ // 调用D-Bus接口的方法 // QDBusPendingCall pcall = m_interFace->asyncCall("get_search_result", m_sourceText); // 设置等待异步消息的信号槽 // QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(pcall, nullptr); // QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, &AppMatch::slotDBusCallFinished); + if(m_interFace->timeout()!=-1) + return; +// qWarning()<<"this :"<timeout(); slotDBusCallFinished(); } +void AppMatch::softWareCenterSearch(QMap &softwarereturn){ + if(m_interFace->timeout()!=-1){ + qWarning()<<"softWareCente Dbus is timeout !"; + return; + } + slotDBusCallFinished(softwarereturn); + qDebug()<<"softWareCenter match app is successful!"; +} + void AppMatch::slotDBusCallFinished() { QDBusReply>> reply = m_interFace->call("get_search_result",m_sourceText); //阻塞,直到远程方法调用完成。 @@ -323,17 +363,61 @@ void AppMatch::slotDBusCallFinished() } // call->deleteLater(); } +void AppMatch::slotDBusCallFinished(QMap &softwarereturn){ + QDBusReply>> reply = m_interFace->call("get_search_result",m_sourceText); //阻塞,直到远程方法调用完成。 +// QDBusPendingReply>> reply = *call; + if (reply.isValid()) + { + parseSoftWareCenterReturn(reply.value(),softwarereturn); + } + else + { + qWarning() << "value method called failed!"; + } +// call->deleteLater(); +} + +void AppMatch::parseSoftWareCenterReturn(QList> list,QMap &softwarereturn){ +// qWarning()<> list){ // qWarning()<getDesktopFilePath(); + this->getAllDesktopFilePath("/usr/share/applications/"); +} diff --git a/libsearch/appsearch/app-match.h b/libsearch/appsearch/app-match.h index f197af0..686f2a1 100644 --- a/libsearch/appsearch/app-match.h +++ b/libsearch/appsearch/app-match.h @@ -28,24 +28,33 @@ #include #include #include +#include -class AppMatch : public QObject +class AppMatch : public QThread { Q_OBJECT public: + static AppMatch *getAppMatch(); explicit AppMatch(QObject *parent = nullptr); ~AppMatch(); -// QStringList startMatchApp(QString input); + void startMatchApp(QString input,QMap &installed,QMap &softwarereturn); QMap> startMatchApp(QString input); private: void getAllDesktopFilePath(QString path); void getDesktopFilePath(); void getAppName(); + void getAppName(QMap &installed); // void appNameMatch(QString appname,QString desktoppath,QString appicon); void appNameMatch(QString appname); + void appNameMatch(QString appname,QMap &installed); + void softWareCenterSearch(); + void softWareCenterSearch(QMap &softwarereturn); + void parseSoftWareCenterReturn(QList> list); + void parseSoftWareCenterReturn(QList> list,QMap &softwarereturn); + void getInstalledAppsVersion(QString appname); void returnAppMap(); @@ -58,7 +67,6 @@ private: QFileSystemWatcher *m_watchAppDir=nullptr; QMap> m_softWareCenterMap; QMap> m_installAppMap; - QMap> m_filterInstallAppMap; QMap> m_matchInstallAppMap; QMap> m_returnResult1; QMap> m_midResult; @@ -66,6 +74,12 @@ private: private Q_SLOTS: void slotDBusCallFinished(); + void slotDBusCallFinished(QMap &softwarereturn); + +//Q_SIGNALS: + +protected: + void run() override; }; diff --git a/src/main.cpp b/src/main.cpp index 365eb04..dd93416 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -237,7 +237,9 @@ int main(int argc, char *argv[]) // FirstIndex* fi = new FirstIndex("/home"); // fi->start(); qDebug() << "main start"; - + AppMatch::getAppMatch()->start(); + AppMatch apm; + apm.start(); QThreadPool::globalInstance()->setExpiryTimeout(5); // QThreadPool::globalInstance()->clear(); // setAutoDelete(true);