[recipes/image-download: 3/11] Turn GrImage into an object



commit d9045c9c69a4a95fd5fedc8e69de7986c7b12375
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Mar 2 22:12:37 2017 -0500

    Turn GrImage into an object
    
    This makes things a little less ad-hoc, and will pave
    the ground for more complex image handling.

 src/gr-cooking-view.c          |    4 +-
 src/gr-edit-page.c             |    2 +-
 src/gr-image-page.c            |    2 +-
 src/gr-image-viewer.c          |   30 ++++++++------------
 src/gr-image.c                 |   58 +++++++++++++++++++++++++++++++++++++++-
 src/gr-image.h                 |   11 +++++--
 src/gr-recipe-exporter.c       |    8 +++---
 src/gr-recipe-importer.c       |    3 +-
 src/gr-recipe-printer.c        |   11 +++----
 src/gr-recipe-small-tile.c     |    2 +-
 src/gr-recipe-store.c          |   11 +++----
 src/gr-recipe-tile.c           |    4 +-
 src/gr-shell-search-provider.c |    7 ++---
 13 files changed, 102 insertions(+), 51 deletions(-)
---
diff --git a/src/gr-cooking-view.c b/src/gr-cooking-view.c
index 4bd6dfb..15c5e0b 100644
--- a/src/gr-cooking-view.c
+++ b/src/gr-cooking-view.c
@@ -322,9 +322,9 @@ setup_step (GrCookingView *view)
                 gtk_widget_set_halign (view->text_box, GTK_ALIGN_START);
                 ri = g_ptr_array_index (view->images, s->image);
                 if (view->wide)
-                        pixbuf = load_pixbuf_fill_size (ri->path, 640, 480);
+                        pixbuf = load_pixbuf_fill_size (gr_image_get_path (ri), 640, 480);
                 else
-                        pixbuf = load_pixbuf_fill_size (ri->path, 320, 240);
+                        pixbuf = load_pixbuf_fill_size (gr_image_get_path (ri), 320, 240);
                 gtk_image_set_from_pixbuf (GTK_IMAGE (view->cooking_image), pixbuf);
                 gtk_stack_set_visible_child_name (GTK_STACK (view->cooking_stack), "image");
         }
diff --git a/src/gr-edit-page.c b/src/gr-edit-page.c
index c5b55e1..9f5b51e 100644
--- a/src/gr-edit-page.c
+++ b/src/gr-edit-page.c
@@ -176,7 +176,7 @@ populate_image_flowbox (GrEditPage *page)
 
         for (i = 0; i < images->len; i++) {
                 GrImage *ri = g_ptr_array_index (images, i);
-                g_autoptr(GdkPixbuf) pb = load_pixbuf_fill_size (ri->path, 60, 40);
+                g_autoptr(GdkPixbuf) pb = load_pixbuf_fill_size (gr_image_get_path (ri), 60, 40);
                 GtkWidget *image;
                 GtkWidget *child;
 
diff --git a/src/gr-image-page.c b/src/gr-image-page.c
index 9f45bc0..94f28d8 100644
--- a/src/gr-image-page.c
+++ b/src/gr-image-page.c
@@ -97,7 +97,7 @@ set_current_image (GrImagePage *page)
                 gdk_monitor_get_geometry (monitor, &geom);
 
                 ri = g_ptr_array_index (page->images, page->index);
-                pixbuf = load_pixbuf_fit_size (ri->path, geom.width - 80, geom.height - 80, FALSE);
+                pixbuf = load_pixbuf_fit_size (gr_image_get_path (ri), geom.width - 80, geom.height - 80, 
FALSE);
                 gtk_image_set_from_pixbuf (GTK_IMAGE (page->image), pixbuf);
         }
 }
diff --git a/src/gr-image-viewer.c b/src/gr-image-viewer.c
index 9fbd3a4..e09fe47 100644
--- a/src/gr-image-viewer.c
+++ b/src/gr-image-viewer.c
@@ -141,7 +141,7 @@ set_current_image (GrImageViewer *viewer)
                 const char *vis;
 
                 ri = g_ptr_array_index (viewer->images, viewer->index);
