3 * Copyright (C) 2003-2009 Jim Evins <evins@snaught.com>.
5 * This file is part of libglabels.
7 * libglabels is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * libglabels is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with libglabels. If not, see <http://www.gnu.org/licenses/>.
25 #include <glib/gi18n.h>
29 #include <sys/types.h>
31 #include "libglabels-private.h"
33 #include "xml-paper.h"
34 #include "xml-category.h"
35 #include "xml-template.h"
38 /*===========================================*/
40 /*===========================================*/
43 /*===========================================*/
45 /*===========================================*/
47 static GList *papers = NULL;
48 static GList *categories = NULL;
49 static GList *templates = NULL;
51 static GHashTable *template_cache = NULL;
54 /*===========================================*/
55 /* Local function prototypes */
56 /*===========================================*/
58 static void init_template_cache (void);
59 static void add_to_template_cache (lglTemplate *template);
61 static GList *read_papers (void);
62 static GList *read_paper_files_from_dir (GList *papers,
63 const gchar *dirname);
65 static GList *read_categories (void);
66 static GList *read_category_files_from_dir (GList *categories,
67 const gchar *dirname);
69 static GList *read_templates (void);
70 static GList *read_template_files_from_dir (GList *templates,
71 const gchar *dirname);
73 static lglTemplate *template_full_page (const gchar *page_size);
76 /*===========================================*/
77 /* Module initialization */
78 /*===========================================*/
83 * Initialize all libglabels subsystems. It is not necessary for an application to call
84 * lgl_db_init(), because libglabels will initialize on demand. An application programmer may
85 * choose to call lgl_db_init() at startup to minimize the impact of the first libglabels call
86 * on GUI response time.
88 * This function initializes its paper definitions, category definitions, and its template
89 * database.. It will search both system and user template directories to locate
95 lglPaper *paper_other;
96 lglCategory *category_user_defined;
106 papers = read_papers ();
108 /* Create and append an "Other" entry. */
109 paper_other = lgl_paper_new ("Other", _("Other"), 0.0, 0.0, NULL);
110 papers = g_list_append (papers, paper_other);
119 categories = read_categories ();
121 /* Create and append a "User defined" entry. */
122 category_user_defined = lgl_category_new ("user-defined", _("User defined"));
123 categories = g_list_append (categories, category_user_defined);
132 templates = read_templates ();
134 /* Create and append generic full page templates. */
135 page_sizes = lgl_db_get_paper_id_list ();
136 for ( p=page_sizes; p != NULL; p=p->next )
138 if ( !lgl_db_is_paper_id_other (p->data) )
140 templates = g_list_append (templates,
141 template_full_page (p->data));
144 lgl_db_free_paper_id_list (page_sizes);
146 init_template_cache ();
151 /*===========================================*/
152 /* Paper db functions. */
153 /*===========================================*/
156 * lgl_db_get_paper_id_list:
158 * Get a list of all paper ids known to libglabels.
160 * Returns: a list of paper ids.
164 lgl_db_get_paper_id_list (void)
175 for ( p=papers; p != NULL; p=p->next )
177 paper = (lglPaper *)p->data;
178 ids = g_list_append (ids, g_strdup (paper->id));
186 * lgl_db_free_paper_id_list:
187 * @ids: List of id strings to be freed.
189 * Free up all storage associated with an id list obtained with
190 * lgl_db_get_paper_id_list().
194 lgl_db_free_paper_id_list (GList *ids)
198 for (p = ids; p != NULL; p = p->next)
209 * lgl_db_get_paper_name_list:
211 * Get a list of all localized paper names known to libglabels.
213 * Returns: a list of localized paper names.
217 lgl_db_get_paper_name_list (void)
228 for ( p=papers; p != NULL; p=p->next )
230 paper = (lglPaper *)p->data;
231 names = g_list_append (names, g_strdup (paper->name));
239 * lgl_db_free_paper_name_list:
240 * @names: List of localized paper name strings to be freed.
242 * Free up all storage associated with a name list obtained with
243 * lgl_db_get_paper_name_list().
247 lgl_db_free_paper_name_list (GList *names)
251 for (p = names; p != NULL; p = p->next)
262 * lgl_db_lookup_paper_from_name:
263 * @name: localized paper name string
265 * Lookup paper definition from localized paper name string.
267 * Returns: pointer to a newly allocated #lglPaper structure.
271 lgl_db_lookup_paper_from_name (const gchar *name)
283 /* If no name, return first paper as a default */
284 return lgl_paper_dup ((lglPaper *) papers->data);
287 for (p = papers; p != NULL; p = p->next)
289 paper = (lglPaper *) p->data;
290 if (UTF8_EQUAL (paper->name, name))
292 return lgl_paper_dup (paper);
301 * lgl_db_lookup_paper_from_id:
302 * @id: paper id string
304 * Lookup paper definition from id string.
306 * Returns: pointer to a newly allocated #lglPaper structure.
310 lgl_db_lookup_paper_from_id (const gchar *id)
322 /* If no id, return first paper as a default */
323 return lgl_paper_dup ((lglPaper *) papers->data);
326 for (p = papers; p != NULL; p = p->next)
328 paper = (lglPaper *) p->data;
329 if (ASCII_EQUAL (paper->id, id))
331 return lgl_paper_dup (paper);
340 * lgl_db_lookup_paper_id_from_name:
341 * @name: localized paper name stringp
343 * Lookup paper name string from localized paper name string.
345 * Returns: pointer to a newly allocated id string.
349 lgl_db_lookup_paper_id_from_name (const gchar *name)
351 lglPaper *paper = NULL;
356 paper = lgl_db_lookup_paper_from_name (name);
359 id = g_strdup (paper->id);
360 lgl_paper_free (paper);
370 * lgl_db_lookup_paper_name_from_id:
371 * @id: paper id string
373 * Lookup localized paper name string from paper id string.
375 * Returns: pointer to a newly allocated localized paper name string.
379 lgl_db_lookup_paper_name_from_id (const gchar *id)
381 lglPaper *paper = NULL;
386 paper = lgl_db_lookup_paper_from_id (id);
389 name = g_strdup (paper->name);
390 lgl_paper_free (paper);
400 * lgl_db_is_paper_id_known:
401 * @id: paper id to test
403 * Determine if given paper id is known to libglabels.
405 * Returns: TRUE if id is known, otherwise FALSE.
409 lgl_db_is_paper_id_known (const gchar *id)
424 for (p = papers; p != NULL; p = p->next)
426 paper = (lglPaper *) p->data;
427 if (ASCII_EQUAL (paper->id, id))
438 * lgl_db_is_paper_id_other:
439 * @id: paper id to test
441 * Determine if given paper id is the special id "Other."
443 * Returns: TRUE if id is "Other", otherwise FALSE.
447 lgl_db_is_paper_id_other (const gchar *id)
454 return (ASCII_EQUAL (id, "Other"));
462 GList *papers = NULL;
464 data_dir = LGL_SYSTEM_DATA_DIR;
465 papers = read_paper_files_from_dir (papers, data_dir);
468 data_dir = LGL_USER_DATA_DIR;
469 papers = read_paper_files_from_dir (papers, data_dir);
472 if (papers == NULL) {
473 g_critical (_("Unable to locate paper size definitions. Libglabels may not be installed correctly!"));
481 read_paper_files_from_dir (GList *papers,
482 const gchar *dirname)
485 const gchar *filename, *extension;
486 gchar *full_filename = NULL;
487 GError *gerror = NULL;
488 GList *new_papers = NULL;
490 if (dirname == NULL) {
494 if (!g_file_test (dirname, G_FILE_TEST_EXISTS)) {
498 dp = g_dir_open (dirname, 0, &gerror);
499 if (gerror != NULL) {
500 g_message ("cannot open data directory: %s", gerror->message );
504 while ((filename = g_dir_read_name (dp)) != NULL) {
506 extension = strrchr (filename, '.');
508 if (extension != NULL) {
510 if ( ASCII_EQUAL (extension, ".paper") ||
511 ASCII_EQUAL (filename, "paper-sizes.xml") )
515 g_build_filename (dirname, filename, NULL);
517 lgl_xml_paper_read_papers_from_file (full_filename);
518 g_free (full_filename);
520 papers = g_list_concat (papers, new_papers);
536 * lgl_db_print_known_papers:
538 * For debugging purposes: print a list of all paper definitions known to
543 lgl_db_print_known_papers (void)
552 g_print ("%s():\n", __FUNCTION__);
553 for (p = papers; p != NULL; p = p->next) {
554 paper = (lglPaper *) p->data;
556 g_print ("PAPER id=\"%s\", name=\"%s\", width=%gpts, height=%gpts\n",
557 paper->id, paper->name, paper->width, paper->height);
565 /*===========================================*/
566 /* Category db functions. */
567 /*===========================================*/
570 * lgl_db_get_category_id_list:
572 * Get a list of all category ids known to libglabels.
574 * Returns: a list of category ids.
578 lgl_db_get_category_id_list (void)
582 lglCategory *category;
589 for ( p=categories; p != NULL; p=p->next )
591 category = (lglCategory *)p->data;
592 ids = g_list_append (ids, g_strdup (category->id));
600 * lgl_db_free_category_id_list:
601 * @ids: List of id strings to be freed.
603 * Free up all storage associated with an id list obtained with
604 * lgl_db_get_category_id_list().
608 lgl_db_free_category_id_list (GList *ids)
612 for (p = ids; p != NULL; p = p->next)
623 * lgl_db_get_category_name_list:
625 * Get a list of all localized category names known to libglabels.
627 * Returns: a list of localized category names.
631 lgl_db_get_category_name_list (void)
635 lglCategory *category;
642 for ( p=categories; p != NULL; p=p->next )
644 category = (lglCategory *)p->data;
645 names = g_list_append (names, g_strdup (category->name));
653 * lgl_db_free_category_name_list:
654 * @names: List of localized category name strings to be freed.
656 * Free up all storage associated with a name list obtained with
657 * lgl_db_get_category_name_list().
661 lgl_db_free_category_name_list (GList *names)
665 for (p = names; p != NULL; p = p->next)
676 * lgl_db_lookup_category_from_name:
677 * @name: localized category name string
679 * Lookup category definition from localized category name string.
681 * Returns: pointer to a newly allocated #lglCategory structure.
685 lgl_db_lookup_category_from_name (const gchar *name)
688 lglCategory *category;
697 /* If no name, return first category as a default */
698 return lgl_category_dup ((lglCategory *) categories->data);
701 for (p = categories; p != NULL; p = p->next)
703 category = (lglCategory *) p->data;
704 if (UTF8_EQUAL (category->name, name))
706 return lgl_category_dup (category);
715 * lgl_db_lookup_category_from_id:
716 * @id: category id string
718 * Lookup category definition from id string.
720 * Returns: pointer to a newly allocated #lglCategory structure.
724 lgl_db_lookup_category_from_id (const gchar *id)
727 lglCategory *category;
736 /* If no id, return first category as a default */
737 return lgl_category_dup ((lglCategory *) categories->data);
740 for (p = categories; p != NULL; p = p->next)
742 category = (lglCategory *) p->data;
743 if (ASCII_EQUAL (category->id, id))
745 return lgl_category_dup (category);
754 * lgl_db_lookup_category_id_from_name:
755 * @name: localized category name stringp
757 * Lookup category name string from localized category name string.
759 * Returns: pointer to a newly allocated id string.
763 lgl_db_lookup_category_id_from_name (const gchar *name)
765 lglCategory *category = NULL;
770 category = lgl_db_lookup_category_from_name (name);
771 if ( category != NULL )
773 id = g_strdup (category->id);
774 lgl_category_free (category);
784 * lgl_db_lookup_category_name_from_id:
785 * @id: category id string
787 * Lookup localized category name string from category id string.
789 * Returns: pointer to a newly allocated localized category name string.
793 lgl_db_lookup_category_name_from_id (const gchar *id)
795 lglCategory *category = NULL;
800 category = lgl_db_lookup_category_from_id (id);
801 if ( category != NULL )
803 name = g_strdup (category->name);
804 lgl_category_free (category);
814 * lgl_db_is_category_id_known:
815 * @id: category id to test
817 * Determine if given category id is known to libglabels.
819 * Returns: TRUE if id is known, otherwise FALSE.
823 lgl_db_is_category_id_known (const gchar *id)
826 lglCategory *category;
838 for (p = categories; p != NULL; p = p->next)
840 category = (lglCategory *) p->data;
841 if (ASCII_EQUAL (category->id, id))
852 read_categories (void)
855 GList *categories = NULL;
857 data_dir = LGL_SYSTEM_DATA_DIR;
858 categories = read_category_files_from_dir (categories, data_dir);
861 data_dir = LGL_USER_DATA_DIR;
862 categories = read_category_files_from_dir (categories, data_dir);
865 if (categories == NULL) {
866 g_critical (_("Unable to locate category definitions. Libglabels may not be installed correctly!"));
874 read_category_files_from_dir (GList *categories,
875 const gchar *dirname)
878 const gchar *filename, *extension;
879 gchar *full_filename = NULL;
880 GError *gerror = NULL;
881 GList *new_categories = NULL;
883 if (dirname == NULL) {
887 if (!g_file_test (dirname, G_FILE_TEST_EXISTS)) {
891 dp = g_dir_open (dirname, 0, &gerror);
892 if (gerror != NULL) {
893 g_message ("cannot open data directory: %s", gerror->message );
897 while ((filename = g_dir_read_name (dp)) != NULL) {
899 extension = strrchr (filename, '.');
901 if (extension != NULL) {
903 if ( ASCII_EQUAL (extension, ".category") ||
904 ASCII_EQUAL (filename, "categories.xml") )
908 g_build_filename (dirname, filename, NULL);
910 lgl_xml_category_read_categories_from_file (full_filename);
911 g_free (full_filename);
913 categories = g_list_concat (categories, new_categories);
914 new_categories = NULL;
929 * lgl_db_print_known_categories:
931 * For debugging purposes: print a list of all category definitions known to
936 lgl_db_print_known_categories (void)
939 lglCategory *category;
945 g_print ("%s():\n", __FUNCTION__);
946 for (p = categories; p != NULL; p = p->next) {
947 category = (lglCategory *) p->data;
949 g_print ("CATEGORY id=\"%s\", name=\"%s\"\n", category->id, category->name);
957 /*===========================================*/
958 /* Brand db functions. */
959 /*===========================================*/
962 * lgl_db_get_brand_list:
963 * @paper_id: If non NULL, limit results to given page size.
964 * @category_id: If non NULL, limit results to given template category.
966 * Get a list of all valid brands of templates in the template database.
967 * Results can be filtered by page size and/or template category. A list of valid page
968 * sizes can be obtained using lgl_db_get_paper_id_list(). A list of valid template
969 * categories can be obtained using lgl_db_get_category_id_list().
971 * Returns: a list of brands
974 lgl_db_get_brand_list (const gchar *paper_id,
975 const gchar *category_id)
977 GList *p_tmplt, *p_alias;
978 lglTemplate *template;
979 lglTemplateAlias *alias;
980 GList *brands = NULL;
987 for (p_tmplt = templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
989 template = (lglTemplate *) p_tmplt->data;
990 if (lgl_template_does_page_size_match (template, paper_id) &&
991 lgl_template_does_category_match (template, category_id))
993 for (p_alias = template->aliases; p_alias != NULL;
994 p_alias = p_alias->next)
996 alias = (lglTemplateAlias *)p_alias->data;
998 if ( !g_list_find_custom (brands, alias->brand,
999 (GCompareFunc)lgl_str_utf8_casecmp) )
1001 brands = g_list_insert_sorted (brands,
1002 g_strdup (alias->brand),
1003 (GCompareFunc)lgl_str_utf8_casecmp);
1014 * lgl_db_free_brand_list:
1015 * @brands: List of template brand strings to be freed.
1017 * Free up all storage associated with a list of template names obtained with
1018 * lgl_db_get_brand_list().
1022 lgl_db_free_brand_list (GList *brands)
1026 for (p_brand = brands; p_brand != NULL; p_brand = p_brand->next)
1028 g_free (p_brand->data);
1029 p_brand->data = NULL;
1032 g_list_free (brands);
1036 /*===========================================*/
1037 /* Template db functions. */
1038 /*===========================================*/
1041 * lgl_db_register_template:
1042 * @template: Pointer to a template structure to add to database.
1044 * Register a template. This function adds a template to the template database.
1045 * The template will be stored in an individual XML file in the user template directory.
1047 * Returns: Status of registration attempt (#lglDbRegStatus)
1050 lgl_db_register_template (const lglTemplate *template)
1052 lglTemplate *template_copy;
1053 gchar *dir, *filename, *abs_filename;
1061 if (lgl_db_does_template_exist (template->brand, template->part))
1063 return LGL_DB_REG_BRAND_PART_EXISTS;
1066 if (lgl_db_is_paper_id_known (template->paper_id))
1068 dir = LGL_USER_DATA_DIR;
1069 mkdir (dir, 0775); /* Try to make sure directory exists. */
1070 filename = g_strdup_printf ("%s_%s.template", template->brand, template->part);
1071 abs_filename = g_build_filename (dir, filename, NULL);
1072 bytes_written = lgl_xml_template_write_template_to_file (template, abs_filename);
1075 g_free (abs_filename);
1077 if (bytes_written > 0)
1079 template_copy = lgl_template_dup (template);
1080 lgl_template_add_category (template_copy, "user-defined");
1081 templates = g_list_append (templates, template_copy);
1082 add_to_template_cache (template_copy);
1083 return LGL_DB_REG_OK;
1087 return LGL_DB_REG_FILE_WRITE_ERROR;
1092 g_message ("Cannot register new template with unknown page size.");
1093 return LGL_DB_REG_BAD_PAPER_ID;
1100 * lgl_db_does_template_exist:
1101 * @brand: Brand name.
1102 * @part: Part name/number.
1104 * This function tests whether a template with the given brand and part name/number exists.
1106 * Returns: TRUE if such a template exists in the database.
1109 lgl_db_does_template_exist (const gchar *brand,
1112 GList *p_tmplt, *p_alias;
1113 lglTemplate *template;
1114 lglTemplateAlias *alias;
1121 if ((brand == NULL) || (part == NULL))
1126 for (p_tmplt = templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1128 template = (lglTemplate *) p_tmplt->data;
1129 for (p_alias = template->aliases; p_alias != NULL; p_alias = p_alias->next)
1131 alias = (lglTemplateAlias *)p_alias->data;
1133 if ( UTF8_EQUAL (brand, alias->brand) &&
1134 UTF8_EQUAL (part, alias->part) )
1146 * lgl_db_does_template_name_exist:
1147 * @name: name string
1149 * This function test whether a template with the given name exists.
1151 * Returns: TRUE if such a template exists in the database.
1155 lgl_db_does_template_name_exist (const gchar *name)
1157 GList *p_tmplt, *p_alias;
1158 lglTemplate *template;
1159 lglTemplateAlias *alias;
1160 gchar *candidate_name;
1172 for (p_tmplt = templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1174 template = (lglTemplate *) p_tmplt->data;
1175 for (p_alias = template->aliases; p_alias != NULL; p_alias = p_alias->next)
1177 alias = (lglTemplateAlias *)p_alias->data;
1178 candidate_name = g_strdup_printf ("%s %s", alias->brand, alias->part);
1180 if ( UTF8_EQUAL (candidate_name, name) ) {
1181 g_free (candidate_name);
1184 g_free (candidate_name);
1193 * lgl_db_get_template_name_list_unique:
1194 * @brand: If non NULL, limit results to given brand
1195 * @paper_id: If non NULL, limit results to given page size.
1196 * @category_id: If non NULL, limit results to given template category.
1198 * Get a list of valid names of unique templates in the template database. Results
1199 * can be filtered by page size and/or template category. A list of valid page sizes
1200 * can be obtained using lgl_db_get_paper_id_list(). A list of valid template categories
1201 * can be obtained using lgl_db_get_category_id_list().
1203 * This function differs from lgl_db_get_template_name_list_all(), because it does not
1204 * return multiple names for the same template.
1206 * Returns: a list of template names.
1209 lgl_db_get_template_name_list_unique (const gchar *brand,
1210 const gchar *paper_id,
1211 const gchar *category_id)
1214 lglTemplate *template;
1216 GList *names = NULL;
1223 for (p_tmplt = templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1225 template = (lglTemplate *) p_tmplt->data;
1227 if (lgl_template_does_brand_match (template, brand) &&
1228 lgl_template_does_page_size_match (template, paper_id) &&
1229 lgl_template_does_category_match (template, category_id))
1231 name = g_strdup_printf ("%s %s", template->brand, template->part);
1232 names = g_list_insert_sorted (names, name,
1233 (GCompareFunc)g_utf8_collate);
1242 * lgl_db_get_template_name_list_all:
1243 * @brand: If non NULL, limit results to given brand
1244 * @paper_id: If non NULL, limit results to given page size.
1245 * @category_id: If non NULL, limit results to given template category.
1247 * Get a list of all valid names and aliases of templates in the template database.
1248 * Results can be filtered by page size and/or template category. A list of valid page
1249 * sizes can be obtained using lgl_db_get_paper_id_list(). A list of valid template
1250 * categories can be obtained using lgl_db_get_category_id_list().
1252 * This function differs from lgl_db_get_template_name_list_unique(), because it will
1253 * return multiple names for the same template.
1255 * Returns: a list of template names and aliases.
1258 lgl_db_get_template_name_list_all (const gchar *brand,
1259 const gchar *paper_id,
1260 const gchar *category_id)
1262 GList *p_tmplt, *p_alias;
1263 lglTemplate *template;
1264 lglTemplateAlias *alias;
1266 GList *names = NULL;
1273 for (p_tmplt = templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1275 template = (lglTemplate *) p_tmplt->data;
1276 if (lgl_template_does_page_size_match (template, paper_id) &&
1277 lgl_template_does_category_match (template, category_id))
1279 for (p_alias = template->aliases; p_alias != NULL;
1280 p_alias = p_alias->next)
1282 alias = (lglTemplateAlias *)p_alias->data;
1284 if ( !brand || UTF8_EQUAL( alias->brand, brand) )
1286 name = g_strdup_printf ("%s %s", alias->brand, alias->part);
1287 names = g_list_insert_sorted (names, name,
1288 (GCompareFunc)g_utf8_collate);
1299 * lgl_db_free_template_name_list:
1300 * @names: List of template name strings to be freed.
1302 * Free up all storage associated with a list of template names obtained with
1303 * lgl_db_get_template_name_list_all() or lgl_db_get_template_name_list_unique().
1307 lgl_db_free_template_name_list (GList *names)
1311 for (p_name = names; p_name != NULL; p_name = p_name->next)
1313 g_free (p_name->data);
1314 p_name->data = NULL;
1317 g_list_free (names);
1322 * lgl_db_lookup_template_from_name:
1323 * @name: name string
1325 * Lookup template in template database from name string.
1327 * Returns: pointer to a newly allocated #lglTemplate structure.
1331 lgl_db_lookup_template_from_name (const gchar *name)
1334 lglTemplate *template;
1335 lglTemplateAlias *alias;
1336 gchar *candidate_name;
1337 lglTemplate *new_template;
1346 /* If no name, return first template as a default */
1347 return lgl_template_dup ((lglTemplate *) templates->data);
1350 template = g_hash_table_lookup (template_cache, name);
1354 for (p_alias = template->aliases; p_alias != NULL; p_alias = p_alias->next)
1356 alias = (lglTemplateAlias *)p_alias->data;
1357 candidate_name = g_strdup_printf ("%s %s", alias->brand, alias->part);
1359 if ( UTF8_EQUAL (candidate_name, name) )
1361 g_free (candidate_name);
1362 new_template = lgl_template_dup (template);
1363 g_free (new_template->brand);
1364 new_template->brand = g_strdup (alias->brand);
1365 g_free (new_template->part);
1366 new_template->part = g_strdup (alias->part);
1367 return new_template;
1370 g_free (candidate_name);
1374 /* No matching template has been found so return the first template */
1375 return lgl_template_dup ((lglTemplate *) templates->data);
1380 init_template_cache (void)
1382 GList *p_tmplt, *p_alias;
1383 lglTemplate *template;
1384 lglTemplateAlias *alias;
1387 template_cache = g_hash_table_new (g_str_hash, g_str_equal);
1389 for ( p_tmplt=templates; p_tmplt != NULL; p_tmplt=p_tmplt->next )
1391 template = (lglTemplate *) p_tmplt->data;
1393 for ( p_alias=template->aliases; p_alias != NULL; p_alias=p_alias->next )
1395 alias = (lglTemplateAlias *)p_alias->data;
1396 name = g_strdup_printf ("%s %s", alias->brand, alias->part);
1398 g_hash_table_insert (template_cache, name, template);
1405 add_to_template_cache (lglTemplate *template)
1408 lglTemplateAlias *alias;
1411 for ( p_alias=template->aliases; p_alias != NULL; p_alias=p_alias->next )
1413 alias = (lglTemplateAlias *)p_alias->data;
1414 name = g_strdup_printf ("%s %s", alias->brand, alias->part);
1416 g_hash_table_insert (template_cache, name, template);
1422 read_templates (void)
1425 GList *templates = NULL;
1427 lglTemplate *template;
1430 * User defined templates. Add to user-defined category.
1432 data_dir = LGL_USER_DATA_DIR;
1433 templates = read_template_files_from_dir (templates, data_dir);
1435 for ( p=templates; p != NULL; p=p->next )
1437 template = (lglTemplate *)p->data;
1438 lgl_template_add_category (template, "user-defined");
1444 data_dir = LGL_SYSTEM_DATA_DIR;
1445 templates = read_template_files_from_dir (templates, data_dir);
1448 if (templates == NULL)
1450 g_critical (_("Unable to locate any template files. Libglabels may not be installed correctly!"));
1458 read_template_files_from_dir (GList *templates,
1459 const gchar *dirname)
1462 const gchar *filename, *extension, *extension2;
1463 gchar *full_filename = NULL;
1464 GError *gerror = NULL;
1465 GList *new_templates = NULL;
1467 if (dirname == NULL)
1470 if (!g_file_test (dirname, G_FILE_TEST_EXISTS))
1475 dp = g_dir_open (dirname, 0, &gerror);
1478 g_message ("cannot open data directory: %s", gerror->message );
1482 while ((filename = g_dir_read_name (dp)) != NULL)
1485 extension = strrchr (filename, '.');
1486 extension2 = strrchr (filename, '-');
1488 if ( (extension && ASCII_EQUAL (extension, ".template")) ||
1489 (extension2 && ASCII_EQUAL (extension2, "-templates.xml")) )
1492 full_filename = g_build_filename (dirname, filename, NULL);
1494 lgl_xml_template_read_templates_from_file (full_filename);
1495 g_free (full_filename);
1497 templates = g_list_concat (templates, new_templates);
1498 new_templates = NULL;
1509 static lglTemplate *
1510 template_full_page (const gchar *paper_id)
1512 lglPaper *paper = NULL;
1513 lglTemplate *template = NULL;
1514 lglTemplateFrame *frame = NULL;
1518 g_return_val_if_fail (paper_id, NULL);
1520 paper = lgl_db_lookup_paper_from_id (paper_id);
1521 if ( paper == NULL )
1526 part = g_strdup_printf ("%s-Full-Page", paper->id);
1527 desc = g_strdup_printf (_("Generic %s full page template"), paper->name);
1529 template = lgl_template_new ("Generic", part, desc,
1530 paper_id, paper->width, paper->height);
1533 frame = lgl_template_frame_rect_new ("0",
1539 lgl_template_add_frame (template, frame);
1541 lgl_template_frame_add_layout (frame, lgl_template_layout_new (1, 1, 0., 0., 0., 0.));
1543 lgl_template_frame_add_markup (frame, lgl_template_markup_margin_new (9.0));
1547 lgl_paper_free (paper);
1555 * lgl_db_print_known_templates:
1557 * Print all known templates (for debugging purposes).
1561 lgl_db_print_known_templates (void)
1564 lglTemplate *template;
1566 g_print ("%s():\n", __FUNCTION__);
1567 for (p=templates; p!=NULL; p=p->next)
1569 template = (lglTemplate *)p->data;
1571 g_print("TEMPLATE brand=\"%s\", part=\"%s\", description=\"%s\"\n",
1572 template->brand, template->part, template->description);
1581 * lgl_db_print_aliases:
1582 * @template: template
1584 * Print all aliases of a template (for debugging purposes).
1588 lgl_db_print_aliases (const lglTemplate *template)
1591 lglTemplateAlias *alias;
1593 g_print ("%s():\n", __FUNCTION__);
1594 for (p=template->aliases; p!=NULL; p=p->next)
1596 alias = (lglTemplateAlias *)p->data;
1598 g_print("Alias: brand=\"%s\", part=\"%s\"\n", alias->brand, alias->part);
1608 * Local Variables: -- emacs
1610 * c-basic-offset: 8 -- emacs
1611 * tab-width: 8 -- emacs
1612 * indent-tabs-mode: nil -- emacs