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.
70 return; /* Already initialized. */
73 papers = read_papers ();
75 /* Create and append an "Other" entry. */
76 other = gl_paper_new ("Other", _("Other"), 0.0, 0.0);
77 papers = g_list_append (papers, other);
83 * @id: Id of paper definition. (E.g. US-Letter, A4, etc.) Should be
85 * @name: Localized name of paper.
86 * @width: Width of paper in points.
87 * @height: Height of paper in points.
89 * Allocates and constructs a new #glPaper structure.
91 * Returns: a pointer to a newly allocated #glPaper structure.
95 gl_paper_new (gchar *id,
102 paper = g_new0 (glPaper,1);
103 paper->id = g_strdup (id);
104 paper->name = g_strdup (name);
105 paper->width = width;
106 paper->height = height;
114 * @orig: #glPaper structure to be duplicated.
116 * Duplicates an existing #glPaper structure.
118 * Returns: a pointer to a newly allocated #glPaper structure.
121 glPaper *gl_paper_dup (const glPaper *orig)
125 g_return_val_if_fail (orig, NULL);
127 paper = g_new0 (glPaper,1);
129 paper->id = g_strdup (orig->id);
130 paper->name = g_strdup (orig->name);
131 paper->width = orig->width;
132 paper->height = orig->height;
140 * @paper: pointer to #glPaper structure to be freed.
142 * Free all memory associated with an existing #glPaper structure.
145 void gl_paper_free (glPaper *paper)
148 if ( paper != NULL ) {
153 g_free (paper->name);
163 * gl_paper_get_id_list:
165 * Get a list of all paper ids known to libglabels.
167 * Returns: a list of paper ids.
171 gl_paper_get_id_list (void)
181 for ( p=papers; p != NULL; p=p->next ) {
182 paper = (glPaper *)p->data;
183 ids = g_list_append (ids, g_strdup (paper->id));
190 * gl_paper_free_id_list:
191 * @ids: List of id strings to be freed.
193 * Free up all storage associated with an id list obtained with
194 * gl_paper_get_id_list().
198 gl_paper_free_id_list (GList *ids)
202 for (p = ids; p != NULL; p = p->next) {
212 * gl_paper_get_name_list:
214 * Get a list of all localized paper names known to libglabels.
216 * Returns: a list of localized paper names.
220 gl_paper_get_name_list (void)
230 for ( p=papers; p != NULL; p=p->next ) {
231 paper = (glPaper *)p->data;
232 names = g_list_append (names, g_strdup (paper->name));
240 * gl_paper_free_name_list:
241 * @names: List of localized paper name strings to be freed.
243 * Free up all storage associated with a name list obtained with
244 * gl_paper_get_name_list().
248 gl_paper_free_name_list (GList *names)
252 for (p = names; p != NULL; p = p->next) {
262 * gl_paper_is_id_known:
263 * @id: paper id to test
265 * Determine if given paper id is known to libglabels.
267 * Returns: TRUE if id is known, otherwise FALSE.
271 gl_paper_is_id_known (const gchar *id)
284 for (p = papers; p != NULL; p = p->next) {
285 paper = (glPaper *) p->data;
286 if (g_strcasecmp (paper->id, id) == 0) {
296 * gl_paper_is_id_other:
297 * @id: paper id to test
299 * Determine if given paper id is the special id "Other."
301 * Returns: TRUE if id is "Other", otherwise FALSE.
305 gl_paper_is_id_other (const gchar *id)
311 return (g_strcasecmp (id, "Other") == 0);
317 * @id: paper id string
319 * Lookup paper definition from id string.
321 * Returns: pointer to a newly allocated #glPaper structure.
325 gl_paper_from_id (const gchar *id)
335 /* If no id, return first paper as a default */
336 return gl_paper_dup ((glPaper *) papers->data);
339 for (p = papers; p != NULL; p = p->next) {
340 paper = (glPaper *) p->data;
341 if (g_strcasecmp (paper->id, id) == 0) {
342 return gl_paper_dup (paper);
351 * gl_paper_from_name:
352 * @name: localized paper name string
354 * Lookup paper definition from localized paper name string.
356 * Returns: pointer to a newly allocated #glPaper structure.
360 gl_paper_from_name (const gchar *name)
370 /* If no name, return first paper as a default */
371 return gl_paper_dup ((glPaper *) papers->data);
374 for (p = papers; p != NULL; p = p->next) {
375 paper = (glPaper *) p->data;
376 if (g_strcasecmp (paper->name, name) == 0) {
377 return gl_paper_dup (paper);
386 * gl_paper_lookup_id_from_name:
387 * @name: localized paper name stringp
389 * Lookup paper name string from localized paper name string.
391 * Returns: pointer to a newly allocated id string.
395 gl_paper_lookup_id_from_name (const gchar *name)
397 glPaper *paper = NULL;
402 paper = gl_paper_from_name (name);
405 id = g_strdup (paper->id);
406 gl_paper_free (paper);
415 * gl_paper_lookup_name_from_id:
416 * @id: paper id string
418 * Lookup localized paper name string from paper id string.
420 * Returns: pointer to a newly allocated localized paper name string.
424 gl_paper_lookup_name_from_id (const gchar *id)
426 glPaper *paper = NULL;
431 paper = gl_paper_from_id (id);
434 name = g_strdup (paper->name);
435 gl_paper_free (paper);
443 /*--------------------------------------------------------------------------*/
444 /* PRIVATE. Read papers from various files. */
445 /*--------------------------------------------------------------------------*/
450 GList *papers = NULL;
452 data_dir = GL_SYSTEM_DATA_DIR;
453 papers = read_paper_files_from_dir (papers, data_dir);
456 data_dir = GL_USER_DATA_DIR;
457 papers = read_paper_files_from_dir (papers, data_dir);
460 if (papers == NULL) {
461 g_critical (_("Unable to locate paper size definitions. Libglabels may not be installed correctly!"));
467 /*--------------------------------------------------------------------------*/
468 /* PRIVATE. Read all paper files from given directory. Append to list. */
469 /*--------------------------------------------------------------------------*/
471 read_paper_files_from_dir (GList *papers,
472 const gchar *dirname)
475 const gchar *filename, *extension;
476 gchar *full_filename = NULL;
477 GError *gerror = NULL;
478 GList *new_papers = NULL;
480 if (dirname == NULL) {
484 if (!g_file_test (dirname, G_FILE_TEST_EXISTS)) {
488 dp = g_dir_open (dirname, 0, &gerror);
489 if (gerror != NULL) {
490 g_message ("cannot open data directory: %s", gerror->message );
494 while ((filename = g_dir_read_name (dp)) != NULL) {
496 extension = strrchr (filename, '.');
498 if (extension != NULL) {
500 if ( (g_strcasecmp (extension, ".paper") == 0)
501 || (g_strcasecmp (filename, "paper-sizes.xml") == 0) ) {
504 g_build_filename (dirname, filename, NULL);
506 gl_xml_paper_read_papers_from_file (full_filename);
507 g_free (full_filename);
509 papers = g_list_concat (papers, new_papers);
525 * gl_paper_print_known_papers:
527 * For debugging purposes: print a list of all paper definitions known to
532 gl_paper_print_known_papers (void)
541 g_print ("%s():\n", __FUNCTION__);
542 for (p = papers; p != NULL; p = p->next) {
543 paper = (glPaper *) p->data;
545 g_print ("PAPER id=\"%s\", name=\"%s\", width=%gpts, height=%gpts\n",
546 paper->id, paper->name, paper->width, paper->height);