2 * (LIBGLABELS) Template library for GLABELS
4 * paper.c: paper module
6 * Copyright (C) 2003, 2004 Jim Evins <evins@snaught.com>.
8 * This file is part of the LIBGLABELS library.
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the Free
22 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29 #include <glib/gi18n.h>
30 #include <glib/gmem.h>
31 #include <glib/gstrfuncs.h>
32 #include <glib/gdir.h>
33 #include <glib/gmessages.h>
36 #include "libglabels-private.h"
38 #include "xml-paper.h"
40 /*===========================================*/
42 /*===========================================*/
44 /*===========================================*/
46 /*===========================================*/
48 static GList *papers = NULL;
50 /*===========================================*/
51 /* Local function prototypes */
52 /*===========================================*/
54 static GList *read_papers (void);
55 static GList *read_paper_files_from_dir (GList *papers,
56 const gchar *dirname);
61 * Initialize libglabels paper module by reading all paper definition
62 * files located in system and user template directories.
64 * The end user would typically call lgl_init() instead.
72 return; /* Already initialized. */
75 papers = read_papers ();
77 /* Create and append an "Other" entry. */
78 other = lgl_paper_new ("Other", _("Other"), 0.0, 0.0);
79 papers = g_list_append (papers, other);
85 * @id: Id of paper definition. (E.g. US-Letter, A4, etc.) Should be
87 * @name: Localized name of paper.
88 * @width: Width of paper in points.
89 * @height: Height of paper in points.
91 * Allocates and constructs a new #lglPaper structure.
93 * Returns: a pointer to a newly allocated #lglPaper structure.
97 lgl_paper_new (gchar *id,
104 paper = g_new0 (lglPaper,1);
105 paper->id = g_strdup (id);
106 paper->name = g_strdup (name);
107 paper->width = width;
108 paper->height = height;
116 * @orig: #lglPaper structure to be duplicated.
118 * Duplicates an existing #lglPaper structure.
120 * Returns: a pointer to a newly allocated #lglPaper structure.
123 lglPaper *lgl_paper_dup (const lglPaper *orig)
127 g_return_val_if_fail (orig, NULL);
129 paper = g_new0 (lglPaper,1);
131 paper->id = g_strdup (orig->id);
132 paper->name = g_strdup (orig->name);
133 paper->width = orig->width;
134 paper->height = orig->height;
142 * @paper: pointer to #lglPaper structure to be freed.
144 * Free all memory associated with an existing #lglPaper structure.
147 void lgl_paper_free (lglPaper *paper)
150 if ( paper != NULL ) {
155 g_free (paper->name);
165 * lgl_paper_get_id_list:
167 * Get a list of all paper ids known to libglabels.
169 * Returns: a list of paper ids.
173 lgl_paper_get_id_list (void)
183 for ( p=papers; p != NULL; p=p->next ) {
184 paper = (lglPaper *)p->data;
185 ids = g_list_append (ids, g_strdup (paper->id));
192 * lgl_paper_free_id_list:
193 * @ids: List of id strings to be freed.
195 * Free up all storage associated with an id list obtained with
196 * lgl_paper_get_id_list().
200 lgl_paper_free_id_list (GList *ids)
204 for (p = ids; p != NULL; p = p->next) {
214 * lgl_paper_get_name_list:
216 * Get a list of all localized paper names known to libglabels.
218 * Returns: a list of localized paper names.
222 lgl_paper_get_name_list (void)
232 for ( p=papers; p != NULL; p=p->next ) {
233 paper = (lglPaper *)p->data;
234 names = g_list_append (names, g_strdup (paper->name));
242 * lgl_paper_free_name_list:
243 * @names: List of localized paper name strings to be freed.
245 * Free up all storage associated with a name list obtained with
246 * lgl_paper_get_name_list().
250 lgl_paper_free_name_list (GList *names)
254 for (p = names; p != NULL; p = p->next) {
264 * lgl_paper_is_id_known:
265 * @id: paper id to test
267 * Determine if given paper id is known to libglabels.
269 * Returns: TRUE if id is known, otherwise FALSE.
273 lgl_paper_is_id_known (const gchar *id)
286 for (p = papers; p != NULL; p = p->next) {
287 paper = (lglPaper *) p->data;
288 if (g_strcasecmp (paper->id, id) == 0) {
298 * lgl_paper_is_id_other:
299 * @id: paper id to test
301 * Determine if given paper id is the special id "Other."
303 * Returns: TRUE if id is "Other", otherwise FALSE.
307 lgl_paper_is_id_other (const gchar *id)
313 return (g_strcasecmp (id, "Other") == 0);
319 * @id: paper id string
321 * Lookup paper definition from id string.
323 * Returns: pointer to a newly allocated #lglPaper structure.
327 lgl_paper_from_id (const gchar *id)
337 /* If no id, return first paper as a default */
338 return lgl_paper_dup ((lglPaper *) papers->data);
341 for (p = papers; p != NULL; p = p->next) {
342 paper = (lglPaper *) p->data;
343 if (g_strcasecmp (paper->id, id) == 0) {
344 return lgl_paper_dup (paper);
353 * lgl_paper_from_name:
354 * @name: localized paper name string
356 * Lookup paper definition from localized paper name string.
358 * Returns: pointer to a newly allocated #lglPaper structure.
362 lgl_paper_from_name (const gchar *name)
372 /* If no name, return first paper as a default */
373 return lgl_paper_dup ((lglPaper *) papers->data);
376 for (p = papers; p != NULL; p = p->next) {
377 paper = (lglPaper *) p->data;
378 if (g_strcasecmp (paper->name, name) == 0) {
379 return lgl_paper_dup (paper);
388 * lgl_paper_lookup_id_from_name:
389 * @name: localized paper name stringp
391 * Lookup paper name string from localized paper name string.
393 * Returns: pointer to a newly allocated id string.
397 lgl_paper_lookup_id_from_name (const gchar *name)
399 lglPaper *paper = NULL;
404 paper = lgl_paper_from_name (name);
407 id = g_strdup (paper->id);
408 lgl_paper_free (paper);
417 * lgl_paper_lookup_name_from_id:
418 * @id: paper id string
420 * Lookup localized paper name string from paper id string.
422 * Returns: pointer to a newly allocated localized paper name string.
426 lgl_paper_lookup_name_from_id (const gchar *id)
428 lglPaper *paper = NULL;
433 paper = lgl_paper_from_id (id);
436 name = g_strdup (paper->name);
437 lgl_paper_free (paper);
445 /*--------------------------------------------------------------------------*/
446 /* PRIVATE. Read papers from various files. */
447 /*--------------------------------------------------------------------------*/
452 GList *papers = NULL;
454 data_dir = LGL_SYSTEM_DATA_DIR;
455 papers = read_paper_files_from_dir (papers, data_dir);
458 data_dir = LGL_USER_DATA_DIR;
459 papers = read_paper_files_from_dir (papers, data_dir);
462 if (papers == NULL) {
463 g_critical (_("Unable to locate paper size definitions. Libglabels may not be installed correctly!"));
469 /*--------------------------------------------------------------------------*/
470 /* PRIVATE. Read all paper files from given directory. Append to list. */
471 /*--------------------------------------------------------------------------*/
473 read_paper_files_from_dir (GList *papers,
474 const gchar *dirname)
477 const gchar *filename, *extension;
478 gchar *full_filename = NULL;
479 GError *gerror = NULL;
480 GList *new_papers = NULL;
482 if (dirname == NULL) {
486 if (!g_file_test (dirname, G_FILE_TEST_EXISTS)) {
490 dp = g_dir_open (dirname, 0, &gerror);
491 if (gerror != NULL) {
492 g_message ("cannot open data directory: %s", gerror->message );
496 while ((filename = g_dir_read_name (dp)) != NULL) {
498 extension = strrchr (filename, '.');
500 if (extension != NULL) {
502 if ( (g_strcasecmp (extension, ".paper") == 0)
503 || (g_strcasecmp (filename, "paper-sizes.xml") == 0) ) {
506 g_build_filename (dirname, filename, NULL);
508 lgl_xml_paper_read_papers_from_file (full_filename);
509 g_free (full_filename);
511 papers = g_list_concat (papers, new_papers);
527 * lgl_paper_print_known_papers:
529 * For debugging purposes: print a list of all paper definitions known to
534 lgl_paper_print_known_papers (void)
543 g_print ("%s():\n", __FUNCTION__);
544 for (p = papers; p != NULL; p = p->next) {
545 paper = (lglPaper *) p->data;
547 g_print ("PAPER id=\"%s\", name=\"%s\", width=%gpts, height=%gpts\n",
548 paper->id, paper->name, paper->width, paper->height);