]> git.sur5r.net Git - glabels/blob - glabels2/src/prefs.c
Initial revision
[glabels] / glabels2 / src / prefs.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  prefs.h:  Application preferences module header file
5  *
6  *  Copyright (C) 2001-2002  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 <libgnome/libgnome.h>
24 #include <libgnomeui/libgnomeui.h>
25
26 #include <bonobo-mdi.h>
27 #include <gconf/gconf-client.h>
28
29 #include "prefs.h"
30 #include "glabels.h"
31 #include "util.h"
32
33 #include "debug.h"
34
35 glPreferences      *gl_prefs     = NULL;
36
37 /*========================================================*/
38 /* Private macros and constants.                          */
39 /*========================================================*/
40
41 /* GConf keys */
42 #define BASE_KEY                            "/apps/glabels"
43
44 #define PREF_UNITS                          "/units"
45 #define PREF_DEFAULT_PAGE_SIZE              "/default-page-size"
46
47 #define PREF_DEFAULT_FONT_FAMILY            "/default-font-family"
48 #define PREF_DEFAULT_FONT_SIZE              "/default-font-size"
49 #define PREF_DEFAULT_FONT_WEIGHT            "/default-font-weight"
50 #define PREF_DEFAULT_FONT_ITALIC_FLAG       "/default-font-italic-flag"
51 #define PREF_DEFAULT_TEXT_COLOR             "/default-text-color"
52 #define PREF_DEFAULT_TEXT_ALIGNMENT         "/default-text-alignment"
53
54 #define PREF_DEFAULT_LINE_WIDTH             "/default-line-width"
55 #define PREF_DEFAULT_LINE_COLOR             "/default-line-color"
56
57 #define PREF_DEFAULT_FILL_COLOR             "/default-fill-color"
58
59 #define PREF_TOOLBAR_VISIBLE                "/toolbar-visible"
60 #define PREF_TOOLBAR_BUTTONS_STYLE          "/toolbar-buttons-style"
61 #define PREF_TOOLBAR_VIEW_TOOLTIPS          "/toolbar-view-tooltips"
62
63 #define PREF_MDI_MODE                       "/mdi-mode"
64 #define PREF_TABS_POSITION                  "/mdi-tabs-position"
65
66 #define PREF_MAX_RECENTS                    "/max-recents"
67
68 /* Default values */
69 #define DEFAULT_UNITS_STRING       units_to_string (GL_PREFS_UNITS_INCHES)
70 #define DEFAULT_PAGE_SIZE          "US Letter"
71
72 #define DEFAULT_FONT_FAMILY        "Helvetica"
73 #define DEFAULT_FONT_SIZE          14.0
74 #define DEFAULT_FONT_WEIGHT_STRING gl_util_weight_to_string (GNOME_FONT_BOOK)
75 #define DEFAULT_FONT_ITALIC_FLAG   FALSE
76 #define DEFAULT_TEXT_JUST_STRING   gl_util_just_to_string (GTK_JUSTIFY_LEFT)
77 #define DEFAULT_TEXT_COLOR         GNOME_CANVAS_COLOR (0,0,0)
78
79 #define DEFAULT_LINE_WIDTH         1.0
80 #define DEFAULT_LINE_COLOR         GNOME_CANVAS_COLOR_A (0, 0, 0, 255)
81
82 #define DEFAULT_FILL_COLOR         GNOME_CANVAS_COLOR_A (0, 255, 0, 255)
83
84 /*========================================================*/
85 /* Private types.                                         */
86 /*========================================================*/
87
88
89 /*========================================================*/
90 /* Private globals.                                       */
91 /*========================================================*/
92 static GConfClient *gconf_client = NULL;
93
94 /*========================================================*/
95 /* Private function prototypes.                           */
96 /*========================================================*/
97
98 static void notify_cb (GConfClient *client,
99                        guint cnxn_id,
100                        GConfEntry *entry,
101                        gpointer user_data);
102
103 static gchar *get_string (GConfClient* client, const gchar* key, const gchar* def);
104 static gboolean get_bool (GConfClient* client, const gchar* key, gboolean def);
105 static gint get_int (GConfClient* client, const gchar* key, gint def);
106 static gdouble get_float (GConfClient* client, const gchar* key, gdouble def);
107
108 static glPrefsUnits string_to_units (const gchar *string);
109 static const gchar *units_to_string (glPrefsUnits units);
110
111
112 \f
113 /*****************************************************************************/
114 /* Initialize preferences module.                                            */
115 /*****************************************************************************/
116 void 
117 gl_prefs_init (void)
118 {
119         gl_debug (DEBUG_PREFS, "");
120
121         gconf_client = gconf_client_get_default ();
122         
123         g_return_if_fail (gconf_client != NULL);
124
125         gconf_client_add_dir (gconf_client,
126                               BASE_KEY,
127                               GCONF_CLIENT_PRELOAD_ONELEVEL,
128                               NULL);
129         
130         gconf_client_notify_add (gconf_client,
131                                  BASE_KEY,
132                                  notify_cb,
133                                  NULL, NULL, NULL);
134 }
135
136
137 /*****************************************************************************/
138 /* Save all settings.                                                        */
139 /*****************************************************************************/
140 void 
141 gl_prefs_save_settings (void)
142 {
143         BonoboWindow* active_window = NULL;             
144
145         gl_debug (DEBUG_PREFS, "START");
146         
147         g_return_if_fail (gconf_client != NULL);
148         g_return_if_fail (gl_prefs != NULL);
149
150         /* Units */
151         gconf_client_set_string (gconf_client,
152                                  BASE_KEY PREF_UNITS,
153                                  units_to_string(gl_prefs->units),
154                                  NULL);
155
156         /* Default page size */
157         gconf_client_set_string (gconf_client,
158                                  BASE_KEY PREF_DEFAULT_PAGE_SIZE,
159                                  gl_prefs->default_page_size,
160                                  NULL);
161
162
163         /* Text properties */
164         gconf_client_set_string (gconf_client,
165                                  BASE_KEY PREF_DEFAULT_FONT_FAMILY,
166                                  gl_prefs->default_font_family,
167                                  NULL);
168
169         gconf_client_set_float  (gconf_client,
170                                  BASE_KEY PREF_DEFAULT_FONT_SIZE,
171                                  gl_prefs->default_font_size,
172                                  NULL);
173
174         gconf_client_set_string (gconf_client,
175                                  BASE_KEY PREF_DEFAULT_FONT_WEIGHT,
176                                  gl_util_weight_to_string(gl_prefs->default_font_weight),
177                                  NULL);
178
179         gconf_client_set_int    (gconf_client,
180                                  BASE_KEY PREF_DEFAULT_TEXT_COLOR,
181                                  gl_prefs->default_text_color,
182                                  NULL);
183
184         gconf_client_set_string (gconf_client,
185                                  BASE_KEY PREF_DEFAULT_TEXT_ALIGNMENT,
186                                  gl_util_just_to_string(gl_prefs->default_text_alignment),
187                                  NULL);
188
189
190         /* Line properties */
191         gconf_client_set_float  (gconf_client,
192                                  BASE_KEY PREF_DEFAULT_LINE_WIDTH,
193                                  gl_prefs->default_line_width,
194                                  NULL);
195
196         gconf_client_set_int    (gconf_client,
197                                  BASE_KEY PREF_DEFAULT_LINE_COLOR,
198                                  gl_prefs->default_line_color,
199                                  NULL);
200
201
202         /* Fill properties */
203         gconf_client_set_int    (gconf_client,
204                                  BASE_KEY PREF_DEFAULT_FILL_COLOR,
205                                  gl_prefs->default_fill_color,
206                                  NULL);
207
208
209         /* Toolbar */
210         gconf_client_set_bool (gconf_client,
211                                BASE_KEY PREF_TOOLBAR_VISIBLE,
212                                gl_prefs->toolbar_visible,
213                                NULL);
214
215         gconf_client_set_int (gconf_client,
216                               BASE_KEY PREF_TOOLBAR_BUTTONS_STYLE,
217                               gl_prefs->toolbar_buttons_style,
218                               NULL);
219
220         gconf_client_set_bool (gconf_client,
221                                BASE_KEY PREF_TOOLBAR_VIEW_TOOLTIPS,
222                                gl_prefs->toolbar_view_tooltips,
223                                NULL);
224
225
226         /* MDI */
227         gconf_client_set_int (gconf_client,
228                               BASE_KEY PREF_MDI_MODE,
229                               gl_prefs->mdi_mode,
230                               NULL);
231
232         gconf_client_set_int (gconf_client,
233                               BASE_KEY PREF_TABS_POSITION,
234                               gl_prefs->mdi_tabs_position,
235                               NULL);
236
237
238         /* Recent files */
239         gconf_client_set_int (gconf_client,
240                               BASE_KEY PREF_MAX_RECENTS,
241                               gl_prefs->max_recents,
242                               NULL);
243
244
245         gconf_client_suggest_sync (gconf_client, NULL);
246         
247         gl_debug (DEBUG_PREFS, "END");
248 }
249
250 /*****************************************************************************/
251 /* Load all settings.                                                        */
252 /*****************************************************************************/
253 void
254 gl_prefs_load_settings (void)
255 {
256         gchar *string;
257
258         gl_debug (DEBUG_PREFS, "START");
259         
260         if (gl_prefs == NULL)
261                 gl_prefs = g_new0 (glPreferences, 1);
262
263         if (gconf_client == NULL)
264         {
265                 /* TODO: in any case set default values */
266                 g_warning ("Cannot load settings.");
267                 return;
268         }
269
270
271         /* Units */
272         string =
273                 get_string (gconf_client,
274                             BASE_KEY PREF_UNITS,
275                             DEFAULT_UNITS_STRING);
276         gl_prefs->units = string_to_units( string );
277         g_free( string );
278
279
280         /* Page size */
281         gl_prefs->default_page_size =
282                 get_string (gconf_client,
283                             BASE_KEY PREF_DEFAULT_PAGE_SIZE,
284                             DEFAULT_PAGE_SIZE);
285
286         /* Text properties */
287         gl_prefs->default_font_family =
288                 get_string (gconf_client,
289                             BASE_KEY PREF_DEFAULT_FONT_FAMILY,
290                             DEFAULT_FONT_FAMILY);
291
292         gl_prefs->default_font_size =
293                 get_float (gconf_client,
294                            BASE_KEY PREF_DEFAULT_FONT_SIZE,
295                            DEFAULT_FONT_SIZE);
296
297         string =
298                 get_string (gconf_client,
299                             BASE_KEY PREF_DEFAULT_FONT_WEIGHT,
300                             DEFAULT_FONT_WEIGHT_STRING);
301         gl_prefs->default_font_weight =
302                 gl_util_string_to_weight( string );
303         g_free( string );
304
305         gl_prefs->default_text_color =
306                 get_int (gconf_client,
307                          BASE_KEY PREF_DEFAULT_TEXT_COLOR,
308                          DEFAULT_TEXT_COLOR);
309
310         string =
311                 get_string (gconf_client,
312                             BASE_KEY PREF_DEFAULT_TEXT_ALIGNMENT,
313                             DEFAULT_TEXT_JUST_STRING);
314         gl_prefs->default_font_weight = gl_util_string_to_just( string );
315         g_free( string );
316
317
318         /* Line properties */
319         gl_prefs->default_line_width =
320                 get_float (gconf_client,
321                            BASE_KEY PREF_DEFAULT_LINE_WIDTH,
322                            DEFAULT_LINE_WIDTH);
323         gl_prefs->default_line_color =
324                 get_int (gconf_client,
325                          BASE_KEY PREF_DEFAULT_LINE_COLOR,
326                          DEFAULT_LINE_COLOR);
327
328         /* Fill properties */
329         gl_prefs->default_fill_color =
330                 get_int (gconf_client,
331                          BASE_KEY PREF_DEFAULT_FILL_COLOR,
332                          DEFAULT_FILL_COLOR);
333
334
335         /* User Inferface/Toolbar */
336         gl_prefs->toolbar_visible =
337                 get_bool (gconf_client,
338                           BASE_KEY PREF_TOOLBAR_VISIBLE,
339                           TRUE);
340
341         gl_prefs->toolbar_buttons_style =
342                 get_int (gconf_client,
343                          BASE_KEY PREF_TOOLBAR_BUTTONS_STYLE,
344                          GL_TOOLBAR_SYSTEM);
345
346         gl_prefs->toolbar_view_tooltips =
347                 get_bool (gconf_client,
348                           BASE_KEY PREF_TOOLBAR_VIEW_TOOLTIPS,
349                           TRUE);
350
351
352         /* User Inferface/MDI */                
353         gl_prefs->mdi_mode =
354                 get_int (gconf_client,
355                          BASE_KEY PREF_MDI_MODE,
356                          0);
357
358         gl_prefs->mdi_tabs_position =
359                 get_int (gconf_client,
360                          BASE_KEY PREF_TABS_POSITION,
361                          0);
362
363
364         /* Recent files */
365         gl_prefs->max_recents =
366                 get_int (gconf_client,
367                          BASE_KEY PREF_MAX_RECENTS,
368                          4);
369
370
371         gl_debug (DEBUG_PREFS, "max_recents = %d", gl_prefs->max_recents);
372
373         gl_debug (DEBUG_PREFS, "END");
374 }
375
376 /*---------------------------------------------------------------------------*/
377 /* PRIVATE.  Key changed callback.                                           */
378 /*---------------------------------------------------------------------------*/
379 static void 
380 notify_cb (GConfClient *client,
381            guint cnxn_id,
382            GConfEntry *entry,
383            gpointer user_data)
384 {
385         gl_debug (DEBUG_PREFS, "Key was changed: %s", entry->key);
386 }
387
388 /*---------------------------------------------------------------------------*/
389 /* PRIVATE.  Utilities to get values with defaults.                          */
390 /*---------------------------------------------------------------------------*/
391 static gchar*
392 get_string (GConfClient* client,
393             const gchar* key,
394             const gchar* def)
395 {
396   gchar* val;
397
398   val = gconf_client_get_string (client, key, NULL);
399
400   if (val != NULL) {
401
402       return val;
403
404   } else {
405
406       return def ? g_strdup (def) : NULL;
407
408   }
409 }
410
411 static gboolean
412 get_bool (GConfClient* client,
413           const gchar* key,
414           gboolean def)
415 {
416   GConfValue* val;
417   gboolean retval;
418
419   val = gconf_client_get (client, key, NULL);
420
421   if (val != NULL) {
422
423           if ( val->type == GCONF_VALUE_BOOL ) {
424                   retval = gconf_value_get_bool (val);
425           } else {
426                   retval = def;
427           }
428
429           gconf_value_free (val);
430
431           return retval;
432
433   } else {
434
435       return def;
436
437   }
438 }
439
440 static gint
441 get_int (GConfClient* client,
442          const gchar* key,
443          gint def)
444 {
445   GConfValue* val;
446   gint retval;
447
448   val = gconf_client_get (client, key, NULL);
449
450   if (val != NULL) {
451
452           if ( val->type == GCONF_VALUE_INT) {
453                   retval = gconf_value_get_int(val);
454           } else {
455                   retval = def;
456           }
457
458           gconf_value_free (val);
459
460           return retval;
461
462   } else {
463
464           return def;
465
466   }
467 }
468
469 static gdouble
470 get_float (GConfClient* client,
471            const gchar* key,
472            gdouble def)
473 {
474   GConfValue* val;
475   gdouble retval;
476
477   val = gconf_client_get (client, key, NULL);
478
479   if (val != NULL) {
480
481           if ( val->type == GCONF_VALUE_FLOAT ) {
482                   retval = gconf_value_get_float(val);
483           } else {
484                   retval = def;
485           }
486
487           gconf_value_free (val);
488
489           return retval;
490
491   } else {
492
493           return def;
494
495   }
496 }
497
498 /*---------------------------------------------------------------------------*/
499 /* PRIVATE.  Utilities to deal with units.                                   */
500 /*---------------------------------------------------------------------------*/
501 static glPrefsUnits string_to_units (const gchar *string)
502 {
503         glPrefsUnits units;
504
505         if (g_strcasecmp (string, "Points") == 0) {
506                 units = GL_PREFS_UNITS_PTS;
507         } else if (g_strcasecmp (string, "Inches") == 0) {
508                 units = GL_PREFS_UNITS_INCHES;
509         } else if (g_strcasecmp (string, "Millimeters") == 0) {
510                 units = GL_PREFS_UNITS_MM;
511         } else {
512                 units = GL_PREFS_UNITS_INCHES;
513         }
514
515         return units;
516 }
517
518 static const gchar *units_to_string (glPrefsUnits units)
519 {
520         switch (units) {
521         case GL_PREFS_UNITS_PTS:
522                 return "Points";
523                 break;
524         case GL_PREFS_UNITS_INCHES:
525                 return "Inches";
526                 break;
527         case GL_PREFS_UNITS_MM:
528                 return "Millimeters";
529                 break;
530         default:
531                 return "Inches";
532                 break;
533         }
534 }
535
536
537 \f
538 /****************************************************************************/
539 /* Get string representing desired units.                                   */
540 /****************************************************************************/
541 const gchar *
542 gl_prefs_get_page_size (void)
543 {
544         return (gl_prefs->default_page_size);
545 }
546
547 /****************************************************************************/
548 /* Get desired units.                                                       */
549 /****************************************************************************/
550 glPrefsUnits
551 gl_prefs_get_units (void)
552 {
553         return gl_prefs->units;
554 }
555
556 /****************************************************************************/
557 /* Get desired units per point.                                             */
558 /****************************************************************************/
559 gdouble
560 gl_prefs_get_units_per_point (void)
561 {
562
563         switch (gl_prefs->units) {
564         case GL_PREFS_UNITS_PTS:
565                 return 1.0;     /* points */
566         case GL_PREFS_UNITS_INCHES:
567                 return 1.0 / 72.0;      /* inches */
568         case GL_PREFS_UNITS_MM:
569                 return 0.35277778;      /* mm */
570         default:
571                 g_warning ("Illegal units");    /* Should not happen */
572                 return 1.0;
573         }
574 }
575
576 /****************************************************************************/
577 /* Get precision for desired units.                                         */
578 /****************************************************************************/
579 gint
580 gl_prefs_get_units_precision (void)
581 {
582
583         switch (gl_prefs->units) {
584         case GL_PREFS_UNITS_PTS:
585                 return 1;       /* points */
586         case GL_PREFS_UNITS_INCHES:
587                 return 3;       /* inches */
588         case GL_PREFS_UNITS_MM:
589                 return 1;       /* mm */
590         default:
591                 g_warning ("Illegal units");    /* Should not happen */
592                 return 1.0;
593         }
594 }
595
596 /****************************************************************************/
597 /* Get step size for desired units.                                         */
598 /****************************************************************************/
599 gdouble
600 gl_prefs_get_units_step_size (void)
601 {
602
603         switch (gl_prefs->units) {
604         case GL_PREFS_UNITS_PTS:
605                 return 0.1;     /* points */
606         case GL_PREFS_UNITS_INCHES:
607                 return 0.001;   /* inches */
608         case GL_PREFS_UNITS_MM:
609                 return 0.1;     /* mm */
610         default:
611                 g_warning ("Illegal units");    /* Should not happen */
612                 return 1.0;
613         }
614 }
615
616 /****************************************************************************/
617 /* Get string representing desired units.                                   */
618 /****************************************************************************/
619 const gchar *
620 gl_prefs_get_units_string (void)
621 {
622
623         switch (gl_prefs->units) {
624         case GL_PREFS_UNITS_PTS:
625                 return _("points");
626         case GL_PREFS_UNITS_INCHES:
627                 return _("inches");
628         case GL_PREFS_UNITS_MM:
629                 return _("mm");
630         default:
631                 g_warning ("Illegal units");    /* Should not happen */
632                 return _("points");
633         }
634 }
635