2018-11-22 21:03:33 -05:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
#include <QDialogButtonBox>
|
2019-02-16 10:19:29 -05:00
|
|
|
#include <QHeaderView>
|
2018-11-22 21:03:33 -05:00
|
|
|
#include <QLabel>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QScrollArea>
|
|
|
|
#include <QStandardItemModel>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
#include "common/file_util.h"
|
|
|
|
#include "common/string_util.h"
|
2019-06-07 17:46:52 -04:00
|
|
|
#include "core/constants.h"
|
2018-11-22 21:03:33 -05:00
|
|
|
#include "core/hle/lock.h"
|
|
|
|
#include "yuzu/applets/profile_select.h"
|
|
|
|
#include "yuzu/main.h"
|
|
|
|
|
2020-04-16 22:43:33 -04:00
|
|
|
namespace {
|
2019-01-10 21:53:07 -05:00
|
|
|
QString FormatUserEntryText(const QString& username, Common::UUID uuid) {
|
2018-11-22 21:03:33 -05:00
|
|
|
return QtProfileSelectionDialog::tr(
|
|
|
|
"%1\n%2", "%1 is the profile username, %2 is the formatted UUID (e.g. "
|
|
|
|
"00112233-4455-6677-8899-AABBCCDDEEFF))")
|
|
|
|
.arg(username, QString::fromStdString(uuid.FormatSwitch()));
|
|
|
|
}
|
|
|
|
|
2019-01-10 21:53:07 -05:00
|
|
|
QString GetImagePath(Common::UUID uuid) {
|
2020-08-15 08:33:16 -04:00
|
|
|
const auto path = Common::FS::GetUserPath(Common::FS::UserPath::NANDDir) +
|
2018-11-22 21:03:33 -05:00
|
|
|
"/system/save/8000000000000010/su/avators/" + uuid.FormatSwitch() + ".jpg";
|
|
|
|
return QString::fromStdString(path);
|
|
|
|
}
|
|
|
|
|
2019-01-10 21:53:07 -05:00
|
|
|
QPixmap GetIcon(Common::UUID uuid) {
|
2018-11-22 21:03:33 -05:00
|
|
|
QPixmap icon{GetImagePath(uuid)};
|
|
|
|
|
|
|
|
if (!icon) {
|
|
|
|
icon.fill(Qt::black);
|
2019-06-07 17:46:52 -04:00
|
|
|
icon.loadFromData(Core::Constants::ACCOUNT_BACKUP_JPEG.data(),
|
|
|
|
static_cast<u32>(Core::Constants::ACCOUNT_BACKUP_JPEG.size()));
|
2018-11-22 21:03:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return icon.scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
|
|
|
}
|
2020-04-16 22:43:33 -04:00
|
|
|
} // Anonymous namespace
|
2018-11-22 21:03:33 -05:00
|
|
|
|
|
|
|
QtProfileSelectionDialog::QtProfileSelectionDialog(QWidget* parent)
|
|
|
|
: QDialog(parent), profile_manager(std::make_unique<Service::Account::ProfileManager>()) {
|
|
|
|
outer_layout = new QVBoxLayout;
|
|
|
|
|
|
|
|
instruction_label = new QLabel(tr("Select a user:"));
|
|
|
|
|
|
|
|
scroll_area = new QScrollArea;
|
|
|
|
|
2019-04-03 11:06:15 -04:00
|
|
|
buttons = new QDialogButtonBox(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
|
2018-11-22 21:03:33 -05:00
|
|
|
connect(buttons, &QDialogButtonBox::accepted, this, &QtProfileSelectionDialog::accept);
|
|
|
|
connect(buttons, &QDialogButtonBox::rejected, this, &QtProfileSelectionDialog::reject);
|
|
|
|
|
|
|
|
outer_layout->addWidget(instruction_label);
|
|
|
|
outer_layout->addWidget(scroll_area);
|
|
|
|
outer_layout->addWidget(buttons);
|
|
|
|
|
|
|
|
layout = new QVBoxLayout;
|
|
|
|
tree_view = new QTreeView;
|
|
|
|
item_model = new QStandardItemModel(tree_view);
|
|
|
|
tree_view->setModel(item_model);
|
|
|
|
|
|
|
|
tree_view->setAlternatingRowColors(true);
|
|
|
|
tree_view->setSelectionMode(QHeaderView::SingleSelection);
|
|
|
|
tree_view->setSelectionBehavior(QHeaderView::SelectRows);
|
|
|
|
tree_view->setVerticalScrollMode(QHeaderView::ScrollPerPixel);
|
|
|
|
tree_view->setHorizontalScrollMode(QHeaderView::ScrollPerPixel);
|
|
|
|
tree_view->setSortingEnabled(true);
|
|
|
|
tree_view->setEditTriggers(QHeaderView::NoEditTriggers);
|
|
|
|
tree_view->setUniformRowHeights(true);
|
|
|
|
tree_view->setIconSize({64, 64});
|
|
|
|
tree_view->setContextMenuPolicy(Qt::NoContextMenu);
|
|
|
|
|
|
|
|
item_model->insertColumns(0, 1);
|
2019-05-19 01:17:53 -04:00
|
|
|
item_model->setHeaderData(0, Qt::Horizontal, tr("Users"));
|
2018-11-22 21:03:33 -05:00
|
|
|
|
|
|
|
// We must register all custom types with the Qt Automoc system so that we are able to use it
|
2019-05-19 01:17:53 -04:00
|
|
|
// with signals/slots. In this case, QList falls under the umbrella of custom types.
|
2018-11-22 21:03:33 -05:00
|
|
|
qRegisterMetaType<QList<QStandardItem*>>("QList<QStandardItem*>");
|
|
|
|
|
|
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
layout->setSpacing(0);
|
|
|
|
layout->addWidget(tree_view);
|
|
|
|
|
|
|
|
scroll_area->setLayout(layout);
|
|
|
|
|
|
|
|
connect(tree_view, &QTreeView::clicked, this, &QtProfileSelectionDialog::SelectUser);
|
|
|
|
|
|
|
|
const auto& profiles = profile_manager->GetAllUsers();
|
|
|
|
for (const auto& user : profiles) {
|
|
|
|
Service::Account::ProfileBase profile;
|
|
|
|
if (!profile_manager->GetProfileBase(user, profile))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto username = Common::StringFromFixedZeroTerminatedBuffer(
|
|
|
|
reinterpret_cast<const char*>(profile.username.data()), profile.username.size());
|
|
|
|
|
|
|
|
list_items.push_back(QList<QStandardItem*>{new QStandardItem{
|
|
|
|
GetIcon(user), FormatUserEntryText(QString::fromStdString(username), user)}});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& item : list_items)
|
|
|
|
item_model->appendRow(item);
|
|
|
|
|
|
|
|
setLayout(outer_layout);
|
|
|
|
setWindowTitle(tr("Profile Selector"));
|
|
|
|
resize(550, 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
QtProfileSelectionDialog::~QtProfileSelectionDialog() = default;
|
|
|
|
|
2020-10-21 14:36:57 -04:00
|
|
|
int QtProfileSelectionDialog::exec() {
|
|
|
|
// Skip profile selection when there's only one.
|
|
|
|
if (profile_manager->GetUserCount() == 1) {
|
|
|
|
user_index = 0;
|
|
|
|
return QDialog::Accepted;
|
|
|
|
}
|
2020-10-25 09:16:43 -04:00
|
|
|
return QDialog::exec();
|
2020-10-21 14:36:57 -04:00
|
|
|
}
|
|
|
|
|
2018-11-22 21:03:33 -05:00
|
|
|
void QtProfileSelectionDialog::accept() {
|
|
|
|
QDialog::accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QtProfileSelectionDialog::reject() {
|
|
|
|
user_index = 0;
|
|
|
|
QDialog::reject();
|
|
|
|
}
|
|
|
|
|
2019-05-29 00:23:46 -04:00
|
|
|
int QtProfileSelectionDialog::GetIndex() const {
|
2018-11-22 21:03:33 -05:00
|
|
|
return user_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QtProfileSelectionDialog::SelectUser(const QModelIndex& index) {
|
|
|
|
user_index = index.row();
|
|
|
|
}
|
|
|
|
|
|
|
|
QtProfileSelector::QtProfileSelector(GMainWindow& parent) {
|
|
|
|
connect(this, &QtProfileSelector::MainWindowSelectProfile, &parent,
|
|
|
|
&GMainWindow::ProfileSelectorSelectProfile, Qt::QueuedConnection);
|
|
|
|
connect(&parent, &GMainWindow::ProfileSelectorFinishedSelection, this,
|
|
|
|
&QtProfileSelector::MainWindowFinishedSelection, Qt::DirectConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
QtProfileSelector::~QtProfileSelector() = default;
|
|
|
|
|
|
|
|
void QtProfileSelector::SelectProfile(
|
2019-01-10 21:53:07 -05:00
|
|
|
std::function<void(std::optional<Common::UUID>)> callback) const {
|
2018-11-22 21:03:33 -05:00
|
|
|
this->callback = std::move(callback);
|
|
|
|
emit MainWindowSelectProfile();
|
|
|
|
}
|
|
|
|
|
2019-01-10 21:53:07 -05:00
|
|
|
void QtProfileSelector::MainWindowFinishedSelection(std::optional<Common::UUID> uuid) {
|
2018-11-22 21:03:33 -05:00
|
|
|
// Acquire the HLE mutex
|
2019-04-01 12:29:59 -04:00
|
|
|
std::lock_guard lock{HLE::g_hle_lock};
|
2018-11-22 21:03:33 -05:00
|
|
|
callback(uuid);
|
|
|
|
}
|