Skip to content

Commit c6ef56a

Browse files
committed
v3.1.0 systemdD service list
1 parent 1d31b4e commit c6ef56a

3 files changed

Lines changed: 74 additions & 48 deletions

File tree

ltm.cpp

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
#include <QDir>
1616
#include <cmath>
1717
#include <cstdlib>
18+
#include <QObject>
19+
#include <QProcess>
20+
#include <QTableWidget>
21+
#include <QTableWidgetItem>
22+
#include <QJsonObject>
23+
#include <QJsonArray>
24+
#include <QJsonDocument>
1825

1926

2027
LTM::LTM(QWidget *parent)
@@ -29,6 +36,15 @@ LTM::LTM(QWidget *parent)
2936
connect(ui->actionInfo,&QAction::triggered,this, &LTM::printInfo);
3037

3138

39+
// Setup headers once
40+
ui->systemdTable->setColumnCount(5);
41+
ui->systemdTable->setHorizontalHeaderLabels({"UNIT", "LOAD", "ACTIVE", "SUB", "DESCRIPTION"});
42+
ui->systemdTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
43+
connect(&serviceUpdater, &QTimer::timeout, this, &LTM::load_services);
44+
serviceUpdater.setInterval(1000);
45+
serviceUpdater.start();
46+
47+
3248

3349
//setting up plots
3450
try {
@@ -246,7 +262,8 @@ void LTM::printInfo()
246262
ui->tabWidget->setCurrentIndex(0);
247263

248264
ui->updateText->appendPlainText("Authored by Juergen Kratochwill 2020");
249-
ui->updateText->appendPlainText("Published under GPLv3");
265+
ui->updateText->appendPlainText("Version 3.1.0");
266+
ui->updateText->appendPlainText("Published under GPLv3, no liabilites or whatsover are taken");
250267
ui->updateText->appendPlainText("All Linux and QT related Licesing info can be accesed under: ");
251268
QDir license(qApp->applicationDirPath());
252269
license.cdUp();
@@ -389,3 +406,42 @@ void LTM::on_butKillProcess_clicked()
389406
QString terminateID = ui->tableWProcess->selectedItems()[0]->data(Qt::DisplayRole).toString();
390407
system((std::string("kill -9 ")+ terminateID.toStdString()).c_str());
391408
}
409+
410+
void LTM::load_services() {
411+
QProcess process;
412+
process.start("systemctl", {"list-units", "--type=service", "--all", "--no-pager", "--output=json"});
413+
if (!process.waitForFinished(5000)) {
414+
// handle error
415+
return;
416+
}
417+
418+
QByteArray jsonOutput = process.readAllStandardOutput();
419+
QJsonDocument doc = QJsonDocument::fromJson(jsonOutput);
420+
421+
if (!doc.isArray()) return;
422+
423+
QJsonArray array = doc.array();
424+
ui->systemdTable->clearContents();
425+
ui->systemdTable->setRowCount(array.size());
426+
427+
int row = 0;
428+
for (const QJsonValue& val : array) {
429+
if (!val.isObject()) continue;
430+
QJsonObject obj = val.toObject();
431+
432+
QString unit = obj["unit"].toString();
433+
QString load = obj["load"].toString();
434+
QString active = obj["active"].toString();
435+
QString sub = obj["sub"].toString();
436+
QString description = obj["description"].toString();
437+
438+
ui->systemdTable->setItem(row, 0, new QTableWidgetItem(unit));
439+
ui->systemdTable->setItem(row, 1, new QTableWidgetItem(load));
440+
ui->systemdTable->setItem(row, 2, new QTableWidgetItem(active));
441+
ui->systemdTable->setItem(row, 3, new QTableWidgetItem(sub));
442+
ui->systemdTable->setItem(row, 4, new QTableWidgetItem(description));
443+
row++;
444+
}
445+
446+
ui->systemdTable->setRowCount(row); // In case some rows were skipped
447+
}

ltm.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "diskitemwidget.h"
1818
#include "processstatreader.h"
1919
#include <QProcess>
20+
#include <QTimer>
2021

2122

2223
QT_BEGIN_NAMESPACE
@@ -51,10 +52,10 @@ private slots:
5152

5253
void on_butUpdateOK_clicked();
5354

54-
55-
5655
void on_butKillProcess_clicked();
5756

57+
void load_services();
58+
5859
private:
5960
Ui::LTM *ui;
6061

@@ -102,6 +103,9 @@ private slots:
102103

103104
QProcess* updateprocess;
104105

106+
//service update
107+
QTimer serviceUpdater;
108+
105109

106110

107111

0 commit comments

Comments
 (0)