Optimized desktop files parser in app search.

This commit is contained in:
iaom 2021-09-10 15:03:50 +08:00
parent 2226087128
commit 4f3c134cc7
1 changed files with 37 additions and 70 deletions

View File

@ -20,6 +20,7 @@
#include "app-match.h" #include "app-match.h"
#include <glib.h> #include <glib.h>
#include <qt5xdg/XdgIcon> #include <qt5xdg/XdgIcon>
#include <qt5xdg/XdgDesktopFile>
#include "file-utils.h" #include "file-utils.h"
#include "app-search-plugin.h" #include "app-search-plugin.h"
#define ANDROID_APP_DESKTOP_PATH QDir::homePath() + "/.local/share/applications/" #define ANDROID_APP_DESKTOP_PATH QDir::homePath() + "/.local/share/applications/"
@ -77,12 +78,6 @@ void AppMatch::startMatchApp(QString input, size_t uniqueSymbol, DataQueue<Searc
*/ */
void AppMatch::getAllDesktopFilePath(QString path) { void AppMatch::getAllDesktopFilePath(QString path) {
char* name;
char* icon;
GKeyFileFlags flags = G_KEY_FILE_NONE;
GKeyFile* keyfile = g_key_file_new();
QDir dir(path); QDir dir(path);
if(!dir.exists()) { if(!dir.exists()) {
return; return;
@ -94,83 +89,54 @@ void AppMatch::getAllDesktopFilePath(QString path) {
if(list.size() < 1) { if(list.size() < 1) {
return; return;
} }
XdgDesktopFile desktopfile;
int i = 0; int i = 0;
//递归算法的核心部分 while(i < list.size()) {
do {
QFileInfo fileInfo = list.at(i); QFileInfo fileInfo = list.at(i);
//如果是文件夹,递归 //如果是文件夹,递归
bool isDir = fileInfo.isDir(); bool isDir = fileInfo.isDir();
if(isDir) { if(isDir) {
getAllDesktopFilePath(fileInfo.filePath()); getAllDesktopFilePath(fileInfo.filePath());
qDebug() << fileInfo.filePath();
} else { } else {
//过滤LXQt、KDE
QString filePathStr = fileInfo.filePath(); QString filePathStr = fileInfo.filePath();
// if(filePathStr.contains("KDE", Qt::CaseInsensitive) || if(m_filePathList.contains(filePathStr)) {
// filePathStr.contains("mate",Qt::CaseInsensitive)|| ++i;
// filePathStr.contains("LX", Qt::CaseInsensitive)) { continue;
// i++; }
// continue;
// }
//过滤后缀不是.desktop的文件 //过滤后缀不是.desktop的文件
if(!filePathStr.endsWith(".desktop")) { if(!filePathStr.endsWith(".desktop")) {
i++; ++i;
continue; continue;
} }
QByteArray fpbyte = filePathStr.toLocal8Bit();
char* filepath = fpbyte.data(); desktopfile.load(filePathStr);
g_key_file_load_from_file(keyfile, filepath, flags, nullptr); if(desktopfile.value("NoDisplay").toString().contains("true") || desktopfile.value("NotShowIn").toString().contains("UKUI")) {
char* ret_1 = g_key_file_get_locale_string(keyfile, "Desktop Entry", "NoDisplay", nullptr, nullptr); ++i;
if(ret_1 != nullptr) {
QString str = QString::fromLocal8Bit(ret_1);
if(str.contains("true")) {
i++;
continue; continue;
} }
}
char* ret_2 = g_key_file_get_locale_string(keyfile, "Desktop Entry", "NotShowIn", nullptr, nullptr); QString name = desktopfile.localizedValue("Name").toString();
if(ret_2 != nullptr) { if(name.isEmpty()) {
QString str = QString::fromLocal8Bit(ret_2); ++i;
if(str.contains("UKUI")) { qDebug() << filePathStr << "name!!";
i++;
continue; continue;
} }
}
//过滤中英文名为空的情况 QString icon = desktopfile.iconName();
QLocale cn;
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()) {
i++;
continue;
}
} else {
char* name = g_key_file_get_string(keyfile, "Desktop Entry", "Name", nullptr);
if(QString::fromLocal8Bit(name).isEmpty()) {
i++;
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);
if(!m_filePathList.contains(filePathStr)) {
NameString appname; NameString appname;
QStringList appInfolist; QStringList appInfolist;
appname.app_name = QString::fromLocal8Bit(name); appname.app_name = name;
appInfolist << filePathStr << QString::fromLocal8Bit(icon); appInfolist << filePathStr << icon;
appInfolist.append(QString::fromLocal8Bit(g_key_file_get_string(keyfile, "Desktop Entry", "Name", nullptr))); appInfolist.append(desktopfile.value("Name").toString());
appInfolist.append(QString::fromLocal8Bit(g_key_file_get_string(keyfile, "Desktop Entry", "Name[zh_CN]", nullptr))); appInfolist.append(desktopfile.value("Name[zh_CN]").toString());
m_installAppMap.insert(appname, appInfolist); m_installAppMap.insert(appname, appInfolist);
++i;
} }
// m_filePathList.append(filePathStr);
}
i++;
} while(i < list.size()); }
g_key_file_free(keyfile);
} }
/** /**
@ -406,7 +372,7 @@ void AppMatch::creatResultInfo(SearchPluginIface::ResultInfo &ri, QMapIterator<N
} }
void AppMatch::run() { void AppMatch::run() {
qDebug() << "AppMatch is run"; qDebug() << "AppMatch running..";
this->getDesktopFilePath(); this->getDesktopFilePath();
this->getAllDesktopFilePath("/usr/share/applications/"); this->getAllDesktopFilePath("/usr/share/applications/");
this->getAllDesktopFilePath(ANDROID_APP_DESKTOP_PATH); this->getAllDesktopFilePath(ANDROID_APP_DESKTOP_PATH);
@ -417,4 +383,5 @@ void AppMatch::run() {
this->getAllDesktopFilePath(ANDROID_APP_DESKTOP_PATH); this->getAllDesktopFilePath(ANDROID_APP_DESKTOP_PATH);
} }
}); });
qDebug() << "AppMatch finish..";
} }