diff --git a/launcher/BaseInstance.cpp b/launcher/BaseInstance.cpp index 92353da10..0087b44f0 100644 --- a/launcher/BaseInstance.cpp +++ b/launcher/BaseInstance.cpp @@ -329,62 +329,6 @@ QString BaseInstance::instanceRoot() const return m_rootDir; } -bool BaseInstance::updateInstanceRoot(QWidget* parent) -{ - QString renamingMode = globalSettings()->get("InstRenamingMode").toString(); - if (renamingMode == "MetadataOnly") - return false; - - auto oldRoot = instanceRoot(); - auto oldName = QFileInfo(oldRoot).baseName(); - auto newRoot = FS::PathCombine(QFileInfo(oldRoot).dir().absolutePath(), name()); - if (oldRoot == newRoot) - return false; - - // Check for conflict - if (QDir(newRoot).exists()) { - QMessageBox::warning(parent, tr("Cannot rename instance"), - tr("New instance root (%1) already exists.
Only the metadata will be renamed.").arg(newRoot)); - return false; - } - - // Ask if we should rename - if (renamingMode == "AskEverytime") { - QMessageBox messageBox(parent); - messageBox.setText(tr("Would you also like to rename the instance folder?")); - messageBox.setInformativeText(tr("Renaming \'%1\' -> \'%2\'").arg(oldName, name())); - messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); - messageBox.setDefaultButton(QMessageBox::Yes); - messageBox.setIcon(QMessageBox::Question); - - auto checkBox = new QCheckBox(tr("&Remember my choice"), parent); - messageBox.setCheckBox(checkBox); - - auto res = messageBox.exec(); - if (checkBox->isChecked()) { - if (res == QMessageBox::Yes) - globalSettings()->set("InstRenamingMode", "PhysicalDir"); - else - globalSettings()->set("InstRenamingMode", "MetadataOnly"); - } - if (res == QMessageBox::No) - return false; - } - - // Now we can confirm that a renaming is happening - auto ret = QFile::rename(oldRoot, newRoot); - if (!ret) { - QMessageBox::warning(parent, tr("Cannot rename instance"), - tr("An error occurred when performing the following renaming operation:
" - " - Old instance root: %1
" - " - New instance root: %2
" - "Only the metadata is renamed.") - .arg(oldRoot, newRoot)); - return false; - } - return true; -} - SettingsObjectPtr BaseInstance::settings() { loadSpecificSettings(); diff --git a/launcher/BaseInstance.h b/launcher/BaseInstance.h index 8efb9e9d8..9827a08b4 100644 --- a/launcher/BaseInstance.h +++ b/launcher/BaseInstance.h @@ -117,9 +117,6 @@ class BaseInstance : public QObject, public std::enable_shared_from_this + * Copyright (c) 2022 Jamie Mansfield + * Copyright (C) 2023 TheKodeToad + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * This file incorporates work covered by the following copyright and + * permission notice: + * + * Copyright 2013-2021 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "InstanceDirUpdate.h" + +#include +#include + +#include "FileSystem.h" + +bool askToUpdateInstanceDirName(SettingsObjectPtr globalSettings, InstancePtr instance, QWidget* parent) +{ + QString renamingMode = globalSettings->get("InstRenamingMode").toString(); + if (renamingMode == "MetadataOnly") + return false; + + auto oldRoot = instance->instanceRoot(); + auto oldName = QFileInfo(oldRoot).baseName(); + auto newRoot = FS::PathCombine(QFileInfo(oldRoot).dir().absolutePath(), instance->name()); + if (oldRoot == newRoot) + return false; + + // Check for conflict + if (QDir(newRoot).exists()) { + QMessageBox::warning(parent, QObject::tr("Cannot rename instance"), + QObject::tr("New instance root (%1) already exists.
Only the metadata will be renamed.").arg(newRoot)); + return false; + } + + // Ask if we should rename + if (renamingMode == "AskEverytime") { + QMessageBox messageBox(parent); + messageBox.setText(QObject::tr("Would you also like to rename the instance folder?")); + messageBox.setInformativeText(QObject::tr("Renaming \'%1\' -> \'%2\'").arg(oldName, instance->name())); + messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); + messageBox.setDefaultButton(QMessageBox::Yes); + messageBox.setIcon(QMessageBox::Question); + + auto checkBox = new QCheckBox(QObject::tr("&Remember my choice"), parent); + messageBox.setCheckBox(checkBox); + + auto res = messageBox.exec(); + if (checkBox->isChecked()) { + if (res == QMessageBox::Yes) + globalSettings->set("InstRenamingMode", "PhysicalDir"); + else + globalSettings->set("InstRenamingMode", "MetadataOnly"); + } + if (res == QMessageBox::No) + 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:
" + " - Old instance root: %1
" + " - New instance root: %2
" + "Only the metadata is renamed.") + .arg(oldRoot, newRoot)); + return false; + } + return true; +} diff --git a/launcher/InstanceDirUpdate.h b/launcher/InstanceDirUpdate.h new file mode 100644 index 000000000..fc0931779 --- /dev/null +++ b/launcher/InstanceDirUpdate.h @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: GPL-3.0-only +/* + * Prism Launcher - Minecraft Launcher + * Copyright (C) 2022 Sefa Eyeoglu + * Copyright (c) 2022 Jamie Mansfield + * Copyright (C) 2023 TheKodeToad + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * This file incorporates work covered by the following copyright and + * permission notice: + * + * Copyright 2013-2021 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#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); diff --git a/launcher/ui/MainWindow.cpp b/launcher/ui/MainWindow.cpp index 17e5a0bc9..23cd6f8c8 100644 --- a/launcher/ui/MainWindow.cpp +++ b/launcher/ui/MainWindow.cpp @@ -124,6 +124,7 @@ #include "KonamiCode.h" #include "InstanceCopyTask.h" +#include "InstanceDirUpdate.h" #include "Json.h" @@ -1704,7 +1705,7 @@ void MainWindow::instanceDataChanged(const QModelIndex& topLeft, const QModelInd QItemSelection test(topLeft, bottomRight); if (test.contains(current)) { instanceChanged(current, current); - if (m_selectedInstance && m_selectedInstance->updateInstanceRoot(this)) { + if (m_selectedInstance && askToUpdateInstanceDirName(APPLICATION->settings(), m_selectedInstance, this)) { auto newID = m_selectedInstance->name(); refreshInstances(); setSelectedInstanceById(newID);