diff options
Diffstat (limited to 'src/yuzu/configuration/configure_system.cpp')
-rw-r--r-- | src/yuzu/configuration/configure_system.cpp | 167 |
1 files changed, 95 insertions, 72 deletions
diff --git a/src/yuzu/configuration/configure_system.cpp b/src/yuzu/configuration/configure_system.cpp index 83cc49dfc..4b34c1e28 100644 --- a/src/yuzu/configuration/configure_system.cpp +++ b/src/yuzu/configuration/configure_system.cpp @@ -21,12 +21,8 @@ #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 = {{ +namespace { +constexpr std::array<int, 12> days_in_month = {{ 31, 29, 31, @@ -42,7 +38,7 @@ static const std::array<int, 12> days_in_month = {{ }}; // Same backup JPEG used by acc IProfile::GetImage if no jpeg found -static constexpr std::array<u8, 107> backup_jpeg{ +constexpr std::array<u8, 107> backup_jpeg{ 0xff, 0xd8, 0xff, 0xdb, 0x00, 0x43, 0x00, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x04, 0x06, 0x04, 0x04, 0x04, 0x04, 0x04, 0x08, 0x06, 0x06, 0x05, 0x06, 0x09, 0x08, 0x0a, 0x0a, 0x09, 0x08, 0x09, 0x09, 0x0a, 0x0c, 0x0f, 0x0c, 0x0a, 0x0b, 0x0e, @@ -52,15 +48,52 @@ static constexpr std::array<u8, 107> backup_jpeg{ 0x01, 0x01, 0x00, 0x00, 0x3f, 0x00, 0xd2, 0xcf, 0x20, 0xff, 0xd9, }; +QString GetImagePath(Service::Account::UUID uuid) { + const auto path = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + + "/system/save/8000000000000010/su/avators/" + uuid.FormatSwitch() + ".jpg"; + return QString::fromStdString(path); +} + +QString GetAccountUsername(const Service::Account::ProfileManager& manager, + Service::Account::UUID uuid) { + Service::Account::ProfileBase profile; + if (!manager.GetProfileBase(uuid, profile)) { + return {}; + } + + const auto text = Common::StringFromFixedZeroTerminatedBuffer( + reinterpret_cast<const char*>(profile.username.data()), profile.username.size()); + return QString::fromStdString(text); +} + +QString FormatUserEntryText(const QString& username, Service::Account::UUID uuid) { + return ConfigureSystem::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())); +} + +QPixmap GetIcon(Service::Account::UUID uuid) { + QPixmap icon{GetImagePath(uuid)}; + + if (!icon) { + icon.fill(Qt::black); + icon.loadFromData(backup_jpeg.data(), backup_jpeg.size()); + } + + return icon.scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); +} +} // Anonymous namespace + 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, - &ConfigureSystem::updateBirthdayComboBox); + &ConfigureSystem::UpdateBirthdayComboBox); connect(ui->button_regenerate_console_id, &QPushButton::clicked, this, - &ConfigureSystem::refreshConsoleID); + &ConfigureSystem::RefreshConsoleID); layout = new QVBoxLayout; tree_view = new QTreeView; @@ -118,18 +151,6 @@ void ConfigureSystem::setConfiguration() { UpdateCurrentUser(); } -static QPixmap GetIcon(Service::Account::UUID uuid) { - const auto icon_url = QString::fromStdString(GetImagePath(uuid)); - QPixmap icon{icon_url}; - - if (!icon) { - icon.fill(Qt::black); - icon.loadFromData(backup_jpeg.data(), backup_jpeg.size()); - } - - return icon; -} - void ConfigureSystem::PopulateUserList() { const auto& profiles = profile_manager->GetAllUsers(); for (const auto& user : profiles) { @@ -141,8 +162,7 @@ void ConfigureSystem::PopulateUserList() { reinterpret_cast<const char*>(profile.username.data()), profile.username.size()); list_items.push_back(QList<QStandardItem*>{new QStandardItem{ - GetIcon(user).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), - QString::fromStdString(username + '\n' + user.FormatSwitch())}}); + GetIcon(user), FormatUserEntryText(QString::fromStdString(username), user)}}); } for (const auto& item : list_items) @@ -153,25 +173,17 @@ void ConfigureSystem::UpdateCurrentUser() { ui->pm_add->setEnabled(profile_manager->GetUserCount() < Service::Account::MAX_USERS); const auto& current_user = profile_manager->GetUser(Settings::values.current_user); - ASSERT(current_user != boost::none); - const auto username = GetAccountUsername(*current_user); + ASSERT(current_user != std::nullopt); + const auto username = GetAccountUsername(*profile_manager, *current_user); scene->clear(); scene->addPixmap( GetIcon(*current_user).scaled(48, 48, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); - ui->current_user_username->setText(QString::fromStdString(username)); + ui->current_user_username->setText(username); } void ConfigureSystem::ReadSystemSettings() {} -std::string ConfigureSystem::GetAccountUsername(Service::Account::UUID uuid) const { - 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; @@ -180,7 +192,7 @@ void ConfigureSystem::applyConfiguration() { Settings::Apply(); } -void ConfigureSystem::updateBirthdayComboBox(int birthmonth_index) { +void ConfigureSystem::UpdateBirthdayComboBox(int birthmonth_index) { if (birthmonth_index < 0 || birthmonth_index >= 12) return; @@ -205,7 +217,7 @@ void ConfigureSystem::updateBirthdayComboBox(int birthmonth_index) { ui->combo_birthday->setCurrentIndex(birthday_index); } -void ConfigureSystem::refreshConsoleID() { +void ConfigureSystem::RefreshConsoleID() { QMessageBox::StandardButton reply; QString warning_text = tr("This will replace your current virtual Switch with a new one. " "Your current virtual Switch will not be recoverable. " @@ -232,8 +244,7 @@ void ConfigureSystem::SelectUser(const QModelIndex& index) { } void ConfigureSystem::AddUser() { - Service::Account::UUID uuid; - uuid.Generate(); + const auto uuid = Service::Account::UUID::Generate(); bool ok = false; const auto username = @@ -244,25 +255,23 @@ void ConfigureSystem::AddUser() { profile_manager->CreateNewUser(uuid, username.toStdString()); - item_model->appendRow(new QStandardItem{ - GetIcon(uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), - QString::fromStdString(username.toStdString() + '\n' + uuid.FormatSwitch())}); + item_model->appendRow(new QStandardItem{GetIcon(uuid), FormatUserEntryText(username, uuid)}); } void ConfigureSystem::RenameUser() { const auto user = tree_view->currentIndex().row(); const auto uuid = profile_manager->GetUser(user); - ASSERT(uuid != boost::none); - const auto username = GetAccountUsername(*uuid); + ASSERT(uuid != std::nullopt); Service::Account::ProfileBase profile; if (!profile_manager->GetProfileBase(*uuid, profile)) return; bool ok = false; + const auto old_username = GetAccountUsername(*profile_manager, *uuid); const auto new_username = QInputDialog::getText(this, tr("Enter Username"), tr("Enter a new username:"), - QLineEdit::Normal, QString::fromStdString(username), &ok); + QLineEdit::Normal, old_username, &ok); if (!ok) return; @@ -280,25 +289,20 @@ void ConfigureSystem::RenameUser() { item_model->setItem( user, 0, - new QStandardItem{ - GetIcon(*uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), - tr("%1\n%2", "%1 is the profile username, %2 is the formatted UUID (e.g. " - "00112233-4455-6677-8899-AABBCCDDEEFF))") - .arg(QString::fromStdString(username_std), - QString::fromStdString(uuid->FormatSwitch()))}); + new QStandardItem{GetIcon(*uuid), + FormatUserEntryText(QString::fromStdString(username_std), *uuid)}); UpdateCurrentUser(); } void ConfigureSystem::DeleteUser() { const auto index = tree_view->currentIndex().row(); const auto uuid = profile_manager->GetUser(index); - ASSERT(uuid != boost::none); - const auto username = GetAccountUsername(*uuid); + ASSERT(uuid != std::nullopt); + const auto username = GetAccountUsername(*profile_manager, *uuid); - const auto confirm = - QMessageBox::question(this, tr("Confirm Delete"), - tr("You are about to delete user with name %1. Are you sure?") - .arg(QString::fromStdString(username))); + const auto confirm = QMessageBox::question( + this, tr("Confirm Delete"), + tr("You are about to delete user with name \"%1\". Are you sure?").arg(username)); if (confirm == QMessageBox::No) return; @@ -320,29 +324,48 @@ void ConfigureSystem::DeleteUser() { void ConfigureSystem::SetUserImage() { const auto index = tree_view->currentIndex().row(); const auto uuid = profile_manager->GetUser(index); - ASSERT(uuid != boost::none); - const auto username = GetAccountUsername(*uuid); + ASSERT(uuid != std::nullopt); const auto file = QFileDialog::getOpenFileName(this, tr("Select User Image"), QString(), - "JPEG Images (*.jpg *.jpeg)"); + tr("JPEG Images (*.jpg *.jpeg)")); - if (file.isEmpty()) + if (file.isEmpty()) { return; + } - FileUtil::Delete(GetImagePath(*uuid)); + const auto image_path = GetImagePath(*uuid); + if (QFile::exists(image_path) && !QFile::remove(image_path)) { + QMessageBox::warning( + this, tr("Error deleting image"), + tr("Error occurred attempting to overwrite previous image at: %1.").arg(image_path)); + return; + } + + const auto raw_path = QString::fromStdString( + FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + "/system/save/8000000000000010"); + const QFileInfo raw_info{raw_path}; + if (raw_info.exists() && !raw_info.isDir() && !QFile::remove(raw_path)) { + QMessageBox::warning(this, tr("Error deleting file"), + tr("Unable to delete existing file: %1.").arg(raw_path)); + return; + } - 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); + const QString absolute_dst_path = QFileInfo{image_path}.absolutePath(); + if (!QDir{raw_path}.mkpath(absolute_dst_path)) { + QMessageBox::warning( + this, tr("Error creating user image directory"), + tr("Unable to create directory %1 for storing user images.").arg(absolute_dst_path)); + return; + } - FileUtil::CreateFullPath(GetImagePath(*uuid)); - FileUtil::Copy(file.toStdString(), GetImagePath(*uuid)); + if (!QFile::copy(file, image_path)) { + QMessageBox::warning(this, tr("Error copying user image"), + tr("Unable to copy image from %1 to %2").arg(file, image_path)); + return; + } - item_model->setItem( - index, 0, - new QStandardItem{ - GetIcon(*uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), - QString::fromStdString(username + '\n' + uuid->FormatSwitch())}); + const auto username = GetAccountUsername(*profile_manager, *uuid); + item_model->setItem(index, 0, + new QStandardItem{GetIcon(*uuid), FormatUserEntryText(username, *uuid)}); UpdateCurrentUser(); } |