use super::{DESTINATION, PATH};
use crate::{
helpers::{call_method, receive_signal},
Error,
};
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::fmt;
use zvariant_derive::{DeserializeDict, SerializeDict, Type, TypeDict};
#[derive(SerializeDict, DeserializeDict, TypeDict, Debug)]
pub struct NetworkStatus {
pub available: bool,
pub metered: bool,
pub connectivity: Connectivity,
}
#[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug, Type)]
#[repr(u32)]
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::azync::Proxy<'a>);
impl<'a> NetworkMonitorProxy<'a> {
pub async fn new(
connection: &zbus::azync::Connection,
) -> Result<NetworkMonitorProxy<'a>, Error> {
let proxy = zbus::ProxyBuilder::new_bare(connection)
.interface("org.freedesktop.portal.NetworkMonitor")
.path(PATH)?
.destination(DESTINATION)
.build_async()
.await?;
Ok(Self(proxy))
}
pub fn inner(&self) -> &zbus::azync::Proxy<'_> {
&self.0
}
#[doc(alias = "CanReach")]
pub async fn can_reach(&self, hostname: &str, port: u32) -> Result<bool, Error> {
call_method(&self.0, "CanReach", &(hostname, port)).await
}
#[doc(alias = "GetAvailable")]
#[doc(alias = "get_available")]
pub async fn is_available(&self) -> Result<bool, Error> {
call_method(&self.0, "GetAvailable", &()).await
}
#[doc(alias = "GetConnectivity")]
#[doc(alias = "get_connectivity")]
pub async fn connectivity(&self) -> Result<Connectivity, Error> {
call_method(&self.0, "GetConnectivity", &()).await
}
#[doc(alias = "GetMetered")]
#[doc(alias = "get_metered")]
pub async fn is_metered(&self) -> Result<bool, Error> {
call_method(&self.0, "GetMetered", &()).await
}
#[doc(alias = "GetStatus")]
#[doc(alias = "get_status")]
pub async fn status(&self) -> Result<NetworkStatus, Error> {
call_method(&self.0, "GetStatus", &()).await
}
pub async fn receive_changed(&self) -> Result<(), Error> {
receive_signal(&self.0, "changed").await
}
}