3 * Copyright (C) 2003-2010 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/gstdio.h>
28 #include <glib-object.h>
31 #include <sys/types.h>
33 #include "libglabels-private.h"
35 #include "lgl-xml-paper.h"
36 #include "lgl-xml-category.h"
37 #include "lgl-xml-vendor.h"
38 #include "lgl-xml-template.h"
40 /*===========================================*/
41 /* Private macros and constants. */
42 /*===========================================*/
44 /* Data system and user data directories. (must free w/ g_free()) */
45 #define SYSTEM_CONFIG_DIR g_build_filename (LIBGLABELS_CONFIG_DIR, "templates", NULL)
46 #define USER_CONFIG_DIR g_build_filename (g_get_user_config_dir (), "libglabels", "templates" , NULL)
47 #define ALT_USER_CONFIG_DIR g_build_filename (g_get_home_dir (), ".glabels", NULL)
50 /*===========================================*/
52 /*===========================================*/
54 #define TYPE_LGL_DB_MODEL (lgl_db_model_get_type ())
55 #define LGL_DB_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_LGL_DB_MODEL, lglDbModel))
56 #define LGL_DB_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_LGL_DB_MODEL, lglDbModelClass))
57 #define IS_LGL_DB_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_LGL_DB_MODEL))
58 #define IS_LGL_DB_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_LGL_DB_MODEL))
59 #define LGL_DB_MODEL_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS ((object), TYPE_LGL_DB_MODEL, lglDbModelClass))
62 typedef struct _lglDbModel lglDbModel;
63 typedef struct _lglDbModelClass lglDbModelClass;
74 GHashTable *template_cache;
78 struct _lglDbModelClass {
79 GObjectClass parent_class;
84 void (*changed) (lglDbModel *this,
96 /*===========================================*/
98 /*===========================================*/
100 static guint signals[LAST_SIGNAL] = {0};
102 static lglDbModel *model = NULL;
105 /*===========================================*/
106 /* Local function prototypes */
107 /*===========================================*/
109 static void lgl_db_model_finalize (GObject *object);
111 static void add_to_template_cache (lglTemplate *template);
113 static GList *read_papers (void);
114 static GList *read_paper_files_from_dir (GList *papers,
115 const gchar *dirname);
117 static GList *read_categories (void);
118 static GList *read_category_files_from_dir (GList *categories,
119 const gchar *dirname);
121 static GList *read_vendors (void);
122 static GList *read_vendor_files_from_dir (GList *vendors,
123 const gchar *dirname);
125 static void read_templates (void);
126 static void read_template_files_from_dir (const gchar *dirname);
128 static lglTemplate *template_full_page (const gchar *page_size);
131 /*****************************************************************************/
132 /* Object infrastructure. */
133 /*****************************************************************************/
134 G_DEFINE_TYPE (lglDbModel, lgl_db_model, G_TYPE_OBJECT);
137 /*****************************************************************************/
138 /* Class Init Function. */
139 /*****************************************************************************/
141 lgl_db_model_class_init (lglDbModelClass *class)
143 GObjectClass *gobject_class = (GObjectClass *) class;
145 lgl_db_model_parent_class = g_type_class_peek_parent (class);
147 gobject_class->finalize = lgl_db_model_finalize;
150 g_signal_new ("changed",
151 G_OBJECT_CLASS_TYPE (gobject_class),
153 G_STRUCT_OFFSET (lglDbModelClass, changed),
155 g_cclosure_marshal_VOID__VOID,
161 /*****************************************************************************/
162 /* Object Instance Init Function. */
163 /*****************************************************************************/
165 lgl_db_model_init (lglDbModel *this)
167 this->template_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)lgl_template_free);
171 /*****************************************************************************/
172 /* Finalize Method. */
173 /*****************************************************************************/
175 lgl_db_model_finalize (GObject *object)
180 g_return_if_fail (object && IS_LGL_DB_MODEL (object));
181 this = LGL_DB_MODEL (object);
183 g_hash_table_unref (this->template_cache);
185 for (p = this->papers; p != NULL; p = p->next)
190 g_list_free (this->papers);
192 for (p = this->categories; p != NULL; p = p->next)
197 g_list_free (this->categories);
199 for (p = this->vendors; p != NULL; p = p->next)
204 g_list_free (this->vendors);
206 for (p = this->templates; p != NULL; p = p->next)
208 lgl_template_free ((lglTemplate *)p->data);
211 g_list_free (this->templates);
213 G_OBJECT_CLASS (lgl_db_model_parent_class)->finalize (object);
217 /*****************************************************************************/
218 /** New Object Generator. */
219 /*****************************************************************************/
221 lgl_db_model_new (void)
225 this = g_object_new (TYPE_LGL_DB_MODEL, NULL);
231 /*===========================================*/
232 /* Module initialization */
233 /*===========================================*/
238 * Initialize all libglabels subsystems. It is not necessary for an application to call
239 * lgl_db_init(), because libglabels will initialize on demand. An application programmer may
240 * choose to call lgl_db_init() at startup to minimize the impact of the first libglabels call
241 * on GUI response time.
243 * This function initializes its paper definitions, category definitions, vendor definitions,
244 * and its template database. It will search both system and user template directories to locate
250 lglPaper *paper_other;
251 lglCategory *category_user_defined;
252 lglTemplate *template;
256 model = lgl_db_model_new ();
261 model->papers = read_papers ();
263 /* Create and append an "Other" entry. */
264 /* Translators: "Other" here means other page size. Meaning a page size
265 * other than the standard ones that libglabels knows about such as
266 * "letter", "A4", etc. */
267 paper_other = lgl_paper_new ("Other", _("Other"), 0.0, 0.0, NULL);
268 model->papers = g_list_append (model->papers, paper_other);
273 model->categories = read_categories ();
275 /* Create and append a "User defined" entry. */
276 category_user_defined = lgl_category_new ("user-defined", _("User defined"));
277 model->categories = g_list_append (model->categories, category_user_defined);
282 model->vendors = read_vendors ();
289 /* Create and append generic full page templates. */
290 page_sizes = lgl_db_get_paper_id_list ();
291 for ( p=page_sizes; p != NULL; p=p->next )
293 if ( !lgl_db_is_paper_id_other (p->data) )
295 template = template_full_page (p->data);
296 _lgl_db_register_template_internal (template);
297 lgl_template_free (template);
300 lgl_db_free_paper_id_list (page_sizes);
307 * @func: Callback function to be called when database changes.
308 * @user_data: Passback user data to supply to callback function.
310 * Register a notification callback function to be called when the database changes.
312 * Returns: an ID for this notification registration.
315 lgl_db_notify_add (lglDbNotifyFunc func,
323 return g_signal_connect_swapped (G_OBJECT (model), "changed", G_CALLBACK (func), user_data);
328 * lgl_db_notify_remove:
329 * @id: ID of notification registration to cancel (see lgl_db_notify_add()).
331 * Cancel a previous registration a notification callback function.
334 lgl_db_notify_remove (gulong id)
336 g_signal_handler_disconnect (G_OBJECT (model), id);
340 /*===========================================*/
341 /* Paper db functions. */
342 /*===========================================*/
345 * lgl_db_get_paper_id_list:
347 * Get a list of all paper ids known to libglabels.
349 * Returns: a list of paper ids.
353 lgl_db_get_paper_id_list (void)
364 for ( p=model->papers; p != NULL; p=p->next )
366 paper = (lglPaper *)p->data;
367 ids = g_list_append (ids, g_strdup (paper->id));
375 * lgl_db_free_paper_id_list:
376 * @ids: List of id strings to be freed.
378 * Free up all storage associated with an id list obtained with
379 * lgl_db_get_paper_id_list().
383 lgl_db_free_paper_id_list (GList *ids)
387 for (p = ids; p != NULL; p = p->next)
398 * lgl_db_get_paper_name_list:
400 * Get a list of all localized paper names known to libglabels.
402 * Returns: a list of localized paper names.
406 lgl_db_get_paper_name_list (void)
417 for ( p=model->papers; p != NULL; p=p->next )
419 paper = (lglPaper *)p->data;
420 names = g_list_append (names, g_strdup (paper->name));
428 * lgl_db_free_paper_name_list:
429 * @names: List of localized paper name strings to be freed.
431 * Free up all storage associated with a name list obtained with
432 * lgl_db_get_paper_name_list().
436 lgl_db_free_paper_name_list (GList *names)
440 for (p = names; p != NULL; p = p->next)
451 * lgl_db_lookup_paper_from_name:
452 * @name: localized paper name string
454 * Lookup paper definition from localized paper name string.
456 * Returns: pointer to a newly allocated #lglPaper structure.
460 lgl_db_lookup_paper_from_name (const gchar *name)
472 /* If no name, return first paper as a default */
473 return lgl_paper_dup ((lglPaper *) model->papers->data);
476 for (p = model->papers; p != NULL; p = p->next)
478 paper = (lglPaper *) p->data;
479 if (UTF8_EQUAL (paper->name, name))
481 return lgl_paper_dup (paper);
490 * lgl_db_lookup_paper_from_id:
491 * @id: paper id string
493 * Lookup paper definition from id string.
495 * Returns: pointer to a newly allocated #lglPaper structure.
499 lgl_db_lookup_paper_from_id (const gchar *id)
511 /* If no id, return first paper as a default */
512 return lgl_paper_dup ((lglPaper *) model->papers->data);
515 for (p = model->papers; p != NULL; p = p->next)
517 paper = (lglPaper *) p->data;
518 if (ASCII_EQUAL (paper->id, id))
520 return lgl_paper_dup (paper);
529 * lgl_db_lookup_paper_id_from_name:
530 * @name: localized paper name stringp
532 * Lookup paper name string from localized paper name string.
534 * Returns: pointer to a newly allocated id string.
538 lgl_db_lookup_paper_id_from_name (const gchar *name)
540 lglPaper *paper = NULL;
545 paper = lgl_db_lookup_paper_from_name (name);
548 id = g_strdup (paper->id);
549 lgl_paper_free (paper);
559 * lgl_db_lookup_paper_name_from_id:
560 * @id: paper id string
562 * Lookup localized paper name string from paper id string.
564 * Returns: pointer to a newly allocated localized paper name string.
568 lgl_db_lookup_paper_name_from_id (const gchar *id)
570 lglPaper *paper = NULL;
575 paper = lgl_db_lookup_paper_from_id (id);
578 name = g_strdup (paper->name);
579 lgl_paper_free (paper);
589 * lgl_db_is_paper_id_known:
590 * @id: paper id to test
592 * Determine if given paper id is known to libglabels.
594 * Returns: TRUE if id is known, otherwise FALSE.
598 lgl_db_is_paper_id_known (const gchar *id)
613 for (p = model->papers; p != NULL; p = p->next)
615 paper = (lglPaper *) p->data;
616 if (ASCII_EQUAL (paper->id, id))
627 * lgl_db_is_paper_id_other:
628 * @id: paper id to test
630 * Determine if given paper id is the special id "Other."
632 * Returns: TRUE if id is "Other", otherwise FALSE.
636 lgl_db_is_paper_id_other (const gchar *id)
643 return (ASCII_EQUAL (id, "Other"));
651 GList *papers = NULL;
653 data_dir = SYSTEM_CONFIG_DIR;
654 papers = read_paper_files_from_dir (papers, data_dir);
657 data_dir = USER_CONFIG_DIR;
658 papers = read_paper_files_from_dir (papers, data_dir);
663 g_critical (_("Unable to locate paper size definitions. Libglabels may not be installed correctly!"));
671 read_paper_files_from_dir (GList *papers,
672 const gchar *dirname)
675 const gchar *filename, *extension;
676 gchar *full_filename = NULL;
677 GError *gerror = NULL;
678 GList *new_papers = NULL;
685 if (!g_file_test (dirname, G_FILE_TEST_EXISTS))
690 dp = g_dir_open (dirname, 0, &gerror);
693 g_message ("cannot open data directory: %s", gerror->message );
697 while ((filename = g_dir_read_name (dp)) != NULL)
700 extension = strrchr (filename, '.');
702 if (extension != NULL)
705 if ( ASCII_EQUAL (filename, "paper-sizes.xml") )
709 g_build_filename (dirname, filename, NULL);
711 lgl_xml_paper_read_papers_from_file (full_filename);
712 g_free (full_filename);
714 papers = g_list_concat (papers, new_papers);
730 * lgl_db_print_known_papers:
732 * For debugging purposes: print a list of all paper definitions known to
737 lgl_db_print_known_papers (void)
747 g_print ("%s():\n", __FUNCTION__);
748 for (p = model->papers; p != NULL; p = p->next)
750 paper = (lglPaper *) p->data;
752 g_print ("PAPER id=\"%s\", name=\"%s\", width=%gpts, height=%gpts\n",
753 paper->id, paper->name, paper->width, paper->height);
760 /*===========================================*/
761 /* Category db functions. */
762 /*===========================================*/
765 * lgl_db_get_category_id_list:
767 * Get a list of all category ids known to libglabels.
769 * Returns: a list of category ids.
773 lgl_db_get_category_id_list (void)
777 lglCategory *category;
784 for ( p=model->categories; p != NULL; p=p->next )
786 category = (lglCategory *)p->data;
787 ids = g_list_append (ids, g_strdup (category->id));
795 * lgl_db_free_category_id_list:
796 * @ids: List of id strings to be freed.
798 * Free up all storage associated with an id list obtained with
799 * lgl_db_get_category_id_list().
803 lgl_db_free_category_id_list (GList *ids)
807 for (p = ids; p != NULL; p = p->next)
818 * lgl_db_get_category_name_list:
820 * Get a list of all localized category names known to libglabels.
822 * Returns: a list of localized category names.
826 lgl_db_get_category_name_list (void)
830 lglCategory *category;
837 for ( p=model->categories; p != NULL; p=p->next )
839 category = (lglCategory *)p->data;
840 names = g_list_append (names, g_strdup (category->name));
848 * lgl_db_free_category_name_list:
849 * @names: List of localized category name strings to be freed.
851 * Free up all storage associated with a name list obtained with
852 * lgl_db_get_category_name_list().
856 lgl_db_free_category_name_list (GList *names)
860 for (p = names; p != NULL; p = p->next)
871 * lgl_db_lookup_category_from_name:
872 * @name: localized category name string
874 * Lookup category definition from localized category name string.
876 * Returns: pointer to a newly allocated #lglCategory structure.
880 lgl_db_lookup_category_from_name (const gchar *name)
883 lglCategory *category;
892 /* If no name, return first category as a default */
893 return lgl_category_dup ((lglCategory *) model->categories->data);
896 for (p = model->categories; p != NULL; p = p->next)
898 category = (lglCategory *) p->data;
899 if (UTF8_EQUAL (category->name, name))
901 return lgl_category_dup (category);
910 * lgl_db_lookup_category_from_id:
911 * @id: category id string
913 * Lookup category definition from id string.
915 * Returns: pointer to a newly allocated #lglCategory structure.
919 lgl_db_lookup_category_from_id (const gchar *id)
922 lglCategory *category;
931 /* If no id, return first category as a default */
932 return lgl_category_dup ((lglCategory *) model->categories->data);
935 for (p = model->categories; p != NULL; p = p->next)
937 category = (lglCategory *) p->data;
938 if (ASCII_EQUAL (category->id, id))
940 return lgl_category_dup (category);
949 * lgl_db_lookup_category_id_from_name:
950 * @name: localized category name stringp
952 * Lookup category name string from localized category name string.
954 * Returns: pointer to a newly allocated id string.
958 lgl_db_lookup_category_id_from_name (const gchar *name)
960 lglCategory *category = NULL;
965 category = lgl_db_lookup_category_from_name (name);
966 if ( category != NULL )
968 id = g_strdup (category->id);
969 lgl_category_free (category);
979 * lgl_db_lookup_category_name_from_id:
980 * @id: category id string
982 * Lookup localized category name string from category id string.
984 * Returns: pointer to a newly allocated localized category name string.
988 lgl_db_lookup_category_name_from_id (const gchar *id)
990 lglCategory *category = NULL;
995 category = lgl_db_lookup_category_from_id (id);
996 if ( category != NULL )
998 name = g_strdup (category->name);
999 lgl_category_free (category);
1009 * lgl_db_is_category_id_known:
1010 * @id: category id to test
1012 * Determine if given category id is known to libglabels.
1014 * Returns: TRUE if id is known, otherwise FALSE.
1018 lgl_db_is_category_id_known (const gchar *id)
1021 lglCategory *category;
1033 for (p = model->categories; p != NULL; p = p->next)
1035 category = (lglCategory *) p->data;
1036 if (ASCII_EQUAL (category->id, id))
1047 read_categories (void)
1050 GList *categories = NULL;
1052 data_dir = SYSTEM_CONFIG_DIR;
1053 categories = read_category_files_from_dir (categories, data_dir);
1056 data_dir = USER_CONFIG_DIR;
1057 categories = read_category_files_from_dir (categories, data_dir);
1060 if (categories == NULL)
1062 g_critical (_("Unable to locate category definitions. Libglabels may not be installed correctly!"));
1070 read_category_files_from_dir (GList *categories,
1071 const gchar *dirname)
1074 const gchar *filename, *extension;
1075 gchar *full_filename = NULL;
1076 GError *gerror = NULL;
1077 GList *new_categories = NULL;
1079 if (dirname == NULL)
1084 if (!g_file_test (dirname, G_FILE_TEST_EXISTS))
1089 dp = g_dir_open (dirname, 0, &gerror);
1092 g_message ("cannot open data directory: %s", gerror->message );
1096 while ((filename = g_dir_read_name (dp)) != NULL)
1099 extension = strrchr (filename, '.');
1101 if (extension != NULL)
1104 if ( ASCII_EQUAL (filename, "categories.xml") )
1108 g_build_filename (dirname, filename, NULL);
1110 lgl_xml_category_read_categories_from_file (full_filename);
1111 g_free (full_filename);
1113 categories = g_list_concat (categories, new_categories);
1114 new_categories = NULL;
1129 * lgl_db_print_known_categories:
1131 * For debugging purposes: print a list of all category definitions known to
1136 lgl_db_print_known_categories (void)
1139 lglCategory *category;
1146 g_print ("%s():\n", __FUNCTION__);
1147 for (p = model->categories; p != NULL; p = p->next)
1149 category = (lglCategory *) p->data;
1151 g_print ("CATEGORY id=\"%s\", name=\"%s\"\n", category->id, category->name);
1158 /*===========================================*/
1159 /* Vendor db functions. */
1160 /*===========================================*/
1163 * lgl_db_get_vendor_name_list:
1165 * Get a list of all localized vendor names known to libglabels.
1167 * Returns: a list of localized vendor names.
1171 lgl_db_get_vendor_name_list (void)
1173 GList *names = NULL;
1182 for ( p=model->vendors; p != NULL; p=p->next )
1184 vendor = (lglVendor *)p->data;
1185 names = g_list_append (names, g_strdup (vendor->name));
1193 * lgl_db_free_vendor_name_list:
1194 * @names: List of localized vendor name strings to be freed.
1196 * Free up all storage associated with a name list obtained with
1197 * lgl_db_get_vendor_name_list().
1201 lgl_db_free_vendor_name_list (GList *names)
1205 for (p = names; p != NULL; p = p->next)
1211 g_list_free (names);
1216 * lgl_db_lookup_vendor_from_name:
1217 * @name: localized vendor name string
1219 * Lookup vendor definition from localized vendor name string.
1221 * Returns: pointer to a newly allocated #lglVendor structure.
1225 lgl_db_lookup_vendor_from_name (const gchar *name)
1237 /* If no name, return first vendor as a default */
1238 return lgl_vendor_dup ((lglVendor *) model->vendors->data);
1241 for (p = model->vendors; p != NULL; p = p->next)
1243 vendor = (lglVendor *) p->data;
1244 if (UTF8_EQUAL (vendor->name, name))
1246 return lgl_vendor_dup (vendor);
1255 * lgl_db_is_vendor_name_known:
1256 * @name: vendor name to test
1258 * Determine if given vendor id is known to libglabels.
1260 * Returns: TRUE if id is known, otherwise FALSE.
1264 lgl_db_is_vendor_name_known (const gchar *name)
1279 for (p = model->vendors; p != NULL; p = p->next)
1281 vendor = (lglVendor *) p->data;
1282 if (UTF8_EQUAL (vendor->name, name))
1296 GList *vendors = NULL;
1298 data_dir = SYSTEM_CONFIG_DIR;
1299 vendors = read_vendor_files_from_dir (vendors, data_dir);
1302 data_dir = USER_CONFIG_DIR;
1303 vendors = read_vendor_files_from_dir (vendors, data_dir);
1311 read_vendor_files_from_dir (GList *vendors,
1312 const gchar *dirname)
1315 const gchar *filename, *extension;
1316 gchar *full_filename = NULL;
1317 GError *gerror = NULL;
1318 GList *new_vendors = NULL;
1320 if (dirname == NULL)
1325 if (!g_file_test (dirname, G_FILE_TEST_EXISTS))
1330 dp = g_dir_open (dirname, 0, &gerror);
1333 g_message ("cannot open data directory: %s", gerror->message );
1337 while ((filename = g_dir_read_name (dp)) != NULL)
1340 extension = strrchr (filename, '.');
1342 if (extension != NULL)
1345 if ( ASCII_EQUAL (filename, "vendors.xml") )
1349 g_build_filename (dirname, filename, NULL);
1351 lgl_xml_vendor_read_vendors_from_file (full_filename);
1352 g_free (full_filename);
1354 vendors = g_list_concat (vendors, new_vendors);
1370 * lgl_db_print_known_vendors:
1372 * For debugging purposes: print a list of all vendor definitions known to
1377 lgl_db_print_known_vendors (void)
1387 g_print ("%s():\n", __FUNCTION__);
1388 for (p = model->vendors; p != NULL; p = p->next)
1390 vendor = (lglVendor *) p->data;
1392 g_print ("VENDOR name=\"%s\", url=\"%s\"\n",
1393 vendor->name, vendor->url);
1400 /*===========================================*/
1401 /* Brand db functions. */
1402 /*===========================================*/
1405 * lgl_db_get_brand_list:
1406 * @paper_id: If non NULL, limit results to given page size.
1407 * @category_id: If non NULL, limit results to given template category.
1409 * Get a list of all valid brands of templates in the template database.
1410 * Results can be filtered by page size and/or template category. A list of valid page
1411 * sizes can be obtained using lgl_db_get_paper_id_list(). A list of valid template
1412 * categories can be obtained using lgl_db_get_category_id_list().
1414 * Returns: a list of brands
1417 lgl_db_get_brand_list (const gchar *paper_id,
1418 const gchar *category_id)
1421 lglTemplate *template;
1422 GList *brands = NULL;
1429 for (p_tmplt = model->templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1431 template = (lglTemplate *) p_tmplt->data;
1432 if (lgl_template_does_page_size_match (template, paper_id) &&
1433 lgl_template_does_category_match (template, category_id))
1436 if ( !g_list_find_custom (brands, template->brand,
1437 (GCompareFunc)lgl_str_utf8_casecmp) )
1439 brands = g_list_insert_sorted (brands,
1440 g_strdup (template->brand),
1441 (GCompareFunc)lgl_str_utf8_casecmp);
1451 * lgl_db_free_brand_list:
1452 * @brands: List of template brand strings to be freed.
1454 * Free up all storage associated with a list of template names obtained with
1455 * lgl_db_get_brand_list().
1459 lgl_db_free_brand_list (GList *brands)
1463 for (p_brand = brands; p_brand != NULL; p_brand = p_brand->next)
1465 g_free (p_brand->data);
1466 p_brand->data = NULL;
1469 g_list_free (brands);
1473 /*===========================================*/
1474 /* Template db functions. */
1475 /*===========================================*/
1478 _lgl_db_register_template_internal (const lglTemplate *template)
1480 lglTemplate *template_copy;
1482 if (!lgl_db_does_template_exist (template->brand, template->part))
1484 template_copy = lgl_template_dup (template);
1485 model->templates = g_list_append (model->templates, template_copy);
1486 add_to_template_cache (template_copy);
1490 g_message ("Duplicate template: %s %s.", template->brand, template->part);
1496 * lgl_db_register_template:
1497 * @template: Pointer to a template structure to add to database.
1499 * Register a template. This function adds a template to the template database.
1500 * The template will be stored in an individual XML file in the user template directory.
1502 * Returns: Status of registration attempt (#lglDbRegStatus)
1505 lgl_db_register_template (const lglTemplate *template)
1507 lglTemplate *template_copy;
1508 gchar *dir, *filename, *abs_filename;
1516 if (lgl_db_does_template_exist (template->brand, template->part))
1518 return LGL_DB_REG_BRAND_PART_EXISTS;
1521 if (lgl_db_is_paper_id_known (template->paper_id))
1523 dir = USER_CONFIG_DIR;
1524 g_mkdir_with_parents (dir, 0775); /* Try to make sure directory exists. */
1525 filename = g_strdup_printf ("%s_%s.template", template->brand, template->part);
1526 abs_filename = g_build_filename (dir, filename, NULL);
1527 bytes_written = lgl_xml_template_write_template_to_file (template, abs_filename);
1530 g_free (abs_filename);
1532 if (bytes_written > 0)
1534 template_copy = lgl_template_dup (template);
1535 lgl_template_add_category (template_copy, "user-defined");
1536 model->templates = g_list_append (model->templates, template_copy);
1537 add_to_template_cache (template_copy);
1538 g_signal_emit (G_OBJECT (model), signals[CHANGED], 0);
1539 return LGL_DB_REG_OK;
1543 return LGL_DB_REG_FILE_WRITE_ERROR;
1548 g_message ("Cannot register new template with unknown page size.");
1549 return LGL_DB_REG_BAD_PAPER_ID;
1556 * lgl_db_delete_template_by_name:
1557 * @name: Name of template to be deleted.
1559 * Delete a user defined template. This function deletes a template from
1560 * the template database. The individual XML file in the user template
1561 * directory will also be removed.
1563 * Returns: Status of registration attempt (#lglDbDeleteStatus)
1566 lgl_db_delete_template_by_name (const gchar *name)
1568 lglTemplate *template, *template1;
1569 gchar *dir, *filename, *abs_filename;
1577 if (!lgl_db_does_template_name_exist (name))
1579 return LGL_DB_DELETE_DOES_NOT_EXIST;
1582 template = lgl_db_lookup_template_from_name (name);
1583 if ( lgl_template_does_category_match (template, "user-defined") )
1585 dir = USER_CONFIG_DIR;
1586 filename = g_strdup_printf ("%s_%s.template", template->brand, template->part);
1587 abs_filename = g_build_filename (dir, filename, NULL);
1589 if (!g_file_test (abs_filename, G_FILE_TEST_EXISTS))
1591 g_message ("File \"%s\" does not exist. Cannot delete template.", abs_filename);
1592 return LGL_DB_DELETE_DOES_NOT_EXIST;
1595 g_unlink (abs_filename);
1599 g_free (abs_filename);
1601 for ( p=model->templates; p != NULL; p=p->next )
1603 template1 = (lglTemplate *)p->data;
1605 if ( lgl_template_do_templates_match (template, template1) )
1607 model->templates = g_list_delete_link (model->templates, p);
1608 g_hash_table_remove (model->template_cache, name);
1613 lgl_template_free (template);
1615 g_signal_emit (G_OBJECT (model), signals[CHANGED], 0);
1616 return LGL_DB_DELETE_OK;
1620 return LGL_DB_DELETE_NOT_USER_DEFINED;
1627 * lgl_db_delete_template_by_brand_part:
1628 * @brand: Brand name or vendor of template to be deleted.
1629 * @part: Part name or number of template to be deleted.
1631 * Delete a user defined template. This function deletes a template from
1632 * the template database. The individual XML file in the user template
1633 * directory will also be removed.
1635 * Returns: Status of registration attempt (#lglDbDeleteStatus)
1638 lgl_db_delete_template_by_brand_part (const gchar *brand,
1642 lglDbDeleteStatus status;
1644 name = g_strdup_printf ("%s %s", brand, part);
1646 status = lgl_db_delete_template_by_name (name);
1655 * lgl_db_does_template_exist:
1656 * @brand: Brand name.
1657 * @part: Part name/number.
1659 * This function tests whether a template with the given brand and part name/number exists.
1661 * Returns: TRUE if such a template exists in the database.
1664 lgl_db_does_template_exist (const gchar *brand,
1668 lglTemplate *template;
1675 if ((brand == NULL) || (part == NULL))
1680 for (p_tmplt = model->templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1682 template = (lglTemplate *) p_tmplt->data;
1684 if ( UTF8_EQUAL (brand, template->brand) &&
1685 UTF8_EQUAL (part, template->part) )
1696 * lgl_db_does_template_name_exist:
1697 * @name: name string
1699 * This function test whether a template with the given name exists.
1701 * Returns: TRUE if such a template exists in the database.
1705 lgl_db_does_template_name_exist (const gchar *name)
1708 lglTemplate *template;
1709 gchar *candidate_name;
1721 for (p_tmplt = model->templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1723 template = (lglTemplate *) p_tmplt->data;
1724 candidate_name = g_strdup_printf ("%s %s", template->brand, template->part);
1726 if ( UTF8_EQUAL (candidate_name, name) )
1728 g_free (candidate_name);
1731 g_free (candidate_name);
1739 * lgl_db_get_template_name_list_all:
1740 * @brand: If non NULL, limit results to given brand
1741 * @paper_id: If non NULL, limit results to given page size.
1742 * @category_id: If non NULL, limit results to given template category.
1744 * Get a list of all valid names of templates in the template database.
1745 * Results can be filtered by page size and/or template category. A list of valid page
1746 * sizes can be obtained using lgl_db_get_paper_id_list(). A list of valid template
1747 * categories can be obtained using lgl_db_get_category_id_list().
1749 * Returns: a list of template names.
1752 lgl_db_get_template_name_list_all (const gchar *brand,
1753 const gchar *paper_id,
1754 const gchar *category_id)
1757 lglTemplate *template;
1759 GList *names = NULL;
1766 for (p_tmplt = model->templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1768 template = (lglTemplate *) p_tmplt->data;
1769 if (lgl_template_does_brand_match (template, brand) &&
1770 lgl_template_does_page_size_match (template, paper_id) &&
1771 lgl_template_does_category_match (template, category_id))
1773 name = g_strdup_printf ("%s %s", template->brand, template->part);
1774 names = g_list_insert_sorted (names, name, (GCompareFunc)lgl_str_part_name_cmp);
1783 * lgl_db_get_similar_template_name_list:
1784 * @name: Name of template under test.
1786 * Get a list of all valid names of templates in the template database that
1787 * have the same size and layout characteristics as the given template.
1789 * Returns: a list of template names.
1792 lgl_db_get_similar_template_name_list (const gchar *name)
1795 lglTemplate *template1;
1796 lglTemplate *template2;
1798 GList *names = NULL;
1810 template1 = lgl_db_lookup_template_from_name (name);
1816 for (p_tmplt = model->templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1818 template2 = (lglTemplate *) p_tmplt->data;
1820 if ( lgl_template_are_templates_identical (template1, template2) )
1823 name2 = g_strdup_printf ("%s %s", template2->brand, template2->part);
1824 if ( !UTF8_EQUAL (name2, name) )
1826 names = g_list_insert_sorted (names, name2,
1827 (GCompareFunc)lgl_str_part_name_cmp);
1838 * lgl_db_free_template_name_list:
1839 * @names: List of template name strings to be freed.
1841 * Free up all storage associated with a list of template names obtained with
1842 * lgl_db_get_template_name_list_all().
1846 lgl_db_free_template_name_list (GList *names)
1850 for (p_name = names; p_name != NULL; p_name = p_name->next)
1852 g_free (p_name->data);
1853 p_name->data = NULL;
1856 g_list_free (names);
1861 * lgl_db_lookup_template_from_name:
1862 * @name: name string
1864 * Lookup template in template database from name string.
1866 * Returns: pointer to a newly allocated #lglTemplate structure.
1870 lgl_db_lookup_template_from_name (const gchar *name)
1872 lglTemplate *template;
1873 lglTemplate *new_template;
1882 /* If no name, return first template as a default */
1883 return lgl_template_dup ((lglTemplate *) model->templates->data);
1886 template = g_hash_table_lookup (model->template_cache, name);
1890 new_template = lgl_template_dup (template);
1891 return new_template;
1894 /* No matching template has been found so return the first template */
1895 return lgl_template_dup ((lglTemplate *) model->templates->data);
1900 * lgl_db_lookup_template_from_brand_part:
1901 * @brand: brand name string
1902 * @part: part name string
1904 * Lookup template in template database from brand and part strings.
1906 * Returns: pointer to a newly allocated #lglTemplate structure.
1910 lgl_db_lookup_template_from_brand_part(const gchar *brand,
1914 lglTemplate *template;
1915 lglTemplate *new_template;
1922 if ((brand == NULL) || (part == NULL))
1924 /* If no name, return first template as a default */
1925 return lgl_template_dup ((lglTemplate *) model->templates->data);
1928 name = g_strdup_printf ("%s %s", brand, part);
1929 template = g_hash_table_lookup (model->template_cache, name);
1933 new_template = lgl_template_dup (template);
1934 return new_template;
1937 /* No matching template has been found so return the first template */
1939 return lgl_template_dup ((lglTemplate *) model->templates->data);
1944 add_to_template_cache (lglTemplate *template)
1948 name = g_strdup_printf ("%s %s", template->brand, template->part);
1950 g_hash_table_insert (model->template_cache, name, template);
1955 read_templates (void)
1959 lglTemplate *template;
1962 * User defined templates. Add to user-defined category.
1964 data_dir = USER_CONFIG_DIR;
1965 read_template_files_from_dir (data_dir);
1967 for ( p=model->templates; p != NULL; p=p->next )
1969 template = (lglTemplate *)p->data;
1970 lgl_template_add_category (template, "user-defined");
1974 * Alternate user defined templates. (Used for manually created templates).
1976 data_dir = ALT_USER_CONFIG_DIR;
1977 read_template_files_from_dir (data_dir);
1983 data_dir = SYSTEM_CONFIG_DIR;
1984 read_template_files_from_dir (data_dir);
1987 if (model->templates == NULL)
1989 g_critical (_("Unable to locate any template files. Libglabels may not be installed correctly!"));
1995 read_template_files_from_dir (const gchar *dirname)
1998 const gchar *filename, *extension, *extension2;
1999 gchar *full_filename = NULL;
2000 GError *gerror = NULL;
2002 if (dirname == NULL)
2005 if (!g_file_test (dirname, G_FILE_TEST_EXISTS))
2010 dp = g_dir_open (dirname, 0, &gerror);
2013 g_message ("cannot open data directory: %s", gerror->message );
2017 while ((filename = g_dir_read_name (dp)) != NULL)
2020 extension = strrchr (filename, '.');
2021 extension2 = strrchr (filename, '-');
2023 if ( (extension && ASCII_EQUAL (extension, ".template")) ||
2024 (extension2 && ASCII_EQUAL (extension2, "-templates.xml")) )
2027 full_filename = g_build_filename (dirname, filename, NULL);
2028 lgl_xml_template_read_templates_from_file (full_filename);
2029 g_free (full_filename);
2038 static lglTemplate *
2039 template_full_page (const gchar *paper_id)
2041 lglPaper *paper = NULL;
2042 lglTemplate *template = NULL;
2043 lglTemplateFrame *frame = NULL;
2047 g_return_val_if_fail (paper_id, NULL);
2049 paper = lgl_db_lookup_paper_from_id (paper_id);
2050 if ( paper == NULL )
2055 part = g_strdup_printf ("%s-Full-Page", paper->id);
2056 desc = g_strdup_printf (_("%s full page label"), paper->name);
2058 template = lgl_template_new ("Generic", part, desc,
2059 paper_id, paper->width, paper->height);
2062 frame = lgl_template_frame_rect_new ("0",
2068 lgl_template_add_frame (template, frame);
2070 lgl_template_frame_add_layout (frame, lgl_template_layout_new (1, 1, 0., 0., 0., 0.));
2072 lgl_template_frame_add_markup (frame, lgl_template_markup_margin_new (9.0));
2076 lgl_paper_free (paper);
2084 * lgl_db_print_known_templates:
2086 * Print all known templates (for debugging purposes).
2090 lgl_db_print_known_templates (void)
2093 lglTemplate *template;
2100 g_print ("%s():\n", __FUNCTION__);
2101 for (p=model->templates; p!=NULL; p=p->next)
2103 template = (lglTemplate *)p->data;
2105 g_print("TEMPLATE brand=\"%s\", part=\"%s\", description=\"%s\"\n",
2106 template->brand, template->part, template->description);
2117 * Local Variables: -- emacs
2119 * c-basic-offset: 8 -- emacs
2120 * tab-width: 8 -- emacs
2121 * indent-tabs-mode: nil -- emacs