From 0fc7cd31248b35914e681b37cd703f5366d88c76 Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Sat, 27 Dec 2003 04:33:08 +0000 Subject: [PATCH] 2003-12-26 Jim Evins * src/paper.c: * src/template.c: * src/util.c: * src/util.h: Moved local versions of get_home_data_dir() in paper.c and template.c to util package. Cleaned up formatting in util.[ch]. * src/template.c: In gl_template_register() save a copy of the template in ~/.glabels if the template is not known. Still need to handle duplicate filename case and duplicate name but different template case. * src/xml-label.c: * src/xml.h: Moved local definition of NAME_SPACE macro from xml-label.c to xml.h. * src/xml-template.c: * src/xml-template.h: Added function gl_xml_template_write_template_to_file(), used in gl_template_register(). git-svn-id: https://glabels.svn.sourceforge.net/svnroot/glabels/trunk@389 f5e0f49d-192f-0410-a22d-a8d8700d0965 --- glabels2/ChangeLog | 27 +++++++++++++++++++++++++ glabels2/src/paper.c | 21 ++----------------- glabels2/src/template.c | 35 ++++++++++++++------------------ glabels2/src/util.c | 32 +++++++++++++++++++++-------- glabels2/src/util.h | 26 ++++++++++++++---------- glabels2/src/xml-label.c | 5 ++--- glabels2/src/xml-template.c | 40 +++++++++++++++++++++++++++++++++++++ glabels2/src/xml-template.h | 3 +++ glabels2/src/xml.h | 2 ++ 9 files changed, 131 insertions(+), 60 deletions(-) diff --git a/glabels2/ChangeLog b/glabels2/ChangeLog index e69de29b..dc06d5ac 100644 --- a/glabels2/ChangeLog +++ b/glabels2/ChangeLog @@ -0,0 +1,27 @@ +2003-12-26 Jim Evins + + * src/paper.c: + * src/template.c: + * src/util.c: + * src/util.h: + Moved local versions of get_home_data_dir() in paper.c and + template.c to util package. Cleaned up formatting in + util.[ch]. + + * src/template.c: + In gl_template_register() save a copy of the template in + ~/.glabels if the template is not known. Still need to + handle duplicate filename case and duplicate name but + different template case. + + * src/xml-label.c: + * src/xml.h: + Moved local definition of NAME_SPACE macro from xml-label.c + to xml.h. + + * src/xml-template.c: + * src/xml-template.h: + Added function gl_xml_template_write_template_to_file(), used + in gl_template_register(). + + \ No newline at end of file diff --git a/glabels2/src/paper.c b/glabels2/src/paper.c index 55875a7a..652eb6c5 100644 --- a/glabels2/src/paper.c +++ b/glabels2/src/paper.c @@ -27,6 +27,7 @@ #include "paper.h" #include "xml-paper.h" +#include "util.h" #include "debug.h" @@ -51,7 +52,6 @@ static GList *papers = NULL; /*===========================================*/ static GList *read_papers (void); -static gchar *get_home_data_dir (void); static GList *read_paper_files_from_dir (GList *papers, const gchar *dirname); @@ -338,7 +338,7 @@ void gl_paper_free (glPaper **paper) static GList * read_papers (void) { - gchar *home_data_dir = get_home_data_dir (); + gchar *home_data_dir = gl_util_get_home_data_dir (); GList *papers = NULL; gl_debug (DEBUG_PAPER, "START"); @@ -356,23 +356,6 @@ read_papers (void) return papers; } -/*--------------------------------------------------------------------------*/ -/* PRIVATE. get '~/.glabels' directory path. */ -/*--------------------------------------------------------------------------*/ -static gchar * -get_home_data_dir (void) -{ - gchar *dir = gnome_util_prepend_user_home (".glabels"); - - gl_debug (DEBUG_PAPER, "START"); - - /* Try to create ~/.glabels directory. If it exists, no problem. */ - mkdir (dir, 0775); - - gl_debug (DEBUG_PAPER, "END"); - return dir; -} - /*--------------------------------------------------------------------------*/ /* PRIVATE. Read all paper files from given directory. Append to list. */ /*--------------------------------------------------------------------------*/ diff --git a/glabels2/src/template.c b/glabels2/src/template.c index dc67ad1e..9fa6887a 100644 --- a/glabels2/src/template.c +++ b/glabels2/src/template.c @@ -57,7 +57,6 @@ static glTemplate *template_full_page (const gchar *page_si 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 gint compare_origins (gconstpointer a, @@ -169,6 +168,10 @@ gl_template_register (const glTemplate *template) template->name, pa1->data); if (g_strcasecmp (template->name, pa1->data) == 0) { + + /* FIXME: make sure templates are really identical */ + /* if not, apply hash to name to make unique. */ + gl_debug (DEBUG_TEMPLATE, "END (found)"); return; } @@ -179,10 +182,19 @@ gl_template_register (const glTemplate *template) if (gl_paper_is_id_known (template->page_size)) { + gchar *dir, *filename, *abs_filename; + 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. */ + /* FIXME: make sure filename is unique */ + dir = gl_util_get_home_data_dir (); + filename = g_strconcat (template->name, ".template", NULL); + abs_filename = g_build_filename (dir, filename, NULL); + gl_xml_template_write_template_to_file (template, abs_filename); + g_free (dir); + g_free (filename); + g_free (abs_filename); } else { g_warning ("Cannot register new template with unknown page size."); @@ -392,7 +404,7 @@ template_full_page (const gchar *page_size) static GList * read_templates (void) { - gchar *home_data_dir = get_home_data_dir (); + gchar *home_data_dir = gl_util_get_home_data_dir (); GList *templates = NULL; gl_debug (DEBUG_TEMPLATE, "START"); @@ -410,23 +422,6 @@ read_templates (void) return templates; } -/*--------------------------------------------------------------------------*/ -/* PRIVATE. get '~/.glabels' directory path. */ -/*--------------------------------------------------------------------------*/ -static gchar * -get_home_data_dir (void) -{ - gchar *dir = gnome_util_prepend_user_home (".glabels"); - - gl_debug (DEBUG_TEMPLATE, "START"); - - /* Try to create ~/.glabels directory. If it exists, no problem. */ - mkdir (dir, 0775); - - gl_debug (DEBUG_TEMPLATE, "END"); - return dir; -} - /*--------------------------------------------------------------------------*/ /* PRIVATE. Read all template files from given directory. Append to list. */ /*--------------------------------------------------------------------------*/ diff --git a/glabels2/src/util.c b/glabels2/src/util.c index c73fac36..124d492c 100644 --- a/glabels2/src/util.c +++ b/glabels2/src/util.c @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -31,12 +32,28 @@ #define FRAC_DELTA 0.00005 + +/****************************************************************************/ +/* Get '~/.glabels' directory path. */ +/****************************************************************************/ +gchar * +gl_util_get_home_data_dir (void) +{ + gchar *dir = gnome_util_prepend_user_home (".glabels"); + + /* Try to create ~/.glabels directory. If it exists, no problem. */ + mkdir (dir, 0775); + + return dir; +} + + /****************************************************************************/ /* Append ".glabels" extension to filename if needed. */ /****************************************************************************/ gchar * -gl_util_add_extension (const gchar * orig_filename) +gl_util_add_extension (const gchar *orig_filename) { gchar *new_filename, *extension; @@ -59,7 +76,7 @@ gl_util_add_extension (const gchar * orig_filename) /* Remove ".glabels" extension from filename if needed. */ /****************************************************************************/ gchar * -gl_util_remove_extension (const gchar * orig_filename) +gl_util_remove_extension (const gchar *orig_filename) { gchar *new_filename, *extension; @@ -79,7 +96,7 @@ gl_util_remove_extension (const gchar * orig_filename) /* Make sure we have an absolute path to filename. */ /****************************************************************************/ gchar * -gl_util_make_absolute (const gchar * filename) +gl_util_make_absolute (const gchar *filename) { gchar *pwd, *absolute_filename; @@ -87,8 +104,7 @@ gl_util_make_absolute (const gchar * filename) absolute_filename = g_strdup (filename); } else { pwd = g_get_current_dir (); - absolute_filename = - g_strjoin (G_DIR_SEPARATOR_S, pwd, filename, NULL); + absolute_filename = g_build_filename (pwd, filename, NULL); g_free (pwd); } @@ -99,7 +115,7 @@ gl_util_make_absolute (const gchar * filename) /* Create fractional representation of number, if possible. */ /****************************************************************************/ gchar * -gl_util_fraction( gdouble x ) +gl_util_fraction (gdouble x) { static gdouble denom[] = { 1., 2., 3., 4., 8., 16., 32., 0. }; gint i; @@ -148,7 +164,7 @@ gl_util_just_to_string (GtkJustification just) } GtkJustification -gl_util_string_to_just (const gchar * string) +gl_util_string_to_just (const gchar *string) { if (g_strcasecmp (string, "Left") == 0) { @@ -180,7 +196,7 @@ gl_util_weight_to_string (GnomeFontWeight weight) } GnomeFontWeight -gl_util_string_to_weight (const gchar * string) +gl_util_string_to_weight (const gchar *string) { if (g_strcasecmp (string, "Regular") == 0) { diff --git a/glabels2/src/util.h b/glabels2/src/util.h index fa62437b..73366d15 100644 --- a/glabels2/src/util.h +++ b/glabels2/src/util.h @@ -3,7 +3,7 @@ * * util.h: various small utility functions * - * Copyright (C) 2001 Jim Evins . + * 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 @@ -26,17 +26,23 @@ #include #include -extern gchar *gl_util_add_extension (const gchar * orig_filename); -extern gchar *gl_util_remove_extension (const gchar * orig_filename); +G_BEGIN_DECLS -extern gchar *gl_util_make_absolute (const gchar * filename); +gchar *gl_util_get_home_data_dir (void); -extern gchar *gl_util_fraction( gdouble x ); +gchar *gl_util_add_extension (const gchar *orig_filename); +gchar *gl_util_remove_extension (const gchar *orig_filename); -extern const gchar *gl_util_just_to_string (GtkJustification just); -extern GtkJustification gl_util_string_to_just (const gchar * string); +gchar *gl_util_make_absolute (const gchar *filename); -extern const gchar *gl_util_weight_to_string (GnomeFontWeight weight); -extern GnomeFontWeight gl_util_string_to_weight (const gchar * string); +gchar *gl_util_fraction (gdouble x); -#endif /* __UTIL_H__ */ +const gchar *gl_util_just_to_string (GtkJustification just); +GtkJustification gl_util_string_to_just (const gchar *string); + +const gchar *gl_util_weight_to_string (GnomeFontWeight weight); +GnomeFontWeight gl_util_string_to_weight (const gchar *string); + +G_END_DECLS + +#endif /* __UTIL_H__ */ diff --git a/glabels2/src/xml-label.c b/glabels2/src/xml-label.c index 16d6eb3c..f2282760 100644 --- a/glabels2/src/xml-label.c +++ b/glabels2/src/xml-label.c @@ -50,7 +50,6 @@ /*========================================================*/ /* Private macros and constants. */ /*========================================================*/ -#define NAME_SPACE "http://snaught.com/glabels/2.0/" #define COMPAT01_NAME_SPACE "http://snaught.com/glabels/0.1/" #define COMPAT04_NAME_SPACE "http://snaught.com/glabels/0.4/" #define COMPAT191_NAME_SPACE "http://snaught.com/glabels/1.92/" @@ -253,7 +252,7 @@ xml_doc_to_label (xmlDocPtr doc, return NULL; } - ns = xmlSearchNsByHref (doc, root, NAME_SPACE); + ns = xmlSearchNsByHref (doc, root, XML_NAME_SPACE); if (ns != NULL) { label = xml_parse_label (root, status); } else { @@ -1013,7 +1012,7 @@ xml_label_to_doc (glLabel *label, doc = xmlNewDoc ("1.0"); doc->xmlRootNode = xmlNewDocNode (doc, NULL, "Glabels-document", NULL); - ns = xmlNewNs (doc->xmlRootNode, NAME_SPACE, NULL); + ns = xmlNewNs (doc->xmlRootNode, XML_NAME_SPACE, NULL); xmlSetNs (doc->xmlRootNode, ns); template = gl_label_get_template (label); diff --git a/glabels2/src/xml-template.c b/glabels2/src/xml-template.c index 6ef101e4..c4e985f8 100644 --- a/glabels2/src/xml-template.c +++ b/glabels2/src/xml-template.c @@ -473,6 +473,46 @@ xml_parse_alias (xmlNodePtr alias_node, gl_debug (DEBUG_TEMPLATE, "END"); } +/****************************************************************************/ +/* Write single template to XML file. */ +/****************************************************************************/ +void +gl_xml_template_write_template_to_file (const glTemplate *template, + const gchar *utf8_filename) +{ + xmlDocPtr doc; + xmlNsPtr ns; + gint xml_ret; + gchar *filename; + + gl_debug (DEBUG_TEMPLATE, "START"); + + doc = xmlNewDoc ("1.0"); + doc->xmlRootNode = xmlNewDocNode (doc, NULL, "Glabels-templates", NULL); + + ns = xmlNewNs (doc->xmlRootNode, XML_NAME_SPACE, NULL); + xmlSetNs (doc->xmlRootNode, ns); + + gl_xml_template_add_template (template, doc->xmlRootNode, ns); + + filename = g_filename_from_utf8 (utf8_filename, -1, NULL, NULL, NULL); + if (!filename) + g_warning (_("Utf8 conversion error.")); + else { + xmlSetDocCompressMode (doc, 0); + xml_ret = xmlSaveFormatFile (filename, doc, TRUE); + xmlFreeDoc (doc); + if (xml_ret == -1) { + + g_warning (_("Problem saving xml file.")); + + } + g_free (filename); + } + + gl_debug (DEBUG_TEMPLATE, "END"); +} + /****************************************************************************/ /* Add XML Template Node */ /****************************************************************************/ diff --git a/glabels2/src/xml-template.h b/glabels2/src/xml-template.h index 0f28d23b..bf4c9caf 100644 --- a/glabels2/src/xml-template.h +++ b/glabels2/src/xml-template.h @@ -36,6 +36,9 @@ GList *gl_xml_template_read_templates_from_file (GList *templa glTemplate *gl_xml_template_parse_template (xmlNodePtr template_node); +void gl_xml_template_write_template_to_file (const glTemplate *template, + const gchar *utf8_filename); + void gl_xml_template_add_template (const glTemplate *template, xmlNodePtr root, xmlNsPtr ns); diff --git a/glabels2/src/xml.h b/glabels2/src/xml.h index a0057cdc..28d87e61 100644 --- a/glabels2/src/xml.h +++ b/glabels2/src/xml.h @@ -25,6 +25,8 @@ #include #include +#define XML_NAME_SPACE "http://snaught.com/glabels/2.0/" + G_BEGIN_DECLS gdouble gl_xml_get_prop_double (xmlNodePtr node, -- 2.39.5