[frogr] add fsp_session_set_location_async



commit 5cca43f5726611a9f36b380cb013a4059f26742a
Author: Christophe Fergeau <teuf gnome org>
Date:   Sat Aug 13 21:29:51 2011 +0200

    add fsp_session_set_location_async
    
    This method is used to set geolocation information on an image.
    All the needed code is direct copy and paste from the license code,
    so there's room for code factoring here...

 src/flicksoup/fsp-data.h    |   13 ++++++
 src/flicksoup/fsp-error.h   |    3 +-
 src/flicksoup/fsp-parser.c  |   30 +++++++++++++++
 src/flicksoup/fsp-parser.h  |    5 ++
 src/flicksoup/fsp-session.c |   88 +++++++++++++++++++++++++++++++++++++++++++
 src/flicksoup/fsp-session.h |   12 ++++++
 6 files changed, 150 insertions(+), 1 deletions(-)
---
diff --git a/src/flicksoup/fsp-data.h b/src/flicksoup/fsp-data.h
index 5e5dae6..ad2fd15 100644
--- a/src/flicksoup/fsp-data.h
+++ b/src/flicksoup/fsp-data.h
@@ -96,6 +96,19 @@ typedef enum {
 } FspLicense;
 
 typedef enum {
+  FSP_LOCATION_CONTEXT_UNKNOWN    = 0,
+  FSP_LOCATION_CONTEXT_INDOORS    = 1,
+  FSP_LOCATION_CONTEXT_OUTDOORS   = 2
+} FspLocationContext;
+
+typedef struct {
+    gdouble latitude;
+    gdouble longitude;
+    unsigned char accuracy;
+    FspLocationContext context;
+} FspLocation;
+
+typedef enum {
   FSP_ROTATION_NONE = 0,
   FSP_ROTATION_90   = 90,
   FSP_ROTATION_180  = 180,
diff --git a/src/flicksoup/fsp-error.h b/src/flicksoup/fsp-error.h
index dec15f8..f3ba521 100644
--- a/src/flicksoup/fsp-error.h
+++ b/src/flicksoup/fsp-error.h
@@ -94,7 +94,8 @@ typedef enum {
   FSP_ERROR_METHOD_GROUP_GET_LIST,
   FSP_ERROR_METHOD_GROUP_ADD_PHOTO,
   FSP_ERROR_METHOD_TAG_GET_LIST,
-  FSP_ERROR_METHOD_SET_LICENSE
+  FSP_ERROR_METHOD_SET_LICENSE,
+  FSP_ERROR_METHOD_SET_LOCATION
 } FspErrorMethod;
 
 FspError
diff --git a/src/flicksoup/fsp-parser.c b/src/flicksoup/fsp-parser.c
index 8550ede..dc06fbb 100644
--- a/src/flicksoup/fsp-parser.c
+++ b/src/flicksoup/fsp-parser.c
@@ -97,6 +97,9 @@ _get_tags_list_parser                   (xmlDoc  *doc,
 static gpointer
 _set_license_parser                     (xmlDoc  *doc,
                                          GError **error);
+static gpointer
+_set_location_parser                     (xmlDoc  *doc,
+                                          GError **error);
 
 static FspDataPhotoInfo *
 _get_photo_info_from_node               (xmlNode *node);
@@ -282,6 +285,8 @@ _get_error_method_from_parser           (gpointer (*body_parser)
     error_method = FSP_ERROR_METHOD_TAG_GET_LIST;
   else if (body_parser == _set_license_parser)
     error_method = FSP_ERROR_METHOD_SET_LICENSE;
+  else if (body_parser == _set_location_parser)
+    error_method = FSP_ERROR_METHOD_SET_LOCATION;
 
   return error_method;
 }
@@ -857,6 +862,14 @@ _set_license_parser                     (xmlDoc  *doc,
   return NULL;
 }
 
+static gpointer
+_set_location_parser                     (xmlDoc  *doc,
+                                         GError **error)
+{
+  /* Dummy parser, as there is no response for this method */
+  return NULL;
+}
+
 static FspDataPhotoInfo *
 _get_photo_info_from_node               (xmlNode *node)
 {
@@ -1343,3 +1356,20 @@ fsp_parser_set_license                  (FspParser  *self,
   /* No return value for this method */
   return GINT_TO_POINTER ((gint)(*error == NULL));
 }
+
+gpointer
+fsp_parser_set_location                  (FspParser  *self,
+                                          const gchar      *buffer,
+                                          gulong            buf_size,
+                                          GError          **error)
+{
+  g_return_val_if_fail (FSP_IS_PARSER (self), NULL);
+  g_return_val_if_fail (buffer != NULL, NULL);
+
+  /* Process the response */
+  _process_xml_response (self, buffer, buf_size,
+                         _set_location_parser, error);
+
+  /* No return value for this method */
+  return GINT_TO_POINTER ((gint)(*error == NULL));
+}
diff --git a/src/flicksoup/fsp-parser.h b/src/flicksoup/fsp-parser.h
index ad5fe29..24dc963 100644
--- a/src/flicksoup/fsp-parser.h
+++ b/src/flicksoup/fsp-parser.h
@@ -139,6 +139,11 @@ fsp_parser_set_license                  (FspParser  *self,
                                          gulong            buf_size,
                                          GError          **error);
 
+gpointer
+fsp_parser_set_location                  (FspParser  *self,
+                                          const gchar      *buffer,
+                                          gulong            buf_size,
+                                          GError          **error);
 G_END_DECLS
 
 #endif
diff --git a/src/flicksoup/fsp-session.c b/src/flicksoup/fsp-session.c
index 8090af6..d51af16 100644
--- a/src/flicksoup/fsp-session.c
+++ b/src/flicksoup/fsp-session.c
@@ -266,6 +266,11 @@ _set_license_soup_session_cb            (SoupSession *session,
                                          SoupMessage *msg,
                                          gpointer     data);
 
+static void
+_set_location_soup_session_cb            (SoupSession *session,
+                                          SoupMessage *msg,
+                                          gpointer     data);
+
 /* Private API */
 
 static void
@@ -1269,6 +1274,19 @@ _set_license_soup_session_cb            (SoupSession *session,
                          data);
 }
 
+static void
+_set_location_soup_session_cb            (SoupSession *session,
+                                          SoupMessage *msg,
+                                          gpointer     data)
+{
+  g_assert (SOUP_IS_MESSAGE (msg));
+  g_assert (data != NULL);
+
+  /* Handle message with the right parser */
+  _handle_soup_response (msg,
+                         (FspParserFunc) fsp_parser_set_location,
+                         data);
+}
 
 /* Public API */
 
@@ -2168,3 +2186,73 @@ fsp_session_set_license_finish          (FspSession    *self,
 
   return result ? TRUE : FALSE;
 }
+
+void
+fsp_session_set_location_async           (FspSession          *self,
+                                          const gchar         *photo_id,
+                                          FspLocation         *location,
+                                          GCancellable        *cancellable,
+                                          GAsyncReadyCallback  callback,
+                                          gpointer             data)
+{
+  FspSessionPrivate *priv = NULL;
+  SoupSession *soup_session = NULL;
+  gchar lat_str[G_ASCII_DTOSTR_BUF_SIZE];
+  gchar lon_str[G_ASCII_DTOSTR_BUF_SIZE];
+  gchar *accuracy_str = NULL;
+  gchar *context_str = NULL;
+  gchar *url = NULL;
+  gchar *signed_query = NULL;
+
+  g_return_if_fail (FSP_IS_SESSION (self));
+  g_return_if_fail (photo_id != NULL);
+
+  /* Build the signed url */
+  priv = self->priv;
+  g_ascii_dtostr (lat_str, sizeof (lat_str), location->latitude);
+  g_ascii_dtostr (lon_str, sizeof (lon_str), location->longitude);
+  if ((location->accuracy >= 1) && (location->accuracy <= 16))
+    {
+      /* restricting to [1..16] since these are the values accepted by flickr */
+      accuracy_str = g_strdup_printf ("%d", location->accuracy);
+    }
+  context_str = g_strdup_printf ("%d", location->context);
+  /* FIXME: not sure how to handle the optional 'accuracy' here... */
+  signed_query = _get_signed_query (priv->secret,
+                                    "method", "flickr.photos.geo.setLocation",
+                                    "api_key", priv->api_key,
+                                    "auth_token", priv->token,
+                                    "photo_id", photo_id,
+                                    "lat", lat_str,
+                                    "lon", lon_str,
+                                    "context", context_str,
+                                    NULL);
+  g_free (accuracy_str);
+  g_free (context_str);
+  url = g_strdup_printf ("%s/?%s", FLICKR_API_BASE_URL, signed_query);
+  g_free (signed_query);
+
+  /* Perform the async request */
+  soup_session = _get_soup_session (self);
+  _perform_async_request (soup_session, url,
+                          _set_location_soup_session_cb, G_OBJECT (self),
+                          cancellable, callback, fsp_session_set_location_async, data);
+
+  g_free (url);
+}
+
+gboolean
+fsp_session_set_location_finish          (FspSession    *self,
+                                          GAsyncResult  *res,
+                                          GError       **error)
+{
+  gpointer result = NULL;
+
+  g_return_val_if_fail (FSP_IS_SESSION (self), FALSE);
+  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
+
+  result = _finish_async_request (G_OBJECT (self), res,
+                                  fsp_session_set_location_async, error);
+
+  return result ? TRUE : FALSE;
+}
diff --git a/src/flicksoup/fsp-session.h b/src/flicksoup/fsp-session.h
index db3e319..4fc86d8 100644
--- a/src/flicksoup/fsp-session.h
+++ b/src/flicksoup/fsp-session.h
@@ -241,6 +241,18 @@ fsp_session_set_license_finish          (FspSession    *self,
                                          GAsyncResult  *res,
                                          GError       **error);
 
+void
+fsp_session_set_location_async           (FspSession          *self,
+                                          const gchar         *photo_id,
+                                          FspLocation         *location,
+                                          GCancellable        *cancellable,
+                                          GAsyncReadyCallback  callback,
+                                          gpointer             data);
+
+gboolean
+fsp_session_set_location_finish          (FspSession    *self,
+                                          GAsyncResult  *res,
+                                          GError       **error);
 G_END_DECLS
 
 #endif



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]