qt_profile_select.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <mutex>
  4. #include <QApplication>
  5. #include <QDialogButtonBox>
  6. #include <QHeaderView>
  7. #include <QLabel>
  8. #include <QLineEdit>
  9. #include <QScrollArea>
  10. #include <QStandardItemModel>
  11. #include <QTreeView>
  12. #include <QVBoxLayout>
  13. #include "common/fs/path_util.h"
  14. #include "common/string_util.h"
  15. #include "core/constants.h"
  16. #include "yuzu/applets/qt_profile_select.h"
  17. #include "yuzu/main.h"
  18. #include "yuzu/util/controller_navigation.h"
  19. namespace {
  20. QString FormatUserEntryText(const QString& username, Common::UUID uuid) {
  21. return QtProfileSelectionDialog::tr(
  22. "%1\n%2", "%1 is the profile username, %2 is the formatted UUID (e.g. "
  23. "00112233-4455-6677-8899-AABBCCDDEEFF))")
  24. .arg(username, QString::fromStdString(uuid.FormattedString()));
  25. }
  26. QString GetImagePath(Common::UUID uuid) {
  27. const auto path =
  28. Common::FS::GetYuzuPath(Common::FS::YuzuPath::NANDDir) /
  29. fmt::format("system/save/8000000000000010/su/avators/{}.jpg", uuid.FormattedString());
  30. return QString::fromStdString(Common::FS::PathToUTF8String(path));
  31. }
  32. QPixmap GetIcon(Common::UUID uuid) {
  33. QPixmap icon{GetImagePath(uuid)};
  34. if (!icon) {
  35. icon.fill(Qt::black);
  36. icon.loadFromData(Core::Constants::ACCOUNT_BACKUP_JPEG.data(),
  37. static_cast<u32>(Core::Constants::ACCOUNT_BACKUP_JPEG.size()));
  38. }
  39. return icon.scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  40. }
  41. } // Anonymous namespace
  42. QtProfileSelectionDialog::QtProfileSelectionDialog(
  43. Core::HID::HIDCore& hid_core, QWidget* parent,
  44. const Core::Frontend::ProfileSelectParameters& parameters)
  45. : QDialog(parent), profile_manager(std::make_unique<Service::Account::ProfileManager>()) {
  46. outer_layout = new QVBoxLayout;
  47. instruction_label = new QLabel();
  48. scroll_area = new QScrollArea;
  49. buttons = new QDialogButtonBox(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
  50. connect(buttons, &QDialogButtonBox::accepted, this, &QtProfileSelectionDialog::accept);
  51. connect(buttons, &QDialogButtonBox::rejected, this, &QtProfileSelectionDialog::reject);
  52. outer_layout->addWidget(instruction_label);
  53. outer_layout->addWidget(scroll_area);
  54. outer_layout->addWidget(buttons);
  55. layout = new QVBoxLayout;
  56. tree_view = new QTreeView;
  57. item_model = new QStandardItemModel(tree_view);
  58. tree_view->setModel(item_model);
  59. controller_navigation = new ControllerNavigation(hid_core, this);
  60. tree_view->setAlternatingRowColors(true);
  61. tree_view->setSelectionMode(QHeaderView::SingleSelection);
  62. tree_view->setSelectionBehavior(QHeaderView::SelectRows);
  63. tree_view->setVerticalScrollMode(QHeaderView::ScrollPerPixel);
  64. tree_view->setHorizontalScrollMode(QHeaderView::ScrollPerPixel);
  65. tree_view->setSortingEnabled(true);
  66. tree_view->setEditTriggers(QHeaderView::NoEditTriggers);
  67. tree_view->setUniformRowHeights(true);
  68. tree_view->setIconSize({64, 64});
  69. tree_view->setContextMenuPolicy(Qt::NoContextMenu);
  70. item_model->insertColumns(0, 1);
  71. item_model->setHeaderData(0, Qt::Horizontal, tr("Users"));
  72. // We must register all custom types with the Qt Automoc system so that we are able to use it
  73. // with signals/slots. In this case, QList falls under the umbrella of custom types.
  74. qRegisterMetaType<QList<QStandardItem*>>("QList<QStandardItem*>");
  75. layout->setContentsMargins(0, 0, 0, 0);
  76. layout->setSpacing(0);
  77. layout->addWidget(tree_view);
  78. scroll_area->setLayout(layout);
  79. connect(tree_view, &QTreeView::clicked, this, &QtProfileSelectionDialog::SelectUser);
  80. connect(tree_view, &QTreeView::doubleClicked, this, &QtProfileSelectionDialog::accept);
  81. connect(controller_navigation, &ControllerNavigation::TriggerKeyboardEvent,
  82. [this](Qt::Key key) {
  83. if (!this->isActiveWindow()) {
  84. return;
  85. }
  86. QKeyEvent* event = new QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier);
  87. QCoreApplication::postEvent(tree_view, event);
  88. SelectUser(tree_view->currentIndex());
  89. });
  90. const auto& profiles = profile_manager->GetAllUsers();
  91. for (const auto& user : profiles) {
  92. Service::Account::ProfileBase profile{};
  93. if (!profile_manager->GetProfileBase(user, profile))
  94. continue;
  95. const auto username = Common::StringFromFixedZeroTerminatedBuffer(
  96. reinterpret_cast<const char*>(profile.username.data()), profile.username.size());
  97. list_items.push_back(QList<QStandardItem*>{new QStandardItem{
  98. GetIcon(user), FormatUserEntryText(QString::fromStdString(username), user)}});
  99. }
  100. for (const auto& item : list_items)
  101. item_model->appendRow(item);
  102. setLayout(outer_layout);
  103. SetWindowTitle(parameters);
  104. SetDialogPurpose(parameters);
  105. resize(550, 400);
  106. }
  107. QtProfileSelectionDialog::~QtProfileSelectionDialog() {
  108. controller_navigation->UnloadController();
  109. };
  110. int QtProfileSelectionDialog::exec() {
  111. // Skip profile selection when there's only one.
  112. if (profile_manager->GetUserCount() == 1) {
  113. user_index = 0;
  114. return QDialog::Accepted;
  115. }
  116. return QDialog::exec();
  117. }
  118. void QtProfileSelectionDialog::accept() {
  119. QDialog::accept();
  120. }
  121. void QtProfileSelectionDialog::reject() {
  122. user_index = 0;
  123. QDialog::reject();
  124. }
  125. int QtProfileSelectionDialog::GetIndex() const {
  126. return user_index;
  127. }
  128. void QtProfileSelectionDialog::SelectUser(const QModelIndex& index) {
  129. user_index = index.row();
  130. }
  131. void QtProfileSelectionDialog::SetWindowTitle(
  132. const Core::Frontend::ProfileSelectParameters& parameters) {
  133. using Service::AM::Applets::UiMode;
  134. switch (parameters.mode) {
  135. case UiMode::UserCreator:
  136. case UiMode::UserCreatorForStarter:
  137. setWindowTitle(tr("Profile Creator"));
  138. return;
  139. case UiMode::EnsureNetworkServiceAccountAvailable:
  140. setWindowTitle(tr("Profile Selector"));
  141. return;
  142. case UiMode::UserIconEditor:
  143. setWindowTitle(tr("Profile Icon Editor"));
  144. return;
  145. case UiMode::UserNicknameEditor:
  146. setWindowTitle(tr("Profile Nickname Editor"));
  147. return;
  148. case UiMode::NintendoAccountAuthorizationRequestContext:
  149. case UiMode::IntroduceExternalNetworkServiceAccount:
  150. case UiMode::IntroduceExternalNetworkServiceAccountForRegistration:
  151. case UiMode::NintendoAccountNnidLinker:
  152. case UiMode::LicenseRequirementsForNetworkService:
  153. case UiMode::LicenseRequirementsForNetworkServiceWithUserContextImpl:
  154. case UiMode::UserCreatorForImmediateNaLoginTest:
  155. case UiMode::UserQualificationPromoter:
  156. case UiMode::UserSelector:
  157. default:
  158. setWindowTitle(tr("Profile Selector"));
  159. }
  160. }
  161. void QtProfileSelectionDialog::SetDialogPurpose(
  162. const Core::Frontend::ProfileSelectParameters& parameters) {
  163. using Service::AM::Applets::UserSelectionPurpose;
  164. switch (parameters.purpose) {
  165. case UserSelectionPurpose::GameCardRegistration:
  166. instruction_label->setText(tr("Who will receive the points?"));
  167. return;
  168. case UserSelectionPurpose::EShopLaunch:
  169. instruction_label->setText(tr("Who is using Nintendo eShop?"));
  170. return;
  171. case UserSelectionPurpose::EShopItemShow:
  172. instruction_label->setText(tr("Who is making this purchase?"));
  173. return;
  174. case UserSelectionPurpose::PicturePost:
  175. instruction_label->setText(tr("Who is posting?"));
  176. return;
  177. case UserSelectionPurpose::NintendoAccountLinkage:
  178. instruction_label->setText(tr("Select a user to link to a Nintendo Account."));
  179. return;
  180. case UserSelectionPurpose::SettingsUpdate:
  181. instruction_label->setText(tr("Change settings for which user?"));
  182. return;
  183. case UserSelectionPurpose::SaveDataDeletion:
  184. instruction_label->setText(tr("Format data for which user?"));
  185. return;
  186. case UserSelectionPurpose::UserMigration:
  187. instruction_label->setText(tr("Which user will be transferred to another console?"));
  188. return;
  189. case UserSelectionPurpose::SaveDataTransfer:
  190. instruction_label->setText(tr("Send save data for which user?"));
  191. return;
  192. case UserSelectionPurpose::General:
  193. default:
  194. instruction_label->setText(tr("Select a user:"));
  195. return;
  196. }
  197. }
  198. QtProfileSelector::QtProfileSelector(GMainWindow& parent) {
  199. connect(this, &QtProfileSelector::MainWindowSelectProfile, &parent,
  200. &GMainWindow::ProfileSelectorSelectProfile, Qt::QueuedConnection);
  201. connect(this, &QtProfileSelector::MainWindowRequestExit, &parent,
  202. &GMainWindow::ProfileSelectorRequestExit, Qt::QueuedConnection);
  203. connect(&parent, &GMainWindow::ProfileSelectorFinishedSelection, this,
  204. &QtProfileSelector::MainWindowFinishedSelection, Qt::DirectConnection);
  205. }
  206. QtProfileSelector::~QtProfileSelector() = default;
  207. void QtProfileSelector::Close() const {
  208. callback = {};
  209. emit MainWindowRequestExit();
  210. }
  211. void QtProfileSelector::SelectProfile(
  212. SelectProfileCallback callback_,
  213. const Core::Frontend::ProfileSelectParameters& parameters) const {
  214. callback = std::move(callback_);
  215. emit MainWindowSelectProfile(parameters);
  216. }
  217. void QtProfileSelector::MainWindowFinishedSelection(std::optional<Common::UUID> uuid) {
  218. if (callback) {
  219. callback(uuid);
  220. }
  221. }