Fix CID#1436912: Dereferencing null pointer "msginfo->folder"
[claws.git] / src / messageview.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2016 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "defs.h"
20
21 #include <glib.h>
22 #include <glib/gi18n.h>
23 #include <gdk/gdkkeysyms.h>
24 #include <gtk/gtk.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <string.h>
28
29 #include "main.h"
30 #include "messageview.h"
31 #include "message_search.h"
32 #include "headerview.h"
33 #include "summaryview.h"
34 #include "textview.h"
35 #include "mimeview.h"
36 #include "menu.h"
37 #include "about.h"
38 #include "filesel.h"
39 #include "foldersel.h"
40 #include "sourcewindow.h"
41 #include "alertpanel.h"
42 #include "inputdialog.h"
43 #include "mainwindow.h"
44 #include "manage_window.h"
45 #include "procmsg.h"
46 #include "procheader.h"
47 #include "procmime.h"
48 #include "account.h"
49 #include "action.h"
50 #include "prefs_common.h"
51 #include "prefs_account.h"
52 #include "gtkutils.h"
53 #include "utils.h"
54 #include "send_message.h"
55 #include "stock_pixmap.h"
56 #include "hooks.h"
57 #include "filtering.h"
58 #include "partial_download.h"
59 #include "uri_opener.h"
60 #include "inc.h"
61 #include "log.h"
62 #include "privacy.h"
63 #include "combobox.h"
64 #include "printing.h"
65 #include "quoted-printable.h"
66 #include "version.h"
67 #include "statusbar.h"
68 #include "folder_item_prefs.h"
69 #include "avatars.h"
70 #ifndef USE_ALT_ADDRBOOK
71         #include "addressbook.h"
72 #else
73         #include "addressadd.h"
74         #include "addressbook-dbus.h"
75 #endif
76 static GList *messageview_list = NULL;
77
78 static gint messageview_delete_cb       (GtkWidget              *widget,
79                                          GdkEventAny            *event,
80                                          MessageView            *messageview);
81 static void messageview_size_allocate_cb(GtkWidget      *widget,
82                                          GtkAllocation  *allocation);
83 static gboolean key_pressed             (GtkWidget      *widget,
84                                          GdkEventKey    *event,
85                                          MessageView    *messageview);
86 static void return_receipt_show         (NoticeView     *noticeview, 
87                                          MsgInfo        *msginfo);      
88 static void return_receipt_send_clicked (NoticeView     *noticeview, 
89                                          MsgInfo        *msginfo);
90 static void partial_recv_show           (NoticeView     *noticeview, 
91                                          MsgInfo        *msginfo);      
92 static void partial_recv_dload_clicked  (NoticeView     *noticeview, 
93                                          MsgInfo        *msginfo);
94 static void partial_recv_del_clicked    (NoticeView     *noticeview, 
95                                          MsgInfo        *msginfo);
96 static void partial_recv_unmark_clicked (NoticeView     *noticeview, 
97                                          MsgInfo        *msginfo);
98 static void save_as_cb                  (GtkAction      *action,
99                                          gpointer        data);
100 static void page_setup_cb               (GtkAction      *action,
101                                          gpointer        data);
102 static void print_cb                    (GtkAction      *action,
103                                          gpointer        data);
104 static void close_cb                    (GtkAction      *action,
105                                          gpointer        data);
106 static void copy_cb                     (GtkAction      *action,
107                                          gpointer        data);
108 static void allsel_cb                   (GtkAction      *action,
109                                          gpointer        data);
110 static void search_cb                   (GtkAction      *action,
111                                          gpointer        data);
112
113 static void prev_cb                     (GtkAction      *action,
114                                          gpointer        data);
115 static void next_cb                     (GtkAction      *action,
116                                          gpointer        data);
117 static void prev_unread_cb              (GtkAction      *action,
118                                          gpointer        data);
119 static void next_unread_cb              (GtkAction      *action,
120                                          gpointer        data);
121 static void prev_new_cb                 (GtkAction      *action,
122                                          gpointer        data);
123 static void next_new_cb                 (GtkAction      *action,
124                                          gpointer        data);
125 static void prev_marked_cb              (GtkAction      *action,
126                                          gpointer        data);
127 static void next_marked_cb              (GtkAction      *action,
128                                          gpointer        data);
129 static void prev_labeled_cb             (GtkAction      *action,
130                                          gpointer        data);
131 static void next_labeled_cb             (GtkAction      *action,
132                                          gpointer        data);
133 static void prev_history_cb             (GtkAction      *action,
134                                          gpointer        data);
135 static void next_history_cb             (GtkAction      *action,
136                                          gpointer        data);
137 static void parent_cb                   (GtkAction      *action,
138                                          gpointer        data);
139 static void goto_unread_folder_cb       (GtkAction      *action,
140                                          gpointer        data);
141 static void goto_folder_cb              (GtkAction      *action,
142                                          gpointer        data);
143
144 static void scroll_prev_line_cb         (GtkAction      *action,
145                                          gpointer        data);
146 static void scroll_next_line_cb         (GtkAction      *action,
147                                           gpointer        data);
148 static void scroll_next_page_cb         (GtkAction      *action,
149                                          gpointer        data);
150 static void scroll_prev_page_cb         (GtkAction      *action,
151                                          gpointer        data);
152
153 static void set_charset_cb              (GtkAction *action, GtkRadioAction *current, gpointer data);
154 static void set_decode_cb               (GtkAction *action, GtkRadioAction *current, gpointer data);
155
156 static void view_source_cb              (GtkAction      *action,
157                                          gpointer        data);
158
159 static void show_all_header_cb          (GtkToggleAction        *action,
160                                          gpointer        data);
161 static void msg_hide_quotes_cb          (GtkToggleAction        *action,
162                                          gpointer        data);
163
164 static void compose_cb                  (GtkAction      *action,
165                                          gpointer        data);
166 static void reply_cb                    (GtkAction      *action,
167                                          gpointer        data);
168
169 static PrefsAccount *select_account_from_list
170                                         (GList          *ac_list);
171 static void addressbook_open_cb         (GtkAction      *action,
172                                          gpointer        data);
173 static void add_address_cb              (GtkAction      *action,
174                                          gpointer        data);
175 static void create_filter_cb            (GtkAction      *action,
176                                          gpointer        data);
177 static void create_processing_cb        (GtkAction      *action,
178                                          gpointer        data);
179 static void open_urls_cb                (GtkAction      *action,
180                                          gpointer        data);
181
182 static void about_cb                    (GtkAction      *action,
183                                          gpointer        data);
184 static void messageview_update          (MessageView    *msgview,
185                                          MsgInfo        *old_msginfo);
186 static gboolean messageview_update_msg  (gpointer source, gpointer data);
187
188 static void save_part_as_cb(GtkAction *action, gpointer data);
189 static void view_part_as_text_cb(GtkAction *action, gpointer data);
190 static void open_part_cb(GtkAction *action, gpointer data);
191 #ifndef G_OS_WIN32
192 static void open_part_with_cb(GtkAction *action, gpointer data);
193 #endif
194 static void check_signature_cb(GtkAction *action, gpointer data);
195 static void goto_next_part_cb(GtkAction *action, gpointer data);
196 static void goto_prev_part_cb(GtkAction *action, gpointer data);
197
198 static void messageview_nothing_cb         (GtkAction *action, gpointer data)
199 {
200
201 }
202
203 static GList *msgview_list = NULL;
204 static GtkActionEntry msgview_entries[] =
205 {
206         {"Menu",                                     NULL, "Menu", NULL, NULL, NULL },
207 /* menus */
208         {"File",                                     NULL, N_("_File"), NULL, NULL, NULL },
209         {"Edit",                                     NULL, N_("_Edit"), NULL, NULL, NULL },
210         {"View",                                     NULL, N_("_View"), NULL, NULL, NULL },
211         {"Message",                                  NULL, N_("_Message"), NULL, NULL, NULL },
212         {"Tools",                                    NULL, N_("_Tools"), NULL, NULL, NULL },
213         {"Help",                                     NULL, N_("_Help"), NULL, NULL, NULL },
214         {"PlaceHolder",                              NULL, "Placeholder", NULL, NULL, G_CALLBACK(messageview_nothing_cb) },
215
216 /* File menu */
217         {"File/SaveAs",                              NULL, N_("_Save email as..."), "<control>S", NULL, G_CALLBACK(save_as_cb) },
218         {"File/SavePartAs",                          NULL, N_("_Save part as..."), "Y", NULL, G_CALLBACK(save_part_as_cb) },
219         {"File/PageSetup",                           NULL, N_("Page setup..."), NULL, NULL, G_CALLBACK(page_setup_cb) },
220         {"File/Print",                               NULL, N_("_Print..."), "<control>P", NULL, G_CALLBACK(print_cb) },
221         {"File/---",                                 NULL, "---", NULL, NULL, NULL },
222         {"File/Close",                               NULL, N_("_Close"), "<control>W", NULL, G_CALLBACK(close_cb) },
223
224 /* Edit menu */
225         {"Edit/Copy",                                NULL, N_("_Copy"), "<control>C", NULL, G_CALLBACK(copy_cb) },
226         {"Edit/SelectAll",                           NULL, N_("_Select all"), "<control>A", NULL, G_CALLBACK(allsel_cb) },
227         {"Edit/---",                                 NULL, "---", NULL, NULL, NULL },
228         {"Edit/Find",                                NULL, N_("_Find"), "<control>F", NULL, G_CALLBACK(search_cb) },
229         
230 /* View menu */
231         {"View/Goto",                                NULL, N_("_Go to"), NULL, NULL, NULL },
232         {"View/Goto/Prev",                           NULL, N_("_Previous message"), "P", NULL, G_CALLBACK(prev_cb) },
233         {"View/Goto/Next",                           NULL, N_("_Next message"), "N", NULL, G_CALLBACK(next_cb) },
234         {"View/Goto/---",                            NULL, "---", NULL, NULL, NULL },
235         {"View/Goto/PrevUnread",                     NULL, N_("P_revious unread message"), "<shift>P", NULL, G_CALLBACK(prev_unread_cb) },
236         {"View/Goto/NextUnread",                     NULL, N_("N_ext unread message"), "<shift>N", NULL, G_CALLBACK(next_unread_cb) },
237         /* {"View/Goto/---",                         NULL, "---", NULL, NULL, NULL }, */
238         {"View/Goto/PrevNew",                        NULL, N_("Previous ne_w message"), NULL, NULL, G_CALLBACK(prev_new_cb) },
239         {"View/Goto/NextNew",                        NULL, N_("Ne_xt new message"), NULL, NULL, G_CALLBACK(next_new_cb) },
240         /* {"View/Goto/---",                         NULL, "---", NULL, NULL, NULL }, */
241         {"View/Goto/PrevMarked",                     NULL, N_("Previous _marked message"), NULL, NULL, G_CALLBACK(prev_marked_cb) },
242         {"View/Goto/NextMarked",                     NULL, N_("Next m_arked message"), NULL, NULL, G_CALLBACK(next_marked_cb) },
243         /* {"View/Goto/---",                         NULL, "---", NULL, NULL, NULL }, */
244         {"View/Goto/PrevLabeled",                    NULL, N_("Previous _labeled message"), NULL, NULL, G_CALLBACK(prev_labeled_cb) },
245         {"View/Goto/NextLabeled",                    NULL, N_("Next la_beled message"), NULL, NULL, G_CALLBACK(next_labeled_cb) },
246         /* {"View/Goto/---",                         NULL, "---", NULL, NULL, NULL }, */
247         {"View/Goto/PrevHistory",                    NULL, N_("Previous opened message"), "<alt>Left", NULL, G_CALLBACK(prev_history_cb) },
248         {"View/Goto/NextHistory",                    NULL, N_("Next opened message"), "<alt>Right", NULL, G_CALLBACK(next_history_cb) },
249         /* {"View/Goto/---",                         NULL, "---", NULL, NULL, NULL }, */
250         {"View/Goto/ParentMessage",                  NULL, N_("Parent message"), "<control>Up", NULL, G_CALLBACK(parent_cb) },
251         /* {"View/Goto/---",                         NULL, "---", NULL, NULL, NULL }, */
252         {"View/Goto/NextUnreadFolder",               NULL, N_("Next unread _folder"), "<shift>G", NULL, G_CALLBACK(goto_unread_folder_cb) },
253         {"View/Goto/Folder",                         NULL, N_("F_older..."), "G", NULL, G_CALLBACK(goto_folder_cb) },
254         /* {"View/Goto/---",                         NULL, "---", NULL, NULL, NULL }, */
255         {"View/Goto/NextPart",                       NULL, N_("Next part"), "A", NULL, G_CALLBACK(goto_next_part_cb) },
256         {"View/Goto/PrevPart",                       NULL, N_("Previous part"), "Z", NULL, G_CALLBACK(goto_prev_part_cb) },
257         {"View/Scroll",                              NULL, N_("Message scroll"), NULL, NULL, NULL },
258         {"View/Scroll/PrevLine",                     NULL, N_("Previous line"), NULL, NULL, G_CALLBACK(scroll_prev_line_cb) },
259         {"View/Scroll/NextLine",                     NULL, N_("Next line"), NULL, NULL, G_CALLBACK(scroll_next_line_cb) },
260         {"View/Scroll/PrevPage",                     NULL, N_("Previous page"), NULL, NULL, G_CALLBACK(scroll_prev_page_cb) },
261         {"View/Scroll/NextPage",                     NULL, N_("Next page"), NULL, NULL, G_CALLBACK(scroll_next_page_cb) },
262         /* {"View/Scroll/---",                       NULL, "---", NULL, NULL, NULL }, */
263
264         {"View/Encoding",                            NULL, N_("Character _encoding"), NULL, NULL, NULL }, /* set_charset_cb */
265         {"View/Encoding/---",                        NULL, "---", NULL, NULL, NULL },
266 #define ENC_ACTION(cs_char,c_char,string) \
267         { "View/Encoding/" cs_char, NULL, N_(string), NULL, NULL, c_char }
268
269         {"View/Encoding/Western",                    NULL, N_("Western European"), NULL, NULL, NULL },
270         {"View/Encoding/Baltic",                     NULL, N_("Baltic"), NULL, NULL, NULL },
271         {"View/Encoding/Hebrew",                     NULL, N_("Hebrew"), NULL, NULL, NULL },
272         {"View/Encoding/Arabic",                     NULL, N_("Arabic"), NULL, NULL, NULL },
273         {"View/Encoding/Cyrillic",                   NULL, N_("Cyrillic"), NULL, NULL, NULL },
274         {"View/Encoding/Japanese",                   NULL, N_("Japanese"), NULL, NULL, NULL },
275         {"View/Encoding/Chinese",                    NULL, N_("Chinese"), NULL, NULL, NULL },
276         {"View/Encoding/Korean",                     NULL, N_("Korean"), NULL, NULL, NULL },
277         {"View/Encoding/Thai",                       NULL, N_("Thai"), NULL, NULL, NULL },
278
279         {"View/Decode",                              NULL, N_("Decode"), NULL, NULL, NULL }, /* set_decode_cb */
280         {"View/Decode/---",                          NULL, "---", NULL, NULL, NULL },
281
282 #define DEC_ACTION(cs_type,c_type,string) \
283         { "View/Decode/" cs_type, NULL, N_(string), NULL, NULL, c_type }
284
285         {"View/---",                                 NULL, "---", NULL, NULL, NULL },
286         {"View/MessageSource",                       NULL, N_("Mess_age source"), "<control>U", NULL, G_CALLBACK(view_source_cb) },
287         {"View/Part",                                NULL, N_("Message part"), NULL, NULL, NULL },
288         {"View/Part/AsText",                         NULL, N_("View as text"), "T", NULL, G_CALLBACK(view_part_as_text_cb) },
289         {"View/Part/Open",                           NULL, N_("Open"), "L", NULL, G_CALLBACK(open_part_cb) },
290 #ifndef G_OS_WIN32
291         {"View/Part/OpenWith",                       NULL, N_("Open with..."), "O", NULL, G_CALLBACK(open_part_with_cb) },
292 #endif
293
294         {"View/Quotes",                              NULL, N_("Quotes"), NULL, NULL, NULL }, 
295
296 /* Message menu */
297         {"Message/Compose",                          NULL, N_("Compose _new message"), "<control>M", NULL, G_CALLBACK(compose_cb) },
298         {"Message/---",                              NULL, "---", NULL, NULL, NULL },
299
300         {"Message/Reply",                            NULL, N_("_Reply"), "<control>R", NULL, G_CALLBACK(reply_cb) }, /* COMPOSE_REPLY */
301         {"Message/ReplyTo",                          NULL, N_("Repl_y to"), NULL, NULL, NULL }, 
302         {"Message/ReplyTo/All",                      NULL, N_("_All"), "<control><shift>R", NULL, G_CALLBACK(reply_cb) }, /* COMPOSE_REPLY_TO_ALL */
303         {"Message/ReplyTo/Sender",                   NULL, N_("_Sender"), NULL, NULL, G_CALLBACK(reply_cb) }, /* COMPOSE_REPLY_TO_SENDER */
304         {"Message/ReplyTo/List",                     NULL, N_("Mailing _list"), "<control>L", NULL, G_CALLBACK(reply_cb) }, /* COMPOSE_REPLY_TO_LIST */
305         /* {"Message/---",                           NULL, "---", NULL, NULL, NULL }, */
306
307         {"Message/Forward",                          NULL, N_("_Forward"), "<control><alt>F", NULL, G_CALLBACK(reply_cb) }, /* COMPOSE_FORWARD_INLINE */
308         {"Message/ForwardAtt",                       NULL, N_("For_ward as attachment"), NULL, NULL, G_CALLBACK(reply_cb) }, /* COMPOSE_FORWARD_AS_ATTACH */
309         {"Message/Redirect",                         NULL, N_("Redirec_t"), NULL, NULL, G_CALLBACK(reply_cb) }, /* COMPOSE_REDIRECT */
310         {"Message/CheckSignature",                   NULL, N_("Check signature"), "C", NULL, G_CALLBACK(check_signature_cb) },
311
312 /* Tools menu */        
313         {"Tools/AddressBook",                        NULL, N_("_Address book"), "<control><shift>A", NULL, G_CALLBACK(addressbook_open_cb) }, 
314         {"Tools/AddSenderToAB",                      NULL, N_("Add sender to address boo_k"), NULL, NULL, G_CALLBACK(add_address_cb) }, 
315         {"Tools/---",                                NULL, "---", NULL, NULL, NULL },
316
317         {"Tools/CreateFilterRule",                   NULL, N_("_Create filter rule"), NULL, NULL, NULL },
318         {"Tools/CreateFilterRule/Automatically",     NULL, N_("_Automatically"), NULL, NULL, G_CALLBACK(create_filter_cb) }, /* FILTER_BY_AUTO */
319         {"Tools/CreateFilterRule/ByFrom",            NULL, N_("By _From"), NULL, NULL, G_CALLBACK(create_filter_cb) }, /* FILTER_BY_FROM */
320         {"Tools/CreateFilterRule/ByTo",              NULL, N_("By _To"), NULL, NULL, G_CALLBACK(create_filter_cb) }, /* FILTER_BY_TO     */
321         {"Tools/CreateFilterRule/BySubject",         NULL, N_("By _Subject"), NULL, NULL, G_CALLBACK(create_filter_cb) }, /* FILTER_BY_SUBJECT */
322
323         {"Tools/CreateProcessingRule",               NULL, N_("Create processing rule"), NULL, NULL, NULL },
324         {"Tools/CreateProcessingRule/Automatically", NULL, N_("_Automatically"), NULL, NULL, G_CALLBACK(create_processing_cb) }, 
325         {"Tools/CreateProcessingRule/ByFrom",        NULL, N_("By _From"), NULL, NULL, G_CALLBACK(create_processing_cb) }, 
326         {"Tools/CreateProcessingRule/ByTo",          NULL, N_("By _To"), NULL, NULL, G_CALLBACK(create_processing_cb) }, 
327         {"Tools/CreateProcessingRule/BySubject",     NULL, N_("By _Subject"), NULL, NULL, G_CALLBACK(create_processing_cb) }, 
328         /* {"Tools/---",                             NULL, "---", NULL, NULL, NULL }, */
329
330         {"Tools/ListUrls",                           NULL, N_("List _URLs..."), "<control><shift>U", NULL, G_CALLBACK(open_urls_cb) }, 
331
332         /* {"Tools/---",                             NULL, "---", NULL, NULL, NULL }, */
333         {"Tools/Actions",                            NULL, N_("Actio_ns"), NULL, NULL, NULL },
334         {"Tools/Actions/PlaceHolder",                NULL, "Placeholder", NULL, NULL, G_CALLBACK(messageview_nothing_cb) },
335
336 /* Help menu */
337         {"Help/About",                               NULL, N_("_About"), NULL, NULL, G_CALLBACK(about_cb) }, 
338 };
339
340 static GtkToggleActionEntry msgview_toggle_entries[] =
341 {
342         {"View/AllHeaders",         NULL, N_("Show all _headers"), "<control>H", NULL, G_CALLBACK(show_all_header_cb), FALSE }, /* toggle */
343         {"View/Quotes/CollapseAll", NULL, N_("_Collapse all"), "<control><shift>Q", NULL, G_CALLBACK(msg_hide_quotes_cb), FALSE }, /* 1 toggle */
344         {"View/Quotes/Collapse2",   NULL, N_("Collapse from level _2"), NULL, NULL, G_CALLBACK(msg_hide_quotes_cb), FALSE }, /* 2 toggle */
345         {"View/Quotes/Collapse3",   NULL, N_("Collapse from level _3"), NULL, NULL, G_CALLBACK(msg_hide_quotes_cb), FALSE }, /* 3 toggle */
346 };
347
348 static GtkRadioActionEntry msgview_radio_enc_entries[] =
349 {
350         ENC_ACTION(CS_AUTO, C_AUTO, N_("_Automatic")), /* RADIO set_charset_cb */
351         ENC_ACTION(CS_US_ASCII, C_US_ASCII, N_("7bit ASCII (US-ASC_II)")), /* RADIO set_charset_cb */
352         ENC_ACTION(CS_UTF_8, C_UTF_8, N_("Unicode (_UTF-8)")), /* RADIO set_charset_cb */
353         ENC_ACTION("Western/"CS_ISO_8859_1, C_ISO_8859_1, "ISO-8859-_1"), /* RADIO set_charset_cb */
354         ENC_ACTION("Western/"CS_ISO_8859_15, C_ISO_8859_15, "ISO-8859-15"), /* RADIO set_charset_cb */
355         ENC_ACTION("Western/"CS_WINDOWS_1252, C_WINDOWS_1252, "Windows-1252"), /* RADIO set_charset_cb */
356         ENC_ACTION(CS_ISO_8859_2, C_ISO_8859_2, N_("Central European (ISO-8859-_2)")), /* RADIO set_charset_cb */
357         ENC_ACTION("Baltic/"CS_ISO_8859_13, C_ISO_8859_13, "ISO-8859-13"), /* RADIO set_charset_cb */
358         ENC_ACTION("Baltic/"CS_ISO_8859_4, C_ISO_8859_14, "ISO-8859-_4"), /* RADIO set_charset_cb */
359         ENC_ACTION(CS_ISO_8859_7, C_ISO_8859_7, N_("Greek (ISO-8859-_7)")), /* RADIO set_charset_cb */
360         ENC_ACTION("Hebrew/"CS_ISO_8859_8, C_ISO_8859_8, "ISO-8859-_8"), /* RADIO set_charset_cb */
361         ENC_ACTION("Hebrew/"CS_WINDOWS_1255, C_WINDOWS_1255, "Windows-1255"), /* RADIO set_charset_cb */
362         ENC_ACTION("Arabic/"CS_ISO_8859_6, C_ISO_8859_6, "ISO-8859-_6"), /* RADIO set_charset_cb */
363         ENC_ACTION("Arabic/"CS_WINDOWS_1256, C_WINDOWS_1256, "Windows-1256"), /* RADIO set_charset_cb */
364         ENC_ACTION(CS_ISO_8859_9, C_ISO_8859_9, N_("Turkish (ISO-8859-_9)")), /* RADIO set_charset_cb */
365         ENC_ACTION("Cyrillic/"CS_ISO_8859_5, C_ISO_8859_5, "ISO-8859-_5"), /* RADIO set_charset_cb */
366         ENC_ACTION("Cyrillic/"CS_KOI8_R, C_KOI8_R, "KOI8-_R"), /* RADIO set_charset_cb */
367         ENC_ACTION("Cyrillic/"CS_MACCYR, C_MACCYR, "MAC_CYR"), /* RADIO set_charset_cb */
368         ENC_ACTION("Cyrillic/"CS_KOI8_U, C_KOI8_U, "KOI8-_U"), /* RADIO set_charset_cb */
369         ENC_ACTION("Cyrillic/"CS_WINDOWS_1251, C_WINDOWS_1251, "Windows-1251"), /* RADIO set_charset_cb */
370         ENC_ACTION("Japanese/"CS_ISO_2022_JP, C_ISO_2022_JP, "ISO-2022-_JP"), /* RADIO set_charset_cb */
371         ENC_ACTION("Japanese/"CS_ISO_2022_JP_2, C_ISO_2022_JP_2, "ISO-2022-JP-_2"), /* RADIO set_charset_cb */
372         ENC_ACTION("Japanese/"CS_EUC_JP, C_EUC_JP, "_EUC-JP"), /* RADIO set_charset_cb */
373         ENC_ACTION("Japanese/"CS_SHIFT_JIS, C_SHIFT_JIS, "_Shift-JIS"), /* RADIO set_charset_cb */
374         ENC_ACTION("Chinese/"CS_GB18030, C_GB18030, "_GB18030"), /* RADIO set_charset_cb */
375         ENC_ACTION("Chinese/"CS_GB2312, C_GB2312, "_GB2312"), /* RADIO set_charset_cb */
376         ENC_ACTION("Chinese/"CS_GBK, C_GBK, "GB_K"), /* RADIO set_charset_cb */
377         ENC_ACTION("Chinese/"CS_BIG5, C_BIG5, "_Big5-JP"), /* RADIO set_charset_cb */
378         ENC_ACTION("Chinese/"CS_EUC_TW, C_EUC_TW, "EUC-_TW"), /* RADIO set_charset_cb */
379         ENC_ACTION("Korean/"CS_EUC_KR, C_EUC_KR, "_EUC-KR"), /* RADIO set_charset_cb */
380         ENC_ACTION("Korean/"CS_ISO_2022_KR, C_ISO_2022_KR, "_ISO-2022-KR"), /* RADIO set_charset_cb */
381         ENC_ACTION("Thai/"CS_TIS_620, C_TIS_620, "_TIS-620-KR"), /* RADIO set_charset_cb */
382         ENC_ACTION("Thai/"CS_WINDOWS_874, C_WINDOWS_874, "_Windows-874"), /* RADIO set_charset_cb */
383 };
384
385 static GtkRadioActionEntry msgview_radio_dec_entries[] =
386 {
387         DEC_ACTION("AutoDetect", 0, N_("_Auto detect")),        /* set_decode_cb */
388         /* --- */
389         DEC_ACTION("8bit", ENC_8BIT, "_8bit"),
390         DEC_ACTION("QP", ENC_QUOTED_PRINTABLE, "_Quoted printable"),
391         DEC_ACTION("B64", ENC_BASE64, "_Base64"),
392         DEC_ACTION("Uuencode", ENC_X_UUENCODE, "_Uuencode"),
393 };
394
395 MessageView *messageview_create(MainWindow *mainwin)
396 {
397         MessageView *messageview;
398         GtkWidget *vbox;
399         HeaderView *headerview;
400         MimeView *mimeview;
401         NoticeView *noticeview;
402
403         debug_print("Creating message view...\n");
404         messageview = g_new0(MessageView, 1);
405
406         headerview = headerview_create();
407
408         noticeview = noticeview_create(mainwin);
409
410         mimeview = mimeview_create(mainwin);
411         mimeview->textview = textview_create();
412         mimeview->textview->messageview = messageview;
413         mimeview->messageview = messageview;
414
415         vbox = gtk_vbox_new(FALSE, 0);
416         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(headerview),
417                            FALSE, FALSE, 0);
418         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(noticeview),
419                            FALSE, FALSE, 0);
420         gtk_box_pack_start(GTK_BOX(vbox),
421                            GTK_WIDGET_PTR(mimeview), TRUE, TRUE, 0);
422         gtk_widget_show(vbox);
423
424         messageview->vbox        = vbox;
425         messageview->new_window  = FALSE;
426         messageview->window      = NULL;
427         messageview->headerview  = headerview;
428         messageview->mimeview    = mimeview;
429         messageview->noticeview = noticeview;
430         messageview->mainwin    = mainwin;
431
432         messageview->statusbar     = NULL;
433         messageview->statusbar_cid = 0;
434
435         messageview->show_full_text= FALSE;
436         messageview->update_needed = FALSE;
437
438         messageview->msginfo_update_callback_id =
439                 hooks_register_hook(MSGINFO_UPDATE_HOOKLIST, messageview_update_msg, (gpointer) messageview);
440
441         return messageview;
442 }
443
444 const GList *messageview_get_msgview_list(void)
445 {
446         return msgview_list;
447 }
448
449 void messageview_update_actions_menu(MessageView *msgview)
450 {
451         /* Messages opened in a new window do not have a menu bar */
452         if (msgview->menubar == NULL)
453                 return;
454         action_update_msgview_menu(msgview->ui_manager, "/Menu/Tools/Actions", msgview);
455 }
456
457 static void messageview_add_toolbar(MessageView *msgview, GtkWidget *window) 
458 {
459         GtkWidget *handlebox;
460         GtkWidget *vbox;
461         GtkWidget *menubar;
462 #ifndef GENERIC_UMPC
463         GtkWidget *statusbar = NULL;
464 #endif
465         GtkActionGroup *action_group;
466
467
468         vbox = gtk_vbox_new(FALSE, 0);
469         gtk_widget_show(vbox);
470         gtk_container_add(GTK_CONTAINER(window), vbox); 
471
472         msgview->ui_manager = gtk_ui_manager_new();
473         action_group = cm_menu_create_action_group_full(msgview->ui_manager,"Menu", msgview_entries,
474                         G_N_ELEMENTS(msgview_entries), (gpointer)msgview);
475         gtk_action_group_add_toggle_actions(action_group, msgview_toggle_entries,
476                         G_N_ELEMENTS(msgview_toggle_entries), (gpointer)msgview);
477         gtk_action_group_add_radio_actions(action_group, msgview_radio_enc_entries,
478                         G_N_ELEMENTS(msgview_radio_enc_entries), C_AUTO, G_CALLBACK(set_charset_cb), (gpointer)msgview);
479         gtk_action_group_add_radio_actions(action_group, msgview_radio_dec_entries,
480                         G_N_ELEMENTS(msgview_radio_dec_entries), C_AUTO, G_CALLBACK(set_decode_cb), (gpointer)msgview);
481
482         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/", "Menu", NULL, GTK_UI_MANAGER_MENUBAR)
483         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu", "File", "File", GTK_UI_MANAGER_MENU)
484         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu", "Edit", "Edit", GTK_UI_MANAGER_MENU)
485         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu", "View", "View", GTK_UI_MANAGER_MENU)
486         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu", "Message", "Message", GTK_UI_MANAGER_MENU)
487         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu", "Tools", "Tools", GTK_UI_MANAGER_MENU)
488         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu", "Help", "Help", GTK_UI_MANAGER_MENU)
489
490 /* File menu */
491         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/File", "SaveAs", "File/SaveAs", GTK_UI_MANAGER_MENUITEM)
492         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/File", "SavePartAs", "File/SavePartAs", GTK_UI_MANAGER_MENUITEM)
493         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/File", "Separator1", "File/---", GTK_UI_MANAGER_SEPARATOR)
494         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/File", "PageSetup", "File/PageSetup", GTK_UI_MANAGER_MENUITEM)
495         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/File", "Print", "File/Print", GTK_UI_MANAGER_MENUITEM)
496         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/File", "Separator2", "File/---", GTK_UI_MANAGER_SEPARATOR)
497         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/File", "Close", "File/Close", GTK_UI_MANAGER_MENUITEM)
498
499 /* Edit menu */
500         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Edit", "Copy", "Edit/Copy", GTK_UI_MANAGER_MENUITEM)
501         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Edit", "SelectAll", "Edit/SelectAll", GTK_UI_MANAGER_MENUITEM)
502         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Edit", "Separator1", "Edit/---", GTK_UI_MANAGER_SEPARATOR)
503         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Edit", "Find", "Edit/Find", GTK_UI_MANAGER_MENUITEM)
504
505 /* View menu */
506         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View", "Goto", "View/Goto", GTK_UI_MANAGER_MENU)
507         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "Prev", "View/Goto/Prev", GTK_UI_MANAGER_MENUITEM)
508         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "Next", "View/Goto/Next", GTK_UI_MANAGER_MENUITEM)
509         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "Separator1", "View/Goto/---", GTK_UI_MANAGER_SEPARATOR)
510         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "PrevUnread", "View/Goto/PrevUnread", GTK_UI_MANAGER_MENUITEM)
511         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "NextUnread", "View/Goto/NextUnread", GTK_UI_MANAGER_MENUITEM)
512         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "Separator2", "View/Goto/---", GTK_UI_MANAGER_SEPARATOR)
513         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "PrevNew", "View/Goto/PrevNew", GTK_UI_MANAGER_MENUITEM)
514         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "NextNew", "View/Goto/NextNew", GTK_UI_MANAGER_MENUITEM)
515         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "Separator3", "View/Goto/---", GTK_UI_MANAGER_SEPARATOR)
516         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "PrevMarked", "View/Goto/PrevMarked", GTK_UI_MANAGER_MENUITEM)
517         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "NextMarked", "View/Goto/NextMarked", GTK_UI_MANAGER_MENUITEM)
518         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "Separator4", "View/Goto/---", GTK_UI_MANAGER_SEPARATOR)
519         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "PrevLabeled", "View/Goto/PrevLabeled", GTK_UI_MANAGER_MENUITEM)
520         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "NextLabeled", "View/Goto/NextLabeled", GTK_UI_MANAGER_MENUITEM)
521         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "Separator5", "View/Goto/---", GTK_UI_MANAGER_SEPARATOR)
522         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "PrevHistory", "View/Goto/PrevHistory", GTK_UI_MANAGER_MENUITEM)
523         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "NextHistory", "View/Goto/NextHistory", GTK_UI_MANAGER_MENUITEM)
524         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "Separator6", "View/Goto/---", GTK_UI_MANAGER_SEPARATOR)
525         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "ParentMessage", "View/Goto/ParentMessage", GTK_UI_MANAGER_MENUITEM)
526         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "Separator7", "View/Goto/---", GTK_UI_MANAGER_SEPARATOR)
527         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "NextUnreadFolder", "View/Goto/NextUnreadFolder", GTK_UI_MANAGER_MENUITEM)
528         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "Folder", "View/Goto/Folder", GTK_UI_MANAGER_MENUITEM)
529         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "Separator8", "View/Goto/---", GTK_UI_MANAGER_SEPARATOR)
530         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "NextPart", "View/Goto/NextPart", GTK_UI_MANAGER_MENUITEM)
531         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "PrevPart", "View/Goto/PrevPart", GTK_UI_MANAGER_MENUITEM)
532
533         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View", "Scroll", "View/Scroll", GTK_UI_MANAGER_MENU)
534         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Scroll", "PrevLine", "View/Scroll/PrevLine", GTK_UI_MANAGER_MENUITEM)
535         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Scroll", "NextLine", "View/Scroll/NextLine", GTK_UI_MANAGER_MENUITEM)
536         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Scroll", "PrevPage", "View/Scroll/PrevPage", GTK_UI_MANAGER_MENUITEM)
537         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Scroll", "NextPage", "View/Scroll/NextPage", GTK_UI_MANAGER_MENUITEM)
538
539         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View", "Separator1", "View/---", GTK_UI_MANAGER_SEPARATOR)
540
541         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View", "Encoding", "View/Encoding", GTK_UI_MANAGER_MENU)
542         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding", CS_AUTO, "View/Encoding/"CS_AUTO, GTK_UI_MANAGER_MENUITEM)
543         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding", "Separator1", "View/Encoding/---", GTK_UI_MANAGER_SEPARATOR)
544         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding", CS_US_ASCII, "View/Encoding/"CS_US_ASCII, GTK_UI_MANAGER_MENUITEM)
545         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding", CS_UTF_8, "View/Encoding/"CS_UTF_8, GTK_UI_MANAGER_MENUITEM)
546         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding", "Separator2", "View/Encoding/---", GTK_UI_MANAGER_SEPARATOR)
547
548         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding", "Western", "View/Encoding/Western", GTK_UI_MANAGER_MENU)
549         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Western", CS_ISO_8859_1, "View/Encoding/Western/"CS_ISO_8859_1, GTK_UI_MANAGER_MENUITEM)
550         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Western", CS_ISO_8859_15, "View/Encoding/Western/"CS_ISO_8859_15, GTK_UI_MANAGER_MENUITEM)
551         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Western", CS_WINDOWS_1252, "View/Encoding/Western/"CS_WINDOWS_1252, GTK_UI_MANAGER_MENUITEM)
552
553         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding", CS_ISO_8859_2, "View/Encoding/"CS_ISO_8859_2, GTK_UI_MANAGER_MENUITEM)
554
555         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding", "Baltic", "View/Encoding/Baltic", GTK_UI_MANAGER_MENU)
556         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Baltic", CS_ISO_8859_13, "View/Encoding/Baltic/"CS_ISO_8859_13, GTK_UI_MANAGER_MENUITEM)
557         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Baltic", CS_ISO_8859_4, "View/Encoding/Baltic/"CS_ISO_8859_4, GTK_UI_MANAGER_MENUITEM)
558
559         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding", CS_ISO_8859_7, "View/Encoding/"CS_ISO_8859_7, GTK_UI_MANAGER_MENUITEM)
560
561         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding", "Hebrew", "View/Encoding/Hebrew", GTK_UI_MANAGER_MENU)
562         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Hebrew", CS_ISO_8859_8, "View/Encoding/Hebrew/"CS_ISO_8859_8, GTK_UI_MANAGER_MENUITEM)
563         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Hebrew", CS_WINDOWS_1255, "View/Encoding/Hebrew/"CS_WINDOWS_1255, GTK_UI_MANAGER_MENUITEM)
564
565         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding", "Arabic", "View/Encoding/Arabic", GTK_UI_MANAGER_MENU)
566         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Arabic", CS_ISO_8859_6, "View/Encoding/Arabic/"CS_ISO_8859_6, GTK_UI_MANAGER_MENUITEM)
567         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Arabic", CS_WINDOWS_1256, "View/Encoding/Arabic/"CS_WINDOWS_1256, GTK_UI_MANAGER_MENUITEM)
568
569         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding", CS_ISO_8859_9, "View/Encoding/"CS_ISO_8859_9, GTK_UI_MANAGER_MENUITEM)
570
571         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding", "Cyrillic", "View/Encoding/Cyrillic", GTK_UI_MANAGER_MENU)
572         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Cyrillic", CS_ISO_8859_5, "View/Encoding/Cyrillic/"CS_ISO_8859_5, GTK_UI_MANAGER_MENUITEM)
573         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Cyrillic", CS_KOI8_R, "View/Encoding/Cyrillic/"CS_KOI8_R, GTK_UI_MANAGER_MENUITEM)
574         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Cyrillic", CS_MACCYR, "View/Encoding/Cyrillic/"CS_MACCYR, GTK_UI_MANAGER_MENUITEM)
575         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Cyrillic", CS_KOI8_U, "View/Encoding/Cyrillic/"CS_KOI8_U, GTK_UI_MANAGER_MENUITEM)
576         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Cyrillic", CS_WINDOWS_1251, "View/Encoding/Cyrillic/"CS_WINDOWS_1251, GTK_UI_MANAGER_MENUITEM)
577
578         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding", "Japanese", "View/Encoding/Japanese", GTK_UI_MANAGER_MENU)
579         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Japanese", CS_ISO_2022_JP, "View/Encoding/Japanese/"CS_ISO_2022_JP, GTK_UI_MANAGER_MENUITEM)
580         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Japanese", CS_ISO_2022_JP_2, "View/Encoding/Japanese/"CS_ISO_2022_JP_2, GTK_UI_MANAGER_MENUITEM)
581         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Japanese", CS_EUC_JP, "View/Encoding/Japanese/"CS_EUC_JP, GTK_UI_MANAGER_MENUITEM)
582         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Japanese", CS_SHIFT_JIS, "View/Encoding/Japanese/"CS_SHIFT_JIS, GTK_UI_MANAGER_MENUITEM)
583
584         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding", "Chinese", "View/Encoding/Chinese", GTK_UI_MANAGER_MENU)
585         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Chinese", CS_GB18030, "View/Encoding/Chinese/"CS_GB18030, GTK_UI_MANAGER_MENUITEM)
586         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Chinese", CS_GB2312, "View/Encoding/Chinese/"CS_GB2312, GTK_UI_MANAGER_MENUITEM)
587         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Chinese", CS_GBK, "View/Encoding/Chinese/"CS_GBK, GTK_UI_MANAGER_MENUITEM)
588         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Chinese", CS_BIG5, "View/Encoding/Chinese/"CS_BIG5, GTK_UI_MANAGER_MENUITEM)
589         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Chinese", CS_EUC_TW, "View/Encoding/Chinese/"CS_EUC_TW, GTK_UI_MANAGER_MENUITEM)
590
591         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding", "Korean", "View/Encoding/Korean", GTK_UI_MANAGER_MENU)
592         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Korean", CS_EUC_KR, "View/Encoding/Korean/"CS_EUC_KR, GTK_UI_MANAGER_MENUITEM)
593         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Korean", CS_ISO_2022_KR, "View/Encoding/Korean/"CS_ISO_2022_KR, GTK_UI_MANAGER_MENUITEM)
594
595         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding", "Thai", "View/Encoding/Thai", GTK_UI_MANAGER_MENU)
596         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Thai", CS_TIS_620, "View/Encoding/Thai/"CS_TIS_620, GTK_UI_MANAGER_MENUITEM)
597         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Thai", CS_WINDOWS_874, "View/Encoding/Thai/"CS_WINDOWS_874, GTK_UI_MANAGER_MENUITEM)
598
599         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View", "Decode", "View/Decode", GTK_UI_MANAGER_MENU)
600         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Decode", "AutoDetect", "View/Decode/AutoDetect", GTK_UI_MANAGER_MENUITEM)
601         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Decode", "Separator1", "View/Decode/---", GTK_UI_MANAGER_SEPARATOR)
602         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Decode", "8bit", "View/Decode/8bit", GTK_UI_MANAGER_MENUITEM)
603         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Decode", "QP", "View/Decode/QP", GTK_UI_MANAGER_MENUITEM)
604         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Decode", "B64", "View/Decode/B64", GTK_UI_MANAGER_MENUITEM)
605         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Decode", "Uuencode", "View/Decode/Uuencode", GTK_UI_MANAGER_MENUITEM)
606
607         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View", "Separator2", "View/---", GTK_UI_MANAGER_SEPARATOR)
608
609         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View", "MessageSource", "View/MessageSource", GTK_UI_MANAGER_MENUITEM)
610         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View", "AllHeaders", "View/AllHeaders", GTK_UI_MANAGER_MENUITEM)
611         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View", "Quotes", "View/Quotes", GTK_UI_MANAGER_MENU)
612         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Quotes", "CollapseAll", "View/Quotes/CollapseAll", GTK_UI_MANAGER_MENUITEM)
613         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Quotes", "Collapse2", "View/Quotes/Collapse2", GTK_UI_MANAGER_MENUITEM)
614         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Quotes", "Collapse3", "View/Quotes/Collapse3", GTK_UI_MANAGER_MENUITEM)
615         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View", "Part", "View/Part", GTK_UI_MANAGER_MENU)
616         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Part", "AsText", "View/Part/AsText", GTK_UI_MANAGER_MENUITEM)
617         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Part", "Open", "View/Part/Open", GTK_UI_MANAGER_MENUITEM)
618 #ifndef G_OS_WIN32
619         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Part", "OpenWith", "View/Part/OpenWith", GTK_UI_MANAGER_MENUITEM)
620 #endif
621
622 /* Message menu */
623         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Message", "Compose", "Message/Compose", GTK_UI_MANAGER_MENUITEM)
624         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Message", "Separator1", "Message/---", GTK_UI_MANAGER_SEPARATOR)
625         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Message", "Reply", "Message/Reply", GTK_UI_MANAGER_MENUITEM)
626         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Message", "ReplyTo", "Message/ReplyTo", GTK_UI_MANAGER_MENU)
627         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Message/ReplyTo", "All", "Message/ReplyTo/All", GTK_UI_MANAGER_MENUITEM)
628         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Message/ReplyTo", "Sender", "Message/ReplyTo/Sender", GTK_UI_MANAGER_MENUITEM)
629         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Message/ReplyTo", "List", "Message/ReplyTo/List", GTK_UI_MANAGER_MENUITEM)
630         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Message", "Separator2", "Message/---", GTK_UI_MANAGER_SEPARATOR)
631         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Message", "Forward", "Message/Forward", GTK_UI_MANAGER_MENUITEM)
632         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Message", "ForwardAtt", "Message/ForwardAtt", GTK_UI_MANAGER_MENUITEM)
633         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Message", "Redirect", "Message/Redirect", GTK_UI_MANAGER_MENUITEM)
634         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Message", "CheckSignature", "Message/CheckSignature", GTK_UI_MANAGER_MENUITEM)
635
636 /* Tools menu */
637         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Tools", "AddressBook", "Tools/AddressBook", GTK_UI_MANAGER_MENUITEM)
638         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Tools", "AddSenderToAB", "Tools/AddSenderToAB", GTK_UI_MANAGER_MENUITEM)
639         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Tools", "Separator1", "Tools/---", GTK_UI_MANAGER_SEPARATOR)
640
641         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Tools", "CreateFilterRule", "Tools/CreateFilterRule", GTK_UI_MANAGER_MENU)
642         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Tools/CreateFilterRule", "Automatically", "Tools/CreateFilterRule/Automatically", GTK_UI_MANAGER_MENUITEM)
643         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Tools/CreateFilterRule", "ByFrom", "Tools/CreateFilterRule/ByFrom", GTK_UI_MANAGER_MENUITEM)
644         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Tools/CreateFilterRule", "ByTo", "Tools/CreateFilterRule/ByTo", GTK_UI_MANAGER_MENUITEM)
645         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Tools/CreateFilterRule", "BySubject", "Tools/CreateFilterRule/BySubject", GTK_UI_MANAGER_MENUITEM)
646
647         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Tools", "CreateProcessingRule", "Tools/CreateProcessingRule", GTK_UI_MANAGER_MENU)
648         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Tools/CreateProcessingRule", "Automatically", "Tools/CreateProcessingRule/Automatically", GTK_UI_MANAGER_MENUITEM)
649         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Tools/CreateProcessingRule", "ByFrom", "Tools/CreateProcessingRule/ByFrom", GTK_UI_MANAGER_MENUITEM)
650         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Tools/CreateProcessingRule", "ByTo", "Tools/CreateProcessingRule/ByTo", GTK_UI_MANAGER_MENUITEM)
651         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Tools/CreateProcessingRule", "BySubject", "Tools/CreateProcessingRule/BySubject", GTK_UI_MANAGER_MENUITEM)
652         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Tools", "Separator2", "Tools/---", GTK_UI_MANAGER_SEPARATOR)
653         
654         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Tools", "ListUrls", "Tools/ListUrls", GTK_UI_MANAGER_MENUITEM)
655         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Tools", "Separator3", "Tools/---", GTK_UI_MANAGER_SEPARATOR)
656
657         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Tools", "Actions", "Tools/Actions", GTK_UI_MANAGER_MENU)
658         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Tools/Actions", "PlaceHolder", "Tools/Actions/PlaceHolder", GTK_UI_MANAGER_MENUITEM)
659
660 /* Help menu */
661         MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/Help", "About", "Help/About", GTK_UI_MANAGER_MENUITEM)
662
663         menubar = gtk_ui_manager_get_widget(msgview->ui_manager, "/Menu");
664         gtk_widget_show_all(menubar);
665         gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(msgview->ui_manager));
666
667         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
668
669         cm_toggle_menu_set_active_full(msgview->ui_manager, "Menu/View/AllHeaders",
670                                         prefs_common.show_all_headers);
671
672         if (prefs_common.toolbar_detachable) {
673                 handlebox = gtk_handle_box_new();
674         } else {
675                 handlebox = gtk_hbox_new(FALSE, 0);
676         }
677         gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
678         gtk_widget_realize(handlebox);
679         msgview->toolbar = toolbar_create(TOOLBAR_MSGVIEW, handlebox,
680                                           (gpointer)msgview);
681 #ifndef GENERIC_UMPC
682         statusbar = gtk_statusbar_new();
683         gtk_widget_show(statusbar);
684         gtk_box_pack_end(GTK_BOX(vbox), statusbar, FALSE, FALSE, 0);
685         msgview->statusbar = statusbar;
686         msgview->statusbar_cid = gtk_statusbar_get_context_id
687                 (GTK_STATUSBAR(statusbar), "Message View");
688 #else
689         msgview->statusbar = NULL;
690         msgview->statusbar_cid = 0;
691 #endif
692
693
694         msgview->handlebox = handlebox;
695         msgview->menubar   = menubar;
696
697         gtk_container_add(GTK_CONTAINER(vbox),
698                           GTK_WIDGET_PTR(msgview));
699
700         messageview_update_actions_menu(msgview);
701
702         msgview_list = g_list_append(msgview_list, msgview);
703 }
704
705 static MessageView *messageview_create_with_new_window_visible(MainWindow *mainwin, gboolean show)
706 {
707         MessageView *msgview;
708         GtkWidget *window;
709         static GdkGeometry geometry;
710
711         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "messageview");
712         gtk_window_set_title(GTK_WINDOW(window), _("Claws Mail - Message View"));
713         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
714
715         if (!geometry.min_height) {
716                 geometry.min_width = 320;
717                 geometry.min_height = 200;
718         }
719         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
720                                       GDK_HINT_MIN_SIZE);
721
722         gtk_widget_set_size_request(window, prefs_common.msgwin_width,
723                                     prefs_common.msgwin_height);
724 #ifdef G_OS_WIN32
725         gtk_window_move(GTK_WINDOW(window), 48, 48);
726 #endif
727
728         msgview = messageview_create(mainwin);
729
730         g_signal_connect(G_OBJECT(window), "size_allocate",
731                          G_CALLBACK(messageview_size_allocate_cb),
732                          msgview);
733         g_signal_connect(G_OBJECT(window), "delete_event",
734                          G_CALLBACK(messageview_delete_cb), msgview);
735         g_signal_connect(G_OBJECT(window), "key_press_event",
736                          G_CALLBACK(key_pressed), msgview);
737         messageview_add_toolbar(msgview, window);
738
739         if (show) {
740                 gtk_widget_show(window);
741         } else {
742                 gtk_widget_realize(window);
743         }
744
745         msgview->new_window = TRUE;
746         msgview->window = window;
747         msgview->visible = TRUE;
748
749         toolbar_set_style(msgview->toolbar->toolbar, msgview->handlebox, 
750                           prefs_common.toolbar_style);
751         messageview_init(msgview);
752
753         if (show) {
754                 GTK_EVENTS_FLUSH();
755                 gtk_widget_grab_focus(msgview->mimeview->textview->text);
756         }
757
758         return msgview;
759 }
760
761 MessageView *messageview_create_with_new_window(MainWindow *mainwin)
762 {
763         return messageview_create_with_new_window_visible(mainwin, TRUE);
764 }
765 void messageview_init(MessageView *messageview)
766 {
767         headerview_init(messageview->headerview);
768         mimeview_init(messageview->mimeview);
769         /*messageview_set_font(messageview);*/
770
771         noticeview_hide(messageview->noticeview);
772 }
773
774 static void notification_convert_header(gchar **dest,
775                                         const gchar *src_,
776                                         gint header_len)
777 {
778         char *src;
779
780         cm_return_if_fail(src_ != NULL);
781
782         if (header_len < 1) {
783                 *dest = g_strdup("");
784                 return;
785         }
786
787         Xstrndup_a(src, src_, strlen(src_), return);
788
789         remove_return(src);
790
791         if (is_ascii_str(src)) {
792                 *dest = g_strdup(src);
793                 return;
794         } else {
795                 *dest = g_malloc(BUFFSIZE);
796                 conv_encode_header(*dest, BUFFSIZE, src, header_len, FALSE);
797         }
798 }
799
800 static gint disposition_notification_send(MsgInfo *msginfo)
801 {
802         gchar *buf = NULL;
803         gchar tmp[MAXPATHLEN + 1];
804         FILE *fp;
805         GList *ac_list;
806         PrefsAccount *account = NULL;
807         gint ok;
808         gchar *to;
809         FolderItem *queue, *outbox;
810         gint num;
811         gchar *path;
812         gchar *addr;
813         gchar *addrp;
814         gchar *foo = NULL;
815         gboolean queued_removed = FALSE;
816         gchar *boundary = NULL;
817         gchar buf_date[RFC822_DATE_BUFFSIZE];
818         gchar *date = NULL;
819         gchar *orig_to = NULL;
820         gchar *enc_sub = NULL;
821
822         if (!msginfo->extradata)
823                 return -1;
824         if (!msginfo->extradata->returnreceiptto && 
825             !msginfo->extradata->dispositionnotificationto) 
826                 return -1;
827
828         /* RFC2298: Test for Return-Path */
829         if (msginfo->extradata->dispositionnotificationto)
830                 to = msginfo->extradata->dispositionnotificationto;
831         else
832                 to = msginfo->extradata->returnreceiptto;
833
834         ok = procheader_get_header_from_msginfo(msginfo, &buf, "Return-Path:");
835         if (ok == 0) {
836                 gchar *to_addr = g_strdup(to);
837                 extract_address(to_addr);
838                 extract_address(buf);
839                 ok = strcasecmp(to_addr, buf);
840                 g_free(to_addr);
841         } else {
842                 buf = g_strdup(_("<No Return-Path found>"));
843         }
844         
845         if (ok != 0) {
846                 AlertValue val;
847                 gchar *message;
848                 message = g_markup_printf_escaped(
849                   _("The notification address to which the return receipt is\n"
850                     "to be sent does not correspond to the return path:\n"
851                     "Notification address: %s\n"
852                     "Return path: %s\n"
853                     "It is advised to not send the return receipt."),
854                   to, buf);
855                 val = alertpanel_full(_("Warning"), message,
856                                 _("_Don't Send"), _("_Send"), NULL, ALERTFOCUS_FIRST, FALSE,
857                                 NULL, ALERT_WARNING);
858                 g_free(message);                                
859                 if (val != G_ALERTALTERNATE) {
860                         g_free(buf);
861                         return -1;
862                 }
863         }
864         g_free(buf);
865         buf = NULL;
866
867         ac_list = account_find_all_from_address(NULL, msginfo->to);
868         ac_list = account_find_all_from_address(ac_list, msginfo->cc);
869
870         if (ac_list == NULL) {
871                 AlertValue val;
872                 gchar *tr;
873                 gchar *text;
874                 tr = g_strdup(C_("'%s' stands for 'To' then 'Cc'",
875                     "This message is asking for a return receipt notification\n"
876                     "but according to its '%s' and '%s' headers it was not\n"
877                     "officially addressed to you.\n"
878                     "It is advised to not send the return receipt."));
879                 text = g_strdup_printf(tr,
880                   prefs_common_translated_header_name("To"),
881                   prefs_common_translated_header_name("Cc"));
882                 val = alertpanel_full(_("Warning"),
883                   text,
884                   _("_Don't Send"), _("_Send"), NULL, ALERTFOCUS_FIRST, FALSE,
885                   NULL, ALERT_WARNING);
886                 g_free(text);
887                 g_free(tr);
888                 if (val != G_ALERTALTERNATE)
889                         return -1;
890         }
891
892         if (g_list_length(ac_list) > 1) {
893                 if ((account = select_account_from_list(ac_list)) == NULL)
894                         return -1;
895         }
896         else if (ac_list != NULL)
897                 account = (PrefsAccount *) ac_list->data;
898         g_list_free(ac_list);
899
900         if (account == NULL)
901                 account = account_get_default();
902         if (!account || account->protocol == A_NNTP) {
903                 alertpanel_error(_("Account for sending mail is not specified.\n"
904                                    "Please select a mail account before sending."));
905                 return -1;
906         }
907
908         /* write to temporary file */
909         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%p",
910                    get_rc_dir(), G_DIR_SEPARATOR, msginfo);
911
912         if ((fp = g_fopen(tmp, "wb")) == NULL) {
913                 FILE_OP_ERROR(tmp, "fopen");
914                 return -1;
915         }
916
917         /* chmod for security */
918         if (change_file_mode_rw(fp, tmp) < 0) {
919                 FILE_OP_ERROR(tmp, "chmod");
920                 g_warning("can't change file mode");
921         }
922         
923         addr = g_strdup(to);
924         
925         extract_address(addr);
926         addrp = addr;
927         
928         /* write queue headers */
929         ok = fprintf(fp, "AF:\n"
930                     "NF:0\n"
931                     "PS:10\n"
932                     "SRH:1\n"
933                     "SFN:\n"
934                     "DSR:\n"
935                     "MID:\n"
936                     "CFG:\n"
937                     "PT:0\n"
938                     "S:%s\n"
939                     "RQ:\n"
940                     "SSV:%s\n"
941                     "SSH:\n"
942                     "R:<%s>\n", 
943                     account->address,
944                     account->smtp_server?account->smtp_server:"",
945                     addrp);
946
947         g_free(addrp);
948         if (ok < 0)
949                 goto FILE_ERROR;
950         
951         /* check whether we need to save the message */
952         outbox = account_get_special_folder(account, F_OUTBOX); 
953         if (folder_get_default_outbox() == outbox && !prefs_common.savemsg)
954                 outbox = NULL;
955         if (outbox) {
956                 path = folder_item_get_identifier(outbox);
957                 ok = fprintf(fp, "SCF:%s\n", path);
958                 g_free(path);
959                 
960                 if (ok < 0)
961                         goto FILE_ERROR;
962         }               
963
964         if (fprintf(fp, "X-Claws-End-Special-Headers: 1\n") < 0)
965                 goto FILE_ERROR;
966
967         /* Date */
968         get_rfc822_date(buf_date, sizeof(buf_date));
969         if (fprintf(fp, "Date: %s\n", buf_date) < 0)
970                 goto FILE_ERROR;
971
972         /* From */
973         if (account->name && *account->name) {
974                 notification_convert_header(&buf, account->name, strlen("From: "));
975                 if (buf == NULL)
976                         goto FILE_ERROR;
977                 if (fprintf(fp, "From: %s <%s>\n", buf, account->address) < 0) {
978                         g_free(buf);
979                         goto FILE_ERROR;
980                 }
981                 g_free(buf);
982                 buf = NULL;
983         } else
984                 if (fprintf(fp, "From: %s\n", account->address) < 0)
985                         goto FILE_ERROR;
986
987         if (fprintf(fp, "To: %s\n", to) < 0)
988                 goto FILE_ERROR;
989
990         /* Subject */
991         notification_convert_header(&buf, msginfo->subject, strlen("Subject: "));
992         if (buf == NULL)
993                 goto FILE_ERROR;
994         if (fprintf(fp, "Subject: Disposition notification: %s\n", buf) < 0) {
995                 g_free(buf);
996                 goto FILE_ERROR;
997         }
998         g_free(buf);
999         buf = NULL;
1000
1001         /* Message ID */
1002         if (account->gen_msgid) {
1003                 gchar *addr = prefs_account_generate_msgid(account);
1004                 if (fprintf(fp, "Message-ID: <%s>\n", addr) < 0) {
1005                         g_free(addr);
1006                         goto FILE_ERROR;
1007                 }
1008                 g_free(addr);
1009         }
1010
1011         boundary = generate_mime_boundary("DN");
1012         date = g_strdup(buf_date);
1013         if (msginfo->to) {
1014                 orig_to = g_strdup(msginfo->to);
1015                 extract_address(orig_to);
1016         }
1017         if (msginfo->subject && *(msginfo->subject)) {
1018                 enc_sub = g_malloc0(strlen(msginfo->subject)*8);
1019                 qp_encode_line(enc_sub, (const guchar *)msginfo->subject);
1020                 g_strstrip(enc_sub);
1021         }
1022         ok = fprintf(fp,"MIME-Version: 1.0\n"
1023                         "Content-Type: multipart/report; report-type=disposition-notification;\n"
1024                         "  boundary=\"%s\"\n"
1025                         "\n"
1026                         "--%s\n"
1027                         "Content-Type: text/plain; charset=UTF-8\n"
1028                         "Content-Transfer-Encoding: quoted-printable\n"
1029                         "\n"
1030                         "The message sent on: %s\n"
1031                         "To: %s\n"
1032                         "With subject: \"%s\"\n"
1033                         "has been displayed at %s.\n"
1034                         "\n"
1035                         "There is no guarantee that the message has been read or understood.\n"
1036                         "\n"
1037                         "--%s\n"
1038                         "Content-Type: message/disposition-notification\n"
1039                         "\n"
1040                         "Reporting-UA: %s\n"
1041                         "Original-Recipient: rfc822;%s\n"
1042                         "Final-Recipient: rfc822;%s\n"
1043                         "Original-Message-ID: <%s>\n"
1044                         "Disposition: manual-action/MDN-sent-manually; displayed\n"
1045                         "\n"
1046                         "--%s\n"
1047                         "Content-Type: application/octet-stream\n"
1048                         "\n"
1049                         "Reporting-UA: %s\n"
1050                         "Original-Recipient: rfc822;%s\n"
1051                         "Final-Recipient: rfc822;%s\n"
1052                         "Original-Message-ID: <%s>\n"
1053                         "Disposition: manual-action/MDN-sent-manually; displayed\n"
1054                         "\n"
1055                         "--%s--\n", 
1056                         boundary, 
1057                         boundary,
1058                         msginfo->date, 
1059                         orig_to?orig_to:"No To:",
1060                         enc_sub?enc_sub:"No subject",
1061                         date,
1062                         boundary,
1063                         PROG_VERSION,
1064                         orig_to?orig_to:"No To:",
1065                         account->address,
1066                         msginfo->msgid?msginfo->msgid:"NO MESSAGE ID",
1067                         boundary,
1068                         PROG_VERSION,
1069                         orig_to?orig_to:"No To:",
1070                         account->address,
1071                         msginfo->msgid?msginfo->msgid:"NO MESSAGE ID",
1072                         boundary);
1073
1074         g_free(enc_sub);
1075         g_free(orig_to);
1076         g_free(date);
1077         g_free(boundary);
1078
1079         if (ok < 0)
1080                 goto FILE_ERROR;        
1081
1082         if (fclose(fp) == EOF) {
1083                 FILE_OP_ERROR(tmp, "fclose");
1084                 claws_unlink(tmp);
1085                 return -1;
1086         }
1087
1088         /* put it in queue */
1089         queue = account_get_special_folder(account, F_QUEUE);
1090         if (!queue) queue = folder_get_default_queue();
1091         if (!queue) {
1092                 g_warning("can't find queue folder");
1093                 claws_unlink(tmp);
1094                 return -1;
1095         }
1096         folder_item_scan(queue);
1097         if ((num = folder_item_add_msg(queue, tmp, NULL, TRUE)) < 0) {
1098                 g_warning("can't queue the message");
1099                 claws_unlink(tmp);
1100                 return -1;
1101         }
1102                 
1103         if (prefs_common.work_offline && 
1104             !inc_offline_should_override(TRUE,
1105                 _("Claws Mail needs network access in order "
1106                   "to send this email.")))
1107                 return 0;
1108
1109         /* send it */
1110         path = folder_item_fetch_msg(queue, num);
1111         ok = procmsg_send_message_queue_with_lock(path, &foo, queue, num, &queued_removed);
1112         g_free(path);
1113         g_free(foo);
1114         if (ok == 0 && !queued_removed)
1115                 folder_item_remove_msg(queue, num);
1116
1117         return ok;
1118
1119 FILE_ERROR:
1120         fclose(fp);
1121         claws_unlink(tmp);
1122         return -1;
1123 }
1124
1125 static gboolean find_encrypted_func(GNode *node, gpointer data)
1126 {
1127         MimeInfo *mimeinfo = (MimeInfo *) node->data;
1128         MimeInfo **encinfo = (MimeInfo **) data;
1129         
1130         if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
1131                 *encinfo = mimeinfo;
1132                 return TRUE;
1133         }
1134         
1135         return FALSE;
1136 }
1137
1138 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
1139 {
1140         MimeInfo *encinfo = NULL;
1141
1142         g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
1143                 find_encrypted_func, &encinfo);
1144         
1145         return encinfo;
1146 }
1147
1148 static gboolean find_broken_func(GNode *node, gpointer data)
1149 {
1150         MimeInfo *mimeinfo = (MimeInfo *) node->data;
1151         MimeInfo **brokeninfo = (MimeInfo **) data;
1152         
1153         if (mimeinfo->broken) {
1154                 *brokeninfo = mimeinfo;
1155                 return TRUE;
1156         }
1157         
1158         return FALSE;
1159 }
1160
1161 static MimeInfo *find_broken_part(MimeInfo *rootinfo)
1162 {
1163         MimeInfo *brokeninfo = NULL;
1164
1165         g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
1166                 find_broken_func, &brokeninfo);
1167         
1168         return brokeninfo;
1169 }
1170
1171 static void messageview_register_nav(MessageView *messageview)
1172 {
1173         gchar *id;
1174         gint pos = -1;
1175         GList *existing;
1176
1177         cm_return_if_fail(messageview);
1178         cm_return_if_fail(messageview->msginfo);
1179
1180         id = procmsg_msginfo_get_identifier(messageview->msginfo);
1181         existing = g_list_find_custom(messageview->trail, id, (GCompareFunc)g_strcmp0);
1182
1183         if (existing != NULL)
1184                 pos = g_list_position(messageview->trail, existing);
1185         else
1186                 pos = -1;
1187
1188         if (pos != -1) {
1189                 messageview->trail_pos = pos;
1190                 g_free(id);
1191         } else {
1192                 /* Cut the end of the list */
1193                 GList *end = g_list_nth(messageview->trail, messageview->trail_pos + 1);
1194                 if (end) {
1195                         if (end->prev) {
1196                                 end->prev->next = NULL;
1197                                 end->prev = NULL;
1198                                 list_free_strings_full(end);
1199                         } else {
1200                                 list_free_strings_full(messageview->trail);
1201                                 messageview->trail = NULL;
1202                         }
1203                 }
1204                 messageview->trail = g_list_append(messageview->trail, id);
1205                 messageview->trail_pos = (gint)g_list_length(messageview->trail) - 1;
1206                 
1207                 /* Cut the beginning if needed */
1208                 while (messageview->trail_pos > prefs_common.nav_history_length) {
1209                         g_free(messageview->trail->data);
1210                         messageview->trail = g_list_delete_link(messageview->trail,
1211                                                 messageview->trail);
1212                         messageview->trail_pos--;
1213                 }
1214         }
1215         messageview_set_menu_sensitive(messageview);
1216 }
1217
1218 gboolean messageview_nav_has_prev(MessageView *messageview) {
1219         return messageview != NULL && messageview->trail != NULL
1220                 && messageview->trail_pos > 0;
1221 }
1222
1223 gboolean messageview_nav_has_next(MessageView *messageview) {
1224         if (!messageview || !messageview->trail)
1225                 return FALSE;
1226         
1227         return sc_g_list_bigger(messageview->trail, messageview->trail_pos + 1);
1228 }
1229
1230 MsgInfo *messageview_nav_get_prev(MessageView *messageview) {
1231         GList *item;
1232         MsgInfo *info;
1233
1234         cm_return_val_if_fail(messageview, NULL);
1235         cm_return_val_if_fail(messageview->trail, NULL);
1236
1237         do {
1238                 if (!messageview_nav_has_prev(messageview))
1239                         return NULL;
1240
1241                 item = g_list_nth(messageview->trail, messageview->trail_pos - 1);
1242                 cm_return_val_if_fail(item != NULL, NULL);
1243
1244                 info = procmsg_get_msginfo_from_identifier((const gchar *)item->data);
1245                 if (info != NULL)
1246                         break;
1247
1248                 g_free(item->data);
1249                 messageview->trail = g_list_delete_link(messageview->trail, item);
1250                 if (messageview->trail_pos > 0)
1251                         messageview->trail_pos--;
1252         } while (info == NULL);
1253
1254         return info;
1255 }
1256
1257 MsgInfo *messageview_nav_get_next(MessageView *messageview) {
1258         GList *item;
1259         MsgInfo *info;
1260
1261         cm_return_val_if_fail(messageview, NULL);
1262         cm_return_val_if_fail(messageview->trail, NULL);
1263
1264         do {
1265                 if (!messageview_nav_has_next(messageview))
1266                         return NULL;
1267
1268                 item = g_list_nth(messageview->trail, messageview->trail_pos + 1);
1269                 cm_return_val_if_fail(item != NULL, NULL);
1270
1271                 info = procmsg_get_msginfo_from_identifier((const gchar *)item->data);
1272                 if (info != NULL)
1273                         break;
1274
1275                 g_free(item->data);
1276                 messageview->trail = g_list_delete_link(messageview->trail, item);
1277         } while (info == NULL);
1278         
1279         return info;
1280 }
1281
1282 static gboolean messageview_try_select_mimeinfo(MessageView *messageview, MsgInfo *msginfo, MimeInfo *mimeinfo)
1283 {
1284         if (mimeinfo->type == MIMETYPE_TEXT) {
1285                 if (!strcasecmp(mimeinfo->subtype, "calendar")
1286                                 && mimeview_has_viewer_for_content_type(messageview->mimeview, "text/calendar")) {
1287                         mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
1288                         return TRUE;
1289                 } else if (!strcasecmp(mimeinfo->subtype, "html")
1290                                 && mimeinfo->disposition != DISPOSITIONTYPE_ATTACHMENT
1291                                 && (msginfo->folder->prefs->promote_html_part == HTML_PROMOTE_ALWAYS
1292                                         || (msginfo->folder->prefs->promote_html_part == HTML_PROMOTE_DEFAULT
1293                                                 && prefs_common.promote_html_part))) {
1294                         mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
1295                         return TRUE;
1296                 }
1297         }
1298         return FALSE;
1299 }
1300
1301 static void messageview_find_part_depth_first(MimeInfoSearch *context, MimeMediaType type, const gchar *subtype)
1302 {
1303         MimeInfo * mimeinfo = context->current;
1304
1305         if (!mimeinfo)
1306                 return;
1307
1308         debug_print("found part %d/%s\n", mimeinfo->type, mimeinfo->subtype);
1309
1310         if (mimeinfo->type == type
1311                         && !strcasecmp(mimeinfo->subtype, subtype)) {
1312                 context->found = mimeinfo;
1313         } else if (mimeinfo->type == MIMETYPE_MULTIPART) {
1314                 if (!strcasecmp(mimeinfo->subtype, "alternative")
1315                                 || !strcasecmp(mimeinfo->subtype, "related")) {
1316                         context->found = procmime_mimeinfo_next(mimeinfo);
1317                         while (context->found && context->found != context->parent) {
1318                                 if (context->found->type == type
1319                                         && !strcasecmp(context->found->subtype, subtype))
1320                                                 break;
1321                                 context->found = procmime_mimeinfo_next(context->found);
1322                         }
1323                         if (context->found == context->parent)
1324                                 context->found = NULL;
1325                 }
1326                 if (!context->found
1327                         && (!strcasecmp(mimeinfo->subtype, "related")
1328                                 || !strcasecmp(mimeinfo->subtype, "mixed"))) {
1329                         context->parent = mimeinfo;
1330                         context->current = procmime_mimeinfo_next(mimeinfo);
1331                         messageview_find_part_depth_first(context, type, subtype);
1332                 }
1333         }
1334 }
1335
1336 gint messageview_show(MessageView *messageview, MsgInfo *msginfo,
1337                       gboolean all_headers)
1338 {
1339         gchar *text = NULL;
1340         gchar *file;
1341         MimeInfo *mimeinfo, *encinfo, *root;
1342         gchar *subject = NULL;
1343         cm_return_val_if_fail(msginfo != NULL, -1);
1344
1345         if (msginfo != messageview->msginfo)
1346                 messageview->show_full_text = FALSE;
1347
1348         if (messageview->mimeview->textview &&
1349             messageview->mimeview->textview->loading) {
1350                 messageview->mimeview->textview->stop_loading = TRUE;
1351                 return 0;
1352         }
1353
1354         if (messageview->toolbar)
1355                 toolbar_set_learn_button
1356                         (messageview->toolbar,
1357                          MSG_IS_SPAM(msginfo->flags)?LEARN_HAM:LEARN_SPAM);
1358         else
1359                 toolbar_set_learn_button
1360                         (messageview->mainwin->toolbar,
1361                          MSG_IS_SPAM(msginfo->flags)?LEARN_HAM:LEARN_SPAM);
1362
1363         if (messageview->toolbar) {
1364                 if (messageview->toolbar->learn_spam_btn) {
1365                         gboolean can_learn = FALSE;
1366                         if (procmsg_spam_can_learn() &&
1367                             (msginfo->folder &&
1368                              msginfo->folder->folder->klass->type != F_UNKNOWN &&
1369                              msginfo->folder->folder->klass->type != F_NEWS))
1370                                 can_learn = TRUE;
1371
1372                         gtk_widget_set_sensitive(
1373                                 messageview->toolbar->learn_spam_btn, 
1374                                 can_learn);
1375                 }
1376         }
1377         
1378         noticeview_hide(messageview->noticeview);
1379         mimeview_clear(messageview->mimeview);
1380         messageview->updating = TRUE;
1381
1382         if (msginfo->size > 1024*1024)
1383                 statusbar_print_all(_("Fetching message (%s)..."),
1384                         to_human_readable(msginfo->size));
1385         
1386         file = procmsg_get_message_file_path(msginfo);
1387
1388         if (msginfo->size > 1024*1024)
1389                 statusbar_pop_all();
1390
1391         if (!file) {
1392                 g_warning("can't get message file path.");
1393                 textview_show_error(messageview->mimeview->textview);
1394                 return -1;
1395         }
1396         
1397         if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
1398             !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
1399                 mimeinfo = procmime_scan_file(file);
1400         else
1401                 mimeinfo = procmime_scan_queue_file(file);
1402
1403         messageview->updating = FALSE;
1404         
1405         if (messageview->deferred_destroy) {
1406                 g_free(file);
1407                 messageview_destroy(messageview);
1408                 return 0;
1409         }
1410
1411         if (!mimeinfo) {
1412                 textview_show_error(messageview->mimeview->textview);
1413                 return -1;
1414         }
1415
1416         while ((encinfo = find_encrypted_part(mimeinfo)) != NULL) {
1417                 debug_print("decrypting message part\n");
1418                 if (privacy_mimeinfo_decrypt(encinfo) < 0) {
1419                         text = g_strdup_printf(_("Couldn't decrypt: %s"),
1420                                                privacy_get_error());
1421                         noticeview_show(messageview->noticeview);
1422                         noticeview_set_icon(messageview->noticeview,
1423                                             STOCK_PIXMAP_NOTICE_WARN);
1424                         noticeview_set_text(messageview->noticeview, text);
1425                         gtk_widget_hide(messageview->noticeview->button);
1426                         gtk_widget_hide(messageview->noticeview->button2);
1427                         g_free(text);
1428                         break;
1429                 }
1430         }
1431                         
1432         if (messageview->msginfo != msginfo) {
1433                 procmsg_msginfo_free(&(messageview->msginfo));
1434                 messageview->msginfo = NULL;
1435                 messageview_set_menu_sensitive(messageview);
1436                 messageview->msginfo = 
1437                         procmsg_msginfo_get_full_info_from_file(msginfo, file);
1438                 if (!messageview->msginfo)
1439                         messageview->msginfo = procmsg_msginfo_copy(msginfo);
1440         } else {
1441                 messageview->msginfo = NULL;
1442                 messageview_set_menu_sensitive(messageview);
1443                 messageview->msginfo = msginfo;
1444         }
1445         if (prefs_common.display_header_pane)
1446                 headerview_show(messageview->headerview, messageview->msginfo);
1447
1448         messageview_register_nav(messageview);
1449         messageview_set_position(messageview, 0);
1450
1451         if (messageview->window) {
1452                 gtk_window_set_title(GTK_WINDOW(messageview->window), 
1453                                 _("Claws Mail - Message View"));
1454                 GTK_EVENTS_FLUSH();
1455         }
1456         mimeview_show_message(messageview->mimeview, mimeinfo, file);
1457         
1458 #ifndef GENERIC_UMPC
1459         messageview_set_position(messageview, 0);
1460 #endif
1461
1462         if (messageview->window && msginfo->subject) {
1463                 subject = g_strdup(msginfo->subject);
1464                 if (!g_utf8_validate(subject, -1, NULL)) {
1465                         g_free(subject);
1466                         subject = g_malloc(strlen(msginfo->subject)*2 +1);
1467                         conv_localetodisp(subject, strlen(msginfo->subject)*2 +1, 
1468                                 msginfo->subject);
1469                 }
1470                 if (g_utf8_validate(subject, -1, NULL))
1471                         gtk_window_set_title(GTK_WINDOW(messageview->window), 
1472                                 subject);
1473                 g_free(subject);
1474         }
1475
1476         if (msginfo && msginfo->folder) {
1477                 msginfo->folder->last_seen = msginfo->msgnum;   
1478         }
1479
1480         main_create_mailing_list_menu(messageview->mainwin, messageview->msginfo);
1481
1482         if (messageview->msginfo && messageview->msginfo->extradata
1483             && messageview->msginfo->extradata->partial_recv
1484             && !noticeview_is_visible(messageview->noticeview))
1485                 partial_recv_show(messageview->noticeview, 
1486                                   messageview->msginfo);
1487         else if (messageview->msginfo && messageview->msginfo->extradata &&
1488             (messageview->msginfo->extradata->dispositionnotificationto || 
1489              messageview->msginfo->extradata->returnreceiptto) &&
1490             !MSG_IS_RETRCPT_SENT(messageview->msginfo->flags) &&
1491             !prefs_common.never_send_retrcpt &&
1492             !noticeview_is_visible(messageview->noticeview))
1493                 return_receipt_show(messageview->noticeview, 
1494                                     messageview->msginfo);
1495
1496         if (find_broken_part(mimeinfo) != NULL) {
1497                 noticeview_set_icon(messageview->noticeview,
1498                                     STOCK_PIXMAP_NOTICE_WARN);
1499                 if (!noticeview_is_visible(messageview->noticeview)) {
1500                         noticeview_set_text(messageview->noticeview, _("Message doesn't conform to MIME standard. "
1501                                                 "It may render wrongly."));
1502                         gtk_widget_hide(messageview->noticeview->button);
1503                         gtk_widget_hide(messageview->noticeview->button2);
1504                 } else {
1505                         gchar *full = g_strconcat(
1506                                         gtk_label_get_text(GTK_LABEL(messageview->noticeview->text)), 
1507                                         "\n", 
1508                                         _("Message doesn't conform to MIME standard. "
1509                                         "It may render wrongly."), NULL);
1510                         noticeview_set_text(messageview->noticeview, full);
1511                         g_free(full);
1512                 }
1513                 noticeview_show(messageview->noticeview);
1514         }
1515                         
1516         root = mimeinfo;
1517         mimeinfo = procmime_mimeinfo_next(mimeinfo);
1518         if (!all_headers && mimeinfo
1519                         && (mimeinfo->type != MIMETYPE_TEXT
1520                                 || strcasecmp(mimeinfo->subtype, "plain"))
1521                         && (mimeinfo->type != MIMETYPE_MULTIPART
1522                                 || strcasecmp(mimeinfo->subtype, "signed"))) {
1523                 if (strcasecmp(mimeinfo->subtype, "html")) {
1524                         MimeInfoSearch context = {
1525                                 .parent = root,
1526                                 .current = mimeinfo,
1527                                 .found = NULL
1528                         };
1529                         if (mimeview_has_viewer_for_content_type(messageview->mimeview, "text/calendar")) {
1530                                 MimeInfoSearch cal_context = context;
1531                                 messageview_find_part_depth_first(&cal_context, MIMETYPE_TEXT, "calendar");
1532                                 if (cal_context.found) { /* calendar found */
1533                                         mimeinfo = cal_context.found;
1534                                         if (messageview_try_select_mimeinfo(messageview, msginfo, mimeinfo))
1535                                                 goto done;
1536                                 }
1537                         }
1538                         messageview_find_part_depth_first(&context, MIMETYPE_TEXT, "html");
1539                         if (context.found &&
1540                             ((msginfo->folder && msginfo->folder->prefs->promote_html_part == HTML_PROMOTE_ALWAYS) ||
1541                              ((msginfo->folder && msginfo->folder->prefs->promote_html_part == HTML_PROMOTE_DEFAULT) &&
1542                               prefs_common.promote_html_part))) { /* html found */
1543                                 mimeinfo = context.found;
1544                                 if (messageview_try_select_mimeinfo(messageview, msginfo, mimeinfo))
1545                                         goto done;
1546                         } else
1547                                 mimeinfo = root; /* nothing found */
1548
1549                         if (!mimeview_show_part(messageview->mimeview, mimeinfo))
1550                                 mimeview_select_mimepart_icon(messageview->mimeview, root);
1551                         goto done;
1552                 } else if (prefs_common.invoke_plugin_on_html) {
1553                         mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
1554                         goto done;
1555                 }
1556         }
1557         if (!all_headers && mimeinfo &&
1558             mimeinfo->type == MIMETYPE_MULTIPART &&
1559             mimeview_has_viewer_for_content_type(messageview->mimeview, "text/calendar")) {
1560                 /* look for a calendar part or it looks really strange */
1561                 while (mimeinfo) {
1562                         if (mimeinfo->type == MIMETYPE_TEXT &&
1563                             !strcasecmp(mimeinfo->subtype, "calendar")) {
1564                                 mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
1565                                 goto done;
1566                         }
1567                         mimeinfo = procmime_mimeinfo_next(mimeinfo);
1568                 }
1569         }
1570
1571         mimeview_select_mimepart_icon(messageview->mimeview, root);
1572 done:
1573         messageview_set_menu_sensitive(messageview);
1574         /* plugins may hook in here to work with the message view */
1575         hooks_invoke(MESSAGE_VIEW_SHOW_DONE_HOOKLIST, messageview);
1576
1577         g_free(file);
1578
1579         return 0;
1580 }
1581
1582 void messageview_reflect_prefs_pixmap_theme(void)
1583 {
1584         GList *cur;
1585         MessageView *msgview;
1586
1587         for (cur = msgview_list; cur != NULL; cur = cur->next) {
1588                 msgview = (MessageView*)cur->data;
1589                 toolbar_update(TOOLBAR_MSGVIEW, msgview);
1590                 mimeview_update(msgview->mimeview);
1591         }
1592 }
1593
1594 void messageview_clear(MessageView *messageview)
1595 {
1596         if (!messageview)
1597                 return;
1598         procmsg_msginfo_free(&(messageview->msginfo));
1599         messageview->msginfo = NULL;
1600         messageview->filtered = FALSE;
1601
1602         if (messageview->window) {
1603                 gtk_window_set_title(GTK_WINDOW(messageview->window), 
1604                                 _("Claws Mail - Message View"));
1605                 GTK_EVENTS_FLUSH();
1606         }
1607
1608         mimeview_clear(messageview->mimeview);
1609         headerview_clear(messageview->headerview);
1610         noticeview_hide(messageview->noticeview);
1611 }
1612
1613 void messageview_destroy(MessageView *messageview)
1614 {
1615         debug_print("destroy messageview\n");
1616         messageview_list = g_list_remove(messageview_list, messageview);
1617
1618         if (messageview->mainwin->summaryview->messageview == messageview) {
1619                 messageview->mainwin->summaryview->displayed = NULL;
1620                 messageview->mainwin->summaryview->messageview = NULL;
1621         }
1622         if (messageview->mainwin->summaryview->ext_messageview == messageview) {
1623                 messageview->mainwin->summaryview->displayed = NULL;
1624                 messageview->mainwin->summaryview->ext_messageview = NULL;
1625         }
1626         if (!messageview->deferred_destroy)
1627                 hooks_unregister_hook(MSGINFO_UPDATE_HOOKLIST,
1628                               messageview->msginfo_update_callback_id);
1629
1630         if (messageview->updating) {
1631                 debug_print("uh oh, better not touch that now (fetching)\n");
1632                 messageview->deferred_destroy = TRUE;
1633                 gtk_widget_hide(messageview->window);
1634                 return;
1635         }
1636         
1637         if (messageview->mimeview->textview
1638         &&  messageview->mimeview->textview->loading) {
1639                 debug_print("uh oh, better not touch that now (loading text)\n");
1640                 messageview->deferred_destroy = TRUE;
1641                 messageview->mimeview->textview->stop_loading = TRUE;
1642                 gtk_widget_hide(messageview->window);
1643                 return;
1644         }
1645
1646         headerview_destroy(messageview->headerview);
1647         mimeview_destroy(messageview->mimeview);
1648         noticeview_destroy(messageview->noticeview);
1649
1650         procmsg_msginfo_free(&(messageview->msginfo));
1651         toolbar_clear_list(TOOLBAR_MSGVIEW);
1652         if (messageview->toolbar) {
1653                 toolbar_destroy(messageview->toolbar);
1654                 g_free(messageview->toolbar);
1655         }
1656
1657         list_free_strings_full(messageview->trail);
1658         msgview_list = g_list_remove(msgview_list, messageview); 
1659
1660         if (messageview->window)
1661                 gtk_widget_destroy(messageview->window);
1662         g_free(messageview);
1663 }
1664
1665 void messageview_delete(MessageView *msgview)
1666 {
1667         MsgInfo *msginfo = NULL;
1668         FolderItem *trash = NULL;
1669         PrefsAccount *ac = NULL;
1670
1671         if (msgview->msginfo && msgview->mainwin && msgview->mainwin->summaryview)
1672                 msginfo = summary_get_selected_msg(msgview->mainwin->summaryview);
1673         
1674         /* need a procmsg_msginfo_equal() */
1675         if (msginfo && msgview->msginfo && 
1676             msginfo->msgnum == msgview->msginfo->msgnum && 
1677             msginfo->folder == msgview->msginfo->folder) {
1678                 summary_delete_trash(msgview->mainwin->summaryview);
1679         } else {                
1680                 msginfo = msgview->msginfo;
1681
1682                 cm_return_if_fail(msginfo != NULL);
1683
1684                 /* to get the trash folder, we have to choose either
1685                  * the folder's or account's trash default - we prefer
1686                  * the one in the account prefs */
1687                 if (msginfo->folder) {
1688                         if (NULL != (ac = account_find_from_item(msginfo->folder)))
1689                                 trash = account_get_special_folder(ac, F_TRASH);
1690                         if (!trash && msginfo->folder->folder)  
1691                                 trash = msginfo->folder->folder->trash;
1692                         /* if still not found, use the default */
1693                         if (!trash) 
1694                                 trash = folder_get_default_trash();
1695                 }       
1696
1697                 cm_return_if_fail(trash != NULL);
1698
1699                 if (prefs_common.immediate_exec)
1700                         /* TODO: Delete from trash */
1701                         folder_item_move_msg(trash, msginfo);
1702                 else {
1703                         procmsg_msginfo_set_to_folder(msginfo, trash);
1704                         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
1705                         /* NOTE: does not update to next message in summaryview */
1706                 }
1707         }
1708 #ifdef GENERIC_UMPC
1709         if (msgview->window && !prefs_common.always_show_msg) {
1710                 messageview_destroy(msgview);
1711         }
1712 #endif
1713 }
1714
1715 /* 
1716  * \brief update messageview with currently selected message in summaryview
1717  *        leave unchanged if summaryview is empty
1718  * \param pointer to MessageView
1719  */     
1720 static void messageview_update(MessageView *msgview, MsgInfo *old_msginfo)
1721 {
1722         SummaryView *summaryview = (SummaryView*)msgview->mainwin->summaryview;
1723
1724         cm_return_if_fail(summaryview != NULL);
1725         
1726         if (summaryview->selected) {
1727                 MsgInfo *msginfo = summary_get_selected_msg(summaryview);
1728                 if (msginfo == NULL || msginfo == old_msginfo)
1729                         return;
1730
1731                 messageview_show(msgview, msginfo, 
1732                                  msgview->all_headers);
1733         } 
1734 }
1735
1736 TextView *messageview_get_current_textview(MessageView *messageview)
1737 {
1738         TextView *text = NULL;
1739
1740         text = messageview->mimeview->textview;
1741
1742         return text;
1743 }
1744
1745 MimeInfo *messageview_get_selected_mime_part(MessageView *messageview)
1746 {
1747         return mimeview_get_selected_part(messageview->mimeview);
1748 }
1749
1750 void messageview_copy_clipboard(MessageView *messageview)
1751 {
1752         gchar *text = messageview_get_selection(messageview);
1753         if (text) {
1754                 gtk_clipboard_set_text(
1755                         gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
1756                         text, -1);
1757         }
1758         g_free(text);
1759 }
1760
1761 void messageview_select_all(MessageView *messageview)
1762 {
1763         TextView *text;
1764
1765         text = messageview_get_current_textview(messageview);
1766         if (text) {
1767                 GtkTextView *textview = GTK_TEXT_VIEW(text->text);
1768                 GtkTextBuffer *buffer;
1769                 GtkTextIter start, end;
1770
1771                 buffer = gtk_text_view_get_buffer(textview);
1772                 gtk_text_buffer_get_bounds(buffer, &start, &end);
1773                 gtk_text_buffer_select_range(buffer, &start, &end);
1774         }
1775 }
1776
1777 void messageview_set_position(MessageView *messageview, gint pos)
1778 {
1779         TextView *text;
1780
1781         text = messageview_get_current_textview(messageview);
1782         if (text)
1783                 textview_set_position(text, pos);
1784 }
1785
1786 gboolean messageview_search_string(MessageView *messageview, const gchar *str,
1787                                    gboolean case_sens)
1788 {
1789         TextView *text;
1790
1791         if (messageview->mimeview->type == MIMEVIEW_VIEWER) {
1792                 MimeViewer *viewer = messageview->mimeview->mimeviewer;
1793                 if (viewer && viewer->text_search) {
1794                         return viewer->text_search(viewer, FALSE, str, case_sens);
1795                 }
1796         }
1797
1798         text = messageview_get_current_textview(messageview);
1799         if (text)
1800                 return textview_search_string(text, str, case_sens);
1801         return FALSE;
1802 }
1803
1804 gboolean messageview_search_string_backward(MessageView *messageview,
1805                                             const gchar *str,
1806                                             gboolean case_sens)
1807 {
1808         TextView *text;
1809
1810         if (messageview->mimeview->type == MIMEVIEW_VIEWER) {
1811                 MimeViewer *viewer = messageview->mimeview->mimeviewer;
1812                 if (viewer && viewer->text_search) {
1813                         return viewer->text_search(viewer, TRUE, str, case_sens);
1814                 }
1815         }
1816
1817         text = messageview_get_current_textview(messageview);
1818         if (text)       
1819                 return textview_search_string_backward(text,
1820                                                        str, case_sens);
1821         return FALSE;
1822 }
1823
1824 gboolean messageview_is_visible(MessageView *messageview)
1825 {
1826         if (messageview == NULL)
1827                 return FALSE;
1828         return messageview->visible;
1829 }
1830
1831 static void messageview_save_as(MessageView *messageview)
1832 {
1833         gchar *filename = NULL;
1834         MsgInfo *msginfo;
1835         gchar *src, *dest, *tmp;
1836
1837         if (!messageview->msginfo) return;
1838         msginfo = messageview->msginfo;
1839
1840         if (msginfo->subject) {
1841                 Xstrdup_a(filename, msginfo->subject, return);
1842                 subst_for_filename(filename);
1843         }
1844         if (filename && !g_utf8_validate(filename, -1, NULL)) {
1845                 gchar *oldstr = filename;
1846                 filename = conv_codeset_strdup(filename,
1847                                                conv_get_locale_charset_str(),
1848                                                CS_UTF_8);
1849                 if (!filename) {
1850                         g_warning("messageview_save_as(): failed to convert character set.");
1851                         filename = g_strdup(oldstr);
1852                 }
1853                 dest = filesel_select_file_save(_("Save as"), filename);
1854                 g_free(filename);
1855         } else
1856                 dest = filesel_select_file_save(_("Save as"), filename);
1857         if (!dest) return;
1858         if (is_file_exist(dest)) {
1859                 AlertValue aval;
1860
1861                 aval = alertpanel(_("Overwrite"),
1862                                   _("Overwrite existing file?"),
1863                                   GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL, ALERTFOCUS_FIRST);
1864                 if (G_ALERTALTERNATE != aval) return;
1865         }
1866
1867         src = procmsg_get_message_file(msginfo);
1868         if (copy_file(src, dest, TRUE) < 0) {
1869                 tmp =  g_path_get_basename(dest);
1870                 alertpanel_error(_("Couldn't save the file '%s'."), tmp);
1871                 g_free(tmp);
1872         }
1873         g_free(dest);
1874         g_free(src);
1875 }
1876
1877 static gint messageview_delete_cb(GtkWidget *widget, GdkEventAny *event,
1878                                   MessageView *messageview)
1879 {
1880         messageview_destroy(messageview);
1881         return TRUE;
1882 }
1883
1884 static void messageview_size_allocate_cb(GtkWidget *widget,
1885                                          GtkAllocation *allocation)
1886 {
1887         cm_return_if_fail(allocation != NULL);
1888
1889         prefs_common.msgwin_width  = allocation->width;
1890         prefs_common.msgwin_height = allocation->height;
1891 }
1892
1893 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
1894                         MessageView *messageview)
1895 {
1896         if (event && event->keyval == GDK_KEY_Escape && messageview->window) {
1897                 messageview_destroy(messageview);
1898                 return TRUE;
1899         }
1900
1901         if (event && (event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0)
1902                 return FALSE;
1903         if (event && (event->state & GDK_SHIFT_MASK) && event->keyval != GDK_KEY_space) 
1904                 return FALSE;
1905
1906         return mimeview_pass_key_press_event(messageview->mimeview, event);
1907 }
1908
1909 static void messageview_show_partial_display_cb(NoticeView *noticeview, MessageView *messageview)
1910 {
1911         messageview->show_full_text = TRUE;
1912         main_window_cursor_wait(mainwindow_get_mainwindow());
1913         noticeview_hide(messageview->noticeview);
1914         messageview->partial_display_shown = FALSE;
1915         GTK_EVENTS_FLUSH();
1916         mimeview_handle_cmd(messageview->mimeview, "sc://display_as_text", NULL, NULL);
1917         main_window_cursor_normal(mainwindow_get_mainwindow());
1918 }
1919
1920 void messageview_show_partial_display(MessageView *messageview, MsgInfo *msginfo,
1921                                              size_t length)
1922 {
1923         gchar *msg = g_strdup_printf(_("Show all %s."), to_human_readable((goffset)length));
1924         noticeview_set_icon(messageview->noticeview, STOCK_PIXMAP_NOTICE_WARN);
1925         noticeview_set_text(messageview->noticeview, _("Only the first megabyte of text is shown."));
1926         noticeview_set_button_text(messageview->noticeview, msg);
1927         g_free(msg);
1928         noticeview_set_button_press_callback(messageview->noticeview,
1929                                              G_CALLBACK(messageview_show_partial_display_cb),
1930                                              (gpointer) messageview);
1931         noticeview_show(messageview->noticeview);
1932         messageview->partial_display_shown = TRUE;
1933 }
1934
1935 static void return_receipt_show(NoticeView *noticeview, MsgInfo *msginfo)
1936 {
1937         gchar *addr = NULL;
1938         gboolean from_me = FALSE;
1939         if (msginfo->folder 
1940                 && (folder_has_parent_of_type(msginfo->folder, F_QUEUE)
1941                  || folder_has_parent_of_type(msginfo->folder, F_DRAFT)))
1942                 return;
1943
1944         addr = g_strdup(msginfo->from);
1945         if (addr) {
1946                 extract_address(addr);
1947                 if (account_find_from_address(addr, FALSE)) {
1948                         from_me = TRUE;
1949                 }
1950                 g_free(addr);
1951         }
1952
1953         if (from_me) {
1954                 noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
1955                 if (MSG_IS_RETRCPT_GOT(msginfo->flags)) {
1956                         noticeview_set_text(noticeview, _("You got a return receipt for this message: "
1957                                                           "it has been displayed by the recipient."));
1958                 } else {
1959                         noticeview_set_text(noticeview, _("You asked for a return receipt in this message."));
1960                 }
1961                 noticeview_set_button_text(noticeview, NULL);
1962                 noticeview_set_button_press_callback(noticeview, NULL, NULL);
1963         } else {
1964                 noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
1965                 noticeview_set_text(noticeview, _("This message asks for a return receipt."));
1966                 noticeview_set_button_text(noticeview, _("Send receipt"));
1967                 noticeview_set_button_press_callback(noticeview,
1968                                                      G_CALLBACK(return_receipt_send_clicked),
1969                                                      (gpointer) msginfo);
1970         }
1971         noticeview_show(noticeview);
1972 }
1973
1974 static void return_receipt_send_clicked(NoticeView *noticeview, MsgInfo *msginfo)
1975 {
1976         MsgInfo *tmpmsginfo;
1977         gchar *file;
1978
1979         file = procmsg_get_message_file_path(msginfo);
1980         if (!file) {
1981                 g_warning("can't get message file path.");
1982                 return;
1983         }
1984
1985         tmpmsginfo = procheader_parse_file(file, msginfo->flags, TRUE, TRUE);
1986         tmpmsginfo->folder = msginfo->folder;
1987         tmpmsginfo->msgnum = msginfo->msgnum;
1988
1989         if (disposition_notification_send(tmpmsginfo) >= 0) {
1990                 procmsg_msginfo_set_flags(msginfo, MSG_RETRCPT_SENT, 0);
1991                 noticeview_hide(noticeview);
1992         }               
1993
1994         procmsg_msginfo_free(&tmpmsginfo);
1995         g_free(file);
1996 }
1997
1998 static void partial_recv_show(NoticeView *noticeview, MsgInfo *msginfo)
1999 {
2000         gchar *text = NULL;
2001         gchar *button1 = NULL;
2002         gchar *button2 = NULL;
2003         void  *button1_cb = NULL;
2004         void  *button2_cb = NULL;
2005
2006         if (!msginfo->extradata)
2007                 return;
2008         if (!partial_msg_in_uidl_list(msginfo)) {
2009                 text = g_strdup_printf(_("This message has been partially "
2010                                 "retrieved,\nand has been deleted from the "
2011                                 "server."));
2012         } else {
2013                 switch (msginfo->planned_download) {
2014                 case POP3_PARTIAL_DLOAD_UNKN:
2015                         text = g_strdup_printf(_("This message has been "
2016                                         "partially retrieved;\nit is %s."),
2017                                         to_human_readable(
2018                                                 (goffset)(msginfo->total_size)));
2019                         button1 = _("Mark for download");
2020                         button2 = _("Mark for deletion");
2021                         button1_cb = partial_recv_dload_clicked;
2022                         button2_cb = partial_recv_del_clicked;
2023                         break;
2024                 case POP3_PARTIAL_DLOAD_DLOAD:
2025                         text = g_strdup_printf(_("This message has been "
2026                                         "partially retrieved;\nit is %s and "
2027                                         "will be downloaded."),
2028                                         to_human_readable(
2029                                                 (goffset)(msginfo->total_size)));
2030                         button1 = _("Unmark");
2031                         button1_cb = partial_recv_unmark_clicked;
2032                         button2 = _("Mark for deletion");
2033                         button2_cb = partial_recv_del_clicked;
2034                         break;
2035                 case POP3_PARTIAL_DLOAD_DELE:
2036                         text = g_strdup_printf(_("This message has been "
2037                                         "partially retrieved;\nit is %s and "
2038                                         "will be deleted."),
2039                                         to_human_readable(
2040                                                 (goffset)(msginfo->total_size)));
2041                         button1 = _("Mark for download");
2042                         button1_cb = partial_recv_dload_clicked;
2043                         button2 = _("Unmark");
2044                         button2_cb = partial_recv_unmark_clicked;
2045                         break;
2046                 default:
2047                         return;
2048                 }
2049         }
2050         
2051         noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
2052         noticeview_set_text(noticeview, text);
2053         g_free(text);
2054         noticeview_set_button_text(noticeview, button1);
2055         noticeview_set_button_press_callback(noticeview,
2056                      G_CALLBACK(button1_cb), (gpointer) msginfo);
2057
2058         noticeview_set_2ndbutton_text(noticeview, button2);
2059         noticeview_set_2ndbutton_press_callback(noticeview,
2060                      G_CALLBACK(button2_cb), (gpointer) msginfo);
2061
2062         noticeview_show(noticeview);
2063 }
2064
2065 static void partial_recv_dload_clicked(NoticeView *noticeview, 
2066                                        MsgInfo *msginfo)
2067 {
2068         if (partial_mark_for_download(msginfo) == 0) {
2069                 partial_recv_show(noticeview, msginfo);
2070         }
2071 }
2072
2073 static void partial_recv_del_clicked(NoticeView *noticeview, 
2074                                        MsgInfo *msginfo)
2075 {
2076         if (partial_mark_for_delete(msginfo) == 0) {
2077                 partial_recv_show(noticeview, msginfo);
2078         }
2079 }
2080
2081 static void partial_recv_unmark_clicked(NoticeView *noticeview, 
2082                                        MsgInfo *msginfo)
2083 {
2084         if (partial_unmark(msginfo) == 0) {
2085                 partial_recv_show(noticeview, msginfo);
2086         }
2087 }
2088
2089 static void select_account_cb(GtkWidget *w, gpointer data)
2090 {
2091         *(gint*)data = combobox_get_active_data(GTK_COMBO_BOX(w));
2092 }
2093
2094 static PrefsAccount *select_account_from_list(GList *ac_list)
2095 {
2096         GtkWidget *optmenu;
2097         gint account_id;
2098
2099         cm_return_val_if_fail(ac_list != NULL, NULL);
2100         cm_return_val_if_fail(ac_list->data != NULL, NULL);
2101         
2102         optmenu = gtkut_account_menu_new(ac_list,
2103                         G_CALLBACK(select_account_cb),
2104                         &account_id);
2105         if (!optmenu)
2106                 return NULL;
2107         account_id = ((PrefsAccount *) ac_list->data)->account_id;
2108         if (alertpanel_with_widget(
2109                                 _("Return Receipt Notification"),
2110                                 _("More than one of your accounts uses the "
2111                                   "address that this message was sent to.\n"
2112                                   "Please choose which account you want to "
2113                                   "use for sending the receipt notification:"),
2114                                 _("_Cancel"), _("_Send Notification"), NULL, ALERTFOCUS_FIRST,
2115                                 FALSE, optmenu) != G_ALERTALTERNATE)
2116                 return NULL;
2117         return account_find_from_id(account_id);
2118 }
2119
2120 /* 
2121  * \brief return selected messageview text, when nothing is 
2122  *        selected and message was filtered, return complete text
2123  *
2124  * \param  pointer to Messageview 
2125  *
2126  * \return pointer to text (needs to be free'd by calling func)
2127  */
2128 gchar *messageview_get_selection(MessageView *msgview)
2129 {
2130         TextView *textview;
2131         gchar *text = NULL;
2132         GtkTextView *edit = NULL;
2133         GtkTextBuffer *textbuf;
2134         gint body_pos = 0;
2135         GtkTextIter start_iter, end_iter;
2136         GtkTextMark *body_start, *body_end;
2137         
2138         cm_return_val_if_fail(msgview != NULL, NULL);
2139
2140         if (msgview->mimeview->type == MIMEVIEW_VIEWER) {
2141                 MimeViewer *viewer = msgview->mimeview->mimeviewer;
2142                 if (viewer && viewer->get_selection) {
2143                         text = viewer->get_selection(viewer);
2144                         if (text)
2145                                 return text;
2146                 }
2147         }
2148
2149         textview = messageview_get_current_textview(msgview);
2150         cm_return_val_if_fail(textview != NULL, NULL);
2151
2152         edit = GTK_TEXT_VIEW(textview->text);
2153         cm_return_val_if_fail(edit != NULL, NULL);
2154         body_pos = textview->body_pos;
2155
2156         textbuf = gtk_text_view_get_buffer(edit);
2157
2158         if (gtk_text_buffer_get_selection_bounds(textbuf, NULL, NULL)) {
2159                 return gtkut_text_view_get_selection(edit);
2160         } else {
2161                 if (msgview->filtered) {
2162                         gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter, body_pos);
2163                         gtk_text_buffer_get_end_iter(textbuf, &end_iter);
2164                 } else {
2165                         body_start = gtk_text_buffer_get_mark(textbuf, "body_start");
2166
2167                         /* If there is no body_start mark, an attachment is likely
2168                          * selected, and we're looking at instructions on what to do
2169                          * with it. No point in quoting that, so we'll just return NULL,
2170                          * so that original message body is quoted instead down the line.
2171                          */
2172                         if (body_start == NULL) {
2173                                 return NULL;
2174                         }
2175
2176                         gtk_text_buffer_get_iter_at_mark(textbuf, &start_iter, body_start);
2177
2178                         body_end = gtk_text_buffer_get_mark(textbuf, "body_end");
2179                         if (body_end != NULL) /* Just in case */
2180                                 gtk_text_buffer_get_iter_at_mark(textbuf, &end_iter, body_end);
2181                         else
2182                                 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
2183                 }
2184
2185                 return gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter, FALSE);
2186         }
2187
2188         return text;
2189 }
2190
2191 static void save_as_cb(GtkAction *action, gpointer data)
2192 {
2193         MessageView *messageview = (MessageView *)data;
2194         messageview_save_as(messageview);
2195 }
2196
2197 static void print_mimeview(MimeView *mimeview, gint sel_start, gint sel_end, gint partnum) 
2198 {
2199         MainWindow *mainwin;
2200         if (!mimeview 
2201         ||  !mimeview->textview
2202         ||  !mimeview->textview->text)
2203                 alertpanel_warning(_("Cannot print: the message doesn't "
2204                                      "contain text."));
2205         else {
2206                 gtk_widget_realize(mimeview->textview->text);
2207                 if (partnum > 0) {
2208                         mimeview_select_part_num(mimeview, partnum);
2209                 }
2210                 if (mimeview->type == MIMEVIEW_VIEWER) {
2211                         MimeViewer *viewer = mimeview->mimeviewer;
2212                         if (viewer && viewer->print) {
2213                                 viewer->print(viewer);
2214                                 return;
2215                         }
2216                 }
2217                 if (sel_start != -1 && sel_end != -1) {
2218                         GtkTextIter start, end;
2219                         GtkTextView *text = GTK_TEXT_VIEW(mimeview->textview->text);
2220                         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
2221
2222                         gtk_text_buffer_get_iter_at_offset(buffer, &start, sel_start);
2223                         gtk_text_buffer_get_iter_at_offset(buffer, &end, sel_end);
2224                         gtk_text_buffer_select_range(buffer, &start, &end);
2225                 }
2226                 /* TODO: Get the real parent window, not the main window */
2227                 mainwin = mainwindow_get_mainwindow();
2228                 printing_print(GTK_TEXT_VIEW(mimeview->textview->text),
2229                                mainwin ? GTK_WINDOW(mainwin->window) : NULL,
2230                                 sel_start, sel_end,
2231                                 (mimeview->textview->image 
2232                                         ? GTK_IMAGE(mimeview->textview->image)
2233                                         : NULL));
2234         }
2235 }
2236
2237 void messageview_print(MsgInfo *msginfo, gboolean all_headers, 
2238                         gint sel_start, gint sel_end, gint partnum) 
2239 {
2240         PangoFontDescription *font_desc = NULL;
2241         MessageView *tmpview = messageview_create_with_new_window_visible(
2242                                 mainwindow_get_mainwindow(), FALSE);
2243
2244         if (prefs_common.use_different_print_font) {
2245                 font_desc = pango_font_description_from_string
2246                                                 (prefs_common.printfont);
2247         } else {
2248                 font_desc = pango_font_description_from_string
2249                                                 (prefs_common.textfont);
2250         }
2251         if (font_desc) {
2252                 gtk_widget_modify_font(tmpview->mimeview->textview->text, 
2253                         font_desc);
2254                 pango_font_description_free(font_desc);
2255         }
2256
2257         tmpview->all_headers = all_headers;
2258         if (msginfo && messageview_show(tmpview, msginfo, 
2259                 tmpview->all_headers) >= 0) {
2260                         print_mimeview(tmpview->mimeview, 
2261                                 sel_start, sel_end, partnum);
2262         }
2263         messageview_destroy(tmpview);
2264 }
2265
2266 static void page_setup_cb(GtkAction *action, gpointer data)
2267 {
2268         MessageView *messageview = (MessageView *)data;
2269         printing_page_setup(messageview ?
2270                             GTK_WINDOW(messageview->window) : NULL);
2271 }
2272
2273 static void print_cb(GtkAction *action, gpointer data)
2274 {
2275         MessageView *messageview = (MessageView *)data;
2276         gint sel_start = -1, sel_end = -1, partnum = 0;
2277
2278         if (!messageview->msginfo) return;
2279
2280         partnum = mimeview_get_selected_part_num(messageview->mimeview);
2281         textview_get_selection_offsets(messageview->mimeview->textview,
2282                 &sel_start, &sel_end);
2283         messageview_print(messageview->msginfo, messageview->all_headers, 
2284                 sel_start, sel_end, partnum);
2285 }
2286
2287 static void close_cb(GtkAction *action, gpointer data)
2288 {
2289         MessageView *messageview = (MessageView *)data;
2290         messageview_destroy(messageview);
2291 }
2292
2293 static void copy_cb(GtkAction *action, gpointer data)
2294 {
2295         MessageView *messageview = (MessageView *)data;
2296         messageview_copy_clipboard(messageview);
2297 }
2298
2299 static void allsel_cb(GtkAction *action, gpointer data)
2300 {
2301         MessageView *messageview = (MessageView *)data;
2302         messageview_select_all(messageview);
2303 }
2304
2305 static void search_cb(GtkAction *action, gpointer data)
2306 {
2307         MessageView *messageview = (MessageView *)data;
2308         message_search(messageview);
2309 }
2310
2311 static void prev_cb(GtkAction *action, gpointer data)
2312 {
2313         MessageView *messageview = (MessageView *)data;
2314         messageview->updating = TRUE;
2315         summary_select_prev(messageview->mainwin->summaryview);
2316         messageview->updating = FALSE;
2317
2318         if (messageview->deferred_destroy) {
2319                 debug_print("messageview got away!\n");
2320                 messageview_destroy(messageview);
2321                 return;
2322         }
2323         if (messageview->mainwin->summaryview->selected) {
2324 #ifndef GENERIC_UMPC
2325                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2326                        
2327                 if (msginfo)
2328                         messageview_show(messageview, msginfo, 
2329                                          messageview->all_headers);
2330 #endif
2331         } else {
2332                 gtk_widget_destroy(messageview->window);
2333         }
2334 }
2335
2336 static void next_cb(GtkAction *action, gpointer data)
2337 {
2338         MessageView *messageview = (MessageView *)data;
2339         messageview->updating = TRUE;
2340         summary_select_next(messageview->mainwin->summaryview);
2341         messageview->updating = FALSE;
2342
2343         if (messageview->deferred_destroy) {
2344                 debug_print("messageview got away!\n");
2345                 messageview_destroy(messageview);
2346                 return;
2347         }
2348         if (messageview->mainwin->summaryview->selected) {
2349 #ifndef GENERIC_UMPC
2350                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2351                        
2352                 if (msginfo)
2353                         messageview_show(messageview, msginfo, 
2354                                          messageview->all_headers);
2355 #endif
2356         } else {
2357                 gtk_widget_destroy(messageview->window);
2358         }
2359 }
2360
2361 static void prev_unread_cb(GtkAction *action, gpointer data)
2362 {
2363         MessageView *messageview = (MessageView *)data;
2364         messageview->updating = TRUE;
2365         summary_select_prev_unread(messageview->mainwin->summaryview);
2366         messageview->updating = FALSE;
2367
2368         if (messageview->deferred_destroy) {
2369                 debug_print("messageview got away!\n");
2370                 messageview_destroy(messageview);
2371                 return;
2372         }
2373         if (messageview->mainwin->summaryview->selected) {
2374 #ifndef GENERIC_UMPC
2375                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2376                        
2377                 if (msginfo)
2378                         messageview_show(messageview, msginfo, 
2379                                          messageview->all_headers);
2380 #endif
2381         } else {
2382                 gtk_widget_destroy(messageview->window);
2383         }
2384 }
2385
2386 static void next_unread_cb(GtkAction *action, gpointer data)
2387 {
2388         MessageView *messageview = (MessageView *)data;
2389         messageview->updating = TRUE;
2390         summary_select_next_unread(messageview->mainwin->summaryview);
2391         messageview->updating = FALSE;
2392
2393         if (messageview->deferred_destroy) {
2394                 debug_print("messageview got away!\n");
2395                 messageview_destroy(messageview);
2396                 return;
2397         }
2398         if (messageview->mainwin->summaryview->selected) {
2399 #ifndef GENERIC_UMPC
2400                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2401                        
2402                 if (msginfo)
2403                         messageview_show(messageview, msginfo, 
2404                                          messageview->all_headers);
2405 #endif
2406         } else {
2407                 gtk_widget_destroy(messageview->window);
2408         }
2409 }
2410
2411 static void prev_new_cb(GtkAction *action, gpointer data)
2412 {
2413         MessageView *messageview = (MessageView *)data;
2414         messageview->updating = TRUE;
2415         summary_select_prev_new(messageview->mainwin->summaryview);
2416         messageview->updating = FALSE;
2417
2418         if (messageview->deferred_destroy) {
2419                 debug_print("messageview got away!\n");
2420                 messageview_destroy(messageview);
2421                 return;
2422         }
2423         if (messageview->mainwin->summaryview->selected) {
2424 #ifndef GENERIC_UMPC
2425                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2426                        
2427                 if (msginfo)
2428                         messageview_show(messageview, msginfo, 
2429                                          messageview->all_headers);
2430 #endif
2431         } else {
2432                 gtk_widget_destroy(messageview->window);
2433         }
2434 }
2435
2436 static void next_new_cb(GtkAction *action, gpointer data)
2437 {
2438         MessageView *messageview = (MessageView *)data;
2439         messageview->updating = TRUE;
2440         summary_select_next_new(messageview->mainwin->summaryview);
2441         messageview->updating = FALSE;
2442
2443         if (messageview->deferred_destroy) {
2444                 debug_print("messageview got away!\n");
2445                 messageview_destroy(messageview);
2446                 return;
2447         }
2448         if (messageview->mainwin->summaryview->selected) {
2449 #ifndef GENERIC_UMPC
2450                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2451                        
2452                 if (msginfo)
2453                         messageview_show(messageview, msginfo, 
2454                                          messageview->all_headers);
2455 #endif
2456         } else {
2457                 gtk_widget_destroy(messageview->window);
2458         }
2459 }
2460
2461 static void prev_marked_cb(GtkAction *action, gpointer data)
2462 {
2463         MessageView *messageview = (MessageView *)data;
2464         messageview->updating = TRUE;
2465         summary_select_prev_marked(messageview->mainwin->summaryview);
2466         messageview->updating = FALSE;
2467
2468         if (messageview->deferred_destroy) {
2469                 debug_print("messageview got away!\n");
2470                 messageview_destroy(messageview);
2471                 return;
2472         }
2473         if (messageview->mainwin->summaryview->selected) {
2474 #ifndef GENERIC_UMPC
2475                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2476                        
2477                 if (msginfo)
2478                         messageview_show(messageview, msginfo, 
2479                                          messageview->all_headers);
2480 #endif
2481         } else {
2482                 gtk_widget_destroy(messageview->window);
2483         }
2484 }
2485
2486 static void next_marked_cb(GtkAction *action, gpointer data)
2487 {
2488         MessageView *messageview = (MessageView *)data;
2489         messageview->updating = TRUE;
2490         summary_select_next_marked(messageview->mainwin->summaryview);
2491         messageview->updating = FALSE;
2492
2493         if (messageview->deferred_destroy) {
2494                 debug_print("messageview got away!\n");
2495                 messageview_destroy(messageview);
2496                 return;
2497         }
2498         if (messageview->mainwin->summaryview->selected) {
2499 #ifndef GENERIC_UMPC
2500                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2501                        
2502                 if (msginfo)
2503                         messageview_show(messageview, msginfo, 
2504                                          messageview->all_headers);
2505 #endif
2506         } else {
2507                 gtk_widget_destroy(messageview->window);
2508         }
2509 }
2510
2511 static void prev_labeled_cb(GtkAction *action, gpointer data)
2512 {
2513         MessageView *messageview = (MessageView *)data;
2514         messageview->updating = TRUE;
2515         summary_select_prev_labeled(messageview->mainwin->summaryview);
2516         messageview->updating = FALSE;
2517
2518         if (messageview->deferred_destroy) {
2519                 debug_print("messageview got away!\n");
2520                 messageview_destroy(messageview);
2521                 return;
2522         }
2523         if (messageview->mainwin->summaryview->selected) {
2524 #ifndef GENERIC_UMPC
2525                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2526                        
2527                 if (msginfo)
2528                         messageview_show(messageview, msginfo, 
2529                                          messageview->all_headers);
2530 #endif
2531         } else {
2532                 gtk_widget_destroy(messageview->window);
2533         }
2534 }
2535
2536 static void next_labeled_cb(GtkAction *action, gpointer data)
2537 {
2538         MessageView *messageview = (MessageView *)data;
2539         messageview->updating = TRUE;
2540         summary_select_next_labeled(messageview->mainwin->summaryview);
2541         messageview->updating = FALSE;
2542
2543         if (messageview->deferred_destroy) {
2544                 debug_print("messageview got away!\n");
2545                 messageview_destroy(messageview);
2546                 return;
2547         }
2548         if (messageview->mainwin->summaryview->selected) {
2549 #ifndef GENERIC_UMPC
2550                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2551                        
2552                 if (msginfo)
2553                         messageview_show(messageview, msginfo, 
2554                                          messageview->all_headers);
2555 #endif
2556         } else {
2557                 gtk_widget_destroy(messageview->window);
2558         }
2559 }
2560
2561 static void prev_history_cb(GtkAction *action, gpointer data)
2562 {
2563         MessageView *messageview = (MessageView *)data;
2564         MsgInfo *info = messageview_nav_get_prev(messageview);
2565         if (info) {
2566                 messageview->updating = TRUE;
2567                 messageview_show(messageview, info, 
2568                                          messageview->all_headers);
2569                 messageview->updating = FALSE;
2570                 procmsg_msginfo_free(&info);
2571                 if (messageview->deferred_destroy) {
2572                         debug_print("messageview got away!\n");
2573                         messageview_destroy(messageview);
2574                         return;
2575                 }
2576         }
2577 }
2578
2579 static void next_history_cb(GtkAction *action, gpointer data)
2580 {
2581         MessageView *messageview = (MessageView *)data;
2582         MsgInfo *info = messageview_nav_get_next(messageview);
2583         if (info) {
2584                 messageview->updating = TRUE;
2585                 messageview_show(messageview, info, 
2586                                          messageview->all_headers);
2587                 messageview->updating = FALSE;
2588                 procmsg_msginfo_free(&info);
2589                 if (messageview->deferred_destroy) {
2590                         debug_print("messageview got away!\n");
2591                         messageview_destroy(messageview);
2592                         return;
2593                 }
2594         }
2595 }
2596
2597 static void parent_cb(GtkAction *action, gpointer data)
2598 {
2599         MessageView *messageview = (MessageView *)data;
2600         messageview->updating = TRUE;
2601         summary_select_parent(messageview->mainwin->summaryview);
2602         messageview->updating = FALSE;
2603
2604         if (messageview->deferred_destroy) {
2605                 debug_print("messageview got away!\n");
2606                 messageview_destroy(messageview);
2607                 return;
2608         }
2609         if (messageview->mainwin->summaryview->selected) {
2610 #ifndef GENERIC_UMPC
2611                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2612                        
2613                 if (msginfo)
2614                         messageview_show(messageview, msginfo, 
2615                                          messageview->all_headers);
2616 #endif
2617         } else {
2618                 gtk_widget_destroy(messageview->window);
2619         }
2620 }
2621
2622 static void goto_unread_folder_cb(GtkAction *action, gpointer data)
2623 {
2624         MessageView *messageview = (MessageView *)data;
2625
2626         messageview->updating = TRUE;
2627         folderview_select_next_with_flag(messageview->mainwin->folderview, MSG_UNREAD);
2628         messageview->updating = FALSE;
2629
2630         if (messageview->deferred_destroy) {
2631                 debug_print("messageview got away!\n");
2632                 messageview_destroy(messageview);
2633                 return;
2634         }
2635         if (messageview->mainwin->summaryview->selected) {
2636 #ifndef GENERIC_UMPC
2637                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2638                        
2639                 if (msginfo)
2640                         messageview_show(messageview, msginfo, 
2641                                          messageview->all_headers);
2642 #endif
2643         } else {
2644                 gtk_widget_destroy(messageview->window);
2645         }
2646 }
2647
2648 static void goto_folder_cb(GtkAction *action, gpointer data)
2649 {
2650         MessageView *messageview = (MessageView *)data;
2651         FolderItem *to_folder;
2652
2653         to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL, FALSE,
2654                         _("Select folder to go to"));
2655
2656         if (to_folder) {
2657                 folderview_select(messageview->mainwin->folderview, to_folder);
2658
2659                 if (messageview->deferred_destroy) {
2660                         debug_print("messageview got away!\n");
2661                         messageview_destroy(messageview);
2662                         return;
2663                 }
2664                 if (messageview->mainwin->summaryview->selected) {
2665 #ifndef GENERIC_UMPC
2666                         MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2667                        
2668                         if (msginfo)
2669                                 messageview_show(messageview, msginfo, 
2670                                                  messageview->all_headers);
2671 #endif
2672                 } else {
2673                         gtk_widget_destroy(messageview->window);
2674                 }
2675         }
2676 }
2677
2678 static void scroll_prev_line_cb(GtkAction *action, gpointer data)
2679 {
2680         MessageView *messageview = (MessageView *)data;
2681         mimeview_scroll_one_line(messageview->mimeview,TRUE);
2682 }
2683
2684 static void scroll_next_line_cb(GtkAction *action, gpointer data)
2685 {
2686         MessageView *messageview = (MessageView *)data;
2687         mimeview_scroll_one_line(messageview->mimeview,FALSE);
2688 }
2689
2690 static void scroll_prev_page_cb(GtkAction *action, gpointer data)
2691 {
2692         MessageView *messageview = (MessageView *)data;
2693         mimeview_scroll_page(messageview->mimeview,TRUE);
2694 }
2695
2696 static void scroll_next_page_cb(GtkAction *action, gpointer data)
2697 {
2698         MessageView *messageview = (MessageView *)data;
2699         mimeview_scroll_page(messageview->mimeview,FALSE);
2700 }
2701
2702 static void set_charset_cb(GtkAction *action, GtkRadioAction *current, gpointer data)
2703 {
2704         MessageView *messageview = (MessageView *)data;
2705         gboolean active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (current));
2706         gint value = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (current));
2707         const gchar *charset;
2708
2709         if (active) {
2710                 charset = conv_get_charset_str((CharSet)value);
2711                 g_free(messageview->forced_charset);
2712                 messageview->forced_charset = g_strdup(charset);
2713                 procmime_force_charset(charset);
2714                 
2715                 messageview_show(messageview, messageview->msginfo, FALSE);
2716         }
2717 }
2718
2719 static void set_decode_cb(GtkAction *action, GtkRadioAction *current, gpointer data)
2720 {
2721         MessageView *messageview = (MessageView *)data;
2722         gboolean active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (current));
2723         gint value = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (current));
2724
2725         if (active) {
2726                 messageview->forced_encoding = (EncodingType)value;
2727
2728                 messageview_show(messageview, messageview->msginfo, FALSE);
2729                 debug_print("forced encoding: %d\n", value);
2730         }
2731 }
2732
2733
2734 static void view_source_cb(GtkAction *action, gpointer data)
2735 {
2736         MessageView *messageview = (MessageView *)data;
2737         SourceWindow *srcwin;
2738
2739         if (!messageview->msginfo) return;
2740
2741         srcwin = source_window_create();
2742         source_window_show_msg(srcwin, messageview->msginfo);
2743         source_window_show(srcwin);
2744 }
2745
2746 static void show_all_header_cb(GtkToggleAction *action, gpointer data)
2747 {
2748         MessageView *messageview = (MessageView *)data;
2749         MsgInfo *msginfo = messageview->msginfo;
2750
2751         if (messageview->mimeview->textview &&
2752             messageview->mimeview->textview->loading) {
2753                 return;
2754         }
2755         if (messageview->updating)
2756                 return;
2757
2758         messageview->all_headers = prefs_common.show_all_headers =
2759                         gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
2760         if (!msginfo) return;
2761         messageview->msginfo = NULL;
2762         messageview_show(messageview, msginfo, messageview->all_headers);
2763         procmsg_msginfo_free(&msginfo);
2764         main_window_set_menu_sensitive(messageview->mainwin);
2765         summary_redisplay_msg(messageview->mainwin->summaryview);
2766 }
2767
2768 static void msg_hide_quotes_cb(GtkToggleAction *action, gpointer data)
2769 {
2770         MessageView *messageview = (MessageView *)data;
2771         MsgInfo *msginfo = messageview->msginfo;
2772         static gboolean updating_menu = FALSE;
2773
2774         if (updating_menu)
2775                 return;
2776         if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action))) {
2777                 const gchar *a_name = gtk_action_get_name(GTK_ACTION(action));
2778                 if (!strcmp(a_name, "View/Quotes/CollapseAll")) prefs_common.hide_quotes = 1;
2779                 else if (!strcmp(a_name, "View/Quotes/Collapse2")) prefs_common.hide_quotes = 2;
2780                 else if (!strcmp(a_name, "View/Quotes/Collapse3")) prefs_common.hide_quotes = 3;
2781         } else
2782                 prefs_common.hide_quotes = 0;
2783         
2784         updating_menu=TRUE;
2785         
2786         cm_toggle_menu_set_active_full(messageview->ui_manager, "Menu/View/Quotes/CollapseAll", (prefs_common.hide_quotes == 1));
2787         cm_toggle_menu_set_active_full(messageview->ui_manager, "Menu/View/Quotes/Collapse2", (prefs_common.hide_quotes == 2));
2788         cm_toggle_menu_set_active_full(messageview->ui_manager, "Menu/View/Quotes/Collapse3", (prefs_common.hide_quotes == 3));
2789
2790         updating_menu=FALSE;
2791         if (!msginfo) return;
2792         messageview->msginfo = NULL;
2793         messageview_show(messageview, msginfo,
2794                          messageview->all_headers);
2795         procmsg_msginfo_free(&msginfo);
2796         
2797         /* update main window */
2798         main_window_set_menu_sensitive(messageview->mainwin);
2799         summary_redisplay_msg(messageview->mainwin->summaryview);
2800 }
2801 #undef SET_CHECK_MENU_ACTIVE
2802
2803 static void compose_cb(GtkAction *action, gpointer data)
2804 {
2805         MessageView *messageview = (MessageView *)data;
2806         PrefsAccount *ac = NULL;
2807         FolderItem *item = NULL;
2808
2809         if (messageview->msginfo)
2810                 item = messageview->msginfo->folder;
2811
2812         if (item) {
2813                 ac = account_find_from_item(item);
2814                 if (ac && ac->protocol == A_NNTP &&
2815                     FOLDER_TYPE(item->folder) == F_NEWS) {
2816                         compose_new(ac, item->path, NULL);
2817                         return;
2818                 }
2819         }
2820
2821         compose_new(ac, NULL, NULL);
2822 }
2823
2824 #define DO_ACTION(name, act)    { if (!strcmp(a_name, name)) action = act; }
2825
2826 static void reply_cb(GtkAction *gaction, gpointer data)
2827 {
2828         MessageView *messageview = (MessageView *)data;
2829         GSList *msginfo_list = NULL;
2830         gint action = COMPOSE_REPLY;
2831         const gchar *a_name = gtk_action_get_name(gaction);
2832         
2833         cm_return_if_fail(messageview->msginfo);
2834
2835         DO_ACTION("Message/Reply", COMPOSE_REPLY);
2836         DO_ACTION("Message/ReplyTo/All", COMPOSE_REPLY_TO_ALL);
2837         DO_ACTION("Message/ReplyTo/Sender", COMPOSE_REPLY_TO_SENDER);
2838         DO_ACTION("Message/ReplyTo/List", COMPOSE_REPLY_TO_LIST);
2839         DO_ACTION("Message/Forward", COMPOSE_FORWARD_INLINE);
2840         DO_ACTION("Message/ForwardAtt", COMPOSE_FORWARD_AS_ATTACH);
2841         DO_ACTION("Message/Redirect", COMPOSE_REDIRECT);
2842
2843         msginfo_list = g_slist_append(msginfo_list, messageview->msginfo);
2844         compose_reply_from_messageview(messageview, msginfo_list, action);
2845         g_slist_free(msginfo_list);
2846 }
2847
2848 static void addressbook_open_cb(GtkAction *action, gpointer data)
2849 {
2850 #ifndef USE_ALT_ADDRBOOK
2851         addressbook_open(NULL);
2852 #else
2853         GError* error = NULL;
2854         
2855         addressbook_dbus_open(FALSE, &error);
2856         if (error) {
2857                 g_warning("Failed to open address book: %s", error->message);
2858                 g_error_free(error);
2859         }
2860 #endif
2861 }
2862
2863 static void add_address_cb(GtkAction *action, gpointer data)
2864 {
2865         MessageView *messageview = (MessageView *)data;
2866         MsgInfo *msginfo, *full_msginfo;
2867         gchar *from;
2868         GdkPixbuf *picture = NULL;
2869         AvatarRender *avatarr;
2870
2871         if (!messageview->msginfo || !messageview->msginfo->from) 
2872                 return;
2873
2874         msginfo = messageview->msginfo;
2875         Xstrdup_a(from, msginfo->from, return);
2876         eliminate_address_comment(from);
2877         extract_address(from);
2878         
2879         full_msginfo = procmsg_msginfo_get_full_info(msginfo);
2880
2881         avatarr = avatars_avatarrender_new(full_msginfo);
2882         hooks_invoke(AVATAR_IMAGE_RENDER_HOOKLIST, avatarr);
2883
2884         procmsg_msginfo_free(&full_msginfo);
2885
2886         if (avatarr->image != NULL)
2887                 picture = gtk_image_get_pixbuf(GTK_IMAGE(avatarr->image));
2888
2889 #ifndef USE_ALT_ADDRBOOK
2890         addressbook_add_contact(msginfo->fromname, from, NULL, picture);
2891 #else
2892         if (addressadd_selection(msginfo->fromname, from, NULL, picture)) {
2893                 debug_print( "addressbook_add_contact - added\n" );
2894         }
2895 #endif
2896         avatars_avatarrender_free(avatarr);
2897 }
2898
2899 static void create_filter_cb(GtkAction *gaction, gpointer data)
2900 {
2901         MessageView *messageview = (MessageView *)data;
2902         FolderItem * item;
2903         gint action = -1;
2904         const gchar *a_name = gtk_action_get_name(gaction);
2905
2906         if (!messageview->msginfo) return;
2907
2908         DO_ACTION("Tools/CreateFilterRule/Automatically", FILTER_BY_AUTO);
2909         DO_ACTION("Tools/CreateFilterRule/ByFrom", FILTER_BY_FROM);
2910         DO_ACTION("Tools/CreateFilterRule/ByTo", FILTER_BY_TO);
2911         DO_ACTION("Tools/CreateFilterRule/BySubject", FILTER_BY_SUBJECT);
2912         
2913         item = messageview->msginfo->folder;
2914         summary_msginfo_filter_open(item,  messageview->msginfo,
2915                                     (PrefsFilterType)action, 0);
2916 }
2917
2918 static void create_processing_cb(GtkAction *gaction, gpointer data)
2919 {
2920         MessageView *messageview = (MessageView *)data;
2921         FolderItem * item;
2922         gint action = -1;
2923         const gchar *a_name = gtk_action_get_name(gaction);
2924         
2925         if (!messageview->msginfo) return;
2926         
2927         DO_ACTION("Tools/CreateProcessingRule/Automatically", FILTER_BY_AUTO);
2928         DO_ACTION("Tools/CreateProcessingRule/ByFrom", FILTER_BY_FROM);
2929         DO_ACTION("Tools/CreateProcessingRule/ByTo", FILTER_BY_TO);
2930         DO_ACTION("Tools/CreateProcessingRule/BySubject", FILTER_BY_SUBJECT);
2931
2932         item = messageview->msginfo->folder;
2933         summary_msginfo_filter_open(item,  messageview->msginfo,
2934                                     (PrefsFilterType)action, 1);
2935 }
2936
2937 static void open_urls_cb(GtkAction *action, gpointer data)
2938 {
2939         MessageView *messageview = (MessageView *)data;
2940         messageview_list_urls(messageview);
2941 }
2942
2943 static void about_cb(GtkAction *gaction, gpointer data)
2944 {
2945         about_show();
2946 }
2947
2948 static gboolean messageview_update_msg(gpointer source, gpointer data)
2949 {
2950         MsgInfoUpdate *msginfo_update = (MsgInfoUpdate *) source;
2951         MessageView *messageview = (MessageView *)data;
2952         MsgInfo *old_msginfo = messageview->msginfo;
2953
2954         if (messageview->msginfo != msginfo_update->msginfo)
2955                 return FALSE;
2956
2957         if ((msginfo_update->flags & MSGINFO_UPDATE_DELETED) ||
2958             MSG_IS_DELETED(old_msginfo->flags))
2959         {
2960                 if (messageview->new_window) {
2961                         if (old_msginfo->folder && old_msginfo->folder->total_msgs == 0) {
2962                                 messageview_clear(messageview);
2963                                 textview_show_info(messageview->mimeview->textview,
2964                                         _("\n  There are no messages in this folder"));
2965                                 return FALSE;
2966                         }
2967                         
2968                         if (!prefs_common.always_show_msg) {
2969                                 messageview_clear(messageview);
2970                                 textview_show_info(messageview->mimeview->textview,
2971                                         MSG_IS_DELETED(old_msginfo->flags) ?
2972                                         _("\n  Message has been deleted") :
2973                                         _("\n  Message has been deleted or moved to another folder"));
2974                         } else
2975                                 messageview->update_needed = TRUE;
2976
2977                 } else {
2978                         messageview_clear(messageview);
2979                         messageview_update(messageview, old_msginfo);
2980                 }
2981         } 
2982
2983         return FALSE;
2984 }
2985
2986 void messageview_set_menu_sensitive(MessageView *messageview)
2987 {
2988         if (!messageview || !messageview->ui_manager)
2989                 return;
2990
2991         cm_toggle_menu_set_active_full(messageview->ui_manager, "Menu/View/Quotes/CollapseAll", (prefs_common.hide_quotes == 1));
2992         cm_toggle_menu_set_active_full(messageview->ui_manager, "Menu/View/Quotes/Collapse2", (prefs_common.hide_quotes == 2));
2993         cm_toggle_menu_set_active_full(messageview->ui_manager, "Menu/View/Quotes/Collapse3", (prefs_common.hide_quotes == 3));
2994         cm_menu_set_sensitive_full(messageview->ui_manager, "Menu/View/Goto/PrevHistory", messageview_nav_has_prev(messageview));
2995         cm_menu_set_sensitive_full(messageview->ui_manager, "Menu/View/Goto/NextHistory", messageview_nav_has_next(messageview));
2996
2997         cm_menu_set_sensitive_full(messageview->ui_manager, "Menu/Message/CheckSignature", messageview->mimeview->signed_part);
2998 }
2999
3000 void messageview_learn (MessageView *msgview, gboolean is_spam)
3001 {
3002         if (is_spam) {
3003                 if (procmsg_spam_learner_learn(msgview->msginfo, NULL, TRUE) == 0)
3004                         procmsg_msginfo_set_flags(msgview->msginfo, MSG_SPAM, 0);
3005                 else
3006                         log_error(LOG_PROTOCOL, _("An error happened while learning.\n"));
3007                 
3008         } else {
3009                 if (procmsg_spam_learner_learn(msgview->msginfo, NULL, FALSE) == 0)
3010                         procmsg_msginfo_unset_flags(msgview->msginfo, MSG_SPAM, 0);
3011                 else
3012                         log_error(LOG_PROTOCOL, _("An error happened while learning.\n"));
3013         }
3014         if (msgview->toolbar)
3015                 toolbar_set_learn_button
3016                         (msgview->toolbar,
3017                          MSG_IS_SPAM(msgview->msginfo->flags)?LEARN_HAM:LEARN_SPAM);
3018         else
3019                 toolbar_set_learn_button
3020                         (msgview->mainwin->toolbar,
3021                          MSG_IS_SPAM(msgview->msginfo->flags)?LEARN_HAM:LEARN_SPAM);
3022 }
3023
3024 void messageview_list_urls (MessageView *msgview)
3025 {
3026         GSList *cur = msgview->mimeview->textview->uri_list;
3027         GSList *newlist = NULL;
3028         GHashTable *uri_hashtable;
3029         gchar *tmp;
3030         
3031         uri_hashtable = g_hash_table_new_full(g_str_hash, g_str_equal,
3032                                          (GDestroyNotify) g_free, NULL);
3033         
3034         for (; cur; cur = cur->next) {
3035                 ClickableText *uri = (ClickableText *)cur->data;
3036                 if (uri->uri &&
3037                     (!g_ascii_strncasecmp(uri->uri, "ftp.", 4) ||
3038                      !g_ascii_strncasecmp(uri->uri, "ftp:", 4) ||
3039                      !g_ascii_strncasecmp(uri->uri, "www.", 4) ||
3040                      !g_ascii_strncasecmp(uri->uri, "http:", 5) ||
3041                      !g_ascii_strncasecmp(uri->uri, "https:", 6)))
3042                 {
3043                         tmp = g_utf8_strdown(uri->uri, -1);
3044                         
3045                         if (g_hash_table_lookup(uri_hashtable, tmp)) {
3046                                 g_free(tmp);
3047                                 continue;
3048                         }
3049                         
3050                         newlist = g_slist_prepend(newlist, uri);
3051                         g_hash_table_insert(uri_hashtable, tmp,
3052                                             GUINT_TO_POINTER(g_str_hash(tmp)));
3053                 }
3054         }
3055         newlist = g_slist_reverse(newlist);
3056         uri_opener_open(msgview, newlist);
3057         g_slist_free(newlist);
3058         g_hash_table_destroy(uri_hashtable);
3059 }
3060
3061 static void save_part_as_cb(GtkAction *action, gpointer data)
3062 {
3063         MessageView *messageview = (MessageView *)data;
3064
3065         if (messageview->mimeview)
3066                 mimeview_save_as(messageview->mimeview);
3067 }
3068
3069 static void view_part_as_text_cb(GtkAction *action, gpointer data)
3070 {
3071         MessageView *messageview = (MessageView *)data;
3072
3073         if (messageview->mimeview)
3074                 mimeview_display_as_text(messageview->mimeview);
3075 }
3076
3077 static void open_part_cb(GtkAction *action, gpointer data)
3078 {
3079         MessageView *messageview = (MessageView *)data;
3080
3081         if (messageview->mimeview)
3082                 mimeview_launch(messageview->mimeview, NULL);
3083 }
3084 #ifndef G_OS_WIN32
3085 static void open_part_with_cb(GtkAction *action, gpointer data)
3086 {
3087         MessageView *messageview = (MessageView *)data;
3088
3089         if (messageview->mimeview)
3090                 mimeview_open_with(messageview->mimeview);
3091 }
3092 #endif
3093 static void check_signature_cb(GtkAction *action, gpointer data)
3094 {
3095         MessageView *messageview = (MessageView *)data;
3096
3097         if (messageview->mimeview)
3098                 mimeview_check_signature(messageview->mimeview);
3099 }
3100
3101 static void goto_next_part_cb(GtkAction *action, gpointer data)
3102 {
3103         MessageView *messageview = (MessageView *)data;
3104
3105         if (messageview->mimeview)
3106                 mimeview_select_next_part(messageview->mimeview);
3107 }
3108
3109 static void goto_prev_part_cb(GtkAction *action, gpointer data)
3110 {
3111         MessageView *messageview = (MessageView *)data;
3112
3113         if (messageview->mimeview)
3114                 mimeview_select_prev_part(messageview->mimeview);
3115 }
3116