From 4b88eec93fe6e90621e6884fc28f8892926f38fd Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Sun, 1 Nov 2020 19:09:31 +0100 Subject: [PATCH] Store music library path --- src/backend.rs | 31 +++++++++++++++++++++++++++---- src/window.rs | 2 ++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/backend.rs b/src/backend.rs index d57c1ba..61aa4ac 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -2,8 +2,10 @@ use super::database::*; use anyhow::{anyhow, Result}; use futures_channel::oneshot::Sender; use futures_channel::{mpsc, oneshot}; +use gio::prelude::*; use std::cell::RefCell; use std::path::PathBuf; +use std::rc::Rc; pub enum BackendState { NoMusicLibrary, @@ -45,6 +47,7 @@ pub struct Backend { pub state_stream: RefCell>, state_sender: RefCell>, action_sender: RefCell>>, + settings: gio::Settings, music_library_path: RefCell>, } @@ -56,10 +59,24 @@ impl Backend { state_stream: RefCell::new(state_stream), state_sender: RefCell::new(state_sender), action_sender: RefCell::new(None), + settings: gio::Settings::new("de.johrpan.musicus"), music_library_path: RefCell::new(None), } } + pub fn init(self: Rc) { + if let Some(path) = self.settings.get_string("music-library-path") { + if !path.is_empty() { + let context = glib::MainContext::default(); + context.spawn_local(async move { + self.set_music_library_path_priv(PathBuf::from(path.to_string())) + .await + .unwrap(); + }); + } + } + } + pub async fn update_person(&self, person: Person) -> Result<()> { let (sender, receiver) = oneshot::channel(); self.unwrap_action_sender()? @@ -229,6 +246,16 @@ impl Backend { } pub async fn set_music_library_path(&self, path: PathBuf) -> Result<()> { + self.settings + .set_string("music-library-path", path.to_str().unwrap())?; + self.set_music_library_path_priv(path).await + } + + pub fn get_music_library_path(&self) -> Option { + self.music_library_path.borrow().clone() + } + + async fn set_music_library_path_priv(&self, path: PathBuf) -> Result<()> { self.music_library_path.replace(Some(path.clone())); self.set_state(BackendState::Loading); @@ -247,10 +274,6 @@ impl Backend { Ok(()) } - pub fn get_music_library_path(&self) -> Option { - self.music_library_path.borrow().clone() - } - fn set_state(&self, state: BackendState) { self.state_sender.borrow_mut().try_send(state).unwrap(); } diff --git a/src/window.rs b/src/window.rs index 39e10b0..eb7d942 100644 --- a/src/window.rs +++ b/src/window.rs @@ -33,6 +33,8 @@ impl Window { get_widget!(builder, gtk::Box, empty_screen); let backend = Rc::new(Backend::new()); + backend.clone().init(); + let poe_list = PoeList::new(backend.clone()); let navigator = Navigator::new(&empty_screen);