+
+static void
+image_save_dialog_response_cb (GtkWidget *fc,
+ gint response_id,
+ EvWindow *ev_window)
+{
+ GFile *target_file;
+ gboolean is_native;
+ GError *error = NULL;
+ GdkPixbuf *pixbuf;
+ gchar *uri;
+ gchar **extensions;
+ gchar *filename;
+ gchar *file_format;
+ GdkPixbufFormat *format;
+ GtkFileFilter *filter;
+
+ if (response_id != GTK_RESPONSE_OK) {
+ gtk_widget_destroy (fc);
+ return;
+ }
+
+ uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fc));
+ filter = gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (fc));
+ format = g_object_get_data (G_OBJECT (filter), "pixbuf-format");
+
+ if (format == NULL) {
+ format = get_gdk_pixbuf_format_by_extension (uri);
+ }
+
+ if (format == NULL) {
+ ev_window_error_message (GTK_WINDOW (ev_window),
+ _("Couldn't find appropriate format to save image"),
+ NULL);
+ g_free (uri);
+ gtk_widget_destroy (fc);
+
+ return;
+ }
+
+ extensions = gdk_pixbuf_format_get_extensions (format);
+ if (g_str_has_suffix(uri, extensions[0])) {
+ gchar *uri_extension;
+
+ uri_extension = g_strconcat (uri, ".", extensions[0], NULL);
+ target_file = g_file_new_for_uri (uri_extension);
+ g_free (uri_extension);
+ } else {
+ target_file = g_file_new_for_uri (uri);
+ }
+ g_strfreev(extensions);
+ g_free (uri);
+
+ is_native = g_file_is_native (target_file);
+ if (is_native) {
+ filename = g_file_get_path (target_file);
+ } else {
+ filename = ev_tmp_filename ("saveimage");
+ }
+
+ ev_document_doc_mutex_lock ();
+ pixbuf = ev_document_images_get_image (EV_DOCUMENT_IMAGES (ev_window->priv->document),
+ ev_window->priv->image);
+ ev_document_doc_mutex_unlock ();
+
+ file_format = gdk_pixbuf_format_get_name (format);
+ gdk_pixbuf_save (pixbuf, filename, file_format, &error, NULL);
+ g_free (file_format);
+ g_object_unref (pixbuf);
+
+ if (error) {
+ ev_window_error_message (GTK_WINDOW (ev_window),
+ _("The image could not be saved."),
+ error);
+ g_error_free (error);
+ g_free (filename);
+ g_object_unref (target_file);
+ gtk_widget_destroy (fc);
+
+ return;
+ }
+
+ if (!is_native) {
+ GFile *source_file;
+
+ source_file = g_file_new_for_uri (filename);
+
+ ev_window_save_remote (ev_window, source_file, target_file);
+ g_object_unref (source_file);
+ }
+
+ g_free (filename);
+ g_object_unref (target_file);
+ gtk_widget_destroy (fc);
+}
+
+static void
+ev_view_popup_cmd_save_image_as (GtkAction *action, EvWindow *window)
+{
+ GtkWidget *fc;
+
+ if (!window->priv->image)
+ return;
+
+ fc = gtk_file_chooser_dialog_new (_("Save Image"),
+ GTK_WINDOW (window),
+ GTK_FILE_CHOOSER_ACTION_SAVE,
+ GTK_STOCK_CANCEL,
+ GTK_RESPONSE_CANCEL,
+ GTK_STOCK_SAVE, GTK_RESPONSE_OK,
+ NULL);
+
+ gtk_dialog_set_default_response (GTK_DIALOG (fc), GTK_RESPONSE_OK);
+ gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (fc), FALSE);
+ gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (fc), TRUE);
+
+ file_chooser_dialog_add_writable_pixbuf_formats (GTK_FILE_CHOOSER (fc));
+
+ g_signal_connect (fc, "response",
+ G_CALLBACK (image_save_dialog_response_cb),
+ window);
+
+ gtk_widget_show (fc);
+}
+
+static void
+ev_view_popup_cmd_copy_image (GtkAction *action, EvWindow *window)
+{
+ GtkClipboard *clipboard;
+ GdkPixbuf *pixbuf;
+
+ if (!window->priv->image)
+ return;
+
+ clipboard = gtk_widget_get_clipboard (GTK_WIDGET (window),
+ GDK_SELECTION_CLIPBOARD);
+ ev_document_doc_mutex_lock ();
+ pixbuf = ev_document_images_get_image (EV_DOCUMENT_IMAGES (window->priv->document),
+ window->priv->image);
+ ev_document_doc_mutex_unlock ();
+
+ gtk_clipboard_set_image (clipboard, pixbuf);
+ g_object_unref (pixbuf);
+}
+
+static void
+ev_attachment_popup_cmd_open_attachment (GtkAction *action, EvWindow *window)
+{
+ GList *l;
+
+ if (!window->priv->attach_list)
+ return;
+
+ for (l = window->priv->attach_list; l && l->data; l = g_list_next (l)) {
+ EvAttachment *attachment;
+ GError *error = NULL;
+
+ attachment = (EvAttachment *) l->data;
+
+ ev_attachment_open (attachment, &error);
+
+ if (error) {
+ ev_window_error_message (GTK_WINDOW (window),
+ _("Unable to open attachment"),
+ error);
+ g_error_free (error);
+ }
+ }
+}
+
+static void
+attachment_save_dialog_response_cb (GtkWidget *fc,
+ gint response_id,
+ EvWindow *ev_window)
+{
+ GFile *target_file;
+ gchar *uri;
+ GList *l;
+ GtkFileChooserAction fc_action;
+ gboolean is_dir;
+ gboolean is_native;
+
+ if (response_id != GTK_RESPONSE_OK) {
+ gtk_widget_destroy (fc);
+ return;
+ }
+
+ uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fc));
+ target_file = g_file_new_for_uri (uri);
+ g_object_get (G_OBJECT (fc), "action", &fc_action, NULL);
+ is_dir = (fc_action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
+ is_native = g_file_is_native (target_file);
+
+ for (l = ev_window->priv->attach_list; l && l->data; l = g_list_next (l)) {
+ EvAttachment *attachment;
+ GFile *save_to;
+ GError *error = NULL;
+
+ attachment = (EvAttachment *) l->data;
+
+ if (is_native) {
+ if (is_dir) {
+ save_to = g_file_get_child (target_file,
+ ev_attachment_get_name (attachment));
+ } else {
+ save_to = g_object_ref (target_file);
+ }
+ } else {
+ save_to = ev_tmp_file_get ("saveattachment");
+ }
+
+ ev_attachment_save (attachment, save_to, &error);
+
+ if (error) {
+ ev_window_error_message (GTK_WINDOW (ev_window),
+ _("The attachment could not be saved."),
+ error);
+ g_error_free (error);
+ g_object_unref (save_to);
+
+ continue;
+ }
+
+ if (!is_native) {
+ GFile *dest_file;
+
+ if (is_dir) {
+ dest_file = g_file_get_child (target_file,
+ ev_attachment_get_name (attachment));
+ } else {
+ dest_file = g_object_ref (target_file);
+ }
+
+ ev_window_save_remote (ev_window, save_to, dest_file);
+
+ g_object_unref (dest_file);
+ }
+
+ g_object_unref (save_to);
+ }
+
+ g_free (uri);
+ g_object_unref (target_file);
+
+ gtk_widget_destroy (fc);
+}
+
+static void
+ev_attachment_popup_cmd_save_attachment_as (GtkAction *action, EvWindow *window)
+{
+ GtkWidget *fc;
+ EvAttachment *attachment = NULL;
+
+ if (!window->priv->attach_list)
+ return;
+
+ if (g_list_length (window->priv->attach_list) == 1)
+ attachment = (EvAttachment *) window->priv->attach_list->data;
+
+ fc = gtk_file_chooser_dialog_new (
+ _("Save Attachment"),
+ GTK_WINDOW (window),
+ attachment ? GTK_FILE_CHOOSER_ACTION_SAVE : GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
+ GTK_STOCK_CANCEL,
+ GTK_RESPONSE_CANCEL,
+ GTK_STOCK_SAVE, GTK_RESPONSE_OK,
+ NULL);
+
+ gtk_dialog_set_default_response (GTK_DIALOG (fc), GTK_RESPONSE_OK);
+ gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (fc), TRUE);
+ gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (fc), FALSE);
+
+ if (attachment)
+ gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (fc),
+ ev_attachment_get_name (attachment));
+
+ g_signal_connect (fc, "response",
+ G_CALLBACK (attachment_save_dialog_response_cb),
+ window);
+
+ gtk_widget_show (fc);
+}
+