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