src/config.rs

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

136 lines · 4069 bytes

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