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 dbus_g_connection_register_g_object (dbus_connection, object_path, G_OBJECT(self));
22 * Find the next free window number, so it can be used by a TildaWindow
25 tilda_controller_find_next_free_window_number (TildaController *self)
28 debug_assert (TILDA_IS_CONTROLLER(self));
33 for (i=0; i<INT_MAX; ++i)
37 for (j=0; j<self->windows->len; ++j)
39 TildaWindow *tw = g_ptr_array_index (self->windows, j);
56 tilda_controller_add_window (TildaController *self)
59 debug_assert (TILDA_IS_CONTROLLER(self));
64 number = tilda_controller_find_next_free_window_number (self);
65 ret = g_object_new (TILDA_TYPE_WINDOW,
67 "controller", G_OBJECT(self),
70 g_ptr_array_add (self->windows, ret);
72 debug_printf ("Adding window: 0x%x (number %d of %d)\n", ret, ret->number, self->windows->len);
77 tilda_controller_remove_window (TildaController *self, gint window_number)
80 debug_assert (TILDA_IS_CONTROLLER(self));
81 debug_assert (window_number >= 0);
86 for (i=0; i<self->windows->len; ++i)
88 win = g_ptr_array_index (self->windows, i);
90 if (win->number == window_number)
92 debug_printf ("Deleting TildaWindow 0x%x (number %d of %d)\n",
93 win, win->number, self->windows->len);
94 g_ptr_array_remove_index (self->windows, i);
95 g_object_unref (G_OBJECT(win));
97 if (self->windows->len == 0)
99 debug_printf ("No windows left, exiting...\n");
101 /* We get unref'd in main() */
105 /* We were able to remove the window */
110 /* There must have been no window to remove */
115 * Check if a key is used in one of our windows.
117 * This is needed because the tomboy_keybinder_bind() function allows
118 * more than one callback to be registered for the same key.
121 tilda_controller_global_key_in_use (const TildaController *self, const gchar *keystr)
127 GdkModifierType mod1, mod2;
129 gtk_accelerator_parse (keystr, &key1, &mod1);
131 for (i=0; i<self->windows->len; ++i)
133 tw = g_ptr_array_index (self->windows, i);
134 gtk_accelerator_parse (tw->key, &key2, &mod2);
136 if (key1 == key2 && mod1 == mod2)
140 /* No identical keys found, we're ok */
145 tilda_controller_quit (TildaController *self, GError **error)
148 debug_assert (TILDA_IS_CONTROLLER(self));
150 /* Not much left but to quit, since we get unref'd in main() */
156 /*******************************************************************************
157 * GObject code below... it is doubtful you'll need to make big changes :)
158 ******************************************************************************/
160 static GObjectClass *parent_class = NULL;
162 enum tilda_controller_properties {
163 TILDA_CONTROLLER_INITIAL_WINDOWS = 1,
167 tilda_controller_instance_init (GTypeInstance *instance,
172 TildaController *self = (TildaController *) instance;
174 self->dispose_has_run = FALSE;
175 self->windows = g_ptr_array_new ();
176 self->initial_windows = 1;
180 tilda_controller_set_property (GObject *object,
187 TildaController *self = (TildaController *) object;
191 case TILDA_CONTROLLER_INITIAL_WINDOWS:
192 self->initial_windows = g_value_get_int (value);
193 debug_printf ("tilda controller initial windows: %d\n", self->initial_windows);
197 /* We don't have any other properties */
198 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
204 tilda_controller_get_property (GObject *object,
211 TildaController *self = (TildaController *) object;
215 case TILDA_CONTROLLER_INITIAL_WINDOWS:
216 g_value_set_int (value, self->initial_windows);
220 /* We don't have any other properties */
221 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
227 tilda_controller_constructor (GType type,
228 guint n_construct_properties,
229 GObjectConstructParam *construct_properties)
234 TildaController *self;
237 /* Invoke the parent constructor */
238 TildaControllerClass *klass;
239 klass = TILDA_CONTROLLER_CLASS (g_type_class_peek (TILDA_TYPE_CONTROLLER));
240 obj = parent_class->constructor (type,
241 n_construct_properties,
242 construct_properties);
243 g_print ("%s all set up\n", __func__);
245 /* The object is ready, and all constructor-time properties have been set.
247 self = TILDA_CONTROLLER(obj);
249 /* Set all of the properties from the config */
250 tilda_controller_set_property_from_config (self, "initial-windows");
252 /* Add initial windows */
253 for (i=0; i<self->initial_windows; ++i)
254 tilda_controller_add_window (self);
256 /* Register this object with DBus */
257 tilda_controller_dbus_register_object (self);
263 tilda_controller_dispose (GObject *obj)
267 TildaController *self = (TildaController *) obj;
269 /* We must only run dispose once ... */
270 if (self->dispose_has_run)
273 self->dispose_has_run = TRUE;
276 * In dispose, you are supposed to free all types referenced from this
277 * object which might themselves hold a reference to self. Generally,
278 * the most simple solution is to unref all members on which you own a
281 g_ptr_array_foreach (self->windows, g_object_unref, NULL);
283 /* Chain up to the parent class */
284 G_OBJECT_CLASS (parent_class)->dispose (obj);
288 tilda_controller_finalize (GObject *obj)
292 TildaController *self = (TildaController *) obj;
295 * Here, complete the object's destruction.
296 * You might not need to do much more than
297 * g_free() any primitives.
300 /* Chain up to the parent class */
301 G_OBJECT_CLASS (parent_class)->finalize (obj);
305 tilda_controller_class_init (gpointer g_class,
306 gpointer g_class_data)
310 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
311 TildaControllerClass *klass = TILDA_CONTROLLER_CLASS (g_class);
314 gobject_class->set_property = tilda_controller_set_property;
315 gobject_class->get_property = tilda_controller_get_property;
316 gobject_class->dispose = tilda_controller_dispose;
317 gobject_class->finalize = tilda_controller_finalize;
318 gobject_class->constructor = tilda_controller_constructor;
320 parent_class = g_type_class_peek_parent (klass);
322 /* Add properties here */
323 pspec = g_param_spec_int ("initial-windows",
324 _("The number of windows that will be opened on startup"),
327 100, /* Sane Limit */
331 g_object_class_install_property (gobject_class,
332 TILDA_CONTROLLER_INITIAL_WINDOWS,
335 /* Hook the TildaController type into DBus */
336 dbus_g_object_type_install_info (tilda_controller_get_type(),
337 &dbus_glib_tilda_controller_object_info);
340 GType tilda_controller_get_type (void)
342 static GType type = 0;
346 static const GTypeInfo info = {
347 sizeof (TildaControllerClass),
348 NULL, /* base_init */
349 NULL, /* base_finalize */
350 tilda_controller_class_init, /* class_init */
351 NULL, /* class_finalize */
352 NULL, /* class_data */
353 sizeof (TildaController),
355 tilda_controller_instance_init, /* instance_init */
358 type = g_type_register_static (G_TYPE_OBJECT,
359 "TildaControllerType",
367 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */