-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinuxappinfo.cpp
More file actions
140 lines (117 loc) · 4.07 KB
/
linuxappinfo.cpp
File metadata and controls
140 lines (117 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "linuxappinfo.h"
#include <QFile>
#include <QDebug>
#include <QTextCodec>
LinuxAppInfo::LinuxAppInfo(QObject *parent)
{
}
void LinuxAppInfo::run()
{
m_process = new QProcess();
m_process->setReadChannel(QProcess::StandardOutput);
m_process->moveToThread(this);
//Generates a list of Desktop filenames for the installed software under Linux
makeLinuxAppsFileNameList();
//Generates a list of the Desktop file paths for the installed software under Linux
makeAppsDesktopFilePathList();
//Store applied information according to certain rules
makeAppsInfoObject();
exec();
}
void LinuxAppInfo::makeLinuxAppsFileNameList()
{
m_process->start("ls /usr/share/applications/");
m_process->waitForFinished();
QString data = m_process->readAllStandardOutput();
//Gets all the file names under /usr/share/applications
m_AppsFileNameList = data.split("\n");
//Remove files that do not have the.Desktop suffix from the list
for(int i=0; i<m_AppsFileNameList.size(); i++)
{
data = m_AppsFileNameList.at(i);
if(!data.contains(".desktop",Qt::CaseSensitive))
m_AppsFileNameList.removeAt(i);
}
}
void LinuxAppInfo::makeAppsDesktopFilePathList()
{
foreach (QString fileName, m_AppsFileNameList) {
m_AppsDesktopFilePathList.append(QString("/usr/share/applications/%1").arg(fileName));
}
}
void LinuxAppInfo::makeAppsInfoObject()
{
foreach (QString filePath, m_AppsDesktopFilePathList) {
QFile file(filePath);
if(!file.open(QIODevice::ReadOnly))
qDebug()<<file.errorString();
//Read All data
QByteArray array = file.readAll();
QTextCodec *tc = QTextCodec::codecForName("UTF-8");
QString buf = tc->toUnicode(array);
if(buf.contains("NoDisplay=true"))
continue;
if(buf.contains("Terminal=true"))
continue;
if(!buf.contains("Type=Application"))
continue;
file.close();
//parse
if(!file.open(QIODevice::ReadOnly))
qDebug()<<file.errorString();
//init
QJsonObject app;
app["appName"] = "";
app["enName"] = "";
app["name"] = "";
app["appPath"] = "";
app["image"] = "";
while(file.atEnd() == false)
{
QByteArray array = file.readLine();
QTextCodec *tc = QTextCodec::codecForName("UTF-8");
//The conversion removes white space characters on both sides of the string
QString buf = tc->toUnicode(array).trimmed();
QStringList content = buf.split("=");
if(buf.contains("Name="))
{
app["name"] = content.last();
app["enName"] = content.last();
app["appName"] = content.last();
}
if(buf.contains("Name[en]="))
app["enName"] = content.last();
if(buf.contains("Name[zh_CN]="))
app["name"] = content.last();
if(buf.contains("Exec="))
app["appPath"] = content.last();
if(buf.contains("Icon="))
app["image"] = getAppIconFilePath(content.last());
}
file.close();
if(!app["name"].toString().isEmpty())
emit findOneApp(app);
}
}
QString LinuxAppInfo::getAppIconFilePath(QString iconStr)
{
//First, determine whether the parameter is a path or a filename
if(iconStr.contains("/"))
return iconStr;
//And then parse out the path of the photo
m_process->start(QString("find /usr/share/icons -name %1*").arg(iconStr));
m_process->waitForFinished();
QString data = m_process->readAllStandardOutput();
if(data.isEmpty())
{
m_process->start(QString("find /usr/share/pixmaps -name %1*").arg(iconStr));
m_process->waitForFinished();
data = m_process->readAllStandardOutput();
}
//There may be a number of different size ICONS
QStringList iconsPath= data.split("\n");
data = iconsPath.at(0);
if(!data.isEmpty())
return iconsPath.at(0);
return "";
}