Addl fix #299: File Compression and Archiving
[geeqie.git] / src / layout_image.c
index c18bf23..949b817 100644 (file)
@@ -283,6 +283,7 @@ static void image_animation_data_free(AnimationData *fd)
        if(!fd) return;
        if(fd->iter) g_object_unref(fd->iter);
        if(fd->gpa) g_object_unref(fd->gpa);
+       if(fd->cancellable) g_object_unref(fd->cancellable);
        g_free(fd);
 }
 
@@ -347,6 +348,10 @@ static gboolean layout_image_animate_check(LayoutWindow *lw)
                if(lw->animation)
                        {
                        lw->animation->valid = FALSE;
+                       if (lw->animation->cancellable)
+                               {
+                               g_cancellable_cancel(lw->animation->cancellable);
+                               }
                        lw->animation = NULL;
                        }
                return FALSE;
@@ -368,32 +373,84 @@ static void layout_image_animate_update_image(LayoutWindow *lw)
                }
 }
 
+
+static void animation_async_ready_cb(GObject *source_object, GAsyncResult *res, gpointer data)
+{
+       GError *error = NULL;
+       AnimationData *animation = data;
+
+       if (animation)
+               {
+               if (g_cancellable_is_cancelled(animation->cancellable))
+                       {
+                       gdk_pixbuf_animation_new_from_stream_finish(res, NULL);
+                       g_object_unref(animation->in_file);
+                       g_object_unref(animation->gfstream);
+                       image_animation_data_free(animation);
+                       return;
+                       }
+
+               animation->gpa = gdk_pixbuf_animation_new_from_stream_finish(res, &error);
+               if (animation->gpa)
+                       {
+                       if (!gdk_pixbuf_animation_is_static_image(animation->gpa))
+                               {
+                               animation->iter = gdk_pixbuf_animation_get_iter(animation->gpa, NULL);
+                               if (animation->iter)
+                                       {
+                                       animation->data_adr = animation->lw->image->image_fd;
+                                       animation->delay = gdk_pixbuf_animation_iter_get_delay_time(animation->iter);
+                                       animation->valid = TRUE;
+
+                                       layout_image_animate_update_image(animation->lw);
+
+                                       g_timeout_add(animation->delay, show_next_frame, animation);
+                                       }
+                               }
+                       }
+               else
+                       {
+                       log_printf("Error reading GIF file: %s\n", error->message);
+                       }
+
+               g_object_unref(animation->in_file);
+               g_object_unref(animation->gfstream);
+               }
+}
+
 static gboolean layout_image_animate_new_file(LayoutWindow *lw)
 {
-       GError *err=NULL;
+       GFileInputStream *gfstream;
+       GError *error = NULL;
+       AnimationData *animation;
+       GFile *in_file;
 
        if(!layout_image_animate_check(lw)) return FALSE;
 
        if(lw->animation) lw->animation->valid = FALSE;
 
-       lw->animation = g_malloc0(sizeof(AnimationData));
-
-       if(!(lw->animation->gpa = gdk_pixbuf_animation_new_from_file(lw->image->image_fd->path,&err)) || err ||
-               gdk_pixbuf_animation_is_static_image(lw->animation->gpa) ||
-               !(lw->animation->iter = gdk_pixbuf_animation_get_iter(lw->animation->gpa,NULL)))
+       if (lw->animation)
                {
-               image_animation_data_free(lw->animation);
-               lw->animation = NULL;
-               return FALSE;
+               g_cancellable_cancel(lw->animation->cancellable);
                }
 
-       lw->animation->data_adr = lw->image->image_fd;
-       lw->animation->delay = gdk_pixbuf_animation_iter_get_delay_time(lw->animation->iter);
-       lw->animation->valid = TRUE;
-
-       layout_image_animate_update_image(lw);
+       animation = g_new0(AnimationData, 1);
+       lw->animation = animation;
+       animation->lw = lw;
+       animation->cancellable = g_cancellable_new();
 
-       g_timeout_add(lw->animation->delay, show_next_frame, lw->animation);
+       in_file = g_file_new_for_path(lw->image->image_fd->path);
+       animation->in_file = in_file;
+       gfstream = g_file_read(in_file, NULL, &error);
+       if (gfstream)
+               {
+               animation->gfstream = gfstream;
+               gdk_pixbuf_animation_new_from_stream_async((GInputStream*)gfstream, animation->cancellable, animation_async_ready_cb, animation);
+               }
+       else
+               {
+               log_printf("Error reading GIF file: %s\nError: %s\n", lw->image->image_fd->path, error->message);
+               }
 
        return TRUE;
 }
