Commit f6f6346137
Unsigned
Layout: unified · split
src/config.rs +78 −73
| @@ -14,9 +14,10 @@ | |||
| 14 | //! The config options created here will be alive as long as the plugin is | 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 | 15 | //! loaded so they don't need to be freed manually. The drop implementation of |
| 16 | //! the section will do so. | 16 | //! the section will do so. |
| 17 | use crate::{MatrixServer, Servers}; | 17 | use crate::{MatrixServer, Servers, ServersHandle}; |
| 18 | use weechat::config::{ | 18 | use weechat::config::{ |
| 19 | Conf, ConfigSection, ConfigSectionSettings, OptionChanged, | 19 | Conf, ConfigSection, ConfigSectionSettings, OptionChanged, |
| 20 | SectionReadCallback, | ||
| 20 | }; | 21 | }; |
| 21 | use weechat::Weechat; | 22 | use weechat::Weechat; |
| 22 | 23 | ||
| @@ -24,93 +25,94 @@ use std::cell::{Ref, RefCell, RefMut}; | |||
| 24 | use std::rc::{Rc, Weak}; | 25 | use std::rc::{Rc, Weak}; |
| 25 | 26 | ||
| 26 | #[derive(Clone)] | 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 | #[derive(Clone, Default)] | 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 | impl ConfigHandle { | 82 | impl ConfigHandle { |
| 33 | pub fn upgrade(&self) -> Config { | 83 | pub fn upgrade(&self) -> Config { |
| 34 | Config( | 84 | Config { |
| 35 | self.0 | 85 | inner: self |
| 86 | .inner | ||
| 36 | .upgrade() | 87 | .upgrade() |
| 37 | .expect("Config got deleted before plugin unload"), | 88 | .expect("Config got deleted before plugin unload"), |
| 38 | ) | 89 | servers: self.servers.upgrade(), |
| 39 | } | 90 | } |
| 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); | ||
| 51 | } | 91 | } |
| 52 | } | 92 | } |
| 53 | 93 | ||
| 54 | impl Config { | 94 | impl Config { |
| 55 | pub fn new(weechat: &Weechat, servers: &Servers) -> Config { | 95 | pub fn new(_weechat: &Weechat, servers: &Servers) -> Config { |
| 56 | let config = Weechat::config_new("matrix-rust") | 96 | let config = Weechat::config_new("matrix-rust") |
| 57 | .expect("Can't create new config"); | 97 | .expect("Can't create new config"); |
| 58 | 98 | ||
| 59 | let servers = servers.clone_weak(); | 99 | let config = Config { |
| 60 | 100 | inner: Rc::new(RefCell::new(config)), | |
| 61 | let config = Config(Rc::new(RefCell::new(config))); | 101 | servers: servers.clone(), |
| 62 | 102 | }; | |
| 63 | let weak_config = config.clone_weak(); | ||
| 64 | 103 | ||
| 65 | let server_section_options = ConfigSectionSettings::new("server") | 104 | let server_section_options = ConfigSectionSettings::new("server") |
| 66 | .set_write_callback(server_write_cb) | 105 | .set_write_callback( |
| 67 | .set_read_callback( | 106 | |_weechat: &Weechat, |
| 68 | move |_, _config, section, option_name, value| { | 107 | config: &Conf, |
| 69 | let config = weak_config.clone(); | 108 | section: &mut ConfigSection| { |
| 70 | let servers = servers.clone(); | 109 | config.write_section(section.name()); |
| 71 | 110 | for option in section.options() { | |
| 72 | if option_name.is_empty() { | 111 | config.write_option(option); |
| 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 | } | 112 | } |
| 111 | }, | 113 | }, |
| 112 | ); | 114 | ) |
| 113 | 115 | .set_read_callback(config.clone()); | |
| 114 | { | 116 | { |
| 115 | let mut config_borrow = config.borrow_mut(); | 117 | let mut config_borrow = config.borrow_mut(); |
| 116 | 118 | ||
| @@ -123,14 +125,17 @@ impl Config { | |||
| 123 | } | 125 | } |
| 124 | 126 | ||
| 125 | pub fn borrow(&self) -> Ref<'_, weechat::config::Config> { | 127 | pub fn borrow(&self) -> Ref<'_, weechat::config::Config> { |
| 126 | self.0.borrow() | 128 | self.inner.borrow() |
| 127 | } | 129 | } |
| 128 | 130 | ||
| 129 | pub fn borrow_mut(&self) -> RefMut<'_, weechat::config::Config> { | 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 | pub fn clone_weak(&self) -> ConfigHandle { | 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 | } |