[next] telepathy-glib: future-account: start adding set_parameter functions

Jonny Lamb jonny at kemper.freedesktop.org
Fri May 11 02:41:33 PDT 2012


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

Author: Jonny Lamb <jonny.lamb at collabora.co.uk>
Date:   Thu Apr 26 10:14:26 2012 +0100

future-account: start adding set_parameter functions

_set_parameter is useful for introspection, but the _set_parameter_*
functions are more useful when using C, and some of us are indeed
still using C.

Signed-off-by: Jonny Lamb <jonny.lamb at collabora.co.uk>

---

 telepathy-glib/future-account.c |   91 +++++++++++++++++++++++++++++++++++++++
 telepathy-glib/future-account.h |    7 +++
 tests/dbus/future-account.c     |   37 ++++++++++++++++
 3 files changed, 135 insertions(+), 0 deletions(-)

diff --git a/telepathy-glib/future-account.c b/telepathy-glib/future-account.c
index 4c5d110..3e3ce70 100644
--- a/telepathy-glib/future-account.c
+++ b/telepathy-glib/future-account.c
@@ -61,6 +61,8 @@ struct _TpFutureAccountPrivate {
   gchar *cm_name;
   gchar *proto_name;
   gchar *display_name;
+
+  GHashTable *parameters;
 };
 
 G_DEFINE_TYPE (TpFutureAccount, tp_future_account, G_TYPE_OBJECT)
@@ -78,6 +80,7 @@ enum {
   PROP_CONNECTION_MANAGER,
   PROP_PROTOCOL,
   PROP_DISPLAY_NAME,
+  PROP_PARAMETERS,
   N_PROPS
 };
 
@@ -89,6 +92,21 @@ tp_future_account_init (TpFutureAccount *self)
 }
 
 static void