@@ -459,12 +516,14 @@ static void li_pop_menu_edit_cb(GtkWidget *widget, gpointer data)
        file_util_start_editor_from_file(key, layout_image_get_fd(lw), lw->window);
 }
 
+#if !GTK_CHECK_VERSION(3,0,0)
 static void li_pop_menu_wallpaper_cb(GtkWidget *widget, gpointer data)
 {
        LayoutWindow *lw = data;
 
        layout_image_to_root(lw);
 }
+#endif
 
 static void li_pop_menu_alter_cb(GtkWidget *widget, gpointer data)
 {
@@ -524,6 +583,17 @@ static void li_pop_menu_copy_path_unquoted_cb(GtkWidget *widget, gpointer data)
        file_util_copy_path_to_clipboard(layout_image_get_fd(lw), FALSE);
 }
 
+static void li_pop_menu_copy_image_cb(GtkWidget *widget, gpointer data)
+{
+       LayoutWindow *lw = data;
+       ImageWindow *imd = lw->image;
+
+       GdkPixbuf *pixbuf;
+       pixbuf = image_get_pixbuf(imd);
+       if (!pixbuf) return;
+       gtk_clipboard_set_image(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), pixbuf);
+}
+
 static void li_pop_menu_move_cb(GtkWidget *widget, gpointer data)
 {
        LayoutWindow *lw = data;
@@ -611,6 +681,27 @@ static void li_set_layout_path_cb(GtkWidget *widget, gpointer data)
        if (fd) layout_set_fd(lw, fd);
 }
 
