[Window] Use the configuration system to retrieve startup values
authorIra W. Snyder <devel@irasnyder.com>
Sat, 26 Jan 2008 19:23:06 +0000 (11:23 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Sat, 26 Jan 2008 19:23:06 +0000 (11:23 -0800)
This hooks into the configuration system to get all of the property
startup values, currently out of the defaults in the configration
system.

tilda-window.c

index dcef0f5..618e862 100644 (file)
@@ -1,7 +1,8 @@
-#include <string.h> /* for strcmp() */
 #include <gdk/gdkx.h> /* for gdk_x11_window_set_user_time() */
+#include <stdlib.h>
 
 #include "tilda.h"
+#include "tilda-config.h"
 #include "tilda-controller.h"
 #include "tilda-window.h"
 #include "tilda-window-dbus-glue.h"
@@ -295,7 +296,7 @@ tilda_window_try_to_bind_key (TildaWindow *self, const gchar *new_key)
        gboolean ret = FALSE;
 
        /* Make sure the new key is not null in any way */
-       if (new_key == NULL || strcmp("", new_key) == 0)
+       if (new_key == NULL || g_ascii_strcasecmp("", new_key) == 0)
                return FALSE;
 
        /* Check that no other windows are using the key */
@@ -321,7 +322,7 @@ tilda_window_try_to_bind_key (TildaWindow *self, const gchar *new_key)
        g_printerr (_("Bind key '%s' failed. Reverting to original keybinding\n"), self->key);
 
        /* Not successful, so rebind the old key, and return FALSE */
-       if (self->key != NULL && strcmp("",self->key) != 0)
+       if (self->key != NULL && g_ascii_strcasecmp("",self->key) != 0)
        {
                ret = tomboy_keybinder_bind (self->key, tilda_window_keybinding_cb, self);
 
@@ -349,6 +350,162 @@ 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
  ******************************************************************************/
@@ -627,9 +784,6 @@ tilda_window_constructor (GType                  type,
         */
        self = TILDA_WINDOW(obj);
 
-       /* Register this object with DBus */
-       tilda_window_dbus_register_object (self);
-
        /* Try to set up real transparency */
        tilda_window_setup_real_transparency (self);
 
@@ -637,17 +791,38 @@ tilda_window_constructor (GType                  type,
        g_object_set (G_OBJECT(self->notebook), "can-focus", FALSE, NULL);
        gtk_widget_show (self->notebook);
 
-       // FIXME: Remove these, and replace with reads from the config system
-       gchar *mykey = g_strdup_printf ("F%d", self->number+3); // TERRIBLE HACK
-       g_object_set (G_OBJECT(self), "key", mykey, NULL);
-       g_free (mykey);
-       g_object_set (G_OBJECT(self), "x-position", 0, "y-position", 0, NULL);
-       g_object_set (G_OBJECT(self), "height", 400, "width", 1680, NULL);
-       g_object_set (G_OBJECT(self), "keep-above", TRUE, "stick", TRUE, NULL);
-       g_object_set (G_OBJECT(self), "hidden-at-start", FALSE, NULL);
-
+       /* Tilda is never decorated */
        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");
+
+       // FIXME: hack -- start the wizard in this case :)
+       if (!self->key)
+       {
+               gchar *key = g_strdup_printf ("F%d", self->number+3);
+               g_object_set (G_OBJECT(self), "key", key, NULL);
+               g_free (key);
+
+               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, "animation-delay");
+
+       tilda_window_config_enum_property (self, "tab-position");
+       tilda_window_config_enum_property (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");
+
        // FIXME: It should be configurable how many terms we add at startup
        tilda_window_add_terminal (self);
        tilda_window_add_terminal (self);
@@ -662,6 +837,9 @@ tilda_window_constructor (GType                  type,
        else
                self->state = WINDOW_UP;
 
+       /* Register this object with DBus */
+       tilda_window_dbus_register_object (self);
+
        return obj;
 }