summaryrefslogtreecommitdiff
path: root/qml_plugin/src/services/network.rs
blob: 4883dba05f79e455d514a192d6168075377f238c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use zbus::{Connection, proxy, zvariant::OwnedObjectPath};

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("The connection is not active.")]
    InactiveConnection,
    #[error("Device doesn't exist")]
    DeviceNotFound,
    #[error("Failed to communicate with NetworkManager: {0}")]
    Zbus(#[from] zbus::Error),
}

#[proxy(
    interface = "org.freedesktop.NetworkManager",
    default_service = "org.freedesktop.NetworkManager",
    default_path = "/org/freedesktop/NetworkManager"
)]
trait NetworkManager {
    #[zbus(property)]
    fn primary_connection(&self) -> zbus::Result<OwnedObjectPath>;
}

#[proxy(
    interface = "org.freedesktop.NetworkManager.Connection.Active",
    default_service = "org.freedesktop.NetworkManager"
)]
trait ActiveConnection {
    #[zbus(property)]
    fn id(&self) -> zbus::Result<String>;

    #[zbus(
        property,
        name = "Type"
    )]
    fn type_(&self) -> zbus::Result<String>;

    #[zbus(property)]
    fn devices(&self) -> zbus::Result<Vec<OwnedObjectPath>>;
}

#[proxy(
    interface = "org.freedesktop.NetworkManager.Device",
    default_service = "org.freedesktop.NetworkManager"
)]
trait Device {
    #[zbus(property)]
    fn interface(&self) -> zbus::Result<String>;
}

pub async fn get() -> Result<String, Error> {
    let conn = Connection::system().await?;

    let nm_conn = NetworkManagerProxy::new(&conn).await?.primary_connection().await?;

    if nm_conn.as_str() == "/" {
        return Err(Error::InactiveConnection);
    }

    if let Some(device_path) =
        ActiveConnectionProxy::builder(&conn).path(nm_conn)?.build().await?.devices().await?.first()
    {
        Ok(DeviceProxy::builder(&conn).path(device_path)?.build().await?.interface().await?)
    } else {
        Err(Error::DeviceNotFound)
    }
}