2025-03-22 18:55:29 +01:00
|
|
|
use std::{
|
|
|
|
|
cell::{Cell, RefCell},
|
|
|
|
|
str::FromStr,
|
|
|
|
|
};
|
2024-06-09 16:26:37 +02:00
|
|
|
|
|
|
|
|
use anyhow::Result;
|
2025-03-16 14:06:57 +01:00
|
|
|
use gtk::{gio, glib, glib::Properties, prelude::*, subclass::prelude::*};
|
2024-06-09 16:26:37 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
2025-03-16 14:06:57 +01:00
|
|
|
use crate::{config, library::LibraryQuery};
|
2024-06-09 16:26:37 +02:00
|
|
|
|
|
|
|
|
mod imp {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[derive(Properties, Serialize, Deserialize, Default)]
|
|
|
|
|
#[properties(wrapper_type = super::Program)]
|
2025-03-15 07:08:19 +01:00
|
|
|
#[serde(default)]
|
2024-06-09 16:26:37 +02:00
|
|
|
pub struct Program {
|
|
|
|
|
#[property(get, set)]
|
|
|
|
|
pub title: RefCell<Option<String>>,
|
|
|
|
|
|
|
|
|
|
#[property(get, set)]
|
|
|
|
|
pub description: RefCell<Option<String>>,
|
|
|
|
|
|
|
|
|
|
#[property(get, set, builder(ProgramDesign::default()))]
|
|
|
|
|
pub design: Cell<ProgramDesign>,
|
|
|
|
|
|
|
|
|
|
#[property(get, set)]
|
|
|
|
|
pub composer_id: RefCell<Option<String>>,
|
|
|
|
|
|
|
|
|
|
#[property(get, set)]
|
|
|
|
|
pub performer_id: RefCell<Option<String>>,
|
|
|
|
|
|
|
|
|
|
#[property(get, set)]
|
|
|
|
|
pub ensemble_id: RefCell<Option<String>>,
|
|
|
|
|
|
2025-03-01 20:54:22 +01:00
|
|
|
#[property(get, set)]
|
|
|
|
|
pub instrument_id: RefCell<Option<String>>,
|
|
|
|
|
|
2024-06-09 16:26:37 +02:00
|
|
|
#[property(get, set)]
|
|
|
|
|
pub work_id: RefCell<Option<String>>,
|
|
|
|
|
|
|
|
|
|
#[property(get, set)]
|
|
|
|
|
pub album_id: RefCell<Option<String>>,
|
|
|
|
|
|
|
|
|
|
#[property(get, set)]
|
|
|
|
|
pub prefer_recently_added: Cell<f64>,
|
|
|
|
|
|
|
|
|
|
#[property(get, set)]
|
|
|
|
|
pub prefer_least_recently_played: Cell<f64>,
|
|
|
|
|
|
2025-03-15 07:08:19 +01:00
|
|
|
#[property(get, set)]
|
2025-03-16 14:06:57 +01:00
|
|
|
pub avoid_repeated_composers: Cell<i32>,
|
2025-03-15 07:08:19 +01:00
|
|
|
|
|
|
|
|
#[property(get, set)]
|
2025-03-16 14:06:57 +01:00
|
|
|
pub avoid_repeated_instruments: Cell<i32>,
|
2025-03-15 07:08:19 +01:00
|
|
|
|
2024-06-09 16:26:37 +02:00
|
|
|
#[property(get, set)]
|
|
|
|
|
pub play_full_recordings: Cell<bool>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[glib::object_subclass]
|
|
|
|
|
impl ObjectSubclass for Program {
|
|
|
|
|
const NAME: &'static str = "MusicusProgram";
|
|
|
|
|
type Type = super::Program;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[glib::derived_properties]
|
|
|
|
|
impl ObjectImpl for Program {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glib::wrapper! {
|
|
|
|
|
pub struct Program(ObjectSubclass<imp::Program>);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Program {
|
|
|
|
|
pub fn new(title: &str, description: &str, design: ProgramDesign) -> Self {
|
|
|
|
|
glib::Object::builder()
|
|
|
|
|
.property("title", title)
|
|
|
|
|
.property("description", description)
|
|
|
|
|
.property("design", design)
|
|
|
|
|
.build()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn from_query(query: LibraryQuery) -> Self {
|
2025-03-16 14:06:57 +01:00
|
|
|
let settings = gio::Settings::new(&config::APP_ID);
|
|
|
|
|
|
2024-06-09 16:26:37 +02:00
|
|
|
glib::Object::builder()
|
2025-03-15 07:08:19 +01:00
|
|
|
.property(
|
|
|
|
|
"composer-id",
|
|
|
|
|
query.composer.as_ref().map(|p| p.person_id.clone()),
|
|
|
|
|
)
|
2024-06-09 16:26:37 +02:00
|
|
|
.property("performer-id", query.performer.map(|p| p.person_id))
|
|
|
|
|
.property("ensemble-id", query.ensemble.map(|e| e.ensemble_id))
|
2025-03-15 07:08:19 +01:00
|
|
|
.property(
|
|
|
|
|
"instrument-id",
|
|
|
|
|
query.instrument.as_ref().map(|i| i.instrument_id.clone()),
|
|
|
|
|
)
|
|
|
|
|
.property("work-id", query.work.as_ref().map(|w| w.work_id.clone()))
|
|
|
|
|
.property(
|
2025-03-16 14:06:57 +01:00
|
|
|
"prefer-recently-added",
|
|
|
|
|
settings.int("prefer-recently-added") as f64 / 100.0,
|
|
|
|
|
)
|
|
|
|
|
.property(
|
|
|
|
|
"prefer-least-recently-played",
|
|
|
|
|
settings.int("prefer-least-recently-played") as f64 / 100.0,
|
|
|
|
|
)
|
|
|
|
|
.property(
|
|
|
|
|
"avoid-repeated-composers",
|
2025-03-15 07:08:19 +01:00
|
|
|
if query.composer.is_none() && query.work.is_none() {
|
2025-03-16 14:06:57 +01:00
|
|
|
settings.int("avoid-repeated-composers")
|
2025-03-15 07:08:19 +01:00
|
|
|
} else {
|
|
|
|
|
0
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.property(
|
2025-03-16 14:06:57 +01:00
|
|
|
"avoid-repeated-instruments",
|
2025-03-15 07:08:19 +01:00
|
|
|
if query.instrument.is_none() && query.work.is_none() {
|
2025-03-16 14:06:57 +01:00
|
|
|
settings.int("avoid-repeated-instruments")
|
2025-03-15 07:08:19 +01:00
|
|
|
} else {
|
|
|
|
|
0
|
|
|
|
|
},
|
|
|
|
|
)
|
2025-03-16 14:06:57 +01:00
|
|
|
.property(
|
|
|
|
|
"play-full-recordings",
|
|
|
|
|
settings.boolean("play-full-recordings"),
|
|
|
|
|
)
|
2024-06-09 16:26:37 +02:00
|
|
|
.build()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn deserialize(input: &str) -> Result<Self> {
|
|
|
|
|
let data: imp::Program = serde_json::from_str(input)?;
|
|
|
|
|
|
|
|
|
|
let obj = glib::Object::builder()
|
|
|
|
|
.property("title", &*data.title.borrow())
|
|
|
|
|
.property("description", &*data.description.borrow())
|
|
|
|
|
.property("design", data.design.get())
|
|
|
|
|
.property("prefer-recently-added", data.prefer_recently_added.get())
|
2024-07-18 15:01:30 +02:00
|
|
|
.property(
|
|
|
|
|
"prefer-least-recently-played",
|
|
|
|
|
data.prefer_least_recently_played.get(),
|
|
|
|
|
)
|
2025-03-15 07:08:19 +01:00
|
|
|
.property(
|
2025-03-16 14:06:57 +01:00
|
|
|
"avoid-repeated-composers",
|
|
|
|
|
data.avoid_repeated_composers.get(),
|
2025-03-15 07:08:19 +01:00
|
|
|
)
|
|
|
|
|
.property(
|
2025-03-16 14:06:57 +01:00
|
|
|
"avoid-repeated-instruments",
|
|
|
|
|
data.avoid_repeated_instruments.get(),
|
2025-03-15 07:08:19 +01:00
|
|
|
)
|
2024-06-09 16:26:37 +02:00
|
|
|
.property("play-full-recordings", data.play_full_recordings.get())
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
Ok(obj)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn serialize(&self) -> String {
|
|
|
|
|
serde_json::to_string(self.imp()).unwrap()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-22 18:55:29 +01:00
|
|
|
impl Default for Program {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
glib::Object::new()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-09 16:26:37 +02:00
|
|
|
#[derive(glib::Enum, Serialize, Deserialize, Eq, PartialEq, Clone, Copy, Debug)]
|
|
|
|
|
#[enum_type(name = "MusicusProgramDesign")]
|
|
|
|
|
pub enum ProgramDesign {
|
|
|
|
|
Program1,
|
|
|
|
|
Program2,
|
|
|
|
|
Program3,
|
|
|
|
|
Program4,
|
|
|
|
|
Program5,
|
|
|
|
|
Program6,
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-22 18:55:29 +01:00
|
|
|
impl ProgramDesign {
|
|
|
|
|
pub fn css_class(&self) -> String {
|
|
|
|
|
self.to_string()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-09 16:26:37 +02:00
|
|
|
impl Default for ProgramDesign {
|
|
|
|
|
fn default() -> Self {
|
2025-03-22 18:55:29 +01:00
|
|
|
Self::Program1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ToString for ProgramDesign {
|
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
|
String::from(match self {
|
|
|
|
|
ProgramDesign::Program1 => "program-1",
|
|
|
|
|
ProgramDesign::Program2 => "program-2",
|
|
|
|
|
ProgramDesign::Program3 => "program-3",
|
|
|
|
|
ProgramDesign::Program4 => "program-4",
|
|
|
|
|
ProgramDesign::Program5 => "program-5",
|
|
|
|
|
ProgramDesign::Program6 => "program-6",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FromStr for ProgramDesign {
|
|
|
|
|
type Err = ();
|
|
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> std::result::Result<Self, ()> {
|
|
|
|
|
match s {
|
|
|
|
|
"program-1" => Ok(ProgramDesign::Program1),
|
|
|
|
|
"program-2" => Ok(ProgramDesign::Program2),
|
|
|
|
|
"program-3" => Ok(ProgramDesign::Program3),
|
|
|
|
|
"program-4" => Ok(ProgramDesign::Program4),
|
|
|
|
|
"program-5" => Ok(ProgramDesign::Program5),
|
|
|
|
|
"program-6" => Ok(ProgramDesign::Program6),
|
|
|
|
|
_ => Err(()),
|
|
|
|
|
}
|
2024-06-09 16:26:37 +02:00
|
|
|
}
|
|
|
|
|
}
|