aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Modules/Tango.PPC.BackupRestore/ViewModels/RestoreViewVM.cs
blob: d925ebad15fc48f0549c0defd5db1a10b34ec1be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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<BackupRestoreModule>(nameof(RestoreProgressView), false);

            try
            {
                IsFree = false;
                NavigationManager.IsBackEnabled = false;
                var result = await BackupManager.Restore(_backupFileLocation, RestoreSettings);
                await NavigationManager.NavigateWithObject<BackupRestoreModule, RestoreCompletedView, RestoreResult>(result, false);
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "The restore operation failed.");

                await NavigationManager.NavigateWithObject<BackupRestoreModule, RestoreErrorView, RestoreErrorNavigationObject>(new RestoreErrorNavigationObject()
                {
                    Error = ex.FlattenMessage(),
                }, false);

                NavigationManager.IsBackEnabled = true;
            }
            finally
            {
                IsFree = true;
            }
        }

        private async void BrowseForBackup()
        {
            _isBrowsing = true;

            var result = await NavigationManager.
               NavigateForResult<StorageModule,
               Storage.Views.MainView, ExplorerFileItem,
               Storage.Models.StorageNavigationRequest>(
               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<bool> 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()
        {

        }
    }
}