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"
39 /*===========================================*/
40 /* Private macros and constants. */
41 /*===========================================*/
43 /* Data system and user data directories. (must free w/ g_free()) */
44 #define SYSTEM_CONFIG_DIR g_build_filename (LIBGLABELS_CONFIG_DIR, "templates", NULL)
45 #define USER_CONFIG_DIR g_build_filename (g_get_user_config_dir (), "libglabels", "templates" , NULL)
46 #define ALT_USER_CONFIG_DIR g_build_filename (g_get_home_dir (), ".glabels", NULL)
49 /*===========================================*/
51 /*===========================================*/
53 #define TYPE_LGL_DB_MODEL (lgl_db_model_get_type ())
54 #define LGL_DB_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_LGL_DB_MODEL, lglDbModel))
55 #define LGL_DB_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_LGL_DB_MODEL, lglDbModelClass))
56 #define IS_LGL_DB_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_LGL_DB_MODEL))
57 #define IS_LGL_DB_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_LGL_DB_MODEL))
58 #define LGL_DB_MODEL_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS ((object), TYPE_LGL_DB_MODEL, lglDbModelClass))
61 typedef struct _lglDbModel lglDbModel;
62 typedef struct _lglDbModelClass lglDbModelClass;
73 GHashTable *template_cache;
77 struct _lglDbModelClass {
78 GObjectClass parent_class;
83 void (*changed) (lglDbModel *this,
95 /*===========================================*/
97 /*===========================================*/
99 static guint signals[LAST_SIGNAL] = {0};
101 static lglDbModel *model = NULL;
104 /*===========================================*/
105 /* Local function prototypes */
106 /*===========================================*/
108 static void lgl_db_model_finalize (GObject *object);
110 static void add_to_template_cache (lglTemplate *template);
112 static GList *read_papers (void);
113 static GList *read_paper_files_from_dir (GList *papers,
114 const gchar *dirname);
116 static GList *read_categories (void);
117 static GList *read_category_files_from_dir (GList *categories,
118 const gchar *dirname);
120 static GList *read_vendors (void);
121 static GList *read_vendor_files_from_dir (GList *vendors,
122 const gchar *dirname);
124 static void read_templates (void);
125 static void read_template_files_from_dir (const gchar *dirname);
127 static lglTemplate *template_full_page (const gchar *page_size);
130 /*****************************************************************************/
131 /* Object infrastructure. */
132 /*****************************************************************************/
133 G_DEFINE_TYPE (lglDbModel, lgl_db_model, G_TYPE_OBJECT);
136 /*****************************************************************************/
137 /* Class Init Function. */
138 /*****************************************************************************/
140 lgl_db_model_class_init (lglDbModelClass *class)
142 GObjectClass *gobject_class = (GObjectClass *) class;
144 lgl_db_model_parent_class = g_type_class_peek_parent (class);
146 gobject_class->finalize = lgl_db_model_finalize;
149 g_signal_new ("changed",
150 G_OBJECT_CLASS_TYPE (gobject_class),
152 G_STRUCT_OFFSET (lglDbModelClass, changed),
154 g_cclosure_marshal_VOID__VOID,
160 /*****************************************************************************/
161 /* Object Instance Init Function. */
162 /*****************************************************************************/
164 lgl_db_model_init (lglDbModel *this)
166 this->template_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)lgl_template_free);
170 /*****************************************************************************/
171 /* Finalize Method. */
172 /*****************************************************************************/
174 lgl_db_model_finalize (GObject *object)
179 g_return_if_fail (object && IS_LGL_DB_MODEL (object));
180 this = LGL_DB_MODEL (object);
182 g_hash_table_unref (this->template_cache);
184 for (p = this->papers; p != NULL; p = p->next)
189 g_list_free (this->papers);
191 for (p = this->categories; p != NULL; p = p->next)
196 g_list_free (this->categories);
198 for (p = this->vendors; p != NULL; p = p->next)
203 g_list_free (this->vendors);
205 for (p = this->templates; p != NULL; p = p->next)
207 lgl_template_free ((lglTemplate *)p->data);
210 g_list_free (this->templates);
212 G_OBJECT_CLASS (lgl_db_model_parent_class)->finalize (object);
216 /*****************************************************************************/
217 /** New Object Generator. */
218 /*****************************************************************************/
220 lgl_db_model_new (void)
224 this = g_object_new (TYPE_LGL_DB_MODEL, NULL);
230 /*===========================================*/
231 /* Module initialization */
232 /*===========================================*/
237 * Initialize all libglabels subsystems. It is not necessary for an application to call
238 * lgl_db_init(), because libglabels will initialize on demand. An application programmer may
239 * choose to call lgl_db_init() at startup to minimize the impact of the first libglabels call
240 * on GUI response time.
242 * This function initializes its paper definitions, category definitions, vendor definitions,
243 * and its template database. It will search both system and user template directories to locate
249 lglPaper *paper_other;
250 lglCategory *category_user_defined;
251 lglTemplate *template;
255 model = lgl_db_model_new ();
260 model->papers = read_papers ();
262 /* Create and append an "Other" entry. */
263 /* Translators: "Other" here means other page size. Meaning a page size
264 * other than the standard ones that libglabels knows about such as
265 * "letter", "A4", etc. */
266 paper_other = lgl_paper_new ("Other", _("Other"), 0.0, 0.0, NULL);
267 model->papers = g_list_append (model->papers, paper_other);
272 model->categories = read_categories ();
274 /* Create and append a "User defined" entry. */
275 category_user_defined = lgl_category_new ("user-defined", _("User defined"));
276 model->categories = g_list_append (model->categories, category_user_defined);
281 model->vendors = read_vendors ();
288 /* Create and append generic full page templates. */
289 page_sizes = lgl_db_get_paper_id_list ();
290 for ( p=page_sizes; p != NULL; p=p->next )
292 if ( !lgl_db_is_paper_id_other (p->data) )
294 template = template_full_page (p->data);
295 _lgl_db_register_template_internal (template);
296 lgl_template_free (template);
299 lgl_db_free_paper_id_list (page_sizes);
306 * @func: Callback function to be called when database changes.
307 * @user_data: Passback user data to supply to callback function.
309 * Register a notification callback function to be called when the database changes.
311 * Returns: an ID for this notification registration.
314 lgl_db_notify_add (lglDbNotifyFunc func,
322 return g_signal_connect_swapped (G_OBJECT (model), "changed", G_CALLBACK (func), user_data);
327 * lgl_db_notify_remove:
328 * @id: ID of notification registration to cancel (see lgl_db_notify_add()).
330 * Cancel a previous registration a notification callback function.
333 lgl_db_notify_remove (gulong id)
335 g_signal_handler_disconnect (G_OBJECT (model), id);
339 /*===========================================*/
340 /* Paper db functions. */
341 /*===========================================*/
344 * lgl_db_get_paper_id_list:
346 * Get a list of all paper ids known to libglabels.
348 * Returns: a list of paper ids.
352 lgl_db_get_paper_id_list (void)
363 for ( p=model->papers; p != NULL; p=p->next )
365 paper = (lglPaper *)p->data;
366 ids = g_list_append (ids, g_strdup (paper->id));
374 * lgl_db_free_paper_id_list:
375 * @ids: List of id strings to be freed.
377 * Free up all storage associated with an id list obtained with
378 * lgl_db_get_paper_id_list().
382 lgl_db_free_paper_id_list (GList *ids)
386 for (p = ids; p != NULL; p = p->next)
397 * lgl_db_get_paper_name_list:
399 * Get a list of all localized paper names known to libglabels.
401 * Returns: a list of localized paper names.
405 lgl_db_get_paper_name_list (void)
416 for ( p=model->papers; p != NULL; p=p->next )
418 paper = (lglPaper *)p->data;
419 names = g_list_append (names, g_strdup (paper->name));
427 * lgl_db_free_paper_name_list:
428 * @names: List of localized paper name strings to be freed.
430 * Free up all storage associated with a name list obtained with
431 * lgl_db_get_paper_name_list().
435 lgl_db_free_paper_name_list (GList *names)
439 for (p = names; p != NULL; p = p->next)
450 * lgl_db_lookup_paper_from_name:
451 * @name: localized paper name string
453 * Lookup paper definition from localized paper name string.
455 * Returns: pointer to a newly allocated #lglPaper structure.
459 lgl_db_lookup_paper_from_name (const gchar *name)
471 /* If no name, return first paper as a default */
472 return lgl_paper_dup ((lglPaper *) model->papers->data);
475 for (p = model->papers; p != NULL; p = p->next)
477 paper = (lglPaper *) p->data;
478 if (UTF8_EQUAL (paper->name, name))
480 return lgl_paper_dup (paper);
489 * lgl_db_lookup_paper_from_id:
490 * @id: paper id string
492 * Lookup paper definition from id string.
494 * Returns: pointer to a newly allocated #lglPaper structure.
498 lgl_db_lookup_paper_from_id (const gchar *id)
510 /* If no id, return first paper as a default */
511 return lgl_paper_dup ((lglPaper *) model->papers->data);
514 for (p = model->papers; p != NULL; p = p->next)
516 paper = (lglPaper *) p->data;
517 if (ASCII_EQUAL (paper->id, id))
519 return lgl_paper_dup (paper);
528 * lgl_db_lookup_paper_id_from_name:
529 * @name: localized paper name stringp
531 * Lookup paper name string from localized paper name string.
533 * Returns: pointer to a newly allocated id string.
537 lgl_db_lookup_paper_id_from_name (const gchar *name)
539 lglPaper *paper = NULL;
544 paper = lgl_db_lookup_paper_from_name (name);
547 id = g_strdup (paper->id);
548 lgl_paper_free (paper);
558 * lgl_db_lookup_paper_name_from_id:
559 * @id: paper id string
561 * Lookup localized paper name string from paper id string.
563 * Returns: pointer to a newly allocated localized paper name string.
567 lgl_db_lookup_paper_name_from_id (const gchar *id)
569 lglPaper *paper = NULL;
574 paper = lgl_db_lookup_paper_from_id (id);
577 name = g_strdup (paper->name);
578 lgl_paper_free (paper);
588 * lgl_db_is_paper_id_known:
589 * @id: paper id to test
591 * Determine if given paper id is known to libglabels.
593 * Returns: TRUE if id is known, otherwise FALSE.
597 lgl_db_is_paper_id_known (const gchar *id)
612 for (p = model->papers; p != NULL; p = p->next)
614 paper = (lglPaper *) p->data;
615 if (ASCII_EQUAL (paper->id, id))
626 * lgl_db_is_paper_id_other:
627 * @id: paper id to test
629 * Determine if given paper id is the special id "Other."
631 * Returns: TRUE if id is "Other", otherwise FALSE.
635 lgl_db_is_paper_id_other (const gchar *id)
642 return (ASCII_EQUAL (id, "Other"));
650 GList *papers = NULL;
652 data_dir = SYSTEM_CONFIG_DIR;
653 papers = read_paper_files_from_dir (papers, data_dir);
656 data_dir = USER_CONFIG_DIR;
657 papers = read_paper_files_from_dir (papers, data_dir);
660 if (papers == NULL) {
661 g_critical (_("Unable to locate paper size definitions. Libglabels may not be installed correctly!"));
669 read_paper_files_from_dir (GList *papers,
670 const gchar *dirname)
673 const gchar *filename, *extension;
674 gchar *full_filename = NULL;
675 GError *gerror = NULL;
676 GList *new_papers = NULL;
678 if (dirname == NULL) {
682 if (!g_file_test (dirname, G_FILE_TEST_EXISTS)) {
686 dp = g_dir_open (dirname, 0, &gerror);
687 if (gerror != NULL) {
688 g_message ("cannot open data directory: %s", gerror->message );
692 while ((filename = g_dir_read_name (dp)) != NULL) {
694 extension = strrchr (filename, '.');
696 if (extension != NULL) {
698 if ( ASCII_EQUAL (filename, "paper-sizes.xml") )
702 g_build_filename (dirname, filename, NULL);
704 lgl_xml_paper_read_papers_from_file (full_filename);
705 g_free (full_filename);
707 papers = g_list_concat (papers, new_papers);
723 * lgl_db_print_known_papers:
725 * For debugging purposes: print a list of all paper definitions known to
730 lgl_db_print_known_papers (void)
739 g_print ("%s():\n", __FUNCTION__);
740 for (p = model->papers; p != NULL; p = p->next) {
741 paper = (lglPaper *) p->data;
743 g_print ("PAPER id=\"%s\", name=\"%s\", width=%gpts, height=%gpts\n",
744 paper->id, paper->name, paper->width, paper->height);
752 /*===========================================*/
753 /* Category db functions. */
754 /*===========================================*/
757 * lgl_db_get_category_id_list:
759 * Get a list of all category ids known to libglabels.
761 * Returns: a list of category ids.
765 lgl_db_get_category_id_list (void)
769 lglCategory *category;
776 for ( p=model->categories; p != NULL; p=p->next )
778 category = (lglCategory *)p->data;
779 ids = g_list_append (ids, g_strdup (category->id));
787 * lgl_db_free_category_id_list:
788 * @ids: List of id strings to be freed.
790 * Free up all storage associated with an id list obtained with
791 * lgl_db_get_category_id_list().
795 lgl_db_free_category_id_list (GList *ids)
799 for (p = ids; p != NULL; p = p->next)
810 * lgl_db_get_category_name_list:
812 * Get a list of all localized category names known to libglabels.
814 * Returns: a list of localized category names.
818 lgl_db_get_category_name_list (void)
822 lglCategory *category;
829 for ( p=model->categories; p != NULL; p=p->next )
831 category = (lglCategory *)p->data;
832 names = g_list_append (names, g_strdup (category->name));
840 * lgl_db_free_category_name_list:
841 * @names: List of localized category name strings to be freed.
843 * Free up all storage associated with a name list obtained with
844 * lgl_db_get_category_name_list().
848 lgl_db_free_category_name_list (GList *names)
852 for (p = names; p != NULL; p = p->next)
863 * lgl_db_lookup_category_from_name:
864 * @name: localized category name string
866 * Lookup category definition from localized category name string.
868 * Returns: pointer to a newly allocated #lglCategory structure.
872 lgl_db_lookup_category_from_name (const gchar *name)
875 lglCategory *category;
884 /* If no name, return first category as a default */
885 return lgl_category_dup ((lglCategory *) model->categories->data);
888 for (p = model->categories; p != NULL; p = p->next)
890 category = (lglCategory *) p->data;
891 if (UTF8_EQUAL (category->name, name))
893 return lgl_category_dup (category);
902 * lgl_db_lookup_category_from_id:
903 * @id: category id string
905 * Lookup category definition from id string.
907 * Returns: pointer to a newly allocated #lglCategory structure.
911 lgl_db_lookup_category_from_id (const gchar *id)
914 lglCategory *category;
923 /* If no id, return first category as a default */
924 return lgl_category_dup ((lglCategory *) model->categories->data);
927 for (p = model->categories; p != NULL; p = p->next)
929 category = (lglCategory *) p->data;
930 if (ASCII_EQUAL (category->id, id))
932 return lgl_category_dup (category);
941 * lgl_db_lookup_category_id_from_name:
942 * @name: localized category name stringp
944 * Lookup category name string from localized category name string.
946 * Returns: pointer to a newly allocated id string.
950 lgl_db_lookup_category_id_from_name (const gchar *name)
952 lglCategory *category = NULL;
957 category = lgl_db_lookup_category_from_name (name);
958 if ( category != NULL )
960 id = g_strdup (category->id);
961 lgl_category_free (category);
971 * lgl_db_lookup_category_name_from_id:
972 * @id: category id string
974 * Lookup localized category name string from category id string.
976 * Returns: pointer to a newly allocated localized category name string.
980 lgl_db_lookup_category_name_from_id (const gchar *id)
982 lglCategory *category = NULL;
987 category = lgl_db_lookup_category_from_id (id);
988 if ( category != NULL )
990 name = g_strdup (category->name);
991 lgl_category_free (category);
1001 * lgl_db_is_category_id_known:
1002 * @id: category id to test
1004 * Determine if given category id is known to libglabels.
1006 * Returns: TRUE if id is known, otherwise FALSE.
1010 lgl_db_is_category_id_known (const gchar *id)
1013 lglCategory *category;
1025 for (p = model->categories; p != NULL; p = p->next)
1027 category = (lglCategory *) p->data;
1028 if (ASCII_EQUAL (category->id, id))
1039 read_categories (void)
1042 GList *categories = NULL;
1044 data_dir = SYSTEM_CONFIG_DIR;
1045 categories = read_category_files_from_dir (categories, data_dir);
1048 data_dir = USER_CONFIG_DIR;
1049 categories = read_category_files_from_dir (categories, data_dir);
1052 if (categories == NULL) {
1053 g_critical (_("Unable to locate category definitions. Libglabels may not be installed correctly!"));
1061 read_category_files_from_dir (GList *categories,
1062 const gchar *dirname)
1065 const gchar *filename, *extension;
1066 gchar *full_filename = NULL;
1067 GError *gerror = NULL;
1068 GList *new_categories = NULL;
1070 if (dirname == NULL) {
1074 if (!g_file_test (dirname, G_FILE_TEST_EXISTS)) {
1078 dp = g_dir_open (dirname, 0, &gerror);
1079 if (gerror != NULL) {
1080 g_message ("cannot open data directory: %s", gerror->message );
1084 while ((filename = g_dir_read_name (dp)) != NULL) {
1086 extension = strrchr (filename, '.');
1088 if (extension != NULL) {
1090 if ( ASCII_EQUAL (filename, "categories.xml") )
1094 g_build_filename (dirname, filename, NULL);
1096 lgl_xml_category_read_categories_from_file (full_filename);
1097 g_free (full_filename);
1099 categories = g_list_concat (categories, new_categories);
1100 new_categories = NULL;
1115 * lgl_db_print_known_categories:
1117 * For debugging purposes: print a list of all category definitions known to
1122 lgl_db_print_known_categories (void)
1125 lglCategory *category;
1131 g_print ("%s():\n", __FUNCTION__);
1132 for (p = model->categories; p != NULL; p = p->next) {
1133 category = (lglCategory *) p->data;
1135 g_print ("CATEGORY id=\"%s\", name=\"%s\"\n", category->id, category->name);
1143 /*===========================================*/
1144 /* Vendor db functions. */
1145 /*===========================================*/
1148 * lgl_db_get_vendor_name_list:
1150 * Get a list of all localized vendor names known to libglabels.
1152 * Returns: a list of localized vendor names.
1156 lgl_db_get_vendor_name_list (void)
1158 GList *names = NULL;
1167 for ( p=model->vendors; p != NULL; p=p->next )
1169 vendor = (lglVendor *)p->data;
1170 names = g_list_append (names, g_strdup (vendor->name));
1178 * lgl_db_free_vendor_name_list:
1179 * @names: List of localized vendor name strings to be freed.
1181 * Free up all storage associated with a name list obtained with
1182 * lgl_db_get_vendor_name_list().
1186 lgl_db_free_vendor_name_list (GList *names)
1190 for (p = names; p != NULL; p = p->next)
1196 g_list_free (names);
1201 * lgl_db_lookup_vendor_from_name:
1202 * @name: localized vendor name string
1204 * Lookup vendor definition from localized vendor name string.
1206 * Returns: pointer to a newly allocated #lglVendor structure.
1210 lgl_db_lookup_vendor_from_name (const gchar *name)
1222 /* If no name, return first vendor as a default */
1223 return lgl_vendor_dup ((lglVendor *) model->vendors->data);
1226 for (p = model->vendors; p != NULL; p = p->next)
1228 vendor = (lglVendor *) p->data;
1229 if (UTF8_EQUAL (vendor->name, name))
1231 return lgl_vendor_dup (vendor);
1240 * lgl_db_is_vendor_name_known:
1241 * @name: vendor name to test
1243 * Determine if given vendor id is known to libglabels.
1245 * Returns: TRUE if id is known, otherwise FALSE.
1249 lgl_db_is_vendor_name_known (const gchar *name)
1264 for (p = model->vendors; p != NULL; p = p->next)
1266 vendor = (lglVendor *) p->data;
1267 if (UTF8_EQUAL (vendor->name, name))
1281 GList *vendors = NULL;
1283 data_dir = SYSTEM_CONFIG_DIR;
1284 vendors = read_vendor_files_from_dir (vendors, data_dir);
1287 data_dir = USER_CONFIG_DIR;
1288 vendors = read_vendor_files_from_dir (vendors, data_dir);
1296 read_vendor_files_from_dir (GList *vendors,
1297 const gchar *dirname)
1300 const gchar *filename, *extension;
1301 gchar *full_filename = NULL;
1302 GError *gerror = NULL;
1303 GList *new_vendors = NULL;
1305 if (dirname == NULL) {
1309 if (!g_file_test (dirname, G_FILE_TEST_EXISTS)) {
1313 dp = g_dir_open (dirname, 0, &gerror);
1314 if (gerror != NULL) {
1315 g_message ("cannot open data directory: %s", gerror->message );
1319 while ((filename = g_dir_read_name (dp)) != NULL) {
1321 extension = strrchr (filename, '.');
1323 if (extension != NULL) {
1325 if ( ASCII_EQUAL (filename, "vendors.xml") )
1329 g_build_filename (dirname, filename, NULL);
1331 lgl_xml_vendor_read_vendors_from_file (full_filename);
1332 g_free (full_filename);
1334 vendors = g_list_concat (vendors, new_vendors);
1350 * lgl_db_print_known_vendors:
1352 * For debugging purposes: print a list of all vendor definitions known to
1357 lgl_db_print_known_vendors (void)
1366 g_print ("%s():\n", __FUNCTION__);
1367 for (p = model->vendors; p != NULL; p = p->next) {
1368 vendor = (lglVendor *) p->data;
1370 g_print ("VENDOR name=\"%s\", url=\"%s\"\n",
1371 vendor->name, vendor->url);
1379 /*===========================================*/
1380 /* Brand db functions. */
1381 /*===========================================*/
1384 * lgl_db_get_brand_list:
1385 * @paper_id: If non NULL, limit results to given page size.
1386 * @category_id: If non NULL, limit results to given template category.
1388 * Get a list of all valid brands of templates in the template database.
1389 * Results can be filtered by page size and/or template category. A list of valid page
1390 * sizes can be obtained using lgl_db_get_paper_id_list(). A list of valid template
1391 * categories can be obtained using lgl_db_get_category_id_list().
1393 * Returns: a list of brands
1396 lgl_db_get_brand_list (const gchar *paper_id,
1397 const gchar *category_id)
1400 lglTemplate *template;
1401 GList *brands = NULL;
1408 for (p_tmplt = model->templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1410 template = (lglTemplate *) p_tmplt->data;
1411 if (lgl_template_does_page_size_match (template, paper_id) &&
1412 lgl_template_does_category_match (template, category_id))
1415 if ( !g_list_find_custom (brands, template->brand,
1416 (GCompareFunc)lgl_str_utf8_casecmp) )
1418 brands = g_list_insert_sorted (brands,
1419 g_strdup (template->brand),
1420 (GCompareFunc)lgl_str_utf8_casecmp);
1430 * lgl_db_free_brand_list:
1431 * @brands: List of template brand strings to be freed.
1433 * Free up all storage associated with a list of template names obtained with
1434 * lgl_db_get_brand_list().
1438 lgl_db_free_brand_list (GList *brands)
1442 for (p_brand = brands; p_brand != NULL; p_brand = p_brand->next)
1444 g_free (p_brand->data);
1445 p_brand->data = NULL;
1448 g_list_free (brands);
1452 /*===========================================*/
1453 /* Template db functions. */
1454 /*===========================================*/
1457 _lgl_db_register_template_internal (const lglTemplate *template)
1459 lglTemplate *template_copy;
1461 if (!lgl_db_does_template_exist (template->brand, template->part))
1463 template_copy = lgl_template_dup (template);
1464 model->templates = g_list_append (model->templates, template_copy);
1465 add_to_template_cache (template_copy);
1469 g_message ("Duplicate template: %s %s.", template->brand, template->part);
1475 * lgl_db_register_template:
1476 * @template: Pointer to a template structure to add to database.
1478 * Register a template. This function adds a template to the template database.
1479 * The template will be stored in an individual XML file in the user template directory.
1481 * Returns: Status of registration attempt (#lglDbRegStatus)
1484 lgl_db_register_template (const lglTemplate *template)
1486 lglTemplate *template_copy;
1487 gchar *dir, *filename, *abs_filename;
1495 if (lgl_db_does_template_exist (template->brand, template->part))
1497 return LGL_DB_REG_BRAND_PART_EXISTS;
1500 if (lgl_db_is_paper_id_known (template->paper_id))
1502 dir = USER_CONFIG_DIR;
1503 g_mkdir_with_parents (dir, 0775); /* Try to make sure directory exists. */
1504 filename = g_strdup_printf ("%s_%s.template", template->brand, template->part);
1505 abs_filename = g_build_filename (dir, filename, NULL);
1506 bytes_written = lgl_xml_template_write_template_to_file (template, abs_filename);
1509 g_free (abs_filename);
1511 if (bytes_written > 0)
1513 template_copy = lgl_template_dup (template);
1514 lgl_template_add_category (template_copy, "user-defined");
1515 model->templates = g_list_append (model->templates, template_copy);
1516 add_to_template_cache (template_copy);
1517 g_signal_emit (G_OBJECT (model), signals[CHANGED], 0);
1518 return LGL_DB_REG_OK;
1522 return LGL_DB_REG_FILE_WRITE_ERROR;
1527 g_message ("Cannot register new template with unknown page size.");
1528 return LGL_DB_REG_BAD_PAPER_ID;
1535 * lgl_db_delete_template_by_name:
1536 * @name: Name of template to be deleted.
1538 * Delete a user defined template. This function deletes a template from
1539 * the template database. The individual XML file in the user template
1540 * directory will also be removed.
1542 * Returns: Status of registration attempt (#lglDbDeleteStatus)
1545 lgl_db_delete_template_by_name (const gchar *name)
1547 lglTemplate *template, *template1;
1548 gchar *dir, *filename, *abs_filename;
1556 if (!lgl_db_does_template_name_exist (name))
1558 return LGL_DB_DELETE_DOES_NOT_EXIST;
1561 template = lgl_db_lookup_template_from_name (name);
1562 if ( lgl_template_does_category_match (template, "user-defined") )
1564 dir = USER_CONFIG_DIR;
1565 filename = g_strdup_printf ("%s_%s.template", template->brand, template->part);
1566 abs_filename = g_build_filename (dir, filename, NULL);
1568 if (!g_file_test (abs_filename, G_FILE_TEST_EXISTS))
1570 g_message ("File \"%s\" does not exist. Cannot delete it.");
1571 return LGL_DB_DELETE_DOES_NOT_EXIST;
1574 g_unlink (abs_filename);
1578 g_free (abs_filename);
1580 for ( p=model->templates; p != NULL; p=p->next )
1582 template1 = (lglTemplate *)p->data;
1584 if ( lgl_template_do_templates_match (template, template1) )
1586 model->templates = g_list_delete_link (model->templates, p);
1587 g_hash_table_remove (model->template_cache, name);
1592 lgl_template_free (template);
1594 g_signal_emit (G_OBJECT (model), signals[CHANGED], 0);
1595 return LGL_DB_DELETE_OK;
1599 return LGL_DB_DELETE_NOT_USER_DEFINED;
1606 * lgl_db_delete_template_by_brand_part:
1607 * @brand: Brand name or vendor of template to be deleted.
1608 * @part: Part name or number of template to be deleted.
1610 * Delete a user defined template. This function deletes a template from
1611 * the template database. The individual XML file in the user template
1612 * directory will also be removed.
1614 * Returns: Status of registration attempt (#lglDbDeleteStatus)
1617 lgl_db_delete_template_by_brand_part (const gchar *brand,
1621 lglDbDeleteStatus status;
1623 name = g_strdup_printf ("%s %s", brand, part);
1625 status = lgl_db_delete_template_by_name (name);
1634 * lgl_db_does_template_exist:
1635 * @brand: Brand name.
1636 * @part: Part name/number.
1638 * This function tests whether a template with the given brand and part name/number exists.
1640 * Returns: TRUE if such a template exists in the database.
1643 lgl_db_does_template_exist (const gchar *brand,
1647 lglTemplate *template;
1654 if ((brand == NULL) || (part == NULL))
1659 for (p_tmplt = model->templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1661 template = (lglTemplate *) p_tmplt->data;
1663 if ( UTF8_EQUAL (brand, template->brand) &&
1664 UTF8_EQUAL (part, template->part) )
1675 * lgl_db_does_template_name_exist:
1676 * @name: name string
1678 * This function test whether a template with the given name exists.
1680 * Returns: TRUE if such a template exists in the database.
1684 lgl_db_does_template_name_exist (const gchar *name)
1687 lglTemplate *template;
1688 gchar *candidate_name;
1700 for (p_tmplt = model->templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1702 template = (lglTemplate *) p_tmplt->data;
1703 candidate_name = g_strdup_printf ("%s %s", template->brand, template->part);
1705 if ( UTF8_EQUAL (candidate_name, name) )
1707 g_free (candidate_name);
1710 g_free (candidate_name);
1718 * lgl_db_get_template_name_list_all:
1719 * @brand: If non NULL, limit results to given brand
1720 * @paper_id: If non NULL, limit results to given page size.
1721 * @category_id: If non NULL, limit results to given template category.
1723 * Get a list of all valid names of templates in the template database.
1724 * Results can be filtered by page size and/or template category. A list of valid page
1725 * sizes can be obtained using lgl_db_get_paper_id_list(). A list of valid template
1726 * categories can be obtained using lgl_db_get_category_id_list().
1728 * Returns: a list of template names.
1731 lgl_db_get_template_name_list_all (const gchar *brand,
1732 const gchar *paper_id,
1733 const gchar *category_id)
1736 lglTemplate *template;
1738 GList *names = NULL;
1745 for (p_tmplt = model->templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1747 template = (lglTemplate *) p_tmplt->data;
1748 if (lgl_template_does_brand_match (template, brand) &&
1749 lgl_template_does_page_size_match (template, paper_id) &&
1750 lgl_template_does_category_match (template, category_id))
1752 name = g_strdup_printf ("%s %s", template->brand, template->part);
1753 names = g_list_insert_sorted (names, name, (GCompareFunc)lgl_str_part_name_cmp);
1762 * lgl_db_get_similar_template_name_list:
1763 * @name: Name of template under test.
1765 * Get a list of all valid names of templates in the template database that
1766 * have the same size and layout characteristics as the given template.
1768 * Returns: a list of template names.
1771 lgl_db_get_similar_template_name_list (const gchar *name)
1774 lglTemplate *template1;
1775 lglTemplate *template2;
1777 GList *names = NULL;
1789 template1 = lgl_db_lookup_template_from_name (name);
1795 for (p_tmplt = model->templates; p_tmplt != NULL; p_tmplt = p_tmplt->next)
1797 template2 = (lglTemplate *) p_tmplt->data;
1799 if ( lgl_template_are_templates_identical (template1, template2) )
1802 name2 = g_strdup_printf ("%s %s", template2->brand, template2->part);
1803 if ( !UTF8_EQUAL (name2, name) )
1805 names = g_list_insert_sorted (names, name2,
1806 (GCompareFunc)lgl_str_part_name_cmp);
1817 * lgl_db_free_template_name_list:
1818 * @names: List of template name strings to be freed.
1820 * Free up all storage associated with a list of template names obtained with
1821 * lgl_db_get_template_name_list_all() or lgl_db_get_template_name_list_unique().
1825 lgl_db_free_template_name_list (GList *names)
1829 for (p_name = names; p_name != NULL; p_name = p_name->next)
1831 g_free (p_name->data);
1832 p_name->data = NULL;
1835 g_list_free (names);
1840 * lgl_db_lookup_template_from_name:
1841 * @name: name string
1843 * Lookup template in template database from name string.
1845 * Returns: pointer to a newly allocated #lglTemplate structure.
1849 lgl_db_lookup_template_from_name (const gchar *name)
1851 lglTemplate *template;
1852 lglTemplate *new_template;
1861 /* If no name, return first template as a default */
1862 return lgl_template_dup ((lglTemplate *) model->templates->data);
1865 template = g_hash_table_lookup (model->template_cache, name);
1869 new_template = lgl_template_dup (template);
1870 return new_template;
1873 /* No matching template has been found so return the first template */
1874 return lgl_template_dup ((lglTemplate *) model->templates->data);
1879 * lgl_db_lookup_template_from_brand_part:
1880 * @brand: brand name string
1881 * @part: part name string
1883 * Lookup template in template database from brand and part strings.
1885 * Returns: pointer to a newly allocated #lglTemplate structure.
1889 lgl_db_lookup_template_from_brand_part(const gchar *brand,
1893 lglTemplate *template;
1894 lglTemplate *new_template;
1901 if ((brand == NULL) || (part == NULL))
1903 /* If no name, return first template as a default */
1904 return lgl_template_dup ((lglTemplate *) model->templates->data);
1907 name = g_strdup_printf ("%s %s", brand, part);
1908 template = g_hash_table_lookup (model->template_cache, name);
1912 new_template = lgl_template_dup (template);
1913 return new_template;
1916 /* No matching template has been found so return the first template */
1918 return lgl_template_dup ((lglTemplate *) model->templates->data);
1923 add_to_template_cache (lglTemplate *template)
1927 name = g_strdup_printf ("%s %s", template->brand, template->part);
1929 g_hash_table_insert (model->template_cache, name, template);
1934 read_templates (void)
1938 lglTemplate *template;
1941 * User defined templates. Add to user-defined category.
1943 data_dir = USER_CONFIG_DIR;
1944 read_template_files_from_dir (data_dir);
1946 for ( p=model->templates; p != NULL; p=p->next )
1948 template = (lglTemplate *)p->data;
1949 lgl_template_add_category (template, "user-defined");
1953 * Alternate user defined templates. (Used for manually created templates).
1955 data_dir = ALT_USER_CONFIG_DIR;
1956 read_template_files_from_dir (data_dir);
1962 data_dir = SYSTEM_CONFIG_DIR;
1963 read_template_files_from_dir (data_dir);
1966 if (model->templates == NULL)
1968 g_critical (_("Unable to locate any template files. Libglabels may not be installed correctly!"));
1974 read_template_files_from_dir (const gchar *dirname)
1977 const gchar *filename, *extension, *extension2;
1978 gchar *full_filename = NULL;
1979 GError *gerror = NULL;
1981 if (dirname == NULL)
1984 if (!g_file_test (dirname, G_FILE_TEST_EXISTS))
1989 dp = g_dir_open (dirname, 0, &gerror);
1992 g_message ("cannot open data directory: %s", gerror->message );
1996 while ((filename = g_dir_read_name (dp)) != NULL)
1999 extension = strrchr (filename, '.');
2000 extension2 = strrchr (filename, '-');
2002 if ( (extension && ASCII_EQUAL (extension, ".template")) ||
2003 (extension2 && ASCII_EQUAL (extension2, "-templates.xml")) )
2006 full_filename = g_build_filename (dirname, filename, NULL);
2007 lgl_xml_template_read_templates_from_file (full_filename);
2008 g_free (full_filename);
2017 static lglTemplate *
2018 template_full_page (const gchar *paper_id)
2020 lglPaper *paper = NULL;
2021 lglTemplate *template = NULL;
2022 lglTemplateFrame *frame = NULL;
2026 g_return_val_if_fail (paper_id, NULL);
2028 paper = lgl_db_lookup_paper_from_id (paper_id);
2029 if ( paper == NULL )
2034 part = g_strdup_printf ("%s-Full-Page", paper->id);
2035 desc = g_strdup_printf (_("%s full page label"), paper->name);
2037 template = lgl_template_new ("Generic", part, desc,
2038 paper_id, paper->width, paper->height);
2041 frame = lgl_template_frame_rect_new ("0",
2047 lgl_template_add_frame (template, frame);
2049 lgl_template_frame_add_layout (frame, lgl_template_layout_new (1, 1, 0., 0., 0., 0.));
2051 lgl_template_frame_add_markup (frame, lgl_template_markup_margin_new (9.0));
2055 lgl_paper_free (paper);
2063 * lgl_db_print_known_templates:
2065 * Print all known templates (for debugging purposes).
2069 lgl_db_print_known_templates (void)
2072 lglTemplate *template;
2079 g_print ("%s():\n", __FUNCTION__);
2080 for (p=model->templates; p!=NULL; p=p->next)
2082 template = (lglTemplate *)p->data;
2084 g_print("TEMPLATE brand=\"%s\", part=\"%s\", description=\"%s\"\n",
2085 template->brand, template->part, template->description);
2096 * Local Variables: -- emacs
2098 * c-basic-offset: 8 -- emacs
2099 * tab-width: 8 -- emacs
2100 * indent-tabs-mode: nil -- emacs