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

View file

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

View file

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