Made JavaDownloader a task and added some quick UI
Signed-off-by: timoreo <contact@timoreo.fr>
This commit is contained in:
parent
54ad91c3b7
commit
53ddba8077
@ -15,17 +15,21 @@ struct File {
|
||||
bool isExec;
|
||||
};
|
||||
|
||||
void JavaDownloader::downloadJava(bool isLegacy, const QString& OS)
|
||||
void JavaDownloader::executeTask()
|
||||
{
|
||||
auto OS = m_OS;
|
||||
auto isLegacy = m_isLegacy;
|
||||
auto netJob = new NetJob(QString("JRE::QueryVersions"), APPLICATION->network());
|
||||
auto response = new QByteArray();
|
||||
setStatus(tr("Querying mojang meta"));
|
||||
netJob->addNetAction(Net::Download::makeByteArray(
|
||||
QUrl("https://piston-meta.mojang.com/v1/products/java-runtime/2ec0cc96c44e5a76b9c8b7c39df7210883d12871/all.json"), response));
|
||||
QObject::connect(netJob, &NetJob::finished, [netJob, response] {
|
||||
netJob->deleteLater();
|
||||
delete response;
|
||||
});
|
||||
QObject::connect(netJob, &NetJob::succeeded, [response, OS, isLegacy] {
|
||||
QObject::connect(netJob, &NetJob::progress, this, &JavaDownloader::progress);
|
||||
QObject::connect(netJob, &NetJob::succeeded, [response, OS, isLegacy, this] {
|
||||
QJsonParseError parse_error{};
|
||||
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
||||
if (parse_error.error != QJsonParseError::NoError) {
|
||||
@ -35,6 +39,7 @@ void JavaDownloader::downloadJava(bool isLegacy, const QString& OS)
|
||||
}
|
||||
auto versionArray = Json::ensureArray(Json::ensureObject(doc.object(), OS), isLegacy ? "jre-legacy" : "java-runtime-gamma");
|
||||
if (!versionArray.empty()) {
|
||||
setStatus(tr("Downloading java from Mojang"));
|
||||
auto url = versionArray[0].toObject()["manifest"].toObject()["url"].toString();
|
||||
auto download = new NetJob(QString("JRE::DownloadJava"), APPLICATION->network());
|
||||
auto files = new QByteArray();
|
||||
@ -45,7 +50,8 @@ void JavaDownloader::downloadJava(bool isLegacy, const QString& OS)
|
||||
download->deleteLater();
|
||||
delete files;
|
||||
});
|
||||
QObject::connect(download, &NetJob::succeeded, [files, isLegacy] {
|
||||
QObject::connect(download, &NetJob::progress, this, &JavaDownloader::progress);
|
||||
QObject::connect(download, &NetJob::succeeded, [files, isLegacy, this] {
|
||||
QJsonParseError parse_error{};
|
||||
QJsonDocument doc = QJsonDocument::fromJson(*files, &parse_error);
|
||||
if (parse_error.error != QJsonParseError::NoError) {
|
||||
@ -89,12 +95,14 @@ void JavaDownloader::downloadJava(bool isLegacy, const QString& OS)
|
||||
elementDownload->addNetAction(dl);
|
||||
}
|
||||
QObject::connect(elementDownload, &NetJob::finished, [elementDownload] { elementDownload->deleteLater(); });
|
||||
QObject::connect(elementDownload, &NetJob::succeeded, [this]{emitSucceeded();});
|
||||
elementDownload->start();
|
||||
});
|
||||
download->start();
|
||||
} else {
|
||||
// mojang does not have a JRE for us, let's get azul zulu
|
||||
QString javaVersion = isLegacy ? QString("8.0") : QString("18.0");
|
||||
setStatus(tr("Querying Azul meta"));
|
||||
QString javaVersion = isLegacy ? QString("8.0") : QString("17.0");
|
||||
QString azulOS;
|
||||
QString arch;
|
||||
QString bitness;
|
||||
@ -117,12 +125,13 @@ void JavaDownloader::downloadJava(bool isLegacy, const QString& OS)
|
||||
}
|
||||
auto metaResponse = new QByteArray();
|
||||
auto downloadJob = new NetJob(QString("JRE::QueryAzulMeta"), APPLICATION->network());
|
||||
downloadJob->addNetAction(Net::Download::makeByteArray(QString("https://api.azul.com/zulu/download/community/v1.0/bundles/?"
|
||||
downloadJob->addNetAction(
|
||||
Net::Download::makeByteArray(QString("https://api.azul.com/zulu/download/community/v1.0/bundles/?"
|
||||
"java_version=%1"
|
||||
"&os=%2"
|
||||
"&arch=%3"
|
||||
"&hw_bitness=%4"
|
||||
"&ext=zip" // as a zip for all os, even linux
|
||||
"&ext=zip" // as a zip for all os, even linux NOTE !! Linux ARM is .deb only !!
|
||||
"&bundle_type=jre" // jre only
|
||||
"&latest=true" // only get the one latest entry
|
||||
)
|
||||
@ -132,8 +141,8 @@ void JavaDownloader::downloadJava(bool isLegacy, const QString& OS)
|
||||
downloadJob->deleteLater();
|
||||
delete metaResponse;
|
||||
});
|
||||
|
||||
QObject::connect(downloadJob, &NetJob::succeeded, [metaResponse, isLegacy] {
|
||||
QObject::connect(downloadJob, &NetJob::progress, this, &JavaDownloader::progress);
|
||||
QObject::connect(downloadJob, &NetJob::succeeded, [metaResponse, isLegacy, this] {
|
||||
QJsonParseError parse_error{};
|
||||
QJsonDocument doc = QJsonDocument::fromJson(*metaResponse, &parse_error);
|
||||
if (parse_error.error != QJsonParseError::NoError) {
|
||||
@ -144,6 +153,7 @@ void JavaDownloader::downloadJava(bool isLegacy, const QString& OS)
|
||||
auto array = doc.array();
|
||||
if (!array.empty()) {
|
||||
// JRE found ! download the zip
|
||||
setStatus(tr("Downloading java from Azul"));
|
||||
auto downloadURL = QUrl(array[0].toObject()["url"].toString());
|
||||
auto download = new NetJob(QString("JRE::DownloadJava"), APPLICATION->network());
|
||||
const QString path = downloadURL.host() + '/' + downloadURL.path();
|
||||
@ -152,15 +162,18 @@ void JavaDownloader::downloadJava(bool isLegacy, const QString& OS)
|
||||
download->addNetAction(Net::Download::makeCached(downloadURL, entry));
|
||||
auto zippath = entry->getFullPath();
|
||||
QObject::connect(download, &NetJob::finished, [download] { download->deleteLater(); });
|
||||
QObject::connect(download, &NetJob::succeeded, [isLegacy, zippath, downloadURL] {
|
||||
QObject::connect(download, &NetJob::progress, this, &JavaDownloader::progress);
|
||||
QObject::connect(download, &NetJob::succeeded, [isLegacy, zippath, downloadURL, this] {
|
||||
setStatus(tr("Extracting java"));
|
||||
auto output = FS::PathCombine(FS::PathCombine(QCoreApplication::applicationDirPath(), "java"),
|
||||
isLegacy ? "java-legacy" : "java-current");
|
||||
// This should do all of the extracting and creating folders
|
||||
MMCZip::extractDir(zippath, downloadURL.fileName().chopped(4), output);
|
||||
emitSucceeded();
|
||||
});
|
||||
download->start();
|
||||
} else {
|
||||
qWarning() << "No suitable JRE found !!";
|
||||
emitFailed(tr("No suitable JRE found"));
|
||||
}
|
||||
});
|
||||
downloadJob->start();
|
||||
|
@ -1,8 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include "tasks/Task.h"
|
||||
|
||||
namespace JavaDownloader {
|
||||
/*Downloads the java to the runtimes folder*/
|
||||
void downloadJava(bool isLegacy, const QString& OS);
|
||||
} // namespace JavaDownloader
|
||||
class JavaDownloader : public Task {
|
||||
Q_OBJECT
|
||||
public:
|
||||
/*Downloads the java to the runtimes folder*/
|
||||
explicit JavaDownloader(bool isLegacy, const QString& OS) : m_isLegacy(isLegacy), m_OS(OS) {}
|
||||
|
||||
void executeTask() override;
|
||||
|
||||
private:
|
||||
bool m_isLegacy;
|
||||
const QString& m_OS;
|
||||
};
|
||||
|
@ -48,13 +48,14 @@
|
||||
#include "java/JavaUtils.h"
|
||||
#include "java/JavaInstallList.h"
|
||||
|
||||
#include "settings/SettingsObject.h"
|
||||
#include <FileSystem.h>
|
||||
#include "Application.h"
|
||||
#include <sys.h>
|
||||
#include "SysInfo.h"
|
||||
#include "Application.h"
|
||||
#include "JavaDownloader.h"
|
||||
|
||||
#include "SysInfo.h"
|
||||
#include "settings/SettingsObject.h"
|
||||
#include "ui/dialogs/ProgressDialog.h"
|
||||
#include <QDialogButtonBox>
|
||||
|
||||
JavaPage::JavaPage(QWidget *parent) : QWidget(parent), ui(new Ui::JavaPage)
|
||||
{
|
||||
@ -214,8 +215,23 @@ void JavaPage::on_javaDownloadBtn_clicked(){
|
||||
QMessageBox::warning(this, tr("Unknown OS"), tr("The OS you are running is not supported by Mojang or Azul. Please install Java manually."));
|
||||
return;
|
||||
}
|
||||
//TODO display a selection for java 8 or 18
|
||||
JavaDownloader::downloadJava(false, version);
|
||||
//Selection using QMessageBox for java 8 or 17
|
||||
QMessageBox box(QMessageBox::Icon::Question, tr("Java version"), tr("Do you want to download Java version 8 or 17?\n Java 8 is recommended for minecraft versions below 1.17\n Java 17 is recommended for minecraft versions above or equal to 1.17"),
|
||||
QMessageBox::NoButton, this);
|
||||
box.addButton("Java 17", QMessageBox::YesRole);
|
||||
auto no = box.addButton("Java 8", QMessageBox::NoRole);
|
||||
auto cancel = box.addButton(tr("Download both"), QMessageBox::AcceptRole);
|
||||
box.exec();
|
||||
bool isLegacy = box.clickedButton() == no;
|
||||
|
||||
auto down = new JavaDownloader(isLegacy, version);
|
||||
ProgressDialog dialog(this);
|
||||
dialog.execWithTask(down);
|
||||
if(box.clickedButton() == cancel) {
|
||||
auto dwn = new JavaDownloader(false, version);
|
||||
ProgressDialog dg(this);
|
||||
dg.execWithTask(dwn);
|
||||
}
|
||||
}
|
||||
|
||||
void JavaPage::checkerFinished()
|
||||
|
Loading…
x
Reference in New Issue
Block a user