Skip to content

Commit

Permalink
feat: Persistant configuration
Browse files Browse the repository at this point in the history
- Persistant Configuration (using confy)
- Prefered lang and region are now available through the config

Ref: #2, #6
Close: #6
  • Loading branch information
fusetim committed Aug 9, 2023
1 parent 756bdb2 commit 65c659e
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 7 deletions.
64 changes: 64 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ unicode-width = "^0.1"
unicode-segmentation = "1.9.0"
log = { version = "0.4", features = ["std", "serde"] }
structured-logger = "1"
confy = "0.5.1"

[features]
default = ["ftp", "smb"]
ftp = ["dep:remotefs-ftp"]
smb = ["dep:remotefs-smb"]
smb = ["dep:remotefs-smb"]
27 changes: 27 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use serde::{Deserialize, Serialize};
use crate::library::Library;

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, Default)]
pub struct Configuration {
#[serde(default)]
pub libraries: Vec<Library>,
#[serde(default)]
pub tmdb_preferences: TmdbPreferences,
}

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct TmdbPreferences {
#[serde(default)]
pub prefered_lang: String,
#[serde(default)]
pub prefered_country: String,
}

impl Default for TmdbPreferences {
fn default() -> Self {
Self {
prefered_lang: "en".into(),
prefered_country: "US".into(),
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod multifs;
pub mod nfo;
pub mod util;
pub mod views;
pub mod config;

use multifs::{MultiFs, OwnedCursor};
pub use views::{AppEvent, AppMessage, AppState};
Expand Down
7 changes: 4 additions & 3 deletions src/library.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::PathBuf;
use url::Url;
use serde::{Deserialize, Serialize};

#[cfg(feature = "ftp")]
use remotefs_ftp::client::FtpFs;
Expand All @@ -9,7 +10,7 @@ use remotefs_smb::{SmbCredentials, SmbFs, SmbOptions};
use crate::localfs::LocalFs;
use crate::multifs::MultiFs;

#[derive(Clone, Debug, Default, PartialEq)]
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
pub enum LibraryType {
#[default]
Local,
Expand All @@ -19,7 +20,7 @@ pub enum LibraryType {
Smb,
}

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub enum LibraryFlavor {
Movie,
TvShow,
Expand All @@ -37,7 +38,7 @@ impl LibraryType {
}
}

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Library {
pub fs_type: LibraryType,
pub flavor: LibraryFlavor,
Expand Down
23 changes: 20 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ use mkube::views;

use multifs::MultiFs;

const APP_NAME: &'static str = "mkube";
const CONFIG_NAME: Option<&'static str> = Some("config");

#[tokio::main]
async fn main() -> Result<()> {
init_logger().await;
Expand Down Expand Up @@ -80,6 +83,7 @@ where
mkube::MESSAGE_SENDER
.set(sender)
.map_err(|err| anyhow!("Failed to init MESSAGE_SENDER, causes:\n{:?}", err))?;
let mut cfg : mkube::config::Configuration = confy::load(APP_NAME, CONFIG_NAME)?;
let app = views::App {
settings_page: views::settings::SettingsPage::new(),
movie_manager: Default::default(),
Expand All @@ -89,6 +93,15 @@ where
let tick = time::interval(Duration::from_millis(1000 / 15));
tokio::pin!(tick);

// Load libraries from config.
for lib in &cfg.libraries {
if let Ok(mut conn) = MultiFs::try_from(lib) {
if !conn.as_mut_rfs().is_connected() { let _ = conn.as_mut_rfs().connect(); }
state.conns.push(conn);
state.libraries.push(lib.clone());
}
}

loop {
let event = event_reader.next().fuse();

Expand Down Expand Up @@ -147,7 +160,9 @@ where
if let Ok(mut conn) = MultiFs::try_from(&lib) {
if !conn.as_mut_rfs().is_connected() { let _ = conn.as_mut_rfs().connect(); }
state.conns.push(conn);
state.libraries.push(lib);
state.libraries.push(lib.clone());
cfg.libraries.push(lib);
let _ = confy::store(APP_NAME, CONFIG_NAME, &cfg);
}
state.register_event(AppEvent::SettingsEvent(SettingsEvent::OpenMenu(state.libraries.clone())));
},
Expand Down Expand Up @@ -183,7 +198,9 @@ where
AppMessage::MovieManagerMessage(MovieManagerMessage::SearchTitle(title)) => {
use tmdb_api::movie::search::MovieSearch;
use tmdb_api::prelude::Command;
let ms = MovieSearch::new(title);
let ms = MovieSearch::new(title)
.with_language(Some(cfg.tmdb_preferences.prefered_lang.clone()))
.with_region(Some(cfg.tmdb_preferences.prefered_country.clone()));
if let Ok(results) = ms.execute(&tmdb_client).await {
state.register_event(AppEvent::MovieManagerEvent(MovieManagerEvent::SearchResults(results.results)));
} else {
Expand All @@ -193,7 +210,7 @@ where
AppMessage::MovieManagerMessage(MovieManagerMessage::SaveNfo((id, fs_id, mut path))) => {
use std::io::Cursor;
use std::io::Seek;
let mut movie_nfo = mkube::transform_as_nfo(&tmdb_client, id, Some("fr".to_owned())).await?;
let mut movie_nfo = mkube::transform_as_nfo(&tmdb_client, id, Some(cfg.tmdb_preferences.prefered_lang.clone())).await?;
let mt = mkube::get_metadata(&mut state.conns[fs_id], (&state.libraries[fs_id]).try_into().expect("Cannot get a baseURL from library."), path.clone()).await?;
movie_nfo.fileinfo = Some(mt);
let nfo_string = quick_xml::se::to_string(&movie_nfo).expect("Failed to produce a valid nfo file.");
Expand Down

0 comments on commit 65c659e

Please sign in to comment.