|
| 1 | +use std::path::{Path, PathBuf}; |
| 2 | + |
| 3 | +use chrono::Local; |
| 4 | +use egui::ScrollArea; |
| 5 | + |
| 6 | +use crate::{ |
| 7 | + config::get_backups_flder, |
| 8 | + steam::{get_shortcuts_paths, SteamSettings}, |
| 9 | +}; |
| 10 | + |
| 11 | +use super::MyEguiApp; |
| 12 | + |
| 13 | +#[derive(Default)] |
| 14 | +pub struct BackupState { |
| 15 | + pub available_backups: Option<Vec<PathBuf>>, |
| 16 | + pub last_restore: Option<PathBuf>, |
| 17 | +} |
| 18 | + |
| 19 | +impl MyEguiApp { |
| 20 | + pub fn render_backup(&mut self, ui: &mut egui::Ui) { |
| 21 | + ui.heading("Backups"); |
| 22 | + ui.label("Here you can restore backed up shortcuts files"); |
| 23 | + ui.label("Click a backup to restore it, your current shortcuts will be backed up first"); |
| 24 | + ui.add_space(15.0); |
| 25 | + |
| 26 | + if let Some(last_restore) = self.backup_state.last_restore.as_ref() { |
| 27 | + ui.heading(format!("Last restored {:?}", last_restore)); |
| 28 | + } |
| 29 | + |
| 30 | + if ui.button("Click here to create a new backup").clicked() { |
| 31 | + backup_shortcuts(&self.settings.steam); |
| 32 | + self.backup_state.available_backups = None; |
| 33 | + } |
| 34 | + |
| 35 | + let available_backups = self |
| 36 | + .backup_state |
| 37 | + .available_backups |
| 38 | + .get_or_insert_with(|| load_backups()); |
| 39 | + |
| 40 | + if available_backups.is_empty() { |
| 41 | + ui.label("No backups found, they will be created every time you run import"); |
| 42 | + } else { |
| 43 | + ScrollArea::vertical() |
| 44 | + .stick_to_right() |
| 45 | + .auto_shrink([false, true]) |
| 46 | + .show(ui, |ui| { |
| 47 | + for backup_path in available_backups { |
| 48 | + if ui |
| 49 | + .button(backup_path.to_string_lossy().to_string()) |
| 50 | + .clicked() |
| 51 | + { |
| 52 | + //Restore |
| 53 | + backup_shortcuts(&self.settings.steam); |
| 54 | + if restore_backup(&self.settings.steam, backup_path.as_path()) { |
| 55 | + self.backup_state.last_restore = Some(backup_path.clone()); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +pub fn restore_backup(steam_settings: &SteamSettings, shortcut_path: &Path) -> bool { |
| 67 | + let file_name = shortcut_path.file_name().unwrap(); |
| 68 | + let paths = get_shortcuts_paths(steam_settings); |
| 69 | + if let Ok(paths) = paths { |
| 70 | + for user in paths { |
| 71 | + if let Some(user_shortcut_path) = user.shortcut_path { |
| 72 | + if file_name.to_string_lossy().starts_with(&user.user_id) { |
| 73 | + std::fs::copy(shortcut_path, Path::new(&user_shortcut_path)).unwrap(); |
| 74 | + println!("Restored shortcut to path : {}", user_shortcut_path); |
| 75 | + return true; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + return false; |
| 81 | +} |
| 82 | + |
| 83 | +pub fn load_backups() -> Vec<PathBuf> { |
| 84 | + let backup_folder = get_backups_flder(); |
| 85 | + let files = std::fs::read_dir(&backup_folder); |
| 86 | + let mut result = vec![]; |
| 87 | + if let Ok(files) = files { |
| 88 | + for file in files { |
| 89 | + if let Ok(file) = file { |
| 90 | + if file |
| 91 | + .path() |
| 92 | + .extension() |
| 93 | + .unwrap_or_default() |
| 94 | + .to_string_lossy() |
| 95 | + == "vdf" |
| 96 | + { |
| 97 | + result.push(file.path().to_path_buf()); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + result.sort(); |
| 103 | + result.reverse(); |
| 104 | + return result; |
| 105 | +} |
| 106 | + |
| 107 | +pub fn backup_shortcuts(steam_settings: &SteamSettings) { |
| 108 | + let backup_folder = get_backups_flder(); |
| 109 | + let paths = get_shortcuts_paths(&steam_settings); |
| 110 | + let date = Local::now(); |
| 111 | + let date_string = date.format("%Y-%m-%d-%H-%M-%S"); |
| 112 | + if let Ok(user_infos) = paths { |
| 113 | + for user_info in user_infos { |
| 114 | + if let Some(shortcut_path) = user_info.shortcut_path { |
| 115 | + let new_path = backup_folder.join(format!( |
| 116 | + "{}-{}-shortcuts.vdf", |
| 117 | + user_info.user_id, date_string |
| 118 | + )); |
| 119 | + println!("Backed up shortcut at: {:?}", new_path); |
| 120 | + std::fs::copy(&shortcut_path, &new_path).unwrap(); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments