2019-10-22 22:14:37 +00:00
# include <filesystem>
# include "ui/MainApplication.hpp"
# include "ui/mainPage.hpp"
2019-11-30 16:12:45 +00:00
# include "ui/sdInstPage.hpp"
# include "sdInstall.hpp"
2019-10-25 01:33:41 +00:00
# include "util/util.hpp"
2019-11-14 01:23:30 +00:00
# include "util/config.hpp"
2019-10-22 22:14:37 +00:00
# define COLOR(hex) pu::ui::Color::FromHex(hex)
namespace inst : : ui {
extern MainApplication * mainApp ;
2019-11-30 16:12:45 +00:00
sdInstPage : : sdInstPage ( ) : Layout : : Layout ( ) {
2019-10-22 22:14:37 +00:00
this - > SetBackgroundColor ( COLOR ( " #670000FF " ) ) ;
2019-11-19 18:08:37 +00:00
if ( std : : filesystem : : exists ( inst : : config : : appDir + " /background.png " ) ) this - > SetBackgroundImage ( inst : : config : : appDir + " /background.png " ) ;
else this - > SetBackgroundImage ( " romfs:/background.jpg " ) ;
2019-11-30 20:41:28 +00:00
this - > topRect = Rectangle : : New ( 0 , 0 , 1280 , 94 , COLOR ( " #170909FF " ) ) ;
this - > infoRect = Rectangle : : New ( 0 , 95 , 1280 , 60 , COLOR ( " #17090980 " ) ) ;
2019-10-27 03:36:45 +00:00
this - > botRect = Rectangle : : New ( 0 , 660 , 1280 , 60 , COLOR ( " #17090980 " ) ) ;
this - > titleImage = Image : : New ( 0 , 0 , " romfs:/logo.png " ) ;
2019-11-14 01:23:30 +00:00
this - > appVersionText = TextBlock : : New ( 480 , 49 , " v " + inst : : config : : appVersion , 22 ) ;
this - > appVersionText - > SetColor ( COLOR ( " #FFFFFFFF " ) ) ;
2019-11-16 18:45:02 +00:00
this - > pageInfoText = TextBlock : : New ( 10 , 109 , " Select what files you want to install, then press the Plus button! " , 30 ) ;
2019-10-22 22:14:37 +00:00
this - > pageInfoText - > SetColor ( COLOR ( " #FFFFFFFF " ) ) ;
2019-11-16 18:16:47 +00:00
this - > butText = TextBlock : : New ( 10 , 678 , " \ue0e0 Select File \ue0e3 Select All \ue0ef Install File(s) \ue0e2 Help \ue0e1 Cancel " , 24 ) ;
2019-10-27 03:36:45 +00:00
this - > butText - > SetColor ( COLOR ( " #FFFFFFFF " ) ) ;
2019-11-30 20:41:28 +00:00
this - > menu = pu : : ui : : elm : : Menu : : New ( 0 , 156 , 1280 , COLOR ( " #FFFFFF00 " ) , 84 , ( 506 / 84 ) ) ;
2019-10-22 22:14:37 +00:00
this - > menu - > SetOnFocusColor ( COLOR ( " #00000033 " ) ) ;
2019-10-27 03:36:45 +00:00
this - > menu - > SetScrollbarColor ( COLOR ( " #17090980 " ) ) ;
this - > Add ( this - > topRect ) ;
this - > Add ( this - > infoRect ) ;
this - > Add ( this - > botRect ) ;
this - > Add ( this - > titleImage ) ;
2019-11-14 01:23:30 +00:00
this - > Add ( this - > appVersionText ) ;
2019-10-27 03:36:45 +00:00
this - > Add ( this - > butText ) ;
2019-10-22 22:14:37 +00:00
this - > Add ( this - > pageInfoText ) ;
this - > Add ( this - > menu ) ;
}
2019-11-30 16:12:45 +00:00
void sdInstPage : : drawMenuItems ( bool clearItems , std : : filesystem : : path ourPath ) {
if ( clearItems ) this - > selectedTitles = { } ;
2019-11-22 17:17:12 +00:00
if ( ourPath = = " sdmc: " ) this - > currentDir = std : : filesystem : : path ( ourPath . string ( ) + " / " ) ;
else this - > currentDir = ourPath ;
2019-11-02 00:12:00 +00:00
this - > menu - > ClearItems ( ) ;
2019-11-22 17:17:12 +00:00
try {
this - > ourDirectories = util : : getDirsAtPath ( this - > currentDir ) ;
2019-11-29 08:23:11 +00:00
this - > ourFiles = util : : getDirectoryFiles ( this - > currentDir , { " .nsp " , " .nsz " , " .xci " , " .xcz " } ) ;
2019-11-22 17:17:12 +00:00
} catch ( std : : exception & e ) {
this - > drawMenuItems ( false , this - > currentDir . parent_path ( ) ) ;
return ;
}
if ( this - > currentDir ! = " sdmc:/ " ) {
pu : : String itm = " .. " ;
auto ourEntry = pu : : ui : : elm : : MenuItem : : New ( itm ) ;
ourEntry - > SetColor ( COLOR ( " #FFFFFFFF " ) ) ;
ourEntry - > SetIcon ( " romfs:/folder-upload.png " ) ;
this - > menu - > AddItem ( ourEntry ) ;
}
for ( auto & file : this - > ourDirectories ) {
if ( file = = " .. " ) break ;
pu : : String itm = file . filename ( ) . string ( ) ;
auto ourEntry = pu : : ui : : elm : : MenuItem : : New ( itm ) ;
ourEntry - > SetColor ( COLOR ( " #FFFFFFFF " ) ) ;
ourEntry - > SetIcon ( " romfs:/folder.png " ) ;
this - > menu - > AddItem ( ourEntry ) ;
}
for ( auto & file : this - > ourFiles ) {
pu : : String itm = file . filename ( ) . string ( ) ;
2019-11-02 00:12:00 +00:00
auto ourEntry = pu : : ui : : elm : : MenuItem : : New ( itm ) ;
ourEntry - > SetColor ( COLOR ( " #FFFFFFFF " ) ) ;
ourEntry - > SetIcon ( " romfs:/checkbox-blank-outline.png " ) ;
2019-11-30 16:12:45 +00:00
for ( long unsigned int i = 0 ; i < this - > selectedTitles . size ( ) ; i + + ) {
if ( this - > selectedTitles [ i ] = = file ) {
2019-11-02 00:12:00 +00:00
ourEntry - > SetIcon ( " romfs:/check-box-outline.png " ) ;
}
}
this - > menu - > AddItem ( ourEntry ) ;
}
}
2019-11-30 16:12:45 +00:00
void sdInstPage : : followDirectory ( ) {
2019-11-22 17:17:12 +00:00
int selectedIndex = this - > menu - > GetSelectedIndex ( ) ;
int dirListSize = this - > ourDirectories . size ( ) ;
if ( this - > currentDir ! = " sdmc:/ " ) {
dirListSize + + ;
selectedIndex - - ;
}
if ( selectedIndex < dirListSize ) {
if ( this - > menu - > GetItems ( ) [ this - > menu - > GetSelectedIndex ( ) ] - > GetName ( ) = = " .. " & & this - > menu - > GetSelectedIndex ( ) = = 0 ) {
this - > drawMenuItems ( true , this - > currentDir . parent_path ( ) ) ;
} else {
this - > drawMenuItems ( true , this - > ourDirectories [ selectedIndex ] ) ;
}
this - > menu - > SetSelectedIndex ( 0 ) ;
}
}
2019-11-30 16:12:45 +00:00
void sdInstPage : : selectNsp ( int selectedIndex ) {
2019-11-22 17:17:12 +00:00
int dirListSize = this - > ourDirectories . size ( ) ;
if ( this - > currentDir ! = " sdmc:/ " ) dirListSize + + ;
2019-11-08 16:41:04 +00:00
if ( this - > menu - > GetItems ( ) [ selectedIndex ] - > GetIcon ( ) = = " romfs:/check-box-outline.png " ) {
2019-11-30 16:12:45 +00:00
for ( long unsigned int i = 0 ; i < this - > selectedTitles . size ( ) ; i + + ) {
if ( this - > selectedTitles [ i ] = = this - > ourFiles [ selectedIndex - dirListSize ] ) this - > selectedTitles . erase ( this - > selectedTitles . begin ( ) + i ) ;
2019-11-02 00:12:00 +00:00
}
2019-11-30 16:12:45 +00:00
} else if ( this - > menu - > GetItems ( ) [ selectedIndex ] - > GetIcon ( ) = = " romfs:/checkbox-blank-outline.png " ) this - > selectedTitles . push_back ( this - > ourFiles [ selectedIndex - dirListSize ] ) ;
2019-11-22 17:17:12 +00:00
else {
this - > followDirectory ( ) ;
return ;
}
this - > drawMenuItems ( false , currentDir ) ;
2019-11-02 00:12:00 +00:00
}
2019-11-30 16:12:45 +00:00
void sdInstPage : : startInstall ( ) {
2019-11-02 00:12:00 +00:00
int dialogResult = - 1 ;
2019-11-30 16:12:45 +00:00
if ( this - > selectedTitles . size ( ) = = 1 ) {
dialogResult = mainApp - > CreateShowDialog ( " Where should " + inst : : util : : shortenString ( std : : filesystem : : path ( this - > selectedTitles [ 0 ] ) . filename ( ) . string ( ) , 32 , true ) + " be installed to? " , " Press B to cancel " , { " SD Card " , " Internal Storage " } , false ) ;
} else dialogResult = mainApp - > CreateShowDialog ( " Where should the selected " + std : : to_string ( this - > selectedTitles . size ( ) ) + " files be installed to? " , " Press B to cancel " , { " SD Card " , " Internal Storage " } , false ) ;
2019-10-23 01:33:13 +00:00
if ( dialogResult = = - 1 ) return ;
2019-11-30 16:12:45 +00:00
nspInstStuff : : installNspFromFile ( this - > selectedTitles , dialogResult ) ;
2019-10-22 22:14:37 +00:00
}
2019-11-30 16:12:45 +00:00
void sdInstPage : : onInput ( u64 Down , u64 Up , u64 Held , pu : : ui : : Touch Pos ) {
2019-10-22 22:14:37 +00:00
if ( Down & KEY_B ) {
mainApp - > LoadLayout ( mainApp - > mainPage ) ;
}
2019-10-31 15:42:40 +00:00
if ( ( Down & KEY_A ) | | ( Up & KEY_TOUCH ) ) {
2019-11-22 17:17:12 +00:00
this - > selectNsp ( this - > menu - > GetSelectedIndex ( ) ) ;
2019-11-30 16:12:45 +00:00
if ( this - > ourFiles . size ( ) = = 1 & & this - > selectedTitles . size ( ) = = 1 ) {
2019-11-22 17:17:12 +00:00
this - > startInstall ( ) ;
2019-11-08 16:41:04 +00:00
}
}
if ( ( Down & KEY_Y ) ) {
2019-11-30 16:12:45 +00:00
if ( this - > selectedTitles . size ( ) = = this - > ourFiles . size ( ) ) this - > drawMenuItems ( true , currentDir ) ;
2019-11-08 16:41:04 +00:00
else {
2019-11-28 16:03:09 +00:00
int topDir = 0 ;
if ( this - > currentDir ! = " sdmc:/ " ) topDir + + ;
for ( long unsigned int i = this - > ourDirectories . size ( ) + topDir ; i < this - > menu - > GetItems ( ) . size ( ) ; i + + ) {
2019-11-08 16:41:04 +00:00
if ( this - > menu - > GetItems ( ) [ i ] - > GetIcon ( ) = = " romfs:/check-box-outline.png " ) continue ;
2019-11-22 17:17:12 +00:00
else this - > selectNsp ( i ) ;
2019-11-08 16:41:04 +00:00
}
2019-11-22 17:17:12 +00:00
this - > drawMenuItems ( false , currentDir ) ;
2019-11-06 22:37:10 +00:00
}
2019-11-02 00:12:00 +00:00
}
2019-11-02 01:13:15 +00:00
if ( ( Down & KEY_X ) ) {
2019-11-30 00:09:31 +00:00
inst : : ui : : mainApp - > CreateShowDialog ( " Help " , " Copy your NSP, NSZ, XCI, or XCZ files to your SD card, browse to and \n select the ones you want to install, then press the Plus button. " , { " OK " } , true ) ;
2019-11-02 01:13:15 +00:00
}
2019-11-02 00:12:00 +00:00
if ( Down & KEY_PLUS ) {
2019-11-30 16:12:45 +00:00
if ( this - > selectedTitles . size ( ) = = 0 & & this - > menu - > GetItems ( ) [ this - > menu - > GetSelectedIndex ( ) ] - > GetIcon ( ) = = " romfs:/checkbox-blank-outline.png " ) {
2019-11-22 17:17:12 +00:00
this - > selectNsp ( this - > menu - > GetSelectedIndex ( ) ) ;
2019-11-06 22:37:10 +00:00
}
2019-11-30 16:12:45 +00:00
if ( this - > selectedTitles . size ( ) > 0 ) this - > startInstall ( ) ;
2019-10-22 22:14:37 +00:00
}
}
}