-                pixbuf = load_pixbuf_fill_size (ri->path, 360, 240);
+                pixbuf = load_pixbuf_fill_size (gr_image_get_path (ri), 360, 240);
 
                 vis = gtk_stack_get_visible_child_name (GTK_STACK (viewer->stack));
                 if (strcmp (vis, "image1") == 0) {
@@ -170,7 +170,7 @@ populate_preview (GrImageViewer *viewer)
 
         for (i = 0; i < viewer->images->len; i++) {
                 GrImage *ri = g_ptr_array_index (viewer->images, i);
-                g_autoptr(GdkPixbuf) pb = load_pixbuf_fill_size (ri->path, 60, 40);
+                g_autoptr(GdkPixbuf) pb = load_pixbuf_fill_size (gr_image_get_path (ri), 60, 40);
                 GtkWidget *image;
 
                 image = gtk_image_new_from_pixbuf (pb);
@@ -386,9 +386,7 @@ image_received (GtkClipboard *clipboard,
                         return;
                 }
 
-                ri = g_new (GrImage, 1);
-                ri->path = g_strdup (path);
-
+                ri = gr_image_new (path);
                 add_image (viewer, ri, TRUE);
         }
 }
@@ -578,13 +576,13 @@ file_chooser_response (GtkNativeDialog *self,
                 names = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (self));
                 for (l = names; l; l = l->next) {
                         GrImage *ri;
+                        g_autofree char *path = NULL;
 
-                        ri = g_new (GrImage, 1);
-                        ri->path = import_image (l->data);
-
+                        path = import_image (l->data);
+                        ri = gr_image_new (path);
                         add_image (viewer, ri, TRUE);
 
-                        g_ptr_array_add (viewer->additions, g_strdup (ri->path));
+                        g_ptr_array_add (viewer->additions, g_strdup (path));
                 }
                 g_slist_free_full (names, g_free);
 
@@ -637,7 +635,7 @@ gr_image_viewer_remove_image (GrImageViewer *viewer)
 
         ri = g_ptr_array_index (viewer->images, viewer->index);
 
-        g_ptr_array_add (viewer->removals, g_strdup (ri->path));
+        g_ptr_array_add (viewer->removals, g_strdup (gr_image_get_path (ri)));
 
         g_ptr_array_remove_index (viewer->images, viewer->index);
 
@@ -671,14 +669,10 @@ gr_image_viewer_rotate_image (GrImageViewer *viewer,
         g_assert (angle == 0 || angle == 90 || angle == 180 || angle == 270);
 
         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));
-
-        g_free (ri->path);
-        ri->path = path;
-
-        g_ptr_array_add (viewer->additions, g_strdup (ri->path));
+        path = rotate_image (gr_image_get_path (ri), angle);
+        gr_image_set_path (ri, path);
+        g_ptr_array_add (viewer->removals, g_strdup (path));
+        g_ptr_array_add (viewer->additions, g_strdup (path));
 
         populate_preview (viewer);
         set_current_image (viewer);
diff --git a/src/gr-image.c b/src/gr-image.c
index 0aab082..dd61b2a 100644
--- a/src/gr-image.c
+++ b/src/gr-image.c
@@ -22,8 +22,64 @@
 
 #include "gr-image.h"
 
+struct _GrImage
+{
+        GObject parent_instance;
+        char *path;
+};
+
+G_DEFINE_TYPE (GrImage, gr_image, G_TYPE_OBJECT)
+
+static void
+gr_image_finalize (GObject *object)
+{
+        GrImage *image = GR_IMAGE (object);
+
+        g_free (image->path);
+
+        G_OBJECT_CLASS (gr_image_parent_class)->finalize (object);
+}
+
+static void
+gr_image_class_init (GrImageClass *klass)
+{
+        GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+        object_class->finalize = gr_image_finalize;
+}
+
+static void
+gr_image_init (GrImage *image)
+{
+}
+
+GrImage *
+gr_image_new (const char *path)
+{
+        GrImage *image;
+
+        image = g_object_new (GR_TYPE_IMAGE, NULL);
+        gr_image_set_path (image, path);
+
+        return image;
+}
+
+void
+gr_image_set_path (GrImage    *image,
+                   const char *path)
+{
+        g_free (image->path);
+        image->path = g_strdup (path);
+}
+
+const char *
+gr_image_get_path (GrImage *image)
+{
+        return image->path;
+}
+
 GPtrArray *
 gr_image_array_new (void)
 {
-        return g_ptr_array_new_with_free_func (g_free);
+        return g_ptr_array_new_with_free_func (g_object_unref);
 }
