2006-09-05 [paul] 2.4.0cvs149
[claws.git] / src / mimeview.c
index 641a7cb64d6259c80ae900d6e95afe7a9662280f..2be0e497b399ab6e03847409456d506517c5aa50 100644 (file)
@@ -63,7 +63,8 @@
 #include "procheader.h"
 #include "stock_pixmap.h"
 #include "gtk/gtkvscrollbutton.h"
-
+#include "gtk/logwindow.h"
+#include "timing.h"
 
 typedef enum
 {
@@ -112,14 +113,21 @@ static void mimeview_drag_data_get      (GtkWidget          *widget,
                                         guint             time,
                                         MimeView         *mimeview);
 
+static gboolean mimeview_scrolled      (GtkWidget      *widget,
+                                        GdkEventScroll *event,
+                                        MimeView       *mimeview);
 static void mimeview_display_as_text   (MimeView       *mimeview);
 static void mimeview_save_as           (MimeView       *mimeview);
 static void mimeview_save_all          (MimeView       *mimeview);
-static void mimeview_launch            (MimeView       *mimeview);
+static void mimeview_launch            (MimeView       *mimeview,
+                                        MimeInfo       *partinfo);
 static void mimeview_open_with         (MimeView       *mimeview);
+static void mimeview_open_part_with    (MimeView       *mimeview,
+                                        MimeInfo       *partinfo,
+                                        gboolean        automatic);
 static void mimeview_view_file         (const gchar    *filename,
                                         MimeInfo       *partinfo,
-                                        const gchar    *cmdline,
+                                        const gchar    *cmd,
                                         MimeView       *mimeview);
 static gboolean icon_clicked_cb                (GtkWidget      *button, 
                                         GdkEventButton *event, 
@@ -150,9 +158,13 @@ static void icon_scroll_size_allocate_cb(GtkWidget         *widget,
                                         GtkAllocation  *layout_size, 
                                         MimeView       *mimeview);
 
+static void mimeview_launch_cb(MimeView *mimeview)
+{
+       mimeview_launch(mimeview, NULL);
+}
 static GtkItemFactoryEntry mimeview_popup_entries[] =
 {
-       {N_("/_Open"),            NULL, mimeview_launch,          0, NULL},
+       {N_("/_Open"),            NULL, mimeview_launch_cb,       0, NULL},
        {N_("/Open _with..."),    NULL, mimeview_open_with,       0, NULL},
        {N_("/_Display as text"), NULL, mimeview_display_as_text, 0, NULL},
        {N_("/_Save as..."),      NULL, mimeview_save_as,         0, NULL},
@@ -243,6 +255,9 @@ MimeView *mimeview_create(MainWindow *mainwin)
        scrollbutton = gtk_vscrollbutton_new(gtk_layout_get_vadjustment(GTK_LAYOUT(icon_scroll)));
        gtk_widget_show(scrollbutton);
 
+       g_signal_connect(G_OBJECT(icon_scroll), "scroll_event",
+                        G_CALLBACK(mimeview_scrolled), mimeview);
+
        mime_toggle = gtk_toggle_button_new();
        gtk_widget_show(mime_toggle);
        arrow = gtk_arrow_new(GTK_ARROW_LEFT, GTK_SHADOW_NONE);
@@ -308,7 +323,7 @@ MimeView *mimeview_create(MainWindow *mainwin)
        mimeview->oldsize       = 60;
        mimeview->mime_toggle   = mime_toggle;
        mimeview->siginfoview   = siginfoview;
-
+       mimeview->scrollbutton  = scrollbutton;
        mimeview->target_list   = gtk_target_list_new(mimeview_mime_types, 1); 
        
        mimeviews = g_slist_prepend(mimeviews, mimeview);
@@ -429,13 +444,22 @@ static void mimeview_set_multipart_tree(MimeView *mimeview,
        }
 }
 
-static const gchar *get_part_name(MimeInfo *partinfo)
+static const gchar *get_real_part_name(MimeInfo *partinfo)
 {
-       const gchar *name;
+       const gchar *name = NULL;
 
        name = procmime_mimeinfo_get_parameter(partinfo, "filename");
        if (name == NULL)
                name = procmime_mimeinfo_get_parameter(partinfo, "name");
+
+       return name;
+}
+
+static const gchar *get_part_name(MimeInfo *partinfo)
+{
+       const gchar *name;
+
+       name = get_real_part_name(partinfo);
        if (name == NULL)
                name = "";
 
@@ -486,6 +510,8 @@ 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;
@@ -602,6 +628,7 @@ gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
                mimeview->mimeviewer = viewer;
                mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
        }
+       viewer->mimeview = mimeview;
        viewer->show_mimepart(viewer, mimeview->file, partinfo);
 
        return TRUE;
@@ -610,6 +637,10 @@ gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
 {
        TextView  *textview  = mimeview->textview;
+       GtkWidget *focused = NULL;
+       
+       if (mainwindow_get_mainwindow())
+               focused = gtkut_get_focused_child(mainwindow_get_mainwindow()->window);
 
        if ((mimeview->type != MIMEVIEW_VIEWER) && 
            (mimeview->type == type)) return;
@@ -628,7 +659,8 @@ static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
        default:
                return;
        }
-
+       if (focused)
+               gtk_widget_grab_focus(focused);
        mimeview->type = type;
 }
 
@@ -1023,6 +1055,7 @@ static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
        MimeInfo *partinfo;
        if (mimeview->opened == node) return;
        mimeview->opened = node;
+       mimeview->spec_part = NULL;
        gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
 
        partinfo = gtk_ctree_node_get_row_data(ctree, node);
@@ -1052,26 +1085,6 @@ static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
                        mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
                        textview_clear(mimeview->textview);
                        textview_show_mime_part(mimeview->textview, partinfo);
-                       
-#if 0 /* this sucks. I'll do better. */
-                       val = alertpanel_full(_("Unknown part type"), 
-                                             _("The type of this part is unknown. What would you like "
-                                               "to do with it?"),
-                                             GTK_STOCK_SAVE, GTK_STOCK_OPEN, _("Display as text"),
-                                             FALSE, NULL, ALERT_WARNING, G_ALERTALTERNATE);
-                       switch (val) {
-                               case G_ALERTDEFAULT:
-                                       mimeview_save_as(mimeview);
-                                       break;
-                               case G_ALERTALTERNATE:
-                                       mimeview_open_with(mimeview);
-                                       break;
-                               case G_ALERTOTHER:
-                                       mimeview_display_as_text(mimeview);
-                                       break;
-                               default:
-                       }
-#endif
                        break;
                }
        }
