diff options
-rw-r--r-- | ChangeLog | 12 | ||||
-rw-r--r-- | embed/ephy-favicon-cache.c | 29 | ||||
-rw-r--r-- | embed/ephy-history.c | 29 | ||||
-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 | ||||
-rw-r--r-- | src/bookmarks/ephy-bookmarks.c | 48 |
7 files changed, 127 insertions, 137 deletions
@@ -1,3 +1,15 @@ +2003-09-18 Christian Persch <chpe@cvs.gnome.org> + + * embed/ephy-favicon-cache.c: (ephy_favicon_cache_init): + * embed/ephy-history.c: (ephy_history_init): + * lib/ephy-node-db.c: (ephy_node_db_load_from_file): + * lib/ephy-node-db.h: + * lib/ephy-state.c: (ensure_states): + * src/bookmarks/ephy-bookmarks.c: (ephy_bookmarks_init): + + Refactor node db loading functions into a common one in + ephy-node-db. Switch to using xmlReader api. + 2003-09-17 Christian Persch <chpe@cvs.gnome.org> * embed/mozilla/ExternalProtocolService.cpp: diff --git a/embed/ephy-favicon-cache.c b/embed/ephy-favicon-cache.c index 3c4bc6654..727094f6d 100644 --- a/embed/ephy-favicon-cache.c +++ b/embed/ephy-favicon-cache.c @@ -31,6 +31,7 @@ #include "ephy-node.h" #include "ephy-debug.h" +#define EPHY_FAVICON_CACHE_XML_ROOT "ephy_favicons_cache" #define EPHY_FAVICON_CACHE_XML_VERSION "1.0" #define EPHY_FAVICON_CACHE_OBSOLETE_DAYS 30 @@ -123,30 +124,6 @@ ephy_favicon_cache_new (void) return cache; } -static void -ephy_favicon_cache_load (EphyFaviconCache *eb) -{ - xmlDocPtr doc; - xmlNodePtr root, child; - - if (g_file_test (eb->priv->xml_file, G_FILE_TEST_EXISTS) == FALSE) - return; - - doc = xmlParseFile (eb->priv->xml_file); - g_assert (doc != NULL); - - root = xmlDocGetRootElement (doc); - - for (child = root->children; child != NULL; child = child->next) - { - EphyNode *node; - - node = ephy_node_new_from_xml (eb->priv->db, child); - } - - xmlFreeDoc (doc); -} - static gboolean icon_is_obsolete (EphyNode *node, GDate *now) { @@ -304,7 +281,9 @@ ephy_favicon_cache_init (EphyFaviconCache *cache) (EphyNodeCallback) icons_removed_cb, G_OBJECT (cache)); - ephy_favicon_cache_load (cache); + ephy_node_db_load_from_file (cache->priv->db, cache->priv->xml_file, + EPHY_FAVICON_CACHE_XML_ROOT, + EPHY_FAVICON_CACHE_XML_VERSION); } static gboolean diff --git a/embed/ephy-history.c b/embed/ephy-history.c index 3a00a9c98..a0e6a6626 100644 --- a/embed/ephy-history.c +++ b/embed/ephy-history.c @@ -34,6 +34,7 @@ #include <bonobo/bonobo-i18n.h> #include <libgnomevfs/gnome-vfs-uri.h> +#define EPHY_HISTORY_XML_ROOT "ephy_history" #define EPHY_HISTORY_XML_VERSION "1.0" /* how often to save the history, in milliseconds */ @@ -202,30 +203,6 @@ ephy_history_class_init (EphyHistoryClass *klass) g_type_class_add_private (object_class, sizeof(EphyHistoryPrivate)); } -static void -ephy_history_load (EphyHistory *eb) -{ - xmlDocPtr doc; - xmlNodePtr root, child; - - if (g_file_test (eb->priv->xml_file, G_FILE_TEST_EXISTS) == FALSE) - return; - - doc = xmlParseFile (eb->priv->xml_file); - g_return_if_fail (doc != NULL); - - root = xmlDocGetRootElement (doc); - - for (child = root->children; child != NULL; child = child->next) - { - EphyNode *node; - - node = ephy_node_new_from_xml (eb->priv->db, child); - } - - xmlFreeDoc (doc); -} - static gboolean page_is_obsolete (EphyNode *node, GDate *now) { @@ -478,7 +455,9 @@ ephy_history_init (EphyHistory *eb) ephy_node_add_child (eb->priv->hosts, eb->priv->pages); - ephy_history_load (eb); + ephy_node_db_load_from_file (eb->priv->db, eb->priv->xml_file, + EPHY_HISTORY_XML_ROOT, + EPHY_HISTORY_XML_VERSION); ephy_history_emit_data_changed (eb); g_hash_table_foreach (eb->priv->hosts_hash, 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); } } diff --git a/src/bookmarks/ephy-bookmarks.c b/src/bookmarks/ephy-bookmarks.c index bf1d575d2..0c05e59c7 100644 --- a/src/bookmarks/ephy-bookmarks.c +++ b/src/bookmarks/ephy-bookmarks.c @@ -39,6 +39,7 @@ #include <bonobo/bonobo-i18n.h> #include <libgnomevfs/gnome-vfs-utils.h> +#define EPHY_BOOKMARKS_XML_ROOT "ephy_bookmarks" #define EPHY_BOOKMARKS_XML_VERSION "1.0" #define BOOKMARKS_SAVE_DELAY (3 * 1000) #define MAX_FAVORITES_NUM 10 @@ -311,45 +312,6 @@ ephy_bookmarks_class_init (EphyBookmarksClass *klass) g_type_class_add_private (object_class, sizeof(EphyBookmarksPrivate)); } -static gboolean -ephy_bookmarks_load (EphyBookmarks *eb) -{ - xmlDocPtr doc; - xmlNodePtr root, child; - char *tmp; - gboolean result = TRUE; - - if (g_file_test (eb->priv->xml_file, G_FILE_TEST_EXISTS) == FALSE) - return FALSE; - - doc = xmlParseFile (eb->priv->xml_file); - g_return_val_if_fail (doc != NULL, FALSE); - - root = xmlDocGetRootElement (doc); - child = root->children; - - tmp = xmlGetProp (root, "version"); - if (tmp != NULL && strcmp (tmp, EPHY_BOOKMARKS_XML_VERSION) != 0) - { - g_warning ("Old bookmarks database format detected"); - - child = NULL; - result = FALSE; - } - g_free (tmp); - - for (; child != NULL; child = child->next) - { - EphyNode *node; - - node = ephy_node_new_from_xml (eb->priv->db, child); - } - - xmlFreeDoc (doc); - - return result; -} - static void ephy_bookmarks_save (EphyBookmarks *eb) { @@ -753,11 +715,13 @@ ephy_bookmarks_init (EphyBookmarks *eb) g_value_unset (&value); ephy_node_add_child (eb->priv->keywords, eb->priv->notcategorized); - if (!ephy_bookmarks_load (eb)) + if (ephy_node_db_load_from_file (eb->priv->db, eb->priv->xml_file, + EPHY_BOOKMARKS_XML_ROOT, + EPHY_BOOKMARKS_XML_VERSION) == FALSE) { - if (!ephy_bookmarks_import_rdf (eb, eb->priv->rdf_file)) + if (ephy_bookmarks_import_rdf (eb, eb->priv->rdf_file) == FALSE) { - eb->priv->init_defaults = !ephy_bookmarks_load (eb); + eb->priv->init_defaults = TRUE; } } |