From: Jim Evins Date: Sat, 17 Nov 2007 04:07:28 +0000 (+0000) Subject: 2007-11-16 Jim Evins X-Git-Tag: glabels-2_3_0~309 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=ab3ea131f3061e41dd0753167ce5493ecca205ad;p=glabels 2007-11-16 Jim Evins * libglabels/db.c: * libglabels/db.h: Added return codes to lgl_db_register_template(). Added lgl_db_does_template_exist() function. Automatically create "User defined" category and add to all user registered templates. * libglabels/xml-template.c: * libglabels/xml-template.h: Return number of bytes written from lgl_xml_template_write_templates_to_file() and lgl_xml_template_write_template_to_file(). * docs/libglabels/libglabels-decl-list.txt: * docs/libglabels/libglabels-sections.txt: * docs/libglabels/libglabels-undocumented.txt: * docs/libglabels/tmpl/db.sgml: * docs/libglabels/tmpl/xml-template.sgml: Update documentation to reflect above changes. git-svn-id: https://glabels.svn.sourceforge.net/svnroot/glabels/trunk@689 f5e0f49d-192f-0410-a22d-a8d8700d0965 --- diff --git a/glabels2/ChangeLog b/glabels2/ChangeLog index 2b6e5fd1..b0f0df18 100644 --- a/glabels2/ChangeLog +++ b/glabels2/ChangeLog @@ -1,3 +1,21 @@ +2007-11-16 Jim Evins + + * libglabels/db.c: + * libglabels/db.h: + Added return codes to lgl_db_register_template(). Added lgl_db_does_template_exist() + function. Automatically create "User defined" category and add to all user + registered templates. + * libglabels/xml-template.c: + * libglabels/xml-template.h: + Return number of bytes written from lgl_xml_template_write_templates_to_file() and + lgl_xml_template_write_template_to_file(). + * docs/libglabels/libglabels-decl-list.txt: + * docs/libglabels/libglabels-sections.txt: + * docs/libglabels/libglabels-undocumented.txt: + * docs/libglabels/tmpl/db.sgml: + * docs/libglabels/tmpl/xml-template.sgml: + Update documentation to reflect above changes. + 2007-10-28 Jim Evins * libglabels/xml-template.c: diff --git a/glabels2/docs/libglabels/libglabels-decl-list.txt b/glabels2/docs/libglabels/libglabels-decl-list.txt index fc9e697b..6e99ec0e 100644 --- a/glabels2/docs/libglabels/libglabels-decl-list.txt +++ b/glabels2/docs/libglabels/libglabels-decl-list.txt @@ -1,5 +1,6 @@
db +lglDbRegStatus lgl_db_init lgl_db_get_paper_id_list lgl_db_free_paper_id_list @@ -23,6 +24,7 @@ lgl_db_is_category_id_known lgl_db_get_brand_list lgl_db_free_brand_list lgl_db_register_template +lgl_db_does_template_exist lgl_db_get_template_name_list_unique lgl_db_get_template_name_list_all lgl_db_free_template_name_list diff --git a/glabels2/docs/libglabels/libglabels-sections.txt b/glabels2/docs/libglabels/libglabels-sections.txt index 1c53d0be..ac954e4d 100644 --- a/glabels2/docs/libglabels/libglabels-sections.txt +++ b/glabels2/docs/libglabels/libglabels-sections.txt @@ -1,6 +1,8 @@
db libglabels/db.h +lglDbRegStatus + lgl_db_init lgl_db_get_paper_id_list @@ -28,6 +30,7 @@ lgl_db_get_brand_list lgl_db_free_brand_list lgl_db_register_template +lgl_db_does_template_exist lgl_db_get_template_name_list_unique lgl_db_get_template_name_list_all lgl_db_free_template_name_list diff --git a/glabels2/docs/libglabels/libglabels-undocumented.txt b/glabels2/docs/libglabels/libglabels-undocumented.txt index 73f1d9e0..a1352226 100644 --- a/glabels2/docs/libglabels/libglabels-undocumented.txt +++ b/glabels2/docs/libglabels/libglabels-undocumented.txt @@ -1,5 +1,5 @@ 99% symbol docs coverage. -135 symbols documented. +137 symbols documented. 0 symbols incomplete. 1 not documented. diff --git a/glabels2/docs/libglabels/tmpl/db.sgml b/glabels2/docs/libglabels/tmpl/db.sgml index 734bb9c1..eaacab3f 100644 --- a/glabels2/docs/libglabels/tmpl/db.sgml +++ b/glabels2/docs/libglabels/tmpl/db.sgml @@ -18,6 +18,16 @@ the LibGlabels template database. + + +This enumeration defines a set of possible return values for lgl_db_register_template(). + + +@LGL_DB_REG_OK: Registration successful. +@LGL_DB_REG_BAD_PAPER_ID: Registration failed because paper id is unknown. +@LGL_DB_REG_BRAND_PART_EXISTS: Registration failed because template with same brand and part name/number already exists. +@LGL_DB_REG_FILE_WRITE_ERROR: Registration failed because an error while writing to disk. + @@ -212,6 +222,17 @@ the LibGlabels template database. @template: +@Returns: + + + + + + + +@brand: +@part: +@Returns: diff --git a/glabels2/docs/libglabels/tmpl/xml-template.sgml b/glabels2/docs/libglabels/tmpl/xml-template.sgml index 5591b9d4..071a08ab 100644 --- a/glabels2/docs/libglabels/tmpl/xml-template.sgml +++ b/glabels2/docs/libglabels/tmpl/xml-template.sgml @@ -53,6 +53,7 @@ template files and document files. @templates: @utf8_filename: +@Returns: @@ -62,6 +63,7 @@ template files and document files. @template: @utf8_filename: +@Returns: diff --git a/glabels2/libglabels/db.c b/glabels2/libglabels/db.c index 89b097d2..c38294ea 100644 --- a/glabels2/libglabels/db.c +++ b/glabels2/libglabels/db.c @@ -84,8 +84,8 @@ static lglTemplate *template_full_page (const gchar *page_size); * lgl_db_init: * * Initialize all libglabels subsystems. It is not necessary for an application to call - * lgl_init(), because libglabels will initialize on demand. An application programmer may - * choose to call lgl_init() at startup to minimize the impact of the first libglabels call + * lgl_db_init(), because libglabels will initialize on demand. An application programmer may + * choose to call lgl_db_init() at startup to minimize the impact of the first libglabels call * on GUI response time. * * This function initializes its paper definitions, category definitions, and its template @@ -95,9 +95,10 @@ static lglTemplate *template_full_page (const gchar *page_size); void lgl_db_init (void) { - lglPaper *other; - GList *page_sizes; - GList *p; + lglPaper *paper_other; + lglCategory *category_user_defined; + GList *page_sizes; + GList *p; /* * Paper definitions @@ -108,8 +109,8 @@ lgl_db_init (void) papers = read_papers (); /* Create and append an "Other" entry. */ - other = lgl_paper_new ("Other", _("Other"), 0.0, 0.0); - papers = g_list_append (papers, other); + paper_other = lgl_paper_new ("Other", _("Other"), 0.0, 0.0); + papers = g_list_append (papers, paper_other); } @@ -119,6 +120,10 @@ lgl_db_init (void) if (!categories) { categories = read_categories (); + + /* Create and append a "User defined" entry. */ + category_user_defined = lgl_category_new ("user-defined", _("User defined")); + categories = g_list_append (categories, category_user_defined); } /* @@ -185,7 +190,7 @@ lgl_db_get_paper_id_list (void) * @ids: List of id strings to be freed. * * Free up all storage associated with an id list obtained with - * lgl_paper_get_id_list(). + * lgl_db_get_paper_id_list(). * */ void @@ -238,7 +243,7 @@ lgl_db_get_paper_name_list (void) * @names: List of localized paper name strings to be freed. * * Free up all storage associated with a name list obtained with - * lgl_paper_get_name_list(). + * lgl_db_get_paper_name_list(). * */ void @@ -598,7 +603,7 @@ lgl_db_get_category_id_list (void) * @ids: List of id strings to be freed. * * Free up all storage associated with an id list obtained with - * lgl_category_get_id_list(). + * lgl_db_get_category_id_list(). * */ void @@ -651,7 +656,7 @@ lgl_db_get_category_name_list (void) * @names: List of localized category name strings to be freed. * * Free up all storage associated with a name list obtained with - * lgl_category_get_name_list(). + * lgl_db_get_category_name_list(). * */ void @@ -963,8 +968,8 @@ lgl_db_print_known_categories (void) * * Get a list of all valid brands of templates in the template database. * Results can be filtered by page size and/or template category. A list of valid page - * sizes can be obtained using lgl_paper_get_id_list(). A list of valid template - * categories can be obtained using lgl_category_get_id_list(). + * sizes can be obtained using lgl_db_get_paper_id_list(). A list of valid template + * categories can be obtained using lgl_db_get_category_id_list(). * * Returns: a list of brands */ @@ -1042,67 +1047,106 @@ lgl_db_free_brand_list (GList *brands) * * Register a template. This function adds a template to the template database. * The template will be stored in an individual XML file in the user template directory. + * + * Returns: Status of registration attempt (#lglDbRegStatus) */ -void +lglDbRegStatus lgl_db_register_template (const lglTemplate *template) { - GList *p_tmplt1, *p_a1; - lglTemplate *template1; - lglTemplateAlias *alias1; + lglTemplate *template_copy; + gchar *dir, *filename, *abs_filename; + gint bytes_written; if (!templates) { lgl_db_init (); } - for (p_tmplt1 = templates; p_tmplt1 != NULL; p_tmplt1 = p_tmplt1->next) + if (lgl_db_does_template_exist (template->brand, template->part)) { - template1 = (lglTemplate *) p_tmplt1->data; - - for (p_a1=template1->aliases; p_a1!=NULL; p_a1=p_a1->next) - { - alias1 = (lglTemplateAlias *) p_a1->data; - - if ( UTF8_EQUAL (template->brand, alias1->brand) && - UTF8_EQUAL (template->part, alias1->part) ) - { - - /* FIXME: make sure templates are really identical */ - /* if not, apply hash to name to make unique. */ - return; - } - - } - - } + return LGL_DB_REG_BRAND_PART_EXISTS; + } if (lgl_db_is_paper_id_known (template->paper_id)) { + template_copy = lgl_template_dup (template); + lgl_template_add_category (template_copy, "user-defined"); - gchar *dir, *filename, *abs_filename; - - templates = g_list_append (templates, - lgl_template_dup (template)); - - /* FIXME: make sure filename is unique */ dir = LGL_USER_DATA_DIR; mkdir (dir, 0775); /* Try to make sure directory exists. */ filename = g_strdup_printf ("%s_%s.template", template->brand, template->part); abs_filename = g_build_filename (dir, filename, NULL); - lgl_xml_template_write_template_to_file (template, abs_filename); + bytes_written = lgl_xml_template_write_template_to_file (template_copy, abs_filename); g_free (dir); g_free (filename); g_free (abs_filename); + if (bytes_written > 0) + { + templates = g_list_append (templates, template_copy); + return LGL_DB_REG_OK; + } + else + { + lgl_template_free (template_copy); + return LGL_DB_REG_FILE_WRITE_ERROR; + } } else { g_message ("Cannot register new template with unknown page size."); + return LGL_DB_REG_BAD_PAPER_ID; } } +/** + * lgl_db_does_template_exist: + * @brand: Brand name. + * @part: Part name/number. + * + * This function tests whether a template with the given brand and part name/number exists. + * + * Returns: TRUE if such a template exists in the database. + */ +gboolean +lgl_db_does_template_exist (const gchar *brand, + const gchar *part) +{ + GList *p_tmplt, *p_alias; + lglTemplate *template; + lglTemplateAlias *alias; + + if (!templates) + { + lgl_db_init (); + } + + if ((brand == NULL) || (part == NULL)) + { + return FALSE; + } + + for (p_tmplt = templates; p_tmplt != NULL; p_tmplt = p_tmplt->next) + { + template = (lglTemplate *) p_tmplt->data; + for (p_alias = template->aliases; p_alias != NULL; p_alias = p_alias->next) + { + alias = (lglTemplateAlias *)p_alias->data; + + if ( UTF8_EQUAL (brand, alias->brand) && + UTF8_EQUAL (part, alias->part) ) + { + return TRUE; + } + } + } + + return FALSE; +} + + /** * lgl_db_get_template_name_list_unique: * @brand: If non NULL, limit results to given brand @@ -1111,8 +1155,8 @@ lgl_db_register_template (const lglTemplate *template) * * Get a list of valid names of unique templates in the template database. Results * can be filtered by page size and/or template category. A list of valid page sizes - * can be obtained using lgl_paper_get_id_list(). A list of valid template categories - * can be obtained using lgl_category_get_id_list(). + * can be obtained using lgl_db_get_paper_id_list(). A list of valid template categories + * can be obtained using lgl_db_get_category_id_list(). * * This function differs from lgl_db_get_template_name_list_all(), because it does not * return multiple names for the same template. @@ -1160,8 +1204,8 @@ lgl_db_get_template_name_list_unique (const gchar *brand, * * Get a list of all valid names and aliases of templates in the template database. * Results can be filtered by page size and/or template category. A list of valid page - * sizes can be obtained using lgl_paper_get_id_list(). A list of valid template - * categories can be obtained using lgl_category_get_id_list(). + * sizes can be obtained using lgl_db_get_paper_id_list(). A list of valid template + * categories can be obtained using lgl_db_get_category_id_list(). * * This function differs from lgl_db_get_template_name_list_unique(), because it will * return multiple names for the same template. @@ -1214,7 +1258,7 @@ lgl_db_get_template_name_list_all (const gchar *brand, * @names: List of template name strings to be freed. * * Free up all storage associated with a list of template names obtained with - * lgl_get_template_name_list_all() or lgl_get_template_name_list_unique(). + * lgl_db_get_template_name_list_all() or lgl_db_get_template_name_list_unique(). * */ void diff --git a/glabels2/libglabels/db.h b/glabels2/libglabels/db.h index f9dd72aa..fe0c85e4 100644 --- a/glabels2/libglabels/db.h +++ b/glabels2/libglabels/db.h @@ -37,6 +37,14 @@ G_BEGIN_DECLS +typedef enum +{ + LGL_DB_REG_OK = 0, + LGL_DB_REG_BAD_PAPER_ID = -1, + LGL_DB_REG_BRAND_PART_EXISTS = -2, + LGL_DB_REG_FILE_WRITE_ERROR = -3 +} lglDbRegStatus; + /* * Module initialization */ @@ -102,7 +110,10 @@ void lgl_db_free_brand_list (GList *brand /* * Templates */ -void lgl_db_register_template (const lglTemplate *template); +lglDbRegStatus lgl_db_register_template (const lglTemplate *template); + +gboolean lgl_db_does_template_exist (const gchar *brand, + const gchar *part); GList *lgl_db_get_template_name_list_unique (const gchar *brand, const gchar *paper_id, diff --git a/glabels2/libglabels/xml-template.c b/glabels2/libglabels/xml-template.c index 343fb772..8d83a577 100644 --- a/glabels2/libglabels/xml-template.c +++ b/glabels2/libglabels/xml-template.c @@ -637,14 +637,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; @@ -662,17 +664,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; } } @@ -685,18 +687,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; } diff --git a/glabels2/libglabels/xml-template.h b/glabels2/libglabels/xml-template.h index 23377e6c..b940db32 100644 --- a/glabels2/libglabels/xml-template.h +++ b/glabels2/libglabels/xml-template.h @@ -40,10 +40,10 @@ GList *lgl_xml_template_parse_templates_doc (const xmlDocPtr templ lglTemplate *lgl_xml_template_parse_template_node (const xmlNodePtr template_node); -void lgl_xml_template_write_templates_to_file (GList *templates, +gint lgl_xml_template_write_templates_to_file (GList *templates, const gchar *utf8_filename); -void lgl_xml_template_write_template_to_file (const lglTemplate *template, +gint lgl_xml_template_write_template_to_file (const lglTemplate *template, const gchar *utf8_filename); void lgl_xml_template_create_template_node (const lglTemplate *template,