settings: create the default app folder

To avoid a crash if the settings folder is
not present and the log can not be created properly
This commit is contained in:
Stéphane Cerveau 2023-09-26 17:21:08 +02:00
parent 886c099dba
commit e12fecf971
3 changed files with 23 additions and 24 deletions

View file

@ -458,7 +458,7 @@ impl GPSApp {
let app_weak = self.downgrade(); let app_weak = self.downgrade();
logger::init_logger( logger::init_logger(
ready_tx, ready_tx,
Settings::default_log_file_path() Settings::log_file_path()
.to_str() .to_str()
.expect("Unable to convert log file path to a string"), .expect("Unable to convert log file path to a string"),
); );
@ -607,7 +607,7 @@ impl GPSApp {
let _ = self let _ = self
.load_graph( .load_graph(
Settings::default_graph_file_path() Settings::graph_file_path()
.to_str() .to_str()
.expect("Unable to convert to string"), .expect("Unable to convert to string"),
true, true,

View file

@ -221,7 +221,7 @@ pub fn create_graphtab(app: &GPSApp, id: u32, name: Option<&str>) {
GPS_DEBUG!("Graph updated id={}", id); GPS_DEBUG!("Graph updated id={}", id);
let _ = app let _ = app
.save_graph( .save_graph(
Settings::default_graph_file_path() Settings::graph_file_path()
.to_str() .to_str()
.expect("Unable to convert to string"), .expect("Unable to convert to string"),
) )

View file

@ -31,41 +31,41 @@ pub struct Settings {
} }
impl Settings { impl Settings {
fn settings_file_exist() { fn create_path_if_not(s: &PathBuf) {
let s = Settings::settings_file_path();
if !s.exists() { if !s.exists() {
if let Some(parent_dir) = s.parent() { if let Err(e) = create_dir_all(s) {
if !parent_dir.exists() { GPS_ERROR!(
if let Err(e) = create_dir_all(parent_dir) { "Error while trying to build settings snapshot_directory '{}': {}",
GPS_ERROR!( s.display(),
"Error while trying to build settings snapshot_directory '{}': {}", e
parent_dir.display(), );
e
);
}
}
} }
} }
} }
fn settings_file_path() -> PathBuf { fn default_app_folder() -> PathBuf {
let mut path = glib::user_config_dir(); let mut path = glib::user_config_dir();
path.push(config::APP_ID); path.push(config::APP_ID);
path
}
fn settings_file_path() -> PathBuf {
let mut path = Settings::default_app_folder();
Settings::create_path_if_not(&path);
path.push("settings.toml"); path.push("settings.toml");
path path
} }
// Public methods // Public methods
pub fn default_graph_file_path() -> PathBuf { pub fn graph_file_path() -> PathBuf {
let mut path = glib::user_config_dir(); let mut path = Settings::default_app_folder();
path.push(config::APP_ID); Settings::create_path_if_not(&path);
path.push("default_graph.toml"); path.push("default_graph.toml");
path path
} }
pub fn default_log_file_path() -> PathBuf { pub fn log_file_path() -> PathBuf {
let mut path = glib::user_config_dir(); let mut path = Settings::default_app_folder();
path.push(config::APP_ID); Settings::create_path_if_not(&path);
path.push("gstpipelinestudio.log"); path.push("gstpipelinestudio.log");
path path
} }
@ -105,7 +105,6 @@ impl Settings {
// Save the provided settings to the settings path // Save the provided settings to the settings path
pub fn save_settings(settings: &Settings) { pub fn save_settings(settings: &Settings) {
Settings::settings_file_exist();
let s = Settings::settings_file_path(); let s = Settings::settings_file_path();
if let Err(e) = serde_any::to_file(&s, settings) { if let Err(e) = serde_any::to_file(&s, settings) {
GPS_ERROR!("Error while trying to save file: {} {}", s.display(), e); GPS_ERROR!("Error while trying to save file: {} {}", s.display(), e);