-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
273 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.IO; | ||
using JetBrains.Annotations; | ||
|
||
namespace Loki | ||
{ | ||
public class BackupFileInfo | ||
{ | ||
public string BackupDate { get; } | ||
public string BackupTimeOfDay { get; } | ||
public FileInfo File { get; } | ||
public string Name { get; } | ||
|
||
public BackupFileInfo([NotNull] FileInfo backupFile) | ||
{ | ||
File = backupFile ?? throw new ArgumentNullException(nameof(backupFile)); | ||
Name = backupFile.Name; | ||
BackupDate = "Sometime"; | ||
BackupTimeOfDay = File.CreationTime.ToString("HH:mm"); | ||
if (File.CreationTime.Date == DateTime.Now.Date) | ||
{ | ||
BackupDate = "Today"; | ||
} | ||
else if((DateTime.Now.Date - File.CreationTime.Date).Days <=7) | ||
{ | ||
BackupDate = File.CreationTime.Date.ToString("dddd"); | ||
} | ||
else | ||
{ | ||
BackupDate = File.CreationTime.Date.ToString("d"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<Window x:Class="Loki.Backups" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:loki="clr-namespace:Loki" | ||
mc:Ignorable="d" | ||
WindowStyle="SingleBorderWindow" | ||
ResizeMode="NoResize" | ||
WindowStartupLocation="CenterOwner" | ||
Title="Restore from a backup" Height="520" Width="500" | ||
Loaded="Backups_OnLoaded"> | ||
|
||
<Window.CommandBindings> | ||
<CommandBinding Command="{x:Static loki:Commands.RestoreCharacter}" CanExecute="RestoreCanExecute" Executed="RestoreExecuted"></CommandBinding> | ||
<CommandBinding Command="Close" Executed="CloseExecuted"></CommandBinding> | ||
</Window.CommandBindings> | ||
<Window.Resources> | ||
<loki:BoolToVisibilityConverter x:Key="InvBoolToVis" False="Visible" True="Collapsed"/> | ||
<Style TargetType="Button"> | ||
<Setter Property="Width" Value="112"/> | ||
<Setter Property="Height" Value="28"/> | ||
<Setter Property="Margin" Value="4"/> | ||
</Style> | ||
</Window.Resources> | ||
<DockPanel> | ||
|
||
<!-- Bit at the bottom with buttons on --> | ||
<Border Background="{x:Static SystemColors.ControlDarkBrush}" | ||
DockPanel.Dock="Bottom"> | ||
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right"> | ||
<CheckBox IsChecked="True" x:Name="BackupCheckbox" VerticalAlignment="Center" | ||
ToolTip="Creates a new backup of the current character file before doing the restore operation."> | ||
Create new backup before restore | ||
</CheckBox> | ||
<Button Command="{x:Static loki:Commands.RestoreCharacter}" | ||
ToolTip="Restore from the selected backup"> | ||
Restore Selected | ||
</Button> | ||
<Button Command="Close" ToolTip="Cancel backup and return to main window"> | ||
Cancel | ||
</Button> | ||
</StackPanel> | ||
|
||
</Border> | ||
|
||
<!-- Warning for doing a naked backup --> | ||
<Border DockPanel.Dock="Bottom" Background="DarkRed" | ||
Visibility="{Binding ElementName=BackupCheckbox, Path=IsChecked, Converter={StaticResource InvBoolToVis}}"> | ||
<Label Foreground="WhiteSmoke" HorizontalAlignment="Center">Restoring will permanently overwrite current character data</Label> | ||
</Border> | ||
<!-- Main area above the buttons --> | ||
<Grid> | ||
|
||
<!-- List of Backups to select --> | ||
<ListView x:Name="BackupFileList" HorizontalContentAlignment="Stretch"> | ||
<ListView.ItemTemplate> | ||
<DataTemplate DataType="loki:BackupFileInfo"> | ||
<DockPanel Margin="2"> | ||
<Border DockPanel.Dock="Right" Background="{x:Static SystemColors.ControlLightBrush}" CornerRadius="4" Padding="2"> | ||
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" UseLayoutRounding="True"> | ||
<Image Source="Resources/calander-16.ico" Margin="8,2"/> | ||
<TextBlock Text="{Binding BackupDate}" Width="80" Margin="2"/> | ||
<Image Source="Resources/clock-16.ico" Margin="8,2"/> | ||
<TextBlock Text="{Binding BackupTimeOfDay}" Margin="2"/> | ||
</StackPanel> | ||
|
||
</Border> | ||
<TextBlock VerticalAlignment="Center" Text="{Binding Name}"/> | ||
</DockPanel> | ||
</DataTemplate> | ||
</ListView.ItemTemplate> | ||
</ListView> | ||
|
||
<!-- Loading Indicator --> | ||
<Label | ||
x:Name="StatusIndicator" | ||
Foreground="#2B2B2B" VerticalAlignment="Center" HorizontalAlignment="Center" | ||
FontSize="24">Loading</Label> | ||
|
||
</Grid> | ||
</DockPanel> | ||
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using System.Windows; | ||
using System.Windows.Input; | ||
using System.Windows.Media; | ||
using JetBrains.Annotations; | ||
|
||
namespace Loki | ||
{ | ||
/// <summary> | ||
/// Interaction logic for Backups.xaml | ||
/// </summary> | ||
public partial class Backups | ||
{ | ||
private readonly CharacterFile _currentFile; | ||
|
||
public Backups([NotNull] CharacterFile currentFile) | ||
{ | ||
_currentFile = currentFile ?? throw new ArgumentNullException(nameof(currentFile)); | ||
InitializeComponent(); | ||
} | ||
|
||
private async void Backups_OnLoaded(object sender, RoutedEventArgs e) | ||
{ | ||
try | ||
{ | ||
Debug.Assert(_currentFile != null); | ||
|
||
var backupFiles = await Task.Run(() => Backup.GetBackupsFor(_currentFile)); | ||
|
||
BackupFileList.ItemsSource = backupFiles.OrderByDescending(file => file.CreationTime) | ||
.Select(file => new BackupFileInfo(file)).ToArray(); | ||
; | ||
if (backupFiles.Length > 0) | ||
{ | ||
StatusIndicator.Visibility = Visibility.Hidden; | ||
} | ||
else | ||
{ | ||
StatusIndicator.Content = "No backups"; | ||
} | ||
} | ||
catch (Exception) | ||
{ | ||
// TODO: Log exception | ||
StatusIndicator.Foreground = Brushes.Firebrick; | ||
StatusIndicator.Content = "Failed to load backups"; | ||
} | ||
} | ||
|
||
private void RestoreCanExecute(object sender, CanExecuteRoutedEventArgs e) => e.CanExecute = BackupFileList?.SelectedItem != null; | ||
|
||
private void RestoreExecuted(object sender, ExecutedRoutedEventArgs e) | ||
{ | ||
try | ||
{ | ||
|
||
if (!(BackupFileList.SelectedItem is BackupFileInfo backupInfo)) | ||
throw new InvalidOperationException("Cannot perform backup without selected backup info"); | ||
|
||
// Create a backup first unless the user opted not to do so. | ||
if (BackupCheckbox.IsChecked == true) | ||
Backup.BackupCharacter(_currentFile); | ||
|
||
backupInfo.File.CopyTo(_currentFile.FilePath, true); | ||
|
||
MessageBox.Show("Character restored successfully!", "Restore complete", MessageBoxButton.OK, | ||
MessageBoxImage.Information); | ||
DialogResult = true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
// TODO: Log errors from restoring. | ||
MessageBox.Show("Failed to restore backup!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); | ||
} | ||
|
||
Close(); | ||
} | ||
|
||
private void CloseExecuted(object sender, ExecutedRoutedEventArgs e) | ||
{ | ||
DialogResult = false; | ||
Close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.