| 43 | #include "ui/dialogs/CustomMessageBox.h" |
| 44 | |
| 45 | QString askToUpdateInstanceDirName(BaseInstance* instance, const QString& oldName, const QString& newName, QWidget* parent) |
| 46 | { |
| 47 | if (oldName == newName) |
| 48 | return QString(); |
| 49 | |
| 50 | QString renamingMode = APPLICATION->settings()->get("InstRenamingMode").toString(); |
| 51 | if (renamingMode == "MetadataOnly") |
| 52 | return QString(); |
| 53 | |
| 54 | auto oldRoot = instance->instanceRoot(); |
| 55 | auto newDirName = FS::DirNameFromString(newName, QFileInfo(oldRoot).dir().absolutePath()); |
| 56 | auto newRoot = FS::PathCombine(QFileInfo(oldRoot).dir().absolutePath(), newDirName); |
| 57 | if (oldRoot == newRoot) |
| 58 | return QString(); |
| 59 | if (oldRoot == FS::PathCombine(QFileInfo(oldRoot).dir().absolutePath(), newName)) |
| 60 | return QString(); |
| 61 | |
| 62 | // Check for conflict |
| 63 | if (QDir(newRoot).exists()) { |
| 64 | QMessageBox::warning(parent, QObject::tr("Cannot rename instance"), |
| 65 | QObject::tr("New instance root (%1) already exists. <br />Only the metadata will be renamed.").arg(newRoot)); |
| 66 | return QString(); |
| 67 | } |
| 68 | |
| 69 | // Ask if we should rename |
| 70 | if (renamingMode == "AskEverytime") { |
| 71 | auto checkBox = new QCheckBox(QObject::tr("&Remember my choice"), parent); |
| 72 | auto dialog = |
| 73 | CustomMessageBox::selectable(parent, QObject::tr("Rename instance folder"), |
| 74 | QObject::tr("Would you also like to rename the instance folder?\n\n" |
| 75 | "Old name: %1\n" |
| 76 | "New name: %2") |
| 77 | .arg(oldName, newName), |
| 78 | QMessageBox::Question, QMessageBox::No | QMessageBox::Yes, QMessageBox::NoButton, checkBox); |
| 79 | |
| 80 | auto res = dialog->exec(); |
| 81 | if (checkBox->isChecked()) { |
| 82 | if (res == QMessageBox::Yes) |
| 83 | APPLICATION->settings()->set("InstRenamingMode", "PhysicalDir"); |
| 84 | else |
| 85 | APPLICATION->settings()->set("InstRenamingMode", "MetadataOnly"); |
| 86 | } |
| 87 | if (res == QMessageBox::No) |
| 88 | return QString(); |
| 89 | } |
| 90 | |
| 91 | // Check for linked instances |
| 92 | if (!checkLinkedInstances(instance->id(), parent, QObject::tr("Renaming"))) |
| 93 | return QString(); |
| 94 | |
| 95 | // Now we can confirm that a renaming is happening |
| 96 | if (!instance->syncInstanceDirName(newRoot)) { |
| 97 | QMessageBox::warning(parent, QObject::tr("Cannot rename instance"), |
| 98 | QObject::tr("An error occurred when performing the following renaming operation: <br/>" |
| 99 | " - Old instance root: %1<br/>" |
| 100 | " - New instance root: %2<br/>" |
| 101 | "Only the metadata is renamed.") |
| 102 | .arg(oldRoot, newRoot)); |
no test coverage detected