configure_system.cpp 4.6 KB

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