Files
romrepomgr/RomRepoMgr/ViewModels/SettingsViewModel.cs

360 lines
13 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.Collections.Generic;
2020-08-22 21:19:34 +01:00
using System.IO;
using System.Linq;
2020-08-22 21:19:34 +01:00
using System.Threading.Tasks;
2025-07-24 11:11:27 +01:00
using System.Windows.Input;
using Avalonia.Platform.Storage;
2020-08-22 21:19:34 +01:00
using Avalonia.Threading;
2025-07-24 11:11:27 +01:00
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
2020-08-22 21:19:34 +01:00
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 RomRepoMgr.Core.EventArgs;
using RomRepoMgr.Core.Workers;
using RomRepoMgr.Database;
2020-08-30 03:00:14 +01:00
using RomRepoMgr.Resources;
using RomRepoMgr.Settings;
2020-08-22 21:19:34 +01:00
using RomRepoMgr.Views;
using Serilog;
using Serilog.Extensions.Logging;
2020-08-22 21:19:34 +01:00
using ErrorEventArgs = RomRepoMgr.Core.EventArgs.ErrorEventArgs;
2024-11-09 01:37:59 +00:00
namespace RomRepoMgr.ViewModels;
2025-07-24 11:11:27 +01:00
public sealed partial class SettingsViewModel : ViewModelBase
2020-08-22 21:19:34 +01:00
{
2024-11-09 01:37:59 +00:00
readonly SettingsDialog _view;
CompressionType _compression;
bool _compressionChanged;
bool _databaseChanged;
string _databasePath;
bool _repositoryChanged;
string _repositoryPath;
bool _temporaryChanged;
string _temporaryPath;
bool _unArChanged;
2025-07-24 11:11:27 +01:00
[ObservableProperty]
string _unArPath;
[ObservableProperty]
string _unArVersion;
2024-11-09 01:37:59 +00:00
// Mock
public SettingsViewModel() {}
2024-11-09 01:37:59 +00:00
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;
2025-07-24 11:11:27 +01:00
CloseCommand = new RelayCommand(ExecuteCloseCommand);
UnArCommand = new AsyncRelayCommand(ExecuteUnArCommandAsync);
TemporaryCommand = new AsyncRelayCommand(ExecuteTemporaryCommandAsync);
RepositoryCommand = new AsyncRelayCommand(ExecuteRepositoryCommandAsync);
DatabaseCommand = new AsyncRelayCommand(ExecuteDatabaseCommandAsync);
SaveCommand = new RelayCommand(ExecuteSaveCommand);
2024-11-09 01:37:59 +00:00
DatabasePath = Settings.Settings.Current.DatabasePath;
RepositoryPath = Settings.Settings.Current.RepositoryPath;
TemporaryPath = Settings.Settings.Current.TemporaryFolder;
UnArPath = Settings.Settings.Current.UnArchiverPath;
Compression = Settings.Settings.Current.Compression;
2024-11-09 01:37:59 +00:00
if(!string.IsNullOrWhiteSpace(UnArPath)) CheckUnAr();
}
2020-08-22 21:19:34 +01:00
public List<CompressionType> CompressionTypes { get; } =
Enum.GetValues(typeof(CompressionType)).Cast<CompressionType>().ToList();
2025-07-24 11:11:27 +01:00
public ICommand UnArCommand { get; }
public ICommand TemporaryCommand { get; }
public ICommand RepositoryCommand { get; }
public ICommand DatabaseCommand { get; }
public ICommand CloseCommand { get; }
public ICommand SaveCommand { get; }
2024-11-09 01:37:59 +00:00
public string DatabasePath
{
get => _databasePath;
set
2020-08-22 21:19:34 +01:00
{
2025-07-24 11:11:27 +01:00
SetProperty(ref _databasePath, value);
2024-11-09 01:37:59 +00:00
_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
{
2025-07-24 11:11:27 +01:00
SetProperty(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
{
2025-07-24 11:11:27 +01:00
SetProperty(ref _temporaryPath, value);
2024-11-09 01:37:59 +00:00
_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
public CompressionType Compression
{
get => _compression;
set
{
SetProperty(ref _compression, value);
_compressionChanged = true;
}
}
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
{
Dispatcher.UIThread.Post(() =>
{
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
{
IReadOnlyList<IStorageFile> result = await _view.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
2020-08-22 21:19:34 +01:00
{
2024-11-09 01:37:59 +00:00
Title = Localization.ChooseUnArExecutable,
AllowMultiple = false,
SuggestedStartLocation =
!string.IsNullOrWhiteSpace(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles))
? await _view.StorageProvider.TryGetFolderFromPathAsync(Environment.GetFolderPath(Environment
.SpecialFolder.ProgramFiles))
: await _view.StorageProvider.TryGetWellKnownFolderAsync(WellKnownFolder.Desktop)
});
2020-08-22 21:19:34 +01:00
if(result.Count != 1) return;
2020-08-22 21:19:34 +01:00
UnArPath = result[0].Path.LocalPath;
2024-11-09 01:37:59 +00:00
CheckUnAr();
}
2020-08-22 21:19:34 +01:00
async Task ExecuteTemporaryCommandAsync()
2024-11-09 01:37:59 +00:00
{
IReadOnlyList<IStorageFolder> result =
await _view.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
{
Title = Localization.ChooseTemporaryFolder
});
2020-08-22 21:19:34 +01:00
if(result.Count < 1) return;
2020-08-22 21:19:34 +01:00
TemporaryPath = result[0].Path.LocalPath;
2024-11-09 01:37:59 +00:00
}
2020-08-22 21:19:34 +01:00
async Task ExecuteRepositoryCommandAsync()
2024-11-09 01:37:59 +00:00
{
IReadOnlyList<IStorageFolder> result =
await _view.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
{
Title = Localization.ChooseRepositoryFolder,
AllowMultiple = false
});
2020-08-22 21:19:34 +01:00
if(result.Count < 1) return;
2020-08-22 21:19:34 +01:00
RepositoryPath = result[0].Path.LocalPath;
2024-11-09 01:37:59 +00:00
}
2020-08-22 21:19:34 +01:00
async Task ExecuteDatabaseCommandAsync()
2024-11-09 01:37:59 +00:00
{
IStorageFile resultFile = await _view.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
2020-08-22 21:19:34 +01:00
{
SuggestedFileName = "romrepo.db",
SuggestedStartLocation = await _view.StorageProvider.TryGetWellKnownFolderAsync(WellKnownFolder.Documents),
Title = Localization.ChooseDatabaseFile,
ShowOverwritePrompt = true
});
2020-08-22 21:19:34 +01:00
if(resultFile == null) return;
2020-08-22 21:19:34 +01:00
string result = resultFile.Path.LocalPath;
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
{
var ctx = Context.Create(result, new SerilogLoggerFactory(Log.Logger));
2024-11-09 01:37:59 +00:00
await ctx.Database.MigrateAsync();
2020-08-22 21:19:34 +01:00
}
2025-07-24 16:20:22 +01:00
catch
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);
}
2025-07-24 16:20:22 +01:00
catch
2020-08-22 21:19:34 +01:00
{
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
2025-07-24 16:20:22 +01:00
#pragma warning disable ERP022
2020-08-22 21:19:34 +01:00
return;
2025-07-24 16:20:22 +01:00
#pragma warning restore ERP022
2020-08-22 21:19:34 +01:00
}
2025-07-24 16:20:22 +01:00
#pragma warning disable ERP022
2020-08-22 21:19:34 +01:00
}
2025-07-24 16:20:22 +01:00
#pragma warning restore ERP022
2020-08-22 21:19:34 +01:00
}
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);
}
2025-07-24 16:20:22 +01:00
catch
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);
2024-11-09 01:37:59 +00:00
2025-07-24 16:20:22 +01:00
#pragma warning disable ERP022
2024-11-09 01:37:59 +00:00
return;
2025-07-24 16:20:22 +01:00
#pragma warning restore ERP022
2024-11-09 01:37:59 +00:00
}
}
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
{
var ctx = Context.Create(result, new SerilogLoggerFactory(Log.Logger));
2024-11-09 01:37:59 +00:00
await ctx.Database.MigrateAsync();
}
2025-07-24 16:20:22 +01:00
catch
2024-11-09 01:37:59 +00:00
{
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
2025-07-24 16:20:22 +01:00
#pragma warning disable ERP022
2024-11-09 01:37:59 +00:00
return;
2025-07-24 16:20:22 +01:00
#pragma warning restore ERP022
2024-11-09 01:37:59 +00:00
}
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(_compressionChanged) Settings.Settings.Current.Compression = Compression;
if(_databaseChanged || _repositoryChanged || _temporaryChanged || _unArChanged || _compressionChanged)
2024-11-09 01:37:59 +00:00
Settings.Settings.SaveSettings();
_view.Close();
2020-08-22 21:19:34 +01:00
}
}