2022-06-02 01:33:51 +05:30
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright ( C ) 2022 Sefa Eyeoglu < contact @ scrumplex . net >
*
* 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/>.
*/
2022-01-14 14:13:42 +05:30
# include "ModDownloadDialog.h"
# include <BaseVersion.h>
# include <icons/IconList.h>
# include <InstanceList.h>
2022-05-30 00:48:34 +05:30
# include "Application.h"
2022-01-14 14:13:42 +05:30
# include "ProgressDialog.h"
2022-03-13 20:20:18 +05:30
# include "ReviewMessageBox.h"
2022-01-14 14:13:42 +05:30
# include <QLayout>
# include <QPushButton>
# include <QValidator>
# include <QDialogButtonBox>
# include "ui/widgets/PageContainer.h"
2022-05-14 23:16:52 +05:30
# include "ui/pages/modplatform/modrinth/ModrinthModPage.h"
2022-01-14 14:13:42 +05:30
# include "ModDownloadTask.h"
2022-01-15 00:52:15 +05:30
ModDownloadDialog : : ModDownloadDialog ( const std : : shared_ptr < ModFolderModel > & mods , QWidget * parent ,
BaseInstance * instance )
: QDialog ( parent ) , mods ( mods ) , m_instance ( instance )
2022-01-14 14:13:42 +05:30
{
setObjectName ( QStringLiteral ( " ModDownloadDialog " ) ) ;
2022-04-15 16:57:40 +05:30
resize ( std : : max ( 0.5 * parent - > width ( ) , 400.0 ) , std : : max ( 0.75 * parent - > height ( ) , 400.0 ) ) ;
2022-01-14 14:13:42 +05:30
m_verticalLayout = new QVBoxLayout ( this ) ;
m_verticalLayout - > setObjectName ( QStringLiteral ( " verticalLayout " ) ) ;
setWindowIcon ( APPLICATION - > getThemedIcon ( " new " ) ) ;
// NOTE: m_buttons must be initialized before PageContainer, because it indirectly accesses m_buttons through setSuggestedPack! Do not move this below.
m_buttons = new QDialogButtonBox ( QDialogButtonBox : : Help | QDialogButtonBox : : Ok | QDialogButtonBox : : Cancel ) ;
m_container = new PageContainer ( this ) ;
m_container - > setSizePolicy ( QSizePolicy : : Policy : : Preferred , QSizePolicy : : Policy : : Expanding ) ;
m_container - > layout ( ) - > setContentsMargins ( 0 , 0 , 0 , 0 ) ;
m_verticalLayout - > addWidget ( m_container ) ;
m_container - > addButtons ( m_buttons ) ;
// Bonk Qt over its stupid head and make sure it understands which button is the default one...
// See: https://stackoverflow.com/questions/24556831/qbuttonbox-set-default-button
auto OkButton = m_buttons - > button ( QDialogButtonBox : : Ok ) ;
2022-02-24 03:47:33 +05:30
OkButton - > setEnabled ( false ) ;
2022-01-14 14:13:42 +05:30
OkButton - > setDefault ( true ) ;
OkButton - > setAutoDefault ( true ) ;
2022-02-22 07:30:50 +05:30
connect ( OkButton , & QPushButton : : clicked , this , & ModDownloadDialog : : confirm ) ;
2022-01-14 14:13:42 +05:30
auto CancelButton = m_buttons - > button ( QDialogButtonBox : : Cancel ) ;
CancelButton - > setDefault ( false ) ;
CancelButton - > setAutoDefault ( false ) ;
connect ( CancelButton , & QPushButton : : clicked , this , & ModDownloadDialog : : reject ) ;
auto HelpButton = m_buttons - > button ( QDialogButtonBox : : Help ) ;
HelpButton - > setDefault ( false ) ;
HelpButton - > setAutoDefault ( false ) ;
connect ( HelpButton , & QPushButton : : clicked , m_container , & PageContainer : : help ) ;
2022-02-22 06:04:06 +05:30
2022-01-14 14:13:42 +05:30
QMetaObject : : connectSlotsByName ( this ) ;
setWindowModality ( Qt : : WindowModal ) ;
2022-06-17 02:55:14 +05:30
setWindowTitle ( dialogTitle ( ) ) ;
2022-01-14 14:13:42 +05:30
}
QString ModDownloadDialog : : dialogTitle ( )
{
return tr ( " Download mods " ) ;
}
void ModDownloadDialog : : reject ( )
{
QDialog : : reject ( ) ;
}
2022-02-22 07:30:50 +05:30
void ModDownloadDialog : : confirm ( )
{
2022-02-22 07:55:33 +05:30
auto keys = modTask . keys ( ) ;
keys . sort ( Qt : : CaseInsensitive ) ;
2022-04-22 21:50:31 +05:30
auto confirm_dialog = ReviewMessageBox : : create ( this , tr ( " Confirm mods to download " ) ) ;
2022-02-22 07:30:50 +05:30
2022-04-22 21:50:31 +05:30
for ( auto & task : keys ) {
confirm_dialog - > appendMod ( { task , modTask . find ( task ) . value ( ) - > getFilename ( ) } ) ;
2022-03-13 20:20:18 +05:30
}
2022-04-22 21:50:31 +05:30
if ( confirm_dialog - > exec ( ) ) {
auto deselected = confirm_dialog - > deselectedMods ( ) ;
for ( auto name : deselected ) {
modTask . remove ( name ) ;
}
2022-02-22 07:30:50 +05:30
2022-04-22 21:50:31 +05:30
this - > accept ( ) ;
}
2022-02-22 07:30:50 +05:30
}
2022-01-14 14:13:42 +05:30
void ModDownloadDialog : : accept ( )
{
QDialog : : accept ( ) ;
}
QList < BasePage * > ModDownloadDialog : : getPages ( )
{
2022-05-30 00:48:34 +05:30
QList < BasePage * > pages ;
pages . append ( new ModrinthModPage ( this , m_instance ) ) ;
if ( APPLICATION - > currentCapabilities ( ) & Application : : SupportsFlame )
pages . append ( new FlameModPage ( this , m_instance ) ) ;
return pages ;
2022-01-14 14:13:42 +05:30
}
2022-02-22 06:04:06 +05:30
void ModDownloadDialog : : addSelectedMod ( const QString & name , ModDownloadTask * task )
{
2022-02-24 03:47:33 +05:30
removeSelectedMod ( name ) ;
modTask . insert ( name , task ) ;
2022-02-22 06:04:06 +05:30
m_buttons - > button ( QDialogButtonBox : : Ok ) - > setEnabled ( ! modTask . isEmpty ( ) ) ;
}
void ModDownloadDialog : : removeSelectedMod ( const QString & name )
{
if ( modTask . contains ( name ) )
delete modTask . find ( name ) . value ( ) ;
modTask . remove ( name ) ;
2022-02-24 03:47:33 +05:30
m_buttons - > button ( QDialogButtonBox : : Ok ) - > setEnabled ( ! modTask . isEmpty ( ) ) ;
2022-02-22 06:04:06 +05:30
}
bool ModDownloadDialog : : isModSelected ( const QString & name , const QString & filename ) const
2022-01-14 14:13:42 +05:30
{
2022-02-22 06:04:06 +05:30
// FIXME: Is there a way to check for versions without checking the filename
// as a heuristic, other than adding such info to ModDownloadTask itself?
auto iter = modTask . find ( name ) ;
return iter ! = modTask . end ( ) & & ( iter . value ( ) - > getFilename ( ) = = filename ) ;
2022-01-14 14:13:42 +05:30
}
2022-04-29 04:44:03 +05:30
bool ModDownloadDialog : : isModSelected ( const QString & name ) const
{
auto iter = modTask . find ( name ) ;
return iter ! = modTask . end ( ) ;
}
2022-01-14 14:13:42 +05:30
ModDownloadDialog : : ~ ModDownloadDialog ( )
{
}
2022-02-22 06:04:06 +05:30
const QList < ModDownloadTask * > ModDownloadDialog : : getTasks ( ) {
return modTask . values ( ) ;
2022-01-14 14:13:42 +05:30
}