~nicohman/signal-rs

c837f62d95930f9b61372652024c89cc5ac42eda — nicohman 9 months ago 09ca26e
Add backend selection
5 files changed, 84 insertions(+), 14 deletions(-)

M src/main.rs
M src/signal.rs
M src/widgets/chat_list.rs
M src/widgets/header.rs
M src/widgets/settings.rs
M src/main.rs => src/main.rs +40 -11
@@ 253,6 253,7 @@ where
    }
    pub fn update_chats(&mut self) {
        self.chat_list.chats = HashMap::new();
        println!("{:?}", self.sbackend.contacts());
        for (i, contact) in self.sbackend.contacts().values().enumerate() {
            if let Some(msgs) = self.sbackend.messages_tel(&contact.tel).ok() {
                if msgs.len() > 0 {


@@ 315,6 316,15 @@ where
    pub fn process_response(&mut self, response: SignalResponse) {
        self.sbackend.process_response(response.clone());
        match response {
            SignalResponse::Quit => {
                self.headerbar
                    .headerbar
                    .get_toplevel()
                    .unwrap()
                    .downcast::<gtk::Window>()
                    .unwrap()
                    .close();
            }
            SignalResponse::ShowTheme(theme) => {
                self.change_theme(theme);
            }


@@ 401,27 411,46 @@ async fn main() -> Result<(), std::boxed::Box<dyn std::error::Error>> {
        let (req_sender, req_receiver) = unbounded_channel::<SignalRequest>();
        let clconf = config.clone();
        let window = ApplicationWindow::new(app);
        let mut state: SignalState<scli::SCLISession> =
            SignalState::new(req_sender.clone(), l_res_sender.clone(), config.clone());
        match config.backend {
            Backend::Axolotl => {
                let mut state: SignalState<AxolotlSession> =
                    SignalState::new(req_sender.clone(), l_res_sender.clone(), config.clone());
                state.change_theme(config.theme);
                state.user_tel = config.number.clone();
                state.connect_signals();
                window.add(&state.app_box);
                state.show_view(View::Chats);
                res_receiver.attach(None, move |value| {
                    state.process_response(value);
                    glib::Continue(true)
                });
            }
            Backend::SignalCLI => {
                let mut state: SignalState<scli::SCLISession> =
                    SignalState::new(req_sender.clone(), l_res_sender.clone(), config.clone());
                state.change_theme(config.theme);
                state.user_tel = config.number.clone();
                state.connect_signals();
                window.add(&state.app_box);
                state.show_view(View::Chats);
                res_receiver.attach(None, move |value| {
                    state.process_response(value);
                    glib::Continue(true)
                });
            }
        };
        StyleContext::add_provider_for_screen(
            &gdk::Screen::get_default().expect("Error initializing gtk css provider."),
            &provider,
            STYLE_PROVIDER_PRIORITY_APPLICATION,
        );
        state.change_theme(config.theme);
        state.user_tel = config.number.clone();

        window.set_title("Signal");
        window.set_icon(Some(&icon));
        window.set_border_width(10);
        window.set_position(WindowPosition::Center);
        window.set_default_size(400, 800);
        state.connect_signals();
        window.add(&state.app_box);
        state.show_view(View::Chats);
        res_receiver.attach(None, move |value| {
            state.process_response(value);
            glib::Continue(true)
        });

        req_sender
            .send(SignalRequest::GetChatList)
            .expect("Couldn't send GetChatList");

M src/signal.rs => src/signal.rs +2 -0
@@ 94,6 94,8 @@ pub enum SignalResponse {
    ShowTheme(crate::config::Theme),
    IDLessMessage(SignalMessage),
    RmContact(String),
    #[serde(skip)]
    Quit
}

#[derive(Serialize, Deserialize, Clone, Debug)]

M src/widgets/chat_list.rs => src/widgets/chat_list.rs +5 -3
@@ 53,16 53,18 @@ impl ChatList {
    /// Enable or disable the ChatList
    pub fn set_enabled(&mut self, enabled: bool) {
        self.enabled = enabled;
        if enabled {
            for (i, child) in self.listbox.get_children().iter().enumerate() {
        println!("{:?}", self.chats);
        if enabled{
            /*for (i, child) in self.listbox.get_children().iter().enumerate() {
                if self.chats[&((i) as i32)].messages.len() > 0 {
                    child.show();
                }
            }
            }*/
            self.listbox.show();
        } else {
            self.listbox.hide();
        }
        self.update_chats();
    }
    pub fn connect(&self, state: &SignalState<impl SignalBackend>) {
        self.listbox.connect_row_activated(clone!(@strong state.res_sender as res_sender, @strong state.req_sender as req_sender  => move |_, chat| {

M src/widgets/header.rs => src/widgets/header.rs +3 -0
@@ 20,6 20,9 @@ impl Header {
        settings.connect_activate(clone!(@strong res_sender => move |_| {
        	res_sender.send(SignalResponse::ShowView(View::Settings)).expect("Couldn't send ShowView");
        }));
        quit.connect_activate(clone!(@strong res_sender => move |_| {
            res_sender.send(SignalResponse::Quit).expect("Couldn't send quit");
        }));
        menu.append(&settings);
        menu.append(&quit);
        menu.show_all();

M src/widgets/settings.rs => src/widgets/settings.rs +34 -0
@@ 2,6 2,7 @@ use crate::*;
pub struct Settings {
    pub config: Rc<RefCell<Config>>,
    pub theme_button: CheckButton,
    pub backend: ComboBoxText,
    pub cbox: Box,
}
impl Settings {


@@ 12,10 13,25 @@ impl Settings {
            theme_button.set_active(true);
        }
        cbox.add(&theme_button);
        cbox.add(&Label::new(Some("Signal Backend:")));
        let backend = ComboBoxText::new();
        backend.append(Some("axolotl"), "axolotl");
        backend.append(Some("signal-cli"), "signal-cli");
        match config.backend {
            Backend::Axolotl => {
                backend.set_active_id(Some("axolotl"));
            },
            Backend::SignalCLI => {
                backend.set_active_id(Some("signal-cli"));
            }
        }
        cbox.add(&backend);
        cbox.add(&Label::new(Some("Change the backend to use to talk to signal. Will close app when changed")));
        Settings {
            config: Rc::new(RefCell::new(config)),
            theme_button,
            cbox,
            backend,
        }
    }
    pub fn connect(&self, state: &SignalState<impl SignalBackend>) {


@@ 36,5 52,23 @@ impl Settings {
 			res_sender.send(SignalResponse::ShowTheme(change_to)).expect("Couldn't send ShowTheme");
 			confy::store("signal-rs", old).expect("Couldn't store config");
 		}));
        self.backend.connect_changed(clone!(@weak self.config as sconf, @weak state.config as stconf, @strong state.res_sender as res_sender => move |back| {
            let change_to = match back.get_active_text().unwrap().as_str() {
                "axolotl" => Backend::Axolotl,
                "signal-cli" => Backend::SignalCLI,
                _ => Backend::Axolotl,
            };
            sconf.replace_with(|mut old| {
                old.backend = change_to;
                old.clone()
            });
            let mut old = stconf.replace_with(|mut old| {
                old.backend = change_to;
                old.clone()
            });
            old.backend = change_to;
            res_sender.send(SignalResponse::Quit).expect("Couldn't send Quit");
            confy::store("signal-rs", old).expect("Couldn't store config");
        }));
    }
}