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>
38 #include <sys/types.h>
40 #include "libglabels-private.h"
42 #include "xml-paper.h"
43 #include "xml-category.h"
44 #include "xml-template.h"
47 /*===========================================*/
49 /*===========================================*/
51 /*===========================================*/
53 /*===========================================*/
55 static GList *papers = NULL;
56 static GList *categories = NULL;
57 static GList *templates = NULL;
59 /*===========================================*/
60 /* Local function prototypes */
61 /*===========================================*/
63 static GList *read_papers (void);
64 static GList *read_paper_files_from_dir (GList *papers,
65 const gchar *dirname);
67 static GList *read_categories (void);
68 static GList *read_category_files_from_dir (GList *categories,
69 const gchar *dirname);
71 static GList *read_templates (void);
72 static GList *read_template_files_from_dir (GList *templates,
73 const gchar *dirname);
75 static lglTemplate *template_full_page (const gchar *page_size);
79 /*===========================================*/
80 /* Module initialization */
81 /*===========================================*/
86 * Initialize all libglabels subsystems. It is not necessary for an application to call
87 * lgl_db_init(), because libglabels will initialize on demand. An application programmer may
88 * choose to call lgl_db_init() at startup to minimize the impact of the first libglabels call
89 * on GUI response time.
91 * This function initializes its paper definitions, category definitions, and its template
92 * database.. It will search both system and user template directories to locate
98 lglPaper *paper_other;
99 lglCategory *category_user_defined;
109 papers = read_papers ();
111 /* Create and append an "Other" entry. */
112 paper_other = lgl_paper_new ("Other", _("Other"), 0.0, 0.0);
113 papers = g_list_append (papers, paper_other);
122 categories = read_categories ();
124 /* Create and append a "User defined" entry. */
125 category_user_defined = lgl_category_new ("user-defined", _("User defined"));
126 categories = g_list_append (categories, category_user_defined);
135 templates = read_templates ();
137 /* Create and append generic full page templates. */
138 page_sizes = lgl_db_get_paper_id_list ();
139 for ( p=page_sizes; p != NULL; p=p->next )
141 if ( !lgl_db_is_paper_id_other (p->data) )
143 templates = g_list_append (templates,
144 template_full_page (p->data));
147 lgl_db_free_paper_id_list (page_sizes);
154 /*===========================================*/
155 /* Paper db functions. */
156 /*===========================================*/
159 * lgl_db_get_paper_id_list:
161 * Get a list of all paper ids known to libglabels.
163 * Returns: a list of paper ids.
167 lgl_db_get_paper_id_list (void)
178 for ( p=papers; p != NULL; p=p->next )
180 paper = (lglPaper *)p->data;
181 ids = g_list_append (ids, g_strdup (paper->id));
189 * lgl_db_free_paper_id_list:
190 * @ids: List of id strings to be freed.
192 * Free up all storage associated with an id list obtained with
193 * lgl_db_get_paper_id_list().
197 lgl_db_free_paper_id_list (GList *ids)
201 for (p = ids; p != NULL; p = p->next)
212 * lgl_db_get_paper_name_list:
214 * Get a list of all localized paper names known to libglabels.
216 * Returns: a list of localized paper names.
220 lgl_db_get_paper_name_list (void)
231 for ( p=papers; p != NULL; p=p->next )
233 paper = (lglPaper *)p->data;
234 names = g_list_append (names, g_strdup (paper->name));
242 * lgl_db_free_paper_name_list:
243 * @names: List of localized paper name strings to be freed.
245 * Free up all storage associated with a name list obtained with
246 * lgl_db_get_paper_name_list().
250 lgl_db_free_paper_name_list (GList *names)
254 for (p = names; p != NULL; p = p->next)
265 * lgl_db_lookup_paper_from_name:
266 * @name: localized paper name string
268 * Lookup paper definition from localized paper name string.
270 * Returns: pointer to a newly allocated #lglPaper structure.
274 lgl_db_lookup_paper_from_name (const gchar *name)
286 /* If no name, return first paper as a default */
287 return lgl_paper_dup ((lglPaper *) papers->data);
290 for (p = papers; p != NULL; p = p->next)
292 paper = (lglPaper *) p->data;
293 if (UTF8_EQUAL (paper->name, name))
295 return lgl_paper_dup (paper);
304 * lgl_db_lookup_paper_from_id:
305 * @id: paper id string
307 * Lookup paper definition from id string.
309 * Returns: pointer to a newly allocated #lglPaper structure.
313 lgl_db_lookup_paper_from_id (const gchar *id)
325 /* If no id, return first paper as a default */
326 return lgl_paper_dup ((lglPaper *) papers->data);
329 for (p = papers; p != NULL; p = p->next)
331 paper = (lglPaper *) p->data;
332 if (ASCII_EQUAL (paper->id, id))
334 return lgl_paper_dup (paper);
343 * lgl_db_lookup_paper_id_from_name:
344 * @name: localized paper name stringp
346 * Lookup paper name string from localized paper name string.
348 * Returns: pointer to a newly allocated id string.
352 lgl_db_lookup_paper_id_from_name (const gchar *name)
354 lglPaper *paper = NULL;
359 paper = lgl_db_lookup_paper_from_name (name);
362 id = g_strdup (paper->id);
363 lgl_paper_free (paper);
373 * lgl_db_lookup_paper_name_from_id:
374 * @id: paper id string
376 * Lookup localized paper name string from paper id string.
378 * Returns: pointer to a newly allocated localized paper name string.
382 lgl_db_lookup_paper_name_from_id (const gchar *id)
384 lglPaper *paper = NULL;
389 paper = lgl_db_lookup_paper_from_id (id);
392 name = g_strdup (paper->name);
393 lgl_paper_free (paper);
403 * lgl_db_is_paper_id_known:
404 * @id: paper id to test
406 * Determine if given paper id is known to libglabels.
408 * Returns: TRUE if id is known, otherwise FALSE.
412 lgl_db_is_paper_id_known (const gchar *id)
427 for (p = papers; p != NULL; p = p->next)
429 paper = (lglPaper *) p->data;
430 if (ASCII_EQUAL (paper->id, id))
440 * lgl_db_is_paper_id_other:
441 * @id: paper id to test
443 * Determine if given paper id is the special id "Other."
445 * Returns: TRUE if id is "Other", otherwise FALSE.
449 lgl_db_is_paper_id_other (const gchar *id)
456 return (ASCII_EQUAL (id, "Other"));
464 GList *papers = NULL;
466 data_dir = LGL_SYSTEM_DATA_DIR;
467 papers = read_paper_files_from_dir (papers, data_dir);
470 data_dir = LGL_USER_DATA_DIR;
471 papers = read_paper_files_from_dir (papers, data_dir);
474 if (papers == NULL) {
475 g_critical (_("Unable to locate paper size definitions. Libglabels may not be installed correctly!"));
483 read_paper_files_from_dir (GList *papers,
484 const gchar *dirname)
487 const gchar *filename, *extension;
488 gchar *full_filename = NULL;
489 GError *gerror = NULL;
490 GList *new_papers = NULL;
492 if (dirname == NULL) {
496 if (!g_file_test (dirname, G_FILE_TEST_EXISTS)) {
500 dp = g_dir_open (dirname, 0, &gerror);
501 if (gerror != NULL) {
502 g_message ("cannot open data directory: %s", gerror->message );
506 while ((filename = g_dir_read_name (dp)) != NULL) {
508 extension = strrchr (filename, '.');
510 if (extension != NULL) {
512 if ( ASCII_EQUAL (extension, ".paper") ||
513 ASCII_EQUAL (filename, "paper-sizes.xml") )
517 g_build_filename (dirname, filename, NULL);
519 lgl_xml_paper_read_papers_from_file (full_filename);
520 g_free (full_filename);
522 papers = g_list_concat (papers, new_papers);
538 * lgl_db_print_known_papers:
540 * For debugging purposes: print a list of all paper definitions known to
545 lgl_db_print_known_papers (void)
554 g_print ("%s():\n", __FUNCTION__);
555 for (p = papers; p != NULL; p = p->next) {
556 paper = (lglPaper *) p->data;
558 g_print ("PAPER id=\"%s\", name=\"%s\", width=%gpts, height=%gpts\n",
559 paper->id, paper->name, paper->width, paper->height);
567 /*===========================================*/
568 /* Category db functions. */
569 /*===========================================*/
572 * lgl_db_get_category_id_list:
574 * Get a list of all category ids known to libglabels.
576 * Returns: a list of category ids.
580 lgl_db_get_category_id_list (void)
584 lglCategory *category;
591 for ( p=categories; p != NULL; p=p->next )
593 category = (lglCategory *)p->data;
594 ids = g_list_append (ids, g_strdup (category->id));
602 * lgl_db_free_category_id_list:
603 * @ids: List of id strings to be freed.
605 * Free up all storage associated with an id list obtained with
606 * lgl_db_get_category_id_list().
610 lgl_db_free_category_id_list (GList *ids)
614 for (p = ids; p != NULL; p = p->next)
625 * lgl_db_get_category_name_list:
627 * Get a list of all localized category names known to libglabels.
629 * Returns: a list of localized category names.
633 lgl_db_get_category_name_list (void)
637 lglCategory *category;
644 for ( p=categories; p != NULL; p=p->next )
646 category = (lglCategory *)p->data;
647 names = g_list_append (names, g_strdup (category->name));
655 * lgl_db_free_category_name_list:
656 * @names: List of localized category name strings to be freed.
658 * Free up all storage associated with a name list obtained with
659 * lgl_db_get_category_name_list().
663 lgl_db_free_category_name_list (GList *names)
667 for (p = names; p != NULL; p = p->next)
678 * lgl_db_lookup_category_from_name:
679 * @name: localized category name string
681 * Lookup category definition from localized category name string.
683 * Returns: pointer to a newly allocated #lglCategory structure.
687 lgl_db_lookup_category_from_name (const gchar *name)
690 lglCategory *category;
699 /* If no name, return first category as a default */
700 return lgl_category_dup ((lglCategory *) categories->data);
703 for (p = categories; p != NULL; p = p->next)
705 category = (lglCategory *) p->data;
706 if (UTF8_EQUAL (category->name, name))
708 return lgl_category_dup (category);
717 * lgl_db_lookup_category_from_id:
718 * @id: category id string
720 * Lookup category definition from id string.
722 * Returns: pointer to a newly allocated #lglCategory structure.
726 lgl_db_lookup_category_from_id (const gchar *id)
729 lglCategory *category;
738 /* If no id, return first category as a default */
739 return lgl_category_dup ((lglCategory *) categories->data);
742 for (p = categories; p != NULL; p = p->next)
744 category = (lglCategory *) p->data;
745 if (ASCII_EQUAL (category->id, id))
747 return lgl_category_dup (category);
756 * lgl_db_lookup_category_id_from_name:
757 * @name: localized category name stringp
759 * Lookup category name string from localized category name string.
761 * Returns: pointer to a newly allocated id string.
765 lgl_db_lookup_category_id_from_name (const gchar *name)
767 lglCategory *category = NULL;
772 category = lgl_db_lookup_category_from_name (name);
773 if ( category != NULL )
775 id = g_strdup (category->id);
776 lgl_category_free (category);
786 * lgl_db_lookup_category_name_from_id:
787 * @id: category id string
789 * Lookup localized category name string from category id string.
791 * Returns: pointer to a newly allocated localized category name string.
795 lgl_db_lookup_category_name_from_id (const gchar *id)
797 lglCategory *category = NULL;
802 category = lgl_db_lookup_category_from_id (id);
803 if ( category != NULL )
805 name = g_strdup (category->name);
806 lgl_category_free (category);
816 * lgl_db_is_category_id_known:
817 * @id: category id to test
819 * Determine if given category id is known to libglabels.
821 * Returns: TRUE if id is known, otherwise FALSE.
825 lgl_db_is_category_id_known (const gchar *id)
828 lglCategory *category;
840 for (p = categories; p != NULL; p = p->next)
842 category = (lglCategory *) p->data;
843 if (ASCII_EQUAL (category->id, id))
854 read_categories (void)
857 GList *categories = NULL;
859 data_dir = LGL_SYSTEM_DATA_DIR;
860 categories = read_category_files_from_dir (categories, data_dir);
863 data_dir = LGL_USER_DATA_DIR;
864 categories = read_category_files_from_dir (categories, data_dir);
867 if (categories == NULL) {
868 g_critical (_("Unable to locate category definitions. Libglabels may not be installed correctly!"));
876 read_category_files_from_dir (GList *categories,
877 const gchar *dirname)
880 const gchar *filename, *extension;
881 gchar *full_filename = NULL;
882 GError *gerror = NULL;
883 GList *new_categories = NULL;
885 if (dirname == NULL) {
889 if (!g_file_test (dirname, G_FILE_TEST_EXISTS)) {
893 dp = g_dir_open (dirname, 0, &gerror);
894 if (gerror != NULL) {
895 g_message ("cannot open data directory: %s", gerror->message );
899 while ((filename = g_dir_read_name (dp)) != NULL) {
901 extension = strrchr (filename, '.');
903 if (extension != NULL) {
905 if ( ASCII_EQUAL (extension, ".category") ||
906 ASCII_EQUAL (filename, "categories.xml") )
910 g_build_filename (dirname, filename, NULL);
912 lgl_xml_category_read_categories_from_file (full_filename);
913 g_free (full_filename);
915 categories = g_list_concat (categories, new_categories);
916 new_categories = NULL;
931 * lgl_db_print_known_categories:
933 * For debugging purposes: print a list of all category definitions known to
938 lgl_db_print_known_categories (void)
941 lglCategory *category;
947 g_print ("%s():\n", __FUNCTION__);
948 for (p = categories; p != NULL; p = p->next) {
949 category = (lglCategory *) p->data;
951 g_print ("CATEGORY id=\"%s\", name=\"%s\"\n", category->id, category->name);
960 /*===========================================*/
961 /* Brand db functions. */
962 /*===========================================*/
965 * lgl_db_get_brand_list:
966 * @paper_id: If non NULL, limit results to given page size.
967 * @category_id: If non NULL, limit results to given template category.
969 * Get a list of all valid brands of templates in the template database.
970 * Results can be filtered by page size and/or template category. A list of valid page
971 * sizes can be obtained using lgl_db_get_paper_id_list(). A list of valid template
972 * categories can be obtained using lgl_db_get_category_id_list().
974 * Returns: a list of brands
977 lgl_db_get_brand_list (const gchar *paper_id,
978 const gchar *category_id)
980 GList *p_tmplt, *p_alias;
981 lglTemplate *template;
982 lglTemplateAlias *alias;
983 GList *brands = NULL;
990 for (p_tmplt = templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
992 template = (lglTemplate *) p_tmplt->data;
993 if (lgl_template_does_page_size_match (template, paper_id) &&
994 lgl_template_does_category_match (template, category_id))
996 for (p_alias = template->aliases; p_alias != NULL;
997 p_alias = p_alias->next)
999 alias = (lglTemplateAlias *)p_alias->data;
1001 if ( !g_list_find_custom (brands, alias->brand,
1002 (GCompareFunc)lgl_str_utf8_casecmp) )
1004 brands = g_list_insert_sorted (brands,
1005 g_strdup (alias->brand),
1006 (GCompareFunc)lgl_str_utf8_casecmp);
1017 * lgl_db_free_brand_list:
1018 * @brands: List of template brand strings to be freed.
1020 * Free up all storage associated with a list of template names obtained with
1021 * lgl_db_get_brand_list().
1025 lgl_db_free_brand_list (GList *brands)
1029 for (p_brand = brands; p_brand != NULL; p_brand = p_brand->next)
1031 g_free (p_brand->data);
1032 p_brand->data = NULL;
1035 g_list_free (brands);
1040 /*===========================================*/
1041 /* Template db functions. */
1042 /*===========================================*/
1045 * lgl_db_register_template:
1046 * @template: Pointer to a template structure to add to database.
1048 * Register a template. This function adds a template to the template database.
1049 * The template will be stored in an individual XML file in the user template directory.
1051 * Returns: Status of registration attempt (#lglDbRegStatus)
1054 lgl_db_register_template (const lglTemplate *template)
1056 lglTemplate *template_copy;
1057 gchar *dir, *filename, *abs_filename;
1065 if (lgl_db_does_template_exist (template->brand, template->part))
1067 return LGL_DB_REG_BRAND_PART_EXISTS;
1070 if (lgl_db_is_paper_id_known (template->paper_id))
1072 dir = LGL_USER_DATA_DIR;
1073 mkdir (dir, 0775); /* Try to make sure directory exists. */
1074 filename = g_strdup_printf ("%s_%s.template", template->brand, template->part);
1075 abs_filename = g_build_filename (dir, filename, NULL);
1076 bytes_written = lgl_xml_template_write_template_to_file (template, abs_filename);
1079 g_free (abs_filename);
1081 if (bytes_written > 0)
1083 template_copy = lgl_template_dup (template);
1084 lgl_template_add_category (template_copy, "user-defined");
1085 templates = g_list_append (templates, template_copy);
1086 return LGL_DB_REG_OK;
1090 lgl_template_free (template_copy);
1091 return LGL_DB_REG_FILE_WRITE_ERROR;
1096 g_message ("Cannot register new template with unknown page size.");
1097 return LGL_DB_REG_BAD_PAPER_ID;
1104 * lgl_db_does_template_exist:
1105 * @brand: Brand name.
1106 * @part: Part name/number.
1108 * This function tests whether a template with the given brand and part name/number exists.
1110 * Returns: TRUE if such a template exists in the database.
1113 lgl_db_does_template_exist (const gchar *brand,
1116 GList *p_tmplt, *p_alias;
1117 lglTemplate *template;
1118 lglTemplateAlias *alias;
1125 if ((brand == NULL) || (part == NULL))
1130 for (p_tmplt = templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1132 template = (lglTemplate *) p_tmplt->data;
1133 for (p_alias = template->aliases; p_alias != NULL; p_alias = p_alias->next)
1135 alias = (lglTemplateAlias *)p_alias->data;
1137 if ( UTF8_EQUAL (brand, alias->brand) &&
1138 UTF8_EQUAL (part, alias->part) )
1150 * lgl_db_get_template_name_list_unique:
1151 * @brand: If non NULL, limit results to given brand
1152 * @paper_id: If non NULL, limit results to given page size.
1153 * @category_id: If non NULL, limit results to given template category.
1155 * Get a list of valid names of unique templates in the template database. Results
1156 * can be filtered by page size and/or template category. A list of valid page sizes
1157 * can be obtained using lgl_db_get_paper_id_list(). A list of valid template categories
1158 * can be obtained using lgl_db_get_category_id_list().
1160 * This function differs from lgl_db_get_template_name_list_all(), because it does not
1161 * return multiple names for the same template.
1163 * Returns: a list of template names.
1166 lgl_db_get_template_name_list_unique (const gchar *brand,
1167 const gchar *paper_id,
1168 const gchar *category_id)
1171 lglTemplate *template;
1173 GList *names = NULL;
1180 for (p_tmplt = templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1182 template = (lglTemplate *) p_tmplt->data;
1184 if (lgl_template_does_brand_match (template, brand) &&
1185 lgl_template_does_page_size_match (template, paper_id) &&
1186 lgl_template_does_category_match (template, category_id))
1188 name = g_strdup_printf ("%s %s", template->brand, template->part);
1189 names = g_list_insert_sorted (names, name,
1190 (GCompareFunc)g_utf8_collate);
1199 * lgl_db_get_template_name_list_all:
1200 * @brand: If non NULL, limit results to given brand
1201 * @paper_id: If non NULL, limit results to given page size.
1202 * @category_id: If non NULL, limit results to given template category.
1204 * Get a list of all valid names and aliases of templates in the template database.
1205 * Results can be filtered by page size and/or template category. A list of valid page
1206 * sizes can be obtained using lgl_db_get_paper_id_list(). A list of valid template
1207 * categories can be obtained using lgl_db_get_category_id_list().
1209 * This function differs from lgl_db_get_template_name_list_unique(), because it will
1210 * return multiple names for the same template.
1212 * Returns: a list of template names and aliases.
1215 lgl_db_get_template_name_list_all (const gchar *brand,
1216 const gchar *paper_id,
1217 const gchar *category_id)
1219 GList *p_tmplt, *p_alias;
1220 lglTemplate *template;
1221 lglTemplateAlias *alias;
1223 GList *names = NULL;
1230 for (p_tmplt = templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1232 template = (lglTemplate *) p_tmplt->data;
1233 if (lgl_template_does_page_size_match (template, paper_id) &&
1234 lgl_template_does_category_match (template, category_id))
1236 for (p_alias = template->aliases; p_alias != NULL;
1237 p_alias = p_alias->next)
1239 alias = (lglTemplateAlias *)p_alias->data;
1241 if ( !brand || UTF8_EQUAL( alias->brand, brand) )
1243 name = g_strdup_printf ("%s %s", alias->brand, alias->part);
1244 names = g_list_insert_sorted (names, name,
1245 (GCompareFunc)g_utf8_collate);
1256 * lgl_db_free_template_name_list:
1257 * @names: List of template name strings to be freed.
1259 * Free up all storage associated with a list of template names obtained with
1260 * lgl_db_get_template_name_list_all() or lgl_db_get_template_name_list_unique().
1264 lgl_db_free_template_name_list (GList *names)
1268 for (p_name = names; p_name != NULL; p_name = p_name->next)
1270 g_free (p_name->data);
1271 p_name->data = NULL;
1274 g_list_free (names);
1279 * lgl_db_lookup_template_from_name:
1280 * @name: name string
1282 * Lookup template in template database from name string.
1284 * Returns: pointer to a newly allocated #lglTemplate structure.
1288 lgl_db_lookup_template_from_name (const gchar *name)
1290 GList *p_tmplt, *p_alias;
1291 lglTemplate *template;
1292 lglTemplateAlias *alias;
1293 gchar *candidate_name;
1302 /* If no name, return first template as a default */
1303 return lgl_template_dup ((lglTemplate *) templates->data);
1306 for (p_tmplt = templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1308 template = (lglTemplate *) p_tmplt->data;
1309 for (p_alias = template->aliases; p_alias != NULL; p_alias = p_alias->next)
1311 alias = (lglTemplateAlias *)p_alias->data;
1312 candidate_name = g_strdup_printf ("%s %s", alias->brand, alias->part);
1314 if ( UTF8_EQUAL (candidate_name, name) ) {
1315 g_free (candidate_name);
1316 return lgl_template_dup (template);
1318 g_free (candidate_name);
1322 /* No matching template has been found so return the first template */
1323 return lgl_template_dup ((lglTemplate *) templates->data);
1328 read_templates (void)
1331 GList *templates = NULL;
1333 lglTemplate *template;
1336 * User defined templates. Add to user-defined category.
1338 data_dir = LGL_USER_DATA_DIR;
1339 templates = read_template_files_from_dir (templates, data_dir);
1341 for ( p=templates; p != NULL; p=p->next )
1343 template = (lglTemplate *)p->data;
1344 lgl_template_add_category (template, "user-defined");
1350 data_dir = LGL_SYSTEM_DATA_DIR;
1351 templates = read_template_files_from_dir (templates, data_dir);
1354 if (templates == NULL)
1356 g_critical (_("Unable to locate any template files. Libglabels may not be installed correctly!"));
1364 read_template_files_from_dir (GList *templates,
1365 const gchar *dirname)
1368 const gchar *filename, *extension, *extension2;
1369 gchar *full_filename = NULL;
1370 GError *gerror = NULL;
1371 GList *new_templates = NULL;
1373 if (dirname == NULL)
1376 if (!g_file_test (dirname, G_FILE_TEST_EXISTS))
1381 dp = g_dir_open (dirname, 0, &gerror);
1384 g_message ("cannot open data directory: %s", gerror->message );
1388 while ((filename = g_dir_read_name (dp)) != NULL)
1391 extension = strrchr (filename, '.');
1392 extension2 = strrchr (filename, '-');
1394 if ( (extension && ASCII_EQUAL (extension, ".template")) ||
1395 (extension2 && ASCII_EQUAL (extension2, "-templates.xml")) )
1398 full_filename = g_build_filename (dirname, filename, NULL);
1400 lgl_xml_template_read_templates_from_file (full_filename);
1401 g_free (full_filename);
1403 templates = g_list_concat (templates, new_templates);
1404 new_templates = NULL;
1415 static lglTemplate *
1416 template_full_page (const gchar *paper_id)
1418 lglPaper *paper = NULL;
1419 lglTemplate *template = NULL;
1420 lglTemplateFrame *frame = NULL;
1424 g_return_val_if_fail (paper_id, NULL);
1426 paper = lgl_db_lookup_paper_from_id (paper_id);
1427 if ( paper == NULL )
1432 part = g_strdup_printf ("%s-Full-Page", paper->id);
1433 desc = g_strdup_printf (_("Generic %s full page template"), paper->name);
1435 template = lgl_template_new ("Generic", part, desc,
1436 paper_id, paper->width, paper->height);
1439 frame = lgl_template_frame_rect_new ("0",
1445 lgl_template_add_frame (template, frame);
1447 lgl_template_frame_add_layout (frame, lgl_template_layout_new (1, 1, 0., 0., 0., 0.));
1449 lgl_template_frame_add_markup (frame, lgl_template_markup_margin_new (9.0));
1453 lgl_paper_free (paper);
1461 * lgl_db_print_known_templates:
1463 * Print all known templates (for debugging purposes).
1467 lgl_db_print_known_templates (void)
1470 lglTemplate *template;
1472 g_print ("%s():\n", __FUNCTION__);
1473 for (p=templates; p!=NULL; p=p->next)
1475 template = (lglTemplate *)p->data;
1477 g_print("TEMPLATE brand=\"%s\", part=\"%s\", description=\"%s\"\n",
1478 template->brand, template->part, template->description);
1486 * lgl_db_print_aliases:
1487 * @template: template
1489 * Print all aliases of a template (for debugging purposes).
1493 lgl_db_print_aliases (const lglTemplate *template)
1496 lglTemplateAlias *alias;
1498 g_print ("%s():\n", __FUNCTION__);
1499 for (p=template->aliases; p!=NULL; p=p->next)
1501 alias = (lglTemplateAlias *)p->data;
1503 g_print("Alias: brand=\"%s\", part=\"%s\"\n", alias->brand, alias->part);