musicus/src/dialogs/preferences.rs

106 lines
3.4 KiB
Rust
Raw Normal View History

2020-11-14 22:32:21 +01:00
use super::{LoginDialog, ServerDialog};
2020-10-31 23:48:11 +01:00
use crate::backend::Backend;
use gettextrs::gettext;
2020-10-31 23:48:11 +01:00
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use libhandy::prelude::*;
use std::rc::Rc;
2020-11-14 22:32:21 +01:00
/// A dialog for configuring the app.
2020-10-31 23:48:11 +01:00
pub struct Preferences {
2020-11-14 22:32:21 +01:00
backend: Rc<Backend>,
2020-11-03 17:53:13 +01:00
window: libhandy::Window,
2020-11-14 22:32:21 +01:00
music_library_path_row: libhandy::ActionRow,
url_row: libhandy::ActionRow,
login_row: libhandy::ActionRow,
2020-10-31 23:48:11 +01:00
}
impl Preferences {
2020-11-14 22:32:21 +01:00
/// Create a new preferences dialog.
pub fn new<P: IsA<gtk::Window>>(backend: Rc<Backend>, parent: &P) -> Rc<Self> {
// Create UI
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/preferences.ui");
2020-10-31 23:48:11 +01:00
2020-11-03 17:53:13 +01:00
get_widget!(builder, libhandy::Window, window);
2020-10-31 23:48:11 +01:00
get_widget!(builder, libhandy::ActionRow, music_library_path_row);
get_widget!(builder, gtk::Button, select_music_library_path_button);
2020-11-14 22:32:21 +01:00
get_widget!(builder, libhandy::ActionRow, url_row);
get_widget!(builder, gtk::Button, url_button);
get_widget!(builder, libhandy::ActionRow, login_row);
get_widget!(builder, gtk::Button, login_button);
2020-10-31 23:48:11 +01:00
window.set_transient_for(Some(parent));
2020-11-14 22:32:21 +01:00
let this = Rc::new(Self {
backend,
window,
music_library_path_row,
url_row,
login_row,
});
// Connect signals and callbacks
select_music_library_path_button.connect_clicked(clone!(@strong this => move |_| {
let dialog = gtk::FileChooserNative::new(
Some(&gettext("Select music library folder")),
Some(&this.window), gtk::FileChooserAction::SelectFolder,None, None);
2020-10-31 23:48:11 +01:00
2020-11-14 22:32:21 +01:00
if let gtk::ResponseType::Accept = dialog.run() {
if let Some(path) = dialog.get_filename() {
this.music_library_path_row.set_subtitle(Some(path.to_str().unwrap()));
let context = glib::MainContext::default();
let backend = this.backend.clone();
context.spawn_local(async move {
backend.set_music_library_path(path).await.unwrap();
});
2020-10-31 23:48:11 +01:00
}
2020-11-14 22:32:21 +01:00
}
}));
url_button.connect_clicked(clone!(@strong this => move |_| {
let dialog = ServerDialog::new(this.backend.clone(), &this.window);
dialog.set_selected_cb(clone!(@strong this => move |url| {
this.url_row.set_subtitle(Some(&url));
}));
dialog.show();
}));
login_button.connect_clicked(clone!(@strong this => move |_| {
let dialog = LoginDialog::new(this.backend.clone(), &this.window);
dialog.set_selected_cb(clone!(@strong this => move |data| {
this.login_row.set_subtitle(Some(&data.username));
}));
dialog.show();
}));
// Initialize
if let Some(path) = this.backend.get_music_library_path() {
this.music_library_path_row
.set_subtitle(Some(path.to_str().unwrap()));
}
if let Some(url) = this.backend.get_server_url() {
this.url_row.set_subtitle(Some(&url));
}
if let Some(data) = this.backend.get_login_data() {
this.login_row.set_subtitle(Some(&data.username));
}
2020-10-31 23:48:11 +01:00
2020-11-14 22:32:21 +01:00
this
2020-10-31 23:48:11 +01:00
}
2020-11-14 22:32:21 +01:00
/// Show the preferences dialog.
2020-10-31 23:48:11 +01:00
pub fn show(&self) {
self.window.show();
}
}