From cca816ac548169679635efa57aae7f9188a47d1b Mon Sep 17 00:00:00 2001 From: Nicolas Dufresne Date: Fri, 14 Oct 2011 14:07:17 -0400 Subject: [PATCH] Fix observer singleton implementation and select features --- src/telepathy-logger.c | 10 ++++- telepathy-logger/call-channel.c | 4 +- telepathy-logger/observer-internal.h | 2 +- telepathy-logger/observer.c | 73 +++++++++++++++-------------- telepathy-logger/streamed-media-channel.c | 4 +- telepathy-logger/text-channel.c | 4 +- tests/dbus/test-tpl-observer.c | 4 +- 7 files changed, 58 insertions(+), 43 deletions(-) diff --git a/src/telepathy-logger.c b/src/telepathy-logger.c index e0548f7..e47b4ed 100644 --- a/src/telepathy-logger.c +++ b/src/telepathy-logger.c @@ -187,7 +187,15 @@ main (int argc, DEBUG ("- TplCallChannel registered."); #endif /* ENABLE_CALL */ - observer = _tpl_observer_new (); + + observer = _tpl_observer_dup (&error); + + if (observer == NULL) { + g_critical ("Failed to create observer: %s", error->message); + g_error_free (error); + goto out; + } + DEBUG ("Registering channel factory into TplObserver"); _tpl_observer_set_channel_factory (observer, _tpl_channel_factory_build); diff --git a/telepathy-logger/call-channel.c b/telepathy-logger/call-channel.c index 9775f7b..4dbc142 100644 --- a/telepathy-logger/call-channel.c +++ b/telepathy-logger/call-channel.c @@ -527,7 +527,9 @@ channel_invalidated_cb (TpProxy *proxy, gpointer user_data) { TplChannel *tpl_chan = TPL_CHANNEL (user_data); - TplObserver *observer = _tpl_observer_new (); + TplObserver *observer = _tpl_observer_dup (NULL); + + g_return_if_fail (observer); PATH_DEBUG (tpl_chan, "%s #%d %s", g_quark_to_string (domain), code, message); diff --git a/telepathy-logger/observer-internal.h b/telepathy-logger/observer-internal.h index 856e26a..972b03b 100644 --- a/telepathy-logger/observer-internal.h +++ b/telepathy-logger/observer-internal.h @@ -60,7 +60,7 @@ typedef struct GType _tpl_observer_get_type (void); -TplObserver * _tpl_observer_new (void); +TplObserver * _tpl_observer_dup (GError **error); gboolean _tpl_observer_unregister_channel (TplObserver *self, TplChannel *channel); diff --git a/telepathy-logger/observer.c b/telepathy-logger/observer.c index fcb18b8..318805c 100644 --- a/telepathy-logger/observer.c +++ b/telepathy-logger/observer.c @@ -291,36 +291,12 @@ tpl_observer_get_property (GObject *object, } } -/* Singleton Constructor */ -static GObject * -tpl_observer_constructor (GType type, - guint n_props, - GObjectConstructParam *props) -{ - GObject *retval; - - if (observer_singleton) - retval = g_object_ref (observer_singleton); - else - { - retval = G_OBJECT_CLASS (_tpl_observer_parent_class)->constructor (type, - n_props, props); - - observer_singleton = TPL_OBSERVER (retval); - g_object_add_weak_pointer (retval, (gpointer *) & observer_singleton); - } - - return retval; -} - - static void _tpl_observer_class_init (TplObserverClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); TpBaseClientClass *base_clt_cls = TP_BASE_CLIENT_CLASS (klass); - object_class->constructor = tpl_observer_constructor; object_class->dispose = tpl_observer_dispose; object_class->get_property = tpl_observer_get_property; @@ -431,22 +407,47 @@ tpl_observer_dispose (GObject *obj) TplObserver * -_tpl_observer_new (void) +_tpl_observer_dup (GError **error) { - TpDBusDaemon *dbus; - TplObserver *result; + /* WARNING Not thread safe */ + if (G_UNLIKELY (observer_singleton == NULL)) + { + GError *dbus_error = NULL; + TpDBusDaemon *dbus = tp_dbus_daemon_dup (&dbus_error); + TpSimpleClientFactory *factory; + + if (dbus == NULL) + { + g_propagate_error (error, dbus_error); + return NULL; + } + + factory = (TpSimpleClientFactory *) tp_automatic_client_factory_new (dbus); + + /* Pre-select feature to be initialized. */ + tp_simple_client_factory_add_contact_features_varargs (factory, + TP_CONTACT_FEATURE_ALIAS, + TP_CONTACT_FEATURE_AVATAR_TOKEN, + TP_CONTACT_FEATURE_INVALID); - dbus = tp_dbus_daemon_dup (NULL); - g_return_val_if_fail (dbus != NULL, NULL); + observer_singleton = g_object_new (TPL_TYPE_OBSERVER, + "factory", factory, + "name", "Logger", + "uniquify-name", FALSE, + NULL); - result = g_object_new (TPL_TYPE_OBSERVER, - "dbus-daemon", dbus, - "name", "Logger", - "uniquify-name", FALSE, - NULL); + g_object_add_weak_pointer (G_OBJECT (observer_singleton), + (gpointer *) &observer_singleton); + + g_object_unref (dbus); + g_object_unref (factory); + } + else + { + g_object_ref (observer_singleton); + } - g_object_unref (dbus); - return result; + return observer_singleton; } /** diff --git a/telepathy-logger/streamed-media-channel.c b/telepathy-logger/streamed-media-channel.c index b51b639..15d831d 100644 --- a/telepathy-logger/streamed-media-channel.c +++ b/telepathy-logger/streamed-media-channel.c @@ -474,7 +474,9 @@ on_channel_invalidated_cb (TpProxy *proxy, gpointer user_data) { TplChannel *tpl_chan = TPL_CHANNEL (user_data); - TplObserver *observer = _tpl_observer_new (); + TplObserver *observer = _tpl_observer_dup (NULL); + + g_return_if_fail (observer); PATH_DEBUG (tpl_chan, "%s #%d %s", g_quark_to_string (domain), code, message); diff --git a/telepathy-logger/text-channel.c b/telepathy-logger/text-channel.c index 2eba427..9761575 100644 --- a/telepathy-logger/text-channel.c +++ b/telepathy-logger/text-channel.c @@ -287,7 +287,9 @@ on_channel_invalidated_cb (TpProxy *proxy, gpointer user_data) { TplChannel *tpl_chan = TPL_CHANNEL (user_data); - TplObserver *observer = _tpl_observer_new (); + TplObserver *observer = _tpl_observer_dup (NULL); + + g_return_if_fail (observer); PATH_DEBUG (tpl_chan, "%s #%d %s", g_quark_to_string (domain), code, message); diff --git a/tests/dbus/test-tpl-observer.c b/tests/dbus/test-tpl-observer.c index 5e07bb9..b15811e 100644 --- a/tests/dbus/test-tpl-observer.c +++ b/tests/dbus/test-tpl-observer.c @@ -21,11 +21,11 @@ main (int argc, char **argv) g_type_init (); - obs = _tpl_observer_new (); + obs = _tpl_observer_dup (NULL); /* TplObserver is a singleton, be sure both references point to the same * memory address */ - obs2 = _tpl_observer_new (); + obs2 = _tpl_observer_dup (NULL); g_assert (obs == obs2); /* unref the second singleton pointer and check that the it is still -- 1.7.6.4