0.8.11claws18
[claws.git] / src / mimeview.c
index f8f42b978091a590d5b1a799e95e0f301a4a698d..db5e0a1165de98e6c9576fab283bdf4fb7c4995d 100644 (file)
@@ -37,7 +37,8 @@
 #include <gtk/gtkselection.h>
 #include <stdio.h>
 #include <unistd.h>
-
+#include <fnmatch.h>
 #include "intl.h"
 #include "main.h"
 #include "mimeview.h"
@@ -108,9 +109,6 @@ static void mimeview_open_with              (MimeView       *mimeview);
 static void mimeview_view_file         (const gchar    *filename,
                                         MimeInfo       *partinfo,
                                         const gchar    *cmdline);
-#if USE_GPGME
-static void mimeview_check_signature   (MimeView       *mimeview);
-#endif
 
 static GtkItemFactoryEntry mimeview_popup_entries[] =
 {
@@ -131,6 +129,9 @@ static GtkTargetEntry mimeview_mime_types[] =
        {"text/uri-list", 0, 0}
 };
 
+GSList *mimeviewer_factories;
+GSList *mimeviews;
+
 MimeView *mimeview_create(void)
 {
        MimeView *mimeview;
@@ -147,7 +148,7 @@ MimeView *mimeview_create(void)
        gint n_entries;
        gint i;
 
-       debug_print(_("Creating MIME view...\n"));
+       debug_print("Creating MIME view...\n");
        mimeview = g_new0(MimeView, 1);
 
        titles[COL_MIMETYPE] = _("MIME Type");
@@ -219,6 +220,8 @@ MimeView *mimeview_create(void)
        mimeview->popupfactory = popupfactory;
        mimeview->type         = -1;
 
+       mimeviews = g_slist_prepend(mimeviews, mimeview);
+
        return mimeview;
 }
 
@@ -308,7 +311,8 @@ void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
 
        procmime_scan_multipart_message(mimeinfo, fp);
 #if USE_GPGME
-       if (prefs_common.auto_check_signatures)
+       if ((prefs_common.auto_check_signatures)
+           && (gpg_started))
                rfc2015_check_signature(mimeinfo, fp);
        else
                set_unchecked_signature(mimeinfo);
@@ -348,9 +352,19 @@ void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
 
 void mimeview_destroy(MimeView *mimeview)
 {
+       GSList *cur;
+       
+       for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
+               MimeViewer *viewer = (MimeViewer *) cur->data;
+               viewer->destroy_viewer(viewer);
+       }
+       g_slist_free(mimeview->viewers);
+
        procmime_mimeinfo_free_all(mimeview->mimeinfo);
        g_free(mimeview->file);
        g_free(mimeview);
+
+       mimeviews = g_slist_remove(mimeviews, mimeview);
 }
 
 static void mimeview_set_multipart_tree(MimeView *mimeview,
@@ -391,6 +405,20 @@ static gchar *get_part_name(MimeInfo *partinfo)
                return partinfo->name;
        else if (partinfo->filename)
                return partinfo->filename;
+       else if (partinfo->description)
+               return partinfo->description;
+       else
+               return "";
+}
+
+static gchar *get_part_description(MimeInfo *partinfo)
+{
+       if (partinfo->description)
+               return partinfo->description;
+       else if (partinfo->name)
+               return partinfo->name;
+       else if (partinfo->filename)
+               return partinfo->filename;
        else
                return "";
 }
@@ -406,7 +434,10 @@ static GtkCTreeNode *mimeview_append_part(MimeView *mimeview,
        str[COL_MIMETYPE] =
                partinfo->content_type ? partinfo->content_type : "";
        str[COL_SIZE] = to_human_readable(partinfo->size);
-       str[COL_NAME] = get_part_name(partinfo);
+       if (prefs_common.attach_desc)
+               str[COL_NAME] = get_part_description(partinfo);
+       else
+               str[COL_NAME] = get_part_name(partinfo);
 
        node = gtk_ctree_insert_node(ctree, parent, NULL, str, 0,
                                     NULL, NULL, NULL, NULL,
@@ -468,20 +499,74 @@ static void mimeview_show_image_part(MimeView *mimeview, MimeInfo *partinfo)
                /* Workaround for the GTK+ bug with handling scroll adjustments
                 * in GtkViewport */
                imageview_clear(mimeview->imageview);
-               imageview_show_image(mimeview->imageview, partinfo, filename);
+               imageview_show_image(mimeview->imageview, partinfo, filename,
+                                    prefs_common.resize_image);
                unlink(filename);
        }
 
        g_free(filename);
 }
 
