[next] telepathy-glib: tp_g_socket_address_from_g_variant, tp_address_g_variant_from_g_socket_address: add

Simon McVittie smcv at kemper.freedesktop.org
Mon Oct 8 08:23:32 PDT 2012


Module: telepathy-glib
Branch: next
Commit: 93dd462e247ee8138b5e5088c72a1f9d6be0d047
URL:    http://cgit.freedesktop.org/telepathy/telepathy-glib/commit/?id=93dd462e247ee8138b5e5088c72a1f9d6be0d047

Author: Simon McVittie <simon.mcvittie at collabora.co.uk>
Date:   Wed Sep 19 13:28:35 2012 +0100

tp_g_socket_address_from_g_variant, tp_address_g_variant_from_g_socket_address: add

https://bugs.freedesktop.org/show_bug.cgi?id=55101

---

 docs/reference/telepathy-glib-sections.txt |    2 +
 telepathy-glib/gnio-util.c                 |   68 +++++++++++++++++++++++++++-
 telepathy-glib/gnio-util.h                 |    9 ++++
 tests/gnio-util.c                          |   54 ++++++++++------------
 4 files changed, 101 insertions(+), 32 deletions(-)

diff --git a/docs/reference/telepathy-glib-sections.txt b/docs/reference/telepathy-glib-sections.txt
index de86b22..e740bc2 100644
--- a/docs/reference/telepathy-glib-sections.txt
+++ b/docs/reference/telepathy-glib-sections.txt
@@ -1772,6 +1772,8 @@ tp_utf8_make_valid
 <INCLUDE>telepathy-glib/telepathy-glib.h</INCLUDE>
 tp_g_socket_address_from_variant
 tp_address_variant_from_g_socket_address
+tp_g_socket_address_from_g_variant
+tp_address_g_variant_from_g_socket_address
 tp_unix_connection_receive_credentials_with_byte
 tp_unix_connection_receive_credentials_with_byte_async
 tp_unix_connection_receive_credentials_with_byte_finish
diff --git a/telepathy-glib/gnio-util.c b/telepathy-glib/gnio-util.c
index 190f1a3..8b8c461 100644
--- a/telepathy-glib/gnio-util.c
+++ b/telepathy-glib/gnio-util.c
@@ -65,7 +65,8 @@
 /**
  * tp_g_socket_address_from_variant:
  * @type: a Telepathy socket address type
- * @variant: an initialised #GValue containing an address variant
+ * @variant: an initialised #GValue containing an address variant,
+ *  as encoded by dbus-glib
  * @error: return location for a #GError (or NULL)
  *
  * Converts an address variant stored in a #GValue into a #GSocketAddress that
@@ -184,7 +185,7 @@ tp_g_socket_address_from_variant (TpSocketAddressType type,
  * @error: return location for a #GError (or NULL)
  *
  * Converts a #GSocketAddress to a #GValue address variant that can be used
- * with Telepathy.
+ * with Telepathy and dbus-glib.
  *
  * Returns: a newly allocated #GValue, free with tp_g_value_slice_free()
  */
@@ -281,6 +282,69 @@ tp_address_variant_from_g_socket_address (GSocketAddress *address,
   return variant;
 }
 
