UserProfilesManagerWindow.cs 11 KB

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