From 6b00de66025093218127fa74d9a423d58e377025 Mon Sep 17 00:00:00 2001 From: Christian Persch Date: Sat, 8 Nov 2003 12:56:44 +0000 Subject: Port node db saving and RDF export to xml writer api. 2003-11-08 Christian Persch * embed/ephy-favicon-cache.c: (ephy_favicon_cache_get_type), (ephy_favicon_cache_finalize): * embed/ephy-history.c: (ephy_history_save): * lib/ephy-file-helpers.c: (ephy_file_switch_temp_file): * lib/ephy-file-helpers.h: * lib/ephy-node-db.c: (ephy_node_db_load_from_file), (ephy_node_db_write_to_xml_valist), (ephy_node_db_write_to_xml_safe): * lib/ephy-node-db.h: * lib/ephy-node.c: (write_parent), (ephy_node_write_to_xml): * lib/ephy-node.h: * lib/ephy-state.c: (ephy_states_save), (ensure_states): * src/bookmarks/ephy-bookmarks-export.c: (write_topics_list), (ephy_bookmarks_export_rdf): * src/bookmarks/ephy-bookmarks.c: (ephy_bookmarks_save): Port node db saving and RDF export to xml writer api. --- src/bookmarks/ephy-bookmarks-export.c | 220 +++++++++++++++++++++++++--------- src/bookmarks/ephy-bookmarks.c | 59 ++------- 2 files changed, 175 insertions(+), 104 deletions(-) (limited to 'src') diff --git a/src/bookmarks/ephy-bookmarks-export.c b/src/bookmarks/ephy-bookmarks-export.c index be9b97ea7..b0ac4bc56 100644 --- a/src/bookmarks/ephy-bookmarks-export.c +++ b/src/bookmarks/ephy-bookmarks-export.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2003 Marco Pesenti Gritti + * Copyright (C) 2003 Christian Persch * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -14,24 +15,29 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * $Id$ */ -#include -#include -#include - #include "ephy-bookmarks-export.h" #include "ephy-node-common.h" #include "ephy-file-helpers.h" #include "ephy-debug.h" -static void -add_topics_list (EphyNode *topics, EphyNode *bmk, - xmlNodePtr parent, xmlNsPtr dc_ns) +#include +#include +#include +#include + +static int +write_topics_list (EphyNode *topics, + EphyNode *bmk, + xmlTextWriterPtr writer) { GPtrArray *children; + GList *keywords = NULL, *l; int i; - GList *bmks = NULL, *l; + int ret = 0; children = ephy_node_get_children (topics); for (i = 0; i < children->len; i++) @@ -47,26 +53,27 @@ add_topics_list (EphyNode *topics, EphyNode *bmk, if (priority == EPHY_NODE_NORMAL_PRIORITY && ephy_node_has_child (kid, bmk)) { - bmks = g_list_append (bmks, kid); + keywords = g_list_prepend (keywords, kid); } } ephy_node_thaw (topics); - if (bmks == NULL) return; - - for (l = bmks; l != NULL; l = l->next) + for (l = keywords; l != NULL; l = l->next) { - const char *name; EphyNode *node = l->data; - xmlNodePtr item_node; + const char *name; name = ephy_node_get_property_string (node, EPHY_NODE_KEYWORD_PROP_NAME); - item_node = xmlNewChild (parent, dc_ns, "subject", NULL); - xmlNodeSetContent (item_node, name); + + ret = xmlTextWriterWriteElementNS + (writer, "dc", "subject", NULL, name); + if (ret < 0) break; } - g_list_free (bmks); + g_list_free (keywords); + + return ret >= 0 ? 0 : -1; } void @@ -74,50 +81,128 @@ ephy_bookmarks_export_rdf (EphyBookmarks *bookmarks, const char *filename) { EphyNode *bmks, *topics, *smart_bmks; - xmlDocPtr doc; - xmlNodePtr root, xml_node, channel_node, channel_seq_node; - xmlNsPtr ephy_ns, rdf_ns, dc_ns; + xmlTextWriterPtr writer; + char *tmp_file; GPtrArray *children; char *file_uri; - int i; + int i, ret; + + LOG ("Exporting as RDF to %s", filename) + + START_PROFILER ("Exporting as RDF") + + tmp_file = g_strconcat (filename, ".tmp", NULL); + + /* FIXME: do we want to turn on compression here? */ + writer = xmlNewTextWriterFilename (tmp_file, 0); + if (writer == NULL) return; - LOG ("Exporting to rdf") + ret = xmlTextWriterStartDocument (writer, "1.0", NULL, NULL); + if (ret < 0) goto out; - xmlIndentTreeOutput = TRUE; - doc = xmlNewDoc ("1.0"); + ret = xmlTextWriterStartElementNS + (writer, "rdf", "RDF", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); + if (ret < 0) goto out; - root = xmlNewDocNode (doc, NULL, "rdf:RDF", NULL); - xmlDocSetRootElement (doc, root); - rdf_ns = xmlNewNs (root, "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf"); - xmlNewNs (root, "http://purl.org/rss/1.0/", NULL); - dc_ns = xmlNewNs (root, "http://purl.org/dc/elements/1.1/", "dc"); - ephy_ns = xmlNewNs (root, "http://gnome.org/ns/epiphany#", "ephy"); - channel_node = xmlNewChild (root, NULL, "channel", NULL); + ret = xmlTextWriterWriteAttribute (writer, "xmlns", "http://purl.org/rss/1.0/"); + if (ret < 0) goto out; + + ret = xmlTextWriterWriteAttributeNS + (writer, "xmlns", "dc", NULL, "http://purl.org/dc/elements/1.1/"); + if (ret < 0) goto out; + + ret = xmlTextWriterWriteAttributeNS + (writer, "xmlns", "ephy", NULL, "http://gnome.org/ns/epiphany#"); + if (ret < 0) goto out; + + ret = xmlTextWriterStartElement (writer, "channel"); + if (ret < 0) goto out; + + /* FIXME is this UTF-8 ? */ file_uri = gnome_vfs_get_uri_from_local_path (filename); - xmlSetProp (channel_node, "rdf:about", file_uri); + ret = xmlTextWriterWriteAttributeNS (writer, "rdf", "about", NULL, file_uri); g_free (file_uri); + if (ret < 0) goto out; - xml_node = xmlNewChild (channel_node, NULL, "title", NULL); - xmlNodeSetContent (xml_node, "Epiphany bookmarks"); + ret = xmlTextWriterWriteElement (writer, "title", "Epiphany bookmarks"); + if (ret < 0) goto out; - xml_node = xmlNewChild (channel_node, NULL, "link", NULL); - xmlNodeSetContent (xml_node, "http://epiphany.mozdev.org"); + ret = xmlTextWriterWriteElement + (writer, "link", "http://www.gnome.org/projects/epiphany/"); + if (ret < 0) goto out; - xml_node = xmlNewChild (channel_node, NULL, "items", NULL); + ret = xmlTextWriterStartElement (writer, "items"); + if (ret < 0) goto out; - channel_seq_node = xmlNewChild (xml_node, rdf_ns, "Seq", NULL); + ret = xmlTextWriterStartElementNS (writer, "rdf", "Seq", NULL); + if (ret < 0) goto out; bmks = ephy_bookmarks_get_bookmarks (bookmarks); topics = ephy_bookmarks_get_keywords (bookmarks); smart_bmks = ephy_bookmarks_get_smart_bookmarks (bookmarks); children = ephy_node_get_children (bmks); - for (i = 0; i < children->len; i++) + for (i=0; i < children->len; i++) + { + EphyNode *kid; + const char *url; + char *link = NULL; + gboolean smart_url; + + kid = g_ptr_array_index (children, i); + + smart_url = ephy_node_has_child (smart_bmks, kid); + url = ephy_node_get_property_string + (kid, EPHY_NODE_BMK_PROP_LOCATION); + if (smart_url) + { + GnomeVFSURI *uri; + + uri = gnome_vfs_uri_new (url); + + if (uri) + { + link = g_strconcat (gnome_vfs_uri_get_scheme (uri), + "://", + gnome_vfs_uri_get_host_name (uri), + NULL); + + gnome_vfs_uri_unref (uri); + } + } + + if (link == NULL) + { + link = g_strdup (url); + } + + ret = xmlTextWriterStartElementNS (writer, "rdf", "li", NULL); + if (ret < 0) break; + + ret = xmlTextWriterWriteAttributeNS + (writer, "rdf", "about", NULL, link); + if (ret < 0) break; + + ret = xmlTextWriterEndElement (writer); /* rdf:li */ + if (ret < 0) break; + } + ephy_node_thaw (bmks); + if (ret < 0) goto out; + + ret = xmlTextWriterEndElement (writer); /* rdf:Seq */ + if (ret < 0) goto out; + + ret = xmlTextWriterEndElement (writer); /* items */ + if (ret < 0) goto out; + + ret = xmlTextWriterEndElement (writer); /* channel */ + if (ret < 0) goto out; + + children = ephy_node_get_children (bmks); + for (i=0; i < children->len; i++) { EphyNode *kid; const char *url, *title; - xmlNodePtr item_node; - xmlChar *encoded_link; char *link = NULL; gboolean smart_url; @@ -151,33 +236,54 @@ ephy_bookmarks_export_rdf (EphyBookmarks *bookmarks, link = g_strdup (url); } - encoded_link = xmlEncodeEntitiesReentrant (doc, link); - - item_node = xmlNewChild (channel_seq_node, rdf_ns, "li", NULL); - xmlSetNsProp (item_node, rdf_ns, "about", link); + ret = xmlTextWriterStartElement (writer, "item"); + if (ret < 0) break; - item_node = xmlNewChild (root, NULL, "item", NULL); - xmlSetNsProp (item_node, rdf_ns, "about", link); + ret = xmlTextWriterWriteAttributeNS + (writer, "rdf", "about", NULL, link); + if (ret < 0) break; - xml_node = xmlNewChild (item_node, NULL, "title", title); + ret = xmlTextWriterWriteElement (writer, "title", title); + if (ret < 0) break; - xml_node = xmlNewChild (item_node, NULL, "link", encoded_link); + ret = xmlTextWriterWriteElement (writer, "link", link); + if (ret < 0) break; if (smart_url) { - xmlChar *encoded_url; - encoded_url = xmlEncodeEntitiesReentrant (doc, url); - xml_node = xmlNewChild (item_node, ephy_ns, "smartlink", encoded_url); - xmlFree (encoded_url); + ret = xmlTextWriterWriteElementNS (writer, "ephy", "smartlink", NULL, url); + if (ret < 0) break; } - add_topics_list (topics, kid, item_node, dc_ns); + ret = write_topics_list (topics, kid, writer); + if (ret < 0) break; + + ret = xmlTextWriterEndElement (writer); /* item */ - xmlFree (encoded_link); g_free (link); + + i++; } ephy_node_thaw (bmks); + if (ret < 0) goto out; + + ret = xmlTextWriterEndElement (writer); /* rdf:RDF */ + if (ret < 0) goto out; + + ret = xmlTextWriterEndDocument (writer); + +out: + xmlFreeTextWriter (writer); + + if (ret >= 0) + { + if (ephy_file_switch_temp_file (filename, tmp_file) == FALSE) + { + ret = -1; + } + } + + STOP_PROFILER ("Exporting as RDF") - ephy_file_save_xml (filename, doc); - xmlFreeDoc(doc); + LOG ("Exporting as RDF %s.", ret >= 0 ? "succeeded" : "FAILED") } diff --git a/src/bookmarks/ephy-bookmarks.c b/src/bookmarks/ephy-bookmarks.c index 020eab429..2caf08c05 100644 --- a/src/bookmarks/ephy-bookmarks.c +++ b/src/bookmarks/ephy-bookmarks.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2002 Marco Pesenti Gritti + * Copyright (C) 2002, 2003 Marco Pesenti Gritti * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,7 +19,7 @@ */ #ifdef HAVE_CONFIG_H -#include +#include "config.h" #endif #include "ephy-bookmarks.h" @@ -249,54 +249,19 @@ ephy_bookmarks_class_init (EphyBookmarksClass *klass) static void ephy_bookmarks_save (EphyBookmarks *eb) { - xmlDocPtr doc; - xmlNodePtr root; - xmlNodePtr comment; - GPtrArray *children; - int i; LOG ("Saving bookmarks") - /* save nodes to xml */ - xmlIndentTreeOutput = TRUE; - doc = xmlNewDoc ("1.0"); - - root = xmlNewDocNode (doc, NULL, "ephy_bookmarks", NULL); - xmlSetProp (root, "version", EPHY_BOOKMARKS_XML_VERSION); - xmlDocSetRootElement (doc, root); - - comment = xmlNewDocComment (doc, "Do not rely on this file, it's only for internal use. Use bookmarks.rdf instead."); - xmlAddChild (root, comment); - - children = ephy_node_get_children (eb->priv->keywords); - for (i = 0; i < children->len; i++) - { - EphyNode *kid; - - kid = g_ptr_array_index (children, i); - - if (kid != eb->priv->bookmarks && - kid != eb->priv->favorites && - kid != eb->priv->notcategorized) - { - ephy_node_save_to_xml (kid, root); - } - } - ephy_node_thaw (eb->priv->keywords); - - children = ephy_node_get_children (eb->priv->bookmarks); - for (i = 0; i < children->len; i++) - { - EphyNode *kid; - - kid = g_ptr_array_index (children, i); - - ephy_node_save_to_xml (kid, root); - } - ephy_node_thaw (eb->priv->bookmarks); - - ephy_file_save_xml (eb->priv->xml_file, doc); - xmlFreeDoc(doc); + ephy_node_db_write_to_xml_safe + (eb->priv->db, eb->priv->xml_file, + EPHY_BOOKMARKS_XML_ROOT, + EPHY_BOOKMARKS_XML_VERSION, + "Do not rely on this file, it's only for internal use. Use bookmarks.rdf instead.", + eb->priv->keywords, + 3, eb->priv->bookmarks, eb->priv->favorites, eb->priv->notcategorized, + eb->priv->bookmarks, + 0, + NULL); /* Export bookmarks in rdf */ ephy_bookmarks_export_rdf (eb, eb->priv->rdf_file); -- cgit v1.2.3