Fix a leak.
[claws.git] / src / messageview.c
index a4e978846d35b4d78cf6646c9c10a69a99b5cf29..f4ff32419e936d4014a341beb923f9f9720cfd8f 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2013 Hiroyuki Yamamoto and the Claws Mail team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2016 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
@@ -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/>.
- * 
  */
 
 #include "defs.h"
@@ -251,7 +250,7 @@ static GtkActionEntry msgview_entries[] =
        {"View/Goto/ParentMessage",     NULL, N_("Parent message"), "<control>Up", NULL, G_CALLBACK(parent_cb) },
        /* {"View/Goto/---",            NULL, "---", NULL, NULL, NULL }, */
        {"View/Goto/NextUnreadFolder",  NULL, N_("Next unread _folder"), "<shift>G", NULL, G_CALLBACK(goto_unread_folder_cb) },
-       {"View/Goto/OtherFolder",       NULL, N_("_Other folder..."), "G", NULL, G_CALLBACK(goto_folder_cb) },
+       {"View/Goto/Folder",            NULL, N_("F_older..."), "G", NULL, G_CALLBACK(goto_folder_cb) },
        /* {"View/Goto/---",            NULL, "---", NULL, NULL, NULL }, */
        {"View/Goto/NextPart",          NULL, N_("Next part"), "A", NULL, G_CALLBACK(goto_next_part_cb) },
        {"View/Goto/PrevPart",          NULL, N_("Previous part"), "Z", NULL, G_CALLBACK(goto_prev_part_cb) },
@@ -527,7 +526,7 @@ static void messageview_add_toolbar(MessageView *msgview, GtkWidget *window)
        MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "ParentMessage", "View/Goto/ParentMessage", GTK_UI_MANAGER_MENUITEM)
        MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "Separator7", "View/Goto/---", GTK_UI_MANAGER_SEPARATOR)
        MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "NextUnreadFolder", "View/Goto/NextUnreadFolder", GTK_UI_MANAGER_MENUITEM)
-       MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "OtherFolder", "View/Goto/OtherFolder", GTK_UI_MANAGER_MENUITEM)
+       MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "Folder", "View/Goto/Folder", GTK_UI_MANAGER_MENUITEM)
        MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "Separator8", "View/Goto/---", GTK_UI_MANAGER_SEPARATOR)
        MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "NextPart", "View/Goto/NextPart", GTK_UI_MANAGER_MENUITEM)
        MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "PrevPart", "View/Goto/PrevPart", GTK_UI_MANAGER_MENUITEM)
@@ -850,7 +849,7 @@ static gint disposition_notification_send(MsgInfo *msginfo)
                    "to be sent does not correspond to the return path:\n"
                    "Notification address: %s\n"
                    "Return path: %s\n"