+static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
+{
+       GSList *cur;
+       MimeViewerFactory *factory = NULL;
+       MimeViewer *viewer = NULL;
+       
+       for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
+               MimeViewerFactory *curfactory = cur->data;
+
+               if(!fnmatch(curfactory->content_type, content_type, 0)) {
+                       factory = curfactory;
+                       break;
+               }
+       }
+       if (factory == NULL)
+               return NULL;
+
+       for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
+               MimeViewer *curviewer = cur->data;
+               
+               if (curviewer->factory == factory)
+                       return curviewer;
+       }
+       viewer = factory->create_viewer();
+       mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
+
+       return viewer;
+}
+
+static gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
+{
+       MimeViewer *viewer;
+       
+       viewer = get_viewer_for_content_type(mimeview, partinfo->content_type);
+       if (viewer == NULL) {
+               if (mimeview->mimeviewer != NULL)
+                       mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
+               mimeview->mimeviewer = NULL;
+               return FALSE;
+       }
+
+       if (mimeview->mimeviewer != viewer) {
+               if (mimeview->mimeviewer != NULL)
+                       mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
+               mimeview->mimeviewer = viewer;
+               mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
+       }
+       viewer->show_mimepart(viewer, mimeview->file, partinfo);
+
+       return TRUE;
+}
+
 static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
 {
        TextView  *textview  = mimeview->textview;
        ImageView *imageview = mimeview->imageview;
        GList *children;
 
-       if (mimeview->type == type) return;
+       if ((mimeview->type != MIMEVIEW_VIEWER) && 
+           (mimeview->type == type)) return;
 
        children = gtk_container_children(GTK_CONTAINER(mimeview->mime_vbox));
        if (children) {
@@ -499,6 +584,10 @@ static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
                gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
                                  GTK_WIDGET_PTR(textview));
                break;
+       case MIMEVIEW_VIEWER:
+               gtk_container_add(GTK_CONTAINER(mimeview->mime_vbox),
+                                 GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer)));
+               break;
        default:
                return;
        }
@@ -516,6 +605,8 @@ static void mimeview_clear(MimeView *mimeview)
        gtk_clist_clear(clist);
        textview_clear(mimeview->textview);
        imageview_clear(mimeview->imageview);
+       if (mimeview->mimeviewer != NULL)
+               mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
 
        mimeview->opened = NULL;
 
@@ -546,38 +637,40 @@ static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
        
        mimeview->textview->default_text = FALSE;
        
