configure_profile_manager.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <QFileDialog>
  6. #include <QGraphicsItem>
  7. #include <QGraphicsScene>
  8. #include <QHeaderView>
  9. #include <QMessageBox>
  10. #include <QStandardItemModel>
  11. #include <QTreeView>
  12. #include <QVBoxLayout>
  13. #include "common/assert.h"
  14. #include "common/file_util.h"
  15. #include "common/string_util.h"
  16. #include "core/core.h"
  17. #include "core/hle/service/acc/profile_manager.h"
  18. #include "core/settings.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(Common::UUID uuid) {
  34. const auto path = Common::FS::GetUserPath(Common::FS::UserPath::NANDDir) +
  35. "/system/save/8000000000000010/su/avators/" + uuid.FormatSwitch() + ".jpg";
  36. return QString::fromStdString(path);
  37. }
  38. QString GetAccountUsername(const Service::Account::ProfileManager& manager, Common::UUID uuid) {
  39. Service::Account::ProfileBase profile{};
  40. if (!manager.GetProfileBase(uuid, profile)) {
  41. return {};
  42. }
  43. const auto text = Common::StringFromFixedZeroTerminatedBuffer(
  44. reinterpret_cast<const char*>(profile.username.data()), profile.username.size());
  45. return QString::fromStdString(text);
  46. }
  47. QString FormatUserEntryText(const QString& username, Common::UUID uuid) {
  48. return ConfigureProfileManager::tr("%1\n%2",
  49. "%1 is the profile username, %2 is the formatted UUID (e.g. "
  50. "00112233-4455-6677-8899-AABBCCDDEEFF))")
  51. .arg(username, QString::fromStdString(uuid.FormatSwitch()));
  52. }
  53. QPixmap GetIcon(Common::UUID uuid) {
  54. QPixmap icon{GetImagePath(uuid)};
  55. if (!icon) {
  56. icon.fill(Qt::black);
  57. icon.loadFromData(backup_jpeg.data(), static_cast<u32>(backup_jpeg.size()));
  58. }
  59. return icon.scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  60. }
  61. QString GetProfileUsernameFromUser(QWidget* parent, const QString& description_text) {
  62. return LimitableInputDialog::GetText(parent, ConfigureProfileManager::tr("Enter Username"),
  63. description_text, 1,
  64. static_cast<int>(Service::Account::profile_username_size));
  65. }
  66. } // Anonymous namespace
  67. ConfigureProfileManager ::ConfigureProfileManager(QWidget* parent)
  68. : QWidget(parent), ui(new Ui::ConfigureProfileManager),
  69. profile_manager(std::make_unique<Service::Account::ProfileManager>()) {
  70. ui->setupUi(this);
  71. tree_view = new QTreeView;
  72. item_model = new QStandardItemModel(tree_view);
  73. item_model->insertColumns(0, 1);
  74. tree_view->setModel(item_model);
  75. tree_view->setAlternatingRowColors(true);
  76. tree_view->setSelectionMode(QHeaderView::SingleSelection);
  77. tree_view->setSelectionBehavior(QHeaderView::SelectRows);
  78. tree_view->setVerticalScrollMode(QHeaderView::ScrollPerPixel);
  79. tree_view->setHorizontalScrollMode(QHeaderView::ScrollPerPixel);
  80. tree_view->setSortingEnabled(true);
  81. tree_view->setEditTriggers(QHeaderView::NoEditTriggers);
  82. tree_view->setUniformRowHeights(true);
  83. tree_view->setIconSize({64, 64});
  84. tree_view->setContextMenuPolicy(Qt::NoContextMenu);
  85. // We must register all custom types with the Qt Automoc system so that we are able to use it
  86. // with signals/slots. In this case, QList falls under the umbrells of custom types.
  87. qRegisterMetaType<QList<QStandardItem*>>("QList<QStandardItem*>");
  88. layout = new QVBoxLayout;
  89. layout->setContentsMargins(0, 0, 0, 0);
  90. layout->setSpacing(0);
  91. layout->addWidget(tree_view);
  92. ui->scrollArea->setLayout(layout);
  93. connect(tree_view, &QTreeView::clicked, this, &ConfigureProfileManager::SelectUser);
  94. connect(ui->pm_add, &QPushButton::clicked, this, &ConfigureProfileManager::AddUser);
  95. connect(ui->pm_rename, &QPushButton::clicked, this, &ConfigureProfileManager::RenameUser);
  96. connect(ui->pm_remove, &QPushButton::clicked, this, &ConfigureProfileManager::DeleteUser);
  97. connect(ui->pm_set_image, &QPushButton::clicked, this, &ConfigureProfileManager::SetUserImage);
  98. scene = new QGraphicsScene;
  99. ui->current_user_icon->setScene(scene);
  100. SetConfiguration();
  101. RetranslateUI();
  102. }
  103. ConfigureProfileManager::~ConfigureProfileManager() = default;
  104. void ConfigureProfileManager::changeEvent(QEvent* event) {
  105. if (event->type() == QEvent::LanguageChange) {
  106. RetranslateUI();
  107. }
  108. QWidget::changeEvent(event);
  109. }
  110. void ConfigureProfileManager::RetranslateUI() {
  111. ui->retranslateUi(this);
  112. item_model->setHeaderData(0, Qt::Horizontal, tr("Users"));
  113. }
  114. void ConfigureProfileManager::SetConfiguration() {
  115. enabled = !Core::System::GetInstance().IsPoweredOn();
  116. item_model->removeRows(0, item_model->rowCount());
  117. list_items.clear();
  118. PopulateUserList();
  119. UpdateCurrentUser();
  120. }
  121. void ConfigureProfileManager::PopulateUserList() {
  122. const auto& profiles = profile_manager->GetAllUsers();
  123. for (const auto& user : profiles) {
  124. Service::Account::ProfileBase profile{};
  125. if (!profile_manager->GetProfileBase(user, profile))
  126. continue;
  127. const auto username = Common::StringFromFixedZeroTerminatedBuffer(
  128. reinterpret_cast<const char*>(profile.username.data()), profile.username.size());
  129. list_items.push_back(QList<QStandardItem*>{new QStandardItem{
  130. GetIcon(user), FormatUserEntryText(QString::fromStdString(username), user)}});
  131. }
  132. for (const auto& item : list_items)
  133. item_model->appendRow(item);
  134. }
  135. void ConfigureProfileManager::UpdateCurrentUser() {
  136. ui->pm_add->setEnabled(profile_manager->GetUserCount() < Service::Account::MAX_USERS);
  137. const auto& current_user = profile_manager->GetUser(Settings::values.current_user);
  138. ASSERT(current_user);
  139. const auto username = GetAccountUsername(*profile_manager, *current_user);
  140. scene->clear();
  141. scene->addPixmap(
  142. GetIcon(*current_user).scaled(48, 48, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
  143. ui->current_user_username->setText(username);
  144. }
  145. void ConfigureProfileManager::ApplyConfiguration() {
  146. if (!enabled) {
  147. return;
  148. }
  149. Settings::Apply(Core::System::GetInstance());
  150. }
  151. void ConfigureProfileManager::SelectUser(const QModelIndex& index) {
  152. Settings::values.current_user =
  153. std::clamp<s32>(index.row(), 0, static_cast<s32>(profile_manager->GetUserCount() - 1));
  154. UpdateCurrentUser();
  155. ui->pm_remove->setEnabled(profile_manager->GetUserCount() >= 2);
  156. ui->pm_rename->setEnabled(true);
  157. ui->pm_set_image->setEnabled(true);
  158. }
  159. void ConfigureProfileManager::AddUser() {
  160. const auto username =
  161. GetProfileUsernameFromUser(this, tr("Enter a username for the new user:"));
  162. if (username.isEmpty()) {
  163. return;
  164. }
  165. const auto uuid = Common::UUID::Generate();
  166. profile_manager->CreateNewUser(uuid, username.toStdString());
  167. item_model->appendRow(new QStandardItem{GetIcon(uuid), FormatUserEntryText(username, uuid)});
  168. }
  169. void ConfigureProfileManager::RenameUser() {
  170. const auto user = tree_view->currentIndex().row();
  171. const auto uuid = profile_manager->GetUser(user);
  172. ASSERT(uuid);
  173. Service::Account::ProfileBase profile{};
  174. if (!profile_manager->GetProfileBase(*uuid, profile))
  175. return;
  176. const auto new_username = GetProfileUsernameFromUser(this, tr("Enter a new username:"));
  177. if (new_username.isEmpty()) {
  178. return;
  179. }
  180. const auto username_std = new_username.toStdString();
  181. std::fill(profile.username.begin(), profile.username.end(), '\0');
  182. std::copy(username_std.begin(), username_std.end(), profile.username.begin());
  183. profile_manager->SetProfileBase(*uuid, profile);
  184. item_model->setItem(
  185. user, 0,
  186. new QStandardItem{GetIcon(*uuid),
  187. FormatUserEntryText(QString::fromStdString(username_std), *uuid)});
  188. UpdateCurrentUser();
  189. }
  190. void ConfigureProfileManager::DeleteUser() {
  191. const auto index = tree_view->currentIndex().row();
  192. const auto uuid = profile_manager->GetUser(index);
  193. ASSERT(uuid);
  194. const auto username = GetAccountUsername(*profile_manager, *uuid);
  195. const auto confirm = QMessageBox::question(
  196. this, tr("Confirm Delete"),
  197. tr("You are about to delete user with name \"%1\". Are you sure?").arg(username));
  198. if (confirm == QMessageBox::No)
  199. return;
  200. if (Settings::values.current_user == tree_view->currentIndex().row())
  201. Settings::values.current_user = 0;
  202. UpdateCurrentUser();
  203. if (!profile_manager->RemoveUser(*uuid))
  204. return;
  205. item_model->removeRows(tree_view->currentIndex().row(), 1);
  206. tree_view->clearSelection();
  207. ui->pm_remove->setEnabled(false);
  208. ui->pm_rename->setEnabled(false);
  209. }
  210. void ConfigureProfileManager::SetUserImage() {
  211. const auto index = tree_view->currentIndex().row();
  212. const auto uuid = profile_manager->GetUser(index);
  213. ASSERT(uuid);
  214. const auto file = QFileDialog::getOpenFileName(this, tr("Select User Image"), QString(),
  215. tr("JPEG Images (*.jpg *.jpeg)"));
  216. if (file.isEmpty()) {
  217. return;
  218. }
  219. const auto image_path = GetImagePath(*uuid);
  220. if (QFile::exists(image_path) && !QFile::remove(image_path)) {
  221. QMessageBox::warning(
  222. this, tr("Error deleting image"),
  223. tr("Error occurred attempting to overwrite previous image at: %1.").arg(image_path));
  224. return;
  225. }
  226. const auto raw_path = QString::fromStdString(
  227. Common::FS::GetUserPath(Common::FS::UserPath::NANDDir) + "/system/save/8000000000000010");
  228. const QFileInfo raw_info{raw_path};
  229. if (raw_info.exists() && !raw_info.isDir() && !QFile::remove(raw_path)) {
  230. QMessageBox::warning(this, tr("Error deleting file"),
  231. tr("Unable to delete existing file: %1.").arg(raw_path));
  232. return;
  233. }
  234. const QString absolute_dst_path = QFileInfo{image_path}.absolutePath();
  235. if (!QDir{raw_path}.mkpath(absolute_dst_path)) {
  236. QMessageBox::warning(
  237. this, tr("Error creating user image directory"),
  238. tr("Unable to create directory %1 for storing user images.").arg(absolute_dst_path));
  239. return;
  240. }
  241. if (!QFile::copy(file, image_path)) {
  242. QMessageBox::warning(this, tr("Error copying user image"),
  243. tr("Unable to copy image from %1 to %2").arg(file, image_path));
  244. return;
  245. }
  246. const auto username = GetAccountUsername(*profile_manager, *uuid);
  247. item_model->setItem(index, 0,
  248. new QStandardItem{GetIcon(*uuid), FormatUserEntryText(username, *uuid)});
  249. UpdateCurrentUser();
  250. }