| 
									
										
										
										
											2022-08-31 16:31:31 +08:00
										 |  |  | use hbb_common::{fs, message_proto::*, log};
 | 
					
						
							| 
									
										
										
										
											2022-05-12 17:35:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-01 11:26:32 +08:00
										 |  |  | use super::{Data, Interface};
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-12 17:35:25 +08:00
										 |  |  | pub trait FileManager: Interface {
 | 
					
						
							| 
									
										
										
										
											2022-05-31 17:36:36 +08:00
										 |  |  |     fn get_home_dir(&self) -> String {
 | 
					
						
							| 
									
										
										
										
											2022-05-12 17:35:25 +08:00
										 |  |  |         fs::get_home_as_string()
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-10 17:09:31 +08:00
										 |  |  |     #[cfg(not(any(target_os = "android", target_os = "ios", feature = "cli", feature = "flutter")))]
 | 
					
						
							| 
									
										
										
										
											2022-05-31 17:36:36 +08:00
										 |  |  |     fn read_dir(&self, path: String, include_hidden: bool) -> sciter::Value {
 | 
					
						
							| 
									
										
										
										
											2022-05-12 17:35:25 +08:00
										 |  |  |         match fs::read_dir(&fs::get_path(&path), include_hidden) {
 | 
					
						
							|  |  |  |             Err(_) => sciter::Value::null(),
 | 
					
						
							|  |  |  |             Ok(fd) => {
 | 
					
						
							|  |  |  |                 use crate::ui::remote::make_fd;
 | 
					
						
							|  |  |  |                 let mut m = make_fd(0, &fd.entries.to_vec(), false);
 | 
					
						
							|  |  |  |                 m.set_item("path", path);
 | 
					
						
							|  |  |  |                 m
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-10 17:09:31 +08:00
										 |  |  |     #[cfg(any(target_os = "android", target_os = "ios", feature = "cli", feature = "flutter"))]
 | 
					
						
							| 
									
										
										
										
											2022-05-31 17:36:36 +08:00
										 |  |  |     fn read_dir(&self, path: &str, include_hidden: bool) -> String {
 | 
					
						
							| 
									
										
										
										
											2022-12-29 00:02:31 +08:00
										 |  |  |         use crate::common::make_fd_to_json;
 | 
					
						
							| 
									
										
										
										
											2022-05-31 17:36:36 +08:00
										 |  |  |         match fs::read_dir(&fs::get_path(path), include_hidden) {
 | 
					
						
							| 
									
										
										
										
											2022-09-05 20:05:23 +08:00
										 |  |  |             Ok(fd) => make_fd_to_json(fd.id, fd.path, &fd.entries),
 | 
					
						
							| 
									
										
										
										
											2022-05-31 17:36:36 +08:00
										 |  |  |             Err(_) => "".into(),
 | 
					
						
							| 
									
										
										
										
											2022-05-12 17:35:25 +08:00
										 |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-31 22:09:36 +08:00
										 |  |  |     fn cancel_job(&self, id: i32) {
 | 
					
						
							| 
									
										
										
										
											2022-05-12 17:35:25 +08:00
										 |  |  |         self.send(Data::CancelJob(id));
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fn read_remote_dir(&self, path: String, include_hidden: bool) {
 | 
					
						
							|  |  |  |         let mut msg_out = Message::new();
 | 
					
						
							|  |  |  |         let mut file_action = FileAction::new();
 | 
					
						
							|  |  |  |         file_action.set_read_dir(ReadDir {
 | 
					
						
							|  |  |  |             path,
 | 
					
						
							|  |  |  |             include_hidden,
 | 
					
						
							|  |  |  |             ..Default::default()
 | 
					
						
							|  |  |  |         });
 | 
					
						
							|  |  |  |         msg_out.set_file_action(file_action);
 | 
					
						
							|  |  |  |         self.send(Data::Message(msg_out));
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-31 22:09:36 +08:00
										 |  |  |     fn remove_file(&self, id: i32, path: String, file_num: i32, is_remote: bool) {
 | 
					
						
							| 
									
										
										
										
											2022-05-12 17:35:25 +08:00
										 |  |  |         self.send(Data::RemoveFile((id, path, file_num, is_remote)));
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-01 11:26:32 +08:00
										 |  |  |     fn remove_dir_all(&self, id: i32, path: String, is_remote: bool, include_hidden: bool) {
 | 
					
						
							|  |  |  |         self.send(Data::RemoveDirAll((id, path, is_remote, include_hidden)));
 | 
					
						
							| 
									
										
										
										
											2022-05-12 17:35:25 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-31 22:09:36 +08:00
										 |  |  |     fn confirm_delete_files(&self, id: i32, file_num: i32) {
 | 
					
						
							| 
									
										
										
										
											2022-05-12 17:35:25 +08:00
										 |  |  |         self.send(Data::ConfirmDeleteFiles((id, file_num)));
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-31 22:09:36 +08:00
										 |  |  |     fn set_no_confirm(&self, id: i32) {
 | 
					
						
							| 
									
										
										
										
											2022-05-12 17:35:25 +08:00
										 |  |  |         self.send(Data::SetNoConfirm(id));
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-31 22:09:36 +08:00
										 |  |  |     fn remove_dir(&self, id: i32, path: String, is_remote: bool) {
 | 
					
						
							| 
									
										
										
										
											2022-05-12 17:35:25 +08:00
										 |  |  |         if is_remote {
 | 
					
						
							|  |  |  |             self.send(Data::RemoveDir((id, path)));
 | 
					
						
							|  |  |  |         } else {
 | 
					
						
							|  |  |  |             fs::remove_all_empty_dir(&fs::get_path(&path)).ok();
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-31 22:09:36 +08:00
										 |  |  |     fn create_dir(&self, id: i32, path: String, is_remote: bool) {
 | 
					
						
							| 
									
										
										
										
											2022-05-12 17:35:25 +08:00
										 |  |  |         self.send(Data::CreateDir((id, path, is_remote)));
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fn send_files(
 | 
					
						
							| 
									
										
										
										
											2022-05-31 17:36:36 +08:00
										 |  |  |         &self,
 | 
					
						
							| 
									
										
										
										
											2022-05-12 17:35:25 +08:00
										 |  |  |         id: i32,
 | 
					
						
							|  |  |  |         path: String,
 | 
					
						
							|  |  |  |         to: String,
 | 
					
						
							| 
									
										
										
										
											2022-05-13 14:46:37 +08:00
										 |  |  |         file_num: i32,
 | 
					
						
							| 
									
										
										
										
											2022-05-12 17:35:25 +08:00
										 |  |  |         include_hidden: bool,
 | 
					
						
							|  |  |  |         is_remote: bool,
 | 
					
						
							|  |  |  |     ) {
 | 
					
						
							| 
									
										
										
										
											2022-05-31 17:36:36 +08:00
										 |  |  |         self.send(Data::SendFiles((
 | 
					
						
							|  |  |  |             id,
 | 
					
						
							|  |  |  |             path,
 | 
					
						
							|  |  |  |             to,
 | 
					
						
							|  |  |  |             file_num,
 | 
					
						
							|  |  |  |             include_hidden,
 | 
					
						
							|  |  |  |             is_remote,
 | 
					
						
							|  |  |  |         )));
 | 
					
						
							| 
									
										
										
										
											2022-05-12 17:35:25 +08:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2022-05-14 11:58:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     fn add_job(
 | 
					
						
							| 
									
										
										
										
											2022-07-11 18:23:58 +08:00
										 |  |  |         &self,
 | 
					
						
							| 
									
										
										
										
											2022-05-14 11:58:47 +08:00
										 |  |  |         id: i32,
 | 
					
						
							|  |  |  |         path: String,
 | 
					
						
							|  |  |  |         to: String,
 | 
					
						
							|  |  |  |         file_num: i32,
 | 
					
						
							|  |  |  |         include_hidden: bool,
 | 
					
						
							|  |  |  |         is_remote: bool,
 | 
					
						
							|  |  |  |     ) {
 | 
					
						
							| 
									
										
										
										
											2022-05-31 17:36:36 +08:00
										 |  |  |         self.send(Data::AddJob((
 | 
					
						
							|  |  |  |             id,
 | 
					
						
							|  |  |  |             path,
 | 
					
						
							|  |  |  |             to,
 | 
					
						
							|  |  |  |             file_num,
 | 
					
						
							|  |  |  |             include_hidden,
 | 
					
						
							|  |  |  |             is_remote,
 | 
					
						
							|  |  |  |         )));
 | 
					
						
							| 
									
										
										
										
											2022-05-14 11:58:47 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-11 18:23:58 +08:00
										 |  |  |     fn resume_job(&self, id: i32, is_remote: bool) {
 | 
					
						
							| 
									
										
										
										
											2022-05-31 17:36:36 +08:00
										 |  |  |         self.send(Data::ResumeJob((id, is_remote)));
 | 
					
						
							| 
									
										
										
										
											2022-05-14 11:58:47 +08:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2022-08-31 16:31:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     fn set_confirm_override_file(
 | 
					
						
							|  |  |  |         &self,
 | 
					
						
							|  |  |  |         id: i32,
 | 
					
						
							|  |  |  |         file_num: i32,
 | 
					
						
							|  |  |  |         need_override: bool,
 | 
					
						
							|  |  |  |         remember: bool,
 | 
					
						
							|  |  |  |         is_upload: bool,
 | 
					
						
							|  |  |  |     ) {
 | 
					
						
							|  |  |  |         log::info!(
 | 
					
						
							|  |  |  |             "confirm file transfer, job: {}, need_override: {}",
 | 
					
						
							|  |  |  |             id,
 | 
					
						
							|  |  |  |             need_override
 | 
					
						
							|  |  |  |         );
 | 
					
						
							|  |  |  |         self.send(Data::SetConfirmOverrideFile((
 | 
					
						
							|  |  |  |             id,
 | 
					
						
							|  |  |  |             file_num,
 | 
					
						
							|  |  |  |             need_override,
 | 
					
						
							|  |  |  |             remember,
 | 
					
						
							|  |  |  |             is_upload,
 | 
					
						
							|  |  |  |         )));
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2022-05-12 17:35:25 +08:00
										 |  |  | }
 |