Docking software store

This commit is contained in:
sunfengsheng 2021-02-26 11:21:41 +08:00
parent 471af5affd
commit 0e771323a6
2 changed files with 156 additions and 34 deletions

View File

@ -21,30 +21,67 @@
#include <glib.h> #include <glib.h>
#include "file-utils.h" #include "file-utils.h"
AppMatch::AppMatch(QObject *parent) : QObject(parent) AppMatch::AppMatch(QObject *parent) : QObject(parent)
// m_versionCommand(new QProcess(this))
{ {
this->getDesktopFilePath(); this->getDesktopFilePath();
qDBusRegisterMetaType<QMap<QString,QString>>();
qDBusRegisterMetaType<QList<QMap<QString,QString>>>();
m_interFace=new QDBusInterface ("com.kylin.softwarecenter.getsearchresults", "/com/kylin/softwarecenter/getsearchresults",
"com.kylin.getsearchresults",
QDBusConnection::sessionBus());
if (!m_interFace->isValid())
{
qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message());
}
} }
QStringList AppMatch::startMatchApp(QString input){ QStringList AppMatch::startMatchApp(QString input){
input.replace(" ",""); input.replace(" ","");
m_sourceText=input; m_sourceText=input;
m_returnResult.clear(); m_softWareCenterMap.clear();
m_matchInstallAppMap.clear();
m_returnResult1.clear();
if(input.isEmpty()){ if(input.isEmpty()){
return m_returnResult; return m_returnResult;
} }
this->getAppName(); softWareCenterSearch();
m_returnResult=m_midResult; getAppName();
returnAppMap();
m_returnResult1=m_midResult;
m_midResult.clear(); m_midResult.clear();
// qWarning()<<"m_returnResult :"<<m_returnResult; qWarning()<<"m_returnResult :"<<m_returnResult1;
return m_returnResult; return m_returnResult;
} }
QMap<QString,QList<QString>> AppMatch::startMatchApp(QString input,int i){
input.replace(" ","");
m_sourceText=input;
m_softWareCenterMap.clear();
m_matchInstallAppMap.clear();
m_returnResult1.clear();
if(input.isEmpty()){
return m_returnResult1;
}
softWareCenterSearch();
getAppName();
returnAppMap();
m_returnResult1=m_midResult;
m_midResult.clear();
qWarning()<<"m_returnResult :"<<m_returnResult1;
return m_returnResult1;
}
/** /**
* @brief AppMatch::getAllDesktopFilePath desktop文件 * @brief AppMatch::getAllDesktopFilePath desktop文件
* @param path desktop文件夹 * @param path desktop文件夹
*/ */
void AppMatch::getAllDesktopFilePath(QString path){ void AppMatch::getAllDesktopFilePath(QString path){
char* name;
char* icon;
QStringList applist;
GKeyFileFlags flags=G_KEY_FILE_NONE; GKeyFileFlags flags=G_KEY_FILE_NONE;
GKeyFile* keyfile=g_key_file_new (); GKeyFile* keyfile=g_key_file_new ();
@ -60,7 +97,6 @@ void AppMatch::getAllDesktopFilePath(QString path){
return; return;
} }
int i=0; int i=0;
//递归算法的核心部分 //递归算法的核心部分
do{ do{
QFileInfo fileInfo = list.at(i); QFileInfo fileInfo = list.at(i);
@ -128,7 +164,10 @@ void AppMatch::getAllDesktopFilePath(QString path){
continue; continue;
} }
} }
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);
m_installAppMap.insert(QString::fromLocal8Bit(name),applist<<filePathStr<<QString::fromLocal8Bit(icon));
applist.clear();
m_filePathList.append(filePathStr); m_filePathList.append(filePathStr);
} }
i++; i++;
@ -188,7 +227,15 @@ void AppMatch::getDesktopFilePath()
m_filePathList.removeAll("/usr/share/applications/openjdk-8-policytool.desktop"); m_filePathList.removeAll("/usr/share/applications/openjdk-8-policytool.desktop");
m_filePathList.removeAll("/usr/share/applications/kylin-io-monitor.desktop"); m_filePathList.removeAll("/usr/share/applications/kylin-io-monitor.desktop");
m_filePathList.removeAll("/usr/share/applications/wps-office-uninstall.desktop"); m_filePathList.removeAll("/usr/share/applications/wps-office-uninstall.desktop");
m_filePathList.removeAll("/usr/share/applications/wps-office-misc.desktop");
QString desktop;
QStringList applist;
QMap<QString, QList<QString>>::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);
}
} }
/** /**
@ -197,27 +244,10 @@ void AppMatch::getDesktopFilePath()
*/ */
void AppMatch::getAppName() void AppMatch::getAppName()
{ {
GKeyFileFlags flags=G_KEY_FILE_NONE; QMap<QString, QList<QString>>::const_iterator i;
GKeyFile* keyfile=g_key_file_new (); for(i=m_filterInstallAppMap.constBegin();i!=m_filterInstallAppMap.constEnd();++i){
appNameMatch(i.key());
QByteArray fpbyte; }
QString str;
char* filepath;
char* name;
QString namestr;
for(int i=0;i<m_filePathList.size();i++){
str=m_filePathList.at(i);
fpbyte=str.toLocal8Bit();
filepath=fpbyte.data();
g_key_file_load_from_file(keyfile,filepath,flags,nullptr);
name=g_key_file_get_locale_string(keyfile,"Desktop Entry","Name", nullptr, nullptr);
namestr=QString::fromLocal8Bit(name);
// qWarning()<<"namestr :"<<namestr;
appNameMatch(namestr,str);
}
g_key_file_load_from_file(keyfile,filepath,flags,nullptr);
g_key_file_free(keyfile);
} }
/** /**
@ -228,20 +258,90 @@ void AppMatch::getAppName()
* @param desktoppath * @param desktoppath
* desktop路径 * desktop路径
*/ */
void AppMatch::appNameMatch(QString appname,QString desktoppath){ void AppMatch::appNameMatch(QString appname){
if(appname.contains(m_sourceText,Qt::CaseInsensitive)){ if(appname.contains(m_sourceText,Qt::CaseInsensitive)){
m_midResult.append(desktoppath); m_matchInstallAppMap.insert(appname,m_filterInstallAppMap.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_midResult.append(desktoppath); m_matchInstallAppMap.insert(appname,m_filterInstallAppMap.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_midResult.append(desktoppath); m_matchInstallAppMap.insert(appname,m_filterInstallAppMap.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);
slotDBusCallFinished();
}
void AppMatch::slotDBusCallFinished()
{
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());
}
else
{
qWarning() << "value method called failed!";
}
// call->deleteLater();
}
void AppMatch::parseSoftWareCenterReturn(QList<QMap<QString,QString>> list){
// qWarning()<<list;
QString appname;
QString appicon;
QStringList applist;
for(int i=0;i<list.size();i++){
// qWarning()<<list.at(i).keys();
appname=list.at(i).value("appname");
appicon=list.at(i).value("icon");
m_softWareCenterMap.insert(appname,applist<<""<<appicon);
applist.clear();
}
}
void AppMatch::getInstalledAppsVersion(QString appname){
// qWarning()<<"apt show "+appname;
// m_versionCommand->start("apt show "+appname);
// m_versionCommand->startDetached(m_versionCommand->program());
// m_versionCommand->waitForFinished();
// connect(m_versionCommand,&QProcess::readyReadStandardOutput,this,[=](){
// QString result=m_versionCommand->readAllStandardOutput();
// if(!result.isEmpty()){
// QStringList strlist=result.split("\n");
// QString str=strlist.at(1);
// if(!str.contains("Version")){
// return;
// }
// qWarning()<<strlist.at(1);
// qWarning()<<"-----------------------------------------------";
// }
// });
// m_versionCommand->close();
}
void AppMatch::returnAppMap(){
QMap<QString, QList<QString>>::const_iterator i;
for(i=m_matchInstallAppMap.constBegin();i!=m_matchInstallAppMap.constEnd();++i){
m_midResult.insert(i.key(),i.value());
}
QMap<QString, QList<QString>>::const_iterator j;
for(j=m_softWareCenterMap.constBegin();j!=m_softWareCenterMap.constEnd();++j){
m_midResult.insert(j.key(),j.value());
}
}

View File

@ -24,24 +24,46 @@
#include <QDir> #include <QDir>
#include <QLocale> #include <QLocale>
#include <QDebug> #include <QDebug>
#include <QDBusInterface>
#include <QDBusReply>
#include <QtDBus>
#include <QElapsedTimer>
class AppMatch : public QObject class AppMatch : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit AppMatch(QObject *parent = nullptr); explicit AppMatch(QObject *parent = nullptr);
QStringList startMatchApp(QString input); QStringList startMatchApp(QString input);
QMap<QString,QList<QString>> startMatchApp(QString input,int i);
private: private:
void getAllDesktopFilePath(QString path); void getAllDesktopFilePath(QString path);
void getDesktopFilePath(); void getDesktopFilePath();
void getAppName(); void getAppName();
void appNameMatch(QString appname,QString desktoppath); // void appNameMatch(QString appname,QString desktoppath,QString appicon);
void appNameMatch(QString appname);
void softWareCenterSearch();
void parseSoftWareCenterReturn(QList<QMap<QString,QString>> list);
void getInstalledAppsVersion(QString appname);
void returnAppMap();
private: private:
QString m_sourceText; QString m_sourceText;
QStringList m_filePathList; QStringList m_filePathList;
QStringList m_returnResult; QStringList m_returnResult;
QStringList m_midResult;
QDBusInterface *m_interFace;
QMap<QString,QList<QString>> m_softWareCenterMap;
QMap<QString,QList<QString>> m_installAppMap;
QMap<QString,QList<QString>> m_filterInstallAppMap;
QMap<QString,QList<QString>> m_matchInstallAppMap;
QMap<QString,QList<QString>> m_returnResult1;
QMap<QString,QList<QString>> m_midResult;
// QProcess *m_versionCommand;
private Q_SLOTS:
void slotDBusCallFinished();
}; };