2020-04-09 04:18:29 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Aaru.Console;
|
|
|
|
|
|
using Aaru.Core;
|
|
|
|
|
|
using Aaru.Database;
|
2020-04-10 22:33:27 +01:00
|
|
|
|
using Aaru.Gui.Views;
|
2020-04-09 04:18:29 +01:00
|
|
|
|
using Aaru.Settings;
|
|
|
|
|
|
using Avalonia.Threading;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2020-04-09 03:28:08 +01:00
|
|
|
|
using ReactiveUI;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Aaru.Gui.ViewModels
|
|
|
|
|
|
{
|
|
|
|
|
|
public class SplashWindowViewModel : ViewModelBase
|
|
|
|
|
|
{
|
2020-04-10 22:33:27 +01:00
|
|
|
|
readonly SplashWindow _view;
|
|
|
|
|
|
double _currentProgress;
|
|
|
|
|
|
double _maxProgress;
|
|
|
|
|
|
string _message;
|
|
|
|
|
|
|
|
|
|
|
|
public SplashWindowViewModel(SplashWindow view) => _view = view;
|
2020-04-09 03:28:08 +01:00
|
|
|
|
|
|
|
|
|
|
public string Message
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _message;
|
|
|
|
|
|
set => this.RaiseAndSetIfChanged(ref _message, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public double MaxProgress
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _maxProgress;
|
|
|
|
|
|
set => this.RaiseAndSetIfChanged(ref _maxProgress, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public double CurrentProgress
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _currentProgress;
|
|
|
|
|
|
set => this.RaiseAndSetIfChanged(ref _currentProgress, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal void OnOpened()
|
|
|
|
|
|
{
|
|
|
|
|
|
Message = "Welcome to Aaru!";
|
2020-04-09 04:18:29 +01:00
|
|
|
|
MaxProgress = 9;
|
2020-04-09 03:28:08 +01:00
|
|
|
|
CurrentProgress = 0;
|
|
|
|
|
|
|
|
|
|
|
|
Dispatcher.UIThread.Post(InitializeConsole);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InitializeConsole()
|
|
|
|
|
|
{
|
|
|
|
|
|
CurrentProgress++;
|
|
|
|
|
|
Message = "Initializing console...";
|
2020-04-09 04:18:29 +01:00
|
|
|
|
|
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ConsoleHandler.Init();
|
2020-04-11 03:05:21 +01:00
|
|
|
|
AaruConsole.WriteLine("Aaru started!");
|
2020-04-09 04:18:29 +01:00
|
|
|
|
|
|
|
|
|
|
Dispatcher.UIThread.Post(LoadSettings);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LoadSettings()
|
|
|
|
|
|
{
|
|
|
|
|
|
CurrentProgress++;
|
|
|
|
|
|
Message = "Loading settings...";
|
2020-04-11 03:05:21 +01:00
|
|
|
|
AaruConsole.WriteLine("Loading settings...");
|
2020-04-09 04:18:29 +01:00
|
|
|
|
|
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
// TODO: Detect there are no settings yet
|
|
|
|
|
|
Settings.Settings.LoadSettings();
|
|
|
|
|
|
|
|
|
|
|
|
Dispatcher.UIThread.Post(MigrateLocalDatabase);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MigrateLocalDatabase()
|
|
|
|
|
|
{
|
|
|
|
|
|
CurrentProgress++;
|
|
|
|
|
|
Message = "Migrating local database...";
|
2020-04-11 03:05:21 +01:00
|
|
|
|
AaruConsole.WriteLine("Migrating local database...");
|
2020-04-09 04:18:29 +01:00
|
|
|
|
|
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var ctx = AaruContext.Create(Settings.Settings.LocalDbPath);
|
|
|
|
|
|
ctx.Database.Migrate();
|
|
|
|
|
|
ctx.SaveChanges();
|
|
|
|
|
|
|
|
|
|
|
|
Dispatcher.UIThread.Post(UpdateMasterDatabase);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UpdateMasterDatabase()
|
|
|
|
|
|
{
|
|
|
|
|
|
CurrentProgress++;
|
|
|
|
|
|
Message = "Updating master database...";
|
2020-04-11 03:05:21 +01:00
|
|
|
|
AaruConsole.WriteLine("Updating master database...");
|
2020-04-09 04:18:29 +01:00
|
|
|
|
|
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
bool masterDbUpdate = false;
|
|
|
|
|
|
|
|
|
|
|
|
if(!File.Exists(Settings.Settings.MasterDbPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
masterDbUpdate = true;
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Update database
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var masterContext = AaruContext.Create(Settings.Settings.MasterDbPath);
|
|
|
|
|
|
|
|
|
|
|
|
if(masterContext.Database.GetPendingMigrations().Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
AaruConsole.WriteLine("New database version, updating...");
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
File.Delete(Settings.Settings.MasterDbPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch(Exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
AaruConsole.
|
|
|
|
|
|
ErrorWriteLine("Exception trying to remove old database version, cannot continue...");
|
|
|
|
|
|
|
|
|
|
|
|
AaruConsole.ErrorWriteLine("Please manually remove file at {0}",
|
|
|
|
|
|
Settings.Settings.MasterDbPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Update database
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Dispatcher.UIThread.Post(CheckGdprCompliance);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-10 22:33:27 +01:00
|
|
|
|
async void CheckGdprCompliance()
|
2020-04-09 04:18:29 +01:00
|
|
|
|
{
|
|
|
|
|
|
CurrentProgress++;
|
|
|
|
|
|
Message = "Checking GDPR compliance...";
|
2020-04-11 03:05:21 +01:00
|
|
|
|
AaruConsole.WriteLine("Checking GDPR compliance...");
|
2020-04-09 04:18:29 +01:00
|
|
|
|
|
2020-04-10 22:33:27 +01:00
|
|
|
|
if(Settings.Settings.Current.GdprCompliance < DicSettings.GdprLevel)
|
2020-04-09 04:18:29 +01:00
|
|
|
|
{
|
2020-04-10 22:33:27 +01:00
|
|
|
|
var settingsDialog = new SettingsDialog();
|
|
|
|
|
|
var settingsDialogViewModel = new SettingsDialogViewModel(settingsDialog, true);
|
|
|
|
|
|
settingsDialog.DataContext = settingsDialogViewModel;
|
|
|
|
|
|
await settingsDialog.ShowDialog(_view);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LoadStatistics();
|
2020-04-09 04:18:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LoadStatistics()
|
|
|
|
|
|
{
|
|
|
|
|
|
CurrentProgress++;
|
|
|
|
|
|
Message = "Loading statistics...";
|
2020-04-11 03:05:21 +01:00
|
|
|
|
AaruConsole.WriteLine("Loading statistics...");
|
2020-04-09 04:18:29 +01:00
|
|
|
|
|
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Statistics.LoadStats();
|
|
|
|
|
|
|
|
|
|
|
|
Dispatcher.UIThread.Post(RegisterEncodings);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RegisterEncodings()
|
|
|
|
|
|
{
|
|
|
|
|
|
CurrentProgress++;
|
|
|
|
|
|
Message = "Registering encodings...";
|
2020-04-11 03:05:21 +01:00
|
|
|
|
AaruConsole.WriteLine("Registering encodings...");
|
2020-04-09 04:18:29 +01:00
|
|
|
|
|
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
|
|
|
|
|
|
|
|
|
|
|
Dispatcher.UIThread.Post(SaveStatistics);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SaveStatistics()
|
|
|
|
|
|
{
|
|
|
|
|
|
CurrentProgress++;
|
|
|
|
|
|
Message = "Saving statistics...";
|
2020-04-11 03:05:21 +01:00
|
|
|
|
AaruConsole.WriteLine("Saving statistics...");
|
2020-04-09 04:18:29 +01:00
|
|
|
|
|
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Statistics.SaveStats();
|
|
|
|
|
|
|
|
|
|
|
|
Dispatcher.UIThread.Post(LoadMainWindow);
|
|
|
|
|
|
});
|
2020-04-09 03:28:08 +01:00
|
|
|
|
}
|
2020-04-09 04:18:29 +01:00
|
|
|
|
|
|
|
|
|
|
void LoadMainWindow()
|
|
|
|
|
|
{
|
|
|
|
|
|
CurrentProgress++;
|
|
|
|
|
|
Message = "Loading main window...";
|
2020-04-11 03:05:21 +01:00
|
|
|
|
AaruConsole.WriteLine("Loading main window...");
|
2020-04-09 04:18:29 +01:00
|
|
|
|
WorkFinished?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal event EventHandler WorkFinished;
|
2020-04-09 03:28:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|