[evolution/eds-mailer: 4/49] Modify filter.xml format per discussion on IRC



commit ccfa2d537f83200c80eec1aae0718942632445bf
Author: Jonathon Jongsma <jonathon quotidian org>
Date:   Tue Dec 22 16:47:47 2009 -0600

    Modify filter.xml format per discussion on IRC
    
    The cached s-expressions are now saved inside the <rule> element for each filter
    as <partsexp> and <actionsexp> elements, as suggested by Matthew Barnes.
    
    Also, moved the logic into EMFilterRule's xml_encode vfunc rather than
    implementing the save() vfunc in EMFilterContext.  This is a bit cleaner and
    makes for less code duplication.
    
    This patch also properly escapes the s-expression text for xml entities, etc.

 mail/em-filter-context.c |   65 ----------------------------------------------
 mail/em-filter-rule.c    |   25 ++++++++++++++++-
 mail/mail-session.c      |   27 +++++++++++++-----
 3 files changed, 42 insertions(+), 75 deletions(-)
---
diff --git a/mail/em-filter-context.c b/mail/em-filter-context.c
index 2e084f6..3c5d3e5 100644
--- a/mail/em-filter-context.c
+++ b/mail/em-filter-context.c
@@ -46,7 +46,6 @@ static void em_filter_context_finalise(GObject *obj);
 static GList *filter_rename_uri(ERuleContext *rc, const gchar *olduri, const gchar *newuri, GCompareFunc cmp);
 static GList *filter_delete_uri(ERuleContext *rc, const gchar *uri, GCompareFunc cmp);
 static EFilterElement *filter_new_element(ERuleContext *rc, const gchar *name);
-static gint filter_context_save (ERuleContext *rc, const gchar *user);
 
 static ERuleContextClass *parent_class = NULL;
 
@@ -88,7 +87,6 @@ em_filter_context_class_init(EMFilterContextClass *klass)
 	rc_class->rename_uri = filter_rename_uri;
 	rc_class->delete_uri = filter_delete_uri;
 	rc_class->new_element = filter_new_element;
-	rc_class->save = filter_context_save;
 }
 
 static void
@@ -299,66 +297,3 @@ filter_new_element(ERuleContext *rc, const gchar *type)
 		return parent_class->new_element(rc, type);
 	}
 }
-
-static gint
-filter_context_save (ERuleContext *context,
-                   const gchar *user)
-{
-	xmlDocPtr doc;
-	xmlNodePtr root, rules, work, cache;
-	GList *l;
-	EFilterRule *rule;
-	struct _rule_set_map *map;
-	gint ret;
-
-	doc = xmlNewDoc ((xmlChar *)"1.0");
-	/* FIXME: set character encoding to UTF-8? */
-	root = xmlNewDocNode (doc, NULL, (xmlChar *)"filteroptions", NULL);
-	xmlDocSetRootElement (doc, root);
-	cache = xmlNewNode (NULL, (xmlChar*) "cache");
-	l = context->rule_set_list;
-	while (l) {
-		map = l->data;
-		rules = xmlNewDocNode (doc, NULL, (xmlChar *)map->name, NULL);
-		xmlAddChild (root, rules);
-		rule = NULL;
-		while ((rule = map->next (context, rule, NULL))) {
-			if (!rule->system) {
-				GString *out = NULL;
-				xmlNodePtr node = NULL;
-
-				work = e_filter_rule_xml_encode (rule);
-				xmlAddChild (rules, work);
-
-				/* cache pre-built s-expressions for filters so
-				 * that backend can read them directly at
-				 * startup */
-				out = g_string_new ("");
-				node = xmlNewNode (NULL, (xmlChar*) "filter");
-				xmlSetProp (node, (xmlChar *)"name", (xmlChar *)rule->name);
-				xmlSetProp (node, (xmlChar *)"source", (xmlChar *)rule->source);
-
-				work = xmlNewNode (NULL, (xmlChar*) "match");
-				e_filter_rule_build_code (rule, out);
-				xmlNodeSetContent (work, (xmlChar*) out->str);
-				g_string_truncate (out, 0);
-				xmlAddChild (node, work);
-
-				work = xmlNewNode (NULL, (xmlChar*) "action");
-				em_filter_rule_build_action ((EMFilterRule*)rule, out);
-				xmlNodeSetContent (work, (xmlChar*) out->str);
-				g_string_free (out, TRUE);
-				xmlAddChild (node, work);
-				xmlAddChild (cache, node);
-			}
-		}
-		l = g_list_next (l);
-	}
-	xmlAddChild (root, cache);
-
-	ret = e_xml_save_file (user, doc);
-
-	xmlFreeDoc (doc);
-
-	return ret;
-}
diff --git a/mail/em-filter-rule.c b/mail/em-filter-rule.c
index aa72175..150516a 100644
--- a/mail/em-filter-rule.c
+++ b/mail/em-filter-rule.c
@@ -217,8 +217,10 @@ static xmlNodePtr
 xml_encode(EFilterRule *fr)
 {
 	EMFilterRule *ff =(EMFilterRule *)fr;
-	xmlNodePtr node, set, work;
+	xmlNodePtr node, set, work, sexp;
 	GList *l;
+	GString *out = NULL;
+	xmlChar *encoded;
 
         node = E_FILTER_RULE_CLASS(parent_class)->xml_encode(fr);
 	g_return_val_if_fail (node != NULL, NULL);
@@ -231,8 +233,27 @@ xml_encode(EFilterRule *fr)
 		l = l->next;
 	}
 
