]> git.sur5r.net Git - glabels/blobdiff - glabels2/libglabels/xml-template.c
2009-09-22 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / libglabels / xml-template.c
index 35cd67713434dc8f14fd4c993ff02fad1dd0fc19..9a880c3c62a70e08bd93fa82468b588207910b44 100644 (file)
@@ -1,41 +1,35 @@
-/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
-
 /*
- *  (LIBGLABELS) Template library for GLABELS
- *
- *  xml-template.c:  template xml module
+ *  xml-template.c
+ *  Copyright (C) 2001-2009  Jim Evins <evins@snaught.com>.
  *
- *  Copyright (C) 2001-2006  Jim Evins <evins@snaught.com>.
+ *  This file is part of libglabels.
  *
- *  This file is part of the LIBGLABELS library.
+ *  libglabels is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU Lesser General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
  *
- *  This library is free software; you can redistribute it and/or
- *  modify it under the terms of the GNU Library General Public
- *  License as published by the Free Software Foundation; either
- *  version 2 of the License, or (at your option) any later version.
- *
- *  This library is distributed in the hope that it will be useful,
+ *  libglabels is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- *  Library General Public License for more details.
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Lesser General Public License for more details.
  *
- *  You should have received a copy of the GNU Library General Public
- *  License along with this library; if not, write to the Free
- *  Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
- *  MA 02111-1307, USA
+ *  You should have received a copy of the GNU Lesser General Public License
+ *  along with libglabels.  If not, see <http://www.gnu.org/licenses/>.
  */
+
 #include <config.h>
 
 #include "xml-template.h"
 
 #include <glib/gi18n.h>
-#include <glib/gmessages.h>
+#include <glib.h>
 #include <string.h>
 #include <libintl.h>
 
 #include "libglabels-private.h"
 
-#include "paper.h"
+#include "db.h"
 #include "xml.h"
 
 /*===========================================*/
@@ -91,7 +85,7 @@ static void  xml_create_markup_circle_node  (const lglTemplateMarkup      *circl
 static void  xml_create_markup_rect_node    (const lglTemplateMarkup      *circle,
                                             xmlNodePtr                    root,
                                             const xmlNsPtr                ns);
-static void  xml_create_alias_node          (const gchar                  *name,
+static void  xml_create_alias_node          (const lglTemplateAlias       *alias,
                                             xmlNodePtr                    root,
                                             const xmlNsPtr                ns);
 
@@ -197,49 +191,71 @@ lgl_xml_template_parse_templates_doc (const xmlDocPtr templates_doc)
 lglTemplate *
 lgl_xml_template_parse_template_node (const xmlNodePtr template_node)
 {
+       gchar                 *brand;
+        gchar                 *part;
        gchar                 *name;
        gchar                 *description;
-       gchar                 *page_size;
+       gchar                 *paper_id;
        gdouble                page_width, page_height;
        lglPaper               *paper = NULL;
        lglTemplate           *template;
        xmlNodePtr             node;
+        gchar                **v;
+
+       brand = lgl_xml_get_prop_string (template_node, "brand", NULL);
+       part  = lgl_xml_get_prop_string (template_node, "part", NULL);
+        if (!brand || !part)
+        {
+                name = lgl_xml_get_prop_string (template_node, "name", NULL);
+                if (name)
+                {
+                        v = g_strsplit (name, " ", 2);
+                        brand = g_strdup (v[0]);
+                        part  = g_strchug (g_strdup (v[1]));
+                        g_free (name);
+                        g_strfreev (v);
+                        
+                }
+                else
+                {
+                       g_message (_("Missing name or brand/part attributes."));
+                }
+        }
 
-       name = lgl_xml_get_prop_string (template_node, "name", NULL);
        description = lgl_xml_get_prop_i18n_string (template_node, "description", NULL);
-       page_size = lgl_xml_get_prop_string (template_node, "size", NULL);
+       paper_id = lgl_xml_get_prop_string (template_node, "size", NULL);
 
-       if (lgl_paper_is_id_other (page_size)) {
+       if (lgl_db_is_paper_id_other (paper_id)) {
 
                page_width = lgl_xml_get_prop_length (template_node, "width", 0);
                page_height = lgl_xml_get_prop_length (template_node, "height", 0);
 
        } else {
-               paper = lgl_paper_from_id (page_size);
+               paper = lgl_db_lookup_paper_from_id (paper_id);
                if (paper == NULL) {
                        /* This should always be an id, but just in case a name
                           slips by! */
                        g_message (_("Unknown page size id \"%s\", trying as name"),
-                                  page_size);
-                       paper = lgl_paper_from_name (page_size);
-                       g_free (page_size);
-                       page_size = g_strdup (paper->id);
+                                  paper_id);
+                       paper = lgl_db_lookup_paper_from_name (paper_id);
+                       g_free (paper_id);
+                       paper_id = g_strdup (paper->id);
                }
                if (paper != NULL) {
                        page_width  = paper->width;
                        page_height = paper->height;
                } else {
+                       page_width  = 612;
+                       page_height = 792;
                        g_message (_("Unknown page size id or name \"%s\""),
-                                  page_size);
+                                  paper_id);
                }
                lgl_paper_free (paper);
                paper = NULL;
        }
 
-       template = lgl_template_new (name,
-                                   description,
-                                   page_size,
-                                   page_width, page_height);
+       template = lgl_template_new (brand, part, description,
+                                     paper_id, page_width, page_height);
 
        for (node = template_node->xmlChildrenNode; node != NULL;
             node = node->next) {
@@ -262,9 +278,10 @@ lgl_xml_template_parse_template_node (const xmlNodePtr template_node)
                }
        }
 
-       g_free (name);
+       g_free (brand);
+       g_free (part);
        g_free (description);
-       g_free (page_size);
+       g_free (paper_id);
 
        return template;
 }
