configure_system.cpp 12 KB

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