1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
4 * (LIBGLABELS) Template library for GLABELS
6 * db.c: template db module
8 * Copyright (C) 2003-2007 Jim Evins <evins@snaught.com>.
10 * This file is part of the LIBGLABELS library.
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with this library; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
31 #include <glib/gi18n.h>
32 #include <glib/gmem.h>
33 #include <glib/gstrfuncs.h>
34 #include <glib/gdir.h>
35 #include <glib/gmessages.h>
36 #include <glib/ghash.h>
39 #include <sys/types.h>
41 #include "libglabels-private.h"
43 #include "xml-paper.h"
44 #include "xml-category.h"
45 #include "xml-template.h"
48 /*===========================================*/
50 /*===========================================*/
52 /*===========================================*/
54 /*===========================================*/
56 static GList *papers = NULL;
57 static GList *categories = NULL;
58 static GList *templates = NULL;
60 static GHashTable *template_cache = NULL;
62 /*===========================================*/
63 /* Local function prototypes */
64 /*===========================================*/
66 static void init_template_cache (void);
67 static void add_to_template_cache (lglTemplate *template);
69 static GList *read_papers (void);
70 static GList *read_paper_files_from_dir (GList *papers,
71 const gchar *dirname);
73 static GList *read_categories (void);
74 static GList *read_category_files_from_dir (GList *categories,
75 const gchar *dirname);
77 static GList *read_templates (void);
78 static GList *read_template_files_from_dir (GList *templates,
79 const gchar *dirname);
81 static lglTemplate *template_full_page (const gchar *page_size);
85 /*===========================================*/
86 /* Module initialization */
87 /*===========================================*/
92 * Initialize all libglabels subsystems. It is not necessary for an application to call
93 * lgl_db_init(), because libglabels will initialize on demand. An application programmer may
94 * choose to call lgl_db_init() at startup to minimize the impact of the first libglabels call
95 * on GUI response time.
97 * This function initializes its paper definitions, category definitions, and its template
98 * database.. It will search both system and user template directories to locate
104 lglPaper *paper_other;
105 lglCategory *category_user_defined;
115 papers = read_papers ();
117 /* Create and append an "Other" entry. */
118 paper_other = lgl_paper_new ("Other", _("Other"), 0.0, 0.0, NULL);
119 papers = g_list_append (papers, paper_other);
128 categories = read_categories ();
130 /* Create and append a "User defined" entry. */
131 category_user_defined = lgl_category_new ("user-defined", _("User defined"));
132 categories = g_list_append (categories, category_user_defined);
141 templates = read_templates ();
143 /* Create and append generic full page templates. */
144 page_sizes = lgl_db_get_paper_id_list ();
145 for ( p=page_sizes; p != NULL; p=p->next )
147 if ( !lgl_db_is_paper_id_other (p->data) )
149 templates = g_list_append (templates,
150 template_full_page (p->data));
153 lgl_db_free_paper_id_list (page_sizes);
155 init_template_cache ();
161 /*===========================================*/
162 /* Paper db functions. */
163 /*===========================================*/
166 * lgl_db_get_paper_id_list:
168 * Get a list of all paper ids known to libglabels.
170 * Returns: a list of paper ids.
174 lgl_db_get_paper_id_list (void)
185 for ( p=papers; p != NULL; p=p->next )
187 paper = (lglPaper *)p->data;
188 ids = g_list_append (ids, g_strdup (paper->id));
196 * lgl_db_free_paper_id_list:
197 * @ids: List of id strings to be freed.
199 * Free up all storage associated with an id list obtained with
200 * lgl_db_get_paper_id_list().
204 lgl_db_free_paper_id_list (GList *ids)
208 for (p = ids; p != NULL; p = p->next)
219 * lgl_db_get_paper_name_list:
221 * Get a list of all localized paper names known to libglabels.
223 * Returns: a list of localized paper names.
227 lgl_db_get_paper_name_list (void)
238 for ( p=papers; p != NULL; p=p->next )
240 paper = (lglPaper *)p->data;
241 names = g_list_append (names, g_strdup (paper->name));
249 * lgl_db_free_paper_name_list:
250 * @names: List of localized paper name strings to be freed.
252 * Free up all storage associated with a name list obtained with
253 * lgl_db_get_paper_name_list().
257 lgl_db_free_paper_name_list (GList *names)
261 for (p = names; p != NULL; p = p->next)
272 * lgl_db_lookup_paper_from_name:
273 * @name: localized paper name string
275 * Lookup paper definition from localized paper name string.
277 * Returns: pointer to a newly allocated #lglPaper structure.
281 lgl_db_lookup_paper_from_name (const gchar *name)
293 /* If no name, return first paper as a default */
294 return lgl_paper_dup ((lglPaper *) papers->data);
297 for (p = papers; p != NULL; p = p->next)
299 paper = (lglPaper *) p->data;
300 if (UTF8_EQUAL (paper->name, name))
302 return lgl_paper_dup (paper);
311 * lgl_db_lookup_paper_from_id:
312 * @id: paper id string
314 * Lookup paper definition from id string.
316 * Returns: pointer to a newly allocated #lglPaper structure.
320 lgl_db_lookup_paper_from_id (const gchar *id)
332 /* If no id, return first paper as a default */
333 return lgl_paper_dup ((lglPaper *) papers->data);
336 for (p = papers; p != NULL; p = p->next)
338 paper = (lglPaper *) p->data;
339 if (ASCII_EQUAL (paper->id, id))
341 return lgl_paper_dup (paper);
350 * lgl_db_lookup_paper_id_from_name:
351 * @name: localized paper name stringp
353 * Lookup paper name string from localized paper name string.
355 * Returns: pointer to a newly allocated id string.
359 lgl_db_lookup_paper_id_from_name (const gchar *name)
361 lglPaper *paper = NULL;
366 paper = lgl_db_lookup_paper_from_name (name);
369 id = g_strdup (paper->id);
370 lgl_paper_free (paper);
380 * lgl_db_lookup_paper_name_from_id:
381 * @id: paper id string
383 * Lookup localized paper name string from paper id string.
385 * Returns: pointer to a newly allocated localized paper name string.
389 lgl_db_lookup_paper_name_from_id (const gchar *id)
391 lglPaper *paper = NULL;
396 paper = lgl_db_lookup_paper_from_id (id);
399 name = g_strdup (paper->name);
400 lgl_paper_free (paper);
410 * lgl_db_is_paper_id_known:
411 * @id: paper id to test
413 * Determine if given paper id is known to libglabels.
415 * Returns: TRUE if id is known, otherwise FALSE.
419 lgl_db_is_paper_id_known (const gchar *id)
434 for (p = papers; p != NULL; p = p->next)
436 paper = (lglPaper *) p->data;
437 if (ASCII_EQUAL (paper->id, id))
447 * lgl_db_is_paper_id_other:
448 * @id: paper id to test
450 * Determine if given paper id is the special id "Other."
452 * Returns: TRUE if id is "Other", otherwise FALSE.
456 lgl_db_is_paper_id_other (const gchar *id)
463 return (ASCII_EQUAL (id, "Other"));
471 GList *papers = NULL;
473 data_dir = LGL_SYSTEM_DATA_DIR;
474 papers = read_paper_files_from_dir (papers, data_dir);
477 data_dir = LGL_USER_DATA_DIR;
478 papers = read_paper_files_from_dir (papers, data_dir);
481 if (papers == NULL) {
482 g_critical (_("Unable to locate paper size definitions. Libglabels may not be installed correctly!"));
490 read_paper_files_from_dir (GList *papers,
491 const gchar *dirname)
494 const gchar *filename, *extension;
495 gchar *full_filename = NULL;
496 GError *gerror = NULL;
497 GList *new_papers = NULL;
499 if (dirname == NULL) {
503 if (!g_file_test (dirname, G_FILE_TEST_EXISTS)) {
507 dp = g_dir_open (dirname, 0, &gerror);
508 if (gerror != NULL) {
509 g_message ("cannot open data directory: %s", gerror->message );
513 while ((filename = g_dir_read_name (dp)) != NULL) {
515 extension = strrchr (filename, '.');
517 if (extension != NULL) {
519 if ( ASCII_EQUAL (extension, ".paper") ||
520 ASCII_EQUAL (filename, "paper-sizes.xml") )
524 g_build_filename (dirname, filename, NULL);
526 lgl_xml_paper_read_papers_from_file (full_filename);
527 g_free (full_filename);
529 papers = g_list_concat (papers, new_papers);
545 * lgl_db_print_known_papers:
547 * For debugging purposes: print a list of all paper definitions known to
552 lgl_db_print_known_papers (void)
561 g_print ("%s():\n", __FUNCTION__);
562 for (p = papers; p != NULL; p = p->next) {
563 paper = (lglPaper *) p->data;
565 g_print ("PAPER id=\"%s\", name=\"%s\", width=%gpts, height=%gpts\n",
566 paper->id, paper->name, paper->width, paper->height);
574 /*===========================================*/
575 /* Category db functions. */
576 /*===========================================*/
579 * lgl_db_get_category_id_list:
581 * Get a list of all category ids known to libglabels.
583 * Returns: a list of category ids.
587 lgl_db_get_category_id_list (void)
591 lglCategory *category;
598 for ( p=categories; p != NULL; p=p->next )
600 category = (lglCategory *)p->data;
601 ids = g_list_append (ids, g_strdup (category->id));
609 * lgl_db_free_category_id_list:
610 * @ids: List of id strings to be freed.
612 * Free up all storage associated with an id list obtained with
613 * lgl_db_get_category_id_list().
617 lgl_db_free_category_id_list (GList *ids)
621 for (p = ids; p != NULL; p = p->next)
632 * lgl_db_get_category_name_list:
634 * Get a list of all localized category names known to libglabels.
636 * Returns: a list of localized category names.
640 lgl_db_get_category_name_list (void)
644 lglCategory *category;
651 for ( p=categories; p != NULL; p=p->next )
653 category = (lglCategory *)p->data;
654 names = g_list_append (names, g_strdup (category->name));
662 * lgl_db_free_category_name_list:
663 * @names: List of localized category name strings to be freed.
665 * Free up all storage associated with a name list obtained with
666 * lgl_db_get_category_name_list().
670 lgl_db_free_category_name_list (GList *names)
674 for (p = names; p != NULL; p = p->next)
685 * lgl_db_lookup_category_from_name:
686 * @name: localized category name string
688 * Lookup category definition from localized category name string.
690 * Returns: pointer to a newly allocated #lglCategory structure.
694 lgl_db_lookup_category_from_name (const gchar *name)
697 lglCategory *category;
706 /* If no name, return first category as a default */
707 return lgl_category_dup ((lglCategory *) categories->data);
710 for (p = categories; p != NULL; p = p->next)
712 category = (lglCategory *) p->data;
713 if (UTF8_EQUAL (category->name, name))
715 return lgl_category_dup (category);
724 * lgl_db_lookup_category_from_id:
725 * @id: category id string
727 * Lookup category definition from id string.
729 * Returns: pointer to a newly allocated #lglCategory structure.
733 lgl_db_lookup_category_from_id (const gchar *id)
736 lglCategory *category;
745 /* If no id, return first category as a default */
746 return lgl_category_dup ((lglCategory *) categories->data);
749 for (p = categories; p != NULL; p = p->next)
751 category = (lglCategory *) p->data;
752 if (ASCII_EQUAL (category->id, id))
754 return lgl_category_dup (category);
763 * lgl_db_lookup_category_id_from_name:
764 * @name: localized category name stringp
766 * Lookup category name string from localized category name string.
768 * Returns: pointer to a newly allocated id string.
772 lgl_db_lookup_category_id_from_name (const gchar *name)
774 lglCategory *category = NULL;
779 category = lgl_db_lookup_category_from_name (name);
780 if ( category != NULL )
782 id = g_strdup (category->id);
783 lgl_category_free (category);
793 * lgl_db_lookup_category_name_from_id:
794 * @id: category id string
796 * Lookup localized category name string from category id string.
798 * Returns: pointer to a newly allocated localized category name string.
802 lgl_db_lookup_category_name_from_id (const gchar *id)
804 lglCategory *category = NULL;
809 category = lgl_db_lookup_category_from_id (id);
810 if ( category != NULL )
812 name = g_strdup (category->name);
813 lgl_category_free (category);
823 * lgl_db_is_category_id_known:
824 * @id: category id to test
826 * Determine if given category id is known to libglabels.
828 * Returns: TRUE if id is known, otherwise FALSE.
832 lgl_db_is_category_id_known (const gchar *id)
835 lglCategory *category;
847 for (p = categories; p != NULL; p = p->next)
849 category = (lglCategory *) p->data;
850 if (ASCII_EQUAL (category->id, id))
861 read_categories (void)
864 GList *categories = NULL;
866 data_dir = LGL_SYSTEM_DATA_DIR;
867 categories = read_category_files_from_dir (categories, data_dir);
870 data_dir = LGL_USER_DATA_DIR;
871 categories = read_category_files_from_dir (categories, data_dir);
874 if (categories == NULL) {
875 g_critical (_("Unable to locate category definitions. Libglabels may not be installed correctly!"));
883 read_category_files_from_dir (GList *categories,
884 const gchar *dirname)
887 const gchar *filename, *extension;
888 gchar *full_filename = NULL;
889 GError *gerror = NULL;
890 GList *new_categories = NULL;
892 if (dirname == NULL) {
896 if (!g_file_test (dirname, G_FILE_TEST_EXISTS)) {
900 dp = g_dir_open (dirname, 0, &gerror);
901 if (gerror != NULL) {
902 g_message ("cannot open data directory: %s", gerror->message );
906 while ((filename = g_dir_read_name (dp)) != NULL) {
908 extension = strrchr (filename, '.');
910 if (extension != NULL) {
912 if ( ASCII_EQUAL (extension, ".category") ||
913 ASCII_EQUAL (filename, "categories.xml") )
917 g_build_filename (dirname, filename, NULL);
919 lgl_xml_category_read_categories_from_file (full_filename);
920 g_free (full_filename);
922 categories = g_list_concat (categories, new_categories);
923 new_categories = NULL;
938 * lgl_db_print_known_categories:
940 * For debugging purposes: print a list of all category definitions known to
945 lgl_db_print_known_categories (void)
948 lglCategory *category;
954 g_print ("%s():\n", __FUNCTION__);
955 for (p = categories; p != NULL; p = p->next) {
956 category = (lglCategory *) p->data;
958 g_print ("CATEGORY id=\"%s\", name=\"%s\"\n", category->id, category->name);
967 /*===========================================*/
968 /* Brand db functions. */
969 /*===========================================*/
972 * lgl_db_get_brand_list:
973 * @paper_id: If non NULL, limit results to given page size.
974 * @category_id: If non NULL, limit results to given template category.
976 * Get a list of all valid brands of templates in the template database.
977 * Results can be filtered by page size and/or template category. A list of valid page
978 * sizes can be obtained using lgl_db_get_paper_id_list(). A list of valid template
979 * categories can be obtained using lgl_db_get_category_id_list().
981 * Returns: a list of brands
984 lgl_db_get_brand_list (const gchar *paper_id,
985 const gchar *category_id)
987 GList *p_tmplt, *p_alias;
988 lglTemplate *template;
989 lglTemplateAlias *alias;
990 GList *brands = NULL;
997 for (p_tmplt = templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
999 template = (lglTemplate *) p_tmplt->data;
1000 if (lgl_template_does_page_size_match (template, paper_id) &&
1001 lgl_template_does_category_match (template, category_id))
1003 for (p_alias = template->aliases; p_alias != NULL;
1004 p_alias = p_alias->next)
1006 alias = (lglTemplateAlias *)p_alias->data;
1008 if ( !g_list_find_custom (brands, alias->brand,
1009 (GCompareFunc)lgl_str_utf8_casecmp) )
1011 brands = g_list_insert_sorted (brands,
1012 g_strdup (alias->brand),
1013 (GCompareFunc)lgl_str_utf8_casecmp);
1024 * lgl_db_free_brand_list:
1025 * @brands: List of template brand strings to be freed.
1027 * Free up all storage associated with a list of template names obtained with
1028 * lgl_db_get_brand_list().
1032 lgl_db_free_brand_list (GList *brands)
1036 for (p_brand = brands; p_brand != NULL; p_brand = p_brand->next)
1038 g_free (p_brand->data);
1039 p_brand->data = NULL;
1042 g_list_free (brands);
1047 /*===========================================*/
1048 /* Template db functions. */
1049 /*===========================================*/
1052 * lgl_db_register_template:
1053 * @template: Pointer to a template structure to add to database.
1055 * Register a template. This function adds a template to the template database.
1056 * The template will be stored in an individual XML file in the user template directory.
1058 * Returns: Status of registration attempt (#lglDbRegStatus)
1061 lgl_db_register_template (const lglTemplate *template)
1063 lglTemplate *template_copy;
1064 gchar *dir, *filename, *abs_filename;
1072 if (lgl_db_does_template_exist (template->brand, template->part))
1074 return LGL_DB_REG_BRAND_PART_EXISTS;
1077 if (lgl_db_is_paper_id_known (template->paper_id))
1079 dir = LGL_USER_DATA_DIR;
1080 mkdir (dir, 0775); /* Try to make sure directory exists. */
1081 filename = g_strdup_printf ("%s_%s.template", template->brand, template->part);
1082 abs_filename = g_build_filename (dir, filename, NULL);
1083 bytes_written = lgl_xml_template_write_template_to_file (template, abs_filename);
1086 g_free (abs_filename);
1088 if (bytes_written > 0)
1090 template_copy = lgl_template_dup (template);
1091 lgl_template_add_category (template_copy, "user-defined");
1092 templates = g_list_append (templates, template_copy);
1093 add_to_template_cache (template_copy);
1094 return LGL_DB_REG_OK;
1098 return LGL_DB_REG_FILE_WRITE_ERROR;
1103 g_message ("Cannot register new template with unknown page size.");
1104 return LGL_DB_REG_BAD_PAPER_ID;
1111 * lgl_db_does_template_exist:
1112 * @brand: Brand name.
1113 * @part: Part name/number.
1115 * This function tests whether a template with the given brand and part name/number exists.
1117 * Returns: TRUE if such a template exists in the database.
1120 lgl_db_does_template_exist (const gchar *brand,
1123 GList *p_tmplt, *p_alias;
1124 lglTemplate *template;
1125 lglTemplateAlias *alias;
1132 if ((brand == NULL) || (part == NULL))
1137 for (p_tmplt = templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1139 template = (lglTemplate *) p_tmplt->data;
1140 for (p_alias = template->aliases; p_alias != NULL; p_alias = p_alias->next)
1142 alias = (lglTemplateAlias *)p_alias->data;
1144 if ( UTF8_EQUAL (brand, alias->brand) &&
1145 UTF8_EQUAL (part, alias->part) )
1157 * lgl_db_does_template_name_exist:
1158 * @name: name string
1160 * This function test whether a template with the given name exists.
1162 * Returns: TRUE if such a template exists in the database.
1166 lgl_db_does_template_name_exist (const gchar *name)
1168 GList *p_tmplt, *p_alias;
1169 lglTemplate *template;
1170 lglTemplateAlias *alias;
1171 gchar *candidate_name;
1183 for (p_tmplt = templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1185 template = (lglTemplate *) p_tmplt->data;
1186 for (p_alias = template->aliases; p_alias != NULL; p_alias = p_alias->next)
1188 alias = (lglTemplateAlias *)p_alias->data;
1189 candidate_name = g_strdup_printf ("%s %s", alias->brand, alias->part);
1191 if ( UTF8_EQUAL (candidate_name, name) ) {
1192 g_free (candidate_name);
1195 g_free (candidate_name);
1204 * lgl_db_get_template_name_list_unique:
1205 * @brand: If non NULL, limit results to given brand
1206 * @paper_id: If non NULL, limit results to given page size.
1207 * @category_id: If non NULL, limit results to given template category.
1209 * Get a list of valid names of unique templates in the template database. Results
1210 * can be filtered by page size and/or template category. A list of valid page sizes
1211 * can be obtained using lgl_db_get_paper_id_list(). A list of valid template categories
1212 * can be obtained using lgl_db_get_category_id_list().
1214 * This function differs from lgl_db_get_template_name_list_all(), because it does not
1215 * return multiple names for the same template.
1217 * Returns: a list of template names.
1220 lgl_db_get_template_name_list_unique (const gchar *brand,
1221 const gchar *paper_id,
1222 const gchar *category_id)
1225 lglTemplate *template;
1227 GList *names = NULL;
1234 for (p_tmplt = templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1236 template = (lglTemplate *) p_tmplt->data;
1238 if (lgl_template_does_brand_match (template, brand) &&
1239 lgl_template_does_page_size_match (template, paper_id) &&
1240 lgl_template_does_category_match (template, category_id))
1242 name = g_strdup_printf ("%s %s", template->brand, template->part);
1243 names = g_list_insert_sorted (names, name,
1244 (GCompareFunc)g_utf8_collate);
1253 * lgl_db_get_template_name_list_all:
1254 * @brand: If non NULL, limit results to given brand
1255 * @paper_id: If non NULL, limit results to given page size.
1256 * @category_id: If non NULL, limit results to given template category.
1258 * Get a list of all valid names and aliases of templates in the template database.
1259 * Results can be filtered by page size and/or template category. A list of valid page
1260 * sizes can be obtained using lgl_db_get_paper_id_list(). A list of valid template
1261 * categories can be obtained using lgl_db_get_category_id_list().
1263 * This function differs from lgl_db_get_template_name_list_unique(), because it will
1264 * return multiple names for the same template.
1266 * Returns: a list of template names and aliases.
1269 lgl_db_get_template_name_list_all (const gchar *brand,
1270 const gchar *paper_id,
1271 const gchar *category_id)
1273 GList *p_tmplt, *p_alias;
1274 lglTemplate *template;
1275 lglTemplateAlias *alias;
1277 GList *names = NULL;
1284 for (p_tmplt = templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1286 template = (lglTemplate *) p_tmplt->data;
1287 if (lgl_template_does_page_size_match (template, paper_id) &&
1288 lgl_template_does_category_match (template, category_id))
1290 for (p_alias = template->aliases; p_alias != NULL;
1291 p_alias = p_alias->next)
1293 alias = (lglTemplateAlias *)p_alias->data;
1295 if ( !brand || UTF8_EQUAL( alias->brand, brand) )
1297 name = g_strdup_printf ("%s %s", alias->brand, alias->part);
1298 names = g_list_insert_sorted (names, name,
1299 (GCompareFunc)g_utf8_collate);
1310 * lgl_db_free_template_name_list:
1311 * @names: List of template name strings to be freed.
1313 * Free up all storage associated with a list of template names obtained with
1314 * lgl_db_get_template_name_list_all() or lgl_db_get_template_name_list_unique().
1318 lgl_db_free_template_name_list (GList *names)
1322 for (p_name = names; p_name != NULL; p_name = p_name->next)
1324 g_free (p_name->data);
1325 p_name->data = NULL;
1328 g_list_free (names);
1333 * lgl_db_lookup_template_from_name:
1334 * @name: name string
1336 * Lookup template in template database from name string.
1338 * Returns: pointer to a newly allocated #lglTemplate structure.
1342 lgl_db_lookup_template_from_name (const gchar *name)
1345 lglTemplate *template;
1346 lglTemplateAlias *alias;
1347 gchar *candidate_name;
1348 lglTemplate *new_template;
1357 /* If no name, return first template as a default */
1358 return lgl_template_dup ((lglTemplate *) templates->data);
1361 template = g_hash_table_lookup (template_cache, name);
1365 for (p_alias = template->aliases; p_alias != NULL; p_alias = p_alias->next)
1367 alias = (lglTemplateAlias *)p_alias->data;
1368 candidate_name = g_strdup_printf ("%s %s", alias->brand, alias->part);
1370 if ( UTF8_EQUAL (candidate_name, name) )
1372 g_free (candidate_name);
1373 new_template = lgl_template_dup (template);
1374 g_free (new_template->brand);
1375 new_template->brand = g_strdup (alias->brand);
1376 g_free (new_template->part);
1377 new_template->part = g_strdup (alias->part);
1378 return new_template;
1381 g_free (candidate_name);
1385 /* No matching template has been found so return the first template */
1386 return lgl_template_dup ((lglTemplate *) templates->data);
1391 init_template_cache (void)
1393 GList *p_tmplt, *p_alias;
1394 lglTemplate *template;
1395 lglTemplateAlias *alias;
1398 template_cache = g_hash_table_new (g_str_hash, g_str_equal);
1400 for ( p_tmplt=templates; p_tmplt != NULL; p_tmplt=p_tmplt->next )
1402 template = (lglTemplate *) p_tmplt->data;
1404 for ( p_alias=template->aliases; p_alias != NULL; p_alias=p_alias->next )
1406 alias = (lglTemplateAlias *)p_alias->data;
1407 name = g_strdup_printf ("%s %s", alias->brand, alias->part);
1409 g_hash_table_insert (template_cache, name, template);
1416 add_to_template_cache (lglTemplate *template)
1419 lglTemplateAlias *alias;
1422 for ( p_alias=template->aliases; p_alias != NULL; p_alias=p_alias->next )
1424 alias = (lglTemplateAlias *)p_alias->data;
1425 name = g_strdup_printf ("%s %s", alias->brand, alias->part);
1427 g_hash_table_insert (template_cache, name, template);
1433 read_templates (void)
1436 GList *templates = NULL;
1438 lglTemplate *template;
1441 * User defined templates. Add to user-defined category.
1443 data_dir = LGL_USER_DATA_DIR;
1444 templates = read_template_files_from_dir (templates, data_dir);
1446 for ( p=templates; p != NULL; p=p->next )
1448 template = (lglTemplate *)p->data;
1449 lgl_template_add_category (template, "user-defined");
1455 data_dir = LGL_SYSTEM_DATA_DIR;
1456 templates = read_template_files_from_dir (templates, data_dir);
1459 if (templates == NULL)
1461 g_critical (_("Unable to locate any template files. Libglabels may not be installed correctly!"));
1469 read_template_files_from_dir (GList *templates,
1470 const gchar *dirname)
1473 const gchar *filename, *extension, *extension2;
1474 gchar *full_filename = NULL;
1475 GError *gerror = NULL;
1476 GList *new_templates = NULL;
1478 if (dirname == NULL)
1481 if (!g_file_test (dirname, G_FILE_TEST_EXISTS))
1486 dp = g_dir_open (dirname, 0, &gerror);
1489 g_message ("cannot open data directory: %s", gerror->message );
1493 while ((filename = g_dir_read_name (dp)) != NULL)
1496 extension = strrchr (filename, '.');
1497 extension2 = strrchr (filename, '-');
1499 if ( (extension && ASCII_EQUAL (extension, ".template")) ||
1500 (extension2 && ASCII_EQUAL (extension2, "-templates.xml")) )
1503 full_filename = g_build_filename (dirname, filename, NULL);
1505 lgl_xml_template_read_templates_from_file (full_filename);
1506 g_free (full_filename);
1508 templates = g_list_concat (templates, new_templates);
1509 new_templates = NULL;
1520 static lglTemplate *
1521 template_full_page (const gchar *paper_id)
1523 lglPaper *paper = NULL;
1524 lglTemplate *template = NULL;
1525 lglTemplateFrame *frame = NULL;
1529 g_return_val_if_fail (paper_id, NULL);
1531 paper = lgl_db_lookup_paper_from_id (paper_id);
1532 if ( paper == NULL )
1537 part = g_strdup_printf ("%s-Full-Page", paper->id);
1538 desc = g_strdup_printf (_("Generic %s full page template"), paper->name);
1540 template = lgl_template_new ("Generic", part, desc,
1541 paper_id, paper->width, paper->height);
1544 frame = lgl_template_frame_rect_new ("0",
1550 lgl_template_add_frame (template, frame);
1552 lgl_template_frame_add_layout (frame, lgl_template_layout_new (1, 1, 0., 0., 0., 0.));
1554 lgl_template_frame_add_markup (frame, lgl_template_markup_margin_new (9.0));
1558 lgl_paper_free (paper);
1566 * lgl_db_print_known_templates:
1568 * Print all known templates (for debugging purposes).
1572 lgl_db_print_known_templates (void)
1575 lglTemplate *template;
1577 g_print ("%s():\n", __FUNCTION__);
1578 for (p=templates; p!=NULL; p=p->next)
1580 template = (lglTemplate *)p->data;
1582 g_print("TEMPLATE brand=\"%s\", part=\"%s\", description=\"%s\"\n",
1583 template->brand, template->part, template->description);
1591 * lgl_db_print_aliases:
1592 * @template: template
1594 * Print all aliases of a template (for debugging purposes).
1598 lgl_db_print_aliases (const lglTemplate *template)
1601 lglTemplateAlias *alias;
1603 g_print ("%s():\n", __FUNCTION__);
1604 for (p=template->aliases; p!=NULL; p=p->next)
1606 alias = (lglTemplateAlias *)p->data;
1608 g_print("Alias: brand=\"%s\", part=\"%s\"\n", alias->brand, alias->part);