src/config.rs

069d0baa7f6dc24bf92dedeb13c990b5b44e5f51
weechat-matrix-rs/src/config.rs history · blame · raw

122 lines · 3684 bytes

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