using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.Core.Commands; using Tango.Core.DI; using Tango.Explorer; using Tango.PPC.BackupRestore.Views; using Tango.PPC.Common; using Tango.PPC.Common.BackupRestore; using Tango.PPC.Storage; using static Tango.PPC.BackupRestore.ViewModels.RestoreErrorViewVM; namespace Tango.PPC.BackupRestore.ViewModels { public class RestoreViewVM : PPCViewModel { private string _backupFileLocation; private bool _isBrowsing; [TangoInject] public IBackupManager BackupManager { get; set; } private String _backupFileName; public String BackupFileName { get { return _backupFileName; } set { _backupFileName = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } private RestoreSettings _restoreSettings; public RestoreSettings RestoreSettings { get { return _restoreSettings; } set { _restoreSettings = value; RaisePropertyChangedAuto(); } } private BackupFile _backupFile; public BackupFile BackupFile { get { return _backupFile; } set { _backupFile = value; RaisePropertyChangedAuto(); } } private BackupRestoreProgressEventArgs _currentRestoreProgress; public BackupRestoreProgressEventArgs CurrentRestoreProgress { get { return _currentRestoreProgress; } set { _currentRestoreProgress = value; RaisePropertyChangedAuto(); } } private long _backupSize; public long BackupSize { get { return _backupSize; } set { _backupSize = value; RaisePropertyChangedAuto(); } } public RelayCommand BrowseForBackupCommand { get; set; } public RelayCommand RestoreCommand { get; set; } public RestoreViewVM() { RestoreSettings = new RestoreSettings(); RestoreCommand = new RelayCommand(StartRestore, () => BackupFileName != null); BrowseForBackupCommand = new RelayCommand(BrowseForBackup); } private async void StartRestore() { await NavigationManager.NavigateTo(nameof(RestoreProgressView), false); try { IsFree = false; NavigationManager.IsBackEnabled = false; var result = await BackupManager.Restore(_backupFileLocation, RestoreSettings); await NavigationManager.NavigateWithObject(result, false); } catch (Exception ex) { LogManager.Log(ex, "The restore operation failed."); await NavigationManager.NavigateWithObject(new RestoreErrorNavigationObject() { Error = ex.FlattenMessage(), }, false); NavigationManager.IsBackEnabled = true; } finally { IsFree = true; } } private async void BrowseForBackup() { _isBrowsing = true; var result = await NavigationManager. NavigateForResult( new Storage.Models.StorageNavigationRequest() { Intent = Storage.Models.StorageNavigationIntent.LoadFile, Filter = ExplorerFileDefinition.Backup.Extension, Title = "Select Backup File", }); _isBrowsing = false; if (result != null) { _backupFileLocation = result.Path; try { BackupFile = await BackupManager.ExtractBackupConfiguration(_backupFileLocation); BackupFileName = Path.GetFileName(result.Path); BackupSize = new System.IO.FileInfo(_backupFileLocation).Length; } catch (Exception ex) { LogManager.Log(ex, $"Error extracting backup configuration from file '{_backupFileLocation}'."); await NotificationProvider.ShowError($"Error occurred while trying to extract the backup file information\n{ex.FlattenMessage()}"); } } } public override Task OnNavigateBackRequest() { return Task.FromResult(IsFree); } public override void OnNavigatedFrom() { base.OnNavigatedFrom(); if (!_isBrowsing) { BackupFileName = null; BackupFile = null; _backupFileLocation = null; } } public override void OnApplicationReady() { base.OnApplicationReady(); BackupManager.Progress += BackupManager_Progress; } private void BackupManager_Progress(object sender, BackupRestoreProgressEventArgs e) { CurrentRestoreProgress = e; } public override void OnApplicationStarted() { } } }