Add link instance detection

Signed-off-by: Yihe Li <winmikedows@hotmail.com>
This commit is contained in:
Yihe Li 2025-03-27 05:47:47 +08:00
parent 7b511f4c67
commit ea44c2465c
No known key found for this signature in database
3 changed files with 41 additions and 23 deletions

View File

@ -40,11 +40,15 @@
#include <QCheckBox>
#include <QMessageBox>
#include "Application.h"
#include "FileSystem.h"
bool askToUpdateInstanceDirName(SettingsObjectPtr globalSettings, InstancePtr instance, QWidget* parent)
#include "InstanceList.h"
#include "ui/dialogs/CustomMessageBox.h"
bool askToUpdateInstanceDirName(InstancePtr instance, QWidget* parent)
{
QString renamingMode = globalSettings->get("InstRenamingMode").toString();
QString renamingMode = APPLICATION->settings()->get("InstRenamingMode").toString();
if (renamingMode == "MetadataOnly")
return false;
@ -76,24 +80,47 @@ bool askToUpdateInstanceDirName(SettingsObjectPtr globalSettings, InstancePtr in
auto res = messageBox.exec();
if (checkBox->isChecked()) {
if (res == QMessageBox::Yes)
globalSettings->set("InstRenamingMode", "PhysicalDir");
APPLICATION->settings()->set("InstRenamingMode", "PhysicalDir");
else
globalSettings->set("InstRenamingMode", "MetadataOnly");
APPLICATION->settings()->set("InstRenamingMode", "MetadataOnly");
}
if (res == QMessageBox::No)
return false;
}
// Check for linked instances
if (!checkLinkedInstances(instance->id(), parent))
return false;
// Now we can confirm that a renaming is happening
auto ret = QFile::rename(oldRoot, newRoot);
if (!ret) {
QMessageBox::warning(parent, QObject::tr("Cannot rename instance"),
QObject::tr("An error occurred when performing the following renaming operation: <br/>"
" - Old instance root: %1<br/>"
" - New instance root: %2<br/>"
"Only the metadata is renamed.")
" - Old instance root: %1<br/>"
" - New instance root: %2<br/>"
"Only the metadata is renamed.")
.arg(oldRoot, newRoot));
return false;
}
return true;
}
bool checkLinkedInstances(const QString& id, QWidget* parent)
{
auto linkedInstances = APPLICATION->instances()->getLinkedInstancesById(id);
if (!linkedInstances.empty()) {
auto response = CustomMessageBox::selectable(parent, QObject::tr("There are linked instances"),
QObject::tr("The following instance(s) might reference files in this instance:\n\n"
"%1\n\n"
"Deleting it could break the other instance(s), \n\n"
"Do you wish to proceed?",
nullptr, linkedInstances.count())
.arg(linkedInstances.join("\n")),
QMessageBox::Warning, QMessageBox::Yes | QMessageBox::No, QMessageBox::No)
->exec();
if (response != QMessageBox::Yes)
return false;
}
return true;
}

View File

@ -40,4 +40,7 @@
#include "BaseInstance.h"
/// Update instanceRoot to make it sync with name/id; return true if a refresh is needed
bool askToUpdateInstanceDirName(SettingsObjectPtr globalSettings, InstancePtr instance, QWidget* parent);
bool askToUpdateInstanceDirName(InstancePtr instance, QWidget* parent);
/// Check if there are linked instances, and display a warning; return true if the operation should proceed
bool checkLinkedInstances(const QString& id, QWidget* parent);

View File

@ -1379,20 +1379,8 @@ void MainWindow::on_actionDeleteInstance_triggered()
if (response != QMessageBox::Yes)
return;
auto linkedInstances = APPLICATION->instances()->getLinkedInstancesById(id);
if (!linkedInstances.empty()) {
response = CustomMessageBox::selectable(this, tr("There are linked instances"),
tr("The following instance(s) might reference files in this instance:\n\n"
"%1\n\n"
"Deleting it could break the other instance(s), \n\n"
"Do you wish to proceed?",
nullptr, linkedInstances.count())
.arg(linkedInstances.join("\n")),
QMessageBox::Warning, QMessageBox::Yes | QMessageBox::No, QMessageBox::No)
->exec();
if (response != QMessageBox::Yes)
return;
}
if (!checkLinkedInstances(id, this))
return;
if (APPLICATION->instances()->trashInstance(id)) {
ui->actionUndoTrashInstance->setEnabled(APPLICATION->instances()->trashedSomething());
@ -1705,7 +1693,7 @@ void MainWindow::instanceDataChanged(const QModelIndex& topLeft, const QModelInd
QItemSelection test(topLeft, bottomRight);
if (test.contains(current)) {
instanceChanged(current, current);
if (m_selectedInstance && askToUpdateInstanceDirName(APPLICATION->settings(), m_selectedInstance, this)) {
if (m_selectedInstance && askToUpdateInstanceDirName(m_selectedInstance, this)) {
auto newID = m_selectedInstance->name();
refreshInstances();
setSelectedInstanceById(newID);