From 083db7aa516ae37f7a0b597b9fe38b5243edcb03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Mikrut?= <41945903+qarmin@users.noreply.github.com> Date: Wed, 30 Dec 2020 13:41:18 +0100 Subject: [PATCH] Add manual adding of directories (#165) --- czkawka_core/src/common.rs | 9 ++++ czkawka_core/src/common_directory.rs | 15 ++++++ czkawka_gui/czkawka.glade | 51 +++++++++++++++++++ .../src/connect_selection_of_directories.rs | 42 +++++++++++++++ czkawka_gui/src/gui_data.rs | 4 ++ 5 files changed, 121 insertions(+) diff --git a/czkawka_core/src/common.rs b/czkawka_core/src/common.rs index 06619b1..6fcf423 100644 --- a/czkawka_core/src/common.rs +++ b/czkawka_core/src/common.rs @@ -109,6 +109,12 @@ impl Common { pub fn normalize_windows_path(path_to_change: impl AsRef) -> PathBuf { let path = path_to_change.as_ref(); + + // Don't do anything, because network path may be case intensive + if path.to_string_lossy().starts_with('\\') { + return path.to_path_buf(); + } + match path.to_str() { Some(path) if path.is_char_boundary(1) => { let replaced = path.replace("/", "\\"); @@ -157,5 +163,8 @@ mod test { assert_eq!(PathBuf::from("C:\\path.txt"), Common::normalize_windows_path("c:/PATH.tXt")); assert_eq!(PathBuf::from("H:\\reka\\weza\\roman.txt"), Common::normalize_windows_path("h:/RekA/Weza\\roMan.Txt")); assert_eq!(PathBuf::from("T:\\a"), Common::normalize_windows_path("T:\\A")); + assert_eq!(PathBuf::from("\\\\aBBa"), Common::normalize_windows_path("\\\\aBBa")); + assert_eq!(PathBuf::from("a"), Common::normalize_windows_path("a")); + assert_eq!(PathBuf::from(""), Common::normalize_windows_path("")); } } diff --git a/czkawka_core/src/common_directory.rs b/czkawka_core/src/common_directory.rs index 7437d08..af040d0 100644 --- a/czkawka_core/src/common_directory.rs +++ b/czkawka_core/src/common_directory.rs @@ -31,10 +31,18 @@ impl Directories { text_messages.warnings.push(format!("Included Directory Warning: Wildcards in path are not supported, ignoring {}", directory.display())); continue; } + + #[cfg(not(target_family = "windows"))] if directory.is_relative() { text_messages.warnings.push(format!("Included Directory Warning: Relative path are not supported, ignoring {}", directory.display())); continue; } + #[cfg(target_family = "windows")] + if directory.is_relative() && !directory.starts_with("\\") { + text_messages.warnings.push(format!("Included Directory Warning: Relative path are not supported, ignoring {}", directory.display())); + continue; + } + if !directory.exists() { text_messages.warnings.push(format!("Included Directory Warning: Provided folder path must exits, ignoring {}", directory.display())); continue; @@ -79,10 +87,17 @@ impl Directories { text_messages.warnings.push(format!("Excluded Directory Warning: Wildcards in path are not supported, ignoring {}", directory.display())); continue; } + #[cfg(not(target_family = "windows"))] if directory.is_relative() { text_messages.warnings.push(format!("Excluded Directory Warning: Relative path are not supported, ignoring {}", directory.display())); continue; } + #[cfg(target_family = "windows")] + if directory.is_relative() && !directory.starts_with("\\") { + text_messages.warnings.push(format!("Excluded Directory Warning: Relative path are not supported, ignoring {}", directory.display())); + continue; + } + if !directory.exists() { // text_messages.warnings.push(format!("Excluded Directory Warning: Provided folder path must exits, ignoring {}", directory.display())); continue; diff --git a/czkawka_gui/czkawka.glade b/czkawka_gui/czkawka.glade index 2e9bacd..d1cc347 100644 --- a/czkawka_gui/czkawka.glade +++ b/czkawka_gui/czkawka.glade @@ -548,6 +548,57 @@ Author: RafaƂ Mikrut 1 + + + True + True + True + + + True + False + 0 + 0 + + + True + False + 4 + + + True + False + insert-link + + + False + True + 0 + + + + + True + False + Manual Add + + + False + True + 1 + + + + + + + + + False + False + 2 + + False diff --git a/czkawka_gui/src/connect_selection_of_directories.rs b/czkawka_gui/src/connect_selection_of_directories.rs index d6099de..16b89dc 100644 --- a/czkawka_gui/src/connect_selection_of_directories.rs +++ b/czkawka_gui/src/connect_selection_of_directories.rs @@ -3,7 +3,49 @@ use crate::gui_data::GuiData; use crate::help_functions::{get_list_store, get_tree_view}; use gtk::prelude::*; +#[cfg(target_family = "windows")] +use czkawka_core::common::Common; + pub fn connect_selection_of_directories(gui_data: &GuiData) { + // Add manually directory + { + let scrolled_window_included_directories = gui_data.scrolled_window_included_directories.clone(); + let window_main = gui_data.window_main.clone(); + let buttons_manual_add_directory = gui_data.buttons_manual_add_directory.clone(); + buttons_manual_add_directory.connect_clicked(move |_| { + let dialog_manual_add_directory = gtk::Dialog::with_buttons(Some("Add directory manually"), Some(&window_main), gtk::DialogFlags::MODAL, &[("Ok", gtk::ResponseType::Ok), ("Close", gtk::ResponseType::Cancel)]); + let entry: gtk::Entry = gtk::Entry::new(); + + for widgets in dialog_manual_add_directory.get_children() { + // By default GtkBox is child of dialog, so we can easily add other things to it + widgets.clone().downcast::().unwrap().add(&entry); + } + + dialog_manual_add_directory.show_all(); + + let response_type = dialog_manual_add_directory.run(); + if response_type == gtk::ResponseType::Ok { + let text = entry.get_text().to_string().trim().to_string(); + + #[cfg(target_family = "windows")] + let text = Common::normalize_windows_path(text).to_string_lossy().to_string(); + + if !text.is_empty() { + let tree_view = scrolled_window_included_directories.get_children().get(0).unwrap().clone().downcast::().unwrap(); + let list_store = tree_view.get_model().unwrap().downcast::().unwrap(); + + let col_indices = [0]; + + let values: [&dyn ToValue; 1] = [&text]; + list_store.set(&list_store.append(), &col_indices, &values); + } + } else { + dialog_manual_add_directory.close(); + return; + } + dialog_manual_add_directory.close(); + }); + } // Add included directory { let scrolled_window_included_directories = gui_data.scrolled_window_included_directories.clone(); diff --git a/czkawka_gui/src/gui_data.rs b/czkawka_gui/src/gui_data.rs index 112ed85..f297dd3 100644 --- a/czkawka_gui/src/gui_data.rs +++ b/czkawka_gui/src/gui_data.rs @@ -63,6 +63,8 @@ pub struct GuiData { pub buttons_show_errors: gtk::Button, pub buttons_names: [String; 5], pub buttons_array: [Button; 5], + + pub buttons_manual_add_directory: gtk::Button, pub buttons_add_included_directory: gtk::Button, pub buttons_remove_included_directory: gtk::Button, pub buttons_add_excluded_directory: gtk::Button, @@ -276,6 +278,7 @@ impl GuiData { let buttons_names = ["search".to_string(), "select".to_string(), "delete".to_string(), "save".to_string(), "symlink".to_string()]; let buttons_array = [buttons_search.clone(), buttons_select.clone(), buttons_delete.clone(), buttons_save.clone(), buttons_symlink.clone()]; + let buttons_manual_add_directory: gtk::Button = builder.get_object("buttons_manual_add_directory").unwrap(); let buttons_add_included_directory: gtk::Button = builder.get_object("buttons_add_included_directory").unwrap(); let buttons_remove_included_directory: gtk::Button = builder.get_object("buttons_remove_included_directory").unwrap(); let buttons_add_excluded_directory: gtk::Button = builder.get_object("buttons_add_excluded_directory").unwrap(); @@ -428,6 +431,7 @@ impl GuiData { buttons_show_errors, buttons_names, buttons_array, + buttons_manual_add_directory, buttons_add_included_directory, buttons_remove_included_directory, buttons_add_excluded_directory,