-	return node;
+	out = g_string_new ("");
+
+	/* cache pre-built s-expressions for filters so that backend can read
+	 * them directly at startup */
+	sexp = xmlNewNode(NULL, (xmlChar *)"partsexp");
+	e_filter_rule_build_code (fr, out);
+	encoded = xmlEncodeEntitiesReentrant (NULL, (xmlChar*) out->str);
+	g_string_truncate (out, 0);
+	xmlNodeSetContent (sexp, encoded);
+	xmlFree (encoded);
+	xmlAddChild (node, sexp);
+
+	sexp = xmlNewNode(NULL, (xmlChar *)"actionsexp");
+	em_filter_rule_build_action ((EMFilterRule*)fr, out);
+	encoded = xmlEncodeEntitiesReentrant (NULL, (xmlChar*) out->str);
+	g_string_free (out, TRUE);
+	xmlNodeSetContent (sexp, encoded);
+	xmlFree (encoded);
+	xmlAddChild (node, sexp);
 
+	return node;
 }
 
 static void
diff --git a/mail/mail-session.c b/mail/mail-session.c
index 457f69e..2670e85 100644
--- a/mail/mail-session.c
+++ b/mail/mail-session.c
@@ -544,31 +544,42 @@ filter_definition_free (FilterDefinition *filter_def)
 static void
 append_filter_definitions (GList **filters, xmlDocPtr doc, const gchar *source)
 {
-	xmlNodePtr root, cache, child, work;
+	xmlNodePtr root, ruleset, child, work;
 	xmlChar *prop;
 
 	g_return_if_fail (filters != NULL && doc != NULL && source != NULL);
 
 	root = xmlDocGetRootElement (doc);
-	cache = e_xml_get_child_by_name (root, (xmlChar*) "cache");
+	ruleset = e_xml_get_child_by_name (root, (xmlChar*) "ruleset");
 
-	if (!cache)
+	if (!ruleset)
 		return;
 
-	for (child = cache->children; child; child = child->next) {
+	for (child = ruleset->children; child; child = child->next) {
+		if (strcmp ("rule", (const gchar*) child->name) != 0)
+			continue;
+
 		prop = xmlGetProp (child, (xmlChar *)"source");
 		if (prop && strcmp ((gchar*) prop, source) == 0) {
 			FilterDefinition *filter = NULL;
-			xmlChar *name, *match, *action;
-			name = xmlNodeGetContent (child);
-			work = e_xml_get_child_by_name (child, (xmlChar*) "match");
+			xmlChar *name=NULL, *match=NULL, *action=NULL;
+			work = e_xml_get_child_by_name (child, (xmlChar*) "title");
+			if (!work)
+				goto skip_rule;
+			name = xmlNodeGetContent (work);
+			work = e_xml_get_child_by_name (child, (xmlChar*) "partsexp");
+			if (!work)
+				goto skip_rule;
 			match = xmlNodeGetContent (work);
-			work = e_xml_get_child_by_name (child, (xmlChar*) "action");
+			work = e_xml_get_child_by_name (child, (xmlChar*) "actionsexp");
+			if (!work)
+				goto skip_rule;
 			action = xmlNodeGetContent (work);
 
 			filter = filter_definition_new (name, match, action);
 			*filters = g_list_append (*filters, filter);
 
+skip_rule:
 			xmlFree (name);
 			xmlFree (match);
 			xmlFree (action);



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