From 3658ae38eb64cb304e9852527797afc1f0ec3349 Mon Sep 17 00:00:00 2001 From: Collecting Date: Thu, 11 Dec 2025 07:31:54 +0000 Subject: [PATCH] feat: Backup Paths & Disable Option for Linux I messed this file up hardcore too, sorry about that. Made sure to clean it up to look nice again. Signed-off-by: Collecting --- .../configuration/configure_filesystem.cpp | 289 ++++++++++++++++-- 1 file changed, 266 insertions(+), 23 deletions(-) diff --git a/src/citron/configuration/configure_filesystem.cpp b/src/citron/configuration/configure_filesystem.cpp index 691bce311..f2d61681b 100644 --- a/src/citron/configuration/configure_filesystem.cpp +++ b/src/citron/configuration/configure_filesystem.cpp @@ -3,7 +3,9 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include "citron/configuration/configure_filesystem.h" +#include #include +#include #include #include #include @@ -34,19 +36,253 @@ ConfigureFilesystem::ConfigureFilesystem(QWidget* parent) connect(ui->reset_game_list_cache, &QPushButton::pressed, this, &ConfigureFilesystem::ResetMetadata); connect(ui->gamecard_inserted, &QCheckBox::checkStateChanged, this, &ConfigureFilesystem::UpdateEnabledControls); connect(ui->gamecard_current_game, &QCheckBox::checkStateChanged, this, &ConfigureFilesystem::UpdateEnabledControls); - connect(this, &ConfigureFilesystem::UpdateInstallProgress, this, &ConfigureFilesystem::OnUpdateInstallProgress); + +#ifdef __linux__ + connect(ui->enable_backups_checkbox, &QCheckBox::toggled, this, &ConfigureFilesystem::UpdateEnabledControls); + connect(ui->custom_backup_location_checkbox, &QCheckBox::toggled, this, &ConfigureFilesystem::UpdateEnabledControls); + connect(ui->custom_backup_location_button, &QToolButton::pressed, this, [this] { + QString dir = QFileDialog::getExistingDirectory(this, tr("Select Backup Directory")); + if (!dir.isEmpty()) { + ui->custom_backup_location_edit->setText(dir); + } + }); +#endif } ConfigureFilesystem::~ConfigureFilesystem() = default; -void ConfigureFilesystem::changeEvent(QEvent* event) { if (event->type() == QEvent::LanguageChange) { RetranslateUI(); } QWidget::changeEvent(event); } -void ConfigureFilesystem::SetConfiguration() { ui->nand_directory_edit->setText(QString::fromStdString(Common::FS::GetCitronPathString(Common::FS::CitronPath::NANDDir))); ui->sdmc_directory_edit->setText(QString::fromStdString(Common::FS::GetCitronPathString(Common::FS::CitronPath::SDMCDir))); ui->gamecard_path_edit->setText(QString::fromStdString(Settings::values.gamecard_path.GetValue())); ui->dump_path_edit->setText(QString::fromStdString(Common::FS::GetCitronPathString(Common::FS::CitronPath::DumpDir))); ui->load_path_edit->setText(QString::fromStdString(Common::FS::GetCitronPathString(Common::FS::CitronPath::LoadDir))); ui->gamecard_inserted->setChecked(Settings::values.gamecard_inserted.GetValue()); ui->gamecard_current_game->setChecked(Settings::values.gamecard_current_game.GetValue()); ui->dump_exefs->setChecked(Settings::values.dump_exefs.GetValue()); ui->dump_nso->setChecked(Settings::values.dump_nso.GetValue()); ui->cache_game_list->setChecked(UISettings::values.cache_game_list.GetValue()); ui->prompt_for_autoloader->setChecked(UISettings::values.prompt_for_autoloader.GetValue()); UpdateEnabledControls(); } -void ConfigureFilesystem::ApplyConfiguration() { Common::FS::SetCitronPath(Common::FS::CitronPath::NANDDir, ui->nand_directory_edit->text().toStdString()); Common::FS::SetCitronPath(Common::FS::CitronPath::SDMCDir, ui->sdmc_directory_edit->text().toStdString()); Common::FS::SetCitronPath(Common::FS::CitronPath::DumpDir, ui->dump_path_edit->text().toStdString()); Common::FS::SetCitronPath(Common::FS::CitronPath::LoadDir, ui->load_path_edit->text().toStdString()); Settings::values.gamecard_inserted = ui->gamecard_inserted->isChecked(); Settings::values.gamecard_current_game = ui->gamecard_current_game->isChecked(); Settings::values.dump_exefs = ui->dump_exefs->isChecked(); Settings::values.dump_nso = ui->dump_nso->isChecked(); UISettings::values.cache_game_list = ui->cache_game_list->isChecked(); UISettings::values.prompt_for_autoloader = ui->prompt_for_autoloader->isChecked(); } -void ConfigureFilesystem::SetDirectory(DirectoryTarget target, QLineEdit* edit) { QString caption; switch (target) { case DirectoryTarget::NAND: caption = tr("Select Emulated NAND Directory..."); break; case DirectoryTarget::SD: caption = tr("Select Emulated SD Directory..."); break; case DirectoryTarget::Gamecard: caption = tr("Select Gamecard Path..."); break; case DirectoryTarget::Dump: caption = tr("Select Dump Directory..."); break; case DirectoryTarget::Load: caption = tr("Select Mod Load Directory..."); break; } QString str; if (target == DirectoryTarget::Gamecard) { str = QFileDialog::getOpenFileName(this, caption, QFileInfo(edit->text()).dir().path(), QStringLiteral("NX Gamecard;*.xci")); } else { str = QFileDialog::getExistingDirectory(this, caption, edit->text()); } if (str.isNull() || str.isEmpty()) { return; } if (str.back() != QChar::fromLatin1('/')) { str.append(QChar::fromLatin1('/')); } edit->setText(str); } -void ConfigureFilesystem::ResetMetadata() { if (!Common::FS::Exists(Common::FS::GetCitronPath(Common::FS::CitronPath::CacheDir) / "game_list/")) { QMessageBox::information(this, tr("Reset Metadata Cache"), tr("The metadata cache is already empty.")); } else if (Common::FS::RemoveDirRecursively(Common::FS::GetCitronPath(Common::FS::CitronPath::CacheDir) / "game_list")) { QMessageBox::information(this, tr("Reset Metadata Cache"), tr("The operation completed successfully.")); UISettings::values.is_game_list_reload_pending.exchange(true); } else { QMessageBox::warning(this, tr("Reset Metadata Cache"), tr("The metadata cache couldn't be deleted. It might be in use or non-existent.")); } } -void ConfigureFilesystem::UpdateEnabledControls() { ui->gamecard_current_game->setEnabled(ui->gamecard_inserted->isChecked()); ui->gamecard_path_edit->setEnabled(ui->gamecard_inserted->isChecked() && !ui->gamecard_current_game->isChecked()); ui->gamecard_path_button->setEnabled(ui->gamecard_inserted->isChecked() && !ui->gamecard_current_game->isChecked()); } -void ConfigureFilesystem::RetranslateUI() { ui->retranslateUi(this); } +void ConfigureFilesystem::changeEvent(QEvent* event) { + if (event->type() == QEvent::LanguageChange) { + RetranslateUI(); + } + QWidget::changeEvent(event); +} + +void ConfigureFilesystem::SetConfiguration() { + ui->nand_directory_edit->setText(QString::fromStdString(Common::FS::GetCitronPathString(Common::FS::CitronPath::NANDDir))); + ui->sdmc_directory_edit->setText(QString::fromStdString(Common::FS::GetCitronPathString(Common::FS::CitronPath::SDMCDir))); + ui->gamecard_path_edit->setText(QString::fromStdString(Settings::values.gamecard_path.GetValue())); + ui->dump_path_edit->setText(QString::fromStdString(Common::FS::GetCitronPathString(Common::FS::CitronPath::DumpDir))); + ui->load_path_edit->setText(QString::fromStdString(Common::FS::GetCitronPathString(Common::FS::CitronPath::LoadDir))); + ui->gamecard_inserted->setChecked(Settings::values.gamecard_inserted.GetValue()); + ui->gamecard_current_game->setChecked(Settings::values.gamecard_current_game.GetValue()); + ui->dump_exefs->setChecked(Settings::values.dump_exefs.GetValue()); + ui->dump_nso->setChecked(Settings::values.dump_nso.GetValue()); + ui->cache_game_list->setChecked(UISettings::values.cache_game_list.GetValue()); + ui->prompt_for_autoloader->setChecked(UISettings::values.prompt_for_autoloader.GetValue()); + +#ifdef __linux__ + ui->enable_backups_checkbox->setChecked(UISettings::values.updater_enable_backups.GetValue()); + const std::string& backup_path = UISettings::values.updater_backup_path.GetValue(); + if (!backup_path.empty()) { + ui->custom_backup_location_checkbox->setChecked(true); + ui->custom_backup_location_edit->setText(QString::fromStdString(backup_path)); + } else { + ui->custom_backup_location_checkbox->setChecked(false); + } + m_old_custom_backup_enabled = ui->custom_backup_location_checkbox->isChecked(); + m_old_backup_path = ui->custom_backup_location_edit->text(); +#endif + + UpdateEnabledControls(); +} + +void ConfigureFilesystem::ApplyConfiguration() { + Common::FS::SetCitronPath(Common::FS::CitronPath::NANDDir, ui->nand_directory_edit->text().toStdString()); + Common::FS::SetCitronPath(Common::FS::CitronPath::SDMCDir, ui->sdmc_directory_edit->text().toStdString()); + Common::FS::SetCitronPath(Common::FS::CitronPath::DumpDir, ui->dump_path_edit->text().toStdString()); + Common::FS::SetCitronPath(Common::FS::CitronPath::LoadDir, ui->load_path_edit->text().toStdString()); + Settings::values.gamecard_inserted = ui->gamecard_inserted->isChecked(); + Settings::values.gamecard_current_game = ui->gamecard_current_game->isChecked(); + Settings::values.dump_exefs = ui->dump_exefs->isChecked(); + Settings::values.dump_nso = ui->dump_nso->isChecked(); + UISettings::values.cache_game_list = ui->cache_game_list->isChecked(); + UISettings::values.prompt_for_autoloader = ui->prompt_for_autoloader->isChecked(); + +#ifdef __linux__ + UISettings::values.updater_enable_backups = ui->enable_backups_checkbox->isChecked(); + const bool new_custom_backup_enabled = ui->custom_backup_location_checkbox->isChecked(); + const QString new_backup_path = ui->custom_backup_location_edit->text(); + + if (new_custom_backup_enabled) { + UISettings::values.updater_backup_path = new_backup_path.toStdString(); + } else { + UISettings::values.updater_backup_path = ""; + } + + QByteArray appimage_path_env = qgetenv("APPIMAGE"); + const QString default_path = appimage_path_env.isEmpty() ? QString() : QFileInfo(QString::fromUtf8(appimage_path_env)).dir().filePath(QStringLiteral("backup")); + + QString old_path_to_check; + if (m_old_custom_backup_enabled && !m_old_backup_path.isEmpty()) { + old_path_to_check = m_old_backup_path; + } else if (!default_path.isEmpty()) { + old_path_to_check = default_path; + } + + QString new_path_to_check; + if (new_custom_backup_enabled && !new_backup_path.isEmpty()) { + new_path_to_check = new_backup_path; + } else if (!default_path.isEmpty()) { + new_path_to_check = default_path; + } + + if (!old_path_to_check.isEmpty() && !new_path_to_check.isEmpty() && old_path_to_check != new_path_to_check) { + QDir old_dir(old_path_to_check); + if (old_dir.exists() && !old_dir.entryInfoList({QStringLiteral("citron-backup-*.AppImage")}, QDir::Files).isEmpty()) { + QMessageBox::StandardButton reply = QMessageBox::question(this, tr("Migrate AppImage Backups?"), + tr("The backup location has changed. Would you like to move your existing backups from the old location to the new one?"), + QMessageBox::Yes | QMessageBox::No); + if (reply == QMessageBox::Yes) { + MigrateBackups(old_path_to_check, new_path_to_check); + } + } + } +#endif +} + +void ConfigureFilesystem::SetDirectory(DirectoryTarget target, QLineEdit* edit) { + QString caption; + switch (target) { + case DirectoryTarget::NAND: + caption = tr("Select Emulated NAND Directory..."); + break; + case DirectoryTarget::SD: + caption = tr("Select Emulated SD Directory..."); + break; + case DirectoryTarget::Gamecard: + caption = tr("Select Gamecard Path..."); + break; + case DirectoryTarget::Dump: + caption = tr("Select Dump Directory..."); + break; + case DirectoryTarget::Load: + caption = tr("Select Mod Load Directory..."); + break; + } + + QString str; + if (target == DirectoryTarget::Gamecard) { + str = QFileDialog::getOpenFileName(this, caption, QFileInfo(edit->text()).dir().path(), + QStringLiteral("NX Gamecard;*.xci")); + } else { + str = QFileDialog::getExistingDirectory(this, caption, edit->text()); + } + + if (str.isNull() || str.isEmpty()) { + return; + } + + if (str.back() != QChar::fromLatin1('/')) { + str.append(QChar::fromLatin1('/')); + } + edit->setText(str); +} + +void ConfigureFilesystem::ResetMetadata() { + if (!Common::FS::Exists(Common::FS::GetCitronPath(Common::FS::CitronPath::CacheDir) / "game_list/")) { + QMessageBox::information(this, tr("Reset Metadata Cache"), tr("The metadata cache is already empty.")); + } else if (Common::FS::RemoveDirRecursively(Common::FS::GetCitronPath(Common::FS::CitronPath::CacheDir) / "game_list")) { + QMessageBox::information(this, tr("Reset Metadata Cache"), tr("The operation completed successfully.")); + UISettings::values.is_game_list_reload_pending.exchange(true); + } else { + QMessageBox::warning(this, tr("Reset Metadata Cache"), tr("The metadata cache couldn't be deleted. It might be in use or non-existent.")); + } +} + +void ConfigureFilesystem::UpdateEnabledControls() { + ui->gamecard_current_game->setEnabled(ui->gamecard_inserted->isChecked()); + ui->gamecard_path_edit->setEnabled(ui->gamecard_inserted->isChecked() && !ui->gamecard_current_game->isChecked()); + ui->gamecard_path_button->setEnabled(ui->gamecard_inserted->isChecked() && !ui->gamecard_current_game->isChecked()); + +#ifdef __linux__ + ui->updater_group->setVisible(true); + bool backups_enabled = ui->enable_backups_checkbox->isChecked(); + ui->custom_backup_location_checkbox->setEnabled(backups_enabled); + + bool useCustomBackup = backups_enabled && ui->custom_backup_location_checkbox->isChecked(); + ui->custom_backup_location_edit->setEnabled(useCustomBackup); + ui->custom_backup_location_button->setEnabled(useCustomBackup); +#else + ui->updater_group->setVisible(false); +#endif +} + +void ConfigureFilesystem::RetranslateUI() { + ui->retranslateUi(this); +} + +#ifdef __linux__ +void ConfigureFilesystem::MigrateBackups(const QString& old_path, const QString& new_path) { + QDir old_dir(old_path); + if (!old_dir.exists()) { + QMessageBox::warning(this, tr("Migration Error"), tr("The old backup location does not exist.")); + return; + } + + QStringList name_filters; + name_filters << QStringLiteral("citron-backup-*.AppImage"); + QFileInfoList files_to_move = old_dir.entryInfoList(name_filters, QDir::Files); + + if (files_to_move.isEmpty()) { + QMessageBox::information(this, tr("Migration Complete"), tr("No backup files were found to migrate.")); + return; + } + + auto progress = new QProgressDialog(tr("Moving backup files..."), tr("Cancel"), 0, files_to_move.count(), this); + progress->setWindowModality(Qt::WindowModal); + progress->setMinimumDuration(1000); + progress->show(); + + auto watcher = new QFutureWatcher(this); + connect(watcher, &QFutureWatcher::finished, this, [this, watcher, progress] { + progress->close(); + if (watcher->future().isCanceled()) { + QMessageBox::warning(this, tr("Migration Canceled"), tr("The migration was canceled. Some files may have been moved.")); + } else if (watcher->future().result()) { + QMessageBox::information(this, tr("Migration Complete"), tr("All backup files were successfully moved to the new location.")); + } else { + QMessageBox::critical(this, tr("Migration Failed"), tr("An error occurred while moving files. Some files may not have been moved. Please check both locations.")); + } + watcher->deleteLater(); + }); + connect(progress, &QProgressDialog::canceled, watcher, &QFutureWatcher::cancel); + + QFuture future = QtConcurrent::run([=] { + QDir new_dir(new_path); + if (!new_dir.exists()) { + if (!new_dir.mkpath(QStringLiteral("."))) { + return false; + } + } + + for (int i = 0; i < files_to_move.count(); ++i) { + if (progress->wasCanceled()) { + return false; + } + progress->setValue(i); + const auto& file_info = files_to_move.at(i); + QString new_file_path = new_dir.filePath(file_info.fileName()); + + if (QFile::exists(new_file_path)) { + if (!QFile::remove(new_file_path)) { + return false; // Failed to remove existing file + } + } + if (!QFile::copy(file_info.absoluteFilePath(), new_file_path)) { + return false; // Copy operation failed + } + if (!QFile::remove(file_info.absoluteFilePath())) { + return false; // Delete operation failed + } + } + return true; + }); + + watcher->setFuture(future); +} +#endif void ConfigureFilesystem::OnUpdateInstallProgress() { if (install_progress) { @@ -59,7 +295,7 @@ void ConfigureFilesystem::OnRunAutoloader(bool skip_confirmation) { QMessageBox msgBox; msgBox.setWindowTitle(tr("Begin Autoloader?")); msgBox.setText(tr("The Autoloader will scan your Game Directories for all .nsp files " - "and attempt to install any found updates or DLC. This may take a while.")); + "and attempt to install any found updates or DLC. This may take a while.")); msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); msgBox.setDefaultButton(QMessageBox::Ok); if (msgBox.exec() != QMessageBox::Ok) { @@ -67,9 +303,8 @@ void ConfigureFilesystem::OnRunAutoloader(bool skip_confirmation) { } } - GMainWindow* main_window = qobject_cast(this->parent()); // Try direct parent first + GMainWindow* main_window = qobject_cast(this->parent()); if (!main_window) { - // Fallback for when it's nested in the config dialog main_window = qobject_cast(this->window()->parent()); } @@ -116,7 +351,6 @@ void ConfigureFilesystem::OnRunAutoloader(bool skip_confirmation) { install_progress->setWindowFlags(install_progress->windowFlags() & ~Qt::WindowContextHelpButtonHint); install_progress->setAttribute(Qt::WA_DeleteOnClose, true); install_progress->setFixedWidth(400); - // When the dialog is destroyed (e.g., user clicks X), set the pointer to nullptr connect(install_progress, &QObject::destroyed, this, [this]() { install_progress = nullptr; }); install_progress->show(); @@ -137,7 +371,7 @@ void ConfigureFilesystem::OnRunAutoloader(bool skip_confirmation) { }; QFuture future = - QtConcurrent::run([&] { return ContentManager::InstallNSP(*system, *vfs, file.toStdString(), progress_callback); }); + QtConcurrent::run([&] { return ContentManager::InstallNSP(*system, *vfs, file.toStdString(), progress_callback); }); while (!future.isFinished()) { QCoreApplication::processEvents(); @@ -147,13 +381,19 @@ void ConfigureFilesystem::OnRunAutoloader(bool skip_confirmation) { ContentManager::InstallResult result = future.result(); switch (result) { - case ContentManager::InstallResult::Success: new_files.append(QFileInfo(file).fileName()); break; - case ContentManager::InstallResult::Overwrite: overwritten_files.append(QFileInfo(file).fileName()); break; - case ContentManager::InstallResult::Failure: failed_files.append(QFileInfo(file).fileName()); break; - case ContentManager::InstallResult::BaseInstallAttempted: - failed_files.append(QFileInfo(file).fileName()); - detected_base_install = true; - break; + case ContentManager::InstallResult::Success: + new_files.append(QFileInfo(file).fileName()); + break; + case ContentManager::InstallResult::Overwrite: + overwritten_files.append(QFileInfo(file).fileName()); + break; + case ContentManager::InstallResult::Failure: + failed_files.append(QFileInfo(file).fileName()); + break; + case ContentManager::InstallResult::BaseInstallAttempted: + failed_files.append(QFileInfo(file).fileName()); + detected_base_install = true; + break; } --remaining; } @@ -173,9 +413,12 @@ void ConfigureFilesystem::OnRunAutoloader(bool skip_confirmation) { } else { QString install_results = tr("Installation Complete!"); install_results.append(QLatin1String("\n\n")); - if (!new_files.isEmpty()) install_results.append(tr("%n file(s) were newly installed.", nullptr, new_files.size())); - if (!overwritten_files.isEmpty()) install_results.append(tr("\n%n file(s) were overwritten.", nullptr, overwritten_files.size())); - if (!failed_files.isEmpty()) install_results.append(tr("\n%n file(s) failed to install.", nullptr, failed_files.size())); + if (!new_files.isEmpty()) + install_results.append(tr("%n file(s) were newly installed.", nullptr, new_files.size())); + if (!overwritten_files.isEmpty()) + install_results.append(tr("\n%n file(s) were overwritten.", nullptr, overwritten_files.size())); + if (!failed_files.isEmpty()) + install_results.append(tr("\n%n file(s) failed to install.", nullptr, failed_files.size())); QMessageBox::information(this, tr("Install Results"), install_results); }