forked from openkylin/ukui-search
add app match
This commit is contained in:
parent
de42faf3fb
commit
b72363ce68
|
@ -0,0 +1,196 @@
|
|||
#include "app-match.h"
|
||||
#include <glib.h>
|
||||
AppMatch::AppMatch(QObject *parent) : QObject(parent)
|
||||
{
|
||||
this->getDesktopFilePath();
|
||||
}
|
||||
|
||||
QStringList AppMatch::startMatchApp(QString input){
|
||||
m_soureText=input;
|
||||
this->getAppName();
|
||||
return m_returnResult;
|
||||
}
|
||||
|
||||
void AppMatch::getAllDesktopFilePath(QString path){
|
||||
|
||||
GError** error=nullptr;
|
||||
GKeyFileFlags flags=G_KEY_FILE_NONE;
|
||||
GKeyFile* keyfile=g_key_file_new ();
|
||||
|
||||
QDir dir(path);
|
||||
if (!dir.exists()) {
|
||||
return;
|
||||
}
|
||||
dir.setFilter(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot);
|
||||
dir.setSorting(QDir::DirsFirst);
|
||||
QFileInfoList list = dir.entryInfoList();
|
||||
list.removeAll(QFileInfo("/usr/share/applications/screensavers"));
|
||||
if(list.size()< 1 ) {
|
||||
return;
|
||||
}
|
||||
int i=0;
|
||||
|
||||
//递归算法的核心部分
|
||||
do{
|
||||
QFileInfo fileInfo = list.at(i);
|
||||
//如果是文件夹,递归
|
||||
bool isDir = fileInfo.isDir();
|
||||
if(isDir) {
|
||||
getAllDesktopFilePath(fileInfo.filePath());
|
||||
}
|
||||
else{
|
||||
//过滤后缀不是.desktop的文件
|
||||
QString filePathStr=fileInfo.filePath();
|
||||
if(!filePathStr.endsWith(".desktop"))
|
||||
{
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
QByteArray fpbyte=filePathStr.toLocal8Bit();
|
||||
char* filepath=fpbyte.data();
|
||||
g_key_file_load_from_file(keyfile,filepath,flags,error);
|
||||
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"))
|
||||
{
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
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"))
|
||||
{
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
//过滤LXQt、KDE
|
||||
char* ret=g_key_file_get_locale_string(keyfile,"Desktop Entry","OnlyShowIn", nullptr, nullptr);
|
||||
if(ret!=nullptr)
|
||||
{
|
||||
QString str=QString::fromLocal8Bit(ret);
|
||||
if(str.contains("LXQt") || str.contains("KDE"))
|
||||
{
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
//过滤中英文名为空的情况
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
m_filePathList.append(filePathStr);
|
||||
}
|
||||
i++;
|
||||
|
||||
} while(i < list.size());
|
||||
}
|
||||
|
||||
void AppMatch::getDesktopFilePath()
|
||||
{
|
||||
m_filePathList.clear();
|
||||
getAllDesktopFilePath("/usr/share/applications/");
|
||||
m_filePathList.removeAll("/usr/share/applications/software-properties-livepatch.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/mate-color-select.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/blueman-adapters.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/blueman-manager.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/mate-user-guide.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/nm-connection-editor.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/debian-uxterm.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/debian-xterm.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/im-config.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/fcitx.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/fcitx-configtool.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/onboard-settings.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/info.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/ukui-power-preferences.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/ukui-power-statistics.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/software-properties-drivers.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/software-properties-gtk.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/gnome-session-properties.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/org.gnome.font-viewer.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/xdiagnose.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/gnome-language-selector.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/mate-notification-properties.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/transmission-gtk.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/mpv.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/system-config-printer.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/org.gnome.DejaDup.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/yelp.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/peony-computer.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/peony-home.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/peony-trash.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/peony.desktop");
|
||||
|
||||
//v10
|
||||
m_filePathList.removeAll("/usr/share/applications/mate-about.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/time.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/network.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/shares.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/mate-power-statistics.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/display-im6.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/display-im6.q16.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/wps-office-uninstall.desktop");
|
||||
m_filePathList.removeAll("/usr/share/applications/wps-office-misc.desktop");
|
||||
}
|
||||
|
||||
void AppMatch::getAppName()
|
||||
{
|
||||
GError** error=nullptr;
|
||||
GKeyFileFlags flags=G_KEY_FILE_NONE;
|
||||
GKeyFile* keyfile=g_key_file_new ();
|
||||
|
||||
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,error);
|
||||
name=g_key_file_get_locale_string(keyfile,"Desktop Entry","Name", nullptr, nullptr);
|
||||
namestr=QString::fromLocal8Bit(name);
|
||||
appNameMatch(namestr,str);
|
||||
}
|
||||
|
||||
g_key_file_load_from_file(keyfile,filepath,flags,error);
|
||||
g_key_file_free(keyfile);
|
||||
}
|
||||
|
||||
void AppMatch::appNameMatch(QString appname,QString desktoppath){
|
||||
if(appname.contains(m_soureText)){
|
||||
m_returnResult.append(desktoppath);
|
||||
}
|
||||
QString pinyin=UkuiChineseLetter::getPinyins(appname).toLower(); // 中文转拼音
|
||||
if(pinyin.contains(m_soureText,Qt::CaseInsensitive)){
|
||||
m_returnResult.append(desktoppath);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
#ifndef APPMATCH_H
|
||||
#define APPMATCH_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QDir>
|
||||
#include <QLocale>
|
||||
#include <QDebug>
|
||||
#include <ukuichineseletter.h>
|
||||
class AppMatch : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit AppMatch(QObject *parent = nullptr);
|
||||
QStringList startMatchApp(QString input);
|
||||
|
||||
private:
|
||||
void getAllDesktopFilePath(QString path);
|
||||
void getDesktopFilePath();
|
||||
void getAppName();
|
||||
void appNameMatch(QString appname,QString desktoppath);
|
||||
|
||||
private:
|
||||
QString m_soureText;
|
||||
QStringList m_filePathList;
|
||||
QStringList m_returnResult;
|
||||
|
||||
};
|
||||
|
||||
#endif // APPMATCH_H
|
|
@ -0,0 +1,9 @@
|
|||
INCLUDEPATH += $$PWD
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/app-match.h \
|
||||
$$PWD/ukuichineseletter.h \
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/app-match.cpp \
|
||||
$$PWD/ukuichineseletter.cpp \
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Tianjin KYLIN Information Technology 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, 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef UKUICHINESELETTER_H
|
||||
#define UKUICHINESELETTER_H
|
||||
#include <QString>
|
||||
|
||||
|
||||
class UkuiChineseLetter
|
||||
{
|
||||
public:
|
||||
UkuiChineseLetter();
|
||||
static bool In(wchar_t start, wchar_t end, wchar_t code);
|
||||
|
||||
static char Convert(int n);
|
||||
|
||||
// 获取第一个汉字的首字母
|
||||
static QString getFirstLetter(const QString &src);
|
||||
|
||||
// 获取所有汉字的首字母
|
||||
static QString getFirstLetters(const QString &src);
|
||||
|
||||
static QString getFirstLettersAll(const QString &src);
|
||||
|
||||
// 获取一个汉字编码的汉语拼音
|
||||
static QString getPinyin(int code);
|
||||
|
||||
// 获取所有汉字的汉语拼音
|
||||
static QString getPinyins(const QString& text);
|
||||
};
|
||||
|
||||
#endif // UKUICHINESELETTER_H
|
|
@ -21,6 +21,7 @@ include(model/model.pri)
|
|||
include(control/control.pri))
|
||||
include(singleapplication/qt-single-application.pri)
|
||||
include(settingsmatch/setting-match.pri)
|
||||
include(appmatch/app-match.pri)
|
||||
|
||||
LIBS = -lxapian -lgsettings-qt
|
||||
# Default rules for deployment.
|
||||
|
|
Loading…
Reference in New Issue