configure_system.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. scene = new QGraphicsScene;
  120. ui->current_user_icon->setScene(scene);
  121. this->setConfiguration();
  122. }
  123. ConfigureSystem::~ConfigureSystem() = default;
  124. void ConfigureSystem::setConfiguration() {
  125. enabled = !Core::System::GetInstance().IsPoweredOn();
  126. ui->combo_language->setCurrentIndex(Settings::values.language_index);
  127. item_model->removeRows(0, item_model->rowCount());
  128. list_items.clear();
  129. PopulateUserList();
  130. UpdateCurrentUser();
  131. }
  132. void ConfigureSystem::PopulateUserList() {
  133. const auto& profiles = profile_manager->GetAllUsers();
  134. for (const auto& user : profiles) {
  135. Service::Account::ProfileBase profile;
  136. if (!profile_manager->GetProfileBase(user, profile))
  137. continue;
  138. const auto username = Common::StringFromFixedZeroTerminatedBuffer(
  139. reinterpret_cast<const char*>(profile.username.data()), profile.username.size());
  140. list_items.push_back(QList<QStandardItem*>{new QStandardItem{
  141. GetIcon(user), FormatUserEntryText(QString::fromStdString(username), user)}});
  142. }
  143. for (const auto& item : list_items)
  144. item_model->appendRow(item);
  145. }
  146. void ConfigureSystem::UpdateCurrentUser() {
  147. ui->pm_add->setEnabled(profile_manager->GetUserCount() < Service::Account::MAX_USERS);
  148. const auto& current_user = profile_manager->GetUser(Settings::values.current_user);
  149. ASSERT(current_user);
  150. const auto username = GetAccountUsername(*profile_manager, *current_user);
  151. scene->clear();
  152. scene->addPixmap(
  153. GetIcon(*current_user).scaled(48, 48, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
  154. ui->current_user_username->setText(username);
  155. }
  156. void ConfigureSystem::ReadSystemSettings() {}
  157. void ConfigureSystem::applyConfiguration() {
  158. if (!enabled)
  159. return;
  160. Settings::values.language_index = ui->combo_language->currentIndex();
  161. Settings::Apply();
  162. }
  163. void ConfigureSystem::UpdateBirthdayComboBox(int birthmonth_index) {
  164. if (birthmonth_index < 0 || birthmonth_index >= 12)
  165. return;
  166. // store current day selection
  167. int birthday_index = ui->combo_birthday->currentIndex();
  168. // get number of days in the new selected month
  169. int days = days_in_month[birthmonth_index];
  170. // if the selected day is out of range,
  171. // reset it to 1st
  172. if (birthday_index < 0 || birthday_index >= days)
  173. birthday_index = 0;
  174. // update the day combo box
  175. ui->combo_birthday->clear();
  176. for (int i = 1; i <= days; ++i) {
  177. ui->combo_birthday->addItem(QString::number(i));
  178. }
  179. // restore the day selection
  180. ui->combo_birthday->setCurrentIndex(birthday_index);
  181. }
  182. void ConfigureSystem::RefreshConsoleID() {
  183. QMessageBox::StandardButton reply;
  184. QString warning_text = tr("This will replace your current virtual Switch with a new one. "
  185. "Your current virtual Switch will not be recoverable. "
  186. "This might have unexpected effects in games. This might fail, "
  187. "if you use an outdated config savegame. Continue?");
  188. reply = QMessageBox::critical(this, tr("Warning"), warning_text,
  189. QMessageBox::No | QMessageBox::Yes);
  190. if (reply == QMessageBox::No)
  191. return;
  192. u64 console_id{};
  193. ui->label_console_id->setText(
  194. tr("Console ID: 0x%1").arg(QString::number(console_id, 16).toUpper()));
  195. }
  196. void ConfigureSystem::SelectUser(const QModelIndex& index) {
  197. Settings::values.current_user =
  198. std::clamp<std::size_t>(index.row(), 0, profile_manager->GetUserCount() - 1);
  199. UpdateCurrentUser();
  200. ui->pm_remove->setEnabled(profile_manager->GetUserCount() >= 2);
  201. ui->pm_rename->setEnabled(true);
  202. ui->pm_set_image->setEnabled(true);
  203. }
  204. void ConfigureSystem::AddUser() {
  205. const auto username =
  206. GetProfileUsernameFromUser(this, tr("Enter a username for the new user:"));
  207. if (username.isEmpty()) {
  208. return;
  209. }
  210. const auto uuid = Service::Account::UUID::Generate();
  211. profile_manager->CreateNewUser(uuid, username.toStdString());
  212. item_model->appendRow(new QStandardItem{GetIcon(uuid), FormatUserEntryText(username, uuid)});
  213. }
  214. void ConfigureSystem::RenameUser() {
  215. const auto user = tree_view->currentIndex().row();
  216. const auto uuid = profile_manager->GetUser(user);
  217. ASSERT(uuid);
  218. Service::Account::ProfileBase profile;
  219. if (!profile_manager->GetProfileBase(*uuid, profile))
  220. return;
  221. const auto new_username = GetProfileUsernameFromUser(this, tr("Enter a new username:"));
  222. if (new_username.isEmpty()) {
  223. return;
  224. }
  225. const auto username_std = new_username.toStdString();
  226. std::fill(profile.username.begin(), profile.username.end(), '\0');
  227. std::copy(username_std.begin(), username_std.end(), profile.username.begin());
  228. profile_manager->SetProfileBase(*uuid, profile);
  229. item_model->setItem(
  230. user, 0,
  231. new QStandardItem{GetIcon(*uuid),
  232. FormatUserEntryText(QString::fromStdString(username_std), *uuid)});
  233. UpdateCurrentUser();
  234. }
  235. void ConfigureSystem::DeleteUser() {
  236. const auto index = tree_view->currentIndex().row();
  237. const auto uuid = profile_manager->GetUser(index);
  238. ASSERT(uuid);
  239. const auto username = GetAccountUsername(*profile_manager, *uuid);
  240. const auto confirm = QMessageBox::question(
  241. this, tr("Confirm Delete"),
  242. tr("You are about to delete user with name \"%1\". Are you sure?").arg(username));
  243. if (confirm == QMessageBox::No)
  244. return;
  245. if (Settings::values.current_user == tree_view->currentIndex().row())
  246. Settings::values.current_user = 0;
  247. UpdateCurrentUser();
  248. if (!profile_manager->RemoveUser(*uuid))
  249. return;
  250. item_model->removeRows(tree_view->currentIndex().row(), 1);
  251. tree_view->clearSelection();
  252. ui->pm_remove->setEnabled(false);
  253. ui->pm_rename->setEnabled(false);
  254. }
  255. void ConfigureSystem::SetUserImage() {
  256. const auto index = tree_view->currentIndex().row();
  257. const auto uuid = profile_manager->GetUser(index);
  258. ASSERT(uuid);
  259. const auto file = QFileDialog::getOpenFileName(this, tr("Select User Image"), QString(),
  260. tr("JPEG Images (*.jpg *.jpeg)"));
  261. if (file.isEmpty()) {
  262. return;
  263. }
  264. const auto image_path = GetImagePath(*uuid);
  265. if (QFile::exists(image_path) && !QFile::remove(image_path)) {
  266. QMessageBox::warning(
  267. this, tr("Error deleting image"),
  268. tr("Error occurred attempting to overwrite previous image at: %1.").arg(image_path));
  269. return;
  270. }
  271. const auto raw_path = QString::fromStdString(
  272. FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + "/system/save/8000000000000010");
  273. const QFileInfo raw_info{raw_path};
  274. if (raw_info.exists() && !raw_info.isDir() && !QFile::remove(raw_path)) {
  275. QMessageBox::warning(this, tr("Error deleting file"),
  276. tr("Unable to delete existing file: %1.").arg(raw_path));
  277. return;
  278. }
  279. const QString absolute_dst_path = QFileInfo{image_path}.absolutePath();
  280. if (!QDir{raw_path}.mkpath(absolute_dst_path)) {
  281. QMessageBox::warning(
  282. this, tr("Error creating user image directory"),
  283. tr("Unable to create directory %1 for storing user images.").arg(absolute_dst_path));
  284. return;
  285. }
  286. if (!QFile::copy(file, image_path)) {
  287. QMessageBox::warning(this, tr("Error copying user image"),
  288. tr("Unable to copy image from %1 to %2").arg(file, image_path));
  289. return;
  290. }
  291. const auto username = GetAccountUsername(*profile_manager, *uuid);
  292. item_model->setItem(index, 0,
  293. new QStandardItem{GetIcon(*uuid), FormatUserEntryText(username, *uuid)});
  294. UpdateCurrentUser();
  295. }