-       switch (partinfo->mime_type) {
-       case MIME_TEXT:
-       case MIME_TEXT_HTML:
-       case MIME_TEXT_ENRICHED:
-       case MIME_MESSAGE_RFC822:
-       case MIME_MULTIPART:
-               mimeview_show_message_part(mimeview, partinfo);
+       if (!mimeview_show_part(mimeview, partinfo)) {
+               switch (partinfo->mime_type) {
+               case MIME_TEXT:
+               case MIME_TEXT_HTML:
+               case MIME_TEXT_ENRICHED:
+               case MIME_MESSAGE_RFC822:
+               case MIME_MULTIPART:
+                       mimeview_show_message_part(mimeview, partinfo);
                
-               break;
+                       break;
 #if (HAVE_GDK_PIXBUF || HAVE_GDK_IMLIB)
-       case MIME_IMAGE:
-               mimeview->textview->default_text = TRUE;        
-               if (prefs_common.display_img)
-                       mimeview_show_image_part(mimeview, partinfo);
-               else {
-                       mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
-                       textview_show_mime_part(mimeview->textview, partinfo);
-               }
-               break;
+               case MIME_IMAGE:
+                       mimeview->textview->default_text = TRUE;        
+                       if (prefs_common.display_img)
+                               mimeview_show_image_part(mimeview, partinfo);
+                       else {
+                               mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
+                               textview_show_mime_part(mimeview->textview, partinfo);
+                       }
+                       break;
 #endif
-       default:
-               mimeview->textview->default_text = TRUE;        
-               mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
+               default:
+                       mimeview->textview->default_text = TRUE;        
+                       mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
 #if USE_GPGME
-               if (g_strcasecmp(partinfo->content_type,
-                                "application/pgp-signature") == 0)
-                       textview_show_signature_part(mimeview->textview,
-                                                    partinfo);
-               else
+                       if (g_strcasecmp(partinfo->content_type,
+                                        "application/pgp-signature") == 0)
+                               textview_show_signature_part(mimeview->textview,
+                                                            partinfo);
+                       else
 #endif
-                       textview_show_mime_part(mimeview->textview, partinfo);
-               break;
+                               textview_show_mime_part(mimeview->textview, partinfo);
+                       break;
+               }
        }
 }
 
@@ -811,40 +904,42 @@ static void mimeview_save_all(MimeView *mimeview)
        if (!dirname) return;
 
        /* return to first children */
+       if (!partinfo->parent->children) return;  /* multipart container? */
        attachment = partinfo->parent->children->next;
        /* for each attachment, extract it in the selected dir. */
