UserProfilesManagerWindow.cs 11 KB

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