summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon McVittie <simon.mcvittie@collabora.co.uk>2014-04-04 16:25:22 +0100
committerSimon McVittie <simon.mcvittie@collabora.co.uk>2014-04-07 16:16:06 +0100
commit98369b4c6bd0e5219ced8af8e251e895a63dc267 (patch)
treeb96f10c15a4d5739e0bb686f310d3edd9a67a323
parent0e8a971c0fc8a5e895dd8bd197ec47a491e489c1 (diff)
tp_account_get_avatar_async: rename to _dup_, return GBytes + MIME type
Also be (somewhat) cancellable, while I'm there.
-rw-r--r--docs/reference/telepathy-glib/telepathy-glib-sections.txt4
-rw-r--r--telepathy-glib/account.c96
-rw-r--r--telepathy-glib/account.h13
-rw-r--r--telepathy-glib/versions/main-1.0.abi2
-rw-r--r--tests/dbus/account.c28
5 files changed, 84 insertions, 59 deletions
diff --git a/docs/reference/telepathy-glib/telepathy-glib-sections.txt b/docs/reference/telepathy-glib/telepathy-glib-sections.txt
index 56c3c1eeb..e5f9c8313 100644
--- a/docs/reference/telepathy-glib/telepathy-glib-sections.txt
+++ b/docs/reference/telepathy-glib/telepathy-glib-sections.txt
@@ -3954,8 +3954,8 @@ tp_account_set_nickname_async
tp_account_set_nickname_finish
tp_account_get_normalized_name
tp_account_get_supersedes
-tp_account_get_avatar_async
-tp_account_get_avatar_finish
+tp_account_dup_avatar_async
+tp_account_dup_avatar_finish
tp_account_set_avatar_async
tp_account_set_avatar_finish
tp_account_bind_connection_status_to_property
diff --git a/telepathy-glib/account.c b/telepathy-glib/account.c
index b887d3ef7..8fd7c55d8 100644
--- a/telepathy-glib/account.c
+++ b/telepathy-glib/account.c
@@ -2105,7 +2105,7 @@ tp_account_class_init (TpAccountClass *klass)
* TpAccount::avatar-changed:
* @self: a #TpAccount
*
- * Emitted when the avatar changes. Call tp_account_get_avatar_async()
+ * Emitted when the avatar changes. Call tp_account_dup_avatar_async()
* to get the new avatar data.
*
* Since: 0.23.0
@@ -3311,93 +3311,109 @@ _tp_account_got_avatar_cb (TpProxy *proxy,
gpointer user_data,
GObject *weak_object)
{
- GSimpleAsyncResult *result = G_SIMPLE_ASYNC_RESULT (user_data);
+ GTask *task = user_data;
if (error != NULL)
{
DEBUG ("Failed to get avatar: %s", error->message);
- g_simple_async_result_set_from_error (result, error);
+ g_task_return_error (task, g_error_copy (error));
}
else if (!G_VALUE_HOLDS (out_Value, TP_STRUCT_TYPE_AVATAR))
{
DEBUG ("Avatar had wrong type: %s", G_VALUE_TYPE_NAME (out_Value));
- g_simple_async_result_set_error (result, TP_ERROR, TP_ERROR_CONFUSED,
+ g_task_return_new_error (task, TP_ERROR, TP_ERROR_CONFUSED,
"Incorrect type for Avatar property");
}
else
{
- GValueArray *avatar;
- GArray *res;
- const GArray *tmp;
- const gchar *mime_type;
-
- avatar = g_value_get_boxed (out_Value);
- tp_value_array_unpack (avatar, 2,
- &tmp,
- &mime_type);
-
- res = g_array_sized_new (FALSE, FALSE, 1, tmp->len);
- g_array_append_vals (res, tmp->data, tmp->len);
- g_simple_async_result_set_op_res_gpointer (result, res,
- (GDestroyNotify) g_array_unref);
+ /* we just put the GValueArray in the task, and use a non-trivial
+ * finish function to split it into data and MIME type */
+ g_task_return_pointer (task, g_value_dup_boxed (out_Value),
+ (GDestroyNotify) tp_value_array_free);
}
- g_simple_async_result_complete (result);
- g_object_unref (result);
+ g_object_unref (task);
}
/**
- * tp_account_get_avatar_async:
+ * tp_account_dup_avatar_async:
* @account: a #TpAccount
+ * @cancellable: (allow-none): may be used to cancel the async request
* @callback: a callback to call when the request is satisfied
* @user_data: data to pass to @callback
*
* Requests an asynchronous get of @account's avatar. When
* the operation is finished, @callback will be called. You can then call
- * tp_account_get_avatar_finish() to get the result of the operation.
+ * tp_account_dup_avatar_finish() to get the result of the operation.
*
* Since: 0.9.0
*/
void
-tp_account_get_avatar_async (TpAccount *account,
+tp_account_dup_avatar_async (TpAccount *account,
+ GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
- GSimpleAsyncResult *result;
+ GTask *task;
g_return_if_fail (TP_IS_ACCOUNT (account));
+ /* this method makes no sense to call for its side-effects */
+ g_return_if_fail (callback != NULL);
- result = g_simple_async_result_new (G_OBJECT (account),
- callback, user_data, tp_account_get_avatar_finish);
+ task = g_task_new (account, cancellable, callback, user_data);
+ g_task_set_source_tag (task, tp_account_dup_avatar_async);
tp_cli_dbus_properties_call_get (account, -1,
TP_IFACE_ACCOUNT_INTERFACE_AVATAR1, "Avatar", _tp_account_got_avatar_cb,
- result, NULL, G_OBJECT (account));
+ task, NULL, NULL);
}
/**
- * tp_account_get_avatar_finish:
+ * tp_account_dup_avatar_finish:
* @account: a #TpAccount
* @result: a #GAsyncResult
+ * @mime_type: (out) (allow-none) (transfer full): optionally used to
+ * return the MIME-type of the avatar, typically "image/png"
* @error: a #GError to fill
*
- * Finishes an async get operation of @account's avatar.
- *
- * Beware that the returned value is only valid until @result is freed.
- * Copy it with g_array_ref() if you need to keep it for longer.
- *
- * Returns: (element-type guchar) (transfer none): a #GArray of #guchar
- * containing the bytes of the account's avatar, or %NULL on failure
+ * Interprets the result of tp_account_dup_avatar_async().
*
- * Since: 0.9.0
+ * Returns: the bytes of the account's avatar, or %NULL on failure
*/
-const GArray *
-tp_account_get_avatar_finish (TpAccount *account,
+GBytes *
+tp_account_dup_avatar_finish (TpAccount *account,
GAsyncResult *result,
+ gchar **mime_type,
GError **error)
{
- _tp_implement_finish_return_copy_pointer (account,
- tp_account_get_avatar_finish, /* do not copy */);
+ GValueArray *va;
+ const GArray *tmp;
+ const gchar *mime_type_tmp;
+ GBytes *ret;
+
+ g_return_val_if_fail (g_task_is_valid (result, account), NULL);
+ g_return_val_if_fail (g_async_result_is_tagged (result,
+ tp_account_dup_avatar_async), NULL);
+
+ if (mime_type != NULL)
+ *mime_type = NULL;
+
+ /* take ownership */
+ va = g_task_propagate_pointer (G_TASK (result), error);
+
+ if (va == NULL)
+ return NULL;
+
+ tp_value_array_unpack (va, 2,
+ &tmp,
+ &mime_type_tmp);
+
+ if (mime_type != NULL)
+ *mime_type = g_strdup (mime_type_tmp);
+
+ ret = g_bytes_new (tmp->data, tmp->len);
+ tp_value_array_free (va);
+ return ret;
}
static void
diff --git a/telepathy-glib/account.h b/telepathy-glib/account.h
index 14215138a..3d7c71eb3 100644
--- a/telepathy-glib/account.h
+++ b/telepathy-glib/account.h
@@ -210,11 +210,16 @@ const gchar * const *
/* ugh, gtk-doc */
tp_account_get_supersedes (TpAccount *self);
-void tp_account_get_avatar_async (TpAccount *account,
- GAsyncReadyCallback callback, gpointer user_data);
+void tp_account_dup_avatar_async (TpAccount *account,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
-const GArray *tp_account_get_avatar_finish (TpAccount *account,
- GAsyncResult *result, GError **error);
+G_GNUC_WARN_UNUSED_RESULT
+GBytes *tp_account_dup_avatar_finish (TpAccount *account,
+ GAsyncResult *result,
+ gchar **mime_type,
+ GError **error);
void tp_account_set_avatar_async (TpAccount *self,
const guchar *avatar,
diff --git a/telepathy-glib/versions/main-1.0.abi b/telepathy-glib/versions/main-1.0.abi
index 28e75e971..387bb7962 100644
--- a/telepathy-glib/versions/main-1.0.abi
+++ b/telepathy-glib/versions/main-1.0.abi
@@ -46,7 +46,6 @@ tp_account_dup_storage_identifier
tp_account_dup_storage_specific_information_async
tp_account_dup_storage_specific_information_finish
tp_account_get_automatic_presence
-tp_account_get_avatar_async
tp_base_client_get_dbus_connection
tp_base_connection_get_dbus_connection
tp_base_connection_manager_get_dbus_connection
@@ -59,7 +58,6 @@ tp_client_factory_ensure_debug_client
tp_client_factory_ensure_logger
tp_client_factory_get_dbus_connection
tp_client_factory_set_default
-tp_account_get_avatar_finish
tp_account_get_changing_presence
tp_account_get_cm_name
tp_account_get_connect_automatically
diff --git a/tests/dbus/account.c b/tests/dbus/account.c
index d4daf1f0e..fc1b4da0c 100644
--- a/tests/dbus/account.c
+++ b/tests/dbus/account.c
@@ -665,7 +665,9 @@ static void
test_avatar (Test *test,
gconstpointer mode)
{
- const GArray *blob;
+ GBytes *blob;
+ gsize len;
+ gchar *mime;
GError *error = NULL;
test->account = tp_tests_account_new (test->dbus, ACCOUNT_PATH, NULL);
@@ -674,17 +676,20 @@ test_avatar (Test *test,
tp_proxy_prepare_async (test->account, NULL, account_prepare_cb, test);
g_main_loop_run (test->mainloop);
- tp_account_get_avatar_async (test->account,
+ tp_account_dup_avatar_async (test->account, NULL,
tp_tests_result_ready_cb, &test->result);
tp_tests_run_until_result (&test->result);
- blob = tp_account_get_avatar_finish (
- test->account, test->result, &error);
+ blob = tp_account_dup_avatar_finish (
+ test->account, test->result, &mime, &error);
g_assert_no_error (error);
- g_assert_cmpuint (blob->len, ==, 4);
- g_assert_cmpstr (((char *) blob->data), ==, ":-)");
+ g_assert_cmpstr ((const char *) g_bytes_get_data (blob, &len), ==, ":-)");
+ g_assert_cmpuint (len, ==, 4);
+ g_assert_cmpstr (mime, ==, "text/plain");
+ g_bytes_unref (blob);
+ g_free (mime);
tp_clear_object (&test->result);
/* change the avatar */
@@ -694,18 +699,19 @@ test_avatar (Test *test,
tp_tests_simple_account_set_avatar (test->account_service, ":-(");
g_main_loop_run (test->mainloop);
- tp_account_get_avatar_async (test->account,
+ tp_account_dup_avatar_async (test->account, NULL,
tp_tests_result_ready_cb, &test->result);
tp_tests_run_until_result (&test->result);
- blob = tp_account_get_avatar_finish (
- test->account, test->result, &error);
+ blob = tp_account_dup_avatar_finish (
+ test->account, test->result, NULL, &error);
g_assert_no_error (error);
g_assert (blob != NULL);
- g_assert_cmpuint (blob->len, ==, 4);
- g_assert_cmpstr (((char *) blob->data), ==, ":-(");
+ g_assert_cmpstr ((const char *) g_bytes_get_data (blob, &len), ==, ":-(");
+ g_assert_cmpuint (len, ==, 4);
+ g_bytes_unref (blob);
tp_clear_object (&test->result);
}