ukui-search/libsearch/appsearch/app-match.cpp

398 lines
16 KiB
C++
Raw Normal View History

2021-01-29 11:43:07 +08:00
/*
* Copyright (C) 2020, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Authors: sunfengsheng <sunfengsheng@kylinos.cn>
*
*/
2020-12-30 15:31:36 +08:00
#include "app-match.h"
#include <glib.h>
2020-12-30 21:41:00 +08:00
#include "file-utils.h"
using namespace Zeeker;
2021-03-05 08:43:02 +08:00
static AppMatch *app_match_Class = nullptr;
2021-02-27 00:57:47 +08:00
2021-04-26 15:06:47 +08:00
AppMatch *AppMatch::getAppMatch() {
if(!app_match_Class) {
2021-03-05 08:43:02 +08:00
app_match_Class = new AppMatch;
}
return app_match_Class;
}
AppMatch::AppMatch(QObject *parent) : QThread(parent)
// m_versionCommand(new QProcess(this))
{
2021-04-26 15:06:47 +08:00
m_watchAppDir = new QFileSystemWatcher(this);
2021-02-27 00:57:47 +08:00
m_watchAppDir->addPath("/usr/share/applications/");
2021-04-26 15:06:47 +08:00
QDir androidPath(QDir::homePath() + "/.local/share/applications/");
if(androidPath.exists()) {
m_watchAppDir->addPath(QDir::homePath() + "/.local/share/applications/");
}
2021-04-26 15:06:47 +08:00
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()) {
2021-02-26 11:21:41 +08:00
qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message());
2021-04-26 15:06:47 +08:00
}
2021-05-30 11:28:04 +08:00
m_interFace->setTimeout(200);
2021-04-26 15:06:47 +08:00
qDebug() << "AppMatch is new";
2020-12-30 15:31:36 +08:00
}
2021-04-26 15:06:47 +08:00
AppMatch::~AppMatch() {
if(m_interFace) {
2021-02-26 11:38:22 +08:00
delete m_interFace;
}
2021-04-26 15:06:47 +08:00
m_interFace = NULL;
if(m_watchAppDir) {
2021-02-27 00:57:47 +08:00
delete m_watchAppDir;
}
2021-04-26 15:06:47 +08:00
m_watchAppDir = NULL;
2021-02-26 11:38:22 +08:00
}
2021-04-26 15:06:47 +08:00
void AppMatch::startMatchApp(QString input, QMap<NameString, QStringList> &installed, QMap<NameString, QStringList> &softwarereturn) {
m_sourceText = input;
2021-03-05 08:43:02 +08:00
getAppName(installed);
softWareCenterSearch(softwarereturn);
2021-04-26 15:06:47 +08:00
qDebug() << "match app is successful!";
2021-03-05 08:43:02 +08:00
}
2020-12-30 15:31:36 +08:00
2020-12-30 21:41:00 +08:00
/**
* @brief AppMatch::getAllDesktopFilePath desktop文件
* @param path desktop文件夹
*/
2021-04-26 15:06:47 +08:00
void AppMatch::getAllDesktopFilePath(QString path) {
2020-12-30 15:31:36 +08:00
2021-02-26 11:21:41 +08:00
char* name;
char* icon;
QStringList applist;
2021-04-26 15:06:47 +08:00
GKeyFileFlags flags = G_KEY_FILE_NONE;
GKeyFile* keyfile = g_key_file_new();
2020-12-30 15:31:36 +08:00
QDir dir(path);
2021-04-26 15:06:47 +08:00
if(!dir.exists()) {
2020-12-30 15:31:36 +08:00
return;
}
2021-04-26 15:06:47 +08:00
dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
2020-12-30 15:31:36 +08:00
dir.setSorting(QDir::DirsFirst);
QFileInfoList list = dir.entryInfoList();
list.removeAll(QFileInfo("/usr/share/applications/screensavers"));
2021-04-26 15:06:47 +08:00
if(list.size() < 1) {
2020-12-30 15:31:36 +08:00
return;
}
2021-04-26 15:06:47 +08:00
int i = 0;
2020-12-30 15:31:36 +08:00
//递归算法的核心部分
2021-04-26 15:06:47 +08:00
do {
2020-12-30 15:31:36 +08:00
QFileInfo fileInfo = list.at(i);
//如果是文件夹,递归
bool isDir = fileInfo.isDir();
if(isDir) {
getAllDesktopFilePath(fileInfo.filePath());
2021-04-26 15:06:47 +08:00
} else {
2020-12-30 15:31:36 +08:00
//过滤LXQt、KDE
2021-04-26 15:06:47 +08:00
QString filePathStr = fileInfo.filePath();
2021-05-19 09:58:57 +08:00
// if(filePathStr.contains("KDE", Qt::CaseInsensitive) ||
// filePathStr.contains("mate",Qt::CaseInsensitive)||
2021-05-19 09:58:57 +08:00
// filePathStr.contains("LX", Qt::CaseInsensitive)) {
// i++;
// continue;
// }
2020-12-30 15:31:36 +08:00
//过滤后缀不是.desktop的文件
2021-04-26 15:06:47 +08:00
if(!filePathStr.endsWith(".desktop")) {
2020-12-30 15:31:36 +08:00
i++;
continue;
}
2021-04-26 15:06:47 +08:00
QByteArray fpbyte = filePathStr.toLocal8Bit();
char* filepath = fpbyte.data();
g_key_file_load_from_file(keyfile, filepath, flags, nullptr);
char* ret_1 = g_key_file_get_locale_string(keyfile, "Desktop Entry", "NoDisplay", nullptr, nullptr);
if(ret_1 != nullptr) {
QString str = QString::fromLocal8Bit(ret_1);
if(str.contains("true")) {
2020-12-30 15:31:36 +08:00
i++;
continue;
}
}
2021-04-26 15:06:47 +08:00
char* ret_2 = g_key_file_get_locale_string(keyfile, "Desktop Entry", "NotShowIn", nullptr, nullptr);
if(ret_2 != nullptr) {
QString str = QString::fromLocal8Bit(ret_2);
if(str.contains("UKUI")) {
2020-12-30 15:31:36 +08:00
i++;
continue;
}
}
//过滤中英文名为空的情况
QLocale cn;
2021-04-26 15:06:47 +08:00
QString language = cn.languageToString(cn.language());
if(QString::compare(language, "Chinese") == 0) {
char* nameCh = g_key_file_get_string(keyfile, "Desktop Entry", "Name[zh_CN]", nullptr);
char* nameEn = g_key_file_get_string(keyfile, "Desktop Entry", "Name", nullptr);
if(QString::fromLocal8Bit(nameCh).isEmpty() && QString::fromLocal8Bit(nameEn).isEmpty()) {
2020-12-30 15:31:36 +08:00
i++;
continue;
}
2021-04-26 15:06:47 +08:00
} else {
char* name = g_key_file_get_string(keyfile, "Desktop Entry", "Name", nullptr);
if(QString::fromLocal8Bit(name).isEmpty()) {
2020-12-30 15:31:36 +08:00
i++;
continue;
}
}
2021-04-26 15:06:47 +08:00
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);
if(!m_filePathList.contains(filePathStr)) {
NameString appname;
appname.app_name = QString::fromLocal8Bit(name);
2021-04-26 15:06:47 +08:00
m_installAppMap.insert(appname, applist << filePathStr << QString::fromLocal8Bit(icon) << "" << "");
applist.clear();
2021-03-05 08:43:02 +08:00
}
// m_filePathList.append(filePathStr);
2020-12-30 15:31:36 +08:00
}
i++;
} while(i < list.size());
g_key_file_free(keyfile);
}
2020-12-30 21:41:00 +08:00
/**
* @brief AppMatch::getDesktopFilePath
*
*/
2021-04-26 15:06:47 +08:00
void AppMatch::getDesktopFilePath() {
2020-12-30 15:31:36 +08:00
m_filePathList.clear();
2021-03-05 08:43:02 +08:00
m_installAppMap.clear();
m_filePathList.append("/usr/share/applications/software-properties-livepatch.desktop");
m_filePathList.append("/usr/share/applications/mate-color-select.desktop");
m_filePathList.append("/usr/share/applications/blueman-adapters.desktop");
m_filePathList.append("/usr/share/applications/blueman-manager.desktop");
m_filePathList.append("/usr/share/applications/mate-user-guide.desktop");
m_filePathList.append("/usr/share/applications/nm-connection-editor.desktop");
m_filePathList.append("/usr/share/applications/debian-uxterm.desktop");
m_filePathList.append("/usr/share/applications/debian-xterm.desktop");
m_filePathList.append("/usr/share/applications/im-config.desktop");
m_filePathList.append("/usr/share/applications/fcitx.desktop");
m_filePathList.append("/usr/share/applications/fcitx-configtool.desktop");
m_filePathList.append("/usr/share/applications/onboard-settings.desktop");
m_filePathList.append("/usr/share/applications/info.desktop");
m_filePathList.append("/usr/share/applications/ukui-power-preferences.desktop");
m_filePathList.append("/usr/share/applications/ukui-power-statistics.desktop");
m_filePathList.append("/usr/share/applications/software-properties-drivers.desktop");
m_filePathList.append("/usr/share/applications/software-properties-gtk.desktop");
m_filePathList.append("/usr/share/applications/gnome-session-properties.desktop");
m_filePathList.append("/usr/share/applications/org.gnome.font-viewer.desktop");
m_filePathList.append("/usr/share/applications/xdiagnose.desktop");
m_filePathList.append("/usr/share/applications/gnome-language-selector.desktop");
m_filePathList.append("/usr/share/applications/mate-notification-properties.desktop");
m_filePathList.append("/usr/share/applications/transmission-gtk.desktop");
m_filePathList.append("/usr/share/applications/mpv.desktop");
m_filePathList.append("/usr/share/applications/system-config-printer.desktop");
m_filePathList.append("/usr/share/applications/org.gnome.DejaDup.desktop");
m_filePathList.append("/usr/share/applications/yelp.desktop");
m_filePathList.append("/usr/share/applications/peony-computer.desktop");
m_filePathList.append("/usr/share/applications/peony-home.desktop");
m_filePathList.append("/usr/share/applications/peony-trash.desktop");
// m_filePathList.append("/usr/share/applications/peony.desktop");
2020-12-30 15:31:36 +08:00
//v10
2021-03-05 08:43:02 +08:00
m_filePathList.append("/usr/share/applications/mate-about.desktop");
m_filePathList.append("/usr/share/applications/time.desktop");
m_filePathList.append("/usr/share/applications/network.desktop");
m_filePathList.append("/usr/share/applications/shares.desktop");
m_filePathList.append("/usr/share/applications/mate-power-statistics.desktop");
m_filePathList.append("/usr/share/applications/display-im6.desktop");
m_filePathList.append("/usr/share/applications/display-im6.q16.desktop");
m_filePathList.append("/usr/share/applications/openjdk-8-policytool.desktop");
m_filePathList.append("/usr/share/applications/kylin-io-monitor.desktop");
m_filePathList.append("/usr/share/applications/wps-office-uninstall.desktop");
2021-02-26 11:21:41 +08:00
2021-03-05 08:43:02 +08:00
// 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);
// }
2020-12-30 15:31:36 +08:00
}
2021-04-26 15:06:47 +08:00
void AppMatch::getAppName(QMap<NameString, QStringList> &installed) {
2021-06-28 19:38:27 +08:00
// QMap<NameString, QStringList>::const_iterator i;
// for(i = m_installAppMap.constBegin(); i != m_installAppMap.constEnd(); ++i) {
// appNameMatch(i.key().app_name, installed);
// }
appNameMatch(installed);
2021-04-26 15:06:47 +08:00
qDebug() << "installed app match is successful!";
2021-03-05 08:43:02 +08:00
}
2020-12-30 21:41:00 +08:00
/**
* @brief AppMatch::appNameMatch
*
* @param appname
*
*/
2021-04-26 15:06:47 +08:00
void AppMatch::appNameMatch(QString appname, QMap<NameString, QStringList> &installed) {
NameString name{appname};
QStringList list;
2021-03-22 11:42:05 +08:00
QStringList pinyinlist;
2021-04-26 15:06:47 +08:00
pinyinlist = FileUtils::findMultiToneWords(appname);
QMapIterator<NameString, QStringList> iter(m_installAppMap);
while(iter.hasNext()) {
iter.next();
2021-04-26 15:06:47 +08:00
if(iter.key().app_name == appname) {
list = iter.value();
break;
}
2021-03-05 08:43:02 +08:00
}
2021-04-26 15:06:47 +08:00
if(appname.contains(m_sourceText, Qt::CaseInsensitive)) {
// installed.insert(name,m_installAppMap.value(name));
2021-04-26 15:06:47 +08:00
installed.insert(name, list);
2020-12-30 21:41:00 +08:00
return;
}
2021-04-26 15:06:47 +08:00
for(int i = 0; i < pinyinlist.size() / 2; i++) {
QString shouzimu = pinyinlist.at(2 * i + 1); // 中文转首字母
if(shouzimu.contains(m_sourceText, Qt::CaseInsensitive)) {
2021-03-22 11:42:05 +08:00
// installed.insert(name,m_installAppMap.value(name));
2021-04-26 15:06:47 +08:00
installed.insert(name, list);
2021-03-22 11:42:05 +08:00
return;
}
2021-04-26 15:06:47 +08:00
if(m_sourceText.size() < 2)
2021-03-22 11:42:05 +08:00
return;
2021-04-26 15:06:47 +08:00
QString pinyin = pinyinlist.at(2 * i); // 中文转拼音
if(pinyin.contains(m_sourceText, Qt::CaseInsensitive)) {
2021-03-22 11:42:05 +08:00
// installed.insert(name,m_installAppMap.value(name));
2021-04-26 15:06:47 +08:00
installed.insert(name, list);
2021-03-22 11:42:05 +08:00
return;
}
2021-02-26 11:21:41 +08:00
}
}
2021-06-28 19:38:27 +08:00
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;
}
QStringList pinyinlist;
pinyinlist = FileUtils::findMultiToneWords(iter.key().app_name);
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);
continue;
}
if(m_sourceText.size() < 2)
continue;
QString pinyin = pinyinlist.at(2 * i); // 中文转拼音
if(pinyin.contains(m_sourceText, Qt::CaseInsensitive)) {
installed.insert(name, list);
continue;
}
}
}
}
2021-02-26 11:21:41 +08:00
2021-04-26 15:06:47 +08:00
void AppMatch::softWareCenterSearch(QMap<NameString, QStringList> &softwarereturn) {
2021-05-30 11:28:04 +08:00
// if(m_interFace->timeout() != -1) {
// qWarning() << "softWareCente Dbus is timeout !";
// return;
// }
2021-03-05 08:43:02 +08:00
slotDBusCallFinished(softwarereturn);
2021-04-26 15:06:47 +08:00
qDebug() << "softWareCenter match app is successful!";
2021-03-05 08:43:02 +08:00
}
2021-04-26 15:06:47 +08:00
void AppMatch::slotDBusCallFinished(QMap<NameString, QStringList> &softwarereturn) {
QDBusReply<QList<QMap<QString, QString>>> reply = m_interFace->call("get_search_result", m_sourceText); //阻塞,直到远程方法调用完成。
2021-03-05 08:43:02 +08:00
// QDBusPendingReply<QList<QMap<QString,QString>>> reply = *call;
2021-04-26 15:06:47 +08:00
if(reply.isValid()) {
parseSoftWareCenterReturn(reply.value(), softwarereturn);
} else {
qWarning() << "value method called failed!";
}
2021-03-05 08:43:02 +08:00
// call->deleteLater();
}
2021-04-26 15:06:47 +08:00
void AppMatch::parseSoftWareCenterReturn(QList<QMap<QString, QString>> list, QMap<NameString, QStringList> &softwarereturn) {
2021-03-05 08:43:02 +08:00
// qWarning()<<list;
QString appname;
NameString name;
2021-03-05 08:43:02 +08:00
QString appicon;
QString appdiscription;
QStringList applist;
QLocale locale;
QString pkgname;
2021-04-26 15:06:47 +08:00
for(int i = 0; i < list.size(); i++) {
2021-03-05 08:43:02 +08:00
// qWarning()<<list.at(i).keys();
2021-04-26 15:06:47 +08:00
if(locale.language() == QLocale::Chinese) {
appname = list.at(i).value("displayname_cn");
pkgname = list.at(i).value("appname");
2021-03-05 08:43:02 +08:00
}
2021-04-26 15:06:47 +08:00
if(locale.language() == QLocale::English) {
appname = list.at(i).value("appname");
2021-03-05 08:43:02 +08:00
}
2021-04-26 15:06:47 +08:00
appdiscription = list.at(i).value("discription");
appicon = list.at(i).value("icon");
name.app_name = appname;
2021-04-26 15:06:47 +08:00
pkgname.isEmpty() ? softwarereturn.insert(name, applist << "" << appicon << "" << appdiscription) : softwarereturn.insert(name, applist << "" << appicon << pkgname << appdiscription);
2021-02-26 11:21:41 +08:00
applist.clear();
2020-12-30 15:31:36 +08:00
}
}
2021-02-26 11:21:41 +08:00
2021-04-26 15:06:47 +08:00
void AppMatch::getInstalledAppsVersion(QString appname) {
2021-02-26 11:21:41 +08:00
// 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();
}
2021-04-26 15:06:47 +08:00
void AppMatch::run() {
qDebug() << "AppMatch is run";
2021-03-05 08:43:02 +08:00
this->getDesktopFilePath();
this->getAllDesktopFilePath("/usr/share/applications/");
2021-04-26 15:06:47 +08:00
QDir androidPath(QDir::homePath() + "/.local/share/applications/");
if(androidPath.exists())
2021-04-26 15:06:47 +08:00
this->getAllDesktopFilePath(QDir::homePath() + "/.local/share/applications/");
connect(m_watchAppDir, &QFileSystemWatcher::directoryChanged, this, [ = ](const QString & path) {
this->getDesktopFilePath();
2021-04-26 15:06:47 +08:00
if(path == "/usr/share/applications/") {
this->getAllDesktopFilePath("/usr/share/applications/");
}
2021-04-26 15:06:47 +08:00
if(androidPath.exists()) {
if(path == QDir::homePath() + "/.local/share/applications/") {
this->getAllDesktopFilePath(QDir::homePath() + "/.local/share/applications/");
}
}
});
2021-03-05 08:43:02 +08:00
}