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/db.h>
29 #include <libglabels/xml.h>
30 #include <gtk/gtkpapersize.h>
38 /*========================================================*/
39 /* Private macros and constants. */
40 /*========================================================*/
43 #define BASE_KEY "/apps/glabels"
45 #define PREF_UNITS "/units"
46 #define PREF_DEFAULT_PAGE_SIZE "/default-page-size"
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"
56 #define PREF_DEFAULT_LINE_WIDTH "/default-line-width"
57 #define PREF_DEFAULT_LINE_COLOR "/default-line-color"
59 #define PREF_DEFAULT_FILL_COLOR "/default-fill-color"
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"
65 #define PREF_DRAWING_TOOLBAR_VISIBLE "/drawing-toolbar-visible"
66 #define PREF_DRAWING_TOOLBAR_VIEW_TOOLTIPS "/drawing-toolbar-view-tooltips"
68 #define PREF_PROPERTY_TOOLBAR_VISIBLE "/property-toolbar-visible"
69 #define PREF_PROPERTY_TOOLBAR_VIEW_TOOLTIPS "/property-toolbar-view-tooltips"
71 #define PREF_GRID_VISIBLE "/grid-visible"
72 #define PREF_MARKUP_VISIBLE "/markup-visible"
74 #define PREF_MAX_RECENTS "/max-recents"
76 #define PREF_RECENT_TEMPLATES "/recent-templates"
77 #define PREF_MAX_RECENT_TEMPLATES "/max-recent-templates"
80 #define DEFAULT_UNITS_STRING_US units_to_string (LGL_UNITS_INCH)
81 #define DEFAULT_PAGE_SIZE_US "US-Letter"
83 #define DEFAULT_UNITS_STRING_METRIC units_to_string (LGL_UNITS_MM)
84 #define DEFAULT_PAGE_SIZE_METRIC "A4"
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
94 #define DEFAULT_LINE_WIDTH 1.0
95 #define DEFAULT_LINE_COLOR GL_COLOR_A (0, 0, 0, 255)
97 #define DEFAULT_FILL_COLOR GL_COLOR_A (0, 255, 0, 255)
99 /*========================================================*/
101 /*========================================================*/
110 /*========================================================*/
111 /* Private globals. */
112 /*========================================================*/
114 static guint signals[LAST_SIGNAL] = {0};
116 /*========================================================*/
117 /* Private function prototypes. */
118 /*========================================================*/
120 static void gl_prefs_model_finalize (GObject *object);
122 static void notify_cb (GConfClient *client,
125 glPrefsModel *prefs_model);
127 static gchar *get_string (GConfClient *client,
131 static gboolean get_bool (GConfClient *client,
135 static gint get_int (GConfClient *client,
139 static gdouble get_float (GConfClient *client,
143 static lglUnitsType string_to_units (const gchar *string);
144 static const gchar *units_to_string (lglUnitsType units);
148 /*****************************************************************************/
149 /* Boilerplate object stuff. */
150 /*****************************************************************************/
151 G_DEFINE_TYPE (glPrefsModel, gl_prefs_model, G_TYPE_OBJECT);
154 gl_prefs_model_class_init (glPrefsModelClass *class)
156 GObjectClass *object_class = G_OBJECT_CLASS (class);
158 gl_debug (DEBUG_PREFS, "START");
160 gl_prefs_model_parent_class = g_type_class_peek_parent (class);
162 object_class->finalize = gl_prefs_model_finalize;
165 g_signal_new ("changed",
166 G_OBJECT_CLASS_TYPE (object_class),
168 G_STRUCT_OFFSET (glPrefsModelClass, changed),
170 gl_marshal_VOID__VOID,
174 gl_debug (DEBUG_PREFS, "END");
178 gl_prefs_model_init (glPrefsModel *prefs_model)
180 gl_debug (DEBUG_PREFS, "START");
182 prefs_model->gconf_client = gconf_client_get_default ();
184 g_return_if_fail (prefs_model->gconf_client != NULL);
186 gconf_client_add_dir (prefs_model->gconf_client,
188 GCONF_CLIENT_PRELOAD_ONELEVEL,
191 gconf_client_notify_add (prefs_model->gconf_client,
193 (GConfClientNotifyFunc)notify_cb, prefs_model,
196 gl_debug (DEBUG_PREFS, "END");
200 gl_prefs_model_finalize (GObject *object)
202 glPrefsModel *prefs_model = GL_PREFS_MODEL (object);
204 gl_debug (DEBUG_PREFS, "START");
206 g_return_if_fail (object && GL_IS_PREFS_MODEL (object));
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);
212 G_OBJECT_CLASS (gl_prefs_model_parent_class)->finalize (object);
214 gl_debug (DEBUG_PREFS, "END");
217 /*****************************************************************************/
218 /* New prefs_model object. */
219 /*****************************************************************************/
221 gl_prefs_model_new (void)
223 glPrefsModel *prefs_model;
225 gl_debug (DEBUG_PREFS, "START");
227 prefs_model = GL_PREFS_MODEL (g_object_new (gl_prefs_model_get_type(), NULL));
229 gl_debug (DEBUG_PREFS, "END");
236 /*****************************************************************************/
237 /* Save all settings. */
238 /*****************************************************************************/
240 gl_prefs_model_save_settings (glPrefsModel *prefs_model)
242 gl_debug (DEBUG_PREFS, "START");
244 g_return_if_fail (prefs_model && GL_IS_PREFS_MODEL(prefs_model));
245 g_return_if_fail (prefs_model->gconf_client != NULL);
247 /* We are saving settings because presumably some of them have been changed. */
248 g_signal_emit (G_OBJECT(prefs_model), signals[CHANGED], 0);
251 gconf_client_set_string (prefs_model->gconf_client,
253 units_to_string(prefs_model->units),
255 lgl_xml_set_default_units (prefs_model->units);
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,
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,
270 gconf_client_set_float (prefs_model->gconf_client,
271 BASE_KEY PREF_DEFAULT_FONT_SIZE,
272 prefs_model->default_font_size,
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),
280 gconf_client_set_int (prefs_model->gconf_client,
281 BASE_KEY PREF_DEFAULT_TEXT_COLOR,
282 prefs_model->default_text_color,
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),
290 gconf_client_set_float (prefs_model->gconf_client,
291 BASE_KEY PREF_DEFAULT_TEXT_LINE_SPACING,
292 prefs_model->default_text_line_spacing,
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,
301 gconf_client_set_int (prefs_model->gconf_client,
302 BASE_KEY PREF_DEFAULT_LINE_COLOR,
303 prefs_model->default_line_color,
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,
315 gconf_client_set_bool (prefs_model->gconf_client,
316 BASE_KEY PREF_MAIN_TOOLBAR_VISIBLE,
317 prefs_model->main_toolbar_visible,
320 gconf_client_set_int (prefs_model->gconf_client,
321 BASE_KEY PREF_MAIN_TOOLBAR_BUTTONS_STYLE,
322 prefs_model->main_toolbar_buttons_style,
325 gconf_client_set_bool (prefs_model->gconf_client,
326 BASE_KEY PREF_MAIN_TOOLBAR_VIEW_TOOLTIPS,
327 prefs_model->main_toolbar_view_tooltips,
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,
336 gconf_client_set_bool (prefs_model->gconf_client,
337 BASE_KEY PREF_DRAWING_TOOLBAR_VIEW_TOOLTIPS,
338 prefs_model->drawing_toolbar_view_tooltips,
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,
347 gconf_client_set_bool (prefs_model->gconf_client,
348 BASE_KEY PREF_PROPERTY_TOOLBAR_VIEW_TOOLTIPS,
349 prefs_model->property_toolbar_view_tooltips,
352 /* View properties */
353 gconf_client_set_bool (prefs_model->gconf_client,
354 BASE_KEY PREF_GRID_VISIBLE,
355 prefs_model->grid_visible,
358 gconf_client_set_bool (prefs_model->gconf_client,
359 BASE_KEY PREF_MARKUP_VISIBLE,
360 prefs_model->markup_visible,
364 gconf_client_set_int (prefs_model->gconf_client,
365 BASE_KEY PREF_MAX_RECENTS,
366 prefs_model->max_recents,
369 /* Recent templates */
370 gconf_client_set_list (prefs_model->gconf_client,
371 BASE_KEY PREF_RECENT_TEMPLATES,
373 prefs_model->recent_templates,
375 gconf_client_set_int (prefs_model->gconf_client,
376 BASE_KEY PREF_MAX_RECENT_TEMPLATES,
377 prefs_model->max_recent_templates,
381 gconf_client_suggest_sync (prefs_model->gconf_client, NULL);
383 gl_debug (DEBUG_PREFS, "END");
386 /*****************************************************************************/
387 /* Load all settings. */
388 /*****************************************************************************/
390 gl_prefs_model_load_settings (glPrefsModel *prefs_model)
392 const gchar *pgsize, *default_units_string, *default_page_size;
397 gl_debug (DEBUG_PREFS, "START");
399 g_return_if_fail (prefs_model && GL_IS_PREFS_MODEL(prefs_model));
400 g_return_if_fail (prefs_model->gconf_client != NULL);
402 /* Make educated guess about locale defaults. */
403 pgsize = gtk_paper_size_get_default ();
404 if ( strcmp (pgsize,GTK_PAPER_NAME_LETTER) == 0 )
406 default_units_string = DEFAULT_UNITS_STRING_US;
407 default_page_size = DEFAULT_PAGE_SIZE_US;
411 default_units_string = DEFAULT_UNITS_STRING_METRIC;
412 default_page_size = DEFAULT_PAGE_SIZE_METRIC;
417 get_string (prefs_model->gconf_client,
419 default_units_string);
420 prefs_model->units = string_to_units( string );
422 lgl_xml_set_default_units (prefs_model->units);
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,
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);
439 prefs_model->default_font_size =
440 get_float (prefs_model->gconf_client,
441 BASE_KEY PREF_DEFAULT_FONT_SIZE,
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 );
451 prefs_model->default_text_color =
452 get_int (prefs_model->gconf_client,
453 BASE_KEY PREF_DEFAULT_TEXT_COLOR,
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 );
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);
468 gl_debug (DEBUG_PREFS, "text_line_spacing = %f", prefs_model->default_text_line_spacing);
470 /* Line properties */
471 prefs_model->default_line_width =
472 get_float (prefs_model->gconf_client,
473 BASE_KEY PREF_DEFAULT_LINE_WIDTH,
475 prefs_model->default_line_color =
476 get_int (prefs_model->gconf_client,
477 BASE_KEY PREF_DEFAULT_LINE_COLOR,
480 /* Fill properties */
481 prefs_model->default_fill_color =
482 get_int (prefs_model->gconf_client,
483 BASE_KEY PREF_DEFAULT_FILL_COLOR,
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,
493 prefs_model->main_toolbar_buttons_style =
494 get_int (prefs_model->gconf_client,
495 BASE_KEY PREF_MAIN_TOOLBAR_BUTTONS_STYLE,
498 prefs_model->main_toolbar_view_tooltips =
499 get_bool (prefs_model->gconf_client,
500 BASE_KEY PREF_MAIN_TOOLBAR_VIEW_TOOLTIPS,
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,
509 prefs_model->drawing_toolbar_view_tooltips =
510 get_bool (prefs_model->gconf_client,
511 BASE_KEY PREF_DRAWING_TOOLBAR_VIEW_TOOLTIPS,
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,
520 prefs_model->property_toolbar_view_tooltips =
521 get_bool (prefs_model->gconf_client,
522 BASE_KEY PREF_PROPERTY_TOOLBAR_VIEW_TOOLTIPS,
526 /* View properties */
527 prefs_model->grid_visible =
528 get_bool (prefs_model->gconf_client,
529 BASE_KEY PREF_GRID_VISIBLE,
532 prefs_model->markup_visible =
533 get_bool (prefs_model->gconf_client,
534 BASE_KEY PREF_MARKUP_VISIBLE,
538 prefs_model->max_recents =
539 get_int (prefs_model->gconf_client,
540 BASE_KEY PREF_MAX_RECENTS,
543 /* Recent templates */
544 for (p=prefs_model->recent_templates; p != NULL; p=p->next)
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,
554 prefs_model->max_recent_templates =
555 get_int (prefs_model->gconf_client,
556 BASE_KEY PREF_MAX_RECENT_TEMPLATES,
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);
566 lgl_paper_free (paper);
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)
576 if ( !lgl_db_does_template_name_exist (p->data) )
579 prefs_model->recent_templates = g_slist_delete_link (prefs_model->recent_templates, p);
583 gl_debug (DEBUG_PREFS, "max_recents = %d", prefs_model->max_recents);
586 g_signal_emit (G_OBJECT(prefs_model), signals[CHANGED], 0);
588 gl_debug (DEBUG_PREFS, "END");
591 /*---------------------------------------------------------------------------*/
592 /* PRIVATE. Key changed callback. */
593 /*---------------------------------------------------------------------------*/
595 notify_cb (GConfClient *client,
598 glPrefsModel *prefs_model)
600 gl_debug (DEBUG_PREFS, "Key was changed: %s", entry->key);
602 gl_prefs_model_load_settings (prefs_model);
605 /*---------------------------------------------------------------------------*/
606 /* PRIVATE. Utilities to get values with defaults. */
607 /*---------------------------------------------------------------------------*/
609 get_string (GConfClient *client,
615 val = gconf_client_get_string (client, key, NULL);
623 return def ? g_strdup (def) : NULL;
629 get_bool (GConfClient *client,
636 val = gconf_client_get (client, key, NULL);
640 if ( val->type == GCONF_VALUE_BOOL ) {
641 retval = gconf_value_get_bool (val);
646 gconf_value_free (val);
658 get_int (GConfClient *client,
665 val = gconf_client_get (client, key, NULL);
669 if ( val->type == GCONF_VALUE_INT) {
670 retval = gconf_value_get_int(val);
675 gconf_value_free (val);
687 get_float (GConfClient *client,
694 val = gconf_client_get (client, key, NULL);
698 if ( val->type == GCONF_VALUE_FLOAT ) {
699 retval = gconf_value_get_float(val);
704 gconf_value_free (val);
714 /*---------------------------------------------------------------------------*/
715 /* PRIVATE. Utilities to deal with units. */
716 /*---------------------------------------------------------------------------*/
718 string_to_units (const gchar *string)
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;
729 units = LGL_UNITS_INCH;
736 gchar *units_to_string (lglUnitsType units)
739 case LGL_UNITS_POINT:
746 return "Millimeters";