2019-10-18 19:01:51 +00:00
# include <filesystem>
2019-10-31 14:31:15 +00:00
# include <switch.h>
2019-10-18 19:01:51 +00:00
# include "ui/MainApplication.hpp"
# include "ui/mainPage.hpp"
# include "ui/netInstPage.hpp"
2019-10-25 01:33:41 +00:00
# include "util/util.hpp"
2019-11-14 01:01:35 +00:00
# include "util/config.hpp"
# include "util/curl.hpp"
2019-10-18 19:01:51 +00:00
# include "netInstall.hpp"
# define COLOR(hex) pu::ui::Color::FromHex(hex)
namespace inst : : ui {
extern MainApplication * mainApp ;
2019-11-15 16:41:29 +00:00
std : : string lastUrl = " https:// " ;
std : : string lastFileID = " " ;
2019-11-30 17:06:49 +00:00
std : : string sourceString = " " ;
2019-10-18 19:01:51 +00:00
netInstPage : : netInstPage ( ) : Layout : : Layout ( ) {
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-10-27 03:36:45 +00:00
this - > topRect = Rectangle : : New ( 0 , 0 , 1280 , 93 , COLOR ( " #170909FF " ) ) ;
this - > infoRect = Rectangle : : New ( 0 , 93 , 1280 , 60 , COLOR ( " #17090980 " ) ) ;
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-10-27 03:36:45 +00:00
this - > pageInfoText = TextBlock : : New ( 10 , 109 , " " , 30 ) ;
2019-10-18 19:01:51 +00:00
this - > pageInfoText - > SetColor ( COLOR ( " #FFFFFFFF " ) ) ;
2019-10-29 01:00:50 +00:00
this - > butText = TextBlock : : New ( 10 , 678 , " " , 24 ) ;
2019-10-27 03:36:45 +00:00
this - > butText - > SetColor ( COLOR ( " #FFFFFFFF " ) ) ;
2019-11-03 18:36:07 +00:00
this - > menu = pu : : ui : : elm : : Menu : : New ( 0 , 154 , 1280 , COLOR ( " #FFFFFF00 " ) , 84 , ( 506 / 84 ) ) ;
2019-10-23 01:33:13 +00:00
this - > menu - > SetOnFocusColor ( COLOR ( " #00000033 " ) ) ;
2019-10-27 03:36:45 +00:00
this - > menu - > SetScrollbarColor ( COLOR ( " #17090980 " ) ) ;
2019-11-25 23:59:25 +00:00
this - > infoImage = Image : : New ( 453 , 292 , " romfs:/lan-connection-waiting.png " ) ;
2019-10-27 03:36:45 +00:00
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-18 19:01:51 +00:00
this - > Add ( this - > pageInfoText ) ;
2019-10-23 01:33:13 +00:00
this - > Add ( this - > menu ) ;
2019-11-25 02:48:17 +00:00
this - > Add ( this - > infoImage ) ;
2019-10-18 19:01:51 +00:00
}
2019-11-02 00:12:00 +00:00
void netInstPage : : drawMenuItems ( bool clearItems ) {
2019-11-22 17:26:34 +00:00
if ( clearItems ) this - > selectedUrls = { } ;
if ( clearItems ) this - > alternativeNames = { } ;
2019-11-02 00:12:00 +00:00
this - > menu - > ClearItems ( ) ;
2019-11-22 17:26:34 +00:00
for ( auto & url : this - > ourUrls ) {
2019-11-08 17:24:31 +00:00
pu : : String itm = inst : : util : : shortenString ( inst : : util : : formatUrlString ( url ) , 56 , true ) ;
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-22 17:26:34 +00:00
for ( long unsigned int i = 0 ; i < this - > selectedUrls . size ( ) ; i + + ) {
if ( this - > selectedUrls [ i ] = = url ) {
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 netInstPage : : selectTitle ( int selectedIndex ) {
2019-11-08 16:41:04 +00:00
if ( this - > menu - > GetItems ( ) [ selectedIndex ] - > GetIcon ( ) = = " romfs:/check-box-outline.png " ) {
2019-11-22 17:26:34 +00:00
for ( long unsigned int i = 0 ; i < this - > selectedUrls . size ( ) ; i + + ) {
if ( this - > selectedUrls [ i ] = = this - > ourUrls [ selectedIndex ] ) this - > selectedUrls . erase ( this - > selectedUrls . begin ( ) + i ) ;
2019-11-02 00:12:00 +00:00
}
2019-11-22 17:26:34 +00:00
} else this - > selectedUrls . push_back ( this - > ourUrls [ selectedIndex ] ) ;
this - > drawMenuItems ( false ) ;
2019-11-02 00:12:00 +00:00
}
2019-10-23 01:33:13 +00:00
void netInstPage : : startNetwork ( ) {
2019-11-14 01:01:35 +00:00
this - > butText - > SetText ( " \ue0e3 Install Over Internet \ue0e2 Help \ue0e1 Cancel " ) ;
2019-10-23 01:33:13 +00:00
this - > menu - > SetVisible ( false ) ;
this - > menu - > ClearItems ( ) ;
2019-11-25 02:48:17 +00:00
this - > infoImage - > SetVisible ( true ) ;
2019-10-21 01:10:27 +00:00
mainApp - > LoadLayout ( mainApp - > netinstPage ) ;
2019-11-22 17:26:34 +00:00
this - > ourUrls = netInstStuff : : OnSelected ( ) ;
if ( ! this - > ourUrls . size ( ) ) {
2019-10-24 19:53:54 +00:00
mainApp - > LoadLayout ( mainApp - > mainPage ) ;
2019-10-21 01:10:27 +00:00
return ;
2019-11-22 17:26:34 +00:00
} else if ( this - > ourUrls [ 0 ] = = " supplyUrl " ) {
2019-11-14 01:01:35 +00:00
std : : string keyboardResult ;
switch ( mainApp - > CreateShowDialog ( " Where do you want to install from? " , " Press B to cancel " , { " URL " , " Google Drive " } , false ) ) {
case 0 :
2019-11-15 16:41:29 +00:00
keyboardResult = inst : : util : : softwareKeyboard ( " Enter the Internet address of a file " , lastUrl , 500 ) ;
2019-11-14 01:01:35 +00:00
if ( keyboardResult . size ( ) > 0 ) {
2019-11-15 16:41:29 +00:00
lastUrl = keyboardResult ;
2019-11-14 01:01:35 +00:00
if ( inst : : util : : formatUrlString ( keyboardResult ) = = " " | | keyboardResult = = " https:// " | | keyboardResult = = " http:// " ) {
mainApp - > CreateShowDialog ( " The URL specified is invalid! " , " " , { " OK " } , false ) ;
break ;
}
2019-11-30 17:06:49 +00:00
sourceString = " from URL " ;
2019-11-22 17:26:34 +00:00
this - > selectedUrls = { keyboardResult } ;
this - > startInstall ( true ) ;
2019-10-31 16:07:04 +00:00
return ;
}
2019-11-14 01:01:35 +00:00
break ;
case 1 :
2019-11-15 16:41:29 +00:00
keyboardResult = inst : : util : : softwareKeyboard ( " Enter the file ID of a public Google Drive file " , lastFileID , 50 ) ;
2019-11-14 01:01:35 +00:00
if ( keyboardResult . size ( ) > 0 ) {
2019-11-15 16:41:29 +00:00
lastFileID = keyboardResult ;
2019-11-14 01:01:35 +00:00
std : : string fileName = inst : : util : : getDriveFileName ( keyboardResult ) ;
2019-11-22 17:26:34 +00:00
if ( fileName . size ( ) > 0 ) this - > alternativeNames = { fileName } ;
else this - > alternativeNames = { " Google Drive File " } ;
2019-11-30 17:06:49 +00:00
sourceString = " from Google Drive " ;
2019-11-22 17:26:34 +00:00
this - > selectedUrls = { " https://www.googleapis.com/drive/v3/files/ " + keyboardResult + " ?key= " + inst : : config : : gAuthKey + " &alt=media " } ;
this - > startInstall ( true ) ;
2019-11-14 01:01:35 +00:00
return ;
}
break ;
2019-10-26 21:00:55 +00:00
}
2019-11-22 17:26:34 +00:00
this - > startNetwork ( ) ;
2019-11-14 01:01:35 +00:00
return ;
2019-10-23 01:33:13 +00:00
} else {
2019-11-30 17:06:49 +00:00
sourceString = " over local network " ;
2019-11-16 18:45:02 +00:00
this - > pageInfoText - > SetText ( " Select what files you want to install from the server, then press the Plus button! " ) ;
2019-11-16 18:16:47 +00:00
this - > butText - > SetText ( " \ue0e0 Select File \ue0e3 Select All \ue0ef Install File(s) \ue0e1 Cancel " ) ;
2019-11-22 17:26:34 +00:00
this - > drawMenuItems ( true ) ;
2019-10-21 01:10:27 +00:00
}
2019-11-25 02:48:17 +00:00
this - > infoImage - > SetVisible ( false ) ;
2019-10-23 01:33:13 +00:00
this - > menu - > SetVisible ( true ) ;
return ;
}
2019-10-27 03:36:45 +00:00
void netInstPage : : startInstall ( bool urlMode ) {
2019-11-02 00:12:00 +00:00
int dialogResult = - 1 ;
2019-11-22 17:26:34 +00:00
if ( this - > selectedUrls . size ( ) = = 1 ) {
2019-11-14 01:01:35 +00:00
std : : string ourUrlString ;
2019-11-22 17:26:34 +00:00
if ( this - > alternativeNames . size ( ) > 0 ) ourUrlString = inst : : util : : shortenString ( this - > alternativeNames [ 0 ] , 32 , true ) ;
else ourUrlString = inst : : util : : shortenString ( inst : : util : : formatUrlString ( this - > selectedUrls [ 0 ] ) , 32 , true ) ;
2019-11-14 01:01:35 +00:00
dialogResult = mainApp - > CreateShowDialog ( " Where should " + ourUrlString + " be installed to? " , " Press B to cancel " , { " SD Card " , " Internal Storage " } , false ) ;
2019-11-22 17:26:34 +00:00
} else dialogResult = mainApp - > CreateShowDialog ( " Where should the selected " + std : : to_string ( this - > selectedUrls . size ( ) ) + " files be installed to? " , " Press B to cancel " , { " SD Card " , " Internal Storage " } , false ) ;
2019-10-27 03:36:45 +00:00
if ( dialogResult = = - 1 & & ! urlMode ) return ;
else if ( dialogResult = = - 1 & & urlMode ) {
2019-11-22 17:26:34 +00:00
this - > startNetwork ( ) ;
2019-10-27 03:36:45 +00:00
return ;
}
2019-11-30 17:06:49 +00:00
netInstStuff : : installTitleNet ( this - > selectedUrls , dialogResult , this - > alternativeNames , sourceString ) ;
2019-10-21 01:10:27 +00:00
return ;
}
2019-10-18 19:01:51 +00:00
void netInstPage : : onInput ( u64 Down , u64 Up , u64 Held , pu : : ui : : Touch Pos ) {
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-30 16:12:45 +00:00
this - > selectTitle ( this - > menu - > GetSelectedIndex ( ) ) ;
2019-11-22 17:26:34 +00:00
if ( this - > menu - > GetItems ( ) . size ( ) = = 1 & & this - > selectedUrls . size ( ) = = 1 ) {
this - > startInstall ( false ) ;
2019-11-08 16:41:04 +00:00
}
}
if ( ( Down & KEY_Y ) ) {
2019-11-22 17:26:34 +00:00
if ( this - > selectedUrls . size ( ) = = this - > menu - > GetItems ( ) . size ( ) ) this - > drawMenuItems ( true ) ;
2019-11-08 16:41:04 +00:00
else {
for ( long unsigned int i = 0 ; i < this - > menu - > GetItems ( ) . size ( ) ; i + + ) {
if ( this - > menu - > GetItems ( ) [ i ] - > GetIcon ( ) = = " romfs:/check-box-outline.png " ) continue ;
2019-11-30 16:12:45 +00:00
else this - > selectTitle ( i ) ;
2019-11-08 16:41:04 +00:00
}
2019-11-22 17:26:34 +00:00
this - > drawMenuItems ( false ) ;
2019-11-06 22:37:10 +00:00
}
2019-11-02 00:12:00 +00:00
}
if ( Down & KEY_PLUS ) {
2019-11-22 17:26:34 +00:00
if ( this - > selectedUrls . size ( ) = = 0 ) {
2019-11-30 16:12:45 +00:00
this - > selectTitle ( this - > menu - > GetSelectedIndex ( ) ) ;
2019-11-22 17:26:34 +00:00
this - > startInstall ( false ) ;
2019-11-06 22:37:10 +00:00
return ;
}
2019-11-22 17:26:34 +00:00
this - > startInstall ( false ) ;
2019-10-23 01:33:13 +00:00
}
2019-10-18 19:01:51 +00:00
}
}