UserProfilesManagerWindow.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using Gtk;
  2. using Ryujinx.Common.Memory;
  3. using Ryujinx.HLE.FileSystem;
  4. using Ryujinx.HLE.HOS.Services.Account.Acc;
  5. using Ryujinx.Ui.Common.Configuration;
  6. using Ryujinx.Ui.Widgets;
  7. using SixLabors.ImageSharp;
  8. using SixLabors.ImageSharp.Processing;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Reflection;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using Image = SixLabors.ImageSharp.Image;
  16. using UserId = Ryujinx.HLE.HOS.Services.Account.Acc.UserId;
  17. namespace Ryujinx.Ui.Windows
  18. {
  19. public partial class UserProfilesManagerWindow : Window
  20. {
  21. private const uint MaxProfileNameLength = 0x20;
  22. private readonly AccountManager _accountManager;
  23. private readonly ContentManager _contentManager;
  24. private byte[] _bufferImageProfile;
  25. private string _tempNewProfileName;
  26. private Gdk.RGBA _selectedColor;
  27. private ManualResetEvent _avatarsPreloadingEvent = new ManualResetEvent(false);
  28. public UserProfilesManagerWindow(AccountManager accountManager, ContentManager contentManager, VirtualFileSystem virtualFileSystem) : base($"Ryujinx {Program.Version} - Manage User Profiles")
  29. {
  30. Icon = new Gdk.Pixbuf(Assembly.GetAssembly(typeof(ConfigurationState)), "Ryujinx.Ui.Common.Resources.Logo_Ryujinx.png");
  31. InitializeComponent();
  32. _selectedColor.Red = 0.212;
  33. _selectedColor.Green = 0.843;
  34. _selectedColor.Blue = 0.718;
  35. _selectedColor.Alpha = 1;
  36. _accountManager = accountManager;
  37. _contentManager = contentManager;
  38. CellRendererToggle userSelectedToggle = new CellRendererToggle();
  39. userSelectedToggle.Toggled += UserSelectedToggle_Toggled;
  40. // NOTE: Uncomment following line when multiple selection of user profiles is supported.
  41. //_usersTreeView.AppendColumn("Selected", userSelectedToggle, "active", 0);
  42. _usersTreeView.AppendColumn("User Icon", new CellRendererPixbuf(), "pixbuf", 1);
  43. _usersTreeView.AppendColumn("User Info", new CellRendererText(), "text", 2, "background-rgba", 3);
  44. _tableStore.SetSortColumnId(0, SortType.Descending);
  45. RefreshList();
  46. if (_contentManager.GetCurrentFirmwareVersion() != null)
  47. {
  48. Task.Run(() =>
  49. {
  50. AvatarWindow.PreloadAvatars(contentManager, virtualFileSystem);
  51. _avatarsPreloadingEvent.Set();
  52. });
  53. }
  54. }
  55. public void RefreshList()
  56. {
  57. _tableStore.Clear();
  58. foreach (UserProfile userProfile in _accountManager.GetAllUsers())
  59. {
  60. _tableStore.AppendValues(userProfile.AccountState == AccountState.Open, new Gdk.Pixbuf(userProfile.Image, 96, 96), $"{userProfile.Name}\n{userProfile.UserId}", Gdk.RGBA.Zero);
  61. if (userProfile.AccountState == AccountState.Open)
  62. {
  63. _selectedUserImage.Pixbuf = new Gdk.Pixbuf(userProfile.Image, 96, 96);
  64. _selectedUserIdLabel.Text = userProfile.UserId.ToString();
  65. _selectedUserNameEntry.Text = userProfile.Name;
  66. _deleteButton.Sensitive = userProfile.UserId != AccountManager.DefaultUserId;
  67. _usersTreeView.Model.GetIterFirst(out TreeIter firstIter);
  68. _tableStore.SetValue(firstIter, 3, _selectedColor);
  69. }
  70. }
  71. }
  72. //
  73. // Events
  74. //
  75. private void UsersTreeView_Activated(object o, RowActivatedArgs args)
  76. {
  77. SelectUserTreeView();
  78. }
  79. private void UserSelectedToggle_Toggled(object o, ToggledArgs args)
  80. {
  81. SelectUserTreeView();
  82. }
  83. private void SelectUserTreeView()
  84. {
  85. // Get selected item informations.
  86. _usersTreeView.Selection.GetSelected(out TreeIter selectedIter);
  87. Gdk.Pixbuf userPicture = (Gdk.Pixbuf)_tableStore.GetValue(selectedIter, 1);
  88. string userName = _tableStore.GetValue(selectedIter, 2).ToString().Split("\n")[0];
  89. string userId = _tableStore.GetValue(selectedIter, 2).ToString().Split("\n")[1];
  90. // Unselect the first user.
  91. _usersTreeView.Model.GetIterFirst(out TreeIter firstIter);
  92. _tableStore.SetValue(firstIter, 0, false);
  93. _tableStore.SetValue(firstIter, 3, Gdk.RGBA.Zero);
  94. // Set new informations.
  95. _tableStore.SetValue(selectedIter, 0, true);
  96. _selectedUserImage.Pixbuf = userPicture;
  97. _selectedUserNameEntry.Text = userName;
  98. _selectedUserIdLabel.Text = userId;
  99. _saveProfileNameButton.Sensitive = false;
  100. // Open the selected one.
  101. _accountManager.OpenUser(new UserId(userId));
  102. _deleteButton.Sensitive = userId != AccountManager.DefaultUserId.ToString();
  103. _tableStore.SetValue(selectedIter, 3, _selectedColor);
  104. }
  105. private void SelectedUserNameEntry_KeyReleaseEvent(object o, KeyReleaseEventArgs args)
  106. {
  107. if (_saveProfileNameButton.Sensitive == false)
  108. {
  109. _saveProfileNameButton.Sensitive = true;
  110. }
  111. }
  112. private void AddButton_Pressed(object sender, EventArgs e)
  113. {
  114. _tempNewProfileName = GtkDialog.CreateInputDialog(this, "Choose the Profile Name", "Please Enter a Profile Name", MaxProfileNameLength);
  115. if (_tempNewProfileName != "")
  116. {
  117. SelectProfileImage(true);
  118. if (_bufferImageProfile != null)
  119. {
  120. AddUser();
  121. }
  122. }
  123. }
  124. private void DeleteButton_Pressed(object sender, EventArgs e)
  125. {
  126. if (GtkDialog.CreateChoiceDialog("Delete User Profile", "Are you sure you want to delete the profile ?", "Deleting this profile will also delete all associated save data."))
  127. {
  128. _accountManager.DeleteUser(GetSelectedUserId());
  129. RefreshList();
  130. }
  131. }
  132. private void EditProfileNameButton_Pressed(object sender, EventArgs e)
  133. {
  134. _saveProfileNameButton.Sensitive = false;
  135. _accountManager.SetUserName(GetSelectedUserId(), _selectedUserNameEntry.Text);
  136. RefreshList();
  137. }
  138. private void ProcessProfileImage(byte[] buffer)
  139. {
  140. using (Image image = Image.Load(buffer))
  141. {
  142. image.Mutate(x => x.Resize(256, 256));
  143. using (MemoryStream streamJpg = MemoryStreamManager.Shared.GetStream())
  144. {
  145. image.SaveAsJpeg(streamJpg);
  146. _bufferImageProfile = streamJpg.ToArray();
  147. }
  148. }
  149. }
  150. private void ProfileImageFileChooser()
  151. {
  152. FileChooserNative fileChooser = new FileChooserNative("Import Custom Profile Image", this, FileChooserAction.Open, "Import", "Cancel")
  153. {
  154. SelectMultiple = false
  155. };
  156. FileFilter filter = new FileFilter()
  157. {
  158. Name = "Custom Profile Images"
  159. };
  160. filter.AddPattern("*.jpg");
  161. filter.AddPattern("*.jpeg");
  162. filter.AddPattern("*.png");
  163. filter.AddPattern("*.bmp");
  164. fileChooser.AddFilter(filter);
  165. if (fileChooser.Run() == (int)ResponseType.Accept)
  166. {
  167. ProcessProfileImage(File.ReadAllBytes(fileChooser.Filename));
  168. }
  169. fileChooser.Dispose();
  170. }
  171. private void SelectProfileImage(bool newUser = false)
  172. {
  173. if (_contentManager.GetCurrentFirmwareVersion() == null)
  174. {
  175. ProfileImageFileChooser();
  176. }
  177. else
  178. {
  179. Dictionary<int, string> buttons = new Dictionary<int, string>()
  180. {
  181. { 0, "Import Image File" },
  182. { 1, "Select Firmware Avatar" }
  183. };
  184. ResponseType responseDialog = GtkDialog.CreateCustomDialog("Profile Image Selection",
  185. "Choose a Profile Image",
  186. "You may import a custom profile image, or select an avatar from the system firmware.",
  187. buttons, MessageType.Question);
  188. if (responseDialog == 0)
  189. {
  190. ProfileImageFileChooser();
  191. }
  192. else if (responseDialog == (ResponseType)1)
  193. {
  194. AvatarWindow avatarWindow = new AvatarWindow()
  195. {
  196. NewUser = newUser
  197. };
  198. avatarWindow.DeleteEvent += AvatarWindow_DeleteEvent;
  199. avatarWindow.SetSizeRequest((int)(avatarWindow.DefaultWidth * Program.WindowScaleFactor), (int)(avatarWindow.DefaultHeight * Program.WindowScaleFactor));
  200. avatarWindow.Show();
  201. }
  202. }
  203. }
  204. private void ChangeProfileImageButton_Pressed(object sender, EventArgs e)
  205. {
  206. if (_contentManager.GetCurrentFirmwareVersion() != null)
  207. {
  208. _avatarsPreloadingEvent.WaitOne();
  209. }
  210. SelectProfileImage();
  211. if (_bufferImageProfile != null)
  212. {
  213. SetUserImage();
  214. }
  215. }
  216. private void AvatarWindow_DeleteEvent(object sender, DeleteEventArgs args)
  217. {
  218. _bufferImageProfile = ((AvatarWindow)sender).SelectedProfileImage;
  219. if (_bufferImageProfile != null)
  220. {
  221. if (((AvatarWindow)sender).NewUser)
  222. {
  223. AddUser();
  224. }
  225. else
  226. {
  227. SetUserImage();
  228. }
  229. }
  230. }
  231. private void AddUser()
  232. {
  233. _accountManager.AddUser(_tempNewProfileName, _bufferImageProfile);
  234. _bufferImageProfile = null;
  235. _tempNewProfileName = "";
  236. RefreshList();
  237. }
  238. private void SetUserImage()
  239. {
  240. _accountManager.SetUserImage(GetSelectedUserId(), _bufferImageProfile);
  241. _bufferImageProfile = null;
  242. RefreshList();
  243. }
  244. private UserId GetSelectedUserId()
  245. {
  246. if (_usersTreeView.Model.GetIterFirst(out TreeIter iter))
  247. {
  248. do
  249. {
  250. if ((bool)_tableStore.GetValue(iter, 0))
  251. {
  252. break;
  253. }
  254. }
  255. while (_usersTreeView.Model.IterNext(ref iter));
  256. }
  257. return new UserId(_tableStore.GetValue(iter, 2).ToString().Split("\n")[1]);
  258. }
  259. private void CloseButton_Pressed(object sender, EventArgs e)
  260. {
  261. Close();
  262. }
  263. }
  264. }