| 1 | // Copyright 2019 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/GCMemcardCreateNewDialog.h" |
| 5 | |
| 6 | #include <QComboBox> |
| 7 | #include <QDialogButtonBox> |
| 8 | #include <QGridLayout> |
| 9 | #include <QLabel> |
| 10 | #include <QPushButton> |
| 11 | #include <QRadioButton> |
| 12 | |
| 13 | #include "Common/FileUtil.h" |
| 14 | #include "Common/Timer.h" |
| 15 | |
| 16 | #include "Core/HW/EXI/EXI_DeviceIPL.h" |
| 17 | #include "Core/HW/GCMemcard/GCMemcard.h" |
| 18 | #include "Core/HW/Sram.h" |
| 19 | |
| 20 | #include "DolphinQt/QtUtils/DolphinFileDialog.h" |
| 21 | |
| 22 | GCMemcardCreateNewDialog::GCMemcardCreateNewDialog(QWidget* parent) : QDialog(parent) |
| 23 | { |
| 24 | m_combobox_size = new QComboBox(); |
| 25 | m_combobox_size->addItem(tr("4 Mbit (59 blocks)"), 4); |
| 26 | m_combobox_size->addItem(tr("8 Mbit (123 blocks)"), 8); |
| 27 | m_combobox_size->addItem(tr("16 Mbit (251 blocks)"), 16); |
| 28 | m_combobox_size->addItem(tr("32 Mbit (507 blocks)"), 32); |
| 29 | m_combobox_size->addItem(tr("64 Mbit (1019 blocks)"), 64); |
| 30 | m_combobox_size->addItem(tr("128 Mbit (2043 blocks)"), 128); |
| 31 | m_combobox_size->setCurrentIndex(5); |
| 32 | |
| 33 | m_radio_western = new QRadioButton(tr("Western (Windows-1252)")); |
| 34 | // i18n: The translation of this string should be consistent with the translation of the |
| 35 | // string "Western (Windows-1252)". Because of this, you may want to parse "Japanese" as |
| 36 | // "a character encoding which is from Japan / used in Japan" rather than "the Japanese language". |
| 37 | m_radio_shiftjis = new QRadioButton(tr("Japanese (Shift-JIS)")); |
| 38 | m_radio_western->setChecked(true); |
| 39 | |
| 40 | auto* card_size_label = new QLabel(tr("Card Size")); |
| 41 | // i18n: Character encoding |
| 42 | auto* card_encoding_label = new QLabel(tr("Encoding")); |
| 43 | auto* button_box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); |
| 44 | button_box->button(QDialogButtonBox::Ok)->setText(tr("Create...")); |
| 45 | |
| 46 | auto* layout = new QGridLayout(); |
| 47 | layout->addWidget(card_size_label, 0, 0); |
| 48 | layout->addWidget(m_combobox_size, 0, 1); |
| 49 | layout->addWidget(card_encoding_label, 1, 0); |
| 50 | layout->addWidget(m_radio_western, 1, 1); |
| 51 | layout->addWidget(m_radio_shiftjis, 2, 1); |
| 52 | layout->addWidget(button_box, 3, 0, 1, 2, Qt::AlignRight); |
| 53 | setLayout(layout); |
| 54 | |
| 55 | connect(button_box, &QDialogButtonBox::rejected, this, &QDialog::reject); |
| 56 | connect(button_box, &QDialogButtonBox::accepted, [this] { |
| 57 | if (CreateCard()) |
| 58 | accept(); |
| 59 | }); |
| 60 | |
| 61 | setWindowTitle(tr("Create New Memory Card")); |
| 62 | } |
| 63 | |
| 64 | GCMemcardCreateNewDialog::~GCMemcardCreateNewDialog() = default; |
| 65 | |
| 66 | bool GCMemcardCreateNewDialog::CreateCard() |
| 67 | { |
| 68 | const u16 size = static_cast<u16>(m_combobox_size->currentData().toInt()); |
| 69 | const bool is_shift_jis = m_radio_shiftjis->isChecked(); |
| 70 | |
| 71 | const QString path = DolphinFileDialog::getSaveFileName( |
| 72 | this, tr("Create New Memory Card"), QString::fromStdString(File::GetUserPath(D_GCUSER_IDX)), |
| 73 | tr("GameCube Memory Cards (*.raw *.gcp)") + QStringLiteral(";;") + tr("All Files (*)")); |
| 74 | |
| 75 | if (path.isEmpty()) |
| 76 | return false; |
| 77 | |
| 78 | const CardFlashId flash_id{}; |
| 79 | const u32 rtc_bias = 0; |
| 80 | const u32 sram_language = 0; |
| 81 | const u64 format_time = |
| 82 | Common::Timer::GetLocalTimeSinceJan1970() - ExpansionInterface::CEXIIPL::GC_EPOCH; |
| 83 | |
| 84 | const std::string p = path.toStdString(); |
| 85 | auto memcard = Memcard::GCMemcard::Create(p, flash_id, size, is_shift_jis, rtc_bias, |
| 86 | sram_language, format_time); |
| 87 | if (memcard && memcard->Save()) |
| 88 | { |
| 89 | m_card_path = p; |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | std::string GCMemcardCreateNewDialog::GetMemoryCardPath() const |
| 97 | { |
| 98 | return m_card_path; |
| 99 | } |