configure_profile_manager.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // SPDX-FileCopyrightText: 2016 Citra Emulator Project & 2024 suyu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <functional>
  5. #include <QDialog>
  6. #include <QDialogButtonBox>
  7. #include <QFileDialog>
  8. #include <QGraphicsItem>
  9. #include <QHeaderView>
  10. #include <QMessageBox>
  11. #include <QStandardItemModel>
  12. #include <QTreeView>
  13. #include "common/assert.h"
  14. #include "common/fs/path_util.h"
  15. #include "common/settings.h"
  16. #include "common/string_util.h"
  17. #include "core/core.h"
  18. #include "core/hle/service/acc/profile_manager.h"
  19. #include "ui_configure_profile_manager.h"
  20. #include "yuzu/configuration/configure_profile_manager.h"
  21. #include "yuzu/util/limitable_input_dialog.h"
  22. namespace {
  23. // Same backup JPEG used by acc IProfile::GetImage if no jpeg found
  24. constexpr std::array<u8, 107> backup_jpeg{
  25. 0xff, 0xd8, 0xff, 0xdb, 0x00, 0x43, 0x00, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02,
  26. 0x02, 0x03, 0x03, 0x03, 0x03, 0x04, 0x06, 0x04, 0x04, 0x04, 0x04, 0x04, 0x08, 0x06, 0x06, 0x05,
  27. 0x06, 0x09, 0x08, 0x0a, 0x0a, 0x09, 0x08, 0x09, 0x09, 0x0a, 0x0c, 0x0f, 0x0c, 0x0a, 0x0b, 0x0e,
  28. 0x0b, 0x09, 0x09, 0x0d, 0x11, 0x0d, 0x0e, 0x0f, 0x10, 0x10, 0x11, 0x10, 0x0a, 0x0c, 0x12, 0x13,
  29. 0x12, 0x10, 0x13, 0x0f, 0x10, 0x10, 0x10, 0xff, 0xc9, 0x00, 0x0b, 0x08, 0x00, 0x01, 0x00, 0x01,
  30. 0x01, 0x01, 0x11, 0x00, 0xff, 0xcc, 0x00, 0x06, 0x00, 0x10, 0x10, 0x05, 0xff, 0xda, 0x00, 0x08,
  31. 0x01, 0x01, 0x00, 0x00, 0x3f, 0x00, 0xd2, 0xcf, 0x20, 0xff, 0xd9,
  32. };
  33. QString GetImagePath(const Common::UUID& uuid) {
  34. const auto path =
  35. Common::FS::GetSuyuPath(Common::FS::SuyuPath::NANDDir) /
  36. fmt::format("system/save/8000000000000010/su/avators/{}.jpg", uuid.FormattedString());
  37. return QString::fromStdString(Common::FS::PathToUTF8String(path));
  38. }
  39. QString GetAccountUsername(const Service::Account::ProfileManager& manager, Common::UUID uuid) {
  40. Service::Account::ProfileBase profile{};
  41. if (!manager.GetProfileBase(uuid, profile)) {
  42. return {};
  43. }
  44. const auto text = Common::StringFromFixedZeroTerminatedBuffer(
  45. reinterpret_cast<const char*>(profile.username.data()), profile.username.size());
  46. return QString::fromStdString(text);
  47. }
  48. QString FormatUserEntryText(const QString& username, Common::UUID uuid) {
  49. return ConfigureProfileManager::tr("%1\n%2",
  50. "%1 is the profile username, %2 is the formatted UUID (e.g. "
  51. "00112233-4455-6677-8899-AABBCCDDEEFF))")
  52. .arg(username, QString::fromStdString(uuid.FormattedString()));
  53. }
  54. QPixmap GetIcon(const Common::UUID& uuid) {
  55. QPixmap icon{GetImagePath(uuid)};
  56. if (!icon) {
  57. icon.fill(Qt::black);
  58. icon.loadFromData(backup_jpeg.data(), static_cast<u32>(backup_jpeg.size()));
  59. }
  60. return icon.scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  61. }
  62. QString GetProfileUsernameFromUser(QWidget* parent, const QString& description_text) {
  63. return LimitableInputDialog::GetText(parent, ConfigureProfileManager::tr("Enter Username"),
  64. description_text, 1,
  65. static_cast<int>(Service::Account::profile_username_size));
  66. }
  67. } // Anonymous namespace
  68. ConfigureProfileManager::ConfigureProfileManager(Core::System& system_, QWidget* parent)
  69. : QWidget(parent), ui{std::make_unique<Ui::ConfigureProfileManager>()},
  70. profile_manager{system_.GetProfileManager()}, system{system_} {
  71. ui->setupUi(this);
  72. tree_view = new QTreeView;
  73. item_model = new QStandardItemModel(tree_view);
  74. item_model->insertColumns(0, 1);
  75. tree_view->setModel(item_model);
  76. tree_view->setAlternatingRowColors(true);
  77. tree_view->setSelectionMode(QHeaderView::SingleSelection);
  78. tree_view->setSelectionBehavior(QHeaderView::SelectRows);
  79. tree_view->setVerticalScrollMode(QHeaderView::ScrollPerPixel);
  80. tree_view->setHorizontalScrollMode(QHeaderView::ScrollPerPixel);
  81. tree_view->setSortingEnabled(true);
  82. tree_view->setEditTriggers(QHeaderView::NoEditTriggers);
  83. tree_view->setUniformRowHeights(true);
  84. tree_view->setIconSize({64, 64});
  85. tree_view->setContextMenuPolicy(Qt::NoContextMenu);
  86. // We must register all custom types with the Qt Automoc system so that we are able to use it
  87. // with signals/slots. In this case, QList falls under the umbrells of custom types.
  88. qRegisterMetaType<QList<QStandardItem*>>("QList<QStandardItem*>");
  89. layout = new QVBoxLayout;
  90. layout->setContentsMargins(0, 0, 0, 0);
  91. layout->setSpacing(0);
  92. layout->addWidget(tree_view);
  93. ui->scrollArea->setLayout(layout);
  94. connect(tree_view, &QTreeView::clicked, this, &ConfigureProfileManager::SelectUser);
  95. connect(ui->pm_add, &QPushButton::clicked, this, &ConfigureProfileManager::AddUser);
  96. connect(ui->pm_rename, &QPushButton::clicked, this, &ConfigureProfileManager::RenameUser);
  97. connect(ui->pm_remove, &QPushButton::clicked, this,
  98. &ConfigureProfileManager::ConfirmDeleteUser);
  99. connect(ui->pm_set_image, &QPushButton::clicked, this, &ConfigureProfileManager::SetUserImage);
  100. confirm_dialog = new ConfigureProfileManagerDeleteDialog(this);
  101. scene = new QGraphicsScene;
  102. ui->current_user_icon->setScene(scene);
  103. RetranslateUI();
  104. SetConfiguration();
  105. }
  106. ConfigureProfileManager::~ConfigureProfileManager() = default;
  107. void ConfigureProfileManager::changeEvent(QEvent* event) {
  108. if (event->type() == QEvent::LanguageChange) {
  109. RetranslateUI();
  110. }
  111. QWidget::changeEvent(event);
  112. }
  113. void ConfigureProfileManager::RetranslateUI() {
  114. ui->retranslateUi(this);
  115. item_model->setHeaderData(0, Qt::Horizontal, tr("Users"));
  116. }
  117. void ConfigureProfileManager::SetConfiguration() {
  118. enabled = !system.IsPoweredOn();
  119. item_model->removeRows(0, item_model->rowCount());
  120. list_items.clear();
  121. PopulateUserList();
  122. UpdateCurrentUser();
  123. }
  124. void ConfigureProfileManager::PopulateUserList() {
  125. const auto& profiles = profile_manager.GetAllUsers();
  126. for (const auto& user : profiles) {
  127. Service::Account::ProfileBase profile{};
  128. if (!profile_manager.GetProfileBase(user, profile))
  129. continue;
  130. const auto username = Common::StringFromFixedZeroTerminatedBuffer(
  131. reinterpret_cast<const char*>(profile.username.data()), profile.username.size());
  132. list_items.push_back(QList<QStandardItem*>{new QStandardItem{
  133. GetIcon(user), FormatUserEntryText(QString::fromStdString(username), user)}});
  134. }
  135. for (const auto& item : list_items)
  136. item_model->appendRow(item);
  137. }
  138. void ConfigureProfileManager::UpdateCurrentUser() {
  139. ui->pm_add->setEnabled(profile_manager.GetUserCount() < Service::Account::MAX_USERS);
  140. const auto& current_user = profile_manager.GetUser(Settings::values.current_user.GetValue());
  141. ASSERT(current_user);
  142. const auto username = GetAccountUsername(profile_manager, *current_user);
  143. scene->clear();
  144. scene->addPixmap(
  145. GetIcon(*current_user).scaled(48, 48, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
  146. ui->current_user_username->setText(username);
  147. }
  148. void ConfigureProfileManager::ApplyConfiguration() {
  149. if (!enabled) {
  150. return;
  151. }
  152. }
  153. void ConfigureProfileManager::SelectUser(const QModelIndex& index) {
  154. Settings::values.current_user =
  155. std::clamp<s32>(index.row(), 0, static_cast<s32>(profile_manager.GetUserCount() - 1));
  156. UpdateCurrentUser();
  157. ui->pm_remove->setEnabled(profile_manager.GetUserCount() >= 2);
  158. ui->pm_rename->setEnabled(true);
  159. ui->pm_set_image->setEnabled(true);
  160. }
  161. void ConfigureProfileManager::AddUser() {
  162. const auto username =
  163. GetProfileUsernameFromUser(this, tr("Enter a username for the new user:"));
  164. if (username.isEmpty()) {
  165. return;
  166. }
  167. const auto uuid = Common::UUID::MakeRandom();
  168. profile_manager.CreateNewUser(uuid, username.toStdString());
  169. profile_manager.WriteUserSaveFile();
  170. item_model->appendRow(new QStandardItem{GetIcon(uuid), FormatUserEntryText(username, uuid)});
  171. }
  172. void ConfigureProfileManager::RenameUser() {
  173. const auto user = tree_view->currentIndex().row();
  174. const auto uuid = profile_manager.GetUser(user);
  175. ASSERT(uuid);
  176. Service::Account::ProfileBase profile{};
  177. if (!profile_manager.GetProfileBase(*uuid, profile))
  178. return;
  179. const auto new_username = GetProfileUsernameFromUser(this, tr("Enter a new username:"));
  180. if (new_username.isEmpty()) {
  181. return;
  182. }
  183. const auto username_std = new_username.toStdString();
  184. std::fill(profile.username.begin(), profile.username.end(), '\0');
  185. std::copy(username_std.begin(), username_std.end(), profile.username.begin());
  186. profile_manager.SetProfileBase(*uuid, profile);
  187. profile_manager.WriteUserSaveFile();
  188. item_model->setItem(
  189. user, 0,
  190. new QStandardItem{GetIcon(*uuid),
  191. FormatUserEntryText(QString::fromStdString(username_std), *uuid)});
  192. UpdateCurrentUser();
  193. }
  194. void ConfigureProfileManager::ConfirmDeleteUser() {
  195. const auto index = tree_view->currentIndex().row();
  196. const auto uuid = profile_manager.GetUser(index);
  197. ASSERT(uuid);
  198. const auto username = GetAccountUsername(profile_manager, *uuid);
  199. confirm_dialog->SetInfo(username, *uuid, [this, uuid]() { DeleteUser(*uuid); });
  200. confirm_dialog->show();
  201. }
  202. void ConfigureProfileManager::DeleteUser(const Common::UUID& uuid) {
  203. if (Settings::values.current_user.GetValue() == tree_view->currentIndex().row()) {
  204. Settings::values.current_user = 0;
  205. }
  206. UpdateCurrentUser();
  207. if (!profile_manager.RemoveUser(uuid)) {
  208. return;
  209. }
  210. profile_manager.WriteUserSaveFile();
  211. item_model->removeRows(tree_view->currentIndex().row(), 1);
  212. tree_view->clearSelection();
  213. ui->pm_remove->setEnabled(false);
  214. ui->pm_rename->setEnabled(false);
  215. }
  216. void ConfigureProfileManager::SetUserImage() {
  217. const auto index = tree_view->currentIndex().row();
  218. const auto uuid = profile_manager.GetUser(index);
  219. ASSERT(uuid);
  220. const auto file = QFileDialog::getOpenFileName(this, tr("Select User Image"), QString(),
  221. tr("JPEG Images (*.jpg *.jpeg)"));
  222. if (file.isEmpty()) {
  223. return;
  224. }
  225. const auto image_path = GetImagePath(*uuid);
  226. if (QFile::exists(image_path) && !QFile::remove(image_path)) {
  227. QMessageBox::warning(
  228. this, tr("Error deleting image"),
  229. tr("Error occurred attempting to overwrite previous image at: %1.").arg(image_path));
  230. return;
  231. }
  232. const auto raw_path = QString::fromStdString(Common::FS::PathToUTF8String(
  233. Common::FS::GetSuyuPath(Common::FS::SuyuPath::NANDDir) / "system/save/8000000000000010"));
  234. const QFileInfo raw_info{raw_path};
  235. if (raw_info.exists() && !raw_info.isDir() && !QFile::remove(raw_path)) {
  236. QMessageBox::warning(this, tr("Error deleting file"),
  237. tr("Unable to delete existing file: %1.").arg(raw_path));
  238. return;
  239. }
  240. const QString absolute_dst_path = QFileInfo{image_path}.absolutePath();
  241. if (!QDir{raw_path}.mkpath(absolute_dst_path)) {
  242. QMessageBox::warning(
  243. this, tr("Error creating user image directory"),
  244. tr("Unable to create directory %1 for storing user images.").arg(absolute_dst_path));
  245. return;
  246. }
  247. if (!QFile::copy(file, image_path)) {
  248. QMessageBox::warning(this, tr("Error copying user image"),
  249. tr("Unable to copy image from %1 to %2").arg(file, image_path));
  250. return;
  251. }
  252. // Profile image must be 256x256
  253. QImage image(image_path);
  254. if (image.width() != 256 || image.height() != 256) {
  255. image = image.scaled(256, 256, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
  256. if (!image.save(image_path)) {
  257. QMessageBox::warning(this, tr("Error resizing user image"),
  258. tr("Unable to resize image"));
  259. return;
  260. }
  261. }
  262. const auto username = GetAccountUsername(profile_manager, *uuid);
  263. item_model->setItem(index, 0,
  264. new QStandardItem{GetIcon(*uuid), FormatUserEntryText(username, *uuid)});
  265. UpdateCurrentUser();
  266. }
  267. ConfigureProfileManagerDeleteDialog::ConfigureProfileManagerDeleteDialog(QWidget* parent)
  268. : QDialog{parent} {
  269. auto dialog_vbox_layout = new QVBoxLayout(this);
  270. dialog_button_box =
  271. new QDialogButtonBox(QDialogButtonBox::Yes | QDialogButtonBox::No, Qt::Horizontal, parent);
  272. auto label_message =
  273. new QLabel(tr("Delete this user? All of the user's save data will be deleted."), this);
  274. label_info = new QLabel(this);
  275. auto dialog_hbox_layout_widget = new QWidget(this);
  276. auto dialog_hbox_layout = new QHBoxLayout(dialog_hbox_layout_widget);
  277. icon_scene = new QGraphicsScene(0, 0, 64, 64, this);
  278. auto icon_view = new QGraphicsView(icon_scene, this);
  279. dialog_hbox_layout_widget->setLayout(dialog_hbox_layout);
  280. icon_view->setMaximumSize(64, 64);
  281. icon_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  282. icon_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  283. this->setLayout(dialog_vbox_layout);
  284. this->setWindowTitle(tr("Confirm Delete"));
  285. this->setSizeGripEnabled(false);
  286. dialog_vbox_layout->addWidget(label_message);
  287. dialog_vbox_layout->addWidget(dialog_hbox_layout_widget);
  288. dialog_vbox_layout->addWidget(dialog_button_box);
  289. dialog_hbox_layout->addWidget(icon_view);
  290. dialog_hbox_layout->addWidget(label_info);
  291. connect(dialog_button_box, &QDialogButtonBox::rejected, this, [this]() { close(); });
  292. }
  293. ConfigureProfileManagerDeleteDialog::~ConfigureProfileManagerDeleteDialog() = default;
  294. void ConfigureProfileManagerDeleteDialog::SetInfo(const QString& username, const Common::UUID& uuid,
  295. std::function<void()> accept_callback) {
  296. label_info->setText(
  297. tr("Name: %1\nUUID: %2").arg(username, QString::fromStdString(uuid.FormattedString())));
  298. icon_scene->clear();
  299. icon_scene->addPixmap(GetIcon(uuid));
  300. connect(dialog_button_box, &QDialogButtonBox::accepted, this, [this, accept_callback]() {
  301. close();
  302. accept_callback();
  303. });
  304. }