Files
telepathy-padfoot/src/padfoot/connection/contacts.rs
Nick Thomas 15174ea03f Add a very broken ContactList + outgoing channel implementation
Right now, messages don't show on the padfoot side any more, but they
do get sent and received successfully on the other side, and empathy
can manage contacts now, so I'm calling it an overall win.
2020-05-21 02:30:47 +01:00

107 lines
3.4 KiB
Rust

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,
) -> Result<HashMap<u32, HashMap<String, VarArg>>, MethodErr> {
println!(
"Connection<{}>::get_contact_attributes({:?}, {:?}, {})",
self.id(),
handles,
interfaces,
hold
);
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
};
let mut props = HashMap::<String, VarArg>::new();
// This is mandatory
props.insert(
"org.freedesktop.Telepathy.Connection/contact-id".to_string(),
var_str(contact.get_addr().to_string()),
);
// The empty string means "no avatar"
props.insert(
"org.freedesktop.Telepathy.Connection.Interface.Avatars/token".to_string(),
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>,
) -> 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(),
])
}
}