From f74e3db804f2fb3d73cbf7ab5fbdb12ecea0f259 Mon Sep 17 00:00:00 2001 From: Petr Mrázek Date: Sat, 29 Jun 2019 01:13:39 +0200 Subject: NOISSUE Import page is now a MultiMC pack page --- application/pages/modplatform/MultiMCPage.cpp | 123 ++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 application/pages/modplatform/MultiMCPage.cpp (limited to 'application/pages/modplatform/MultiMCPage.cpp') diff --git a/application/pages/modplatform/MultiMCPage.cpp b/application/pages/modplatform/MultiMCPage.cpp new file mode 100644 index 00000000..c98596c2 --- /dev/null +++ b/application/pages/modplatform/MultiMCPage.cpp @@ -0,0 +1,123 @@ +#include "MultiMCPage.h" +#include "ui_MultiMCPage.h" + +#include "MultiMC.h" +#include "dialogs/NewInstanceDialog.h" +#include +#include +#include + +class UrlValidator : public QValidator +{ +public: + using QValidator::QValidator; + + State validate(QString &in, int &pos) const + { + const QUrl url(in); + if (url.isValid() && !url.isRelative() && !url.isEmpty()) + { + return Acceptable; + } + else if (QFile::exists(in)) + { + return Acceptable; + } + else + { + return Intermediate; + } + } +}; + +MultiMCPage::MultiMCPage(NewInstanceDialog* dialog, QWidget *parent) + : QWidget(parent), ui(new Ui::MultiMCPage), dialog(dialog) +{ + ui->setupUi(this); + ui->modpackEdit->setValidator(new UrlValidator(ui->modpackEdit)); + connect(ui->modpackEdit, &QLineEdit::textChanged, this, &MultiMCPage::updateState); +} + +MultiMCPage::~MultiMCPage() +{ + delete ui; +} + +bool MultiMCPage::shouldDisplay() const +{ + return true; +} + +void MultiMCPage::openedImpl() +{ + updateState(); +} + +void MultiMCPage::updateState() +{ + if(!isOpened) + { + return; + } + if(ui->modpackEdit->hasAcceptableInput()) + { + QString input = ui->modpackEdit->text(); + auto url = QUrl::fromUserInput(input); + if(url.isLocalFile()) + { + // FIXME: actually do some validation of what's inside here... this is fake AF + QFileInfo fi(input); + if(fi.exists() && fi.suffix() == "zip") + { + QFileInfo fi(url.fileName()); + dialog->setSuggestedPack(fi.completeBaseName(), new InstanceImportTask(url)); + } + } + else + { + // hook, line and sinker. + QFileInfo fi(url.fileName()); + dialog->setSuggestedPack(fi.completeBaseName(), new InstanceImportTask(url)); + } + } + else + { + dialog->setSuggestedPack(); + } +} + +void MultiMCPage::setUrl(const QString& url) +{ + ui->modpackEdit->setText(url); + updateState(); +} + +void MultiMCPage::on_modpackBtn_clicked() +{ + const QUrl url = QFileDialog::getOpenFileUrl(this, tr("Choose modpack"), modpackUrl(), tr("Zip (*.zip)")); + if (url.isValid()) + { + if (url.isLocalFile()) + { + ui->modpackEdit->setText(url.toLocalFile()); + } + else + { + ui->modpackEdit->setText(url.toString()); + } + } +} + + +QUrl MultiMCPage::modpackUrl() const +{ + const QUrl url(ui->modpackEdit->text()); + if (url.isValid() && !url.isRelative() && !url.host().isEmpty()) + { + return url; + } + else + { + return QUrl::fromLocalFile(ui->modpackEdit->text()); + } +} -- cgit