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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use zbus::Connection;

use crate::device::Device;
use crate::errors::Error;
use crate::raw::networkmanager::NetworkManagerProxy;
use crate::settings::Settings;
use crate::types::ReloadFlags;

#[derive(Clone, Debug)]
pub struct NetworkManager {
    zbus: Connection,
}

crate::zproxy_unpathed!(NetworkManager, NetworkManagerProxy<'_>);

impl NetworkManager {
    /// Create a new NetworkManager instance.
    pub async fn new() -> Result<Self, Error> {
        let zbus = Connection::system().await?;
        Ok(Self::new_with_zbus(zbus))
    }

    /// Create a new NetworkManager instance with a custom D-Bus connection.
    pub fn new_with_zbus(zbus: Connection) -> Self {
        NetworkManager { zbus }
    }

    /// Reload NetworkManager.
    pub async fn reload(&self, flags: ReloadFlags) -> Result<(), Error> {
        self.raw()
            .await?
            .reload(flags.bits())
            .await
            .map_err(Error::ZBus)
    }

    /// Get the list of realized network devices.
    ///
    /// Returns the network devices known to the system. Does not include device placeholders (see
    /// [`get_all_devices()`](#method.get_all_devices)).
    pub async fn get_devices(&self) -> Result<impl Iterator<Item = Device> + '_, Error> {
        Ok(self
            .raw()
            .await?
            .get_devices()
            .await?
            .into_iter()
            .map(|path| Device {
                zbus: self.zbus.clone(),
                path,
            }))
    }

    /// Get the list of all network devices.
    ///
    /// Includes device placeholders (eg, devices that do not yet exist but which could be
    /// automatically created by NetworkManager if one of their AvailableConnections was activated).
    pub async fn get_all_devices(&self) -> Result<impl Iterator<Item = Device> + '_, Error> {
        Ok(self
            .raw()
            .await?
            .get_all_devices()
            .await?
            .into_iter()
            .map(|path| Device {
                zbus: self.zbus.clone(),
                path,
            }))
    }

    /// Get the network device referenced by its IP interface name.
    ///
    /// Note that some devices (usually modems) only have an IP interface name when they are
    /// connected.
    pub async fn get_device_by_ip_interface_name(&self, iface: &str) -> Result<Device, Error> {
        Ok(Device {
            zbus: self.zbus.clone(),
            path: self.raw().await?.get_device_by_ip_iface(iface).await?,
        })
    }

    // TODO: ActivateConnection()
    // TODO: AddAndActivateConnection()
    // TODO: AddAndActivateConnection2()
    // TODO: DeactivateConnection()
    // TODO: Sleep()
    // TODO: GetPermissions()
    // TODO: SetLogging()
    // TODO: GetLogging()
    // TODO: CheckConnectivity()
    // TODO: State()
    // TODO: CheckpointCreate()
    // TODO: CheckpointDestroy()
    // TODO: CheckpointRollback()
    // TODO: CheckpointAdjustRollbackTimeout()

    pub async fn enable(&self, enabled: bool) -> Result<(), Error> {
        self.raw().await?.enable(enabled).await?;
        Ok(())
    }

    pub async fn is_networking_enabled(&self) -> Result<bool, Error> {
        Ok(self.raw().await?.networking_enabled().await?)
    }

    pub async fn is_wireless_enabled(&self) -> Result<bool, Error> {
        Ok(self.raw().await?.wireless_enabled().await?)
    }

    pub async fn is_wireless_hardware_enabled(&self) -> Result<bool, Error> {
        Ok(self.raw().await?.wireless_hardware_enabled().await?)
    }

    pub async fn is_wimax_enabled(&self) -> Result<bool, Error> {
        Ok(self.raw().await?.wimax_enabled().await?)
    }

    pub async fn is_wimax_hardware_enabled(&self) -> Result<bool, Error> {
        Ok(self.raw().await?.wimax_hardware_enabled().await?)
    }

    pub async fn is_wwan_enabled(&self) -> Result<bool, Error> {
        Ok(self.raw().await?.wwan_enabled().await?)
    }

    pub async fn is_wwan_hardware_enabled(&self) -> Result<bool, Error> {
        Ok(self.raw().await?.wwan_hardware_enabled().await?)
    }

    pub async fn is_connectivity_check_enabled(&self) -> Result<bool, Error> {
        Ok(self.raw().await?.connectivity_check_enabled().await?)
    }

    /// Indicates whether NetworkManager is still starting up.
    ///
    /// This becomes `false` when NetworkManager has finished attempting to activate every
    /// connection that it might be able to activate at startup.
    pub async fn is_starting_up(&self) -> Result<bool, Error> {
        Ok(self.raw().await?.startup().await?)
    }

    /// Settings service object
    pub fn settings(&self) -> Settings {
        Settings::new_with_zbus(self.zbus.clone())
    }
}