@@ -1114,6 +1127,19 @@ static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
        return FALSE;
 }
 
+static gboolean mimeview_scrolled(GtkWidget *widget, GdkEventScroll *event,
+                                   MimeView *mimeview)
+{
+       GtkVScrollbutton *scrollbutton = (GtkVScrollbutton *)mimeview->scrollbutton;
+       if (event->direction == GDK_SCROLL_UP) {
+               scrollbutton->scroll_type = GTK_SCROLL_STEP_BACKWARD;
+       } else {
+               scrollbutton->scroll_type = GTK_SCROLL_STEP_FORWARD;
+       }
+       gtk_vscrollbutton_scroll(scrollbutton);
+       return TRUE;
+}
+
 /* from gdkevents.c */
 #define DOUBLE_CLICK_TIME 250
 
@@ -1126,11 +1152,10 @@ static gboolean part_button_pressed(MimeView *mimeview, GdkEventButton *event,
        if (event->button == 2 ||
            (event->button == 1 && (event->time - lasttime) < DOUBLE_CLICK_TIME && lastinfo == partinfo)) {
                /* call external program for image, audio or html */
-               mimeview_launch(mimeview);
+               mimeview_launch(mimeview, partinfo);
                return TRUE;
        } else if (event->button == 3) {
-               if (partinfo && (partinfo->type == MIMETYPE_TEXT ||
-                                partinfo->type == MIMETYPE_MESSAGE ||
+               if (partinfo && (partinfo->type == MIMETYPE_MESSAGE ||
                                 partinfo->type == MIMETYPE_IMAGE ||
                                 partinfo->type == MIMETYPE_MULTIPART))
                        menu_set_sensitive(mimeview->popupfactory,
@@ -1191,7 +1216,7 @@ static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
                
        switch (event->keyval) {
        case GDK_space:
-               if (textview_scroll_page(mimeview->textview, FALSE))
+               if (mimeview_scroll_page(mimeview, FALSE))
                        return TRUE;
 
                node = GTK_CTREE_NODE_NEXT(mimeview->opened);
@@ -1202,10 +1227,10 @@ static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
                }
                break;
        case GDK_BackSpace:
-               textview_scroll_page(mimeview->textview, TRUE);
+               mimeview_scroll_page(mimeview, TRUE);
                return TRUE;
        case GDK_Return:
-               textview_scroll_one_line(mimeview->textview,
+               mimeview_scroll_one_line(mimeview,
                                         (event->state & GDK_MOD1_MASK) != 0);
                return TRUE;
        case GDK_n:
@@ -1237,7 +1262,7 @@ static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
        case GDK_l:
                BREAK_ON_MODIFIER_KEY();
                KEY_PRESS_EVENT_STOP();
-               mimeview_launch(mimeview);
+               mimeview_launch(mimeview, NULL);
                return TRUE;
        case GDK_o:
                BREAK_ON_MODIFIER_KEY();
@@ -1308,20 +1333,24 @@ static void mimeview_drag_data_get(GtkWidget        *widget,
                filename = g_path_get_basename("Unnamed part");
                
 
-
-       tmp = filename;
+       tmp = g_filename_from_utf8(filename, -1, NULL, NULL, NULL);
        
+       if (tmp == NULL) {
+               g_warning("filename not in UTF-8");
+               tmp = g_strdup("Unnamed part");
+       }
        filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
-                              filename, NULL);
+                              tmp, NULL);
 
        g_free(tmp);
 
        if (procmime_get_part(filename, partinfo) < 0)
                alertpanel_error
                        (_("Can't save the part of multipart message."));
-       uriname = g_strconcat("file://", filename, NULL);
+       uriname = g_strconcat("file://", filename, "\r\n", NULL);
+
        gtk_selection_data_set(selection_data, selection_data->target, 8,
-                              uriname, strlen(uriname));
+                              (guchar *)uriname, strlen(uriname));
 
        g_free(uriname);
        g_free(filename);
@@ -1427,14 +1456,14 @@ static void mimeview_save_all(MimeView *mimeview)
 
        dirname = filesel_select_file_save_folder(_("Select destination folder"), startdir);
        if (!dirname) {
-               if (startdir) g_free(startdir);
+               g_free(startdir);
                return;
        }
 
        if (!is_dir_exist (dirname)) {
                alertpanel_error(_("'%s' is not a directory."),
                                 dirname);
-               if (startdir) g_free(startdir);
+               g_free(startdir);
                return;
        }
 
@@ -1453,7 +1482,8 @@ static void mimeview_save_all(MimeView *mimeview)
        while (partinfo != NULL) {
                if (partinfo->type != MIMETYPE_MESSAGE &&
                    partinfo->type != MIMETYPE_MULTIPART &&
-                   partinfo->disposition != DISPOSITIONTYPE_INLINE) {
+                   (partinfo->disposition != DISPOSITIONTYPE_INLINE
+                    || get_real_part_name(partinfo) != NULL)) {
                        gchar *filename = mimeview_get_filename_for_part
                                (partinfo, dirname, number++);
 
@@ -1463,14 +1493,30 @@ static void mimeview_save_all(MimeView *mimeview)
                partinfo = procmime_mimeinfo_next(partinfo);
        }
 
-       if (prefs_common.attach_save_dir)
-               g_free(prefs_common.attach_save_dir);
-
+       g_free(prefs_common.attach_save_dir);
+       g_free(startdir);
        prefs_common.attach_save_dir = g_strdup(dirname);
-
-       if (startdir) g_free(startdir);
 }
 
+static MimeInfo *mimeview_get_part_to_use(MimeView *mimeview)
+{
+       MimeInfo *partinfo = NULL;
+       if (mimeview->spec_part) {
+               partinfo = mimeview->spec_part;
+               mimeview->spec_part = NULL;
+       } else {
+               partinfo = mimeview_get_selected_part(mimeview);
+               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);
+               }                        
+       }
+
+       return partinfo;
+}
 /**
  * Menu callback: Save the selected attachment
  * \param mimeview Current display
@@ -1486,14 +1532,8 @@ static void mimeview_save_as(MimeView *mimeview)
        if (!mimeview->opened) return;
        if (!mimeview->file) return;
 
-       partinfo = mimeview_get_selected_part(mimeview);
-       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_part_to_use(mimeview);
+
        g_return_if_fail(partinfo != NULL);
        
        if (get_part_name(partinfo) == NULL) {
@@ -1527,8 +1567,7 @@ static void mimeview_save_as(MimeView *mimeview)
 
        filedir = g_path_get_dirname(filename);
        if (filedir && strcmp(filedir, ".")) {
-               if (prefs_common.attach_save_dir)
-                       g_free(prefs_common.attach_save_dir);
+               g_free(prefs_common.attach_save_dir);
                prefs_common.attach_save_dir = g_strdup(filedir);
        }
 
@@ -1542,35 +1581,22 @@ static void mimeview_display_as_text(MimeView *mimeview)
 
        if (!mimeview->opened) return;
 
-       partinfo = mimeview_get_selected_part(mimeview);
-       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_part_to_use(mimeview);
+
        g_return_if_fail(partinfo != NULL);
        mimeview_show_message_part(mimeview, partinfo);
 }
 
-static void mimeview_launch(MimeView *mimeview)
+static void mimeview_launch(MimeView *mimeview, MimeInfo *partinfo)
 {
-       MimeInfo *partinfo;
        gchar *filename;
 
        if (!mimeview->opened) return;
        if (!mimeview->file) return;
 
-       partinfo = mimeview_get_selected_part(mimeview);
-       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);
-       }                        
+       if (!partinfo)
+               partinfo = mimeview_get_part_to_use(mimeview);
+
        g_return_if_fail(partinfo != NULL);
 
        filename = procmime_get_tmp_file_name(partinfo);
@@ -1587,23 +1613,23 @@ static void mimeview_launch(MimeView *mimeview)
 static void mimeview_open_with(MimeView *mimeview)
 {
        MimeInfo *partinfo;
-       gchar *filename;
-       gchar *cmd;
-       gchar *mime_command = NULL;
-       gchar *content_type = NULL;
 
        if (!mimeview) return;
        if (!mimeview->opened) return;
        if (!mimeview->file) return;
 
-       partinfo = mimeview_get_selected_part(mimeview);
-       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_part_to_use(mimeview);
+
+       mimeview_open_part_with(mimeview, partinfo, FALSE);
+}
+
+static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo, gboolean automatic)
+{
+       gchar *filename;
+       gchar *cmd;
+       gchar *mime_command = NULL;
+       gchar *content_type = NULL;
+
        g_return_if_fail(partinfo != NULL);
 
        filename = procmime_get_tmp_file_name(partinfo);
@@ -1628,16 +1654,55 @@ static void mimeview_open_with(MimeView *mimeview)
                content_type = procmime_get_content_type_str(partinfo->type,
                        partinfo->subtype);
        }
-       mime_command = mailcap_get_command_for_type(content_type);
-       g_free(content_type);
-       cmd = input_dialog_combo
-               (_("Open with"),
-                _("Enter the command line to open file:\n"
-                  "('%s' will be replaced with file name)"),
-                mime_command ? mime_command : prefs_common.mime_open_cmd,
-                prefs_common.mime_open_cmd_history,
-                TRUE);
-       g_free(mime_command);
+       
+       if ((partinfo->type == MIMETYPE_TEXT && !strcmp(partinfo->subtype, "html"))
+       && prefs_common.uri_cmd && prefs_common.uri_cmd[0]) {
+               mime_command = g_strdup(prefs_common.uri_cmd);
+               g_free(content_type);
+               content_type = NULL;
+       } else if (partinfo->type != MIMETYPE_TEXT || !prefs_common.ext_editor_cmd
+       ||  !prefs_common.ext_editor_cmd[0]) {
+               mime_command = mailcap_get_command_for_type(content_type, filename);
+       } else {
+               mime_command = g_strdup(prefs_common.ext_editor_cmd);
+               g_free(content_type);
+               content_type = NULL;
+       }
+       if (mime_command == NULL) {
+               /* try with extension this time */
+               g_free(content_type);
+               content_type = procmime_get_mime_type(filename);
+               mime_command = mailcap_get_command_for_type(content_type, filename);
+       }
+
+       if (mime_command == NULL)
+               automatic = FALSE;
+       
+       if (!automatic) {
+               gboolean remember = FALSE;
+               if (content_type != NULL)
+                       cmd = input_dialog_combo_remember
+                               (_("Open with"),
+                                _("Enter the command line to open file:\n"
+                                  "('%s' will be replaced with file name)"),
+                                mime_command ? mime_command : prefs_common.mime_open_cmd,
+                                prefs_common.mime_open_cmd_history,
+                                TRUE, &remember);
+               else
+                       cmd = input_dialog_combo
+                               (_("Open with"),
+                                _("Enter the command line to open file:\n"
+                                  "('%s' will be replaced with file name)"),
+                                mime_command ? mime_command : prefs_common.mime_open_cmd,
+                                prefs_common.mime_open_cmd_history,
+                                TRUE);
+               if (cmd && remember) {
+                       mailcap_update_default(content_type, cmd);
+               }
+               g_free(mime_command);
+       } else {
+               cmd = mime_command;
+       }
        if (cmd) {
                mimeview_view_file(filename, partinfo, cmd, mimeview);
                g_free(prefs_common.mime_open_cmd);
@@ -1645,66 +1710,28 @@ static void mimeview_open_with(MimeView *mimeview)
                prefs_common.mime_open_cmd_history =
                        add_history(prefs_common.mime_open_cmd_history, cmd);
        }
-
+       g_free(content_type);
        g_free(filename);
 }
 
 static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
