fix engrish
[claws.git] / src / mimeview.c
index 69e590691caf2e74e6093305a5ce756aa6e66a8a..b7068eaf1c1e6a5e04b5d0391e125c4858c9eb84 100644 (file)
@@ -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
@@ -56,6 +55,7 @@
 #include "timing.h"
 #include "manage_window.h"
 #include "privacy.h"
+#include "file-utils.h"
 
 typedef enum
 {
@@ -74,11 +74,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);
@@ -188,7 +189,7 @@ 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 G_OS_WIN32)
        { "MimeView/OpenWith", NULL, N_("Open _with..."), NULL, "Open MIME part with...", G_CALLBACK(mimeview_open_with_cb) },
@@ -286,6 +287,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);
@@ -299,9 +301,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);
@@ -334,7 +334,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);
        
@@ -569,16 +569,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)
@@ -670,7 +663,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 {
@@ -698,7 +692,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) {
@@ -798,21 +793,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)
@@ -1405,6 +1400,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))
@@ -1452,6 +1448,9 @@ 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) {
@@ -1507,6 +1506,8 @@ static gboolean part_button_pressed(MimeView *mimeview, GdkEventButton *event,
                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))
@@ -1521,7 +1522,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);
                                    
@@ -1691,7 +1693,7 @@ static void mimeview_drag_data_get(GtkWidget          *widget,
                GPtrArray *headers = NULL;
                FILE *fp;
 
-               fp = g_fopen(partinfo->data.filename, "rb");
+               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) {
@@ -1708,7 +1710,7 @@ static void mimeview_drag_data_get(GtkWidget          *widget,
                        }
                }
                if (fp != NULL)
-                       fclose(fp);
+                       claws_fclose(fp);
                if (name)
                        filename = g_path_get_basename(name);
                g_free(name);
@@ -1816,7 +1818,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;
@@ -1839,22 +1842,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"), 
-                               g_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
@@ -1864,7 +1909,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;
@@ -1872,8 +1918,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());
 
@@ -1885,8 +1930,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;
@@ -1903,17 +1947,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);
        }
@@ -1923,6 +1976,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)
@@ -1998,7 +2053,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, ".")) {
@@ -2213,7 +2268,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);
@@ -2235,8 +2290,8 @@ static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
        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);
@@ -2311,7 +2366,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 {
@@ -2504,29 +2560,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);