1 use crate::help_functions::*;
2 use gtk::prelude::*;
3 
4 const KEY_ENTER: u16 = 36;
5 const KEY_SPACE: u16 = 65;
6 
7 // TODO add option to open files and folders from context menu activated by pressing ONCE with right mouse button
8 
opening_double_click_function_duplicates(tree_view: &gtk::TreeView, event: &gdk::EventButton) -> gtk::Inhibit9 pub fn opening_double_click_function_duplicates(tree_view: &gtk::TreeView, event: &gdk::EventButton) -> gtk::Inhibit {
10     if event.event_type() == gdk::EventType::DoubleButtonPress && event.button() == 1 {
11         common_open_function(tree_view, ColumnsDuplicates::Name as i32, ColumnsDuplicates::Path as i32, OpenMode::PathAndName);
12     } else if event.event_type() == gdk::EventType::DoubleButtonPress && event.button() == 3 {
13         common_open_function(tree_view, ColumnsDuplicates::Name as i32, ColumnsDuplicates::Path as i32, OpenMode::OnlyPath);
14     }
15     gtk::Inhibit(false)
16 }
opening_enter_function_duplicates(tree_view: &gtk::TreeView, event: &gdk::EventKey) -> gtk::Inhibit17 pub fn opening_enter_function_duplicates(tree_view: &gtk::TreeView, event: &gdk::EventKey) -> gtk::Inhibit {
18     handle_tree_keypress(tree_view, event, ColumnsDuplicates::Name as u32, ColumnsDuplicates::Path as u32, ColumnsDuplicates::ActiveSelectButton as u32)
19 }
20 
opening_double_click_function_empty_folders(tree_view: &gtk::TreeView, event: &gdk::EventButton) -> gtk::Inhibit21 pub fn opening_double_click_function_empty_folders(tree_view: &gtk::TreeView, event: &gdk::EventButton) -> gtk::Inhibit {
22     if event.event_type() == gdk::EventType::DoubleButtonPress && event.button() == 1 {
23         common_open_function(tree_view, ColumnsEmptyFolders::Name as i32, ColumnsEmptyFolders::Path as i32, OpenMode::PathAndName);
24     } else if event.event_type() == gdk::EventType::DoubleButtonPress && event.button() == 3 {
25         common_open_function(tree_view, ColumnsEmptyFolders::Name as i32, ColumnsEmptyFolders::Path as i32, OpenMode::OnlyPath);
26     }
27     gtk::Inhibit(false)
28 }
opening_enter_function_empty_folders(tree_view: &gtk::TreeView, event: &gdk::EventKey) -> gtk::Inhibit29 pub fn opening_enter_function_empty_folders(tree_view: &gtk::TreeView, event: &gdk::EventKey) -> gtk::Inhibit {
30     handle_tree_keypress(tree_view, event, ColumnsEmptyFolders::Name as u32, ColumnsEmptyFolders::Path as u32, ColumnsEmptyFolders::ActiveSelectButton as u32)
31 }
32 
opening_double_click_function_empty_files(tree_view: &gtk::TreeView, event: &gdk::EventButton) -> gtk::Inhibit33 pub fn opening_double_click_function_empty_files(tree_view: &gtk::TreeView, event: &gdk::EventButton) -> gtk::Inhibit {
34     if event.event_type() == gdk::EventType::DoubleButtonPress && event.button() == 1 {
35         common_open_function(tree_view, ColumnsEmptyFiles::Name as i32, ColumnsEmptyFiles::Path as i32, OpenMode::PathAndName);
36     } else if event.event_type() == gdk::EventType::DoubleButtonPress && event.button() == 3 {
37         common_open_function(tree_view, ColumnsEmptyFiles::Name as i32, ColumnsEmptyFiles::Path as i32, OpenMode::OnlyPath);
38     }
39     gtk::Inhibit(false)
40 }
opening_enter_function_empty_files(tree_view: &gtk::TreeView, event: &gdk::EventKey) -> gtk::Inhibit41 pub fn opening_enter_function_empty_files(tree_view: &gtk::TreeView, event: &gdk::EventKey) -> gtk::Inhibit {
42     handle_tree_keypress(tree_view, event, ColumnsEmptyFiles::Name as u32, ColumnsEmptyFiles::Path as u32, ColumnsEmptyFiles::ActiveSelectButton as u32)
43 }
44 
opening_double_click_function_temporary_files(tree_view: &gtk::TreeView, event: &gdk::EventButton) -> gtk::Inhibit45 pub fn opening_double_click_function_temporary_files(tree_view: &gtk::TreeView, event: &gdk::EventButton) -> gtk::Inhibit {
46     if event.event_type() == gdk::EventType::DoubleButtonPress && event.button() == 1 {
47         common_open_function(tree_view, ColumnsTemporaryFiles::Name as i32, ColumnsTemporaryFiles::Path as i32, OpenMode::PathAndName);
48     } else if event.event_type() == gdk::EventType::DoubleButtonPress && event.button() == 3 {
49         common_open_function(tree_view, ColumnsTemporaryFiles::Name as i32, ColumnsTemporaryFiles::Path as i32, OpenMode::OnlyPath);
50     }
51     gtk::Inhibit(false)
52 }
opening_enter_function_temporary_files(tree_view: &gtk::TreeView, event: &gdk::EventKey) -> gtk::Inhibit53 pub fn opening_enter_function_temporary_files(tree_view: &gtk::TreeView, event: &gdk::EventKey) -> gtk::Inhibit {
54     handle_tree_keypress(tree_view, event, ColumnsTemporaryFiles::Name as u32, ColumnsTemporaryFiles::Path as u32, ColumnsTemporaryFiles::ActiveSelectButton as u32)
55 }
56 
opening_double_click_function_big_files(tree_view: &gtk::TreeView, event: &gdk::EventButton) -> gtk::Inhibit57 pub fn opening_double_click_function_big_files(tree_view: &gtk::TreeView, event: &gdk::EventButton) -> gtk::Inhibit {
58     if event.event_type() == gdk::EventType::DoubleButtonPress && event.button() == 1 {
59         common_open_function(tree_view, ColumnsBigFiles::Name as i32, ColumnsBigFiles::Path as i32, OpenMode::PathAndName);
60     } else if event.event_type() == gdk::EventType::DoubleButtonPress && event.button() == 3 {
61         common_open_function(tree_view, ColumnsBigFiles::Name as i32, ColumnsBigFiles::Path as i32, OpenMode::OnlyPath);
62     }
63     gtk::Inhibit(false)
64 }
opening_enter_function_big_files(tree_view: &gtk::TreeView, event: &gdk::EventKey) -> gtk::Inhibit65 pub fn opening_enter_function_big_files(tree_view: &gtk::TreeView, event: &gdk::EventKey) -> gtk::Inhibit {
66     handle_tree_keypress(tree_view, event, ColumnsBigFiles::Name as u32, ColumnsBigFiles::Path as u32, ColumnsBigFiles::ActiveSelectButton as u32)
67 }
68 
opening_double_click_function_zeroed_files(tree_view: &gtk::TreeView, event: &gdk::EventButton) -> gtk::Inhibit69 pub fn opening_double_click_function_zeroed_files(tree_view: &gtk::TreeView, event: &gdk::EventButton) -> gtk::Inhibit {
70     if event.event_type() == gdk::EventType::DoubleButtonPress && event.button() == 1 {
71         common_open_function(tree_view, ColumnsZeroedFiles::Name as i32, ColumnsZeroedFiles::Path as i32, OpenMode::PathAndName);
72     } else if event.event_type() == gdk::EventType::DoubleButtonPress && event.button() == 3 {
73         common_open_function(tree_view, ColumnsZeroedFiles::Name as i32, ColumnsZeroedFiles::Path as i32, OpenMode::OnlyPath);
74     }
75     gtk::Inhibit(false)
76 }
opening_enter_function_zeroed_files(tree_view: &gtk::TreeView, event: &gdk::EventKey) -> gtk::Inhibit77 pub fn opening_enter_function_zeroed_files(tree_view: &gtk::TreeView, event: &gdk::EventKey) -> gtk::Inhibit {
78     handle_tree_keypress(tree_view, event, ColumnsZeroedFiles::Name as u32, ColumnsZeroedFiles::Path as u32, ColumnsZeroedFiles::ActiveSelectButton as u32)
79 }
80 
opening_double_click_function_same_music(tree_view: &gtk::TreeView, event: &gdk::EventButton) -> gtk::Inhibit81 pub fn opening_double_click_function_same_music(tree_view: &gtk::TreeView, event: &gdk::EventButton) -> gtk::Inhibit {
82     if event.event_type() == gdk::EventType::DoubleButtonPress && event.button() == 1 {
83         common_open_function(tree_view, ColumnsSameMusic::Name as i32, ColumnsSameMusic::Path as i32, OpenMode::PathAndName);
84     } else if event.event_type() == gdk::EventType::DoubleButtonPress && event.button() == 3 {
85         common_open_function(tree_view, ColumnsSameMusic::Name as i32, ColumnsSameMusic::Path as i32, OpenMode::OnlyPath);
86     }
87     gtk::Inhibit(false)
88 }
opening_enter_function_same_music(tree_view: &gtk::TreeView, event: &gdk::EventKey) -> gtk::Inhibit89 pub fn opening_enter_function_same_music(tree_view: &gtk::TreeView, event: &gdk::EventKey) -> gtk::Inhibit {
90     handle_tree_keypress(tree_view, event, ColumnsSameMusic::Name as u32, ColumnsSameMusic::Path as u32, ColumnsSameMusic::ActiveSelectButton as u32)
91 }
92 
opening_double_click_function_similar_images(tree_view: &gtk::TreeView, event: &gdk::EventButton) -> gtk::Inhibit93 pub fn opening_double_click_function_similar_images(tree_view: &gtk::TreeView, event: &gdk::EventButton) -> gtk::Inhibit {
94     if event.event_type() == gdk::EventType::DoubleButtonPress && event.button() == 1 {
95         common_open_function(tree_view, ColumnsSimilarImages::Name as i32, ColumnsSimilarImages::Path as i32, OpenMode::PathAndName);
96     } else if event.event_type() == gdk::EventType::DoubleButtonPress && event.button() == 3 {
97         common_open_function(tree_view, ColumnsSimilarImages::Name as i32, ColumnsSimilarImages::Path as i32, OpenMode::OnlyPath);
98     }
99     gtk::Inhibit(false)
100 }
101 
opening_enter_function_similar_images(tree_view: &gtk::TreeView, event: &gdk::EventKey) -> gtk::Inhibit102 pub fn opening_enter_function_similar_images(tree_view: &gtk::TreeView, event: &gdk::EventKey) -> gtk::Inhibit {
103     handle_tree_keypress(tree_view, event, ColumnsSimilarImages::Name as u32, ColumnsSimilarImages::Path as u32, ColumnsSimilarImages::ActiveSelectButton as u32)
104 }
105 
opening_double_click_function_invalid_symlinks(tree_view: &gtk::TreeView, event: &gdk::EventButton) -> gtk::Inhibit106 pub fn opening_double_click_function_invalid_symlinks(tree_view: &gtk::TreeView, event: &gdk::EventButton) -> gtk::Inhibit {
107     if event.event_type() == gdk::EventType::DoubleButtonPress && event.button() == 1 {
108         common_open_function(tree_view, ColumnsInvalidSymlinks::Name as i32, ColumnsInvalidSymlinks::Path as i32, OpenMode::PathAndName);
109     } else if event.event_type() == gdk::EventType::DoubleButtonPress && event.button() == 3 {
110         common_open_function(tree_view, ColumnsInvalidSymlinks::Name as i32, ColumnsInvalidSymlinks::Path as i32, OpenMode::OnlyPath);
111     }
112     gtk::Inhibit(false)
113 }
opening_enter_function_invalid_symlinks(tree_view: &gtk::TreeView, event: &gdk::EventKey) -> gtk::Inhibit114 pub fn opening_enter_function_invalid_symlinks(tree_view: &gtk::TreeView, event: &gdk::EventKey) -> gtk::Inhibit {
115     handle_tree_keypress(tree_view, event, ColumnsInvalidSymlinks::Name as u32, ColumnsInvalidSymlinks::Path as u32, ColumnsInvalidSymlinks::ActiveSelectButton as u32)
116 }
117 
opening_double_click_function_broken_files(tree_view: &gtk::TreeView, event: &gdk::EventButton) -> gtk::Inhibit118 pub fn opening_double_click_function_broken_files(tree_view: &gtk::TreeView, event: &gdk::EventButton) -> gtk::Inhibit {
119     if event.event_type() == gdk::EventType::DoubleButtonPress && event.button() == 1 {
120         common_open_function(tree_view, ColumnsBrokenFiles::Name as i32, ColumnsBrokenFiles::Path as i32, OpenMode::PathAndName);
121     } else if event.event_type() == gdk::EventType::DoubleButtonPress && event.button() == 3 {
122         common_open_function(tree_view, ColumnsBrokenFiles::Name as i32, ColumnsBrokenFiles::Path as i32, OpenMode::OnlyPath);
123     }
124     gtk::Inhibit(false)
125 }
opening_enter_function_broken_files(tree_view: &gtk::TreeView, event: &gdk::EventKey) -> gtk::Inhibit126 pub fn opening_enter_function_broken_files(tree_view: &gtk::TreeView, event: &gdk::EventKey) -> gtk::Inhibit {
127     handle_tree_keypress(tree_view, event, ColumnsBrokenFiles::Name as u32, ColumnsBrokenFiles::Path as u32, ColumnsBrokenFiles::ActiveSelectButton as u32)
128 }
129 
130 enum OpenMode {
131     OnlyPath,
132     PathAndName,
133 }
134 
common_mark_function(tree_view: &gtk::TreeView, column_name: u32)135 fn common_mark_function(tree_view: &gtk::TreeView, column_name: u32) {
136     let selection = tree_view.selection();
137     let (selection_rows, tree_model) = selection.selected_rows();
138 
139     let model = get_list_store(tree_view);
140 
141     for tree_path in selection_rows.iter().rev() {
142         let value = !tree_model.value(&tree_model.iter(tree_path).unwrap(), column_name as i32).get::<bool>().unwrap();
143         model.set_value(&tree_model.iter(tree_path).unwrap(), column_name, &value.to_value());
144     }
145 }
146 
common_open_function(tree_view: &gtk::TreeView, column_name: i32, column_path: i32, opening_mode: OpenMode)147 fn common_open_function(tree_view: &gtk::TreeView, column_name: i32, column_path: i32, opening_mode: OpenMode) {
148     let selection = tree_view.selection();
149     let (selection_rows, tree_model) = selection.selected_rows();
150 
151     for tree_path in selection_rows.iter().rev() {
152         let end_path;
153         let name = tree_model.value(&tree_model.iter(tree_path).unwrap(), column_name).get::<String>().unwrap();
154         let path = tree_model.value(&tree_model.iter(tree_path).unwrap(), column_path).get::<String>().unwrap();
155 
156         match opening_mode {
157             OpenMode::OnlyPath => {
158                 end_path = path;
159             }
160             OpenMode::PathAndName => {
161                 end_path = format!("{}/{}", path, name);
162             }
163         }
164 
165         open::that_in_background(&end_path);
166 
167         // if let Err(e) = open::that(&end_path) {
168         //     println!("Failed to open {} - Error {}", end_path, e);
169         // }
170     }
171 }
172 
handle_tree_keypress(tree_view: &gtk::TreeView, event: &gdk::EventKey, name_column: u32, path_column: u32, mark_column: u32) -> gtk::Inhibit173 fn handle_tree_keypress(tree_view: &gtk::TreeView, event: &gdk::EventKey, name_column: u32, path_column: u32, mark_column: u32) -> gtk::Inhibit {
174     match event.keycode() {
175         Some(KEY_ENTER) => {
176             // Enter
177             common_open_function(tree_view, name_column as i32, path_column as i32, OpenMode::PathAndName);
178         }
179         Some(KEY_SPACE) => {
180             // Space
181             common_mark_function(tree_view, mark_column);
182         }
183         _ => {}
184     }
185     gtk::Inhibit(false)
186 }
187