fix fat-fingered crash when v (hiding msgview) is pressed just before c (check signature)
[claws.git] / src / mimeview.c
index 8c328d02496bccdc95f05fdee180d470d93f07ba..f91ea146f41a821af23e3ac6b07c6d59333ac3cd 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2015 Hiroyuki Yamamoto and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -14,7 +14,6 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * 
  */
 
 #ifdef HAVE_CONFIG_H
 
 #include "defs.h"
 
+#ifdef G_OS_WIN32
+#define UNICODE
+#define _UNICODE
+#endif
+
 #include <glib.h>
 #include <glib/gi18n.h>
 #include <gdk/gdkkeysyms.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#ifndef HAVE_APACHE_FNMATCH
-/* kludge: apache's fnmatch clashes with <regex.h>, don't include
- * fnmatch.h */
-#include <fnmatch.h>
-#endif
-
 #include "main.h"
 #include "mimeview.h"
 #include "textview.h"
 #include "timing.h"
 #include "manage_window.h"
 #include "privacy.h"
-
-#ifdef MAEMO
-#include <libosso.h>
-#ifndef CHINOOK
-#include <osso-mime.h>
-#else
-#include <hildon-mime.h>
-#endif
-#endif
+#include "file-utils.h"
 
 typedef enum
 {
@@ -73,9 +63,10 @@ typedef enum
        COL_SIZE     = 1,
        COL_NAME     = 2,
        COL_DATA     = 3,
+       N_MIMEVIEW_COLUMNS
 } MimeViewColumnPos;
 
-#define N_MIMEVIEW_COLS        4
+#define N_MIMEVIEW_COLS        (N_MIMEVIEW_COLUMNS)
 
 static void mimeview_set_multipart_tree                (MimeView       *mimeview,
                                                 MimeInfo       *mimeinfo,
@@ -84,11 +75,12 @@ static void mimeview_show_message_part              (MimeView       *mimeview,
                                                 MimeInfo       *partinfo);
 static void mimeview_change_view_type          (MimeView       *mimeview,
                                                 MimeViewType    type);
-static gchar *mimeview_get_filename_for_part           (MimeInfo       *partinfo,
+static gchar *mimeview_get_filename_for_part   (MimeInfo       *partinfo,
                                                 const gchar    *basedir,
                                                 gint            number);
 static gboolean mimeview_write_part            (const gchar    *filename,
-                                                MimeInfo       *partinfo);
+                                                MimeInfo       *partinfo,
+                                                gboolean        handle_error);
 
 static void mimeview_selected          (GtkTreeSelection       *selection,
                                         MimeView       *mimeview);
@@ -166,6 +158,33 @@ static void mimeview_open_with_cb(GtkAction *action, gpointer data)
 }
 #endif
 
+static void mimeview_copy_cb(GtkAction *action, gpointer data)
+{
+       MimeView *mimeview = (MimeView *)data;
+       MimeInfo *mimeinfo = mimeview_get_part_to_use(mimeview);
+
+       if (mimeinfo == NULL)
+               return;
+
+       if (mimeinfo->type == MIMETYPE_IMAGE) {
+               GError *error = NULL;
+               GdkPixbuf *pixbuf = procmime_get_part_as_pixbuf(mimeinfo, &error);
+               if (error != NULL) {
+                       g_warning("could not copy: %s", error->message);
+                       g_error_free(error);
+               } else {
+                       gtk_clipboard_set_image(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
+                                               pixbuf);
+               }
+               g_object_unref(pixbuf);
+       } else {
+               void *data = procmime_get_part_as_string(mimeinfo, FALSE);
+               gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
+                                      data, mimeinfo->length);
+               g_free(data);
+       }
+}
+
 static void mimeview_send_to_cb(GtkAction *action, gpointer data)
 {
        MimeView *mimeview = (MimeView *)data;  
@@ -198,11 +217,12 @@ static void mimeview_select_prev_part_cb(GtkAction *action, gpointer data)
 }
 
 static GtkActionEntry mimeview_menu_actions[] = {
-       { "MimeView", NULL, "MimeView" },
+       { "MimeView", NULL, "MimeView", NULL, NULL, NULL },
        { "MimeView/Open", NULL, N_("_Open"), NULL, "Open MIME part", G_CALLBACK(mimeview_launch_cb) },
-#if (!defined MAEMO && !defined G_OS_WIN32)
+#if (!defined G_OS_WIN32)
        { "MimeView/OpenWith", NULL, N_("Open _with..."), NULL, "Open MIME part with...", G_CALLBACK(mimeview_open_with_cb) },
 #endif
+       { "MimeView/Copy", NULL, N_("Copy"), NULL, "Copy", G_CALLBACK(mimeview_copy_cb) },
        { "MimeView/SendTo", NULL, N_("Send to..."), NULL, "Send to", G_CALLBACK(mimeview_send_to_cb) },
        { "MimeView/DisplayAsText", NULL, N_("_Display as text"), NULL, "Display as text", G_CALLBACK(mimeview_display_as_text_cb) },
        { "MimeView/SaveAs", NULL, N_("_Save as..."), NULL, "Save as", G_CALLBACK(mimeview_save_as_cb) },
@@ -296,6 +316,7 @@ MimeView *mimeview_create(MainWindow *mainwin)
                                   G_TYPE_POINTER);
 
        ctree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
+       g_object_unref(model);
        gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ctree), FALSE);
        gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(ctree),
                                        prefs_common.use_stripes_everywhere);
@@ -309,9 +330,7 @@ MimeView *mimeview_create(MainWindow *mainwin)
        column = gtk_tree_view_get_column(GTK_TREE_VIEW(ctree), cols-1);
                                                           
        renderer = gtk_cell_renderer_text_new();
-#if(GTK_CHECK_VERSION(2,18,0))
        gtk_cell_renderer_set_alignment(renderer, 1, 0.5);
-#endif
        cols = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(ctree),
                                        -1, titles[COL_SIZE], renderer,
                                        "text", COL_SIZE, NULL);
@@ -344,7 +363,7 @@ MimeView *mimeview_create(MainWindow *mainwin)
 
        mime_notebook = gtk_notebook_new();
         gtk_widget_show(mime_notebook);
-        gtkut_widget_set_can_focus(mime_notebook, FALSE);
+        gtk_widget_set_can_focus(mime_notebook, FALSE);
         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(mime_notebook), FALSE);
         gtk_notebook_set_show_border(GTK_NOTEBOOK(mime_notebook), FALSE);
        
@@ -404,12 +423,15 @@ MimeView *mimeview_create(MainWindow *mainwin)
        MENUITEM_ADDUI_MANAGER(mimeview->ui_manager, 
                        "/Menus/MimeView/", "Open", "MimeView/Open",
                        GTK_UI_MANAGER_MENUITEM);
-#if (!defined MAEMO && !defined G_OS_WIN32)
+#if (!defined G_OS_WIN32)
        MENUITEM_ADDUI_MANAGER(mimeview->ui_manager, 
                        "/Menus/MimeView/", "OpenWith", "MimeView/OpenWith",
                        GTK_UI_MANAGER_MENUITEM);
 #endif
        MENUITEM_ADDUI_MANAGER(mimeview->ui_manager, 
+                       "/Menus/MimeView/", "Copy", "MimeView/Copy",
+                       GTK_UI_MANAGER_MENUITEM);
+       MENUITEM_ADDUI_MANAGER(mimeview->ui_manager,
                        "/Menus/MimeView/", "SendTo", "MimeView/SendTo",
                        GTK_UI_MANAGER_MENUITEM);
        MENUITEM_ADDUI_MANAGER(mimeview->ui_manager, 
@@ -467,9 +489,6 @@ MimeView *mimeview_create(MainWindow *mainwin)
        mimeview->icon_mainbox  = icon_mainbox;
        mimeview->icon_count    = 0;
        mimeview->mainwin       = mainwin;
-#if !(GTK_CHECK_VERSION(2,12,0))
-       mimeview->tooltips      = tips;
-#endif
        mimeview->mime_toggle   = mime_toggle;
        mimeview->siginfoview   = siginfoview;
        mimeview->scrollbutton  = scrollbutton;
@@ -543,7 +562,7 @@ static void mimeview_free_mimeinfo(MimeView *mimeview)
                mimeview->check_data->free_after_use = TRUE;
 #endif
        if (mimeview->mimeinfo != NULL && !defer) {
-               procmime_mimeinfo_free_all(mimeview->mimeinfo);
+               procmime_mimeinfo_free_all(&mimeview->mimeinfo);
                mimeview->mimeinfo = NULL;
        } else if (defer) {
 #ifdef USE_PTHREAD
@@ -582,16 +601,9 @@ void mimeview_destroy(MimeView *mimeview)
 
 MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
 {
-       GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(mimeview->ctree));
-       GtkTreeIter iter;
-       GtkTreeSelection *selection;
-       MimeInfo *partinfo = NULL;
-       
-       selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(mimeview->ctree));
-       if (gtk_tree_selection_get_selected(selection, NULL, &iter))
-               gtk_tree_model_get(model, &iter, COL_DATA, &partinfo, -1);
-
-       return partinfo;
+       return gtkut_tree_view_get_selected_pointer(
+                       GTK_TREE_VIEW(mimeview->ctree), COL_DATA,
+                       NULL, NULL, NULL);
 }
 
 MimeInfo *mimeview_get_node_part(MimeView *mimeview, GtkTreePath *path)
@@ -683,7 +695,8 @@ gint mimeview_get_selected_part_num(MimeView *mimeview)
        GtkTreePath *path;
        gint i = 0;
 
