2022-12-02 13:16:43 +00:00
|
|
|
using Avalonia;
|
2022-07-11 22:25:33 +00:00
|
|
|
using Avalonia.Threading;
|
2022-12-02 13:16:43 +00:00
|
|
|
using FluentAvalonia.UI.Controls;
|
|
|
|
using LibHac.Common;
|
|
|
|
using LibHac.Fs;
|
|
|
|
using LibHac.Fs.Shim;
|
2022-07-08 18:47:11 +00:00
|
|
|
using Ryujinx.Ava.Common.Locale;
|
|
|
|
using Ryujinx.Ava.Ui.Controls;
|
|
|
|
using Ryujinx.HLE.HOS.Services.Account.Acc;
|
|
|
|
using System;
|
2022-12-02 13:16:43 +00:00
|
|
|
using System.Collections.Generic;
|
2022-07-08 18:47:11 +00:00
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
using System.Linq;
|
|
|
|
using UserProfile = Ryujinx.Ava.Ui.Models.UserProfile;
|
|
|
|
|
|
|
|
namespace Ryujinx.Ava.Ui.ViewModels
|
|
|
|
{
|
|
|
|
public class UserProfileViewModel : BaseModel, IDisposable
|
|
|
|
{
|
2022-07-24 17:38:38 +00:00
|
|
|
private readonly NavigationDialogHost _owner;
|
2022-07-08 18:47:11 +00:00
|
|
|
|
|
|
|
private UserProfile _selectedProfile;
|
2022-07-24 17:38:38 +00:00
|
|
|
private UserProfile _highlightedProfile;
|
2022-07-08 18:47:11 +00:00
|
|
|
|
|
|
|
public UserProfileViewModel()
|
|
|
|
{
|
|
|
|
Profiles = new ObservableCollection<UserProfile>();
|
2022-12-02 13:16:43 +00:00
|
|
|
LostProfiles = new ObservableCollection<UserProfile>();
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
|
2022-07-24 17:38:38 +00:00
|
|
|
public UserProfileViewModel(NavigationDialogHost owner) : this()
|
2022-07-08 18:47:11 +00:00
|
|
|
{
|
|
|
|
_owner = owner;
|
|
|
|
|
|
|
|
LoadProfiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
public ObservableCollection<UserProfile> Profiles { get; set; }
|
|
|
|
|
2022-12-02 13:16:43 +00:00
|
|
|
public ObservableCollection<UserProfile> LostProfiles { get; set; }
|
|
|
|
|
2022-07-08 18:47:11 +00:00
|
|
|
public UserProfile SelectedProfile
|
|
|
|
{
|
|
|
|
get => _selectedProfile;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_selectedProfile = value;
|
|
|
|
|
|
|
|
OnPropertyChanged(nameof(SelectedProfile));
|
2022-07-24 17:38:38 +00:00
|
|
|
OnPropertyChanged(nameof(IsHighlightedProfileDeletable));
|
|
|
|
OnPropertyChanged(nameof(IsHighlightedProfileEditable));
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 17:16:23 +00:00
|
|
|
public bool IsHighlightedProfileEditable => _highlightedProfile != null;
|
2022-07-24 17:38:38 +00:00
|
|
|
|
2022-07-28 17:16:23 +00:00
|
|
|
public bool IsHighlightedProfileDeletable => _highlightedProfile != null && _highlightedProfile.UserId != AccountManager.DefaultUserId;
|
2022-07-24 17:38:38 +00:00
|
|
|
|
|
|
|
public UserProfile HighlightedProfile
|
|
|
|
{
|
|
|
|
get => _highlightedProfile;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_highlightedProfile = value;
|
|
|
|
|
|
|
|
OnPropertyChanged(nameof(HighlightedProfile));
|
|
|
|
OnPropertyChanged(nameof(IsHighlightedProfileDeletable));
|
|
|
|
OnPropertyChanged(nameof(IsHighlightedProfileEditable));
|
|
|
|
}
|
|
|
|
}
|
2022-07-08 18:47:11 +00:00
|
|
|
|
2022-07-28 17:16:23 +00:00
|
|
|
public void Dispose() { }
|
2022-07-08 18:47:11 +00:00
|
|
|
|
|
|
|
public void LoadProfiles()
|
|
|
|
{
|
|
|
|
Profiles.Clear();
|
2022-12-02 13:16:43 +00:00
|
|
|
LostProfiles.Clear();
|
2022-07-08 18:47:11 +00:00
|
|
|
|
2022-07-28 17:16:23 +00:00
|
|
|
var profiles = _owner.AccountManager.GetAllUsers().OrderByDescending(x => x.AccountState == AccountState.Open);
|
2022-07-08 18:47:11 +00:00
|
|
|
|
|
|
|
foreach (var profile in profiles)
|
|
|
|
{
|
2022-12-02 13:16:43 +00:00
|
|
|
Profiles.Add(new UserProfile(profile, _owner));
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SelectedProfile = Profiles.FirstOrDefault(x => x.UserId == _owner.AccountManager.LastOpenedUser.UserId);
|
|
|
|
|
|
|
|
if (SelectedProfile == null)
|
|
|
|
{
|
|
|
|
SelectedProfile = Profiles.First();
|
|
|
|
|
|
|
|
if (SelectedProfile != null)
|
|
|
|
{
|
|
|
|
_owner.AccountManager.OpenUser(_selectedProfile.UserId);
|
|
|
|
}
|
|
|
|
}
|
2022-12-02 13:16:43 +00:00
|
|
|
|
|
|
|
var saveDataFilter = SaveDataFilter.Make(programId: default, saveType: SaveDataType.Account,
|
|
|
|
default, saveDataId: default, index: default);
|
|
|
|
|
|
|
|
using var saveDataIterator = new UniqueRef<SaveDataIterator>();
|
|
|
|
|
|
|
|
_owner.HorizonClient.Fs.OpenSaveDataIterator(ref saveDataIterator.Ref(), SaveDataSpaceId.User, in saveDataFilter).ThrowIfFailure();
|
|
|
|
|
|
|
|
Span<SaveDataInfo> saveDataInfo = stackalloc SaveDataInfo[10];
|
|
|
|
|
|
|
|
HashSet<HLE.HOS.Services.Account.Acc.UserId> lostAccounts = new HashSet<HLE.HOS.Services.Account.Acc.UserId>();
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
saveDataIterator.Get.ReadSaveDataInfo(out long readCount, saveDataInfo).ThrowIfFailure();
|
|
|
|
|
|
|
|
if (readCount == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < readCount; i++)
|
|
|
|
{
|
|
|
|
var save = saveDataInfo[i];
|
|
|
|
var id = new HLE.HOS.Services.Account.Acc.UserId((long)save.UserId.Id.Low, (long)save.UserId.Id.High);
|
|
|
|
if (Profiles.FirstOrDefault( x=> x.UserId == id) == null)
|
|
|
|
{
|
|
|
|
lostAccounts.Add(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(var account in lostAccounts)
|
|
|
|
{
|
|
|
|
LostProfiles.Add(new UserProfile(new HLE.HOS.Services.Account.Acc.UserProfile(account, "", null), _owner));
|
|
|
|
}
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
|
2022-07-24 17:38:38 +00:00
|
|
|
public void AddUser()
|
2022-07-08 18:47:11 +00:00
|
|
|
{
|
2022-07-24 17:38:38 +00:00
|
|
|
UserProfile userProfile = null;
|
2022-07-28 17:16:23 +00:00
|
|
|
|
2022-07-24 17:38:38 +00:00
|
|
|
_owner.Navigate(typeof(UserEditor), (this._owner, userProfile, true));
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 13:16:43 +00:00
|
|
|
public async void ManageSaves()
|
|
|
|
{
|
|
|
|
UserProfile userProfile = _highlightedProfile ?? SelectedProfile;
|
|
|
|
|
|
|
|
SaveManager manager = new SaveManager(userProfile, _owner.HorizonClient, _owner.VirtualFileSystem);
|
|
|
|
|
|
|
|
ContentDialog contentDialog = new ContentDialog
|
|
|
|
{
|
|
|
|
Title = string.Format(LocaleManager.Instance["SaveManagerHeading"], userProfile.Name),
|
|
|
|
PrimaryButtonText = "",
|
|
|
|
SecondaryButtonText = "",
|
|
|
|
CloseButtonText = LocaleManager.Instance["UserProfilesClose"],
|
|
|
|
Content = manager,
|
|
|
|
Padding = new Thickness(0)
|
|
|
|
};
|
|
|
|
|
|
|
|
await contentDialog.ShowAsync();
|
|
|
|
}
|
|
|
|
|
2022-07-24 17:38:38 +00:00
|
|
|
public void EditUser()
|
2022-07-08 18:47:11 +00:00
|
|
|
{
|
2022-07-24 17:38:38 +00:00
|
|
|
_owner.Navigate(typeof(UserEditor), (this._owner, _highlightedProfile ?? SelectedProfile, false));
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async void DeleteUser()
|
|
|
|
{
|
2022-07-24 17:38:38 +00:00
|
|
|
if (_highlightedProfile != null)
|
2022-07-08 18:47:11 +00:00
|
|
|
{
|
|
|
|
var lastUserId = _owner.AccountManager.LastOpenedUser.UserId;
|
|
|
|
|
2022-07-24 17:38:38 +00:00
|
|
|
if (_highlightedProfile.UserId == lastUserId)
|
2022-07-08 18:47:11 +00:00
|
|
|
{
|
|
|
|
// If we are deleting the currently open profile, then we must open something else before deleting.
|
|
|
|
var profile = Profiles.FirstOrDefault(x => x.UserId != lastUserId);
|
|
|
|
|
|
|
|
if (profile == null)
|
|
|
|
{
|
2022-07-11 22:25:33 +00:00
|
|
|
Dispatcher.UIThread.Post(async () =>
|
|
|
|
{
|
2022-07-24 17:38:38 +00:00
|
|
|
await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance["DialogUserProfileDeletionWarningMessage"]);
|
2022-07-11 22:25:33 +00:00
|
|
|
});
|
|
|
|
|
2022-07-08 18:47:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_owner.AccountManager.OpenUser(profile.UserId);
|
|
|
|
}
|
|
|
|
|
|
|
|
var result =
|
2022-07-24 17:38:38 +00:00
|
|
|
await ContentDialogHelper.CreateConfirmationDialog(LocaleManager.Instance["DialogUserProfileDeletionConfirmMessage"], "",
|
2022-07-08 18:47:11 +00:00
|
|
|
LocaleManager.Instance["InputDialogYes"], LocaleManager.Instance["InputDialogNo"], "");
|
|
|
|
|
|
|
|
if (result == UserResult.Yes)
|
|
|
|
{
|
2022-07-24 17:38:38 +00:00
|
|
|
_owner.AccountManager.DeleteUser(_highlightedProfile.UserId);
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadProfiles();
|
|
|
|
}
|
2022-12-02 13:16:43 +00:00
|
|
|
|
|
|
|
public void GoBack()
|
|
|
|
{
|
|
|
|
_owner.GoBack();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RecoverLostAccounts()
|
|
|
|
{
|
|
|
|
_owner.Navigate(typeof(UserRecoverer), (this._owner, this));
|
|
|
|
}
|
2022-07-08 18:47:11 +00:00
|
|
|
}
|
|
|
|
}
|