[frogr] Replace callbacks in FrogrFileLoader with signals, to be used from the controller.



commit 2807aa6a4e48b47fb9055a48182d37ef59e1e9e9
Author: Mario Sanchez Prada <msanchez gnome org>
Date:   Tue Oct 30 11:58:24 2012 +0100

    Replace callbacks in FrogrFileLoader with signals, to be used from the controller.

 src/frogr-controller.c  |   72 ++++++++++++++++++---------------------------
 src/frogr-file-loader.c |   75 ++++++++++++++++++++++++++++++++--------------
 src/frogr-file-loader.h |   12 +-------
 3 files changed, 82 insertions(+), 77 deletions(-)
---
diff --git a/src/frogr-controller.c b/src/frogr-controller.c
index 84c3e25..ee0935b 100644
--- a/src/frogr-controller.c
+++ b/src/frogr-controller.c
@@ -210,9 +210,9 @@ static void _notify_adding_to_group (FrogrController *self,
                                      FrogrPicture *picture,
                                      FrogrGroup *group);
 
-static gboolean _on_file_loaded (FrogrController *self, FrogrPicture *picture);
+static void _on_file_loaded (FrogrFileLoader *loader, FrogrPicture *picture, FrogrController *self);
 
-static void _on_files_loaded (FrogrController *self);
+static void _on_files_loaded (FrogrFileLoader *loader, FrogrController *self);
 
 static void _fetch_everything (FrogrController *self, gboolean force_fetch);
 
@@ -1344,55 +1344,24 @@ _notify_adding_to_group (FrogrController *self,
   g_free (debug_msg);
 }
 
-static gboolean
-_on_file_loaded (FrogrController *self, FrogrPicture *picture)
+static void
+_on_file_loaded (FrogrFileLoader *loader, FrogrPicture *picture, FrogrController *self)
 {
   FrogrControllerPrivate *priv = NULL;
   FrogrMainViewModel *mainview_model = NULL;
-  gulong picture_filesize = 0;
-  gulong max_filesize = G_MAXULONG;
 
-  g_return_val_if_fail (FROGR_IS_CONTROLLER (self), FALSE);
-  g_return_val_if_fail (FROGR_IS_PICTURE (picture), FALSE);
+  g_return_if_fail (FROGR_IS_CONTROLLER (self));
+  g_return_if_fail (FROGR_IS_PICTURE (picture));
 
   priv = FROGR_CONTROLLER_GET_PRIVATE (self);
 
-  /* We need this info if account info was already fetched. */
-  if (priv->account && priv->account_extra_info_fetched)
-    {
-      max_filesize = frogr_account_get_max_filesize (priv->account);
-      picture_filesize = frogr_picture_get_filesize (picture);
-    }
-
-  /* Check max filesize limit, and stop the process if needed. */
-  if (picture_filesize > max_filesize)
-    {
-      GtkWindow *window = NULL;
-      gchar *msg = NULL;
-
-      /* First %s is the title of the picture (filename of the file by
-         default). Second %s is the max allowed size for a picture to be
-         uploaded to flickr (different for free and PRO accounts). */
-      msg = g_strdup_printf (_("Can't load picture %s: size of file is bigger "
-                               "than the maximum allowed for this account (%s)"),
-                             frogr_picture_get_title (picture),
-                             frogr_util_get_datasize_string (max_filesize));
-
-      window = frogr_main_view_get_window (priv->mainview);
-      frogr_util_show_error_dialog (window, msg);
-      g_free (msg);
-
-      return FALSE;
-    }
-
   mainview_model = frogr_main_view_get_model (priv->mainview);
   frogr_main_view_model_add_picture (mainview_model, picture);
   g_signal_emit (self, signals[PICTURE_LOADED], 0, picture);
-  return TRUE;
 }
 
 static void
