[frogr] FrogrController to deal with setting licenses when needed



commit 86c95d7b0e249973c775bdff9b5661cda1b8fb03
Author: Mario Sanchez Prada <msanchez igalia com>
Date:   Sat Jul 9 01:22:38 2011 +0200

    FrogrController to deal with setting licenses when needed

 src/frogr-controller.c |  103 +++++++++++++++++++++++++++++++++++++++++++++--
 src/frogr-picture.c    |   42 +++++++++++++++++++
 src/frogr-picture.h    |    3 +
 3 files changed, 143 insertions(+), 5 deletions(-)
---
diff --git a/src/frogr-controller.c b/src/frogr-controller.c
index b871d22..860f8bd 100644
--- a/src/frogr-controller.c
+++ b/src/frogr-controller.c
@@ -75,6 +75,7 @@ struct _FrogrControllerPrivate
   gboolean fetching_sets;
   gboolean fetching_groups;
   gboolean fetching_tags;
+  gboolean setting_license;
   gboolean adding_to_set;
   gboolean adding_to_group;
 
@@ -146,6 +147,10 @@ static void _upload_picture (FrogrController *self, FrogrPicture *picture,
 
 static void _upload_picture_cb (GObject *object, GAsyncResult *res, gpointer data);
 
+static void _set_license_cb (GObject *object, GAsyncResult *res, gpointer data);
+
+static gboolean _create_set_or_add_picture_on_idle (gpointer data);
+
 static gboolean _create_set_or_add_picture (FrogrController *self,
                                             FrogrPicture *picture,
                                             upload_picture_st *up_st);
@@ -160,6 +165,9 @@ static gboolean _add_picture_to_groups_on_idle (gpointer data);
 
 static gboolean _complete_picture_upload_on_idle (gpointer data);
 
+static void _notify_setting_license (FrogrController *self,
+                                     FrogrPicture *picture);
+
 static void _notify_creating_set (FrogrController *self,
                                   FrogrPicture *picture,
                                   FrogrPhotoSet *set);
@@ -552,23 +560,40 @@ _upload_picture_cb (GObject *object, GAsyncResult *res, gpointer data)
   /* Stop reporting to the user */
   g_signal_handlers_disconnect_by_func (priv->session, _data_fraction_sent_cb, controller);
 
-  /* Check whether it's needed or not to add the picture to the set */
+  /* Check whether it's needed or not to set a specific license for a
+     picture or to add the picture to sets or groups */
   if (!error)
     {
       GSList *sets = NULL;
       GSList *groups = NULL;
+      FspLicense license = FSP_LICENSE_UNKNOWN;
 
+      license = frogr_picture_get_license (picture);
       sets = frogr_picture_get_sets (picture);
       groups = frogr_picture_get_groups (picture);
 
+      /* Set license if needed */
+      if (license != FSP_LICENSE_UNKNOWN)
+        {
+          priv->setting_license = TRUE;
+
+          _notify_setting_license (controller, picture);
+          fsp_session_set_license_async (session,
+                                         frogr_picture_get_id (picture),
+                                         license,
+                                         priv->cancellable,
+                                         _set_license_cb,
+                                         up_st);
+        }
+
+      /* Add picture to set if needed (and maybe create a new one) */
       if (g_slist_length (sets) > 0)
         {
           up_st->sets = sets;
-          _create_set_or_add_picture (controller, picture, up_st);
+          gdk_threads_add_timeout (DEFAULT_TIMEOUT, _create_set_or_add_picture_on_idle, up_st);
         }
 
-      /* Pictures will be added to groups AFTER being added to sets,
-         so that's why we don't start the process now but on idle */
+      /* Add picture to groups if needed */
       if (g_slist_length (groups) > 0)
         {
           up_st->groups = groups;
@@ -581,6 +606,52 @@ _upload_picture_cb (GObject *object, GAsyncResult *res, gpointer data)
   gdk_threads_add_timeout (DEFAULT_TIMEOUT, _complete_picture_upload_on_idle, up_st);
 }
 
+static void
+_set_license_cb (GObject *object, GAsyncResult *res, gpointer data)
+{
+  FspSession *session = NULL;
+  upload_picture_st *up_st = NULL;
+  FrogrController *controller = NULL;
+  FrogrControllerPrivate *priv = NULL;
+  GError *error = NULL;
+
+  session = FSP_SESSION (object);
+  up_st = (upload_picture_st*) data;
+  controller = up_st->controller;
+
+  fsp_session_set_license_finish (session, res, &error);
+  if (error)
+    {
+      /* We do not anything special if something went wrong here */
+      DEBUG ("Error setting license for picture: %s", error->message);
+      g_free (error);
+    }
+
+  priv = FROGR_CONTROLLER_GET_PRIVATE (controller);
+  priv->setting_license = FALSE;
+}
+
+static gboolean
+_create_set_or_add_picture_on_idle (gpointer data)
+{
+  upload_picture_st *up_st = NULL;
+  FrogrController *controller = NULL;
+  FrogrControllerPrivate *priv = NULL;
+  FrogrPicture *picture = NULL;
+
+  up_st = (upload_picture_st*) data;
+  controller = up_st->controller;
+  picture = up_st->picture;
+
+  /* Keep the source while busy */
+  priv = FROGR_CONTROLLER_GET_PRIVATE (controller);
+  if (priv->setting_license)
+    return TRUE;
+
+  _create_set_or_add_picture (controller, picture, up_st);
+  return FALSE;
+}
+
 static gboolean
 _create_set_or_add_picture (FrogrController *self,
                             FrogrPicture *picture,
@@ -862,7 +933,7 @@ _complete_picture_upload_on_idle (gpointer data)
   priv = FROGR_CONTROLLER_GET_PRIVATE (controller);
 
   /* Keep the source while busy */
-  if (priv->adding_to_set || priv->adding_to_group)
+  if (priv->setting_license || priv->adding_to_set || priv->adding_to_group)
     {
       frogr_main_view_pulse_progress (priv->mainview);
       return TRUE;
@@ -892,6 +963,27 @@ _complete_picture_upload_on_idle (gpointer data)
 }
 
 static void
+_notify_setting_license (FrogrController *self,
+                         FrogrPicture *picture)
+{
+  FrogrControllerPrivate *priv = NULL;
+  const gchar *picture_title = NULL;
+  FspLicense license = FSP_LICENSE_UNKNOWN;
+  gchar *progress_text = NULL;
+
+  priv = FROGR_CONTROLLER_GET_PRIVATE (self);
+  frogr_main_view_set_progress_description(priv->mainview, _("Setting license for pictureâ"));
+
+  picture_title = frogr_picture_get_title (picture);
+  license = frogr_picture_get_license (picture);
+  progress_text = g_strdup_printf ("Adding license %d for picture %sâ",
+                                   license, picture_title);
+  DEBUG ("%s", progress_text);
+
+  g_free (progress_text);
+}
+
+static void
 _notify_creating_set (FrogrController *self,
                       FrogrPicture *picture,
                       FrogrPhotoSet *set)
@@ -1777,6 +1869,7 @@ frogr_controller_init (FrogrController *self)
   priv->fetching_groups = FALSE;
   priv->fetching_account_extra_info = FALSE;
   priv->fetching_tags = FALSE;
+  priv->setting_license = FALSE;
   priv->adding_to_set = FALSE;
   priv->adding_to_group = FALSE;
   priv->account_info_fetched = FALSE;
diff --git a/src/frogr-picture.c b/src/frogr-picture.c
index 21bd983..0c26b49 100644
--- a/src/frogr-picture.c
+++ b/src/frogr-picture.c
@@ -52,6 +52,7 @@ struct _FrogrPicturePrivate
 
   FspSafetyLevel safety_level;
   FspContentType content_type;
+  FspLicense license;
   gboolean show_in_search;
 
   GdkPixbuf *pixbuf;
@@ -70,6 +71,7 @@ enum  {
   PROP_IS_FRIEND,
   PROP_SAFETY_LEVEL,
   PROP_CONTENT_TYPE,
+  PROP_LICENSE,
   PROP_SHOW_IN_SEARCH,
   PROP_PIXBUF,
   PROP_FILESIZE,
@@ -216,6 +218,9 @@ _frogr_picture_set_property (GObject *object,
     case PROP_CONTENT_TYPE:
       frogr_picture_set_content_type (self, g_value_get_int (value));
       break;
+    case PROP_LICENSE:
+      frogr_picture_set_license (self, g_value_get_int (value));
+      break;
     case PROP_SHOW_IN_SEARCH:
       frogr_picture_set_show_in_search (self, g_value_get_boolean (value));
       break;
@@ -274,6 +279,9 @@ _frogr_picture_get_property (GObject *object,
     case PROP_CONTENT_TYPE:
       g_value_set_int (value, priv->content_type);
       break;
+    case PROP_LICENSE:
+      g_value_set_int (value, priv->license);
+      break;
     case PROP_SHOW_IN_SEARCH:
       g_value_set_boolean (value, priv->show_in_search);
       break;
@@ -440,6 +448,15 @@ frogr_picture_class_init(FrogrPictureClass *klass)
                                                      FSP_CONTENT_TYPE_PHOTO,
                                                      G_PARAM_READWRITE));
   g_object_class_install_property (obj_class,
+                                   PROP_LICENSE,
+                                   g_param_spec_int ("license",
+                                                     "license",
+                                                     "License for this picture",
+                                                     FSP_LICENSE_UNKNOWN,
+                                                     FSP_LICENSE_ND,
+                                                     FSP_LICENSE_UNKNOWN,
+                                                     G_PARAM_READWRITE));
+  g_object_class_install_property (obj_class,
                                    PROP_SHOW_IN_SEARCH,
                                    g_param_spec_boolean ("show-in-search",
                                                          "show-in-search",
@@ -501,6 +518,8 @@ frogr_picture_init (FrogrPicture *self)
 
   priv->safety_level = FSP_SAFETY_LEVEL_SAFE;
   priv->content_type = FSP_CONTENT_TYPE_PHOTO;
+  priv->license = FSP_LICENSE_UNKNOWN;
+
   priv->show_in_search = TRUE;
 
   priv->pixbuf = NULL;
@@ -527,6 +546,7 @@ frogr_picture_new (const gchar *fileuri,
                                      "is-friend", friend,
                                      "safety-level", FSP_SAFETY_LEVEL_SAFE,
                                      "content-type", FSP_CONTENT_TYPE_PHOTO,
+                                     "license", FSP_LICENSE_UNKNOWN,
                                      "show_in_search", TRUE,
                                      NULL));
 }
@@ -802,6 +822,28 @@ frogr_picture_set_content_type (FrogrPicture *self,
   priv->content_type = content_type;
 }
 
+FspLicense
+frogr_picture_get_license (FrogrPicture *self)
+{
+  FrogrPicturePrivate *priv = NULL;
+
+  g_return_val_if_fail(FROGR_IS_PICTURE(self), FALSE);
+
+  priv = FROGR_PICTURE_GET_PRIVATE (self);
+  return priv->license;
+}
+
+void
+frogr_picture_set_license (FrogrPicture *self, FspLicense license)
+{
+  FrogrPicturePrivate *priv = NULL;
+
+  g_return_if_fail(FROGR_IS_PICTURE(self));
+
+  priv = FROGR_PICTURE_GET_PRIVATE (self);
+  priv->license = license;
+}
+
 gboolean
 frogr_picture_show_in_search (FrogrPicture *self)
 {
diff --git a/src/frogr-picture.h b/src/frogr-picture.h
index 14e4bfa..4160adb 100644
--- a/src/frogr-picture.h
+++ b/src/frogr-picture.h
@@ -103,6 +103,9 @@ void frogr_picture_set_safety_level (FrogrPicture *self,
 FspContentType frogr_picture_get_content_type (FrogrPicture *self);
 void frogr_picture_set_content_type (FrogrPicture *self,
                                      FspContentType content_type);
+FspLicense frogr_picture_get_license (FrogrPicture *self);
+void frogr_picture_set_license (FrogrPicture *self, FspLicense license);
+
 gboolean frogr_picture_show_in_search (FrogrPicture *self);
 void frogr_picture_set_show_in_search (FrogrPicture *self,
                                        gboolean show_in_search);



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