profile_select.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <mutex>
  5. #include <QDialogButtonBox>
  6. #include <QHeaderView>
  7. #include <QLabel>
  8. #include <QLineEdit>
  9. #include <QScrollArea>
  10. #include <QStandardItemModel>
  11. #include <QVBoxLayout>
  12. #include "common/file_util.h"
  13. #include "common/string_util.h"
  14. #include "core/hle/lock.h"
  15. #include "yuzu/applets/profile_select.h"
  16. #include "yuzu/main.h"
  17. // Same backup JPEG used by acc IProfile::GetImage if no jpeg found
  18. constexpr std::array<u8, 107> backup_jpeg{
  19. 0xff, 0xd8, 0xff, 0xdb, 0x00, 0x43, 0x00, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02,
  20. 0x02, 0x03, 0x03, 0x03, 0x03, 0x04, 0x06, 0x04, 0x04, 0x04, 0x04, 0x04, 0x08, 0x06, 0x06, 0x05,
  21. 0x06, 0x09, 0x08, 0x0a, 0x0a, 0x09, 0x08, 0x09, 0x09, 0x0a, 0x0c, 0x0f, 0x0c, 0x0a, 0x0b, 0x0e,
  22. 0x0b, 0x09, 0x09, 0x0d, 0x11, 0x0d, 0x0e, 0x0f, 0x10, 0x10, 0x11, 0x10, 0x0a, 0x0c, 0x12, 0x13,
  23. 0x12, 0x10, 0x13, 0x0f, 0x10, 0x10, 0x10, 0xff, 0xc9, 0x00, 0x0b, 0x08, 0x00, 0x01, 0x00, 0x01,
  24. 0x01, 0x01, 0x11, 0x00, 0xff, 0xcc, 0x00, 0x06, 0x00, 0x10, 0x10, 0x05, 0xff, 0xda, 0x00, 0x08,
  25. 0x01, 0x01, 0x00, 0x00, 0x3f, 0x00, 0xd2, 0xcf, 0x20, 0xff, 0xd9,
  26. };
  27. QString FormatUserEntryText(const QString& username, Service::Account::UUID uuid) {
  28. return QtProfileSelectionDialog::tr(
  29. "%1\n%2", "%1 is the profile username, %2 is the formatted UUID (e.g. "
  30. "00112233-4455-6677-8899-AABBCCDDEEFF))")
  31. .arg(username, QString::fromStdString(uuid.FormatSwitch()));
  32. }
  33. QString GetImagePath(Service::Account::UUID uuid) {
  34. const auto path = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) +
  35. "/system/save/8000000000000010/su/avators/" + uuid.FormatSwitch() + ".jpg";
  36. return QString::fromStdString(path);
  37. }
  38. QPixmap GetIcon(Service::Account::UUID uuid) {
  39. QPixmap icon{GetImagePath(uuid)};
  40. if (!icon) {
  41. icon.fill(Qt::black);
  42. icon.loadFromData(backup_jpeg.data(), static_cast<u32>(backup_jpeg.size()));
  43. }
  44. return icon.scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  45. }
  46. QtProfileSelectionDialog::QtProfileSelectionDialog(QWidget* parent)
  47. : QDialog(parent), profile_manager(std::make_unique<Service::Account::ProfileManager>()) {
  48. outer_layout = new QVBoxLayout;
  49. instruction_label = new QLabel(tr("Select a user:"));
  50. scroll_area = new QScrollArea;
  51. buttons = new QDialogButtonBox(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
  52. connect(buttons, &QDialogButtonBox::accepted, this, &QtProfileSelectionDialog::accept);
  53. connect(buttons, &QDialogButtonBox::rejected, this, &QtProfileSelectionDialog::reject);
  54. outer_layout->addWidget(instruction_label);
  55. outer_layout->addWidget(scroll_area);
  56. outer_layout->addWidget(buttons);
  57. layout = new QVBoxLayout;
  58. tree_view = new QTreeView;
  59. item_model = new QStandardItemModel(tree_view);
  60. tree_view->setModel(item_model);
  61. tree_view->setAlternatingRowColors(true);
  62. tree_view->setSelectionMode(QHeaderView::SingleSelection);
  63. tree_view->setSelectionBehavior(QHeaderView::SelectRows);
  64. tree_view->setVerticalScrollMode(QHeaderView::ScrollPerPixel);
  65. tree_view->setHorizontalScrollMode(QHeaderView::ScrollPerPixel);
  66. tree_view->setSortingEnabled(true);
  67. tree_view->setEditTriggers(QHeaderView::NoEditTriggers);
  68. tree_view->setUniformRowHeights(true);
  69. tree_view->setIconSize({64, 64});
  70. tree_view->setContextMenuPolicy(Qt::NoContextMenu);
  71. item_model->insertColumns(0, 1);
  72. item_model->setHeaderData(0, Qt::Horizontal, tr("Users"));
  73. // We must register all custom types with the Qt Automoc system so that we are able to use it
  74. // with signals/slots. In this case, QList falls under the umbrella of custom types.
  75. qRegisterMetaType<QList<QStandardItem*>>("QList<QStandardItem*>");
  76. layout->setContentsMargins(0, 0, 0, 0);
  77. layout->setSpacing(0);
  78. layout->addWidget(tree_view);
  79. scroll_area->setLayout(layout);
  80. connect(tree_view, &QTreeView::clicked, this, &QtProfileSelectionDialog::SelectUser);
  81. const auto& profiles = profile_manager->GetAllUsers();
  82. for (const auto& user : profiles) {
  83. Service::Account::ProfileBase profile;
  84. if (!profile_manager->GetProfileBase(user, profile))
  85. continue;
  86. const auto username = Common::StringFromFixedZeroTerminatedBuffer(
  87. reinterpret_cast<const char*>(profile.username.data()), profile.username.size());
  88. list_items.push_back(QList<QStandardItem*>{new QStandardItem{
  89. GetIcon(user), FormatUserEntryText(QString::fromStdString(username), user)}});
  90. }
  91. for (const auto& item : list_items)
  92. item_model->appendRow(item);
  93. setLayout(outer_layout);
  94. setWindowTitle(tr("Profile Selector"));
  95. resize(550, 400);
  96. }
  97. QtProfileSelectionDialog::~QtProfileSelectionDialog() = default;
  98. void QtProfileSelectionDialog::accept() {
  99. ok = true;
  100. QDialog::accept();
  101. }
  102. void QtProfileSelectionDialog::reject() {
  103. ok = false;
  104. user_index = 0;
  105. QDialog::reject();
  106. }
  107. bool QtProfileSelectionDialog::GetStatus() const {
  108. return ok;
  109. }
  110. int QtProfileSelectionDialog::GetIndex() const {
  111. return user_index;
  112. }
  113. void QtProfileSelectionDialog::SelectUser(const QModelIndex& index) {
  114. user_index = index.row();
  115. }
  116. QtProfileSelector::QtProfileSelector(GMainWindow& parent) {
  117. connect(this, &QtProfileSelector::MainWindowSelectProfile, &parent,
  118. &GMainWindow::ProfileSelectorSelectProfile, Qt::QueuedConnection);
  119. connect(&parent, &GMainWindow::ProfileSelectorFinishedSelection, this,
  120. &QtProfileSelector::MainWindowFinishedSelection, Qt::DirectConnection);
  121. }
  122. QtProfileSelector::~QtProfileSelector() = default;
  123. void QtProfileSelector::SelectProfile(
  124. std::function<void(std::optional<Service::Account::UUID>)> callback) const {
  125. this->callback = std::move(callback);
  126. emit MainWindowSelectProfile();
  127. }
  128. void QtProfileSelector::MainWindowFinishedSelection(std::optional<Service::Account::UUID> uuid) {
  129. // Acquire the HLE mutex
  130. std::lock_guard lock{HLE::g_hle_lock};
  131. callback(uuid);
  132. }