epiphany r8841 - branches/gnome-2-24/src/bookmarks



Author: vuntz
Date: Tue Mar  3 13:15:54 2009
New Revision: 8841
URL: http://svn.gnome.org/viewvc/epiphany?rev=8841&view=rev

Log:
Fix the RDF bookmark importer (bug #573220).

Correctly handle translations (for title, but also for localized URL).
Correctly handle bookmark topics.


Modified:
   branches/gnome-2-24/src/bookmarks/ephy-bookmarks-import.c

Modified: branches/gnome-2-24/src/bookmarks/ephy-bookmarks-import.c
==============================================================================
--- branches/gnome-2-24/src/bookmarks/ephy-bookmarks-import.c	(original)
+++ branches/gnome-2-24/src/bookmarks/ephy-bookmarks-import.c	Tue Mar  3 13:15:54 2009
@@ -813,49 +813,65 @@
 	return ret >= 0 ? TRUE : FALSE;
 }
 
-#define OLD_RDF_TEMPORARY_HACK
-
 static void
-parse_rdf_subjects (xmlNodePtr node,
-		    GList **subjects)
+parse_rdf_lang_tag (xmlNode  *child,
+		    xmlChar **value,
+		    int      *best_match)
 {
-	xmlChar *subject;
+	const char * const *locales;
+	char *this_language;
+	xmlChar *lang;
+	xmlChar *content;
+	int i;
+
+	if (*best_match == 0)
+		/* there's no way we can do better */
+		return;
+
+	content = xmlNodeGetContent (child);
+	if (!content)
+		return;
+
+	lang = xmlNodeGetLang (child);
+	if (lang == NULL)
+	{
+		const char *translated;
+
+		translated = _((char *) content);
+		if ((char *) content != translated)
+		{
+			/* if we have a translation for the content of the
+			 * node, then we just use this */
+			if (*value) xmlFree (*value);
+			*value = (xmlChar *) g_strdup (translated);
+			*best_match = 0;
 
-#ifdef OLD_RDF_TEMPORARY_HACK
-	xmlNode *child;
+			xmlFree (content);
+			return;
+		}
 
-	child = node->children;
+		this_language = "C";
+	}
+	else
+		this_language = (char *) lang;
 
-	while (child != NULL)
-	{
-		if (xmlStrEqual (child->name, (xmlChar *) "Bag"))
-		{
-			child = child->children;
+	locales = g_get_language_names ();
 
-			while (child != NULL)
-			{
-				if (xmlStrEqual (child->name, (xmlChar *) "li"))
-				{
-					subject = xmlNodeGetContent (child);
-					*subjects = g_list_append (*subjects, subject);
-				}
+	for (i = 0; locales[i] && i < *best_match; i++) {
+		if (!strcmp (locales[i], this_language)) {
+			/* if we've already encountered a less accurate
+			 * translation, then free it */
+			if (*value) xmlFree (*value);
 
-				child = child->next;
-			}
+			*value = content;
+			*best_match = i;
 
-			return;
+			break;
 		}
-
-		child = child->next;
 	}
-#endif
 
-	subject = xmlNodeGetContent (node);
-
-	if (subject)
-	{
-		*subjects = g_list_append (*subjects, subject);
-	}
+	if (lang) xmlFree (lang);
+	if (*value != content) xmlFree (content);
 }
 
 static void
@@ -863,43 +879,59 @@
 		xmlNodePtr node)
 {
 	xmlChar *title = NULL;
+	int best_match_title = INT_MAX;
 	xmlChar *link = NULL;
+	int best_match_link = INT_MAX;
+	/* we consider that it's better to use a non-localized smart link than
+	 * a localized link */
+	gboolean use_smartlink = FALSE;
+	xmlChar *subject = NULL;
 	GList *subjects = NULL, *l = NULL;
 	xmlNode *child;
-	EphyNode *bmk;
+	EphyNode *bmk = NULL;
 
 	child = node->children;
 
-#ifdef OLD_RDF_TEMPORARY_HACK
 	link = xmlGetProp (node, (xmlChar *) "about");
-#endif
 
 	while (child != NULL)
 	{
 		if (xmlStrEqual (child->name, (xmlChar *) "title"))
 		{
-			title = xmlNodeGetContent (child);
+			parse_rdf_lang_tag (child, &title, &best_match_title);
 		}
-#ifndef OLD_RDF_TEMPORARY_HACK
-		else if (xmlStrEqual (child->name, (xmlChar *) "link"))
+		else if (xmlStrEqual (child->name, (xmlChar *) "link") &&
+			 !use_smartlink)
 		{
-			link = xmlNodeGetContent (child);
+			parse_rdf_lang_tag (child, &link, &best_match_link);
 		}
-#endif
-		else if (xmlStrEqual (child->name, (xmlChar *) "subject"))
+		else if (child->ns &&
+			 xmlStrEqual (child->ns->prefix, (xmlChar *) "ephy") &&
+			 xmlStrEqual (child->name, (xmlChar *) "smartlink"))
 		{
-			parse_rdf_subjects (child, &subjects);
+			if (!use_smartlink)
+			{
+				use_smartlink = TRUE;
+				best_match_link = INT_MAX;
+			}
+
+			parse_rdf_lang_tag (child, &link, &best_match_link);
 		}
-		else if (xmlStrEqual (child->name, (xmlChar *) "smartlink"))
-		{
-			if (link) xmlFree (link);
-			link = xmlNodeGetContent (child);
+		else if (child->ns &&
+			 xmlStrEqual (child->ns->prefix, (xmlChar *) "dc") &&
+			 xmlStrEqual (child->name, (xmlChar *) "subject"))
+		{
+			subject = xmlNodeGetContent (child);
+			if (subject)
+				subjects = g_list_prepend (subjects, subject);
 		}
 
 		child = child->next;
 	}
 
-	bmk = bookmark_add (bookmarks, (char *) title, (char *) link);
+	if (link)
+		bmk = bookmark_add (bookmarks, (char *) title, (char *) link);
+
 	if (bmk)
 	{
 		l = subjects;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]