Skip to content

Commit

Permalink
Simplify some GUI code (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
qarmin authored Nov 4, 2020
1 parent 1fb66b3 commit 5d4f4db
Show file tree
Hide file tree
Showing 17 changed files with 443 additions and 555 deletions.
8 changes: 4 additions & 4 deletions czkawka_core/src/big_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ impl BigFile {
}
}

pub fn find_big_files(&mut self, rx: Option<&Receiver<()>>) {
pub fn find_big_files(&mut self, stop_receiver: Option<&Receiver<()>>) {
self.optimize_directories();
if !self.look_for_big_files(rx) {
if !self.look_for_big_files(stop_receiver) {
self.stopped_search = true;
return;
}
Expand Down Expand Up @@ -111,7 +111,7 @@ impl BigFile {
self.allowed_extensions.set_allowed_extensions(allowed_extensions, &mut self.text_messages);
}

fn look_for_big_files(&mut self, rx: Option<&Receiver<()>>) -> bool {
fn look_for_big_files(&mut self, stop_receiver: Option<&Receiver<()>>) -> bool {
let start_time: SystemTime = SystemTime::now();
let mut folders_to_check: Vec<PathBuf> = Vec::with_capacity(1024 * 2); // This should be small enough too not see to big difference and big enough to store most of paths without needing to resize vector

Expand All @@ -122,7 +122,7 @@ impl BigFile {
self.information.number_of_checked_folders += folders_to_check.len();

while !folders_to_check.is_empty() {
if rx.is_some() && rx.unwrap().try_recv().is_ok() {
if stop_receiver.is_some() && stop_receiver.unwrap().try_recv().is_ok() {
return false;
}
let current_folder = folders_to_check.pop().unwrap();
Expand Down
24 changes: 12 additions & 12 deletions czkawka_core/src/duplicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,28 +114,28 @@ impl DuplicateFinder {
}
}

pub fn find_duplicates(&mut self, rx: Option<&Receiver<()>>) {
pub fn find_duplicates(&mut self, stop_receiver: Option<&Receiver<()>>) {
self.directories.optimize_directories(self.recursive_search, &mut self.text_messages);

match self.check_method {
CheckingMethod::Name => {
if !self.check_files_name(rx) {
if !self.check_files_name(stop_receiver) {
self.stopped_search = true;
return;
}
}
CheckingMethod::Size => {
if !self.check_files_size(rx) {
if !self.check_files_size(stop_receiver) {
self.stopped_search = true;
return;
}
}
CheckingMethod::HashMB | CheckingMethod::Hash => {
if !self.check_files_size(rx) {
if !self.check_files_size(stop_receiver) {
self.stopped_search = true;
return;
}
if !self.check_files_hash(rx) {
if !self.check_files_hash(stop_receiver) {
self.stopped_search = true;
return;
}
Expand Down Expand Up @@ -210,7 +210,7 @@ impl DuplicateFinder {
self.excluded_items.set_excluded_items(excluded_items, &mut self.text_messages);
}

fn check_files_name(&mut self, rx: Option<&Receiver<()>>) -> bool {
fn check_files_name(&mut self, stop_receiver: Option<&Receiver<()>>) -> bool {
// TODO maybe add multithreading checking files
let start_time: SystemTime = SystemTime::now();
let mut folders_to_check: Vec<PathBuf> = Vec::with_capacity(1024 * 2); // This should be small enough too not see to big difference and big enough to store most of paths without needing to resize vector
Expand All @@ -222,7 +222,7 @@ impl DuplicateFinder {
self.information.number_of_checked_folders += folders_to_check.len();

while !folders_to_check.is_empty() {
if rx.is_some() && rx.unwrap().try_recv().is_ok() {
if stop_receiver.is_some() && stop_receiver.unwrap().try_recv().is_ok() {
return false;
}
let current_folder = folders_to_check.pop().unwrap();
Expand Down Expand Up @@ -348,7 +348,7 @@ impl DuplicateFinder {

/// Read file length and puts it to different boxes(each for different lengths)
/// If in box is only 1 result, then it is removed
fn check_files_size(&mut self, rx: Option<&Receiver<()>>) -> bool {
fn check_files_size(&mut self, stop_receiver: Option<&Receiver<()>>) -> bool {
// TODO maybe add multithreading checking files
let start_time: SystemTime = SystemTime::now();
let mut folders_to_check: Vec<PathBuf> = Vec::with_capacity(1024 * 2); // This should be small enough too not see to big difference and big enough to store most of paths without needing to resize vector
Expand All @@ -360,7 +360,7 @@ impl DuplicateFinder {
self.information.number_of_checked_folders += folders_to_check.len();

while !folders_to_check.is_empty() {
if rx.is_some() && rx.unwrap().try_recv().is_ok() {
if stop_receiver.is_some() && stop_receiver.unwrap().try_recv().is_ok() {
return false;
}
let current_folder = folders_to_check.pop().unwrap();
Expand Down Expand Up @@ -486,7 +486,7 @@ impl DuplicateFinder {
}

/// The slowest checking type, which must be applied after checking for size
fn check_files_hash(&mut self, rx: Option<&Receiver<()>>) -> bool {
fn check_files_hash(&mut self, stop_receiver: Option<&Receiver<()>>) -> bool {
if self.hash_type != HashType::Blake3 {
panic!(); // TODO Add more hash types
}
Expand All @@ -501,7 +501,7 @@ impl DuplicateFinder {
hashmap_with_hash = Default::default();

for file_entry in vector {
if rx.is_some() && rx.unwrap().try_recv().is_ok() {
if stop_receiver.is_some() && stop_receiver.unwrap().try_recv().is_ok() {
return false;
}
file_handler = match File::open(&file_entry.path) {
Expand Down Expand Up @@ -547,7 +547,7 @@ impl DuplicateFinder {
hashmap_with_hash = Default::default();

for file_entry in vector {
if rx.is_some() && rx.unwrap().try_recv().is_ok() {
if stop_receiver.is_some() && stop_receiver.unwrap().try_recv().is_ok() {
return false;
}
file_handler = match File::open(&file_entry.path) {
Expand Down
8 changes: 4 additions & 4 deletions czkawka_core/src/empty_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ impl EmptyFiles {
}

/// Finding empty files, save results to internal struct variables
pub fn find_empty_files(&mut self, rx: Option<&Receiver<()>>) {
pub fn find_empty_files(&mut self, stop_receiver: Option<&Receiver<()>>) {
self.directories.optimize_directories(self.recursive_search, &mut self.text_messages);
if !self.check_files(rx) {
if !self.check_files(stop_receiver) {
self.stopped_search = true;
return;
}
Expand Down Expand Up @@ -120,7 +120,7 @@ impl EmptyFiles {
}

/// Check files for any with size == 0
fn check_files(&mut self, rx: Option<&Receiver<()>>) -> bool {
fn check_files(&mut self, stop_receiver: Option<&Receiver<()>>) -> bool {
let start_time: SystemTime = SystemTime::now();
let mut folders_to_check: Vec<PathBuf> = Vec::with_capacity(1024 * 2); // This should be small enough too not see to big difference and big enough to store most of paths without needing to resize vector

Expand All @@ -131,7 +131,7 @@ impl EmptyFiles {
self.information.number_of_checked_folders += folders_to_check.len();

while !folders_to_check.is_empty() {
if rx.is_some() && rx.unwrap().try_recv().is_ok() {
if stop_receiver.is_some() && stop_receiver.unwrap().try_recv().is_ok() {
return false;
}
let current_folder = folders_to_check.pop().unwrap();
Expand Down
8 changes: 4 additions & 4 deletions czkawka_core/src/empty_folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ impl EmptyFolder {
self.directories.set_excluded_directory(excluded_directory, &mut self.text_messages);
}
/// Public function used by CLI to search for empty folders
pub fn find_empty_folders(&mut self, rx: Option<&Receiver<()>>) {
pub fn find_empty_folders(&mut self, stop_receiver: Option<&Receiver<()>>) {
self.directories.optimize_directories(true, &mut self.text_messages);
if !self.check_for_empty_folders(rx) {
if !self.check_for_empty_folders(stop_receiver) {
self.stopped_search = true;
return;
}
Expand Down Expand Up @@ -128,7 +128,7 @@ impl EmptyFolder {

/// Function to check if folder are empty.
/// Parameter initial_checking for second check before deleting to be sure that checked folder is still empty
fn check_for_empty_folders(&mut self, rx: Option<&Receiver<()>>) -> bool {
fn check_for_empty_folders(&mut self, stop_receiver: Option<&Receiver<()>>) -> bool {
let start_time: SystemTime = SystemTime::now();
let mut folders_to_check: Vec<PathBuf> = Vec::with_capacity(1024 * 2); // This should be small enough too not see to big difference and big enough to store most of paths without needing to resize vector
let mut folders_checked: BTreeMap<PathBuf, FolderEntry> = Default::default();
Expand All @@ -147,7 +147,7 @@ impl EmptyFolder {
}

while !folders_to_check.is_empty() {
if rx.is_some() && rx.unwrap().try_recv().is_ok() {
if stop_receiver.is_some() && stop_receiver.unwrap().try_recv().is_ok() {
return false;
}
self.information.number_of_checked_folders += 1;
Expand Down
22 changes: 11 additions & 11 deletions czkawka_core/src/same_music.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ impl SameMusic {
}
}

pub fn find_same_music(&mut self, rx: Option<&Receiver<()>>) {
pub fn find_same_music(&mut self, stop_receiver: Option<&Receiver<()>>) {
self.directories.optimize_directories(self.recursive_search, &mut self.text_messages);
if !self.check_files(rx) {
if !self.check_files(stop_receiver) {
self.stopped_search = true;
return;
}
if !self.check_for_duplicates(rx) {
if !self.check_for_duplicates(stop_receiver) {
self.stopped_search = true;
return;
}
Expand Down Expand Up @@ -159,7 +159,7 @@ impl SameMusic {
}

/// Check files for any with size == 0
fn check_files(&mut self, rx: Option<&Receiver<()>>) -> bool {
fn check_files(&mut self, stop_receiver: Option<&Receiver<()>>) -> bool {
let start_time: SystemTime = SystemTime::now();
let mut folders_to_check: Vec<PathBuf> = Vec::with_capacity(1024 * 2); // This should be small enough too not see to big difference and big enough to store most of paths without needing to resize vector

Expand All @@ -170,7 +170,7 @@ impl SameMusic {
self.information.number_of_checked_folders += folders_to_check.len();

while !folders_to_check.is_empty() {
if rx.is_some() && rx.unwrap().try_recv().is_ok() {
if stop_receiver.is_some() && stop_receiver.unwrap().try_recv().is_ok() {
return false;
}
let current_folder = folders_to_check.pop().unwrap();
Expand Down Expand Up @@ -289,7 +289,7 @@ impl SameMusic {
true
}

fn check_for_duplicates(&mut self, rx: Option<&Receiver<()>>) -> bool {
fn check_for_duplicates(&mut self, stop_receiver: Option<&Receiver<()>>) -> bool {
if MusicSimilarity::NONE == self.music_similarity {
panic!("This can't be none");
}
Expand All @@ -300,7 +300,7 @@ impl SameMusic {

if (self.music_similarity & MusicSimilarity::TITLE) == MusicSimilarity::TITLE {
for vec_file_entry in old_duplicates {
if rx.is_some() && rx.unwrap().try_recv().is_ok() {
if stop_receiver.is_some() && stop_receiver.unwrap().try_recv().is_ok() {
return false;
}
let mut hash_map: HashMap<String, Vec<FileEntry>> = Default::default();
Expand All @@ -323,7 +323,7 @@ impl SameMusic {

if (self.music_similarity & MusicSimilarity::ARTIST) == MusicSimilarity::ARTIST {
for vec_file_entry in old_duplicates {
if rx.is_some() && rx.unwrap().try_recv().is_ok() {
if stop_receiver.is_some() && stop_receiver.unwrap().try_recv().is_ok() {
return false;
}
let mut hash_map: HashMap<String, Vec<FileEntry>> = Default::default();
Expand All @@ -346,7 +346,7 @@ impl SameMusic {

if (self.music_similarity & MusicSimilarity::ALBUM_TITLE) == MusicSimilarity::ALBUM_TITLE {
for vec_file_entry in old_duplicates {
if rx.is_some() && rx.unwrap().try_recv().is_ok() {
if stop_receiver.is_some() && stop_receiver.unwrap().try_recv().is_ok() {
return false;
}
let mut hash_map: HashMap<String, Vec<FileEntry>> = Default::default();
Expand All @@ -369,7 +369,7 @@ impl SameMusic {

if (self.music_similarity & MusicSimilarity::ALBUM_ARTIST) == MusicSimilarity::ALBUM_ARTIST {
for vec_file_entry in old_duplicates {
if rx.is_some() && rx.unwrap().try_recv().is_ok() {
if stop_receiver.is_some() && stop_receiver.unwrap().try_recv().is_ok() {
return false;
}
let mut hash_map: HashMap<String, Vec<FileEntry>> = Default::default();
Expand All @@ -392,7 +392,7 @@ impl SameMusic {

if (self.music_similarity & MusicSimilarity::YEAR) == MusicSimilarity::YEAR {
for vec_file_entry in old_duplicates {
if rx.is_some() && rx.unwrap().try_recv().is_ok() {
if stop_receiver.is_some() && stop_receiver.unwrap().try_recv().is_ok() {
return false;
}
let mut hash_map: HashMap<i32, Vec<FileEntry>> = Default::default();
Expand Down
10 changes: 5 additions & 5 deletions czkawka_core/src/similar_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ impl SimilarImages {
}

/// Public function used by CLI to search for empty folders
pub fn find_similar_images(&mut self, rx: Option<&Receiver<()>>) {
pub fn find_similar_images(&mut self, stop_receiver: Option<&Receiver<()>>) {
self.directories.optimize_directories(true, &mut self.text_messages);
if !self.check_for_similar_images(rx) {
if !self.check_for_similar_images(stop_receiver) {
self.stopped_search = true;
return;
}
Expand All @@ -147,7 +147,7 @@ impl SimilarImages {

/// Function to check if folder are empty.
/// Parameter initial_checking for second check before deleting to be sure that checked folder is still empty
fn check_for_similar_images(&mut self, rx: Option<&Receiver<()>>) -> bool {
fn check_for_similar_images(&mut self, stop_receiver: Option<&Receiver<()>>) -> bool {
let start_time: SystemTime = SystemTime::now();
let mut folders_to_check: Vec<PathBuf> = Vec::with_capacity(1024 * 2); // This should be small enough too not see to big difference and big enough to store most of paths without needing to resize vector

Expand All @@ -158,7 +158,7 @@ impl SimilarImages {
self.information.number_of_checked_folders += folders_to_check.len();

while !folders_to_check.is_empty() {
if rx.is_some() && rx.unwrap().try_recv().is_ok() {
if stop_receiver.is_some() && stop_receiver.unwrap().try_recv().is_ok() {
return false;
}
let current_folder = folders_to_check.pop().unwrap();
Expand Down Expand Up @@ -280,7 +280,7 @@ impl SimilarImages {

let mut new_vector: Vec<StructSimilar> = Vec::new();
for (hash, vec_file_entry) in &self.image_hashes {
if rx.is_some() && rx.unwrap().try_recv().is_ok() {
if stop_receiver.is_some() && stop_receiver.unwrap().try_recv().is_ok() {
return false;
}
let vector_with_found_similar_hashes = self.bktree.find(hash, 3).collect::<Vec<_>>();
Expand Down
8 changes: 4 additions & 4 deletions czkawka_core/src/temporary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ impl Temporary {
}

/// Finding temporary files, save results to internal struct variables
pub fn find_temporary_files(&mut self, rx: Option<&Receiver<()>>) {
pub fn find_temporary_files(&mut self, stop_receiver: Option<&Receiver<()>>) {
self.directories.optimize_directories(self.recursive_search, &mut self.text_messages);
if !self.check_files(rx) {
if !self.check_files(stop_receiver) {
self.stopped_search = true;
return;
}
Expand Down Expand Up @@ -111,7 +111,7 @@ impl Temporary {
self.excluded_items.set_excluded_items(excluded_items, &mut self.text_messages);
}

fn check_files(&mut self, rx: Option<&Receiver<()>>) -> bool {
fn check_files(&mut self, stop_receiver: Option<&Receiver<()>>) -> bool {
let start_time: SystemTime = SystemTime::now();
let mut folders_to_check: Vec<PathBuf> = Vec::with_capacity(1024 * 2); // This should be small enough too not see to big difference and big enough to store most of paths without needing to resize vector

Expand All @@ -122,7 +122,7 @@ impl Temporary {
self.information.number_of_checked_folders += folders_to_check.len();

while !folders_to_check.is_empty() {
if rx.is_some() && rx.unwrap().try_recv().is_ok() {
if stop_receiver.is_some() && stop_receiver.unwrap().try_recv().is_ok() {
return false;
}

Expand Down
8 changes: 4 additions & 4 deletions czkawka_core/src/zeroed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ impl ZeroedFiles {
}
}

pub fn find_zeroed_files(&mut self, rx: Option<&Receiver<()>>) {
pub fn find_zeroed_files(&mut self, stop_receiver: Option<&Receiver<()>>) {
self.directories.optimize_directories(self.recursive_search, &mut self.text_messages);
if !self.check_files(rx) {
if !self.check_files(stop_receiver) {
self.stopped_search = true;
return;
}
Expand Down Expand Up @@ -129,7 +129,7 @@ impl ZeroedFiles {
}

/// Check files for files which have 0
fn check_files(&mut self, rx: Option<&Receiver<()>>) -> bool {
fn check_files(&mut self, stop_receiver: Option<&Receiver<()>>) -> bool {
let start_time: SystemTime = SystemTime::now();
let mut folders_to_check: Vec<PathBuf> = Vec::with_capacity(1024 * 2); // This should be small enough too not see to big difference and big enough to store most of paths without needing to resize vector

Expand All @@ -140,7 +140,7 @@ impl ZeroedFiles {
self.information.number_of_checked_folders += folders_to_check.len();

while !folders_to_check.is_empty() {
if rx.is_some() && rx.unwrap().try_recv().is_ok() {
if stop_receiver.is_some() && stop_receiver.unwrap().try_recv().is_ok() {
return false;
}
let current_folder = folders_to_check.pop().unwrap();
Expand Down
Loading

0 comments on commit 5d4f4db

Please sign in to comment.