diff --git a/src/gr-image.h b/src/gr-image.h
index 9fe19a3..f438bef 100644
--- a/src/gr-image.h
+++ b/src/gr-image.h
@@ -24,9 +24,14 @@
 
 G_BEGIN_DECLS
 
-typedef struct {
-        char *path;
-} GrImage;
+#define GR_TYPE_IMAGE (gr_image_get_type())
+
+G_DECLARE_FINAL_TYPE (GrImage, gr_image, GR, IMAGE, GObject)
+
+GrImage    *gr_image_new      (const char *path);
+void        gr_image_set_path (GrImage    *image,
+                               const char *path);
+const char *gr_image_get_path (GrImage    *image);
 
 GPtrArray *gr_image_array_new (void);
 
diff --git a/src/gr-recipe-exporter.c b/src/gr-recipe-exporter.c
index 57ad896..3d61389 100644
--- a/src/gr-recipe-exporter.c
+++ b/src/gr-recipe-exporter.c
@@ -274,7 +274,7 @@ export_one_recipe (GrRecipeExporter  *exporter,
         GrDiets diets;
         GDateTime *ctime;
         GDateTime *mtime;
-        g_autoptr(GArray) images = NULL;
+        GPtrArray *images;
         g_auto(GStrv) paths = NULL;
         int i;
         g_autofree char *imagedir = NULL;
@@ -298,20 +298,20 @@ export_one_recipe (GrRecipeExporter  *exporter,
         default_image = gr_recipe_get_default_image (recipe);
         spiciness = gr_recipe_get_spiciness (recipe);
 
-        g_object_get (recipe, "images", &images, NULL);
+        images = gr_recipe_get_images (recipe);
 
         imagedir = g_build_filename (exporter->dir, "images", NULL);
         g_mkdir_with_parents (imagedir, 0755);
 
         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);
                 g_autoptr(GFile) source = NULL;
                 g_autoptr(GFile) dest = NULL;
                 g_autofree char *basename = NULL;
                 g_autofree char *destname = NULL;
 
-                source = g_file_new_for_path (ri->path);
+                source = g_file_new_for_path (gr_image_get_path (ri));
                 basename = g_file_get_basename (source);
                 destname = g_build_filename (imagedir, basename, NULL);
 
diff --git a/src/gr-recipe-importer.c b/src/gr-recipe-importer.c
index b1374fe..af3a0b1 100644
--- a/src/gr-recipe-importer.c
+++ b/src/gr-recipe-importer.c
@@ -300,8 +300,7 @@ import_recipe (GrRecipeImporter *importer)
                                 return FALSE;
                         }
 
-                        ri = g_new (GrImage, 1);
-                        ri->path = new_path;
+                        ri = gr_image_new (new_path);
                         g_ptr_array_add (images, ri);
                 }
         }
