Files
romrepomgr/RomRepoMgr/ViewModels/SettingsViewModel.cs

341 lines
12 KiB
C#
Raw Normal View History

2020-08-22 21:19:34 +01:00
/******************************************************************************
// RomRepoMgr - ROM repository manager
// ----------------------------------------------------------------------------
//
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
2024-11-08 19:13:57 +00:00
// Copyright © 2017-2024 Natalia Portillo
2020-08-22 21:19:34 +01:00
*******************************************************************************/
using System;
using System.IO;
using System.Reactive;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Threading;
using Microsoft.EntityFrameworkCore;
2024-11-09 06:21:49 +00:00
using MsBox.Avalonia;
using MsBox.Avalonia.Enums;
2020-08-22 21:19:34 +01:00
using ReactiveUI;
using RomRepoMgr.Core.EventArgs;
using RomRepoMgr.Core.Workers;
using RomRepoMgr.Database;
2020-08-30 03:00:14 +01:00
using RomRepoMgr.Resources;
2020-08-22 21:19:34 +01:00
using RomRepoMgr.Views;
using ErrorEventArgs = RomRepoMgr.Core.EventArgs.ErrorEventArgs;
2024-11-09 01:37:59 +00:00
namespace RomRepoMgr.ViewModels;
public sealed class SettingsViewModel : ViewModelBase
2020-08-22 21:19:34 +01:00
{
2024-11-09 01:37:59 +00:00
readonly SettingsDialog _view;
bool _databaseChanged;
string _databasePath;
bool _repositoryChanged;
string _repositoryPath;
bool _temporaryChanged;
string _temporaryPath;
bool _unArChanged;
string _unArPath;
string _unArVersion;
public SettingsViewModel(SettingsDialog view)
2020-08-22 21:19:34 +01:00
{
2024-11-09 01:37:59 +00:00
_view = view;
_databaseChanged = false;
_repositoryChanged = false;
_temporaryChanged = false;
_unArChanged = false;
CloseCommand = ReactiveCommand.Create(ExecuteCloseCommand);
UnArCommand = ReactiveCommand.CreateFromTask(ExecuteUnArCommandAsync);
TemporaryCommand = ReactiveCommand.CreateFromTask(ExecuteTemporaryCommandAsync);
RepositoryCommand = ReactiveCommand.CreateFromTask(ExecuteRepositoryCommandAsync);
DatabaseCommand = ReactiveCommand.CreateFromTask(ExecuteDatabaseCommandAsync);
2024-11-09 01:37:59 +00:00
SaveCommand = ReactiveCommand.Create(ExecuteSaveCommand);
DatabasePath = Settings.Settings.Current.DatabasePath;
RepositoryPath = Settings.Settings.Current.RepositoryPath;
TemporaryPath = Settings.Settings.Current.TemporaryFolder;
UnArPath = Settings.Settings.Current.UnArchiverPath;
if(!string.IsNullOrWhiteSpace(UnArPath)) CheckUnAr();
}
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
public string ChooseLabel => Localization.ChooseLabel;
public string Title => Localization.SettingsTitle;
public string CloseLabel => Localization.CloseLabel;
public string DatabaseLabel => Localization.DatabaseFileLabel;
public string RepositoryLabel => Localization.RepositoryFolderLabel;
public string TemporaryLabel => Localization.TemporaryFolderLabel;
public string UnArPathLabel => Localization.UnArPathLabel;
public ReactiveCommand<Unit, Unit> UnArCommand { get; }
public ReactiveCommand<Unit, Unit> TemporaryCommand { get; }
public ReactiveCommand<Unit, Unit> RepositoryCommand { get; }
public ReactiveCommand<Unit, Unit> DatabaseCommand { get; }
public ReactiveCommand<Unit, Unit> CloseCommand { get; }
public ReactiveCommand<Unit, Unit> SaveCommand { get; }
public string DatabasePath
{
get => _databasePath;
set
2020-08-22 21:19:34 +01:00
{
2024-11-09 01:37:59 +00:00
this.RaiseAndSetIfChanged(ref _databasePath, value);
_databaseChanged = true;
2020-08-22 21:19:34 +01:00
}
2024-11-09 01:37:59 +00:00
}
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
public string RepositoryPath
{
get => _repositoryPath;
set
2020-08-22 21:19:34 +01:00
{
2024-11-09 01:37:59 +00:00
this.RaiseAndSetIfChanged(ref _repositoryPath, value);
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
// TODO: Refresh repository existing files
_repositoryChanged = true;
2020-08-22 21:19:34 +01:00
}
2024-11-09 01:37:59 +00:00
}
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
public string TemporaryPath
{
get => _temporaryPath;
set
2020-08-22 21:19:34 +01:00
{
2024-11-09 01:37:59 +00:00
this.RaiseAndSetIfChanged(ref _temporaryPath, value);
_temporaryChanged = true;
2020-08-22 21:19:34 +01:00
}
2024-11-09 01:37:59 +00:00
}
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
public string UnArPath
{
get => _unArPath;
set => this.RaiseAndSetIfChanged(ref _unArPath, value);
}
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
public string UnArVersion
{
get => _unArVersion;
set => this.RaiseAndSetIfChanged(ref _unArVersion, value);
}
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
public string SaveLabel => Localization.SaveLabel;
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
void CheckUnAr()
{
var worker = new Compression();
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
worker.FinishedWithText += CheckUnArFinished;
worker.FailedWithText += CheckUnArFailed;
2020-08-22 21:19:34 +01:00
_ = Task.Run(() => worker.CheckUnAr(UnArPath));
2024-11-09 01:37:59 +00:00
}
2020-08-22 21:19:34 +01:00
void CheckUnArFailed(object sender, ErrorEventArgs args)
2024-11-09 01:37:59 +00:00
{
UnArVersion = "";
UnArPath = "";
2020-08-22 21:19:34 +01:00
_ = MessageBoxManager.GetMessageBoxStandard(Localization.Error, $"{args.Message}", ButtonEnum.Ok, Icon.Error)
.ShowWindowDialogAsync(_view);
2024-11-09 01:37:59 +00:00
}
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
void CheckUnArFinished(object sender, MessageEventArgs args) => Dispatcher.UIThread.Post(() =>
{
UnArVersion = string.Format(Localization.TheUnarchiverVersionLabel, args.Message);
_unArChanged = true;
});
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
void ExecuteCloseCommand() => _view.Close();
2020-08-22 21:19:34 +01:00
async Task ExecuteUnArCommandAsync()
2024-11-09 01:37:59 +00:00
{
var dlgFile = new OpenFileDialog
2020-08-22 21:19:34 +01:00
{
2024-11-09 01:37:59 +00:00
Title = Localization.ChooseUnArExecutable,
AllowMultiple = false
};
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
if(!string.IsNullOrWhiteSpace(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)))
dlgFile.Directory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
string[] result = await dlgFile.ShowAsync(_view);
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
if(result?.Length != 1) return;
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
UnArPath = result[0];
CheckUnAr();
}
2020-08-22 21:19:34 +01:00
async Task ExecuteTemporaryCommandAsync()
2024-11-09 01:37:59 +00:00
{
var dlgFolder = new OpenFolderDialog
2020-08-22 21:19:34 +01:00
{
2024-11-09 01:37:59 +00:00
Title = Localization.ChooseTemporaryFolder
};
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
string result = await dlgFolder.ShowAsync(_view);
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
if(result == null) return;
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
TemporaryPath = result;
}
2020-08-22 21:19:34 +01:00
async Task ExecuteRepositoryCommandAsync()
2024-11-09 01:37:59 +00:00
{
var dlgFolder = new OpenFolderDialog
2020-08-22 21:19:34 +01:00
{
2024-11-09 01:37:59 +00:00
Title = Localization.ChooseRepositoryFolder
};
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
string result = await dlgFolder.ShowAsync(_view);
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
if(result == null) return;
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
RepositoryPath = result;
}
2020-08-22 21:19:34 +01:00
async Task ExecuteDatabaseCommandAsync()
2024-11-09 01:37:59 +00:00
{
var dlgFile = new SaveFileDialog
2020-08-22 21:19:34 +01:00
{
2024-11-09 01:37:59 +00:00
InitialFileName = "romrepo.db",
Directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
Title = Localization.ChooseDatabaseFile
};
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
string result = await dlgFile.ShowAsync(_view);
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
if(result == null) return;
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
if(File.Exists(result))
{
ButtonResult btnResult = await MessageBoxManager
2024-11-09 06:21:49 +00:00
.GetMessageBoxStandard(Localization.DatabaseFileExistsMsgBoxTitle,
Localization.DatabaseFileTryOpenCaption,
ButtonEnum.YesNo,
Icon.Database)
.ShowWindowDialogAsync(_view);
2024-11-09 01:37:59 +00:00
if(btnResult == ButtonResult.Yes)
2020-08-22 21:19:34 +01:00
{
2024-11-09 01:37:59 +00:00
try
2020-08-22 21:19:34 +01:00
{
2024-11-09 01:37:59 +00:00
var ctx = Context.Create(result);
await ctx.Database.MigrateAsync();
2020-08-22 21:19:34 +01:00
}
2024-11-09 01:37:59 +00:00
catch(Exception)
2020-08-22 21:19:34 +01:00
{
2024-11-09 01:37:59 +00:00
btnResult = await MessageBoxManager
2024-11-09 06:21:49 +00:00
.GetMessageBoxStandard(Localization.DatabaseFileUnusableMsgBoxTitle,
Localization.DatabaseFileUnusableDeleteMsgBoxCaption,
ButtonEnum.YesNo,
Icon.Error)
.ShowWindowDialogAsync(_view);
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
if(btnResult == ButtonResult.No) return;
2020-08-22 21:19:34 +01:00
try
{
File.Delete(result);
}
catch(Exception)
{
2024-11-09 01:37:59 +00:00
await MessageBoxManager
2024-11-09 06:21:49 +00:00
.GetMessageBoxStandard(Localization.DatabaseFileCannotDeleteTitle,
Localization.DatabaseFileCannotDeleteCaption,
ButtonEnum.Ok,
Icon.Error)
.ShowWindowDialogAsync(_view);
2020-08-22 21:19:34 +01:00
return;
}
}
}
2024-11-09 01:37:59 +00:00
else
2020-08-22 21:19:34 +01:00
{
2024-11-09 01:37:59 +00:00
btnResult = await MessageBoxManager
2024-11-09 06:21:49 +00:00
.GetMessageBoxStandard(Localization.DatabaseFileExistsMsgBoxTitle,
Localization.DatabaseFileDeleteCaption,
ButtonEnum.YesNo,
Icon.Error)
.ShowWindowDialogAsync(_view);
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
if(btnResult == ButtonResult.No) return;
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
try
{
File.Delete(result);
}
catch(Exception)
{
await MessageBoxManager
2024-11-09 06:21:49 +00:00
.GetMessageBoxStandard(Localization.DatabaseFileCannotDeleteTitle,
Localization.DatabaseFileCannotDeleteCaption,
ButtonEnum.Ok,
Icon.Error)
.ShowWindowDialogAsync(_view);
2024-11-09 01:37:59 +00:00
return;
}
}
2020-08-22 21:19:34 +01:00
}
2024-11-09 01:37:59 +00:00
try
2020-08-22 21:19:34 +01:00
{
2024-11-09 01:37:59 +00:00
var ctx = Context.Create(result);
await ctx.Database.MigrateAsync();
}
catch(Exception)
{
await MessageBoxManager
2024-11-09 06:21:49 +00:00
.GetMessageBoxStandard(Localization.DatabaseFileUnusableMsgBoxTitle,
Localization.DatabaseFileUnusableMsgBoxCaption,
ButtonEnum.Ok,
Icon.Error)
.ShowWindowDialogAsync(_view);
2024-11-09 01:37:59 +00:00
return;
}
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
DatabasePath = result;
}
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
void ExecuteSaveCommand()
{
if(_databaseChanged) Settings.Settings.Current.DatabasePath = DatabasePath;
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
if(_repositoryChanged) Settings.Settings.Current.RepositoryPath = RepositoryPath;
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
if(_temporaryChanged) Settings.Settings.Current.TemporaryFolder = TemporaryPath;
2020-08-22 21:19:34 +01:00
2024-11-09 01:37:59 +00:00
if(_unArChanged)
{
Settings.Settings.Current.UnArchiverPath = UnArPath;
Settings.Settings.UnArUsable = true;
2020-08-22 21:19:34 +01:00
}
2024-11-09 01:37:59 +00:00
if(_databaseChanged || _repositoryChanged || _temporaryChanged || _unArChanged)
Settings.Settings.SaveSettings();
_view.Close();
2020-08-22 21:19:34 +01:00
}
}