+static void li_open_archive_cb(GtkWidget *widget, gpointer data)
+{
+       LayoutWindow *lw = data;
+       LayoutWindow *lw_new;
+       gchar *dest_dir;
+
+       if (!layout_valid(&lw)) return;
+
+       dest_dir = open_archive(layout_image_get_fd(lw));
+       if (dest_dir)
+               {
+               lw_new = layout_new_from_default();
+               layout_set_path(lw_new, dest_dir);
+               g_free(dest_dir);
+               }
+       else
+               {
+               warning_dialog(_("Cannot open archive file"), _("See the Log Window"), GTK_STOCK_DIALOG_WARNING, NULL);
+               }
+}
+
 static gboolean li_check_if_current_path(LayoutWindow *lw, const gchar *path)
 {
        gchar *dirname;
@@ -704,6 +795,12 @@ static GtkWidget *layout_image_pop_menu(LayoutWindow *lw)
        item = menu_item_add(menu, _("_Go to directory view"), G_CALLBACK(li_set_layout_path_cb), lw);
        if (!path || li_check_if_current_path(lw, path)) gtk_widget_set_sensitive(item, FALSE);
 
+       item = menu_item_add_stock(menu, _("Open archive"), GTK_STOCK_OPEN, G_CALLBACK(li_open_archive_cb), lw);
+       if (!path || lw->image->image_fd->format_class != FORMAT_CLASS_ARCHIVE)
+               {
+               gtk_widget_set_sensitive(item, FALSE);
+               }
+
        menu_item_add_divider(menu);
 
        item = menu_item_add_stock(menu, _("_Copy..."), GTK_STOCK_COPY, G_CALLBACK(li_pop_menu_copy_cb), lw);
@@ -714,6 +811,7 @@ static GtkWidget *layout_image_pop_menu(LayoutWindow *lw)
        if (!path) gtk_widget_set_sensitive(item, FALSE);
        item = menu_item_add(menu, _("_Copy path"), G_CALLBACK(li_pop_menu_copy_path_cb), lw);
        item = menu_item_add(menu, _("_Copy path unquoted"), G_CALLBACK(li_pop_menu_copy_path_unquoted_cb), lw);
+       item = menu_item_add(menu, _("Copy _image"), G_CALLBACK(li_pop_menu_copy_image_cb), lw);
        if (!path) gtk_widget_set_sensitive(item, FALSE);
        menu_item_add_divider(menu);
 
@@ -804,6 +902,7 @@ static void layout_image_dnd_receive(GtkWidget *widget, GdkDragContext *context,
 {
        LayoutWindow *lw = data;
        gint i;
+       gchar *url;
 
 
        for (i = 0; i < MAX_SPLIT_IMAGES; i++)
@@ -817,8 +916,13 @@ static void layout_image_dnd_receive(GtkWidget *widget, GdkDragContext *context,
                layout_image_activate(lw, i, FALSE);
                }
 
-
-       if (info == TARGET_URI_LIST || info == TARGET_APP_COLLECTION_MEMBER)
+       if (info == TARGET_TEXT_PLAIN)
+               {
+               url = g_strdup((gchar *)gtk_selection_data_get_data(selection_data));
+               download_web_file(url, FALSE, lw);
+               g_free(url);
+               }
+       else if (info == TARGET_URI_LIST || info == TARGET_APP_COLLECTION_MEMBER)
                {
                CollectionData *source;
                GList *list;
@@ -1202,7 +1306,14 @@ void layout_image_reset_orientation(LayoutWindow *lw)
 
        if (options->image.exif_rotate_enable)
                {
-               imd->orientation = metadata_read_int(imd->image_fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
+               if (g_strcmp0(imd->image_fd->format_name, "heif") != 0)
+                       {
+                       imd->orientation = metadata_read_int(imd->image_fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
+                       }
+               else
+                       {
+                       imd->orientation = EXIF_ORIENTATION_TOP_LEFT;
+                       }
                }
        else
                {
@@ -1231,6 +1342,28 @@ gboolean layout_image_get_desaturate(LayoutWindow *lw)
        return image_get_desaturate(lw->image);
 }
 
+void layout_image_set_overunderexposed(LayoutWindow *lw, gboolean overunderexposed)
+{
+       if (!layout_valid(&lw)) return;
+
+       image_set_overunderexposed(lw->image, overunderexposed);
+}
+
+gboolean layout_image_get_overunderexposed(LayoutWindow *lw)
+{
+       if (!layout_valid(&lw)) return FALSE;
+
+       return image_get_overunderexposed(lw->image);
+}
+
+void layout_image_set_ignore_alpha(LayoutWindow *lw, gboolean ignore_alpha)
+{
+   if (!layout_valid(&lw)) return;
+
+   lw->options.ignore_alpha = ignore_alpha;
+   image_set_ignore_alpha(lw->image, ignore_alpha);
+}
+
 /* stereo */
 /*
 gint layout_image_stereo_get(LayoutWindow *lw)
@@ -1323,6 +1456,11 @@ void layout_image_set_fd(LayoutWindow *lw, FileData *fd)
        layout_image_slideshow_continue_check(lw);
        layout_bars_new_image(lw);
        layout_image_animate_new_file(lw);
+
+       if (fd)
+               {
+               image_chain_append_end(fd->path);
+               }
 }
 
 void layout_image_set_with_ahead(LayoutWindow *lw, FileData *fd, FileData *read_ahead_fd)
@@ -1514,7 +1652,12 @@ void layout_image_next(LayoutWindow *lw)
                        if (y->next)
                                layout_image_set_index(lw, GPOINTER_TO_INT(y->next->data));
                        else
-                               layout_image_set_index(lw, GPOINTER_TO_INT(x->data));
+                               {
+                               if (options->circular_selection_lists)
+                                       {
+                                       layout_image_set_index(lw, GPOINTER_TO_INT(x->data));
+                                       }
+                               }
                        }
                while (x)
                        x = g_list_remove(x, x->data);
@@ -1587,7 +1730,12 @@ void layout_image_prev(LayoutWindow *lw)
                        if (y->prev)
                                layout_image_set_index(lw, GPOINTER_TO_INT(y->prev->data));
                        else
-                               layout_image_set_index(lw, GPOINTER_TO_INT(last->data));
+                               {
+                               if (options->circular_selection_lists)
+                                       {
+                                       layout_image_set_index(lw, GPOINTER_TO_INT(last->data));
+                                       }
+                               }
                        }
                while (x)
                        x = g_list_remove(x, x->data);
@@ -1722,12 +1870,27 @@ static void layout_image_button_cb(ImageWindow *imd, GdkEventButton *event, gpoi
 {
        LayoutWindow *lw = data;
        GtkWidget *menu;
-       FileData *dir_fd;
+       LayoutWindow *lw_new;
+       gchar *dest_dir;
 
        switch (event->button)
                {
                case MOUSE_BUTTON_LEFT:
-                       if (options->image_l_click_video && options->image_l_click_video_editor && imd->image_fd && imd->image_fd->format_class == FORMAT_CLASS_VIDEO)
+                       if (options->image_l_click_archive && imd-> image_fd && imd->image_fd->format_class == FORMAT_CLASS_ARCHIVE)
+                               {
+                               dest_dir = open_archive(imd->image_fd);
+                               if (dest_dir)
+                                       {
+                                       lw_new = layout_new_from_default();
+                                       layout_set_path(lw_new, dest_dir);
+                                       g_free(dest_dir);
+                                       }
+                               else
+                                       {
+                                       warning_dialog(_("Cannot open archive file"), _("See the Log Window"), GTK_STOCK_DIALOG_WARNING, NULL);
+                                       }
+                               }
+                       else if (options->image_l_click_video && options->image_l_click_video_editor && imd-> image_fd && imd->image_fd->format_class == FORMAT_CLASS_VIDEO)
                                {
                                start_editor_from_file(options->image_l_click_video_editor, imd->image_fd);
                                }
@@ -1746,16 +1909,6 @@ static void layout_image_button_cb(ImageWindow *imd, GdkEventButton *event, gpoi
                                }
                        gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 3, event->time);
                        break;
-               case MOUSE_BUTTON_BACK:
-                       dir_fd = file_data_new_dir(history_chain_back());
-                       layout_set_fd(lw, dir_fd);
-                       file_data_unref(dir_fd);
-                       break;
-               case MOUSE_BUTTON_FORWARD:
-                       dir_fd = file_data_new_dir(history_chain_forward());
-                       layout_set_fd(lw, dir_fd);
-                       file_data_unref(dir_fd);
-                       break;
                default:
                        break;
                }
@@ -2058,8 +2211,8 @@ void layout_image_activate(LayoutWindow *lw, gint i, gboolean force)
 
        image_attach_window(lw->image, lw->window, NULL, GQ_APPNAME, FALSE);
 
-       /* do not hilight selected image in SPLIT_NONE */
-       /* maybe the image should be selected always and hilight should be controled by
+       /* do not highlight selected image in SPLIT_NONE */
+       /* maybe the image should be selected always and highlight should be controlled by
           another image option */
        if (lw->split_mode != SPLIT_NONE)
                image_select(lw->split_images[i], TRUE);
@@ -2182,9 +2335,15 @@ GtkWidget *layout_image_setup_split_hv(LayoutWindow *lw, gboolean horizontal)
 
        /* horizontal split means vpaned and vice versa */
        if (horizontal)
+               {
                paned = gtk_vpaned_new();
+               DEBUG_NAME(paned);
+               }
        else
+               {
                paned = gtk_hpaned_new();
+               DEBUG_NAME(paned);
+               }
 
        gtk_paned_pack1(GTK_PANED(paned), lw->split_images[0]->widget, TRUE, TRUE);
        gtk_paned_pack2(GTK_PANED(paned), lw->split_images[1]->widget, TRUE, TRUE);
@@ -2210,8 +2369,11 @@ GtkWidget *layout_image_setup_split_quad(LayoutWindow *lw)
        layout_image_setup_split_common(lw, 4);
 
        hpaned = gtk_hpaned_new();
+       DEBUG_NAME(hpaned);
        vpaned1 = gtk_vpaned_new();
+       DEBUG_NAME(vpaned1);
        vpaned2 = gtk_vpaned_new();
+       DEBUG_NAME(vpaned2);
 
        gtk_paned_pack1(GTK_PANED(vpaned1), lw->split_images[0]->widget, TRUE, TRUE);
        gtk_paned_pack2(GTK_PANED(vpaned1), lw->split_images[2]->widget, TRUE, TRUE);