[Config] Add type-safe configuration parsing
[tilda-gobject.git] / tilda-window.c
index 8e8ff6a..822a987 100644 (file)
@@ -350,162 +350,6 @@ tilda_window_dbus_register_object (TildaWindow *self)
        g_free (object_path);
 }
 
-/*******************************************************************************
- * All configuration subsystem code is below
- ******************************************************************************/
-
-/**
- * Lookup a setting in the config file for this TildaWindow.
- *
- * The returned string MUST be g_strdup()'d if you want to actually use
- * it as a value. It is owned by the config system. You MUST NOT g_free() it.
- */
-static gchar *
-tilda_window_lookup_from_config (TildaWindow *self, const gchar key[])
-{
-       debug_enter  ();
-       debug_assert (TILDA_IS_WINDOW(self));
-       debug_assert (key != NULL);
-
-       GError *error = NULL;
-       gchar *group_name;
-       gpointer value;
-
-       /* Do the bottom-most lookup */
-       group_name = g_strdup_printf ("Window%d", self->number);
-       value = g_key_file_get_string (config_userprefs, group_name, key, &error);
-       g_free (group_name);
-
-       if (!error)
-               return value;
-       else
-               g_clear_error (&error);
-
-       /* Do the global lookup */
-       value = g_key_file_get_string (config_userprefs, "Global", key, &error);
-
-       if (!error)
-               return value;
-       else
-               g_clear_error (&error);
-
-       /* Look in the defaults */
-       if (!g_hash_table_lookup_extended (config_defaults, key, NULL, &value))
-       {
-               /* If this happened, the developers forgot to set the default for
-                * a key they added. Please email them. */
-               g_critical ("Error: unable to find the default for key=%s\n", key);
-               exit (1);
-       }
-
-       /* Return the default key */
-       return value;
-}
-
-/**
- * Set one of TildaWindow's integer properties from the config file or defaults.
- */
-static void
-tilda_window_config_int_property (TildaWindow *self, const gchar *property)
-{
-       debug_enter  ();
-       debug_assert (TILDA_IS_WINDOW(self));
-       debug_assert (property != NULL);
-
-       gint config_value;
-       gchar *config_value_raw;
-
-       config_value_raw = tilda_window_lookup_from_config (self, property);
-       config_value = atoi (config_value_raw);
-       g_object_set (G_OBJECT(self), property, config_value, NULL);
-}
-
-static void
-tilda_window_config_enum_property (TildaWindow *self, const gchar *property)
-{
-       debug_enter  ();
-       debug_assert (TILDA_IS_WINDOW(self));
-       debug_assert (property != NULL);
-
-       gint config_value;
-       gchar *config_value_raw;
-
-       /* Copy, then strip spaces off of the string from the config */
-       config_value_raw = g_strstrip (g_strdup (tilda_window_lookup_from_config (self, property)));
-
-begin_parsing:
-
-       /* Just try all of the possible enums */
-       if (g_ascii_strcasecmp (config_value_raw, "LEFT") == 0)
-               config_value = GTK_POS_LEFT;
-       else if (g_ascii_strcasecmp (config_value_raw, "RIGHT") == 0)
-               config_value = GTK_POS_RIGHT;
-       else if (g_ascii_strcasecmp (config_value_raw, "TOP") == 0)
-               config_value = GTK_POS_TOP;
-       else if (g_ascii_strcasecmp (config_value_raw, "BOTTOM") == 0)
-               config_value = GTK_POS_BOTTOM;
-       else
-       {
-               g_critical ("Unable to parse: '%s' as an enum\n", config_value_raw);
-
-               /* Use the default -- which I sure hope is valid (famous last words) */
-               config_value_raw = g_hash_table_lookup (config_defaults, property);
-               goto begin_parsing;
-       }
-
-       /* Free the value from the config system */
-       g_free (config_value_raw);
-
-       g_object_set (G_OBJECT(self), property, config_value, NULL);
-}
-
-static void
-tilda_window_config_string_property (TildaWindow *self, const gchar *property)
-{
-       debug_enter  ();
-       debug_assert (TILDA_IS_WINDOW(self));
-       debug_assert (property != NULL);
-
-       gchar *config_value_raw;
-
-       config_value_raw = tilda_window_lookup_from_config (self, property);
-
-       /* The property system g_strdup()s strings for us! */
-       g_object_set (G_OBJECT(self), property, config_value_raw, NULL);
-}
-
-static void
-tilda_window_config_boolean_property (TildaWindow *self, const gchar *property)
-{
-       debug_enter  ();
-       debug_assert (TILDA_IS_WINDOW(self));
-       debug_assert (property != NULL);
-
-       gboolean config_value;
-       gchar *config_value_raw;
-
-       config_value_raw = tilda_window_lookup_from_config (self, property);
-
-begin_parsing:
-
-       if (g_ascii_strcasecmp (config_value_raw, "true") == 0)
-               config_value = TRUE;
-       else if (g_ascii_strcasecmp (config_value_raw, "false") == 0)
-               config_value = FALSE;
-       else
-       {
-               g_critical ("Unable to parse: '%s' as a boolean\n", config_value_raw);
-
-               /* Use the default -- which I sure hope is valid (famous last words) */
-               config_value_raw = g_hash_table_lookup (config_defaults, property);
-               goto begin_parsing;
-       }
-
-       g_object_set (G_OBJECT(self), property, config_value, NULL);
-}
-
-
-
 /*******************************************************************************
  * ALL GOBJECT STUFF BELOW PLEASE
  ******************************************************************************/
