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