-       gtk_tree_model_get_iter_first(model, &iter);
+       if (!gtk_tree_model_get_iter_first(model, &iter))
+               return -1;
        path = gtk_tree_model_get_path(model, &iter);
        
        do {
@@ -711,7 +724,8 @@ void mimeview_select_part_num(MimeView *mimeview, gint i)
        if (i < 0)
                return;
        
-       gtk_tree_model_get_iter_first(model, &iter);
+       if (!gtk_tree_model_get_iter_first(model, &iter))
+               return;
        path = gtk_tree_model_get_path(model, &iter);
        
        while (x != i) {
@@ -811,21 +825,21 @@ static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
        fname = mimeview->file;
        if (!fname) return;
 
-       if ((fp = g_fopen(fname, "rb")) == NULL) {
-               FILE_OP_ERROR(fname, "fopen");
+       if ((fp = claws_fopen(fname, "rb")) == NULL) {
+               FILE_OP_ERROR(fname, "claws_fopen");
                return;
        }
 
        if (fseek(fp, partinfo->offset, SEEK_SET) < 0) {
                FILE_OP_ERROR(mimeview->file, "fseek");
-               fclose(fp);
+               claws_fclose(fp);
                return;
        }
 
        mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
        textview_show_part(mimeview->textview, partinfo, fp);
 
-       fclose(fp);
+       claws_fclose(fp);
 }
 
 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
@@ -833,28 +847,23 @@ static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *
        GSList *cur;
        MimeViewerFactory *factory = NULL;
        MimeViewer *viewer = NULL;
-       gchar *real_contenttype = NULL;
+       gchar *real_contenttype = NULL, *tmp;
 
-/*
- * FNM_CASEFOLD is a GNU extension
- */
-#ifndef FNM_CASEFOLD
-#define FNM_CASEFOLD 0
        real_contenttype = g_utf8_strdown((gchar *)content_type, -1);
-#else
-       real_contenttype = g_strdup(content_type);
-#endif
        
        for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
                MimeViewerFactory *curfactory = cur->data;
                gint i = 0;
 
                while (curfactory->content_types[i] != NULL) {
-                       if(!fnmatch(curfactory->content_types[i], real_contenttype, FNM_CASEFOLD)) {
+                       tmp = g_utf8_strdown(curfactory->content_types[i], -1);
+                       if (g_pattern_match_simple(tmp, real_contenttype)) {
                                debug_print("%s\n", curfactory->content_types[i]);
                                factory = curfactory;
+                               g_free(tmp);
                                break;
                        }
+                       g_free(tmp);
                        i++;
                }
                if (factory != NULL)
@@ -1103,6 +1112,9 @@ static void update_signature_noticeview(MimeView *mimeview, MimeInfo *mimeinfo,
                (gpointer) mimeview);
        noticeview_set_icon(mimeview->siginfoview, icon);
        noticeview_set_tooltip(mimeview->siginfoview, button_text);
+
+       icon_list_clear(mimeview);
+       icon_list_create(mimeview, mimeview->mimeinfo);
 }
 
 #ifdef USE_PTHREAD
@@ -1125,13 +1137,14 @@ static void mimeview_check_data_reset(MimeView *mimeview)
                pthread_cancel(mimeview->check_data->cancel_th);
        }
 
-       g_free(mimeview->check_data);
-       mimeview->check_data = NULL;
-
        if (must_free) {
                debug_print("freeing deferred mimeinfo\n");
-               procmime_mimeinfo_free_all(mimeview->check_data->siginfo);
+               procmime_mimeinfo_free_all(&mimeview->check_data->siginfo);
        }
+
+       g_free(mimeview->check_data);
+       mimeview->check_data = NULL;
+
        if (must_destroy) {
                debug_print("freeing deferred mimeview\n");
                mimeview_free_mimeinfo(mimeview);
@@ -1140,7 +1153,6 @@ static void mimeview_check_data_reset(MimeView *mimeview)
                g_free(mimeview);
                mimeviews = g_slist_remove(mimeviews, mimeview);
        }
-
 }
 
 /* GUI update once the checker thread is done or killed */
@@ -1153,18 +1165,18 @@ static gboolean mimeview_check_sig_thread_cb(void *data)
        
        if (mimeinfo == NULL) {
                /* message changed !? */
-               g_warning("no more siginfo!\n");
+               g_warning("no more siginfo!");
                goto end;
        }
        
        if (!mimeview->check_data) {
-               g_warning("nothing to check\n");
+               g_warning("nothing to check");
                return FALSE;
        }
 
        if (mimeview->check_data->siginfo != mimeinfo) {
                /* message changed !? */
-               g_warning("different siginfo!\n");
+               g_warning("different siginfo!");
                goto end;
        }
 
@@ -1181,9 +1193,7 @@ static gboolean mimeview_check_sig_thread_cb(void *data)
        else
                update_signature_noticeview(mimeview, mimeview->siginfo, 
                        FALSE, 0);
-       icon_list_clear(mimeview);
-       icon_list_create(mimeview, mimeview->mimeinfo);
-       
+
 end:
        mimeview_check_data_reset(mimeview);
        return FALSE;
@@ -1209,7 +1219,7 @@ static void *mimeview_check_sig_worker_thread(void *data)
        } else {
                /* that's strange! we changed message without 
                 * getting killed. */
-               g_warning("different siginfo!\n");
+               g_warning("different siginfo!");
                mimeview_check_data_reset(mimeview);
                return NULL;
        }
@@ -1297,7 +1307,7 @@ static void mimeview_check_sig_in_thread(MimeView *mimeview)
                        mimeview_check_sig_worker_thread, 
                        mimeview) != 0) {
                /* arh. We'll do it synchronously. */
-               g_warning("can't create thread");
+               g_warning("can't create checked thread");
                g_free(mimeview->check_data);
                mimeview->check_data = NULL;
                return;
@@ -1307,12 +1317,17 @@ static void mimeview_check_sig_in_thread(MimeView *mimeview)
        }
 
        /* create the killer thread */
-       pthread_create(&th2, &detach2, 
+       if (pthread_create(&th2, &detach2, 
                        mimeview_check_sig_cancel_thread, 
-                       mimeview);
-
-       mimeview->check_data->cancel_th = th2;
-       mimeview->check_data->cancel_th_init = TRUE;
+                       mimeview) != 0) {
+               g_warning("can't create killer thread");
+               g_free(mimeview->check_data);
+               mimeview->check_data = NULL;
+               return;
+       } else {
+               mimeview->check_data->cancel_th = th2;
+               mimeview->check_data->cancel_th_init = TRUE;
+       }
 }
 #endif
 
