2006-08-23 [colin] 2.4.0cvs78
[claws.git] / src / mimeview.c
index 5ee7a17a08163634c47abbaa3ff7402cfaf60473..3a3cff20eeee7a04710c7c179c81a18e8d035fab 100644 (file)
@@ -64,7 +64,7 @@
 #include "stock_pixmap.h"
 #include "gtk/gtkvscrollbutton.h"
 #include "gtk/logwindow.h"
-
+#include "timing.h"
 
 typedef enum
 {
@@ -113,16 +113,20 @@ 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_open_with         (MimeView       *mimeview);
 static void mimeview_open_part_with    (MimeView       *mimeview,
-                                        MimeInfo       *partinfo);
+                                        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, 
@@ -246,6 +250,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);
@@ -311,7 +318,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);
@@ -1108,6 +1115,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
 
@@ -1588,10 +1608,10 @@ static void mimeview_open_with(MimeView *mimeview)
 
        partinfo = mimeview_get_part_to_use(mimeview);
 
-       mimeview_open_part_with(mimeview, partinfo);
+       mimeview_open_part_with(mimeview, partinfo, FALSE);
 }
 
-static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo)
+static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo, gboolean automatic)
 {
        gchar *filename;
        gchar *cmd;
@@ -1622,24 +1642,55 @@ static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo)
                content_type = procmime_get_content_type_str(partinfo->type,
                        partinfo->subtype);
        }
-       mime_command = mailcap_get_command_for_type(content_type);
-
+       
+       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);
+               mime_command = mailcap_get_command_for_type(content_type, filename);
        }
 
-       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 (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);
@@ -1647,66 +1698,28 @@ static void mimeview_open_part_with(MimeView *mimeview, MimeInfo *partinfo)
                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_part_with(mimeview, partinfo);
-               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_part_with(mimeview, partinfo);
-                       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_part_with(mimeview, partinfo);
 }
 
 void mimeview_register_viewer_factory(MimeViewerFactory *factory)
@@ -1772,6 +1785,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), \
@@ -2114,7 +2133,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)