2009-12-12 19:04:35 +01:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2009
|
|
|
|
* by USB Loader GX Team
|
|
|
|
*
|
|
|
|
* This software is provided 'as-is', without any express or implied
|
|
|
|
* warranty. In no event will the authors be held liable for any
|
|
|
|
* damages arising from the use of this software.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any
|
|
|
|
* purpose, including commercial applications, and to alter it and
|
|
|
|
* redistribute it freely, subject to the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The origin of this software must not be misrepresented; you
|
|
|
|
* must not claim that you wrote the original software. If you use
|
|
|
|
* this software in a product, an acknowledgment in the product
|
|
|
|
* documentation would be appreciated but is not required.
|
|
|
|
*
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and
|
|
|
|
* must not be misrepresented as being the original software.
|
|
|
|
*
|
|
|
|
* 3. This notice may not be removed or altered from any source
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* Theme_List Class
|
|
|
|
* for the USB Loader GX
|
|
|
|
***************************************************************************/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <gctypes.h>
|
|
|
|
|
|
|
|
#include "Theme_List.h"
|
|
|
|
#include "xml/xml.h"
|
|
|
|
#include "prompts/PromptWindows.h"
|
|
|
|
|
|
|
|
#define stringcompare(text, cmp, pos) strncasecmp((const char*) &text[pos-strlen(cmp)], (const char*) cmp, strlen((const char*) cmp))
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
static void copyhtmlsting(const char *from, char *outtext, const char *stopat, u32 &cnt)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2009-12-12 19:04:35 +01:00
|
|
|
u32 cnt2 = 0;
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
u32 stringlength = strlen(from);
|
2009-12-12 19:04:35 +01:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
while ((stringcompare( from, stopat, cnt + strlen( stopat ) ) != 0) && (cnt2 < 1024) && (cnt < stringlength))
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2009-12-12 19:04:35 +01:00
|
|
|
outtext[cnt2] = from[cnt];
|
|
|
|
cnt2++;
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
outtext[cnt2] = '\0';
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
Theme_List::Theme_List(const char * url)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2009-12-12 19:04:35 +01:00
|
|
|
Theme = NULL;
|
|
|
|
themescount = 0;
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!IsNetworkInit())
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2009-12-12 19:04:35 +01:00
|
|
|
themescount = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
struct block file = downloadfile(url);
|
2009-12-12 19:04:35 +01:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
if (!file.data || !file.size)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2009-12-12 19:04:35 +01:00
|
|
|
themescount = -2;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
themescount = CountThemes(file.data);
|
|
|
|
if (themescount <= 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
free(file.data);
|
2009-12-12 19:04:35 +01:00
|
|
|
return;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2009-12-12 19:04:35 +01:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
ParseXML(file.data);
|
2009-12-12 19:04:35 +01:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
free(file.data);
|
2009-12-12 19:04:35 +01:00
|
|
|
}
|
|
|
|
|
2010-02-09 11:59:55 +01:00
|
|
|
Theme_List::~Theme_List()
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
for (int i = 0; i < themescount; i++)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (Theme[i].themetitle) delete[] Theme[i].themetitle;
|
|
|
|
if (Theme[i].author) delete[] Theme[i].author;
|
|
|
|
if (Theme[i].imagelink) delete[] Theme[i].imagelink;
|
|
|
|
if (Theme[i].downloadlink) delete[] Theme[i].downloadlink;
|
2009-12-12 19:04:35 +01:00
|
|
|
Theme[i].themetitle = NULL;
|
|
|
|
Theme[i].author = NULL;
|
|
|
|
Theme[i].imagelink = NULL;
|
|
|
|
Theme[i].downloadlink = NULL;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
if (Theme) delete[] Theme;
|
2009-12-12 19:04:35 +01:00
|
|
|
Theme = NULL;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
int Theme_List::CountThemes(const u8 * xmlfile)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
char tmp[200];
|
|
|
|
u32 cnt = 0;
|
2010-09-24 02:48:03 +02:00
|
|
|
u32 stringlength = strlen((const char *) xmlfile);
|
|
|
|
memset(tmp, 0, sizeof(tmp));
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
while (cnt < stringlength)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (stringcompare( xmlfile, "<totalthemes>", cnt ) == 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
copyhtmlsting((const char *) xmlfile, tmp, ">", cnt);
|
2010-09-19 01:16:05 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
cnt++;
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
tmp[cnt + 1] = 0;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
return atoi(tmp);
|
2009-12-12 19:04:35 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
bool Theme_List::ParseXML(const u8 * xmlfile)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
char element_text[1024];
|
2010-09-24 02:48:03 +02:00
|
|
|
memset(element_text, 0, sizeof(element_text));
|
2010-09-19 01:16:05 +02:00
|
|
|
mxml_node_t *nodetree = NULL;
|
|
|
|
mxml_node_t *nodedata = NULL;
|
|
|
|
mxml_node_t *nodeid = NULL;
|
|
|
|
mxml_index_t *nodeindex = NULL;
|
2009-12-12 19:04:35 +01:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
nodetree = mxmlLoadString(NULL, (const char *) xmlfile, MXML_OPAQUE_CALLBACK);
|
2009-12-12 19:04:35 +01:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
if (nodetree == NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-12-12 19:04:35 +01:00
|
|
|
return false;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2009-12-12 19:04:35 +01:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
nodedata = mxmlFindElement(nodetree, nodetree, "themes", NULL, NULL, MXML_DESCEND);
|
|
|
|
if (nodedata == NULL)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-12-12 19:04:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
nodeindex = mxmlIndexNew(nodedata, "name", NULL);
|
|
|
|
nodeid = mxmlIndexReset(nodeindex);
|
2009-12-12 19:04:35 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
Theme = new Theme_Info[themescount];
|
2010-09-24 02:48:03 +02:00
|
|
|
memset(Theme, 0, sizeof(Theme));
|
2009-12-12 19:04:35 +01:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
for (int i = 0; i < themescount; i++)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
nodeid = mxmlIndexFind(nodeindex, "name", NULL);
|
|
|
|
if (nodeid != NULL)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
get_nodetext(nodeid, element_text, sizeof(element_text));
|
|
|
|
Theme[i].themetitle = new char[strlen(element_text) + 2];
|
|
|
|
snprintf(Theme[i].themetitle, strlen(element_text) + 1, "%s", element_text);
|
|
|
|
|
|
|
|
GetTextFromNode(nodeid, nodedata, (char *) "creator", NULL, NULL, MXML_NO_DESCEND, element_text,
|
|
|
|
sizeof(element_text));
|
|
|
|
Theme[i].author = new char[strlen(element_text) + 2];
|
|
|
|
snprintf(Theme[i].author, strlen(element_text) + 1, "%s", element_text);
|
|
|
|
|
|
|
|
GetTextFromNode(nodeid, nodedata, (char *) "thumbpath", NULL, NULL, MXML_NO_DESCEND, element_text,
|
|
|
|
sizeof(element_text));
|
|
|
|
Theme[i].imagelink = new char[strlen(element_text) + 2];
|
|
|
|
snprintf(Theme[i].imagelink, strlen(element_text) + 1, "%s", element_text);
|
|
|
|
|
|
|
|
GetTextFromNode(nodeid, nodedata, (char *) "downloadpath", NULL, NULL, MXML_NO_DESCEND, element_text,
|
|
|
|
sizeof(element_text));
|
|
|
|
Theme[i].downloadlink = new char[strlen(element_text) + 2];
|
|
|
|
snprintf(Theme[i].downloadlink, strlen(element_text) + 1, "%s", element_text);
|
|
|
|
|
|
|
|
GetTextFromNode(nodeid, nodedata, (char *) "averagerating", NULL, NULL, MXML_NO_DESCEND, element_text,
|
|
|
|
sizeof(element_text));
|
|
|
|
Theme[i].rating = atoi(element_text);
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
mxmlIndexDelete(nodeindex);
|
|
|
|
free(nodetree);
|
|
|
|
free(nodedata);
|
|
|
|
free(nodeid);
|
2010-09-19 01:16:05 +02:00
|
|
|
nodetree = NULL;
|
|
|
|
nodedata = NULL;
|
|
|
|
nodeid = NULL;
|
|
|
|
nodeindex = NULL;
|
2009-12-12 19:04:35 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
const char * Theme_List::GetThemeTitle(int ind)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (ind > themescount || ind < 0 || !Theme || themescount <= 0)
|
2009-12-12 19:04:35 +01:00
|
|
|
return NULL;
|
2010-09-24 02:48:03 +02:00
|
|
|
else return Theme[ind].themetitle;
|
2009-12-12 19:04:35 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
const char * Theme_List::GetThemeAuthor(int ind)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (ind > themescount || ind < 0 || !Theme || themescount <= 0)
|
2009-12-12 19:04:35 +01:00
|
|
|
return NULL;
|
2010-09-24 02:48:03 +02:00
|
|
|
else return Theme[ind].author;
|
2009-12-12 19:04:35 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
const char * Theme_List::GetImageLink(int ind)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (ind > themescount || ind < 0 || !Theme || themescount <= 0)
|
2009-12-12 19:04:35 +01:00
|
|
|
return NULL;
|
2010-09-24 02:48:03 +02:00
|
|
|
else return Theme[ind].imagelink;
|
2009-12-12 19:04:35 +01:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
const char * Theme_List::GetDownloadLink(int ind)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (ind > themescount || ind < 0 || !Theme || themescount <= 0)
|
2009-12-12 19:04:35 +01:00
|
|
|
return NULL;
|
2010-09-24 02:48:03 +02:00
|
|
|
else return Theme[ind].downloadlink;
|
2009-12-12 19:04:35 +01:00
|
|
|
}
|
|
|
|
|
2010-02-09 11:59:55 +01:00
|
|
|
int Theme_List::GetThemeCount()
|
|
|
|
{
|
2009-12-12 19:04:35 +01:00
|
|
|
return themescount;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
static int ListCompare(const void *a, const void *b)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
Theme_Info *ab = (Theme_Info*) a;
|
|
|
|
Theme_Info *bb = (Theme_Info*) b;
|
2009-12-12 19:04:35 +01:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
return stricmp((char *) ab->themetitle, (char *) bb->themetitle);
|
2009-12-12 19:04:35 +01:00
|
|
|
}
|
2010-02-09 11:59:55 +01:00
|
|
|
void Theme_List::SortList()
|
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
qsort(Theme, themescount, sizeof(Theme_Info), ListCompare);
|
2009-12-12 19:04:35 +01:00
|
|
|
}
|