-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystemmanager.cpp
More file actions
195 lines (171 loc) · 5.94 KB
/
Copy pathsystemmanager.cpp
File metadata and controls
195 lines (171 loc) · 5.94 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
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
#include "systemmanager.h"
#include <QDir>
#include <QFileInfo>
#include <QStorageInfo>
#include <QVariantMap>
SystemManager::SystemManager(QObject *parent) : QObject(parent) {}
void SystemManager::shutdown()
{
QProcess::startDetached("/bin/systemctl", {"poweroff"});
}
void SystemManager::reboot()
{
QProcess::startDetached("/bin/systemctl", {"reboot"});
}
void SystemManager::powerOff()
{
QProcess::startDetached("/bin/systemctl", {"poweroff"});
}
void SystemManager::emergencyStop()
{
QProcess::startDetached("/bin/systemctl", {"halt"});
}
void SystemManager::launchTerminal()
{
startShell();
}
void SystemManager::launchFileManager()
{
// handled by embedded file browser in QML
}
void SystemManager::executeCommand(const QString &command, const QStringList &args)
{
QProcess *proc = new QProcess(this);
proc->setProcessChannelMode(QProcess::MergedChannels);
connect(proc, &QProcess::readyReadStandardOutput, this, [this, proc]() {
emit shellOutput(QString::fromUtf8(proc->readAllStandardOutput()));
});
connect(proc, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, [this, proc](int code, QProcess::ExitStatus) {
// Flush any remaining output before signalling completion
QString remaining = QString::fromUtf8(proc->readAllStandardOutput());
if (!remaining.isEmpty())
emit shellOutput(remaining);
emit commandFinished(code);
proc->deleteLater();
});
connect(proc, &QProcess::errorOccurred, this, [this, proc](QProcess::ProcessError) {
emit commandError(proc->errorString());
proc->deleteLater();
});
proc->start(command, args);
}
void SystemManager::startShell()
{
if (shellProcess) return;
shellProcess = new QProcess(this);
shellProcess->setProcessChannelMode(QProcess::MergedChannels);
connect(shellProcess, &QProcess::readyReadStandardOutput, this, [this]() {
emit shellOutput(QString::fromUtf8(shellProcess->readAllStandardOutput()));
});
shellProcess->start("/bin/sh", {"-i"});
}
void SystemManager::stopShell()
{
if (shellProcess) {
shellProcess->kill();
shellProcess->deleteLater();
shellProcess = nullptr;
}
}
void SystemManager::sendShellCommand(const QString &cmd)
{
if (shellProcess && shellProcess->state() == QProcess::Running)
shellProcess->write((cmd + "\n").toUtf8());
}
QVariantList SystemManager::listDirectory(const QString &path)
{
QVariantList result;
QDir dir(path);
if (!dir.exists()) return result;
if (path != "/") {
QVariantMap parent;
parent["name"] = "..";
parent["isDir"] = true;
parent["size"] = "";
result.append(parent);
}
dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot | QDir::Hidden);
dir.setSorting(QDir::DirsFirst | QDir::Name | QDir::IgnoreCase);
for (const QFileInfo &info : dir.entryInfoList()) {
QVariantMap entry;
entry["name"] = info.fileName();
entry["isDir"] = info.isDir();
if (info.isDir()) {
entry["size"] = "";
} else {
qint64 sz = info.size();
if (sz < 1024)
entry["size"] = QString("%1B").arg(sz);
else if (sz < 1024 * 1024)
entry["size"] = QString("%1K").arg(sz / 1024);
else
entry["size"] = QString("%1M").arg(sz / (1024 * 1024));
}
result.append(entry);
}
return result;
}
bool SystemManager::isDir(const QString &path)
{
return QFileInfo(path).isDir();
}
void SystemManager::playFile(const QString &path)
{
emit shellOutput(QString("[PLAY] %1\n").arg(path));
}
QString SystemManager::getSystemUptime()
{
QProcess proc;
proc.start("/bin/sh", {"-c", "uptime | awk -F'up' '{print $2}' | cut -d',' -f1"});
proc.waitForFinished(2000);
return QString::fromUtf8(proc.readAllStandardOutput()).trimmed();
}
QString SystemManager::getSystemLoad()
{
QProcess proc;
proc.start("/bin/sh", {"-c", "cat /proc/loadavg | awk '{print $1, $2, $3}'"});
proc.waitForFinished(2000);
return QString::fromUtf8(proc.readAllStandardOutput()).trimmed();
}
QString SystemManager::getDiskUsage(const QString &path)
{
QStorageInfo storage(path);
if (!storage.isValid())
return QStringLiteral("N/A");
constexpr qint64 MB = 1024LL * 1024;
constexpr qint64 GB = 1024LL * 1024 * 1024;
auto fmt = [MB, GB](qint64 bytes) -> QString {
if (bytes < GB)
return QString("%1M").arg(bytes / MB);
return QString("%1G").arg(bytes / GB);
};
return QString("%1 used: %2 avail: %3")
.arg(fmt(storage.bytesTotal()),
fmt(storage.bytesTotal() - storage.bytesAvailable()),
fmt(storage.bytesAvailable()));
}
void SystemManager::restartService(const QString &serviceName)
{
QProcess::startDetached("/bin/systemctl", {"restart", serviceName});
emit shellOutput(QString("[SERVICE] Restarting %1...\n").arg(serviceName));
}
QString SystemManager::getServiceStatus(const QString &serviceName)
{
QProcess proc;
proc.start("/bin/systemctl", {"is-active", serviceName});
proc.waitForFinished(2000);
return QString::fromUtf8(proc.readAllStandardOutput()).trimmed();
}
QString SystemManager::runShellCommand(const QString &cmd)
{
QProcess proc;
proc.setProcessChannelMode(QProcess::MergedChannels);
QString safeCmd = cmd;
safeCmd.replace(";", " ").replace("&&", " ").replace("||", " ");
safeCmd.replace("|", " ").replace(">", " ").replace("<", " ");
safeCmd.replace("`", " ").replace("$", " ").replace("(", " ").replace(")", " ");
proc.start("/bin/sh", {"-c", safeCmd});
proc.waitForFinished(3000);
return QString::fromUtf8(proc.readAllStandardOutput()).trimmed();
}