+2003-12-26 Jim Evins <evins@snaught.com>
+
+ * 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
#include "paper.h"
#include "xml-paper.h"
+#include "util.h"
#include "debug.h"
/*===========================================*/
static GList *read_papers (void);
-static gchar *get_home_data_dir (void);
static GList *read_paper_files_from_dir (GList *papers,
const gchar *dirname);
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");
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. */
/*--------------------------------------------------------------------------*/
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,
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;
}
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.");
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");
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. */
/*--------------------------------------------------------------------------*/
#include <string.h>
#include <glib.h>
+#include <libgnome/gnome-util.h>
#include <math.h>
#include <libgnomeprint/gnome-font.h>
#define FRAC_DELTA 0.00005
+\f
+/****************************************************************************/
+/* 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;
+}
+
+
\f
/****************************************************************************/
/* 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;
/* 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;
/* 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;
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);
}
/* 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;
}
GtkJustification
-gl_util_string_to_just (const gchar * string)
+gl_util_string_to_just (const gchar *string)
{
if (g_strcasecmp (string, "Left") == 0) {
}
GnomeFontWeight
-gl_util_string_to_weight (const gchar * string)
+gl_util_string_to_weight (const gchar *string)
{
if (g_strcasecmp (string, "Regular") == 0) {
*
* util.h: various small utility functions
*
- * Copyright (C) 2001 Jim Evins <evins@snaught.com>.
+ * Copyright (C) 2001-2003 Jim Evins <evins@snaught.com>.
*
* 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
#include <gtk/gtk.h>
#include <libgnomeprint/gnome-font.h>
-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__ */
/*========================================================*/
/* 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/"
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 {
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);
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 */
/****************************************************************************/
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);
#include <glib.h>
#include <libxml/tree.h>
+#define XML_NAME_SPACE "http://snaught.com/glabels/2.0/"
+
G_BEGIN_DECLS
gdouble gl_xml_get_prop_double (xmlNodePtr node,