PrismLauncher/launcher/pages/modplatform/ImportPage.cpp

131 lines
3.0 KiB
C++
Raw Normal View History

#include "ImportPage.h"
#include "ui_ImportPage.h"
2018-03-19 01:36:12 +00:00
#include "Launcher.h"
2018-03-19 01:36:12 +00:00
#include "dialogs/NewInstanceDialog.h"
#include <QFileDialog>
2018-06-01 15:20:33 +01:00
#include <QValidator>
2018-03-19 01:36:12 +00:00
#include <InstanceImportTask.h>
class UrlValidator : public QValidator
{
public:
2018-07-15 13:51:05 +01:00
using QValidator::QValidator;
2018-03-19 01:36:12 +00:00
2018-07-15 13:51:05 +01:00
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;
}
}
2018-03-19 01:36:12 +00:00
};
ImportPage::ImportPage(NewInstanceDialog* dialog, QWidget *parent)
: QWidget(parent), ui(new Ui::ImportPage), dialog(dialog)
2018-03-19 01:36:12 +00:00
{
2018-07-15 13:51:05 +01:00
ui->setupUi(this);
ui->modpackEdit->setValidator(new UrlValidator(ui->modpackEdit));
connect(ui->modpackEdit, &QLineEdit::textChanged, this, &ImportPage::updateState);
2018-03-19 01:36:12 +00:00
}
ImportPage::~ImportPage()
2018-03-19 01:36:12 +00:00
{
2018-07-15 13:51:05 +01:00
delete ui;
2018-03-19 01:36:12 +00:00
}
bool ImportPage::shouldDisplay() const
2018-03-19 01:36:12 +00:00
{
2018-07-15 13:51:05 +01:00
return true;
2018-03-19 01:36:12 +00:00
}
void ImportPage::openedImpl()
2018-03-19 01:36:12 +00:00
{
2018-07-15 13:51:05 +01:00
updateState();
2018-03-19 01:36:12 +00:00
}
void ImportPage::updateState()
2018-03-19 01:36:12 +00:00
{
2018-07-15 13:51:05 +01:00
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));
dialog->setSuggestedIcon("default");
2018-07-15 13:51:05 +01:00
}
}
else
{
if(input.endsWith("?client=y")) {
input.chop(9);
input.append("/file");
url = QUrl::fromUserInput(input);
}
2018-07-15 13:51:05 +01:00
// hook, line and sinker.
QFileInfo fi(url.fileName());
dialog->setSuggestedPack(fi.completeBaseName(), new InstanceImportTask(url));
dialog->setSuggestedIcon("default");
2018-07-15 13:51:05 +01:00
}
}
else
{
dialog->setSuggestedPack();
}
2018-03-19 01:36:12 +00:00
}
void ImportPage::setUrl(const QString& url)
2018-03-19 01:36:12 +00:00
{
2018-07-15 13:51:05 +01:00
ui->modpackEdit->setText(url);
updateState();
2018-03-19 01:36:12 +00:00
}
void ImportPage::on_modpackBtn_clicked()
2018-03-19 01:36:12 +00:00
{
2018-07-15 13:51:05 +01:00
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());
}
}
2018-03-19 01:36:12 +00:00
}
QUrl ImportPage::modpackUrl() const
2018-03-19 01:36:12 +00:00
{
2018-07-15 13:51:05 +01:00
const QUrl url(ui->modpackEdit->text());
if (url.isValid() && !url.isRelative() && !url.host().isEmpty())
{
return url;
}
else
{
return QUrl::fromLocalFile(ui->modpackEdit->text());
}
2018-03-19 01:36:12 +00:00
}