diff --git a/src/gr-recipe-printer.c b/src/gr-recipe-printer.c
index 2e396b6..253ded1 100644
--- a/src/gr-recipe-printer.c
+++ b/src/gr-recipe-printer.c
@@ -108,8 +108,7 @@ begin_print (GtkPrintOperation *operation,
         double page_height;
         GList *page_breaks;
         g_autoptr(GString) s = NULL;
-        g_autoptr(GArray) images = NULL;
-        GrImage *ri;
+        GPtrArray *images;
         PangoRectangle title_rect;
         PangoRectangle left_rect;
         int num_lines;
@@ -128,11 +127,11 @@ begin_print (GtkPrintOperation *operation,
         width = gtk_print_context_get_width (context);
         height = gtk_print_context_get_height (context);
 
-        g_object_get (printer->recipe, "images", &images, NULL);
+        images = gr_recipe_get_images (printer->recipe);
         if (images && images->len > 0) {
-                int def_index = gr_recipe_get_default_image(printer->recipe);
-                ri = &g_array_index (images, GrImage, def_index);
-                printer->image = load_pixbuf_fit_size (ri->path, width / 2, height / 4, FALSE);
+                int def_index = gr_recipe_get_default_image (printer->recipe);
+                GrImage *ri = g_ptr_array_index (images, def_index);
+                printer->image = load_pixbuf_fit_size (gr_image_get_path (ri), width / 2, height / 4, FALSE);
         }
 
         title_font = pango_font_description_from_string ("Cantarell Bold 18");
diff --git a/src/gr-recipe-small-tile.c b/src/gr-recipe-small-tile.c
index 8c455ca..4d5968c 100644
--- a/src/gr-recipe-small-tile.c
+++ b/src/gr-recipe-small-tile.c
@@ -111,7 +111,7 @@ recipe_small_tile_set_recipe (GrRecipeSmallTile *tile,
                                 index = 0;
 
                         ri = g_ptr_array_index (images, index);
-                        pixbuf = load_pixbuf_fill_size (ri->path, 64, 64);
+                        pixbuf = load_pixbuf_fill_size (gr_image_get_path (ri), 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 0d79f56..6d346db 100644
--- a/src/gr-recipe-store.c
+++ b/src/gr-recipe-store.c
@@ -370,9 +370,7 @@ load_recipes (GrRecipeStore *self,
                 if (paths) {
                         for (j = 0; paths[j]; j++) {
                                 GrImage *ri;
-
-                                ri = g_new (GrImage, 1);
-                                ri->path = g_strdup (paths[j]);
+                                ri = gr_image_new (paths[j]);
                                 g_ptr_array_add (images, ri);
                         }
                 }
@@ -551,10 +549,11 @@ save_recipes (GrRecipeStore *self)
                 paths = g_new0 (char *, images->len + 1);
                 for (i = 0; i < images->len; 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);
+                        const char *img_path = gr_image_get_path (ri);
+                        if (g_str_has_prefix (img_path, dir))
+                                paths[i] = g_strdup (img_path + strlen (dir) + 1);
                         else
-                                paths[i] = g_strdup (ri->path);
+                                paths[i] = g_strdup (img_path);
                 }
 
                 // For readonly recipes, we just store notes
diff --git a/src/gr-recipe-tile.c b/src/gr-recipe-tile.c
index a0bb6b7..d142072 100644
--- a/src/gr-recipe-tile.c
+++ b/src/gr-recipe-tile.c
@@ -81,7 +81,7 @@ recipe_tile_set_recipe (GrRecipeTile *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;
@@ -97,7 +97,7 @@ recipe_tile_set_recipe (GrRecipeTile *tile,
                                 index = 0;
 
                         ri = g_ptr_array_index (images, index);
-                        pixbuf = load_pixbuf_fill_size (ri->path, width, height);
+                        pixbuf = load_pixbuf_fill_size (gr_image_get_path (ri), width, height);
                         gtk_image_set_from_pixbuf (GTK_IMAGE (tile->image), pixbuf);
                 }
         }
diff --git a/src/gr-shell-search-provider.c b/src/gr-shell-search-provider.c
index b68ff92..bb05394 100644
--- a/src/gr-shell-search-provider.c
+++ b/src/gr-shell-search-provider.c
@@ -166,14 +166,13 @@ handle_get_subsearch_result_set (GrShellSearchProvider2  *skeleton,
 static GdkPixbuf *
 gr_recipe_get_pixbuf (GrRecipe *recipe)
 {
-        g_autoptr(GArray) images = NULL;
-
-        g_object_get (recipe, "images", &images, NULL);
+        GPtrArray *images;
 
+        images = gr_recipe_get_images (recipe);
         if (images->len > 0) {
                 int index = gr_recipe_get_default_image (recipe);
                 GrImage *ri = &g_array_index (images, GrImage, index);
-                return load_pixbuf_fill_size (ri->path, 64, 64);
+                return load_pixbuf_fill_size (gr_image_get_path (ri), 64, 64);
         }
 
         return NULL;


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