Files
telepathy-padfoot/src/padfoot/connection/contacts.rs

107 lines
3.4 KiB
Rust
Raw Normal View History

use crate::padfoot::{var_str, var_u32, VarArg};
use crate::telepathy;
use dbus::tree::MethodErr;
use deltachat::contact::{Contact, Origin};
use std::collections::HashMap;
use super::Connection;
impl AsRef<dyn telepathy::ConnectionInterfaceContacts + 'static> for std::rc::Rc<Connection> {
fn as_ref(&self) -> &(dyn telepathy::ConnectionInterfaceContacts + 'static) {
&**self
}
}
impl telepathy::ConnectionInterfaceContacts for Connection {
fn get_contact_attributes(
&self,
handles: Vec<u32>,
interfaces: Vec<&str>,
hold: bool,
2020-05-16 22:11:38 +01:00
) -> Result<HashMap<u32, HashMap<String, VarArg>>, MethodErr> {
println!(
"Connection<{}>::get_contact_attributes({:?}, {:?}, {})",
self.id(),
handles,
interfaces,
hold
);
2020-05-16 22:11:38 +01:00
let mut out = HashMap::<u32, HashMap<String, VarArg>>::new();
for id in handles.iter() {
// FIXME: work out how to use get_all
let contact = match Contact::get_by_id(&self.ctx.read().unwrap(), *id) {
Ok(c) => c,
Err(_e) => continue, // Invalid IDs are silently ignored
};
2020-05-16 22:11:38 +01:00
let mut props = HashMap::<String, VarArg>::new();
// This is mandatory
props.insert(
"org.freedesktop.Telepathy.Connection/contact-id".to_string(),
2020-05-16 22:11:38 +01:00
var_str(contact.get_addr().to_string()),
);
// The empty string means "no avatar"
props.insert(
"org.freedesktop.Telepathy.Connection.Interface.Avatars/token".to_string(),
2020-05-16 22:11:38 +01:00
var_str("".to_string()),
);
props.insert(
"org.freedesktop.Telepathy.Connection.Interface.ContactList/publish".to_string(),
var_u32(4), // YES (faking it for now)
);
props.insert(
"org.freedesktop.Telepathy.Connection.Interface.ContactList/subscribe".to_string(),
var_u32(4), // YES (faking it for now)
);
out.insert(*id, props);
}
Ok(out)
}
fn get_contact_by_id(
&self,
identifier: &str,
interfaces: Vec<&str>,
2020-05-16 22:11:38 +01:00
) -> Result<(u32, HashMap<String, VarArg>), MethodErr> {
println!(
"Connection<{}>::get_contact_by_id({}, {:?})",
self.id(),
identifier,
interfaces
);
let id = {
let ctx = &self.ctx.read().unwrap();
Contact::lookup_id_by_addr(ctx, identifier, Origin::Unknown)
};
if id == 0 {
return Err(MethodErr::no_arg()); // FIXME: should be InvalidHandle
};
let mut contacts = self.get_contact_attributes(vec![id], interfaces, true)?;
if let Some(contact) = contacts.remove(&id) {
Ok((id, contact))
} else {
Err(MethodErr::no_arg()) // FIXME: should be InvalidHandle
}
}
fn contact_attribute_interfaces(&self) -> Result<Vec<String>, MethodErr> {
println!("Connection<{}>::contact_attribute_interfaces()", self.id());
Ok(vec![
"org.freedesktop.Telepathy.Connection".to_string(),
"org.freedesktop.Telepathy.Connection.Interface.Avatars".to_string(),
"org.freedesktop.Telepathy.Connection.Interface.ContactList".to_string(),
])
}
}