-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackupWindow.xaml.cs
More file actions
374 lines (329 loc) · 15 KB
/
Copy pathBackupWindow.xaml.cs
File metadata and controls
374 lines (329 loc) · 15 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;
using static NodaStack.NotificationManager;
namespace NodaStack
{
public partial class BackupWindow : Window
{
private BackupManager backupManager;
private List<BackupInfo> backupList;
private BackupInfo? selectedBackup;
public BackupWindow()
{
InitializeComponent();
backupManager = new BackupManager(new ConfigurationManager(), new LogManager());
backupList = new List<BackupInfo>();
LoadBackupHistory();
}
private void LoadBackupHistory()
{
try
{
backupList = backupManager.GetBackupList();
BackupHistoryListView.ItemsSource = backupList;
StatusText.Text = $"Loaded {backupList.Count} backup(s)";
}
catch (Exception ex)
{
StatusText.Text = $"Error loading backups: {ex.Message}";
}
}
private void RefreshButton_Click(object sender, RoutedEventArgs e)
{
LoadBackupHistory();
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
Close();
}
private bool ValidateBackupName(string name)
{
if (string.IsNullOrWhiteSpace(name))
return true; // Auto-generated name is OK
// Check for invalid characters
var invalidChars = Path.GetInvalidFileNameChars();
if (name.Any(c => invalidChars.Contains(c)))
{
MessageBox.Show("Backup name contains invalid characters. Please use only letters, numbers, spaces, and common punctuation.",
"Invalid Backup Name", MessageBoxButton.OK, MessageBoxImage.Warning);
return false;
}
// Check if name already exists
var existingBackup = backupList.FirstOrDefault(b => b.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
if (existingBackup != null)
{
var result = MessageBox.Show($"A backup named '{name}' already exists. Do you want to replace it?",
"Backup Name Exists", MessageBoxButton.YesNo, MessageBoxImage.Question);
return result == MessageBoxResult.Yes;
}
return true;
}
private async void CreateFullBackupButton_Click(object sender, RoutedEventArgs e)
{
try
{
string backupName = BackupNameTextBox.Text.Trim();
if (!ValidateBackupName(backupName))
return;
if (string.IsNullOrEmpty(backupName))
{
backupName = $"backup_{DateTime.Now:yyyy-MM-dd_HH-mm}";
}
StatusText.Text = "Creating full backup...";
CreateFullBackupButton.IsEnabled = false;
CreateConfigBackupButton.IsEnabled = false;
var success = await backupManager.CreateFullBackupAsync(backupName);
if (success)
{
StatusText.Text = "Full backup created successfully";
LoadBackupHistory();
BackupNameTextBox.Clear();
NotificationManager.ShowNotification("Backup Created", "Full backup completed successfully", NotificationType.Success);
}
else
{
StatusText.Text = "Failed to create full backup";
NotificationManager.ShowNotification("Backup Failed", "Failed to create full backup", NotificationType.Error);
}
}
catch (Exception ex)
{
StatusText.Text = $"Error creating backup: {ex.Message}";
NotificationManager.ShowNotification("Backup Error", $"Error creating backup: {ex.Message}", NotificationType.Error);
}
finally
{
CreateFullBackupButton.IsEnabled = true;
CreateConfigBackupButton.IsEnabled = true;
}
}
private async void CreateConfigBackupButton_Click(object sender, RoutedEventArgs e)
{
try
{
string backupName = BackupNameTextBox.Text.Trim();
if (!ValidateBackupName(backupName))
return;
if (string.IsNullOrEmpty(backupName))
{
backupName = $"config_backup_{DateTime.Now:yyyy-MM-dd_HH-mm}";
}
StatusText.Text = "Creating configuration backup...";
CreateFullBackupButton.IsEnabled = false;
CreateConfigBackupButton.IsEnabled = false;
var success = await backupManager.CreateConfigBackupAsync(backupName);
if (success)
{
StatusText.Text = "Configuration backup created successfully";
LoadBackupHistory();
BackupNameTextBox.Clear();
NotificationManager.ShowNotification("Backup Created", "Configuration backup completed successfully", NotificationType.Success);
}
else
{
StatusText.Text = "Failed to create configuration backup";
NotificationManager.ShowNotification("Backup Failed", "Failed to create configuration backup", NotificationType.Error);
}
}
catch (Exception ex)
{
StatusText.Text = $"Error creating config backup: {ex.Message}";
NotificationManager.ShowNotification("Backup Error", $"Error creating config backup: {ex.Message}", NotificationType.Error);
}
finally
{
CreateFullBackupButton.IsEnabled = true;
CreateConfigBackupButton.IsEnabled = true;
}
}
private void ImportBackupButton_Click(object sender, RoutedEventArgs e)
{
try
{
var openFileDialog = new OpenFileDialog
{
Title = "Select Backup File",
Filter = "Backup files (*.zip)|*.zip|All files (*.*)|*.*",
DefaultExt = "zip"
};
if (openFileDialog.ShowDialog() == true)
{
var sourcePath = openFileDialog.FileName;
var fileName = Path.GetFileName(sourcePath);
var targetPath = Path.Combine(backupManager.GetBackupDirectory(), fileName);
if (File.Exists(targetPath))
{
var result = MessageBox.Show(
"A backup with this name already exists. Do you want to replace it?",
"Backup Import",
MessageBoxButton.YesNo,
MessageBoxImage.Question);
if (result != MessageBoxResult.Yes)
return;
}
File.Copy(sourcePath, targetPath, true);
StatusText.Text = $"Backup imported successfully: {fileName}";
LoadBackupHistory();
NotificationManager.ShowNotification("Backup Imported", $"Backup '{fileName}' imported successfully", NotificationType.Success);
}
}
catch (Exception ex)
{
StatusText.Text = $"Error importing backup: {ex.Message}";
NotificationManager.ShowNotification("Import Error", $"Error importing backup: {ex.Message}", NotificationType.Error);
}
}
private void ExportSelectedButton_Click(object sender, RoutedEventArgs e)
{
if (selectedBackup == null)
{
MessageBox.Show("Please select a backup to export.", "Export Backup", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
try
{
var saveFileDialog = new SaveFileDialog
{
Title = "Export Backup",
Filter = "Backup files (*.zip)|*.zip|All files (*.*)|*.*",
DefaultExt = "zip",
FileName = $"{selectedBackup.Name}.zip"
};
if (saveFileDialog.ShowDialog() == true)
{
File.Copy(selectedBackup.FilePath, saveFileDialog.FileName, true);
StatusText.Text = $"Backup exported successfully: {Path.GetFileName(saveFileDialog.FileName)}";
NotificationManager.ShowNotification("Backup Exported", $"Backup exported to {Path.GetFileName(saveFileDialog.FileName)}", NotificationType.Success);
}
}
catch (Exception ex)
{
StatusText.Text = $"Error exporting backup: {ex.Message}";
NotificationManager.ShowNotification("Export Error", $"Error exporting backup: {ex.Message}", NotificationType.Error);
}
}
private async void RestoreBackupButton_Click(object sender, RoutedEventArgs e)
{
if (selectedBackup == null)
{
MessageBox.Show("Please select a backup to restore.", "Restore Backup", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
var result = MessageBox.Show(
$"Are you sure you want to restore the backup '{selectedBackup.Name}'?\n\nThis will overwrite current configuration and data.",
"Restore Backup",
MessageBoxButton.YesNo,
MessageBoxImage.Warning);
if (result == MessageBoxResult.Yes)
{
try
{
StatusText.Text = "Restoring backup...";
RestoreBackupButton.IsEnabled = false;
DeleteBackupButton.IsEnabled = false;
var success = await backupManager.RestoreBackupAsync(selectedBackup.FilePath);
if (success)
{
StatusText.Text = "Backup restored successfully";
NotificationManager.ShowNotification("Backup Restored", "Backup restored successfully", NotificationType.Success);
MessageBox.Show("Backup restored successfully. The application may need to be restarted.", "Restore Complete", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
StatusText.Text = "Failed to restore backup";
NotificationManager.ShowNotification("Restore Failed", "Failed to restore backup", NotificationType.Error);
MessageBox.Show("Failed to restore backup. Please check the logs for details.", "Restore Failed", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
catch (Exception ex)
{
StatusText.Text = $"Error restoring backup: {ex.Message}";
NotificationManager.ShowNotification("Restore Error", $"Error restoring backup: {ex.Message}", NotificationType.Error);
MessageBox.Show($"Error restoring backup: {ex.Message}", "Restore Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
RestoreBackupButton.IsEnabled = true;
DeleteBackupButton.IsEnabled = true;
}
}
}
private void DeleteBackupButton_Click(object sender, RoutedEventArgs e)
{
if (selectedBackup == null)
{
MessageBox.Show("Please select a backup to delete.", "Delete Backup", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
var result = MessageBox.Show(
$"Are you sure you want to delete the backup '{selectedBackup.Name}'?\n\nThis action cannot be undone.",
"Delete Backup",
MessageBoxButton.YesNo,
MessageBoxImage.Warning);
if (result == MessageBoxResult.Yes)
{
try
{
var success = backupManager.DeleteBackup(selectedBackup.FilePath);
if (success)
{
StatusText.Text = "Backup deleted successfully";
LoadBackupHistory();
selectedBackup = null;
NotificationManager.ShowNotification("Backup Deleted", "Backup deleted successfully", NotificationType.Success);
}
else
{
StatusText.Text = "Failed to delete backup";
NotificationManager.ShowNotification("Delete Failed", "Failed to delete backup", NotificationType.Error);
}
}
catch (Exception ex)
{
StatusText.Text = $"Error deleting backup: {ex.Message}";
NotificationManager.ShowNotification("Delete Error", $"Error deleting backup: {ex.Message}", NotificationType.Error);
}
}
}
private void BackupHistoryListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
selectedBackup = BackupHistoryListView.SelectedItem as BackupInfo;
ExportSelectedButton.IsEnabled = selectedBackup != null;
RestoreBackupButton.IsEnabled = selectedBackup != null;
DeleteBackupButton.IsEnabled = selectedBackup != null;
UpdateBackupDetails();
}
private void UpdateBackupDetails()
{
if (selectedBackup == null)
{
BackupDetailsText.Text = "Select a backup to view details";
return;
}
var details = $"Name: {selectedBackup.Name}\n" +
$"Type: {selectedBackup.Type}\n" +
$"Created: {selectedBackup.CreatedAt:dd/MM/yyyy HH:mm:ss}\n" +
$"Size: {FormatFileSize(selectedBackup.FileSize)}\n" +
$"Path: {selectedBackup.FilePath}";
BackupDetailsText.Text = details;
}
private string FormatFileSize(long bytes)
{
string[] sizes = { "B", "KB", "MB", "GB" };
double len = bytes;
int order = 0;
while (len >= 1024 && order < sizes.Length - 1)
{
order++;
len = len / 1024;
}
return $"{len:0.##} {sizes[order]}";
}
}
}