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
extern crate dbus;

pub mod block;
pub mod drive;
pub(crate) mod utils;

use block::*;
use dbus::arg::{Variant, RefArg};
use dbus::stdintf::org_freedesktop_dbus::*;
use drive::*;
use std::collections::HashMap;
use std::ops::Deref;
use utils::*;

const DEST: &str = "org.freedesktop.UDisks2";
const PATH: &str = "/org/freedesktop/UDisks2";

pub struct UDisks2 {
    conn: dbus::Connection,
    cache: HashMap<
        dbus::Path<'static>,
        HashMap<String, HashMap<String, Variant<Box<RefArg>>>>
    >
}

impl UDisks2 {
    pub fn new() -> Result<Self, dbus::Error> {
        let mut udisks2 = Self {
            conn: dbus::Connection::get_private(dbus::BusType::System)?,
            cache: Default::default()
        };

        udisks2.update()?;
        Ok(udisks2)
    }

    fn path(&self) -> dbus::ConnPath<&dbus::Connection> {
        self.conn.with_path(DEST, PATH, 3000)
    }

    /// Refresh the managed objects fetched from the DBus server.
    pub fn update(&mut self) -> Result<(), dbus::Error> {
        self.cache = self.path().get_managed_objects()?;
        Ok(())
    }

    fn get_object<T: ParseFrom>(&self, path: &str) -> Option<T> {
        self.cache.iter().flat_map(|object| {
            if object.0.deref() == path {
                T::parse_from(&object.0, &object.1)
            } else {
                None
            }
        }).next()
    }

    /// Find the drive that corresponds to the given dbus object path.
    pub fn get_drive(&self, path: &str) -> Option<Drive> {
        self.get_object::<Drive>(path)
    }

    /// An iterator of `Drive` objects fetched from the inner cached managed objects.
    pub fn get_drives<'a>(&'a self) -> impl Iterator<Item = Drive> + 'a {
        self.cache.iter().flat_map(|object| Drive::parse_from(&object.0, &object.1))
    }

    /// Find the block that corresponds to the given dbus object path.
    pub fn get_block(&self, path: &str) -> Option<Block> {
        self.get_object::<Block>(path)
    }

    /// An iterator of `Block` objects fetched from the inner cached managed objects.
    pub fn get_blocks<'a>(&'a self) -> impl Iterator<Item = Block> + 'a {
        self.cache.iter().flat_map(|object| Block::parse_from(&object.0, &object.1))
    }
}