]> git.sur5r.net Git - glabels/blob - src/prefs-model.c
Imported Upstream version 2.2.8
[glabels] / src / prefs-model.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
3 /*
4  *  (GLABELS) Label and Business Card Creation program for GNOME
5  *
6  *  prefs-model.c:  Application preferences model module
7  *
8  *  Copyright (C) 2001-2003  Jim Evins <evins@snaught.com>.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program 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
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  */
24 #include <config.h>
25
26 #include "prefs-model.h"
27
28 #include <libglabels/db.h>
29 #include <libglabels/xml.h>
30 #include <gtk/gtkpapersize.h>
31
32 #include "marshal.h"
33 #include "util.h"
34 #include "color.h"
35
36 #include "debug.h"
37
38 /*========================================================*/
39 /* Private macros and constants.                          */
40 /*========================================================*/
41
42 /* GConf keys */
43 #define BASE_KEY                            "/apps/glabels"
44
45 #define PREF_UNITS                          "/units"
46 #define PREF_DEFAULT_PAGE_SIZE              "/default-page-size"
47
48 #define PREF_DEFAULT_FONT_FAMILY            "/default-font-family"
49 #define PREF_DEFAULT_FONT_SIZE              "/default-font-size"
50 #define PREF_DEFAULT_FONT_WEIGHT            "/default-font-weight"
51 #define PREF_DEFAULT_FONT_ITALIC_FLAG       "/default-font-italic-flag"
52 #define PREF_DEFAULT_TEXT_COLOR             "/default-text-color"
53 #define PREF_DEFAULT_TEXT_ALIGNMENT         "/default-text-alignment"
54 #define PREF_DEFAULT_TEXT_LINE_SPACING      "/default-text-line-spacing"
55
56 #define PREF_DEFAULT_LINE_WIDTH             "/default-line-width"
57 #define PREF_DEFAULT_LINE_COLOR             "/default-line-color"
58
59 #define PREF_DEFAULT_FILL_COLOR             "/default-fill-color"
60
61 #define PREF_MAIN_TOOLBAR_VISIBLE           "/main-toolbar-visible"
62 #define PREF_MAIN_TOOLBAR_BUTTONS_STYLE     "/main-toolbar-buttons-style"
63 #define PREF_MAIN_TOOLBAR_VIEW_TOOLTIPS     "/main-toolbar-view-tooltips"
64
65 #define PREF_DRAWING_TOOLBAR_VISIBLE        "/drawing-toolbar-visible"
66 #define PREF_DRAWING_TOOLBAR_VIEW_TOOLTIPS  "/drawing-toolbar-view-tooltips"
67
68 #define PREF_PROPERTY_TOOLBAR_VISIBLE        "/property-toolbar-visible"
69 #define PREF_PROPERTY_TOOLBAR_VIEW_TOOLTIPS  "/property-toolbar-view-tooltips"
70
71 #define PREF_GRID_VISIBLE                   "/grid-visible"
72 #define PREF_MARKUP_VISIBLE                 "/markup-visible"
73
74 #define PREF_MAX_RECENTS                    "/max-recents"
75
76 #define PREF_RECENT_TEMPLATES               "/recent-templates"
77 #define PREF_MAX_RECENT_TEMPLATES           "/max-recent-templates"
78
79 /* Default values */
80 #define DEFAULT_UNITS_STRING_US    units_to_string (LGL_UNITS_INCH)
81 #define DEFAULT_PAGE_SIZE_US       "US-Letter"
82
83 #define DEFAULT_UNITS_STRING_METRIC units_to_string (LGL_UNITS_MM)
84 #define DEFAULT_PAGE_SIZE_METRIC   "A4"
85
86 #define DEFAULT_FONT_FAMILY        "Sans"
87 #define DEFAULT_FONT_SIZE          14.0
88 #define DEFAULT_FONT_WEIGHT_STRING gl_util_weight_to_string (PANGO_WEIGHT_NORMAL)
89 #define DEFAULT_FONT_ITALIC_FLAG   FALSE
90 #define DEFAULT_TEXT_ALIGN_STRING  gl_util_align_to_string (PANGO_ALIGN_LEFT)
91 #define DEFAULT_TEXT_COLOR         GL_COLOR (0,0,0)
92 #define DEFAULT_TEXT_LINE_SPACING  1.0
93
94 #define DEFAULT_LINE_WIDTH         1.0
95 #define DEFAULT_LINE_COLOR         GL_COLOR_A (0, 0, 0, 255)
96
97 #define DEFAULT_FILL_COLOR         GL_COLOR_A (0, 255, 0, 255)
98
99 /*========================================================*/
100 /* Private types.                                         */
101 /*========================================================*/
102
103 enum {
104         CHANGED,
105         LAST_SIGNAL
106 };
107
108
109
110 /*========================================================*/
111 /* Private globals.                                       */
112 /*========================================================*/
113
114 static guint signals[LAST_SIGNAL] = {0};
115
116 /*========================================================*/
117 /* Private function prototypes.                           */
118 /*========================================================*/
119
120 static void           gl_prefs_model_finalize      (GObject             *object);
121
122 static void           notify_cb                    (GConfClient         *client,
123                                                     guint                cnxn_id,
124                                                     GConfEntry          *entry,
125                                                     glPrefsModel        *prefs_model);
126
127 static gchar         *get_string                   (GConfClient         *client,
128                                                     const gchar         *key,
129                                                     const gchar         *def);
130
131 static gboolean       get_bool                     (GConfClient         *client,
132                                                     const gchar         *key,
133                                                     gboolean             def);
134
135 static gint           get_int                      (GConfClient         *client,
136                                                     const gchar         *key,
137                                                     gint                 def);
138
139 static gdouble        get_float                    (GConfClient         *client,
140                                                     const gchar         *key,
141                                                     gdouble              def);
142
143 static lglUnitsType   string_to_units              (const gchar         *string);
144 static const gchar   *units_to_string              (lglUnitsType         units);
145
146
147 \f
148 /*****************************************************************************/
149 /* Boilerplate object stuff.                                                 */
150 /*****************************************************************************/
151 G_DEFINE_TYPE (glPrefsModel, gl_prefs_model, G_TYPE_OBJECT);
152
153 static void
154 gl_prefs_model_class_init (glPrefsModelClass *class)
155 {
156         GObjectClass *object_class = G_OBJECT_CLASS (class);
157
158         gl_debug (DEBUG_PREFS, "START");
159
160         gl_prefs_model_parent_class = g_type_class_peek_parent (class);
161
162         object_class->finalize = gl_prefs_model_finalize;
163
164         signals[CHANGED] =
165                 g_signal_new ("changed",
166                               G_OBJECT_CLASS_TYPE (object_class),
167                               G_SIGNAL_RUN_LAST,
168                               G_STRUCT_OFFSET (glPrefsModelClass, changed),
169                               NULL, NULL,
170                               gl_marshal_VOID__VOID,
171                               G_TYPE_NONE,
172                               0);
173
174         gl_debug (DEBUG_PREFS, "END");
175 }
176
177 static void
178 gl_prefs_model_init (glPrefsModel *prefs_model)
179 {
180         gl_debug (DEBUG_PREFS, "START");
181
182         prefs_model->gconf_client = gconf_client_get_default ();
183
184         g_return_if_fail (prefs_model->gconf_client != NULL);
185  
186         gconf_client_add_dir (prefs_model->gconf_client,
187                               BASE_KEY,
188                               GCONF_CLIENT_PRELOAD_ONELEVEL,
189                               NULL);
190          
191         gconf_client_notify_add (prefs_model->gconf_client,
192                                  BASE_KEY,
193                                  (GConfClientNotifyFunc)notify_cb, prefs_model,
194                                  NULL, NULL);
195
196         gl_debug (DEBUG_PREFS, "END");
197 }
198
199 static void
200 gl_prefs_model_finalize (GObject *object)
201 {
202         glPrefsModel *prefs_model = GL_PREFS_MODEL (object);
203
204         gl_debug (DEBUG_PREFS, "START");
205
206         g_return_if_fail (object && GL_IS_PREFS_MODEL (object));
207
208         g_object_unref (G_OBJECT(prefs_model->gconf_client));
209         g_free (prefs_model->default_page_size);
210         g_free (prefs_model->default_font_family);
211
212         G_OBJECT_CLASS (gl_prefs_model_parent_class)->finalize (object);
213
214         gl_debug (DEBUG_PREFS, "END");
215 }
216
217 /*****************************************************************************/
218 /* New prefs_model object.                                                   */
219 /*****************************************************************************/
220 glPrefsModel *
221 gl_prefs_model_new (void)
222 {
223         glPrefsModel *prefs_model;
224
225         gl_debug (DEBUG_PREFS, "START");
226
227         prefs_model = GL_PREFS_MODEL (g_object_new (gl_prefs_model_get_type(), NULL));
228
229         gl_debug (DEBUG_PREFS, "END");
230
231         return prefs_model;
232 }
233
234
235
236 /*****************************************************************************/
237 /* Save all settings.                                                        */
238 /*****************************************************************************/
239 void 
240 gl_prefs_model_save_settings (glPrefsModel *prefs_model)
241 {
242         gl_debug (DEBUG_PREFS, "START");
243         
244         g_return_if_fail (prefs_model && GL_IS_PREFS_MODEL(prefs_model));
245         g_return_if_fail (prefs_model->gconf_client != NULL);
246
247         /* We are saving settings because presumably some of them have been changed. */
248         g_signal_emit (G_OBJECT(prefs_model), signals[CHANGED], 0);
249
250         /* Units */
251         gconf_client_set_string (prefs_model->gconf_client,
252                                  BASE_KEY PREF_UNITS,
253                                  units_to_string(prefs_model->units),
254                                  NULL);
255         lgl_xml_set_default_units (prefs_model->units);
256
257         /* Default page size */
258         gconf_client_set_string (prefs_model->gconf_client,
259                                  BASE_KEY PREF_DEFAULT_PAGE_SIZE,
260                                  prefs_model->default_page_size,
261                                  NULL);
262
263
264         /* Text properties */
265         gconf_client_set_string (prefs_model->gconf_client,
266                                  BASE_KEY PREF_DEFAULT_FONT_FAMILY,
267                                  prefs_model->default_font_family,
268                                  NULL);
269
270         gconf_client_set_float  (prefs_model->gconf_client,
271                                  BASE_KEY PREF_DEFAULT_FONT_SIZE,
272                                  prefs_model->default_font_size,
273                                  NULL);
274
275         gconf_client_set_string (prefs_model->gconf_client,
276                                  BASE_KEY PREF_DEFAULT_FONT_WEIGHT,
277                                  gl_util_weight_to_string(prefs_model->default_font_weight),
278                                  NULL);
279
280         gconf_client_set_int    (prefs_model->gconf_client,
281                                  BASE_KEY PREF_DEFAULT_TEXT_COLOR,
282                                  prefs_model->default_text_color,
283                                  NULL);
284
285         gconf_client_set_string (prefs_model->gconf_client,
286                                  BASE_KEY PREF_DEFAULT_TEXT_ALIGNMENT,
287                                  gl_util_align_to_string(prefs_model->default_text_alignment),
288                                  NULL);
289
290         gconf_client_set_float  (prefs_model->gconf_client,
291                                  BASE_KEY PREF_DEFAULT_TEXT_LINE_SPACING,
292                                  prefs_model->default_text_line_spacing,
293                                  NULL);
294
295         /* Line properties */
296         gconf_client_set_float  (prefs_model->gconf_client,
297                                  BASE_KEY PREF_DEFAULT_LINE_WIDTH,
298                                  prefs_model->default_line_width,
299                                  NULL);
300
301         gconf_client_set_int    (prefs_model->gconf_client,
302                                  BASE_KEY PREF_DEFAULT_LINE_COLOR,
303                                  prefs_model->default_line_color,
304                                  NULL);
305
306
307         /* Fill properties */
308         gconf_client_set_int    (prefs_model->gconf_client,
309                                  BASE_KEY PREF_DEFAULT_FILL_COLOR,
310                                  prefs_model->default_fill_color,
311                                  NULL);
312
313
314         /* Main Toolbar */
315         gconf_client_set_bool (prefs_model->gconf_client,
316                                BASE_KEY PREF_MAIN_TOOLBAR_VISIBLE,
317                                prefs_model->main_toolbar_visible,
318                                NULL);
319
320         gconf_client_set_int (prefs_model->gconf_client,
321                               BASE_KEY PREF_MAIN_TOOLBAR_BUTTONS_STYLE,
322                               prefs_model->main_toolbar_buttons_style,
323                               NULL);
324
325         gconf_client_set_bool (prefs_model->gconf_client,
326                                BASE_KEY PREF_MAIN_TOOLBAR_VIEW_TOOLTIPS,
327                                prefs_model->main_toolbar_view_tooltips,
328                                NULL);
329
330         /* Drawing Toolbar */
331         gconf_client_set_bool (prefs_model->gconf_client,
332                                BASE_KEY PREF_DRAWING_TOOLBAR_VISIBLE,
333                                prefs_model->drawing_toolbar_visible,
334                                NULL);
335
336         gconf_client_set_bool (prefs_model->gconf_client,
337                                BASE_KEY PREF_DRAWING_TOOLBAR_VIEW_TOOLTIPS,
338                                prefs_model->drawing_toolbar_view_tooltips,
339                                NULL);
340
341         /* Property Toolbar */
342         gconf_client_set_bool (prefs_model->gconf_client,
343                                BASE_KEY PREF_PROPERTY_TOOLBAR_VISIBLE,
344                                prefs_model->property_toolbar_visible,
345                                NULL);
346
347         gconf_client_set_bool (prefs_model->gconf_client,
348                                BASE_KEY PREF_PROPERTY_TOOLBAR_VIEW_TOOLTIPS,
349                                prefs_model->property_toolbar_view_tooltips,
350                                NULL);
351
352         /* View properties */
353         gconf_client_set_bool (prefs_model->gconf_client,
354                                BASE_KEY PREF_GRID_VISIBLE,
355                                prefs_model->grid_visible,
356                                NULL);
357
358         gconf_client_set_bool (prefs_model->gconf_client,
359                                BASE_KEY PREF_MARKUP_VISIBLE,
360                                prefs_model->markup_visible,
361                                NULL);
362
363         /* Recent files */
364         gconf_client_set_int (prefs_model->gconf_client,
365                               BASE_KEY PREF_MAX_RECENTS,
366                               prefs_model->max_recents,
367                               NULL);
368
369         /* Recent templates */
370         gconf_client_set_list (prefs_model->gconf_client,
371                                BASE_KEY PREF_RECENT_TEMPLATES,
372                                GCONF_VALUE_STRING,
373                                prefs_model->recent_templates,
374                                NULL);
375         gconf_client_set_int (prefs_model->gconf_client,
376                               BASE_KEY PREF_MAX_RECENT_TEMPLATES,
377                               prefs_model->max_recent_templates,
378                               NULL);
379
380
381         gconf_client_suggest_sync (prefs_model->gconf_client, NULL);
382         
383         gl_debug (DEBUG_PREFS, "END");
384 }
385
386 /*****************************************************************************/
387 /* Load all settings.                                                        */
388 /*****************************************************************************/
389 void
390 gl_prefs_model_load_settings (glPrefsModel *prefs_model)
391 {
392         const gchar *pgsize, *default_units_string, *default_page_size;
393         gchar    *string;
394         lglPaper *paper;
395         GSList   *p, *p_next;
396
397         gl_debug (DEBUG_PREFS, "START");
398         
399         g_return_if_fail (prefs_model && GL_IS_PREFS_MODEL(prefs_model));
400         g_return_if_fail (prefs_model->gconf_client != NULL);
401
402         /* Make educated guess about locale defaults. */
403         pgsize = gtk_paper_size_get_default ();
404         if ( strcmp (pgsize,GTK_PAPER_NAME_LETTER) == 0 )
405         {
406                 default_units_string = DEFAULT_UNITS_STRING_US;
407                 default_page_size    = DEFAULT_PAGE_SIZE_US;
408         }
409         else
410         {
411                 default_units_string = DEFAULT_UNITS_STRING_METRIC;
412                 default_page_size    = DEFAULT_PAGE_SIZE_METRIC;
413         }
414
415         /* Units */
416         string =
417                 get_string (prefs_model->gconf_client,
418                             BASE_KEY PREF_UNITS,
419                             default_units_string);
420         prefs_model->units = string_to_units( string );
421         g_free( string );
422         lgl_xml_set_default_units (prefs_model->units);
423
424
425         /* Page size */
426         g_free (prefs_model->default_page_size);
427         prefs_model->default_page_size =
428                 get_string (prefs_model->gconf_client,
429                             BASE_KEY PREF_DEFAULT_PAGE_SIZE,
430                             default_page_size);
431
432         /* Text properties */
433         g_free (prefs_model->default_font_family);
434         prefs_model->default_font_family =
435                 get_string (prefs_model->gconf_client,
436                             BASE_KEY PREF_DEFAULT_FONT_FAMILY,
437                             DEFAULT_FONT_FAMILY);
438
439         prefs_model->default_font_size =
440                 get_float (prefs_model->gconf_client,
441                            BASE_KEY PREF_DEFAULT_FONT_SIZE,
442                            DEFAULT_FONT_SIZE);
443
444         string =
445                 get_string (prefs_model->gconf_client,
446                             BASE_KEY PREF_DEFAULT_FONT_WEIGHT,
447                             DEFAULT_FONT_WEIGHT_STRING);
448         prefs_model->default_font_weight = gl_util_string_to_weight( string );
449         g_free( string );
450
451         prefs_model->default_text_color =
452                 get_int (prefs_model->gconf_client,
453                          BASE_KEY PREF_DEFAULT_TEXT_COLOR,
454                          DEFAULT_TEXT_COLOR);
455
456         string =
457                 get_string (prefs_model->gconf_client,
458                             BASE_KEY PREF_DEFAULT_TEXT_ALIGNMENT,
459                             DEFAULT_TEXT_ALIGN_STRING);
460         prefs_model->default_text_alignment = gl_util_string_to_align( string );
461         g_free( string );
462
463         prefs_model->default_text_line_spacing =
464                 get_float (prefs_model->gconf_client,
465                            BASE_KEY PREF_DEFAULT_TEXT_LINE_SPACING,
466                            DEFAULT_TEXT_LINE_SPACING);
467
468         gl_debug (DEBUG_PREFS, "text_line_spacing = %f", prefs_model->default_text_line_spacing);
469
470         /* Line properties */
471         prefs_model->default_line_width =
472                 get_float (prefs_model->gconf_client,
473                            BASE_KEY PREF_DEFAULT_LINE_WIDTH,
474                            DEFAULT_LINE_WIDTH);
475         prefs_model->default_line_color =
476                 get_int (prefs_model->gconf_client,
477                          BASE_KEY PREF_DEFAULT_LINE_COLOR,
478                          DEFAULT_LINE_COLOR);
479
480         /* Fill properties */
481         prefs_model->default_fill_color =
482                 get_int (prefs_model->gconf_client,
483                          BASE_KEY PREF_DEFAULT_FILL_COLOR,
484                          DEFAULT_FILL_COLOR);
485
486
487         /* User Inferface/Main Toolbar */
488         prefs_model->main_toolbar_visible =
489                 get_bool (prefs_model->gconf_client,
490                           BASE_KEY PREF_MAIN_TOOLBAR_VISIBLE,
491                           TRUE);
492
493         prefs_model->main_toolbar_buttons_style =
494                 get_int (prefs_model->gconf_client,
495                          BASE_KEY PREF_MAIN_TOOLBAR_BUTTONS_STYLE,
496                          GL_TOOLBAR_SYSTEM);
497
498         prefs_model->main_toolbar_view_tooltips =
499                 get_bool (prefs_model->gconf_client,
500                           BASE_KEY PREF_MAIN_TOOLBAR_VIEW_TOOLTIPS,
501                           TRUE);
502
503         /* User Inferface/Drawing Toolbar */
504         prefs_model->drawing_toolbar_visible =
505                 get_bool (prefs_model->gconf_client,
506                           BASE_KEY PREF_DRAWING_TOOLBAR_VISIBLE,
507                           TRUE);
508
509         prefs_model->drawing_toolbar_view_tooltips =
510                 get_bool (prefs_model->gconf_client,
511                           BASE_KEY PREF_DRAWING_TOOLBAR_VIEW_TOOLTIPS,
512                           TRUE);
513
514         /* User Inferface/Property Toolbar */
515         prefs_model->property_toolbar_visible =
516                 get_bool (prefs_model->gconf_client,
517                           BASE_KEY PREF_PROPERTY_TOOLBAR_VISIBLE,
518                           TRUE);
519
520         prefs_model->property_toolbar_view_tooltips =
521                 get_bool (prefs_model->gconf_client,
522                           BASE_KEY PREF_PROPERTY_TOOLBAR_VIEW_TOOLTIPS,
523                           TRUE);
524
525
526         /* View properties */
527         prefs_model->grid_visible =
528                 get_bool (prefs_model->gconf_client,
529                           BASE_KEY PREF_GRID_VISIBLE,
530                           TRUE);
531
532         prefs_model->markup_visible =
533                 get_bool (prefs_model->gconf_client,
534                           BASE_KEY PREF_MARKUP_VISIBLE,
535                           TRUE);
536
537         /* Recent files */
538         prefs_model->max_recents =
539                 get_int (prefs_model->gconf_client,
540                          BASE_KEY PREF_MAX_RECENTS,
541                          -1);
542
543         /* Recent templates */
544         for (p=prefs_model->recent_templates; p != NULL; p=p->next)
545         {
546                 g_free (p->data);
547         }
548         g_slist_free (prefs_model->recent_templates);
549         prefs_model->recent_templates =
550                 gconf_client_get_list (prefs_model->gconf_client,
551                                        BASE_KEY PREF_RECENT_TEMPLATES,
552                                        GCONF_VALUE_STRING,
553                                        NULL);
554         prefs_model->max_recent_templates =
555                 get_int (prefs_model->gconf_client,
556                          BASE_KEY PREF_MAX_RECENT_TEMPLATES,
557                          5);
558
559
560         /* Proof read the default page size -- it must be a valid id. */
561         /* (For compatability with older versions.) */
562         paper = lgl_db_lookup_paper_from_id (prefs_model->default_page_size);
563         if ( paper == NULL ) {
564                 prefs_model->default_page_size = g_strdup (DEFAULT_PAGE_SIZE_US);
565         } else {
566                 lgl_paper_free (paper);
567                 paper = NULL;
568         }
569
570         /* Proof read the recent templates list.  Make sure the template names */
571         /* are valid.  Remove from list if not. */
572         for (p=prefs_model->recent_templates; p != NULL; p=p_next)
573         {
574                 p_next = p->next;
575
576                 if ( !lgl_db_does_template_name_exist (p->data) )
577                 {
578                         g_free (p->data);
579                         prefs_model->recent_templates = g_slist_delete_link (prefs_model->recent_templates, p);
580                 }
581         }
582
583         gl_debug (DEBUG_PREFS, "max_recents = %d", prefs_model->max_recents);
584
585
586         g_signal_emit (G_OBJECT(prefs_model), signals[CHANGED], 0);
587
588         gl_debug (DEBUG_PREFS, "END");
589 }
590
591 /*---------------------------------------------------------------------------*/
592 /* PRIVATE.  Key changed callback.                                           */
593 /*---------------------------------------------------------------------------*/
594 static void 
595 notify_cb (GConfClient  *client,
596            guint         cnxn_id,
597            GConfEntry   *entry,
598            glPrefsModel *prefs_model)
599 {
600         gl_debug (DEBUG_PREFS, "Key was changed: %s", entry->key);
601
602         gl_prefs_model_load_settings (prefs_model);
603 }
604
605 /*---------------------------------------------------------------------------*/
606 /* PRIVATE.  Utilities to get values with defaults.                          */
607 /*---------------------------------------------------------------------------*/
608 static gchar*
609 get_string (GConfClient *client,
610             const gchar *key,
611             const gchar *def)
612 {
613         gchar* val;
614
615         val = gconf_client_get_string (client, key, NULL);
616
617         if (val != NULL) {
618
619                 return val;
620
621         } else {
622
623                 return def ? g_strdup (def) : NULL;
624
625         }
626 }
627
628 static gboolean
629 get_bool (GConfClient *client,
630           const gchar *key,
631           gboolean     def)
632 {
633         GConfValue* val;
634         gboolean retval;
635
636         val = gconf_client_get (client, key, NULL);
637
638         if (val != NULL) {
639
640                 if ( val->type == GCONF_VALUE_BOOL ) {
641                         retval = gconf_value_get_bool (val);
642                 } else {
643                         retval = def;
644                 }
645
646                 gconf_value_free (val);
647
648                 return retval;
649
650         } else {
651
652                 return def;
653
654         }
655 }
656
657 static gint
658 get_int (GConfClient *client,
659          const gchar *key,
660          gint         def)
661 {
662         GConfValue* val;
663         gint retval;
664
665         val = gconf_client_get (client, key, NULL);
666
667         if (val != NULL) {
668
669                 if ( val->type == GCONF_VALUE_INT) {
670                         retval = gconf_value_get_int(val);
671                 } else {
672                         retval = def;
673                 }
674
675                 gconf_value_free (val);
676
677                 return retval;
678
679         } else {
680
681                 return def;
682
683         }
684 }
685
686 static gdouble
687 get_float (GConfClient *client,
688            const gchar *key,
689            gdouble      def)
690 {
691         GConfValue* val;
692         gdouble retval;
693
694         val = gconf_client_get (client, key, NULL);
695
696         if (val != NULL) {
697
698                 if ( val->type == GCONF_VALUE_FLOAT ) {
699                         retval = gconf_value_get_float(val);
700                 } else {
701                         retval = def;
702                 }
703
704                 gconf_value_free (val);
705
706                 return retval;
707
708         } else {
709                 return def;
710
711         }
712 }
713
714 /*---------------------------------------------------------------------------*/
715 /* PRIVATE.  Utilities to deal with units.                                   */
716 /*---------------------------------------------------------------------------*/
717 static lglUnitsType
718 string_to_units (const gchar *string)
719 {
720         lglUnitsType units;
721
722         if (g_strcasecmp (string, "Points") == 0) {
723                 units = LGL_UNITS_POINT;
724         } else if (g_strcasecmp (string, "Inches") == 0) {
725                 units = LGL_UNITS_INCH;
726         } else if (g_strcasecmp (string, "Millimeters") == 0) {
727                 units = LGL_UNITS_MM;
728         } else {
729                 units = LGL_UNITS_INCH;
730         }
731
732         return units;
733 }
734
735 static const
736 gchar *units_to_string (lglUnitsType units)
737 {
738         switch (units) {
739         case LGL_UNITS_POINT:
740                 return "Points";
741                 break;
742         case LGL_UNITS_INCH:
743                 return "Inches";
744                 break;
745         case LGL_UNITS_MM:
746                 return "Millimeters";
747                 break;
748         default:
749                 return "Inches";
750                 break;
751         }
752 }
753
754