@@ -448,7 +465,7 @@ xml_parse_layout_node (xmlNodePtr          layout_node,
        dx = lgl_xml_get_prop_length (layout_node, "dx", 0);
        dy = lgl_xml_get_prop_length (layout_node, "dy", 0);
 
-       lgl_template_add_layout (frame, lgl_template_layout_new (nx, ny, x0, y0, dx, dy));
+       lgl_template_frame_add_layout (frame, lgl_template_layout_new (nx, ny, x0, y0, dx, dy));
 
        for (node = layout_node->xmlChildrenNode; node != NULL;
             node = node->next) {
@@ -473,7 +490,7 @@ xml_parse_markup_margin_node (xmlNodePtr          markup_node,
 
        size = lgl_xml_get_prop_length (markup_node, "size", 0);
 
-       lgl_template_add_markup (frame, lgl_template_markup_margin_new (size));
+       lgl_template_frame_add_markup (frame, lgl_template_markup_margin_new (size));
 
        for (node = markup_node->xmlChildrenNode; node != NULL;
             node = node->next) {
@@ -501,7 +518,7 @@ xml_parse_markup_line_node (xmlNodePtr          markup_node,
        x2 = lgl_xml_get_prop_length (markup_node, "x2", 0);
        y2 = lgl_xml_get_prop_length (markup_node, "y2", 0);
 
-       lgl_template_add_markup (frame, lgl_template_markup_line_new (x1, y1, x2, y2));
+       lgl_template_frame_add_markup (frame, lgl_template_markup_line_new (x1, y1, x2, y2));
 
        for (node = markup_node->xmlChildrenNode; node != NULL;
             node = node->next) {
@@ -528,7 +545,7 @@ xml_parse_markup_circle_node (xmlNodePtr          markup_node,
        y0 = lgl_xml_get_prop_length (markup_node, "y0", 0);
        r  = lgl_xml_get_prop_length (markup_node, "radius", 0);
 
-       lgl_template_add_markup (frame, lgl_template_markup_circle_new (x0, y0, r));
+       lgl_template_frame_add_markup (frame, lgl_template_markup_circle_new (x0, y0, r));
 
        for (node = markup_node->xmlChildrenNode; node != NULL;
             node = node->next) {
@@ -557,7 +574,7 @@ xml_parse_markup_rect_node (xmlNodePtr          markup_node,
        h  = lgl_xml_get_prop_length (markup_node, "h", 0);
        r  = lgl_xml_get_prop_length (markup_node, "r", 0);
 
-       lgl_template_add_markup (frame, lgl_template_markup_rect_new (x1, y1, w, h, r));
+       lgl_template_frame_add_markup (frame, lgl_template_markup_rect_new (x1, y1, w, h, r));
 
        for (node = markup_node->xmlChildrenNode; node != NULL;
             node = node->next) {
@@ -577,13 +594,36 @@ static void
 xml_parse_alias_node (xmlNodePtr   alias_node,
                      lglTemplate *template)
 {
-       gchar       *name;
-
-       name = lgl_xml_get_prop_string (alias_node, "name", NULL);
-
-       lgl_template_add_alias (template, (gchar *)name);
-
-       g_free (name);
+       gchar             *brand;
+       gchar             *part;
+       gchar             *name;
+        gchar            **v;
+
+       brand = lgl_xml_get_prop_string (alias_node, "brand", NULL);
+       part  = lgl_xml_get_prop_string (alias_node, "part", NULL);
+        if (!brand || !part)
+        {
+                name = lgl_xml_get_prop_string (alias_node, "name", NULL);
+                if (name)
+                {
+                       g_message (_("Missing required \"brand\" or \"part\" attribute, trying deprecated name."));
+                        v = g_strsplit (name, " ", 2);
+                        brand = g_strdup (v[0]);
+                        part  = g_strdup (v[1]);
+                        g_free (name);
+                        g_strfreev (v);
+                        
+                }
+                else
+                {
+                       g_message (_("Name attribute also missing."));
+                }
+        }
+
+       lgl_template_add_alias (template, lgl_template_alias_new (brand, part));
+
+       g_free (brand);
+       g_free (part);
 }
 
 /**
@@ -593,14 +633,16 @@ xml_parse_alias_node (xmlNodePtr   alias_node,
  *
  * Write a list of #lglTemplate structures to a glabels XML template file.
  *
+ * Returns: the number of bytes written or -1 in case of failure
+ *
  */
-void
+gint
 lgl_xml_template_write_templates_to_file (GList       *templates,
                                           const gchar *utf8_filename)
 {
        xmlDocPtr    doc;
        xmlNsPtr     ns;
-       gint         xml_ret;
+       gint         bytes_written;
        GList       *p;
        lglTemplate *template;
        gchar       *filename;
@@ -618,17 +660,17 @@ lgl_xml_template_write_templates_to_file (GList       *templates,
 
        filename = g_filename_from_utf8 (utf8_filename, -1, NULL, NULL, NULL);
        if (!filename)
+        {
                g_message (_("Utf8 conversion error."));
-       else {
+                return -1;
+        }
+       else
+        {
                xmlSetDocCompressMode (doc, 0);
-               xml_ret = xmlSaveFormatFile (filename, doc, TRUE);
+               bytes_written = xmlSaveFormatFile (filename, doc, TRUE);
                xmlFreeDoc (doc);
-               if (xml_ret == -1) {
-
-                       g_message (_("Problem saving xml file."));
-
-               }
                g_free (filename);
+                return bytes_written;
        }
 
 }
@@ -641,18 +683,23 @@ lgl_xml_template_write_templates_to_file (GList       *templates,
  *
  * Write a single #lglTemplate structures to a glabels XML template file.
  *
+ * Returns: the number of bytes written or -1 in case of failure
+ *
  */
-void
+gint
 lgl_xml_template_write_template_to_file (const lglTemplate  *template,
                                          const gchar        *utf8_filename)
 {
        GList     *templates = NULL;
+       gint       bytes_written;
 
        templates = g_list_append (templates, (gpointer)template);
 
-       lgl_xml_template_write_templates_to_file (templates, utf8_filename);
+       bytes_written = lgl_xml_template_write_templates_to_file (templates, utf8_filename);
 
        g_list_free (templates);
+
+        return bytes_written;
 }
 
 
@@ -672,14 +719,17 @@ lgl_xml_template_create_template_node (const lglTemplate *template,
 {
        xmlNodePtr          node;
        GList              *p;
+       lglTemplateAlias   *alias;
        lglTemplateFrame   *frame;
 
        node = xmlNewChild (root, ns, (xmlChar *)"Template", NULL);
 
-       lgl_xml_set_prop_string (node, "name", template->name);
+       lgl_xml_set_prop_string (node, "brand", template->brand);
+       lgl_xml_set_prop_string (node, "part", template->part);
 
-       lgl_xml_set_prop_string (node, "size", template->page_size);
-       if (xmlStrEqual ((xmlChar *)template->page_size, (xmlChar *)"Other")) {
+       lgl_xml_set_prop_string (node, "size", template->paper_id);
+       if (xmlStrEqual ((xmlChar *)template->paper_id, (xmlChar *)"Other"))
+        {
 
                lgl_xml_set_prop_length (node, "width", template->page_width);
                lgl_xml_set_prop_length (node, "height", template->page_height);
@@ -688,20 +738,24 @@ lgl_xml_template_create_template_node (const lglTemplate *template,
 
        lgl_xml_set_prop_string (node, "description", template->description);
 
-       for ( p=template->categories; p != NULL; p=p->next ) {
+       for ( p=template->aliases; p != NULL; p=p->next ) {
+                alias = (lglTemplateAlias *)p->data;
+               if ( !(xmlStrEqual ((xmlChar *)template->brand, (xmlChar *)alias->brand) &&
+                       xmlStrEqual ((xmlChar *)template->part, (xmlChar *)alias->part)) )
+                {
+                       xml_create_alias_node ( alias, node, ns );
+               }
+       }
+       for ( p=template->category_ids; p != NULL; p=p->next )
+        {
                 xml_create_meta_node ( p->data, node, ns );
        }
-       for ( p=template->frames; p != NULL; p=p->next ) {
+       for ( p=template->frames; p != NULL; p=p->next )
+        {
                frame = (lglTemplateFrame *)p->data;
                xml_create_label_node (frame, node, ns);
        }
 
-       for ( p=template->aliases; p != NULL; p=p->next ) {
-               if (!xmlStrEqual ((xmlChar *)template->name, (xmlChar *)p->data)) {
-                       xml_create_alias_node ( p->data, node, ns );
-               }
-       }
-
 }
 
 /*--------------------------------------------------------------------------*/
@@ -897,14 +951,26 @@ xml_create_markup_rect_node (const lglTemplateMarkup *markup,
 /* PRIVATE.  Add XML Template->Alias Node.                                  */
 /*--------------------------------------------------------------------------*/
 static void
-xml_create_alias_node (const gchar      *name,
-                      xmlNodePtr        root,
-                      const xmlNsPtr    ns)
+xml_create_alias_node (const lglTemplateAlias *alias,
+                      xmlNodePtr              root,
+                      const xmlNsPtr          ns)
 {
        xmlNodePtr node;
 
        node = xmlNewChild (root, ns, (xmlChar *)"Alias", NULL);
-       lgl_xml_set_prop_string (node, "name", name);
+
+       lgl_xml_set_prop_string (node, "brand", alias->brand);
+       lgl_xml_set_prop_string (node, "part",  alias->part);
 
 }
 
+
+
+/*
+ * Local Variables:       -- emacs
+ * mode: C                -- emacs
+ * c-basic-offset: 8      -- emacs
+ * tab-width: 8           -- emacs
+ * indent-tabs-mode: nil  -- emacs
+ * End:                   -- emacs
+ */