@@ -1321,7 +1336,7 @@ static void check_signature_cb(GtkWidget *widget, gpointer user_data)
        MimeView *mimeview = (MimeView *) user_data;
        MimeInfo *mimeinfo = mimeview->siginfo;
        
-       if (mimeinfo == NULL)
+       if (mimeinfo == NULL || !noticeview_is_visible(mimeview->siginfoview))
                return;
 #ifdef USE_PTHREAD
        if (mimeview->check_data)
@@ -1338,8 +1353,6 @@ static void check_signature_cb(GtkWidget *widget, gpointer user_data)
                debug_print("checking without thread\n");
                privacy_mimeinfo_check_signature(mimeinfo);
                update_signature_noticeview(mimeview, mimeview->siginfo, FALSE, 0);
-               icon_list_clear(mimeview);
-               icon_list_create(mimeview, mimeview->mimeinfo);
        }
 }
 
@@ -1417,6 +1430,14 @@ static void update_signature_info(MimeView *mimeview, MimeInfo *selected)
        noticeview_show(mimeview->siginfoview);
 }
 
+void mimeview_show_part_as_text(MimeView *mimeview, MimeInfo *partinfo)
+{
+       cm_return_if_fail(mimeview != NULL);
+       cm_return_if_fail(partinfo != NULL);
+
+       mimeview_show_message_part(mimeview, partinfo);
+}
+
 static void mimeview_selected(GtkTreeSelection *selection, MimeView *mimeview)
 {
        GtkTreeView *ctree = GTK_TREE_VIEW(mimeview->ctree);
@@ -1424,6 +1445,7 @@ static void mimeview_selected(GtkTreeSelection *selection, MimeView *mimeview)
        GtkTreeIter iter;
        GtkTreePath *path;
        MimeInfo *partinfo;
+       MainWindow *mainwin;
 
        selection = gtk_tree_view_get_selection(ctree);
        if (!gtk_tree_selection_get_selected(selection, &model, &iter))
@@ -1471,6 +1493,14 @@ static void mimeview_selected(GtkTreeSelection *selection, MimeView *mimeview)
                        break;
                }
        }
+       mainwin = mainwindow_get_mainwindow();
+       if (mainwin)
+               main_window_set_menu_sensitive(mainwin);
+
+       if (mimeview->siginfo && privacy_auto_check_signatures(mimeview->siginfo)
+       &&  privacy_mimeinfo_get_sig_status(mimeview->siginfo) == SIGNATURE_UNCHECKED) {
+               mimeview_check_signature(mimeview);
+       }
 }
 
 static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
@@ -1506,27 +1536,36 @@ static gboolean mimeview_scrolled(GtkWidget *widget, GdkEventScroll *event,
        return TRUE;
 }
 
