-
Notifications
You must be signed in to change notification settings - Fork 4
/
packagemanager.cpp
95 lines (72 loc) · 2.31 KB
/
packagemanager.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
#include "packagemanager.h"
#include <QFile>
PackageManager::PackageManager(const QString &binaryPath, PackageModel *packageModel, Terminal terminal, QObject *parent)
: QObject{parent}
{
m_preferredTerminal = terminal;
m_pacmanBinary = binaryPath;
m_model = packageModel;
populateInstalledPackages();
}
bool PackageManager::installPackages()
{
Terminal terminal = getTerminal();
if (terminal.binary.isEmpty()) {
qWarning() << tr("No supported terminal found, cannot install packages");
return false;
}
const QStringList packageList = m_model->getCheckedPackages();
if (packageList.isEmpty()) {
return false;
}
QProcess proc;
proc.setProgram(terminal.binary);
proc.setArguments(QStringList() << terminal.args << "pkexec" << "bash" << "-c" << m_pacmanBinary + " -Syu " + packageList.join(" ") + " ; read -p '" + tr("Press enter to resume") + "...'");
proc.start();
proc.waitForFinished(-1);
// Update the list of installed packages and refresh the model and the view
populateInstalledPackages();
m_model->refresh(m_installedPackages);
if( proc.exitCode() == 0) {
return true;
} else {
return false;
}
}
bool PackageManager::populateInstalledPackages()
{
if (m_pacmanBinary.isEmpty()) {
qWarning() << tr("ERROR: Package Manager not set");
return false;
}
m_installedPackages.clear();
QProcess proc;
proc.setProgram(m_pacmanBinary);
proc.setArguments({"-Qq"});
proc.start();
if (!proc.waitForFinished()) {
qWarning() << tr("Failed to start process ") << proc.program() << proc.arguments();
return false;
}
QString output = proc.readAllStandardOutput();
const QStringList lines = output.split('\n');
for (const QString &line : lines) {
if (!line.trimmed().isEmpty()) {
m_installedPackages.append(line.trimmed());
}
}
return true;
}
Terminal PackageManager::getTerminal()
{
if (QFile::exists(m_preferredTerminal.binary)) {
return m_preferredTerminal;
}
// Iterate over the list until we find an installed terminal
for (const Terminal &terminal : m_terminalList) {
if (QFile::exists(terminal.binary)) {
return terminal;
}
}
return Terminal();
}