Make Tilda work without DBus
[tilda-gobject.git] / tilda-controller.c
index aa61823..4127920 100644 (file)
@@ -1,4 +1,5 @@
 #include "tilda.h"
+#include "tilda-config.h"
 #include "tilda-controller.h"
 #include "tilda-controller-dbus-glue.h"
 #include "tilda-window.h"
@@ -14,6 +15,10 @@ tilda_controller_dbus_register_object (TildaController *self)
 
        static const gchar object_path[] = "/net/sourceforge/Tilda";
 
+       /* If DBus is not running, leave */
+       if (!dbus_connection)
+               return;
+
        dbus_g_connection_register_g_object (dbus_connection, object_path, G_OBJECT(self));
 }
 
@@ -73,7 +78,7 @@ tilda_controller_add_window (TildaController *self)
 }
 
 gboolean
-tilda_controller_delete_window (TildaController *self, gint window_number)
+tilda_controller_remove_window (TildaController *self, gint window_number)
 {
        debug_enter  ();
        debug_assert (TILDA_IS_CONTROLLER(self));
@@ -101,12 +106,42 @@ tilda_controller_delete_window (TildaController *self, gint window_number)
                                gtk_main_quit ();
                        }
 
-                       /* We were able to delete the window */
+                       /* We were able to remove the window */
                        return TRUE;
                }
        }
 
-       /* There must have been no window to delete */
+       /* There must have been no window to remove */
+       return FALSE;
+}
+
+/**
+ * Check if a key is used in one of our windows.
+ *
+ * This is needed because the tomboy_keybinder_bind() function allows
+ * more than one callback to be registered for the same key.
+ */
+gboolean
+tilda_controller_global_key_in_use (const TildaController *self, const gchar *keystr)
+{
+       gint i;
+       TildaWindow *tw;
+
+       guint key1, key2;
+       GdkModifierType mod1, mod2;
+
+       gtk_accelerator_parse (keystr, &key1, &mod1);
+
+       for (i=0; i<self->windows->len; ++i)
+       {
+               tw = g_ptr_array_index (self->windows, i);
+               gtk_accelerator_parse (tw->key, &key2, &mod2);
+
+               if (key1 == key2 && mod1 == mod2)
+                       return TRUE;
+       }
+
+       /* No identical keys found, we're ok */
        return FALSE;
 }
 
@@ -128,6 +163,10 @@ tilda_controller_quit (TildaController *self, GError **error)
 
 static GObjectClass *parent_class = NULL;
 
+enum tilda_controller_properties {
+       TILDA_CONTROLLER_INITIAL_WINDOWS = 1,
+};
+
 static void
 tilda_controller_instance_init (GTypeInstance *instance,
                                                                gpointer       g_class)
@@ -138,6 +177,7 @@ tilda_controller_instance_init (GTypeInstance *instance,
 
        self->dispose_has_run = FALSE;
        self->windows = g_ptr_array_new ();
+       self->initial_windows = 1;
 }
 
 static void
@@ -146,10 +186,17 @@ tilda_controller_set_property (GObject      *object,
                                                           const GValue *value,
                                                           GParamSpec   *pspec)
 {
-       TildaController *self = (TildaController *) self;
+       debug_enter ();
+
+       TildaController *self = (TildaController *) object;
 
        switch (property_id)
        {
+               case TILDA_CONTROLLER_INITIAL_WINDOWS:
+                       self->initial_windows = g_value_get_int (value);
+                       debug_printf ("tilda controller initial windows: %d\n", self->initial_windows);
+                       break;
+
                default:
                        /* We don't have any other properties */
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -163,10 +210,16 @@ tilda_controller_get_property (GObject    *object,
                                                           GValue     *value,
                                                           GParamSpec *pspec)
 {
+       debug_enter ();
+
        TildaController *self = (TildaController *) object;
 
        switch (property_id)
        {
+               case TILDA_CONTROLLER_INITIAL_WINDOWS:
+                       g_value_set_int (value, self->initial_windows);
+                       break;
+
                default:
                        /* We don't have any other properties */
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -182,10 +235,11 @@ tilda_controller_constructor (GType                  type,
        debug_enter ();
 
        GObject *obj;
-       TildaControllerClass *klass;
        TildaController *self;
+       gint i;
 
        /* Invoke the parent constructor */
+       TildaControllerClass *klass;
        klass = TILDA_CONTROLLER_CLASS (g_type_class_peek (TILDA_TYPE_CONTROLLER));
        obj = parent_class->constructor (type,
                                                                         n_construct_properties,
@@ -195,12 +249,16 @@ tilda_controller_constructor (GType                  type,
         * Have fun! */
        self = TILDA_CONTROLLER(obj);
 
+       /* Set all of the properties from the config */
+       tilda_controller_set_property_from_config (self, "initial-windows");
+
+       /* Add initial windows */
+       for (i=0; i<self->initial_windows; ++i)
+               tilda_controller_add_window (self);
+
        /* Register this object with DBus */
        tilda_controller_dbus_register_object (self);
 
-       /* Add a window -- FIXME: the number should be configurable */
-       tilda_controller_add_window (self);
-
        return obj;
 }
 
@@ -248,7 +306,7 @@ tilda_controller_finalize (GObject *obj)
 
 static void
 tilda_controller_class_init (gpointer g_class,
-                                          gpointer g_class_data)
+                                                        gpointer g_class_data)
 {
        debug_enter ();
 
@@ -265,6 +323,17 @@ tilda_controller_class_init (gpointer g_class,
        parent_class = g_type_class_peek_parent (klass);
 
        /* Add properties here */
+       pspec = g_param_spec_int ("initial-windows",
+                                                         _("The number of windows that will be opened on startup"),
+                                                         NULL,
+                                                         1,
+                                                         100, /* Sane Limit */
+                                                         1,
+                                                         G_PARAM_READWRITE);
+
+       g_object_class_install_property (gobject_class,
+                                                                        TILDA_CONTROLLER_INITIAL_WINDOWS,
+                                                                        pspec);
 
        /* Hook the TildaController type into DBus */
        dbus_g_object_type_install_info (tilda_controller_get_type(),