PrismLauncher/launcher/ui/pages/modplatform/ImportPage.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

159 lines
4.9 KiB
C++
Raw Normal View History

2022-03-20 19:01:08 +00:00
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
2022-03-20 19:01:08 +00:00
* Copyright (c) 2022 Jamie Mansfield <jmansfield@cadixdev.org>
* 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.
*/
#include "ImportPage.h"
#include "ui_ImportPage.h"
2018-03-19 01:36:12 +00:00
#include <QFileDialog>
2018-06-01 15:20:33 +01:00
#include <QValidator>
#include "ui/dialogs/NewInstanceDialog.h"
#include "InstanceImportTask.h"
class UrlValidator : public QValidator {
public:
2018-03-19 01:36:12 +00:00
using QValidator::QValidator;
State validate(QString& in, [[maybe_unused]] int& pos) const
2018-03-19 01:36:12 +00:00
{
const QUrl url(in);
if (url.isValid() && !url.isRelative() && !url.isEmpty()) {
2018-03-19 01:36:12 +00:00
return Acceptable;
} else if (QFile::exists(in)) {
2018-03-19 01:36:12 +00:00
return Acceptable;
} else {
2018-03-19 01:36:12 +00:00
return Intermediate;
}
}
};
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));
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
{
delete ui;
}
bool ImportPage::shouldDisplay() const
2018-03-19 01:36:12 +00:00
{
return true;
}
void ImportPage::retranslate()
{
ui->retranslateUi(this);
}
void ImportPage::openedImpl()
2018-03-19 01:36:12 +00:00
{
updateState();
}
void ImportPage::updateState()
2018-03-19 01:36:12 +00:00
{
if (!isOpened) {
2018-03-19 01:36:12 +00:00
return;
}
if (ui->modpackEdit->hasAcceptableInput()) {
2018-03-19 01:36:12 +00:00
QString input = ui->modpackEdit->text();
auto url = QUrl::fromUserInput(input);
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!
bool isZip = QMimeDatabase().mimeTypeForUrl(url).suffixes().contains("zip");
// mrpack is a modrinth pack
bool isMRPack = fi.suffix() == "mrpack";
if (fi.exists() && (isZip || isMRPack)) {
QFileInfo file_info(url.fileName());
dialog->setSuggestedPack(file_info.completeBaseName(), new InstanceImportTask(url, this));
dialog->setSuggestedIcon("default");
2018-03-19 01:36:12 +00:00
}
} else {
if (input.endsWith("?client=y")) {
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());
dialog->setSuggestedPack(fi.completeBaseName(), new InstanceImportTask(url, this));
dialog->setSuggestedIcon("default");
2018-03-19 01:36:12 +00:00
}
} else {
2018-03-19 01:36:12 +00:00
dialog->setSuggestedPack();
}
}
void ImportPage::setUrl(const QString& url)
2018-03-19 01:36:12 +00:00
{
ui->modpackEdit->setText(url);
updateState();
}
void ImportPage::on_modpackBtn_clicked()
2018-03-19 01:36:12 +00:00
{
auto filter = QMimeDatabase().mimeTypeForName("application/zip").filterString();
//: Option for filtering for *.mrpack files when importing
filter += ";;" + tr("Modrinth pack") + " (*.mrpack)";
const QUrl url = QFileDialog::getOpenFileUrl(this, tr("Choose modpack"), modpackUrl(), filter);
if (url.isValid()) {
if (url.isLocalFile()) {
2018-03-19 01:36:12 +00:00
ui->modpackEdit->setText(url.toLocalFile());
} else {
2018-03-19 01:36:12 +00:00
ui->modpackEdit->setText(url.toString());
}
}
}
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()) {
2018-03-19 01:36:12 +00:00
return url;
} else {
2018-03-19 01:36:12 +00:00
return QUrl::fromLocalFile(ui->modpackEdit->text());
}
}