2 #include "tilda-controller.h"
3 #include "tilda-controller-dbus-glue.h"
4 #include "tilda-window.h"
7 * Register this object with DBus, so it can be interacted with!
10 tilda_controller_dbus_register_object (TildaController *self)
13 debug_assert (TILDA_IS_CONTROLLER(self));
15 static const gchar object_path[] = "/net/sourceforge/Tilda";
17 dbus_g_connection_register_g_object (dbus_connection, object_path, G_OBJECT(self));
21 * Find the next free window number, so it can be used by a TildaWindow
24 tilda_controller_find_next_free_window_number (TildaController *self)
27 debug_assert (TILDA_IS_CONTROLLER(self));
32 for (i=0; i<INT_MAX; ++i)
36 for (j=0; j<self->windows->len; ++j)
38 TildaWindow *tw = g_ptr_array_index (self->windows, j);
55 tilda_controller_add_window (TildaController *self)
58 debug_assert (TILDA_IS_CONTROLLER(self));
63 number = tilda_controller_find_next_free_window_number (self);
64 ret = g_object_new (TILDA_TYPE_WINDOW,
66 "controller", G_OBJECT(self),
69 g_ptr_array_add (self->windows, ret);
71 debug_printf ("Adding window: 0x%x (number %d of %d)\n", ret, ret->number, self->windows->len);
76 tilda_controller_remove_window (TildaController *self, gint window_number)
79 debug_assert (TILDA_IS_CONTROLLER(self));
80 debug_assert (window_number >= 0);
85 for (i=0; i<self->windows->len; ++i)
87 win = g_ptr_array_index (self->windows, i);
89 if (win->number == window_number)
91 debug_printf ("Deleting TildaWindow 0x%x (number %d of %d)\n",
92 win, win->number, self->windows->len);
93 g_ptr_array_remove_index (self->windows, i);
94 g_object_unref (G_OBJECT(win));
96 if (self->windows->len == 0)
98 debug_printf ("No windows left, exiting...\n");
100 /* We get unref'd in main() */
104 /* We were able to remove the window */
109 /* There must have been no window to remove */
114 * Check if a key is used in one of our windows.
116 * This is needed because the tomboy_keybinder_bind() function allows
117 * more than one callback to be registered for the same key.
120 tilda_controller_global_key_in_use (const TildaController *self, const gchar *keystr)
126 GdkModifierType mod1, mod2;
128 gtk_accelerator_parse (keystr, &key1, &mod1);
130 for (i=0; i<self->windows->len; ++i)
132 tw = g_ptr_array_index (self->windows, i);
133 gtk_accelerator_parse (tw->key, &key2, &mod2);
135 if (key1 == key2 && mod1 == mod2)
139 /* No identical keys found, we're ok */
144 tilda_controller_quit (TildaController *self, GError **error)
147 debug_assert (TILDA_IS_CONTROLLER(self));
149 /* Not much left but to quit, since we get unref'd in main() */
155 /*******************************************************************************
156 * GObject code below... it is doubtful you'll need to make big changes :)
157 ******************************************************************************/
159 static GObjectClass *parent_class = NULL;
162 tilda_controller_instance_init (GTypeInstance *instance,
167 TildaController *self = (TildaController *) instance;
169 self->dispose_has_run = FALSE;
170 self->windows = g_ptr_array_new ();
174 tilda_controller_set_property (GObject *object,
179 TildaController *self = (TildaController *) self;
184 /* We don't have any other properties */
185 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
191 tilda_controller_get_property (GObject *object,
196 TildaController *self = (TildaController *) object;
201 /* We don't have any other properties */
202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
208 tilda_controller_constructor (GType type,
209 guint n_construct_properties,
210 GObjectConstructParam *construct_properties)
215 TildaControllerClass *klass;
216 TildaController *self;
218 /* Invoke the parent constructor */
219 klass = TILDA_CONTROLLER_CLASS (g_type_class_peek (TILDA_TYPE_CONTROLLER));
220 obj = parent_class->constructor (type,
221 n_construct_properties,
222 construct_properties);
224 /* The object is ready, and all constructor-time properties have been set.
226 self = TILDA_CONTROLLER(obj);
228 /* Register this object with DBus */
229 tilda_controller_dbus_register_object (self);
231 /* Add a window -- FIXME: the number should be configurable */
232 tilda_controller_add_window (self);
238 tilda_controller_dispose (GObject *obj)
242 TildaController *self = (TildaController *) obj;
244 /* We must only run dispose once ... */
245 if (self->dispose_has_run)
248 self->dispose_has_run = TRUE;
251 * In dispose, you are supposed to free all types referenced from this
252 * object which might themselves hold a reference to self. Generally,
253 * the most simple solution is to unref all members on which you own a
256 g_ptr_array_foreach (self->windows, g_object_unref, NULL);
258 /* Chain up to the parent class */
259 G_OBJECT_CLASS (parent_class)->dispose (obj);
263 tilda_controller_finalize (GObject *obj)
267 TildaController *self = (TildaController *) obj;
270 * Here, complete the object's destruction.
271 * You might not need to do much more than
272 * g_free() any primitives.
275 /* Chain up to the parent class */
276 G_OBJECT_CLASS (parent_class)->finalize (obj);
280 tilda_controller_class_init (gpointer g_class,
281 gpointer g_class_data)
285 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
286 TildaControllerClass *klass = TILDA_CONTROLLER_CLASS (g_class);
289 gobject_class->set_property = tilda_controller_set_property;
290 gobject_class->get_property = tilda_controller_get_property;
291 gobject_class->dispose = tilda_controller_dispose;
292 gobject_class->finalize = tilda_controller_finalize;
293 gobject_class->constructor = tilda_controller_constructor;
295 parent_class = g_type_class_peek_parent (klass);
297 /* Add properties here */
299 /* Hook the TildaController type into DBus */
300 dbus_g_object_type_install_info (tilda_controller_get_type(),
301 &dbus_glib_tilda_controller_object_info);
304 GType tilda_controller_get_type (void)
306 static GType type = 0;
310 static const GTypeInfo info = {
311 sizeof (TildaControllerClass),
312 NULL, /* base_init */
313 NULL, /* base_finalize */
314 tilda_controller_class_init, /* class_init */
315 NULL, /* class_finalize */
316 NULL, /* class_data */
317 sizeof (TildaController),
319 tilda_controller_instance_init, /* instance_init */
322 type = g_type_register_static (G_TYPE_OBJECT,
323 "TildaControllerType",
331 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */