mirror of
https://github.com/PrismLauncher/PrismLauncher.git
synced 2025-04-29 22:24:26 +02:00
Refactor updateInstanceRoot() to BaseInstance
Signed-off-by: Yihe Li <winmikedows@hotmail.com>
This commit is contained in:
parent
abac3db125
commit
7ea5b6173c
@ -37,11 +37,13 @@
|
||||
|
||||
#include "BaseInstance.h"
|
||||
|
||||
#include <QCheckBox>
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QFileInfo>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QMessageBox>
|
||||
#include <QRegularExpression>
|
||||
|
||||
#include "settings/INISettingsObject.h"
|
||||
@ -327,6 +329,64 @@ 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 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. <br />Only the metadata will be renamed.").arg(newRoot));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Ask if we should rename
|
||||
if (renamingMode == "AskEverytime") {
|
||||
QMessageBox messageBox(parent);
|
||||
messageBox.setText(tr("Do you want to also rename the instance\'s physical directory?"));
|
||||
messageBox.setInformativeText(tr("The following renaming operation will be performed: <br/>"
|
||||
" - Old instance root: %1<br/>"
|
||||
" - New instance root: %2")
|
||||
.arg(oldRoot, newRoot));
|
||||
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: <br/>"
|
||||
" - Old instance root: %1<br/>"
|
||||
" - New instance root: %2<br/>"
|
||||
"Only the metadata is renamed.")
|
||||
.arg(oldRoot, newRoot));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
SettingsObjectPtr BaseInstance::settings()
|
||||
{
|
||||
loadSpecificSettings();
|
||||
|
@ -117,6 +117,9 @@ class BaseInstance : public QObject, public std::enable_shared_from_this<BaseIns
|
||||
/// Path to the instance's root directory.
|
||||
QString instanceRoot() const;
|
||||
|
||||
/// Update instanceRoot to make it sync with name/id; return true if a refresh is needed
|
||||
bool updateInstanceRoot(QWidget* parent);
|
||||
|
||||
/// Path to the instance's game root directory.
|
||||
virtual QString gameRoot() const { return instanceRoot(); }
|
||||
|
||||
|
@ -149,11 +149,6 @@ QStringList InstanceList::getLinkedInstancesById(const QString& id) const
|
||||
return linkedInstances;
|
||||
}
|
||||
|
||||
QString InstanceList::getInstanceRootById(const InstanceId& id) const
|
||||
{
|
||||
return FS::PathCombine(m_instDir, id);
|
||||
}
|
||||
|
||||
int InstanceList::rowCount(const QModelIndex& parent) const
|
||||
{
|
||||
Q_UNUSED(parent);
|
||||
@ -632,7 +627,7 @@ InstancePtr InstanceList::loadInstance(const InstanceId& id)
|
||||
loadGroupList();
|
||||
}
|
||||
|
||||
auto instanceRoot = getInstanceRootById(id);
|
||||
auto instanceRoot = FS::PathCombine(m_instDir, id);
|
||||
auto instanceSettings = std::make_shared<INISettingsObject>(FS::PathCombine(instanceRoot, "instance.cfg"));
|
||||
InstancePtr inst;
|
||||
|
||||
|
@ -152,7 +152,6 @@ class InstanceList : public QAbstractListModel {
|
||||
QMimeData* mimeData(const QModelIndexList& indexes) const override;
|
||||
|
||||
QStringList getLinkedInstancesById(const QString& id) const;
|
||||
QString getInstanceRootById(const InstanceId& id) const;
|
||||
|
||||
signals:
|
||||
void dataIsInvalid();
|
||||
|
@ -565,66 +565,6 @@ void MainWindow::showInstanceContextMenu(const QPoint& pos)
|
||||
myMenu.exec(view->mapToGlobal(pos));
|
||||
}
|
||||
|
||||
void MainWindow::updateInstanceRoot()
|
||||
{
|
||||
QString renamingMode = APPLICATION->settings()->get("InstRenamingMode").toString();
|
||||
if (renamingMode == "MetadataOnly")
|
||||
return;
|
||||
|
||||
auto oldRoot = m_selectedInstance->instanceRoot();
|
||||
auto newID = m_selectedInstance->name();
|
||||
auto newRoot = APPLICATION->instances()->getInstanceRootById(newID);
|
||||
if (oldRoot == newRoot)
|
||||
return;
|
||||
|
||||
// Check for conflict
|
||||
if (QDir(newRoot).exists()) {
|
||||
QMessageBox::warning(this, tr("Cannot rename instance"),
|
||||
tr("New instance root (%1) already exists. <br />Only the metadata will be renamed.").arg(newRoot));
|
||||
return;
|
||||
}
|
||||
|
||||
// Ask if we should rename
|
||||
if (renamingMode == "AskEverytime") {
|
||||
QMessageBox messageBox(this);
|
||||
messageBox.setText(tr("Do you want to also rename the instance\'s physical directory?"));
|
||||
messageBox.setInformativeText(tr("The following renaming operation will be performed: <br/>"
|
||||
" - Old instance root: %1<br/>"
|
||||
" - New instance root: %2")
|
||||
.arg(oldRoot, newRoot));
|
||||
messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
|
||||
messageBox.setDefaultButton(QMessageBox::Yes);
|
||||
messageBox.setIcon(QMessageBox::Question);
|
||||
|
||||
auto checkBox = new QCheckBox(tr("&Remember my choice"), this);
|
||||
messageBox.setCheckBox(checkBox);
|
||||
|
||||
auto res = messageBox.exec();
|
||||
if (checkBox->isChecked()) {
|
||||
if (res == QMessageBox::Yes)
|
||||
APPLICATION->settings()->set("InstRenamingMode", "PhysicalDir");
|
||||
else
|
||||
APPLICATION->settings()->set("InstRenamingMode", "MetadataOnly");
|
||||
}
|
||||
if (res == QMessageBox::No)
|
||||
return;
|
||||
}
|
||||
|
||||
// Now we can confirm that a renaming is happening
|
||||
auto ret = QFile::rename(oldRoot, newRoot);
|
||||
if (!ret) {
|
||||
QMessageBox::warning(this, tr("Cannot rename instance"),
|
||||
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.")
|
||||
.arg(oldRoot, newRoot));
|
||||
return;
|
||||
}
|
||||
refreshInstances();
|
||||
setSelectedInstanceById(newID);
|
||||
}
|
||||
|
||||
void MainWindow::updateMainToolBar()
|
||||
{
|
||||
ui->menuBar->setVisible(APPLICATION->settings()->get("MenuBarInsteadOfToolBar").toBool());
|
||||
@ -1764,7 +1704,11 @@ void MainWindow::instanceDataChanged(const QModelIndex& topLeft, const QModelInd
|
||||
QItemSelection test(topLeft, bottomRight);
|
||||
if (test.contains(current)) {
|
||||
instanceChanged(current, current);
|
||||
updateInstanceRoot();
|
||||
if (m_selectedInstance && m_selectedInstance->updateInstanceRoot(this)) {
|
||||
auto newID = m_selectedInstance->name();
|
||||
refreshInstances();
|
||||
setSelectedInstanceById(newID);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -176,8 +176,6 @@ class MainWindow : public QMainWindow {
|
||||
|
||||
void showInstanceContextMenu(const QPoint&);
|
||||
|
||||
void updateInstanceRoot();
|
||||
|
||||
void updateMainToolBar();
|
||||
|
||||
void updateLaunchButton();
|
||||
|
Loading…
x
Reference in New Issue
Block a user