2022-03-20 19:01:08 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (c) 2022 Jamie Mansfield <jmansfield@cadixdev.org>
|
2022-05-26 22:18:54 +01:00
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
2022-03-20 19:01:08 +00:00
|
|
|
*
|
|
|
|
* 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-09 20:51:19 +01:00
|
|
|
#include "ImportPage.h"
|
2023-05-16 00:40:56 +01:00
|
|
|
|
2023-04-08 00:54:25 +01:00
|
|
|
#include "ui/dialogs/ProgressDialog.h"
|
2019-07-09 20:51:19 +01:00
|
|
|
#include "ui_ImportPage.h"
|
2018-03-19 01:36:12 +00:00
|
|
|
|
|
|
|
#include <QFileDialog>
|
2018-06-01 15:20:33 +01:00
|
|
|
#include <QValidator>
|
2023-05-16 00:34:33 +01:00
|
|
|
#include <utility>
|
2021-11-22 02:55:16 +00:00
|
|
|
|
|
|
|
#include "ui/dialogs/NewInstanceDialog.h"
|
2023-04-08 00:54:25 +01:00
|
|
|
#include "ui/dialogs/CustomMessageBox.h"
|
|
|
|
|
2023-05-16 00:34:33 +01:00
|
|
|
#include "modplatform/flame/FlameAPI.h"
|
|
|
|
|
2023-04-08 00:54:25 +01:00
|
|
|
#include "Json.h"
|
2021-11-22 02:55:16 +00:00
|
|
|
|
|
|
|
#include "InstanceImportTask.h"
|
|
|
|
|
2018-03-19 01:36:12 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-09 20:51:19 +01:00
|
|
|
ImportPage::ImportPage(NewInstanceDialog* dialog, QWidget *parent)
|
|
|
|
: QWidget(parent), ui(new Ui::ImportPage), dialog(dialog)
|
2018-03-19 01:36:12 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
ui->modpackEdit->setValidator(new UrlValidator(ui->modpackEdit));
|
2019-07-09 20:51:19 +01:00
|
|
|
connect(ui->modpackEdit, &QLineEdit::textChanged, this, &ImportPage::updateState);
|
2018-03-19 01:36:12 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 20:51:19 +01:00
|
|
|
ImportPage::~ImportPage()
|
2018-03-19 01:36:12 +00:00
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2019-07-09 20:51:19 +01:00
|
|
|
bool ImportPage::shouldDisplay() const
|
2018-03-19 01:36:12 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-22 18:23:53 +00:00
|
|
|
void ImportPage::retranslate()
|
|
|
|
{
|
|
|
|
ui->retranslateUi(this);
|
|
|
|
}
|
|
|
|
|
2019-07-09 20:51:19 +01:00
|
|
|
void ImportPage::openedImpl()
|
2018-03-19 01:36:12 +00:00
|
|
|
{
|
|
|
|
updateState();
|
|
|
|
}
|
|
|
|
|
2019-07-09 20:51:19 +01:00
|
|
|
void ImportPage::updateState()
|
2018-03-19 01:36:12 +00:00
|
|
|
{
|
2023-05-13 00:37:45 +01:00
|
|
|
if (!isOpened) {
|
2018-03-19 01:36:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-05-13 00:37:45 +01:00
|
|
|
if (ui->modpackEdit->hasAcceptableInput()) {
|
2018-03-19 01:36:12 +00:00
|
|
|
QString input = ui->modpackEdit->text();
|
|
|
|
auto url = QUrl::fromUserInput(input);
|
2023-05-13 00:37:45 +01:00
|
|
|
if (url.isLocalFile()) {
|
2018-03-19 01:36:12 +00:00
|
|
|
// FIXME: actually do some validation of what's inside here... this is fake AF
|
|
|
|
QFileInfo fi(input);
|
2022-05-15 01:50:54 +01:00
|
|
|
|
|
|
|
// Allow non-latin people to use ZIP files!
|
2022-06-11 12:57:40 +01:00
|
|
|
bool isZip = QMimeDatabase().mimeTypeForUrl(url).suffixes().contains("zip");
|
|
|
|
// mrpack is a modrinth pack
|
|
|
|
bool isMRPack = fi.suffix() == "mrpack";
|
|
|
|
|
2023-05-13 00:37:45 +01:00
|
|
|
if (fi.exists() && (isZip || isMRPack)) {
|
2018-03-19 01:36:12 +00:00
|
|
|
QFileInfo fi(url.fileName());
|
2023-05-16 00:34:33 +01:00
|
|
|
auto extra_info = QMap(m_extra_info);
|
|
|
|
qDebug() << "Pack Extra Info" << extra_info << m_extra_info;
|
|
|
|
dialog->setSuggestedPack(fi.completeBaseName(), new InstanceImportTask(url, this, std::move(extra_info)));
|
2021-06-19 15:19:39 +01:00
|
|
|
dialog->setSuggestedIcon("default");
|
2018-03-19 01:36:12 +00:00
|
|
|
}
|
2023-05-13 00:37:45 +01:00
|
|
|
} else if (url.scheme() == "curseforge") {
|
2023-04-08 00:54:25 +01:00
|
|
|
// need to find the download link for the modpack
|
|
|
|
// format of url curseforge://install?addonId=IDHERE&fileId=IDHERE
|
|
|
|
QUrlQuery query(url);
|
|
|
|
auto addonId = query.allQueryItemValues("addonId")[0];
|
|
|
|
auto fileId = query.allQueryItemValues("fileId")[0];
|
2023-08-06 20:51:03 +01:00
|
|
|
auto array = std::make_shared<QByteArray>();
|
2023-04-08 00:54:25 +01:00
|
|
|
|
2023-05-16 00:34:33 +01:00
|
|
|
auto api = FlameAPI();
|
|
|
|
auto job = api.getFile(addonId, fileId, array);
|
|
|
|
|
|
|
|
connect(job.get(), &NetJob::failed, this,
|
2023-05-13 00:37:45 +01:00
|
|
|
[this](QString reason) { CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show(); });
|
2023-05-16 00:34:33 +01:00
|
|
|
connect(job.get(), &NetJob::succeeded, this, [this, array, addonId, fileId] {
|
2023-04-08 00:54:25 +01:00
|
|
|
qDebug() << "Returned CFURL Json:\n" << array->toStdString().c_str();
|
|
|
|
auto doc = Json::requireDocument(*array);
|
2023-05-28 19:54:32 +01:00
|
|
|
auto data = Json::ensureObject(Json::ensureObject(doc.object()), "data");
|
2023-04-08 00:54:25 +01:00
|
|
|
// No way to find out if it's a mod or a modpack before here
|
|
|
|
// And also we need to check if it ends with .zip, instead of any better way
|
2023-05-28 19:54:32 +01:00
|
|
|
auto fileName = Json::ensureString(data, "fileName");
|
2023-04-08 00:54:25 +01:00
|
|
|
if (fileName.endsWith(".zip")) {
|
|
|
|
// Have to use ensureString then use QUrl to get proper url encoding
|
2023-05-28 19:54:32 +01:00
|
|
|
auto dl_url = QUrl(Json::ensureString(data, "downloadUrl", "", "downloadUrl"));
|
2023-04-08 00:54:25 +01:00
|
|
|
if (!dl_url.isValid()) {
|
2023-05-28 19:54:32 +01:00
|
|
|
CustomMessageBox::selectable(
|
|
|
|
this, tr("Error"),
|
|
|
|
tr("The modpack %1 is blocked for third-parties! Please download it manually.").arg(fileName),
|
|
|
|
QMessageBox::Critical)
|
2023-05-13 00:37:45 +01:00
|
|
|
->show();
|
2023-04-08 00:54:25 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFileInfo dl_file(dl_url.fileName());
|
2023-05-28 19:54:32 +01:00
|
|
|
QString pack_name = Json::ensureString(data, "displayName", dl_file.completeBaseName(), "displayName");
|
2023-04-08 00:54:25 +01:00
|
|
|
|
|
|
|
QMap<QString, QString> extra_info;
|
|
|
|
extra_info.insert("pack_id", addonId);
|
|
|
|
extra_info.insert("pack_version_id", fileId);
|
|
|
|
|
|
|
|
dialog->setSuggestedPack(pack_name, new InstanceImportTask(dl_url, this, std::move(extra_info)));
|
|
|
|
dialog->setSuggestedIcon("default");
|
2023-05-13 00:37:45 +01:00
|
|
|
|
2023-04-08 00:54:25 +01:00
|
|
|
} else {
|
|
|
|
CustomMessageBox::selectable(this, tr("Error"), tr("This url isn't a valid modpack !"), QMessageBox::Critical)->show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ProgressDialog dlUrlDialod(this);
|
|
|
|
dlUrlDialod.setSkipButton(true, tr("Abort"));
|
2023-05-16 00:34:33 +01:00
|
|
|
dlUrlDialod.execWithTask(job.get());
|
2023-04-08 00:54:25 +01:00
|
|
|
return;
|
2023-05-13 00:37:45 +01:00
|
|
|
} else {
|
|
|
|
if (input.endsWith("?client=y")) {
|
2019-07-09 20:51:19 +01:00
|
|
|
input.chop(9);
|
|
|
|
input.append("/file");
|
|
|
|
url = QUrl::fromUserInput(input);
|
|
|
|
}
|
2018-03-19 01:36:12 +00:00
|
|
|
// hook, line and sinker.
|
|
|
|
QFileInfo fi(url.fileName());
|
2023-05-16 00:34:33 +01:00
|
|
|
auto extra_info = QMap(m_extra_info);
|
|
|
|
dialog->setSuggestedPack(fi.completeBaseName(), new InstanceImportTask(url, this, std::move(extra_info)));
|
2021-06-19 15:19:39 +01:00
|
|
|
dialog->setSuggestedIcon("default");
|
2018-03-19 01:36:12 +00:00
|
|
|
}
|
2023-05-13 00:37:45 +01:00
|
|
|
} else {
|
2018-03-19 01:36:12 +00:00
|
|
|
dialog->setSuggestedPack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 20:51:19 +01:00
|
|
|
void ImportPage::setUrl(const QString& url)
|
2018-03-19 01:36:12 +00:00
|
|
|
{
|
|
|
|
ui->modpackEdit->setText(url);
|
|
|
|
updateState();
|
|
|
|
}
|
|
|
|
|
2023-05-16 00:34:33 +01:00
|
|
|
void ImportPage::setExtraInfo(const QMap<QString, QString>& extra_info) {
|
2023-05-28 19:54:32 +01:00
|
|
|
m_extra_info = extra_info;
|
2023-05-16 00:34:33 +01:00
|
|
|
updateState();
|
|
|
|
}
|
|
|
|
|
2019-07-09 20:51:19 +01:00
|
|
|
void ImportPage::on_modpackBtn_clicked()
|
2018-03-19 01:36:12 +00:00
|
|
|
{
|
2022-05-08 00:16:55 +01:00
|
|
|
auto filter = QMimeDatabase().mimeTypeForName("application/zip").filterString();
|
2022-06-11 12:57:40 +01:00
|
|
|
//: Option for filtering for *.mrpack files when importing
|
|
|
|
filter += ";;" + tr("Modrinth pack") + " (*.mrpack)";
|
2022-05-08 00:16:55 +01:00
|
|
|
const QUrl url = QFileDialog::getOpenFileUrl(this, tr("Choose modpack"), modpackUrl(), filter);
|
2018-03-19 01:36:12 +00:00
|
|
|
if (url.isValid())
|
|
|
|
{
|
|
|
|
if (url.isLocalFile())
|
|
|
|
{
|
|
|
|
ui->modpackEdit->setText(url.toLocalFile());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui->modpackEdit->setText(url.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-09 20:51:19 +01:00
|
|
|
QUrl ImportPage::modpackUrl() const
|
2018-03-19 01:36:12 +00:00
|
|
|
{
|
|
|
|
const QUrl url(ui->modpackEdit->text());
|
|
|
|
if (url.isValid() && !url.isRelative() && !url.host().isEmpty())
|
|
|
|
{
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return QUrl::fromLocalFile(ui->modpackEdit->text());
|
|
|
|
}
|
|
|
|
}
|