2021-06-05 14:59:38 +00:00
// Copyright (c) 2013- PPSSPP Project.
// 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 2.0 or later versions.
// 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 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
# include "ppsspp_config.h"
# include "android/jni/app-android.h"
# include "Common/Log.h"
# include "Common/UI/UI.h"
# include "Common/UI/View.h"
# include "Common/UI/ViewGroup.h"
# include "Common/StringUtils.h"
# include "Common/System/System.h"
# include "Common/System/NativeApp.h"
# include "Common/System/Display.h"
2021-07-25 13:33:11 +00:00
# include "Common/Data/Text/I18n.h"
2021-07-26 20:11:07 +00:00
# include "Common/Data/Text/Parsers.h"
2021-07-25 13:33:11 +00:00
# include "Common/File/AndroidStorage.h"
# include "Common/File/FileUtil.h"
# include "Common/File/Path.h"
2021-07-24 22:16:30 +00:00
# include "Common/File/DiskFree.h"
2021-06-05 14:59:38 +00:00
2021-07-29 21:03:43 +00:00
# include "Common/Thread/ThreadManager.h"
2021-06-05 14:59:38 +00:00
# include "Core/Util/GameManager.h"
# include "Core/System.h"
2021-07-25 13:33:11 +00:00
# include "Core/Config.h"
2021-06-05 14:59:38 +00:00
# include "UI/MemStickScreen.h"
# include "UI/MainScreen.h"
# include "UI/MiscScreens.h"
2021-07-24 22:16:30 +00:00
static bool FolderSeemsToBeUsed ( Path newMemstickFolder ) {
// Inspect the potential new folder.
2021-08-05 19:53:08 +00:00
if ( File : : Exists ( newMemstickFolder / " PSP/SAVEDATA " ) | | File : : Exists ( newMemstickFolder / " SAVEDATA " ) ) {
// Does seem likely. We could add more criteria like checking for actual savegames or something.
2021-07-24 22:16:30 +00:00
return true ;
} else {
return false ;
}
}
static bool SwitchMemstickFolderTo ( Path newMemstickFolder ) {
Path testWriteFile = newMemstickFolder / " .write_verify_file " ;
// Doesn't already exist, create.
// Should this ever happen?
if ( newMemstickFolder . Type ( ) = = PathType : : NATIVE ) {
if ( ! File : : Exists ( newMemstickFolder ) ) {
File : : CreateFullPath ( newMemstickFolder ) ;
}
if ( ! File : : WriteDataToFile ( true , " 1 " , 1 , testWriteFile ) ) {
2021-07-26 20:15:34 +00:00
return false ;
2021-07-24 22:16:30 +00:00
}
File : : Delete ( testWriteFile ) ;
} else {
// TODO: Do the same but with scoped storage? Not really necessary, right? If it came from a browse
// for folder, we can assume it exists and is writable, barring wacky race conditions like the user
// being connected by USB and deleting it.
}
Path memStickDirFile = g_Config . internalDataDirectory / " memstick_dir.txt " ;
std : : string str = newMemstickFolder . ToString ( ) ;
if ( ! File : : WriteDataToFile ( true , str . c_str ( ) , ( unsigned int ) str . size ( ) , memStickDirFile ) ) {
ERROR_LOG ( SYSTEM , " Failed to write memstick path '%s' to '%s' " , newMemstickFolder . c_str ( ) , memStickDirFile . c_str ( ) ) ;
// Not sure what to do if this file.
}
// Save so the settings, at least, are transferred.
g_Config . memStickDirectory = newMemstickFolder ;
g_Config . SetSearchPath ( GetSysDirectory ( DIRECTORY_SYSTEM ) ) ;
g_Config . UpdateIniLocation ( ) ;
return true ;
}
static std : : string FormatSpaceString ( int64_t space ) {
if ( space > = 0 ) {
2021-07-26 20:11:07 +00:00
char buffer [ 50 ] ;
NiceSizeFormat ( space , buffer , sizeof ( buffer ) ) ;
return buffer ;
2021-07-24 22:16:30 +00:00
} else {
return " N/A " ;
}
}
MemStickScreen : : MemStickScreen ( bool initialSetup )
: initialSetup_ ( initialSetup ) {
2021-07-25 13:33:11 +00:00
}
2021-06-05 14:59:38 +00:00
void MemStickScreen : : CreateViews ( ) {
using namespace UI ;
auto di = GetI18NCategory ( " Dialog " ) ;
auto iz = GetI18NCategory ( " MemStick " ) ;
2021-06-05 21:49:39 +00:00
Margins actionMenuMargins ( 15 , 15 , 15 , 0 ) ;
2021-06-05 14:59:38 +00:00
2021-07-24 22:16:30 +00:00
root_ = new LinearLayout ( ORIENT_HORIZONTAL ) ;
2021-06-05 14:59:38 +00:00
2021-07-25 13:33:11 +00:00
Spacer * spacerColumn = new Spacer ( new LinearLayoutParams ( 20.0 , FILL_PARENT , 0.0f ) ) ;
2021-08-05 19:53:08 +00:00
ScrollView * leftColumnScroll = new ScrollView ( ORIENT_VERTICAL , new LinearLayoutParams ( 1.0 ) ) ;
ViewGroup * leftColumn = new LinearLayoutList ( ORIENT_VERTICAL ) ;
2021-06-05 14:59:38 +00:00
ViewGroup * rightColumnItems = new LinearLayout ( ORIENT_VERTICAL , new LinearLayoutParams ( 300 , FILL_PARENT , actionMenuMargins ) ) ;
2021-07-25 13:33:11 +00:00
root_ - > Add ( spacerColumn ) ;
2021-08-05 19:53:08 +00:00
root_ - > Add ( leftColumnScroll ) ;
2021-07-25 13:33:11 +00:00
root_ - > Add ( rightColumnItems ) ;
2021-08-05 19:53:08 +00:00
leftColumnScroll - > Add ( leftColumn ) ;
2021-07-25 13:33:11 +00:00
if ( initialSetup_ ) {
leftColumn - > Add ( new TextView ( iz - > T ( " Welcome to PPSSPP! " ) , ALIGN_LEFT , false ) ) ;
leftColumn - > Add ( new Spacer ( new LinearLayoutParams ( FILL_PARENT , 12.0f , 0.0f ) ) ) ;
}
2021-08-05 19:53:08 +00:00
if ( System_GetPropertyBool ( SYSPROP_ANDROID_SCOPED_STORAGE ) ) {
leftColumn - > Add ( new TextView ( iz - > T ( " ScopedStorageWarning " , " WARNING: BETA ANDROID SCOPED STORAGE SUPPORT \n MAY EAT YOUR DATA " ) , ALIGN_LEFT , false ) ) ;
}
2021-07-25 13:33:11 +00:00
leftColumn - > Add ( new TextView ( iz - > T ( " MemoryStickDescription " , " Choose PSP data storage (Memory Stick) " ) , ALIGN_LEFT , false ) ) ;
2021-08-04 21:21:28 +00:00
// For legacy Android systems, so you can switch back to the old ways if you move to SD or something.
// TODO: Gonna need a scroll view.
# if PPSSPP_PLATFORM(ANDROID)
if ( ! System_GetPropertyBool ( SYSPROP_ANDROID_SCOPED_STORAGE ) ) {
leftColumn - > Add ( new Choice ( iz - > T ( " Use PSP folder at root of storage " ) ) ) - > OnClick . Handle ( this , & MemStickScreen : : OnUseStorageRoot ) ;
2021-08-05 19:53:08 +00:00
leftColumn - > Add ( new TextView ( iz - > T ( " ChooseFolderDesc " , " * Data will stay even if you uninstall PPSSPP. \n * Data can be shared with PPSSPP Gold \n * Easy USB access " ) , ALIGN_LEFT , false ) ) ;
2021-08-04 21:21:28 +00:00
}
# endif
2021-07-25 13:33:11 +00:00
leftColumn - > Add ( new Choice ( iz - > T ( " Create or Choose a PSP folder " ) ) ) - > OnClick . Handle ( this , & MemStickScreen : : OnBrowse ) ;
leftColumn - > Add ( new TextView ( iz - > T ( " ChooseFolderDesc " , " * Data will stay even if you uninstall PPSSPP. \n * Data can be shared with PPSSPP Gold \n * Easy USB access " ) , ALIGN_LEFT , false ) ) ;
2021-06-05 14:59:38 +00:00
2021-07-25 13:33:11 +00:00
leftColumn - > Add ( new Choice ( iz - > T ( " Use App Private Directory " ) ) ) - > OnClick . Handle ( this , & MemStickScreen : : OnUseInternalStorage ) ;
leftColumn - > Add ( new TextView ( iz - > T ( " InternalStorageDesc " , " * Warning! Data will be deleted if you uninstall PPSSPP! \n * Data cannot be shared with PPSSPP Gold \n * USB access through Android/data/org.ppsspp.ppsspp/files " ) , ALIGN_LEFT , false ) ) ;
2021-06-05 14:59:38 +00:00
2021-07-25 13:33:11 +00:00
leftColumn - > Add ( new Spacer ( new LinearLayoutParams ( FILL_PARENT , 12.0f , 0.0f ) ) ) ;
2021-06-05 14:59:38 +00:00
2021-07-25 13:33:11 +00:00
if ( ! initialSetup_ ) {
2021-08-05 19:53:08 +00:00
rightColumnItems - > Add ( new Choice ( di - > T ( " Back " ) ) ) - > OnClick . Handle < UIScreen > ( this , & UIScreen : : OnBack ) ;
2021-06-05 14:59:38 +00:00
}
2021-07-25 13:33:11 +00:00
INFO_LOG ( SYSTEM , " MemStickScreen: initialSetup=%d " , ( int ) initialSetup_ ) ;
}
2021-06-05 14:59:38 +00:00
2021-07-25 13:33:11 +00:00
UI : : EventReturn MemStickScreen : : OnUseInternalStorage ( UI : : EventParams & params ) {
2021-08-04 21:21:28 +00:00
Path pendingMemStickFolder = Path ( g_extFilesDir ) ;
if ( initialSetup_ ) {
// There's not gonna be any files here in this case since it's a fresh install.
// Let's just accept it and move on. No need to move files either.
if ( SwitchMemstickFolderTo ( pendingMemStickFolder ) ) {
TriggerFinish ( DialogResult : : DR_OK ) ;
} else {
// This can't really happen?? Not worth making an error message.
}
} else {
// Always ask for confirmation when called from the UI. Likely there's already some data.
screenManager ( ) - > push ( new ConfirmMemstickMoveScreen ( pendingMemStickFolder , false ) ) ;
}
return UI : : EVENT_DONE ;
}
UI : : EventReturn MemStickScreen : : OnUseStorageRoot ( UI : : EventParams & params ) {
Path pendingMemStickFolder = Path ( g_externalDir ) ;
2021-07-24 22:16:30 +00:00
if ( initialSetup_ ) {
// There's not gonna be any files here in this case since it's a fresh install.
// Let's just accept it and move on. No need to move files either.
2021-08-04 21:21:28 +00:00
if ( SwitchMemstickFolderTo ( pendingMemStickFolder ) ) {
2021-07-26 20:15:34 +00:00
TriggerFinish ( DialogResult : : DR_OK ) ;
} else {
// This can't really happen?? Not worth making an error message.
}
2021-07-24 22:16:30 +00:00
} else {
// Always ask for confirmation when called from the UI. Likely there's already some data.
2021-08-04 21:21:28 +00:00
screenManager ( ) - > push ( new ConfirmMemstickMoveScreen ( pendingMemStickFolder , false ) ) ;
2021-07-24 22:16:30 +00:00
}
2021-07-25 13:33:11 +00:00
return UI : : EVENT_DONE ;
2021-06-05 14:59:38 +00:00
}
UI : : EventReturn MemStickScreen : : OnBrowse ( UI : : EventParams & params ) {
System_SendMessage ( " browse_folder " , " " ) ;
return UI : : EVENT_DONE ;
}
void MemStickScreen : : sendMessage ( const char * message , const char * value ) {
// Always call the base class method first to handle the most common messages.
UIDialogScreenWithBackground : : sendMessage ( message , value ) ;
if ( screenManager ( ) - > topScreen ( ) = = this ) {
if ( ! strcmp ( message , " browse_folderSelect " ) ) {
std : : string filename ;
filename = value ;
INFO_LOG ( SYSTEM , " Got folder: '%s' " , filename . c_str ( ) ) ;
2021-07-24 22:16:30 +00:00
// Browse finished. Let's pop up the confirmation dialog.
2021-08-04 21:21:28 +00:00
Path pendingMemStickFolder = Path ( filename ) ;
2021-08-05 19:53:08 +00:00
if ( pendingMemStickFolder = = g_Config . memStickDirectory ) {
auto iz = GetI18NCategory ( " MemStick " ) ;
2021-08-05 21:37:35 +00:00
# if PPSSPP_PLATFORM(ANDROID)
2021-08-05 19:53:08 +00:00
SystemToast ( iz - > T ( " That's the folder being used! " ) ) ;
2021-08-05 21:37:35 +00:00
# endif
return ;
2021-08-05 19:53:08 +00:00
}
2021-08-04 21:21:28 +00:00
bool existingFiles = FolderSeemsToBeUsed ( pendingMemStickFolder ) ;
screenManager ( ) - > push ( new ConfirmMemstickMoveScreen ( pendingMemStickFolder , initialSetup_ ) ) ;
2021-06-05 14:59:38 +00:00
}
}
}
2021-07-24 22:16:30 +00:00
void MemStickScreen : : dialogFinished ( const Screen * dialog , DialogResult result ) {
if ( result = = DialogResult : : DR_OK ) {
INFO_LOG ( SYSTEM , " Confirmation screen done - moving on. " ) ;
// There's a screen manager bug if we call TriggerFinish directly.
// Can't be bothered right now, so we pick this up in update().
done_ = true ;
}
// otherwise, we just keep going.
}
void MemStickScreen : : update ( ) {
UIDialogScreenWithBackground : : update ( ) ;
if ( done_ ) {
TriggerFinish ( DialogResult : : DR_OK ) ;
done_ = false ;
}
}
static bool ListFileSuffixesRecursively ( const Path & root , Path folder , std : : vector < std : : string > & dirSuffixes , std : : vector < std : : string > & fileSuffixes ) {
std : : vector < File : : FileInfo > files ;
if ( ! File : : GetFilesInDir ( folder , & files ) ) {
return false ;
}
for ( auto & file : files ) {
if ( file . isDirectory ) {
2021-07-24 22:54:41 +00:00
std : : string dirSuffix ;
if ( root . ComputePathTo ( file . fullName , dirSuffix ) ) {
dirSuffixes . push_back ( dirSuffix ) ;
ListFileSuffixesRecursively ( root , folder / file . name , dirSuffixes , fileSuffixes ) ;
} else {
ERROR_LOG ( SYSTEM , " Failed to compute PathTo from '%s' to '%s' " , root . c_str ( ) , folder . c_str ( ) ) ;
}
2021-07-24 22:16:30 +00:00
} else {
2021-07-24 22:54:41 +00:00
std : : string fileSuffix ;
if ( root . ComputePathTo ( file . fullName , fileSuffix ) ) {
fileSuffixes . push_back ( fileSuffix ) ;
}
2021-07-24 22:16:30 +00:00
}
}
return true ;
}
ConfirmMemstickMoveScreen : : ConfirmMemstickMoveScreen ( Path newMemstickFolder , bool initialSetup )
: newMemstickFolder_ ( newMemstickFolder ) , initialSetup_ ( initialSetup ) {
existingFilesInNewFolder_ = FolderSeemsToBeUsed ( newMemstickFolder ) ;
if ( initialSetup_ ) {
moveData_ = false ;
}
}
2021-07-29 21:03:43 +00:00
ConfirmMemstickMoveScreen : : ~ ConfirmMemstickMoveScreen ( ) {
if ( moveDataTask_ ) {
INFO_LOG ( SYSTEM , " Move Data task still running, blocking on it " ) ;
moveDataTask_ - > BlockUntilReady ( ) ;
delete moveDataTask_ ;
}
}
2021-07-24 22:16:30 +00:00
void ConfirmMemstickMoveScreen : : CreateViews ( ) {
using namespace UI ;
auto di = GetI18NCategory ( " Dialog " ) ;
2021-06-05 14:59:38 +00:00
auto sy = GetI18NCategory ( " System " ) ;
2021-07-24 22:16:30 +00:00
auto iz = GetI18NCategory ( " MemStick " ) ;
2021-06-05 14:59:38 +00:00
2021-07-24 22:16:30 +00:00
root_ = new LinearLayout ( ORIENT_HORIZONTAL ) ;
2021-06-05 14:59:38 +00:00
2021-07-24 22:16:30 +00:00
Path oldMemstickFolder = g_Config . memStickDirectory ;
Spacer * spacerColumn = new Spacer ( new LinearLayoutParams ( 20.0 , FILL_PARENT , 0.0f ) ) ;
ViewGroup * leftColumn = new LinearLayout ( ORIENT_VERTICAL , new LinearLayoutParams ( 1.0 ) ) ;
ViewGroup * rightColumn = new LinearLayout ( ORIENT_VERTICAL , new LinearLayoutParams ( 1.0 ) ) ;
root_ - > Add ( spacerColumn ) ;
root_ - > Add ( leftColumn ) ;
root_ - > Add ( rightColumn ) ;
int64_t freeSpaceNew ;
int64_t freeSpaceOld ;
free_disk_space ( newMemstickFolder_ , freeSpaceNew ) ;
free_disk_space ( oldMemstickFolder , freeSpaceOld ) ;
leftColumn - > Add ( new TextView ( iz - > T ( " New PSP Data Folder " ) , ALIGN_LEFT , false ) ) ;
2021-08-05 19:53:08 +00:00
if ( ! initialSetup_ ) {
leftColumn - > Add ( new TextView ( iz - > T ( " PPSSPP will restart after the change. " ) , ALIGN_LEFT , false ) ) ;
}
2021-07-24 22:16:30 +00:00
leftColumn - > Add ( new TextView ( newMemstickFolder_ . ToVisualString ( ) , ALIGN_LEFT , false ) ) ;
std : : string newFreeSpaceText = std : : string ( iz - > T ( " Free space " ) ) + " : " + FormatSpaceString ( freeSpaceNew ) ;
leftColumn - > Add ( new TextView ( newFreeSpaceText , ALIGN_LEFT , false ) ) ;
if ( existingFilesInNewFolder_ ) {
2021-08-05 19:53:08 +00:00
leftColumn - > Add ( new TextView ( iz - > T ( " Already contains data. " ) , ALIGN_LEFT , false ) ) ;
if ( ! moveData_ ) {
leftColumn - > Add ( new TextView ( iz - > T ( " No data will be changed. " ) , ALIGN_LEFT , false ) ) ;
}
2021-07-24 22:16:30 +00:00
}
if ( ! error_ . empty ( ) ) {
leftColumn - > Add ( new TextView ( error_ , ALIGN_LEFT , false ) ) ;
}
if ( ! oldMemstickFolder . empty ( ) ) {
std : : string oldFreeSpaceText = std : : string ( iz - > T ( " Free space " ) ) + " : " + FormatSpaceString ( freeSpaceOld ) ;
rightColumn - > Add ( new TextView ( iz - > T ( " Old PSP Data Folder " ) , ALIGN_LEFT , false ) ) ;
rightColumn - > Add ( new TextView ( oldMemstickFolder . ToVisualString ( ) , ALIGN_LEFT , false ) ) ;
rightColumn - > Add ( new TextView ( oldFreeSpaceText , ALIGN_LEFT , false ) ) ;
}
2021-07-29 21:03:43 +00:00
if ( moveDataTask_ ) {
progressView_ = leftColumn - > Add ( new TextView ( progressReporter_ . Get ( ) ) ) ;
} else {
progressView_ = nullptr ;
2021-07-24 22:16:30 +00:00
}
2021-07-29 21:03:43 +00:00
if ( ! moveDataTask_ ) {
if ( ! initialSetup_ ) {
2021-08-05 19:53:08 +00:00
leftColumn - > Add ( new CheckBox ( & moveData_ , iz - > T ( " Move Data " ) ) ) - > OnClick . Handle ( this , & ConfirmMemstickMoveScreen : : OnMoveDataClick ) ;
2021-07-29 21:03:43 +00:00
}
leftColumn - > Add ( new Choice ( di - > T ( " OK " ) ) ) - > OnClick . Handle ( this , & ConfirmMemstickMoveScreen : : OnConfirm ) ;
leftColumn - > Add ( new Choice ( di - > T ( " Back " ) ) ) - > OnClick . Handle < UIScreen > ( this , & UIScreen : : OnBack ) ;
}
2021-07-24 22:16:30 +00:00
}
2021-08-05 19:53:08 +00:00
UI : : EventReturn ConfirmMemstickMoveScreen : : OnMoveDataClick ( UI : : EventParams & params ) {
RecreateViews ( ) ;
return UI : : EVENT_DONE ;
}
2021-07-29 21:03:43 +00:00
void ConfirmMemstickMoveScreen : : update ( ) {
UIDialogScreenWithBackground : : update ( ) ;
if ( moveDataTask_ ) {
if ( progressView_ ) {
progressView_ - > SetText ( progressReporter_ . Get ( ) ) ;
}
bool * result = moveDataTask_ - > Poll ( ) ;
if ( result ) {
if ( * result ) {
progressReporter_ . Set ( " Done! " ) ;
INFO_LOG ( SYSTEM , " Move data task finished successfully! " ) ;
// Succeeded!
FinishFolderMove ( ) ;
} else {
INFO_LOG ( SYSTEM , " Move data task failed! " ) ;
// What do we do here? We might be in the middle of a move... Bad.
RecreateViews ( ) ;
}
delete moveDataTask_ ;
moveDataTask_ = nullptr ;
}
}
}
2021-07-24 22:16:30 +00:00
UI : : EventReturn ConfirmMemstickMoveScreen : : OnConfirm ( UI : : EventParams & params ) {
auto sy = GetI18NCategory ( " System " ) ;
auto iz = GetI18NCategory ( " MemStick " ) ;
// Transfer all the files in /PSP from the original directory.
// Should probably be done on a background thread so we can show some UI.
// So we probably need another screen for this with a progress bar..
// If the directory itself is called PSP, don't go below.
if ( moveData_ ) {
2021-07-29 21:03:43 +00:00
progressReporter_ . Set ( " Starting move... " ) ;
moveDataTask_ = Promise < bool > : : Spawn ( & g_threadManager , [ & ] ( ) - > bool * {
Path moveSrc = g_Config . memStickDirectory ;
Path moveDest = newMemstickFolder_ ;
if ( moveSrc . GetFilename ( ) ! = " PSP " ) {
moveSrc = moveSrc / " PSP " ;
}
if ( moveDest . GetFilename ( ) ! = " PSP " ) {
moveDest = moveDest / " PSP " ;
File : : CreateDir ( moveDest ) ;
}
2021-07-24 22:16:30 +00:00
2021-07-29 21:03:43 +00:00
INFO_LOG ( SYSTEM , " About to move PSP data from '%s' to '%s' " , moveSrc . c_str ( ) , moveDest . c_str ( ) ) ;
2021-07-24 22:16:30 +00:00
2021-07-29 21:03:43 +00:00
// Search through recursively, listing the files to move and also summing their sizes.
std : : vector < std : : string > fileSuffixesToMove ;
std : : vector < std : : string > directorySuffixesToCreate ;
2021-07-24 22:16:30 +00:00
2021-07-29 21:03:43 +00:00
// NOTE: It's correct to pass moveSrc twice here, it's to keep the root in the recursion.
if ( ! ListFileSuffixesRecursively ( moveSrc , moveSrc , directorySuffixesToCreate , fileSuffixesToMove ) ) {
// TODO: Handle failure listing files.
std : : string error = " Failed to read old directory " ;
INFO_LOG ( SYSTEM , " %s " , error . c_str ( ) ) ;
progressReporter_ . Set ( error ) ;
return new bool ( false ) ;
}
2021-06-05 14:59:38 +00:00
2021-07-29 21:03:43 +00:00
bool dryRun = false ; // Useful for debugging.
2021-07-24 22:16:30 +00:00
2021-07-29 21:03:43 +00:00
size_t moveFailures = 0 ;
2021-07-24 22:16:30 +00:00
2021-07-29 21:03:43 +00:00
if ( ! moveSrc . empty ( ) ) {
// Better not interrupt the app while this is happening!
2021-07-24 22:16:30 +00:00
2021-07-29 21:03:43 +00:00
// Create all the necessary directories.
for ( auto & dirSuffix : directorySuffixesToCreate ) {
Path dir = moveDest / dirSuffix ;
if ( dryRun ) {
INFO_LOG ( SYSTEM , " dry run: Would have created dir '%s' " , dir . c_str ( ) ) ;
} else {
INFO_LOG ( SYSTEM , " Creating dir '%s' " , dir . c_str ( ) ) ;
if ( ! File : : Exists ( dir ) ) {
File : : CreateDir ( dir ) ;
}
2021-07-24 22:16:30 +00:00
}
}
2021-07-24 22:54:41 +00:00
2021-07-29 21:03:43 +00:00
for ( auto & fileSuffix : fileSuffixesToMove ) {
progressReporter_ . Set ( fileSuffix ) ;
Path from = moveSrc / fileSuffix ;
Path to = moveDest / fileSuffix ;
if ( dryRun ) {
INFO_LOG ( SYSTEM , " dry run: Would have moved '%s' to '%s' " , from . c_str ( ) , to . c_str ( ) ) ;
2021-07-24 22:54:41 +00:00
} else {
2021-07-29 21:03:43 +00:00
// Remove the "from" prefix from the path.
// We have to drop down to string operations for this.
if ( ! File : : Move ( from , to ) ) {
ERROR_LOG ( SYSTEM , " Failed to move file '%s' to '%s' " , from . c_str ( ) , to . c_str ( ) ) ;
moveFailures + + ;
// Should probably just bail?
} else {
INFO_LOG ( SYSTEM , " Moved file '%s' to '%s' " , from . c_str ( ) , to . c_str ( ) ) ;
}
}
}
// Delete all the old, now hopefully empty, directories.
for ( auto & dirSuffix : directorySuffixesToCreate ) {
Path dir = moveSrc / dirSuffix ;
if ( dryRun ) {
INFO_LOG ( SYSTEM , " dry run: Would have deleted dir '%s' " , dir . c_str ( ) ) ;
} else {
INFO_LOG ( SYSTEM , " Deleting dir '%s' " , dir . c_str ( ) ) ;
if ( ! File : : Exists ( dir ) ) {
File : : DeleteDir ( dir ) ;
}
2021-07-24 22:16:30 +00:00
}
}
}
2021-07-29 21:03:43 +00:00
if ( moveFailures > 0 ) {
std : : string error = " Failed to move some files! " ;
progressReporter_ . Set ( error ) ;
return new bool ( false ) ;
}
return new bool ( true ) ;
} , TaskType : : IO_BLOCKING ) ;
RecreateViews ( ) ;
} else {
FinishFolderMove ( ) ;
2021-07-25 13:33:11 +00:00
}
2021-06-05 14:59:38 +00:00
2021-07-29 21:03:43 +00:00
return UI : : EVENT_DONE ;
}
void ConfirmMemstickMoveScreen : : FinishFolderMove ( ) {
auto iz = GetI18NCategory ( " MemStick " ) ;
2021-07-24 22:16:30 +00:00
// Successful so far, switch the memstick folder.
2021-07-26 20:15:34 +00:00
if ( ! SwitchMemstickFolderTo ( newMemstickFolder_ ) ) {
// TODO: More precise errors.
error_ = iz - > T ( " That folder doesn't work as a memstick folder. " ) ;
2021-07-29 21:03:43 +00:00
return ;
2021-07-26 20:15:34 +00:00
}
2021-07-24 22:16:30 +00:00
// If the chosen folder already had a config, reload it!
g_Config . Load ( ) ;
2021-08-05 19:53:08 +00:00
if ( ! initialSetup_ ) {
// We restart the app here, to get the new settings.
System_SendMessage ( " graphics_restart " , " " ) ;
}
2021-07-25 13:33:11 +00:00
if ( g_Config . Save ( " MemstickPathChanged " ) ) {
TriggerFinish ( DialogResult : : DR_OK ) ;
} else {
2021-07-24 22:16:30 +00:00
error_ = iz - > T ( " Failed to save config " ) ;
2021-07-25 13:33:11 +00:00
RecreateViews ( ) ;
2021-06-05 14:59:38 +00:00
}
}