Skip to content

Commit

Permalink
Qt: Add a permission error dialog when converting memorycards in a R/…
Browse files Browse the repository at this point in the history
…O directory
  • Loading branch information
thethunderturner authored and F0bes committed Sep 22, 2024
1 parent c82671f commit 064678d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/scripts/linux/build-dependencies-qt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ aa27e4454ce631c5a17924ce0624eac736da19fc6f5a2ab15a6c58da7b36950f shaderc-glslan
03ee1a2c06f3b61008478f4abe9423454e53e580b9488b47c8071547c6a9db47 shaderc-spirv-tools-$SHADERC_SPIRVTOOLS.tar.gz
EOF

curl -C - -L \
curl -L \
-O "https://github.com/ianlancetaylor/libbacktrace/archive/$LIBBACKTRACE.zip" \
-O "https://ijg.org/files/jpegsrc.v$LIBJPEG.tar.gz" \
-O "https://downloads.sourceforge.net/project/libpng/libpng16/$LIBPNG/libpng-$LIBPNG.tar.xz" \
Expand Down
55 changes: 45 additions & 10 deletions pcsx2-qt/Settings/MemoryCardConvertDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
#include "common/Path.h"
#include "common/StringUtil.h"

// Platform-specific includes
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <sys/stat.h>
#endif

MemoryCardConvertDialog::MemoryCardConvertDialog(QWidget* parent, QString selectedCard)
: QDialog(parent)
{
Expand All @@ -36,7 +44,7 @@ MemoryCardConvertDialog::MemoryCardConvertDialog(QWidget* parent, QString select
m_ui.progressBar->setRange(0, 100);
m_ui.progressBar->setValue(0);

connect(m_ui.conversionTypeSelect, &QComboBox::currentIndexChanged, this, [this]()
connect(m_ui.conversionTypeSelect, &QComboBox::currentIndexChanged, this, [this]()
{
switch (m_srcCardInfo.type)
{
Expand Down Expand Up @@ -71,7 +79,7 @@ MemoryCardConvertDialog::MemoryCardConvertDialog(QWidget* parent, QString select
}
}
);

disconnect(m_ui.buttonBox, &QDialogButtonBox::accepted, this, nullptr);

connect(m_ui.buttonBox->button(QDialogButtonBox::Ok), &QPushButton::clicked, this, &MemoryCardConvertDialog::ConvertCard);
Expand Down Expand Up @@ -154,7 +162,7 @@ bool MemoryCardConvertDialog::SetupPicklist()
for (auto dirEntry : rootDir)
{
const std::string_view fileName = Path::GetFileName(dirEntry.FileName);

if (fileName.size() >= 7 && fileName.substr(0, 7).compare("_pcsx2_") == 0)
{
continue;
Expand All @@ -176,7 +184,7 @@ bool MemoryCardConvertDialog::SetupPicklist()
if (sizeBytes < CardCapacity::_8_MB)
{
m_ui.conversionTypeSelect->addItem(tr("8 MB File"), 8);

if (!typeSet)
{
SetType_8();
Expand All @@ -187,7 +195,7 @@ bool MemoryCardConvertDialog::SetupPicklist()
if (sizeBytes < CardCapacity::_16_MB)
{
m_ui.conversionTypeSelect->addItem(tr("16 MB File"), 16);

if (!typeSet)
{
SetType_16();
Expand All @@ -198,7 +206,7 @@ bool MemoryCardConvertDialog::SetupPicklist()
if (sizeBytes < CardCapacity::_32_MB)
{
m_ui.conversionTypeSelect->addItem(tr("32 MB File"), 32);

if (!typeSet)
{
SetType_32();
Expand All @@ -209,7 +217,7 @@ bool MemoryCardConvertDialog::SetupPicklist()
if (sizeBytes < CardCapacity::_64_MB)
{
m_ui.conversionTypeSelect->addItem(tr("64 MB File"), 64);

if (!typeSet)
{
SetType_64();
Expand Down Expand Up @@ -242,7 +250,7 @@ void MemoryCardConvertDialog::ConvertCard()
else
{
QString baseName = m_selectedCard;

// Get our destination file name
size_t extensionPos = baseName.lastIndexOf(".ps2", -1);
// Strip the extension off of it
Expand All @@ -253,15 +261,37 @@ void MemoryCardConvertDialog::ConvertCard()
size_t num = 0;
QString destName = baseName;
destName.append(".ps2");


const QDir destinationDir(Path::Combine(EmuFolders::MemoryCards, destName.toStdString()).c_str());
const QString absolutePath = destinationDir.absolutePath();

const QString dirPath = destinationDir.absolutePath();

#ifdef _WIN32
const DWORD fileAttributes = GetFileAttributes(dirPath.toStdString().c_str());
const bool canRead = !(fileAttributes & FILE_ATTRIBUTE_READONLY);

if (!canRead)
{
PermissionError(absolutePath);
return;
}
#else
if (access(dirPath.toStdString().c_str(), R_OK | W_OK | X_OK) != 0)
{
PermissionError(absolutePath);
return;
}
#endif

// If a match is found, revert back to the base name, add a number and the extension, and try again.
// Keep incrementing the number until we get a unique result.
while (m_srcCardInfo.type == MemoryCardType::File ? FileSystem::DirectoryExists(Path::Combine(EmuFolders::MemoryCards, destName.toStdString()).c_str()) : FileSystem::FileExists(Path::Combine(EmuFolders::MemoryCards, destName.toStdString()).c_str()))
{
destName = baseName;
destName.append(StringUtil::StdStringFromFormat("_%02zd.ps2", ++num).c_str());
}

m_destCardName = destName;
StartThread();
}
Expand Down Expand Up @@ -303,3 +333,8 @@ void MemoryCardConvertDialog::SetType_Folder()
{
SetType(MemoryCardType::Folder, MemoryCardFileType::Unknown, tr("Uses a folder on your PC filesystem, instead of a file. Infinite capacity, while keeping the same compatibility as an 8 MB Memory Card."));
}

void MemoryCardConvertDialog::PermissionError(const QString absolutePath)
{
QMessageBox::critical(this, tr("Cannot Convert Memory Card"),tr("You have insufficient permissions to convert this Memory Card. Please ensure that your user owns the directory of your Memory Cards. \n \"%1\".").arg(absolutePath));
}
1 change: 1 addition & 0 deletions pcsx2-qt/Settings/MemoryCardConvertDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ private Q_SLOTS:
void SetType_32();
void SetType_64();
void SetType_Folder();
void PermissionError(const QString absolutePath);

Ui::MemoryCardConvertDialog m_ui;

Expand Down

0 comments on commit 064678d

Please sign in to comment.