From 44669290747b1d254d10c130e4e728051c8c4c5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 3 Nov 2013 21:28:04 +0100 Subject: Add instance copying, fix instance creation (with identical names) --- gui/mainwindow.cpp | 96 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 77 insertions(+), 19 deletions(-) (limited to 'gui/mainwindow.cpp') diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 316117b7..fd086f11 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -74,6 +74,7 @@ #include "IconPickerDialog.h" #include "LabeledToolButton.h" #include "EditNotesDialog.h" +#include "CopyInstanceDialog.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { @@ -91,7 +92,9 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi // The instance action toolbar customizations { + // disabled until we have an instance selected ui->instanceToolBar->setEnabled(false); + // the rename label is inside the rename tool button renameButton = new LabeledToolButton(); renameButton->setText("Instance Name"); @@ -267,8 +270,9 @@ void MainWindow::on_actionAddInstance_triggered() BaseInstance *newInstance = NULL; - QString instDirName = DirNameFromString(newInstDlg.instName()); - QString instDir = PathCombine(MMC->settings()->get("InstanceDir").toString(), instDirName); + QString instancesDir = MMC->settings()->get("InstanceDir").toString(); + QString instDirName = DirNameFromString(newInstDlg.instName(), instancesDir); + QString instDir = PathCombine(instancesDir, instDirName); auto &loader = InstanceFactory::get(); @@ -305,6 +309,56 @@ void MainWindow::on_actionAddInstance_triggered() } } +void MainWindow::on_actionCopyInstance_triggered() +{ + if (!m_selectedInstance) + return; + + CopyInstanceDialog copyInstDlg(m_selectedInstance, this); + if (!copyInstDlg.exec()) + return; + + QString instancesDir = MMC->settings()->get("InstanceDir").toString(); + QString instDirName = DirNameFromString(copyInstDlg.instName(), instancesDir); + QString instDir = PathCombine(instancesDir, instDirName); + + auto &loader = InstanceFactory::get(); + + BaseInstance *newInstance = NULL; + auto error = loader.copyInstance(newInstance, m_selectedInstance, instDir); + + QString errorMsg = QString("Failed to create instance %1: ").arg(instDirName); + switch (error) + { + case InstanceFactory::NoCreateError: + newInstance->setName(copyInstDlg.instName()); + newInstance->setIconKey(copyInstDlg.iconKey()); + MMC->instances()->add(InstancePtr(newInstance)); + return; + + case InstanceFactory::InstExists: + { + errorMsg += "An instance with the given directory name already exists."; + CustomMessageBox::selectable(this, tr("Error"), errorMsg, QMessageBox::Warning)->show(); + break; + } + + case InstanceFactory::CantCreateDir: + { + errorMsg += "Failed to create the instance directory."; + CustomMessageBox::selectable(this, tr("Error"), errorMsg, QMessageBox::Warning)->show(); + break; + } + + default: + { + errorMsg += QString("Unknown instance loader error %1").arg(error); + CustomMessageBox::selectable(this, tr("Error"), errorMsg, QMessageBox::Warning)->show(); + break; + } + } +} + void MainWindow::on_actionChangeInstIcon_triggered() { if (!m_selectedInstance) @@ -395,10 +449,10 @@ void MainWindow::on_actionDeleteInstance_triggered() { if (m_selectedInstance) { - auto response = CustomMessageBox::selectable(this, tr("CAREFUL"), - tr("This is permanent! Are you sure?\nAbout to delete: ") - + m_selectedInstance->name(), - QMessageBox::Question, QMessageBox::Yes | QMessageBox::No)->exec(); + auto response = CustomMessageBox::selectable( + this, tr("CAREFUL"), tr("This is permanent! Are you sure?\nAbout to delete: ") + + m_selectedInstance->name(), + QMessageBox::Question, QMessageBox::Yes | QMessageBox::No)->exec(); if (response == QMessageBox::Yes) { m_selectedInstance->nuke(); @@ -640,7 +694,8 @@ void MainWindow::onGameUpdateComplete() void MainWindow::onGameUpdateError(QString error) { - CustomMessageBox::selectable(this, tr("Error updating instance"), error, QMessageBox::Warning)->show(); + CustomMessageBox::selectable(this, tr("Error updating instance"), error, + QMessageBox::Warning)->show(); } void MainWindow::launchInstance(BaseInstance *instance, LoginResponse response) @@ -709,9 +764,10 @@ void MainWindow::on_actionMakeDesktopShortcut_triggered() QStringList() << "-dl" << QDir::currentPath() << "test", name, "application-x-octet-stream"); - CustomMessageBox::selectable(this, tr("Not useful"), - tr("A Dummy Shortcut was created. it will not do anything productive"), - QMessageBox::Warning)->show(); + CustomMessageBox::selectable( + this, tr("Not useful"), + tr("A Dummy Shortcut was created. it will not do anything productive"), + QMessageBox::Warning)->show(); } // BrowserDialog @@ -732,10 +788,11 @@ void MainWindow::on_actionChangeInstMCVersion_triggered() { if (m_selectedInstance->versionIsCustom()) { - auto result = CustomMessageBox::selectable(this, tr("Are you sure?"), - tr("This will remove any library/version customization you did previously. " - "This includes things like Forge install and similar."), - QMessageBox::Warning, QMessageBox::Ok, QMessageBox::Abort)->exec(); + auto result = CustomMessageBox::selectable( + this, tr("Are you sure?"), + tr("This will remove any library/version customization you did previously. " + "This includes things like Forge install and similar."), + QMessageBox::Warning, QMessageBox::Ok, QMessageBox::Abort)->exec(); if (result != QMessageBox::Ok) return; @@ -867,11 +924,12 @@ void MainWindow::checkSetDefaultJava() java = std::dynamic_pointer_cast(vselect.selectedVersion()); else { - CustomMessageBox::selectable(this, tr("Invalid version selected"), - tr("You didn't select a valid Java version, so MultiMC will " - "select the default. " - "You can change this in the settings dialog."), - QMessageBox::Warning)->show(); + CustomMessageBox::selectable( + this, tr("Invalid version selected"), + tr("You didn't select a valid Java version, so MultiMC will " + "select the default. " + "You can change this in the settings dialog."), + QMessageBox::Warning)->show(); JavaUtils ju; java = ju.GetDefaultJava(); -- cgit v1.2.3