|
|
|
@ -2,10 +2,15 @@ |
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QGraphicsItem>
|
|
|
|
#include <QList>
|
|
|
|
#include <QGraphicsScene>
|
|
|
|
#include <QInputDialog>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <qinputdialog.h>
|
|
|
|
#include <QStandardItemModel>
|
|
|
|
#include <QTreeView>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
#include "common/common_paths.h"
|
|
|
|
#include "common/logging/backend.h"
|
|
|
|
#include "core/core.h"
|
|
|
|
@ -14,6 +19,11 @@ |
|
|
|
#include "yuzu/configuration/configure_system.h"
|
|
|
|
#include "yuzu/main.h"
|
|
|
|
|
|
|
|
static std::string GetImagePath(Service::Account::UUID uuid) { |
|
|
|
return FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + |
|
|
|
"/system/save/8000000000000010/su/avators/" + uuid.FormatSwitch() + ".jpg"; |
|
|
|
} |
|
|
|
|
|
|
|
static const std::array<int, 12> days_in_month = {{ |
|
|
|
31, |
|
|
|
29, |
|
|
|
@ -40,7 +50,9 @@ static constexpr std::array<u8, 107> backup_jpeg{ |
|
|
|
0x01, 0x01, 0x00, 0x00, 0x3f, 0x00, 0xd2, 0xcf, 0x20, 0xff, 0xd9, |
|
|
|
}; |
|
|
|
|
|
|
|
ConfigureSystem::ConfigureSystem(QWidget* parent) : QWidget(parent), ui(new Ui::ConfigureSystem) { |
|
|
|
ConfigureSystem::ConfigureSystem(QWidget* parent) |
|
|
|
: QWidget(parent), ui(new Ui::ConfigureSystem), |
|
|
|
profile_manager(std::make_unique<Service::Account::ProfileManager>()) { |
|
|
|
ui->setupUi(this); |
|
|
|
connect(ui->combo_birthmonth, |
|
|
|
static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, |
|
|
|
@ -82,6 +94,7 @@ ConfigureSystem::ConfigureSystem(QWidget* parent) : QWidget(parent), ui(new Ui:: |
|
|
|
connect(ui->pm_add, &QPushButton::pressed, this, &ConfigureSystem::AddUser); |
|
|
|
connect(ui->pm_rename, &QPushButton::pressed, this, &ConfigureSystem::RenameUser); |
|
|
|
connect(ui->pm_remove, &QPushButton::pressed, this, &ConfigureSystem::DeleteUser); |
|
|
|
connect(ui->pm_set_image, &QPushButton::pressed, this, &ConfigureSystem::SetUserImage); |
|
|
|
|
|
|
|
scene = new QGraphicsScene; |
|
|
|
ui->current_user_icon->setScene(scene); |
|
|
|
@ -99,49 +112,69 @@ void ConfigureSystem::setConfiguration() { |
|
|
|
item_model->removeRows(0, item_model->rowCount()); |
|
|
|
list_items.clear(); |
|
|
|
|
|
|
|
std::transform(Settings::values.users.begin(), Settings::values.users.end(), |
|
|
|
std::back_inserter(list_items), |
|
|
|
[](const std::pair<std::string, Service::Account::UUID>& user) { |
|
|
|
const auto icon_url = QString::fromStdString( |
|
|
|
FileUtil::GetUserPath(FileUtil::UserPath::ConfigDir) + "users" + |
|
|
|
DIR_SEP + user.first + ".jpg"); |
|
|
|
QPixmap icon{icon_url}; |
|
|
|
|
|
|
|
if (!icon) { |
|
|
|
icon.fill(QColor::fromRgb(0, 0, 0)); |
|
|
|
icon.loadFromData(backup_jpeg.data(), backup_jpeg.size()); |
|
|
|
} |
|
|
|
|
|
|
|
return QList{new QStandardItem{ |
|
|
|
icon.scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), |
|
|
|
QString::fromStdString(user.first + "\n" + user.second.Format())}}; |
|
|
|
}); |
|
|
|
|
|
|
|
for (const auto& item : list_items) |
|
|
|
item_model->appendRow(item); |
|
|
|
ui->pm_add->setEnabled(profile_manager->GetUserCount() < 8); |
|
|
|
|
|
|
|
PopulateUserList(); |
|
|
|
UpdateCurrentUser(); |
|
|
|
} |
|
|
|
|
|
|
|
void ConfigureSystem::UpdateCurrentUser() { |
|
|
|
const auto& current_user = Settings::values.users[Settings::values.current_user]; |
|
|
|
const auto icon_url = |
|
|
|
QString::fromStdString(FileUtil::GetUserPath(FileUtil::UserPath::ConfigDir) + "users" + |
|
|
|
DIR_SEP + current_user.first + ".jpg"); |
|
|
|
static QPixmap GetIcon(Service::Account::UUID uuid) { |
|
|
|
const auto icon_url = QString::fromStdString(GetImagePath(uuid)); |
|
|
|
QPixmap icon{icon_url}; |
|
|
|
|
|
|
|
if (!icon) { |
|
|
|
icon.fill(QColor::fromRgb(0, 0, 0)); |
|
|
|
icon.fill(Qt::black); |
|
|
|
icon.loadFromData(backup_jpeg.data(), backup_jpeg.size()); |
|
|
|
} |
|
|
|
|
|
|
|
return icon; |
|
|
|
} |
|
|
|
|
|
|
|
void ConfigureSystem::PopulateUserList() { |
|
|
|
const auto& profiles = profile_manager->GetAllUsers(); |
|
|
|
std::transform( |
|
|
|
profiles.begin(), profiles.end(), std::back_inserter(list_items), |
|
|
|
[this](const Service::Account::UUID& user) { |
|
|
|
Service::Account::ProfileBase profile; |
|
|
|
if (!profile_manager->GetProfileBase(user, profile)) |
|
|
|
return QList<QStandardItem*>{}; |
|
|
|
const auto username = Common::StringFromFixedZeroTerminatedBuffer( |
|
|
|
reinterpret_cast<const char*>(profile.username.data()), profile.username.size()); |
|
|
|
|
|
|
|
return QList<QStandardItem*>{new QStandardItem{ |
|
|
|
GetIcon(user).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), |
|
|
|
QString::fromStdString(username + '\n' + user.FormatSwitch())}}; |
|
|
|
}); |
|
|
|
|
|
|
|
list_items.erase( |
|
|
|
std::remove_if(list_items.begin(), list_items.end(), |
|
|
|
[](const auto& list) { return list == QList<QStandardItem*>{}; }), |
|
|
|
list_items.end()); |
|
|
|
|
|
|
|
for (const auto& item : list_items) |
|
|
|
item_model->appendRow(item); |
|
|
|
} |
|
|
|
|
|
|
|
void ConfigureSystem::UpdateCurrentUser() { |
|
|
|
const auto& current_user = profile_manager->GetAllUsers()[Settings::values.current_user]; |
|
|
|
const auto username = GetAccountUsername(current_user); |
|
|
|
|
|
|
|
scene->clear(); |
|
|
|
scene->addPixmap(icon.scaled(48, 48, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); |
|
|
|
ui->current_user_username->setText(QString::fromStdString(current_user.first)); |
|
|
|
scene->addPixmap( |
|
|
|
GetIcon(current_user).scaled(48, 48, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); |
|
|
|
ui->current_user_username->setText(QString::fromStdString(username)); |
|
|
|
} |
|
|
|
|
|
|
|
void ConfigureSystem::ReadSystemSettings() {} |
|
|
|
|
|
|
|
std::string ConfigureSystem::GetAccountUsername(Service::Account::UUID uuid) { |
|
|
|
Service::Account::ProfileBase profile; |
|
|
|
if (!profile_manager->GetProfileBase(uuid, profile)) |
|
|
|
return ""; |
|
|
|
return Common::StringFromFixedZeroTerminatedBuffer( |
|
|
|
reinterpret_cast<const char*>(profile.username.data()), profile.username.size()); |
|
|
|
} |
|
|
|
|
|
|
|
void ConfigureSystem::applyConfiguration() { |
|
|
|
if (!enabled) |
|
|
|
return; |
|
|
|
@ -192,16 +225,16 @@ void ConfigureSystem::refreshConsoleID() { |
|
|
|
|
|
|
|
void ConfigureSystem::SelectUser(const QModelIndex& index) { |
|
|
|
Settings::values.current_user = |
|
|
|
std::clamp<std::size_t>(index.row(), 0, Settings::values.users.size() - 1); |
|
|
|
std::clamp<std::size_t>(index.row(), 0, profile_manager->GetUserCount() - 1); |
|
|
|
|
|
|
|
UpdateCurrentUser(); |
|
|
|
|
|
|
|
if (Settings::values.users.size() >= 2) |
|
|
|
ui->pm_remove->setEnabled(true); |
|
|
|
else |
|
|
|
ui->pm_remove->setEnabled(false); |
|
|
|
ui->pm_remove->setEnabled(profile_manager->GetUserCount() >= 2); |
|
|
|
ui->pm_remove->setEnabled(false); |
|
|
|
|
|
|
|
ui->pm_rename->setEnabled(true); |
|
|
|
|
|
|
|
ui->pm_set_image->setEnabled(true); |
|
|
|
} |
|
|
|
|
|
|
|
void ConfigureSystem::AddUser() { |
|
|
|
@ -212,33 +245,57 @@ void ConfigureSystem::AddUser() { |
|
|
|
const auto username = |
|
|
|
QInputDialog::getText(this, tr("Enter Username"), tr("Enter a username for the new user:"), |
|
|
|
QLineEdit::Normal, QString(), &ok); |
|
|
|
if (!ok) |
|
|
|
return; |
|
|
|
|
|
|
|
Settings::values.users.emplace_back(username.toStdString(), uuid); |
|
|
|
profile_manager->CreateNewUser(uuid, username.toStdString()); |
|
|
|
|
|
|
|
setConfiguration(); |
|
|
|
item_model->appendRow(new QStandardItem{ |
|
|
|
GetIcon(uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), |
|
|
|
QString::fromStdString(username.toStdString() + '\n' + uuid.FormatSwitch())}); |
|
|
|
} |
|
|
|
|
|
|
|
void ConfigureSystem::RenameUser() { |
|
|
|
const auto user = tree_view->currentIndex().row(); |
|
|
|
ASSERT(user < 8); |
|
|
|
|
|
|
|
const auto uuid = profile_manager->GetAllUsers()[user]; |
|
|
|
const auto username = GetAccountUsername(uuid); |
|
|
|
|
|
|
|
Service::Account::ProfileBase profile; |
|
|
|
if (!profile_manager->GetProfileBase(uuid, profile)) |
|
|
|
return; |
|
|
|
|
|
|
|
bool ok = false; |
|
|
|
const auto new_username = QInputDialog::getText( |
|
|
|
this, tr("Enter Username"), tr("Enter a new username:"), QLineEdit::Normal, |
|
|
|
QString::fromStdString(Settings::values.users[user].first), &ok); |
|
|
|
const auto new_username = |
|
|
|
QInputDialog::getText(this, tr("Enter Username"), tr("Enter a new username:"), |
|
|
|
QLineEdit::Normal, QString::fromStdString(username), &ok); |
|
|
|
|
|
|
|
if (!ok) |
|
|
|
return; |
|
|
|
|
|
|
|
Settings::values.users[user].first = new_username.toStdString(); |
|
|
|
const auto username_std = new_username.toStdString(); |
|
|
|
if (username_std.size() > profile.username.size()) |
|
|
|
std::copy_n(username_std.begin(), profile.username.size(), profile.username.begin()); |
|
|
|
else |
|
|
|
std::copy(username_std.begin(), username_std.end(), profile.username.begin()); |
|
|
|
|
|
|
|
setConfiguration(); |
|
|
|
profile_manager->SetProfileBase(uuid, profile); |
|
|
|
|
|
|
|
list_items[user][0] = new QStandardItem{ |
|
|
|
GetIcon(uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), |
|
|
|
QString::fromStdString(username_std + '\n' + uuid.FormatSwitch())}; |
|
|
|
} |
|
|
|
|
|
|
|
void ConfigureSystem::DeleteUser() { |
|
|
|
const auto user = Settings::values.users.begin() + tree_view->currentIndex().row(); |
|
|
|
const auto index = tree_view->currentIndex().row(); |
|
|
|
ASSERT(index < 8); |
|
|
|
const auto uuid = profile_manager->GetAllUsers()[index]; |
|
|
|
const auto username = GetAccountUsername(uuid); |
|
|
|
|
|
|
|
const auto confirm = QMessageBox::question( |
|
|
|
this, tr("Confirm Delete"), |
|
|
|
tr("You are about to delete user with name %1. Are you sure?").arg(user->first.c_str())); |
|
|
|
tr("You are about to delete user with name %1. Are you sure?").arg(username.c_str())); |
|
|
|
|
|
|
|
if (confirm == QMessageBox::No) |
|
|
|
return; |
|
|
|
@ -246,10 +303,38 @@ void ConfigureSystem::DeleteUser() { |
|
|
|
if (Settings::values.current_user == tree_view->currentIndex().row()) |
|
|
|
Settings::values.current_user = 0; |
|
|
|
|
|
|
|
Settings::values.users.erase(user); |
|
|
|
if (!profile_manager->RemoveUser(uuid)) |
|
|
|
return; |
|
|
|
|
|
|
|
setConfiguration(); |
|
|
|
item_model->removeRows(tree_view->currentIndex().row(), 1); |
|
|
|
|
|
|
|
ui->pm_remove->setEnabled(false); |
|
|
|
ui->pm_rename->setEnabled(false); |
|
|
|
} |
|
|
|
|
|
|
|
void ConfigureSystem::SetUserImage() { |
|
|
|
const auto index = tree_view->currentIndex().row(); |
|
|
|
ASSERT(index < 8); |
|
|
|
const auto uuid = profile_manager->GetAllUsers()[index]; |
|
|
|
const auto username = GetAccountUsername(uuid); |
|
|
|
|
|
|
|
const auto file = QFileDialog::getOpenFileName(this, tr("Select User Image"), QString(), |
|
|
|
"JPEG Images (*.jpg *.jpeg)"); |
|
|
|
|
|
|
|
if (file.isEmpty()) |
|
|
|
return; |
|
|
|
|
|
|
|
FileUtil::Delete(GetImagePath(uuid)); |
|
|
|
|
|
|
|
const auto raw_path = |
|
|
|
FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + "/system/save/8000000000000010"; |
|
|
|
if (FileUtil::Exists(raw_path) && !FileUtil::IsDirectory(raw_path)) |
|
|
|
FileUtil::Delete(raw_path); |
|
|
|
|
|
|
|
FileUtil::CreateFullPath(GetImagePath(uuid)); |
|
|
|
FileUtil::Copy(file.toStdString(), GetImagePath(uuid)); |
|
|
|
|
|
|
|
list_items[index][0] = new QStandardItem{ |
|
|
|
GetIcon(uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), |
|
|
|
QString::fromStdString(username + '\n' + uuid.FormatSwitch())}; |
|
|
|
} |