src/config.rs
141 lines · 3894 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, ServersHandle};
18use weechat::config::{
19 Conf, ConfigSection, ConfigSectionSettings, OptionChanged,
20 SectionReadCallback,
21};
22use weechat::Weechat;
23
24use std::cell::{Ref, RefCell, RefMut};
25use std::rc::{Rc, Weak};
26
27#[derive(Clone)]
28pub struct Config {
29 inner: Rc<RefCell<weechat::config::Config>>,
30 servers: Servers,
31}
32
33#[derive(Clone, Default)]
34pub struct ConfigHandle {
35 inner: Weak<RefCell<weechat::config::Config>>,
36 servers: ServersHandle,
37}
38
39impl 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}
81
82impl ConfigHandle {
83 pub fn upgrade(&self) -> Config {
84 Config {
85 inner: self
86 .inner
87 .upgrade()
88 .expect("Config got deleted before plugin unload"),
89 servers: self.servers.upgrade(),
90 }
91 }
92}
93
94impl Config {
95 pub fn new(_weechat: &Weechat, servers: &Servers) -> Config {
96 let config = Weechat::config_new("matrix-rust")
97 .expect("Can't create new config");
98
99 let config = Config {
100 inner: Rc::new(RefCell::new(config)),
101 servers: servers.clone(),
102 };
103
104 let server_section_options = ConfigSectionSettings::new("server")
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);
112 }
113 },
114 )
115 .set_read_callback(config.clone());
116 {
117 let mut config_borrow = config.borrow_mut();
118
119 config_borrow
120 .new_section(server_section_options)
121 .expect("Can't create server section");
122 }
123
124 config
125 }
126
127 pub fn borrow(&self) -> Ref<'_, weechat::config::Config> {
128 self.inner.borrow()
129 }
130
131 pub fn borrow_mut(&self) -> RefMut<'_, weechat::config::Config> {
132 self.inner.borrow_mut()
133 }
134
135 pub fn clone_weak(&self) -> ConfigHandle {
136 ConfigHandle {
137 inner: Rc::downgrade(&self.inner),
138 servers: self.servers.clone_weak(),
139 }
140 }
141}