Reading and writing settings through UI

This commit is contained in:
Philip Kristoffersen
2021-10-02 13:45:52 +02:00
parent da4531ecaf
commit 088016843d
14 changed files with 195 additions and 82 deletions
Generated
+1
View File
@@ -100,6 +100,7 @@ dependencies = [
"steam_shortcuts_util", "steam_shortcuts_util",
"steamgriddb_api", "steamgriddb_api",
"tokio", "tokio",
"toml",
] ]
[[package]] [[package]]
+2 -1
View File
@@ -18,10 +18,11 @@ fltk = { version = "^1.2", features = ["fltk-bundled"], optional = true }
nom = "^7.0.*" nom = "^7.0.*"
nom_locate = "^3.0.*" nom_locate = "^3.0.*"
flate2 = "^1.0.22" flate2 = "^1.0.22"
toml = { version = "^0.5.8", optional = true }
[build-dependencies] [build-dependencies]
fl2rust = { version = "0.4", optional = true } fl2rust = { version = "0.4", optional = true }
[features] [features]
# default = ["ui"] # default = ["ui"]
ui = ["fltk", "fl2rust"] ui = ["fltk", "fl2rust", "toml"]
+14 -7
View File
@@ -4,7 +4,7 @@ use std::env::{self};
use std::fs::{DirEntry, File}; use std::fs::{DirEntry, File};
use std::io::BufReader; use std::io::BufReader;
use std::path::Path; use std::path::{Path, PathBuf};
use failure::*; use failure::*;
@@ -64,10 +64,20 @@ fn get_manifest_dir_path(
}); });
} }
} else { } else {
let path = get_default_location();
match path {
Some(path) => Ok(path.to_str().unwrap().to_string()),
None => Err(PathNotDefined),
}
}
}
pub fn get_default_location() -> Option<PathBuf> {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
//No path defined for epic gamestore, and we cannot guess on linux //No path defined for epic gamestore, and we cannot guess on linux
Err(PathNotDefined) None
} }
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
@@ -83,12 +93,9 @@ fn get_manifest_dir_path(
.join("Data") .join("Data")
.join("Manifests"); .join("Manifests");
if path.exists() { if path.exists() {
return Ok(path.to_str().unwrap().to_string()); Some(path)
} else { } else {
return Err(PathNotFound { None
path: path.to_str().unwrap().to_string(),
});
}
} }
} }
} }
+1
View File
@@ -5,5 +5,6 @@ mod epic_platform;
pub(crate) use manifest_item::*; pub(crate) use manifest_item::*;
use get_manifests::get_egs_manifests; use get_manifests::get_egs_manifests;
pub use get_manifests::get_default_location;
pub use settings::EpicGamesLauncherSettings; pub use settings::EpicGamesLauncherSettings;
pub use epic_platform::*; pub use epic_platform::*;
+2 -2
View File
@@ -1,6 +1,6 @@
use serde::Deserialize; use serde::{Serialize,Deserialize};
#[derive(Debug, Deserialize,Clone)] #[derive(Debug,Serialize, Deserialize,Clone)]
pub struct EpicGamesLauncherSettings { pub struct EpicGamesLauncherSettings {
pub enabled: bool, pub enabled: bool,
pub location: Option<String>, pub location: Option<String>,
+1 -1
View File
@@ -79,7 +79,7 @@ fn dbpath_to_game(paths: &DbPaths<'_>) -> Option<ItchGame> {
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
fn get_default_location() -> String { pub fn get_default_location() -> String {
//If we don't have a home drive we have to just die //If we don't have a home drive we have to just die
let home = std::env::var("HOME").expect("Expected a home variable to be defined"); let home = std::env::var("HOME").expect("Expected a home variable to be defined");
format!("{}/.config/itch/", home) format!("{}/.config/itch/", home)
+2 -2
View File
@@ -1,6 +1,6 @@
use serde::Deserialize; use serde::{Serialize,Deserialize};
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ItchSettings { pub struct ItchSettings {
pub enabled: bool, pub enabled: bool,
pub location: Option<String>, pub location: Option<String>,
+2 -2
View File
@@ -1,6 +1,6 @@
use serde::Deserialize; use serde::{Serialize,Deserialize};
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Deserialize, Serialize,Clone)]
pub struct LegendarySettings { pub struct LegendarySettings {
pub enabled: bool, pub enabled: bool,
pub executable: Option<String>, pub executable: Option<String>,
+125 -15
View File
@@ -3,7 +3,13 @@ use crate::{
origin::OriginPlatform, origin::OriginPlatform,
steamgriddb::{download_images, CachedSearch}, steamgriddb::{download_images, CachedSearch},
}; };
use std::{fs::File, io::Write, path::Path}; use std::{
cell::{Ref, RefCell},
fs::File,
io::Write,
path::Path,
rc::Rc,
};
mod egs; mod egs;
mod itch; mod itch;
mod legendary; mod legendary;
@@ -12,6 +18,7 @@ mod platform;
mod settings; mod settings;
mod steam; mod steam;
mod steamgriddb; mod steamgriddb;
use egs::get_default_location;
#[cfg(feature = "ui")] #[cfg(feature = "ui")]
use fltk::{app, prelude::*, window::Window}; use fltk::{app, prelude::*, window::Window};
#[cfg(feature = "ui")] #[cfg(feature = "ui")]
@@ -31,11 +38,83 @@ use steam_shortcuts_util::{shortcut::ShortcutOwned, shortcuts_to_bytes, Shortcut
async fn main() -> Result<(), Box<dyn Error>> { async fn main() -> Result<(), Box<dyn Error>> {
#[cfg(feature = "ui")] #[cfg(feature = "ui")]
{ {
let settings = Settings::new()?; {
let app = app::App::default().with_scheme(app::Scheme::Gtk); let app = app::App::default().with_scheme(app::Scheme::Gtk);
let mut ui = mainview::UserInterface::make_window();
let settings = Rc::new(RefCell::new(Settings::new()?));
let mut ui = mainview::UserInterface::make_window();
update_ui_with_settings(&mut ui, &settings.clone().borrow());
{
let save_ui = ui.clone();
let mut save_button = ui.save_settings_button.clone();
let save_settings = settings.clone();
save_button.set_callback(move |cb| {
update_settings_with_ui_values(&mut save_settings.borrow_mut(), &save_ui);
save_settings_to_file(&save_settings.borrow());
cb.set_label("Saved");
});
}
{
let mut synch_bytton = ui.synchronize_button.clone();
let synch_ui = ui.clone();
let sync_settings = settings.clone();
synch_bytton.set_callback(move |cb| {
update_settings_with_ui_values(&mut sync_settings.borrow_mut(), &synch_ui);
run_sync(&sync_settings.borrow());
cb.set_label("Synched");
});
}
app.run().unwrap();
}
Ok(())
}
#[cfg(not(feature = "ui"))]
{
let settings = Settings::new()?;
run_sync(&settings).await
}
}
fn empty_or_whitespace(input: String) -> Option<String> {
if input.trim().is_empty() {
None
} else {
Some(input)
}
}
fn update_settings_with_ui_values(settings: &mut Settings, ui: &mainview::UserInterface) {
// Steam location
settings.steam.location = empty_or_whitespace(ui.steam_location_input.value());
// Steamgrid db
settings.steamgrid_db.enabled = ui.enable_steamgrid_db_checkbox.value();
settings.steamgrid_db.auth_key = empty_or_whitespace(ui.steamgrid_db_auth_key_input.value());
// Legendary
settings.legendary.enabled = ui.enable_legendary_checkbox.value();
settings.legendary.executable = empty_or_whitespace(ui.legendary_executable_input.value());
// Origin
settings.origin.enabled = ui.enable_origin_checkbox.value();
settings.origin.path = empty_or_whitespace(ui.origin_folder_input.value());
// Epic
settings.epic_games.enabled = ui.enable_egs_checkbox.value();
settings.epic_games.location = empty_or_whitespace(ui.epic_location_input.value());
// Itch
settings.itch.enabled = ui.enable_itch_checkbox.value();
settings.itch.location = empty_or_whitespace(ui.itch_locatoin_input.value());
}
fn save_settings_to_file(settings: &Settings) {
let toml = toml::to_string(&settings).unwrap();
std::fs::write("config.toml", toml).unwrap();
}
fn update_ui_with_settings(ui: &mut mainview::UserInterface, settings: &Settings) {
ui.enable_steamgrid_db_checkbox ui.enable_steamgrid_db_checkbox
.set_value(settings.steamgrid_db.enabled); .set_value(settings.steamgrid_db.enabled);
ui.steamgrid_db_auth_key_input.set_value( ui.steamgrid_db_auth_key_input.set_value(
@@ -45,27 +124,58 @@ async fn main() -> Result<(), Box<dyn Error>> {
.clone() .clone()
.unwrap_or(String::from("")), .unwrap_or(String::from("")),
); );
ui.enable_legendary_checkbox ui.enable_legendary_checkbox
.set_value(settings.legendary.enabled); .set_value(settings.legendary.enabled);
ui.legendary_executable_input.set_value( ui.legendary_executable_input.set_value(
&settings settings
.legendary .legendary
.executable .executable
.unwrap_or(String::from("legendary")), .clone()
.unwrap_or(String::from("legendary"))
.as_str(),
); );
ui.enable_egs_checkbox ui.enable_egs_checkbox
.set_value(settings.epic_games.enabled); .set_value(settings.epic_games.enabled);
app.run().unwrap(); let default_egs = get_default_location();
let default_egs = default_egs
.map(|p| p.to_str().unwrap().to_owned())
.unwrap_or(String::from(""));
let egs_location = settings
.epic_games
.location
.clone()
.unwrap_or(String::from(default_egs));
ui.epic_location_input.set_value(&egs_location);
ui.enable_egs_checkbox
.set_value(settings.epic_games.enabled);
ui.enable_itch_checkbox.set_value(settings.itch.enabled);
let mut default_itch_location = itch::get_default_location();
if !Path::new(&default_itch_location).exists() {
default_itch_location = String::from("");
}
ui.enable_itch_checkbox.set_value(settings.itch.enabled);
ui.itch_locatoin_input.set_value(
&settings
.itch
.location
.clone()
.unwrap_or(default_itch_location),
);
ui.enable_origin_checkbox.set_value(settings.origin.enabled);
let mut default_origin_location = origin::get_default_location();
if !Path::new(&default_origin_location).exists() {
default_origin_location = String::from("");
}
ui.origin_folder_input.set_value(
&settings
.origin
.path
.clone()
.unwrap_or(default_origin_location),
);
} }
run_sync().await async fn run_sync(settings: &Settings) -> Result<(), Box<dyn Error>> {
}
async fn run_sync() -> Result<(), Box<dyn Error>> {
let settings = Settings::new()?;
let userinfo_shortcuts = get_shortcuts_paths(&settings.steam)?; let userinfo_shortcuts = get_shortcuts_paths(&settings.steam)?;
println!("Found {} user(s)", userinfo_shortcuts.len()); println!("Found {} user(s)", userinfo_shortcuts.len());
for user in userinfo_shortcuts.iter() { for user in userinfo_shortcuts.iter() {
+1 -1
View File
@@ -93,7 +93,7 @@ fn parse_id_from_file(i: &str) -> nom::IResult<&str, &str> {
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
fn get_default_location() -> String { pub fn get_default_location() -> String {
//TODO implement this for linux: //TODO implement this for linux:
// https://www.toptensoftware.com/blog/running-ea-origin-games-under-linux-via-steam-and-proton/ // https://www.toptensoftware.com/blog/running-ea-origin-games-under-linux-via-steam-and-proton/
+2 -2
View File
@@ -1,6 +1,6 @@
use serde::Deserialize; use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize,Clone)] #[derive(Debug, Deserialize, Serialize, Clone)]
pub struct OriginSettings { pub struct OriginSettings {
pub enabled: bool, pub enabled: bool,
+7 -14
View File
@@ -1,10 +1,13 @@
use crate::{egs::EpicGamesLauncherSettings, itch::ItchSettings, legendary::LegendarySettings, origin::OriginSettings, steam::SteamSettings, steamgriddb::SteamGridDbSettings}; use crate::{
egs::EpicGamesLauncherSettings, itch::ItchSettings, legendary::LegendarySettings,
origin::OriginSettings, steam::SteamSettings, steamgriddb::SteamGridDbSettings,
};
use config::{Config, ConfigError, Environment, File}; use config::{Config, ConfigError, Environment, File};
use serde::Deserialize; use serde::{Deserialize, Serialize};
use std::{env}; use std::env;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct Settings { pub struct Settings {
pub debug: bool, pub debug: bool,
pub epic_games: EpicGamesLauncherSettings, pub epic_games: EpicGamesLauncherSettings,
@@ -22,16 +25,6 @@ impl Settings {
let default_str = include_str!("defaultconfig.toml"); let default_str = include_str!("defaultconfig.toml");
s.merge(File::from_str(default_str, config::FileFormat::Toml))?; s.merge(File::from_str(default_str, config::FileFormat::Toml))?;
#[cfg(target_os = "linux")]
let enable_legendary = true;
#[cfg(target_os = "linux")]
let enable_epic = false;
#[cfg(target_os = "windows")]
let enable_legendary = false;
#[cfg(target_os = "windows")]
let enable_epic = true;
// Start off by merging in the "default" configuration file // Start off by merging in the "default" configuration file
s.merge(File::with_name("config.toml").required(false))?; s.merge(File::with_name("config.toml").required(false))?;
+2 -2
View File
@@ -1,6 +1,6 @@
use serde::Deserialize; use serde::{Serialize,Deserialize};
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SteamSettings{ pub struct SteamSettings{
pub location: Option<String>, pub location: Option<String>,
} }
+2 -2
View File
@@ -1,6 +1,6 @@
use serde::Deserialize; use serde::{Serialize,Deserialize};
#[derive(Debug, Deserialize)] #[derive(Debug,Serialize, Deserialize)]
pub struct SteamGridDbSettings { pub struct SteamGridDbSettings {
pub enabled: bool, pub enabled: bool,
pub auth_key: Option<String>, pub auth_key: Option<String>,