diff options
author | Álvaro Peña <alvaropg@gmail.com> | 2013-05-09 20:42:22 +0200 |
---|---|---|
committer | Álvaro Peña <alvaropg@gmail.com> | 2013-05-09 20:42:22 +0200 |
commit | d6aee851d70d45d91c6383185745e467a293d1c0 (patch) | |
tree | ff9d7d661382437f88a76f2639108dbeb1251980 | |
parent | 078f0739d4f1062765683d7314c4d097c5e047d3 (diff) | |
download | libgfbgraph-d6aee851d70d45d91c6383185745e467a293d1c0.tar.gz |
Added more usefull functions
-rw-r--r-- | gfbgraph/gfbgraph-connectable.c | 2 | ||||
-rw-r--r-- | gfbgraph/gfbgraph-node.c | 46 | ||||
-rw-r--r-- | gfbgraph/gfbgraph-node.h | 1 | ||||
-rw-r--r-- | gfbgraph/gfbgraph-user.c | 141 | ||||
-rw-r--r-- | gfbgraph/gfbgraph-user.h | 5 |
5 files changed, 190 insertions, 5 deletions
diff --git a/gfbgraph/gfbgraph-connectable.c b/gfbgraph/gfbgraph-connectable.c index 7561730..6233709 100644 --- a/gfbgraph/gfbgraph-connectable.c +++ b/gfbgraph/gfbgraph-connectable.c @@ -177,7 +177,7 @@ gfbgraph_connectable_get_connection_path (GFBGraphConnectable *self, GType node_ * Normally, Facebook Graph API returns the connections in the same way, using JSON objects, * with a root object called "data". * - * Returns: + * Returns: a #GList of #GFBGraphNode with the same #GType as @self. **/ GList* gfbgraph_connectable_default_parse_connected_data (GFBGraphConnectable *self, const gchar *payload, GError **error) diff --git a/gfbgraph/gfbgraph-node.c b/gfbgraph/gfbgraph-node.c index fcd190b..79a768d 100644 --- a/gfbgraph/gfbgraph-node.c +++ b/gfbgraph/gfbgraph-node.c @@ -32,6 +32,8 @@ **/ #include <rest/rest-proxy-call.h> +#include <json-glib/json-glib.h> +#include <string.h> #include "gfbgraph-common.h" #include "gfbgraph-connectable.h" @@ -194,6 +196,50 @@ gfbgraph_node_new (void) } /** + * gfbgraph_node_new_from_id: + * @id: a const #gchar with the node ID. + * @node_type: a #GFBGraphNode type #GType. + * @authorizer: a #GFBGraphAuthorizer. + * @error: (allow-none): a #GError or %NULL. + * + * Retrieve a node object as a #GFBgraphNode of #node_type type, with the given @id from the Facebook Graph. + * + * Returns: a #GFBGraphNode or %NULL. + **/ +GFBGraphNode* + gfbgraph_node_new_from_id (GFBGraphAuthorizer *authorizer, const gchar *id, GType node_type, GError **error) +{ + GFBGraphNode *node; + RestProxyCall *rest_call; + + g_return_val_if_fail ((strlen (id) > 0), NULL); + g_return_val_if_fail (GFBGRAPH_IS_AUTHORIZER (authorizer), NULL); + g_return_val_if_fail (g_type_is_a (node_type, GFBGRAPH_TYPE_NODE), NULL); + + rest_call = gfbgraph_new_rest_call (authorizer); + rest_proxy_call_set_method (rest_call, "GET"); + rest_proxy_call_set_function (rest_call, id); + + node = NULL; + if (rest_proxy_call_sync (rest_call, error)) { + JsonParser *jparser; + JsonNode *jnode; + const gchar *payload; + + payload = rest_proxy_call_get_payload (rest_call); + jparser = json_parser_new (); + if (json_parser_load_from_data (jparser, payload, -1, error)) { + jnode = json_parser_get_root (jparser); + node = GFBGRAPH_NODE (json_gobject_deserialize (node_type, jnode)); + } + + g_object_unref (jparser); + } + + return node; +} + +/** * gfbgraph_node_get_connection_nodes: * @node: a #GFBGraphNode object which retrieve the connected nodes. * @node_type: a #GFBGraphNode type #GType that determines the kind of nodes to retrieve. diff --git a/gfbgraph/gfbgraph-node.h b/gfbgraph/gfbgraph-node.h index 9fd0b64..df545a1 100644 --- a/gfbgraph/gfbgraph-node.h +++ b/gfbgraph/gfbgraph-node.h @@ -57,6 +57,7 @@ GType gfbgraph_node_get_type (void) G_GNUC_CONST; GQuark gfbgraph_node_error_quark (void) G_GNUC_CONST; GFBGraphNode* gfbgraph_node_new (void); +GFBGraphNode* gfbgraph_node_new_from_id (GFBGraphAuthorizer *authorizer, const gchar *id, GType node_type, GError **error); GList* gfbgraph_node_get_connection_nodes (GFBGraphNode *node, GType node_type, diff --git a/gfbgraph/gfbgraph-user.c b/gfbgraph/gfbgraph-user.c index 8771427..74f4c24 100644 --- a/gfbgraph/gfbgraph-user.c +++ b/gfbgraph/gfbgraph-user.c @@ -27,12 +27,12 @@ * With the "me" functions, (see gfbgraph_user_get_me()) you can query for the logged user node. **/ +#include <json-glib/json-glib.h> + #include "gfbgraph-user.h" #include "gfbgraph-album.h" #include "gfbgraph-common.h" -#include <json-glib/json-glib.h> - #define ME_FUNCTION "me" enum { @@ -49,6 +49,11 @@ typedef struct { GFBGraphUser *user; } GFBGraphUserAsyncData; +typedef struct { + GFBGraphAuthorizer *authorizer; + GList *nodes; +} GFBGraphUserConnectionAsyncData; + static void gfbgraph_user_init (GFBGraphUser *obj); static void gfbgraph_user_class_init (GFBGraphUserClass *klass); static void gfbgraph_user_finalize (GObject *obj); @@ -57,7 +62,9 @@ static void gfbgraph_user_get_property (GObject *object, guint prop_id, GValue * /* Private functions */ static void gfbgraph_user_async_data_free (GFBGraphUserAsyncData *data); +static void gfbgraph_user_connection_async_data_free (GFBGraphUserConnectionAsyncData *data); static void gfbgraph_user_get_me_async_thread (GSimpleAsyncResult *simple_async, GFBGraphAuthorizer *authorizer, GCancellable cancellable); +static void gfbgraph_user_get_albums_async_thread (GSimpleAsyncResult *simple_async, GFBGraphUser *user, GCancellable cancellable); #define GFBGRAPH_USER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), GFBGRAPH_TYPE_USER, GFBGraphUserPrivate)) @@ -147,6 +154,14 @@ gfbgraph_user_async_data_free (GFBGraphUserAsyncData *data) } static void +gfbgraph_user_connection_async_data_free (GFBGraphUserConnectionAsyncData *data) +{ + g_object_unref (data->authorizer); + + g_slice_free (GFBGraphUserConnectionAsyncData, data); +} + +static void gfbgraph_user_get_me_async_thread (GSimpleAsyncResult *simple_async, GFBGraphAuthorizer *authorizer, GCancellable cancellable) { GFBGraphUserAsyncData *data; @@ -160,12 +175,26 @@ gfbgraph_user_get_me_async_thread (GSimpleAsyncResult *simple_async, GFBGraphAut g_simple_async_result_take_error (simple_async, error); } +static void +gfbgraph_user_get_albums_async_thread (GSimpleAsyncResult *simple_async, GFBGraphUser *user, GCancellable cancellable) +{ + GFBGraphUserConnectionAsyncData *data; + GError *error; + + data = (GFBGraphUserConnectionAsyncData *) g_simple_async_result_get_op_res_gpointer (simple_async); + + error = NULL; + data->nodes = gfbgraph_user_get_albums (user, data->authorizer, &error); + if (error != NULL) + g_simple_async_result_take_error (simple_async, error); +} + /** * gfbgraph_user_new: * - * Creates a new #GFBGraphAlbum. + * Creates a new #GFBGraphUser. * - * Returns: a new #GFBGraphAlbum; unref with g_object_unref() + * Returns: a new #GFBGraphUser; unref with g_object_unref() **/ GFBGraphUser* gfbgraph_user_new (void) @@ -174,6 +203,22 @@ gfbgraph_user_new (void) } /** + * gfbgraph_user_new_from_id: + * @authorizer: a #GFBGraphAuthorizer. + * @id: a const #gchar with the user ID. + * @error: (allow-none): a #GError or %NULL. + * + * Retrieves a user from the Facebook Graph with the give ID. + * + * Returns: a new #GFBGraphUser; unref with g_object_unref() + **/ +GFBGraphUser* +gfbgraph_user_new_from_id (GFBGraphAuthorizer *authorizer, const gchar *id, GError **error) +{ + return GFBGRAPH_USER (gfbgraph_node_new_from_id (authorizer, id, GFBGRAPH_TYPE_USER, error)); +} + +/** * gfbgraph_user_get_me: * @authorizer: a #GFBGraphAuthorizer. * @error: (allow-none) a #GError or %NULL. @@ -280,3 +325,91 @@ gfbgraph_user_get_me_async_finish (GFBGraphAuthorizer *authorizer, GAsyncResult data = (GFBGraphUserAsyncData *) g_simple_async_result_get_op_res_gpointer (simple_async); return data->user; } + +/** + * gfbgraph_user_get_albums: + * @user: a #GFBGraphUser. + * @authorizer: a #GFBGraphAuthorizer. + * @error: (allow-none): An optional #GError, or %NULL. + * + * Retrieve the albums nodes owned by the @user. This functions call the function ID/albums. + * + * Returns: a #GList with the albums nodes (#GFBGraphAlbums) owned by the given user. + **/ +GList* +gfbgraph_user_get_albums (GFBGraphUser *user, GFBGraphAuthorizer *authorizer, GError **error) +{ + g_return_val_if_fail (GFBGRAPH_IS_USER (user), NULL); + g_return_val_if_fail (GFBGRAPH_IS_AUTHORIZER (authorizer), NULL); + + return gfbgraph_node_get_connection_nodes (GFBGRAPH_NODE (user), GFBGRAPH_TYPE_ALBUM, authorizer, error); +} + +/** + * gfbgraph_user_get_albums_async: + * @user: a #GFBGraphUser. + * @authorizer: a #GFBGraphAuthorizer. + * @cancellable: (allow-none): An optional #GCancellable object, or %NULL. + * @callback: (scope async): A #GAsyncReadyCallback to call when the request is completed. + * @user_data: (closure); The data to pass to @callback. + * + * Asynchronously retrieve the albums nodes owned by the @user. See gfbgraph_user_get_albums() for the + * synchronous version of this call. + * + * When the operation is finished, @callback will be called. You can then call gfbgraph_user_get_albums_async_finish() + * to get the #GList of #GFBGraphAlbums owned by the @user. + **/ +void +gfbgraph_user_get_albums_async (GFBGraphUser *user, GFBGraphAuthorizer *authorizer, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) +{ + GSimpleAsyncResult *simple_async; + GFBGraphUserConnectionAsyncData *data; + + g_return_if_fail (GFBGRAPH_IS_USER (user)); + g_return_if_fail (GFBGRAPH_IS_AUTHORIZER (authorizer)); + g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable)); + g_return_if_fail (callback != NULL); + + simple_async = g_simple_async_result_new (G_OBJECT (user), callback, user_data, gfbgraph_user_get_albums_async); + g_simple_async_result_set_check_cancellable (simple_async, cancellable); + + data = g_slice_new (GFBGraphUserConnectionAsyncData); + data->nodes = NULL; + data->authorizer = authorizer; + g_object_ref (data->authorizer); + + g_simple_async_result_set_op_res_gpointer (simple_async, data, (GDestroyNotify) gfbgraph_user_connection_async_data_free); + g_simple_async_result_run_in_thread (simple_async, (GSimpleAsyncThreadFunc) gfbgraph_user_get_albums_async_thread, G_PRIORITY_DEFAULT, cancellable); + + g_object_unref (simple_async); +} + +/** + * gfbgraph_user_get_albums_async_finish: + * @user: a #GFBGraphUser. + * @result: A #GAsyncResult. + * @error: (allow-none): An optional #GError, or %NULL. + * + * Finishes an asynchronous operation started with + * gfbgraph_user_get_albums_async(). + * + * Returns: a #GList of #GFBGraphAlbums owned by the @user. + **/ +GList* +gfbgraph_user_get_albums_async_finish (GFBGraphUser *user, GAsyncResult *result, GError **error) +{ + GSimpleAsyncResult *simple_async; + GFBGraphUserConnectionAsyncData *data; + + g_return_val_if_fail (GFBGRAPH_IS_USER (user), NULL); + g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (user), gfbgraph_user_get_albums_async), NULL); + g_return_val_if_fail (error == NULL || *error == NULL, NULL); + + simple_async = G_SIMPLE_ASYNC_RESULT (result); + + if (g_simple_async_result_propagate_error (simple_async, error)) + return NULL; + + data = (GFBGraphUserConnectionAsyncData *) g_simple_async_result_get_op_res_gpointer (simple_async); + return data->nodes; +} diff --git a/gfbgraph/gfbgraph-user.h b/gfbgraph/gfbgraph-user.h index 889004e..ed8500b 100644 --- a/gfbgraph/gfbgraph-user.h +++ b/gfbgraph/gfbgraph-user.h @@ -50,11 +50,16 @@ struct _GFBGraphUserClass { GType gfbgraph_user_get_type (void) G_GNUC_CONST; GFBGraphUser* gfbgraph_user_new (void); +GFBGraphUser* gfbgraph_user_new_from_id (GFBGraphAuthorizer *authorizer, const gchar *id, GError **error); GFBGraphUser* gfbgraph_user_get_me (GFBGraphAuthorizer *authorizer, GError **error); void gfbgraph_user_get_me_async (GFBGraphAuthorizer *authorizer, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data); GFBGraphUser* gfbgraph_user_get_me_async_finish (GFBGraphAuthorizer *authorizer, GAsyncResult *result, GError **error); +GList* gfbgraph_user_get_albums (GFBGraphUser *user, GFBGraphAuthorizer *authorizer, GError **error); +void gfbgraph_user_get_albums_async (GFBGraphUser *user, GFBGraphAuthorizer *authorizer, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data); +GList* gfbgraph_user_get_albums_async_finish (GFBGraphUser *user, GAsyncResult *result, GError **error); + G_END_DECLS #endif /* __GFBGRAPH_USER_H__ */ |