-                              const gchar *cmdline, MimeView *mimeview)
-{
-       static gchar *default_image_cmdline = DEFAULT_IMAGE_VIEWER_CMD;
-       static gchar *default_audio_cmdline = DEFAULT_AUDIO_PLAYER_CMD;
-       static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
-       static gchar *mime_cmdline = DEFAULT_MIME_CMD;
-       gchar buf[1024];
-       gchar m_buf[1024];
-       const gchar *cmd;
-       const gchar *def_cmd;
-       const gchar *p;
-
-       if (cmdline) {
-               cmd = cmdline;
-               def_cmd = NULL;
-       } else if (MIMETYPE_APPLICATION == partinfo->type &&
-                  !g_ascii_strcasecmp(partinfo->subtype, "octet-stream")) {
-               mimeview_open_with(mimeview);
-               return;
-       } else if (MIMETYPE_IMAGE == partinfo->type) {
-               cmd = prefs_common.mime_image_viewer;
-               def_cmd = default_image_cmdline;
-       } else if (MIMETYPE_AUDIO == partinfo->type) {
-               cmd = prefs_common.mime_audio_player;
-               def_cmd = default_audio_cmdline;
-       } else if (MIMETYPE_TEXT == partinfo->type && !strcmp(partinfo->subtype, "html")) {
-               cmd = prefs_common.uri_cmd;
-               def_cmd = default_html_cmdline;
-       } else {
-               gchar *content_type;
-               
-               content_type = procmime_get_content_type_str(partinfo->type, partinfo->subtype);
-               g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
-                          content_type, "%s");
-               g_free(content_type);
-               cmd = m_buf;
-               def_cmd = NULL;
-       }
-
-       if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
-           !strchr(p + 2, '%'))
-               g_snprintf(buf, sizeof(buf), cmd, filename);
+                              const gchar *cmd, MimeView *mimeview)
+{
+       gchar *p;
+       gchar buf[BUFFSIZE];
+       if (cmd == NULL)
+               mimeview_open_part_with(mimeview, partinfo, TRUE);
        else {
-               if (cmd)
+               if ((p = strchr(cmd, '%')) && *(p + 1) == 's' &&
+                   !strchr(p + 2, '%'))
+                       g_snprintf(buf, sizeof(buf), cmd, filename);
+               else {
                        g_warning("MIME viewer command line is invalid: '%s'", cmd);
-               if (def_cmd)
-                       g_snprintf(buf, sizeof(buf), def_cmd, filename);
-               else {
-                       mimeview_open_with(mimeview);
-                       return;
-               }
+                       mimeview_open_part_with(mimeview, partinfo, FALSE);
+               }
+               if (execute_command_line(buf, TRUE) != 0)
+                       mimeview_open_part_with(mimeview, partinfo, FALSE);
        }
-
-       if (execute_command_line(buf, TRUE) != 0)
-               mimeview_open_with(mimeview);
 }
 
 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
