[recipes/image-download: 1/12] Switch image to a GPtrArray



commit ed698e2a68edf6fc793a412cba612aa42167eee1
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Mar 2 21:31:14 2017 -0500

    Switch image to a GPtrArray
    
    This is the first step towards turning GrImage into
    an object that can provide methods for downloading
    and caching images.

 src/gr-cooking-page.c      |    2 +-
 src/gr-cooking-view.c      |   24 +++++++++---------
 src/gr-cooking-view.h      |    2 +-
 src/gr-details-page.c      |    6 ++--
 src/gr-edit-page.c         |   26 +++++++++----------
 src/gr-image-page.c        |   37 +++++----------------------
 src/gr-image-page.h        |    2 +-
 src/gr-image-viewer.c      |   60 ++++++++++++++++++++-----------------------
 src/gr-image-viewer.h      |    3 +-
 src/gr-images.c            |   17 +-----------
 src/gr-images.h            |    2 +-
 src/gr-recipe-importer.c   |    9 +++---
 src/gr-recipe-small-tile.c |    6 ++--
 src/gr-recipe-store.c      |   14 ++++++----
 src/gr-recipe-tile.c       |    4 +-
 src/gr-recipe.c            |   35 +++++++++----------------
 src/gr-recipe.h            |    1 +
 src/gr-window.c            |    6 ++--
 src/gr-window.h            |    2 +-
 19 files changed, 106 insertions(+), 152 deletions(-)
