]> git.0d.be Git - empathy.git/commitdiff
add empathy_connection_managers_call_when_ready
authorGuillaume Desmottes <guillaume.desmottes@collabora.co.uk>
Thu, 21 Jan 2010 11:57:18 +0000 (11:57 +0000)
committerGuillaume Desmottes <guillaume.desmottes@collabora.co.uk>
Fri, 22 Jan 2010 11:58:40 +0000 (11:58 +0000)
That's easier to use than checking if ready and connecting a callback.

libempathy/empathy-connection-managers.c
libempathy/empathy-connection-managers.h

index 5d381075eda87d6ac69fa23e67cdd1c9308ad203..7085b507bab2d4a481ac87ebeba99c2a1cd1f111 100644 (file)
@@ -291,3 +291,66 @@ empathy_connection_managers_get_cms_num (EmpathyConnectionManagers *self)
 
   return g_list_length (priv->cms);
 }
+
+typedef struct
+{
+  EmpathyConnectionManagers *self;
+  EmpathyConnectionManagersWhenReadyCb callback;
+  gpointer user_data;
+  guint ready_id;
+} CallWhenReadyContext;
+
+static void
+call_when_ready_ctx_free (CallWhenReadyContext *ctx)
+{
+  g_signal_handler_disconnect (ctx->self, ctx->ready_id);
+  g_object_unref (ctx->self);
+  g_slice_free (CallWhenReadyContext, ctx);
+}
+
+static void
+cwr_ready (EmpathyConnectionManagers *self,
+    GParamSpec *spec,
+    CallWhenReadyContext *ctx)
+{
+  g_assert (ctx->callback != NULL);
+
+  ctx->callback (self, NULL, ctx->user_data);
+
+  call_when_ready_ctx_free (ctx);
+}
+
+static CallWhenReadyContext *
+call_when_ready_ctx_new (EmpathyConnectionManagers *self,
+    EmpathyConnectionManagersWhenReadyCb callback,
+    gpointer user_data)
+{
+  CallWhenReadyContext *ctx = g_slice_new (CallWhenReadyContext);
+
+  ctx->self = g_object_ref (self);
+  ctx->callback = callback;
+  ctx->user_data = user_data;
+
+  ctx->ready_id = g_signal_connect (self, "notify::ready",
+      G_CALLBACK (cwr_ready), ctx);
+
+  return ctx;
+}
+
+void
+empathy_connection_managers_call_when_ready (
+    EmpathyConnectionManagers *self,
+    EmpathyConnectionManagersWhenReadyCb callback,
+    gpointer user_data)
+{
+  EmpathyConnectionManagersPriv *priv = GET_PRIV (self);
+
+  if (priv->ready)
+    {
+      callback (self, NULL, user_data);
+    }
+  else
+    {
+      call_when_ready_ctx_new (self, callback, user_data);
+    }
+}
index 17289d36dfff7404f7af1da6a230b5ac28192bb2..956f243157f6e06e6ef8cb9beb45848478a916f9 100644 (file)
@@ -72,6 +72,16 @@ guint empathy_connection_managers_get_cms_num
 TpConnectionManager *empathy_connection_managers_get_cm (
   EmpathyConnectionManagers *managers, const gchar *cm);
 
+typedef void (*EmpathyConnectionManagersWhenReadyCb) (
+    EmpathyConnectionManagers *managers,
+    const GError *error,
+    gpointer user_data);
+
+void empathy_connection_managers_call_when_ready (
+    EmpathyConnectionManagers *managers,
+    EmpathyConnectionManagersWhenReadyCb callback,
+    gpointer user_data);
+
 G_END_DECLS
 
 #endif /* #ifndef __EMPATHY_CONNECTION_MANAGERS_H__*/