1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
4 * (GLABELS) Label and Business Card Creation program for GNOME
6 * prefs-model.c: Application preferences model module
8 * Copyright (C) 2001-2003 Jim Evins <evins@snaught.com>.
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.
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.
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
26 #include "prefs-model.h"
28 #include <libglabels/paper.h>
36 /*========================================================*/
37 /* Private macros and constants. */
38 /*========================================================*/
41 #define BASE_KEY "/apps/glabels"
43 #define PREF_UNITS "/units"
44 #define PREF_DEFAULT_PAGE_SIZE "/default-page-size"
46 #define PREF_DEFAULT_FONT_FAMILY "/default-font-family"
47 #define PREF_DEFAULT_FONT_SIZE "/default-font-size"
48 #define PREF_DEFAULT_FONT_WEIGHT "/default-font-weight"
49 #define PREF_DEFAULT_FONT_ITALIC_FLAG "/default-font-italic-flag"
50 #define PREF_DEFAULT_TEXT_COLOR "/default-text-color"
51 #define PREF_DEFAULT_TEXT_ALIGNMENT "/default-text-alignment"
52 #define PREF_DEFAULT_TEXT_LINE_SPACING "/default-text-line-spacing"
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_MAIN_TOOLBAR_VISIBLE "/main-toolbar-visible"
60 #define PREF_MAIN_TOOLBAR_BUTTONS_STYLE "/main-toolbar-buttons-style"
61 #define PREF_MAIN_TOOLBAR_VIEW_TOOLTIPS "/main-toolbar-view-tooltips"
63 #define PREF_DRAWING_TOOLBAR_VISIBLE "/drawing-toolbar-visible"
64 #define PREF_DRAWING_TOOLBAR_VIEW_TOOLTIPS "/drawing-toolbar-view-tooltips"
66 #define PREF_PROPERTY_TOOLBAR_VISIBLE "/property-toolbar-visible"
67 #define PREF_PROPERTY_TOOLBAR_VIEW_TOOLTIPS "/property-toolbar-view-tooltips"
69 #define PREF_GRID_VISIBLE "/grid-visible"
70 #define PREF_MARKUP_VISIBLE "/markup-visible"
72 #define PREF_MAX_RECENTS "/max-recents"
75 #define DEFAULT_UNITS_STRING units_to_string (GL_UNITS_INCH)
76 #define DEFAULT_PAGE_SIZE "US-Letter"
78 #define DEFAULT_FONT_FAMILY "Sans"
79 #define DEFAULT_FONT_SIZE 14.0
80 #define DEFAULT_FONT_WEIGHT_STRING gl_util_weight_to_string (PANGO_WEIGHT_NORMAL)
81 #define DEFAULT_FONT_ITALIC_FLAG FALSE
82 #define DEFAULT_TEXT_ALIGN_STRING gl_util_align_to_string (PANGO_ALIGN_LEFT)
83 #define DEFAULT_TEXT_COLOR GL_COLOR (0,0,0)
84 #define DEFAULT_TEXT_LINE_SPACING 1.0
86 #define DEFAULT_LINE_WIDTH 1.0
87 #define DEFAULT_LINE_COLOR GL_COLOR_A (0, 0, 0, 255)
89 #define DEFAULT_FILL_COLOR GL_COLOR_A (0, 255, 0, 255)
91 /*========================================================*/
93 /*========================================================*/
102 /*========================================================*/
103 /* Private globals. */
104 /*========================================================*/
106 static guint signals[LAST_SIGNAL] = {0};
108 /*========================================================*/
109 /* Private function prototypes. */
110 /*========================================================*/
112 static void gl_prefs_model_finalize (GObject *object);
114 static void notify_cb (GConfClient *client,
117 glPrefsModel *prefs_model);
119 static gchar *get_string (GConfClient *client,
123 static gboolean get_bool (GConfClient *client,
127 static gint get_int (GConfClient *client,
131 static gdouble get_float (GConfClient *client,
135 static glUnitsType string_to_units (const gchar *string);
136 static const gchar *units_to_string (glUnitsType units);
140 /*****************************************************************************/
141 /* Boilerplate object stuff. */
142 /*****************************************************************************/
143 G_DEFINE_TYPE (glPrefsModel, gl_prefs_model, G_TYPE_OBJECT);
146 gl_prefs_model_class_init (glPrefsModelClass *class)
148 GObjectClass *object_class = G_OBJECT_CLASS (class);
150 gl_debug (DEBUG_PREFS, "START");
152 gl_prefs_model_parent_class = g_type_class_peek_parent (class);
154 object_class->finalize = gl_prefs_model_finalize;
157 g_signal_new ("changed",
158 G_OBJECT_CLASS_TYPE (object_class),
160 G_STRUCT_OFFSET (glPrefsModelClass, changed),
162 gl_marshal_VOID__VOID,
166 gl_debug (DEBUG_PREFS, "END");
170 gl_prefs_model_init (glPrefsModel *prefs_model)
172 gl_debug (DEBUG_PREFS, "START");
174 prefs_model->gconf_client = gconf_client_get_default ();
176 g_return_if_fail (prefs_model->gconf_client != NULL);
178 gconf_client_add_dir (prefs_model->gconf_client,
180 GCONF_CLIENT_PRELOAD_ONELEVEL,
183 gconf_client_notify_add (prefs_model->gconf_client,
185 (GConfClientNotifyFunc)notify_cb, prefs_model,
188 gl_debug (DEBUG_PREFS, "END");
192 gl_prefs_model_finalize (GObject *object)
194 glPrefsModel *prefs_model = GL_PREFS_MODEL (object);
196 gl_debug (DEBUG_PREFS, "START");
198 g_return_if_fail (object && GL_IS_PREFS_MODEL (object));
200 g_object_unref (G_OBJECT(prefs_model->gconf_client));
201 g_free (prefs_model->default_page_size);
202 g_free (prefs_model->default_font_family);
204 G_OBJECT_CLASS (gl_prefs_model_parent_class)->finalize (object);
206 gl_debug (DEBUG_PREFS, "END");
209 /*****************************************************************************/
210 /* New prefs_model object. */
211 /*****************************************************************************/
213 gl_prefs_model_new (void)
215 glPrefsModel *prefs_model;
217 gl_debug (DEBUG_PREFS, "START");
219 prefs_model = GL_PREFS_MODEL (g_object_new (gl_prefs_model_get_type(), NULL));
221 gl_debug (DEBUG_PREFS, "END");
228 /*****************************************************************************/
229 /* Save all settings. */
230 /*****************************************************************************/
232 gl_prefs_model_save_settings (glPrefsModel *prefs_model)
234 gl_debug (DEBUG_PREFS, "START");
236 g_return_if_fail (prefs_model && GL_IS_PREFS_MODEL(prefs_model));
237 g_return_if_fail (prefs_model->gconf_client != NULL);
239 /* We are saving settings because presumably some of them have been changed. */
240 g_signal_emit (G_OBJECT(prefs_model), signals[CHANGED], 0);
243 gconf_client_set_string (prefs_model->gconf_client,
245 units_to_string(prefs_model->units),
248 /* Default page size */
249 gconf_client_set_string (prefs_model->gconf_client,
250 BASE_KEY PREF_DEFAULT_PAGE_SIZE,
251 prefs_model->default_page_size,
255 /* Text properties */
256 gconf_client_set_string (prefs_model->gconf_client,
257 BASE_KEY PREF_DEFAULT_FONT_FAMILY,
258 prefs_model->default_font_family,
261 gconf_client_set_float (prefs_model->gconf_client,
262 BASE_KEY PREF_DEFAULT_FONT_SIZE,
263 prefs_model->default_font_size,
266 gconf_client_set_string (prefs_model->gconf_client,
267 BASE_KEY PREF_DEFAULT_FONT_WEIGHT,
268 gl_util_weight_to_string(prefs_model->default_font_weight),
271 gconf_client_set_int (prefs_model->gconf_client,
272 BASE_KEY PREF_DEFAULT_TEXT_COLOR,
273 prefs_model->default_text_color,
276 gconf_client_set_string (prefs_model->gconf_client,
277 BASE_KEY PREF_DEFAULT_TEXT_ALIGNMENT,
278 gl_util_align_to_string(prefs_model->default_text_alignment),
281 gconf_client_set_float (prefs_model->gconf_client,
282 BASE_KEY PREF_DEFAULT_TEXT_LINE_SPACING,
283 prefs_model->default_text_line_spacing,
286 /* Line properties */
287 gconf_client_set_float (prefs_model->gconf_client,
288 BASE_KEY PREF_DEFAULT_LINE_WIDTH,
289 prefs_model->default_line_width,
292 gconf_client_set_int (prefs_model->gconf_client,
293 BASE_KEY PREF_DEFAULT_LINE_COLOR,
294 prefs_model->default_line_color,
298 /* Fill properties */
299 gconf_client_set_int (prefs_model->gconf_client,
300 BASE_KEY PREF_DEFAULT_FILL_COLOR,
301 prefs_model->default_fill_color,
306 gconf_client_set_bool (prefs_model->gconf_client,
307 BASE_KEY PREF_MAIN_TOOLBAR_VISIBLE,
308 prefs_model->main_toolbar_visible,
311 gconf_client_set_int (prefs_model->gconf_client,
312 BASE_KEY PREF_MAIN_TOOLBAR_BUTTONS_STYLE,
313 prefs_model->main_toolbar_buttons_style,
316 gconf_client_set_bool (prefs_model->gconf_client,
317 BASE_KEY PREF_MAIN_TOOLBAR_VIEW_TOOLTIPS,
318 prefs_model->main_toolbar_view_tooltips,
321 /* Drawing Toolbar */
322 gconf_client_set_bool (prefs_model->gconf_client,
323 BASE_KEY PREF_DRAWING_TOOLBAR_VISIBLE,
324 prefs_model->drawing_toolbar_visible,
327 gconf_client_set_bool (prefs_model->gconf_client,
328 BASE_KEY PREF_DRAWING_TOOLBAR_VIEW_TOOLTIPS,
329 prefs_model->drawing_toolbar_view_tooltips,
332 /* Property Toolbar */
333 gconf_client_set_bool (prefs_model->gconf_client,
334 BASE_KEY PREF_PROPERTY_TOOLBAR_VISIBLE,
335 prefs_model->property_toolbar_visible,
338 gconf_client_set_bool (prefs_model->gconf_client,
339 BASE_KEY PREF_PROPERTY_TOOLBAR_VIEW_TOOLTIPS,
340 prefs_model->property_toolbar_view_tooltips,
343 /* View properties */
344 gconf_client_set_bool (prefs_model->gconf_client,
345 BASE_KEY PREF_GRID_VISIBLE,
346 prefs_model->grid_visible,
349 gconf_client_set_bool (prefs_model->gconf_client,
350 BASE_KEY PREF_MARKUP_VISIBLE,
351 prefs_model->markup_visible,
355 gconf_client_set_int (prefs_model->gconf_client,
356 BASE_KEY PREF_MAX_RECENTS,
357 prefs_model->max_recents,
361 gconf_client_suggest_sync (prefs_model->gconf_client, NULL);
363 gl_debug (DEBUG_PREFS, "END");
366 /*****************************************************************************/
367 /* Load all settings. */
368 /*****************************************************************************/
370 gl_prefs_model_load_settings (glPrefsModel *prefs_model)
375 gl_debug (DEBUG_PREFS, "START");
377 g_return_if_fail (prefs_model && GL_IS_PREFS_MODEL(prefs_model));
378 g_return_if_fail (prefs_model->gconf_client != NULL);
382 get_string (prefs_model->gconf_client,
384 DEFAULT_UNITS_STRING);
385 prefs_model->units = string_to_units( string );
390 prefs_model->default_page_size =
391 get_string (prefs_model->gconf_client,
392 BASE_KEY PREF_DEFAULT_PAGE_SIZE,
395 /* Text properties */
396 prefs_model->default_font_family =
397 get_string (prefs_model->gconf_client,
398 BASE_KEY PREF_DEFAULT_FONT_FAMILY,
399 DEFAULT_FONT_FAMILY);
401 prefs_model->default_font_size =
402 get_float (prefs_model->gconf_client,
403 BASE_KEY PREF_DEFAULT_FONT_SIZE,
407 get_string (prefs_model->gconf_client,
408 BASE_KEY PREF_DEFAULT_FONT_WEIGHT,
409 DEFAULT_FONT_WEIGHT_STRING);
410 prefs_model->default_font_weight = gl_util_string_to_weight( string );
413 prefs_model->default_text_color =
414 get_int (prefs_model->gconf_client,
415 BASE_KEY PREF_DEFAULT_TEXT_COLOR,
419 get_string (prefs_model->gconf_client,
420 BASE_KEY PREF_DEFAULT_TEXT_ALIGNMENT,
421 DEFAULT_TEXT_ALIGN_STRING);
422 prefs_model->default_text_alignment = gl_util_string_to_align( string );
425 prefs_model->default_text_line_spacing =
426 get_float (prefs_model->gconf_client,
427 BASE_KEY PREF_DEFAULT_TEXT_LINE_SPACING,
428 DEFAULT_TEXT_LINE_SPACING);
430 gl_debug (DEBUG_PREFS, "text_line_spacing = %f", prefs_model->default_text_line_spacing);
432 /* Line properties */
433 prefs_model->default_line_width =
434 get_float (prefs_model->gconf_client,
435 BASE_KEY PREF_DEFAULT_LINE_WIDTH,
437 prefs_model->default_line_color =
438 get_int (prefs_model->gconf_client,
439 BASE_KEY PREF_DEFAULT_LINE_COLOR,
442 /* Fill properties */
443 prefs_model->default_fill_color =
444 get_int (prefs_model->gconf_client,
445 BASE_KEY PREF_DEFAULT_FILL_COLOR,
449 /* User Inferface/Main Toolbar */
450 prefs_model->main_toolbar_visible =
451 get_bool (prefs_model->gconf_client,
452 BASE_KEY PREF_MAIN_TOOLBAR_VISIBLE,
455 prefs_model->main_toolbar_buttons_style =
456 get_int (prefs_model->gconf_client,
457 BASE_KEY PREF_MAIN_TOOLBAR_BUTTONS_STYLE,
460 prefs_model->main_toolbar_view_tooltips =
461 get_bool (prefs_model->gconf_client,
462 BASE_KEY PREF_MAIN_TOOLBAR_VIEW_TOOLTIPS,
465 /* User Inferface/Drawing Toolbar */
466 prefs_model->drawing_toolbar_visible =
467 get_bool (prefs_model->gconf_client,
468 BASE_KEY PREF_DRAWING_TOOLBAR_VISIBLE,
471 prefs_model->drawing_toolbar_view_tooltips =
472 get_bool (prefs_model->gconf_client,
473 BASE_KEY PREF_DRAWING_TOOLBAR_VIEW_TOOLTIPS,
476 /* User Inferface/Property Toolbar */
477 prefs_model->property_toolbar_visible =
478 get_bool (prefs_model->gconf_client,
479 BASE_KEY PREF_PROPERTY_TOOLBAR_VISIBLE,
482 prefs_model->property_toolbar_view_tooltips =
483 get_bool (prefs_model->gconf_client,
484 BASE_KEY PREF_PROPERTY_TOOLBAR_VIEW_TOOLTIPS,
488 /* View properties */
489 prefs_model->grid_visible =
490 get_bool (prefs_model->gconf_client,
491 BASE_KEY PREF_GRID_VISIBLE,
494 prefs_model->markup_visible =
495 get_bool (prefs_model->gconf_client,
496 BASE_KEY PREF_MARKUP_VISIBLE,
500 prefs_model->max_recents =
501 get_int (prefs_model->gconf_client,
502 BASE_KEY PREF_MAX_RECENTS,
506 /* Proof read the default page size -- it must be a valid id. */
507 /* (For compatability with older versions.) */
508 /* Note: paper module must be initialized for this to work. */
509 paper = gl_paper_from_id (prefs_model->default_page_size);
510 if ( paper == NULL ) {
511 prefs_model->default_page_size = g_strdup (DEFAULT_PAGE_SIZE);
513 gl_paper_free (paper);
517 gl_debug (DEBUG_PREFS, "max_recents = %d", prefs_model->max_recents);
520 g_signal_emit (G_OBJECT(prefs_model), signals[CHANGED], 0);
522 gl_debug (DEBUG_PREFS, "END");
525 /*---------------------------------------------------------------------------*/
526 /* PRIVATE. Key changed callback. */
527 /*---------------------------------------------------------------------------*/
529 notify_cb (GConfClient *client,
532 glPrefsModel *prefs_model)
534 gl_debug (DEBUG_PREFS, "Key was changed: %s", entry->key);
536 gl_prefs_model_load_settings (prefs_model);
539 /*---------------------------------------------------------------------------*/
540 /* PRIVATE. Utilities to get values with defaults. */
541 /*---------------------------------------------------------------------------*/
543 get_string (GConfClient *client,
549 val = gconf_client_get_string (client, key, NULL);
557 return def ? g_strdup (def) : NULL;
563 get_bool (GConfClient *client,
570 val = gconf_client_get (client, key, NULL);
574 if ( val->type == GCONF_VALUE_BOOL ) {
575 retval = gconf_value_get_bool (val);
580 gconf_value_free (val);
592 get_int (GConfClient *client,
599 val = gconf_client_get (client, key, NULL);
603 if ( val->type == GCONF_VALUE_INT) {
604 retval = gconf_value_get_int(val);
609 gconf_value_free (val);
621 get_float (GConfClient *client,
628 val = gconf_client_get (client, key, NULL);
632 if ( val->type == GCONF_VALUE_FLOAT ) {
633 retval = gconf_value_get_float(val);
638 gconf_value_free (val);
648 /*---------------------------------------------------------------------------*/
649 /* PRIVATE. Utilities to deal with units. */
650 /*---------------------------------------------------------------------------*/
652 string_to_units (const gchar *string)
656 if (g_strcasecmp (string, "Points") == 0) {
657 units = GL_UNITS_POINT;
658 } else if (g_strcasecmp (string, "Inches") == 0) {
659 units = GL_UNITS_INCH;
660 } else if (g_strcasecmp (string, "Millimeters") == 0) {
663 units = GL_UNITS_INCH;
670 gchar *units_to_string (glUnitsType units)
680 return "Millimeters";