diff --git a/LICENCE b/LICENCE index a17f27a..1472528 100644 --- a/LICENCE +++ b/LICENCE @@ -1,4 +1,4 @@ -Copyright © 2013 Mikko Ahlroth +Copyright © 2013–2014 Mikko Ahlroth Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/harbour-sailtime.pro b/harbour-sailtime.pro index 1b927b2..90b5379 100644 --- a/harbour-sailtime.pro +++ b/harbour-sailtime.pro @@ -10,10 +10,14 @@ TARGET = harbour-sailtime CONFIG += sailfishapp -HEADERS += src/uptimechecker.h +HEADERS += src/uptimechecker.h \ + src/sysinfoc.h \ + src/sysload.h SOURCES += src/harbour-sailtime.cpp \ - src/uptimechecker.cpp + src/uptimechecker.cpp \ + src/sysinfoc.cpp \ + src/sysload.cpp OTHER_FILES += qml/harbour-sailtime.qml \ qml/cover/CoverPage.qml \ diff --git a/qml/cover/CoverPage.qml b/qml/cover/CoverPage.qml index 9a6aeaf..ca72a13 100644 --- a/qml/cover/CoverPage.qml +++ b/qml/cover/CoverPage.qml @@ -13,46 +13,34 @@ CoverBackground { property alias cover : cover function updateCover(uptime) { - days.text = uptime.days + " days" - time.text = uptime.hours + " h " + uptime.minutes + " min" - load1.text = uptime.load1.toFixed(2) - load5_15.text = uptime.load5.toFixed(2) + " " + uptime.load15 -.toFixed(2) + var strings = formatUptime(uptime.days, uptime.hours, uptime.minutes); + + days.text = strings.days; + time.text = strings.hours + " " + strings.minutes; + load1.text = uptime.load1.toFixed(2); + load5.text = uptime.load5.toFixed(2); + load15.text = uptime.load15.toFixed(2); } Column { width: cover.width spacing: Theme.paddingSmall - - Label { - id: heading - anchors.horizontalCenter: parent.horizontalCenter - text: "Uptime" - color: "white" - font.pixelSize: Theme.fontSizeMedium - } - - Separator { - x: Theme.paddingLarge - width: parent.width - Theme.paddingLarge * 2 - horizontalAlignment: Qt.AlignCenter - color: Theme.highlightColor - } + y: Theme.paddingLarge Label { id: days anchors.horizontalCenter: parent.horizontalCenter text: "Refreshing..." - color: Theme.highlightColor - font.pixelSize: Theme.fontSizeSmall + color: Theme.primaryColor + font.family: Theme.fontFamilyHeading } Label { id: time anchors.horizontalCenter: parent.horizontalCenter text: "" - color: Theme.highlightColor - font.pixelSize: Theme.fontSizeSmall + color: Theme.primaryColor + font.family: Theme.fontFamilyHeading } Separator { @@ -62,20 +50,73 @@ CoverBackground { color: Theme.highlightColor } - Label { - id: load1 + Row { + width: parent.width - Theme.paddingLarge * 2 anchors.horizontalCenter: parent.horizontalCenter - text: "" - color: Theme.highlightColor - font.pixelSize: Theme.fontSizeSmall + spacing: 15 + + Label { + text: "1" + color: Theme.secondaryColor + horizontalAlignment: Qt.AlignRight + width: parent.width / 4 + font.family: Theme.fontFamilyHeading + } + + Label { + id: load1 + text: "" + color: Theme.primaryColor + horizontalAlignment: Qt.AlignLeft + width: 3 * parent.width / 4 + font.family: Theme.fontFamilyHeading + } } - Label { - id: load5_15 + Row { + width: parent.width - Theme.paddingLarge * 2 anchors.horizontalCenter: parent.horizontalCenter - text: "" - color: Theme.highlightColor - font.pixelSize: Theme.fontSizeSmall + spacing: 15 + + Label { + text: "5" + color: Theme.secondaryColor + horizontalAlignment: Qt.AlignRight + width: parent.width / 4 + font.family: Theme.fontFamilyHeading + } + + Label { + id: load5 + text: "" + color: Theme.primaryColor + horizontalAlignment: Qt.AlignLeft + width: 3 * parent.width / 4 + font.family: Theme.fontFamilyHeading + } + } + + Row { + width: parent.width - Theme.paddingLarge * 2 + anchors.horizontalCenter: parent.horizontalCenter + spacing: 15 + + Label { + text: "15" + color: Theme.secondaryColor + horizontalAlignment: Qt.AlignRight + width: parent.width / 4 + font.family: Theme.fontFamilyHeading + } + + Label { + id: load15 + text: "" + color: Theme.primaryColor + horizontalAlignment: Qt.AlignLeft + width: 3 * parent.width / 4 + font.family: Theme.fontFamilyHeading + } } } diff --git a/qml/harbour-sailtime.qml b/qml/harbour-sailtime.qml index 5b5a943..ad73feb 100644 --- a/qml/harbour-sailtime.qml +++ b/qml/harbour-sailtime.qml @@ -6,9 +6,12 @@ import QtQuick 2.0 import Sailfish.Silica 1.0 -import harbour.sailtime.UptimeChecker 1.0 +import harbour.sailtime.UptimeChecker 2.0 +import harbour.sailtime.SysinfoC 1.0 +import harbour.sailtime.Sysload 1.0 import "cover" import "pages" +import "js/storage.js" as Storage ApplicationWindow { @@ -17,7 +20,7 @@ ApplicationWindow // Refresh whenever app is activated onApplicationActiveChanged: { if (applicationActive) { - updateDisplay() + updateDisplay(); } } @@ -30,17 +33,21 @@ ApplicationWindow onChangeTimer: { if (enabled) { //console.log("Starting timer") - refreshTimer.start() + refreshTimer.start(); } else { //console.log("Stopping timer") - refreshTimer.stop() + refreshTimer.stop(); } } onChangeInterval: { //console.log("Timer interval: " + interval) - refreshTimer.interval = interval * 60 * 1000 + refreshTimer.interval = interval * 60 * 1000; + } + + onChangeRecord: { + firstpage.setRecordEnabled(enabled); } } @@ -61,40 +68,61 @@ ApplicationWindow } function updateDisplay() { - var uptime_str = checker.fetchUptime(); - uptime_str = uptime_str.replace(/^\s+|\s+$/g, ''); + Storage.db = Storage.connect(); - var uptime_re = /\d+:\d\d:\d\d up\s+(?:(\d+) days?,\s+)?(?:(?:(\d+):(\d+))|(?:(\d?\d) min)),\s+(\d+) users?,\s+load average: (\d+).(\d\d), (\d+).(\d\d), (\d+).(\d\d)/; - var match = uptime_str.match(uptime_re) - //console.log(uptime_str) + var sysinfo = checker.fetchUptime(); + var uptime_s = sysinfo.uptime; + var uptime_d = divideUptime(uptime_s); - var days = match[1] - if (days === undefined) { - days = 0 - } - - var hours = match[2] - if (hours === undefined) { - hours = 0 - } - - var minutes = match[3] - if (minutes === undefined) { - minutes = match[4] - } + var record = parseInt(Storage.readSetting(Storage.db, "record", 0)); + var record_d = divideUptime(record); var uptime = { - "days": days, - "hours": hours, - "minutes": minutes, - "users": match[5], - "load1": parseInt(match[6]) + match[7] / 100, - "load5": parseInt(match[8]) + match[9] / 100, - "load15": parseInt(match[10]) + match[11] / 100, + "days": uptime_d.days, + "hours": uptime_d.hours, + "minutes": uptime_d.minutes, + "load1": sysinfo.loads.avg_1, + "load5": sysinfo.loads.avg_5, + "load15": sysinfo.loads.avg_15, + "record": record, + "rDays": record_d.days, + "rHours": record_d.hours, + "rMinutes": record_d.minutes, } - firstpage.updatePage(uptime); + firstpage.updatePage(sysinfo, uptime); coverpage.updateCover(uptime); + + var recordEnable = Storage.readSetting(Storage.db, "recordEnable", false); + + // Update new record to db if feature is activated + if (recordEnable && (uptime_s >= record)) { + Storage.storeSetting(Storage.db, "record", uptime_s); + } + } + + // Divide uptime to days, hours and minutes + function divideUptime(seconds) { + var days = Math.floor(seconds / 84600); + var hours = Math.floor(seconds / 3600 % 24); + var minutes = Math.floor(seconds / 60 % 60); + return { + "days": days, + "hours": hours, + "minutes": minutes + }; + } + + // Return formatted language strings with plural forms + function formatUptime(days, hours, minutes) { + var days_str = days + " day" + ((days !== 1)? "s" : ""); + var hours_str = hours + " h"; + var minutes_str = minutes + " min"; + return { + "days": days_str, + "hours": hours_str, + "minutes": minutes_str + }; } UptimeChecker { diff --git a/qml/pages/AboutPage.qml b/qml/pages/AboutPage.qml index e9d318a..acaa628 100644 --- a/qml/pages/AboutPage.qml +++ b/qml/pages/AboutPage.qml @@ -29,7 +29,7 @@ Page { Label { anchors.horizontalCenter: parent.horizontalCenter - text: "SailTime 1.0.2" + text: "SailTime 1.1.1" color: Theme.highlightColor font.pixelSize: Theme.fontSizeLarge } diff --git a/qml/pages/FirstPage.qml b/qml/pages/FirstPage.qml index 090bc1d..1659f80 100644 --- a/qml/pages/FirstPage.qml +++ b/qml/pages/FirstPage.qml @@ -20,12 +20,89 @@ Page { updateDisplay(); } - function updatePage(uptime) { - days.text = uptime.days + " days" - time.text = uptime.hours + " h " + uptime.minutes + " min" - load1.text = uptime.load1.toFixed(2) - load5_15.text = uptime.load5.toFixed(2) + " " + uptime.load15 -.toFixed(2) + function updatePage(sysinfo, uptime) { + var strings = formatUptime(uptime.days, uptime.hours, uptime.minutes); + + days.text = strings.days; + time.text = strings.hours + " " + strings.minutes; + + if (sysinfo.uptime >= uptime.record) { + recordLabel.text = "Making new record!"; + } + else { + var rStrings = formatUptime(uptime.rDays, uptime.rHours, uptime.rMinutes); + recordLabel.text = "Record: " + rStrings.days + " " + rStrings.hours + " " + rStrings.minutes; + } + + + load1.text = uptime.load1.toFixed(2); + load5.text = uptime.load5.toFixed(2); + load15.text = uptime.load15.toFixed(2); + + + // Memory bar + + var meminuse = sysinfo.totalram - sysinfo.freeram; + + usedmem.width = (5 * usedmem.parent.width / 6 - Theme.paddingLarge * 2) * (meminuse / sysinfo.totalram); + freemem.width = (5 * freemem.parent.width / 6 - Theme.paddingLarge * 2) * (sysinfo.freeram / sysinfo.totalram); + + var usedram = scaleMem((sysinfo.totalram - sysinfo.freeram), sysinfo.mem_unit); + var totalram = scaleMem(sysinfo.totalram, sysinfo.mem_unit); + + memlegend.text = usedram[0].toFixed(2) + " " + usedram[1] + "B/" + + totalram[0].toFixed(2) + " " + totalram[1] + "B (" + + (usedram[0] / totalram[0] * 100).toFixed(2) + " %)"; + + + // Swap bar + + var swapinuse = sysinfo.totalswap - sysinfo.freeswap; + + usedswap.width = (5 * usedswap.parent.width / 6 - Theme.paddingLarge * 2) * (swapinuse / sysinfo.totalswap); + freeswap.width = (5 * freeswap.parent.width / 6 - Theme.paddingLarge * 2) * (sysinfo.freeswap / sysinfo.totalswap); + + var usedswap_scale = scaleMem((sysinfo.totalswap - sysinfo.freeswap), sysinfo.mem_unit); + var totalswap_scale = scaleMem(sysinfo.totalswap, sysinfo.mem_unit); + + swaplegend.text = usedswap_scale[0].toFixed(2) + " " + usedswap_scale[1] + "B/" + + totalswap_scale[0].toFixed(2) + " " + totalswap_scale[1] + "B (" + + (usedswap_scale[0] / totalswap_scale[0] * 100).toFixed(2) + " %)"; + + + // Processes amount + + procs.text = sysinfo.procs + " processes + threads"; + } + + + function setRecordEnabled(enable) + { + recordLabel.visible = enable; + } + + + // Scale memory sizes to human readable bytes according to mem_unit + // Return memory size in bytes and prefix + function scaleMem(mem, mem_unit) { + var scaled = mem * mem_unit; + + var size_factor = 1024; + var size_units = ['', 'ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi']; + + var remainder = 0; + + for (var i = 0; i < size_units.length; ++i) { + remainder = scaled / Math.pow(size_factor, i); + + if (remainder < size_factor) { + return [remainder, size_units[i]]; + } + } + + // If we have passed yottabytes in memory size, just rudely display as + // YB. + return [remainder, size_units[size_units.length - 1]]; } // To enable PullDownMenu, place our content in a SilicaFlickable @@ -57,18 +134,18 @@ Page { // of the page, followed by our content. Column { id: column - width: page.width - spacing: Theme.paddingLarge + spacing: Theme.paddingMedium + PageHeader { - title: "SailTime" + title: "Uptime" } Label { id: days anchors.horizontalCenter: parent.horizontalCenter text: "Refreshing..." - color: Theme.highlightColor + color: Theme.primaryColor font.pixelSize: Theme.fontSizeLarge } @@ -76,31 +153,169 @@ Page { id: time anchors.horizontalCenter: parent.horizontalCenter text: "" - color: Theme.highlightColor + color: Theme.primaryColor font.pixelSize: Theme.fontSizeLarge } - Separator { - x: Theme.paddingLarge + Label { + id: recordLabel + anchors.horizontalCenter: parent.horizontalCenter + text: "" + color: Theme.secondaryColor + font.pixelSize: Theme.fontSizeMedium + visible: false + } + + + PageHeader { + title: "Load averages" + } + + Row + { width: parent.width - Theme.paddingLarge * 2 - horizontalAlignment: Qt.AlignCenter - color: Theme.highlightColor + anchors.horizontalCenter: parent.horizontalCenter + spacing: 18 + + Label { + text: "1" + color: Theme.secondaryColor + horizontalAlignment: Qt.AlignRight + width: parent.width / 24 + } + + Label { + id: load1 + text: "" + color: Theme.primaryColor + width: 3 * parent.width / 12 + horizontalAlignment: Qt.AlignLeft + } + + Label { + text: "5" + color: Theme.secondaryColor + horizontalAlignment: Qt.AlignRight + width: parent.width / 24 + } + + Label { + id: load5 + text: "" + color: Theme.primaryColor + width: 3 * parent.width / 12 + horizontalAlignment: Qt.AlignLeft + } + + Label { + text: "15" + color: Theme.secondaryColor + horizontalAlignment: Qt.AlignRight + width: parent.width / 24 + } + + Label { + id: load15 + text: "" + color: Theme.primaryColor + width: 3 * parent.width / 12 + horizontalAlignment: Qt.AlignLeft + } + } + + PageHeader { + title: "System information" + } + + Item { + height: Theme.fontSizeLarge + width: parent.width + + Label { + id: memlabel + text: "Mem" + color: Theme.secondaryColor + width: parent.width / 6 + horizontalAlignment: Qt.AlignRight + } + + Rectangle { + anchors.left: memlabel.right + anchors.leftMargin: Theme.paddingLarge + id: usedmem + height: Theme.fontSizeLarge + width: 0 + color: Theme.highlightColor + } + + Rectangle { + anchors.left: usedmem.right + id: freemem + height: Theme.fontSizeLarge + width: 0 + color: Theme.secondaryHighlightColor + } + + Label { + anchors.left: memlabel.right + anchors.leftMargin: Theme.paddingLarge + id: memlegend + color: "black" + width: 5 * parent.width / 6 - Theme.paddingLarge * 2 + horizontalAlignment: Qt.AlignCenter + + // Overlay the label on the bar + z: 1 + font.pixelSize: Theme.fontSizeExtraSmall + } + } + + Item { + height: Theme.fontSizeLarge + width: parent.width + + Label { + id: swaplabel + text: "Swap" + color: Theme.secondaryColor + width: parent.width / 6 + horizontalAlignment: Qt.AlignRight + } + + Rectangle { + anchors.left: swaplabel.right + anchors.leftMargin: Theme.paddingLarge + id: usedswap + height: Theme.fontSizeLarge + width: 0 + color: Theme.highlightColor + } + + Rectangle { + anchors.left: usedswap.right + id: freeswap + height: Theme.fontSizeLarge + width: 0 + color: Theme.secondaryHighlightColor + } + + Label { + anchors.left: swaplabel.right + anchors.leftMargin: Theme.paddingLarge + id: swaplegend + color: "black" + width: 5 * parent.width / 6 - Theme.paddingLarge * 2 + horizontalAlignment: Qt.AlignCenter + + // Overlay the label on the bar + z: 1 + font.pixelSize: Theme.fontSizeExtraSmall + } } Label { - id: load1 + id: procs anchors.horizontalCenter: parent.horizontalCenter - text: "" - color: Theme.highlightColor - font.pixelSize: Theme.fontSizeLarge - } - - Label { - id: load5_15 - anchors.horizontalCenter: parent.horizontalCenter - text: "" - color: Theme.highlightColor - font.pixelSize: Theme.fontSizeLarge } } } diff --git a/qml/pages/SecondPage.qml b/qml/pages/SecondPage.qml index 360a02a..464aa48 100644 --- a/qml/pages/SecondPage.qml +++ b/qml/pages/SecondPage.qml @@ -14,6 +14,7 @@ Dialog { signal changeTimer(bool enabled) signal changeInterval(int interval) + signal changeRecord(bool enabled) property alias timerEnable : timerEnable property alias refreshInterval : intervalSlider @@ -29,7 +30,10 @@ Dialog { } timerEnable.checked = Storage.readSetting(Storage.db, - "timerEnable", false) + "timerEnable", false); + + recordEnable.checked = Storage.readSetting(Storage.db, + "recordEnable", false); } // Load values when app is started to start timers if needed @@ -37,6 +41,7 @@ Dialog { getSettings(); changeTimer(timerEnable.checked); changeInterval(intervalSlider.value); + changeRecord(recordEnable.checked); } onOpened: { @@ -46,13 +51,18 @@ Dialog { onAccepted: { Storage.storeSetting(Storage.db, "timerEnable", - timerEnable.checked) - changeTimer(timerEnable.checked) + timerEnable.checked); + changeTimer(timerEnable.checked); Storage.storeSetting(Storage.db, "interval", - intervalSlider.value) - changeInterval(intervalSlider.value) + intervalSlider.value); + changeInterval(intervalSlider.value); + + Storage.storeSetting(Storage.db, + "recordEnable", + recordEnable.checked); + changeRecord(recordEnable.checked); } SilicaFlickable { @@ -65,8 +75,8 @@ Dialog { spacing: Theme.paddingLarge DialogHeader { - title: "Settings" - acceptText: "Save" + title: "Save" + acceptText: "Settings" } TextSwitch { @@ -85,6 +95,30 @@ Dialog { valueText: value + " min" width: parent.width - Theme.paddingLarge * 2 } + + Label { + font.pixelSize: Theme.fontSizeSmall + color: Theme.secondaryColor + text: "Note that due to the power saving features of Sailfish OS, the update may not occur at the specified interval when the device is locked." + anchors.horizontalCenter: parent.horizontalCenter + width: parent.width - Theme.paddingLarge * 2 + wrapMode: Text.WordWrap + } + + TextSwitch { + id: recordEnable + checked: false + text: "Store uptime record" + } + + Label { + font.pixelSize: Theme.fontSizeSmall + color: Theme.secondaryColor + text: "Record is stored whenever the uptime is updated." + anchors.horizontalCenter: parent.horizontalCenter + width: parent.width - Theme.paddingLarge * 2 + wrapMode: Text.WordWrap + } } } } diff --git a/rpm/harbour-sailtime.spec b/rpm/harbour-sailtime.spec index f225fad..8e90210 100644 --- a/rpm/harbour-sailtime.spec +++ b/rpm/harbour-sailtime.spec @@ -13,18 +13,18 @@ Name: harbour-sailtime %{!?qtc_make:%define qtc_make make} %{?qtc_builddir:%define _builddir %qtc_builddir} Summary: SailTime -Version: 1.0.2 -Release: 1 +Version: 1.1.1 +Release: 2 Group: Qt/Qt License: MIT Expat licence URL: http://example.org/ Source0: %{name}-%{version}.tar.bz2 Source100: harbour-sailtime.yaml Requires: sailfishsilica-qt5 >= 0.10.9 -BuildRequires: pkgconfig(Qt5Quick) -BuildRequires: pkgconfig(Qt5Qml) -BuildRequires: pkgconfig(Qt5Core) BuildRequires: pkgconfig(sailfishapp) >= 0.0.10 +BuildRequires: pkgconfig(Qt5Core) +BuildRequires: pkgconfig(Qt5Qml) +BuildRequires: pkgconfig(Qt5Quick) BuildRequires: desktop-file-utils %description @@ -64,13 +64,13 @@ desktop-file-install --delete-original \ %files %defattr(-,root,root,-) -%{_bindir} -%{_datadir}/%{name}/qml -%{_datadir}/applications/%{name}.desktop -%{_datadir}/icons/hicolor/86x86/apps/%{name}.png -/usr/bin -/usr/share/harbour-sailtime -/usr/share/applications /usr/share/icons/hicolor/86x86/apps +/usr/share/applications +/usr/share/harbour-sailtime +/usr/bin +%{_datadir}/icons/hicolor/86x86/apps/%{name}.png +%{_datadir}/applications/%{name}.desktop +%{_datadir}/%{name}/qml +%{_bindir} # >> files # << files diff --git a/rpm/harbour-sailtime.yaml b/rpm/harbour-sailtime.yaml index 3371290..c1ff5ef 100644 --- a/rpm/harbour-sailtime.yaml +++ b/rpm/harbour-sailtime.yaml @@ -1,7 +1,7 @@ Name: harbour-sailtime Summary: SailTime -Version: 1.0.2 -Release: 1 +Version: 1.1.1 +Release: 2 Group: Qt/Qt URL: http://example.org/ License: "MIT Expat licence" @@ -14,19 +14,19 @@ Description: |- Configure: none Builder: qtc5 PkgConfigBR: -- Qt5Quick -- Qt5Qml -- Qt5Core - sailfishapp >= 0.0.10 +- Qt5Core +- Qt5Qml +- Qt5Quick Requires: - sailfishsilica-qt5 >= 0.10.9 Files: -- '%{_bindir}' -- '%{_datadir}/%{name}/qml' -- '%{_datadir}/applications/%{name}.desktop' -- '%{_datadir}/icons/hicolor/86x86/apps/%{name}.png' -- /usr/bin -- /usr/share/harbour-sailtime -- /usr/share/applications - /usr/share/icons/hicolor/86x86/apps +- /usr/share/applications +- /usr/share/harbour-sailtime +- /usr/bin +- '%{_datadir}/icons/hicolor/86x86/apps/%{name}.png' +- '%{_datadir}/applications/%{name}.desktop' +- '%{_datadir}/%{name}/qml' +- '%{_bindir}' PkgBR: [] diff --git a/src/harbour-sailtime.cpp b/src/harbour-sailtime.cpp index 30574af..b1d88ee 100644 --- a/src/harbour-sailtime.cpp +++ b/src/harbour-sailtime.cpp @@ -5,10 +5,13 @@ */ #include +#include #include #include "uptimechecker.h" +#include "sysinfoc.h" +#include "sysload.h" int main(int argc, char *argv[]) @@ -22,7 +25,11 @@ int main(int argc, char *argv[]) // // To display the view, call "show()" (will show fullscreen on device). - qmlRegisterType("harbour.sailtime.UptimeChecker", 1, 0, "UptimeChecker"); + // Register needed metatypes + qmlRegisterType("harbour.sailtime.UptimeChecker", 2, 0, "UptimeChecker"); + qmlRegisterUncreatableType("harbour.sailtime.SysinfoC", 1, 0, "SysinfoC", "Cannot create sysinfo"); + qmlRegisterUncreatableType("harbour.sailtime.Sysload", 1, 0, "Sysload", "Cannot create sysload"); + return SailfishApp::main(argc, argv); } diff --git a/src/sysinfoc.cpp b/src/sysinfoc.cpp new file mode 100644 index 0000000..a18750c --- /dev/null +++ b/src/sysinfoc.cpp @@ -0,0 +1,90 @@ +#include "sysinfoc.h" +#include + +SysinfoC::SysinfoC(const struct sysinfo* info, QObject* parent) : + QObject(parent), + uptime(info->uptime), + totalram(info->totalram), + freeram(info->freeram), + sharedram(info->sharedram), + bufferram(info->bufferram), + totalswap(info->totalswap), + freeswap(info->freeswap), + procs(info->procs), + totalhigh(info->totalhigh), + freehigh(info->freehigh), + mem_unit(info->mem_unit) +{ + float in_loads[3] = {0.0, 0.0, 0.0}; + in_loads[0] = load2Float(info->loads[0]); + in_loads[1] = load2Float(info->loads[1]); + in_loads[2] = load2Float(info->loads[2]); + + this->loads = new Sysload(in_loads, this); +} + +float SysinfoC::load2Float(unsigned long load) +{ + // As per http://stackoverflow.com/a/4993273 + return load / ((float) (1 << SI_LOAD_SHIFT)); +} + +long SysinfoC::getUptime() const +{ + return this->uptime; +} + +Sysload* SysinfoC::getLoads() const +{ + return this->loads; +} + +unsigned long SysinfoC::getTotalram() const +{ + return this->totalram; +} + +unsigned long SysinfoC::getFreeram() const +{ + return this->freeram; +} + +unsigned long SysinfoC::getSharedram() const +{ + return this->sharedram; +} + +unsigned long SysinfoC::getBufferram() const +{ + return this->bufferram; +} + +unsigned long SysinfoC::getTotalswap() const +{ + return this->totalswap; +} + +unsigned long SysinfoC::getFreeswap() const +{ + return this->freeswap; +} + +unsigned short SysinfoC::getProcs() const +{ + return this->procs; +} + +unsigned long SysinfoC::getTotalhigh() const +{ + return this->totalhigh; +} + +unsigned long SysinfoC::getFreehigh() const +{ + return this->freehigh; +} + +unsigned int SysinfoC::getMemunit() const +{ + return this->mem_unit; +} diff --git a/src/sysinfoc.h b/src/sysinfoc.h new file mode 100644 index 0000000..c01ef0b --- /dev/null +++ b/src/sysinfoc.h @@ -0,0 +1,70 @@ +#ifndef SYSINFO_H +#define SYSINFO_H + +#include +#include + +#include "sysload.h" + +/* + * This class exists because we cannot pass plain structs onto the QML side, + * we need to inherit from QObject. + */ +class SysinfoC : public QObject +{ + Q_OBJECT +public: + explicit SysinfoC(const struct sysinfo* info, QObject* parent = 0); + + /* + * Loads are reported as unsigned longs in the sysinfo struct, this function + * will convert them to floats which are usually used for displaying loads. + */ + static float load2Float(unsigned long load); + + Q_PROPERTY(long uptime READ getUptime) + Q_PROPERTY(Sysload* loads READ getLoads) + Q_PROPERTY(unsigned long totalram READ getTotalram) + Q_PROPERTY(unsigned long freeram READ getFreeram) + Q_PROPERTY(unsigned long sharedram READ getSharedram) + Q_PROPERTY(unsigned long bufferram READ getBufferram) + Q_PROPERTY(unsigned long totalswap READ getTotalswap) + Q_PROPERTY(unsigned long freeswap READ getFreeswap) + Q_PROPERTY(unsigned short procs READ getProcs) + Q_PROPERTY(unsigned long totalhigh READ getTotalhigh) + Q_PROPERTY(unsigned long freehigh READ getFreehigh) + Q_PROPERTY(unsigned int mem_unit READ getMemunit) + + long getUptime() const; + Sysload* getLoads() const; + unsigned long getTotalram() const; + unsigned long getFreeram() const; + unsigned long getSharedram() const; + unsigned long getBufferram() const; + unsigned long getTotalswap() const; + unsigned long getFreeswap() const; + unsigned short getProcs() const; + unsigned long getTotalhigh() const; + unsigned long getFreehigh() const; + unsigned int getMemunit() const; + +signals: + +public slots: + +private: + long uptime; /* Seconds since boot */ + Sysload* loads; /* 1, 5, and 15 minute load averages */ + unsigned long totalram; /* Total usable main memory size */ + unsigned long freeram; /* Available memory size */ + unsigned long sharedram; /* Amount of shared memory */ + unsigned long bufferram; /* Memory used by buffers */ + unsigned long totalswap; /* Total swap space size */ + unsigned long freeswap; /* swap space still available */ + unsigned short procs; /* Number of current processes */ + unsigned long totalhigh; /* Total high memory size */ + unsigned long freehigh; /* Available high memory size */ + unsigned int mem_unit; /* Memory unit size in bytes */ +}; + +#endif // SYSINFO_H diff --git a/src/sysload.cpp b/src/sysload.cpp new file mode 100644 index 0000000..fefec18 --- /dev/null +++ b/src/sysload.cpp @@ -0,0 +1,24 @@ +#include "sysload.h" + +Sysload::Sysload(float loads[3], QObject *parent) : + QObject(parent), + avg_1(loads[0]), + avg_5(loads[1]), + avg_15(loads[2]) +{ +} + +float Sysload::getAvg1() const +{ + return avg_1; +} + +float Sysload::getAvg5() const +{ + return avg_5; +} + +float Sysload::getAvg15() const +{ + return avg_15; +} diff --git a/src/sysload.h b/src/sysload.h new file mode 100644 index 0000000..0eb39f1 --- /dev/null +++ b/src/sysload.h @@ -0,0 +1,33 @@ +#ifndef SYSLOAD_H +#define SYSLOAD_H + +#include +#include + +class Sysload : public QObject +{ + Q_OBJECT +public: + explicit Sysload(float loads[3], QObject* parent = 0); + + Q_PROPERTY(float avg_1 READ getAvg1) + Q_PROPERTY(float avg_5 READ getAvg5) + Q_PROPERTY(float avg_15 READ getAvg15) + + float getAvg1() const; + float getAvg5() const; + float getAvg15() const; + +signals: + +public slots: + +private: + float avg_1; + float avg_5; + float avg_15; + + +}; + +#endif // SYSLOAD_H diff --git a/src/uptimechecker.cpp b/src/uptimechecker.cpp index 253072b..b519eea 100644 --- a/src/uptimechecker.cpp +++ b/src/uptimechecker.cpp @@ -11,11 +11,9 @@ UptimeChecker::UptimeChecker(QObject *parent) : QObject(parent) {} -QString UptimeChecker::fetchUptime() +SysinfoC* UptimeChecker::fetchUptime() { - QProcess process; - process.start("uptime"); - process.waitForFinished(-1); - QByteArray out = process.readAllStandardOutput(); - return QString(out); + struct sysinfo info; + sysinfo(&info); + return new SysinfoC(&info, this); } diff --git a/src/uptimechecker.h b/src/uptimechecker.h index 1898e82..2357e50 100644 --- a/src/uptimechecker.h +++ b/src/uptimechecker.h @@ -8,8 +8,8 @@ #define UPTIMECHECKER_H #include -#include -#include +#include +#include "sysinfoc.h" class UptimeChecker : public QObject { @@ -17,7 +17,7 @@ class UptimeChecker : public QObject public: explicit UptimeChecker(QObject *parent = 0); - Q_INVOKABLE QString fetchUptime(); + Q_INVOKABLE SysinfoC* fetchUptime(); }; #endif // UPTIMECHECKER_H