+/**
+ * tp_g_socket_address_from_g_variant:
+ * @type: a Telepathy socket address type
+ * @variant: a socket address as encoded by Telepathy according to @type
+ * @error: return location for a #GError (or %NULL)
+ *
+ * Converts an address variant stored in a #GVariant into a #GSocketAddress
+ * that can be used to make a socket connection with GIO.
+ *
+ * If @variant is a floating reference, this function takes ownership
+ * of it.
+ *
+ * Returns: a newly allocated #GSocketAddress for the given variant, or %NULL
+ * on error
+ *
+ * Since: 0.UNRELEASED
+ */
+GSocketAddress *
+tp_g_socket_address_from_g_variant (TpSocketAddressType type,
+    GVariant *variant,
+    GError **error)
+{
+  GValue value = G_VALUE_INIT;
+  GSocketAddress *ret;
+
+  g_variant_ref_sink (variant);
+  dbus_g_value_parse_g_variant (variant, &value);
+  g_variant_unref (variant);
+  ret = tp_g_socket_address_from_variant (type, &value, error);
+  g_value_unset (&value);
+  return ret;
+}
+
+/**
+ * tp_address_g_variant_from_g_socket_address:
+ * @address: a #GSocketAddress to convert
+ * @type: optional return of the Telepathy socket type (or NULL)
+ * @error: return location for a #GError (or NULL)
+ *
+ * Converts a #GSocketAddress to a #GVariant address variant that can be used
+ * with Telepathy.
+ *
+ * Returns: (transfer none): a new variant with a floating reference, or %NULL
+ *
+ * Since: 0.UNRELEASED
+ */
+GVariant *
+tp_address_g_variant_from_g_socket_address (GSocketAddress *address,
+    TpSocketAddressType *type,
+    GError **error)
+{
+  GValue *value = tp_address_variant_from_g_socket_address (address,
+      type, error);
+  GVariant *ret;
+
+  if (value == NULL)
+    return NULL;
+
+  ret = dbus_g_value_build_g_variant (value);
+  tp_g_value_slice_free (value);
+  return ret;
+}
+
 #ifdef HAVE_GIO_UNIX
 static gboolean
 _tp_unix_connection_send_credentials_with_byte (GUnixConnection *connection,
diff --git a/telepathy-glib/gnio-util.h b/telepathy-glib/gnio-util.h
index d82c17d..0bf58d3 100644
--- a/telepathy-glib/gnio-util.h
+++ b/telepathy-glib/gnio-util.h
@@ -41,6 +41,15 @@ GValue *tp_address_variant_from_g_socket_address (GSocketAddress *address,
     TpSocketAddressType *type,
     GError **error) G_GNUC_WARN_UNUSED_RESULT;
 
+_TP_AVAILABLE_IN_UNRELEASED
+GSocketAddress *tp_g_socket_address_from_g_variant (TpSocketAddressType type,
+    GVariant *variant,
+    GError **error) G_GNUC_WARN_UNUSED_RESULT;
+_TP_AVAILABLE_IN_UNRELEASED
+GVariant *tp_address_g_variant_from_g_socket_address (GSocketAddress *address,
+    TpSocketAddressType *type,
+    GError **error) G_GNUC_WARN_UNUSED_RESULT;
+
 gboolean tp_unix_connection_send_credentials_with_byte (
     GSocketConnection *connection,
     guchar byte,
diff --git a/tests/gnio-util.c b/tests/gnio-util.c
index 0657b70..90de7a7 100644
--- a/tests/gnio-util.c
+++ b/tests/gnio-util.c
@@ -26,8 +26,6 @@
 static void
 test_variant_to_sockaddr_ipv4 (void)
 {
-  GValueArray *array;
-  GValue value = { 0, };
   GSocketAddress *sockaddr;
   GInetSocketAddress *inetaddr;
   GInetAddress *hostaddr;
@@ -35,19 +33,8 @@ test_variant_to_sockaddr_ipv4 (void)
   guint16 port;
   GError *error = NULL;
 
-  /* set up an address variant */
-  array = tp_value_array_build (2,
-      G_TYPE_STRING, IPV4_ADDR,
-      G_TYPE_UINT, PORT,
-      G_TYPE_INVALID);
-
-  g_value_init (&value, TP_STRUCT_TYPE_SOCKET_ADDRESS_IPV4);
-  g_value_take_boxed (&value, array);
-
-  /* convert to a GSocketAddress */
-  sockaddr = tp_g_socket_address_from_variant (TP_SOCKET_ADDRESS_TYPE_IPV4,
-                                               &value, &error);
-  g_value_unset (&value);
+  sockaddr = tp_g_socket_address_from_g_variant (TP_SOCKET_ADDRESS_TYPE_IPV4,
+      g_variant_new_parsed ("(%s, %u)", IPV4_ADDR, (guint32) PORT), &error);
 
   /* check the socket address */
   g_assert_no_error (error);
@@ -70,8 +57,6 @@ test_variant_to_sockaddr_ipv4 (void)
 static void
 test_variant_to_sockaddr_ipv6 (void)
 {
-  GValueArray *array;
-  GValue value = { 0, };
   GSocketAddress *sockaddr;
   GInetSocketAddress *inetaddr;
   GInetAddress *hostaddr;
@@ -79,19 +64,8 @@ test_variant_to_sockaddr_ipv6 (void)
   guint16 port;
   GError *error = NULL;
 
-  /* set up an address variant */
-  array = tp_value_array_build (2,
-      G_TYPE_STRING, IPV6_ADDR,
-      G_TYPE_UINT, PORT,
-      G_TYPE_INVALID);
-
-  g_value_init (&value, TP_STRUCT_TYPE_SOCKET_ADDRESS_IPV6);
-  g_value_take_boxed (&value, array);
-
-  /* convert to a GSocketAddress */
-  sockaddr = tp_g_socket_address_from_variant (TP_SOCKET_ADDRESS_TYPE_IPV6,
-                                               &value, &error);
-  g_value_unset (&value);
+  sockaddr = tp_g_socket_address_from_g_variant (TP_SOCKET_ADDRESS_TYPE_IPV6,
+      g_variant_new_parsed ("(%s, %u)", IPV6_ADDR, (guint32) PORT), &error);
 
   /* check the socket address */
   g_assert_no_error (error);
@@ -117,6 +91,7 @@ test_sockaddr_to_variant_ipv4 (void)
   GInetAddress *hostaddr = g_inet_address_new_from_string (IPV4_ADDR);
   GSocketAddress *sockaddr = g_inet_socket_address_new (hostaddr, PORT);
   GValue *variant, *value;
+  GVariant *gvariant, *other;
   GValueArray *array;
   TpSocketAddressType type;
   GError *error = NULL;
@@ -124,6 +99,9 @@ test_sockaddr_to_variant_ipv4 (void)
   g_object_unref (hostaddr);
 
   variant = tp_address_variant_from_g_socket_address (sockaddr, &type, &error);
+  g_assert_no_error (error);
+  gvariant = tp_address_g_variant_from_g_socket_address (sockaddr, &type,
+      &error);
   g_object_unref (sockaddr);
 
   g_assert_no_error (error);
@@ -142,6 +120,12 @@ test_sockaddr_to_variant_ipv4 (void)
   g_assert_cmpuint (g_value_get_uint (value), ==, PORT);
 
   tp_g_value_slice_free (variant);
+
+  g_assert (g_variant_is_floating (gvariant));
+  other = g_variant_new_parsed ("(%s, %u)", IPV4_ADDR, (guint32) PORT);
+  g_assert (g_variant_equal (gvariant, other));
+  g_variant_unref (gvariant);
+  g_variant_unref (other);
 }
 
 static void
@@ -151,12 +135,16 @@ test_sockaddr_to_variant_ipv6 (void)
   GSocketAddress *sockaddr = g_inet_socket_address_new (hostaddr, PORT);
   GValue *variant, *value;
   GValueArray *array;
+  GVariant *gvariant, *other;
   TpSocketAddressType type;
   GError *error = NULL;
 
   g_object_unref (hostaddr);
 
   variant = tp_address_variant_from_g_socket_address (sockaddr, &type, &error);
+  g_assert_no_error (error);
+  gvariant = tp_address_g_variant_from_g_socket_address (sockaddr, &type,
+      &error);
   g_object_unref (sockaddr);
 
   g_assert_no_error (error);
@@ -175,6 +163,12 @@ test_sockaddr_to_variant_ipv6 (void)
   g_assert_cmpuint (g_value_get_uint (value), ==, PORT);
 
   tp_g_value_slice_free (variant);
+
+  g_assert (g_variant_is_floating (gvariant));
+  other = g_variant_new_parsed ("(%s, %u)", IPV6_ADDR, (guint32) PORT);
+  g_assert (g_variant_equal (gvariant, other));
+  g_variant_unref (gvariant);
+  g_variant_unref (other);
 }
 
 #ifdef HAVE_GIO_UNIX



More information about the telepathy-commits mailing list