Files
Aaru/Aaru.Gui/ViewModels/Windows/SplashWindowViewModel.cs

288 lines
8.6 KiB
C#
Raw Normal View History

2020-04-17 21:45:50 +01:00
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : SplashWindowViewModel.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : GUI view models.
//
// --[ Description ] ----------------------------------------------------------
//
// View model and code for the splash window.
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General public License for more details.
//
// You should have received a copy of the GNU General public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
2020-04-17 21:45:50 +01:00
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
namespace Aaru.Gui.ViewModels.Windows;
2020-04-17 21:45:50 +01:00
using System;
2020-04-09 04:18:29 +01:00
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aaru.Console;
using Aaru.Core;
using Aaru.Database;
2020-04-16 20:40:25 +01:00
using Aaru.Gui.ViewModels.Dialogs;
using Aaru.Gui.Views.Dialogs;
using Aaru.Gui.Views.Windows;
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;
2022-03-06 13:29:38 +00:00
public sealed class SplashWindowViewModel : ViewModelBase
2020-04-09 03:28:08 +01:00
{
2022-03-06 13:29:38 +00:00
readonly SplashWindow _view;
double _currentProgress;
double _maxProgress;
string _message;
2022-03-06 13:29:38 +00:00
public SplashWindowViewModel(SplashWindow view) => _view = view;
2020-04-09 03:28:08 +01:00
2022-03-06 13:29:38 +00:00
public string Message
{
get => _message;
set => this.RaiseAndSetIfChanged(ref _message, value);
}
2020-04-09 03:28:08 +01:00
2022-03-06 13:29:38 +00:00
public double MaxProgress
{
get => _maxProgress;
set => this.RaiseAndSetIfChanged(ref _maxProgress, value);
}
2020-04-09 03:28:08 +01:00
2022-03-06 13:29:38 +00:00
public double CurrentProgress
{
get => _currentProgress;
set => this.RaiseAndSetIfChanged(ref _currentProgress, value);
}
2020-04-09 03:28:08 +01:00
2022-03-06 13:29:38 +00:00
internal void OnOpened()
{
Message = "Welcome to Aaru!";
MaxProgress = 9;
CurrentProgress = 0;
2020-04-09 03:28:08 +01:00
2022-03-06 13:29:38 +00:00
Dispatcher.UIThread.Post(InitializeConsole);
}
void InitializeConsole()
{
CurrentProgress++;
Message = "Initializing console...";
2020-04-09 03:28:08 +01:00
2022-03-06 13:29:38 +00:00
Task.Run(() =>
2020-04-09 03:28:08 +01:00
{
2022-03-06 13:29:38 +00:00
ConsoleHandler.Init();
AaruConsole.WriteLine("Aaru started!");
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
Dispatcher.UIThread.Post(LoadSettings);
});
}
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
void LoadSettings()
{
CurrentProgress++;
Message = "Loading settings...";
AaruConsole.WriteLine("Loading settings...");
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
Task.Run(() =>
2020-04-09 04:18:29 +01:00
{
2022-03-06 13:29:38 +00:00
// TODO: Detect there are no settings yet
2022-03-07 07:36:44 +00:00
Settings.LoadSettings();
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
Dispatcher.UIThread.Post(MigrateLocalDatabase);
});
}
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
void MigrateLocalDatabase()
{
CurrentProgress++;
Message = "Migrating local database...";
AaruConsole.WriteLine("Migrating local database...");
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
Task.Run(() =>
2020-04-09 04:18:29 +01:00
{
2022-03-06 13:29:38 +00:00
AaruContext ctx = null;
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
try
{
2022-03-07 07:36:44 +00:00
ctx = AaruContext.Create(Settings.LocalDbPath, false);
2022-03-06 13:29:38 +00:00
ctx.Database.Migrate();
}
catch(NotSupportedException)
2020-04-09 04:18:29 +01:00
{
2020-07-10 17:38:52 +01:00
try
{
2022-03-06 13:29:38 +00:00
ctx?.Database.CloseConnection();
ctx?.Dispose();
2020-07-10 17:38:52 +01:00
}
2022-03-06 13:29:38 +00:00
catch(Exception)
2020-07-10 17:38:52 +01:00
{
2022-03-06 13:29:38 +00:00
// Should not ever arrive here, but if it does, keep trying to replace it anyway
}
2020-07-10 17:38:52 +01:00
2022-03-07 07:36:44 +00:00
File.Delete(Settings.LocalDbPath);
ctx = AaruContext.Create(Settings.LocalDbPath);
2022-03-06 13:29:38 +00:00
ctx.Database.EnsureCreated();
2020-07-10 17:38:52 +01:00
2022-03-06 13:29:38 +00:00
ctx.Database.
ExecuteSqlRaw("CREATE TABLE IF NOT EXISTS \"__EFMigrationsHistory\" (\"MigrationId\" TEXT PRIMARY KEY, \"ProductVersion\" TEXT)");
2022-03-06 13:29:38 +00:00
foreach(string migration in ctx.Database.GetPendingMigrations())
ctx.Database.
ExecuteSqlRaw($"INSERT INTO \"__EFMigrationsHistory\" (MigrationId, ProductVersion) VALUES ('{migration}', '0.0.0')");
2020-04-09 04:18:29 +01:00
ctx.SaveChanges();
2022-03-06 13:29:38 +00:00
}
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
// Remove duplicates
foreach(var duplicate in ctx.SeenDevices.AsEnumerable()!.GroupBy(a => new
{
a.Manufacturer,
a.Model,
a.Revision,
a.Bus
}).Where(a => a.Count() > 1).Distinct().Select(a => a.Key))
ctx.RemoveRange(ctx.SeenDevices!.
2022-03-07 07:36:44 +00:00
Where(d => d.Manufacturer == duplicate.Manufacturer && d.Model == duplicate.Model &&
d.Revision == duplicate.Revision && d.Bus == duplicate.Bus).Skip(1));
2022-03-06 13:29:38 +00:00
// Remove nulls
ctx.RemoveRange(ctx.SeenDevices!.Where(d => d.Manufacturer == null && d.Model == null &&
d.Revision == null));
ctx.SaveChanges();
Dispatcher.UIThread.Post(UpdateMainDatabase);
});
}
void UpdateMainDatabase()
{
CurrentProgress++;
Message = "Updating main database...";
AaruConsole.WriteLine("Updating main database...");
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
Task.Run(() =>
2020-04-09 04:18:29 +01:00
{
2022-03-07 07:36:44 +00:00
var mainDbUpdate = false;
2020-04-09 04:18:29 +01:00
2022-03-07 07:36:44 +00:00
if(!File.Exists(Settings.MainDbPath))
2022-03-06 13:29:38 +00:00
mainDbUpdate = true;
2020-04-09 04:18:29 +01:00
2022-03-07 07:36:44 +00:00
// TODO: Update database
2020-04-09 04:18:29 +01:00
2022-03-07 07:36:44 +00:00
var mainContext = AaruContext.Create(Settings.MainDbPath, false);
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
if(mainContext.Database.GetPendingMigrations().Any())
{
AaruConsole.WriteLine("New database version, updating...");
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
try
2020-04-09 04:18:29 +01:00
{
2022-03-07 07:36:44 +00:00
File.Delete(Settings.MainDbPath);
2020-04-09 04:18:29 +01:00
}
2022-03-06 13:29:38 +00:00
catch(Exception)
{
2022-03-07 07:36:44 +00:00
AaruConsole.ErrorWriteLine("Exception trying to remove old database version, cannot continue...");
2020-04-09 04:18:29 +01:00
2022-03-07 07:36:44 +00:00
AaruConsole.ErrorWriteLine("Please manually remove file at {0}", Settings.MainDbPath);
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
return;
}
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
// TODO: Update database
}
2022-03-06 13:29:38 +00:00
Dispatcher.UIThread.Post(CheckGdprCompliance);
});
}
async void CheckGdprCompliance()
{
CurrentProgress++;
Message = "Checking GDPR compliance...";
AaruConsole.WriteLine("Checking GDPR compliance...");
2020-04-09 04:18:29 +01:00
2022-03-07 07:36:44 +00:00
if(Settings.Current.GdprCompliance < DicSettings.GDPR_LEVEL)
2020-04-09 04:18:29 +01:00
{
2022-03-06 13:29:38 +00:00
var settingsDialog = new SettingsDialog();
var settingsDialogViewModel = new SettingsViewModel(settingsDialog, true);
settingsDialog.DataContext = settingsDialogViewModel;
await settingsDialog.ShowDialog(_view);
}
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
LoadStatistics();
}
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
void LoadStatistics()
{
CurrentProgress++;
Message = "Loading statistics...";
AaruConsole.WriteLine("Loading statistics...");
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
Task.Run(() =>
2020-04-09 04:18:29 +01:00
{
2022-03-06 13:29:38 +00:00
Statistics.LoadStats();
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
Dispatcher.UIThread.Post(RegisterEncodings);
});
}
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
void RegisterEncodings()
{
CurrentProgress++;
Message = "Registering encodings...";
AaruConsole.WriteLine("Registering encodings...");
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
Task.Run(() =>
2020-04-09 04:18:29 +01:00
{
2022-03-06 13:29:38 +00:00
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
Dispatcher.UIThread.Post(SaveStatistics);
});
}
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
void SaveStatistics()
{
CurrentProgress++;
Message = "Saving statistics...";
AaruConsole.WriteLine("Saving statistics...");
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
Task.Run(() =>
2020-04-09 04:18:29 +01:00
{
2022-03-06 13:29:38 +00:00
Statistics.SaveStats();
2020-04-09 04:18:29 +01:00
2022-03-06 13:29:38 +00:00
Dispatcher.UIThread.Post(LoadMainWindow);
});
2020-04-09 03:28:08 +01:00
}
2022-03-06 13:29:38 +00:00
void LoadMainWindow()
{
CurrentProgress++;
Message = "Loading main window...";
AaruConsole.WriteLine("Loading main window...");
WorkFinished?.Invoke(this, EventArgs.Empty);
}
internal event EventHandler WorkFinished;
2020-04-09 03:28:08 +01:00
}