profile_select.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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;
  52. buttons->addButton(tr("Cancel"), QDialogButtonBox::RejectRole);
  53. buttons->addButton(tr("OK"), QDialogButtonBox::AcceptRole);
  54. connect(buttons, &QDialogButtonBox::accepted, this, &QtProfileSelectionDialog::accept);
  55. connect(buttons, &QDialogButtonBox::rejected, this, &QtProfileSelectionDialog::reject);
  56. outer_layout->addWidget(instruction_label);
  57. outer_layout->addWidget(scroll_area);
  58. outer_layout->addWidget(buttons);
  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. scroll_area->setLayout(layout);
  82. connect(tree_view, &QTreeView::clicked, this, &QtProfileSelectionDialog::SelectUser);
  83. const auto& profiles = profile_manager->GetAllUsers();
  84. for (const auto& user : profiles) {
  85. Service::Account::ProfileBase profile;
  86. if (!profile_manager->GetProfileBase(user, profile))
  87. continue;
  88. const auto username = Common::StringFromFixedZeroTerminatedBuffer(
  89. reinterpret_cast<const char*>(profile.username.data()), profile.username.size());
  90. list_items.push_back(QList<QStandardItem*>{new QStandardItem{
  91. GetIcon(user), FormatUserEntryText(QString::fromStdString(username), user)}});
  92. }
  93. for (const auto& item : list_items)
  94. item_model->appendRow(item);
  95. setLayout(outer_layout);
  96. setWindowTitle(tr("Profile Selector"));
  97. resize(550, 400);
  98. }
  99. QtProfileSelectionDialog::~QtProfileSelectionDialog() = default;
  100. void QtProfileSelectionDialog::accept() {
  101. ok = true;
  102. QDialog::accept();
  103. }
  104. void QtProfileSelectionDialog::reject() {
  105. ok = false;
  106. user_index = 0;
  107. QDialog::reject();
  108. }
  109. bool QtProfileSelectionDialog::GetStatus() const {
  110. return ok;
  111. }
  112. u32 QtProfileSelectionDialog::GetIndex() const {
  113. return user_index;
  114. }
  115. void QtProfileSelectionDialog::SelectUser(const QModelIndex& index) {
  116. user_index = index.row();
  117. }
  118. QtProfileSelector::QtProfileSelector(GMainWindow& parent) {
  119. connect(this, &QtProfileSelector::MainWindowSelectProfile, &parent,
  120. &GMainWindow::ProfileSelectorSelectProfile, Qt::QueuedConnection);
  121. connect(&parent, &GMainWindow::ProfileSelectorFinishedSelection, this,
  122. &QtProfileSelector::MainWindowFinishedSelection, Qt::DirectConnection);
  123. }
  124. QtProfileSelector::~QtProfileSelector() = default;
  125. void QtProfileSelector::SelectProfile(
  126. std::function<void(std::optional<Service::Account::UUID>)> callback) const {
  127. this->callback = std::move(callback);
  128. emit MainWindowSelectProfile();
  129. }
  130. void QtProfileSelector::MainWindowFinishedSelection(std::optional<Service::Account::UUID> uuid) {
  131. // Acquire the HLE mutex
  132. std::lock_guard<std::recursive_mutex> lock(HLE::g_hle_lock);
  133. callback(uuid);
  134. }