[frogr] Implemented flickr.photos.setDates API in flicksoup



commit 81337080343fe796e607cee8b962aafbd02d4e0a
Author: Mario Sanchez Prada <msanchez gnome org>
Date:   Sat Sep 13 08:44:20 2014 +0100

    Implemented flickr.photos.setDates API in flicksoup
    
    This is needed to be able to set the posted date to the taken date.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=734193

 src/examples/example.c      |   52 ++++++++++++++++++++++++++++--
 src/flicksoup/fsp-error.c   |   17 ++++++++++
 src/flicksoup/fsp-error.h   |    3 +-
 src/flicksoup/fsp-parser.c  |   31 +++++++++++++++++-
 src/flicksoup/fsp-parser.h  |    7 ++++
 src/flicksoup/fsp-session.c |   74 +++++++++++++++++++++++++++++++++++++++++++
 src/flicksoup/fsp-session.h |   14 ++++++++
 7 files changed, 192 insertions(+), 6 deletions(-)
---
diff --git a/src/examples/example.c b/src/examples/example.c
index ec24548..ab853eb 100644
--- a/src/examples/example.c
+++ b/src/examples/example.c
@@ -44,6 +44,7 @@ void added_to_photoset_cb (GObject *object, GAsyncResult *res, gpointer unused);
 void photoset_created_cb (GObject *object, GAsyncResult *res, gpointer unused);
 void get_photosets_cb (GObject *object, GAsyncResult *res, gpointer unused);
 void photo_get_info_cb (GObject *object, GAsyncResult *res, gpointer unused);
+void set_posted_date_cb (GObject *object, GAsyncResult *res, gpointer user_data);
 void get_location_cb (GObject *object, GAsyncResult *res, gpointer unused);
 void set_location_cb (GObject *object, GAsyncResult *res, gpointer unused);
 void set_license_cb (GObject *object, GAsyncResult *res, gpointer unused);
@@ -373,6 +374,37 @@ photo_get_info_cb                       (GObject      *object,
 }
 
 void
+set_posted_date_cb                      (GObject      *object,
+                                         GAsyncResult *res,
+                                         gpointer      user_data)
+{
+  FspSession *session = FSP_SESSION (object);
+  GError *error = NULL;
+  gboolean result = FALSE;
+
+  result = fsp_session_set_posted_date_finish (session, res, &error);
+  if (error != NULL)
+    {
+      g_print ("Error setting the posted date: %s\n", error->message);
+      g_error_free (error);
+    }
+  else
+    {
+      g_print ("[set_posted_date_cb]::Success! (%s)\n\n",
+               result ? "OK" : "FAIL");
+
+      /* Make a pause before continuing */
+      g_print ("Press ENTER to continue...\n\n");
+      getchar ();
+
+      /* Continue getting info about the picture */
+      g_print ("Getting info for photo %s...\n", uploaded_photo_id);
+      fsp_session_get_info (session, uploaded_photo_id, NULL,
+                            photo_get_info_cb, NULL);
+    }
+}
+
+void
 get_location_cb                         (GObject      *object,
                                          GAsyncResult *res,
                                          gpointer      user_data)
@@ -389,6 +421,10 @@ get_location_cb                         (GObject      *object,
     }
   else
     {
+      GDateTime *date_now = NULL;
+      GDateTime *date = NULL;
+      gchar* date_str = NULL;
+
       g_print ("[get_location_cb]::Success! Location got:\n");
       g_print ("[get_location_cb]::\tLatitude: %g\n", location->latitude);
       g_print ("[get_location_cb]::\tLongitude: %g\n", location->longitude);
@@ -398,15 +434,23 @@ get_location_cb                         (GObject      *object,
       g_print ("Press ENTER to continue...\n\n");
       getchar ();
 
-      /* Continue getting info about the picture */
-      g_print ("Getting info for photo %s...\n", uploaded_photo_id);
-      fsp_session_get_info (session, uploaded_photo_id, NULL,
-                            photo_get_info_cb, NULL);
+      /* Continue setting the posted date to one year ago */
+      date_now = g_date_time_new_now_local ();
+      date = g_date_time_add_years (date_now, -1);
+      date_str = g_date_time_format (date, "%Y-%m-%d %H:%M:%S");
+      g_print ("Setting posted date for photo %s to %s...\n", uploaded_photo_id, date_str);
+      fsp_session_set_posted_date (session, uploaded_photo_id, date, NULL,
+                                   set_posted_date_cb, NULL);
+
+      g_free (date_str);
+      g_date_time_unref (date);
+      g_date_time_unref (date_now);
 
       fsp_data_free (FSP_DATA (location));
     }
 }
 