-/* from gdkevents.c */
-#define DOUBLE_CLICK_TIME 250
-
 static gboolean part_button_pressed(MimeView *mimeview, GdkEventButton *event, 
                                    MimeInfo *partinfo)
 {
        static MimeInfo *lastinfo;
        static guint32 lasttime;
 
+       gint double_click_time;
+       g_object_get(gtk_settings_get_default(), "gtk-double-click-time", &double_click_time, NULL);
+
        if (event->button == 2 ||
-           (event->button == 1 && (event->time - lasttime) < DOUBLE_CLICK_TIME && lastinfo == partinfo)) {
+           (event->button == 1 && (event->time - lasttime) < double_click_time && lastinfo == partinfo)) {
                /* call external program for image, audio or html */
                mimeview_launch(mimeview, partinfo);
                return TRUE;
        } else if (event->button == 3) {
+               MainWindow *mainwin = mainwindow_get_mainwindow();
+
                if (partinfo && (partinfo->type == MIMETYPE_MESSAGE ||
                                 partinfo->type == MIMETYPE_IMAGE ||
                                 partinfo->type == MIMETYPE_MULTIPART))
                        cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", FALSE);
                else
                        cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/DisplayAsText", TRUE);
+
+               if (partinfo && (partinfo->type == MIMETYPE_MESSAGE ||
+                                partinfo->type == MIMETYPE_IMAGE ||
+                                partinfo->type == MIMETYPE_TEXT))
+                       cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Copy", TRUE);
+               else
+                       cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Copy", FALSE);
 #ifndef G_OS_WIN32
                if (partinfo &&
                    partinfo->type == MIMETYPE_APPLICATION &&
@@ -1535,7 +1574,8 @@ static gboolean part_button_pressed(MimeView *mimeview, GdkEventButton *event,
                else
 #endif
                        cm_menu_set_sensitive_full(mimeview->ui_manager, "Menus/MimeView/Open", TRUE);
-
+               if (mainwin)
+                       main_window_set_menu_sensitive(mainwin);
                g_object_set_data(G_OBJECT(mimeview->popupmenu),
                                  "pop_partinfo", partinfo);
                                    
@@ -1566,6 +1606,8 @@ void mimeview_select_next_part(MimeView *mimeview)
        MimeInfo *partinfo = NULL;
        gboolean has_next;
        
+       if (!mimeview->opened) return;
+
        gtk_tree_model_get_iter(model, &iter, mimeview->opened);
        path = gtk_tree_model_get_path(model, &iter);
 skip:
@@ -1599,6 +1641,8 @@ void mimeview_select_prev_part(MimeView *mimeview)
        MimeInfo *partinfo = NULL;
        gboolean has_prev;
        
+       if (!mimeview->opened) return;
+
        gtk_tree_model_get_iter(model, &iter, mimeview->opened);
        path = gtk_tree_model_get_path(model, &iter);
 skip:
@@ -1701,22 +1745,24 @@ static void mimeview_drag_data_get(GtkWidget        *widget,
                GPtrArray *headers = NULL;
                FILE *fp;
 
-               fp = g_fopen(partinfo->data.filename, "rb");
-               fseek(fp, partinfo->offset, SEEK_SET);
-               headers = procheader_get_header_array_asis(fp);
-               if (headers) {
-                       gint i;
-                       for (i = 0; i < headers->len; i++) {
-                               Header *header = g_ptr_array_index(headers, i);
-                               if (procheader_headername_equal(header->name, "Subject")) {
-                                       unfold_line(header->body);
-                                       name = g_strconcat(header->body, ".txt", NULL);
-                                       subst_for_filename(name);
+               fp = claws_fopen(partinfo->data.filename, "rb");
+               if (fp != NULL && fseek(fp, partinfo->offset, SEEK_SET) == 0) {
+                       headers = procheader_get_header_array_asis(fp);
+                       if (headers) {
+                               gint i;
+                               for (i = 0; i < headers->len; i++) {
+                                       Header *header = g_ptr_array_index(headers, i);
+                                       if (procheader_headername_equal(header->name, "Subject")) {
+                                               unfold_line(header->body);
+                                               name = g_strconcat(header->body, ".txt", NULL);
+                                               subst_for_filename(name);
+                                       }
                                }
+                               procheader_header_array_destroy(headers);
                        }
-                       procheader_header_array_destroy(headers);
                }
-               fclose(fp);
+               if (fp != NULL)
+                       claws_fclose(fp);
                if (name)
                        filename = g_path_get_basename(name);
                g_free(name);
@@ -1767,7 +1813,7 @@ check_new_file:
        if ((err = procmime_get_part(filename, partinfo)) < 0)
                alertpanel_error
                        (_("Couldn't save the part of multipart message: %s"), 
-                               strerror(-err));
+                               g_strerror(-err));
 
        tmp = g_filename_to_uri(filename, NULL, NULL);
        uriname = g_strconcat(tmp, "\r\n", NULL);
@@ -1824,7 +1870,8 @@ static gchar *mimeview_get_filename_for_part(MimeInfo *partinfo,
  * \param partinfo Attachment to save
  */
 static gboolean mimeview_write_part(const gchar *filename,
-                                   MimeInfo *partinfo)
+                                   MimeInfo *partinfo,
+                                   gboolean handle_error)
 {
        gchar *dir;
        gint err;
@@ -1847,22 +1894,64 @@ static gboolean mimeview_write_part(const gchar *filename,
                res = g_strdup_printf(_("Overwrite existing file '%s'?"),
                                      tmp);
                g_free(tmp);
-               aval = alertpanel(_("Overwrite"), res, GTK_STOCK_CANCEL, 
-                                 GTK_STOCK_OK, NULL);
-               g_free(res);                                      
+               aval = alertpanel(_("Overwrite"), res, GTK_STOCK_CANCEL,
+                                 GTK_STOCK_OK, NULL, ALERTFOCUS_FIRST);
+               g_free(res);
                if (G_ALERTALTERNATE != aval) return FALSE;
        }
 
        if ((err = procmime_get_part(filename, partinfo)) < 0) {
-               alertpanel_error
-                       (_("Couldn't save the part of multipart message: %s"), 
-                               strerror(-err));
+               debug_print("error saving MIME part: %d\n", err);
+               if (handle_error)
+                       alertpanel_error
+                               (_("Couldn't save the part of multipart message: %s"),
+                                g_strerror(-err));
                return FALSE;
        }
 
        return TRUE;
 }
 
+static AlertValue mimeview_save_all_error_ask(gint n)
+{
+       gchar *message = g_strdup_printf(
+               _("An error has occurred while saving message part #%d. "
+               "Do you want to cancel operation or skip error and "
+               "continue?"), n);
+       AlertValue av = alertpanel_full(_("Error saving all message parts"),
+               message, GTK_STOCK_CANCEL, _("Skip"), _("Skip all"),
+               ALERTFOCUS_FIRST, FALSE, NULL, ALERT_WARNING);
+       g_free(message);
+       return av;
+}
+
+static void mimeview_save_all_info(gint errors, gint total)
+{
+       if (!errors) {
+               gchar *msg = g_strdup_printf(
+                               ngettext("%d file saved successfully.",
+                                       "%d files saved successfully.",
+                                       total),
+                               total);
+               alertpanel_notice("%s", msg);
+               g_free(msg);
+       } else {
+               gchar *msg1 = g_strdup_printf(
+                               ngettext("%d file saved successfully",
+                                       "%d files saved successfully",
+                                       total - errors),
+                               total - errors);
+               gchar *msg2 = g_strdup_printf(
+                               ngettext("%s, %d file failed.",
+                                       "%s, %d files failed.",
+                                       errors),
+                               msg1, errors);
+               alertpanel_warning("%s", msg2);
+               g_free(msg2);
+               g_free(msg1);
+       }
+}
+
 /**
  * Menu callback: Save all attached files
  * \param mimeview Current display
@@ -1872,7 +1961,8 @@ static void mimeview_save_all(MimeView *mimeview)
        MimeInfo *partinfo;
        gchar *dirname;
        gchar *startdir = NULL;
-       gint number = 1;
+       gint number = 1, errors = 0;
+       gboolean skip_errors = FALSE;
 
        if (!mimeview->opened) return;
        if (!mimeview->file) return;
@@ -1880,8 +1970,7 @@ static void mimeview_save_all(MimeView *mimeview)
 
        partinfo = mimeview->mimeinfo;
        if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
-               startdir = g_strconcat(prefs_common.attach_save_dir,
-                                      G_DIR_SEPARATOR_S, NULL);
+               startdir = g_strconcat(prefs_common.attach_save_dir, G_DIR_SEPARATOR_S, NULL);
        else
                startdir = g_strdup(get_home_dir());
 
@@ -1893,8 +1982,7 @@ static void mimeview_save_all(MimeView *mimeview)
        }
 
        if (!is_dir_exist (dirname)) {
-               alertpanel_error(_("'%s' is not a directory."),
-                                dirname);
+               alertpanel_error(_("'%s' is not a directory."), dirname);
                g_free(startdir);
                g_free(dirname);
                return;
@@ -1911,17 +1999,26 @@ static void mimeview_save_all(MimeView *mimeview)
                if (partinfo && partinfo->type == MIMETYPE_TEXT)
                        partinfo = procmime_mimeinfo_next(partinfo);
        }
-               
+
        while (partinfo != NULL) {
                if (partinfo->type != MIMETYPE_MESSAGE &&
                    partinfo->type != MIMETYPE_MULTIPART &&
                    (partinfo->disposition != DISPOSITIONTYPE_INLINE
                     || get_real_part_name(partinfo) != NULL)) {
-                       gchar *filename = mimeview_get_filename_for_part
-                               (partinfo, dirname, number++);
+                       gchar *filename = mimeview_get_filename_for_part(
+                               partinfo, dirname, number++);
 
-                       mimeview_write_part(filename, partinfo);
+                       gboolean ok = mimeview_write_part(filename, partinfo, FALSE);
                        g_free(filename);
+                       if (!ok) {
+                               ++errors;
+                               if (!skip_errors) {
+                                       AlertValue av = mimeview_save_all_error_ask(number - 1);
+                                       skip_errors = (av == G_ALERTOTHER);
+                                       if (av == G_ALERTDEFAULT) /* cancel */
+                                               break;
+                               }
+                       }
                }
                partinfo = procmime_mimeinfo_next(partinfo);
        }
@@ -1931,6 +2028,8 @@ static void mimeview_save_all(MimeView *mimeview)
        prefs_common.attach_save_dir = g_filename_to_utf8(dirname,
                                        -1, NULL, NULL, NULL);
        g_free(dirname);
+
+       mimeview_save_all_info(errors, number - 1);
 }
 
 static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview)
@@ -2006,7 +2105,7 @@ void mimeview_save_as(MimeView *mimeview)
                return;
        }
 
