Files
telepathy-padfoot/src/padfoot/protocol.rs

184 lines
4.9 KiB
Rust

use crate::padfoot::{var_bool, var_str, var_u32, VarArg};
use crate::telepathy;
use dbus::tree::MethodErr;
use deltachat as dc;
use std::collections::HashMap;
use super::ConnSettings;
pub const PROTO_OBJECT_PATH: &str = "/org/freedesktop/Telepathy/ConnectionManager/padfoot/delta";
pub const PROTO_BUS_NAME: &str = "org.freedesktop.Telepathy.ConnectionManager.padfoot.delta";
pub const PROTO_NAME: &str = "delta";
#[derive(Debug)]
pub struct Protocol {}
pub type ParamSpec = (
String, // Name
u32, // Flags (Conn_Mgr_Param_Flags)
String, // Signature
VarArg, // Default value
);
// Requestable_Channel_Class
pub type RequestableChannelSpec = (
HashMap<String, VarArg>, // Fixed properties
Vec<String>, // Allowed properties
);
// FIXME: these should come from codegen
//const FLAG_NONE: u32 = 0;
const FLAG_REQUIRED: u32 = 1;
//const FLAG_REGISTER: u32 = 2;
//const FLAG_HAS_DEFAULT: u32 = 4;
const FLAG_SECRET: u32 = 8;
//const FLAG_DBUS_PROP: u32 = 16;
pub fn parameters() -> Vec<ParamSpec> {
vec![
(
"account".to_string(),
FLAG_REQUIRED,
"s".to_string(),
var_str("".to_string()),
),
(
"password".to_string(),
FLAG_REQUIRED | FLAG_SECRET,
"s".to_string(),
var_str("".to_string()),
),
("bcc-self".to_string(), 0, "b".to_string(), var_bool(false)),
]
}
pub fn protocol_interfaces() -> Vec<String> {
vec![
"org.freedesktop.Telepathy.Protocol".to_string(),
"org.freedesktop.Telepathy.Protocol.Interface.Presence".to_string(),
]
}
pub fn requestables() -> Vec<RequestableChannelSpec> {
let mut rf = HashMap::<String, VarArg>::new();
rf.insert(
"org.freedesktop.Telepathy.Channel.ChannelType".to_string(),
var_str("org.freedesktop.Telepathy.Channel.Type.Text".to_string()),
);
rf.insert(
"org.freedesktop.Telepathy.Channel.TargetHandleType".to_string(),
var_u32(1),
);
let ra = vec![
"org.freedesktop.Telepathy.Channel.TargetHandle".to_string(),
"org.freedesktop.Telepathy.Channel.TargetID".to_string(),
];
vec![(rf, ra)]
}
impl AsRef<dyn telepathy::Protocol + 'static> for std::rc::Rc<Protocol> {
fn as_ref(&self) -> &(dyn telepathy::Protocol + 'static) {
&**self
}
}
impl telepathy::Protocol for Protocol {
fn identify_account(&self, params: HashMap<&str, VarArg>) -> Result<String, MethodErr> {
println!("Protocol::identify_account(...)");
let settings = ConnSettings::from_params(params)?;
Ok(settings.id())
}
fn normalize_contact(&self, contact_id: &str) -> Result<String, MethodErr> {
println!("Protocol::normalize_contact({})", contact_id);
Ok(dc::contact::addr_normalize(contact_id).to_string())
}
fn interfaces(&self) -> Result<Vec<String>, MethodErr> {
println!("Protocol::interfaces()");
Ok(protocol_interfaces())
}
fn parameters(&self) -> Result<Vec<ParamSpec>, MethodErr> {
println!("Protocol::parameters()");
Ok(parameters())
}
fn connection_interfaces(&self) -> Result<Vec<String>, MethodErr> {
println!("Protocol::connection_interfaces()");
Ok(super::connection_interfaces())
}
fn requestable_channel_classes(&self) -> Result<Vec<RequestableChannelSpec>, MethodErr> {
println!("Protocol::requestable_channel_classes()");
Ok(requestables())
}
fn vcard_field(&self) -> Result<String, MethodErr> {
println!("Protocol::vcard_field()");
Ok("email".to_string())
}
fn english_name(&self) -> Result<String, MethodErr> {
println!("Protocol::english_name()");
Ok("Delta Chat".to_string())
}
fn icon(&self) -> Result<String, MethodErr> {
println!("Protocol::icon()");
Ok("im-delta".to_string())
}
fn authentication_types(&self) -> Result<Vec<String>, MethodErr> {
println!("Protocol::authentication_types()");
Ok(vec![
"org.freedesktop.Telepathy.Channel.Type.ServerTLSConnection".to_string(),
])
}
}
pub type SimpleStatusSpec = (
u32, // connection presence type
bool, // may set on self?
bool, // can have message?
);
pub fn statuses() -> HashMap<String, SimpleStatusSpec> {
let mut out = HashMap::<String, SimpleStatusSpec>::new();
out.insert("available".to_string(), (2, true, false));
out.insert("offline".to_string(), (1, true, false));
out
}
impl AsRef<dyn telepathy::ProtocolInterfacePresence + 'static> for std::rc::Rc<Protocol> {
fn as_ref(&self) -> &(dyn telepathy::ProtocolInterfacePresence + 'static) {
&**self
}
}
impl telepathy::ProtocolInterfacePresence for Protocol {
fn statuses(&self) -> Result<HashMap<String, SimpleStatusSpec>, MethodErr> {
println!("Protocol::presences()");
Ok(statuses())
}
}