+tp_future_account_constructed (GObject *object)
+{
+  TpFutureAccount *self = TP_FUTURE_ACCOUNT (object);
+  TpFutureAccountPrivate *priv = self->priv;
+  void (*chain_up) (GObject *) =
+    ((GObjectClass *) tp_future_account_parent_class)->constructed;
+
+  priv->parameters = g_hash_table_new_full (g_str_hash, g_str_equal,
+      g_free, (GDestroyNotify) tp_g_value_slice_free);
+
+  if (chain_up != NULL)
+    chain_up (object);
+}
+
+static void
 tp_future_account_get_property (GObject *object,
     guint prop_id,
     GValue *value,
@@ -110,6 +128,9 @@ tp_future_account_get_property (GObject *object,
     case PROP_DISPLAY_NAME:
       g_value_set_string (value, self->priv->display_name);
       break;
+    case PROP_PARAMETERS:
+      g_value_set_boxed (value, self->priv->parameters);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -156,6 +177,8 @@ tp_future_account_dispose (GObject *object)
 
   priv->dispose_has_run = TRUE;
 
+  tp_clear_pointer (&priv->parameters, g_hash_table_unref);
+
   /* release any references held by the object here */
 
   if (G_OBJECT_CLASS (tp_future_account_parent_class)->dispose != NULL)
@@ -185,6 +208,7 @@ tp_future_account_class_init (TpFutureAccountClass *klass)
 
   g_type_class_add_private (klass, sizeof (TpFutureAccountPrivate));
 
+  object_class->constructed = tp_future_account_constructed;
   object_class->get_property = tp_future_account_get_property;
   object_class->set_property = tp_future_account_set_property;
   object_class->dispose = tp_future_account_dispose;
@@ -239,6 +263,18 @@ tp_future_account_class_init (TpFutureAccountClass *klass)
           "The account's display name",
           NULL,
           G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
+
+  /**
+   * TpFutureAccount:parameters:
+   *
+   * TODO
+   */
+  g_object_class_install_property (object_class, PROP_PARAMETERS,
+      g_param_spec_boxed ("parameters",
+          "Parameters",
+          "Connection parameters of the account",
+          G_TYPE_HASH_TABLE,
+          G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
 }
 
 /**
@@ -289,3 +325,58 @@ tp_future_account_set_display_name (TpFutureAccount *self,
   g_free (priv->display_name);
   priv->display_name = g_strdup (name);
 }
+
+/**
+ * tp_future_account_set_parameter:
+ * @self: a #TpFutureAccount
+ * @key: the parameter key
+ * @value: (transfer none): a variant containing the parameter value
+ *
+ * Set an account parameter, @key, to @value.
+ */
+void
+tp_future_account_set_parameter (TpFutureAccount *self,
+    const gchar *key,
+    GVariant *value)
+{
+  TpFutureAccountPrivate *priv;
+  GValue one = G_VALUE_INIT, *two;
+
+  g_return_if_fail (TP_IS_FUTURE_ACCOUNT (self));
+  g_return_if_fail (key != NULL);
+  g_return_if_fail (value != NULL);
+
+  priv = self->priv;
+
+  dbus_g_value_parse_g_variant (value, &one);
+  two = tp_g_value_slice_dup (&one);
+
+  g_hash_table_insert (priv->parameters, g_strdup (key), two);
+
+  g_value_unset (&one);
+}
+
+/**
+ * tp_future_account_set_parameter_string: (skip)
+ * @self: a #TpFutureAccount
+ * @key: the parameter key
+ * @value: the parameter value
+ *
+ * Set an account parameter, @key, to @value.
+ */
+void
+tp_future_account_set_parameter_string (TpFutureAccount *self,
+    const gchar *key,
+    const gchar *value)
+{
+  TpFutureAccountPrivate *priv;
+
+  g_return_if_fail (TP_IS_FUTURE_ACCOUNT (self));
+  g_return_if_fail (key != NULL);
+  g_return_if_fail (value != NULL);
+
+  priv = self->priv;
+
+  g_hash_table_insert (priv->parameters, g_strdup (key),
+      tp_g_value_slice_new_string (value));
+}
diff --git a/telepathy-glib/future-account.h b/telepathy-glib/future-account.h
index be40e76..8aef7ac 100644
--- a/telepathy-glib/future-account.h
+++ b/telepathy-glib/future-account.h
@@ -65,6 +65,13 @@ TpFutureAccount * tp_future_account_new (TpAccountManager *account_manager,
 void tp_future_account_set_display_name (TpFutureAccount *self,
     const gchar *name);
 
+/* parameters */
+void tp_future_account_set_parameter (TpFutureAccount *self,
+    const gchar *key, GVariant *value);
+
+void tp_future_account_set_parameter_string (TpFutureAccount *self,
+    const gchar *key, const gchar *value);
+
 G_END_DECLS
 
 #endif
diff --git a/tests/dbus/future-account.c b/tests/dbus/future-account.c
index 5320b0f..291fac0 100644
--- a/tests/dbus/future-account.c
+++ b/tests/dbus/future-account.c
@@ -93,6 +93,41 @@ test_properties (Test *test,
   g_free (display_name);
 }
 
+static void
+test_parameters (Test *test,
+    gconstpointer data G_GNUC_UNUSED)
+{
+  GVariant *v_str, *v_int;
+  GHashTable *params;
+
+  test->account = tp_future_account_new (test->account_manager,
+      "gabble", "jabber");
+
+  v_str = g_variant_new_string ("banana");
+  tp_future_account_set_parameter (test->account, "cheese", v_str);
+
+  v_int = g_variant_new_uint32 (42);
+  tp_future_account_set_parameter (test->account, "life", v_int);
+
+  tp_future_account_set_parameter_string (test->account,
+      "great", "expectations");
+
+  g_object_get (test->account,
+      "parameters", &params,
+      NULL);
+
+  g_assert_cmpuint (g_hash_table_size (params), ==, 3);
+
+  g_assert_cmpstr (tp_asv_get_string (params, "cheese"), ==, "banana");
+  g_assert_cmpuint (tp_asv_get_uint32 (params, "life", NULL), ==, 42);
+  g_assert_cmpstr (tp_asv_get_string (params, "great"), ==, "expectations");
+
+  g_variant_unref (v_str);
+  g_variant_unref (v_int);
+
+  g_hash_table_unref (params);
+}
+
 int
 main (int argc,
     char **argv)
@@ -107,6 +142,8 @@ main (int argc,
   g_test_add ("/future-account/new", Test, NULL, setup, test_new, teardown);
   g_test_add ("/future-account/properties", Test, NULL, setup,
       test_properties, teardown);
+  g_test_add ("/future-account/parameters", Test, NULL, setup,
+      test_parameters, teardown);
 
   return g_test_run ();
 }



More information about the telepathy-commits mailing list