-       mimeview_write_part(filename, partinfo);
+       mimeview_write_part(filename, partinfo, TRUE);
 
        filedir = g_path_get_dirname(filename);
        if (filedir && strcmp(filedir, ".")) {
@@ -2048,7 +2147,7 @@ void mimeview_launch(MimeView *mimeview, MimeInfo *partinfo)
        if ((err = procmime_get_part(filename, partinfo)) < 0)
                alertpanel_error
                        (_("Couldn't save the part of multipart message: %s"), 
-                               strerror(-err));
+                               g_strerror(-err));
        else
                mimeview_view_file(filename, partinfo, NULL, mimeview);
 
@@ -2069,9 +2168,6 @@ void mimeview_open_with(MimeView *mimeview)
        mimeview_open_part_with(mimeview, partinfo, FALSE);
 }
 
-#ifdef MAEMO
-osso_context_t *get_osso_context(void);
-#endif
 static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo, gboolean automatic)
 {
        gchar *filename;
@@ -2079,11 +2175,7 @@ static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo, gboo
        gchar *mime_command = NULL;
        gchar *content_type = NULL;
        gint err;
-#ifdef MAEMO
-       DBusConnection *dbusconn;
-       gchar *uri;
-       int r = 0;
-#endif
+
        cm_return_if_fail(partinfo != NULL);
 
        filename = procmime_get_tmp_file_name(partinfo);
@@ -2091,7 +2183,7 @@ static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo, gboo
        if ((err = procmime_get_part(filename, partinfo)) < 0) {
                alertpanel_error
                        (_("Couldn't save the part of multipart message: %s"), 
-                               strerror(-err));
+                               g_strerror(-err));
                g_free(filename);
                return;
        }
@@ -2110,23 +2202,6 @@ static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo, gboo
                        partinfo->subtype);
        }
        
-#ifdef MAEMO
-       if (content_type != NULL) {
-               uri = g_filename_to_uri(filename, NULL, NULL);
-               dbusconn = osso_get_dbus_connection (get_osso_context());
-#ifdef CHINOOK
-               r = hildon_mime_open_file_with_mime_type (dbusconn, uri, content_type);
-#else
-               r = osso_mime_open_file_with_mime_type (dbusconn, uri, content_type);
-#endif
-               g_free(uri);
-       }
-       if (r != 1) {
-               alertpanel_error(_("No registered viewer for this file type."));
-       }
-       goto out;
-#endif
-       
        if ((partinfo->type == MIMETYPE_TEXT && !strcmp(partinfo->subtype, "html"))
        && prefs_common_get_uri_cmd() && prefs_common.uri_cmd[0]) {
                mime_command = g_strdup(prefs_common_get_uri_cmd());
@@ -2180,9 +2255,7 @@ static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo, gboo
                prefs_common.mime_open_cmd_history =
                        add_history(prefs_common.mime_open_cmd_history, cmd);
        }
