Skip to content

Commit

Permalink
feat(data): allow kilobyte for network unit
Browse files Browse the repository at this point in the history
This improve #9
  • Loading branch information
orblazer committed Dec 4, 2021
1 parent fcce8b1 commit 3110650
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 153 deletions.
6 changes: 3 additions & 3 deletions package/contents/config/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<entry name="updateInterval" type="Double">
<default>1.0</default>
</entry>
<entry name="networkUnit" type="String">
<default>kibibyte</default>
</entry>

<entry name="actionService" type="String">
<default>org.kde.plasma-systemmonitor</default>
Expand Down Expand Up @@ -40,9 +43,6 @@
</entry>
<entry name="ignoredNetworkInterfaces" type="StringList">
</entry>
<entry name="networkInKilobit" type="Bool">
<default>false</default>
</entry>
<entry name="downloadMaxKiBps" type="Double">
<default>100000.0</default>
</entry>
Expand Down
Binary file not shown.
116 changes: 96 additions & 20 deletions package/contents/ui/components/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,106 @@ function getPercentUsage(current, max) {
return isNaN(x) ? 0 : Math.min(x, 1);
}

function humanReadableBits(value) {
if (isNaN(parseInt(value))) {
return "";
function getNetworkUnit(type) {
switch (type) {
case "kilobyte":
return {
suffix: i18nc("kilobyte suffix", "Bps"),
KiBDiff: 1.024,
multiplier: 1024,
};
case "kilobit":
return {
suffix: i18nc("kilobit suffix", "bps"),
KiBDiff: 8.192,
multiplier: 1024,
};
default:
return {
suffix: i18nc("kibibyte suffix", "iB/s"),
KiBDiff: 1,
multiplier: 1024,
};
}
}

function getSpeedOptions(type) {
var unit = getNetworkUnit(type);

if (value < 1000) {
return Math.max(value, 0) + " bps";
return [
{
label: i18n("Custom"),
value: -1.0,
},
{
label: "100 K" + unit.suffix,
value: 100 * unit.KiBDiff,
rawValue: 0.1,
},
{
label: "1 M" + unit.suffix,
value: 1000 * unit.KiBDiff,
rawValue: 1.0,
},
{
label: "10 M" + unit.suffix,
value: 10000 * unit.KiBDiff,
rawValue: 10.0,
},
{
label: "100 M" + unit.suffix,
value: 100000 * unit.KiBDiff,
rawValue: 100.0,
},
{
label: "1 G" + unit.suffix,
value: 1000000 * unit.KiBDiff,
rawValue: 1000.0,
},
{
label: "2.5 G" + unit.suffix,
value: 2500000 * unit.KiBDiff,
rawValue: 2500.0,
},
{
label: "5 G" + unit.suffix,
value: 5000000 * unit.KiBDiff,
rawValue: 5000.0,
},
{
label: "10 G" + unit.suffix,
value: 10000000 * unit.KiBDiff,
rawValue: 10000.0,
},
];
}

function humanReadableNetworkSpeed(kibibytes, decimals = 2, type = "kibibyte") {
var unit = getNetworkUnit(type);

if (kibibytes === 0 || isNaN(parseInt(kibibytes))) {
return 0 + " " + unit.suffix.replace("i", "");
}

var i = -1;
var byteUnits = [
" Kbps",
" Mbps",
" Gbps",
" Tbps",
" Pbps",
" Ebps",
" Zbps",
" Ybps",
// Convert from kibibyte to right unit
var value = kibibytes * 1024 * unit.KiBDiff;

decimals = decimals < 0 ? 0 : decimals;
var sizes = [
unit.suffix,
(type === "kibibyte" ? "K" : "k") + unit.suffix,
"M" + unit.suffix,
"G" + unit.suffix,
"T" + unit.suffix,
"P" + unit.suffix,
"Z" + unit.suffix,
"Y" + unit.suffix,
];
do {
value = value / 1000;
i++;
} while (value > 1000);

return Math.round(Math.max(value, 0) * 10) / 10 + byteUnits[i];
var i = Math.floor(Math.log(value) / Math.log(unit.multiplier));
return (
parseFloat((value / Math.pow(unit.multiplier, i)).toFixed(decimals)) +
" " +
sizes[i]
);
}
126 changes: 37 additions & 89 deletions package/contents/ui/config/ConfigData.qml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.kde.plasma.components 2.0 as PlasmaComponents

import "../components" as RMComponents
import "../controls" as RMControls
import "../components/functions.js" as Functions

QtLayouts.ColumnLayout {
id: dataPage
Expand All @@ -15,79 +16,12 @@ QtLayouts.ColumnLayout {

property alias cfg_memoryUseAllocated: memoryUseAllocated.checked
property alias cfg_memorySwapGraph: memorySwapGraph.checked
property alias cfg_networkInKilobit: networkInKilobit.checked

property var networkUnit: Functions.getNetworkUnit(plasmoid.configuration.networkUnit)
property var networkSpeedOptions: Functions.getSpeedOptions(plasmoid.configuration.networkUnit)
property double cfg_downloadMaxKiBps: 0.0
property double cfg_uploadMaxKiBps: 0.0

readonly property var networkKiBpsSpeedOptions: [
{
"label": i18n("Custom"),
"value": -1.0
},{
"label": "100 KiB/s",
"value": 100.0
}, {
"label": "1 MiB/s",
"value": 1000.0
}, {
"label": "10 MiB/s",
"value": 10000.0
}, {
"label": "100 MiB/s",
"value": 100000.0
}, {
"label": "1 GiB/s",
"value": 1000000.0
}, {
"label": "2.5 GiB/s",
"value": 2500000.0
}, {
"label": "5 GiB/s",
"value": 5000000.0
}, {
"label": "10 GiB/s",
"value": 10000000.0
}
]
readonly property var networkKbpsSpeedOptions: [
{
"label": i18n("Custom"),
"value": -1.0
},{
"label": "100 Kbps",
"value": 12.207,
"rawValue": 0.1
}, {
"label": "1 Mbps",
"value": 122.07,
"rawValue": 1.0
}, {
"label": "10 Mbps",
"value": 1220.7,
"rawValue": 10.0
}, {
"label": "100 Mbps",
"value": 12207.0,
"rawValue": 100.0
}, {
"label": "1 Gbps",
"value": 122070.0,
"rawValue": 1000.0
}, {
"label": "2.5 Gbps",
"value": 305176.0,
"rawValue": 2500.0
}, {
"label": "5 Gbps",
"value": 610352.0,
"rawValue": 5000.0
}, {
"label": "10 Gbps",
"value": 1221000.0,
"rawValue": 10000.0
}
]

// Detect network interfaces
RMComponents.SensorDetector {
id: sensorDetector
Expand Down Expand Up @@ -188,21 +122,29 @@ QtLayouts.ColumnLayout {
}
}

QtControls.CheckBox {
id: networkInKilobit
text: i18n("Speed in kilobit (Kbps)")
onCheckedChanged: {
downloadMaxSpeed.model = checked ? networkKbpsSpeedOptions : networkKiBpsSpeedOptions
// uploadMaxSpeed.model = checked ? networkKbpsSpeedOptions : networkKiBpsSpeedOptions
}
// Separator
Rectangle {
height: Kirigami.Units.largeSpacing * 2
color: "transparent"
}

PlasmaComponents.Label {
text: i18n("Maximum transfer speed")
font.pointSize: PlasmaCore.Theme.defaultFont.pointSize * 1.2
}

// Separator
Rectangle {
height: Kirigami.Units.largeSpacing
color: "transparent"
}

// Receiving speed
QtControls.ComboBox {
id: downloadMaxSpeed
Kirigami.FormData.label: i18n("Max receiving speed:")
Kirigami.FormData.label: i18n("Receiving:")
textRole: "label"
model: cfg_networkInKilobit ? networkKbpsSpeedOptions : networkKiBpsSpeedOptions
model: networkSpeedOptions

onCurrentIndexChanged: {
var current = model[currentIndex]
Expand All @@ -224,35 +166,41 @@ QtLayouts.ColumnLayout {
}
RMControls.SpinBox {
id: customDownloadMaxSpeed
Kirigami.FormData.label: i18n("Custom max receiving speed:")
Kirigami.FormData.label: i18n("Custom value:")
QtLayouts.Layout.fillWidth: true
decimals: 3
stepSize: 1
minimumValue: 0.001
visible: downloadMaxSpeed.currentIndex === 0

textFromValue: function(value) {
return valueToText(value) + (cfg_networkInKilobit ? " Mbps" : " MiB/s")
return valueToText(value) + " M" + networkUnit.suffix
}

onValueChanged: {
var value = valueReal / (cfg_networkInKilobit ? 8.192 : 1)
var value = valueReal / networkUnit.multiplier

cfg_downloadMaxKiBps = Math.round(value * 1000)
dataPage.configurationChanged()
}
Component.onCompleted: {
var value = parseFloat(plasmoid.configuration.downloadMaxKiBps) * (cfg_networkInKilobit ? 8.192 : 1)
var value = parseFloat(plasmoid.configuration.downloadMaxKiBps) * networkUnit.multiplier
valueReal = value / 1000
}
}

// Separator
Rectangle {
height: Kirigami.Units.largeSpacing
color: "transparent"
}

// Sending speed
QtControls.ComboBox {
id: uploadMaxSpeed
Kirigami.FormData.label: i18n("Max sending speed:")
Kirigami.FormData.label: i18n("Sending:")
textRole: "label"
model: cfg_networkInKilobit ? networkKbpsSpeedOptions : networkKiBpsSpeedOptions
model: networkSpeedOptions

onCurrentIndexChanged: {
var current = model[currentIndex]
Expand All @@ -274,25 +222,25 @@ QtLayouts.ColumnLayout {
}
RMControls.SpinBox {
id: customUploadMaxSpeed
Kirigami.FormData.label: i18n("Custom max sending speed:")
Kirigami.FormData.label: i18n("Custom value:")
QtLayouts.Layout.fillWidth: true
decimals: 3
stepSize: 1
minimumValue: 0.001
visible: uploadMaxSpeed.currentIndex === 0

textFromValue: function(value) {
return valueToText(value) + (cfg_networkInKilobit ? " Mbps" : " MiB/s")
return valueToText(value) + " M" + networkUnit.suffix
}

onValueChanged: {
var value = valueReal / (cfg_networkInKilobit ? 8.192 : 1)
var value = valueReal / networkUnit.multiplier

cfg_uploadMaxKiBps = Math.round(value * 1000)
dataPage.configurationChanged()
}
Component.onCompleted: {
var value = parseFloat(plasmoid.configuration.uploadMaxKiBps) * (cfg_networkInKilobit ? 8.192 : 1)
var value = parseFloat(plasmoid.configuration.uploadMaxKiBps) * networkUnit.multiplier
valueReal = value / 1000
}
}
Expand Down
Loading

0 comments on commit 3110650

Please sign in to comment.