@@ -1748,15 +1775,16 @@ 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");
 
-       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 == 2 || event->button == 3)
-                       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button),
-                                                    TRUE);
+       if (event->button != 2) {
+               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);
 
        return FALSE;
@@ -1770,6 +1798,12 @@ static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
                gtk_ctree_select(GTK_CTREE(mimeview->ctree), node);
 }              
 
+void mimeview_select_mimepart_icon(MimeView *mimeview, MimeInfo *partinfo)
+{
+       icon_list_toggle_by_mime_info(mimeview, partinfo);
+       icon_selected(mimeview, -1, partinfo);
+}
+
 #undef  KEY_PRESS_EVENT_STOP
 #define KEY_PRESS_EVENT_STOP() \
         g_signal_stop_emission_by_name(G_OBJECT(button), \
@@ -1795,7 +1829,7 @@ static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
                if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
                        /* stop the button being untoggled */
                        KEY_PRESS_EVENT_STOP();
-                       if (textview_scroll_page(textview, FALSE))
+                       if (mimeview_scroll_page(mimeview, FALSE))
                                return TRUE;
 
                        if (icon_list_select_by_number(mimeview, num + 1))
@@ -1808,12 +1842,12 @@ static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
 
                break;
        case GDK_BackSpace:
-               textview_scroll_page(textview, TRUE);
+               mimeview_scroll_page(mimeview, TRUE);
                return TRUE;
        case GDK_Return:
                if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
                        KEY_PRESS_EVENT_STOP();
-                       textview_scroll_one_line(textview,
+                       mimeview_scroll_one_line(mimeview,
                                                 (event->state & GDK_MOD1_MASK) != 0);
                        return TRUE;
                } else {
@@ -1853,7 +1887,7 @@ static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
        case GDK_l:
                BREAK_ON_MODIFIER_KEY();
                KEY_PRESS_EVENT_STOP();
-               mimeview_launch(mimeview);
+               mimeview_launch(mimeview, NULL);
                return TRUE;
        case GDK_o:
                BREAK_ON_MODIFIER_KEY();
@@ -1871,8 +1905,7 @@ static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
 
        if (!mimeview->messageview->mainwin) return FALSE;
        summaryview = mimeview->messageview->mainwin->summaryview;
-       summary_pass_key_press_event(summaryview, event);
-       return TRUE;
+       return summary_pass_key_press_event(summaryview, event);
 }
 
 static void toggle_icon(GtkToggleButton *button, MimeView *mimeview)
@@ -2041,7 +2074,9 @@ static void icon_list_clear (MimeView *mimeview)
        for (; child != NULL; child = g_list_next(child)) {
                gtkut_container_remove(GTK_CONTAINER(mimeview->icon_vbox), 
                                       GTK_WIDGET(child->data));
+               gtk_widget_destroy(GTK_WIDGET(child->data));
        }
+       g_list_free(child);
        mimeview->icon_count = 0;
        adj  = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
        gtk_adjustment_set_value(adj, adj->lower);
@@ -2111,7 +2146,7 @@ static void icon_scroll_size_allocate_cb(GtkWidget *widget,
        gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll), 
                            GTK_LAYOUT(mimeview->icon_scroll)->width, 
                            MAX(vbox_size->height, layout_size->height));
