Commit fcd6a30d authored by David Redondo's avatar David Redondo 🏎
Browse files

Add uptime sensor to system information plugin

The old update method is renamed to the more accurate "init". Updating of the uptime
happens in a new update() method. Because it's only a few lines I just #ifdef'ed them.
parent 9604ca5a
......@@ -21,6 +21,11 @@
#include <systemstats/SensorObject.h>
#include <systemstats/SensorProperty.h>
#ifdef Q_OS_LINUX
#include <sys/sysinfo.h>
#endif
#include <time.h>
// Uppercase the first letter of each word.
QString upperCaseFirst(const QString &input)
{
......@@ -61,6 +66,7 @@ public:
virtual ~OSInfoPrivate() = default;
virtual void update();
virtual void init();
OSInfoPlugin *q;
......@@ -78,6 +84,7 @@ public:
KSysGuard::SensorProperty *osPrettyNameProperty = nullptr;
KSysGuard::SensorProperty *osLogoProperty = nullptr;
KSysGuard::SensorProperty *osUrlProperty = nullptr;
KSysGuard::SensorProperty *uptimeProperty = nullptr;
KSysGuard::SensorObject *plasmaObject = nullptr;
KSysGuard::SensorProperty *qtVersionProperty = nullptr;
......@@ -90,7 +97,7 @@ class LinuxPrivate : public OSInfoPrivate
public:
LinuxPrivate(OSInfoPlugin *qq) : OSInfoPrivate(qq) { }
void update() override;
void init() override;
};
OSInfoPrivate::OSInfoPrivate(OSInfoPlugin *qq)
......@@ -112,6 +119,8 @@ OSInfoPrivate::OSInfoPrivate(OSInfoPlugin *qq)
osPrettyNameProperty->setShortName(i18nc("@title Operating System Name and Version", "OS"));
osLogoProperty = new KSysGuard::SensorProperty(QStringLiteral("logo"), i18nc("@title", "Operating System Logo"), QString{}, systemObject);
osUrlProperty = new KSysGuard::SensorProperty(QStringLiteral("url"), i18nc("@title", "Operating System URL"), QString{}, systemObject);
uptimeProperty = new KSysGuard::SensorProperty(QStringLiteral("uptime"), i18nc("@title", "Uptime"), QString{}, systemObject);
uptimeProperty->setUnit(KSysGuard::UnitTime);
plasmaObject = new KSysGuard::SensorObject(QStringLiteral("plasma"), i18nc("@title", "KDE Plasma"), container);
qtVersionProperty = new KSysGuard::SensorProperty(QStringLiteral("qtVersion"), i18nc("@title", "Qt Version"), QString{}, plasmaObject);
......@@ -121,7 +130,7 @@ OSInfoPrivate::OSInfoPrivate(OSInfoPlugin *qq)
OSInfoPlugin::~OSInfoPlugin() = default;
void OSInfoPrivate::update()
void OSInfoPrivate::init()
{
auto kernelName = upperCaseFirst(QSysInfo::kernelType());
kernelNameProperty->setValue(kernelName);
......@@ -157,9 +166,23 @@ void OSInfoPrivate::update()
);
}
void LinuxPrivate::update()
void OSInfoPrivate::update()
{
#if defined Q_OS_LINUX
struct sysinfo info;
sysinfo(&info);
// can't send a long over the bus
uptimeProperty->setValue(QVariant::fromValue<qlonglong>(info.uptime));
#elif defined Q_OS_FREEBSD
timespec time;
clock_gettime(CLOCK_UPTIME, &time);
uptimeProperty->setValue(QVariant::fromValue<qlonglong>(time.tv_sec));
#endif
}
void LinuxPrivate::init()
{
OSInfoPrivate::update();
OSInfoPrivate::init();
// Override some properties with values from hostnamed, if available.
dbusCall<QVariantMap>(
......@@ -199,6 +222,11 @@ OSInfoPlugin::OSInfoPlugin(QObject *parent, const QVariantList &args)
#else
d = std::make_unique<OSInfoPrivate>(this);
#endif
d->init();
}
void OSInfoPlugin::update()
{
d->update();
}
......
......@@ -22,6 +22,8 @@ public:
return QStringLiteral("osinfo");
}
void update() override;
private:
std::unique_ptr<OSInfoPrivate> d;
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment