| 1 |
extern crate dbus; |
| 2 |
|
| 3 |
use dbus::{Connection, BusType, stdintf, arg}; |
| 4 |
use std::collections::HashMap; |
| 5 |
|
| 6 |
fn print_refarg(value: &arg::RefArg) { |
| 7 |
// We don't know what type the value is. We'll try a few and fall back to |
| 8 |
// debug printing if the value is more complex than that. |
| 9 |
if let Some(s) = value.as_str() { println!("{}", s); } |
| 10 |
else if let Some(i) = value.as_i64() { println!("{}", i); } |
| 11 |
else { println!("{:?}", value); } |
| 12 |
} |
| 13 |
|
| 14 |
fn main() { |
| 15 |
// Connect to server and create a ConnPath. A ConnPath implements several interfaces, |
| 16 |
// in this case we'll use OrgFreedesktopDBusProperties, which allows us to call "get". |
| 17 |
let c = Connection::get_private(BusType::Session).unwrap(); |
| 18 |
let p = c.with_path("org.mpris.MediaPlayer2.rhythmbox", "/org/mpris/MediaPlayer2", 5000); |
| 19 |
use stdintf::org_freedesktop_dbus::Properties; |
| 20 |
|
| 21 |
// The Metadata property is a Dict<String, Variant>. |
| 22 |
|
| 23 |
// Option 1: we can get the dict straight into a hashmap, like this: |
| 24 |
|
| 25 |
let metadata: HashMap<String, arg::Variant<Box<arg::RefArg>>> = p.get("org.mpris.MediaPlayer2.Player", "Metadata").unwrap(); |
| 26 |
|
| 27 |
println!("Option 1:"); |
| 28 |
|
| 29 |
// We now iterate over the hashmap. |
| 30 |
for (key, value) in metadata.iter() { |
| 31 |
print!(" {}: ", key); |
| 32 |
print_refarg(&value); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
// Option 2: we can get the entire dict as a RefArg and get the values out by iterating over it. |
| 37 |
|
| 38 |
let metadata: Box<arg::RefArg> = p.get("org.mpris.MediaPlayer2.Player", "Metadata").unwrap(); |
| 39 |
|
| 40 |
// When using "as_iter()" for a dict, we'll get one key, it's value, next key, it's value, etc. |
| 41 |
let mut iter = metadata.as_iter().unwrap(); |
| 42 |
|
| 43 |
println!("Option 2:"); |
| 44 |
while let Some(key) = iter.next() { |
| 45 |
// Printing the key is easy, since we know it's a String. |
| 46 |
print!(" {}: ", key.as_str().unwrap()); |
| 47 |
let value = iter.next().unwrap(); |
| 48 |
print_refarg(&value); |
| 49 |
} |
| 50 |
} |