From 87c05770789958e6786e6490eb1fa25cd116b361 Mon Sep 17 00:00:00 2001 From: Ricky Kresslein Date: Sat, 24 Sep 2022 00:34:54 +0200 Subject: Ability to change database location - #29 --- src/database.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'src/database.rs') diff --git a/src/database.rs b/src/database.rs index 1f7b762..589ff2d 100755 --- a/src/database.rs +++ b/src/database.rs @@ -27,6 +27,7 @@ use std::path::PathBuf; use std::time::Duration; use crate::ui::FurtheranceWindow; +use crate::settings_manager; #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] pub struct Task { @@ -121,12 +122,26 @@ impl TaskSort { } pub fn get_directory() -> PathBuf { - if let Some(proj_dirs) = ProjectDirs::from("com", "lakoliu", "Furtherance") { - let mut path = PathBuf::from(proj_dirs.data_dir()); - create_dir_all(path.clone()).expect("Unable to create database directory"); - path.extend(&["furtherance.db"]); - return path; + let dir_from_settings = settings_manager::get_string("database-loc"); + + if dir_from_settings != "default" && PathBuf::from(dir_from_settings.clone()).exists() { + return PathBuf::from(dir_from_settings); + } else { + if let Some(proj_dirs) = ProjectDirs::from("com", "lakoliu", "Furtherance") { + let mut path = PathBuf::from(proj_dirs.data_dir()); + create_dir_all(path.clone()).expect("Unable to create database directory"); + path.extend(&["furtherance.db"]); + + let path_str = path.to_string_lossy().to_string(); + if path_str != dir_from_settings { + let settings = settings_manager::get_settings(); + let _ = settings.set_string("database-loc", &path_str); + } + + return path; + } } + PathBuf::new() } -- cgit 1.4.1