-#ifdef MAEMO
-out:
-#endif
+
        g_free(content_type);
        g_free(filename);
 }
@@ -2222,7 +2295,7 @@ static void mimeview_send_to(MimeView *mimeview, MimeInfo *partinfo)
        } else
                alertpanel_error
                        (_("Couldn't save the part of multipart message: %s"), 
-                               strerror(-err));
+                               g_strerror(-err));
        g_free(filename);
 }
 
@@ -2232,6 +2305,7 @@ static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
 #ifndef G_OS_WIN32
        gchar *p;
        gchar buf[BUFFSIZE];
+
        if (cmd == NULL)
                mimeview_open_part_with(mimeview, partinfo, TRUE);
        else {
@@ -2246,7 +2320,7 @@ static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
                        g_warning("MIME viewer command-line is invalid: '%s'", cmd);
                        mimeview_open_part_with(mimeview, partinfo, FALSE);
                }
-               if (execute_command_line(buf, TRUE) != 0) {
+               if (execute_command_line(buf, TRUE, NULL) != 0) {
                        if (!prefs_common.save_parts_readwrite)
                                g_chmod(filename, S_IRUSR|S_IWUSR);
                        mimeview_open_part_with(mimeview, partinfo, FALSE);
@@ -2254,20 +2328,34 @@ static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
        }
 #else
        SHFILEINFO file_info;
-       if ((SHGetFileInfo(filename, 0, &file_info, sizeof(SHFILEINFO), SHGFI_EXETYPE)) != 0) {
+       GError *error = NULL;
+       gunichar2 *fn16 = g_utf8_to_utf16(filename, -1, NULL, NULL, &error);
+
+       if (error != NULL) {
+               alertpanel_error(_("Could not convert attachment name to UTF-16:\n\n%s"),
+                                       error->message);
+               debug_print("filename '%s' conversion to UTF-16 failed\n", filename);
+               g_error_free(error);
+               return;
+       }
+
+       if ((SHGetFileInfo((LPCWSTR)fn16, 0, &file_info, sizeof(SHFILEINFO), SHGFI_EXETYPE)) != 0) {
                AlertValue val = alertpanel_full(_("Execute untrusted binary?"), 
                                      _("This attachment is an executable file. Executing "
-                                       "untrusted binaries is dangerous and could probably "
-                                       "lead to compromission of your computer.\n\n"
+                                       "untrusted binaries is dangerous and could compromise "
+                                       "your computer.\n\n"
                                        "Do you want to run this file?"), GTK_STOCK_CANCEL, 
                                        _("Run binary"),
-                                     NULL, FALSE, NULL, ALERT_WARNING, G_ALERTDEFAULT);
+                                     NULL, ALERTFOCUS_FIRST, FALSE, NULL, ALERT_WARNING);
                if (val == G_ALERTALTERNATE) {
                        debug_print("executing binary\n");
-                       ShellExecute(NULL, "open", filename, NULL, NULL, SW_SHOW);
+                       ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
                }
-       } else
-               ShellExecute(NULL, "open", filename, NULL, NULL, SW_SHOW);
+       } else {
+               ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
+       }
+
+       g_free(fn16);
        
 #endif
 }
@@ -2330,7 +2418,8 @@ static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
        GtkTreePath *path;
        MimeInfo *curr = NULL;
        
-       gtk_tree_model_get_iter_first(model, &iter);
+       if (!gtk_tree_model_get_iter_first(model, &iter))
+               return;
        path = gtk_tree_model_get_path(model, &iter);
        
        do {
@@ -2443,9 +2532,6 @@ static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo)
 #ifdef GENERIC_UMPC
        GtkRequisition r;
 #endif
-#if !(GTK_CHECK_VERSION(2,12,0))
-       GtkTooltips *tips = mimeview->tooltips;
-#endif
        
        if (!prefs_common.show_inline_attachments && mimeinfo->id)
                return;
@@ -2526,29 +2612,29 @@ static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo)
                case SIGNATURE_UNCHECKED:
                case SIGNATURE_CHECK_FAILED:
                case SIGNATURE_CHECK_TIMEOUT:
-                       pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
+                       pixmap = stock_pixmap_widget_with_overlay(stockp,
                            STOCK_PIXMAP_PRIVACY_EMBLEM_SIGNED, OVERLAY_BOTTOM_RIGHT, 6, 3);
                        break;
                case SIGNATURE_OK:
-                       pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
+                       pixmap = stock_pixmap_widget_with_overlay(stockp,
                            STOCK_PIXMAP_PRIVACY_EMBLEM_PASSED, OVERLAY_BOTTOM_RIGHT, 6, 3);
                        break;
                case SIGNATURE_WARN:
                case SIGNATURE_KEY_EXPIRED:
-                       pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
+                       pixmap = stock_pixmap_widget_with_overlay(stockp,
                            STOCK_PIXMAP_PRIVACY_EMBLEM_WARN, OVERLAY_BOTTOM_RIGHT, 6, 3);
                        break;
                case SIGNATURE_INVALID:
