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