forked from song-kang/rekols-optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
291 lines (226 loc) · 7.52 KB
/
utils.cpp
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* -*- Mode: C++; indent-tabs-mode: nil; tab-width: 4 -*-
* -*- coding: utf-8 -*-
*
* Copyright (C) 2017 Rekols
*
* Author: Rekols <rekols@foxmail.com>
* Maintainer: Rekols <rekols@foxmail.com>
*
* 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
* 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/>.
*/
#include "utils.h"
#include <QFile>
#include <QDebug>
#include <QProcess>
#include <QFileInfo>
#include <QDir>
#include <QStandardPaths>
QString Utils::getQssContent(const QString &path)
{
QFile file(path);
QString qss;
file.open(QIODevice::ReadOnly);
qss = file.readAll();
file.close();
return qss;
}
QString Utils::getUserName()
{
QString name = qgetenv("USER");
if (name.isEmpty())
name = qgetenv("USERNAME");
return name;
}
QString Utils::getPlatform()
{
return QString("%1 %2")
.arg(QSysInfo::kernelType())
.arg(QSysInfo::currentCpuArchitecture());
}
QString Utils::getDistribution()
{
return QSysInfo::prettyProductName();
}
QString Utils::getKernel()
{
return QSysInfo::kernelVersion();
}
void Utils::getCpuInfo(QString &cpuModel, QString &cpuCore)
{
QFile file("/proc/cpuinfo");
file.open(QIODevice::ReadOnly);
QString buffer = file.readAll();
QStringList model_line = buffer.split("\n").filter(QRegExp("^model name"));
QStringList core_line = buffer.split("\n");
cpuModel = model_line.first().split(":").at(1);
cpuCore = QString::number(core_line.filter(QRegExp("^processor")).count());
file.close();
}
void Utils::getCpuTime(unsigned long long &workTime, unsigned long long &totalTime)
{
QFile file("/proc/stat");
file.open(QIODevice::ReadOnly);
QString buffer = file.readAll();
QStringList list = buffer.split("\n").filter(QRegExp("^cpu "));
QString line = list.first();
QStringList lines = line.trimmed().split(QRegExp("\\s+"));
unsigned long long user = lines.at(1).toLong();
unsigned long long nice = lines.at(2).toLong();
unsigned long long system = lines.at(3).toLong();
unsigned long long idle = lines.at(4).toLong();
unsigned long long iowait = lines.at(5).toLong();
unsigned long long irq = lines.at(6).toLong();
unsigned long long softirq = lines.at(7).toLong();
unsigned long long steal = lines.at(8).toLong();
//unsigned long long guest = lines.at(9).toLong();
//unsigned long long guestnice = lines.at(10).toLong();
workTime = user + nice + system;
totalTime = user + nice + system + idle + iowait + irq + softirq + steal;
file.close();
}
void Utils::getMemoryInfo(QString &memory, float &percent)
{
QFile file("/proc/meminfo");
file.open(QIODevice::ReadOnly);
QString buffer = file.readAll();
QStringList lines = buffer.split("\n").filter(QRegExp("^MemTotal|^MemAvailable|^SwapTotal|^SwapFree"));
QRegExp sep("\\s+");
unsigned long long memTotal = lines.at(0).split(sep).at(1).toLong();
unsigned long long memAvailable = lines.at(1).split(sep).at(1).toLong();
unsigned long long swapTotal = lines.at(2).split(sep).at(1).toLong();
unsigned long long swapFree = lines.at(3).split(sep).at(1).toLong();
memory = QString("%1 / %2").arg(formatBytes((memTotal - memAvailable) * 1024)).arg(formatBytes(memTotal * 1024));
percent = (memTotal - memAvailable) * 100.0 / memTotal;
file.close();
}
void Utils::getDiskInfo(QString &disk, float &percent)
{
QProcess *process = new QProcess;
process->start("df -Pl");
process->waitForFinished();
QString buffer = process->readAllStandardOutput();
QStringList result = buffer.trimmed().split(QChar('\n'));
long long size = 0, used = 0, free = 0;
long long totalSize = 0, totalFree = 0;
process->kill();
process->close();
for (const QString &line : result.filter(QRegExp("^/")))
{
QStringList slist = line.split(QRegExp("\\s+"));
size = slist.at(1).toLong() << 10;
used = slist.at(2).toLong() << 10;
free = slist.at(3).toLong() << 10;
totalSize += size;
totalFree += free;
}
disk = QString("%1 / %2").arg(formatBytes(totalSize - totalFree)).arg(formatBytes(totalSize));
percent = used * 100.0 / size;
}
void Utils::getNetworkBandWidth(unsigned long long &receiveBytes, unsigned long long &sendBytes)
{
QFile file("/proc/net/dev");
file.open(QIODevice::ReadOnly);
file.readLine();
file.readLine();
QString buffer;
receiveBytes = 0;
sendBytes = 0;
while ((buffer = file.readLine()) != nullptr)
{
QStringList lines = buffer.trimmed().split(QRegExp("\\s+"));
if (lines.first() != "lo:") {
receiveBytes += lines.at(1).toLong();
sendBytes += lines.at(9).toLong();
}
}
file.close();
}
QString Utils::formatBytes(unsigned long long bytes)
{
if (bytes < 1024)
return QString::number(bytes, 'r', 1) + "B";
else if (bytes / 1024 < 1024)
return QString::number(bytes / 1024.0, 'r', 1) + "KB";
else if (bytes / 1024 / 1024 < 1024)
return QString::number(bytes / 1024.0 / 1024.0, 'r', 1) + "MB";
else if (bytes / 1024 / 1024 / 1024 < 1024)
return QString::number(bytes / 1024.0 / 1024.0 / 1024.0, 'r', 1) + "GB";
}
quint64 Utils::getFileSize(const QString &path)
{
quint64 totalSize = 0;
QFileInfo info(path);
if (info.exists()) {
if (info.isFile()) {
totalSize += info.size();
}else if (info.isDir()) {
QDir dir(path);
for (const QFileInfo &i : dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs)) {
totalSize += getFileSize(i.absoluteFilePath());
}
}
}
return totalSize;
}
QFileInfoList Utils::getDpkgPackages()
{
QDir reports("/var/cache/apt/archives");
return reports.entryInfoList(QDir::Files);
}
QFileInfoList Utils::getCrashReports()
{
QDir reports("/var/crash");
return reports.entryInfoList(QDir::Files);
}
QFileInfoList Utils::getAppLogs()
{
QDir logs("/var/log");
return logs.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
}
QFileInfoList Utils::getAppCaches()
{
QDir caches(getHomePath() + "/.cache");
return caches.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
}
QString Utils::getHomePath()
{
return QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
}
QString exec(const QString &cmd, QStringList args)
{
QProcess *process = new QProcess;
if (args.isEmpty())
process->start(cmd);
else
process->start(cmd, args);
process->waitForFinished();
QString out = process->readAllStandardOutput();
QString error = process->errorString();
process->kill();
process->close();
if (process->error() != QProcess::UnknownError)
throw error;
return out.trimmed();
}
QString Utils::sudoExec(const QString &cmd, QStringList args)
{
args.push_front(cmd);
QString result("");
try {
result = exec("pkexec", args);
} catch (QString &ex) {
qCritical() << ex;
}
return result;
}