2024-01-23 13:32:35 +00:00
|
|
|
// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team
|
2023-12-22 11:57:49 +00:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0+
|
2022-03-25 09:24:41 +00:00
|
|
|
|
2022-04-12 12:16:05 +00:00
|
|
|
#include "common/FileSystem.h"
|
2022-05-19 14:46:33 +00:00
|
|
|
#include "common/Path.h"
|
2022-03-25 09:24:41 +00:00
|
|
|
#include "common/StringUtil.h"
|
|
|
|
|
|
|
|
#include <QtWidgets/QMessageBox>
|
|
|
|
#include <QtWidgets/QPushButton>
|
|
|
|
|
2023-07-04 10:51:56 +00:00
|
|
|
#include "Settings/MemoryCardCreateDialog.h"
|
2022-03-25 09:24:41 +00:00
|
|
|
|
2023-06-11 06:06:40 +00:00
|
|
|
#include "pcsx2/SIO/Memcard/MemoryCardFile.h"
|
2022-03-25 09:24:41 +00:00
|
|
|
|
2023-07-04 10:51:56 +00:00
|
|
|
MemoryCardCreateDialog::MemoryCardCreateDialog(QWidget* parent /* = nullptr */)
|
2022-03-25 09:24:41 +00:00
|
|
|
: QDialog(parent)
|
|
|
|
{
|
|
|
|
m_ui.setupUi(this);
|
2023-06-15 10:58:53 +00:00
|
|
|
m_ui.icon->setPixmap(QIcon::fromTheme("memcard-line").pixmap(m_ui.icon->width()));
|
2022-03-25 09:24:41 +00:00
|
|
|
|
|
|
|
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
|
|
|
|
2023-07-04 10:51:56 +00:00
|
|
|
connect(m_ui.name, &QLineEdit::textChanged, this, &MemoryCardCreateDialog::nameTextChanged);
|
2022-03-25 09:24:41 +00:00
|
|
|
|
|
|
|
connect(m_ui.size8MB, &QRadioButton::clicked, this, [this]() { setType(MemoryCardType::File, MemoryCardFileType::PS2_8MB); });
|
|
|
|
connect(m_ui.size16MB, &QRadioButton::clicked, this, [this]() { setType(MemoryCardType::File, MemoryCardFileType::PS2_16MB); });
|
|
|
|
connect(m_ui.size32MB, &QRadioButton::clicked, this, [this]() { setType(MemoryCardType::File, MemoryCardFileType::PS2_32MB); });
|
|
|
|
connect(m_ui.size64MB, &QRadioButton::clicked, this, [this]() { setType(MemoryCardType::File, MemoryCardFileType::PS2_64MB); });
|
|
|
|
connect(m_ui.size128KB, &QRadioButton::clicked, this, [this]() { setType(MemoryCardType::File, MemoryCardFileType::PS1); });
|
|
|
|
connect(m_ui.sizeFolder, &QRadioButton::clicked, this, [this]() { setType(MemoryCardType::Folder, MemoryCardFileType::Unknown); });
|
|
|
|
|
|
|
|
disconnect(m_ui.buttonBox, &QDialogButtonBox::accepted, this, nullptr);
|
|
|
|
|
2023-07-04 10:51:56 +00:00
|
|
|
connect(m_ui.buttonBox->button(QDialogButtonBox::Ok), &QPushButton::clicked, this, &MemoryCardCreateDialog::createCard);
|
|
|
|
connect(m_ui.buttonBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &MemoryCardCreateDialog::close);
|
|
|
|
connect(m_ui.buttonBox->button(QDialogButtonBox::RestoreDefaults), &QPushButton::clicked, this, &MemoryCardCreateDialog::restoreDefaults);
|
2022-03-25 09:24:41 +00:00
|
|
|
|
2022-12-25 06:05:43 +00:00
|
|
|
#ifndef _WIN32
|
2024-01-23 13:32:35 +00:00
|
|
|
m_ui.ntfsCompressionLayout->removeWidget(m_ui.ntfsCompression);
|
|
|
|
safe_delete(m_ui.ntfsCompression);
|
|
|
|
m_ui.ntfsCompressionLayout->removeWidget(m_ui.ntfsCompressionLabel);
|
|
|
|
safe_delete(m_ui.ntfsCompressionLabel);
|
|
|
|
m_ui.mainLayout->removeItem(m_ui.ntfsCompressionLayout);
|
|
|
|
safe_delete(m_ui.ntfsCompressionLayout);
|
|
|
|
resize(600, 480);
|
2022-03-26 08:48:23 +00:00
|
|
|
#endif
|
|
|
|
|
2022-03-25 09:24:41 +00:00
|
|
|
updateState();
|
|
|
|
}
|
|
|
|
|
2023-07-04 10:51:56 +00:00
|
|
|
MemoryCardCreateDialog::~MemoryCardCreateDialog() = default;
|
2022-03-25 09:24:41 +00:00
|
|
|
|
2023-07-04 10:51:56 +00:00
|
|
|
void MemoryCardCreateDialog::nameTextChanged()
|
2022-03-25 09:24:41 +00:00
|
|
|
{
|
2022-06-21 18:20:18 +00:00
|
|
|
QString controlName(m_ui.name->text());
|
2022-03-25 09:24:41 +00:00
|
|
|
const int cursorPos = m_ui.name->cursorPosition();
|
|
|
|
|
2022-06-21 18:20:18 +00:00
|
|
|
controlName.replace(".", "");
|
2022-03-25 09:24:41 +00:00
|
|
|
|
|
|
|
QSignalBlocker sb(m_ui.name);
|
2022-06-21 18:20:18 +00:00
|
|
|
if (controlName.isEmpty())
|
2022-03-25 09:24:41 +00:00
|
|
|
m_ui.name->setText(QString());
|
|
|
|
else
|
2022-06-21 18:20:18 +00:00
|
|
|
m_ui.name->setText(controlName);
|
2022-03-25 09:24:41 +00:00
|
|
|
|
|
|
|
m_ui.name->setCursorPosition(cursorPos);
|
|
|
|
updateState();
|
|
|
|
}
|
|
|
|
|
2023-07-04 10:51:56 +00:00
|
|
|
void MemoryCardCreateDialog::setType(MemoryCardType type, MemoryCardFileType fileType)
|
2022-03-25 09:24:41 +00:00
|
|
|
{
|
|
|
|
m_type = type;
|
|
|
|
m_fileType = fileType;
|
|
|
|
updateState();
|
|
|
|
}
|
|
|
|
|
2023-07-04 10:51:56 +00:00
|
|
|
void MemoryCardCreateDialog::restoreDefaults()
|
2022-03-25 09:24:41 +00:00
|
|
|
{
|
|
|
|
setType(MemoryCardType::File, MemoryCardFileType::PS2_8MB);
|
|
|
|
m_ui.size8MB->setChecked(true);
|
|
|
|
m_ui.size16MB->setChecked(false);
|
|
|
|
m_ui.size32MB->setChecked(false);
|
|
|
|
m_ui.size64MB->setChecked(false);
|
|
|
|
m_ui.size128KB->setChecked(false);
|
|
|
|
m_ui.sizeFolder->setChecked(false);
|
|
|
|
}
|
|
|
|
|
2023-07-04 10:51:56 +00:00
|
|
|
void MemoryCardCreateDialog::updateState()
|
2022-03-25 09:24:41 +00:00
|
|
|
{
|
2022-06-21 20:16:07 +00:00
|
|
|
const bool okay = (m_ui.name->text().length() > 0);
|
2022-03-25 09:24:41 +00:00
|
|
|
|
|
|
|
m_ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(okay);
|
2022-03-26 08:48:23 +00:00
|
|
|
#ifdef _WIN32
|
2022-03-25 09:24:41 +00:00
|
|
|
m_ui.ntfsCompression->setEnabled(m_type == MemoryCardType::File);
|
2022-03-26 08:48:23 +00:00
|
|
|
#endif
|
2022-03-25 09:24:41 +00:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:51:56 +00:00
|
|
|
void MemoryCardCreateDialog::createCard()
|
2022-03-25 09:24:41 +00:00
|
|
|
{
|
2023-07-04 11:03:52 +00:00
|
|
|
const QString name = m_ui.name->text();
|
|
|
|
const std::string name_str = QStringLiteral("%1.%2").arg(name)
|
|
|
|
.arg((m_fileType == MemoryCardFileType::PS1) ? QStringLiteral("mcr") : QStringLiteral("ps2"))
|
|
|
|
.toStdString();
|
|
|
|
if (!Path::IsValidFileName(name_str, false))
|
2022-06-21 18:20:18 +00:00
|
|
|
{
|
2023-07-04 11:03:52 +00:00
|
|
|
QMessageBox::critical(this, tr("Create Memory Card"),
|
|
|
|
tr("Failed to create the Memory Card, because the name '%1' contains one or more invalid characters.").arg(name));
|
|
|
|
return;
|
2022-06-21 18:20:18 +00:00
|
|
|
}
|
|
|
|
|
2023-07-04 11:03:52 +00:00
|
|
|
if (FileMcd_GetCardInfo(name_str).has_value())
|
2022-03-25 09:24:41 +00:00
|
|
|
{
|
|
|
|
QMessageBox::critical(this, tr("Create Memory Card"),
|
2023-03-13 00:34:20 +00:00
|
|
|
tr("Failed to create the Memory Card, because another card with the name '%1' already exists.").arg(name));
|
2022-03-25 09:24:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-04 11:03:52 +00:00
|
|
|
if (!FileMcd_CreateNewCard(name_str, m_type, m_fileType))
|
2022-03-25 09:24:41 +00:00
|
|
|
{
|
|
|
|
QMessageBox::critical(this, tr("Create Memory Card"),
|
2023-03-13 00:34:20 +00:00
|
|
|
tr("Failed to create the Memory Card, the log may contain more information."));
|
2022-03-25 09:24:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-26 08:48:23 +00:00
|
|
|
#ifdef _WIN32
|
2024-01-23 12:33:36 +00:00
|
|
|
if (m_type == MemoryCardType::File)
|
2022-03-25 09:24:41 +00:00
|
|
|
{
|
2024-01-23 12:33:36 +00:00
|
|
|
const std::string fullPath = Path::Combine(EmuFolders::MemoryCards, name_str);
|
|
|
|
FileSystem::SetPathCompression(fullPath.c_str(), m_ui.ntfsCompression->isChecked());
|
2022-03-25 09:24:41 +00:00
|
|
|
}
|
2022-03-26 08:48:23 +00:00
|
|
|
#endif
|
2022-03-25 09:24:41 +00:00
|
|
|
|
2023-03-13 00:34:20 +00:00
|
|
|
QMessageBox::information(this, tr("Create Memory Card"), tr("Memory Card '%1' created.").arg(name));
|
2022-03-25 09:24:41 +00:00
|
|
|
accept();
|
|
|
|
}
|