2 * (GLABELS) Label and Business Card Creation program for GNOME
4 * paper.c: paper module
6 * Copyright (C) 2003 Jim Evins <evins@snaught.com>.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <libgnome/libgnome.h>
29 #include "xml-paper.h"
34 #define GL_DATA_DIR gnome_program_locate_file (NULL,\
35 GNOME_FILE_DOMAIN_APP_DATADIR,\
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);
59 /*****************************************************************************/
60 /* Initialize module. */
61 /*****************************************************************************/
67 gl_debug (DEBUG_PAPER, "START");
69 papers = read_papers ();
71 /* Create and append an "Other" entry. */
72 other = g_new0 (glPaper,1);
73 other->id = g_strdup ("Other");
74 other->name = g_strdup (_("Other"));
75 papers = g_list_append (papers, other);
77 gl_debug (DEBUG_PAPER, "END");
80 /*****************************************************************************/
81 /* Get a list of valid page size ids */
82 /*****************************************************************************/
84 gl_paper_get_id_list (void)
90 gl_debug (DEBUG_PAPER, "START");
92 for ( p=papers; p != NULL; p=p->next ) {
93 paper = (glPaper *)p->data;
94 ids = g_list_append (ids, g_strdup (paper->id));
97 gl_debug (DEBUG_PAPER, "END");
101 /*****************************************************************************/
102 /* Free a list of page size ids. */
103 /*****************************************************************************/
105 gl_paper_free_id_list (GList **ids)
109 gl_debug (DEBUG_PAPER, "START");
111 for (p = *ids; p != NULL; p = p->next) {
119 gl_debug (DEBUG_PAPER, "END");
122 /*****************************************************************************/
123 /* Get a list of valid page size names */
124 /*****************************************************************************/
126 gl_paper_get_name_list (void)
132 gl_debug (DEBUG_PAPER, "START");
134 for ( p=papers; p != NULL; p=p->next ) {
135 paper = (glPaper *)p->data;
136 names = g_list_append (names, g_strdup (paper->name));
139 gl_debug (DEBUG_PAPER, "END");
143 /*****************************************************************************/
144 /* Free a list of page size names. */
145 /*****************************************************************************/
147 gl_paper_free_name_list (GList **names)
151 gl_debug (DEBUG_PAPER, "START");
153 for (p = *names; p != NULL; p = p->next) {
158 g_list_free (*names);
161 gl_debug (DEBUG_PAPER, "END");
164 /*****************************************************************************/
165 /* Is page size id known? */
166 /*****************************************************************************/
168 gl_paper_is_id_known (const gchar *id)
173 gl_debug (DEBUG_PAPER, "START");
176 gl_debug (DEBUG_PAPER, "END (false, id=NULL)");
180 for (p = papers; p != NULL; p = p->next) {
181 paper = (glPaper *) p->data;
182 if (g_strcasecmp (paper->id, id) == 0) {
183 gl_debug (DEBUG_PAPER, "END (true)");
188 gl_debug (DEBUG_PAPER, "END (false)");
192 /*****************************************************************************/
193 /* Return a paper structure from id. */
194 /*****************************************************************************/
196 gl_paper_from_id (const gchar *id)
201 gl_debug (DEBUG_PAPER, "START");
204 /* If no id, return first paper as a default */
205 return gl_paper_dup ((glPaper *) papers->data);
208 for (p = papers; p != NULL; p = p->next) {
209 paper = (glPaper *) p->data;
210 if (g_strcasecmp (paper->id, id) == 0) {
211 gl_debug (DEBUG_PAPER, "END");
212 return gl_paper_dup (paper);
216 gl_debug (DEBUG_PAPER, "END");
220 /*****************************************************************************/
221 /* Return a paper structure from name. */
222 /*****************************************************************************/
224 gl_paper_from_name (const gchar *name)
229 gl_debug (DEBUG_PAPER, "START");
232 /* If no name, return first paper as a default */
233 return gl_paper_dup ((glPaper *) papers->data);
236 for (p = papers; p != NULL; p = p->next) {
237 paper = (glPaper *) p->data;
238 if (g_strcasecmp (paper->name, name) == 0) {
239 gl_debug (DEBUG_PAPER, "END");
240 return gl_paper_dup (paper);
244 gl_debug (DEBUG_PAPER, "END");
248 /*****************************************************************************/
249 /* Lookup paper id from name. */
250 /*****************************************************************************/
252 gl_paper_lookup_id_from_name (const gchar *name)
257 gl_debug (DEBUG_PAPER, "START");
259 paper = gl_paper_from_name (name);
260 if ( paper != NULL ) {
261 id = g_strdup (paper->id);
262 gl_paper_free (&paper);
265 gl_debug (DEBUG_PAPER, "END");
269 /*****************************************************************************/
270 /* Lookup paper name from id. */
271 /*****************************************************************************/
273 gl_paper_lookup_name_from_id (const gchar *id)
278 gl_debug (DEBUG_PAPER, "START");
280 paper = gl_paper_from_id (id);
281 if ( paper != NULL ) {
282 name = g_strdup (paper->name);
283 gl_paper_free (&paper);
286 gl_debug (DEBUG_PAPER, "END");
290 /*****************************************************************************/
292 /*****************************************************************************/
293 glPaper *gl_paper_dup (const glPaper *orig_paper)
297 gl_debug (DEBUG_PAPER, "START");
299 paper = g_new0 (glPaper,1);
301 paper->id = g_strdup (orig_paper->id);
302 paper->name = g_strdup (orig_paper->name);
303 paper->width = orig_paper->width;
304 paper->height = orig_paper->height;
306 gl_debug (DEBUG_PAPER, "END");
310 /*****************************************************************************/
311 /* Free up a paper. */
312 /*****************************************************************************/
313 void gl_paper_free (glPaper **paper)
317 gl_debug (DEBUG_PAPER, "START");
319 if ( *paper != NULL ) {
321 g_free ((*paper)->id);
322 (*paper)->name = NULL;
324 g_free ((*paper)->name);
325 (*paper)->name = NULL;
332 gl_debug (DEBUG_PAPER, "END");
335 /*--------------------------------------------------------------------------*/
336 /* PRIVATE. Read papers from various files. */
337 /*--------------------------------------------------------------------------*/
341 gchar *home_data_dir = gl_util_get_home_data_dir ();
342 GList *papers = NULL;
344 gl_debug (DEBUG_PAPER, "START");
346 papers = read_paper_files_from_dir (papers, GL_DATA_DIR);
347 papers = read_paper_files_from_dir (papers, home_data_dir);
349 g_free (home_data_dir);
351 if (papers == NULL) {
352 g_warning (_("No paper files found!"));
355 gl_debug (DEBUG_PAPER, "END");
359 /*--------------------------------------------------------------------------*/
360 /* PRIVATE. Read all paper files from given directory. Append to list. */
361 /*--------------------------------------------------------------------------*/
363 read_paper_files_from_dir (GList *papers,
364 const gchar *dirname)
367 const gchar *filename, *extension;
368 gchar *full_filename = NULL;
369 GError *gerror = NULL;
371 gl_debug (DEBUG_PAPER, "START");
376 dp = g_dir_open (dirname, 0, &gerror);
377 if (gerror != NULL) {
378 g_warning ("cannot open data directory: %s", gerror->message );
379 gl_debug (DEBUG_PAPER, "END");
383 while ((filename = g_dir_read_name (dp)) != NULL) {
385 extension = strrchr (filename, '.');
387 if (extension != NULL) {
389 if ( (g_strcasecmp (extension, ".paper") == 0)
390 || (g_strcasecmp (filename, "paper-sizes.xml") == 0) ) {
393 g_build_filename (dirname, filename, NULL);
395 gl_xml_paper_read_papers_from_file (papers,
397 g_free (full_filename);
407 gl_debug (DEBUG_PAPER, "END");