-_on_files_loaded (FrogrController *self)
+_on_files_loaded (FrogrFileLoader *loader, FrogrController *self)
 {
   g_return_if_fail (FROGR_IS_CONTROLLER (self));
 
@@ -2693,11 +2662,28 @@ void
 frogr_controller_load_pictures (FrogrController *self,
                                 GSList *fileuris)
 {
-  FrogrFileLoader *loader;
-  loader = frogr_file_loader_new (fileuris,
-                                  (FrogrFileLoadedCallback) _on_file_loaded,
-                                  (FrogrFilesLoadedCallback) _on_files_loaded,
-                                  self);
+  FrogrControllerPrivate *priv = NULL;
+  FrogrFileLoader *loader = NULL;
+  gulong max_filesize = G_MAXULONG;
+
+  g_return_if_fail(FROGR_IS_CONTROLLER (self));
+
+  priv = FROGR_CONTROLLER_GET_PRIVATE (self);
+
+  /* We need this info if account info was already fetched. */
+  if (priv->account && priv->account_extra_info_fetched)
+    max_filesize = frogr_account_get_max_filesize (priv->account);
+
+  loader = frogr_file_loader_new (fileuris, max_filesize);
+
+  g_signal_connect (G_OBJECT (loader), "file-loaded",
+                    G_CALLBACK (_on_file_loaded),
+                    self);
+
+  g_signal_connect (G_OBJECT (loader), "files-loaded",
+                    G_CALLBACK (_on_files_loaded),
+                    self);
+
   /* Load the pictures! */
   _set_state (self, FROGR_STATE_LOADING_PICTURES);
   frogr_file_loader_load (loader);
diff --git a/src/frogr-file-loader.c b/src/frogr-file-loader.c
index 550de70..3eb9d7d 100644
--- a/src/frogr-file-loader.c
+++ b/src/frogr-file-loader.c
@@ -61,6 +61,7 @@ struct _FrogrFileLoaderPrivate
   guint index;
   guint n_files;
 
+  gulong max_filesize;
   gboolean keep_file_extensions;
   gboolean import_tags;
   gboolean public_visibility;
@@ -71,12 +72,17 @@ struct _FrogrFileLoaderPrivate
   FspLicense license;
   FspSafetyLevel safety_level;
   FspContentType content_type;
+};
 
-  FrogrFileLoadedCallback file_loaded_cb;
-  FrogrFilesLoadedCallback files_loaded_cb;
-  GObject *object;
+/* Signals */
+enum {
+  FILE_LOADED,
+  FILES_LOADED,
+  N_SIGNALS
 };
 
+static guint signals[N_SIGNALS] = { 0 };
+
 #ifndef MAC_INTEGRATION
 /* Don't use this in Mac OSX, where GNOME VFS daemon won't be running,
    so we couldn't reliably check mime types (will be text/plain) */
@@ -213,6 +219,7 @@ _load_next_file_cb (GObject *object,
   GError *error = NULL;
   gchar *contents = NULL;
   gsize length = 0;
+  gulong picture_filesize = 0;
   gboolean keep_going = TRUE;
 
   self = FROGR_FILE_LOADER (data);;
@@ -380,10 +387,29 @@ _load_next_file_cb (GObject *object,
 
   /* Update status and progress */
   _update_status_and_progress (self);
+  g_signal_emit (self, signals[FILE_LOADED], 0, fpicture);
 
-  /* Execute 'file-loaded' callback */
-  if (priv->file_loaded_cb && fpicture)
-    keep_going = priv->file_loaded_cb (priv->object, fpicture);
+  /* Check if we must interrupt the process */
+  picture_filesize = frogr_picture_get_filesize (fpicture);
+  if (picture_filesize > priv->max_filesize)
+    {
+      GtkWindow *window = NULL;
+      gchar *msg = NULL;
+
+      /* First %s is the title of the picture (filename of the file by
+         default). Second %s is the max allowed size for a picture to be
+         uploaded to flickr (different for free and PRO accounts). */
+      msg = g_strdup_printf (_("Can't load picture %s: size of file is bigger "
+                               "than the maximum allowed for this account (%s)"),
+                             frogr_picture_get_title (fpicture),
+                             frogr_util_get_datasize_string (priv->max_filesize));
+
+      window = frogr_main_view_get_window (priv->mainview);
+      frogr_util_show_error_dialog (window, msg);
+      g_free (msg);
+
+      keep_going = FALSE;
+    }
 
   if (fpicture != NULL)
     g_object_unref (fpicture);
@@ -555,14 +581,7 @@ import_tags_from_xmp_keywords (const char *buffer, size_t len)
 static void
 _finish_task_and_self_destruct (FrogrFileLoader *self)
 {
-  FrogrFileLoaderPrivate *priv =
-    FROGR_FILE_LOADER_GET_PRIVATE (self);
-
-  /* Execute final callback */
-  if (priv->files_loaded_cb)
-    priv->files_loaded_cb (priv->object);
-
-  /* Process finished, self-destruct */
+  g_signal_emit (self, signals[FILES_LOADED], 0);
   g_object_unref (self);
 }
 
@@ -608,6 +627,22 @@ frogr_file_loader_class_init(FrogrFileLoaderClass *klass)
   obj_class->dispose = _frogr_file_loader_dispose;
   obj_class->finalize = _frogr_file_loader_finalize;
 
+  signals[FILE_LOADED] =
+    g_signal_new ("file-loaded",
+                  G_OBJECT_CLASS_TYPE (klass),
+                  G_SIGNAL_RUN_FIRST,
+                  0, NULL, NULL,
+                  g_cclosure_marshal_VOID__OBJECT,
+                  G_TYPE_NONE, 1, FROGR_TYPE_PICTURE);
+
+  signals[FILES_LOADED] =
+    g_signal_new ("files-loaded",
+                  G_OBJECT_CLASS_TYPE (klass),
+                  G_SIGNAL_RUN_FIRST,
+                  0, NULL, NULL,
+                  g_cclosure_marshal_VOID__VOID,
+                  G_TYPE_NONE, 0);
+
   g_type_class_add_private (obj_class, sizeof (FrogrFileLoaderPrivate));
 }
 
@@ -626,6 +661,7 @@ frogr_file_loader_init (FrogrFileLoader *self)
   priv->mainview = g_object_ref (frogr_controller_get_main_view (priv->controller));
 
   /* Initialize values from frogr configuration */
+  priv->max_filesize = G_MAXULONG;
   priv->keep_file_extensions = frogr_config_get_keep_file_extensions (config);
   priv->import_tags = frogr_config_get_import_tags_from_metadata (config);
   priv->public_visibility = frogr_config_get_default_public (config);
@@ -647,10 +683,7 @@ frogr_file_loader_init (FrogrFileLoader *self)
 /* Public API */
 
 FrogrFileLoader *
-frogr_file_loader_new (GSList *file_uris,
-                       FrogrFileLoadedCallback file_loaded_cb,
-                       FrogrFilesLoadedCallback files_loaded_cb,
-                       gpointer object)
+frogr_file_loader_new (GSList *file_uris, gulong max_filesize)
 {
   FrogrFileLoader *self = NULL;
   FrogrFileLoaderPrivate *priv = NULL;
@@ -665,15 +698,11 @@ frogr_file_loader_new (GSList *file_uris,
   priv->file_uris = g_slist_reverse (priv->file_uris);
 
   /* Other internal data */
+  priv->max_filesize = max_filesize;
   priv->current = priv->file_uris;
   priv->index = 0;
   priv->n_files = g_slist_length (priv->file_uris);
 
-  /* Callback data */
-  priv->file_loaded_cb = file_loaded_cb;
-  priv->files_loaded_cb = files_loaded_cb;
-  priv->object = object;
-
   return self;
 }
 
diff --git a/src/frogr-file-loader.h b/src/frogr-file-loader.h
index b4699ff..1382873 100644
--- a/src/frogr-file-loader.h
+++ b/src/frogr-file-loader.h
@@ -38,13 +38,6 @@ G_BEGIN_DECLS
 typedef struct _FrogrFileLoader FrogrFileLoader;
 typedef struct _FrogrFileLoaderClass FrogrFileLoaderClass;
 
-/* Callback to be executed after every single load */
-typedef gboolean (*FrogrFileLoadedCallback) (GObject *source,
-                                             FrogrPicture *picture);
-
-/* Callback to be executed after all the files are loaded */
-typedef void (*FrogrFilesLoadedCallback) (GObject *source);
-
 struct _FrogrFileLoader
 {
   GObject parent_instance;
@@ -58,10 +51,7 @@ struct _FrogrFileLoaderClass
 
 GType frogr_file_loader_get_type(void) G_GNUC_CONST;
 
-FrogrFileLoader *frogr_file_loader_new (GSList *file_uris,
-                                        FrogrFileLoadedCallback file_loaded_cb,
-                                        FrogrFilesLoadedCallback files_loaded_cb,
-                                        gpointer object);
+FrogrFileLoader *frogr_file_loader_new (GSList *file_uris, gulong max_filesize);
 
 void frogr_file_loader_load (FrogrFileLoader *self);
 



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