configure_system.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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_system.h"
  20. #include "yuzu/configuration/configure_system.h"
  21. #include "yuzu/util/limitable_input_dialog.h"
  22. namespace {
  23. constexpr std::array<int, 12> days_in_month = {{
  24. 31,
  25. 29,
  26. 31,
  27. 30,
  28. 31,
  29. 30,
  30. 31,
  31. 31,
  32. 30,
  33. 31,
  34. 30,
  35. 31,
  36. }};
  37. // Same backup JPEG used by acc IProfile::GetImage if no jpeg found
  38. constexpr std::array<u8, 107> backup_jpeg{
  39. 0xff, 0xd8, 0xff, 0xdb, 0x00, 0x43, 0x00, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02,
  40. 0x02, 0x03, 0x03, 0x03, 0x03, 0x04, 0x06, 0x04, 0x04, 0x04, 0x04, 0x04, 0x08, 0x06, 0x06, 0x05,
  41. 0x06, 0x09, 0x08, 0x0a, 0x0a, 0x09, 0x08, 0x09, 0x09, 0x0a, 0x0c, 0x0f, 0x0c, 0x0a, 0x0b, 0x0e,
  42. 0x0b, 0x09, 0x09, 0x0d, 0x11, 0x0d, 0x0e, 0x0f, 0x10, 0x10, 0x11, 0x10, 0x0a, 0x0c, 0x12, 0x13,
  43. 0x12, 0x10, 0x13, 0x0f, 0x10, 0x10, 0x10, 0xff, 0xc9, 0x00, 0x0b, 0x08, 0x00, 0x01, 0x00, 0x01,
  44. 0x01, 0x01, 0x11, 0x00, 0xff, 0xcc, 0x00, 0x06, 0x00, 0x10, 0x10, 0x05, 0xff, 0xda, 0x00, 0x08,
  45. 0x01, 0x01, 0x00, 0x00, 0x3f, 0x00, 0xd2, 0xcf, 0x20, 0xff, 0xd9,
  46. };
  47. QString GetImagePath(Service::Account::UUID uuid) {
  48. const auto path = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) +
  49. "/system/save/8000000000000010/su/avators/" + uuid.FormatSwitch() + ".jpg";
  50. return QString::fromStdString(path);
  51. }
  52. QString GetAccountUsername(const Service::Account::ProfileManager& manager,
  53. Service::Account::UUID uuid) {
  54. Service::Account::ProfileBase profile;
  55. if (!manager.GetProfileBase(uuid, profile)) {
  56. return {};
  57. }
  58. const auto text = Common::StringFromFixedZeroTerminatedBuffer(
  59. reinterpret_cast<const char*>(profile.username.data()), profile.username.size());
  60. return QString::fromStdString(text);
  61. }
  62. QString FormatUserEntryText(const QString& username, Service::Account::UUID uuid) {
  63. return ConfigureSystem::tr("%1\n%2",
  64. "%1 is the profile username, %2 is the formatted UUID (e.g. "
  65. "00112233-4455-6677-8899-AABBCCDDEEFF))")
  66. .arg(username, QString::fromStdString(uuid.FormatSwitch()));
  67. }
  68. QPixmap GetIcon(Service::Account::UUID uuid) {
  69. QPixmap icon{GetImagePath(uuid)};
  70. if (!icon) {
  71. icon.fill(Qt::black);
  72. icon.loadFromData(backup_jpeg.data(), static_cast<u32>(backup_jpeg.size()));
  73. }
  74. return icon.scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  75. }
  76. QString GetProfileUsernameFromUser(QWidget* parent, const QString& description_text) {
  77. return LimitableInputDialog::GetText(parent, ConfigureSystem::tr("Enter Username"),
  78. description_text, 1,
  79. static_cast<int>(Service::Account::profile_username_size));
  80. }
  81. } // Anonymous namespace
  82. ConfigureSystem::ConfigureSystem(QWidget* parent)
  83. : QWidget(parent), ui(new Ui::ConfigureSystem),
  84. profile_manager(std::make_unique<Service::Account::ProfileManager>()) {
  85. ui->setupUi(this);
  86. connect(ui->combo_birthmonth,
  87. static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
  88. &ConfigureSystem::UpdateBirthdayComboBox);
  89. connect(ui->button_regenerate_console_id, &QPushButton::clicked, this,
  90. &ConfigureSystem::RefreshConsoleID);
  91. layout = new QVBoxLayout;
  92. tree_view = new QTreeView;
  93. item_model = new QStandardItemModel(tree_view);
  94. tree_view->setModel(item_model);
  95. tree_view->setAlternatingRowColors(true);
  96. tree_view->setSelectionMode(QHeaderView::SingleSelection);
  97. tree_view->setSelectionBehavior(QHeaderView::SelectRows);
  98. tree_view->setVerticalScrollMode(QHeaderView::ScrollPerPixel);
  99. tree_view->setHorizontalScrollMode(QHeaderView::ScrollPerPixel);
  100. tree_view->setSortingEnabled(true);
  101. tree_view->setEditTriggers(QHeaderView::NoEditTriggers);
  102. tree_view->setUniformRowHeights(true);
  103. tree_view->setIconSize({64, 64});
  104. tree_view->setContextMenuPolicy(Qt::NoContextMenu);
  105. item_model->insertColumns(0, 1);
  106. item_model->setHeaderData(0, Qt::Horizontal, "Users");
  107. // We must register all custom types with the Qt Automoc system so that we are able to use it
  108. // with signals/slots. In this case, QList falls under the umbrells of custom types.
  109. qRegisterMetaType<QList<QStandardItem*>>("QList<QStandardItem*>");
  110. layout->setContentsMargins(0, 0, 0, 0);
  111. layout->setSpacing(0);
  112. layout->addWidget(tree_view);
  113. ui->scrollArea->setLayout(layout);
  114. connect(tree_view, &QTreeView::clicked, this, &ConfigureSystem::SelectUser);
  115. connect(ui->pm_add, &QPushButton::pressed, this, &ConfigureSystem::AddUser);
  116. connect(ui->pm_rename, &QPushButton::pressed, this, &ConfigureSystem::RenameUser);
  117. connect(ui->pm_remove, &QPushButton::pressed, this, &ConfigureSystem::DeleteUser);
  118. connect(ui->pm_set_image, &QPushButton::pressed, this, &ConfigureSystem::SetUserImage);
  119. connect(ui->rng_seed_checkbox, &QCheckBox::stateChanged, this, [this](bool checked) {
  120. ui->rng_seed_edit->setEnabled(checked);
  121. if (!checked)
  122. ui->rng_seed_edit->setText(QStringLiteral("00000000"));
  123. });
  124. scene = new QGraphicsScene;
  125. ui->current_user_icon->setScene(scene);
  126. this->setConfiguration();
  127. }
  128. ConfigureSystem::~ConfigureSystem() = default;
  129. void ConfigureSystem::setConfiguration() {
  130. enabled = !Core::System::GetInstance().IsPoweredOn();
  131. ui->combo_language->setCurrentIndex(Settings::values.language_index);
  132. item_model->removeRows(0, item_model->rowCount());
  133. list_items.clear();
  134. PopulateUserList();
  135. UpdateCurrentUser();
  136. ui->rng_seed_checkbox->setChecked(Settings::values.rng_seed.has_value());
  137. ui->rng_seed_edit->setEnabled(Settings::values.rng_seed.has_value());
  138. const auto rng_seed =
  139. QString("%1").arg(Settings::values.rng_seed.value_or(0), 8, 16, QLatin1Char{'0'}).toUpper();
  140. ui->rng_seed_edit->setText(rng_seed);
  141. }
  142. void ConfigureSystem::PopulateUserList() {
  143. const auto& profiles = profile_manager->GetAllUsers();
  144. for (const auto& user : profiles) {
  145. Service::Account::ProfileBase profile;
  146. if (!profile_manager->GetProfileBase(user, profile))
  147. continue;
  148. const auto username = Common::StringFromFixedZeroTerminatedBuffer(
  149. reinterpret_cast<const char*>(profile.username.data()), profile.username.size());
  150. list_items.push_back(QList<QStandardItem*>{new QStandardItem{
  151. GetIcon(user), FormatUserEntryText(QString::fromStdString(username), user)}});
  152. }
  153. for (const auto& item : list_items)
  154. item_model->appendRow(item);
  155. }
  156. void ConfigureSystem::UpdateCurrentUser() {
  157. ui->pm_add->setEnabled(profile_manager->GetUserCount() < Service::Account::MAX_USERS);
  158. const auto& current_user = profile_manager->GetUser(Settings::values.current_user);
  159. ASSERT(current_user);
  160. const auto username = GetAccountUsername(*profile_manager, *current_user);
  161. scene->clear();
  162. scene->addPixmap(
  163. GetIcon(*current_user).scaled(48, 48, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
  164. ui->current_user_username->setText(username);
  165. }
  166. void ConfigureSystem::ReadSystemSettings() {}
  167. void ConfigureSystem::applyConfiguration() {
  168. if (!enabled)
  169. return;
  170. Settings::values.language_index = ui->combo_language->currentIndex();
  171. if (ui->rng_seed_checkbox->isChecked())
  172. Settings::values.rng_seed = ui->rng_seed_edit->text().toULongLong(nullptr, 16);
  173. else
  174. Settings::values.rng_seed = std::nullopt;
  175. Settings::Apply();
  176. }
  177. void ConfigureSystem::UpdateBirthdayComboBox(int birthmonth_index) {
  178. if (birthmonth_index < 0 || birthmonth_index >= 12)
  179. return;
  180. // store current day selection
  181. int birthday_index = ui->combo_birthday->currentIndex();
  182. // get number of days in the new selected month
  183. int days = days_in_month[birthmonth_index];
  184. // if the selected day is out of range,
  185. // reset it to 1st
  186. if (birthday_index < 0 || birthday_index >= days)
  187. birthday_index = 0;
  188. // update the day combo box
  189. ui->combo_birthday->clear();
  190. for (int i = 1; i <= days; ++i) {
  191. ui->combo_birthday->addItem(QString::number(i));
  192. }
  193. // restore the day selection
  194. ui->combo_birthday->setCurrentIndex(birthday_index);
  195. }
  196. void ConfigureSystem::RefreshConsoleID() {
  197. QMessageBox::StandardButton reply;
  198. QString warning_text = tr("This will replace your current virtual Switch with a new one. "
  199. "Your current virtual Switch will not be recoverable. "
  200. "This might have unexpected effects in games. This might fail, "
  201. "if you use an outdated config savegame. Continue?");
  202. reply = QMessageBox::critical(this, tr("Warning"), warning_text,
  203. QMessageBox::No | QMessageBox::Yes);
  204. if (reply == QMessageBox::No)
  205. return;
  206. u64 console_id{};
  207. ui->label_console_id->setText(
  208. tr("Console ID: 0x%1").arg(QString::number(console_id, 16).toUpper()));
  209. }
  210. void ConfigureSystem::SelectUser(const QModelIndex& index) {
  211. Settings::values.current_user =
  212. std::clamp<s32>(index.row(), 0, static_cast<s32>(profile_manager->GetUserCount() - 1));
  213. UpdateCurrentUser();
  214. ui->pm_remove->setEnabled(profile_manager->GetUserCount() >= 2);
  215. ui->pm_rename->setEnabled(true);
  216. ui->pm_set_image->setEnabled(true);
  217. }
  218. void ConfigureSystem::AddUser() {
  219. const auto username =
  220. GetProfileUsernameFromUser(this, tr("Enter a username for the new user:"));
  221. if (username.isEmpty()) {
  222. return;
  223. }
  224. const auto uuid = Service::Account::UUID::Generate();
  225. profile_manager->CreateNewUser(uuid, username.toStdString());
  226. item_model->appendRow(new QStandardItem{GetIcon(uuid), FormatUserEntryText(username, uuid)});
  227. }
  228. void ConfigureSystem::RenameUser() {
  229. const auto user = tree_view->currentIndex().row();
  230. const auto uuid = profile_manager->GetUser(user);
  231. ASSERT(uuid);
  232. Service::Account::ProfileBase profile;
  233. if (!profile_manager->GetProfileBase(*uuid, profile))
  234. return;
  235. const auto new_username = GetProfileUsernameFromUser(this, tr("Enter a new username:"));
  236. if (new_username.isEmpty()) {
  237. return;
  238. }
  239. const auto username_std = new_username.toStdString();
  240. std::fill(profile.username.begin(), profile.username.end(), '\0');
  241. std::copy(username_std.begin(), username_std.end(), profile.username.begin());
  242. profile_manager->SetProfileBase(*uuid, profile);
  243. item_model->setItem(
  244. user, 0,
  245. new QStandardItem{GetIcon(*uuid),
  246. FormatUserEntryText(QString::fromStdString(username_std), *uuid)});
  247. UpdateCurrentUser();
  248. }
  249. void ConfigureSystem::DeleteUser() {
  250. const auto index = tree_view->currentIndex().row();
  251. const auto uuid = profile_manager->GetUser(index);
  252. ASSERT(uuid);
  253. const auto username = GetAccountUsername(*profile_manager, *uuid);
  254. const auto confirm = QMessageBox::question(
  255. this, tr("Confirm Delete"),
  256. tr("You are about to delete user with name \"%1\". Are you sure?").arg(username));
  257. if (confirm == QMessageBox::No)
  258. return;
  259. if (Settings::values.current_user == tree_view->currentIndex().row())
  260. Settings::values.current_user = 0;
  261. UpdateCurrentUser();
  262. if (!profile_manager->RemoveUser(*uuid))
  263. return;
  264. item_model->removeRows(tree_view->currentIndex().row(), 1);
  265. tree_view->clearSelection();
  266. ui->pm_remove->setEnabled(false);
  267. ui->pm_rename->setEnabled(false);
  268. }
  269. void ConfigureSystem::SetUserImage() {
  270. const auto index = tree_view->currentIndex().row();
  271. const auto uuid = profile_manager->GetUser(index);
  272. ASSERT(uuid);
  273. const auto file = QFileDialog::getOpenFileName(this, tr("Select User Image"), QString(),
  274. tr("JPEG Images (*.jpg *.jpeg)"));
  275. if (file.isEmpty()) {
  276. return;
  277. }
  278. const auto image_path = GetImagePath(*uuid);
  279. if (QFile::exists(image_path) && !QFile::remove(image_path)) {
  280. QMessageBox::warning(
  281. this, tr("Error deleting image"),
  282. tr("Error occurred attempting to overwrite previous image at: %1.").arg(image_path));
  283. return;
  284. }
  285. const auto raw_path = QString::fromStdString(
  286. FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + "/system/save/8000000000000010");
  287. const QFileInfo raw_info{raw_path};
  288. if (raw_info.exists() && !raw_info.isDir() && !QFile::remove(raw_path)) {
  289. QMessageBox::warning(this, tr("Error deleting file"),
  290. tr("Unable to delete existing file: %1.").arg(raw_path));
  291. return;
  292. }
  293. const QString absolute_dst_path = QFileInfo{image_path}.absolutePath();
  294. if (!QDir{raw_path}.mkpath(absolute_dst_path)) {
  295. QMessageBox::warning(
  296. this, tr("Error creating user image directory"),
  297. tr("Unable to create directory %1 for storing user images.").arg(absolute_dst_path));
  298. return;
  299. }
  300. if (!QFile::copy(file, image_path)) {
  301. QMessageBox::warning(this, tr("Error copying user image"),
  302. tr("Unable to copy image from %1 to %2").arg(file, image_path));
  303. return;
  304. }
  305. const auto username = GetAccountUsername(*profile_manager, *uuid);
  306. item_model->setItem(index, 0,
  307. new QStandardItem{GetIcon(*uuid), FormatUserEntryText(username, *uuid)});
  308. UpdateCurrentUser();
  309. }