From e2494d240f4bf0dcf8c1a347510adba30eb6e53f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Mikrut?= <41945903+qarmin@users.noreply.github.com> Date: Sat, 25 Dec 2021 13:19:41 +0100 Subject: [PATCH] Reorganize saving/loading data from file (#524) --- czkawka_gui/src/saving_loading.rs | 1230 ++++++++++++----------------- 1 file changed, 516 insertions(+), 714 deletions(-) diff --git a/czkawka_gui/src/saving_loading.rs b/czkawka_gui/src/saving_loading.rs index 5791d15..31ab449 100644 --- a/czkawka_gui/src/saving_loading.rs +++ b/czkawka_gui/src/saving_loading.rs @@ -1,6 +1,7 @@ +use std::collections::HashMap; use std::fs::File; -use std::io::Write; -use std::path::Path; +use std::io::{Read, Write}; +use std::path::{Path, PathBuf}; use std::{env, fs}; use directories_next::ProjectDirs; @@ -16,7 +17,324 @@ use crate::language_functions::{get_language_from_combo_box_text, LANGUAGES_ALL} use crate::localizer::generate_translation_hashmap; // TODO organize this better, add specific functions that will allow to load from files specific strings -const SAVE_FILE_NAME: &str = "czkawka_gui_config.txt"; +const SAVE_FILE_NAME: &str = "czkawka_gui_config_4.txt"; + +const DEFAULT_SAVE_ON_EXIT: bool = true; +const DEFAULT_LOAD_AT_START: bool = true; +const DEFAULT_CONFIRM_DELETION: bool = true; +const DEFAULT_CONFIRM_GROUP_DELETION: bool = true; +const DEFAULT_SHOW_IMAGE_PREVIEW: bool = true; +const DEFAULT_SHOW_DUPLICATE_IMAGE_PREVIEW: bool = true; +const DEFAULT_BOTTOM_TEXT_VIEW: bool = true; +const DEFAULT_USE_CACHE: bool = true; +const DEFAULT_HIDE_HARD_LINKS: bool = true; +const DEFAULT_USE_PRECACHE: bool = false; +const DEFAULT_USE_TRASH: bool = false; +const DEFAULT_MINIMAL_CACHE_SIZE: &str = "257144"; +const DEFAULT_PREHASH_MINIMAL_CACHE_SIZE: &str = "0"; +const DEFAULT_VIDEO_REMOVE_AUTO_OUTDATED_CACHE: bool = false; +const DEFAULT_IMAGE_REMOVE_AUTO_OUTDATED_CACHE: bool = true; +const DEFAULT_DUPLICATE_REMOVE_AUTO_OUTDATED_CACHE: bool = true; + +struct LoadSaveStruct { + loaded_items: HashMap>, +} + +impl LoadSaveStruct { + pub fn new() -> Self { + Self { loaded_items: Default::default() } + } + + pub fn get_vector_string(&self, key: String, default_value: Vec) -> Vec { + if self.loaded_items.contains_key(&key) { + return self.loaded_items.get(&key).unwrap().clone(); + } + + default_value + } + pub fn get_string(&self, key: String, default_value: String) -> String { + if self.loaded_items.contains_key(&key) { + let item = self.loaded_items.get(&key).unwrap().clone().into_iter().filter(|e| !e.is_empty()).collect::>(); + return if item.len() == 1 { + item[0].clone() + } else if item.is_empty() { + "".to_string() + } else { + println!("For key {}, found invalid {:?} result(not string)", key, item); + default_value + }; + } + + default_value + } + /* pub fn get_integer(&self, key: String, default_value: T) -> T { + if self.loaded_items.contains_key(&key) { + let item = self.loaded_items.get(&key).unwrap().clone().into_iter().filter(|e| !e.is_empty()).collect::>(); + + return if item.len() == 1 { + match item[0].parse::() { + Ok(t) => t, + Err(_) => { + println!("Failed to decode integer from \"{}\", found {:?}", key, item[0]); + default_value + } + } + } else { + println!("For key {}, found invalid {:?} result(not string)", key, item); + default_value + }; + } + + default_value + }*/ + pub fn get_bool(&self, key: String, default_value: bool) -> bool { + if self.loaded_items.contains_key(&key) { + let item = self.loaded_items.get(&key).unwrap().clone().into_iter().filter(|e| !e.is_empty()).collect::>(); + return if item.len() == 1 { + let text = item[0].clone().trim().to_lowercase(); + if text == "false" || text == "0" { + false + } else if text == "true" || text == "1" { + true + } else { + println!("Failed to decode bool from \"{}\", found {}", key, item[0]); + default_value + } + } else { + println!("For key {}, found invalid {:?} result(not bool)", key, item); + default_value + }; + } + + default_value + } + + // Bool, int, string + pub fn save_var(&mut self, key: String, value: T) { + if self.loaded_items.contains_key(&key) { + println!("Already exists in hashmap key {}", key) + } + + self.loaded_items.insert(key, vec![value.to_string()]); + } + + pub fn save_list_store(&mut self, key: String, tree_view: >k::TreeView, column_path: i32) { + let mut vec_string = vec![]; + let list_store = get_list_store(tree_view); + if let Some(iter) = list_store.iter_first() { + loop { + // TODO maybe save also here reference directories? + vec_string.push(list_store.value(&iter, column_path).get::().unwrap()); + if !list_store.iter_next(&iter) { + break; + } + } + } + self.loaded_items.insert(key, vec_string); + } + + pub fn open_save_file(&self, text_view_errors: &TextView, save_configuration: bool, manual_execution: bool) -> Option<(File, PathBuf)> { + if let Some(proj_dirs) = ProjectDirs::from("pl", "Qarmin", "Czkawka") { + // Lin: /home/username/.config/czkawka + // Win: C:\Users\Username\AppData\Roaming\Qarmin\Czkawka\config + // Mac: /Users/Username/Library/Application Support/pl.Qarmin.Czkawka + + let config_dir = proj_dirs.config_dir(); + let config_file = config_dir.join(Path::new(SAVE_FILE_NAME)); + + if save_configuration { + if config_dir.exists() { + if !config_dir.is_dir() { + add_text_to_text_view( + text_view_errors, + format!( + "Cannot create or open save configuration file in path {} because already there is a folder.", + config_dir.display() + ) + .as_str(), + ); + return None; + } + } else if let Err(e) = fs::create_dir_all(config_dir) { + add_text_to_text_view( + text_view_errors, + format!("Failed configuration to create configuration folder {}, reason {}", config_dir.display(), e).as_str(), + ); + return None; + } + + let config_file_handler = match File::create(&config_file) { + Ok(t) => t, + Err(e) => { + add_text_to_text_view(text_view_errors, format!("Failed to create config file {}, reason {}", config_dir.display(), e).as_str()); + return None; + } + }; + return Some((config_file_handler, config_file)); + } else { + if !config_file.exists() || !config_file.is_file() { + if manual_execution { + // Don't show errors when there is no configuration file when starting app + add_text_to_text_view(text_view_errors, format!("Cannot load configuration from file {:?}.", config_file.display()).as_str()); + } + return None; + } + + let config_file_handler = match File::open(&config_file) { + Ok(t) => t, + Err(e) => { + add_text_to_text_view(text_view_errors, format!("Failed to create config file {}, reason {}", config_dir.display(), e).as_str()); + return None; + } + }; + return Some((config_file_handler, config_file)); + } + } else { + add_text_to_text_view(text_view_errors, fl!("saving_loading_failed_to_get_home_directory").as_str()); + } + None + } + + pub fn open_and_read_content(&mut self, text_view_errors: &TextView, manual_execution: bool) { + if let Some((mut config_file_handler, config_file)) = self.open_save_file(text_view_errors, false, manual_execution) { + let mut loaded_data: String = String::new(); + if let Err(e) = config_file_handler.read_to_string(&mut loaded_data) { + add_text_to_text_view(text_view_errors, format!("Failed to read data from file {:?}, reason {}", config_file, e).as_str()); + return; + } + + let mut header: String = "".to_string(); + let lines: Vec = loaded_data.replace('\r', "").split('\n').map(String::from).collect::>(); + for (index, line) in lines.iter().enumerate() { + let line = line.trim(); + if line.starts_with("--") { + header = line.to_string(); + } else if !header.is_empty() { + self.loaded_items.entry(header.clone()).or_insert_with(Vec::new); + self.loaded_items.get_mut(&header).unwrap().push(line.to_string()); + } else { + println!("Failed {} orphan data in line {}", line, index); + } + } + + let (_, hashmap_sl) = create_hash_map(); + for setting in self.loaded_items.keys() { + if !hashmap_sl.contains_key(setting) { + println!("Setting {} is not valid", setting); + } + } + + // dbg!(&self.loaded_items); + } + } + + pub fn save_to_file(&self, text_view_errors: &TextView) { + if let Some((mut config_file_handler, config_file)) = self.open_save_file(text_view_errors, true, false) { + let mut data_saved: bool = false; + for (key, vec_string) in &self.loaded_items { + match writeln!(config_file_handler, "{}", key) { + Ok(_inspected) => { + data_saved = true; + } + Err(_inspected) => { + data_saved = false; + break; + } + } + for data in vec_string { + match writeln!(config_file_handler, "{}", data) { + Ok(_inspected) => { + data_saved = true; + } + Err(_inspected) => { + data_saved = false; + break; + } + } + } + } + if data_saved { + add_text_to_text_view( + text_view_errors, + fl!( + "saving_loading_saving_success", + generate_translation_hashmap(vec![("name", config_file.display().to_string())]) + ) + .as_str(), + ); + } else { + add_text_to_text_view( + text_view_errors, + fl!( + "saving_loading_saving_failure", + generate_translation_hashmap(vec![("name", config_file.display().to_string())]) + ) + .as_str(), + ); + } + } + } +} + +#[derive(Copy, Clone, Hash, Eq, PartialEq)] +enum LoadText { + IncludedDirectories, + ExcludedDirectories, + ExcludedItems, + AllowedExtensions, + SaveAtExit, + LoadAtStart, + ConfirmDeletionFiles, + ConfirmDeletionAllFilesInGroup, + ShowBottomTextPanel, + HideHardLinks, + UseCache, + DeleteToTrash, + MinimalCacheSize, + ImagePreviewImage, + DuplicatePreviewImage, + DuplicateDeleteOutdatedCacheEntries, + ImageDeleteOutdatedCacheEntries, + VideoDeleteOutdatedCacheEntries, + UsePrehashCache, + MinimalPrehashCacheSize, + Language, +} + +fn create_hash_map() -> (HashMap, HashMap) { + let values = [ + (LoadText::IncludedDirectories, "included_directories"), + (LoadText::ExcludedDirectories, "excluded_directories"), + (LoadText::ExcludedItems, "excluded_items"), + (LoadText::AllowedExtensions, "allowed_extensions"), + (LoadText::SaveAtExit, "save_at_exit"), + (LoadText::LoadAtStart, "load_at_start"), + (LoadText::ConfirmDeletionFiles, "confirm_deletion_files"), + (LoadText::ConfirmDeletionAllFilesInGroup, "confirm_deletion_all_files_in_group"), + (LoadText::ShowBottomTextPanel, "show_bottom_text_panel"), + (LoadText::HideHardLinks, "hide_hard_links"), + (LoadText::UseCache, "use_cache"), + (LoadText::DeleteToTrash, "delete_to_trash"), + (LoadText::MinimalCacheSize, "minimal_cache_size"), + (LoadText::ImagePreviewImage, "image_preview_image"), + (LoadText::DuplicatePreviewImage, "duplicate_preview_image"), + (LoadText::DuplicateDeleteOutdatedCacheEntries, "duplicate_delete_outdated_cache_entries"), + (LoadText::ImageDeleteOutdatedCacheEntries, "image_delete_outdated_cache_entries"), + (LoadText::VideoDeleteOutdatedCacheEntries, "video_delete_outdated_cache_entries"), + (LoadText::UsePrehashCache, "use_prehash_cache"), + (LoadText::MinimalPrehashCacheSize, "minimal_prehash_cache_size"), + (LoadText::Language, "language"), + ]; + let mut hashmap_ls: HashMap = Default::default(); + let mut hashmap_sl: HashMap = Default::default(); + + for (load_text, string) in values { + hashmap_ls.insert(load_text, format!("--{}", string)); + hashmap_sl.insert(format!("--{}", string), load_text); + } + + (hashmap_ls, hashmap_sl) +} pub fn save_configuration(manual_execution: bool, upper_notebook: &GuiUpperNotebook, settings: &GuiSettings, text_view_errors: &TextView) { let check_button_settings_save_at_exit = settings.check_button_settings_save_at_exit.clone(); @@ -28,238 +346,101 @@ pub fn save_configuration(manual_execution: bool, upper_notebook: &GuiUpperNoteb // When check button is deselected, not save configuration at exit return; } - if let Some(proj_dirs) = ProjectDirs::from("pl", "Qarmin", "Czkawka") { - // Lin: /home/username/.config/czkawka - // Win: C:\Users\Username\AppData\Roaming\Qarmin\Czkawka\config - // Mac: /Users/Username/Library/Application Support/pl.Qarmin.Czkawka - let config_dir = proj_dirs.config_dir(); - if config_dir.exists() { - if !config_dir.is_dir() { - add_text_to_text_view( - &text_view_errors, - format!("Cannot create save file inside {} because this isn't a folder.", config_dir.display()).as_str(), - ); - return; - } - } else if let Err(e) = fs::create_dir_all(config_dir) { - add_text_to_text_view( - &text_view_errors, - format!("Failed configuration to create configuration folder {}, reason {}", config_dir.display(), e).as_str(), - ); - return; - } - let mut data_to_save: Vec = Vec::with_capacity(16); + let mut saving_struct = LoadSaveStruct::new(); - //// Included Directories - data_to_save.push("--included_directories:".to_string()); - let tree_view_included_directories = upper_notebook.tree_view_included_directories.clone(); - let list_store = get_list_store(&tree_view_included_directories); - if let Some(iter) = list_store.iter_first() { - loop { - // TODO maybe save also here reference directories? - data_to_save.push(list_store.value(&iter, ColumnsIncludedDirectory::Path as i32).get::().unwrap()); - if !list_store.iter_next(&iter) { - break; - } - } - } + let (hashmap_ls, _hashmap_sl) = create_hash_map(); - //// Excluded Directories - data_to_save.push("--excluded_directories:".to_string()); - let tree_view_excluded_directories = upper_notebook.tree_view_excluded_directories.clone(); - let list_store = get_list_store(&tree_view_excluded_directories); - if let Some(iter) = list_store.iter_first() { - loop { - data_to_save.push(list_store.value(&iter, ColumnsExcludedDirectory::Path as i32).get::().unwrap()); - if !list_store.iter_next(&iter) { - break; - } - } - } + // Upper notebook + saving_struct.save_list_store( + hashmap_ls.get(&LoadText::IncludedDirectories).unwrap().to_string(), + &upper_notebook.tree_view_included_directories.clone(), + ColumnsIncludedDirectory::Path as i32, + ); + saving_struct.save_list_store( + hashmap_ls.get(&LoadText::ExcludedDirectories).unwrap().to_string(), + &upper_notebook.tree_view_excluded_directories.clone(), + ColumnsExcludedDirectory::Path as i32, + ); + saving_struct.save_var(hashmap_ls.get(&LoadText::ExcludedItems).unwrap().to_string(), upper_notebook.entry_excluded_items.text()); + saving_struct.save_var( + hashmap_ls.get(&LoadText::AllowedExtensions).unwrap().to_string(), + upper_notebook.entry_allowed_extensions.text(), + ); - { - //// Excluded Items - data_to_save.push("--excluded_items:".to_string()); - let entry_excluded_items = upper_notebook.entry_excluded_items.clone(); - for item in entry_excluded_items.text().split(',') { - if item.trim().is_empty() { - continue; - } - data_to_save.push(item.to_string()); - } + // Check buttons + saving_struct.save_var( + hashmap_ls.get(&LoadText::SaveAtExit).unwrap().to_string(), + settings.check_button_settings_save_at_exit.is_active(), + ); + saving_struct.save_var( + hashmap_ls.get(&LoadText::LoadAtStart).unwrap().to_string(), + settings.check_button_settings_load_at_start.is_active(), + ); + saving_struct.save_var( + hashmap_ls.get(&LoadText::ConfirmDeletionFiles).unwrap().to_string(), + settings.check_button_settings_confirm_deletion.is_active(), + ); + saving_struct.save_var( + hashmap_ls.get(&LoadText::ConfirmDeletionAllFilesInGroup).unwrap().to_string(), + settings.check_button_settings_confirm_group_deletion.is_active(), + ); + saving_struct.save_var( + hashmap_ls.get(&LoadText::ImagePreviewImage).unwrap().to_string(), + settings.check_button_settings_show_preview_similar_images.is_active(), + ); + saving_struct.save_var( + hashmap_ls.get(&LoadText::DuplicatePreviewImage).unwrap().to_string(), + settings.check_button_settings_show_preview_duplicates.is_active(), + ); + saving_struct.save_var( + hashmap_ls.get(&LoadText::HideHardLinks).unwrap().to_string(), + settings.check_button_settings_hide_hard_links.is_active(), + ); + saving_struct.save_var( + hashmap_ls.get(&LoadText::UseCache).unwrap().to_string(), + settings.check_button_settings_use_cache.is_active(), + ); + saving_struct.save_var( + hashmap_ls.get(&LoadText::DeleteToTrash).unwrap().to_string(), + settings.check_button_settings_use_trash.is_active(), + ); + saving_struct.save_var( + hashmap_ls.get(&LoadText::ImageDeleteOutdatedCacheEntries).unwrap().to_string(), + settings.check_button_settings_similar_images_delete_outdated_cache.is_active(), + ); + saving_struct.save_var( + hashmap_ls.get(&LoadText::DuplicateDeleteOutdatedCacheEntries).unwrap().to_string(), + settings.check_button_settings_duplicates_delete_outdated_cache.is_active(), + ); + saving_struct.save_var( + hashmap_ls.get(&LoadText::VideoDeleteOutdatedCacheEntries).unwrap().to_string(), + settings.check_button_settings_similar_videos_delete_outdated_cache.is_active(), + ); + saving_struct.save_var( + hashmap_ls.get(&LoadText::UsePrehashCache).unwrap().to_string(), + settings.check_button_duplicates_use_prehash_cache.is_active(), + ); + saving_struct.save_var( + hashmap_ls.get(&LoadText::ShowBottomTextPanel).unwrap().to_string(), + settings.check_button_settings_show_text_view.is_active(), + ); - //// Allowed extensions - data_to_save.push("--allowed_extensions:".to_string()); - let entry_allowed_extensions = upper_notebook.entry_allowed_extensions.clone(); - for extension in entry_allowed_extensions.text().split(',') { - if extension.trim().is_empty() { - continue; - } - data_to_save.push(extension.to_string()); - } + // Others + saving_struct.save_var( + hashmap_ls.get(&LoadText::MinimalCacheSize).unwrap().to_string(), + settings.entry_settings_cache_file_minimal_size.text(), + ); + saving_struct.save_var( + hashmap_ls.get(&LoadText::MinimalPrehashCacheSize).unwrap().to_string(), + settings.entry_settings_prehash_cache_file_minimal_size.text(), + ); + saving_struct.save_var( + hashmap_ls.get(&LoadText::Language).unwrap().to_string(), + get_language_from_combo_box_text(settings.combo_box_settings_language.active_text().unwrap().to_string()).short_text, + ); - //// Save at exit - data_to_save.push("--save_at_exit:".to_string()); - let check_button_settings_save_at_exit = settings.check_button_settings_save_at_exit.clone(); - data_to_save.push(check_button_settings_save_at_exit.is_active().to_string()); - - //// Load at start - data_to_save.push("--load_at_start:".to_string()); - let check_button_settings_load_at_start = settings.check_button_settings_load_at_start.clone(); - data_to_save.push(check_button_settings_load_at_start.is_active().to_string()); - - //// Confirm deletion of files - data_to_save.push("--confirm_deletion:".to_string()); - let check_button_settings_confirm_deletion = settings.check_button_settings_confirm_deletion.clone(); - data_to_save.push(check_button_settings_confirm_deletion.is_active().to_string()); - - //// Confirm deletion of all files in group - data_to_save.push("--confirm_group_deletion:".to_string()); - let check_button_settings_confirm_group_deletion = settings.check_button_settings_confirm_group_deletion.clone(); - data_to_save.push(check_button_settings_confirm_group_deletion.is_active().to_string()); - - //// Show image previews in similar images - data_to_save.push("--show_previews_similar_images:".to_string()); - let check_button_settings_show_preview_similar_images = settings.check_button_settings_show_preview_similar_images.clone(); - data_to_save.push(check_button_settings_show_preview_similar_images.is_active().to_string()); - - //// Show image previews in duplicates - data_to_save.push("--show_previews_duplicates:".to_string()); - let check_button_settings_show_preview_duplicates = settings.check_button_settings_show_preview_duplicates.clone(); - data_to_save.push(check_button_settings_show_preview_duplicates.is_active().to_string()); - - //// Show bottom text panel with errors - data_to_save.push("--bottom_text_panel:".to_string()); - let check_button_settings_show_text_view = settings.check_button_settings_show_text_view.clone(); - data_to_save.push(check_button_settings_show_text_view.is_active().to_string()); - - //// Hide/Show hard linked files, with same inodes - data_to_save.push("--hide_hard_links:".to_string()); - let check_button_settings_hide_hard_links = settings.check_button_settings_hide_hard_links.clone(); - data_to_save.push(check_button_settings_hide_hard_links.is_active().to_string()); - - //// Use cache system - data_to_save.push("--use_cache:".to_string()); - let check_button_settings_use_cache = settings.check_button_settings_use_cache.clone(); - data_to_save.push(check_button_settings_use_cache.is_active().to_string()); - - //// Delete to trash - data_to_save.push("--use_trash:".to_string()); - let check_button_settings_use_trash = settings.check_button_settings_use_trash.clone(); - data_to_save.push(check_button_settings_use_trash.is_active().to_string()); - - //// minimal cache file size - data_to_save.push("--cache_minimal_file_size:".to_string()); - let entry_settings_cache_file_minimal_size = settings.entry_settings_cache_file_minimal_size.clone(); - data_to_save.push(entry_settings_cache_file_minimal_size.text().as_str().parse::().unwrap_or(1024 * 1024 / 4).to_string()); - - //// Duplicates, delete outdated entries to trash - data_to_save.push("--delete_outdated_entries_duplicates:".to_string()); - let check_button_settings_duplicates_delete_outdated_cache = settings.check_button_settings_duplicates_delete_outdated_cache.clone(); - data_to_save.push(check_button_settings_duplicates_delete_outdated_cache.is_active().to_string()); - - //// Similar Images, delete outdated entries to trash - data_to_save.push("--delete_outdated_entries_similar_images:".to_string()); - let check_button_settings_similar_images_delete_outdated_cache = settings.check_button_settings_similar_images_delete_outdated_cache.clone(); - data_to_save.push(check_button_settings_similar_images_delete_outdated_cache.is_active().to_string()); - - //// Similar Videos, delete outdated entries to trash - data_to_save.push("--delete_outdated_entries_similar_videos:".to_string()); - let check_button_settings_similar_videos_delete_outdated_cache = settings.check_button_settings_similar_videos_delete_outdated_cache.clone(); - data_to_save.push(check_button_settings_similar_videos_delete_outdated_cache.is_active().to_string()); - - //// Use prehash cache system - data_to_save.push("--use_prehash_cache:".to_string()); - let check_button_duplicates_use_prehash_cache = settings.check_button_duplicates_use_prehash_cache.clone(); - data_to_save.push(check_button_duplicates_use_prehash_cache.is_active().to_string()); - - //// minimal prehash cache file size - data_to_save.push("--cache_prehash_minimal_file_size:".to_string()); - let entry_settings_prehash_cache_file_minimal_size = settings.entry_settings_prehash_cache_file_minimal_size.clone(); - data_to_save.push(entry_settings_prehash_cache_file_minimal_size.text().as_str().parse::().unwrap_or(0).to_string()); - - //// language - data_to_save.push("--language:".to_string()); - let combo_box_settings_language = settings.combo_box_settings_language.clone(); - data_to_save.push( - get_language_from_combo_box_text(combo_box_settings_language.active_text().unwrap().to_string()) - .short_text - .to_string(), - ); - } - - // Creating/Opening config file - - let config_file = config_dir.join(Path::new(SAVE_FILE_NAME)); - - let mut config_file_handler = match File::create(&config_file) { - Ok(t) => t, - Err(e) => { - add_text_to_text_view(&text_view_errors, format!("Failed to create config file {}, reason {}", config_dir.display(), e).as_str()); - return; - } - }; - - let mut data_saved: bool = false; - for data in data_to_save { - match writeln!(config_file_handler, "{}", data) { - Ok(_inspected) => { - data_saved = true; - } - Err(_inspected) => { - data_saved = false; - break; - } - } - } - if data_saved { - add_text_to_text_view( - &text_view_errors, - fl!( - "saving_loading_saving_success", - generate_translation_hashmap(vec![("name", config_file.display().to_string())]) - ) - .as_str(), - ); - } else { - add_text_to_text_view( - &text_view_errors, - fl!( - "saving_loading_saving_failure", - generate_translation_hashmap(vec![("name", config_file.display().to_string())]) - ) - .as_str(), - ); - } - } else { - add_text_to_text_view(&text_view_errors, fl!("saving_loading_failed_to_get_home_directory").as_str()); - } -} - -enum TypeOfLoadedData { - None, - IncludedDirectories, - ExcludedDirectories, - ExcludedItems, - AllowedExtensions, - LoadingAtStart, - SavingAtExit, - ConfirmDeletion, - ConfirmGroupDeletion, - ShowPreviewSimilarImages, - ShowPreviewDuplicates, - BottomTextPanel, - HideHardLinks, - UseCache, - UseTrash, - CacheMinimalSize, - DeleteCacheDuplicates, - DeleteCacheSimilarImages, - DeleteCacheSimilarVideos, - UsePrehashCache, - CachePrehashMinimalSize, - Language, + saving_struct.save_to_file(&text_view_errors); } pub fn load_configuration(manual_execution: bool, upper_notebook: &GuiUpperNotebook, settings: &GuiSettings, text_view_errors: &TextView, scrolled_window_errors: &ScrolledWindow) { @@ -267,431 +448,56 @@ pub fn load_configuration(manual_execution: bool, upper_notebook: &GuiUpperNoteb reset_text_view(&text_view_errors); - if let Some(proj_dirs) = ProjectDirs::from("pl", "Qarmin", "Czkawka") { - // Lin: /home/username/.config/czkawka - // Win: C:\Users\Username\AppData\Roaming\Qarmin\Czkawka\config - // Mac: /Users/Username/Library/Application Support/pl.Qarmin.Czkawka + let mut loaded_entries = LoadSaveStruct::new(); + loaded_entries.open_and_read_content(&text_view_errors, manual_execution); - let config_dir = proj_dirs.config_dir(); - let config_file = config_dir.join(Path::new(SAVE_FILE_NAME)); - if !config_file.exists() || !config_file.is_file() { - if manual_execution { - // Don't show errors when there is no configuration file when starting app - add_text_to_text_view(&text_view_errors, format!("Cannot load configuration from file {:?}.", config_file.display()).as_str()); - } - return; - } + // Load here language, default system language could change value in settings so we don't want to lose this value + let short_language = get_language_from_combo_box_text(settings.combo_box_settings_language.active_text().unwrap().to_string()) + .short_text + .to_string(); - // Loading Data - let loaded_data: String = match fs::read_to_string(&config_file) { - Ok(t) => t, - Err(e) => { - add_text_to_text_view(&text_view_errors, format!("Failed to read data from file {:?}, reason {}", config_file, e).as_str()); - return; - } - }; + let (hashmap_ls, _hashmap_sl) = create_hash_map(); - let mut short_language: String; + let included_directories: Vec = loaded_entries.get_vector_string(hashmap_ls.get(&LoadText::IncludedDirectories).unwrap().clone(), Vec::new()); + let excluded_directories: Vec = loaded_entries.get_vector_string(hashmap_ls.get(&LoadText::ExcludedDirectories).unwrap().clone(), Vec::new()); + let excluded_items: String = loaded_entries.get_string(hashmap_ls.get(&LoadText::ExcludedItems).unwrap().clone(), "".to_string()); + let allowed_extensions: String = loaded_entries.get_string(hashmap_ls.get(&LoadText::AllowedExtensions).unwrap().clone(), "".to_string()); - // Load here language, default system language could change value in settings so we don't want to lose this value + let loading_at_start: bool = loaded_entries.get_bool(hashmap_ls.get(&LoadText::LoadAtStart).unwrap().clone(), DEFAULT_LOAD_AT_START); + let saving_at_exit: bool = loaded_entries.get_bool(hashmap_ls.get(&LoadText::SaveAtExit).unwrap().clone(), DEFAULT_SAVE_ON_EXIT); + let confirm_deletion: bool = loaded_entries.get_bool(hashmap_ls.get(&LoadText::ConfirmDeletionFiles).unwrap().clone(), DEFAULT_CONFIRM_DELETION); + let confirm_group_deletion: bool = loaded_entries.get_bool(hashmap_ls.get(&LoadText::ConfirmDeletionAllFilesInGroup).unwrap().clone(), DEFAULT_CONFIRM_GROUP_DELETION); + let show_previews_similar_images: bool = loaded_entries.get_bool(hashmap_ls.get(&LoadText::ImagePreviewImage).unwrap().clone(), DEFAULT_SHOW_IMAGE_PREVIEW); + let show_previews_duplicates: bool = loaded_entries.get_bool(hashmap_ls.get(&LoadText::DuplicatePreviewImage).unwrap().clone(), DEFAULT_SHOW_DUPLICATE_IMAGE_PREVIEW); + let bottom_text_panel: bool = loaded_entries.get_bool(hashmap_ls.get(&LoadText::ShowBottomTextPanel).unwrap().clone(), DEFAULT_BOTTOM_TEXT_VIEW); + let hide_hard_links: bool = loaded_entries.get_bool(hashmap_ls.get(&LoadText::HideHardLinks).unwrap().clone(), DEFAULT_HIDE_HARD_LINKS); + let use_cache: bool = loaded_entries.get_bool(hashmap_ls.get(&LoadText::UseCache).unwrap().clone(), DEFAULT_USE_CACHE); + let use_trash: bool = loaded_entries.get_bool(hashmap_ls.get(&LoadText::DeleteToTrash).unwrap().clone(), DEFAULT_USE_TRASH); + let delete_outdated_cache_duplicates: bool = loaded_entries.get_bool( + hashmap_ls.get(&LoadText::DuplicateDeleteOutdatedCacheEntries).unwrap().clone(), + DEFAULT_DUPLICATE_REMOVE_AUTO_OUTDATED_CACHE, + ); + let delete_outdated_cache_similar_images: bool = loaded_entries.get_bool( + hashmap_ls.get(&LoadText::ImageDeleteOutdatedCacheEntries).unwrap().clone(), + DEFAULT_IMAGE_REMOVE_AUTO_OUTDATED_CACHE, + ); + let delete_outdated_cache_similar_videos: bool = loaded_entries.get_bool( + hashmap_ls.get(&LoadText::VideoDeleteOutdatedCacheEntries).unwrap().clone(), + DEFAULT_VIDEO_REMOVE_AUTO_OUTDATED_CACHE, + ); + let use_prehash_cache: bool = loaded_entries.get_bool(hashmap_ls.get(&LoadText::UsePrehashCache).unwrap().clone(), DEFAULT_USE_PRECACHE); + + let cache_prehash_minimal_size: String = loaded_entries.get_string( + hashmap_ls.get(&LoadText::MinimalPrehashCacheSize).unwrap().clone(), + DEFAULT_PREHASH_MINIMAL_CACHE_SIZE.to_string(), + ); + let cache_minimal_size: String = loaded_entries.get_string(hashmap_ls.get(&LoadText::MinimalCacheSize).unwrap().clone(), DEFAULT_MINIMAL_CACHE_SIZE.to_string()); + let short_language = loaded_entries.get_string(hashmap_ls.get(&LoadText::Language).unwrap().clone(), short_language); + + // Setting data + if manual_execution || loading_at_start { { - short_language = get_language_from_combo_box_text(settings.combo_box_settings_language.active_text().unwrap().to_string()) - .short_text - .to_string(); - } - - // Parsing Data - this are default values - - let mut included_directories: Vec = Vec::new(); - let mut excluded_directories: Vec = Vec::new(); - let mut excluded_items: Vec = Vec::new(); - let mut allowed_extensions: Vec = Vec::new(); - let mut loading_at_start: bool = true; - let mut saving_at_exit: bool = true; - let mut confirm_deletion: bool = true; - let mut confirm_group_deletion: bool = true; - let mut show_previews_similar_images: bool = true; - let mut show_previews_duplicates: bool = true; - let mut bottom_text_panel: bool = true; - let mut hide_hard_links: bool = true; - let mut use_cache: bool = true; - let mut use_trash: bool = false; - let mut cache_minimal_size: u64 = 2 * 1024 * 1024; - let mut delete_outdated_cache_dupliactes: bool = true; - let mut delete_outdated_cache_similar_images: bool = true; - let mut delete_outdated_cache_similar_videos: bool = false; - let mut use_prehash_cache: bool = false; - let mut cache_prehash_minimal_size: u64 = 0; - - let mut current_type = TypeOfLoadedData::None; - for (line_number, line) in loaded_data.replace("\r\n", "\n").split('\n').enumerate() { - let line: String = line.trim().to_string(); - if line.is_empty() { - continue; // Empty line, so we just skip it - } - if line.starts_with("--included_directories") { - current_type = TypeOfLoadedData::IncludedDirectories; - } else if line.starts_with("--excluded_directories") { - current_type = TypeOfLoadedData::ExcludedDirectories; - } else if line.starts_with("--excluded_items") { - current_type = TypeOfLoadedData::ExcludedItems; - } else if line.starts_with("--allowed_extensions") { - current_type = TypeOfLoadedData::AllowedExtensions; - } else if line.starts_with("--load_at_start") { - current_type = TypeOfLoadedData::LoadingAtStart; - } else if line.starts_with("--save_at_exit") { - current_type = TypeOfLoadedData::SavingAtExit; - } else if line.starts_with("--confirm_deletion") { - current_type = TypeOfLoadedData::ConfirmDeletion; - } else if line.starts_with("--confirm_group_deletion") { - current_type = TypeOfLoadedData::ConfirmGroupDeletion; - } else if line.starts_with("--show_previews_similar_images") { - current_type = TypeOfLoadedData::ShowPreviewSimilarImages; - } else if line.starts_with("--show_previews_duplicates") { - current_type = TypeOfLoadedData::ShowPreviewDuplicates; - } else if line.starts_with("--bottom_text_panel") { - current_type = TypeOfLoadedData::BottomTextPanel; - } else if line.starts_with("--hide_hard_links") { - current_type = TypeOfLoadedData::HideHardLinks; - } else if line.starts_with("--use_cache") { - current_type = TypeOfLoadedData::UseCache; - } else if line.starts_with("--use_trash") { - current_type = TypeOfLoadedData::UseTrash; - } else if line.starts_with("--cache_minimal_file_size") { - current_type = TypeOfLoadedData::CacheMinimalSize; - } else if line.starts_with("--delete_outdated_entries_duplicates") { - current_type = TypeOfLoadedData::DeleteCacheDuplicates; - } else if line.starts_with("--delete_outdated_entries_similar_videos") { - current_type = TypeOfLoadedData::DeleteCacheSimilarVideos; - } else if line.starts_with("--delete_outdated_entries_similar_images") { - current_type = TypeOfLoadedData::DeleteCacheSimilarImages; - } else if line.starts_with("--use_prehash_cache") { - current_type = TypeOfLoadedData::UsePrehashCache; - } else if line.starts_with("--cache_prehash_minimal_file_size") { - current_type = TypeOfLoadedData::CachePrehashMinimalSize; - } else if line.starts_with("--language") { - current_type = TypeOfLoadedData::Language; - } else if line.starts_with("--") { - current_type = TypeOfLoadedData::None; - add_text_to_text_view( - &text_view_errors, - fl!( - "settings_load_orphan_data", - generate_translation_hashmap(vec![("name", config_file.display().to_string()), ("line_number", line_number.to_string()), ("line", line)]) - ) - .as_str(), - ); - } else { - match current_type { - TypeOfLoadedData::None => { - add_text_to_text_view( - &text_view_errors, - fl!( - "settings_load_orphan_data", - generate_translation_hashmap(vec![("name", config_file.display().to_string()), ("line_number", line_number.to_string()), ("line", line)]) - ) - .as_str(), - ); - } - TypeOfLoadedData::IncludedDirectories => { - included_directories.push(line); - } - TypeOfLoadedData::ExcludedDirectories => { - excluded_directories.push(line); - } - TypeOfLoadedData::ExcludedItems => { - excluded_items.push(line); - } - TypeOfLoadedData::AllowedExtensions => { - allowed_extensions.push(line); - } - TypeOfLoadedData::LoadingAtStart => { - let line = line.to_lowercase(); - if line == "1" || line == "true" { - loading_at_start = true; - } else if line == "0" || line == "false" { - loading_at_start = false; - } else { - add_text_to_text_view( - &text_view_errors, - fl!( - "settings_load_invalid_bool_value", - generate_translation_hashmap(vec![("name", config_file.display().to_string()), ("line_number", line_number.to_string()), ("line", line)]) - ) - .as_str(), - ); - } - } - TypeOfLoadedData::SavingAtExit => { - let line = line.to_lowercase(); - if line == "1" || line == "true" { - saving_at_exit = true; - } else if line == "0" || line == "false" { - saving_at_exit = false; - } else { - add_text_to_text_view( - &text_view_errors, - fl!( - "settings_load_invalid_bool_value", - generate_translation_hashmap(vec![("name", config_file.display().to_string()), ("line_number", line_number.to_string()), ("line", line)]) - ) - .as_str(), - ); - } - } - TypeOfLoadedData::ConfirmDeletion => { - let line = line.to_lowercase(); - if line == "1" || line == "true" { - confirm_deletion = true; - } else if line == "0" || line == "false" { - confirm_deletion = false; - } else { - add_text_to_text_view( - &text_view_errors, - fl!( - "settings_load_invalid_bool_value", - generate_translation_hashmap(vec![("name", config_file.display().to_string()), ("line_number", line_number.to_string()), ("line", line)]) - ) - .as_str(), - ); - } - } - TypeOfLoadedData::ConfirmGroupDeletion => { - let line = line.to_lowercase(); - if line == "1" || line == "true" { - confirm_group_deletion = true; - } else if line == "0" || line == "false" { - confirm_group_deletion = false; - } else { - add_text_to_text_view( - &text_view_errors, - fl!( - "settings_load_invalid_bool_value", - generate_translation_hashmap(vec![("name", config_file.display().to_string()), ("line_number", line_number.to_string()), ("line", line)]) - ) - .as_str(), - ); - } - } - TypeOfLoadedData::ShowPreviewSimilarImages => { - let line = line.to_lowercase(); - if line == "1" || line == "true" { - show_previews_similar_images = true; - } else if line == "0" || line == "false" { - show_previews_similar_images = false; - } else { - add_text_to_text_view( - &text_view_errors, - fl!( - "settings_load_invalid_bool_value", - generate_translation_hashmap(vec![("name", config_file.display().to_string()), ("line_number", line_number.to_string()), ("line", line)]) - ) - .as_str(), - ); - } - } - TypeOfLoadedData::ShowPreviewDuplicates => { - let line = line.to_lowercase(); - if line == "1" || line == "true" { - show_previews_duplicates = true; - } else if line == "0" || line == "false" { - show_previews_duplicates = false; - } else { - add_text_to_text_view( - &text_view_errors, - fl!( - "settings_load_invalid_bool_value", - generate_translation_hashmap(vec![("name", config_file.display().to_string()), ("line_number", line_number.to_string()), ("line", line)]) - ) - .as_str(), - ); - } - } - TypeOfLoadedData::BottomTextPanel => { - let line = line.to_lowercase(); - if line == "1" || line == "true" { - bottom_text_panel = true; - } else if line == "0" || line == "false" { - bottom_text_panel = false; - } else { - add_text_to_text_view( - &text_view_errors, - fl!( - "settings_load_invalid_bool_value", - generate_translation_hashmap(vec![("name", config_file.display().to_string()), ("line_number", line_number.to_string()), ("line", line)]) - ) - .as_str(), - ); - } - } - TypeOfLoadedData::HideHardLinks => { - let line = line.to_lowercase(); - if line == "1" || line == "true" { - hide_hard_links = true; - } else if line == "0" || line == "false" { - hide_hard_links = false; - } else { - add_text_to_text_view( - &text_view_errors, - fl!( - "settings_load_invalid_bool_value", - generate_translation_hashmap(vec![("name", config_file.display().to_string()), ("line_number", line_number.to_string()), ("line", line)]) - ) - .as_str(), - ); - } - } - TypeOfLoadedData::UseCache => { - let line = line.to_lowercase(); - if line == "1" || line == "true" { - use_cache = true; - } else if line == "0" || line == "false" { - use_cache = false; - } else { - add_text_to_text_view( - &text_view_errors, - fl!( - "settings_load_invalid_bool_value", - generate_translation_hashmap(vec![("name", config_file.display().to_string()), ("line_number", line_number.to_string()), ("line", line)]) - ) - .as_str(), - ); - } - } - TypeOfLoadedData::UseTrash => { - let line = line.to_lowercase(); - if line == "1" || line == "true" { - use_trash = true; - } else if line == "0" || line == "false" { - use_trash = false; - } else { - add_text_to_text_view( - &text_view_errors, - fl!( - "settings_load_invalid_bool_value", - generate_translation_hashmap(vec![("name", config_file.display().to_string()), ("line_number", line_number.to_string()), ("line", line)]) - ) - .as_str(), - ); - } - } - TypeOfLoadedData::CacheMinimalSize => { - if let Ok(number) = line.parse::() { - cache_minimal_size = number; - } else { - add_text_to_text_view( - &text_view_errors, - format!( - "Found invalid data in line {} \"{}\" isn't proper value(u64) when loading file {:?}", - line_number, line, config_file - ) - .as_str(), - ); - } - } - TypeOfLoadedData::DeleteCacheDuplicates => { - let line = line.to_lowercase(); - if line == "1" || line == "true" { - delete_outdated_cache_dupliactes = true; - } else if line == "0" || line == "false" { - delete_outdated_cache_dupliactes = false; - } else { - add_text_to_text_view( - &text_view_errors, - fl!( - "settings_load_invalid_bool_value", - generate_translation_hashmap(vec![("name", config_file.display().to_string()), ("line_number", line_number.to_string()), ("line", line)]) - ) - .as_str(), - ); - } - } - TypeOfLoadedData::DeleteCacheSimilarImages => { - let line = line.to_lowercase(); - if line == "1" || line == "true" { - delete_outdated_cache_similar_images = true; - } else if line == "0" || line == "false" { - delete_outdated_cache_similar_images = false; - } else { - add_text_to_text_view( - &text_view_errors, - fl!( - "settings_load_invalid_bool_value", - generate_translation_hashmap(vec![("name", config_file.display().to_string()), ("line_number", line_number.to_string()), ("line", line)]) - ) - .as_str(), - ); - } - } - TypeOfLoadedData::DeleteCacheSimilarVideos => { - let line = line.to_lowercase(); - if line == "1" || line == "true" { - delete_outdated_cache_similar_videos = true; - } else if line == "0" || line == "false" { - delete_outdated_cache_similar_videos = false; - } else { - add_text_to_text_view( - &text_view_errors, - fl!( - "settings_load_invalid_bool_value", - generate_translation_hashmap(vec![("name", config_file.display().to_string()), ("line_number", line_number.to_string()), ("line", line)]) - ) - .as_str(), - ); - } - } - TypeOfLoadedData::UsePrehashCache => { - let line = line.to_lowercase(); - if line == "1" || line == "true" { - use_prehash_cache = true; - } else if line == "0" || line == "false" { - use_prehash_cache = false; - } else { - add_text_to_text_view( - &text_view_errors, - fl!( - "settings_load_invalid_bool_value", - generate_translation_hashmap(vec![("name", config_file.display().to_string()), ("line_number", line_number.to_string()), ("line", line)]) - ) - .as_str(), - ); - } - } - TypeOfLoadedData::CachePrehashMinimalSize => { - if let Ok(number) = line.parse::() { - cache_prehash_minimal_size = number; - } else { - add_text_to_text_view( - &text_view_errors, - format!( - "Found invalid data in line {} \"{}\" isn't proper value(u64) when loading file {:?}", - line_number, line, config_file - ) - .as_str(), - ); - } - } - TypeOfLoadedData::Language => { - if LANGUAGES_ALL.iter().any(|e| e.short_text == line) { - short_language = line; - } else { - add_text_to_text_view( - &text_view_errors, - format!( - "Found invalid data in line {} \"{}\" isn't proper language value when loading file {:?}", - line_number, line, config_file - ) - .as_str(), - ); - } - } - } - } - } - - // Setting data - if manual_execution || loading_at_start { - //// Included Directories + // Include Directories let tree_view_included_directories = upper_notebook.tree_view_included_directories.clone(); let list_store = get_list_store(&tree_view_included_directories); list_store.clear(); @@ -713,63 +519,53 @@ pub fn load_configuration(manual_execution: bool, upper_notebook: &GuiUpperNoteb let values: [(u32, &dyn ToValue); 1] = [(ColumnsExcludedDirectory::Path as u32, &directory)]; list_store.set(&list_store.append(), &values); } - - //// Excluded Items - let entry_excluded_items = upper_notebook.entry_excluded_items.clone(); - entry_excluded_items.set_text(excluded_items.iter().map(|e| e.to_string() + ",").collect::().as_str()); - - //// Allowed extensions - let entry_allowed_extensions = upper_notebook.entry_allowed_extensions.clone(); - entry_allowed_extensions.set_text(allowed_extensions.iter().map(|e| e.to_string() + ",").collect::().as_str()); - - //// ComboText - { - for (index, lang) in LANGUAGES_ALL.iter().enumerate() { - if short_language == lang.short_text { - settings.combo_box_settings_language.set_active(Some(index as u32)); - } + } + //// ComboText + { + for (index, lang) in LANGUAGES_ALL.iter().enumerate() { + if short_language == lang.short_text { + settings.combo_box_settings_language.set_active(Some(index as u32)); } } + } - //// Buttons - settings.check_button_settings_load_at_start.set_active(loading_at_start); - settings.check_button_settings_save_at_exit.set_active(saving_at_exit); - settings.check_button_settings_confirm_deletion.set_active(confirm_deletion); - settings.check_button_settings_confirm_group_deletion.set_active(confirm_group_deletion); - settings.check_button_settings_show_preview_similar_images.set_active(show_previews_similar_images); - settings.check_button_settings_show_preview_duplicates.set_active(show_previews_duplicates); + upper_notebook.entry_excluded_items.set_text(&excluded_items); + upper_notebook.entry_allowed_extensions.set_text(&allowed_extensions); - settings - .check_button_settings_similar_videos_delete_outdated_cache - .set_active(delete_outdated_cache_similar_videos); - settings - .check_button_settings_similar_images_delete_outdated_cache - .set_active(delete_outdated_cache_similar_images); - settings.check_button_settings_duplicates_delete_outdated_cache.set_active(delete_outdated_cache_dupliactes); + //// Buttons + settings.check_button_settings_load_at_start.set_active(loading_at_start); + settings.check_button_settings_save_at_exit.set_active(saving_at_exit); + settings.check_button_settings_confirm_deletion.set_active(confirm_deletion); + settings.check_button_settings_confirm_group_deletion.set_active(confirm_group_deletion); + settings.check_button_settings_show_preview_similar_images.set_active(show_previews_similar_images); + settings.check_button_settings_show_preview_duplicates.set_active(show_previews_duplicates); - settings.check_button_settings_show_text_view.set_active(bottom_text_panel); - if !bottom_text_panel { - scrolled_window_errors.hide(); - } else { - scrolled_window_errors.show(); - } - settings.check_button_settings_hide_hard_links.set_active(hide_hard_links); - settings.check_button_settings_use_cache.set_active(use_cache); - settings.check_button_duplicates_use_prehash_cache.set_active(use_prehash_cache); - settings.check_button_settings_use_trash.set_active(use_trash); - settings.entry_settings_cache_file_minimal_size.set_text(cache_minimal_size.to_string().as_str()); - settings - .entry_settings_prehash_cache_file_minimal_size - .set_text(cache_prehash_minimal_size.to_string().as_str()); + settings + .check_button_settings_similar_videos_delete_outdated_cache + .set_active(delete_outdated_cache_similar_videos); + settings + .check_button_settings_similar_images_delete_outdated_cache + .set_active(delete_outdated_cache_similar_images); + settings.check_button_settings_duplicates_delete_outdated_cache.set_active(delete_outdated_cache_duplicates); + + settings.check_button_settings_show_text_view.set_active(bottom_text_panel); + if !bottom_text_panel { + scrolled_window_errors.hide(); } else { - settings.check_button_settings_load_at_start.set_active(false); - } - - if manual_execution { - add_text_to_text_view(&text_view_errors, format!("{} {:?}", &fl!("saving_loading_reset_configuration"), config_file).as_str()); + scrolled_window_errors.show(); } + settings.check_button_settings_hide_hard_links.set_active(hide_hard_links); + settings.check_button_settings_use_cache.set_active(use_cache); + settings.check_button_duplicates_use_prehash_cache.set_active(use_prehash_cache); + settings.check_button_settings_use_trash.set_active(use_trash); + settings.entry_settings_cache_file_minimal_size.set_text(&cache_minimal_size); + settings.entry_settings_prehash_cache_file_minimal_size.set_text(&cache_prehash_minimal_size); } else { - add_text_to_text_view(&text_view_errors, "Failed to get home directory, so can't load file."); + settings.check_button_settings_load_at_start.set_active(false); + } + + if manual_execution { + add_text_to_text_view(&text_view_errors, &fl!("saving_loading_reset_configuration")); } } @@ -836,22 +632,28 @@ pub fn reset_configuration(manual_clearing: bool, upper_notebook: &GuiUpperNoteb // Set default settings { - settings.check_button_settings_save_at_exit.set_active(true); - settings.check_button_settings_load_at_start.set_active(true); - settings.check_button_settings_confirm_deletion.set_active(true); - settings.check_button_settings_confirm_group_deletion.set_active(true); - settings.check_button_settings_show_preview_similar_images.set_active(true); - settings.check_button_settings_show_preview_duplicates.set_active(true); - settings.check_button_settings_show_text_view.set_active(true); - settings.check_button_settings_hide_hard_links.set_active(true); - settings.check_button_settings_use_cache.set_active(true); - settings.check_button_settings_use_trash.set_active(false); - settings.entry_settings_cache_file_minimal_size.set_text("257144"); - settings.check_button_settings_similar_videos_delete_outdated_cache.set_active(false); - settings.check_button_settings_similar_images_delete_outdated_cache.set_active(true); - settings.check_button_settings_duplicates_delete_outdated_cache.set_active(true); - settings.check_button_duplicates_use_prehash_cache.set_active(false); - settings.entry_settings_prehash_cache_file_minimal_size.set_text("0"); + settings.check_button_settings_save_at_exit.set_active(DEFAULT_SAVE_ON_EXIT); + settings.check_button_settings_load_at_start.set_active(DEFAULT_LOAD_AT_START); + settings.check_button_settings_confirm_deletion.set_active(DEFAULT_CONFIRM_DELETION); + settings.check_button_settings_confirm_group_deletion.set_active(DEFAULT_CONFIRM_GROUP_DELETION); + settings.check_button_settings_show_preview_similar_images.set_active(DEFAULT_SHOW_IMAGE_PREVIEW); + settings.check_button_settings_show_preview_duplicates.set_active(DEFAULT_SHOW_DUPLICATE_IMAGE_PREVIEW); + settings.check_button_settings_show_text_view.set_active(DEFAULT_BOTTOM_TEXT_VIEW); + settings.check_button_settings_hide_hard_links.set_active(DEFAULT_HIDE_HARD_LINKS); + settings.check_button_settings_use_cache.set_active(DEFAULT_USE_CACHE); + settings.check_button_settings_use_trash.set_active(DEFAULT_USE_TRASH); + settings.entry_settings_cache_file_minimal_size.set_text(DEFAULT_MINIMAL_CACHE_SIZE); + settings + .check_button_settings_similar_videos_delete_outdated_cache + .set_active(DEFAULT_VIDEO_REMOVE_AUTO_OUTDATED_CACHE); + settings + .check_button_settings_similar_images_delete_outdated_cache + .set_active(DEFAULT_IMAGE_REMOVE_AUTO_OUTDATED_CACHE); + settings + .check_button_settings_duplicates_delete_outdated_cache + .set_active(DEFAULT_DUPLICATE_REMOVE_AUTO_OUTDATED_CACHE); + settings.check_button_duplicates_use_prehash_cache.set_active(DEFAULT_USE_PRECACHE); + settings.entry_settings_prehash_cache_file_minimal_size.set_text(DEFAULT_PREHASH_MINIMAL_CACHE_SIZE); settings.combo_box_settings_language.set_active(Some(0)); } if manual_clearing {