+
 void
 set_location_cb                         (GObject      *object,
                                          GAsyncResult *res,
diff --git a/src/flicksoup/fsp-error.c b/src/flicksoup/fsp-error.c
index bb23f03..b79dc83 100644
--- a/src/flicksoup/fsp-error.c
+++ b/src/flicksoup/fsp-error.c
@@ -154,6 +154,19 @@ static const FspError get_location_translations [N_SPECIFIC_ERRORS] = {
   FSP_ERROR_UNKNOWN,                    /* 9 */
 };
 
+static const FspError set_dates_translations [N_SPECIFIC_ERRORS] = {
+  FSP_ERROR_UNKNOWN,                    /* 0 */
+  FSP_ERROR_PHOTO_NOT_FOUND,            /* 1 */
+  FSP_ERROR_UNKNOWN,                    /* 2 */
+  FSP_ERROR_UNKNOWN,                    /* 3 */
+  FSP_ERROR_UNKNOWN,                    /* 4 */
+  FSP_ERROR_UNKNOWN,                    /* 5 */
+  FSP_ERROR_UNKNOWN,                    /* 6 */
+  FSP_ERROR_UNKNOWN,                    /* 7 */
+  FSP_ERROR_UNKNOWN,                    /* 8 */
+  FSP_ERROR_UNKNOWN,                    /* 9 */
+};
+
 static const FspError general_translations [N_GENERAL_ERRORS] = {
   FSP_ERROR_UNKNOWN,                    /* 10 */
   FSP_ERROR_UNKNOWN,                    /* 11 */
@@ -326,6 +339,10 @@ fsp_error_get_from_response_code        (FspErrorMethod method, gint code)
           retval = get_location_translations[code];
           break;
 
+        case FSP_ERROR_METHOD_SET_DATES:
+          retval = set_dates_translations[code];
+          break;
+
         default:
           retval = FSP_ERROR_UNKNOWN;
         }
diff --git a/src/flicksoup/fsp-error.h b/src/flicksoup/fsp-error.h
index 451349f..39625e4 100644
--- a/src/flicksoup/fsp-error.h
+++ b/src/flicksoup/fsp-error.h
@@ -104,7 +104,8 @@ typedef enum {
   FSP_ERROR_METHOD_TAG_GET_LIST,
   FSP_ERROR_METHOD_SET_LICENSE,
   FSP_ERROR_METHOD_SET_LOCATION,
-  FSP_ERROR_METHOD_GET_LOCATION
+  FSP_ERROR_METHOD_GET_LOCATION,
+  FSP_ERROR_METHOD_SET_DATES
 } FspErrorMethod;
 
 FspError
diff --git a/src/flicksoup/fsp-parser.c b/src/flicksoup/fsp-parser.c
index fc106b0..e335a40 100644
--- a/src/flicksoup/fsp-parser.c
+++ b/src/flicksoup/fsp-parser.c
@@ -104,7 +104,9 @@ _set_location_parser                    (xmlDoc  *doc,
 static gpointer
 _get_location_parser                    (xmlDoc  *doc,
                                          GError **error);
-
+static gpointer
+_set_dates_parser                       (xmlDoc  *doc,
+                                         GError **error);
 static FspDataPhotoInfo *
 _get_photo_info_from_node               (xmlNode *node);
 
@@ -293,6 +295,8 @@ _get_error_method_from_parser           (gpointer (*body_parser)
     error_method = FSP_ERROR_METHOD_SET_LOCATION;
   else if (body_parser == _get_location_parser)
     error_method = FSP_ERROR_METHOD_GET_LOCATION;
+  else if (body_parser == _set_dates_parser)
+    error_method = FSP_ERROR_METHOD_SET_DATES;
 
   return error_method;
 }
@@ -964,6 +968,14 @@ _get_location_parser                    (xmlDoc  *doc,
   return location;
 }
 
+static gpointer
+_set_dates_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)
 {
@@ -1589,3 +1601,20 @@ fsp_parser_get_location                  (FspParser   *self,
   /* Return value */
   return location;
 }
+
+gpointer
+fsp_parser_set_dates                     (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_dates_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 3f209f9..d9f75f5 100644
--- a/src/flicksoup/fsp-parser.h
+++ b/src/flicksoup/fsp-parser.h
@@ -162,6 +162,13 @@ fsp_parser_get_location                  (FspParser   *self,
                                           const gchar *buffer,
                                           gulong       buf_size,
                                           GError     **error);
+
+gpointer
+fsp_parser_set_dates                     (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 57a6613..ab2234e 100644
--- a/src/flicksoup/fsp-session.c
+++ b/src/flicksoup/fsp-session.c
@@ -341,6 +341,11 @@ _get_location_soup_session_cb            (SoupSession *session,
                                           SoupMessage *msg,
                                           gpointer     data);
 
+static void
+_set_dates_soup_session_cb              (SoupSession *session,
+                                         SoupMessage *msg,
+                                         gpointer     data);
+
 /* Private API */
 
 static void
@@ -1648,6 +1653,20 @@ _get_location_soup_session_cb            (SoupSession *session,
                          data);
 }
 
+static void
+_set_dates_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_dates,
+                         data);
+}
+
 /* Public API */
 
 FspSession *
@@ -2705,3 +2724,58 @@ fsp_session_get_location_finish          (FspSession    *self,
                                               error));
   return location;
 }
