summaryrefslogtreecommitdiff
path: root/meshmc/launcher/ui/dialogs/JavaDownloadDialog.cpp
blob: c83542543c19880c8ccf62af1fd0d47776d5ff75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/* SPDX-FileCopyrightText: 2026 Project Tick
 * SPDX-FileContributor: Project Tick
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 *   MeshMC - A Custom Launcher for Minecraft
 *   Copyright (C) 2026 Project Tick
 *
 *   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, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   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/>.
 */

#include "JavaDownloadDialog.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QMessageBox>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>
#include <QDir>
#include <QHeaderView>
#include <QSplitter>

#include "Application.h"
#include "BuildConfig.h"
#include "FileSystem.h"
#include "Json.h"
#include "java/JavaUtils.h"
#include "net/Download.h"

JavaDownloadDialog::JavaDownloadDialog(QWidget* parent) : QDialog(parent)
{
	setWindowTitle(tr("Download Java"));
	setMinimumSize(700, 450);
	resize(800, 500);
	m_providers = JavaDownload::JavaProviderInfo::availableProviders();
	setupUi();
}

void JavaDownloadDialog::setupUi()
{
	auto* mainLayout = new QVBoxLayout(this);

	// --- Three-column selection area ---
	auto* columnsLayout = new QHBoxLayout();

	// Left: Provider list
	auto* providerGroup = new QGroupBox(tr("Provider"), this);
	auto* providerLayout = new QVBoxLayout(providerGroup);
	m_providerList = new QListWidget(this);
	m_providerList->setIconSize(QSize(24, 24));
	for (const auto& provider : m_providers) {
		auto* item = new QListWidgetItem(
			APPLICATION->getThemedIcon(provider.iconName), provider.name);
		m_providerList->addItem(item);
	}
	providerLayout->addWidget(m_providerList);
	columnsLayout->addWidget(providerGroup, 1);

	// Center: Major version list
	auto* versionGroup = new QGroupBox(tr("Version"), this);
	auto* versionLayout = new QVBoxLayout(versionGroup);
	m_versionList = new QListWidget(this);
	versionLayout->addWidget(m_versionList);
	columnsLayout->addWidget(versionGroup, 1);

	// Right: Sub-version / build list
	auto* subVersionGroup = new QGroupBox(tr("Build"), this);
	auto* subVersionLayout = new QVBoxLayout(subVersionGroup);
	m_subVersionList = new QListWidget(this);
	subVersionLayout->addWidget(m_subVersionList);
	columnsLayout->addWidget(subVersionGroup, 1);

	mainLayout->addLayout(columnsLayout, 1);

	// Info label
	m_infoLabel = new QLabel(this);
	m_infoLabel->setWordWrap(true);
	m_infoLabel->setText(
		tr("Select a Java provider, version, and build to download."));
	mainLayout->addWidget(m_infoLabel);

	// Progress bar
	m_progressBar = new QProgressBar(this);
	m_progressBar->setVisible(false);
	m_progressBar->setRange(0, 100);
	mainLayout->addWidget(m_progressBar);

	// Status label
	m_statusLabel = new QLabel(this);
	m_statusLabel->setVisible(false);
	mainLayout->addWidget(m_statusLabel);

	// Buttons
	auto* buttonLayout = new QHBoxLayout();
	buttonLayout->addStretch();

	m_downloadBtn = new QPushButton(tr("Download"), this);
	m_downloadBtn->setEnabled(false);
	buttonLayout->addWidget(m_downloadBtn);

	m_cancelBtn = new QPushButton(tr("Cancel"), this);
	buttonLayout->addWidget(m_cancelBtn);

	mainLayout->addLayout(buttonLayout);

	// Connections
	connect(m_providerList, &QListWidget::currentRowChanged, this,
			&JavaDownloadDialog::providerChanged);
	connect(m_versionList, &QListWidget::currentRowChanged, this,
			&JavaDownloadDialog::majorVersionChanged);
	connect(m_subVersionList, &QListWidget::currentRowChanged, this,
			&JavaDownloadDialog::subVersionChanged);
	connect(m_downloadBtn, &QPushButton::clicked, this,
			&JavaDownloadDialog::onDownloadClicked);
	connect(m_cancelBtn, &QPushButton::clicked, this,
			&JavaDownloadDialog::onCancelClicked);

	// Select first provider
	if (m_providerList->count() > 0) {
		m_providerList->setCurrentRow(0);
	}
}

