2007-01-25 [wwp] 2.7.1cvs58
[claws.git] / src / mimeview.c
index 2be0e497b399ab6e03847409456d506517c5aa50..f44a9d4866b0a43e09e6946cc98b472a6446d7c7 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
+ * Copyright (C) 1999-2007 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
@@ -85,7 +85,7 @@ static void mimeview_show_message_part                (MimeView       *mimeview,
                                                 MimeInfo       *partinfo);
 static void mimeview_change_view_type          (MimeView       *mimeview,
                                                 MimeViewType    type);
-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,
@@ -157,10 +157,11 @@ static gboolean part_button_pressed       (MimeView       *mimeview,
 static void icon_scroll_size_allocate_cb(GtkWidget     *widget, 
                                         GtkAllocation  *layout_size, 
                                         MimeView       *mimeview);
+static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview);
 
 static void mimeview_launch_cb(MimeView *mimeview)
 {
-       mimeview_launch(mimeview, NULL);
+       mimeview_launch(mimeview, mimeview_get_part_to_use(mimeview));
 }
 static GtkItemFactoryEntry mimeview_popup_entries[] =
 {
@@ -282,7 +283,6 @@ MimeView *mimeview_create(MainWindow *mainwin)
        popupmenu = menu_create_items(mimeview_popup_entries, n_entries,
                                      "<MimeView>", &popupfactory, mimeview);
        tooltips = gtk_tooltips_new();
-       gtk_tooltips_set_delay(tooltips, 0); 
 
        vbox = gtk_vbox_new(FALSE, 0);
        gtk_widget_show(vbox);
@@ -339,6 +339,17 @@ void mimeview_init(MimeView *mimeview)
                GTK_WIDGET_PTR(mimeview->textview));
 }
 
+static gboolean any_part_is_signed(MimeInfo *mimeinfo)
+{
+       while (mimeinfo) {
+               if (privacy_mimeinfo_is_signed(mimeinfo))
+                       return TRUE;
+               mimeinfo = procmime_mimeinfo_next(mimeinfo);
+       }
+
+       return FALSE;
+}
+
 void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
                           const gchar *file)
 {
@@ -357,7 +368,13 @@ void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
        g_signal_handlers_block_by_func(G_OBJECT(ctree), mimeview_selected,
                                        mimeview);
 
+       /* check if the mail's signed - it can change the mail structure */
+       
+       if (any_part_is_signed(mimeinfo))
+               debug_print("signed mail\n");
+
        mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
+       icon_list_clear(mimeview);
        icon_list_create(mimeview, mimeinfo);
 
        g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
@@ -427,6 +444,28 @@ MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
                (GTK_CTREE(mimeview->ctree), mimeview->opened);
 }
 
+gint mimeview_get_selected_part_num(MimeView *mimeview)
+{
+       gint i = 0;
+       GList *rows = GTK_CLIST(mimeview->ctree)->row_list;
+       while (rows) {
+               if (mimeview->opened == GTK_CTREE_NODE(rows))
+                       return i;
+               i++;
+               rows = rows->next;
+       }
+       return -1;
+}
+
+void mimeview_select_part_num(MimeView *mimeview, gint i)
+{
+       if (i < 0)
+               return;
+       gtk_clist_unselect_all(GTK_CLIST(mimeview->ctree));
+       gtk_clist_select_row(GTK_CLIST(mimeview->ctree), i, -1);
+       gtkut_clist_set_focus_row(GTK_CLIST(mimeview->ctree), i);
+}
+
 static void mimeview_set_multipart_tree(MimeView *mimeview,
                                        MimeInfo *mimeinfo,
                                        GtkCTreeNode *parent)
@@ -510,8 +549,6 @@ static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
        const gchar *fname;
 
        if (!partinfo) return;
-       if (partinfo == mimeview->opened)
-               return;
 
        fname = mimeview->file;
        if (!fname) return;
@@ -640,7 +677,8 @@ static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
        GtkWidget *focused = NULL;
        
        if (mainwindow_get_mainwindow())
-               focused = gtkut_get_focused_child(mainwindow_get_mainwindow()->window);
+               focused = gtkut_get_focused_child(
+                               GTK_CONTAINER(mainwindow_get_mainwindow()->window));
 
        if ((mimeview->type != MIMEVIEW_VIEWER) && 
            (mimeview->type == type)) return;
