diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ephy-node-db.c | 84 | ||||
-rw-r--r-- | lib/ephy-node-db.h | 5 | ||||
-rw-r--r-- | lib/ephy-state.c | 57 |
3 files changed, 101 insertions, 45 deletions
diff --git a/lib/ephy-node-db.c b/lib/ephy-node-db.c index c6d084bed..58ca3fe91 100644 --- a/lib/ephy-node-db.c +++ b/lib/ephy-node-db.c @@ -19,6 +19,9 @@ */ #include "ephy-node-db.h" +#include "ephy-debug.h" + +#include <libxml/xmlreader.h> static void ephy_node_db_class_init (EphyNodeDbClass *klass); static void ephy_node_db_init (EphyNodeDb *node); @@ -291,3 +294,84 @@ _ephy_node_db_remove_id (EphyNodeDb *db, g_static_rw_lock_writer_unlock (db->priv->id_to_node_lock); } + +gboolean +ephy_node_db_load_from_file (EphyNodeDb *db, + const char *xml_file, + const xmlChar *xml_root, + const xmlChar *xml_version) +{ + xmlTextReaderPtr reader; + gboolean success = TRUE; + int ret; + + LOG ("ephy_node_db_load_from_file %s", xml_file) + + START_PROFILER ("loading node db") + + if (g_file_test (xml_file, G_FILE_TEST_EXISTS) == FALSE) + { + return FALSE; + } + + reader = xmlNewTextReaderFilename (xml_file); + if (reader == NULL) + { + return FALSE; + } + + ret = xmlTextReaderRead (reader); + while (ret == 1) + { + xmlChar *name; + xmlReaderTypes type; + gboolean skip = FALSE; + + name = xmlTextReaderName (reader); + type = xmlTextReaderNodeType (reader); + + if (xmlStrEqual (name, "node") + && type == XML_READER_TYPE_ELEMENT) + { + xmlNodePtr subtree; + EphyNode *node; + + /* grow the subtree and load the node from it */ + subtree = xmlTextReaderExpand (reader); + + node = ephy_node_new_from_xml (db, subtree); + + skip = TRUE; + } + else if (xmlStrEqual (name, xml_root) + && type == XML_READER_TYPE_ELEMENT) + { + xmlChar *version; + + /* check version info */ + version = xmlTextReaderGetAttribute (reader, "version"); + if (xmlStrEqual (version, xml_version) == FALSE) + { + success = FALSE; + xmlFree (version); + xmlFree (name); + + break; + } + + xmlFree (version); + } + + xmlFree (name); + + /* next one, please */ + ret = skip ? xmlTextReaderNext (reader) + : xmlTextReaderRead (reader); + } + + xmlFreeTextReader (reader); + + STOP_PROFILER ("loading node db") + + return (success && ret == 0); +} diff --git a/lib/ephy-node-db.h b/lib/ephy-node-db.h index 2b1f8fd7d..f5ca81107 100644 --- a/lib/ephy-node-db.h +++ b/lib/ephy-node-db.h @@ -56,6 +56,11 @@ EphyNodeDb *ephy_node_db_get_by_name (const char *name); EphyNodeDb *ephy_node_db_new (const char *name); +gboolean ephy_node_db_load_from_file (EphyNodeDb *db, + const char *xml_file, + const xmlChar *xml_root, + const xmlChar *xml_version); + const char *ephy_node_db_get_name (EphyNodeDb *db); EphyNode *ephy_node_db_get_node_from_id (EphyNodeDb *db, diff --git a/lib/ephy-state.c b/lib/ephy-state.c index 3cbcb5020..822f9f393 100644 --- a/lib/ephy-state.c +++ b/lib/ephy-state.c @@ -30,6 +30,7 @@ #include <gtk/gtkpaned.h> #define STATES_FILE "states.xml" +#define EPHY_STATES_XML_ROOT "ephy_states" #define EPHY_STATES_XML_VERSION "1.0" enum @@ -48,50 +49,6 @@ static EphyNode *states = NULL; static EphyNodeDb *states_db = NULL; static void -ephy_states_load (void) -{ - xmlDocPtr doc; - xmlNodePtr root, child; - char *xml_file; - char *tmp; - - xml_file = g_build_filename (ephy_dot_dir (), - STATES_FILE, - NULL); - - if (g_file_test (xml_file, G_FILE_TEST_EXISTS) == FALSE) - return; - - doc = xmlParseFile (xml_file); - g_assert (doc != NULL); - - root = xmlDocGetRootElement (doc); - - tmp = xmlGetProp (root, "version"); - if (tmp != NULL && strcmp (tmp, EPHY_STATES_XML_VERSION) == 0) - { - child = root->children; - } - else - { - g_warning ("Old version of states.xml discarded"); - child = NULL; - } - g_free (tmp); - - for (; child != NULL; child = child->next) - { - EphyNode *node; - - node = ephy_node_new_from_xml (states_db, child); - } - - xmlFreeDoc (doc); - - g_free (xml_file); -} - -static void ephy_states_save (void) { xmlDocPtr doc; @@ -161,9 +118,19 @@ ensure_states (void) { if (states == NULL) { + char *xml_file; + + xml_file = g_build_filename (ephy_dot_dir (), + STATES_FILE, + NULL); + states_db = ephy_node_db_new (EPHY_NODE_DB_STATES); states = ephy_node_new_with_id (states_db, STATES_NODE_ID); - ephy_states_load (); + ephy_node_db_load_from_file (states_db, xml_file, + EPHY_STATES_XML_ROOT, + EPHY_STATES_XML_VERSION); + + g_free (xml_file); } } |