From f2ebc60ef0f00f01ff6611d534e201e13e8d1f86 Mon Sep 17 00:00:00 2001 From: James Bursa Date: Thu, 29 Jul 2004 11:40:48 +0000 Subject: [project @ 2004-07-29 11:40:48 by bursa] Rewrite saving to use libxml2. Clean up and simplify loading. svn path=/import/netsurf/; revision=1162 --- riscos/hotlist.c | 526 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 361 insertions(+), 165 deletions(-) (limited to 'riscos/hotlist.c') diff --git a/riscos/hotlist.c b/riscos/hotlist.c index d051df3c3..36b34a774 100644 --- a/riscos/hotlist.c +++ b/riscos/hotlist.c @@ -15,6 +15,7 @@ #include #include #include "libxml/HTMLparser.h" +#include "libxml/HTMLtree.h" #include "oslib/colourtrans.h" #include "oslib/dragasprite.h" #include "oslib/osfile.h" @@ -68,7 +69,7 @@ struct hotlist_entry { */ int children; - /** The title of the hotlist entry/folder + /** The title of the hotlist entry/folder, UTF-8. */ char *title; @@ -202,16 +203,21 @@ bool dialog_folder_add = false; bool dialog_entry_add = false; bool hotlist_insert = false; -/* Hotlist loading buffers -*/ -static char *load_title = NULL; -static char *load_url = NULL; - static bool ro_gui_hotlist_initialise_sprite(const char *name, int number); static bool ro_gui_hotlist_load(void); -static void ro_gui_hotlist_load_entry(xmlNode *cur, struct hotlist_entry *entry, bool allow_add); -static bool ro_gui_hotlist_save_entry(FILE *fp, struct hotlist_entry *entry); +static void ro_gui_hotlist_load_file(const char *filename); +static void ro_gui_hotlist_load_directory(xmlNode *ul, + struct hotlist_entry *directory); +static void ro_gui_hotlist_load_entry(xmlNode *li, + struct hotlist_entry *directory); +xmlNode *ro_gui_hotlist_find_element(xmlNode *node, const char *name); +bool ro_gui_hotlist_save_directory(struct hotlist_entry *directory, + xmlNode *node); +bool ro_gui_hotlist_save_entry(struct hotlist_entry *entry, + xmlNode *node); +bool ro_gui_hotlist_save_entry_comment(xmlNode *node, + const char *name, int value); static void ro_gui_hotlist_link_entry(struct hotlist_entry *link, struct hotlist_entry *entry, bool before); static void ro_gui_hotlist_delink_entry(struct hotlist_entry *entry); static void ro_gui_hotlist_delete_entry(struct hotlist_entry *entry, bool siblings); @@ -405,11 +411,9 @@ void ro_gui_hotlist_show(void) { bool ro_gui_hotlist_load(void) { - htmlDocPtr doc; - const char *encoding; fileswitch_object_type obj_type = 0; struct hotlist_entry *netsurf; - struct hotlist_entry *entry = &root; + struct hotlist_entry *entry; /* Check if we have an initial hotlist. OS_File does funny things relating to errors, so we use the object type to determine success @@ -417,37 +421,30 @@ bool ro_gui_hotlist_load(void) { xosfile_read_stamped_no_path("Choices:WWW.NetSurf.Hotlist", &obj_type, (bits)0, (bits)0, (int *)0, (fileswitch_attr)0, (bits)0); if (obj_type == fileswitch_IS_FILE) { - /* Read our file - */ - encoding = xmlGetCharEncodingName(XML_CHAR_ENCODING_8859_1); - doc = htmlParseFile("Choices:WWW.NetSurf.Hotlist", encoding); - if ((!doc) || (!(doc->children))) { - xmlFreeDoc(doc); - warn_user("HotlistLoadError", 0); - return false; - } - - /* Perform our recursive load - */ - ro_gui_hotlist_load_entry(doc->children, &root, true); - - /* Exit cleanly - */ - xmlFreeDoc(doc); + ro_gui_hotlist_load_file("Choices:WWW.NetSurf.Hotlist"); return true; + } else { /* Create a folder */ netsurf = ro_gui_hotlist_create_entry("NetSurf", NULL, 0, &root); + if (!netsurf) + return false; /* Add some content */ - entry = ro_gui_hotlist_create_entry("NetSurf homepage", "http://netsurf.sourceforge.net/", + entry = ro_gui_hotlist_create_entry("NetSurf homepage", + "http://netsurf.sourceforge.net/", 0xfaf, netsurf); - entry->add_date = (time_t)-1; - entry = ro_gui_hotlist_create_entry("NetSurf test builds", "http://netsurf.strcprstskrzkrk.co.uk/", + if (!entry) + return false; + entry->add_date = (time_t) -1; + entry = ro_gui_hotlist_create_entry("NetSurf test builds", + "http://netsurf.strcprstskrzkrk.co.uk/", 0xfaf, netsurf); - entry->add_date = (time_t)-1; + if (!entry) + return false; + entry->add_date = (time_t) -1; /* We succeeded */ @@ -456,103 +453,182 @@ bool ro_gui_hotlist_load(void) { } -void ro_gui_hotlist_load_entry(xmlNode *cur, struct hotlist_entry *entry, bool allow_add) { - struct hotlist_entry *last_entry = entry; - char *xml_comment = NULL; - char *comment = comment; - int filetype = 0xfaf; - int add_date = -1; - int last_date = -1; - int visits = 0; - bool add_entry; +/** + * Load the hotlist from file. + * + * \param filename name of file to read + */ - while (true) { - /* Add any items that have had all the data they can have - */ - if ((allow_add) && (load_title != NULL)) { - if ((cur == NULL) || ((cur->type == XML_ELEMENT_NODE) && - ((!(strcmp(cur->name, "li"))) || (!(strcmp(cur->name, "h4"))) || - (!(strcmp(cur->name, "ul")))))) { +void ro_gui_hotlist_load_file(const char *filename) +{ + xmlDoc *doc; + xmlNode *html, *body, *ul; - /* Add the entry - */ - last_entry = ro_gui_hotlist_create_entry(load_title, load_url, filetype, entry); - last_entry->add_date = add_date; - if (last_entry->url) { - last_entry->last_date = last_date; - last_entry->visits = visits; - last_entry->filetype = filetype; - } + doc = htmlParseFile(filename, "iso-8859-1"); + if (!doc) { + warn_user("HotlistLoadError", messages_get("ParsingFail")); + return; + } - /* Reset our variables - */ - if (load_title) xmlFree(load_title); - load_title = NULL; - if (load_url) xmlFree(load_url); - load_url = NULL; - filetype = 0xfaf; - add_date = -1; - last_date = -1; - visits = 0; - } - } + html = ro_gui_hotlist_find_element((xmlNode *) doc, "html"); + body = ro_gui_hotlist_find_element(html, "body"); + ul = ro_gui_hotlist_find_element(body, "ul"); + if (!ul) { + xmlFreeDoc(doc); + warn_user("HotlistLoadError", + "(......