2 * (GLABELS) Label and Business Card Creation program for GNOME
4 * prefs.h: Application preferences module header file
6 * Copyright (C) 2001-2002 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
23 #include <libgnome/libgnome.h>
24 #include <libgnomeui/libgnomeui.h>
26 #include <bonobo-mdi.h>
27 #include <gconf/gconf-client.h>
35 glPreferences *gl_prefs = NULL;
37 /*========================================================*/
38 /* Private macros and constants. */
39 /*========================================================*/
42 #define BASE_KEY "/apps/glabels"
44 #define PREF_UNITS "/units"
45 #define PREF_DEFAULT_PAGE_SIZE "/default-page-size"
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"
54 #define PREF_DEFAULT_LINE_WIDTH "/default-line-width"
55 #define PREF_DEFAULT_LINE_COLOR "/default-line-color"
57 #define PREF_DEFAULT_FILL_COLOR "/default-fill-color"
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"
63 #define PREF_MDI_MODE "/mdi-mode"
64 #define PREF_TABS_POSITION "/mdi-tabs-position"
66 #define PREF_MAX_RECENTS "/max-recents"
69 #define DEFAULT_UNITS_STRING units_to_string (GL_PREFS_UNITS_INCHES)
70 #define DEFAULT_PAGE_SIZE "US Letter"
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)
79 #define DEFAULT_LINE_WIDTH 1.0
80 #define DEFAULT_LINE_COLOR GNOME_CANVAS_COLOR_A (0, 0, 0, 255)
82 #define DEFAULT_FILL_COLOR GNOME_CANVAS_COLOR_A (0, 255, 0, 255)
84 /*========================================================*/
86 /*========================================================*/
89 /*========================================================*/
90 /* Private globals. */
91 /*========================================================*/
92 static GConfClient *gconf_client = NULL;
94 /*========================================================*/
95 /* Private function prototypes. */
96 /*========================================================*/
98 static void notify_cb (GConfClient *client,
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);
108 static glPrefsUnits string_to_units (const gchar *string);
109 static const gchar *units_to_string (glPrefsUnits units);
113 /*****************************************************************************/
114 /* Initialize preferences module. */
115 /*****************************************************************************/
119 gl_debug (DEBUG_PREFS, "");
121 gconf_client = gconf_client_get_default ();
123 g_return_if_fail (gconf_client != NULL);
125 gconf_client_add_dir (gconf_client,
127 GCONF_CLIENT_PRELOAD_ONELEVEL,
130 gconf_client_notify_add (gconf_client,
137 /*****************************************************************************/
138 /* Save all settings. */
139 /*****************************************************************************/
141 gl_prefs_save_settings (void)
143 BonoboWindow* active_window = NULL;
145 gl_debug (DEBUG_PREFS, "START");
147 g_return_if_fail (gconf_client != NULL);
148 g_return_if_fail (gl_prefs != NULL);
151 gconf_client_set_string (gconf_client,
153 units_to_string(gl_prefs->units),
156 /* Default page size */
157 gconf_client_set_string (gconf_client,
158 BASE_KEY PREF_DEFAULT_PAGE_SIZE,
159 gl_prefs->default_page_size,
163 /* Text properties */
164 gconf_client_set_string (gconf_client,
165 BASE_KEY PREF_DEFAULT_FONT_FAMILY,
166 gl_prefs->default_font_family,
169 gconf_client_set_float (gconf_client,
170 BASE_KEY PREF_DEFAULT_FONT_SIZE,
171 gl_prefs->default_font_size,
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),
179 gconf_client_set_int (gconf_client,
180 BASE_KEY PREF_DEFAULT_TEXT_COLOR,
181 gl_prefs->default_text_color,
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),
190 /* Line properties */
191 gconf_client_set_float (gconf_client,
192 BASE_KEY PREF_DEFAULT_LINE_WIDTH,
193 gl_prefs->default_line_width,
196 gconf_client_set_int (gconf_client,
197 BASE_KEY PREF_DEFAULT_LINE_COLOR,
198 gl_prefs->default_line_color,
202 /* Fill properties */
203 gconf_client_set_int (gconf_client,
204 BASE_KEY PREF_DEFAULT_FILL_COLOR,
205 gl_prefs->default_fill_color,
210 gconf_client_set_bool (gconf_client,
211 BASE_KEY PREF_TOOLBAR_VISIBLE,
212 gl_prefs->toolbar_visible,
215 gconf_client_set_int (gconf_client,
216 BASE_KEY PREF_TOOLBAR_BUTTONS_STYLE,
217 gl_prefs->toolbar_buttons_style,
220 gconf_client_set_bool (gconf_client,
221 BASE_KEY PREF_TOOLBAR_VIEW_TOOLTIPS,
222 gl_prefs->toolbar_view_tooltips,
227 gconf_client_set_int (gconf_client,
228 BASE_KEY PREF_MDI_MODE,
232 gconf_client_set_int (gconf_client,
233 BASE_KEY PREF_TABS_POSITION,
234 gl_prefs->mdi_tabs_position,
239 gconf_client_set_int (gconf_client,
240 BASE_KEY PREF_MAX_RECENTS,
241 gl_prefs->max_recents,
245 gconf_client_suggest_sync (gconf_client, NULL);
247 gl_debug (DEBUG_PREFS, "END");
250 /*****************************************************************************/
251 /* Load all settings. */
252 /*****************************************************************************/
254 gl_prefs_load_settings (void)
258 gl_debug (DEBUG_PREFS, "START");
260 if (gl_prefs == NULL)
261 gl_prefs = g_new0 (glPreferences, 1);
263 if (gconf_client == NULL)
265 /* TODO: in any case set default values */
266 g_warning ("Cannot load settings.");
273 get_string (gconf_client,
275 DEFAULT_UNITS_STRING);
276 gl_prefs->units = string_to_units( string );
281 gl_prefs->default_page_size =
282 get_string (gconf_client,
283 BASE_KEY PREF_DEFAULT_PAGE_SIZE,
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);
292 gl_prefs->default_font_size =
293 get_float (gconf_client,
294 BASE_KEY PREF_DEFAULT_FONT_SIZE,
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 );
305 gl_prefs->default_text_color =
306 get_int (gconf_client,
307 BASE_KEY PREF_DEFAULT_TEXT_COLOR,
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 );
318 /* Line properties */
319 gl_prefs->default_line_width =
320 get_float (gconf_client,
321 BASE_KEY PREF_DEFAULT_LINE_WIDTH,
323 gl_prefs->default_line_color =
324 get_int (gconf_client,
325 BASE_KEY PREF_DEFAULT_LINE_COLOR,
328 /* Fill properties */
329 gl_prefs->default_fill_color =
330 get_int (gconf_client,
331 BASE_KEY PREF_DEFAULT_FILL_COLOR,
335 /* User Inferface/Toolbar */
336 gl_prefs->toolbar_visible =
337 get_bool (gconf_client,
338 BASE_KEY PREF_TOOLBAR_VISIBLE,
341 gl_prefs->toolbar_buttons_style =
342 get_int (gconf_client,
343 BASE_KEY PREF_TOOLBAR_BUTTONS_STYLE,
346 gl_prefs->toolbar_view_tooltips =
347 get_bool (gconf_client,
348 BASE_KEY PREF_TOOLBAR_VIEW_TOOLTIPS,
352 /* User Inferface/MDI */
354 get_int (gconf_client,
355 BASE_KEY PREF_MDI_MODE,
358 gl_prefs->mdi_tabs_position =
359 get_int (gconf_client,
360 BASE_KEY PREF_TABS_POSITION,
365 gl_prefs->max_recents =
366 get_int (gconf_client,
367 BASE_KEY PREF_MAX_RECENTS,
371 gl_debug (DEBUG_PREFS, "max_recents = %d", gl_prefs->max_recents);
373 gl_debug (DEBUG_PREFS, "END");
376 /*---------------------------------------------------------------------------*/
377 /* PRIVATE. Key changed callback. */
378 /*---------------------------------------------------------------------------*/
380 notify_cb (GConfClient *client,
385 gl_debug (DEBUG_PREFS, "Key was changed: %s", entry->key);
388 /*---------------------------------------------------------------------------*/
389 /* PRIVATE. Utilities to get values with defaults. */
390 /*---------------------------------------------------------------------------*/
392 get_string (GConfClient* client,
398 val = gconf_client_get_string (client, key, NULL);
406 return def ? g_strdup (def) : NULL;
412 get_bool (GConfClient* client,
419 val = gconf_client_get (client, key, NULL);
423 if ( val->type == GCONF_VALUE_BOOL ) {
424 retval = gconf_value_get_bool (val);
429 gconf_value_free (val);
441 get_int (GConfClient* client,
448 val = gconf_client_get (client, key, NULL);
452 if ( val->type == GCONF_VALUE_INT) {
453 retval = gconf_value_get_int(val);
458 gconf_value_free (val);
470 get_float (GConfClient* client,
477 val = gconf_client_get (client, key, NULL);
481 if ( val->type == GCONF_VALUE_FLOAT ) {
482 retval = gconf_value_get_float(val);
487 gconf_value_free (val);
498 /*---------------------------------------------------------------------------*/
499 /* PRIVATE. Utilities to deal with units. */
500 /*---------------------------------------------------------------------------*/
501 static glPrefsUnits string_to_units (const gchar *string)
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;
512 units = GL_PREFS_UNITS_INCHES;
518 static const gchar *units_to_string (glPrefsUnits units)
521 case GL_PREFS_UNITS_PTS:
524 case GL_PREFS_UNITS_INCHES:
527 case GL_PREFS_UNITS_MM:
528 return "Millimeters";
538 /****************************************************************************/
539 /* Get string representing desired units. */
540 /****************************************************************************/
542 gl_prefs_get_page_size (void)
544 return (gl_prefs->default_page_size);
547 /****************************************************************************/
548 /* Get desired units. */
549 /****************************************************************************/
551 gl_prefs_get_units (void)
553 return gl_prefs->units;
556 /****************************************************************************/
557 /* Get desired units per point. */
558 /****************************************************************************/
560 gl_prefs_get_units_per_point (void)
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 */
571 g_warning ("Illegal units"); /* Should not happen */
576 /****************************************************************************/
577 /* Get precision for desired units. */
578 /****************************************************************************/
580 gl_prefs_get_units_precision (void)
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:
591 g_warning ("Illegal units"); /* Should not happen */
596 /****************************************************************************/
597 /* Get step size for desired units. */
598 /****************************************************************************/
600 gl_prefs_get_units_step_size (void)
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:
611 g_warning ("Illegal units"); /* Should not happen */
616 /****************************************************************************/
617 /* Get string representing desired units. */
618 /****************************************************************************/
620 gl_prefs_get_units_string (void)
623 switch (gl_prefs->units) {
624 case GL_PREFS_UNITS_PTS:
626 case GL_PREFS_UNITS_INCHES:
628 case GL_PREFS_UNITS_MM:
631 g_warning ("Illegal units"); /* Should not happen */