-       adj->step_increment = 5;
+       adj->step_increment = 10;
 }
 
 static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
@@ -2185,3 +2220,63 @@ void mimeview_update (MimeView *mimeview) {
                icon_list_create(mimeview, mimeview->mimeinfo);
        }
 }
+
+void mimeview_handle_cmd(MimeView *mimeview, const gchar *cmd, GdkEventButton *event, gpointer data)
+{
+       MessageView *msgview = NULL;
+       MainWindow *mainwin = NULL;
+       
+       if (!cmd)
+               return;
+       
+       msgview = mimeview->messageview;
+       if (!msgview)
+               return;
+               
+       mainwin = msgview->mainwin;
+       if (!mainwin)
+               return;
+               
+       else if (!strcmp(cmd, "sc://view_log"))
+               log_window_show(mainwin->logwin);
+       else if (!strcmp(cmd, "sc://save_as"))
+               mimeview_save_as(mimeview);
+       else if (!strcmp(cmd, "sc://display_as_text"))
+               mimeview_display_as_text(mimeview);
+       else if (!strcmp(cmd, "sc://open_with"))
+               mimeview_open_with(mimeview);
+       else if (!strcmp(cmd, "sc://open"))
+               mimeview_launch(mimeview, NULL);
+       else if (!strcmp(cmd, "sc://select_attachment") && data != NULL) {
+               icon_list_toggle_by_mime_info(mimeview, (MimeInfo *)data);
+               icon_selected(mimeview, -1, (MimeInfo *)data);
+       } else if (!strcmp(cmd, "sc://open_attachment") && data != NULL) {
+               mimeview_launch(mimeview, (MimeInfo *)data);
+       } else if (!strcmp(cmd, "sc://menu_attachment") && data != NULL) {
+               mimeview->spec_part = (MimeInfo *)data;
+               part_button_pressed(mimeview, event, (MimeInfo *)data);
+       }
+}
+
+gboolean mimeview_scroll_page(MimeView *mimeview, gboolean up)
+{
+       if (mimeview->type == MIMEVIEW_TEXT)
+               return textview_scroll_page(mimeview->textview, up);
+       else if (mimeview->mimeviewer) {
+               MimeViewer *mimeviewer = mimeview->mimeviewer;
+               if (mimeviewer->scroll_page)
+                       return mimeviewer->scroll_page(mimeviewer, up);
+       }
+       return TRUE;
+}
+
+void mimeview_scroll_one_line(MimeView *mimeview, gboolean up)
+{
+       if (mimeview->type == MIMEVIEW_TEXT)
+               textview_scroll_one_line(mimeview->textview, up);
+       else if (mimeview->mimeviewer) {
+               MimeViewer *mimeviewer = mimeview->mimeviewer;
+               if (mimeviewer->scroll_one_line)
+                       mimeviewer->scroll_one_line(mimeviewer, up);
+       }
+}