]> git.sur5r.net Git - glabels/blob - glabels2/src/paper.c
2004-01-03 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / src / paper.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  paper.c:  paper module
5  *
6  *  Copyright (C) 2003  Jim Evins <evins@snaught.com>.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26 #include <libgnome/libgnome.h>
27
28 #include "paper.h"
29 #include "xml-paper.h"
30 #include "util.h"
31
32 #include "debug.h"
33
34 #define GL_DATA_DIR gnome_program_locate_file (NULL,\
35                                          GNOME_FILE_DOMAIN_APP_DATADIR,\
36                                          "glabels",\
37                                          FALSE, NULL)
38
39
40 /*===========================================*/
41 /* Private types                             */
42 /*===========================================*/
43
44 /*===========================================*/
45 /* Private globals                           */
46 /*===========================================*/
47
48 static GList *papers = NULL;
49
50 /*===========================================*/
51 /* Local function prototypes                 */
52 /*===========================================*/
53
54 static GList *read_papers (void);
55 static GList *read_paper_files_from_dir (GList       *papers,
56                                          const gchar *dirname);
57
58 \f
59 /*****************************************************************************/
60 /* Initialize module.                                                        */
61 /*****************************************************************************/
62 void
63 gl_paper_init (void)
64 {
65         glPaper *other;
66
67         gl_debug (DEBUG_PAPER, "START");
68
69         papers = read_papers ();
70
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);
76
77         gl_debug (DEBUG_PAPER, "END");
78 }
79
80 /*****************************************************************************/
81 /* Get a list of valid page size ids                                         */
82 /*****************************************************************************/
83 GList *
84 gl_paper_get_id_list (void)
85 {
86         GList           *ids = NULL;
87         GList           *p;
88         glPaper         *paper;
89
90         gl_debug (DEBUG_PAPER, "START");
91
92         for ( p=papers; p != NULL; p=p->next ) {
93                 paper = (glPaper *)p->data;
94                 ids = g_list_append (ids, g_strdup (paper->id));
95         }
96
97         gl_debug (DEBUG_PAPER, "END");
98         return ids;
99 }
100
101 /*****************************************************************************/
102 /* Free a list of page size ids.                                             */
103 /*****************************************************************************/
104 void
105 gl_paper_free_id_list (GList **ids)
106 {
107         GList *p;
108
109         gl_debug (DEBUG_PAPER, "START");
110
111         for (p = *ids; p != NULL; p = p->next) {
112                 g_free (p->data);
113                 p->data = NULL;
114         }
115
116         g_list_free (*ids);
117         *ids = NULL;
118
119         gl_debug (DEBUG_PAPER, "END");
120 }
121
122 /*****************************************************************************/
123 /* Get a list of valid page size names                                       */
124 /*****************************************************************************/
125 GList *
126 gl_paper_get_name_list (void)
127 {
128         GList           *names = NULL;
129         GList           *p;
130         glPaper         *paper;
131
132         gl_debug (DEBUG_PAPER, "START");
133
134         for ( p=papers; p != NULL; p=p->next ) {
135                 paper = (glPaper *)p->data;
136                 names = g_list_append (names, g_strdup (paper->name));
137         }
138
139         gl_debug (DEBUG_PAPER, "END");
140         return names;
141 }
142
143 /*****************************************************************************/
144 /* Free a list of page size names.                                           */
145 /*****************************************************************************/
146 void
147 gl_paper_free_name_list (GList **names)
148 {
149         GList *p;
150
151         gl_debug (DEBUG_PAPER, "START");
152
153         for (p = *names; p != NULL; p = p->next) {
154                 g_free (p->data);
155                 p->data = NULL;
156         }
157
158         g_list_free (*names);
159         *names = NULL;
160
161         gl_debug (DEBUG_PAPER, "END");
162 }
163
164 /*****************************************************************************/
165 /* Is page size id known?                                                    */
166 /*****************************************************************************/
167 gboolean
168 gl_paper_is_id_known (const gchar *id)
169 {
170         GList       *p;
171         glPaper     *paper;
172
173         gl_debug (DEBUG_PAPER, "START");
174
175         if (id == NULL) {
176                 gl_debug (DEBUG_PAPER, "END (false, id=NULL)");
177                 return FALSE;
178         }
179
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)");
184                         return TRUE;
185                 }
186         }
187
188         gl_debug (DEBUG_PAPER, "END (false)");
189         return FALSE;
190 }
191
192 /*****************************************************************************/
193 /* Return a paper structure from id.                                         */
194 /*****************************************************************************/
195 glPaper *
196 gl_paper_from_id (const gchar *id)
197 {
198         GList       *p;
199         glPaper     *paper;
200
201         gl_debug (DEBUG_PAPER, "START");
202
203         if (id == NULL) {
204                 /* If no id, return first paper as a default */
205                 return gl_paper_dup ((glPaper *) papers->data);
206         }
207
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);
213                 }
214         }
215
216         gl_debug (DEBUG_PAPER, "END");
217         return NULL;
218 }
219
220 /*****************************************************************************/
221 /* Return a paper structure from name.                                       */
222 /*****************************************************************************/
223 glPaper *
224 gl_paper_from_name (const gchar *name)
225 {
226         GList       *p;
227         glPaper     *paper;
228
229         gl_debug (DEBUG_PAPER, "START");
230
231         if (name == NULL) {
232                 /* If no name, return first paper as a default */
233                 return gl_paper_dup ((glPaper *) papers->data);
234         }
235
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);
241                 }
242         }
243
244         gl_debug (DEBUG_PAPER, "END");
245         return NULL;
246 }
247
248 /*****************************************************************************/
249 /* Lookup paper id from name.                                                */
250 /*****************************************************************************/
251 gchar *
252 gl_paper_lookup_id_from_name (const gchar       *name)
253 {
254         glPaper *paper;
255         gchar   *id = NULL;
256
257         gl_debug (DEBUG_PAPER, "START");
258
259         paper = gl_paper_from_name (name);
260         if ( paper != NULL ) {
261                 id = g_strdup (paper->id);
262                 gl_paper_free (&paper);
263         }
264
265         gl_debug (DEBUG_PAPER, "END");
266         return id;
267 }
268
269 /*****************************************************************************/
270 /* Lookup paper name from id.                                                */
271 /*****************************************************************************/
272 gchar *
273 gl_paper_lookup_name_from_id (const gchar       *id)
274 {
275         glPaper *paper;
276         gchar   *name = NULL;
277
278         gl_debug (DEBUG_PAPER, "START");
279
280         paper = gl_paper_from_id (id);
281         if ( paper != NULL ) {
282                 name = g_strdup (paper->name);
283                 gl_paper_free (&paper);
284         }
285
286         gl_debug (DEBUG_PAPER, "END");
287         return name;
288 }
289
290 /*****************************************************************************/
291 /* Copy a paper.                                                          */
292 /*****************************************************************************/
293 glPaper *gl_paper_dup (const glPaper *orig_paper)
294 {
295         glPaper       *paper;
296
297         gl_debug (DEBUG_PAPER, "START");
298
299         paper = g_new0 (glPaper,1);
300
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;
305
306         gl_debug (DEBUG_PAPER, "END");
307         return paper;
308 }
309
310 /*****************************************************************************/
311 /* Free up a paper.                                                       */
312 /*****************************************************************************/
313 void gl_paper_free (glPaper **paper)
314 {
315         GList *p;
316
317         gl_debug (DEBUG_PAPER, "START");
318
319         if ( *paper != NULL ) {
320
321                 g_free ((*paper)->id);
322                 (*paper)->name = NULL;
323
324                 g_free ((*paper)->name);
325                 (*paper)->name = NULL;
326
327                 g_free (*paper);
328                 *paper = NULL;
329
330         }
331
332         gl_debug (DEBUG_PAPER, "END");
333 }
334
335 /*--------------------------------------------------------------------------*/
336 /* PRIVATE.  Read papers from various  files.                            */
337 /*--------------------------------------------------------------------------*/
338 static GList *
339 read_papers (void)
340 {
341         gchar *home_data_dir = gl_util_get_home_data_dir ();
342         GList *papers = NULL;
343
344         gl_debug (DEBUG_PAPER, "START");
345
346         papers = read_paper_files_from_dir (papers, GL_DATA_DIR);
347         papers = read_paper_files_from_dir (papers, home_data_dir);
348
349         g_free (home_data_dir);
350
351         if (papers == NULL) {
352                 g_warning (_("No paper files found!"));
353         }
354
355         gl_debug (DEBUG_PAPER, "END");
356         return papers;
357 }
358
359 /*--------------------------------------------------------------------------*/
360 /* PRIVATE.  Read all paper files from given directory.  Append to list. */
361 /*--------------------------------------------------------------------------*/
362 static GList *
363 read_paper_files_from_dir (GList       *papers,
364                            const gchar *dirname)
365 {
366         GDir        *dp;
367         const gchar *filename, *extension;
368         gchar       *full_filename = NULL;
369         GError      *gerror = NULL;
370
371         gl_debug (DEBUG_PAPER, "START");
372
373         if (dirname == NULL)
374                 return papers;
375
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");
380                 return papers;
381         }
382
383         while ((filename = g_dir_read_name (dp)) != NULL) {
384
385                 extension = strrchr (filename, '.');
386
387                 if (extension != NULL) {
388
389                         if ( (g_strcasecmp (extension, ".paper") == 0)
390                              || (g_strcasecmp (filename, "paper-sizes.xml") == 0) ) {
391
392                                 full_filename =
393                                     g_build_filename (dirname, filename, NULL);
394                                 papers =
395                                     gl_xml_paper_read_papers_from_file (papers,
396                                                                         full_filename);
397                                 g_free (full_filename);
398
399                         }
400
401                 }
402
403         }
404
405         g_dir_close (dp);
406
407         gl_debug (DEBUG_PAPER, "END");
408         return papers;
409 }
410