-                       pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
+                       pixmap = stock_pixmap_widget_with_overlay(stockp,
                            STOCK_PIXMAP_PRIVACY_EMBLEM_FAILED, OVERLAY_BOTTOM_RIGHT, 6, 3);
                        break;
                }
                sigshort = privacy_mimeinfo_sig_info_short(siginfo);
        } else if (encrypted != NULL) {
-                       pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp,
+                       pixmap = stock_pixmap_widget_with_overlay(stockp,
                            STOCK_PIXMAP_PRIVACY_EMBLEM_ENCRYPTED, OVERLAY_BOTTOM_RIGHT, 6, 3);         
        } else {
-               pixmap = stock_pixmap_widget_with_overlay(mimeview->mainwin->window, stockp, 0,
+               pixmap = stock_pixmap_widget_with_overlay(stockp, 0,
                                                          OVERLAY_NONE, 6, 3);
        }
        gtk_container_add(GTK_CONTAINER(button), pixmap);
@@ -2567,23 +2653,30 @@ static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo)
                          to_human_readable((goffset)mimeinfo->length), NULL);
        g_free(content_type);
        if (desc && *desc) {
-               gchar *tmp = NULL;
+               gchar *tmp = NULL, *escaped = NULL;
                if (!g_utf8_validate(desc, -1, NULL)) {
                        tmp = conv_filename_to_utf8(desc);
                } else {
                        tmp = g_strdup(desc);
                }
+               escaped = g_markup_escape_text(tmp,-1);
+               
                tiptmp = g_strconcat(tip, "\n<b>",
                                prefs_common.attach_desc && mimeinfo->description ?
                                _("Description:") : _("Filename:"),
-                               " </b>", g_markup_escape_text(tmp,-1), NULL);
+                               " </b>", escaped, NULL);
                g_free(tip);
                tip = tiptmp;
                g_free(tmp);
+               g_free(escaped);
        }
        if (sigshort && *sigshort) {
-               tiptmp = g_strjoin("\n", tip, g_markup_escape_text(sigshort, -1), NULL);
+               gchar *sigshort_escaped =
+                       g_markup_escape_text(sigshort, -1);
+
+               tiptmp = g_strjoin("\n", tip, sigshort_escaped, NULL);
                g_free(tip);
+               g_free(sigshort_escaped);
                tip = tiptmp;
        }
        g_free(sigshort);
@@ -2593,15 +2686,9 @@ static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo)
        gtk_widget_show_all(button);
        gtk_drag_source_set(button, GDK_BUTTON1_MASK|GDK_BUTTON3_MASK, 
                            mimeview_mime_types, 1, GDK_ACTION_COPY);
-#ifndef MAEMO
+
        g_signal_connect(G_OBJECT(button), "popup-menu",
                         G_CALLBACK(icon_popup_menu), mimeview);
-#else
-       gtk_widget_tap_and_hold_setup(GTK_WIDGET(button), NULL, NULL,
-                       GTK_TAP_AND_HOLD_NONE | GTK_TAP_AND_HOLD_NO_INTERNALS);
-       g_signal_connect(G_OBJECT(button), "tap-and-hold",
-                        G_CALLBACK(icon_popup_menu), mimeview);
-#endif
        g_signal_connect(G_OBJECT(button), "button_release_event", 
                         G_CALLBACK(icon_clicked_cb), mimeview);
        g_signal_connect(G_OBJECT(button), "key_press_event", 
@@ -2682,32 +2769,31 @@ static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
                gtk_widget_set_size_request(mimeview->icon_mainbox, 
                                            size.width, -1);
        }
+       if (mimeview->opened)
+               icon_list_toggle_by_mime_info(mimeview,
+                       mimeview_get_node_part(mimeview, mimeview->opened));
 }
 
 static void icon_list_toggle_by_mime_info (MimeView    *mimeview,
                                           MimeInfo     *mimeinfo)
 {
-       GList *child;
+       GList *children, *child;
        
-       child = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
-       for (; child != NULL; child = g_list_next(child)) {
+       children = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
+       for (child = children; child != NULL; child = g_list_next(child)) {
+               gboolean *highlight = NULL;
+               GtkWidget *icon = gtk_bin_get_child(GTK_BIN(child->data));
+
                if (!GTK_IS_EVENT_BOX(child->data))
                        continue;
-               if(g_object_get_data(G_OBJECT(child->data),
-                                     "partinfo") == (gpointer)mimeinfo) {
-                       gboolean *highlight = NULL;
-                       GtkWidget *icon = gtk_bin_get_child(GTK_BIN(child->data));
-                       highlight = g_object_get_data(G_OBJECT(icon), "highlight");
-                       *highlight = TRUE;
-                       gtk_widget_queue_draw(icon);
-               } else {
-                       gint *highlight = NULL;
-                       GtkWidget *icon = gtk_bin_get_child(GTK_BIN(child->data));
-                       highlight = g_object_get_data(G_OBJECT(icon), "highlight");
-                       *highlight = FALSE;
-                       gtk_widget_queue_draw(icon);
-               }                        
+
+               highlight = g_object_get_data(G_OBJECT(icon), "highlight");
+               *highlight = (g_object_get_data(G_OBJECT(child->data),
+                                     "partinfo") == (gpointer)mimeinfo);
+
+               gtk_widget_queue_draw(icon);
        }
+       g_list_free(children);
 }
 
 static void ctree_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation,
@@ -2757,7 +2843,8 @@ static gint mime_toggle_button_cb(GtkWidget *button, GdkEventButton *event,
        return TRUE;
 }
 
-void mimeview_update (MimeView *mimeview) {
+void mimeview_update (MimeView *mimeview)
+{
        if (mimeview && mimeview->mimeinfo) {
                icon_list_clear(mimeview);
                icon_list_create(mimeview, mimeview->mimeinfo);