usbloadergx/source/prompts/filebrowser.cpp

538 lines
18 KiB
C++
Raw Normal View History

/****************************************************************************
* libwiigui Template
* Tantric 2009
*
* modified by dimok
*
* filebrowser.cpp
*
* Generic file routines - reading, writing, browsing
***************************************************************************/
#include <gccore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wiiuse/wpad.h>
#include <sys/dir.h>
#include <sys/iosupport.h>
#include <malloc.h>
#include <algorithm>
#include "menu.h"
#include "themes/CTheme.h"
#include "FileOperations/fileops.h"
#include "language/gettext.h"
#include "PromptWindows.h"
#include "libwiigui/gui.h"
#include "sys.h"
#include "filebrowser.h"
/*** Extern variables ***/
extern GuiWindow * mainWindow;
extern u8 shutdown;
extern u8 reset;
/*** Extern functions ***/
extern void ResumeGui();
extern void HaltGui();
static int curDevice = -1;
static std::vector<BROWSERINFO> browsers;
BROWSERINFO *browser = NULL;
/****************************************************************************
* FileFilterCallbacks
* return: 1-visible 0-hidden
***************************************************************************/
2010-09-24 02:48:03 +02:00
int noDIRS(BROWSERENTRY *Entry, void* Args)
{
return !Entry->isdir;
}
2010-09-24 02:48:03 +02:00
int noFILES(BROWSERENTRY *Entry, void* Args)
{
return Entry->isdir;
}
2010-09-24 02:48:03 +02:00
int noEXT(BROWSERENTRY *Entry, void* Args)
{
2010-09-24 02:48:03 +02:00
if (!Entry->isdir)
{
2010-09-24 02:48:03 +02:00
char *cptr = strrchr(Entry->displayname, '.');
if (cptr && cptr != Entry->displayname) *cptr = 0;
}
return 1;
}
2010-09-24 02:48:03 +02:00
void ResetBrowser(BROWSERINFO *browser);
/****************************************************************************
* InitBrowsers()
* Clears the file browser memory, and allocates one initial entry
***************************************************************************/
int InitBrowsers()
{
curDevice = -1;
browsers.clear();
browser = NULL;
char rootdir[ROOTDIRLEN];
2010-09-24 02:48:03 +02:00
for (int i = 3; i < STD_MAX; i++)
{
2010-09-24 02:48:03 +02:00
if (strcmp(devoptab_list[i]->name, "stdnull") && devoptab_list[i]->write_r != NULL)
{
2010-09-24 02:48:03 +02:00
snprintf(rootdir, sizeof(rootdir), "%s:/", devoptab_list[i]->name);
if ( DIR_ITER *dir = diropen( rootdir ) )
{
2010-09-24 02:48:03 +02:00
dirclose(dir);
BROWSERINFO browser;
browser.dir[0] = '\0';
2010-09-24 02:48:03 +02:00
strcpy(browser.rootdir, rootdir);
ResetBrowser(&browser);
browsers.push_back(browser);
}
}
}
2010-09-24 02:48:03 +02:00
if (!browsers.size()) return -1;
curDevice = 0;
browser = &browsers[curDevice];
return 0;
}
/****************************************************************************
* ResetBrowser()
* Clears the file browser memory, and allocates one initial entry
***************************************************************************/
2010-09-24 02:48:03 +02:00
void ResetBrowser(BROWSERINFO *browser)
{
2010-09-24 02:48:03 +02:00
browser->pageIndex = 0;
browser->browserList.clear();
/*
2010-09-24 02:48:03 +02:00
// Clear any existing values
if (browser->browserList != NULL) {
free(browser->browserList);
browser->browserList = NULL;
}
// set aside space for 1 entry
browser->browserList = (BROWSERENTRY *)malloc(sizeof(BROWSERENTRY));
if(browser->browserList)
memset(browser->browserList, 0, sizeof(BROWSERENTRY));
*/
}
/****************************************************************************
* FileSortCallback
*
* sort callback to sort file entries with the following order:
* .
* ..
* <dirs>
* <files>
***************************************************************************/
//int FileSortCallback(const void *f1, const void *f2) {
2010-09-24 02:48:03 +02:00
bool operator<(const BROWSERENTRY &f1, const BROWSERENTRY &f2)
{
/* Special case for implicit directories */
2010-09-24 02:48:03 +02:00
if (f1.filename[0] == '.' || f2.filename[0] == '.')
{
2010-09-24 02:48:03 +02:00
if (strcmp(f1.filename, ".") == 0)
{
return true;
}
2010-09-24 02:48:03 +02:00
if (strcmp(f2.filename, ".") == 0)
{
return false;
}
2010-09-24 02:48:03 +02:00
if (strcmp(f1.filename, "..") == 0)
{
return true;
}
2010-09-24 02:48:03 +02:00
if (strcmp(f2.filename, "..") == 0)
{
return false;
}
}
/* If one is a file and one is a directory the directory is first. */
2010-09-24 02:48:03 +02:00
if (f1.isdir && !(f2.isdir)) return true;
if (!(f1.isdir) && f2.isdir) return false;
2010-09-24 02:48:03 +02:00
return stricmp(f1.filename, f2.filename) < 0;
}
2010-09-24 02:48:03 +02:00
int ParseFilter(FILTERCASCADE *Filter, BROWSERENTRY* Entry)
{
2010-09-24 02:48:03 +02:00
while (Filter)
{
2010-09-24 02:48:03 +02:00
if (Filter->filter && Filter->filter(Entry, Filter->filter_args) == 0) return 0;
Filter = Filter->next;
}
return 1;
}
/***************************************************************************
* Browse subdirectories
**************************************************************************/
2010-09-24 02:48:03 +02:00
int ParseDirectory(const char* Path, int Flags, FILTERCASCADE *Filter)
{
DIR_ITER *dir = NULL;
char fulldir[MAXPATHLEN];
char filename[MAXPATHLEN];
struct stat filestat;
unsigned int i;
2010-09-24 02:48:03 +02:00
if (curDevice == -1) if (InitBrowsers()) return -1; // InitBrowser fails
2010-09-24 02:48:03 +02:00
if (Path) // note in this codeblock use filename temporary
{
2010-09-24 02:48:03 +02:00
strlcpy(fulldir, Path, sizeof(fulldir));
if (*fulldir && fulldir[strlen(fulldir) - 1] != '/') // a file
{
2010-09-24 02:48:03 +02:00
char * chrp = strrchr(fulldir, '/');
if (chrp) chrp[1] = 0;
}
2010-09-24 02:48:03 +02:00
if (strchr(fulldir, ':') == NULL) // Path has no device device
{
2010-09-24 02:48:03 +02:00
getcwd(filename, sizeof(filename)); // save the current working dir
if (*fulldir == 0) // if path is empty
strlcpy(fulldir, filename, sizeof(fulldir)); // we use the current working dir
else
2010-09-24 02:48:03 +02:00
{ // path is not empty
if (chdir(fulldir)) ; // sets the path to concatenate and validate
{
2010-09-24 02:48:03 +02:00
if (Flags & (FB_TRYROOTDIR | FB_TRYSTDDEV)) if (chdir("/") && !(Flags & FB_TRYSTDDEV)) // try to set root if is needed
return -1;
}
2010-09-24 02:48:03 +02:00
if (getcwd(fulldir, sizeof(fulldir))) return -1; // gets the concatenated current working dir
chdir(filename); // restore the saved cwd
}
}
2010-09-24 02:48:03 +02:00
for (i = 0; i < browsers.size(); i++) // searchs the browser who match the path
{
2010-09-24 02:48:03 +02:00
if (strnicmp(fulldir, browsers[i].rootdir, strlen(browsers[i].rootdir) - 1 /*means without trailing '/'*/)
== 0)
{
browser = &browsers[curDevice];
break;
}
}
2010-09-24 02:48:03 +02:00
if (i != browsers.size()) // found browser
{
curDevice = i;
browser = &browsers[curDevice];
2010-09-24 02:48:03 +02:00
strcpy(browser->dir, &fulldir[strlen(browser->rootdir)]);
}
2010-09-24 02:48:03 +02:00
else if (Flags & FB_TRYSTDDEV)
{
curDevice = 0;
2010-09-24 02:48:03 +02:00
browser = &browsers[curDevice]; // when no browser was found and
browser->dir[0] = 0; // we alowed try StdDevice and try RootDir
strlcpy(fulldir, browser->rootdir, sizeof(fulldir)); // set the first browser with root-dir
}
2010-09-24 02:48:03 +02:00
else return -1;
}
2010-09-24 02:48:03 +02:00
else snprintf(fulldir, sizeof(fulldir), "%s%s", browser->rootdir, browser->dir);
// reset browser
2010-09-24 02:48:03 +02:00
ResetBrowser(browser);
// open the directory
2010-09-24 02:48:03 +02:00
if ((dir = diropen(fulldir)) == NULL)
{
2010-09-24 02:48:03 +02:00
if (Flags & FB_TRYROOTDIR)
{
browser->dir[0] = 0;
2010-09-24 02:48:03 +02:00
if ((dir = diropen(browser->rootdir)) == NULL) return -1;
}
2010-09-24 02:48:03 +02:00
else return -1;
}
2010-09-24 02:48:03 +02:00
while (dirnext(dir, filename, &filestat) == 0)
{
2010-09-24 02:48:03 +02:00
if (strcmp(filename, ".") != 0)
{
BROWSERENTRY newEntry;
2010-09-24 02:48:03 +02:00
memset(&newEntry, 0, sizeof(BROWSERENTRY)); // clear the new entry
strlcpy(newEntry.filename, filename, sizeof(newEntry.filename));
strlcpy(newEntry.displayname, filename, sizeof(newEntry.displayname));
newEntry.length = filestat.st_size;
2010-09-24 02:48:03 +02:00
newEntry.isdir = (filestat.st_mode & S_IFDIR) == 0 ? 0 : 1; // flag this as a dir
if (ParseFilter(Filter, &newEntry)) browser->browserList.push_back(newEntry);
}
}
// close directory
2010-09-24 02:48:03 +02:00
dirclose(dir);
// Sort the file list
2010-09-24 02:48:03 +02:00
std::sort(browser->browserList.begin(), browser->browserList.end());
return 0;
}
2010-09-24 02:48:03 +02:00
int ParseDirectory(int Device, int Flags, FILTERCASCADE *Filter)
{
2010-09-24 02:48:03 +02:00
if (Device >= 0 && Device < (int) browsers.size())
{
int old_curDevice = curDevice;
curDevice = Device;
browser = &browsers[curDevice];
2010-09-24 02:48:03 +02:00
if (ParseDirectory((char*) NULL, Flags, Filter) == 0) return 0;
curDevice = old_curDevice;
browser = &browsers[old_curDevice];
}
return -1;
}
/****************************************************************************
* BrowseDevice
* Displays a list of files on the selected path
***************************************************************************/
2010-09-24 02:48:03 +02:00
int BrowseDevice(char * Path, int Path_size, int Flags, FILTERCASCADE *Filter/*=NULL*/)
{
int result = -1;
int i;
2010-09-24 02:48:03 +02:00
if (InitBrowsers() || ParseDirectory(Path, Flags, Filter))
{
2010-09-24 02:48:03 +02:00
WindowPrompt(tr( "Error" ), 0, tr( "OK" ));
return -1;
}
int menu = MENU_NONE;
GuiTrigger trigA;
2010-09-24 02:48:03 +02:00
trigA.SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);
GuiTrigger trigB;
2010-09-24 02:48:03 +02:00
trigB.SetButtonOnlyTrigger(-1, WPAD_BUTTON_B | WPAD_CLASSIC_BUTTON_B, PAD_BUTTON_B);
GuiImageData folderImgData(Resources::GetFile("icon_folder.png"), Resources::GetFileSize("icon_folder.png"));
2010-09-24 02:48:03 +02:00
GuiImage folderImg(&folderImgData);
GuiButton folderBtn(folderImg.GetWidth(), folderImg.GetHeight());
folderBtn.SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
folderBtn.SetPosition(-210, -145);
folderBtn.SetImage(&folderImg);
folderBtn.SetTrigger(&trigA);
folderBtn.SetEffectGrow();
GuiImageData btnOutline(Resources::GetFile("button_dialogue_box.png"), Resources::GetFileSize("button_dialogue_box.png"));
GuiText ExitBtnTxt(tr( "Cancel" ), 24, ( GXColor ) {0, 0, 0, 255});
2010-09-24 02:48:03 +02:00
GuiImage ExitBtnImg(&btnOutline);
2010-10-27 16:45:27 +02:00
if (Settings.wsprompt)
{
2010-09-24 02:48:03 +02:00
ExitBtnTxt.SetWidescreen(Settings.widescreen);
ExitBtnImg.SetWidescreen(Settings.widescreen);
}
2010-09-24 02:48:03 +02:00
GuiButton ExitBtn(btnOutline.GetWidth(), btnOutline.GetHeight());
ExitBtn.SetAlignment(ALIGN_RIGHT, ALIGN_BOTTOM);
ExitBtn.SetPosition(-40, -35);
ExitBtn.SetLabel(&ExitBtnTxt);
ExitBtn.SetImage(&ExitBtnImg);
ExitBtn.SetTrigger(&trigA);
ExitBtn.SetTrigger(&trigB);
ExitBtn.SetEffectGrow();
GuiText usbBtnTxt(browsers[(curDevice + 1) % browsers.size()].rootdir, 24, ( GXColor ) {0, 0, 0, 255});
2010-09-24 02:48:03 +02:00
GuiImage usbBtnImg(&btnOutline);
2010-10-27 16:45:27 +02:00
if (Settings.wsprompt)
{
2010-09-24 02:48:03 +02:00
usbBtnTxt.SetWidescreen(Settings.widescreen);
usbBtnImg.SetWidescreen(Settings.widescreen);
}
2010-09-24 02:48:03 +02:00
GuiButton usbBtn(btnOutline.GetWidth(), btnOutline.GetHeight());
usbBtn.SetAlignment(ALIGN_CENTRE, ALIGN_BOTTOM);
usbBtn.SetPosition(0, -35);
usbBtn.SetLabel(&usbBtnTxt);
usbBtn.SetImage(&usbBtnImg);
usbBtn.SetTrigger(&trigA);
usbBtn.SetEffectGrow();
GuiText okBtnTxt(tr( "OK" ), 22, thColor("r=0 g=0 b=0 a=255 - prompt windows text color"));
2010-09-24 02:48:03 +02:00
GuiImage okBtnImg(&btnOutline);
2010-10-27 16:45:27 +02:00
if (Settings.wsprompt)
{
2010-09-24 02:48:03 +02:00
okBtnTxt.SetWidescreen(Settings.widescreen);
okBtnImg.SetWidescreen(Settings.widescreen);
}
GuiButton okBtn(&okBtnImg, &okBtnImg, 0, 4, 40, -35, &trigA, btnSoundOver, btnSoundClick2, 1);
2010-09-24 02:48:03 +02:00
okBtn.SetLabel(&okBtnTxt);
GuiFileBrowser fileBrowser(396, 248);
fileBrowser.SetAlignment(ALIGN_CENTRE, ALIGN_TOP);
fileBrowser.SetPosition(0, 120);
GuiImageData Address(Resources::GetFile("addressbar_textbox.png"), Resources::GetFileSize("addressbar_textbox.png"));
GuiText AdressText((char*) NULL, 20, ( GXColor ) {0, 0, 0, 255});
2010-09-24 02:48:03 +02:00
AdressText.SetTextf("%s%s", browser->rootdir, browser->dir);
AdressText.SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE);
AdressText.SetPosition(20, 0);
AdressText.SetMaxWidth(Address.GetWidth() - 40, SCROLL_HORIZONTAL);
GuiImage AdressbarImg(&Address);
GuiButton Adressbar(Address.GetWidth(), Address.GetHeight());
Adressbar.SetAlignment(ALIGN_CENTRE, ALIGN_TOP);
Adressbar.SetPosition(0, fileBrowser.GetTop() - 45);
Adressbar.SetImage(&AdressbarImg);
Adressbar.SetLabel(&AdressText);
HaltGui();
2010-09-24 02:48:03 +02:00
GuiWindow w(screenwidth, screenheight);
w.Append(&ExitBtn);
// w.Append(&titleTxt);
w.Append(&fileBrowser);
w.Append(&Adressbar);
w.Append(&okBtn);
if (!(Flags & FB_NOFOLDER_BTN)) w.Append(&folderBtn);
if (browsers.size() > 1 && !(Flags & FB_NODEVICE_BTN)) w.Append(&usbBtn);
mainWindow->Append(&w);
ResumeGui();
int clickedIndex = -1;
2010-09-24 02:48:03 +02:00
while (menu == MENU_NONE)
{
VIDEO_WaitVSync();
2010-09-24 02:48:03 +02:00
if (shutdown == 1) Sys_Shutdown();
2010-09-24 02:48:03 +02:00
if (reset == 1) Sys_Reboot();
2010-09-24 02:48:03 +02:00
for (i = 0; i < FILEBROWSERSIZE; i++)
{
2010-09-24 02:48:03 +02:00
if (fileBrowser.fileList[i]->GetState() == STATE_CLICKED)
{
fileBrowser.fileList[i]->ResetState();
clickedIndex = browser->pageIndex + i;
bool pathCanged = false;
// check corresponding browser entry
2010-09-24 02:48:03 +02:00
if (browser->browserList[clickedIndex].isdir)
{
/* go up to parent directory */
2010-09-24 02:48:03 +02:00
if (strcmp(browser->browserList[clickedIndex].filename, "..") == 0)
{
/* remove last subdirectory name */
2010-09-24 02:48:03 +02:00
int len = strlen(browser->dir);
while (browser->dir[0] && browser->dir[len - 1] == '/')
browser->dir[--len] = '\0'; // remove all trailing '/'
char *cptr = strrchr(browser->dir, '/');
if (cptr)
*++cptr = 0;
else browser->dir[0] = '\0'; // remove trailing dir
pathCanged = true;
}
/* Open a directory */
/* current directory doesn't change */
2010-09-24 02:48:03 +02:00
else if (strcmp(browser->browserList[clickedIndex].filename, "."))
{
/* test new directory namelength */
2010-09-24 02:48:03 +02:00
if ((strlen(browser->dir) + strlen(browser->browserList[clickedIndex].filename) + 1/*'/'*/)
< MAXPATHLEN)
{
/* update current directory name */
2010-09-24 02:48:03 +02:00
sprintf(browser->dir, "%s%s/", browser->dir, browser->browserList[clickedIndex].filename);
pathCanged = true;
}
}
2010-09-24 02:48:03 +02:00
if (pathCanged)
{
LOCK( &fileBrowser );
2010-09-24 02:48:03 +02:00
ParseDirectory((char*) NULL, Flags, Filter);
fileBrowser.ResetState();
fileBrowser.TriggerUpdate();
2010-09-24 02:48:03 +02:00
AdressText.SetTextf("%s%s", browser->rootdir, browser->dir);
}
clickedIndex = -1;
}
else /* isFile */
{
2010-09-24 02:48:03 +02:00
AdressText.SetTextf("%s%s%s", browser->rootdir, browser->dir,
browser->browserList[clickedIndex].filename);
}
}
}
2010-09-24 02:48:03 +02:00
if (ExitBtn.GetState() == STATE_CLICKED)
{
result = 0;
break;
}
2010-09-24 02:48:03 +02:00
else if (okBtn.GetState() == STATE_CLICKED)
{
2010-09-24 02:48:03 +02:00
if (clickedIndex >= 0)
snprintf(Path, Path_size, "%s%s%s", browser->rootdir, browser->dir,
browser->browserList[clickedIndex].filename);
else snprintf(Path, Path_size, "%s%s", browser->rootdir, browser->dir);
result = 1;
break;
}
2010-09-24 02:48:03 +02:00
else if (usbBtn.GetState() == STATE_CLICKED)
{
usbBtn.ResetState();
2010-09-24 02:48:03 +02:00
for (u32 i = 1; i < browsers.size(); i++)
{
LOCK( &fileBrowser );
2010-09-24 02:48:03 +02:00
if (ParseDirectory((curDevice + i) % browsers.size(), Flags, Filter) == 0)
{
fileBrowser.ResetState();
fileBrowser.TriggerUpdate();
2010-09-24 02:48:03 +02:00
AdressText.SetTextf("%s%s", browser->rootdir, browser->dir);
usbBtnTxt.SetText(browsers[(curDevice + 1) % browsers.size()].rootdir);
break;
}
}
}
2010-09-24 02:48:03 +02:00
else if (folderBtn.GetState() == STATE_CLICKED)
{
folderBtn.ResetState();
HaltGui();
2010-09-24 02:48:03 +02:00
mainWindow->Remove(&w);
ResumeGui();
char newfolder[100];
char oldfolder[100];
2010-09-24 02:48:03 +02:00
snprintf(newfolder, sizeof(newfolder), "%s%s", browser->rootdir, browser->dir);
strcpy(oldfolder, newfolder);
2010-09-24 02:48:03 +02:00
int result = OnScreenKeyboard(newfolder, sizeof(newfolder), strlen(browser->rootdir));
if (result == 1)
{
2010-09-24 02:48:03 +02:00
unsigned int len = strlen(newfolder);
if (len > 0 && len + 1 < sizeof(newfolder) && newfolder[len - 1] != '/')
{
newfolder[len] = '/';
2010-09-24 02:48:03 +02:00
newfolder[len + 1] = '\0';
}
struct stat st;
2010-09-24 02:48:03 +02:00
if (stat(newfolder, &st) != 0)
{
2010-09-24 02:48:03 +02:00
if (WindowPrompt(tr( "Directory does not exist!" ),
tr( "The entered directory does not exist. Would you like to create it?" ),
tr( "OK" ), tr( "Cancel" )) == 1) if (CreateSubfolder(newfolder) == false) WindowPrompt(
2010-09-24 02:48:03 +02:00
tr( "Error !" ), tr( "Can't create directory" ), tr( "OK" ));
}
2010-09-24 02:48:03 +02:00
if (ParseDirectory(newfolder, Flags, Filter) == 0)
{
fileBrowser.ResetState();
fileBrowser.TriggerUpdate();
2010-09-24 02:48:03 +02:00
AdressText.SetTextf("%s%s", browser->rootdir, browser->dir);
usbBtnTxt.SetText(browsers[(curDevice + 1) % browsers.size()].rootdir);
}
}
HaltGui();
2010-09-24 02:48:03 +02:00
mainWindow->Append(&w);
ResumeGui();
}
}
HaltGui();
2010-09-24 02:48:03 +02:00
mainWindow->Remove(&w);
ResumeGui();
//}
return result;
}
2010-09-24 02:48:03 +02:00
int BrowseDevice(char * Path, int Path_size, int Flags, FILEFILTERCALLBACK Filter, void *FilterArgs)
{
2010-09-24 02:48:03 +02:00
if (Filter)
{
2010-09-24 02:48:03 +02:00
FILTERCASCADE filter = { Filter, FilterArgs, NULL };
return BrowseDevice(Path, Path_size, Flags, &filter);
}
2010-09-24 02:48:03 +02:00
return BrowseDevice(Path, Path_size, Flags);
}