Skip to content

Instantly share code, notes, and snippets.

@ekke
Last active August 7, 2023 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekke/f726d944a9f19e7b99e58ee41a7f44d3 to your computer and use it in GitHub Desktop.
Save ekke/f726d944a9f19e7b99e58ee41a7f44d3 to your computer and use it in GitHub Desktop.
QNetworkInformation - current state as QVariantMap to be used from QML
QVariantMap DataServer::networkInfo()
{
QVariantMap networkInfoMap;
// Features
QVariantMap supportedFeaturesMap;
supportedFeaturesMap.insert("reachability",QNetworkInformation::instance()->supportedFeatures().testFlag(QNetworkInformation::Feature::Reachability));
supportedFeaturesMap.insert("transportMedium",QNetworkInformation::instance()->supportedFeatures().testFlag(QNetworkInformation::Feature::TransportMedium));
supportedFeaturesMap.insert("captivePortal",QNetworkInformation::instance()->supportedFeatures().testFlag(QNetworkInformation::Feature::CaptivePortal));
supportedFeaturesMap.insert("metered",QNetworkInformation::instance()->supportedFeatures().testFlag(QNetworkInformation::Feature::Metered));
networkInfoMap.insert("supportedFeatures", supportedFeaturesMap);
// Reachability
QVariantMap reachabilityMap;
reachabilityMap.insert("description","Can be UNKNOWN, DISCONNECTED, LOCAL, SITE, ONLINE");
QString currentReachability = "?????";
switch (QNetworkInformation::instance()->reachability()) {
case QNetworkInformation::Reachability::Unknown:;
currentReachability = "Unknown";
break;
case QNetworkInformation::Reachability::Disconnected:;
currentReachability = "Disconnected";
break;
case QNetworkInformation::Reachability::Local:;
currentReachability = "Local";
break;
case QNetworkInformation::Reachability::Site:;
currentReachability = "Site";
break;
case QNetworkInformation::Reachability::Online:;
currentReachability = "Online";
break;
default:
break;
}
reachabilityMap.insert("currentReachability", currentReachability);
networkInfoMap.insert("reachability", reachabilityMap);
// TransportMedium
QVariantMap transportMediumMap;
transportMediumMap.insert("description","Can be UNKNOWN, ETHERNET, CELLULAR, WIFI, BLUETOOTH");
QString currentTransportMedium = "?????";
switch (QNetworkInformation::instance()->transportMedium()) {
case QNetworkInformation::TransportMedium::Unknown:
currentTransportMedium = "Unknown";
break;
case QNetworkInformation::TransportMedium::Ethernet:
currentTransportMedium = "Ethernet";
break;
case QNetworkInformation::TransportMedium::Cellular:
currentTransportMedium = "Cellular";
break;
case QNetworkInformation::TransportMedium::WiFi:
currentTransportMedium = "WiFi";
break;
case QNetworkInformation::TransportMedium::Bluetooth:
currentTransportMedium = "Bluetooth";
break;
default:
break;
}
transportMediumMap.insert("currentTransportMedium", currentTransportMedium);
networkInfoMap.insert("transportMedium", transportMediumMap);
// Captive Portal
networkInfoMap.insert("isBehindCaptivePortal", QNetworkInformation::instance()->isBehindCaptivePortal());
// Metered
networkInfoMap.insert("isMetered", QNetworkInformation::instance()->isMetered());
// Backends
QVariantMap backendMap;
backendMap.insert("description", "Backends: networklistmanager (Windows), scnetworkreachability (Apple macOS, iOS), android (Android), networkmanager (Linux)");
backendMap.insert("currentBackend", QNetworkInformation::instance()->backendName());
networkInfoMap.insert("backend", backendMap);
return networkInfoMap;
}
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Controls.Material
Popup {
id: popupNetworkInfo
property var infoDataMap: dataServer.networkInfo()
property string supportedFeatures
property string unsupportedFeatures
modal: true
focus: true
closePolicy: Popup.CloseOnEscape | Popup.NoAutoClose
parent: Overlay.overlay
anchors.centerIn: Overlay.overlay
padding: 24
ColumnLayout {
id: mainColumn
anchors.fill: parent
Label {
Layout.fillWidth: true
Layout.alignment: Qt.AlignHCenter
text: qsTr("Network Information")
color: Material.primaryColor
}
Label {
Layout.fillWidth: true
text: qsTr("Supported Features")
color: Material.accentColor
}
Label {
Layout.fillWidth: true
visible: popupNetworkInfo.supportedFeatures.length
text: popupNetworkInfo.supportedFeatures
wrapMode: Label.WordWrap
}
Label {
Layout.fillWidth: true
text: qsTr("Unsupported Features")
color: Material.accentColor
}
Label {
Layout.fillWidth: true
visible: popupNetworkInfo.unsupportedFeatures.length
text: popupNetworkInfo.unsupportedFeatures
wrapMode: Label.WordWrap
}
Label {
Layout.fillWidth: true
text: qsTr("Current Reachability: %1").arg(infoDataMap.reachability.currentReachability)
color: Material.accentColor
}
Label {
Layout.fillWidth: true
text: infoDataMap.reachability.description
wrapMode: Label.WordWrap
}
Label {
Layout.fillWidth: true
visible: infoDataMap.supportedFeatures.transportMedium
text: qsTr("Current TransportMedium: %1").arg(infoDataMap.transportMedium.currentTransportMedium)
color: Material.accentColor
}
Label {
Layout.fillWidth: true
visible: infoDataMap.supportedFeatures.transportMedium
text: infoDataMap.transportMedium.description
wrapMode: Label.WordWrap
}
Label {
Layout.fillWidth: true
visible: infoDataMap.supportedFeatures.captivePortal
text: qsTr("Is behind captive Portal ? %1").arg(infoDataMap.isBehindCaptivePortal?"yes":"no")
color: Material.accentColor
}
// https://de.wikipedia.org/wiki/Captive_Portal
Label {
Layout.fillWidth: true
visible: infoDataMap.supportedFeatures.captivePortal
text: "per ex. in Hotels, Trains, Public WLAN HotSpots, ..."
wrapMode: Label.WordWrap
}
Label {
Layout.fillWidth: true
visible: infoDataMap.supportedFeatures.metered
text: qsTr("Is metered ? %1").arg(infoDataMap.isMetered?"yes":"no")
color: Material.accentColor
}
Label {
Layout.fillWidth: true
visible: infoDataMap.supportedFeatures.metered
text: "You probably have to pay for data traffic"
wrapMode: Label.WordWrap
}
Label {
Layout.fillWidth: true
text: qsTr("Current Backend: %1").arg(infoDataMap.backend.currentBackend)
color: Material.accentColor
}
Label {
Layout.fillWidth: true
text: infoDataMap.backend.description
wrapMode: Label.WordWrap
}
Button {
text: qsTr("OK")
onClicked: popupNetworkInfo.close()
}
} // main column layout
function allSupportedFeatures() {
popupNetworkInfo.supportedFeatures = ""
if(infoDataMap.supportedFeatures.reachability) {
popupNetworkInfo.supportedFeatures+=qsTr("Reachability")
}
if(infoDataMap.supportedFeatures.transportMedium) {
if(popupNetworkInfo.supportedFeatures.length) {
popupNetworkInfo.supportedFeatures+=", "
}
popupNetworkInfo.supportedFeatures+=qsTr("TransportMedium")
}
if(infoDataMap.supportedFeatures.captivePortal) {
if(popupNetworkInfo.supportedFeatures.length) {
popupNetworkInfo.supportedFeatures+=", "
}
popupNetworkInfo.supportedFeatures+=qsTr("CaptivePortal")
}
if(infoDataMap.supportedFeatures.metered) {
if(popupNetworkInfo.supportedFeatures.length) {
popupNetworkInfo.supportedFeatures+=", "
}
popupNetworkInfo.supportedFeatures+=qsTr("Metered")
}
}
function allUnsupportedFeatures() {
popupNetworkInfo.unsupportedFeatures = ""
if(!infoDataMap.supportedFeatures.reachability) {
popupNetworkInfo.unsupportedFeatures+=qsTr("Reachability")
}
if(!infoDataMap.supportedFeatures.transportMedium) {
if(popupNetworkInfo.unsupportedFeatures.length) {
popupNetworkInfo.unsupportedFeatures+=", "
}
popupNetworkInfo.unsupportedFeatures+=qsTr("TransportMedium")
}
if(!infoDataMap.supportedFeatures.captivePortal) {
if(popupNetworkInfo.unsupportedFeatures.length) {
popupNetworkInfo.unsupportedFeatures+=", "
}
popupNetworkInfo.unsupportedFeatures+=qsTr("CaptivePortal")
}
if(!infoDataMap.supportedFeatures.metered) {
if(popupNetworkInfo.unsupportedFeatures.length) {
popupNetworkInfo.unsupportedFeatures+=", "
}
popupNetworkInfo.unsupportedFeatures+=qsTr("Metered")
}
}
onOpened: {
popupNetworkInfo.allSupportedFeatures()
popupNetworkInfo.allUnsupportedFeatures()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment