From 9fdf56d5ed43a11afb733530e4496426d99f0040 Mon Sep 17 00:00:00 2001 From: Philip Kristoffersen Date: Sun, 1 May 2022 07:14:24 +0200 Subject: [PATCH] Use config folder (#110) --- .gitignore | 4 ++- src/config.rs | 43 ++++++++++++++++++++++++++++++++ src/main.rs | 10 ++++++++ src/migration.rs | 41 ++++++++++++++++++++++++++++++ src/settings.rs | 12 ++++++--- src/steamgriddb/cached_search.rs | 9 ++++--- src/sync/symlinks.rs | 6 +---- src/ui/ui_image_download.rs | 5 ++-- src/ui/ui_import_games.rs | 5 ++-- 9 files changed, 117 insertions(+), 18 deletions(-) create mode 100644 src/config.rs create mode 100644 src/migration.rs diff --git a/.gitignore b/.gitignore index ab24616..21c2d1a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ auth_key.txt cache.json config.toml -.thumbnails \ No newline at end of file +.thumbnails +thumbnails +.flatpak-builder \ No newline at end of file diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..a9d413a --- /dev/null +++ b/src/config.rs @@ -0,0 +1,43 @@ +use std::{ + fs::create_dir_all, + path::{Path, PathBuf}, +}; + +#[cfg(target_family = "unix")] +pub fn get_config_folder() -> PathBuf { + let config_home = std::env::var("XDG_CONFIG_HOME"); + let home = std::env::var("HOME"); + match (config_home, home) { + (Ok(p), _) => Path::new(&p).to_path_buf(), + (Err(_), Ok(home)) => Path::new(&home).join(".config").join("boilr").to_path_buf(), + _ => Path::new("").to_path_buf(), + } +} + +#[cfg(windows)] +pub fn get_config_folder() -> PathBuf { + let config_home = std::env::var("APPDATA"); + match config_home { + Ok(p) => Path::new(&p).join("boilr").to_path_buf(), + Err(_) => Path::new("").to_path_buf(), + } +} + +pub fn get_thumbnails_folder() -> PathBuf { + let thumbnails_path = get_config_folder().join("thumbnails"); + let _ = create_dir_all(&thumbnails_path); + thumbnails_path.to_path_buf() +} + +pub fn get_config_file() -> PathBuf { + get_config_folder().join("config.toml").to_path_buf() +} + +pub fn get_cache_file() -> PathBuf { + get_config_folder().join("cache.json").to_path_buf() +} + +#[cfg(target_family = "unix")] +pub fn get_boilr_links_path() -> PathBuf { + get_config_folder().join("links").to_path_buf() +} diff --git a/src/main.rs b/src/main.rs index c72488d..17fb3a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,12 @@ mod amazon; +mod config; mod egs; mod gog; mod heroic; mod itch; mod legendary; mod lutris; +mod migration; mod origin; mod platform; mod settings; @@ -16,6 +18,9 @@ mod uplay; use std::error::Error; fn main() -> Result<(), Box> { + ensure_config_folder(); + migration::migrate_config(); + let mut args = std::env::args(); if args.len() > 1 && args.nth(1).unwrap_or_default() == "--no-ui" { ui::run_sync(); @@ -24,3 +29,8 @@ fn main() -> Result<(), Box> { ui::run_ui() } } + +fn ensure_config_folder() { + let path = config::get_config_folder(); + let _ = std::fs::create_dir_all(&path); +} diff --git a/src/migration.rs b/src/migration.rs new file mode 100644 index 0000000..dee468e --- /dev/null +++ b/src/migration.rs @@ -0,0 +1,41 @@ +use std::path::Path; + +pub fn migrate_config() { + let version = &crate::settings::Settings::new() + .map(|s| s.config_version) + .unwrap_or_default(); + + let mut save_version = false; + if version.is_none() { + //Migration from 0 to 1 + let old_path = &Path::new("config.toml"); + if old_path.exists() { + println!("Migrating from configuration version 0 to version 1"); + let new_path = crate::config::get_config_file(); + println!("Your configuration file will be moved to {:?}", new_path); + let _ = std::fs::copy(old_path, new_path); + let _ = std::fs::remove_file(old_path); + } + + let old_path = &Path::new(".thumbnails"); + if old_path.exists() { + //thumbnails are just cache can be removed + let _ = std::fs::remove_dir_all(old_path); + } + + let old_path = &Path::new("cache.json"); + if old_path.exists() { + let new_path = crate::config::get_cache_file(); + let _ = std::fs::copy(old_path, new_path); + let _ = std::fs::remove_file(old_path); + } + save_version = true; + } + + if save_version { + if let Ok(mut settings) = crate::settings::Settings::new() { + settings.config_version = Some(1); + crate::ui::MyEguiApp::save_settings_to_file(&settings); + } + } +} diff --git a/src/settings.rs b/src/settings.rs index 4c8bc76..b5dcc40 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,6 +1,6 @@ use crate::{ - amazon::AmazonSettings, egs::EpicGamesLauncherSettings, gog::GogSettings, - heroic::HeroicSettings, itch::ItchSettings, legendary::LegendarySettings, + amazon::AmazonSettings, config::get_config_file, egs::EpicGamesLauncherSettings, + gog::GogSettings, heroic::HeroicSettings, itch::ItchSettings, legendary::LegendarySettings, lutris::settings::LutrisSettings, origin::OriginSettings, steam::SteamSettings, steamgriddb::SteamGridDbSettings, uplay::UplaySettings, }; @@ -12,6 +12,7 @@ use std::env; #[derive(Debug, Deserialize, Serialize, Clone)] pub struct Settings { pub debug: bool, + pub config_version: Option, pub blacklisted_games: Vec, pub epic_games: EpicGamesLauncherSettings, pub legendary: LegendarySettings, @@ -33,8 +34,11 @@ impl Settings { let default_str = include_str!("defaultconfig.toml"); s.merge(File::from_str(default_str, config::FileFormat::Toml))?; + let config_file = get_config_file(); + let config_file = config_file.to_string_lossy(); + // Start off by merging in the "default" configuration file - s.merge(File::with_name("config.toml").required(false))?; + s.merge(File::with_name(config_file.as_ref()).required(false))?; // Add in the current environment file // Default to 'development' env @@ -48,7 +52,7 @@ impl Settings { // Add in settings from the environment (with a prefix of STEAMSYNC) // Eg.. `STEAMSYNC_DEBUG=1 ./target/app` would set the `debug` key - s.merge(Environment::with_prefix("steamsync").separator("-"))?; + s.merge(Environment::with_prefix("boilr").separator("-"))?; let mut result: Result = s.try_into(); diff --git a/src/steamgriddb/cached_search.rs b/src/steamgriddb/cached_search.rs index 08bbb88..4e89f28 100644 --- a/src/steamgriddb/cached_search.rs +++ b/src/steamgriddb/cached_search.rs @@ -1,5 +1,7 @@ use dashmap::DashMap; -use std::{fs::File, io::Write, path::Path}; +use std::{fs::File, io::Write}; + +use crate::config::get_cache_file; type SearchMap = DashMap; @@ -54,7 +56,7 @@ impl<'a> CachedSearch<'a> { } fn get_search_map() -> SearchMap { - let path = Path::new("cache.json"); + let path = get_cache_file(); if path.exists() { let string = std::fs::read_to_string(path).unwrap(); serde_json::from_str::(&string).expect("Failed to parse cache.json") @@ -65,6 +67,7 @@ fn get_search_map() -> SearchMap { fn save_search_map(search_map: &SearchMap) { let string = serde_json::to_string(search_map).unwrap(); - let mut file = File::create("cache.json").unwrap(); + let path = get_cache_file(); + let mut file = File::create(&path).unwrap(); file.write_all(string.as_bytes()).unwrap(); } diff --git a/src/sync/symlinks.rs b/src/sync/symlinks.rs index 3ce3b61..9e0d13c 100644 --- a/src/sync/symlinks.rs +++ b/src/sync/symlinks.rs @@ -2,11 +2,7 @@ use std::path::Path; use steam_shortcuts_util::{shortcut::ShortcutOwned, Shortcut}; -fn get_boilr_links_path() -> std::path::PathBuf { - let home = std::env::var("HOME").expect("Expected a home variable to be defined"); - let boilr_links_path = Path::new(&home).join(".boilr").join("links"); - boilr_links_path -} +use crate::config::get_boilr_links_path; pub fn create_sym_links(shortcut: &ShortcutOwned) -> ShortcutOwned { let links_folder = get_boilr_links_path(); diff --git a/src/ui/ui_image_download.rs b/src/ui/ui_image_download.rs index 904455a..cae3e47 100644 --- a/src/ui/ui_image_download.rs +++ b/src/ui/ui_image_download.rs @@ -4,6 +4,7 @@ use std::{ }; use crate::{ + config::get_thumbnails_folder, steam::{get_shortcuts_paths, SteamUsersInfo}, steamgriddb::{get_query_type, CachedSearch, ImageType, ToDownload}, }; @@ -399,8 +400,6 @@ impl MyEguiApp { } fn handle_image_type_selected(&mut self, image_type: ImageType) { - let _ = std::fs::create_dir_all(".thumbnails"); - let state = &mut self.image_selected_state; state.image_type_selected = Some(image_type); let (tx, rx) = watch::channel(FetcStatus::Fetching); @@ -411,7 +410,7 @@ impl MyEguiApp { let auth_key = auth_key; let image_type = image_type; self.rt.spawn_blocking(move || { - let thumbnails_folder = Path::new(".thumbnails"); + let thumbnails_folder = get_thumbnails_folder(); let client = steamgriddb_api::Client::new(auth_key); let query = get_query_type(false, &image_type); let search_res = block_on(client.get_images_for_id(grid_id, &query)); diff --git a/src/ui/ui_import_games.rs b/src/ui/ui_import_games.rs index ae26b65..b8a556b 100644 --- a/src/ui/ui_import_games.rs +++ b/src/ui/ui_import_games.rs @@ -127,8 +127,9 @@ impl MyEguiApp { }); } - fn save_settings_to_file(settings: &Settings) { + pub fn save_settings_to_file(settings: &Settings) { let toml = toml::to_string(&settings).unwrap(); - std::fs::write("config.toml", toml).unwrap(); + let config_path = crate::config::get_config_file(); + std::fs::write(config_path, toml).unwrap(); } }