2 #include "tilda-config.h"
3 #include "tilda-controller.h"
4 #include "tilda-controller-dbus-glue.h"
5 #include "tilda-window.h"
8 * Register this object with DBus, so it can be interacted with!
11 tilda_controller_dbus_register_object (TildaController *self)
14 debug_assert (TILDA_IS_CONTROLLER(self));
16 static const gchar object_path[] = "/net/sourceforge/Tilda";
18 /* If DBus is not running, leave */
22 dbus_g_connection_register_g_object (dbus_connection, object_path, G_OBJECT(self));
26 * Find the next free window number, so it can be used by a TildaWindow
29 tilda_controller_find_next_free_window_number (TildaController *self)
32 debug_assert (TILDA_IS_CONTROLLER(self));
37 for (i=0; i<INT_MAX; ++i)
41 for (j=0; j<self->windows->len; ++j)
43 TildaWindow *tw = g_ptr_array_index (self->windows, j);
60 tilda_controller_add_window (TildaController *self)
63 debug_assert (TILDA_IS_CONTROLLER(self));
68 number = tilda_controller_find_next_free_window_number (self);
69 ret = g_object_new (TILDA_TYPE_WINDOW,
71 "controller", G_OBJECT(self),
74 g_ptr_array_add (self->windows, ret);
76 debug_printf ("Adding window: 0x%p (number %d of %d)\n", ret, ret->number, self->windows->len);
81 tilda_controller_remove_window (TildaController *self, gint window_number)
84 debug_assert (TILDA_IS_CONTROLLER(self));
85 debug_assert (window_number >= 0);
90 for (i=0; i<self->windows->len; ++i)
92 win = g_ptr_array_index (self->windows, i);
94 if (win->number == window_number)
96 debug_printf ("Deleting TildaWindow 0x%p (number %d of %d)\n",
97 win, win->number, self->windows->len);
98 g_ptr_array_remove_index (self->windows, i);
99 g_object_unref (G_OBJECT(win));
101 if (self->windows->len == 0)
103 debug_printf ("No windows left, exiting...\n");
105 /* We get unref'd in main() */
109 /* We were able to remove the window */
114 /* There must have been no window to remove */
119 * Check if a key is used in one of our windows.
121 * This is needed because the tomboy_keybinder_bind() function allows
122 * more than one callback to be registered for the same key.
125 tilda_controller_global_key_in_use (const TildaController *self, const gchar *keystr)
131 GdkModifierType mod1, mod2;
133 gtk_accelerator_parse (keystr, &key1, &mod1);
135 for (i=0; i<self->windows->len; ++i)
137 tw = g_ptr_array_index (self->windows, i);
138 gtk_accelerator_parse (tw->key, &key2, &mod2);
140 if (key1 == key2 && mod1 == mod2)
144 /* No identical keys found, we're ok */
149 tilda_controller_quit (TildaController *self, GError **error)
152 debug_assert (TILDA_IS_CONTROLLER(self));
154 /* Not much left but to quit, since we get unref'd in main() */
160 /*******************************************************************************
161 * GObject code below... it is doubtful you'll need to make big changes :)
162 ******************************************************************************/
164 static GObjectClass *parent_class = NULL;
166 enum tilda_controller_properties {
167 TILDA_CONTROLLER_INITIAL_WINDOWS = 1,
171 tilda_controller_instance_init (GTypeInstance *instance,
176 TildaController *self = (TildaController *) instance;
178 self->dispose_has_run = FALSE;
179 self->windows = g_ptr_array_new ();
180 self->initial_windows = 1;
184 tilda_controller_set_property (GObject *object,
191 TildaController *self = (TildaController *) object;
195 case TILDA_CONTROLLER_INITIAL_WINDOWS:
196 self->initial_windows = g_value_get_int (value);
197 debug_printf ("tilda controller initial windows: %d\n", self->initial_windows);
201 /* We don't have any other properties */
202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
208 tilda_controller_get_property (GObject *object,
215 TildaController *self = (TildaController *) object;
219 case TILDA_CONTROLLER_INITIAL_WINDOWS:
220 g_value_set_int (value, self->initial_windows);
224 /* We don't have any other properties */
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
231 tilda_controller_constructor (GType type,
232 guint n_construct_properties,
233 GObjectConstructParam *construct_properties)
238 TildaController *self;
241 /* Invoke the parent constructor */
242 TildaControllerClass *klass;
243 klass = TILDA_CONTROLLER_CLASS (g_type_class_peek (TILDA_TYPE_CONTROLLER));
244 obj = parent_class->constructor (type,
245 n_construct_properties,
246 construct_properties);
248 /* The object is ready, and all constructor-time properties have been set.
250 self = TILDA_CONTROLLER(obj);
252 /* Set all of the properties from the config */
253 tilda_controller_set_property_from_config (self, "initial-windows");
255 /* Add initial windows */
256 for (i=0; i<self->initial_windows; ++i)
257 tilda_controller_add_window (self);
259 /* Register this object with DBus */
260 tilda_controller_dbus_register_object (self);
266 tilda_controller_dispose (GObject *obj)
270 TildaController *self = (TildaController *) obj;
272 /* We must only run dispose once ... */
273 if (self->dispose_has_run)
276 self->dispose_has_run = TRUE;
279 * In dispose, you are supposed to free all types referenced from this
280 * object which might themselves hold a reference to self. Generally,
281 * the most simple solution is to unref all members on which you own a
284 g_ptr_array_foreach (self->windows, g_object_unref, NULL);
286 /* Chain up to the parent class */
287 G_OBJECT_CLASS (parent_class)->dispose (obj);
291 tilda_controller_finalize (GObject *obj)
295 TildaController *self = (TildaController *) obj;
298 * Here, complete the object's destruction.
299 * You might not need to do much more than
300 * g_free() any primitives.
303 /* Chain up to the parent class */
304 G_OBJECT_CLASS (parent_class)->finalize (obj);
308 tilda_controller_class_init (gpointer g_class,
309 gpointer g_class_data)
313 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
314 TildaControllerClass *klass = TILDA_CONTROLLER_CLASS (g_class);
317 gobject_class->set_property = tilda_controller_set_property;
318 gobject_class->get_property = tilda_controller_get_property;
319 gobject_class->dispose = tilda_controller_dispose;
320 gobject_class->finalize = tilda_controller_finalize;
321 gobject_class->constructor = tilda_controller_constructor;
323 parent_class = g_type_class_peek_parent (klass);
325 /* Add properties here */
326 pspec = g_param_spec_int ("initial-windows",
327 _("The number of windows that will be opened on startup"),
330 100, /* Sane Limit */
334 g_object_class_install_property (gobject_class,
335 TILDA_CONTROLLER_INITIAL_WINDOWS,
338 /* Hook the TildaController type into DBus */
339 dbus_g_object_type_install_info (tilda_controller_get_type(),
340 &dbus_glib_tilda_controller_object_info);
343 GType tilda_controller_get_type (void)
345 static GType type = 0;
349 static const GTypeInfo info = {
350 sizeof (TildaControllerClass),
351 NULL, /* base_init */
352 NULL, /* base_finalize */
353 tilda_controller_class_init, /* class_init */
354 NULL, /* class_finalize */
355 NULL, /* class_data */
356 sizeof (TildaController),
358 tilda_controller_instance_init, /* instance_init */
361 type = g_type_register_static (G_TYPE_OBJECT,
362 "TildaControllerType",
370 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */