configure_system.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "citra_qt/configure_system.h"
  5. #include "citra_qt/ui_settings.h"
  6. #include "core/core.h"
  7. #include "core/hle/service/cfg/cfg.h"
  8. #include "core/hle/service/fs/archive.h"
  9. #include "ui_configure_system.h"
  10. static const std::array<int, 12> days_in_month = {{
  11. 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
  12. }};
  13. ConfigureSystem::ConfigureSystem(QWidget* parent) : QWidget(parent), ui(new Ui::ConfigureSystem) {
  14. ui->setupUi(this);
  15. connect(ui->combo_birthmonth, SIGNAL(currentIndexChanged(int)),
  16. SLOT(updateBirthdayComboBox(int)));
  17. this->setConfiguration();
  18. }
  19. ConfigureSystem::~ConfigureSystem() {}
  20. void ConfigureSystem::setConfiguration() {
  21. enabled = !Core::System::GetInstance().IsPoweredOn();
  22. if (!enabled) {
  23. ReadSystemSettings();
  24. ui->group_system_settings->setEnabled(false);
  25. } else {
  26. // This tab is enabled only when game is not running (i.e. all service are not initialized).
  27. // Temporarily register archive types and load the config savegame file to memory.
  28. Service::FS::RegisterArchiveTypes();
  29. ResultCode result = Service::CFG::LoadConfigNANDSaveFile();
  30. Service::FS::UnregisterArchiveTypes();
  31. if (result.IsError()) {
  32. ui->label_disable_info->setText(tr("Failed to load system settings data."));
  33. ui->group_system_settings->setEnabled(false);
  34. enabled = false;
  35. return;
  36. }
  37. ReadSystemSettings();
  38. ui->label_disable_info->hide();
  39. }
  40. }
  41. void ConfigureSystem::ReadSystemSettings() {
  42. // set username
  43. username = Service::CFG::GetUsername();
  44. // TODO(wwylele): Use this when we move to Qt 5.5
  45. // ui->edit_username->setText(QString::fromStdU16String(username));
  46. ui->edit_username->setText(
  47. QString::fromUtf16(reinterpret_cast<const ushort*>(username.data())));
  48. // set birthday
  49. std::tie(birthmonth, birthday) = Service::CFG::GetBirthday();
  50. ui->combo_birthmonth->setCurrentIndex(birthmonth - 1);
  51. updateBirthdayComboBox(
  52. birthmonth -
  53. 1); // explicitly update it because the signal from setCurrentIndex is not reliable
  54. ui->combo_birthday->setCurrentIndex(birthday - 1);
  55. // set system language
  56. language_index = Service::CFG::GetSystemLanguage();
  57. ui->combo_language->setCurrentIndex(language_index);
  58. // set sound output mode
  59. sound_index = Service::CFG::GetSoundOutputMode();
  60. ui->combo_sound->setCurrentIndex(sound_index);
  61. }
  62. void ConfigureSystem::applyConfiguration() {
  63. if (!enabled)
  64. return;
  65. bool modified = false;
  66. // apply username
  67. // TODO(wwylele): Use this when we move to Qt 5.5
  68. // std::u16string new_username = ui->edit_username->text().toStdU16String();
  69. std::u16string new_username(
  70. reinterpret_cast<const char16_t*>(ui->edit_username->text().utf16()));
  71. if (new_username != username) {
  72. Service::CFG::SetUsername(new_username);
  73. modified = true;
  74. }
  75. // apply birthday
  76. int new_birthmonth = ui->combo_birthmonth->currentIndex() + 1;
  77. int new_birthday = ui->combo_birthday->currentIndex() + 1;
  78. if (birthmonth != new_birthmonth || birthday != new_birthday) {
  79. Service::CFG::SetBirthday(new_birthmonth, new_birthday);
  80. modified = true;
  81. }
  82. // apply language
  83. int new_language = ui->combo_language->currentIndex();
  84. if (language_index != new_language) {
  85. Service::CFG::SetSystemLanguage(static_cast<Service::CFG::SystemLanguage>(new_language));
  86. modified = true;
  87. }
  88. // apply sound
  89. int new_sound = ui->combo_sound->currentIndex();
  90. if (sound_index != new_sound) {
  91. Service::CFG::SetSoundOutputMode(static_cast<Service::CFG::SoundOutputMode>(new_sound));
  92. modified = true;
  93. }
  94. // update the config savegame if any item is modified.
  95. if (modified)
  96. Service::CFG::UpdateConfigNANDSavegame();
  97. }
  98. void ConfigureSystem::updateBirthdayComboBox(int birthmonth_index) {
  99. if (birthmonth_index < 0 || birthmonth_index >= 12)
  100. return;
  101. // store current day selection
  102. int birthday_index = ui->combo_birthday->currentIndex();
  103. // get number of days in the new selected month
  104. int days = days_in_month[birthmonth_index];
  105. // if the selected day is out of range,
  106. // reset it to 1st
  107. if (birthday_index < 0 || birthday_index >= days)
  108. birthday_index = 0;
  109. // update the day combo box
  110. ui->combo_birthday->clear();
  111. for (int i = 1; i <= days; ++i) {
  112. ui->combo_birthday->addItem(QString::number(i));
  113. }
  114. // restore the day selection
  115. ui->combo_birthday->setCurrentIndex(birthday_index);
  116. }