+
+void
+fsp_session_set_posted_date             (FspSession          *self,
+                                         const gchar         *photo_id,
+                                         GDateTime           *date,
+                                         GCancellable        *cancellable,
+                                         GAsyncReadyCallback  callback,
+                                         gpointer             data)
+{
+  SoupSession *soup_session = NULL;
+  gchar* date_str;
+  gchar *url = NULL;
+
+  g_return_if_fail (FSP_IS_SESSION (self));
+  g_return_if_fail (photo_id != NULL);
+  g_return_if_fail (date != NULL);
+
+  /* The flickr API expects a UTC Unix timestamp for 'date_posted' */
+  date_str = g_date_time_format (date, "%s");
+
+  /* Build the signed url */
+  url = _get_signed_url (self,
+                         FLICKR_API_BASE_URL,
+                         AUTHORIZATION_METHOD_OAUTH_1,
+                         TOKEN_TYPE_PERMANENT,
+                         "method", "flickr.photos.setDates",
+                         "photo_id", photo_id,
+                         "date_posted", date_str,
+                         NULL);
+
+  /* Perform the async request */
+  soup_session = _get_soup_session (self);
+  _perform_async_request (soup_session, url,
+                          _set_dates_soup_session_cb, G_OBJECT (self),
+                          cancellable, callback, fsp_session_set_posted_date, data);
+
+  g_free (date_str);
+  g_free (url);
+}
+
+gboolean
+fsp_session_set_posted_date_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_posted_date, error);
+
+  return result ? TRUE : FALSE;
+}
diff --git a/src/flicksoup/fsp-session.h b/src/flicksoup/fsp-session.h
index 8226722..62c53f8 100644
--- a/src/flicksoup/fsp-session.h
+++ b/src/flicksoup/fsp-session.h
@@ -282,6 +282,20 @@ FspDataLocation *
 fsp_session_get_location_finish          (FspSession    *self,
                                           GAsyncResult  *res,
                                           GError       **error);
+
+void
+fsp_session_set_posted_date             (FspSession          *self,
+                                         const gchar         *photo_id,
+                                         GDateTime           *date,
+                                         GCancellable        *cancellable,
+                                         GAsyncReadyCallback  callback,
+                                         gpointer             data);
+
+gboolean
+fsp_session_set_posted_date_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]