-       while(attachment != NULL)
-       {
-               if(attachment->filename) {
-                       gchar *attachdir;
-                       gchar *attachname = g_strdup(attachment->filename);
-
-                       subst_chars(attachname, "/\\", G_DIR_SEPARATOR);
-                       subst_chars(attachname, ":?*&|<>\t\r\n", '_');
-                       g_snprintf(buf, sizeof(buf), "%s%s",
-                                  dirname,
-                                  (attachname[0] == G_DIR_SEPARATOR)
-                                  ? &attachname[1]
-                                  : attachname);
-                       attachdir = g_dirname(buf);
-                       make_dir_hier(attachdir);
-                       g_free(attachdir);
-                       g_free(attachname);
-
-                       if (is_file_exist(buf)) {
-                               AlertValue aval;
-
-                               aval = alertpanel(_("Overwrite"),
-                                                 _("Overwrite existing file?"),
-                                                 _("OK"), _("Cancel"), NULL);
-                               if (G_ALERTDEFAULT != aval) return;
-                       }
-                       if (procmime_get_part(buf, mimeview->file, attachment) < 0)
-                               alertpanel_error(_("Can't save the part of multipart message."));
+       while (attachment != NULL) {
+               static guint subst_cnt = 1;
+               gchar *attachdir;
+               gchar *attachname = g_strdup(get_part_name(attachment));
+               AlertValue aval = G_ALERTDEFAULT;
+               gchar *res;
+
+               if (!attachname || !strlen(attachname))
+                       attachname = g_strdup_printf("noname.%d",subst_cnt++);
+               subst_chars(attachname, ":?*&|<>\t\r\n", '_');
+               g_snprintf(buf, sizeof(buf), "%s%s",
+                          dirname,
+                          (attachname[0] == G_DIR_SEPARATOR)
+                          ? &attachname[1]
+                          : attachname);
+               subst_chars(buf, "/\\", G_DIR_SEPARATOR);
+               attachdir = g_dirname(buf);
+               make_dir_hier(attachdir);
+               g_free(attachdir);
+
+               if (is_file_exist(buf)) {
+                       res = g_strdup_printf(_("Overwrite existing file '%s'?"),
+                                             attachname);
+                       aval = alertpanel(_("Overwrite"), res, _("OK"), 
+                                         _("Cancel"), NULL);
+                       g_free(res);                                      
                }
+               g_free(attachname);
+
+               if ((G_ALERTDEFAULT != aval) || (procmime_get_part(buf, mimeview->file, attachment) < 0))
+                       alertpanel_error(_("Can't save the part of multipart message."));
                attachment = attachment->next;
        }
-       g_free(dirname);
 }
 
 static void mimeview_display_as_text(MimeView *mimeview)
@@ -874,6 +969,7 @@ static void mimeview_save_as(MimeView *mimeview)
        gchar *filename;
        gchar *defname = NULL;
        MimeInfo *partinfo;
+       gchar *res;
 
        if (!mimeview->opened) return;
        if (!mimeview->file) return;
@@ -893,10 +989,11 @@ static void mimeview_save_as(MimeView *mimeview)
        if (!filename) return;
        if (is_file_exist(filename)) {
                AlertValue aval;
-
-               aval = alertpanel(_("Overwrite"),
-                                 _("Overwrite existing file?"),
-                                 _("OK"), _("Cancel"), NULL);
+               res = g_strdup_printf(_("Overwrite existing file '%s'?"),
+                                     filename);
+               aval = alertpanel(_("Overwrite"), res, _("OK"), 
+                                 _("Cancel"), NULL);
+               g_free(res);                                      
                if (G_ALERTDEFAULT != aval) return;
        }
 
@@ -977,8 +1074,7 @@ static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
 {
        static gchar *default_image_cmdline = "display '%s'";
        static gchar *default_audio_cmdline = "play '%s'";
-       static gchar *default_html_cmdline =
-               "netscape -remote 'openURL(%s,raise)'";
+       static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
        static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
        gchar buf[1024];
        gchar m_buf[1024];
@@ -1012,7 +1108,7 @@ static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
                g_snprintf(buf, sizeof(buf), cmd, filename);
        else {
                if (cmd)
-                       g_warning(_("MIME viewer command line is invalid: `%s'"), cmd);
+                       g_warning("MIME viewer command line is invalid: `%s'", cmd);
                if (def_cmd)
                        g_snprintf(buf, sizeof(buf), def_cmd, filename);
                else
@@ -1061,13 +1157,13 @@ static void mimeview_update_signature_info(MimeView *mimeview)
        }
 }
 
-static void mimeview_check_signature(MimeView *mimeview)
+void mimeview_check_signature(MimeView *mimeview)
 {
        MimeInfo *mimeinfo;
-       gchar buf[BUFFSIZE];
        FILE *fp;
 
        g_return_if_fail (mimeview_is_signed(mimeview));
+       g_return_if_fail (gpg_started);
 
        mimeinfo = gtk_ctree_node_get_row_data
                (GTK_CTREE(mimeview->ctree), mimeview->opened);
@@ -1082,19 +1178,46 @@ static void mimeview_check_signature(MimeView *mimeview)
                return;
        }
 
-       /* skip headers */
-       if (mimeinfo->mime_type == MIME_MULTIPART) {
-               if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
-               FILE_OP_ERROR(mimeview->file, "fseek");
-               while (fgets(buf, sizeof(buf), fp) != NULL)
-                       if (buf[0] == '\r' || buf[0] == '\n') break;
-       }
-
-       procmime_scan_multipart_message(mimeinfo, fp);
        rfc2015_check_signature(mimeinfo, fp);
        fclose(fp);
 
        mimeview_update_names(mimeview);
        mimeview_update_signature_info(mimeview);
+
+       textview_show_message(mimeview->messageview->textview, mimeinfo,
+                             mimeview->file);
 }
 #endif /* USE_GPGME */
+
+void mimeview_register_viewer_factory(MimeViewerFactory *factory)
+{
+       mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
+}
+
+static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
+{
+       return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
+}
+
+void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
+{
+       GSList *mimeview_list, *viewer_list;
+
+       for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
+               MimeView *mimeview = (MimeView *) mimeview_list->data;
+               
+               if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
+                       mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
+                       mimeview->mimeviewer = NULL;
+               }
+
+               while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
+                       MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
+
+                       mimeviewer->destroy_viewer(mimeviewer);
+                       mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
+               }
+       }
+
+       mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
+}