src/config.rs

5c1cf0dafd71169d39428b82706d761dcb59012e
weechat-matrix-rs/src/config.rs history · blame · raw

119 lines · 3700 bytes

  1use crate::{MatrixServer, Servers};
  2use weechat::config::{Conf, ConfigSection, ConfigSectionSettings};
  3use weechat::{OptionChanged, Weechat};
  4
  5use std::cell::{Ref, RefCell, RefMut};
  6use std::rc::{Rc, Weak};
  7
  8#[derive(Clone)]
  9pub struct Config(Rc<RefCell<weechat::config::Config>>);
 10
 11#[derive(Clone)]
 12pub struct ConfigHandle(Weak<RefCell<weechat::config::Config>>);
 13
 14impl ConfigHandle {
 15    fn upgrade(&self) -> Config {
 16        Config(
 17            self.0
 18                .upgrade()
 19                .expect("Config got deleted before plugin unload"),
 20        )
 21    }
 22}
 23
 24fn server_write_cb(
 25    _weechat: &Weechat,
 26    config: &Conf,
 27    section: &mut ConfigSection,
 28) {
 29    config.write_section(section.name());
 30
 31    for option in section.options() {
 32        config.write_option(option);
 33    }
 34}
 35
 36impl Config {
 37    pub fn new(weechat: &Weechat, servers: &Servers) -> Config {
 38        let mut config = weechat
 39            .config_new("matrix-rust")
 40            .expect("Can't create new config");
 41
 42        let servers = servers.clone_weak();
 43
 44        let config = Config(Rc::new(RefCell::new(config)));
 45
 46        let weak_config = config.clone_weak();
 47
 48        let server_section_options = ConfigSectionSettings::new("server")
 49            .set_write_callback(server_write_cb)
 50            .set_read_callback(
 51                move |weechat, _config, section, option_name, _value| {
 52                    let config = weak_config.clone();
 53                    let servers = servers.clone();
 54                    weechat.print("Hello server read cb");
 55
 56                    if option_name.is_empty() {
 57                        return OptionChanged::Error;
 58                    }
 59
 60                    let option_args: Vec<&str> =
 61                        option_name.splitn(2, '.').collect();
 62
 63                    weechat
 64                        .print(&format!("Hello option args {:?}", option_args));
 65
 66                    if option_args.len() != 2 {
 67                        return OptionChanged::Error;
 68                    }
 69
 70                    let server_name = option_args[0];
 71                    let option_name = option_args[1];
 72
 73                    let servers = servers.upgrade();
 74
 75                    let mut servers_borrow = servers.borrow_mut();
 76
 77                    // We are reading the config, if the server doesn't yet exists
 78                    // we need to create it before setting the option and running
 79                    // the option change callback.
 80                    if !servers_borrow.contains_key(server_name) {
 81                        let config = Config(config.upgrade().unwrap());
 82                        let server =
 83                            MatrixServer::new(server_name, &config, section);
 84                        servers_borrow.insert(server_name.to_owned(), server);
 85                    }
 86
 87                    let option = section.search_option(option_name);
 88
 89                    match option {
 90                        // TODO instead of resetting set the option with the value.
 91                        Some(o) => o.reset(true),
 92                        None => OptionChanged::Error,
 93                    }
 94                },
 95            );
 96
 97        {
 98            let mut config_borrow = config.borrow_mut();
 99
100            config_borrow
101                .new_section(server_section_options)
102                .expect("Can't create server section");
103        }
104
105        config
106    }
107
108    pub fn borrow(&self) -> Ref<'_, weechat::config::Config> {
109        self.0.borrow()
110    }
111
112    pub fn borrow_mut(&self) -> RefMut<'_, weechat::config::Config> {
113        self.0.borrow_mut()
114    }
115
116    pub fn clone_weak(&self) -> ConfigHandle {
117        ConfigHandle(Rc::downgrade(&self.0))
118    }
119}