2022-03-20 20:01:08 +01:00
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright ( c ) 2022 Jamie Mansfield < jmansfield @ cadixdev . org >
2022-03-24 16:00:23 +01:00
* Copyright ( C ) 2022 Sefa Eyeoglu < contact @ scrumplex . net >
2014-07-20 23:47:46 +02:00
*
2022-03-20 20:01:08 +01: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.
2014-07-20 23:47:46 +02:00
*
2022-03-20 20:01:08 +01:00
* 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 .
2014-07-20 23:47:46 +02:00
*
2022-03-20 20:01:08 +01:00
* 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 .
2014-07-20 23:47:46 +02:00
*/
# include "JavaPage.h"
2015-05-04 01:20:48 +02:00
# include "JavaCommon.h"
2014-07-20 23:47:46 +02:00
# include "ui_JavaPage.h"
# include <QFileDialog>
# include <QMessageBox>
# include <QDir>
2018-06-01 21:20:33 +07:00
# include <QTabBar>
2014-07-20 23:47:46 +02:00
2021-11-22 03:55:16 +01:00
# include "ui/dialogs/VersionSelectDialog.h"
2014-07-20 23:47:46 +02:00
2015-02-09 01:51:14 +01:00
# include "java/JavaUtils.h"
2016-01-02 00:35:54 +01:00
# include "java/JavaInstallList.h"
2014-07-20 23:47:46 +02:00
2015-10-05 01:47:27 +02:00
# include <FileSystem.h>
2017-07-11 22:43:35 +02:00
# include <sys.h>
2022-07-10 19:56:52 +02:00
# include "Application.h"
2022-07-01 14:05:14 +02:00
# include "JavaDownloader.h"
2022-07-10 19:56:52 +02:00
# include "SysInfo.h"
# include "settings/SettingsObject.h"
# include "ui/dialogs/ProgressDialog.h"
# include <QDialogButtonBox>
2014-07-20 23:47:46 +02:00
JavaPage : : JavaPage ( QWidget * parent ) : QWidget ( parent ) , ui ( new Ui : : JavaPage )
{
2018-07-15 14:51:05 +02:00
ui - > setupUi ( this ) ;
ui - > tabWidget - > tabBar ( ) - > hide ( ) ;
2015-02-01 18:54:58 +01:00
2021-06-23 09:55:09 +01:00
auto sysMiB = Sys : : getSystemRam ( ) / Sys : : mebibyte ;
ui - > maxMemSpinBox - > setMaximum ( sysMiB ) ;
2018-07-15 14:51:05 +02:00
loadSettings ( ) ;
2014-07-20 23:47:46 +02:00
}
JavaPage : : ~ JavaPage ( )
{
2018-07-15 14:51:05 +02:00
delete ui ;
2014-07-20 23:47:46 +02:00
}
bool JavaPage : : apply ( )
{
2018-07-15 14:51:05 +02:00
applySettings ( ) ;
return true ;
2014-07-20 23:47:46 +02:00
}
void JavaPage : : applySettings ( )
{
2021-11-20 16:22:22 +01:00
auto s = APPLICATION - > settings ( ) ;
2018-07-15 14:51:05 +02:00
// Memory
int min = ui - > minMemSpinBox - > value ( ) ;
int max = ui - > maxMemSpinBox - > value ( ) ;
if ( min < max )
{
s - > set ( " MinMemAlloc " , min ) ;
s - > set ( " MaxMemAlloc " , max ) ;
}
else
{
s - > set ( " MinMemAlloc " , max ) ;
s - > set ( " MaxMemAlloc " , min ) ;
}
s - > set ( " PermGen " , ui - > permGenSpinBox - > value ( ) ) ;
// Java Settings
s - > set ( " JavaPath " , ui - > javaPathTextBox - > text ( ) ) ;
2022-05-29 12:15:20 +08:00
s - > set ( " JvmArgs " , ui - > jvmArgsTextBox - > toPlainText ( ) . replace ( " \n " , " " ) ) ;
2022-03-24 14:47:22 +01:00
s - > set ( " IgnoreJavaCompatibility " , ui - > skipCompatibilityCheckbox - > isChecked ( ) ) ;
2022-04-30 15:19:57 +02:00
s - > set ( " IgnoreJavaWizard " , ui - > skipJavaWizardCheckbox - > isChecked ( ) ) ;
2018-07-15 14:51:05 +02:00
JavaCommon : : checkJVMArgs ( s - > get ( " JvmArgs " ) . toString ( ) , this - > parentWidget ( ) ) ;
2014-07-20 23:47:46 +02:00
}
void JavaPage : : loadSettings ( )
{
2021-11-20 16:22:22 +01:00
auto s = APPLICATION - > settings ( ) ;
2018-07-15 14:51:05 +02:00
// Memory
int min = s - > get ( " MinMemAlloc " ) . toInt ( ) ;
int max = s - > get ( " MaxMemAlloc " ) . toInt ( ) ;
if ( min < max )
{
ui - > minMemSpinBox - > setValue ( min ) ;
ui - > maxMemSpinBox - > setValue ( max ) ;
}
else
{
ui - > minMemSpinBox - > setValue ( max ) ;
ui - > maxMemSpinBox - > setValue ( min ) ;
}
ui - > permGenSpinBox - > setValue ( s - > get ( " PermGen " ) . toInt ( ) ) ;
// Java Settings
ui - > javaPathTextBox - > setText ( s - > get ( " JavaPath " ) . toString ( ) ) ;
2022-05-23 16:50:17 +08:00
ui - > jvmArgsTextBox - > setPlainText ( s - > get ( " JvmArgs " ) . toString ( ) ) ;
2022-03-24 14:47:22 +01:00
ui - > skipCompatibilityCheckbox - > setChecked ( s - > get ( " IgnoreJavaCompatibility " ) . toBool ( ) ) ;
2022-04-30 15:19:57 +02:00
ui - > skipJavaWizardCheckbox - > setChecked ( s - > get ( " IgnoreJavaWizard " ) . toBool ( ) ) ;
2014-07-20 23:47:46 +02:00
}
void JavaPage : : on_javaDetectBtn_clicked ( )
{
2022-06-14 22:00:24 +02:00
if ( JavaUtils : : getJavaCheckPath ( ) . isEmpty ( ) ) {
JavaCommon : : javaCheckNotFound ( this ) ;
return ;
}
2018-07-15 14:51:05 +02:00
JavaInstallPtr java ;
2014-07-20 23:47:46 +02:00
2021-11-20 16:22:22 +01:00
VersionSelectDialog vselect ( APPLICATION - > javalist ( ) . get ( ) , tr ( " Select a Java version " ) , this , true ) ;
2018-07-15 14:51:05 +02:00
vselect . setResizeOn ( 2 ) ;
vselect . exec ( ) ;
2014-07-20 23:47:46 +02:00
2018-07-15 14:51:05 +02:00
if ( vselect . result ( ) = = QDialog : : Accepted & & vselect . selectedVersion ( ) )
{
java = std : : dynamic_pointer_cast < JavaInstall > ( vselect . selectedVersion ( ) ) ;
ui - > javaPathTextBox - > setText ( java - > path ) ;
}
2014-07-20 23:47:46 +02:00
}
2015-09-30 22:52:55 +02:00
2014-07-20 23:47:46 +02:00
void JavaPage : : on_javaBrowseBtn_clicked ( )
{
2018-07-15 14:51:05 +02:00
QString raw_path = QFileDialog : : getOpenFileName ( this , tr ( " Find Java executable " ) ) ;
// do not allow current dir - it's dirty. Do not allow dirs that don't exist
if ( raw_path . isEmpty ( ) )
{
return ;
}
QString cooked_path = FS : : NormalizePath ( raw_path ) ;
QFileInfo javaInfo ( cooked_path ) ; ;
if ( ! javaInfo . exists ( ) | | ! javaInfo . isExecutable ( ) )
{
return ;
}
ui - > javaPathTextBox - > setText ( cooked_path ) ;
2014-07-20 23:47:46 +02:00
}
2015-05-04 01:20:48 +02:00
2014-07-20 23:47:46 +02:00
void JavaPage : : on_javaTestBtn_clicked ( )
{
2018-07-15 14:51:05 +02:00
if ( checker )
{
return ;
}
checker . reset ( new JavaCommon : : TestCheck (
2022-05-29 20:35:57 +08:00
this , ui - > javaPathTextBox - > text ( ) , ui - > jvmArgsTextBox - > toPlainText ( ) . replace ( " \n " , " " ) ,
2018-07-15 14:51:05 +02:00
ui - > minMemSpinBox - > value ( ) , ui - > maxMemSpinBox - > value ( ) , ui - > permGenSpinBox - > value ( ) ) ) ;
connect ( checker . get ( ) , SIGNAL ( finished ( ) ) , SLOT ( checkerFinished ( ) ) ) ;
checker - > run ( ) ;
2014-07-20 23:47:46 +02:00
}
2022-07-01 14:05:14 +02:00
void JavaPage : : on_javaDownloadBtn_clicked ( ) {
QString sys = SysInfo : : currentSystem ( ) ;
if ( sys = = " osx " ) {
sys = " mac-os " ;
}
QString arch = SysInfo : : useQTForArch ( ) ;
QString version ;
if ( sys = = " windows " ) {
if ( arch = = " x86_64 " ) {
version = " windows-x64 " ;
} else if ( arch = = " i386 " ) {
version = " windows-x86 " ;
} else {
//Unknown, maybe arm, appending arch for downloader
version = " windows- " + arch ;
}
} else if ( sys = = " mac-os " ) {
if ( arch = = " arm64 " ) {
version = " mac-os-arm64 " ;
} else {
version = " mac-os " ;
}
} else if ( sys = = " linux " ) {
if ( arch = = " x86_64 " ) {
version = " linux " ;
} else {
// will work for i386, and arm(64)
version = " linux- " + arch ;
}
} else {
// ? ? ? ? ? unknown os, at least it won't have a java version on mojang or azul, display warning
QMessageBox : : warning ( this , tr ( " Unknown OS " ) , tr ( " The OS you are running is not supported by Mojang or Azul. Please install Java manually. " ) ) ;
return ;
}
2022-07-10 19:56:52 +02:00
//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 ) ;
}
2022-07-01 14:05:14 +02:00
}
2015-05-04 01:20:48 +02:00
void JavaPage : : checkerFinished ( )
2014-07-20 23:47:46 +02:00
{
2018-07-15 14:51:05 +02:00
checker . reset ( ) ;
2014-07-20 23:47:46 +02:00
}
2022-02-22 18:23:53 +00:00
void JavaPage : : retranslate ( )
{
ui - > retranslateUi ( this ) ;
}