From: Jim Evins Date: Mon, 28 Apr 2003 02:35:17 +0000 (+0000) Subject: Moved template xml functions to separate module (xml-template.[ch]). X-Git-Tag: glabels-2_3_0~635 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=9b6570c2ad57f9394121cc17175b95f72d43dc4c;p=glabels Moved template xml functions to separate module (xml-template.[ch]). git-svn-id: https://glabels.svn.sourceforge.net/svnroot/glabels/trunk@254 f5e0f49d-192f-0410-a22d-a8d8700d0965 --- diff --git a/glabels2/po/POTFILES.in b/glabels2/po/POTFILES.in index e5c4af76..20685685 100644 --- a/glabels2/po/POTFILES.in +++ b/glabels2/po/POTFILES.in @@ -77,6 +77,8 @@ src/xml-label.c src/xml-label.h src/xml-label-04.c src/xml-label-04.h +src/xml-template.c +src/xml-template.h src/xml.c src/xml.h src/pixbuf-cache.c diff --git a/glabels2/src/Makefile.am b/glabels2/src/Makefile.am index 244ff6b7..b2c8b384 100644 --- a/glabels2/src/Makefile.am +++ b/glabels2/src/Makefile.am @@ -118,6 +118,8 @@ glabels_SOURCES = \ xml-label.h \ xml-label-04.c \ xml-label-04.h \ + xml-template.c \ + xml-template.h \ xml.c \ xml.h \ pixbuf-cache.c \ @@ -214,6 +216,8 @@ glabels_batch_SOURCES = \ xml-label.h \ xml-label-04.c \ xml-label-04.h \ + xml-template.c \ + xml-template.h \ xml.c \ xml.h \ pixbuf-cache.c \ diff --git a/glabels2/src/template.c b/glabels2/src/template.c index 374d6fd0..de953614 100644 --- a/glabels2/src/template.c +++ b/glabels2/src/template.c @@ -29,6 +29,7 @@ #include "util.h" #include "xml.h" #include "template.h" +#include "xml-template.h" #include "debug.h" @@ -59,52 +60,13 @@ static GList *read_templates (void); static gchar *get_home_data_dir (void); static GList *read_template_files_from_dir (GList *templates, const gchar *dirname); -static GList *read_templates_from_file (GList *templates, - gchar *xml_filename); - -static void xml_parse_label (xmlNodePtr label_node, - glTemplate *template); -static void xml_parse_layout (xmlNodePtr layout_node, - glTemplate *template); -static void xml_parse_markup (xmlNodePtr markup_node, - glTemplate *template); -static void xml_parse_alias (xmlNodePtr alias_node, - glTemplate *template); - -static void xml_add_label (const glTemplate *template, - xmlNodePtr root, - xmlNsPtr ns); -static void xml_add_layout (glTemplateLayout *layout, - xmlNodePtr root, - xmlNsPtr ns); -static void xml_add_markup_margin (glTemplateMarkupMargin *margin, - xmlNodePtr root, - xmlNsPtr ns); -static void xml_add_markup_line (glTemplateMarkupLine *line, - xmlNodePtr root, - xmlNsPtr ns); -static void xml_add_alias (gchar *name, - xmlNodePtr root, - xmlNsPtr ns); - static gint compare_origins (gconstpointer a, gconstpointer b, gpointer user_data); -static glTemplateLayout *layout_new (gdouble nx, - gdouble ny, - gdouble x0, - gdouble y0, - gdouble dx, - gdouble dy); static glTemplateLayout *layout_dup (glTemplateLayout *orig_layout); static void layout_free (glTemplateLayout **layout); -static glTemplateMarkup *markup_margin_new (gdouble size); -static glTemplateMarkup *markup_line_new (gdouble x1, - gdouble y1, - gdouble x2, - gdouble y2); static glTemplateMarkup *markup_dup (glTemplateMarkup *orig_markup); static void markup_free (glTemplateMarkup **markup); @@ -392,11 +354,11 @@ template_full_page (const gchar *page_size) template->label.any.layouts = g_list_append (template->label.any.layouts, - layout_new (1, 1, 0., 0., 0., 0.)); + gl_template_layout_new (1, 1, 0., 0., 0., 0.)); template->label.any.markups = g_list_append (template->label.any.markups, - markup_margin_new (5.0)); + gl_template_markup_margin_new (5.0)); return template; } @@ -479,8 +441,8 @@ read_template_files_from_dir (GList *templates, full_filename = g_build_filename (dirname, filename, NULL); templates = - read_templates_from_file (templates, - full_filename); + gl_xml_template_read_templates_from_file (templates, + full_filename); g_free (full_filename); } @@ -495,473 +457,6 @@ read_template_files_from_dir (GList *templates, return templates; } -/*--------------------------------------------------------------------------*/ -/* PRIVATE. Read templates from template file. */ -/*--------------------------------------------------------------------------*/ -static GList * -read_templates_from_file (GList *templates, - gchar *xml_filename) -{ - xmlDocPtr doc; - xmlNodePtr root, node; - glTemplate *template; - - gl_debug (DEBUG_TEMPLATE, "START"); - - doc = xmlParseFile (xml_filename); - if (!doc) { - g_warning ("\"%s\" is not a glabels template file (not XML)", - xml_filename); - return templates; - } - - root = xmlDocGetRootElement (doc); - if (!root || !root->name) { - g_warning ("\"%s\" is not a glabels template file (no root node)", - xml_filename); - xmlFreeDoc (doc); - return templates; - } - if (g_strcasecmp (root->name, "glabels-templates") != 0) { - g_warning ("\"%s\" is not a glabels template file (wrong root node)", - xml_filename); - xmlFreeDoc (doc); - return templates; - } - - for (node = root->xmlChildrenNode; node != NULL; node = node->next) { - - if (g_strcasecmp (node->name, "Sheet") == 0) { - template = gl_template_xml_parse_sheet (node); - templates = g_list_append (templates, template); - } else { - if ( !xmlNodeIsText(node) ) { - if (g_strcasecmp (node->name,"comment") != 0) { - g_warning ("bad node = \"%s\"",node->name); - } - } - } - } - - xmlFreeDoc (doc); - - gl_debug (DEBUG_TEMPLATE, "END"); - return templates; -} - -/*****************************************************************************/ -/* Parse XML template Node. */ -/*****************************************************************************/ -glTemplate * -gl_template_xml_parse_sheet (xmlNodePtr sheet_node) -{ - glTemplate *template; - xmlNodePtr node; - const GnomePrintPaper *paper; - - gl_debug (DEBUG_TEMPLATE, "START"); - - template = g_new0 (glTemplate, 1); - - template->name = g_list_append (template->name, - xmlGetProp (sheet_node, "name")); - gl_debug (DEBUG_TEMPLATE, "Sheet = %s", template->name->data); - - template->page_size = xmlGetProp (sheet_node, "size"); - if ( strcmp (template->page_size,"US-Letter") == 0 ) { - template->page_size = "US Letter"; - } - if (g_strcasecmp (template->page_size, "Other") == 0) { - - template->page_width = gl_xml_get_prop_double (sheet_node, "width", 0); - template->page_height = gl_xml_get_prop_double (sheet_node, "height", 0); - - } else { - paper = gnome_print_paper_get_by_name (template->page_size); - if (paper != NULL) { - template->page_width = paper->width; - template->page_height = paper->height; - } else { - g_warning (_("Unknown page size \"%s\""), template->page_size); - } - } - - template->description = xmlGetProp (sheet_node, "description"); - - for (node = sheet_node->xmlChildrenNode; node != NULL; - node = node->next) { - if (g_strcasecmp (node->name, "Label") == 0) { - xml_parse_label (node, template); - } else if (g_strcasecmp (node->name, "Alias") == 0) { - xml_parse_alias (node, template); - } else { - if (g_strcasecmp (node->name, "text") != 0) { - g_warning ("bad node = \"%s\"", node->name); - } - } - } - - gl_debug (DEBUG_TEMPLATE, "END"); - - return template; -} - -/*--------------------------------------------------------------------------*/ -/* PRIVATE. Parse XML Sheet->Label Node. */ -/*--------------------------------------------------------------------------*/ -static void -xml_parse_label (xmlNodePtr label_node, - glTemplate *template) -{ - xmlNodePtr node; - gchar *style; - - gl_debug (DEBUG_TEMPLATE, "START"); - - style = xmlGetProp (label_node, "style"); - if (g_strcasecmp (style, "rectangle") == 0) { - template->label.style = GL_TEMPLATE_STYLE_RECT; - } else if (g_strcasecmp (style, "round") == 0) { - template->label.style = GL_TEMPLATE_STYLE_ROUND; - } else if (g_strcasecmp (style, "cd") == 0) { - template->label.style = GL_TEMPLATE_STYLE_CD; - } else { - template->label.style = GL_TEMPLATE_STYLE_RECT; - g_warning ("Unknown label style in template"); - } - g_free (style); - - switch (template->label.style) { - - case GL_TEMPLATE_STYLE_RECT: - template->label.rect.w = gl_xml_get_prop_double (label_node, "width", 0); - template->label.rect.h = gl_xml_get_prop_double (label_node, "height", 0); - template->label.rect.r = gl_xml_get_prop_double (label_node, "round", 0); - break; - - case GL_TEMPLATE_STYLE_ROUND: - template->label.round.r = gl_xml_get_prop_double (label_node, "radius", 0); - break; - - case GL_TEMPLATE_STYLE_CD: - template->label.cd.r1 = gl_xml_get_prop_double (label_node, "radius", 0); - template->label.cd.r2 = gl_xml_get_prop_double (label_node, "hole", 0); - template->label.cd.w = gl_xml_get_prop_double (label_node, "width", 0); - template->label.cd.h = gl_xml_get_prop_double (label_node, "height", 0); - break; - - default: - break; - - } - - for (node = label_node->xmlChildrenNode; node != NULL; - node = node->next) { - if (g_strcasecmp (node->name, "Layout") == 0) { - xml_parse_layout (node, template); - } else if (g_strcasecmp (node->name, "Markup") == 0) { - xml_parse_markup (node, template); - } else if (g_strcasecmp (node->name, "text") != 0) { - g_warning ("bad node = \"%s\"", node->name); - } - } - - gl_debug (DEBUG_TEMPLATE, "END"); -} - -/*--------------------------------------------------------------------------*/ -/* PRIVATE. Parse XML Sheet->Label->Layout Node. */ -/*--------------------------------------------------------------------------*/ -static void -xml_parse_layout (xmlNodePtr layout_node, - glTemplate *template) -{ - gint nx, ny; - gdouble x0, y0, dx, dy; - xmlNodePtr node; - - gl_debug (DEBUG_TEMPLATE, "START"); - - nx = gl_xml_get_prop_int (layout_node, "nx", 1); - ny = gl_xml_get_prop_int (layout_node, "ny", 1); - - x0 = gl_xml_get_prop_double (layout_node, "x0", 0); - y0 = gl_xml_get_prop_double (layout_node, "y0", 0); - - dx = gl_xml_get_prop_double (layout_node, "dx", 0); - dy = gl_xml_get_prop_double (layout_node, "dy", 0); - - for (node = layout_node->xmlChildrenNode; node != NULL; - node = node->next) { - if (g_strcasecmp (node->name, "text") != 0) { - g_warning ("bad node = \"%s\"", node->name); - } - } - - template->label.any.layouts = - g_list_append (template->label.any.layouts, - layout_new (nx, ny, x0, y0, dx, dy)); - - gl_debug (DEBUG_TEMPLATE, "END"); -} - -/*--------------------------------------------------------------------------*/ -/* PRIVATE. Parse XML Sheet->Label->Markup Node. */ -/*--------------------------------------------------------------------------*/ -static void -xml_parse_markup (xmlNodePtr markup_node, - glTemplate *template) -{ - gchar *type; - gdouble size; - gdouble x1, y1, x2, y2; - xmlNodePtr node; - - gl_debug (DEBUG_TEMPLATE, "START"); - - type = xmlGetProp (markup_node, "type"); - if (g_strcasecmp (type, "margin") == 0) { - - size = gl_xml_get_prop_double (markup_node, "size", 0); - - template->label.any.markups = - g_list_append (template->label.any.markups, - markup_margin_new (size)); - - } else if (g_strcasecmp (type, "line") == 0) { - - x1 = gl_xml_get_prop_double (markup_node, "x1", 0); - y1 = gl_xml_get_prop_double (markup_node, "y1", 0); - x2 = gl_xml_get_prop_double (markup_node, "x2", 0); - y2 = gl_xml_get_prop_double (markup_node, "y2", 0); - - template->label.any.markups = - g_list_append (template->label.any.markups, - markup_line_new (x1, y1, x2, y2)); - } - g_free (type); - - for (node = markup_node->xmlChildrenNode; node != NULL; - node = node->next) { - if (g_strcasecmp (node->name, "text") != 0) { - g_warning ("bad node = \"%s\"", node->name); - } - } - - gl_debug (DEBUG_TEMPLATE, "END"); -} - -/*--------------------------------------------------------------------------*/ -/* PRIVATE. Parse XML Sheet->Alias Node. */ -/*--------------------------------------------------------------------------*/ -static void -xml_parse_alias (xmlNodePtr alias_node, - glTemplate *template) -{ - gl_debug (DEBUG_TEMPLATE, "START"); - - template->name = g_list_append (template->name, - xmlGetProp (alias_node, "name")); - - gl_debug (DEBUG_TEMPLATE, "END"); -} - -/****************************************************************************/ -/* Add XML Template Node */ -/****************************************************************************/ -void -gl_template_xml_add_sheet (const glTemplate *template, - xmlNodePtr root, - xmlNsPtr ns) -{ - xmlNodePtr node; - GList *p; - - gl_debug (DEBUG_TEMPLATE, "START"); - - node = xmlNewChild (root, ns, "Sheet", NULL); - - xmlSetProp (node, "name", template->name->data); - - xmlSetProp (node, "size", template->page_size); - if (g_strcasecmp (template->page_size, "Other") == 0) { - - gl_xml_set_prop_double (node, "width", template->page_width); - gl_xml_set_prop_double (node, "height", template->page_height); - - } - - xmlSetProp (node, "description", template->description); - - xml_add_label (template, node, ns); - - for ( p=template->name->next; p != NULL; p=p->next ) { - xml_add_alias( p->data, node, ns ); - } - - gl_debug (DEBUG_TEMPLATE, "END"); -} - -/*--------------------------------------------------------------------------*/ -/* PRIVATE. Add XML Sheet->Label Node. */ -/*--------------------------------------------------------------------------*/ -static void -xml_add_label (const glTemplate *template, - xmlNodePtr root, - xmlNsPtr ns) -{ - xmlNodePtr node; - GList *p; - glTemplateMarkup *markup; - glTemplateLayout *layout; - - gl_debug (DEBUG_TEMPLATE, "START"); - - node = xmlNewChild(root, ns, "Label", NULL); - - xmlSetProp (node, "id", "0"); - - switch (template->label.style) { - - case GL_TEMPLATE_STYLE_RECT: - xmlSetProp (node, "style", "rectangle"); - gl_xml_set_prop_double (node, "width", template->label.rect.w); - gl_xml_set_prop_double (node, "height", template->label.rect.h); - gl_xml_set_prop_double (node, "round", template->label.rect.r); - break; - - case GL_TEMPLATE_STYLE_ROUND: - xmlSetProp (node, "style", "round"); - gl_xml_set_prop_double (node, "radius", template->label.round.r); - break; - - case GL_TEMPLATE_STYLE_CD: - xmlSetProp (node, "style", "cd"); - gl_xml_set_prop_double (node, "radius", template->label.cd.r1); - gl_xml_set_prop_double (node, "hole", template->label.cd.r2); - if (template->label.cd.w != 0.0) { - gl_xml_set_prop_double (node, "width", template->label.cd.w); - } - if (template->label.cd.h != 0.0) { - gl_xml_set_prop_double (node, "height", template->label.cd.h); - } - break; - - default: - g_warning ("Unknown label style"); - break; - - } - - for ( p=template->label.any.markups; p != NULL; p=p->next ) { - markup = (glTemplateMarkup *)p->data; - switch (markup->type) { - case GL_TEMPLATE_MARKUP_MARGIN: - xml_add_markup_margin ((glTemplateMarkupMargin *)markup, - node, ns); - break; - case GL_TEMPLATE_MARKUP_LINE: - xml_add_markup_line ((glTemplateMarkupLine *)markup, - node, ns); - break; - default: - g_warning ("Unknown markup type"); - break; - } - } - - for ( p=template->label.any.layouts; p != NULL; p=p->next ) { - layout = (glTemplateLayout *)p->data; - xml_add_layout (layout, node, ns); - } - - gl_debug (DEBUG_TEMPLATE, "END"); -} - -/*--------------------------------------------------------------------------*/ -/* PRIVATE. Add XML Sheet->Label->Layout Node. */ -/*--------------------------------------------------------------------------*/ -static void -xml_add_layout (glTemplateLayout *layout, - xmlNodePtr root, - xmlNsPtr ns) -{ - xmlNodePtr node; - - gl_debug (DEBUG_TEMPLATE, "START"); - - node = xmlNewChild(root, ns, "Layout", NULL); - gl_xml_set_prop_int (node, "nx", layout->nx); - gl_xml_set_prop_int (node, "ny", layout->ny); - gl_xml_set_prop_double (node, "x0", layout->x0); - gl_xml_set_prop_double (node, "y0", layout->y0); - gl_xml_set_prop_double (node, "dx", layout->dx); - gl_xml_set_prop_double (node, "dy", layout->dy); - - gl_debug (DEBUG_TEMPLATE, "END"); -} - -/*--------------------------------------------------------------------------*/ -/* PRIVATE. Add XML Sheet->Label->Markup (margin) Node. */ -/*--------------------------------------------------------------------------*/ -static void -xml_add_markup_margin (glTemplateMarkupMargin *margin, - xmlNodePtr root, - xmlNsPtr ns) -{ - xmlNodePtr node; - - gl_debug (DEBUG_TEMPLATE, "START"); - - node = xmlNewChild(root, ns, "Markup", NULL); - xmlSetProp (node, "type", "margin"); - - gl_xml_set_prop_double (node, "size", margin->size); - - gl_debug (DEBUG_TEMPLATE, "END"); -} - -/*--------------------------------------------------------------------------*/ -/* PRIVATE. Add XML Sheet->Label->Markup (line) Node. */ -/*--------------------------------------------------------------------------*/ -static void -xml_add_markup_line (glTemplateMarkupLine *line, - xmlNodePtr root, - xmlNsPtr ns) -{ - xmlNodePtr node; - - gl_debug (DEBUG_TEMPLATE, "START"); - - node = xmlNewChild(root, ns, "Markup", NULL); - xmlSetProp (node, "type", "line"); - - gl_xml_set_prop_double (node, "x1", line->x1); - gl_xml_set_prop_double (node, "y1", line->y1); - gl_xml_set_prop_double (node, "x2", line->x2); - gl_xml_set_prop_double (node, "y2", line->y2); - - gl_debug (DEBUG_TEMPLATE, "END"); -} - -/*--------------------------------------------------------------------------*/ -/* PRIVATE. Add XML Sheet->Alias Node. */ -/*--------------------------------------------------------------------------*/ -static void -xml_add_alias (gchar *name, - xmlNodePtr root, - xmlNsPtr ns) -{ - xmlNodePtr node; - - gl_debug (DEBUG_TEMPLATE, "START"); - - node = xmlNewChild (root, ns, "Alias", NULL); - xmlSetProp (node, "name", name); - - gl_debug (DEBUG_TEMPLATE, "END"); -} - /****************************************************************************/ /* Get label size description. */ /****************************************************************************/ @@ -1167,16 +662,16 @@ gl_template_get_layout_desc (const glTemplate *template) return string; } -/*--------------------------------------------------------------------------*/ -/* PRIVATE. Create new layout structure. */ -/*--------------------------------------------------------------------------*/ -static glTemplateLayout * -layout_new (gdouble nx, - gdouble ny, - gdouble x0, - gdouble y0, - gdouble dx, - gdouble dy) +/*****************************************************************************/ +/* Create new layout structure. */ +/*****************************************************************************/ +glTemplateLayout * +gl_template_layout_new (gdouble nx, + gdouble ny, + gdouble x0, + gdouble y0, + gdouble dx, + gdouble dy) { glTemplateLayout *layout; @@ -1218,11 +713,11 @@ layout_free (glTemplateLayout **layout) *layout = NULL; } -/*--------------------------------------------------------------------------*/ -/* PRIVATE. Create new margin markup structure. */ -/*--------------------------------------------------------------------------*/ -static glTemplateMarkup * -markup_margin_new (gdouble size) +/*****************************************************************************/ +/* Create new margin markup structure. */ +/*****************************************************************************/ +glTemplateMarkup * +gl_template_markup_margin_new (gdouble size) { glTemplateMarkup *markup; @@ -1234,14 +729,14 @@ markup_margin_new (gdouble size) return markup; } -/*--------------------------------------------------------------------------*/ -/* PRIVATE. Create new margin line structure. */ -/*--------------------------------------------------------------------------*/ -static glTemplateMarkup * -markup_line_new (gdouble x1, - gdouble y1, - gdouble x2, - gdouble y2) +/*****************************************************************************/ +/* Create new margin line structure. */ +/*****************************************************************************/ +glTemplateMarkup * +gl_template_markup_line_new (gdouble x1, + gdouble y1, + gdouble x2, + gdouble y2) { glTemplateMarkup *markup; diff --git a/glabels2/src/template.h b/glabels2/src/template.h index d9e596a0..43a25f75 100644 --- a/glabels2/src/template.h +++ b/glabels2/src/template.h @@ -164,6 +164,20 @@ gint gl_template_get_n_labels (const glTemplate *templat glTemplateOrigin *gl_template_get_origins (const glTemplate *template); gchar *gl_template_get_layout_desc (const glTemplate *template); +glTemplateLayout *gl_template_layout_new (gdouble nx, + gdouble ny, + gdouble x0, + gdouble y0, + gdouble dx, + gdouble dy); + +glTemplateMarkup *gl_template_markup_margin_new (gdouble size); +glTemplateMarkup *gl_template_markup_line_new (gdouble x1, + gdouble y1, + gdouble x2, + gdouble y2); + + G_END_DECLS #endif diff --git a/glabels2/src/xml-label.c b/glabels2/src/xml-label.c index 37f7f71e..15de4064 100644 --- a/glabels2/src/xml-label.c +++ b/glabels2/src/xml-label.c @@ -40,6 +40,7 @@ #include "base64.h" #include "xml-label.h" #include "xml-label-04.h" +#include "xml-template.h" #include "xml.h" #include "util.h" @@ -302,7 +303,7 @@ xml_parse_label (xmlNodePtr root, for (node = root->xmlChildrenNode; node != NULL; node = node->next) { if (g_strcasecmp (node->name, "Sheet") == 0) { - template = gl_template_xml_parse_sheet (node); + template = gl_xml_template_parse_sheet (node); if (!template) { *status = XML_LABEL_UNKNOWN_MEDIA; return NULL; @@ -884,7 +885,7 @@ xml_label_to_doc (glLabel *label, xmlSetNs (doc->xmlRootNode, ns); template = gl_label_get_template (label); - gl_template_xml_add_sheet (template, doc->xmlRootNode, ns); + gl_xml_template_add_sheet (template, doc->xmlRootNode, ns); xml_create_objects (doc->xmlRootNode, ns, label); diff --git a/glabels2/src/xml-template.c b/glabels2/src/xml-template.c new file mode 100644 index 00000000..5fc3b22e --- /dev/null +++ b/glabels2/src/xml-template.c @@ -0,0 +1,537 @@ +/* + * (GLABELS) Label and Business Card Creation program for GNOME + * + * xml-template.c: template xml module + * + * Copyright (C) 2001-2003 Jim Evins . + * + * 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * 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 + */ + +#include + +#include +#include + +#include "util.h" +#include "xml.h" +#include "xml-template.h" + +#include "debug.h" + +/*===========================================*/ +/* Private types */ +/*===========================================*/ + +/*===========================================*/ +/* Private globals */ +/*===========================================*/ + +/*===========================================*/ +/* Local function prototypes */ +/*===========================================*/ +static void xml_parse_label (xmlNodePtr label_node, + glTemplate *template); +static void xml_parse_layout (xmlNodePtr layout_node, + glTemplate *template); +static void xml_parse_markup (xmlNodePtr markup_node, + glTemplate *template); +static void xml_parse_alias (xmlNodePtr alias_node, + glTemplate *template); + +static void xml_add_label (const glTemplate *template, + xmlNodePtr root, + xmlNsPtr ns); +static void xml_add_layout (glTemplateLayout *layout, + xmlNodePtr root, + xmlNsPtr ns); +static void xml_add_markup_margin (glTemplateMarkupMargin *margin, + xmlNodePtr root, + xmlNsPtr ns); +static void xml_add_markup_line (glTemplateMarkupLine *line, + xmlNodePtr root, + xmlNsPtr ns); +static void xml_add_alias (gchar *name, + xmlNodePtr root, + xmlNsPtr ns); + + +/*****************************************************************************/ +/* Read templates from template file. */ +/*****************************************************************************/ +GList * +gl_xml_template_read_templates_from_file (GList *templates, + gchar *xml_filename) +{ + xmlDocPtr doc; + xmlNodePtr root, node; + glTemplate *template; + + gl_debug (DEBUG_TEMPLATE, "START"); + + doc = xmlParseFile (xml_filename); + if (!doc) { + g_warning ("\"%s\" is not a glabels template file (not XML)", + xml_filename); + return templates; + } + + root = xmlDocGetRootElement (doc); + if (!root || !root->name) { + g_warning ("\"%s\" is not a glabels template file (no root node)", + xml_filename); + xmlFreeDoc (doc); + return templates; + } + if (g_strcasecmp (root->name, "glabels-templates") != 0) { + g_warning ("\"%s\" is not a glabels template file (wrong root node)", + xml_filename); + xmlFreeDoc (doc); + return templates; + } + + for (node = root->xmlChildrenNode; node != NULL; node = node->next) { + + if (g_strcasecmp (node->name, "Sheet") == 0) { + template = gl_xml_template_parse_sheet (node); + templates = g_list_append (templates, template); + } else { + if ( !xmlNodeIsText(node) ) { + if (g_strcasecmp (node->name,"comment") != 0) { + g_warning ("bad node = \"%s\"",node->name); + } + } + } + } + + xmlFreeDoc (doc); + + gl_debug (DEBUG_TEMPLATE, "END"); + return templates; +} + +/*****************************************************************************/ +/* Parse XML template Node. */ +/*****************************************************************************/ +glTemplate * +gl_xml_template_parse_sheet (xmlNodePtr sheet_node) +{ + glTemplate *template; + xmlNodePtr node; + const GnomePrintPaper *paper; + + gl_debug (DEBUG_TEMPLATE, "START"); + + template = g_new0 (glTemplate, 1); + + template->name = g_list_append (template->name, + xmlGetProp (sheet_node, "name")); + gl_debug (DEBUG_TEMPLATE, "Sheet = %s", template->name->data); + + template->page_size = xmlGetProp (sheet_node, "size"); + if ( strcmp (template->page_size,"US-Letter") == 0 ) { + template->page_size = "US Letter"; + } + if (g_strcasecmp (template->page_size, "Other") == 0) { + + template->page_width = gl_xml_get_prop_double (sheet_node, "width", 0); + template->page_height = gl_xml_get_prop_double (sheet_node, "height", 0); + + } else { + paper = gnome_print_paper_get_by_name (template->page_size); + if (paper != NULL) { + template->page_width = paper->width; + template->page_height = paper->height; + } else { + g_warning (_("Unknown page size \"%s\""), template->page_size); + } + } + + template->description = xmlGetProp (sheet_node, "description"); + + for (node = sheet_node->xmlChildrenNode; node != NULL; + node = node->next) { + if (g_strcasecmp (node->name, "Label") == 0) { + xml_parse_label (node, template); + } else if (g_strcasecmp (node->name, "Alias") == 0) { + xml_parse_alias (node, template); + } else { + if (g_strcasecmp (node->name, "text") != 0) { + g_warning ("bad node = \"%s\"", node->name); + } + } + } + + gl_debug (DEBUG_TEMPLATE, "END"); + + return template; +} + +/*--------------------------------------------------------------------------*/ +/* PRIVATE. Parse XML Sheet->Label Node. */ +/*--------------------------------------------------------------------------*/ +static void +xml_parse_label (xmlNodePtr label_node, + glTemplate *template) +{ + xmlNodePtr node; + gchar *style; + + gl_debug (DEBUG_TEMPLATE, "START"); + + style = xmlGetProp (label_node, "style"); + if (g_strcasecmp (style, "rectangle") == 0) { + template->label.style = GL_TEMPLATE_STYLE_RECT; + } else if (g_strcasecmp (style, "round") == 0) { + template->label.style = GL_TEMPLATE_STYLE_ROUND; + } else if (g_strcasecmp (style, "cd") == 0) { + template->label.style = GL_TEMPLATE_STYLE_CD; + } else { + template->label.style = GL_TEMPLATE_STYLE_RECT; + g_warning ("Unknown label style in template"); + } + g_free (style); + + switch (template->label.style) { + + case GL_TEMPLATE_STYLE_RECT: + template->label.rect.w = gl_xml_get_prop_double (label_node, "width", 0); + template->label.rect.h = gl_xml_get_prop_double (label_node, "height", 0); + template->label.rect.r = gl_xml_get_prop_double (label_node, "round", 0); + break; + + case GL_TEMPLATE_STYLE_ROUND: + template->label.round.r = gl_xml_get_prop_double (label_node, "radius", 0); + break; + + case GL_TEMPLATE_STYLE_CD: + template->label.cd.r1 = gl_xml_get_prop_double (label_node, "radius", 0); + template->label.cd.r2 = gl_xml_get_prop_double (label_node, "hole", 0); + template->label.cd.w = gl_xml_get_prop_double (label_node, "width", 0); + template->label.cd.h = gl_xml_get_prop_double (label_node, "height", 0); + break; + + default: + break; + + } + + for (node = label_node->xmlChildrenNode; node != NULL; + node = node->next) { + if (g_strcasecmp (node->name, "Layout") == 0) { + xml_parse_layout (node, template); + } else if (g_strcasecmp (node->name, "Markup") == 0) { + xml_parse_markup (node, template); + } else if (g_strcasecmp (node->name, "text") != 0) { + g_warning ("bad node = \"%s\"", node->name); + } + } + + gl_debug (DEBUG_TEMPLATE, "END"); +} + +/*--------------------------------------------------------------------------*/ +/* PRIVATE. Parse XML Sheet->Label->Layout Node. */ +/*--------------------------------------------------------------------------*/ +static void +xml_parse_layout (xmlNodePtr layout_node, + glTemplate *template) +{ + gint nx, ny; + gdouble x0, y0, dx, dy; + xmlNodePtr node; + + gl_debug (DEBUG_TEMPLATE, "START"); + + nx = gl_xml_get_prop_int (layout_node, "nx", 1); + ny = gl_xml_get_prop_int (layout_node, "ny", 1); + + x0 = gl_xml_get_prop_double (layout_node, "x0", 0); + y0 = gl_xml_get_prop_double (layout_node, "y0", 0); + + dx = gl_xml_get_prop_double (layout_node, "dx", 0); + dy = gl_xml_get_prop_double (layout_node, "dy", 0); + + for (node = layout_node->xmlChildrenNode; node != NULL; + node = node->next) { + if (g_strcasecmp (node->name, "text") != 0) { + g_warning ("bad node = \"%s\"", node->name); + } + } + + template->label.any.layouts = + g_list_append (template->label.any.layouts, + gl_template_layout_new (nx, ny, x0, y0, dx, dy)); + + gl_debug (DEBUG_TEMPLATE, "END"); +} + +/*--------------------------------------------------------------------------*/ +/* PRIVATE. Parse XML Sheet->Label->Markup Node. */ +/*--------------------------------------------------------------------------*/ +static void +xml_parse_markup (xmlNodePtr markup_node, + glTemplate *template) +{ + gchar *type; + gdouble size; + gdouble x1, y1, x2, y2; + xmlNodePtr node; + + gl_debug (DEBUG_TEMPLATE, "START"); + + type = xmlGetProp (markup_node, "type"); + if (g_strcasecmp (type, "margin") == 0) { + + size = gl_xml_get_prop_double (markup_node, "size", 0); + + template->label.any.markups = + g_list_append (template->label.any.markups, + gl_template_markup_margin_new (size)); + + } else if (g_strcasecmp (type, "line") == 0) { + + x1 = gl_xml_get_prop_double (markup_node, "x1", 0); + y1 = gl_xml_get_prop_double (markup_node, "y1", 0); + x2 = gl_xml_get_prop_double (markup_node, "x2", 0); + y2 = gl_xml_get_prop_double (markup_node, "y2", 0); + + template->label.any.markups = + g_list_append (template->label.any.markups, + gl_template_markup_line_new (x1, y1, x2, y2)); + } + g_free (type); + + for (node = markup_node->xmlChildrenNode; node != NULL; + node = node->next) { + if (g_strcasecmp (node->name, "text") != 0) { + g_warning ("bad node = \"%s\"", node->name); + } + } + + gl_debug (DEBUG_TEMPLATE, "END"); +} + +/*--------------------------------------------------------------------------*/ +/* PRIVATE. Parse XML Sheet->Alias Node. */ +/*--------------------------------------------------------------------------*/ +static void +xml_parse_alias (xmlNodePtr alias_node, + glTemplate *template) +{ + gl_debug (DEBUG_TEMPLATE, "START"); + + template->name = g_list_append (template->name, + xmlGetProp (alias_node, "name")); + + gl_debug (DEBUG_TEMPLATE, "END"); +} + +/****************************************************************************/ +/* Add XML Template Node */ +/****************************************************************************/ +void +gl_xml_template_add_sheet (const glTemplate *template, + xmlNodePtr root, + xmlNsPtr ns) +{ + xmlNodePtr node; + GList *p; + + gl_debug (DEBUG_TEMPLATE, "START"); + + node = xmlNewChild (root, ns, "Sheet", NULL); + + xmlSetProp (node, "name", template->name->data); + + xmlSetProp (node, "size", template->page_size); + if (g_strcasecmp (template->page_size, "Other") == 0) { + + gl_xml_set_prop_double (node, "width", template->page_width); + gl_xml_set_prop_double (node, "height", template->page_height); + + } + + xmlSetProp (node, "description", template->description); + + xml_add_label (template, node, ns); + + for ( p=template->name->next; p != NULL; p=p->next ) { + xml_add_alias( p->data, node, ns ); + } + + gl_debug (DEBUG_TEMPLATE, "END"); +} + +/*--------------------------------------------------------------------------*/ +/* PRIVATE. Add XML Sheet->Label Node. */ +/*--------------------------------------------------------------------------*/ +static void +xml_add_label (const glTemplate *template, + xmlNodePtr root, + xmlNsPtr ns) +{ + xmlNodePtr node; + GList *p; + glTemplateMarkup *markup; + glTemplateLayout *layout; + + gl_debug (DEBUG_TEMPLATE, "START"); + + node = xmlNewChild(root, ns, "Label", NULL); + + xmlSetProp (node, "id", "0"); + + switch (template->label.style) { + + case GL_TEMPLATE_STYLE_RECT: + xmlSetProp (node, "style", "rectangle"); + gl_xml_set_prop_double (node, "width", template->label.rect.w); + gl_xml_set_prop_double (node, "height", template->label.rect.h); + gl_xml_set_prop_double (node, "round", template->label.rect.r); + break; + + case GL_TEMPLATE_STYLE_ROUND: + xmlSetProp (node, "style", "round"); + gl_xml_set_prop_double (node, "radius", template->label.round.r); + break; + + case GL_TEMPLATE_STYLE_CD: + xmlSetProp (node, "style", "cd"); + gl_xml_set_prop_double (node, "radius", template->label.cd.r1); + gl_xml_set_prop_double (node, "hole", template->label.cd.r2); + if (template->label.cd.w != 0.0) { + gl_xml_set_prop_double (node, "width", template->label.cd.w); + } + if (template->label.cd.h != 0.0) { + gl_xml_set_prop_double (node, "height", template->label.cd.h); + } + break; + + default: + g_warning ("Unknown label style"); + break; + + } + + for ( p=template->label.any.markups; p != NULL; p=p->next ) { + markup = (glTemplateMarkup *)p->data; + switch (markup->type) { + case GL_TEMPLATE_MARKUP_MARGIN: + xml_add_markup_margin ((glTemplateMarkupMargin *)markup, + node, ns); + break; + case GL_TEMPLATE_MARKUP_LINE: + xml_add_markup_line ((glTemplateMarkupLine *)markup, + node, ns); + break; + default: + g_warning ("Unknown markup type"); + break; + } + } + + for ( p=template->label.any.layouts; p != NULL; p=p->next ) { + layout = (glTemplateLayout *)p->data; + xml_add_layout (layout, node, ns); + } + + gl_debug (DEBUG_TEMPLATE, "END"); +} + +/*--------------------------------------------------------------------------*/ +/* PRIVATE. Add XML Sheet->Label->Layout Node. */ +/*--------------------------------------------------------------------------*/ +static void +xml_add_layout (glTemplateLayout *layout, + xmlNodePtr root, + xmlNsPtr ns) +{ + xmlNodePtr node; + + gl_debug (DEBUG_TEMPLATE, "START"); + + node = xmlNewChild(root, ns, "Layout", NULL); + gl_xml_set_prop_int (node, "nx", layout->nx); + gl_xml_set_prop_int (node, "ny", layout->ny); + gl_xml_set_prop_double (node, "x0", layout->x0); + gl_xml_set_prop_double (node, "y0", layout->y0); + gl_xml_set_prop_double (node, "dx", layout->dx); + gl_xml_set_prop_double (node, "dy", layout->dy); + + gl_debug (DEBUG_TEMPLATE, "END"); +} + +/*--------------------------------------------------------------------------*/ +/* PRIVATE. Add XML Sheet->Label->Markup (margin) Node. */ +/*--------------------------------------------------------------------------*/ +static void +xml_add_markup_margin (glTemplateMarkupMargin *margin, + xmlNodePtr root, + xmlNsPtr ns) +{ + xmlNodePtr node; + + gl_debug (DEBUG_TEMPLATE, "START"); + + node = xmlNewChild(root, ns, "Markup", NULL); + xmlSetProp (node, "type", "margin"); + + gl_xml_set_prop_double (node, "size", margin->size); + + gl_debug (DEBUG_TEMPLATE, "END"); +} + +/*--------------------------------------------------------------------------*/ +/* PRIVATE. Add XML Sheet->Label->Markup (line) Node. */ +/*--------------------------------------------------------------------------*/ +static void +xml_add_markup_line (glTemplateMarkupLine *line, + xmlNodePtr root, + xmlNsPtr ns) +{ + xmlNodePtr node; + + gl_debug (DEBUG_TEMPLATE, "START"); + + node = xmlNewChild(root, ns, "Markup", NULL); + xmlSetProp (node, "type", "line"); + + gl_xml_set_prop_double (node, "x1", line->x1); + gl_xml_set_prop_double (node, "y1", line->y1); + gl_xml_set_prop_double (node, "x2", line->x2); + gl_xml_set_prop_double (node, "y2", line->y2); + + gl_debug (DEBUG_TEMPLATE, "END"); +} + +/*--------------------------------------------------------------------------*/ +/* PRIVATE. Add XML Sheet->Alias Node. */ +/*--------------------------------------------------------------------------*/ +static void +xml_add_alias (gchar *name, + xmlNodePtr root, + xmlNsPtr ns) +{ + xmlNodePtr node; + + gl_debug (DEBUG_TEMPLATE, "START"); + + node = xmlNewChild (root, ns, "Alias", NULL); + xmlSetProp (node, "name", name); + + gl_debug (DEBUG_TEMPLATE, "END"); +} + diff --git a/glabels2/src/xml-template.h b/glabels2/src/xml-template.h new file mode 100644 index 00000000..b2efac9c --- /dev/null +++ b/glabels2/src/xml-template.h @@ -0,0 +1,45 @@ +/* + * (GLABELS) Label and Business Card Creation program for GNOME + * + * xml-template.h: template xml module header file + * + * Copyright (C) 2001-2003 Jim Evins . + * + * 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * 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 + */ + +#ifndef __XML_TEMPLATE_H__ +#define __XML_TEMPLATE_H__ + +#include +#include +#include + +#include "template.h" + +G_BEGIN_DECLS + +GList *gl_xml_template_read_templates_from_file (GList *templates, + gchar *xml_filename); + +glTemplate *gl_xml_template_parse_sheet (xmlNodePtr sheet_node); + +void gl_xml_template_add_sheet (const glTemplate *template, + xmlNodePtr root, + xmlNsPtr ns); + +G_END_DECLS + +#endif /* __XML_TEMPLATE_H__ */