diff options
author | Xavier Claessens <xavier.claessens@collabora.com> | 2014-03-29 16:00:05 -0400 |
---|---|---|
committer | Xavier Claessens <xavier.claessens@collabora.com> | 2014-03-29 16:00:05 -0400 |
commit | bdb6dd9ff6aaae5690b29916c8f497a920014d1c (patch) | |
tree | 80b625b3baaebc7f2c7d61375041a77f2e6f20f1 | |
parent | 3ec480675c078c44f949e7e1f33b59ce0b5aa3b8 (diff) | |
download | telepathy-glib-bdb6dd9ff6aaae5690b29916c8f497a920014d1c.tar.gz |
TpClientFactory: Add a singleton factory
tp_client_factory_dup()'s documentation is a preview from what
it will be in a future commit. I did not write a temporary
documentation in the meantime.
-rw-r--r-- | docs/reference/telepathy-glib/telepathy-glib-sections.txt | 3 | ||||
-rw-r--r-- | telepathy-glib/client-factory.c | 101 | ||||
-rw-r--r-- | telepathy-glib/client-factory.h | 3 |
3 files changed, 107 insertions, 0 deletions
diff --git a/docs/reference/telepathy-glib/telepathy-glib-sections.txt b/docs/reference/telepathy-glib/telepathy-glib-sections.txt index 04c1064d7..8ba81eafb 100644 --- a/docs/reference/telepathy-glib/telepathy-glib-sections.txt +++ b/docs/reference/telepathy-glib/telepathy-glib-sections.txt @@ -5728,6 +5728,9 @@ tp_cli_call_stream_endpoint_add_signals TpClientFactory TpClientFactoryClass tp_client_factory_new +tp_client_factory_dup +tp_client_factory_set_default +tp_client_factory_can_set_default tp_client_factory_get_dbus_daemon <SUBSECTION> tp_client_factory_ensure_account diff --git a/telepathy-glib/client-factory.c b/telepathy-glib/client-factory.c index ed9080a4d..7dfb58c6a 100644 --- a/telepathy-glib/client-factory.c +++ b/telepathy-glib/client-factory.c @@ -125,6 +125,7 @@ #include "telepathy-glib/client-factory.h" +#include <telepathy-glib/automatic-client-factory.h> #include <telepathy-glib/util.h> #define DEBUG_FLAG TP_DEBUG_CLIENT @@ -484,6 +485,106 @@ tp_client_factory_new (TpDBusDaemon *dbus) NULL); } +static GWeakRef singleton; + +/** + * tp_client_factory_dup: + * @error: Used to raise an error if getting the session #GDBusConnection fails + * + * Get a reference to a #TpClientFactory singleton. It can fail and block only + * if the session #GDBusConnection singleton doesn't exist yet. It is thus + * recommended to call g_bus_get() before using a #TpClientFactory if the + * application must not block. + * + * By default it will create a #TpAutomaticClientFactory. + * + * Returns: (transfer full): a reference to a #TpClientFactory singleton. + * Since: 0.UNRELEASED + */ +TpClientFactory * +tp_client_factory_dup (GError **error) +{ + TpClientFactory *self; + + self = g_weak_ref_get (&singleton); + if (self == NULL) + { + TpDBusDaemon *dbus; + + dbus = tp_dbus_daemon_dup (error); + if (dbus == NULL) + return NULL; + + self = tp_automatic_client_factory_new (dbus); + g_weak_ref_set (&singleton, self); + g_object_unref (dbus); + } + + return self; +} + +/** + * tp_client_factory_set_default: + * @self: a #TpClientFactory + * + * Define the #TpClientFactory singleton that will be returned by + * tp_client_factory_dup(). + * + * This function may only be called before the first call to + * tp_client_factory_dup(), and may not be called more than once. Applications + * which use a custom #TpClientFactory and want it to be the default factory + * should call this. + * + * Only a weak reference is taken on @self. It is the caller's responsibility + * to keep it alive. If @self is disposed after calling this function, the + * next call to tp_client_factory_dup() will return a newly created + * #TpClientFactory. + * + * Since: 0.UNRELEASED + */ +void +tp_client_factory_set_default (TpClientFactory *self) +{ + TpClientFactory *tmp; + + g_return_if_fail (TP_IS_CLIENT_FACTORY (self)); + + tmp = g_weak_ref_get (&singleton); + if (tmp != NULL) + { + CRITICAL ("tp_client_factory_set_default() may only be called once and" + "before first call of tp_client_factory_dup()"); + g_object_unref (tmp); + g_return_if_reached (); + } + + g_weak_ref_set (&singleton, self); +} + +/** + * tp_client_factory_can_set_default: + * + * Check if tp_client_factory_set_default() has already successfully been + * called. + * + * Returns: %TRUE if tp_client_factory_set_default() has already successfully + * been called in this process, %FALSE otherwise. + * + * Since: 0.UNRELEASED + */ +gboolean +tp_client_factory_can_set_default (void) +{ + TpClientFactory *tmp; + gboolean ret; + + tmp = g_weak_ref_get (&singleton); + ret = (tmp == NULL); + g_clear_object (&tmp); + + return ret; +} + /** * tp_client_factory_get_dbus_daemon: * @self: a #TpClientFactory object diff --git a/telepathy-glib/client-factory.h b/telepathy-glib/client-factory.h index 38afd5cb7..38bccb5df 100644 --- a/telepathy-glib/client-factory.h +++ b/telepathy-glib/client-factory.h @@ -124,6 +124,9 @@ GType tp_client_factory_get_type (void); TpClientFactoryClass)) TpClientFactory * tp_client_factory_new (TpDBusDaemon *dbus); +TpClientFactory * tp_client_factory_dup (GError **error); +void tp_client_factory_set_default (TpClientFactory *self); +gboolean tp_client_factory_can_set_default (void); TpDBusDaemon *tp_client_factory_get_dbus_daemon (TpClientFactory *self); |