@@ -616,13 +460,13 @@ tilda_window_set_property (GObject      *object,
                        break;
 
                case TILDA_WINDOW_TAB_POSITION:
-                       self->tab_position = g_value_get_int (value);
+                       self->tab_position = g_value_get_enum (value);
                        gtk_notebook_set_tab_pos (GTK_NOTEBOOK(self->notebook), self->tab_position);
                        debug_printf ("window tab position: %d\n", self->tab_position);
                        break;
 
                case TILDA_WINDOW_ANIMATION_ORIENTATION:
-                       self->animation_orientation = g_value_get_int (value);
+                       self->animation_orientation = g_value_get_enum (value);
                        debug_printf ("window animation orientation: %d\n", self->animation_orientation);
                        break;
 
@@ -726,11 +570,11 @@ tilda_window_get_property (GObject    *object,
                        break;
 
                case TILDA_WINDOW_TAB_POSITION:
-                       g_value_set_int (value, self->tab_position);
+                       g_value_set_enum (value, self->tab_position);
                        break;
 
                case TILDA_WINDOW_ANIMATION_ORIENTATION:
-                       g_value_set_int (value, self->animation_orientation);
+                       g_value_set_enum (value, self->animation_orientation);
                        break;
 
                case TILDA_WINDOW_ANIMATION_DELAY:
@@ -806,7 +650,7 @@ tilda_window_constructor (GType                  type,
        gtk_window_set_decorated (GTK_WINDOW(self->window), FALSE);
 
        /* Set all of the properties out of the config file */
-       tilda_window_config_string_property (self, "key");
+       tilda_window_set_property_from_config (self, "key");
 
        // FIXME: hack -- start the wizard in this case :)
        if (!self->key)
@@ -818,22 +662,22 @@ tilda_window_constructor (GType                  type,
                g_critical ("HACK: start the wizard here\n");
        }
 
-       tilda_window_config_int_property (self, "height");
-       tilda_window_config_int_property (self, "width");
-       tilda_window_config_int_property (self, "x-position");
-       tilda_window_config_int_property (self, "y-position");
-       tilda_window_config_int_property (self, "initial-terminals");
-       tilda_window_config_int_property (self, "animation-delay");
+       tilda_window_set_property_from_config (self, "height");
+       tilda_window_set_property_from_config (self, "width");
+       tilda_window_set_property_from_config (self, "x-position");
+       tilda_window_set_property_from_config (self, "y-position");
+       tilda_window_set_property_from_config (self, "initial-terminals");
+       tilda_window_set_property_from_config (self, "animation-delay");
 
-       tilda_window_config_enum_property (self, "tab-position");
-       tilda_window_config_enum_property (self, "animation-orientation");
+       tilda_window_set_property_from_config (self, "tab-position");
+       tilda_window_set_property_from_config (self, "animation-orientation");
 
-       tilda_window_config_boolean_property (self, "keep-above");
-       tilda_window_config_boolean_property (self, "skip-taskbar-hint");
-       tilda_window_config_boolean_property (self, "stick");
-       tilda_window_config_boolean_property (self, "hidden-at-start");
-       tilda_window_config_boolean_property (self, "centered-horizontally");
-       tilda_window_config_boolean_property (self, "centered-vertically");
+       tilda_window_set_property_from_config (self, "keep-above");
+       tilda_window_set_property_from_config (self, "skip-taskbar-hint");
+       tilda_window_set_property_from_config (self, "stick");
+       tilda_window_set_property_from_config (self, "hidden-at-start");
+       tilda_window_set_property_from_config (self, "centered-horizontally");
+       tilda_window_set_property_from_config (self, "centered-vertically");
 
        /* Add the initial terminal(s) */
        for (i=0; i<self->initial_terminals; ++i)
@@ -1016,25 +860,23 @@ tilda_window_class_init (gpointer g_class,
                                                                         TILDA_WINDOW_INITIAL_TERMINALS,
                                                                         pspec);
 
-       pspec = g_param_spec_int ("tab-position",
-                                                         _("Position of window's tab bar"),
-                                                         NULL,
-                                                         0,
-                                                         INT_MAX,
-                                                         0,
-                                                         G_PARAM_READWRITE);
+       pspec = g_param_spec_enum ("tab-position",
+                                                          _("Position of window's tab bar"),
+                                                          NULL,
+                                                          gtk_position_type_get_type(),
+                                                          GTK_POS_TOP,
+                                                          G_PARAM_READWRITE);
 
        g_object_class_install_property (gobject_class,
                                                                         TILDA_WINDOW_TAB_POSITION,
                                                                         pspec);
 
-       pspec = g_param_spec_int ("animation-orientation",
-                                                         _("Window's animation orientation"),
-                                                         NULL,
-                                                         0,
-                                                         INT_MAX,
-                                                         0,
-                                                         G_PARAM_READWRITE);
+       pspec = g_param_spec_enum ("animation-orientation",
+                                                          _("Window's animation orientation"),
+                                                          NULL,
+                                                          gtk_position_type_get_type(),
+                                                          GTK_POS_TOP,
+                                                          G_PARAM_READWRITE);
 
        g_object_class_install_property (gobject_class,
                                                                         TILDA_WINDOW_ANIMATION_ORIENTATION,