use std::fmt;
use serde_repr::{Deserialize_repr, Serialize_repr};
use zbus::zvariant::{DeserializeDict, SerializeDict, Type};
use super::{DESTINATION, PATH};
use crate::{
helpers::{call_method, receive_signal},
Error,
};
#[derive(SerializeDict, DeserializeDict, Type, Debug)]
#[zvariant(signature = "dict")]
pub struct NetworkStatus {
available: bool,
metered: bool,
connectivity: Connectivity,
}
impl NetworkStatus {
pub fn is_available(&self) -> bool {
self.available
}
pub fn is_metered(&self) -> bool {
self.metered
}
pub fn connectivity(&self) -> Connectivity {
self.connectivity
}
}
#[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug, Clone, Copy, Type)]
#[repr(u8)]
pub enum Connectivity {
Local = 1,
Limited = 2,
CaptivePortal = 3,
FullNetwork = 4,
}
impl fmt::Display for Connectivity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let connectivity = match self {
Self::Local => "local",
Self::Limited => "limited",
Self::CaptivePortal => "captive portal",
Self::FullNetwork => "full network",
};
f.write_str(connectivity)
}
}
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.NetworkMonitor")]
pub struct NetworkMonitorProxy<'a>(zbus::Proxy<'a>);
impl<'a> NetworkMonitorProxy<'a> {
pub async fn new(connection: &zbus::Connection) -> Result<NetworkMonitorProxy<'a>, Error> {
let proxy = zbus::ProxyBuilder::new_bare(connection)
.interface("org.freedesktop.portal.NetworkMonitor")?
.path(PATH)?
.destination(DESTINATION)?
.build()
.await?;
Ok(Self(proxy))
}
pub fn inner(&self) -> &zbus::Proxy<'_> {
&self.0
}
#[doc(alias = "CanReach")]
pub async fn can_reach(&self, hostname: &str, port: u32) -> Result<bool, Error> {
call_method(self.inner(), "CanReach", &(hostname, port)).await
}
#[doc(alias = "GetAvailable")]
#[doc(alias = "get_available")]
pub async fn is_available(&self) -> Result<bool, Error> {
call_method(self.inner(), "GetAvailable", &()).await
}
#[doc(alias = "GetConnectivity")]
#[doc(alias = "get_connectivity")]
pub async fn connectivity(&self) -> Result<Connectivity, Error> {
call_method(self.inner(), "GetConnectivity", &()).await
}
#[doc(alias = "GetMetered")]
#[doc(alias = "get_metered")]
pub async fn is_metered(&self) -> Result<bool, Error> {
call_method(self.inner(), "GetMetered", &()).await
}
#[doc(alias = "GetStatus")]
#[doc(alias = "get_status")]
pub async fn status(&self) -> Result<NetworkStatus, Error> {
call_method(self.inner(), "GetStatus", &()).await
}
pub async fn receive_changed(&self) -> Result<(), Error> {
receive_signal(self.inner(), "changed").await
}
}