qt_profile_select.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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(controller_navigation, &ControllerNavigation::TriggerKeyboardEvent,
  81. [this](Qt::Key key) {
  82. if (!this->isActiveWindow()) {
  83. return;
  84. }
  85. QKeyEvent* event = new QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier);
  86. QCoreApplication::postEvent(tree_view, event);
  87. SelectUser(tree_view->currentIndex());
  88. });
  89. const auto& profiles = profile_manager->GetAllUsers();
  90. for (const auto& user : profiles) {
  91. Service::Account::ProfileBase profile{};
  92. if (!profile_manager->GetProfileBase(user, profile))
  93. continue;
  94. const auto username = Common::StringFromFixedZeroTerminatedBuffer(
  95. reinterpret_cast<const char*>(profile.username.data()), profile.username.size());
  96. list_items.push_back(QList<QStandardItem*>{new QStandardItem{
  97. GetIcon(user), FormatUserEntryText(QString::fromStdString(username), user)}});
  98. }
  99. for (const auto& item : list_items)
  100. item_model->appendRow(item);
  101. setLayout(outer_layout);
  102. SetWindowTitle(parameters);
  103. SetDialogPurpose(parameters);
  104. resize(550, 400);
  105. }
  106. QtProfileSelectionDialog::~QtProfileSelectionDialog() {
  107. controller_navigation->UnloadController();
  108. };
  109. int QtProfileSelectionDialog::exec() {
  110. // Skip profile selection when there's only one.
  111. if (profile_manager->GetUserCount() == 1) {
  112. user_index = 0;
  113. return QDialog::Accepted;
  114. }
  115. return QDialog::exec();
  116. }
  117. void QtProfileSelectionDialog::accept() {
  118. QDialog::accept();
  119. }
  120. void QtProfileSelectionDialog::reject() {
  121. user_index = 0;
  122. QDialog::reject();
  123. }
  124. int QtProfileSelectionDialog::GetIndex() const {
  125. return user_index;
  126. }
  127. void QtProfileSelectionDialog::SelectUser(const QModelIndex& index) {
  128. user_index = index.row();
  129. }
  130. void QtProfileSelectionDialog::SetWindowTitle(
  131. const Core::Frontend::ProfileSelectParameters& parameters) {
  132. using Service::AM::Applets::UiMode;
  133. switch (parameters.mode) {
  134. case UiMode::UserCreator:
  135. case UiMode::UserCreatorForStarter:
  136. setWindowTitle(tr("Profile Creator"));
  137. return;
  138. case UiMode::EnsureNetworkServiceAccountAvailable:
  139. setWindowTitle(tr("Profile Selector"));
  140. return;
  141. case UiMode::UserIconEditor:
  142. setWindowTitle(tr("Profile Icon Editor"));
  143. return;
  144. case UiMode::UserNicknameEditor:
  145. setWindowTitle(tr("Profile Nickname Editor"));
  146. return;
  147. case UiMode::NintendoAccountAuthorizationRequestContext:
  148. case UiMode::IntroduceExternalNetworkServiceAccount:
  149. case UiMode::IntroduceExternalNetworkServiceAccountForRegistration:
  150. case UiMode::NintendoAccountNnidLinker:
  151. case UiMode::LicenseRequirementsForNetworkService:
  152. case UiMode::LicenseRequirementsForNetworkServiceWithUserContextImpl:
  153. case UiMode::UserCreatorForImmediateNaLoginTest:
  154. case UiMode::UserQualificationPromoter:
  155. case UiMode::UserSelector:
  156. default:
  157. setWindowTitle(tr("Profile Selector"));
  158. }
  159. }
  160. void QtProfileSelectionDialog::SetDialogPurpose(
  161. const Core::Frontend::ProfileSelectParameters& parameters) {
  162. using Service::AM::Applets::UserSelectionPurpose;
  163. switch (parameters.purpose) {
  164. case UserSelectionPurpose::GameCardRegistration:
  165. instruction_label->setText(tr("Who will receive the points?"));
  166. return;
  167. case UserSelectionPurpose::EShopLaunch:
  168. instruction_label->setText(tr("Who is using Nintendo eShop?"));
  169. return;
  170. case UserSelectionPurpose::EShopItemShow:
  171. instruction_label->setText(tr("Who is making this purchase?"));
  172. return;
  173. case UserSelectionPurpose::PicturePost:
  174. instruction_label->setText(tr("Who is posting?"));
  175. return;
  176. case UserSelectionPurpose::NintendoAccountLinkage:
  177. instruction_label->setText(tr("Select a user to link to a Nintendo Account."));
  178. return;
  179. case UserSelectionPurpose::SettingsUpdate:
  180. instruction_label->setText(tr("Change settings for which user?"));
  181. return;
  182. case UserSelectionPurpose::SaveDataDeletion:
  183. instruction_label->setText(tr("Format data for which user?"));
  184. return;
  185. case UserSelectionPurpose::UserMigration:
  186. instruction_label->setText(tr("Which user will be transferred to another console?"));
  187. return;
  188. case UserSelectionPurpose::SaveDataTransfer:
  189. instruction_label->setText(tr("Send save data for which user?"));
  190. return;
  191. case UserSelectionPurpose::General:
  192. default:
  193. instruction_label->setText(tr("Select a user:"));
  194. return;
  195. }
  196. }
  197. QtProfileSelector::QtProfileSelector(GMainWindow& parent) {
  198. connect(this, &QtProfileSelector::MainWindowSelectProfile, &parent,
  199. &GMainWindow::ProfileSelectorSelectProfile, Qt::QueuedConnection);
  200. connect(this, &QtProfileSelector::MainWindowRequestExit, &parent,
  201. &GMainWindow::ProfileSelectorRequestExit, Qt::QueuedConnection);
  202. connect(&parent, &GMainWindow::ProfileSelectorFinishedSelection, this,
  203. &QtProfileSelector::MainWindowFinishedSelection, Qt::DirectConnection);
  204. }
  205. QtProfileSelector::~QtProfileSelector() = default;
  206. void QtProfileSelector::Close() const {
  207. callback = {};
  208. emit MainWindowRequestExit();
  209. }
  210. void QtProfileSelector::SelectProfile(
  211. SelectProfileCallback callback_,
  212. const Core::Frontend::ProfileSelectParameters& parameters) const {
  213. callback = std::move(callback_);
  214. emit MainWindowSelectProfile(parameters);
  215. }
  216. void QtProfileSelector::MainWindowFinishedSelection(std::optional<Common::UUID> uuid) {
  217. if (callback) {
  218. callback(uuid);
  219. }
  220. }