void JavaDownloadDialog::providerChanged(int index)
{
	if (index < 0 || index >= m_providers.size())
		return;

	m_versionList->clear();
	m_subVersionList->clear();
	m_downloadBtn->setEnabled(false);
	m_versions.clear();
	m_runtimes.clear();

	const auto& provider = m_providers[index];
	m_infoLabel->setText(tr("Loading versions for %1...").arg(provider.name));

	fetchVersionList(provider.uid);
}

void JavaDownloadDialog::majorVersionChanged(int index)
{
	if (index < 0 || index >= m_versions.size())
		return;

	m_subVersionList->clear();
	m_downloadBtn->setEnabled(false);
	m_runtimes.clear();

	const auto& version = m_versions[index];
	m_infoLabel->setText(tr("Loading builds for %1...").arg(version.versionId));

	fetchRuntimes(version.uid, version.versionId);
}

void JavaDownloadDialog::subVersionChanged(int index)
{
	if (index < 0 || index >= m_runtimes.size()) {
		m_downloadBtn->setEnabled(false);
		return;
	}

	const auto& rt = m_runtimes[index];
	m_infoLabel->setText(tr("Ready to download: %1\n"
							"Version: %2\n"
							"Platform: %3\n"
							"Checksum: %4")
							 .arg(rt.name, rt.version.toString(), rt.runtimeOS,
								  rt.checksumHash.isEmpty()
									  ? tr("None")
									  : tr("Yes (%1)").arg(rt.checksumType)));
	m_downloadBtn->setEnabled(true);
}

void JavaDownloadDialog::fetchVersionList(const QString& uid)
{
	m_fetchJob.reset();
	m_fetchData.clear();

	QString url = QString("%1%2/index.json").arg(BuildConfig.META_URL, uid);
	m_fetchJob = new NetJob(tr("Fetch Java versions"), APPLICATION->network());
	auto dl = Net::Download::makeByteArray(QUrl(url), &m_fetchData);
	m_fetchJob->addNetAction(dl);

	connect(m_fetchJob.get(), &NetJob::succeeded, this, [this, uid]() {
		m_fetchJob.reset();

		QJsonDocument doc;
		try {
			doc = Json::requireDocument(m_fetchData);
		} catch (const Exception& e) {
			m_infoLabel->setText(
				tr("Failed to parse version list: %1").arg(e.cause()));
			return;
		}
		if (!doc.isObject()) {
			m_infoLabel->setText(tr("Failed to parse version list."));
			return;
		}

		m_versions = JavaDownload::parseVersionIndex(doc.object(), uid);
		m_versionList->clear();

		for (const auto& ver : m_versions) {
			QString displayName = ver.versionId;
			if (displayName.startsWith("java")) {
				displayName = "Java " + displayName.mid(4);
			}
			m_versionList->addItem(displayName);
		}

		if (m_versions.size() > 0) {
			m_infoLabel->setText(tr("Select a version."));
		} else {
			m_infoLabel->setText(
				tr("No versions available for this provider."));
		}
	});

	connect(m_fetchJob.get(), &NetJob::failed, this, [this](QString reason) {
		m_fetchJob.reset();
		m_infoLabel->setText(tr("Failed to load versions: %1").arg(reason));
	});

	m_fetchJob->start();
}

