[Telepathy-commits] [telepathy-glib/master] tp_connection_get_contact_attributes: add

Simon McVittie simon.mcvittie at collabora.co.uk
Mon Dec 1 03:13:56 PST 2008


This is a wrapper for GetContactAttributes that correctly updates the
handle reference tracking infrastructure if @hold is TRUE.
---
 docs/reference/telepathy-glib-sections.txt |    1 +
 telepathy-glib/connection-handles.c        |  135 ++++++++++++++++++++++++++++
 telepathy-glib/connection.h                |   13 +++
 3 files changed, 149 insertions(+), 0 deletions(-)

diff --git a/docs/reference/telepathy-glib-sections.txt b/docs/reference/telepathy-glib-sections.txt
index 1c5456e..2a748aa 100644
--- a/docs/reference/telepathy-glib-sections.txt
+++ b/docs/reference/telepathy-glib-sections.txt
@@ -2080,6 +2080,7 @@ tp_connection_get_status
 TpConnectionRequestHandlesCb
 tp_connection_request_handles
 TpConnectionHoldHandlesCb
+tp_connection_get_contact_attributes
 tp_connection_hold_handles
 tp_connection_unref_handles
 tp_connection_init_known_interfaces
diff --git a/telepathy-glib/connection-handles.c b/telepathy-glib/connection-handles.c
index 6d1ff10..575ee06 100644
--- a/telepathy-glib/connection-handles.c
+++ b/telepathy-glib/connection-handles.c
@@ -649,3 +649,138 @@ tp_connection_request_handles (TpConnection *self,
       (const gchar **) context->ids, connection_requested_handles,
       context, request_handles_context_free, weak_object);
 }
+
+
+typedef struct {
+    tp_cli_connection_interface_contacts_callback_for_get_contact_attributes
+        callback;
+    gpointer user_data;
+    GDestroyNotify destroy;
+    gboolean hold;
+} GetContactAttributesContext;
+
+
+static void
+get_contact_attributes_context_free (gpointer p)
+{
+  GetContactAttributesContext *c = p;
+
+  if (c->destroy != NULL)
+    c->destroy (c->user_data);
+
+  g_slice_free (GetContactAttributesContext, c);
+}
+
+
+static void
+connection_got_contact_attributes (TpConnection *self,
+                                   GHashTable *attributes,
+                                   const GError *error,
+                                   gpointer user_data,
+                                   GObject *weak_object)
+{
+  GetContactAttributesContext *c = user_data;
+
+  DEBUG ("%u handles, hold=%c", g_hash_table_size (attributes),
+      c->hold ? 'T' : 'F');
+
+  if (c->hold)
+    {
+      GHashTableIter iter;
+      gpointer key, value;
+      GArray *handles = g_array_sized_new (FALSE, FALSE, sizeof (guint),
+          g_hash_table_size (attributes));
+
+      g_hash_table_iter_init (&iter, attributes);
+
+      while (g_hash_table_iter_next (&iter, &key, &value))
+        {
+          TpHandle handle = GPOINTER_TO_UINT (key);
+
+          DEBUG ("- %u", handle);
+
+          g_array_append_val (handles, handle);
+        }
+
+      /* remember that we have a ref */
+      _tp_connection_ref_handles (self, TP_HANDLE_TYPE_CONTACT, handles);
+      g_array_free (handles, TRUE);
+    }
+
+  c->callback (self, attributes, error, c->user_data, weak_object);
+}
+
+
+/**
+ * tp_connection_get_contact_attributes:
+ * @self: a connection
+ * @timeout_ms: the timeout in milliseconds, or -1 to use the default
+ * @n_handles: the number of handles in @handles (must be at least 1)
+ * @handles: an array of handles
+ * @interfaces: a #GStrv of interfaces
+ * @hold: if %TRUE, the callback will hold one reference to each valid handle
+ * @callback: called on success or failure (unless @weak_object has become
+ *  unreferenced)
+ * @user_data: arbitrary user-supplied data
+ * @destroy: called to destroy @user_data after calling @callback, or when
+ *  @weak_object becomes unreferenced (whichever occurs sooner)
+ * @weak_object: if not %NULL, an object to be weakly referenced: if it is
+ *  destroyed, @callback will not be called
+ *
+ * Return (via a callback) any number of attributes of the given handles, and
+ * if they are valid and @hold is TRUE, hold a reference to them.
+ *
+ * This is a thin wrapper around the GetContactAttributes D-Bus method, and
+ * should be used in preference to
+ * tp_cli_connection_interface_contacts_get_contact_attributes().
+ * The #TpContact API provides a higher-level abstraction which should
+ * usually be used instead.
+ *
+ * @callback will later be called with the attributes of those of the given
+ * handles that were valid. Invalid handles are simply omitted from the
+ * parameter to the callback.
+ *
+ * If @hold is %TRUE, the @callback is given one reference to each handle
+ * that appears as a key in the callback's @attributes parameter.
+ */
+void
+tp_connection_get_contact_attributes (TpConnection *self,
+    gint timeout_ms,
+    guint n_handles,
+    const TpHandle *handles,
+    const gchar * const *interfaces,
+    gboolean hold,
+    tp_cli_connection_interface_contacts_callback_for_get_contact_attributes callback,
+    gpointer user_data,
+    GDestroyNotify destroy,
+    GObject *weak_object)
+{
+  GetContactAttributesContext *c;
+  GArray *a;
+  guint i;
+
+  DEBUG ("%u handles, hold=%c", n_handles, hold ? 'T' : 'F');
+
+  for (i = 0; i < n_handles; i++)
+    DEBUG ("- %u", handles[i]);
+
+  g_return_if_fail (TP_IS_CONNECTION (self));
+  g_return_if_fail (n_handles >= 1);
+  g_return_if_fail (handles != NULL);
+  g_return_if_fail (callback != NULL);
+
+  c = g_slice_new0 (GetContactAttributesContext);
+  a = g_array_sized_new (FALSE, FALSE, sizeof (guint), n_handles);
+
+  g_array_append_vals (a, handles, n_handles);
+
+  c->destroy = destroy;
+  c->user_data = user_data;
+  c->callback = callback;
+  c->hold = hold;
+
+  tp_cli_connection_interface_contacts_call_get_contact_attributes (self, -1,
+      a, (const gchar **) interfaces, hold, connection_got_contact_attributes,
+      c, get_contact_attributes_context_free, weak_object);
+  g_array_free (a, TRUE);
+}
diff --git a/telepathy-glib/connection.h b/telepathy-glib/connection.h
index 557b464..248316f 100644
--- a/telepathy-glib/connection.h
+++ b/telepathy-glib/connection.h
@@ -131,4 +131,17 @@ G_END_DECLS
 
 #include <telepathy-glib/_gen/tp-cli-connection.h>
 
+G_BEGIN_DECLS
+
+/* connection-handles.c again - this has to come after the auto-generated
+ * stuff because it uses an auto-generated typedef */
+
+void tp_connection_get_contact_attributes (TpConnection *self,
+    gint timeout_ms, guint n_handles, const TpHandle *handles,
+    const gchar * const *interfaces, gboolean hold,
+    tp_cli_connection_interface_contacts_callback_for_get_contact_attributes callback,
+    gpointer user_data, GDestroyNotify destroy, GObject *weak_object);
+
+G_END_DECLS
+
 #endif
-- 
1.5.6.5




More information about the Telepathy-commits mailing list