Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn copy_file(source_path: &Path, target_dir: &Path, _source_panel: &Rc<FilePanel>, target_panel: &Rc<FilePanel>) {
- // Создаем окно для отображения ошибок - получаем его из target_panel
- let parent_window = target_panel.widget()
- .root()
- .and_then(|root| root.downcast::<gtk::Window>().ok())
- .or_else(|| {
- // Резервный способ получения родительского окна
- target_panel.widget()
- .ancestor(gtk::Window::static_type())
- .and_then(|w| w.downcast::<gtk::Window>().ok())
- });
- if !source_path.exists() {
- let message = format!("Файл или директория не существует:\n{}", source_path.display());
- if let Some(window) = parent_window.and_then(|w| w.downcast::<ApplicationWindow>().ok()) {
- Self::show_error_dialog(&window, "Ошибка копирования", &message);
- }
- return;
- }
- let file_name = source_path.file_name().unwrap();
- let target_path = target_dir.join(file_name);
- if target_path.exists() {
- let message = format!(
- "Файл или директория с таким именем уже существует в целевой директории:\n{}",
- target_path.display()
- );
- if let Some(window) = parent_window.and_then(|w| w.downcast::<ApplicationWindow>().ok()) {
- Self::show_error_dialog(&window, "Ошибка копирования", &message);
- }
- return;
- }
- if source_path.is_dir() {
- // Копирование директории рекурсивно
- match Self::copy_directory_recursive(source_path, &target_path) {
- Ok(()) => {
- println!("Директория скопирована: {:?} -> {:?}", source_path, target_path);
- // Обновляем целевую панель
- target_panel.refresh();
- }
- Err(err) => {
- let message = format!(
- "Не удалось скопировать директорию:\n{}\n\nОшибка: {}",
- source_path.display(),
- err
- );
- if let Some(window) = parent_window.and_then(|w| w.downcast::<ApplicationWindow>().ok()) {
- Self::show_error_dialog(&window, "Ошибка копирования", &message);
- }
- }
- }
- } else {
- // Копирование файла
- match std::fs::copy(source_path, &target_path) {
- Ok(bytes_copied) => {
- println!("Скопировано {} байт: {:?} -> {:?}", bytes_copied, source_path, target_path);
- // Обновляем целевую панель
- target_panel.refresh();
- }
- Err(err) => {
- let message = format!(
- "Не удалось скопировать файл:\n{}\n\nОшибка: {}",
- source_path.display(),
- err
- );
- if let Some(window) = parent_window.and_then(|w| w.downcast::<ApplicationWindow>().ok()) {
- Self::show_error_dialog(&window, "Ошибка копирования", &message);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment