[recipes] Import images



commit d59ff4db4b279a191ebd36e57daf6f8dd67e7f6a
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Feb 18 16:19:15 2017 -0500

    Import images
    
    When an image is added to a recipe, copy it to
    $XDG_DATA_HOME/gnome-recipes/images, applying any
    embedded orientation.

 src/gr-image-viewer.c |   79 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 78 insertions(+), 1 deletions(-)
---
diff --git a/src/gr-image-viewer.c b/src/gr-image-viewer.c
index ded702e..7ac8812 100644
--- a/src/gr-image-viewer.c
+++ b/src/gr-image-viewer.c
@@ -536,6 +536,83 @@ gr_image_viewer_set_images (GrImageViewer *viewer,
         g_object_thaw_notify (G_OBJECT (viewer));
 }
 
+static char *
+get_import_name (const char *path)
+{
+        g_autofree char *dir = NULL;
+        g_autofree char *basename = NULL;
+        char *filename;
+        char *dot;
+        char *suffix;
+        int i;
+
+        dir = g_build_filename (get_user_data_dir (), "images", NULL);
+        g_mkdir_with_parents (dir, 0755);
+
+        basename = g_path_get_basename (path);
+
+        filename = g_build_filename (dir, basename, NULL);
+        if (!g_file_test (filename, G_FILE_TEST_EXISTS))
+                return filename;
+
+        g_free (filename);
+
+        dot = strrchr (basename, '.');
+        if (dot) {
+                suffix = dot + 1;
+                *dot = '\0';
+        }
+        else {
+                suffix = NULL;
+        }
+        for (i = 0; i < 100; i++) {
+                char buf[10];
+
+                g_snprintf (buf, 10, "%d", i);
+                filename = g_strconcat (basename, buf, ".", suffix, NULL);
+                if (!g_file_test (filename, G_FILE_TEST_EXISTS))
+                        return filename;
+                g_free (filename);
+        }
+
+        return NULL;
+}
+
+static char *
+import_image (const char *path)
+{
+        g_autoptr(GdkPixbuf) pixbuf = NULL;
+        g_autoptr(GdkPixbuf) oriented = NULL;
+        g_autoptr(GError) error = NULL;
+        char *imported;
+        GdkPixbufFormat *format;
+        g_autofree char *format_name = NULL;
+
+        // FIXME: this could be much more efficient
+        pixbuf = gdk_pixbuf_new_from_file (path, &error);
+        if (pixbuf== NULL) {
+                g_message ("Failed to load image '%s': %s", path, error->message);
+                return NULL;
+        }
+
+        imported = get_import_name (path);
+        format = gdk_pixbuf_get_file_info (path, NULL, NULL);
+        if (gdk_pixbuf_format_is_writable (format))
+                format_name = gdk_pixbuf_format_get_name (format);
+        else
+                format_name = g_strdup ("png");
+
+        g_print ("Loading %s (format %s), importing to %s\n", path, format_name, imported);
+        oriented = gdk_pixbuf_apply_embedded_orientation (pixbuf);
+
+        if (!gdk_pixbuf_save (oriented, imported, format_name, &error, NULL)) {
+                g_message ("Failed to import image: %s", error->message);
+                return NULL;
+        }
+
+        return imported;
+}
+
 static void
 file_chooser_response (GtkNativeDialog *self,
                        gint             response_id,
@@ -548,7 +625,7 @@ file_chooser_response (GtkNativeDialog *self,
                 for (l = names; l; l = l->next) {
                         GrRotatedImage ri;
 
-                        ri.path = g_strdup (l->data);
+                        ri.path = import_image (l->data);
                         ri.angle = 0;
 
                         add_image (viewer, &ri, TRUE);


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