mirror of
https://github.com/johrpan/musicus.git
synced 2025-10-26 11:47:25 +01:00
Allow uploading from person editor
This commit is contained in:
parent
5c3377e246
commit
cb2a23606a
7 changed files with 353 additions and 121 deletions
|
|
@ -9,103 +9,226 @@
|
|||
<property name="destroy-with-parent">True</property>
|
||||
<property name="type-hint">dialog</property>
|
||||
<child>
|
||||
<object class="GtkBox">
|
||||
<object class="GtkStack" id="stack">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">False</property>
|
||||
<property name="orientation">vertical</property>
|
||||
<property name="transition-type">crossfade</property>
|
||||
<child>
|
||||
<object class="HdyHeaderBar">
|
||||
<object class="GtkBox">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">False</property>
|
||||
<property name="title" translatable="yes">Person</property>
|
||||
<property name="orientation">vertical</property>
|
||||
<child>
|
||||
<object class="GtkButton" id="cancel_button">
|
||||
<property name="label" translatable="yes">Cancel</property>
|
||||
<object class="HdyHeaderBar">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
<property name="receives-default">True</property>
|
||||
</object>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkButton" id="save_button">
|
||||
<property name="label" translatable="yes">Save</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
<property name="receives-default">True</property>
|
||||
<style>
|
||||
<class name="suggested-action"/>
|
||||
</style>
|
||||
<property name="can-focus">False</property>
|
||||
<property name="title" translatable="yes">Person</property>
|
||||
<child>
|
||||
<object class="GtkButton" id="cancel_button">
|
||||
<property name="label" translatable="yes">Cancel</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
<property name="receives-default">True</property>
|
||||
</object>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkButton" id="save_button">
|
||||
<property name="label" translatable="yes">Save</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
<property name="receives-default">True</property>
|
||||
<style>
|
||||
<class name="suggested-action"/>
|
||||
</style>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="pack-type">end</property>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="pack-type">end</property>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<!-- n-columns=2 n-rows=3 -->
|
||||
<object class="GtkGrid">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">False</property>
|
||||
<property name="border-width">18</property>
|
||||
<property name="row-spacing">12</property>
|
||||
<property name="column-spacing">6</property>
|
||||
<child>
|
||||
<object class="GtkLabel">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">False</property>
|
||||
<property name="halign">end</property>
|
||||
<property name="label" translatable="yes">First name</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">0</property>
|
||||
<property name="top-attach">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkEntry" id="first_name_entry">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
<property name="hexpand">True</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">1</property>
|
||||
<property name="top-attach">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkLabel">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">False</property>
|
||||
<property name="halign">end</property>
|
||||
<property name="label" translatable="yes">Last name</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">0</property>
|
||||
<property name="top-attach">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkEntry" id="last_name_entry">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
<property name="hexpand">True</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">1</property>
|
||||
<property name="top-attach">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkLabel">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">False</property>
|
||||
<property name="halign">end</property>
|
||||
<property name="label" translatable="yes">Publish</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">0</property>
|
||||
<property name="top-attach">2</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkSwitch" id="upload_switch">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
<property name="halign">start</property>
|
||||
<property name="active">True</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">1</property>
|
||||
<property name="top-attach">2</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkInfoBar" id="info_bar">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">False</property>
|
||||
<property name="revealed">False</property>
|
||||
<child internal-child="action_area">
|
||||
<object class="GtkButtonBox">
|
||||
<property name="can-focus">False</property>
|
||||
<property name="spacing">6</property>
|
||||
<property name="layout-style">end</property>
|
||||
<child>
|
||||
<placeholder/>
|
||||
</child>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">False</property>
|
||||
<property name="position">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child internal-child="content_area">
|
||||
<object class="GtkBox">
|
||||
<property name="can-focus">False</property>
|
||||
<property name="spacing">16</property>
|
||||
<child>
|
||||
<object class="GtkLabel">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">False</property>
|
||||
<property name="label" translatable="yes">Failed to save person!</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">False</property>
|
||||
<property name="position">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<placeholder/>
|
||||
</child>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">0</property>
|
||||
<property name="name">content</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<!-- n-columns=2 n-rows=2 -->
|
||||
<object class="GtkGrid">
|
||||
<object class="GtkBox">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">False</property>
|
||||
<property name="border-width">18</property>
|
||||
<property name="row-spacing">12</property>
|
||||
<property name="column-spacing">6</property>
|
||||
<property name="orientation">vertical</property>
|
||||
<child>
|
||||
<object class="GtkLabel">
|
||||
<object class="HdyHeaderBar">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">False</property>
|
||||
<property name="halign">end</property>
|
||||
<property name="label" translatable="yes">First name</property>
|
||||
<property name="title" translatable="yes">Person</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">0</property>
|
||||
<property name="top-attach">0</property>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkEntry" id="first_name_entry">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
<property name="hexpand">True</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">1</property>
|
||||
<property name="top-attach">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkLabel">
|
||||
<object class="GtkSpinner">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">False</property>
|
||||
<property name="halign">end</property>
|
||||
<property name="label" translatable="yes">Last name</property>
|
||||
<property name="vexpand">True</property>
|
||||
<property name="active">True</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">0</property>
|
||||
<property name="top-attach">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkEntry" id="last_name_entry">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
<property name="hexpand">True</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="left-attach">1</property>
|
||||
<property name="top-attach">1</property>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="name">loading</property>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ use gio::prelude::*;
|
|||
use isahc::http::StatusCode;
|
||||
use isahc::prelude::*;
|
||||
use serde::Serialize;
|
||||
use std::time::Duration;
|
||||
|
||||
pub mod persons;
|
||||
pub use persons::*;
|
||||
|
|
@ -64,6 +65,7 @@ impl Backend {
|
|||
pub async fn set_login_data(&self, data: LoginData) -> Result<()> {
|
||||
secure::store_login_data(data.clone()).await?;
|
||||
self.login_data.replace(Some(data));
|
||||
self.token.replace(None);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -82,7 +84,6 @@ impl Backend {
|
|||
StatusCode::OK => {
|
||||
let token = response.text_async().await?;
|
||||
self.set_token(&token);
|
||||
println!("{}", &token);
|
||||
true
|
||||
}
|
||||
StatusCode::UNAUTHORIZED => false,
|
||||
|
|
@ -91,4 +92,65 @@ impl Backend {
|
|||
|
||||
Ok(success)
|
||||
}
|
||||
|
||||
/// Make an unauthenticated get request to the server.
|
||||
async fn get(&self, url: &str) -> Result<String> {
|
||||
let server_url = self.get_server_url().ok_or(anyhow!("No server URL set!"))?;
|
||||
|
||||
let mut response = Request::get(format!("{}/{}", server_url, url))
|
||||
.timeout(Duration::from_secs(10))
|
||||
.body(())?
|
||||
.send_async()
|
||||
.await?;
|
||||
|
||||
let body = response.text_async().await?;
|
||||
|
||||
Ok(body)
|
||||
}
|
||||
|
||||
/// Make an authenticated post request to the server.
|
||||
async fn post(&self, url: &str, body: String) -> Result<String> {
|
||||
let body = match self.get_token() {
|
||||
Some(_) => {
|
||||
let mut response = self.post_priv(url, body.clone()).await?;
|
||||
|
||||
// Try one more time (maybe the token was expired)
|
||||
if response.status() == StatusCode::UNAUTHORIZED {
|
||||
if self.login().await? {
|
||||
response = self.post_priv(url, body).await?;
|
||||
} else {
|
||||
bail!("Login failed!");
|
||||
}
|
||||
}
|
||||
|
||||
response.text_async().await?
|
||||
}
|
||||
None => {
|
||||
let mut response = if self.login().await? {
|
||||
self.post_priv(url, body).await?
|
||||
} else {
|
||||
bail!("Login failed!");
|
||||
};
|
||||
|
||||
response.text_async().await?
|
||||
}
|
||||
};
|
||||
|
||||
Ok(body)
|
||||
}
|
||||
|
||||
/// Post something to the server assuming there is a valid login token.
|
||||
async fn post_priv(&self, url: &str, body: String) -> Result<Response<Body>> {
|
||||
let server_url = self.get_server_url().ok_or(anyhow!("No server URL set!"))?;
|
||||
let token = self.get_token().ok_or(anyhow!("No login token!"))?;
|
||||
|
||||
let response = Request::post(format!("{}/{}", server_url, url))
|
||||
.header("Authorization", format!("Bearer {}", token))
|
||||
.header("Content-Type", "application/json")
|
||||
.body(body)?
|
||||
.send_async()
|
||||
.await?;
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,24 +1,18 @@
|
|||
use super::Backend;
|
||||
use crate::database::Person;
|
||||
use anyhow::{anyhow, Result};
|
||||
use isahc::prelude::*;
|
||||
use std::time::Duration;
|
||||
use anyhow::Result;
|
||||
|
||||
impl Backend {
|
||||
/// Get all available persons from the server.
|
||||
pub async fn get_persons(&self) -> Result<Vec<Person>> {
|
||||
let server_url = self.get_server_url().ok_or(anyhow!("No server URL set!"))?;
|
||||
|
||||
let mut response = Request::get(format!("{}/persons", server_url))
|
||||
.timeout(Duration::from_secs(10))
|
||||
.body(())?
|
||||
.send_async()
|
||||
.await?;
|
||||
|
||||
let body = response.text_async().await?;
|
||||
|
||||
let body = self.get("persons").await?;
|
||||
let persons: Vec<Person> = serde_json::from_str(&body)?;
|
||||
|
||||
Ok(persons)
|
||||
}
|
||||
|
||||
/// Post a new person to the server and return the ID.
|
||||
pub async fn post_person(&self, data: &Person) -> Result<()> {
|
||||
self.post("persons", serde_json::to_string(data)?).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,10 @@ impl Backend {
|
|||
pub async fn set_music_library_path_priv(&self, path: PathBuf) -> Result<()> {
|
||||
self.set_state(BackendState::Loading);
|
||||
|
||||
if let Some(db) = &*self.database.borrow() {
|
||||
db.stop().await?;
|
||||
}
|
||||
|
||||
self.music_library_path.replace(Some(path.clone()));
|
||||
|
||||
let mut db_path = path.clone();
|
||||
|
|
@ -66,6 +70,12 @@ impl Backend {
|
|||
self.player.borrow().clone()
|
||||
}
|
||||
|
||||
/// Notify the frontend that the library was changed.
|
||||
pub fn library_changed(&self) {
|
||||
self.set_state(BackendState::Loading);
|
||||
self.set_state(BackendState::Ready);
|
||||
}
|
||||
|
||||
/// Get an interface to the player and panic if there is none.
|
||||
pub fn pl(&self) -> Rc<Player> {
|
||||
self.get_player().unwrap()
|
||||
|
|
|
|||
|
|
@ -2,9 +2,7 @@ use super::schema::{ensembles, performances, persons, recordings};
|
|||
use super::{Database, Ensemble, Instrument, Person, Work};
|
||||
use anyhow::{anyhow, Error, Result};
|
||||
use diesel::prelude::*;
|
||||
use diesel::{Insertable, Queryable};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::convert::TryInto;
|
||||
|
||||
/// Database table data for a recording.
|
||||
#[derive(Insertable, Queryable, Debug, Clone)]
|
||||
|
|
|
|||
|
|
@ -1,85 +1,129 @@
|
|||
use crate::backend::Backend;
|
||||
use crate::database::*;
|
||||
use anyhow::Result;
|
||||
use glib::clone;
|
||||
use gtk::prelude::*;
|
||||
use gtk_macros::get_widget;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub struct PersonEditor<F>
|
||||
where
|
||||
F: Fn(Person) -> () + 'static,
|
||||
{
|
||||
/// A dialog for creating or editing a person.
|
||||
pub struct PersonEditor {
|
||||
backend: Rc<Backend>,
|
||||
id: String,
|
||||
window: libhandy::Window,
|
||||
callback: F,
|
||||
id: u32,
|
||||
stack: gtk::Stack,
|
||||
info_bar: gtk::InfoBar,
|
||||
first_name_entry: gtk::Entry,
|
||||
last_name_entry: gtk::Entry,
|
||||
upload_switch: gtk::Switch,
|
||||
saved_cb: RefCell<Option<Box<dyn Fn(Person) -> ()>>>,
|
||||
}
|
||||
|
||||
impl<F> PersonEditor<F>
|
||||
where
|
||||
F: Fn(Person) -> () + 'static,
|
||||
{
|
||||
impl PersonEditor {
|
||||
/// Create a new person editor and optionally initialize it.
|
||||
pub fn new<P: IsA<gtk::Window>>(
|
||||
backend: Rc<Backend>,
|
||||
parent: &P,
|
||||
person: Option<Person>,
|
||||
callback: F,
|
||||
) -> Rc<Self> {
|
||||
// Create UI
|
||||
|
||||
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/person_editor.ui");
|
||||
|
||||
get_widget!(builder, libhandy::Window, window);
|
||||
get_widget!(builder, gtk::Button, cancel_button);
|
||||
get_widget!(builder, gtk::Button, save_button);
|
||||
get_widget!(builder, gtk::Stack, stack);
|
||||
get_widget!(builder, gtk::InfoBar, info_bar);
|
||||
get_widget!(builder, gtk::Entry, first_name_entry);
|
||||
get_widget!(builder, gtk::Entry, last_name_entry);
|
||||
get_widget!(builder, gtk::Switch, upload_switch);
|
||||
|
||||
let id = match person {
|
||||
Some(person) => {
|
||||
first_name_entry.set_text(&person.first_name);
|
||||
last_name_entry.set_text(&person.last_name);
|
||||
|
||||
person.id
|
||||
}
|
||||
None => rand::random::<u32>().into(),
|
||||
None => generate_id(),
|
||||
};
|
||||
|
||||
let result = Rc::new(PersonEditor {
|
||||
backend: backend,
|
||||
window: window,
|
||||
callback: callback,
|
||||
id: id,
|
||||
first_name_entry: first_name_entry,
|
||||
last_name_entry: last_name_entry,
|
||||
let this = Rc::new(Self {
|
||||
backend,
|
||||
id,
|
||||
window,
|
||||
stack,
|
||||
info_bar,
|
||||
first_name_entry,
|
||||
last_name_entry,
|
||||
upload_switch,
|
||||
saved_cb: RefCell::new(None),
|
||||
});
|
||||
|
||||
cancel_button.connect_clicked(clone!(@strong result => move |_| {
|
||||
result.window.close();
|
||||
// Connect signals and callbacks
|
||||
|
||||
cancel_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
this.window.close();
|
||||
}));
|
||||
|
||||
save_button.connect_clicked(clone!(@strong result => move |_| {
|
||||
let person = Person {
|
||||
id: result.id,
|
||||
first_name: result.first_name_entry.get_text().to_string(),
|
||||
last_name: result.last_name_entry.get_text().to_string(),
|
||||
};
|
||||
save_button.connect_clicked(clone!(@strong this => move |_| {
|
||||
let context = glib::MainContext::default();
|
||||
let clone = this.clone();
|
||||
context.spawn_local(async move {
|
||||
clone.stack.set_visible_child_name("loading");
|
||||
match clone.clone().save().await {
|
||||
Ok(_) => {
|
||||
clone.window.close();
|
||||
}
|
||||
Err(_) => {
|
||||
clone.info_bar.set_revealed(true);
|
||||
clone.stack.set_visible_child_name("content");
|
||||
}
|
||||
}
|
||||
|
||||
let c = glib::MainContext::default();
|
||||
let clone = result.clone();
|
||||
c.spawn_local(async move {
|
||||
clone.backend.db().update_person(person.clone()).await.unwrap();
|
||||
clone.window.close();
|
||||
(clone.callback)(person.clone());
|
||||
});
|
||||
}));
|
||||
|
||||
result.window.set_transient_for(Some(parent));
|
||||
this.window.set_transient_for(Some(parent));
|
||||
|
||||
result
|
||||
this
|
||||
}
|
||||
|
||||
/// Set the closure to be called if the person was saved.
|
||||
pub fn set_saved_cb<F: Fn(Person) -> () + 'static>(&self, cb: F) {
|
||||
self.saved_cb.replace(Some(Box::new(cb)));
|
||||
}
|
||||
|
||||
/// Show the person editor.
|
||||
pub fn show(&self) {
|
||||
self.window.show();
|
||||
}
|
||||
|
||||
/// Save the person and possibly upload it to the server.
|
||||
async fn save(self: Rc<Self>) -> Result<()> {
|
||||
let first_name = self.first_name_entry.get_text().to_string();
|
||||
let last_name = self.last_name_entry.get_text().to_string();
|
||||
|
||||
let person = Person {
|
||||
id: self.id.clone(),
|
||||
first_name,
|
||||
last_name,
|
||||
};
|
||||
|
||||
let upload = self.upload_switch.get_active();
|
||||
if upload {
|
||||
self.backend.post_person(&person).await?;
|
||||
}
|
||||
|
||||
self.backend.db().update_person(person.clone()).await?;
|
||||
self.backend.library_changed();
|
||||
|
||||
if let Some(cb) = &*self.saved_cb.borrow() {
|
||||
cb(person.clone());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -58,15 +58,16 @@ impl PersonSelector {
|
|||
this.backend.clone(),
|
||||
&this.window,
|
||||
None,
|
||||
clone!(@strong this => move |person| {
|
||||
if let Some(cb) = &*this.selected_cb.borrow() {
|
||||
cb(person);
|
||||
}
|
||||
|
||||
this.window.close();
|
||||
}),
|
||||
);
|
||||
|
||||
editor.set_saved_cb(clone!(@strong this => move |person| {
|
||||
if let Some(cb) = &*this.selected_cb.borrow() {
|
||||
cb(person);
|
||||
}
|
||||
|
||||
this.window.close();
|
||||
}));
|
||||
|
||||
editor.show();
|
||||
}));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue