Don't build a new connection if it already exists

There was also a lot of unneeded overhead in Connection::new() to get
the path to compare against, so split that out into a settings struct
This commit is contained in:
2020-05-16 18:44:02 +01:00
parent e08ec6b476
commit 169249b716
8 changed files with 166 additions and 92 deletions

View File

@@ -38,20 +38,26 @@ pub const CONN_OBJECT_PATH: &str = "/org/freedesktop/Telepathy/Connection/padfoo
#[derive(Debug)]
// A Deltachast connection uses email addresses as handles, and delta's Db IDs
pub struct Connection {
id: String,
ctx: Arc<RwLock<Context>>,
settings: ConnSettings,
state: Arc<RwLock<ConnState>>,
// Used for sending out messages
msgq: Arc<Mutex<VecDeque<dbus::Message>>>,
}
impl Connection {
pub fn new(params: HashMap<&str, super::Variant>) -> Result<Self, MethodErr> {
#[derive(Debug)]
pub struct ConnSettings {
account: String,
password: String,
id: String,
}
impl ConnSettings {
pub fn from_params(params: HashMap<&str, super::Variant>) -> Result<Self, MethodErr> {
let err = Err(MethodErr::no_arg());
let acct = match params.get("account") {
let account = match params.get("account") {
Some(variant) => match variant.0.as_str() {
Some(str) => str.to_string(),
None => return err,
@@ -59,7 +65,7 @@ impl Connection {
None => return err,
};
let id = escape(acct.to_owned());
let id = escape(account.to_owned());
let password = match params.get("password") {
Some(variant) => match variant.0.as_str() {
@@ -69,31 +75,53 @@ impl Connection {
None => return err,
};
Ok(Self {
account,
password,
id,
})
}
pub fn id(&self) -> String {
self.id.to_owned()
}
pub fn bus(&self) -> String {
CONN_BUS_NAME.to_owned() + "." + &self.id
}
pub fn path(&self) -> String {
CONN_OBJECT_PATH.to_owned() + "/" + &self.id
}
}
impl Connection {
pub fn new(settings: ConnSettings) -> Result<Self, MethodErr> {
let mut dbfile = directories::ProjectDirs::from("gs", "ur", "telepathy-padfoot")
.ok_or_else(MethodErr::no_arg)
.and_then(|p| Ok(p.data_local_dir().to_path_buf()))?;
dbfile.push(&id);
dbfile.push(settings.id());
dbfile.push("db.sqlite3");
// FIXME: how to give it access to the connection (initialized later)?
let msgq = Arc::new(Mutex::new(VecDeque::<dbus::Message>::new()));
let id2 = id.clone();
let id = settings.id();
// Use this if we need to send messages in response to DC events:
// let msgq2 = msgq.clone();
let f = move |_c: &Context, e: Event| {
match e {
Event::Info(msg) => println!("Connection<{}>: INFO: {}", id2, msg),
Event::Warning(msg) => println!("Connection<{}>: WARN : {}", id2, msg),
Event::Info(msg) => println!("Connection<{}>: INFO: {}", id, msg),
Event::Warning(msg) => println!("Connection<{}>: WARN : {}", id, msg),
Event::Error(msg) | Event::ErrorNetwork(msg) | Event::ErrorSelfNotInGroup(msg) => {
println!("Connection<{}>: ERR : {}", id2, msg)
println!("Connection<{}>: ERR : {}", id, msg)
}
Event::ConfigureProgress(progress) => {
println!("Connection<{}>: Configuration progress: {}", id2, progress)
println!("Connection<{}>: Configuration progress: {}", id, progress)
}
Event::ImapConnected(msg) | Event::SmtpConnected(msg) => {
println!("Connection<{}>: Network: {}", id2, msg);
println!("Connection<{}>: Network: {}", id, msg);
}
/* Unhandled messages:
SmtpMessageSent(String),
@@ -115,19 +143,23 @@ impl Connection {
SecurejoinInviterProgress
SecurejoinJoinerProgress
*/
_ => println!("Connection<{}>: unhandled event received: {:?}", id2, e),
_ => println!("Connection<{}>: unhandled event received: {:?}", id, e),
};
};
let ctx =
Context::new(Box::new(f), "telepathy-padfoot".to_string(), dbfile).map_err(|e| {
println!("Connection<{}>: couldn't get delta context: {}", id, e);
println!(
"Connection<{}>::new(): couldn't get delta context: {}",
settings.id(),
e
);
MethodErr::no_arg() // FIXME: better error handling
})?;
ctx.set_config(Config::Addr, Some(&acct))
ctx.set_config(Config::Addr, Some(&settings.account))
.map_err(|_e| MethodErr::no_arg())?;
ctx.set_config(Config::MailPw, Some(&password))
ctx.set_config(Config::MailPw, Some(&settings.password))
.map_err(|_e| MethodErr::no_arg())?;
ctx.set_config(Config::SentboxWatch, Some(&"Sent"))
.map_err(|_e| MethodErr::no_arg())?;
@@ -137,7 +169,7 @@ impl Connection {
};
Ok(Connection {
id,
settings,
msgq,
ctx: Arc::new(RwLock::new(ctx)),
state: Arc::new(RwLock::new(ConnState::Initial)),
@@ -154,7 +186,7 @@ impl Connection {
let path = self.path();
let state = self.state.clone();
let msgq = self.msgq.clone();
let id = self.id.clone();
let id = self.id();
let c_rc = std::rc::Rc::new(self);
let f = dbus::tree::Factory::new_fn::<()>();
@@ -248,12 +280,16 @@ impl Connection {
// connect() ?
}
pub fn id(&self) -> String {
self.settings.id.to_string()
}
pub fn bus(&self) -> String {
CONN_BUS_NAME.to_owned() + "." + &self.id
self.settings.bus()
}
pub fn path(&self) -> String {
CONN_OBJECT_PATH.to_owned() + "/" + &self.id
self.settings.path()
}
}