void JavaDownloadDialog::fetchRuntimes(const QString& uid,
									   const QString& versionId)
{
	m_fetchJob.reset();
	m_fetchData.clear();

	QString url =
		QString("%1%2/%3.json").arg(BuildConfig.META_URL, uid, versionId);
	m_fetchJob =
		new NetJob(tr("Fetch Java runtime details"), APPLICATION->network());
	auto dl = Net::Download::makeByteArray(QUrl(url), &m_fetchData);
	m_fetchJob->addNetAction(dl);

	connect(m_fetchJob.get(), &NetJob::succeeded, this, [this]() {
		m_fetchJob.reset();

		QJsonDocument doc;
		try {
			doc = Json::requireDocument(m_fetchData);
		} catch (const Exception& e) {
			m_infoLabel->setText(
				tr("Failed to parse runtime details: %1").arg(e.cause()));
			return;
		}
		if (!doc.isObject()) {
			m_infoLabel->setText(tr("Failed to parse runtime details."));
			return;
		}

		auto allRuntimes = JavaDownload::parseRuntimes(doc.object());
		QString myOS = JavaDownload::currentRuntimeOS();

		m_runtimes.clear();
		m_subVersionList->clear();
		for (const auto& rt : allRuntimes) {
			if (rt.runtimeOS == myOS) {
				m_runtimes.append(rt);
				m_subVersionList->addItem(rt.version.toString());
			}
		}

		if (m_runtimes.isEmpty()) {
			m_infoLabel->setText(
				tr("No builds available for your platform (%1).").arg(myOS));
			m_downloadBtn->setEnabled(false);
		} else {
			m_infoLabel->setText(tr("Select a build to download."));
			m_subVersionList->setCurrentRow(0);
		}
	});

	connect(m_fetchJob.get(), &NetJob::failed, this, [this](QString reason) {
		m_fetchJob.reset();
		m_infoLabel->setText(
			tr("Failed to load runtime details: %1").arg(reason));
	});

	m_fetchJob->start();
}

QString JavaDownloadDialog::javaInstallDir() const
{
	return JavaUtils::managedJavaRoot();
}

void JavaDownloadDialog::onDownloadClicked()
{
	int idx = m_subVersionList->currentRow();
	if (idx < 0 || idx >= m_runtimes.size())
		return;

	const auto& runtime = m_runtimes[idx];

	// Build target directory path: {dataPath}/java/{vendor}/{name}-{version}/
	QString dirName =
		QString("%1-%2").arg(runtime.name, runtime.version.toString());
	QString targetDir =
		FS::PathCombine(javaInstallDir(), runtime.vendor, dirName);

	// Check if already installed
	if (QDir(targetDir).exists()) {
		auto result = QMessageBox::question(
			this, tr("Already Installed"),
			tr("This Java version appears to be already installed at:\n%1\n\n"
			   "Do you want to reinstall it?")
				.arg(targetDir),
			QMessageBox::Yes | QMessageBox::No);
		if (result != QMessageBox::Yes) {
			return;
		}
		// Remove existing installation
		QDir(targetDir).removeRecursively();
	}

	m_downloadBtn->setEnabled(false);
	m_providerList->setEnabled(false);
	m_versionList->setEnabled(false);
	m_subVersionList->setEnabled(false);
	m_progressBar->setVisible(true);
	m_progressBar->setValue(0);
	m_statusLabel->setVisible(true);

	m_downloadTask =
		std::make_unique<JavaDownloadTask>(runtime, targetDir, this);

	connect(m_downloadTask.get(), &Task::progress, this,
			[this](qint64 current, qint64 total) {
				if (total > 0) {
					m_progressBar->setValue(
						static_cast<int>(current * 100 / total));
				}
			});

	connect(m_downloadTask.get(), &Task::status, this,
			[this](const QString& status) { m_statusLabel->setText(status); });

	connect(m_downloadTask.get(), &Task::succeeded, this, [this]() {
		m_installedJavaPath = m_downloadTask->installedJavaPath();
		m_progressBar->setValue(100);
		m_statusLabel->setText(tr("Java installed successfully!"));

		QMessageBox::information(
			this, tr("Download Complete"),
			tr("Java has been downloaded and installed successfully.\n\n"
			   "Java binary: %1")
				.arg(m_installedJavaPath));

		accept();
	});

	connect(m_downloadTask.get(), &Task::failed, this,
			[this](const QString& reason) {
				m_progressBar->setVisible(false);
				m_statusLabel->setText(tr("Download failed: %1").arg(reason));
				m_downloadBtn->setEnabled(true);
				m_providerList->setEnabled(true);
				m_versionList->setEnabled(true);
				m_subVersionList->setEnabled(true);

				QMessageBox::warning(
					this, tr("Download Failed"),
					tr("Failed to download Java:\n%1").arg(reason));
			});

	m_downloadTask->start();
}

void JavaDownloadDialog::onCancelClicked()
{
	if (m_fetchJob) {
		m_fetchJob->abort();
		m_fetchJob.reset();
	}
	if (m_downloadTask) {
		m_downloadTask->abort();
		m_downloadTask.reset();
	}
	reject();
}