@@ -1293,6 +1331,7 @@ static void mimeview_drag_data_get(GtkWidget          *widget,
 {
        gchar *filename = NULL, *uriname, *tmp;
        MimeInfo *partinfo;
+       gint err;
 
        if (!mimeview->opened) return;
        if (!mimeview->file) return;
@@ -1344,9 +1383,11 @@ static void mimeview_drag_data_get(GtkWidget         *widget,
 
        g_free(tmp);
 
-       if (procmime_get_part(filename, partinfo) < 0)
+       if ((err = procmime_get_part(filename, partinfo)) < 0)
                alertpanel_error
-                       (_("Can't save the part of multipart message."));
+                       (_("Couldn't save the part of multipart message: %s"), 
+                               strerror(-err));
+
        uriname = g_strconcat("file://", filename, "\r\n", NULL);
 
        gtk_selection_data_set(selection_data, selection_data->target, 8,
@@ -1362,7 +1403,7 @@ static void mimeview_drag_data_get(GtkWidget          *widget,
  * \param basedir The target directory
  * \param number Used for dummy filename if attachment is unnamed
  */
-gchar *mimeview_get_filename_for_part(MimeInfo *partinfo,
+static gchar *mimeview_get_filename_for_part(MimeInfo *partinfo,
                                      const gchar *basedir,
                                      gint number)
 {
@@ -1400,7 +1441,8 @@ static gboolean mimeview_write_part(const gchar *filename,
                                    MimeInfo *partinfo)
 {
        gchar *dir;
-       
+       gint err;
+
        dir= g_path_get_dirname(filename);
        if (!is_dir_exist(dir))
                make_dir_hier(dir);
@@ -1425,9 +1467,10 @@ static gboolean mimeview_write_part(const gchar *filename,
                if (G_ALERTALTERNATE != aval) return FALSE;
        }
 
-       if (procmime_get_part(filename, partinfo) < 0) {
+       if ((err = procmime_get_part(filename, partinfo)) < 0) {
                alertpanel_error
-                       (_("Can't save the part of multipart message."));
+                       (_("Couldn't save the part of multipart message: %s"), 
+                               strerror(-err));
                return FALSE;
        }
 
@@ -1450,9 +1493,11 @@ static void mimeview_save_all(MimeView *mimeview)
        if (!mimeview->mimeinfo) return;
 
        partinfo = mimeview->mimeinfo;
-       if (prefs_common.attach_save_dir)
+       if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
                startdir = g_strconcat(prefs_common.attach_save_dir,
                                       G_DIR_SEPARATOR_S, NULL);
+       else
+               startdir = g_strdup(get_home_dir());
 
        dirname = filesel_select_file_save_folder(_("Select destination folder"), startdir);
        if (!dirname) {
@@ -1495,7 +1540,8 @@ static void mimeview_save_all(MimeView *mimeview)
 
        g_free(prefs_common.attach_save_dir);
        g_free(startdir);
-       prefs_common.attach_save_dir = g_strdup(dirname);
+       prefs_common.attach_save_dir = g_filename_to_utf8(dirname,
+                                       -1, NULL, NULL, NULL);
 }
 
 static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview)
@@ -1505,13 +1551,13 @@ static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview)
                partinfo = mimeview->spec_part;
                mimeview->spec_part = NULL;
        } else {
-               partinfo = mimeview_get_selected_part(mimeview);
+               partinfo = (MimeInfo *) g_object_get_data
+                        (G_OBJECT(mimeview->popupmenu),
+                        "pop_partinfo");
+               g_object_set_data(G_OBJECT(mimeview->popupmenu),
+                                 "pop_partinfo", NULL);
                if (!partinfo) { 
-                       partinfo = (MimeInfo *) g_object_get_data
-                                (G_OBJECT(mimeview->popupmenu),
-                                "pop_partinfo");
-                       g_object_set_data(G_OBJECT(mimeview->popupmenu),
-                                         "pop_partinfo", NULL);
+                       partinfo = mimeview_get_selected_part(mimeview);
                }                        
        }
 
@@ -1568,7 +1614,7 @@ static void mimeview_save_as(MimeView *mimeview)
        filedir = g_path_get_dirname(filename);
        if (filedir && strcmp(filedir, ".")) {
                g_free(prefs_common.attach_save_dir);
-               prefs_common.attach_save_dir = g_strdup(filedir);
+               prefs_common.attach_save_dir = g_filename_to_utf8(filedir, -1, NULL, NULL, NULL);
        }
 
        g_free(filedir);
@@ -1582,7 +1628,7 @@ static void mimeview_display_as_text(MimeView *mimeview)
        if (!mimeview->opened) return;
 
        partinfo = mimeview_get_part_to_use(mimeview);
-
+       mimeview_select_mimepart_icon(mimeview, partinfo);
        g_return_if_fail(partinfo != NULL);
        mimeview_show_message_part(mimeview, partinfo);
 }
@@ -1590,6 +1636,7 @@ static void mimeview_display_as_text(MimeView *mimeview)
 static void mimeview_launch(MimeView *mimeview, MimeInfo *partinfo)
 {
        gchar *filename;
+       gint err;
 
        if (!mimeview->opened) return;
        if (!mimeview->file) return;
@@ -1601,9 +1648,10 @@ static void mimeview_launch(MimeView *mimeview, MimeInfo *partinfo)
 
        filename = procmime_get_tmp_file_name(partinfo);
 
-       if (procmime_get_part(filename, partinfo) < 0)
+       if ((err = procmime_get_part(filename, partinfo)) < 0)
                alertpanel_error
-                       (_("Can't save the part of multipart message."));
+                       (_("Couldn't save the part of multipart message: %s"), 
+                               strerror(-err));
        else
                mimeview_view_file(filename, partinfo, NULL, mimeview);
 
@@ -1629,14 +1677,15 @@ static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo, gboo
        gchar *cmd;
        gchar *mime_command = NULL;
        gchar *content_type = NULL;
-
+       gint err;
        g_return_if_fail(partinfo != NULL);
 
        filename = procmime_get_tmp_file_name(partinfo);
 
-       if (procmime_get_part(filename, partinfo) < 0) {
+       if ((err = procmime_get_part(filename, partinfo)) < 0) {
                alertpanel_error
-                       (_("Can't save the part of multipart message."));
+                       (_("Couldn't save the part of multipart message: %s"), 
+                               strerror(-err));
                g_free(filename);
                return;
        }
@@ -1775,14 +1824,11 @@ static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeV
        num      = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "icon_number"));
        partinfo = g_object_get_data(G_OBJECT(button), "partinfo");
 
-       if (event->button != 2) {
+       if (event->button == 1) {
                icon_selected(mimeview, num, partinfo);
                gtk_widget_grab_focus(button);
                if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
                        toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
-                       if (event->button == 3)
-                               gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button),
-                                                            TRUE);
                }
        }
        part_button_pressed(mimeview, event, partinfo);
@@ -1794,6 +1840,7 @@ static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
 {
        GtkCTreeNode *node;
        node = gtk_ctree_find_by_row_data(GTK_CTREE(mimeview->ctree), NULL, partinfo);
+
        if (node)
                gtk_ctree_select(GTK_CTREE(mimeview->ctree), node);
 }              
@@ -1946,7 +1993,7 @@ static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo)
                          GINT_TO_POINTER(mimeview->icon_count));
        g_object_set_data(G_OBJECT(button), "partinfo", 
                          mimeinfo);
-       
+
        switch (mimeinfo->type) {
                
        case MIMETYPE_TEXT:
@@ -2069,7 +2116,7 @@ static void icon_list_clear (MimeView *mimeview)
 {
        GList     *child;
        GtkAdjustment *adj;
-       
+               
        child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
        for (; child != NULL; child = g_list_next(child)) {
                gtkut_container_remove(GTK_CONTAINER(mimeview->icon_vbox), 
@@ -2237,7 +2284,10 @@ void mimeview_handle_cmd(MimeView *mimeview, const gchar *cmd, GdkEventButton *e
        if (!mainwin)
                return;
                
-       else if (!strcmp(cmd, "sc://view_log"))
+       g_object_set_data(G_OBJECT(mimeview->popupmenu),
+                         "pop_partinfo", NULL);
+
+       if (!strcmp(cmd, "sc://view_log"))
                log_window_show(mainwin->logwin);
        else if (!strcmp(cmd, "sc://save_as"))
                mimeview_save_as(mimeview);