dg_network_manager 1.0.0

Network Manager DBUS API
Documentation
use crate::dbus_api::{NMConnectionSettings, Options};
use std::collections::HashMap;
use std::io::{Error, ErrorKind};
use std::sync::Arc;
use zbus::proxy::CacheProperties;
use zbus::zvariant::OwnedValue;
use zbus::{Connection, Result as ZResult, proxy};

#[proxy(
    interface = "org.freedesktop.NetworkManager.Settings.Connection",
    default_service = "org.freedesktop.NetworkManager"
)]
pub trait ConnectionSettings {
    fn update(&self, properties: NMConnectionSettings) -> ZResult<()>;

    fn update_unsaved(&self, properties: NMConnectionSettings) -> ZResult<()>;

    fn delete(&self) -> ZResult<()>;

    fn get_settings(&self) -> ZResult<NMConnectionSettings>;

    fn get_secrets(&self, setting_name: &str) -> ZResult<NMConnectionSettings>;

    fn clear_secrets(&self) -> ZResult<()>;

    fn save(&self) -> ZResult<()>;

    fn update2(
        &self,
        settings: NMConnectionSettings,
        flags: u32,
        args: Options,
    ) -> ZResult<HashMap<String, OwnedValue>>;

    #[zbus(property)]
    fn unsaved(&self) -> ZResult<bool>;

    #[zbus(property)]
    fn flags(&self) -> ZResult<u32>;

    #[zbus(property)]
    fn filename(&self) -> ZResult<String>;
}

pub struct ConnectionSettingsClient {
    service_path: String,
    proxy: ConnectionSettingsProxy<'static>,
}

impl ConnectionSettingsClient {
    pub async fn new(connection: Arc<Connection>, service_path: String) -> Result<Self, Error> {
        let proxy = ConnectionSettingsProxy::builder(&connection)
            .path(service_path.clone())
            .map_err(|e| Error::new(ErrorKind::InvalidInput, e.to_string()))?
            .cache_properties(CacheProperties::Yes)
            .build()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?;

        Ok(Self {
            service_path,
            proxy,
        })
    }

    pub fn service_path(&self) -> &str {
        &self.service_path
    }

    // Methods for accessing and manipulating connection settings
    pub async fn update(&self, properties: NMConnectionSettings) -> Result<(), Error> {
        self.proxy
            .update(properties)
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn update_unsaved(&self, properties: NMConnectionSettings) -> Result<(), Error> {
        self.proxy
            .update_unsaved(properties)
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn delete(&self) -> Result<(), Error> {
        self.proxy
            .delete()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn get_settings(&self) -> Result<NMConnectionSettings, Error> {
        self.proxy
            .get_settings()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn get_secrets(&self, setting_name: &str) -> Result<NMConnectionSettings, Error> {
        self.proxy
            .get_secrets(setting_name)
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn clear_secrets(&self) -> Result<(), Error> {
        self.proxy
            .clear_secrets()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn save(&self) -> Result<(), Error> {
        self.proxy
            .save()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn update2(
        &self,
        settings: NMConnectionSettings,
        flags: u32,
        args: Options,
    ) -> Result<Options, Error> {
        self.proxy
            .update2(settings, flags, args)
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn unsaved(&self) -> Result<bool, Error> {
        self.proxy
            .unsaved()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn flags(&self) -> Result<u32, Error> {
        self.proxy
            .flags()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn filename(&self) -> Result<String, Error> {
        self.proxy
            .filename()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }
}