2022-03-21 00:31:08 +05:30
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (c) 2022 Jamie Mansfield <jmansfield@cadixdev.org>
|
2022-05-15 16:50:05 +05:30
|
|
|
* Copyright (c) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
2022-03-21 00:31:08 +05:30
|
|
|
*
|
|
|
|
* 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 <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2019-07-10 01:21:19 +05:30
|
|
|
#include "ImportPage.h"
|
|
|
|
#include "ui_ImportPage.h"
|
2018-03-19 07:06:12 +05:30
|
|
|
|
|
|
|
#include <QFileDialog>
|
2018-06-01 19:50:33 +05:30
|
|
|
#include <QValidator>
|
2021-11-22 08:25:16 +05:30
|
|
|
|
|
|
|
#include "ui/dialogs/NewInstanceDialog.h"
|
|
|
|
|
|
|
|
#include "InstanceImportTask.h"
|
|
|
|
|
2018-03-19 07:06:12 +05:30
|
|
|
|
|
|
|
class UrlValidator : public QValidator
|
|
|
|
{
|
|
|
|
public:
|
2018-07-15 18:21:05 +05:30
|
|
|
using QValidator::QValidator;
|
2018-03-19 07:06:12 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
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 07:06:12 +05:30
|
|
|
};
|
|
|
|
|
2019-07-10 01:21:19 +05:30
|
|
|
ImportPage::ImportPage(NewInstanceDialog* dialog, QWidget *parent)
|
|
|
|
: QWidget(parent), ui(new Ui::ImportPage), dialog(dialog)
|
2018-03-19 07:06:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
ui->setupUi(this);
|
|
|
|
ui->modpackEdit->setValidator(new UrlValidator(ui->modpackEdit));
|
2019-07-10 01:21:19 +05:30
|
|
|
connect(ui->modpackEdit, &QLineEdit::textChanged, this, &ImportPage::updateState);
|
2018-03-19 07:06:12 +05:30
|
|
|
}
|
|
|
|
|
2019-07-10 01:21:19 +05:30
|
|
|
ImportPage::~ImportPage()
|
2018-03-19 07:06:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
delete ui;
|
2018-03-19 07:06:12 +05:30
|
|
|
}
|
|
|
|
|
2019-07-10 01:21:19 +05:30
|
|
|
bool ImportPage::shouldDisplay() const
|
2018-03-19 07:06:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return true;
|
2018-03-19 07:06:12 +05:30
|
|
|
}
|
|
|
|
|
2022-02-22 23:53:53 +05:30
|
|
|
void ImportPage::retranslate()
|
|
|
|
{
|
|
|
|
ui->retranslateUi(this);
|
|
|
|
}
|
|
|
|
|
2019-07-10 01:21:19 +05:30
|
|
|
void ImportPage::openedImpl()
|
2018-03-19 07:06:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
updateState();
|
2018-03-19 07:06:12 +05:30
|
|
|
}
|
|
|
|
|
2019-07-10 01:21:19 +05:30
|
|
|
void ImportPage::updateState()
|
2018-03-19 07:06:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
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);
|
2022-01-31 19:55:36 +05:30
|
|
|
// mrpack is a modrinth pack
|
2022-05-15 06:20:54 +05:30
|
|
|
|
|
|
|
// Allow non-latin people to use ZIP files!
|
|
|
|
auto zip = QMimeDatabase().mimeTypeForUrl(url).suffixes().contains("zip");
|
|
|
|
if(fi.exists() && (zip || fi.suffix() == "mrpack"))
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
|
|
|
QFileInfo fi(url.fileName());
|
|
|
|
dialog->setSuggestedPack(fi.completeBaseName(), new InstanceImportTask(url));
|
2021-06-19 19:49:39 +05:30
|
|
|
dialog->setSuggestedIcon("default");
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-07-10 01:21:19 +05:30
|
|
|
if(input.endsWith("?client=y")) {
|
|
|
|
input.chop(9);
|
|
|
|
input.append("/file");
|
|
|
|
url = QUrl::fromUserInput(input);
|
|
|
|
}
|
2018-07-15 18:21:05 +05:30
|
|
|
// hook, line and sinker.
|
|
|
|
QFileInfo fi(url.fileName());
|
|
|
|
dialog->setSuggestedPack(fi.completeBaseName(), new InstanceImportTask(url));
|
2021-06-19 19:49:39 +05:30
|
|
|
dialog->setSuggestedIcon("default");
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dialog->setSuggestedPack();
|
|
|
|
}
|
2018-03-19 07:06:12 +05:30
|
|
|
}
|
|
|
|
|
2019-07-10 01:21:19 +05:30
|
|
|
void ImportPage::setUrl(const QString& url)
|
2018-03-19 07:06:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
ui->modpackEdit->setText(url);
|
|
|
|
updateState();
|
2018-03-19 07:06:12 +05:30
|
|
|
}
|
|
|
|
|
2019-07-10 01:21:19 +05:30
|
|
|
void ImportPage::on_modpackBtn_clicked()
|
2018-03-19 07:06:12 +05:30
|
|
|
{
|
2022-05-08 04:46:55 +05:30
|
|
|
auto filter = QMimeDatabase().mimeTypeForName("application/zip").filterString();
|
2022-05-14 23:26:38 +05:30
|
|
|
filter += ";;" + tr("Modrinth pack (*.mrpack)");
|
2022-05-08 04:46:55 +05:30
|
|
|
const QUrl url = QFileDialog::getOpenFileUrl(this, tr("Choose modpack"), modpackUrl(), filter);
|
2018-07-15 18:21:05 +05:30
|
|
|
if (url.isValid())
|
|
|
|
{
|
|
|
|
if (url.isLocalFile())
|
|
|
|
{
|
|
|
|
ui->modpackEdit->setText(url.toLocalFile());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui->modpackEdit->setText(url.toString());
|
|
|
|
}
|
|
|
|
}
|
2018-03-19 07:06:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-10 01:21:19 +05:30
|
|
|
QUrl ImportPage::modpackUrl() const
|
2018-03-19 07:06:12 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
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 07:06:12 +05:30
|
|
|
}
|