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>
27 #include <glib-object.h>
30 #include <sys/types.h>
32 #include "libglabels-private.h"
34 #include "xml-paper.h"
35 #include "xml-category.h"
36 #include "xml-vendor.h"
37 #include "xml-template.h"
40 /*===========================================*/
42 /*===========================================*/
44 #define TYPE_LGL_DB_MODEL (lgl_db_model_get_type ())
45 #define LGL_DB_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_LGL_DB_MODEL, lglDbModel))
46 #define LGL_DB_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_LGL_DB_MODEL, lglDbModelClass))
47 #define IS_LGL_DB_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_LGL_DB_MODEL))
48 #define IS_LGL_DB_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_LGL_DB_MODEL))
49 #define LGL_DB_MODEL_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS ((object), TYPE_LGL_DB_MODEL, lglDbModelClass))
52 typedef struct _lglDbModel lglDbModel;
53 typedef struct _lglDbModelClass lglDbModelClass;
64 GHashTable *template_cache;
68 struct _lglDbModelClass {
69 GObjectClass parent_class;
74 void (*changed) (lglDbModel *this,
86 /*===========================================*/
88 /*===========================================*/
90 static guint signals[LAST_SIGNAL] = {0};
92 static lglDbModel *model = NULL;
95 /*===========================================*/
96 /* Local function prototypes */
97 /*===========================================*/
99 static void lgl_db_model_finalize (GObject *object);
101 static void add_to_template_cache (lglTemplate *template);
103 static GList *read_papers (void);
104 static GList *read_paper_files_from_dir (GList *papers,
105 const gchar *dirname);
107 static GList *read_categories (void);
108 static GList *read_category_files_from_dir (GList *categories,
109 const gchar *dirname);
111 static GList *read_vendors (void);
112 static GList *read_vendor_files_from_dir (GList *vendors,
113 const gchar *dirname);
115 static void read_templates (void);
116 static void read_template_files_from_dir (const gchar *dirname);
118 static lglTemplate *template_full_page (const gchar *page_size);
121 /*****************************************************************************/
122 /* Object infrastructure. */
123 /*****************************************************************************/
124 G_DEFINE_TYPE (lglDbModel, lgl_db_model, G_TYPE_OBJECT);
127 /*****************************************************************************/
128 /* Class Init Function. */
129 /*****************************************************************************/
131 lgl_db_model_class_init (lglDbModelClass *class)
133 GObjectClass *gobject_class = (GObjectClass *) class;
135 lgl_db_model_parent_class = g_type_class_peek_parent (class);
137 gobject_class->finalize = lgl_db_model_finalize;
140 g_signal_new ("changed",
141 G_OBJECT_CLASS_TYPE (gobject_class),
143 G_STRUCT_OFFSET (lglDbModelClass, changed),
145 g_cclosure_marshal_VOID__VOID,
151 /*****************************************************************************/
152 /* Object Instance Init Function. */
153 /*****************************************************************************/
155 lgl_db_model_init (lglDbModel *this)
157 this->template_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)lgl_template_free);
161 /*****************************************************************************/
162 /* Finalize Method. */
163 /*****************************************************************************/
165 lgl_db_model_finalize (GObject *object)
170 g_return_if_fail (object && IS_LGL_DB_MODEL (object));
171 this = LGL_DB_MODEL (object);
173 g_hash_table_unref (this->template_cache);
175 for (p = this->papers; p != NULL; p = p->next)
180 g_list_free (this->papers);
182 for (p = this->categories; p != NULL; p = p->next)
187 g_list_free (this->categories);
189 for (p = this->vendors; p != NULL; p = p->next)
194 g_list_free (this->vendors);
196 for (p = this->templates; p != NULL; p = p->next)
198 lgl_template_free ((lglTemplate *)p->data);
201 g_list_free (this->templates);
203 G_OBJECT_CLASS (lgl_db_model_parent_class)->finalize (object);
207 /*****************************************************************************/
208 /** New Object Generator. */
209 /*****************************************************************************/
211 lgl_db_model_new (void)
215 this = g_object_new (TYPE_LGL_DB_MODEL, NULL);
221 /*===========================================*/
222 /* Module initialization */
223 /*===========================================*/
228 * Initialize all libglabels subsystems. It is not necessary for an application to call
229 * lgl_db_init(), because libglabels will initialize on demand. An application programmer may
230 * choose to call lgl_db_init() at startup to minimize the impact of the first libglabels call
231 * on GUI response time.
233 * This function initializes its paper definitions, category definitions, vendor definitions,
234 * and its template database. It will search both system and user template directories to locate
240 lglPaper *paper_other;
241 lglCategory *category_user_defined;
242 lglTemplate *template;
246 model = lgl_db_model_new ();
251 model->papers = read_papers ();
253 /* Create and append an "Other" entry. */
254 paper_other = lgl_paper_new ("Other", _("Other"), 0.0, 0.0, NULL);
255 model->papers = g_list_append (model->papers, paper_other);
260 model->categories = read_categories ();
262 /* Create and append a "User defined" entry. */
263 category_user_defined = lgl_category_new ("user-defined", _("User defined"));
264 model->categories = g_list_append (model->categories, category_user_defined);
269 model->vendors = read_vendors ();
276 /* Create and append generic full page templates. */
277 page_sizes = lgl_db_get_paper_id_list ();
278 for ( p=page_sizes; p != NULL; p=p->next )
280 if ( !lgl_db_is_paper_id_other (p->data) )
282 template = template_full_page (p->data);
283 _lgl_db_register_template_internal (template);
284 lgl_template_free (template);
287 lgl_db_free_paper_id_list (page_sizes);
293 lgl_db_notify_add (lglDbNotifyFunc func,
301 return g_signal_connect_swapped (G_OBJECT (model), "changed", G_CALLBACK (func), user_data);
306 lgl_db_notify_remove (gulong id)
308 g_signal_handler_disconnect (G_OBJECT (model), id);
312 /*===========================================*/
313 /* Paper db functions. */
314 /*===========================================*/
317 * lgl_db_get_paper_id_list:
319 * Get a list of all paper ids known to libglabels.
321 * Returns: a list of paper ids.
325 lgl_db_get_paper_id_list (void)
336 for ( p=model->papers; p != NULL; p=p->next )
338 paper = (lglPaper *)p->data;
339 ids = g_list_append (ids, g_strdup (paper->id));
347 * lgl_db_free_paper_id_list:
348 * @ids: List of id strings to be freed.
350 * Free up all storage associated with an id list obtained with
351 * lgl_db_get_paper_id_list().
355 lgl_db_free_paper_id_list (GList *ids)
359 for (p = ids; p != NULL; p = p->next)
370 * lgl_db_get_paper_name_list:
372 * Get a list of all localized paper names known to libglabels.
374 * Returns: a list of localized paper names.
378 lgl_db_get_paper_name_list (void)
389 for ( p=model->papers; p != NULL; p=p->next )
391 paper = (lglPaper *)p->data;
392 names = g_list_append (names, g_strdup (paper->name));
400 * lgl_db_free_paper_name_list:
401 * @names: List of localized paper name strings to be freed.
403 * Free up all storage associated with a name list obtained with
404 * lgl_db_get_paper_name_list().
408 lgl_db_free_paper_name_list (GList *names)
412 for (p = names; p != NULL; p = p->next)
423 * lgl_db_lookup_paper_from_name:
424 * @name: localized paper name string
426 * Lookup paper definition from localized paper name string.
428 * Returns: pointer to a newly allocated #lglPaper structure.
432 lgl_db_lookup_paper_from_name (const gchar *name)
444 /* If no name, return first paper as a default */
445 return lgl_paper_dup ((lglPaper *) model->papers->data);
448 for (p = model->papers; p != NULL; p = p->next)
450 paper = (lglPaper *) p->data;
451 if (UTF8_EQUAL (paper->name, name))
453 return lgl_paper_dup (paper);
462 * lgl_db_lookup_paper_from_id:
463 * @id: paper id string
465 * Lookup paper definition from id string.
467 * Returns: pointer to a newly allocated #lglPaper structure.
471 lgl_db_lookup_paper_from_id (const gchar *id)
483 /* If no id, return first paper as a default */
484 return lgl_paper_dup ((lglPaper *) model->papers->data);
487 for (p = model->papers; p != NULL; p = p->next)
489 paper = (lglPaper *) p->data;
490 if (ASCII_EQUAL (paper->id, id))
492 return lgl_paper_dup (paper);
501 * lgl_db_lookup_paper_id_from_name:
502 * @name: localized paper name stringp
504 * Lookup paper name string from localized paper name string.
506 * Returns: pointer to a newly allocated id string.
510 lgl_db_lookup_paper_id_from_name (const gchar *name)
512 lglPaper *paper = NULL;
517 paper = lgl_db_lookup_paper_from_name (name);
520 id = g_strdup (paper->id);
521 lgl_paper_free (paper);
531 * lgl_db_lookup_paper_name_from_id:
532 * @id: paper id string
534 * Lookup localized paper name string from paper id string.
536 * Returns: pointer to a newly allocated localized paper name string.
540 lgl_db_lookup_paper_name_from_id (const gchar *id)
542 lglPaper *paper = NULL;
547 paper = lgl_db_lookup_paper_from_id (id);
550 name = g_strdup (paper->name);
551 lgl_paper_free (paper);
561 * lgl_db_is_paper_id_known:
562 * @id: paper id to test
564 * Determine if given paper id is known to libglabels.
566 * Returns: TRUE if id is known, otherwise FALSE.
570 lgl_db_is_paper_id_known (const gchar *id)
585 for (p = model->papers; p != NULL; p = p->next)
587 paper = (lglPaper *) p->data;
588 if (ASCII_EQUAL (paper->id, id))
599 * lgl_db_is_paper_id_other:
600 * @id: paper id to test
602 * Determine if given paper id is the special id "Other."
604 * Returns: TRUE if id is "Other", otherwise FALSE.
608 lgl_db_is_paper_id_other (const gchar *id)
615 return (ASCII_EQUAL (id, "Other"));
623 GList *papers = NULL;
625 data_dir = LGL_SYSTEM_DATA_DIR;
626 papers = read_paper_files_from_dir (papers, data_dir);
629 data_dir = LGL_USER_DATA_DIR;
630 papers = read_paper_files_from_dir (papers, data_dir);
633 if (papers == NULL) {
634 g_critical (_("Unable to locate paper size definitions. Libglabels may not be installed correctly!"));
642 read_paper_files_from_dir (GList *papers,
643 const gchar *dirname)
646 const gchar *filename, *extension;
647 gchar *full_filename = NULL;
648 GError *gerror = NULL;
649 GList *new_papers = NULL;
651 if (dirname == NULL) {
655 if (!g_file_test (dirname, G_FILE_TEST_EXISTS)) {
659 dp = g_dir_open (dirname, 0, &gerror);
660 if (gerror != NULL) {
661 g_message ("cannot open data directory: %s", gerror->message );
665 while ((filename = g_dir_read_name (dp)) != NULL) {
667 extension = strrchr (filename, '.');
669 if (extension != NULL) {
671 if ( ASCII_EQUAL (filename, "paper-sizes.xml") )
675 g_build_filename (dirname, filename, NULL);
677 lgl_xml_paper_read_papers_from_file (full_filename);
678 g_free (full_filename);
680 papers = g_list_concat (papers, new_papers);
696 * lgl_db_print_known_papers:
698 * For debugging purposes: print a list of all paper definitions known to
703 lgl_db_print_known_papers (void)
712 g_print ("%s():\n", __FUNCTION__);
713 for (p = model->papers; p != NULL; p = p->next) {
714 paper = (lglPaper *) p->data;
716 g_print ("PAPER id=\"%s\", name=\"%s\", width=%gpts, height=%gpts\n",
717 paper->id, paper->name, paper->width, paper->height);
725 /*===========================================*/
726 /* Category db functions. */
727 /*===========================================*/
730 * lgl_db_get_category_id_list:
732 * Get a list of all category ids known to libglabels.
734 * Returns: a list of category ids.
738 lgl_db_get_category_id_list (void)
742 lglCategory *category;
749 for ( p=model->categories; p != NULL; p=p->next )
751 category = (lglCategory *)p->data;
752 ids = g_list_append (ids, g_strdup (category->id));
760 * lgl_db_free_category_id_list:
761 * @ids: List of id strings to be freed.
763 * Free up all storage associated with an id list obtained with
764 * lgl_db_get_category_id_list().
768 lgl_db_free_category_id_list (GList *ids)
772 for (p = ids; p != NULL; p = p->next)
783 * lgl_db_get_category_name_list:
785 * Get a list of all localized category names known to libglabels.
787 * Returns: a list of localized category names.
791 lgl_db_get_category_name_list (void)
795 lglCategory *category;
802 for ( p=model->categories; p != NULL; p=p->next )
804 category = (lglCategory *)p->data;
805 names = g_list_append (names, g_strdup (category->name));
813 * lgl_db_free_category_name_list:
814 * @names: List of localized category name strings to be freed.
816 * Free up all storage associated with a name list obtained with
817 * lgl_db_get_category_name_list().
821 lgl_db_free_category_name_list (GList *names)
825 for (p = names; p != NULL; p = p->next)
836 * lgl_db_lookup_category_from_name:
837 * @name: localized category name string
839 * Lookup category definition from localized category name string.
841 * Returns: pointer to a newly allocated #lglCategory structure.
845 lgl_db_lookup_category_from_name (const gchar *name)
848 lglCategory *category;
857 /* If no name, return first category as a default */
858 return lgl_category_dup ((lglCategory *) model->categories->data);
861 for (p = model->categories; p != NULL; p = p->next)
863 category = (lglCategory *) p->data;
864 if (UTF8_EQUAL (category->name, name))
866 return lgl_category_dup (category);
875 * lgl_db_lookup_category_from_id:
876 * @id: category id string
878 * Lookup category definition from id string.
880 * Returns: pointer to a newly allocated #lglCategory structure.
884 lgl_db_lookup_category_from_id (const gchar *id)
887 lglCategory *category;
896 /* If no id, return first category as a default */
897 return lgl_category_dup ((lglCategory *) model->categories->data);
900 for (p = model->categories; p != NULL; p = p->next)
902 category = (lglCategory *) p->data;
903 if (ASCII_EQUAL (category->id, id))
905 return lgl_category_dup (category);
914 * lgl_db_lookup_category_id_from_name:
915 * @name: localized category name stringp
917 * Lookup category name string from localized category name string.
919 * Returns: pointer to a newly allocated id string.
923 lgl_db_lookup_category_id_from_name (const gchar *name)
925 lglCategory *category = NULL;
930 category = lgl_db_lookup_category_from_name (name);
931 if ( category != NULL )
933 id = g_strdup (category->id);
934 lgl_category_free (category);
944 * lgl_db_lookup_category_name_from_id:
945 * @id: category id string
947 * Lookup localized category name string from category id string.
949 * Returns: pointer to a newly allocated localized category name string.
953 lgl_db_lookup_category_name_from_id (const gchar *id)
955 lglCategory *category = NULL;
960 category = lgl_db_lookup_category_from_id (id);
961 if ( category != NULL )
963 name = g_strdup (category->name);
964 lgl_category_free (category);
974 * lgl_db_is_category_id_known:
975 * @id: category id to test
977 * Determine if given category id is known to libglabels.
979 * Returns: TRUE if id is known, otherwise FALSE.
983 lgl_db_is_category_id_known (const gchar *id)
986 lglCategory *category;
998 for (p = model->categories; p != NULL; p = p->next)
1000 category = (lglCategory *) p->data;
1001 if (ASCII_EQUAL (category->id, id))
1012 read_categories (void)
1015 GList *categories = NULL;
1017 data_dir = LGL_SYSTEM_DATA_DIR;
1018 categories = read_category_files_from_dir (categories, data_dir);
1021 data_dir = LGL_USER_DATA_DIR;
1022 categories = read_category_files_from_dir (categories, data_dir);
1025 if (categories == NULL) {
1026 g_critical (_("Unable to locate category definitions. Libglabels may not be installed correctly!"));
1034 read_category_files_from_dir (GList *categories,
1035 const gchar *dirname)
1038 const gchar *filename, *extension;
1039 gchar *full_filename = NULL;
1040 GError *gerror = NULL;
1041 GList *new_categories = NULL;
1043 if (dirname == NULL) {
1047 if (!g_file_test (dirname, G_FILE_TEST_EXISTS)) {
1051 dp = g_dir_open (dirname, 0, &gerror);
1052 if (gerror != NULL) {
1053 g_message ("cannot open data directory: %s", gerror->message );
1057 while ((filename = g_dir_read_name (dp)) != NULL) {
1059 extension = strrchr (filename, '.');
1061 if (extension != NULL) {
1063 if ( ASCII_EQUAL (filename, "categories.xml") )
1067 g_build_filename (dirname, filename, NULL);
1069 lgl_xml_category_read_categories_from_file (full_filename);
1070 g_free (full_filename);
1072 categories = g_list_concat (categories, new_categories);
1073 new_categories = NULL;
1088 * lgl_db_print_known_categories:
1090 * For debugging purposes: print a list of all category definitions known to
1095 lgl_db_print_known_categories (void)
1098 lglCategory *category;
1104 g_print ("%s():\n", __FUNCTION__);
1105 for (p = model->categories; p != NULL; p = p->next) {
1106 category = (lglCategory *) p->data;
1108 g_print ("CATEGORY id=\"%s\", name=\"%s\"\n", category->id, category->name);
1116 /*===========================================*/
1117 /* Vendor db functions. */
1118 /*===========================================*/
1121 * lgl_db_get_vendor_name_list:
1123 * Get a list of all localized vendor names known to libglabels.
1125 * Returns: a list of localized vendor names.
1129 lgl_db_get_vendor_name_list (void)
1131 GList *names = NULL;
1140 for ( p=model->vendors; p != NULL; p=p->next )
1142 vendor = (lglVendor *)p->data;
1143 names = g_list_append (names, g_strdup (vendor->name));
1151 * lgl_db_free_vendor_name_list:
1152 * @names: List of localized vendor name strings to be freed.
1154 * Free up all storage associated with a name list obtained with
1155 * lgl_db_get_vendor_name_list().
1159 lgl_db_free_vendor_name_list (GList *names)
1163 for (p = names; p != NULL; p = p->next)
1169 g_list_free (names);
1174 * lgl_db_lookup_vendor_from_name:
1175 * @name: localized vendor name string
1177 * Lookup vendor definition from localized vendor name string.
1179 * Returns: pointer to a newly allocated #lglVendor structure.
1183 lgl_db_lookup_vendor_from_name (const gchar *name)
1195 /* If no name, return first vendor as a default */
1196 return lgl_vendor_dup ((lglVendor *) model->vendors->data);
1199 for (p = model->vendors; p != NULL; p = p->next)
1201 vendor = (lglVendor *) p->data;
1202 if (UTF8_EQUAL (vendor->name, name))
1204 return lgl_vendor_dup (vendor);
1213 * lgl_db_is_vendor_name_known:
1214 * @name: vendor name to test
1216 * Determine if given vendor id is known to libglabels.
1218 * Returns: TRUE if id is known, otherwise FALSE.
1222 lgl_db_is_vendor_name_known (const gchar *name)
1237 for (p = model->vendors; p != NULL; p = p->next)
1239 vendor = (lglVendor *) p->data;
1240 if (UTF8_EQUAL (vendor->name, name))
1254 GList *vendors = NULL;
1256 data_dir = LGL_SYSTEM_DATA_DIR;
1257 vendors = read_vendor_files_from_dir (vendors, data_dir);
1260 data_dir = LGL_USER_DATA_DIR;
1261 vendors = read_vendor_files_from_dir (vendors, data_dir);
1269 read_vendor_files_from_dir (GList *vendors,
1270 const gchar *dirname)
1273 const gchar *filename, *extension;
1274 gchar *full_filename = NULL;
1275 GError *gerror = NULL;
1276 GList *new_vendors = NULL;
1278 if (dirname == NULL) {
1282 if (!g_file_test (dirname, G_FILE_TEST_EXISTS)) {
1286 dp = g_dir_open (dirname, 0, &gerror);
1287 if (gerror != NULL) {
1288 g_message ("cannot open data directory: %s", gerror->message );
1292 while ((filename = g_dir_read_name (dp)) != NULL) {
1294 extension = strrchr (filename, '.');
1296 if (extension != NULL) {
1298 if ( ASCII_EQUAL (filename, "vendors.xml") )
1302 g_build_filename (dirname, filename, NULL);
1304 lgl_xml_vendor_read_vendors_from_file (full_filename);
1305 g_free (full_filename);
1307 vendors = g_list_concat (vendors, new_vendors);
1323 * lgl_db_print_known_vendors:
1325 * For debugging purposes: print a list of all vendor definitions known to
1330 lgl_db_print_known_vendors (void)
1339 g_print ("%s():\n", __FUNCTION__);
1340 for (p = model->vendors; p != NULL; p = p->next) {
1341 vendor = (lglVendor *) p->data;
1343 g_print ("VENDOR name=\"%s\", url=\"%s\"\n",
1344 vendor->name, vendor->url);
1352 /*===========================================*/
1353 /* Brand db functions. */
1354 /*===========================================*/
1357 * lgl_db_get_brand_list:
1358 * @paper_id: If non NULL, limit results to given page size.
1359 * @category_id: If non NULL, limit results to given template category.
1361 * Get a list of all valid brands of templates in the template database.
1362 * Results can be filtered by page size and/or template category. A list of valid page
1363 * sizes can be obtained using lgl_db_get_paper_id_list(). A list of valid template
1364 * categories can be obtained using lgl_db_get_category_id_list().
1366 * Returns: a list of brands
1369 lgl_db_get_brand_list (const gchar *paper_id,
1370 const gchar *category_id)
1372 GList *p_tmplt, *p_alias;
1373 lglTemplate *template;
1374 lglTemplateAlias *alias;
1375 GList *brands = NULL;
1382 for (p_tmplt = model->templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1384 template = (lglTemplate *) p_tmplt->data;
1385 if (lgl_template_does_page_size_match (template, paper_id) &&
1386 lgl_template_does_category_match (template, category_id))
1388 for (p_alias = template->aliases; p_alias != NULL;
1389 p_alias = p_alias->next)
1391 alias = (lglTemplateAlias *)p_alias->data;
1393 if ( !g_list_find_custom (brands, alias->brand,
1394 (GCompareFunc)lgl_str_utf8_casecmp) )
1396 brands = g_list_insert_sorted (brands,
1397 g_strdup (alias->brand),
1398 (GCompareFunc)lgl_str_utf8_casecmp);
1409 * lgl_db_free_brand_list:
1410 * @brands: List of template brand strings to be freed.
1412 * Free up all storage associated with a list of template names obtained with
1413 * lgl_db_get_brand_list().
1417 lgl_db_free_brand_list (GList *brands)
1421 for (p_brand = brands; p_brand != NULL; p_brand = p_brand->next)
1423 g_free (p_brand->data);
1424 p_brand->data = NULL;
1427 g_list_free (brands);
1431 /*===========================================*/
1432 /* Template db functions. */
1433 /*===========================================*/
1436 _lgl_db_register_template_internal (const lglTemplate *template)
1438 lglTemplate *template_copy;
1440 if (!lgl_db_does_template_exist (template->brand, template->part))
1442 template_copy = lgl_template_dup (template);
1443 model->templates = g_list_append (model->templates, template_copy);
1444 add_to_template_cache (template_copy);
1448 g_message ("Duplicate template: %s %s.", template->brand, template->part);
1454 * lgl_db_register_template:
1455 * @template: Pointer to a template structure to add to database.
1457 * Register a template. This function adds a template to the template database.
1458 * The template will be stored in an individual XML file in the user template directory.
1460 * Returns: Status of registration attempt (#lglDbRegStatus)
1463 lgl_db_register_template (const lglTemplate *template)
1465 lglTemplate *template_copy;
1466 gchar *dir, *filename, *abs_filename;
1474 if (lgl_db_does_template_exist (template->brand, template->part))
1476 return LGL_DB_REG_BRAND_PART_EXISTS;
1479 if (lgl_db_is_paper_id_known (template->paper_id))
1481 dir = LGL_USER_DATA_DIR;
1482 mkdir (dir, 0775); /* Try to make sure directory exists. */
1483 filename = g_strdup_printf ("%s_%s.template", template->brand, template->part);
1484 abs_filename = g_build_filename (dir, filename, NULL);
1485 bytes_written = lgl_xml_template_write_template_to_file (template, abs_filename);
1488 g_free (abs_filename);
1490 if (bytes_written > 0)
1492 template_copy = lgl_template_dup (template);
1493 lgl_template_add_category (template_copy, "user-defined");
1494 model->templates = g_list_append (model->templates, template_copy);
1495 add_to_template_cache (template_copy);
1496 g_signal_emit (G_OBJECT (model), signals[CHANGED], 0);
1497 return LGL_DB_REG_OK;
1501 return LGL_DB_REG_FILE_WRITE_ERROR;
1506 g_message ("Cannot register new template with unknown page size.");
1507 return LGL_DB_REG_BAD_PAPER_ID;
1514 lgl_db_delete_template_by_name (const gchar *name)
1516 lglTemplate *template, *template1;
1517 gchar *dir, *filename, *abs_filename;
1525 if (!lgl_db_does_template_name_exist (name))
1527 return LGL_DB_DELETE_DOES_NOT_EXIST;
1530 template = lgl_db_lookup_template_from_name (name);
1531 if ( lgl_template_does_category_match (template, "user-defined") )
1533 dir = LGL_USER_DATA_DIR;
1534 filename = g_strdup_printf ("%s_%s.template", template->brand, template->part);
1535 abs_filename = g_build_filename (dir, filename, NULL);
1537 g_free (abs_filename);
1541 g_free (abs_filename);
1543 for ( p=model->templates; p != NULL; p=p->next )
1545 template1 = (lglTemplate *)p->data;
1547 if ( lgl_template_do_templates_match (template, template1) )
1549 model->templates = g_list_delete_link (model->templates, p);
1550 g_hash_table_remove (model->template_cache, name);
1555 lgl_template_free (template);
1557 g_signal_emit (G_OBJECT (model), signals[CHANGED], 0);
1558 return LGL_DB_DELETE_OK;
1562 return LGL_DB_DELETE_NOT_USER_DEFINED;
1569 lgl_db_delete_template_by_brand_part (const gchar *brand,
1573 lglDbDeleteStatus status;
1575 name = g_strdup_printf ("%s %s", brand, part);
1577 status = lgl_db_delete_template_by_name (name);
1586 * lgl_db_does_template_exist:
1587 * @brand: Brand name.
1588 * @part: Part name/number.
1590 * This function tests whether a template with the given brand and part name/number exists.
1592 * Returns: TRUE if such a template exists in the database.
1595 lgl_db_does_template_exist (const gchar *brand,
1598 GList *p_tmplt, *p_alias;
1599 lglTemplate *template;
1600 lglTemplateAlias *alias;
1607 if ((brand == NULL) || (part == NULL))
1612 for (p_tmplt = model->templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1614 template = (lglTemplate *) p_tmplt->data;
1615 for (p_alias = template->aliases; p_alias != NULL; p_alias = p_alias->next)
1617 alias = (lglTemplateAlias *)p_alias->data;
1619 if ( UTF8_EQUAL (brand, alias->brand) &&
1620 UTF8_EQUAL (part, alias->part) )
1632 * lgl_db_does_template_name_exist:
1633 * @name: name string
1635 * This function test whether a template with the given name exists.
1637 * Returns: TRUE if such a template exists in the database.
1641 lgl_db_does_template_name_exist (const gchar *name)
1643 GList *p_tmplt, *p_alias;
1644 lglTemplate *template;
1645 lglTemplateAlias *alias;
1646 gchar *candidate_name;
1658 for (p_tmplt = model->templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1660 template = (lglTemplate *) p_tmplt->data;
1661 for (p_alias = template->aliases; p_alias != NULL; p_alias = p_alias->next)
1663 alias = (lglTemplateAlias *)p_alias->data;
1664 candidate_name = g_strdup_printf ("%s %s", alias->brand, alias->part);
1666 if ( UTF8_EQUAL (candidate_name, name) ) {
1667 g_free (candidate_name);
1670 g_free (candidate_name);
1679 * lgl_db_get_template_name_list_unique:
1680 * @brand: If non NULL, limit results to given brand
1681 * @paper_id: If non NULL, limit results to given page size.
1682 * @category_id: If non NULL, limit results to given template category.
1684 * Get a list of valid names of unique templates in the template database. Results
1685 * can be filtered by page size and/or template category. A list of valid page sizes
1686 * can be obtained using lgl_db_get_paper_id_list(). A list of valid template categories
1687 * can be obtained using lgl_db_get_category_id_list().
1689 * This function differs from lgl_db_get_template_name_list_all(), because it does not
1690 * return multiple names for the same template.
1692 * Returns: a list of template names.
1695 lgl_db_get_template_name_list_unique (const gchar *brand,
1696 const gchar *paper_id,
1697 const gchar *category_id)
1700 lglTemplate *template;
1702 GList *names = NULL;
1709 for (p_tmplt = model->templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1711 template = (lglTemplate *) p_tmplt->data;
1713 if (lgl_template_does_brand_match (template, brand) &&
1714 lgl_template_does_page_size_match (template, paper_id) &&
1715 lgl_template_does_category_match (template, category_id))
1717 name = g_strdup_printf ("%s %s", template->brand, template->part);
1718 names = g_list_insert_sorted (names, name,
1719 (GCompareFunc)lgl_str_part_name_cmp);
1728 * lgl_db_get_template_name_list_all:
1729 * @brand: If non NULL, limit results to given brand
1730 * @paper_id: If non NULL, limit results to given page size.
1731 * @category_id: If non NULL, limit results to given template category.
1733 * Get a list of all valid names and aliases of templates in the template database.
1734 * Results can be filtered by page size and/or template category. A list of valid page
1735 * sizes can be obtained using lgl_db_get_paper_id_list(). A list of valid template
1736 * categories can be obtained using lgl_db_get_category_id_list().
1738 * This function differs from lgl_db_get_template_name_list_unique(), because it will
1739 * return multiple names for the same template.
1741 * Returns: a list of template names and aliases.
1744 lgl_db_get_template_name_list_all (const gchar *brand,
1745 const gchar *paper_id,
1746 const gchar *category_id)
1748 GList *p_tmplt, *p_alias;
1749 lglTemplate *template;
1750 lglTemplateAlias *alias;
1752 GList *names = NULL;
1759 for (p_tmplt = model->templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1761 template = (lglTemplate *) p_tmplt->data;
1762 if (lgl_template_does_page_size_match (template, paper_id) &&
1763 lgl_template_does_category_match (template, category_id))
1765 for (p_alias = template->aliases; p_alias != NULL;
1766 p_alias = p_alias->next)
1768 alias = (lglTemplateAlias *)p_alias->data;
1770 if ( !brand || UTF8_EQUAL( alias->brand, brand) )
1772 name = g_strdup_printf ("%s %s", alias->brand, alias->part);
1773 names = g_list_insert_sorted (names, name,
1774 (GCompareFunc)lgl_str_part_name_cmp);
1785 * lgl_db_get_similar_template_name_list:
1786 * @name: Name of template under test.
1788 * Get a list of all valid names and aliases of templates in the template database that
1789 * have the same size and layout characteristics as the given template.
1791 * Returns: a list of template names and aliases.
1794 lgl_db_get_similar_template_name_list (const gchar *name)
1796 GList *p_tmplt, *p_alias;
1797 lglTemplate *template1;
1798 lglTemplate *template2;
1799 lglTemplateAlias *alias;
1801 GList *names = NULL;
1813 template1 = lgl_db_lookup_template_from_name (name);
1819 for (p_alias = template1->aliases; p_alias != NULL; p_alias = p_alias->next)
1821 alias = (lglTemplateAlias *)p_alias->data;
1823 name2 = g_strdup_printf ("%s %s", alias->brand, alias->part);
1824 if ( !UTF8_EQUAL (name2, name) )
1826 names = g_list_insert_sorted (names, name2,
1827 (GCompareFunc)lgl_str_part_name_cmp);
1831 for (p_tmplt = model->templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1833 template2 = (lglTemplate *) p_tmplt->data;
1835 if ( lgl_template_are_templates_identical (template1, template2) )
1837 for (p_alias = template2->aliases; p_alias != NULL; p_alias = p_alias->next)
1839 alias = (lglTemplateAlias *)p_alias->data;
1841 name2 = g_strdup_printf ("%s %s", alias->brand, alias->part);
1842 if ( !UTF8_EQUAL (name2, name) )
1844 names = g_list_insert_sorted (names, name2,
1845 (GCompareFunc)lgl_str_part_name_cmp);
1856 * lgl_db_free_template_name_list:
1857 * @names: List of template name strings to be freed.
1859 * Free up all storage associated with a list of template names obtained with
1860 * lgl_db_get_template_name_list_all() or lgl_db_get_template_name_list_unique().
1864 lgl_db_free_template_name_list (GList *names)
1868 for (p_name = names; p_name != NULL; p_name = p_name->next)
1870 g_free (p_name->data);
1871 p_name->data = NULL;
1874 g_list_free (names);
1879 * lgl_db_lookup_template_from_name:
1880 * @name: name string
1882 * Lookup template in template database from name string.
1884 * Returns: pointer to a newly allocated #lglTemplate structure.
1888 lgl_db_lookup_template_from_name (const gchar *name)
1891 lglTemplate *template;
1892 lglTemplateAlias *alias;
1893 gchar *candidate_name;
1894 lglTemplate *new_template;
1903 /* If no name, return first template as a default */
1904 return lgl_template_dup ((lglTemplate *) model->templates->data);
1907 template = g_hash_table_lookup (model->template_cache, name);
1911 for (p_alias = template->aliases; p_alias != NULL; p_alias = p_alias->next)
1913 alias = (lglTemplateAlias *)p_alias->data;
1914 candidate_name = g_strdup_printf ("%s %s", alias->brand, alias->part);
1916 if ( UTF8_EQUAL (candidate_name, name) )
1918 g_free (candidate_name);
1919 new_template = lgl_template_dup (template);
1920 g_free (new_template->brand);
1921 new_template->brand = g_strdup (alias->brand);
1922 g_free (new_template->part);
1923 new_template->part = g_strdup (alias->part);
1924 return new_template;
1927 g_free (candidate_name);
1931 /* No matching template has been found so return the first template */
1932 return lgl_template_dup ((lglTemplate *) model->templates->data);
1937 * lgl_db_lookup_template_from_brand_part:
1938 * @brand: brand name string
1939 * @part: part name string
1941 * Lookup template in template database from brand and part strings.
1943 * Returns: pointer to a newly allocated #lglTemplate structure.
1947 lgl_db_lookup_template_from_brand_part(const gchar *brand,
1952 lglTemplate *template;
1953 lglTemplateAlias *alias;
1954 gchar *candidate_name;
1955 lglTemplate *new_template;
1962 if ((brand == NULL) || (part == NULL))
1964 /* If no name, return first template as a default */
1965 return lgl_template_dup ((lglTemplate *) model->templates->data);
1968 name = g_strdup_printf ("%s %s", brand, part);
1969 template = g_hash_table_lookup (model->template_cache, name);
1973 for (p_alias = template->aliases; p_alias != NULL; p_alias = p_alias->next)
1975 alias = (lglTemplateAlias *)p_alias->data;
1976 candidate_name = g_strdup_printf ("%s %s", alias->brand, alias->part);
1978 if ( UTF8_EQUAL (candidate_name, name) )
1980 g_free (candidate_name);
1981 new_template = lgl_template_dup (template);
1982 g_free (new_template->brand);
1983 new_template->brand = g_strdup (alias->brand);
1984 g_free (new_template->part);
1985 new_template->part = g_strdup (alias->part);
1987 return new_template;
1990 g_free (candidate_name);
1994 /* No matching template has been found so return the first template */
1996 return lgl_template_dup ((lglTemplate *) model->templates->data);
2001 add_to_template_cache (lglTemplate *template)
2004 lglTemplateAlias *alias;
2007 for ( p_alias=template->aliases; p_alias != NULL; p_alias=p_alias->next )
2009 alias = (lglTemplateAlias *)p_alias->data;
2010 name = g_strdup_printf ("%s %s", alias->brand, alias->part);
2012 g_hash_table_insert (model->template_cache, name, template);
2018 read_templates (void)
2022 lglTemplate *template;
2025 * User defined templates. Add to user-defined category.
2027 data_dir = LGL_USER_DATA_DIR;
2028 read_template_files_from_dir (data_dir);
2030 for ( p=model->templates; p != NULL; p=p->next )
2032 template = (lglTemplate *)p->data;
2033 lgl_template_add_category (template, "user-defined");
2039 data_dir = LGL_SYSTEM_DATA_DIR;
2040 read_template_files_from_dir (data_dir);
2043 if (model->templates == NULL)
2045 g_critical (_("Unable to locate any template files. Libglabels may not be installed correctly!"));
2051 read_template_files_from_dir (const gchar *dirname)
2054 const gchar *filename, *extension, *extension2;
2055 gchar *full_filename = NULL;
2056 GError *gerror = NULL;
2058 if (dirname == NULL)
2061 if (!g_file_test (dirname, G_FILE_TEST_EXISTS))
2066 dp = g_dir_open (dirname, 0, &gerror);
2069 g_message ("cannot open data directory: %s", gerror->message );
2073 while ((filename = g_dir_read_name (dp)) != NULL)
2076 extension = strrchr (filename, '.');
2077 extension2 = strrchr (filename, '-');
2079 if ( (extension && ASCII_EQUAL (extension, ".template")) ||
2080 (extension2 && ASCII_EQUAL (extension2, "-templates.xml")) )
2083 full_filename = g_build_filename (dirname, filename, NULL);
2084 lgl_xml_template_read_templates_from_file (full_filename);
2085 g_free (full_filename);
2094 static lglTemplate *
2095 template_full_page (const gchar *paper_id)
2097 lglPaper *paper = NULL;
2098 lglTemplate *template = NULL;
2099 lglTemplateFrame *frame = NULL;
2103 g_return_val_if_fail (paper_id, NULL);
2105 paper = lgl_db_lookup_paper_from_id (paper_id);
2106 if ( paper == NULL )
2111 part = g_strdup_printf ("%s-Full-Page", paper->id);
2112 desc = g_strdup_printf (_("%s full page label"), paper->name);
2114 template = lgl_template_new ("Generic", part, desc,
2115 paper_id, paper->width, paper->height);
2118 frame = lgl_template_frame_rect_new ("0",
2124 lgl_template_add_frame (template, frame);
2126 lgl_template_frame_add_layout (frame, lgl_template_layout_new (1, 1, 0., 0., 0., 0.));
2128 lgl_template_frame_add_markup (frame, lgl_template_markup_margin_new (9.0));
2132 lgl_paper_free (paper);
2140 * lgl_db_print_known_templates:
2142 * Print all known templates (for debugging purposes).
2146 lgl_db_print_known_templates (void)
2149 lglTemplate *template;
2156 g_print ("%s():\n", __FUNCTION__);
2157 for (p=model->templates; p!=NULL; p=p->next)
2159 template = (lglTemplate *)p->data;
2161 g_print("TEMPLATE brand=\"%s\", part=\"%s\", description=\"%s\"\n",
2162 template->brand, template->part, template->description);
2171 * lgl_db_print_aliases:
2172 * @template: template
2174 * Print all aliases of a template (for debugging purposes).
2178 lgl_db_print_aliases (const lglTemplate *template)
2181 lglTemplateAlias *alias;
2188 g_print ("%s():\n", __FUNCTION__);
2189 for (p=template->aliases; p!=NULL; p=p->next)
2191 alias = (lglTemplateAlias *)p->data;
2193 g_print("Alias: brand=\"%s\", part=\"%s\"\n", alias->brand, alias->part);
2203 * Local Variables: -- emacs
2205 * c-basic-offset: 8 -- emacs
2206 * tab-width: 8 -- emacs
2207 * indent-tabs-mode: nil -- emacs