---
diff --git a/src/gr-cooking-page.c b/src/gr-cooking-page.c
index 6d34056..5d934fc 100644
--- a/src/gr-cooking-page.c
+++ b/src/gr-cooking-page.c
@@ -530,7 +530,7 @@ void
 gr_cooking_page_set_recipe (GrCookingPage *page,
                             GrRecipe      *recipe)
 {
-        g_autoptr(GArray) images = NULL;
+        g_autoptr(GPtrArray) images = NULL;
         const char *id;
         const char *instructions;
 
diff --git a/src/gr-cooking-view.c b/src/gr-cooking-view.c
index a45e715..b1b362d 100644
--- a/src/gr-cooking-view.c
+++ b/src/gr-cooking-view.c
@@ -50,7 +50,7 @@ struct _GrCookingView
         GtkWidget *text_box;
         GtkWidget *timer_box;
 
-        GArray *images;
+        GPtrArray *images;
         char *id;
         char *instructions;
 
@@ -139,12 +139,12 @@ timer_active (GrTimer    *timer,
 }
 
 static StepData *
-step_data_new (int         num,
-               int         n_steps,
-               const char *text,
-               guint64     duration,
-               const char *title,
-               int         image,
+step_data_new (int            num,
+               int            n_steps,
+               const char    *text,
+               guint64        duration,
+               const char    *title,
+               int            image,
                GrCookingView *view)
 {
         StepData *d;
@@ -241,7 +241,7 @@ gr_cooking_view_finalize (GObject *object)
         GrCookingView *self = GR_COOKING_VIEW (object);
 
         g_clear_pointer (&self->id, g_free);
-        g_clear_pointer (&self->images, g_array_unref);
+        g_clear_pointer (&self->images, g_ptr_array_unref);
         g_clear_pointer (&self->instructions, g_free);
         g_clear_pointer (&self->steps, g_ptr_array_unref);
 
@@ -320,7 +320,7 @@ setup_step (GrCookingView *view)
 
                 gtk_widget_show (view->cooking_stack);
                 gtk_widget_set_halign (view->text_box, GTK_ALIGN_START);
-                ri = &g_array_index (view->images, GrImage, s->image);
+                ri = g_ptr_array_index (view->images, s->image);
                 if (view->wide)
                         pixbuf = load_pixbuf_fill_size (ri->path, 640, 480);
                 else
@@ -584,14 +584,14 @@ void
 gr_cooking_view_set_data (GrCookingView *view,
                           const char    *id,
                           const char    *instructions,
-                          GArray        *images)
+                          GPtrArray     *images)
 {
         g_free (view->id);
         view->id = g_strdup (id);
         g_free (view->instructions);
         view->instructions = g_strdup (instructions);
-        g_clear_pointer (&view->images, g_array_unref);
-        view->images = g_array_ref (images);
+        g_clear_pointer (&view->images, g_ptr_array_unref);
+        view->images = g_ptr_array_ref (images);
 
         setup_steps (view);
 }
diff --git a/src/gr-cooking-view.h b/src/gr-cooking-view.h
index c714d6e..44a0eb5 100644
--- a/src/gr-cooking-view.h
+++ b/src/gr-cooking-view.h
@@ -34,7 +34,7 @@ GrCookingView *gr_cooking_view_new           (void);
 void           gr_cooking_view_set_data      (GrCookingView *view,
                                               const char    *recipe_id,
                                               const char    *instructions,
-                                              GArray        *images);
+                                              GPtrArray     *images);
 int            gr_cooking_view_get_n_steps   (GrCookingView *view);
 int            gr_cooking_view_get_step      (GrCookingView *view);
 void           gr_cooking_view_set_step      (GrCookingView *view,
diff --git a/src/gr-details-page.c b/src/gr-details-page.c
index fadd4da..8e6e57f 100644
--- a/src/gr-details-page.c
+++ b/src/gr-details-page.c
@@ -346,7 +346,7 @@ static void
 activate_image (GrDetailsPage *page)
 {
         GtkWidget *window;
-        g_autoptr(GArray) images = NULL;
+        g_autoptr(GPtrArray) images = NULL;
         int idx;
 
         g_object_get (page->recipe_image, "images", &images, "index", &idx, NULL);
@@ -630,7 +630,7 @@ gr_details_page_set_recipe (GrDetailsPage *page,
         GrRecipeStore *store;
         g_autoptr(GrChef) chef = NULL;
         g_autoptr(GrIngredientsList) ing = NULL;
-        g_autoptr(GArray) images = NULL;
+        GPtrArray *images;
         gboolean favorite;
         gboolean same_recipe;
         int index;
@@ -659,7 +659,7 @@ gr_details_page_set_recipe (GrDetailsPage *page,
         else
                 index = gr_recipe_get_default_image (recipe);
 
-        g_object_get (recipe, "images", &images, NULL);
+        images = gr_recipe_get_images (recipe);
         gr_image_viewer_set_images (GR_IMAGE_VIEWER (page->recipe_image), images, index);
 
         ing = gr_ingredients_list_new (ingredients);
diff --git a/src/gr-edit-page.c b/src/gr-edit-page.c
index d6c023b..e0067bc 100644
--- a/src/gr-edit-page.c
+++ b/src/gr-edit-page.c
@@ -167,15 +167,15 @@ static void
 populate_image_flowbox (GrEditPage *page)
 {
         int i;
-        g_autoptr(GArray) images = NULL;
+        GPtrArray *images;
         GtkWidget *button;
 
-        g_object_get (page->images, "images", &images, NULL);
+        images = gr_image_viewer_get_images (GR_IMAGE_VIEWER (page->images));
 
         container_remove_all (GTK_CONTAINER (page->image_flowbox));
 
         for (i = 0; i < images->len; i++) {
-                GrImage *ri = &g_array_index (images, GrImage, i);
+                GrImage *ri = g_ptr_array_index (images, i);
                 g_autoptr(GdkPixbuf) pb = load_pixbuf_fill_size (ri->path, 60, 40);
                 GtkWidget *image;
                 GtkWidget *child;
@@ -197,10 +197,10 @@ populate_image_flowbox (GrEditPage *page)
 static void
 update_image_button_sensitivity (GrEditPage *page)
 {
-        g_autoptr(GArray) images = NULL;
+        GPtrArray *images;
         int length;
 
-        g_object_get (page->images, "images", &images, NULL);
+        images = gr_image_viewer_get_images (GR_IMAGE_VIEWER (page->images));
         length = images->len;
         gtk_widget_set_sensitive (page->add_image_button, TRUE);
         gtk_widget_set_sensitive (page->remove_image_button, length > 0);
@@ -1472,7 +1472,7 @@ preview_visible_changed (GrEditPage *page)
                 gr_cooking_view_stop (GR_COOKING_VIEW (page->cooking_view), TRUE);
         }
         else {
-                g_autoptr(GArray) images = NULL;
+                GPtrArray *images;
                 g_autofree char *instructions = NULL;
 
                 gtk_widget_set_sensitive (page->add_step_button, FALSE);
@@ -1482,7 +1482,7 @@ preview_visible_changed (GrEditPage *page)
                 gtk_widget_set_visible (page->prev_step_button, TRUE);
                 gtk_widget_set_visible (page->next_step_button, TRUE);
 
-                g_object_get (page->images, "images", &images, NULL);
+                images = gr_image_viewer_get_images (GR_IMAGE_VIEWER (page->images));
                 instructions = get_text_view_text (GTK_TEXT_VIEW (page->instructions_field));
 
                 gr_cooking_view_set_data (GR_COOKING_VIEW (page->cooking_view), NULL, instructions, images);
@@ -1987,7 +1987,7 @@ scroll_up (GrEditPage *page)
 void
 gr_edit_page_clear (GrEditPage *page)
 {
-        GArray *images;
+        g_autoptr(GPtrArray) images = NULL;
         GrRecipeStore *store;
 
         gr_image_viewer_revert_changes (GR_IMAGE_VIEWER (page->images));
@@ -2015,7 +2015,6 @@ gr_edit_page_clear (GrEditPage *page)
 
         images = gr_image_array_new ();
         g_object_set (page->images, "images", images, NULL);
-        g_array_unref (images);
 
         if (page->index_handler_id) {
                 g_signal_handler_disconnect (page->recipe, page->index_handler_id);
@@ -2140,7 +2139,7 @@ gr_edit_page_edit (GrEditPage *page,
         const char *ingredients;
         GrDiets diets;
         int index;
-        g_autoptr(GArray) images = NULL;
+        GPtrArray *images;
         g_autoptr(GrChef) chef = NULL;
         GrRecipeStore *store;
 
@@ -2162,8 +2161,7 @@ gr_edit_page_edit (GrEditPage *page,
         ingredients = gr_recipe_get_ingredients (recipe);
         author = gr_recipe_get_author (recipe);
         index = gr_recipe_get_default_image (recipe);
-
-        g_object_get (recipe, "images", &images, NULL);
+        images = gr_recipe_get_images (recipe);
 
         g_free (page->author);
         page->author = g_strdup (author);
@@ -2231,7 +2229,7 @@ gr_edit_page_save (GrEditPage *page)
         g_autoptr(GError) error = NULL;
         gboolean ret = TRUE;
         GrDiets diets;
-        g_autoptr(GArray) images = NULL;
+        GPtrArray *images;
 
         store = gr_recipe_store_get ();
 
@@ -2252,7 +2250,7 @@ gr_edit_page_save (GrEditPage *page)
                (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (page->vegetarian_check)) ? 
GR_DIET_VEGETARIAN : 0) |
                 (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (page->milk_free_check)) ? 
GR_DIET_MILK_FREE : 0);
 
-        g_object_get (page->images, "images", &images, NULL);
+        images = gr_image_viewer_get_images (GR_IMAGE_VIEWER (page->images));
 
         if (page->recipe) {
                 g_autofree char *old_id = NULL;
diff --git a/src/gr-image-page.c b/src/gr-image-page.c
index 6909fb8..957171b 100644
--- a/src/gr-image-page.c
+++ b/src/gr-image-page.c
@@ -38,7 +38,7 @@ struct _GrImagePage
         GtkWidget *prev_revealer;
         GtkWidget *close_revealer;
 
-        GArray *images;
+        GPtrArray *images;
         int index;
 
         guint hide_timeout;
@@ -75,7 +75,7 @@ gr_image_page_finalize (GObject *object)
         GrImagePage *page = GR_IMAGE_PAGE (object);
 
         remove_hide_timeout (page);
-        g_clear_pointer (&page->images, g_array_unref);
+        g_clear_pointer (&page->images, g_ptr_array_unref);
 
         G_OBJECT_CLASS (gr_image_page_parent_class)->finalize (object);
 }
@@ -96,7 +96,7 @@ set_current_image (GrImagePage *page)
                 monitor = gdk_display_get_monitor_at_window (display, win);
                 gdk_monitor_get_geometry (monitor, &geom);
 
-                ri = &g_array_index (page->images, GrImage, page->index);
+                ri = g_ptr_array_index (page->images, page->index);
                 pixbuf = load_pixbuf_fit_size (ri->path, geom.width - 80, geom.height - 80, FALSE);
                 gtk_image_set_from_pixbuf (GTK_IMAGE (page->image), pixbuf);
         }
@@ -249,7 +249,7 @@ gr_image_page_set_property (GObject      *object,
         switch (prop_id)
           {
           case PROP_IMAGES:
-                  gr_image_page_set_images (self, (GArray *) g_value_get_boxed (value));
+                  gr_image_page_set_images (self, (GPtrArray *) g_value_get_boxed (value));
                   break;
 
           default:
@@ -297,37 +297,14 @@ gr_image_page_class_init (GrImagePageClass *klass)
         gtk_widget_class_bind_template_callback (widget_class, motion_notify);
 }
 
-static void
-add_image (GrImagePage    *page,
-           GrImage *ri,
-           gboolean        select)
-{
-        g_array_append_vals (page->images, ri, 1);
-        ri = &g_array_index (page->images, GrImage, page->images->len - 1);
-        ri->path = g_strdup (ri->path);
-
-        if (select)
-                page->index = page->images->len - 1;
-        set_current_image (page);
-
-        g_object_notify (G_OBJECT (page), "images");
-}
-
 void
 gr_image_page_set_images (GrImagePage *page,
-                          GArray      *images)
+                          GPtrArray   *images)
 {
-        int i;
-
         g_object_freeze_notify (G_OBJECT (page));
 
-        g_array_remove_range (page->images, 0, page->images->len);
-        g_object_notify (G_OBJECT (page), "images");
-
-        for (i = 0; i < images->len; i++) {
-                GrImage *ri = &g_array_index (images, GrImage, i);
-                add_image (page, ri, FALSE);
-        }
+        g_ptr_array_unref (page->images);
+        page->images = g_ptr_array_ref (images);
 
         page->index = 0;
         set_current_image (page);
diff --git a/src/gr-image-page.h b/src/gr-image-page.h
index 111e454..05bce5c 100644
--- a/src/gr-image-page.h
+++ b/src/gr-image-page.h
@@ -30,7 +30,7 @@ G_DECLARE_FINAL_TYPE (GrImagePage, gr_image_page, GR, IMAGE_PAGE, GtkBox)
 
 GrImagePage *gr_image_page_new        (void);
 void         gr_image_page_set_images (GrImagePage *page,
-                                       GArray      *images);
+                                       GPtrArray   *images);
 
 void         gr_image_page_show_image (GrImagePage *page,
                                        int          idx);
diff --git a/src/gr-image-viewer.c b/src/gr-image-viewer.c
index 606a3dd..ce9644b 100644
--- a/src/gr-image-viewer.c
+++ b/src/gr-image-viewer.c
@@ -70,7 +70,7 @@ struct _GrImageViewer
         GtkWidget *preview_revealer;
         GtkWidget *preview_list;
 
-        GArray *images;
+        GPtrArray *images;
         int index;
 
         GPtrArray *additions;
@@ -140,7 +140,7 @@ set_current_image (GrImageViewer *viewer)
                 g_autoptr(GdkPixbuf) pixbuf = NULL;
                 const char *vis;
 
-                ri = &g_array_index (viewer->images, GrImage, viewer->index);
+                ri = g_ptr_array_index (viewer->images, viewer->index);
                 pixbuf = load_pixbuf_fill_size (ri->path, 360, 240);
 
                 vis = gtk_stack_get_visible_child_name (GTK_STACK (viewer->stack));
@@ -169,7 +169,7 @@ populate_preview (GrImageViewer *viewer)
         container_remove_all (GTK_CONTAINER (viewer->preview_list));
 
         for (i = 0; i < viewer->images->len; i++) {
-                GrImage *ri = &g_array_index (viewer->images, GrImage, i);
+                GrImage *ri = g_ptr_array_index (viewer->images, i);
                 g_autoptr(GdkPixbuf) pb = load_pixbuf_fill_size (ri->path, 60, 40);
                 GtkWidget *image;
 
@@ -346,12 +346,10 @@ preview_selected (GrImageViewer *viewer)
 
 static void
 add_image (GrImageViewer  *viewer,
-           GrImage *ri,
+           GrImage        *ri,
            gboolean        select)
 {
-        g_array_append_vals (viewer->images, ri, 1);
-        ri = &g_array_index (viewer->images, GrImage, viewer->images->len - 1);
-        ri->path = g_strdup (ri->path);
+        g_ptr_array_add (viewer->images, ri);
 
         populate_preview (viewer);
         if (select)
@@ -369,7 +367,7 @@ image_received (GtkClipboard *clipboard,
         GrImageViewer *viewer = data;
 
         if (pixbuf) {
-                GrImage ri;
+                GrImage *ri;
                 g_autofree char *dir = NULL;
                 g_autofree char *path = NULL;
                 int fd;
@@ -388,9 +386,10 @@ image_received (GtkClipboard *clipboard,
                         return;
                 }
 
-                ri.path = g_strdup (path);
+                ri = g_new (GrImage, 1);
+                ri->path = g_strdup (path);
 
-                add_image (viewer, &ri, TRUE);
+                add_image (viewer, ri, TRUE);
         }
 }
 
@@ -485,7 +484,7 @@ gr_image_viewer_set_property (GObject      *object,
           {
           case PROP_IMAGES:
                   gr_image_viewer_set_images (self,
-                                              (GArray *) g_value_get_boxed (value),
+                                              (GPtrArray *) g_value_get_boxed (value),
                                               gr_image_viewer_get_index (self));
                   break;
 
@@ -544,23 +543,15 @@ gr_image_viewer_class_init (GrImageViewerClass *klass)
 
 void
 gr_image_viewer_set_images (GrImageViewer *viewer,
-                            GArray        *images,
+                            GPtrArray     *images,
                             int            index)
 {
-        int i;
-
         g_object_freeze_notify (G_OBJECT (viewer));
 
-        g_array_remove_range (viewer->images, 0, viewer->images->len);
-        g_object_notify (G_OBJECT (viewer), "images");
+        g_ptr_array_unref (viewer->images);
+        viewer->images = g_ptr_array_ref (images);
 
-        for (i = 0; i < images->len; i++) {
-                GrImage *ri = &g_array_index (images, GrImage, i);
-
-                g_array_append_vals (viewer->images, ri, 1);
-                ri = &g_array_index (viewer->images, GrImage, viewer->images->len - 1);
-                ri->path = g_strdup (ri->path);
-        }
+        g_object_notify (G_OBJECT (viewer), "images");
 
         populate_preview (viewer);
         viewer->index = index;
@@ -570,6 +561,12 @@ gr_image_viewer_set_images (GrImageViewer *viewer,
         g_object_thaw_notify (G_OBJECT (viewer));
 }
 
+GPtrArray *
+gr_image_viewer_get_images (GrImageViewer *viewer)
+{
+        return viewer->images;
+}
+
 static void
 file_chooser_response (GtkNativeDialog *self,
                        gint             response_id,
@@ -580,15 +577,14 @@ file_chooser_response (GtkNativeDialog *self,
 
                 names = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (self));
                 for (l = names; l; l = l->next) {
-                        GrImage ri;
-
-                        ri.path = import_image (l->data);
+                        GrImage *ri;
 
-                        add_image (viewer, &ri, TRUE);
+                        ri = g_new (GrImage, 1);
+                        ri->path = import_image (l->data);
 
-                        g_ptr_array_add (viewer->additions, g_strdup (ri.path));
+                        add_image (viewer, ri, TRUE);
 
-                        g_free (ri.path);
+                        g_ptr_array_add (viewer->additions, g_strdup (ri->path));
                 }
                 g_slist_free_full (names, g_free);
 
@@ -639,11 +635,11 @@ gr_image_viewer_remove_image (GrImageViewer *viewer)
 {
         GrImage *ri;
 
-        ri = &g_array_index (viewer->images, GrImage, viewer->index);
+        ri = g_ptr_array_index (viewer->images, viewer->index);
 
         g_ptr_array_add (viewer->removals, g_strdup (ri->path));
 
-        g_array_remove_index (viewer->images, viewer->index);
+        g_ptr_array_remove_index (viewer->images, viewer->index);
 
         if (viewer->index < viewer->images->len) {
                 populate_preview (viewer);
@@ -674,7 +670,7 @@ gr_image_viewer_rotate_image (GrImageViewer *viewer,
 
         g_assert (angle == 0 || angle == 90 || angle == 180 || angle == 270);
 
-        ri = &g_array_index (viewer->images, GrImage, viewer->index);
+        ri = g_ptr_array_index (viewer->images, viewer->index);
 
         path = rotate_image (ri->path, angle);
         g_ptr_array_add (viewer->removals, g_strdup (ri->path));
diff --git a/src/gr-image-viewer.h b/src/gr-image-viewer.h
index 3e8e32d..06b4524 100644
--- a/src/gr-image-viewer.h
+++ b/src/gr-image-viewer.h
@@ -30,9 +30,10 @@ G_DECLARE_FINAL_TYPE (GrImageViewer, gr_image_viewer, GR, IMAGE_VIEWER, GtkBox)
 
 GrImageViewer *gr_image_viewer_new          (void);
 void           gr_image_viewer_set_images   (GrImageViewer *viewer,
-                                             GArray        *images,
+                                             GPtrArray     *images,
                                              int            index);
 
+GPtrArray     *gr_image_viewer_get_images   (GrImageViewer *viewer);
 void           gr_image_viewer_add_image    (GrImageViewer *viewer);
 void           gr_image_viewer_remove_image (GrImageViewer *viewer);
 void           gr_image_viewer_rotate_image (GrImageViewer *viewer,
diff --git a/src/gr-images.c b/src/gr-images.c
index a56e4d5..7cd8fc8 100644
--- a/src/gr-images.c
+++ b/src/gr-images.c
@@ -22,21 +22,8 @@
 
 #include "gr-images.h"
 
-static void
-gr_image_clear (gpointer data)
-{
-        GrImage *image = data;
-
-        g_clear_pointer (&image->path, g_free);
-}
-
-GArray *
+GPtrArray *
 gr_image_array_new (void)
 {
-        GArray *a;
-
-        a = g_array_new (TRUE, TRUE, sizeof (GrImage));
-        g_array_set_clear_func (a, gr_image_clear);
-
-        return a;
+        return g_ptr_array_new_with_free_func (g_free);
 }
diff --git a/src/gr-images.h b/src/gr-images.h
index 2bd195a..9fe19a3 100644
--- a/src/gr-images.h
+++ b/src/gr-images.h
@@ -28,6 +28,6 @@ typedef struct {
         char *path;
 } GrImage;
 
-GArray *gr_image_array_new (void);
+GPtrArray *gr_image_array_new (void);
 
 G_END_DECLS
diff --git a/src/gr-recipe-importer.c b/src/gr-recipe-importer.c
index d6bb843..9dd62df 100644
--- a/src/gr-recipe-importer.c
+++ b/src/gr-recipe-importer.c
@@ -280,7 +280,7 @@ static gboolean
 import_recipe (GrRecipeImporter *importer)
 {
         GrRecipeStore *store;
-        g_autoptr(GArray) images = NULL;
+        g_autoptr(GPtrArray) images = NULL;
         g_autoptr(GrRecipe) recipe = NULL;
         g_autoptr(GError) error = NULL;
         g_autofree char *id = NULL;
@@ -292,7 +292,7 @@ import_recipe (GrRecipeImporter *importer)
         if (importer->recipe_paths) {
                 int i;
                 for (i = 0; importer->recipe_paths[i]; i++) {
-                        GrImage ri;
+                        GrImage *ri;
                         char *new_path;
 
                         if (!copy_image (importer, importer->recipe_paths[i], &new_path, &error)) {
@@ -300,8 +300,9 @@ import_recipe (GrRecipeImporter *importer)
                                 return FALSE;
                         }
 
-                        ri.path = new_path;
-                        g_array_append_val (images, ri);
+                        ri = g_new (GrImage, 1);
+                        ri->path = new_path;
+                        g_ptr_array_add (images, ri);
                 }
         }
 
diff --git a/src/gr-recipe-small-tile.c b/src/gr-recipe-small-tile.c
index 1316a74..e4a184e 100644
--- a/src/gr-recipe-small-tile.c
+++ b/src/gr-recipe-small-tile.c
@@ -90,7 +90,7 @@ recipe_small_tile_set_recipe (GrRecipeSmallTile *tile,
                 const char *author;
                 g_autoptr(GrChef) chef = NULL;
                 g_autofree char *tmp = NULL;
-                g_autoptr(GArray) images = NULL;
+                GPtrArray *images;
 
                 name = gr_recipe_get_translated_name (recipe);
                 author = gr_recipe_get_author (recipe);
@@ -100,7 +100,7 @@ recipe_small_tile_set_recipe (GrRecipeSmallTile *tile,
                 tmp = g_strdup_printf (_("by %s"), chef ? gr_chef_get_name (chef) : _("Anonymous"));
                 gtk_label_set_label (GTK_LABEL (tile->author), tmp);
 
-                g_object_get (recipe, "images", &images, NULL);
+                images = gr_recipe_get_images (recipe);
                 if (images->len > 0) {
                         int index;
                         GrImage *ri;
@@ -110,7 +110,7 @@ recipe_small_tile_set_recipe (GrRecipeSmallTile *tile,
                         if (index < 0 || index >= images->len)
                                 index = 0;
 
-                        ri = &g_array_index (images, GrImage, index);
+                        ri = g_ptr_array_index (images, index);
                         pixbuf = load_pixbuf_fill_size (ri->path, 64, 64);
                         gtk_image_set_from_pixbuf (GTK_IMAGE (tile->image), pixbuf);
                 }
diff --git a/src/gr-recipe-store.c b/src/gr-recipe-store.c
index 7b09389..511b2e2 100644
--- a/src/gr-recipe-store.c
+++ b/src/gr-recipe-store.c
@@ -216,8 +216,7 @@ load_recipes (GrRecipeStore *self,
                 int spiciness;
                 int default_image = 0;
                 GrDiets diets;
-                g_autoptr(GArray) images = NULL;
-                GrImage ri;
+                g_autoptr(GPtrArray) images = NULL;
                 g_autoptr(GDateTime) ctime = NULL;
                 g_autoptr(GDateTime) mtime = NULL;
                 char *tmp;
@@ -369,8 +368,11 @@ load_recipes (GrRecipeStore *self,
                 images = gr_image_array_new ();
                 if (paths) {
                         for (j = 0; paths[j]; j++) {
-                                ri.path = g_strdup (paths[j]);
-                                g_array_append_val (images, ri);
+                                GrImage *ri;
+
+                                ri = g_new (GrImage, 1);
+                                ri->path = g_strdup (paths[j]);
+                                g_ptr_array_add (images, ri);
                         }
                 }
 
@@ -510,7 +512,7 @@ save_recipes (GrRecipeStore *self)
                 const char *ingredients;
                 const char *instructions;
                 const char *notes;
-                g_autoptr(GArray) images = NULL;
+                g_autoptr(GPtrArray) images = NULL;
                 int serves;
                 int spiciness;
                 GrDiets diets;
@@ -547,7 +549,7 @@ save_recipes (GrRecipeStore *self)
 
                 paths = g_new0 (char *, images->len + 1);
                 for (i = 0; i < images->len; i++) {
-                        GrImage *ri = &g_array_index (images, GrImage, i);
+                        GrImage *ri = g_ptr_array_index (images, i);
                         if (g_str_has_prefix (ri->path, dir))
                                 paths[i] = g_strdup (ri->path + strlen (dir) + 1);
                         else
diff --git a/src/gr-recipe-tile.c b/src/gr-recipe-tile.c
index 0d1f9a6..457d12a 100644
--- a/src/gr-recipe-tile.c
+++ b/src/gr-recipe-tile.c
@@ -71,7 +71,7 @@ recipe_tile_set_recipe (GrRecipeTile *tile,
                 const char *author;
                 g_autoptr(GrChef) chef = NULL;
                 g_autofree char *tmp = NULL;
-                g_autoptr(GArray) images = NULL;
+                GPtrArray *images;
 
                 name = gr_recipe_get_translated_name (recipe);
                 author = gr_recipe_get_author (recipe);
@@ -96,7 +96,7 @@ recipe_tile_set_recipe (GrRecipeTile *tile,
                         if (index < 0 || index >= images->len)
                                 index = 0;
 
-                        ri = &g_array_index (images, GrImage, index);
+                        ri = g_ptr_array_index (images, index);
                         pixbuf = load_pixbuf_fill_size (ri->path, width, height);
                         gtk_image_set_from_pixbuf (GTK_IMAGE (tile->image), pixbuf);
                 }
diff --git a/src/gr-recipe.c b/src/gr-recipe.c
index b572570..25b1628 100644
--- a/src/gr-recipe.c
+++ b/src/gr-recipe.c
@@ -49,7 +49,7 @@ struct _GrRecipe
         char *name;
         char *author;
         char *description;
-        GArray *images;
+        GPtrArray *images;
         int default_image;
 
         char *cuisine;
@@ -128,7 +128,7 @@ gr_recipe_finalize (GObject *object)
         g_free (self->ingredients);
         g_free (self->instructions);
         g_free (self->notes);
-        g_array_free (self->images, TRUE);
+        g_ptr_array_unref (self->images);
 
         g_free (self->cf_name);
         g_free (self->cf_description);
@@ -242,23 +242,6 @@ gr_recipe_get_property (GObject    *object,
         }
 }
 
-static void
-set_images (GrRecipe *self,
-            GArray   *images)
-{
-        int i;
-
-        g_array_remove_range (self->images, 0, self->images->len);
-        for (i = 0; i < images->len; i++) {
-                GrImage *ri = &g_array_index (images, GrImage, i);
-                g_array_append_vals (self->images, ri, 1);
-                ri = &g_array_index (self->images, GrImage, i);
-                ri->path = g_strdup (ri->path);
-        }
-
-        g_object_notify (G_OBJECT (self), "images");
-}
-
 static char *
 gr_recipe_get_chef_fullname (GrRecipe *self)
 {
@@ -324,7 +307,9 @@ gr_recipe_set_property (GObject      *object,
                 break;
 
         case PROP_IMAGES:
-                set_images (self, (GArray *) g_value_get_boxed (value));
+                if (self->images)
+                        g_ptr_array_unref (self->images);
+                self->images = g_ptr_array_ref ((GPtrArray *) g_value_get_boxed (value));
                 break;
 
         case PROP_DEFAULT_IMAGE:
@@ -454,8 +439,8 @@ gr_recipe_class_init (GrRecipeClass *klass)
         g_object_class_install_property (object_class, PROP_DESCRIPTION, pspec);
 
         pspec = g_param_spec_boxed ("images", NULL, NULL,
-                                     G_TYPE_ARRAY,
-                                     G_PARAM_READWRITE);
+                                    G_TYPE_PTR_ARRAY,
+                                    G_PARAM_READWRITE);
         g_object_class_install_property (object_class, PROP_IMAGES, pspec);
 
         pspec = g_param_spec_int ("default-image", NULL, NULL,
@@ -716,6 +701,12 @@ gr_recipe_is_contributed (GrRecipe *recipe)
         return recipe->contributed;
 }
 
+GPtrArray *
+gr_recipe_get_images (GrRecipe *recipe)
+{
+        return recipe->images;
+}
+
 /* terms are assumed to be g_utf8_casefold'ed where appropriate */
 gboolean
 gr_recipe_matches (GrRecipe    *recipe,
diff --git a/src/gr-recipe.h b/src/gr-recipe.h
index 8ca5709..10408e0 100644
--- a/src/gr-recipe.h
+++ b/src/gr-recipe.h
@@ -55,6 +55,7 @@ GDateTime      *gr_recipe_get_ctime        (GrRecipe   *recipe);
 GDateTime      *gr_recipe_get_mtime        (GrRecipe   *recipe);
 gboolean        gr_recipe_is_readonly      (GrRecipe   *recipe);
 gboolean        gr_recipe_is_contributed   (GrRecipe   *recipe);
+GPtrArray      *gr_recipe_get_images       (GrRecipe   *recipe);
 
 const char     *gr_recipe_get_translated_name         (GrRecipe   *recipe);
 const char     *gr_recipe_get_translated_description  (GrRecipe   *recipe);
diff --git a/src/gr-window.c b/src/gr-window.c
index 8dd7a9d..c9d0e90 100644
--- a/src/gr-window.c
+++ b/src/gr-window.c
@@ -1403,9 +1403,9 @@ gr_window_show_season (GrWindow   *window,
 }
 
 void
-gr_window_show_image (GrWindow *window,
-                      GArray   *images,
-                      int       index)
+gr_window_show_image (GrWindow  *window,
+                      GPtrArray *images,
+                      int        index)
 {
         if (images && images->len > 0) {
                 gr_image_page_set_images (GR_IMAGE_PAGE (window->image_page), images);
diff --git a/src/gr-window.h b/src/gr-window.h
index 5eb8220..e56c56c 100644
--- a/src/gr-window.h
+++ b/src/gr-window.h
@@ -64,7 +64,7 @@ void            gr_window_show_season                (GrWindow   *window,
                                                       const char *season,
                                                       const char *title);
 void            gr_window_show_image                 (GrWindow   *window,
-                                                      GArray     *images,
+                                                      GPtrArray  *images,
                                                       int         index);
 
 void            gr_window_offer_undelete             (GrWindow   *window,


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