-
Notifications
You must be signed in to change notification settings - Fork 0
/
statusbar.cpp
59 lines (51 loc) · 1.36 KB
/
statusbar.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
#include "statusbar.h"
#include <QCoreApplication>
#include <QThread>
#include <QProcess>
#include <QDebug>
StatusBar::StatusBar(QStatusBar* status_bar, QObject* parent)
: QObject(parent), statusBar(status_bar), isAcpi(QProcess::execute("acpi") == 0), timer(new QTimer(this))
{
connect(timer, &QTimer::timeout, this, &StatusBar::updateStatusBar);
timer->start(1000); // Update every 1000 milliseconds (1 second)
}
StatusBar::~StatusBar()
{
if (timer->isActive())
timer->stop();
}
void StatusBar::updateStatusBar()
{
try
{
QDateTime current = QDateTime::currentDateTime();
QString statusText = current.toString("MM-dd-yyyy HH:mm");
if (isAcpi)
{
QString battery = getBatteryInfo();
statusText += "\t\t" + battery;
}
statusBar->showMessage(statusText);
}
catch (const std::exception& e)
{
qDebug() << "StatusBar.updateStatusBar";
qDebug() << e.what();
qDebug() << "message";
}
}
QString StatusBar::getBatteryInfo()
{
QProcess acpiProcess;
acpiProcess.start("acpi");
acpiProcess.waitForFinished();
if (acpiProcess.exitCode() == 0)
{
QByteArray batteryOutput = acpiProcess.readAllStandardOutput();
return batteryOutput.trimmed();
}
else
{
return "Error retrieving battery info";
}
}