-                   "It is advised to not to send the return receipt."),
+                   "It is advised to not send the return receipt."),
                  to, buf);
                val = alertpanel_full(_("Warning"), message,
                                _("_Don't Send"), _("_Send"), NULL, FALSE,
@@ -864,14 +863,21 @@ static gint disposition_notification_send(MsgInfo *msginfo)
        ac_list = account_find_all_from_address(ac_list, msginfo->cc);
 
        if (ac_list == NULL) {
-               AlertValue val = 
-               alertpanel_full(_("Warning"),
-                 _("This message is asking for a return receipt notification\n"
-                   "but according to its 'To:' and 'CC:' headers it was not\n"
+               AlertValue val;
+               gchar *text;
+               text = g_strdup_printf(
+                 C_("'%s' stands for 'To' then 'Cc'",
+                   "This message is asking for a return receipt notification\n"
+                   "but according to its '%s' and '%s' headers it was not\n"
                    "officially addressed to you.\n"
-                   "It is advised to not to send the return receipt."),
+                   "It is advised to not send the return receipt."),
+                 prefs_common_translated_header_name("To"),
+                 prefs_common_translated_header_name("Cc"));
+               val = alertpanel_full(_("Warning"),
+                 text,
                  _("_Don't Send"), _("_Send"), NULL, FALSE,
                  NULL, ALERT_WARNING, G_ALERTDEFAULT);
+               g_free(text);
                if (val != G_ALERTALTERNATE)
                        return -1;
        }
@@ -977,26 +983,13 @@ static gint disposition_notification_send(MsgInfo *msginfo)
                goto FILE_ERROR;
 
        /* Message ID */
-       if (account->set_domain && account->domain) {
-               g_snprintf(buf, sizeof(buf), "%s", account->domain); 
-       } else if (!strncmp(get_domain_name(), "localhost", strlen("localhost"))) {
-               g_snprintf(buf, sizeof(buf), "%s", 
-                       strchr(account->address, '@') ?
-                               strchr(account->address, '@')+1 :
-                               account->address);
-       } else {
-               g_snprintf(buf, sizeof(buf), "%s", "");
-       }
-       
        if (account->gen_msgid) {
-               gchar *addr = NULL;
-               if (account->msgid_with_addr) {
-                       addr = account->address;
-               }
-               generate_msgid(buf, sizeof(buf), addr);
-
-               if (fprintf(fp, "Message-ID: <%s>\n", buf) < 0)
+               gchar *addr = prefs_account_generate_msgid(account);
+               if (fprintf(fp, "Message-ID: <%s>\n", addr) < 0) {
+                       g_free(addr);
                        goto FILE_ERROR;
+               }
+               g_free(addr);
        }
 
        boundary = generate_mime_boundary("DN");
@@ -1273,6 +1266,60 @@ MsgInfo *messageview_nav_get_next(MessageView *messageview) {
        return info;
 }
 
+static gboolean messageview_try_select_mimeinfo(MessageView *messageview, MsgInfo *msginfo, MimeInfo *mimeinfo)
+{
+       if (mimeinfo->type == MIMETYPE_TEXT) {
+               if (!strcasecmp(mimeinfo->subtype, "calendar")
+                               && mimeview_has_viewer_for_content_type(messageview->mimeview, "text/calendar")) {
+                       mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
+                       return TRUE;
+               } else if (!strcasecmp(mimeinfo->subtype, "html")
+                               && mimeinfo->disposition != DISPOSITIONTYPE_ATTACHMENT
+                               && (msginfo->folder->prefs->promote_html_part == HTML_PROMOTE_ALWAYS
+                                       || (msginfo->folder->prefs->promote_html_part == HTML_PROMOTE_DEFAULT
+                                               && prefs_common.promote_html_part))) {
+                       mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
+                       return TRUE;
+               }
+       }
+       return FALSE;
+}
+
+static void messageview_find_part_depth_first(MimeInfoSearch *context, MimeMediaType type, const gchar *subtype)
+{
+       MimeInfo * mimeinfo = context->current;
+
+       if (!mimeinfo)
+               return;
+
+       debug_print("found part %d/%s\n", mimeinfo->type, mimeinfo->subtype);
+
+       if (mimeinfo->type == type
+                       && !strcasecmp(mimeinfo->subtype, subtype)) {
+               context->found = mimeinfo;
+       } else if (mimeinfo->type == MIMETYPE_MULTIPART) {
+               if (!strcasecmp(mimeinfo->subtype, "alternative")
+                               || !strcasecmp(mimeinfo->subtype, "related")) {
+                       context->found = procmime_mimeinfo_next(mimeinfo);
+                       while (context->found && context->found != context->parent) {
+                               if (context->found->type == type
+                                       && !strcasecmp(context->found->subtype, subtype))
+                                               break;
+                               context->found = procmime_mimeinfo_next(context->found);
+                       }
+                       if (context->found == context->parent)
+                               context->found = NULL;
+               }
+               if (!context->found
+                       && (!strcasecmp(mimeinfo->subtype, "related")
+                               || !strcasecmp(mimeinfo->subtype, "mixed"))) {
+                       context->parent = mimeinfo;
+                       context->current = procmime_mimeinfo_next(mimeinfo);
+                       messageview_find_part_depth_first(context, type, subtype);
+               }
+       }
+}
+
 gint messageview_show(MessageView *messageview, MsgInfo *msginfo,
                      gboolean all_headers)
 {
@@ -1370,7 +1417,7 @@ gint messageview_show(MessageView *messageview, MsgInfo *msginfo,
        }
                        
        if (messageview->msginfo != msginfo) {
-               procmsg_msginfo_free(messageview->msginfo);
+               procmsg_msginfo_free(&(messageview->msginfo));
                messageview->msginfo = NULL;
                messageview_set_menu_sensitive(messageview);
                messageview->msginfo = 
@@ -1455,82 +1502,36 @@ gint messageview_show(MessageView *messageview, MsgInfo *msginfo,
                        
        root = mimeinfo;
        mimeinfo = procmime_mimeinfo_next(mimeinfo);
-       if (!all_headers && mimeinfo 
-                       && (mimeinfo->type != MIMETYPE_TEXT || 
-           strcasecmp(mimeinfo->subtype, "plain")) 
-                       && (mimeinfo->type != MIMETYPE_MULTIPART || 
-           strcasecmp(mimeinfo->subtype, "signed"))) {
-               if (strcasecmp(mimeinfo->subtype, "html")) {
-                       MimeInfo *saved_mimeinfo = mimeinfo;
-                       MimeInfo *alt_parent = mimeinfo;
-
-                       /* if multipart/{related,mixed} part, look inside for a multipart/alternative child */
-                       if (mimeinfo->type == MIMETYPE_MULTIPART &&
-                           (!strcasecmp(mimeinfo->subtype, "related") ||
-                            !strcasecmp(mimeinfo->subtype, "mixed"))) {
-                               for (; mimeinfo; mimeinfo = procmime_mimeinfo_next(mimeinfo)) {
-                                       if (mimeinfo->node->parent != saved_mimeinfo->node) {
-                                               /* only consider children of the 
-                                                * multipart/{related,mixed} part */
-                                               continue;
-                                       }
-                                       if (mimeinfo->type == MIMETYPE_MULTIPART && 
-                                           !strcasecmp(mimeinfo->subtype, "alternative")) {
-                                               /* we got an alternative part */
-                                               alt_parent = mimeinfo;
-                                               break;
-                                       }
-                                       if (mimeinfo->type == MIMETYPE_TEXT && 
-                                           !strcasecmp(mimeinfo->subtype, "calendar") &&
-                                           mimeview_has_viewer_for_content_type(messageview->mimeview,
-                                                                                "text/calendar")) {
-                                               mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
-                                               goto done;
-                                       } else if (mimeinfo->type == MIMETYPE_TEXT && 
-                                           !strcasecmp(mimeinfo->subtype, "html") &&
-                                           mimeinfo->disposition != DISPOSITIONTYPE_ATTACHMENT &&
-                                                       (msginfo->folder->prefs->promote_html_part == HTML_PROMOTE_ALWAYS ||
-                                                        (msginfo->folder->prefs->promote_html_part == HTML_PROMOTE_DEFAULT &&
-                                                               prefs_common.promote_html_part))) {
-                                               mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
+       if (!all_headers && mimeinfo
+                       && (mimeinfo->type != MIMETYPE_TEXT
+                               || strcasecmp(mimeinfo->subtype, "plain"))
+                       && (mimeinfo->type != MIMETYPE_MULTIPART
+                               || strcasecmp(mimeinfo->subtype, "signed"))) {
+               if (strcasecmp(mimeinfo->subtype, "html")) {
+                       MimeInfoSearch context = {
+                               .parent = root,
+                               .current = mimeinfo,
+                               .found = NULL
+                       };
+                       if (mimeview_has_viewer_for_content_type(messageview->mimeview, "text/calendar")) {
+                               MimeInfoSearch cal_context = context;
+                               messageview_find_part_depth_first(&cal_context, MIMETYPE_TEXT, "calendar");
+                               if (cal_context.found) { /* calendar found */
+                                       mimeinfo = cal_context.found;
+                                       if (messageview_try_select_mimeinfo(messageview, msginfo, mimeinfo))
                                                goto done;
-                                       }
                                }
                        }
-
-                       /* if we now have a multipart/alternative part (possibly inside a
-                        * multipart/{related,mixed} part, look for an HTML part inside */
-                       if (mimeinfo && mimeinfo->type == MIMETYPE_MULTIPART &&
-                           !strcasecmp(mimeinfo->subtype, "alternative")) {
-                               for (; mimeinfo; mimeinfo = procmime_mimeinfo_next(mimeinfo)) {
-                                       if (mimeinfo->node->parent != alt_parent->node) {
-                                               /* only consider children of the 
-                                                * multipart/alternative part, so as
-                                                * not to show html attachments */
-                                               continue;
-                                       }
-                                       
-                                       if (mimeinfo->type == MIMETYPE_TEXT && 
-                                           !strcasecmp(mimeinfo->subtype, "calendar") &&
-                                           mimeview_has_viewer_for_content_type(messageview->mimeview,
-                                                                                "text/calendar")) {
-                                               mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
-                                               goto done;
-                                       } else if (mimeinfo->type == MIMETYPE_TEXT && 
-                                           !strcasecmp(mimeinfo->subtype, "html") &&
-                                           mimeinfo->disposition != DISPOSITIONTYPE_ATTACHMENT &&
-                                                       (msginfo->folder->prefs->promote_html_part == HTML_PROMOTE_ALWAYS ||
-                                                        (msginfo->folder->prefs->promote_html_part == HTML_PROMOTE_DEFAULT &&
-                                                               prefs_common.promote_html_part))) {
-                                               mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
-                                               goto done;
-                                       }
-                               }
-                       }
-                       
-                       /* if we didn't find anything, go back to start */
-                       if (!mimeinfo) 
-                               mimeinfo = saved_mimeinfo;
+                       messageview_find_part_depth_first(&context, MIMETYPE_TEXT, "html");
+                       if (context.found &&
+                           (msginfo->folder->prefs->promote_html_part == HTML_PROMOTE_ALWAYS ||
+                            (msginfo->folder->prefs->promote_html_part == HTML_PROMOTE_DEFAULT &&
+                             prefs_common.promote_html_part))) { /* html found */
+                               mimeinfo = context.found;
+                               if (messageview_try_select_mimeinfo(messageview, msginfo, mimeinfo))
+                                       goto done;
+                       } else
+                               mimeinfo = root; /* nothing found */
 
                        if (!mimeview_show_part(messageview->mimeview, mimeinfo))
                                mimeview_select_mimepart_icon(messageview->mimeview, root);
@@ -1581,7 +1582,7 @@ void messageview_clear(MessageView *messageview)
 {
        if (!messageview)
                return;
-       procmsg_msginfo_free(messageview->msginfo);
+       procmsg_msginfo_free(&(messageview->msginfo));
        messageview->msginfo = NULL;
        messageview->filtered = FALSE;
 
@@ -1633,7 +1634,7 @@ void messageview_destroy(MessageView *messageview)
        mimeview_destroy(messageview->mimeview);
        noticeview_destroy(messageview->noticeview);
 
-       procmsg_msginfo_free(messageview->msginfo);
+       procmsg_msginfo_free(&(messageview->msginfo));
        toolbar_clear_list(TOOLBAR_MSGVIEW);
        if (messageview->toolbar) {
                toolbar_destroy(messageview->toolbar);
@@ -1940,7 +1941,7 @@ static void return_receipt_show(NoticeView *noticeview, MsgInfo *msginfo)
        if (from_me) {
                noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
                if (MSG_IS_RETRCPT_GOT(msginfo->flags)) {
-                       noticeview_set_text(noticeview, _("You got a return receipt for this message : "
+                       noticeview_set_text(noticeview, _("You got a return receipt for this message: "
                                                          "it has been displayed by the recipient."));
                } else {
                        noticeview_set_text(noticeview, _("You asked for a return receipt in this message."));
@@ -1978,7 +1979,7 @@ static void return_receipt_send_clicked(NoticeView *noticeview, MsgInfo *msginfo
                noticeview_hide(noticeview);
        }               
 
-       procmsg_msginfo_free(tmpmsginfo);
+       procmsg_msginfo_free(&tmpmsginfo);
        g_free(file);
 }
 
@@ -2299,7 +2300,7 @@ static void prev_cb(GtkAction *action, gpointer data)
 {
        MessageView *messageview = (MessageView *)data;
        messageview->updating = TRUE;
-       summary_step(messageview->mainwin->summaryview, GTK_SCROLL_STEP_BACKWARD);
+       summary_select_prev(messageview->mainwin->summaryview);
        messageview->updating = FALSE;
 
        if (messageview->deferred_destroy) {
@@ -2324,7 +2325,7 @@ static void next_cb(GtkAction *action, gpointer data)
 {
        MessageView *messageview = (MessageView *)data;
        messageview->updating = TRUE;
-       summary_step(messageview->mainwin->summaryview, GTK_SCROLL_STEP_FORWARD);
+       summary_select_next(messageview->mainwin->summaryview);
        messageview->updating = FALSE;
 
        if (messageview->deferred_destroy) {
@@ -2554,7 +2555,7 @@ static void prev_history_cb(GtkAction *action, gpointer data)
                messageview_show(messageview, info, 
                                         messageview->all_headers);
                messageview->updating = FALSE;
-               procmsg_msginfo_free(info);
+               procmsg_msginfo_free(&info);
                if (messageview->deferred_destroy) {
                        debug_print("messageview got away!\n");
                        messageview_destroy(messageview);
@@ -2572,7 +2573,7 @@ static void next_history_cb(GtkAction *action, gpointer data)
                messageview_show(messageview, info, 
                                         messageview->all_headers);
                messageview->updating = FALSE;
-               procmsg_msginfo_free(info);
+               procmsg_msginfo_free(&info);
                if (messageview->deferred_destroy) {
                        debug_print("messageview got away!\n");
                        messageview_destroy(messageview);
@@ -2611,7 +2612,7 @@ static void goto_unread_folder_cb(GtkAction *action, gpointer data)
        MessageView *messageview = (MessageView *)data;
 
        messageview->updating = TRUE;
-       folderview_select_next_with_flag(messageview->mainwin->folderview, MSG_UNREAD, FALSE);
+       folderview_select_next_with_flag(messageview->mainwin->folderview, MSG_UNREAD);
        messageview->updating = FALSE;
 
        if (messageview->deferred_destroy) {
@@ -2635,9 +2636,7 @@ static void goto_unread_folder_cb(GtkAction *action, gpointer data)
 static void goto_folder_cb(GtkAction *action, gpointer data)
 {
        MessageView *messageview = (MessageView *)data;
-       messageview->updating = TRUE;
        FolderItem *to_folder;
-       messageview->updating = FALSE;
 
        to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL, FALSE);
 
@@ -2748,7 +2747,7 @@ static void show_all_header_cb(GtkToggleAction *action, gpointer data)
        if (!msginfo) return;
        messageview->msginfo = NULL;
        messageview_show(messageview, msginfo, messageview->all_headers);
-       procmsg_msginfo_free(msginfo);
+       procmsg_msginfo_free(&msginfo);
        main_window_set_menu_sensitive(messageview->mainwin);
        summary_redisplay_msg(messageview->mainwin->summaryview);
 }
@@ -2780,7 +2779,7 @@ static void msg_hide_quotes_cb(GtkToggleAction *action, gpointer data)
        messageview->msginfo = NULL;
        messageview_show(messageview, msginfo,
                         messageview->all_headers);
-       procmsg_msginfo_free(msginfo);
+       procmsg_msginfo_free(&msginfo);
        
        /* update main window */
        main_window_set_menu_sensitive(messageview->mainwin);
@@ -2869,7 +2868,7 @@ static void add_address_cb(GtkAction *action, gpointer data)
        avatarr = avatars_avatarrender_new(full_msginfo);
        hooks_invoke(AVATAR_IMAGE_RENDER_HOOKLIST, avatarr);
 
-       procmsg_msginfo_free(full_msginfo);
+       procmsg_msginfo_free(&full_msginfo);
 
        if (avatarr->image != NULL)
                picture = gtk_image_get_pixbuf(GTK_IMAGE(avatarr->image));