From 33b3bebc5d206145cf608c7112ecf8b188ac0a1c Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Fri, 26 Dec 2003 06:56:12 +0000 Subject: [PATCH] When looking up templates by name and name isn't found, return first template instead of NULL (suggested by Wayne Schuller ). In addition, added code to register templates (make known, if not already known) when they are encountered when reading a label. git-svn-id: https://glabels.svn.sourceforge.net/svnroot/glabels/trunk@386 f5e0f49d-192f-0410-a22d-a8d8700d0965 --- glabels2/src/paper.c | 28 ++++++++++++++++++++++ glabels2/src/paper.h | 2 ++ glabels2/src/template.c | 46 +++++++++++++++++++++++++++++++++++- glabels2/src/template.h | 2 ++ glabels2/src/xml-label-191.c | 1 + glabels2/src/xml-label.c | 5 ++++ 6 files changed, 83 insertions(+), 1 deletion(-) diff --git a/glabels2/src/paper.c b/glabels2/src/paper.c index 7f0f21b8..55875a7a 100644 --- a/glabels2/src/paper.c +++ b/glabels2/src/paper.c @@ -161,6 +161,34 @@ gl_paper_free_name_list (GList **names) gl_debug (DEBUG_PAPER, "END"); } +/*****************************************************************************/ +/* Is page size id known? */ +/*****************************************************************************/ +gboolean +gl_paper_is_id_known (const gchar *id) +{ + GList *p; + glPaper *paper; + + gl_debug (DEBUG_PAPER, "START"); + + if (id == NULL) { + gl_debug (DEBUG_PAPER, "END (false, id=NULL)"); + return FALSE; + } + + for (p = papers; p != NULL; p = p->next) { + paper = (glPaper *) p->data; + if (g_strcasecmp (paper->id, id) == 0) { + gl_debug (DEBUG_PAPER, "END (true)"); + return TRUE; + } + } + + gl_debug (DEBUG_PAPER, "END (false)"); + return FALSE; +} + /*****************************************************************************/ /* Return a paper structure from id. */ /*****************************************************************************/ diff --git a/glabels2/src/paper.h b/glabels2/src/paper.h index a765204b..ce418d56 100644 --- a/glabels2/src/paper.h +++ b/glabels2/src/paper.h @@ -46,6 +46,8 @@ void gl_paper_free_id_list (GList **ids); GList *gl_paper_get_name_list (void); void gl_paper_free_name_list (GList **names); +gboolean gl_paper_is_id_known (const gchar *id); + glPaper *gl_paper_from_id (const gchar *id); glPaper *gl_paper_from_name (const gchar *name); diff --git a/glabels2/src/template.c b/glabels2/src/template.c index 22e65605..dc67ad1e 100644 --- a/glabels2/src/template.c +++ b/glabels2/src/template.c @@ -149,6 +149,48 @@ gl_template_free_name_list (GList **names) gl_debug (DEBUG_TEMPLATE, "END"); } +/*****************************************************************************/ +/* Register template: if not in current list, add it. */ +/*****************************************************************************/ +void +gl_template_register (const glTemplate *template) +{ + GList *p_tmplt, *pa1; + glTemplate *template1; + + gl_debug (DEBUG_TEMPLATE, "START"); + + for (p_tmplt = templates; p_tmplt != NULL; p_tmplt = p_tmplt->next) { + template1 = (glTemplate *) p_tmplt->data; + + for (pa1=template1->alias; pa1!=NULL; pa1=pa1->next) { + + gl_debug (DEBUG_TEMPLATE, "comparing \"%s\" & \"%s\"", + template->name, pa1->data); + + if (g_strcasecmp (template->name, pa1->data) == 0) { + gl_debug (DEBUG_TEMPLATE, "END (found)"); + return; + } + + } + + } + + if (gl_paper_is_id_known (template->page_size)) { + + gl_debug (DEBUG_TEMPLATE, "adding \"%s\"", template->name); + templates = g_list_prepend (templates, gl_template_dup (template)); + + /* TODO: write to a unique file in .glabels. */ + + } else { + g_warning ("Cannot register new template with unknown page size."); + } + + gl_debug (DEBUG_TEMPLATE, "END"); +} + /*****************************************************************************/ /* Return a template structure from a name. */ /*****************************************************************************/ @@ -191,7 +233,9 @@ gl_template_from_name (const gchar *name) g_strfreev (split_name); gl_debug (DEBUG_TEMPLATE, "END"); - return NULL; + + /* No matching template has been found so return the first template */ + return gl_template_dup ((glTemplate *) templates->data); } /*****************************************************************************/ diff --git a/glabels2/src/template.h b/glabels2/src/template.h index 7d8da895..95bd47f8 100644 --- a/glabels2/src/template.h +++ b/glabels2/src/template.h @@ -151,6 +151,8 @@ void gl_template_init (void); GList *gl_template_get_name_list (const gchar *page_size); void gl_template_free_name_list (GList **names); +void gl_template_register (const glTemplate *template); + glTemplate *gl_template_from_name (const gchar *name); gchar *gl_template_get_name_with_desc (const glTemplate *template); diff --git a/glabels2/src/xml-label-191.c b/glabels2/src/xml-label-191.c index bbf1dc19..09552b5c 100644 --- a/glabels2/src/xml-label-191.c +++ b/glabels2/src/xml-label-191.c @@ -151,6 +151,7 @@ gl_xml_label_191_parse (xmlNodePtr root, *status = XML_LABEL_UNKNOWN_MEDIA; return NULL; } + gl_template_register (template); gl_label_set_template (label, template); gl_template_free (&template); } else if (xmlStrEqual (node->name, "Objects")) { diff --git a/glabels2/src/xml-label.c b/glabels2/src/xml-label.c index 849a74b4..16d6eb3c 100644 --- a/glabels2/src/xml-label.c +++ b/glabels2/src/xml-label.c @@ -329,9 +329,11 @@ xml_parse_label (xmlNodePtr root, if (xmlStrEqual (child_node->name, "Template")) { template = gl_xml_template_parse_template (child_node); if (!template) { + g_object_unref (label); *status = XML_LABEL_UNKNOWN_MEDIA; return NULL; } + gl_template_register (template); gl_label_set_template (label, template); gl_template_free (&template); } else if (xmlStrEqual (child_node->name, "Objects")) { @@ -344,6 +346,9 @@ xml_parse_label (xmlNodePtr root, if (!xmlNodeIsText (child_node)) { g_warning (_("bad node in Document node = \"%s\""), child_node->name); + g_object_unref (label); + *status = XML_LABEL_ERROR_OPEN_PARSE; + return NULL; } } } -- 2.39.5