From c466b91a7538be6e91a484773f63a5cf848189a0 Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Tue, 29 May 2012 12:49:39 +0200 Subject: [PATCH] TpHandleRepoIface: add tp_handle_ensure_async() A default implementation is provided that just use tp_handle_ensure() and return the handle in an idle callback. https://bugs.freedesktop.org/show_bug.cgi?id=50341 --- docs/reference/telepathy-glib-sections.txt | 2 + telepathy-glib/handle-repo-internal.h | 10 +++ telepathy-glib/handle-repo.c | 99 ++++++++++++++++++++++++++++ telepathy-glib/handle-repo.h | 17 +++++ 4 files changed, 128 insertions(+) diff --git a/docs/reference/telepathy-glib-sections.txt b/docs/reference/telepathy-glib-sections.txt index 302c508..f8d7790 100644 --- a/docs/reference/telepathy-glib-sections.txt +++ b/docs/reference/telepathy-glib-sections.txt @@ -252,6 +252,8 @@ tp_handle_set_qdata tp_handle_get_qdata tp_handle_ensure tp_handle_lookup +tp_handle_ensure_async +tp_handle_ensure_finish TP_HANDLE_REPO_IFACE TP_IS_HANDLE_REPO_IFACE diff --git a/telepathy-glib/handle-repo-internal.h b/telepathy-glib/handle-repo-internal.h index 9441fc0..931a51c 100644 --- a/telepathy-glib/handle-repo-internal.h +++ b/telepathy-glib/handle-repo-internal.h @@ -74,6 +74,16 @@ struct _TpHandleRepoIfaceClass { TpHandle (*lookup_handle) (TpHandleRepoIface *self, const char *id, gpointer context, GError **error); + void (*ensure_handle_async) (TpHandleRepoIface *self, + TpBaseConnection *connection, + const gchar *id, + gpointer context, + GAsyncReadyCallback callback, + gpointer user_data); + TpHandle (*ensure_handle_finish) (TpHandleRepoIface *self, + GAsyncResult *result, + GError **error); + void (*set_qdata) (TpHandleRepoIface *repo, TpHandle handle, GQuark key_id, gpointer data, GDestroyNotify destroy); gpointer (*get_qdata) (TpHandleRepoIface *repo, TpHandle handle, diff --git a/telepathy-glib/handle-repo.c b/telepathy-glib/handle-repo.c index 4918831..13b6814 100644 --- a/telepathy-glib/handle-repo.c +++ b/telepathy-glib/handle-repo.c @@ -37,6 +37,7 @@ #include #include +#include G_DEFINE_INTERFACE (TpHandleRepoIface, tp_handle_repo_iface, G_TYPE_OBJECT); @@ -302,6 +303,54 @@ tp_handle_ensure (TpHandleRepoIface *self, id, context, error); } +/** + * tp_handle_ensure_async: (skip) + * @self: A handle repository implementation + * @connection: the #TpBaseConnection using this handle repo + * @id: A string whose handle is required + * @context: User data to be passed to the normalization callback + * @callback: a callback to call when the operation finishes + * @user_data: data to pass to @callback + * + * Asyncronously normalize an identifier and create an handle for it. This could + * involve a server round-trip. This should be used instead of + * tp_handle_ensure() for user provided contact identifiers, but it is not + * necessary for identifiers from the server. + * + * Since: 0.UNRELEASED + */ +void +tp_handle_ensure_async (TpHandleRepoIface *self, + TpBaseConnection *connection, + const gchar *id, + gpointer context, + GAsyncReadyCallback callback, + gpointer user_data) +{ + return TP_HANDLE_REPO_IFACE_GET_CLASS (self)->ensure_handle_async (self, + connection, id, context, callback, user_data); +} + +/** + * tp_handle_ensure_finish: (skip) + * @self: A handle repository implementation + * @result: a #GAsyncResult + * @error: a #GError to fill + * + * Finishes tp_handle_ensure_async() + * + * Returns: non-0 #TpHandle if the operation was successful, otherwise 0. + * + * Since: 0.UNRELEASED + */ +TpHandle +tp_handle_ensure_finish (TpHandleRepoIface *self, + GAsyncResult *result, + GError **error) +{ + return TP_HANDLE_REPO_IFACE_GET_CLASS (self)->ensure_handle_finish (self, + result, error); +} /** * tp_handle_lookup: (skip) @@ -378,10 +427,60 @@ tp_handle_get_qdata (TpHandleRepoIface *repo, TpHandle handle, } static void +_tp_handle_repo_default_ensure_handle_async (TpHandleRepoIface *self, + TpBaseConnection *connection, + const gchar *id, + gpointer context, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GSimpleAsyncResult *result; + TpHandle handle; + GError *error = NULL; + + result = g_simple_async_result_new (G_OBJECT (self), callback, user_data, + _tp_handle_repo_default_ensure_handle_async); + + handle = tp_handle_ensure (self, id, context, &error); + if (handle == 0) + { + g_simple_async_result_take_error (result, error); + } + else + { + g_simple_async_result_set_op_res_gpointer (result, + GUINT_TO_POINTER (handle), NULL); + } + + g_simple_async_result_complete_in_idle (result); + + g_object_unref (result); +} + +static TpHandle +_tp_handle_repo_default_ensure_handle_finish (TpHandleRepoIface *self, + GAsyncResult *result, + GError **error) +{ + GSimpleAsyncResult *simple = (GSimpleAsyncResult *) result; + + g_return_val_if_fail (g_simple_async_result_is_valid (result, + G_OBJECT (self), NULL), 0); + + if (g_simple_async_result_propagate_error (simple, error)) + return 0; + + return GPOINTER_TO_UINT (g_simple_async_result_get_op_res_gpointer (simple)); +} + +static void tp_handle_repo_iface_default_init (TpHandleRepoIfaceInterface *iface) { GParamSpec *param_spec; + iface->ensure_handle_async = _tp_handle_repo_default_ensure_handle_async; + iface->ensure_handle_finish = _tp_handle_repo_default_ensure_handle_finish; + param_spec = g_param_spec_uint ("handle-type", "Handle type", "The TpHandleType held in this handle repository.", 0, G_MAXUINT32, 0, diff --git a/telepathy-glib/handle-repo.h b/telepathy-glib/handle-repo.h index cfe58e4..e8de641 100644 --- a/telepathy-glib/handle-repo.h +++ b/telepathy-glib/handle-repo.h @@ -26,11 +26,16 @@ #include +#include + #include #include G_BEGIN_DECLS +/* Forward declaration to avoid circular includes */ +typedef struct _TpBaseConnection TpBaseConnection; + /* Forward declaration because it's in the HandleRepo API */ #define TP_TYPE_HANDLE_SET (tp_handle_set_get_type ()) @@ -111,6 +116,18 @@ TpHandle tp_handle_ensure (TpHandleRepoIface *self, const gchar *id, gpointer context, GError **error) G_GNUC_WARN_UNUSED_RESULT; +_TP_AVAILABLE_IN_UNRELEASED +void tp_handle_ensure_async (TpHandleRepoIface *self, + TpBaseConnection *connection, + const gchar *id, + gpointer context, + GAsyncReadyCallback callback, + gpointer user_data); +_TP_AVAILABLE_IN_UNRELEASED +TpHandle tp_handle_ensure_finish (TpHandleRepoIface *self, + GAsyncResult *result, + GError **error); + void tp_handle_set_qdata (TpHandleRepoIface *repo, TpHandle handle, GQuark key_id, gpointer data, GDestroyNotify destroy); gpointer tp_handle_get_qdata (TpHandleRepoIface *repo, TpHandle handle, -- 1.7.9.5