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_delete_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 delete the window */
109 /* There must have been no window to delete */
114 tilda_controller_quit (TildaController *self, GError **error)
117 debug_assert (TILDA_IS_CONTROLLER(self));
119 /* Not much left but to quit, since we get unref'd in main() */
125 /*******************************************************************************
126 * GObject code below... it is doubtful you'll need to make big changes :)
127 ******************************************************************************/
129 static GObjectClass *parent_class = NULL;
132 tilda_controller_instance_init (GTypeInstance *instance,
137 TildaController *self = (TildaController *) instance;
139 self->dispose_has_run = FALSE;
140 self->windows = g_ptr_array_new ();
144 tilda_controller_set_property (GObject *object,
149 TildaController *self = (TildaController *) self;
154 /* We don't have any other properties */
155 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
161 tilda_controller_get_property (GObject *object,
166 TildaController *self = (TildaController *) object;
171 /* We don't have any other properties */
172 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
178 tilda_controller_constructor (GType type,
179 guint n_construct_properties,
180 GObjectConstructParam *construct_properties)
185 TildaControllerClass *klass;
186 TildaController *self;
188 /* Invoke the parent constructor */
189 klass = TILDA_CONTROLLER_CLASS (g_type_class_peek (TILDA_TYPE_CONTROLLER));
190 obj = parent_class->constructor (type,
191 n_construct_properties,
192 construct_properties);
194 /* The object is ready, and all constructor-time properties have been set.
196 self = TILDA_CONTROLLER(obj);
198 /* Register this object with DBus */
199 tilda_controller_dbus_register_object (self);
201 /* Add a window -- FIXME: the number should be configurable */
202 tilda_controller_add_window (self);
208 tilda_controller_dispose (GObject *obj)
212 TildaController *self = (TildaController *) obj;
214 /* We must only run dispose once ... */
215 if (self->dispose_has_run)
218 self->dispose_has_run = TRUE;
221 * In dispose, you are supposed to free all types referenced from this
222 * object which might themselves hold a reference to self. Generally,
223 * the most simple solution is to unref all members on which you own a
226 g_ptr_array_foreach (self->windows, g_object_unref, NULL);
228 /* Chain up to the parent class */
229 G_OBJECT_CLASS (parent_class)->dispose (obj);
233 tilda_controller_finalize (GObject *obj)
237 TildaController *self = (TildaController *) obj;
240 * Here, complete the object's destruction.
241 * You might not need to do much more than
242 * g_free() any primitives.
245 /* Chain up to the parent class */
246 G_OBJECT_CLASS (parent_class)->finalize (obj);
250 tilda_controller_class_init (gpointer g_class,
251 gpointer g_class_data)
255 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
256 TildaControllerClass *klass = TILDA_CONTROLLER_CLASS (g_class);
259 gobject_class->set_property = tilda_controller_set_property;
260 gobject_class->get_property = tilda_controller_get_property;
261 gobject_class->dispose = tilda_controller_dispose;
262 gobject_class->finalize = tilda_controller_finalize;
263 gobject_class->constructor = tilda_controller_constructor;
265 parent_class = g_type_class_peek_parent (klass);
267 /* Add properties here */
269 /* Hook the TildaController type into DBus */
270 dbus_g_object_type_install_info (tilda_controller_get_type(),
271 &dbus_glib_tilda_controller_object_info);
274 GType tilda_controller_get_type (void)
276 static GType type = 0;
280 static const GTypeInfo info = {
281 sizeof (TildaControllerClass),
282 NULL, /* base_init */
283 NULL, /* base_finalize */
284 tilda_controller_class_init, /* class_init */
285 NULL, /* class_finalize */
286 NULL, /* class_data */
287 sizeof (TildaController),
289 tilda_controller_instance_init, /* instance_init */
292 type = g_type_register_static (G_TYPE_OBJECT,
293 "TildaControllerType",
301 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */