Debian Codegraph @

src/desktop/network_monitor.rs

raw
1 //! **Note** This portal doesn't work for sandboxed applications.
2 //! # Examples
3 //!
4 //! ```rust,no_run
5 //! use ashpd::desktop::network_monitor::NetworkMonitor;
6 //!
7 //! async fn run() -> ashpd::Result<()> {
8 //! let proxy = NetworkMonitor::new().await?;
9 //!
10 //! println!("{}", proxy.can_reach("www.google.com", 80).await?);
11 //! println!("{}", proxy.is_available().await?);
12 //! println!("{:#?}", proxy.connectivity().await?);
13 //! println!("{}", proxy.is_metered().await?);
14 //! println!("{:#?}", proxy.status().await?);
15 //!
16 //! Ok(())
17 //! }
18 //! ```
19
20 use std::fmt;
21
22 use futures_util::Stream;
23 use serde::Deserialize;
24 use serde_repr::Deserialize_repr;
25 use zbus::zvariant::{Type, as_value};
26
27 use crate::{Error, proxy::Proxy};
28
29 #[derive(Deserialize, Type, Debug)]
30 /// The network status, composed of the availability, metered & connectivity
31 #[zvariant(signature = "dict")]
32 pub struct NetworkStatus {
33 /// Whether the network is considered available.
34 #[serde(with = "as_value")]
35 available: bool,
36 /// Whether the network is considered metered.
37 #[serde(with = "as_value")]
38 metered: bool,
39 /// More detailed information about the host's network connectivity
40 #[serde(with = "as_value")]
41 connectivity: Connectivity,
42 }
43
44 impl NetworkStatus {
45 /// Returns whether the network is considered available.
46 pub fn is_available(&self) -> bool {
47 self.available
48 }
49
50 /// Returns whether the network is considered metered.
51 pub fn is_metered(&self) -> bool {
52 self.metered
53 }
54
55 /// Returns more detailed information about the host's network connectivity.
56 pub fn connectivity(&self) -> Connectivity {
57 self.connectivity
58 }
59 }
60
61 #[cfg_attr(feature = "glib", derive(glib::Enum))]
62 #[cfg_attr(feature = "glib", enum_type(name = "AshpdConnectivity"))]
63 #[derive(Deserialize_repr, PartialEq, Eq, Debug, Clone, Copy, Type)]
64 #[repr(u32)]
65 /// Host's network activity
66 pub enum Connectivity {
67 /// The host is not configured with a route to the internet.
68 Local = 1,
69 /// The host is connected to a network, but can't reach the full internet.
70 Limited = 2,
71 /// The host is behind a captive portal and cannot reach the full internet.
72 CaptivePortal = 3,
73 /// The host connected to a network, and can reach the full internet.
74 FullNetwork = 4,
75 }
76
77 impl fmt::Display for Connectivity {
78 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79 let connectivity = match self {
80 Self::Local => "local",
81 Self::Limited => "limited",
82 Self::CaptivePortal => "captive portal",
83 Self::FullNetwork => "full network",
84 };
85 f.write_str(connectivity)
86 }
87 }
88
89 /// The interface provides network status information to sandboxed applications.
90 ///
91 /// It is not a portal in the strict sense, since it does not involve user
92 /// interaction. Applications are expected to use this interface indirectly,
93 /// via a library API such as the GLib [`gio::NetworkMonitor`](https://gtk-rs.org/gtk-rs-core/stable/latest/docs/gio/struct.NetworkMonitor.html) interface.
94 ///
95 /// Wrapper of the DBus interface: [`org.freedesktop.portal.NetworkMonitor`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.NetworkMonitor.html).
96 #[derive(Debug)]
97 #[doc(alias = "org.freedesktop.portal.NetworkMonitor")]
98 pub struct NetworkMonitor(Proxy<'static>);
99
100 impl NetworkMonitor {
101 /// Create a new instance of [`NetworkMonitor`].
102 pub async fn new() -> Result<Self, Error> {
103 let proxy = Proxy::new_desktop("org.freedesktop.portal.NetworkMonitor").await?;
104 Ok(Self(proxy))
105 }
106
107 /// Create a new instance of [`NetworkMonitor`].
108 pub async fn with_connection(connection: zbus::Connection) -> Result<Self, Error> {
109 let proxy =
110 Proxy::new_desktop_with_connection(connection, "org.freedesktop.portal.NetworkMonitor")
111 .await?;
112 Ok(Self(proxy))
113 }
114
115 /// Returns the version of the portal interface.
116 pub fn version(&self) -> u32 {
117 self.0.version()
118 }
119
120 /// Returns whether the given hostname is believed to be reachable.
121 ///
122 /// # Arguments
123 ///
124 /// * `hostname` - The hostname to reach.
125 /// * `port` - The port to reach.
126 ///
127 /// # Required version
128 ///
129 /// The method requires the 3nd version implementation of the portal and
130 /// would fail with [`Error::RequiresVersion`] otherwise.
131 ///
132 /// # Specifications
133 ///
134 /// See also [`CanReach`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.NetworkMonitor.html#org-freedesktop-portal-networkmonitor-canreach).
135 #[doc(alias = "CanReach")]
136 pub async fn can_reach(&self, hostname: &str, port: u32) -> Result<bool, Error> {
137 self.0
138 .call_versioned("CanReach", &(hostname, port), 3)
139 .await
140 }
141
142 /// Returns whether the network is considered available.
143 /// That is, whether the system as a default route for at least one of IPv4
144 /// or IPv6.
145 ///
146 /// # Required version
147 ///
148 /// The method requires the 2nd version implementation of the portal and
149 /// would fail with [`Error::RequiresVersion`] otherwise.
150 ///
151 /// # Specifications
152 ///
153 /// See also [`GetAvailable`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.NetworkMonitor.html#org-freedesktop-portal-networkmonitor-getavailable).
154 #[doc(alias = "GetAvailable")]
155 #[doc(alias = "get_available")]
156 pub async fn is_available(&self) -> Result<bool, Error> {
157 self.0.call_versioned("GetAvailable", &(), 2).await
158 }
159
160 /// Returns more detailed information about the host's network connectivity.
161 ///
162 /// # Required version
163 ///
164 /// The method requires the 2nd version implementation of the portal and
165 /// would fail with [`Error::RequiresVersion`] otherwise.
166 ///
167 /// # Specifications
168 ///
169 /// See also [`GetConnectivity`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.NetworkMonitor.html#org-freedesktop-portal-networkmonitor-getconnectivity).
170 #[doc(alias = "GetConnectivity")]
171 #[doc(alias = "get_connectivity")]
172 pub async fn connectivity(&self) -> Result<Connectivity, Error> {
173 self.0.call_versioned("GetConnectivity", &(), 2).await
174 }
175
176 /// Returns whether the network is considered metered.
177 /// That is, whether the system as traffic flowing through the default
178 /// connection that is subject to limitations by service providers.
179 ///
180 /// # Required version
181 ///
182 /// The method requires the 2nd version implementation of the portal and
183 /// would fail with [`Error::RequiresVersion`] otherwise.
184 ///
185 /// # Specifications
186 ///
187 /// See also [`GetMetered`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.NetworkMonitor.html#org-freedesktop-portal-networkmonitor-getmetered).
188 #[doc(alias = "GetMetered")]
189 #[doc(alias = "get_metered")]
190 pub async fn is_metered(&self) -> Result<bool, Error> {
191 self.0.call_versioned("GetMetered", &(), 2).await
192 }
193
194 /// Returns the three values all at once.
195 ///
196 /// # Required version
197 ///
198 /// The method requires the 3nd version implementation of the portal and
199 /// would fail with [`Error::RequiresVersion`] otherwise.
200 ///
201 /// # Specifications
202 ///
203 /// See also [`GetStatus`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.NetworkMonitor.html#org-freedesktop-portal-networkmonitor-getstatus).
204 #[doc(alias = "GetStatus")]
205 #[doc(alias = "get_status")]
206 pub async fn status(&self) -> Result<NetworkStatus, Error> {
207 self.0.call_versioned("GetStatus", &(), 3).await
208 }
209
210 /// Emitted when the network configuration changes.
211 ///
212 /// # Specifications
213 ///
214 /// See also [`changed`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.NetworkMonitor.html#org-freedesktop-portal-networkmonitor-changed).
215 #[doc(alias = "changed")]
216 pub async fn receive_changed(&self) -> Result<impl Stream<Item = ()>, Error> {
217 self.0.signal("changed").await
218 }
219 }
220
221 impl std::ops::Deref for NetworkMonitor {
222 type Target = zbus::Proxy<'static>;
223
224 fn deref(&self) -> &Self::Target {
225 &self.0
226 }
227 }