2011-10-31 [pawel] 3.7.10cvs66
[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_with_lock(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         return mimeview_pass_key_press_event(messageview->mimeview, event);
1731 }
1732 #endif
1733
1734 static void messageview_show_partial_display_cb(NoticeView *noticeview, MessageView *messageview)
1735 {
1736         messageview->show_full_text = TRUE;
1737         main_window_cursor_wait(mainwindow_get_mainwindow());
1738         noticeview_hide(messageview->noticeview);
1739         messageview->partial_display_shown = FALSE;
1740         GTK_EVENTS_FLUSH();
1741         mimeview_handle_cmd(messageview->mimeview, "sc://display_as_text", NULL, NULL);
1742         main_window_cursor_normal(mainwindow_get_mainwindow());
1743 }
1744
1745 void messageview_show_partial_display(MessageView *messageview, MsgInfo *msginfo,
1746                                              size_t length)
1747 {
1748         gchar *msg = g_strdup_printf(_("Show all %s."), to_human_readable((goffset)length));
1749         noticeview_set_icon(messageview->noticeview, STOCK_PIXMAP_NOTICE_WARN);
1750         noticeview_set_text(messageview->noticeview, _("Only the first megabyte of text is shown."));
1751         noticeview_set_button_text(messageview->noticeview, msg);
1752         g_free(msg);
1753         noticeview_set_button_press_callback(messageview->noticeview,
1754                                              G_CALLBACK(messageview_show_partial_display_cb),
1755                                              (gpointer) messageview);
1756         noticeview_show(messageview->noticeview);
1757         messageview->partial_display_shown = TRUE;
1758 }
1759
1760 static void return_receipt_show(NoticeView *noticeview, MsgInfo *msginfo)
1761 {
1762         gchar *addr = NULL;
1763         gboolean from_me = FALSE;
1764         if (msginfo->folder 
1765                 && (folder_has_parent_of_type(msginfo->folder, F_QUEUE)
1766                  || folder_has_parent_of_type(msginfo->folder, F_DRAFT)))
1767                 return;
1768
1769         addr = g_strdup(msginfo->from);
1770         if (addr) {
1771                 extract_address(addr);
1772                 if (account_find_from_address(addr, FALSE)) {
1773                         from_me = TRUE;
1774                 }
1775                 g_free(addr);
1776         }
1777
1778         if (from_me) {
1779                 noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
1780                 if (MSG_IS_RETRCPT_GOT(msginfo->flags)) {
1781                         noticeview_set_text(noticeview, _("You got a return receipt for this message : "
1782                                                           "it has been displayed by the recipient."));
1783                 } else {
1784                         noticeview_set_text(noticeview, _("You asked for a return receipt in this message."));
1785                 }
1786                 noticeview_set_button_text(noticeview, NULL);
1787                 noticeview_set_button_press_callback(noticeview, NULL, NULL);
1788         } else {
1789                 noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
1790                 noticeview_set_text(noticeview, _("This message asks for a return receipt."));
1791                 noticeview_set_button_text(noticeview, _("Send receipt"));
1792                 noticeview_set_button_press_callback(noticeview,
1793                                                      G_CALLBACK(return_receipt_send_clicked),
1794                                                      (gpointer) msginfo);
1795         }
1796         noticeview_show(noticeview);
1797 }
1798
1799 static void return_receipt_send_clicked(NoticeView *noticeview, MsgInfo *msginfo)
1800 {
1801         MsgInfo *tmpmsginfo;
1802         gchar *file;
1803
1804         file = procmsg_get_message_file_path(msginfo);
1805         if (!file) {
1806                 g_warning("can't get message file path.\n");
1807                 return;
1808         }
1809
1810         tmpmsginfo = procheader_parse_file(file, msginfo->flags, TRUE, TRUE);
1811         tmpmsginfo->folder = msginfo->folder;
1812         tmpmsginfo->msgnum = msginfo->msgnum;
1813
1814         if (disposition_notification_send(tmpmsginfo) >= 0) {
1815                 procmsg_msginfo_set_flags(msginfo, MSG_RETRCPT_SENT, 0);
1816                 noticeview_hide(noticeview);
1817         }               
1818
1819         procmsg_msginfo_free(tmpmsginfo);
1820         g_free(file);
1821 }
1822
1823 static void partial_recv_show(NoticeView *noticeview, MsgInfo *msginfo)
1824 {
1825         gchar *text = NULL;
1826         gchar *button1 = NULL;
1827         gchar *button2 = NULL;
1828         void  *button1_cb = NULL;
1829         void  *button2_cb = NULL;
1830
1831         if (!msginfo->extradata)
1832                 return;
1833         if (!partial_msg_in_uidl_list(msginfo)) {
1834                 text = g_strdup_printf(_("This message has been partially "
1835                                 "retrieved,\nand has been deleted from the "
1836                                 "server."));
1837         } else {
1838                 switch (msginfo->planned_download) {
1839                 case POP3_PARTIAL_DLOAD_UNKN:
1840                         text = g_strdup_printf(_("This message has been "
1841                                         "partially retrieved;\nit is %s."),
1842                                         to_human_readable(
1843                                                 (goffset)(msginfo->total_size)));
1844                         button1 = _("Mark for download");
1845                         button2 = _("Mark for deletion");
1846                         button1_cb = partial_recv_dload_clicked;
1847                         button2_cb = partial_recv_del_clicked;
1848                         break;
1849                 case POP3_PARTIAL_DLOAD_DLOAD:
1850                         text = g_strdup_printf(_("This message has been "
1851                                         "partially retrieved;\nit is %s and "
1852                                         "will be downloaded."),
1853                                         to_human_readable(
1854                                                 (goffset)(msginfo->total_size)));
1855                         button1 = _("Unmark");
1856                         button1_cb = partial_recv_unmark_clicked;
1857                         button2 = _("Mark for deletion");
1858                         button2_cb = partial_recv_del_clicked;
1859                         break;
1860                 case POP3_PARTIAL_DLOAD_DELE:
1861                         text = g_strdup_printf(_("This message has been "
1862                                         "partially retrieved;\nit is %s and "
1863                                         "will be deleted."),
1864                                         to_human_readable(
1865                                                 (goffset)(msginfo->total_size)));
1866                         button1 = _("Mark for download");
1867                         button1_cb = partial_recv_dload_clicked;
1868                         button2 = _("Unmark");
1869                         button2_cb = partial_recv_unmark_clicked;
1870                         break;
1871                 default:
1872                         return;
1873                 }
1874         }
1875         
1876         noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
1877         noticeview_set_text(noticeview, text);
1878         g_free(text);
1879         noticeview_set_button_text(noticeview, button1);
1880         noticeview_set_button_press_callback(noticeview,
1881                      G_CALLBACK(button1_cb), (gpointer) msginfo);
1882
1883         noticeview_set_2ndbutton_text(noticeview, button2);
1884         noticeview_set_2ndbutton_press_callback(noticeview,
1885                      G_CALLBACK(button2_cb), (gpointer) msginfo);
1886
1887         noticeview_show(noticeview);
1888 }
1889
1890 static void partial_recv_dload_clicked(NoticeView *noticeview, 
1891                                        MsgInfo *msginfo)
1892 {
1893         if (partial_mark_for_download(msginfo) == 0) {
1894                 partial_recv_show(noticeview, msginfo);
1895         }
1896 }
1897
1898 static void partial_recv_del_clicked(NoticeView *noticeview, 
1899                                        MsgInfo *msginfo)
1900 {
1901         if (partial_mark_for_delete(msginfo) == 0) {
1902                 partial_recv_show(noticeview, msginfo);
1903         }
1904 }
1905
1906 static void partial_recv_unmark_clicked(NoticeView *noticeview, 
1907                                        MsgInfo *msginfo)
1908 {
1909         if (partial_unmark(msginfo) == 0) {
1910                 partial_recv_show(noticeview, msginfo);
1911         }
1912 }
1913
1914 static void select_account_cb(GtkWidget *w, gpointer data)
1915 {
1916         *(gint*)data = combobox_get_active_data(GTK_COMBO_BOX(w));
1917 }
1918
1919 static PrefsAccount *select_account_from_list(GList *ac_list)
1920 {
1921         GtkWidget *optmenu;
1922         gint account_id;
1923
1924         cm_return_val_if_fail(ac_list != NULL, NULL);
1925         cm_return_val_if_fail(ac_list->data != NULL, NULL);
1926         
1927         optmenu = gtkut_account_menu_new(ac_list,
1928                         G_CALLBACK(select_account_cb),
1929                         &account_id);
1930         if (!optmenu)
1931                 return NULL;
1932         account_id = ((PrefsAccount *) ac_list->data)->account_id;
1933         if (alertpanel_with_widget(
1934                                 _("Return Receipt Notification"),
1935                                 _("The message was sent to several of your "
1936                                   "accounts.\n"
1937                                   "Please choose which account do you want to "
1938                                   "use for sending the receipt notification:"),
1939                                 _("_Cancel"), _("_Send Notification"), NULL,
1940                                 FALSE, G_ALERTDEFAULT, optmenu) != G_ALERTALTERNATE)
1941                 return NULL;
1942         return account_find_from_id(account_id);
1943 }
1944
1945 /* 
1946  * \brief return selected messageview text, when nothing is 
1947  *        selected and message was filtered, return complete text
1948  *
1949  * \param  pointer to Messageview 
1950  *
1951  * \return pointer to text (needs to be free'd by calling func)
1952  */
1953 gchar *messageview_get_selection(MessageView *msgview)
1954 {
1955         TextView *textview;
1956         gchar *text = NULL;
1957         GtkTextView *edit = NULL;
1958         GtkTextBuffer *textbuf;
1959         gint body_pos = 0;
1960         
1961         cm_return_val_if_fail(msgview != NULL, NULL);
1962
1963         if (msgview->mimeview->type == MIMEVIEW_VIEWER) {
1964                 MimeViewer *viewer = msgview->mimeview->mimeviewer;
1965                 if (viewer && viewer->get_selection) {
1966                         text = viewer->get_selection(viewer);
1967                         if (text)
1968                                 return text;
1969                 }
1970         }
1971
1972         textview = messageview_get_current_textview(msgview);
1973         cm_return_val_if_fail(textview != NULL, NULL);
1974
1975         edit = GTK_TEXT_VIEW(textview->text);
1976         cm_return_val_if_fail(edit != NULL, NULL);
1977         body_pos = textview->body_pos;
1978
1979         textbuf = gtk_text_view_get_buffer(edit);
1980
1981         if (gtk_text_buffer_get_selection_bounds(textbuf, NULL, NULL))
1982                 return gtkut_text_view_get_selection(edit);
1983         else if (msgview->filtered) {
1984                 GtkTextIter start_iter, end_iter;
1985                 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter, body_pos);
1986                 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
1987                 gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter, FALSE);
1988         } else
1989                 text = NULL;
1990
1991         return text;
1992 }
1993
1994 static void save_as_cb(GtkAction *action, gpointer data)
1995 {
1996         MessageView *messageview = (MessageView *)data;
1997         messageview_save_as(messageview);
1998 }
1999
2000 static void print_mimeview(MimeView *mimeview, gint sel_start, gint sel_end, gint partnum) 
2001 {
2002         MainWindow *mainwin;
2003         if (!mimeview 
2004         ||  !mimeview->textview
2005         ||  !mimeview->textview->text)
2006                 alertpanel_warning(_("Cannot print: the message doesn't "
2007                                      "contain text."));
2008         else {
2009                 gtk_widget_realize(mimeview->textview->text);
2010                 if (partnum > 0) {
2011                         mimeview_select_part_num(mimeview, partnum);
2012                 }
2013                 if (mimeview->type == MIMEVIEW_VIEWER) {
2014                         MimeViewer *viewer = mimeview->mimeviewer;
2015                         if (viewer && viewer->print) {
2016                                 viewer->print(viewer);
2017                                 return;
2018                         }
2019                 }
2020                 if (sel_start != -1 && sel_end != -1) {
2021                         GtkTextIter start, end;
2022                         GtkTextView *text = GTK_TEXT_VIEW(mimeview->textview->text);
2023                         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
2024
2025                         gtk_text_buffer_get_iter_at_offset(buffer, &start, sel_start);
2026                         gtk_text_buffer_get_iter_at_offset(buffer, &end, sel_end);
2027                         gtk_text_buffer_select_range(buffer, &start, &end);
2028                 }
2029                 /* TODO: Get the real parent window, not the main window */
2030                 mainwin = mainwindow_get_mainwindow();
2031                 printing_print(GTK_TEXT_VIEW(mimeview->textview->text),
2032                                mainwin ? GTK_WINDOW(mainwin->window) : NULL,
2033                                 sel_start, sel_end);
2034         }
2035 }
2036
2037 void messageview_print(MsgInfo *msginfo, gboolean all_headers, 
2038                         gint sel_start, gint sel_end, gint partnum) 
2039 {
2040         PangoFontDescription *font_desc = NULL;
2041         MessageView *tmpview = messageview_create_with_new_window_visible(
2042                                 mainwindow_get_mainwindow(), FALSE);
2043
2044         if (prefs_common.use_different_print_font) {
2045                 font_desc = pango_font_description_from_string
2046                                                 (prefs_common.printfont);
2047         } else {
2048                 font_desc = pango_font_description_from_string
2049                                                 (prefs_common.textfont);
2050         }
2051         if (font_desc) {
2052                 gtk_widget_modify_font(tmpview->mimeview->textview->text, 
2053                         font_desc);
2054                 pango_font_description_free(font_desc);
2055         }
2056
2057         tmpview->all_headers = all_headers;
2058         if (msginfo && messageview_show(tmpview, msginfo, 
2059                 tmpview->all_headers) >= 0) {
2060                         print_mimeview(tmpview->mimeview, 
2061                                 sel_start, sel_end, partnum);
2062         }
2063         messageview_destroy(tmpview);
2064 }
2065
2066 static void page_setup_cb(GtkAction *action, gpointer data)
2067 {
2068         MessageView *messageview = (MessageView *)data;
2069         printing_page_setup(messageview ?
2070                             GTK_WINDOW(messageview->window) : NULL);
2071 }
2072
2073 static void print_cb(GtkAction *action, gpointer data)
2074 {
2075         MessageView *messageview = (MessageView *)data;
2076         gint sel_start = -1, sel_end = -1, partnum = 0;
2077
2078         if (!messageview->msginfo) return;
2079
2080         partnum = mimeview_get_selected_part_num(messageview->mimeview);
2081         textview_get_selection_offsets(messageview->mimeview->textview,
2082                 &sel_start, &sel_end);
2083         messageview_print(messageview->msginfo, messageview->all_headers, 
2084                 sel_start, sel_end, partnum);
2085 }
2086
2087 static void close_cb(GtkAction *action, gpointer data)
2088 {
2089         MessageView *messageview = (MessageView *)data;
2090         messageview_destroy(messageview);
2091 }
2092
2093 static void copy_cb(GtkAction *action, gpointer data)
2094 {
2095         MessageView *messageview = (MessageView *)data;
2096         messageview_copy_clipboard(messageview);
2097 }
2098
2099 static void allsel_cb(GtkAction *action, gpointer data)
2100 {
2101         MessageView *messageview = (MessageView *)data;
2102         messageview_select_all(messageview);
2103 }
2104
2105 static void search_cb(GtkAction *action, gpointer data)
2106 {
2107         MessageView *messageview = (MessageView *)data;
2108         message_search(messageview);
2109 }
2110
2111 static void prev_cb(GtkAction *action, gpointer data)
2112 {
2113         MessageView *messageview = (MessageView *)data;
2114         messageview->updating = TRUE;
2115         summary_step(messageview->mainwin->summaryview, GTK_SCROLL_STEP_BACKWARD);
2116         messageview->updating = FALSE;
2117
2118         if (messageview->deferred_destroy) {
2119                 debug_print("messageview got away!\n");
2120                 messageview_destroy(messageview);
2121                 return;
2122         }
2123         if (messageview->mainwin->summaryview->selected) {
2124 #ifndef GENERIC_UMPC
2125                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2126                        
2127                 if (msginfo)
2128                         messageview_show(messageview, msginfo, 
2129                                          messageview->all_headers);
2130 #endif
2131         } else {
2132                 gtk_widget_destroy(messageview->window);
2133         }
2134 }
2135
2136 static void next_cb(GtkAction *action, gpointer data)
2137 {
2138         MessageView *messageview = (MessageView *)data;
2139         messageview->updating = TRUE;
2140         summary_step(messageview->mainwin->summaryview, GTK_SCROLL_STEP_FORWARD);
2141         messageview->updating = FALSE;
2142
2143         if (messageview->deferred_destroy) {
2144                 debug_print("messageview got away!\n");
2145                 messageview_destroy(messageview);
2146                 return;
2147         }
2148         if (messageview->mainwin->summaryview->selected) {
2149 #ifndef GENERIC_UMPC
2150                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2151                        
2152                 if (msginfo)
2153                         messageview_show(messageview, msginfo, 
2154                                          messageview->all_headers);
2155 #endif
2156         } else {
2157                 gtk_widget_destroy(messageview->window);
2158         }
2159 }
2160
2161 static void prev_unread_cb(GtkAction *action, gpointer data)
2162 {
2163         MessageView *messageview = (MessageView *)data;
2164         messageview->updating = TRUE;
2165         summary_select_prev_unread(messageview->mainwin->summaryview);
2166         messageview->updating = FALSE;
2167
2168         if (messageview->deferred_destroy) {
2169                 debug_print("messageview got away!\n");
2170                 messageview_destroy(messageview);
2171                 return;
2172         }
2173         if (messageview->mainwin->summaryview->selected) {
2174 #ifndef GENERIC_UMPC
2175                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2176                        
2177                 if (msginfo)
2178                         messageview_show(messageview, msginfo, 
2179                                          messageview->all_headers);
2180 #endif
2181         } else {
2182                 gtk_widget_destroy(messageview->window);
2183         }
2184 }
2185
2186 static void next_unread_cb(GtkAction *action, gpointer data)
2187 {
2188         MessageView *messageview = (MessageView *)data;
2189         messageview->updating = TRUE;
2190         summary_select_next_unread(messageview->mainwin->summaryview);
2191         messageview->updating = FALSE;
2192
2193         if (messageview->deferred_destroy) {
2194                 debug_print("messageview got away!\n");
2195                 messageview_destroy(messageview);
2196                 return;
2197         }
2198         if (messageview->mainwin->summaryview->selected) {
2199 #ifndef GENERIC_UMPC
2200                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2201                        
2202                 if (msginfo)
2203                         messageview_show(messageview, msginfo, 
2204                                          messageview->all_headers);
2205 #endif
2206         } else {
2207                 gtk_widget_destroy(messageview->window);
2208         }
2209 }
2210
2211 static void prev_new_cb(GtkAction *action, gpointer data)
2212 {
2213         MessageView *messageview = (MessageView *)data;
2214         messageview->updating = TRUE;
2215         summary_select_prev_new(messageview->mainwin->summaryview);
2216         messageview->updating = FALSE;
2217
2218         if (messageview->deferred_destroy) {
2219                 debug_print("messageview got away!\n");
2220                 messageview_destroy(messageview);
2221                 return;
2222         }
2223         if (messageview->mainwin->summaryview->selected) {
2224 #ifndef GENERIC_UMPC
2225                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2226                        
2227                 if (msginfo)
2228                         messageview_show(messageview, msginfo, 
2229                                          messageview->all_headers);
2230 #endif
2231         } else {
2232                 gtk_widget_destroy(messageview->window);
2233         }
2234 }
2235
2236 static void next_new_cb(GtkAction *action, gpointer data)
2237 {
2238         MessageView *messageview = (MessageView *)data;
2239         messageview->updating = TRUE;
2240         summary_select_next_new(messageview->mainwin->summaryview);
2241         messageview->updating = FALSE;
2242
2243         if (messageview->deferred_destroy) {
2244                 debug_print("messageview got away!\n");
2245                 messageview_destroy(messageview);
2246                 return;
2247         }
2248         if (messageview->mainwin->summaryview->selected) {
2249 #ifndef GENERIC_UMPC
2250                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2251                        
2252                 if (msginfo)
2253                         messageview_show(messageview, msginfo, 
2254                                          messageview->all_headers);
2255 #endif
2256         } else {
2257                 gtk_widget_destroy(messageview->window);
2258         }
2259 }
2260
2261 static void prev_marked_cb(GtkAction *action, gpointer data)
2262 {
2263         MessageView *messageview = (MessageView *)data;
2264         messageview->updating = TRUE;
2265         summary_select_prev_marked(messageview->mainwin->summaryview);
2266         messageview->updating = FALSE;
2267
2268         if (messageview->deferred_destroy) {
2269                 debug_print("messageview got away!\n");
2270                 messageview_destroy(messageview);
2271                 return;
2272         }
2273         if (messageview->mainwin->summaryview->selected) {
2274 #ifndef GENERIC_UMPC
2275                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2276                        
2277                 if (msginfo)
2278                         messageview_show(messageview, msginfo, 
2279                                          messageview->all_headers);
2280 #endif
2281         } else {
2282                 gtk_widget_destroy(messageview->window);
2283         }
2284 }
2285
2286 static void next_marked_cb(GtkAction *action, gpointer data)
2287 {
2288         MessageView *messageview = (MessageView *)data;
2289         messageview->updating = TRUE;
2290         summary_select_next_marked(messageview->mainwin->summaryview);
2291         messageview->updating = FALSE;
2292
2293         if (messageview->deferred_destroy) {
2294                 debug_print("messageview got away!\n");
2295                 messageview_destroy(messageview);
2296                 return;
2297         }
2298         if (messageview->mainwin->summaryview->selected) {
2299 #ifndef GENERIC_UMPC
2300                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2301                        
2302                 if (msginfo)
2303                         messageview_show(messageview, msginfo, 
2304                                          messageview->all_headers);
2305 #endif
2306         } else {
2307                 gtk_widget_destroy(messageview->window);
2308         }
2309 }
2310
2311 static void prev_labeled_cb(GtkAction *action, gpointer data)
2312 {
2313         MessageView *messageview = (MessageView *)data;
2314         messageview->updating = TRUE;
2315         summary_select_prev_labeled(messageview->mainwin->summaryview);
2316         messageview->updating = FALSE;
2317
2318         if (messageview->deferred_destroy) {
2319                 debug_print("messageview got away!\n");
2320                 messageview_destroy(messageview);
2321                 return;
2322         }
2323         if (messageview->mainwin->summaryview->selected) {
2324 #ifndef GENERIC_UMPC
2325                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2326                        
2327                 if (msginfo)
2328                         messageview_show(messageview, msginfo, 
2329                                          messageview->all_headers);
2330 #endif
2331         } else {
2332                 gtk_widget_destroy(messageview->window);
2333         }
2334 }
2335
2336 static void next_labeled_cb(GtkAction *action, gpointer data)
2337 {
2338         MessageView *messageview = (MessageView *)data;
2339         messageview->updating = TRUE;
2340         summary_select_next_labeled(messageview->mainwin->summaryview);
2341         messageview->updating = FALSE;
2342
2343         if (messageview->deferred_destroy) {
2344                 debug_print("messageview got away!\n");
2345                 messageview_destroy(messageview);
2346                 return;
2347         }
2348         if (messageview->mainwin->summaryview->selected) {
2349 #ifndef GENERIC_UMPC
2350                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2351                        
2352                 if (msginfo)
2353                         messageview_show(messageview, msginfo, 
2354                                          messageview->all_headers);
2355 #endif
2356         } else {
2357                 gtk_widget_destroy(messageview->window);
2358         }
2359 }
2360
2361 static void last_read_cb(GtkAction *action, gpointer data)
2362 {
2363         MessageView *messageview = (MessageView *)data;
2364         messageview->updating = TRUE;
2365         summary_select_last_read(messageview->mainwin->summaryview);
2366         messageview->updating = FALSE;
2367
2368         if (messageview->deferred_destroy) {
2369                 debug_print("messageview got away!\n");
2370                 messageview_destroy(messageview);
2371                 return;
2372         }
2373         if (messageview->mainwin->summaryview->selected) {
2374 #ifndef GENERIC_UMPC
2375                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2376                        
2377                 if (msginfo)
2378                         messageview_show(messageview, msginfo, 
2379                                          messageview->all_headers);
2380 #endif
2381         } else {
2382                 gtk_widget_destroy(messageview->window);
2383         }
2384 }
2385
2386 static void parent_cb(GtkAction *action, gpointer data)
2387 {
2388         MessageView *messageview = (MessageView *)data;
2389         messageview->updating = TRUE;
2390         summary_select_parent(messageview->mainwin->summaryview);
2391         messageview->updating = FALSE;
2392
2393         if (messageview->deferred_destroy) {
2394                 debug_print("messageview got away!\n");
2395                 messageview_destroy(messageview);
2396                 return;
2397         }
2398         if (messageview->mainwin->summaryview->selected) {
2399 #ifndef GENERIC_UMPC
2400                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2401                        
2402                 if (msginfo)
2403                         messageview_show(messageview, msginfo, 
2404                                          messageview->all_headers);
2405 #endif
2406         } else {
2407                 gtk_widget_destroy(messageview->window);
2408         }
2409 }
2410
2411 static void goto_unread_folder_cb(GtkAction *action, gpointer data)
2412 {
2413         MessageView *messageview = (MessageView *)data;
2414         messageview->updating = TRUE;
2415         folderview_select_next_unread(messageview->mainwin->folderview, FALSE);
2416         messageview->updating = FALSE;
2417
2418         if (messageview->deferred_destroy) {
2419                 debug_print("messageview got away!\n");
2420                 messageview_destroy(messageview);
2421                 return;
2422         }
2423         if (messageview->mainwin->summaryview->selected) {
2424 #ifndef GENERIC_UMPC
2425                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2426                        
2427                 if (msginfo)
2428                         messageview_show(messageview, msginfo, 
2429                                          messageview->all_headers);
2430 #endif
2431         } else {
2432                 gtk_widget_destroy(messageview->window);
2433         }
2434 }
2435
2436 static void goto_folder_cb(GtkAction *action, gpointer data)
2437 {
2438         MessageView *messageview = (MessageView *)data;
2439         messageview->updating = TRUE;
2440         FolderItem *to_folder;
2441         messageview->updating = FALSE;
2442
2443         to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL, FALSE);
2444
2445         if (to_folder) {
2446                 folderview_select(messageview->mainwin->folderview, to_folder);
2447
2448                 if (messageview->deferred_destroy) {
2449                         debug_print("messageview got away!\n");
2450                         messageview_destroy(messageview);
2451                         return;
2452                 }
2453                 if (messageview->mainwin->summaryview->selected) {
2454 #ifndef GENERIC_UMPC
2455                         MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2456                        
2457                         if (msginfo)
2458                                 messageview_show(messageview, msginfo, 
2459                                                  messageview->all_headers);
2460 #endif
2461                 } else {
2462                         gtk_widget_destroy(messageview->window);
2463                 }
2464         }
2465 }
2466
2467 static void scroll_prev_line_cb(GtkAction *action, gpointer data)
2468 {
2469         MessageView *messageview = (MessageView *)data;
2470         mimeview_scroll_one_line(messageview->mimeview,TRUE);
2471 }
2472
2473 static void scroll_next_line_cb(GtkAction *action, gpointer data)
2474 {
2475         MessageView *messageview = (MessageView *)data;
2476         mimeview_scroll_one_line(messageview->mimeview,FALSE);
2477 }
2478
2479 static void scroll_prev_page_cb(GtkAction *action, gpointer data)
2480 {
2481         MessageView *messageview = (MessageView *)data;
2482         mimeview_scroll_page(messageview->mimeview,TRUE);
2483 }
2484
2485 static void scroll_next_page_cb(GtkAction *action, gpointer data)
2486 {
2487         MessageView *messageview = (MessageView *)data;
2488         mimeview_scroll_page(messageview->mimeview,FALSE);
2489 }
2490
2491 static void set_charset_cb(GtkAction *action, GtkRadioAction *current, gpointer data)
2492 {
2493         MessageView *messageview = (MessageView *)data;
2494         gboolean active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (current));
2495         gint value = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (current));
2496         const gchar *charset;
2497
2498         if (active) {
2499                 charset = conv_get_charset_str((CharSet)value);
2500                 g_free(messageview->forced_charset);
2501                 messageview->forced_charset = g_strdup(charset);
2502                 procmime_force_charset(charset);
2503                 
2504                 messageview_show(messageview, messageview->msginfo, FALSE);
2505         }
2506 }
2507
2508 static void set_decode_cb(GtkAction *action, GtkRadioAction *current, gpointer data)
2509 {
2510         MessageView *messageview = (MessageView *)data;
2511         gboolean active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (current));
2512         gint value = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (current));
2513
2514         if (active) {
2515                 messageview->forced_encoding = (EncodingType)value;
2516
2517                 messageview_show(messageview, messageview->msginfo, FALSE);
2518                 debug_print("forced encoding: %d\n", value);
2519         }
2520 }
2521
2522
2523 static void view_source_cb(GtkAction *action, gpointer data)
2524 {
2525         MessageView *messageview = (MessageView *)data;
2526         SourceWindow *srcwin;
2527
2528         if (!messageview->msginfo) return;
2529
2530         srcwin = source_window_create();
2531         source_window_show_msg(srcwin, messageview->msginfo);
2532         source_window_show(srcwin);
2533 }
2534
2535 static void show_all_header_cb(GtkToggleAction *action, gpointer data)
2536 {
2537         MessageView *messageview = (MessageView *)data;
2538         MsgInfo *msginfo = messageview->msginfo;
2539
2540         if (messageview->mimeview->textview &&
2541             messageview->mimeview->textview->loading) {
2542                 return;
2543         }
2544         if (messageview->updating)
2545                 return;
2546
2547         messageview->all_headers = 
2548                         gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
2549         if (!msginfo) return;
2550         messageview->msginfo = NULL;
2551         messageview_show(messageview, msginfo,gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
2552         procmsg_msginfo_free(msginfo);
2553         main_window_set_menu_sensitive(messageview->mainwin);
2554 }
2555
2556 static void msg_hide_quotes_cb(GtkToggleAction *action, gpointer data)
2557 {
2558         MessageView *messageview = (MessageView *)data;
2559         MsgInfo *msginfo = messageview->msginfo;
2560         static gboolean updating_menu = FALSE;
2561
2562         if (updating_menu)
2563                 return;
2564         if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action))) {
2565                 const gchar *a_name = gtk_action_get_name(GTK_ACTION(action));
2566                 if (!strcmp(a_name, "View/Quotes/CollapseAll")) prefs_common.hide_quotes = 1;
2567                 else if (!strcmp(a_name, "View/Quotes/Collapse2")) prefs_common.hide_quotes = 2;
2568                 else if (!strcmp(a_name, "View/Quotes/Collapse3")) prefs_common.hide_quotes = 3;
2569         } else
2570                 prefs_common.hide_quotes = 0;
2571         
2572         updating_menu=TRUE;
2573         
2574         cm_toggle_menu_set_active_full(messageview->ui_manager, "Menu/View/Quotes/CollapseAll", (prefs_common.hide_quotes == 1));
2575         cm_toggle_menu_set_active_full(messageview->ui_manager, "Menu/View/Quotes/Collapse2", (prefs_common.hide_quotes == 2));
2576         cm_toggle_menu_set_active_full(messageview->ui_manager, "Menu/View/Quotes/Collapse3", (prefs_common.hide_quotes == 3));
2577
2578         updating_menu=FALSE;
2579         if (!msginfo) return;
2580         messageview->msginfo = NULL;
2581         messageview_show(messageview, msginfo,
2582                          messageview->all_headers);
2583         procmsg_msginfo_free(msginfo);
2584         
2585         /* update main window */
2586         main_window_set_menu_sensitive(messageview->mainwin);
2587         summary_redisplay_msg(messageview->mainwin->summaryview);
2588 }
2589 #undef SET_CHECK_MENU_ACTIVE
2590
2591 static void compose_cb(GtkAction *action, gpointer data)
2592 {
2593         MessageView *messageview = (MessageView *)data;
2594         PrefsAccount *ac = NULL;
2595         FolderItem *item = NULL;
2596
2597         if (messageview->msginfo)
2598                 item = messageview->msginfo->folder;
2599
2600         if (item) {
2601                 ac = account_find_from_item(item);
2602                 if (ac && ac->protocol == A_NNTP &&
2603                     FOLDER_TYPE(item->folder) == F_NEWS) {
2604                         compose_new(ac, item->path, NULL);
2605                         return;
2606                 }
2607         }
2608
2609         compose_new(ac, NULL, NULL);
2610 }
2611
2612 #define DO_ACTION(name, act)    { if (!strcmp(a_name, name)) action = act; }
2613
2614 static void reply_cb(GtkAction *gaction, gpointer data)
2615 {
2616         MessageView *messageview = (MessageView *)data;
2617         GSList *msginfo_list = NULL;
2618         gint action = COMPOSE_REPLY;
2619         const gchar *a_name = gtk_action_get_name(gaction);
2620         
2621         cm_return_if_fail(messageview->msginfo);
2622
2623         DO_ACTION("Message/Reply", COMPOSE_REPLY);
2624         DO_ACTION("Message/ReplyTo/All", COMPOSE_REPLY_TO_ALL);
2625         DO_ACTION("Message/ReplyTo/Sender", COMPOSE_REPLY_TO_SENDER);
2626         DO_ACTION("Message/ReplyTo/List", COMPOSE_REPLY_TO_LIST);
2627         DO_ACTION("Message/Forward", COMPOSE_FORWARD_INLINE);
2628         DO_ACTION("Message/ForwardAtt", COMPOSE_FORWARD_AS_ATTACH);
2629         DO_ACTION("Message/Redirect", COMPOSE_REDIRECT);
2630
2631         msginfo_list = g_slist_append(msginfo_list, messageview->msginfo);
2632         compose_reply_from_messageview(messageview, msginfo_list, action);
2633         g_slist_free(msginfo_list);
2634 }
2635
2636 static void addressbook_open_cb(GtkAction *action, gpointer data)
2637 {
2638         addressbook_open(NULL);
2639 }
2640
2641 static void add_address_cb(GtkAction *action, gpointer data)
2642 {
2643         MessageView *messageview = (MessageView *)data;
2644         MsgInfo *msginfo, *full_msginfo;
2645         gchar *from;
2646         GtkWidget *image = NULL;
2647         GdkPixbuf *picture = NULL;
2648
2649         if (!messageview->msginfo || !messageview->msginfo->from) 
2650                 return;
2651
2652         msginfo = messageview->msginfo;
2653         Xstrdup_a(from, msginfo->from, return);
2654         eliminate_address_comment(from);
2655         extract_address(from);
2656         
2657         full_msginfo = procmsg_msginfo_get_full_info(msginfo);
2658         if (full_msginfo &&
2659             full_msginfo->extradata &&
2660             full_msginfo->extradata->face) {
2661                 image = face_get_from_header(full_msginfo->extradata->face);
2662         } 
2663 #if HAVE_LIBCOMPFACE
2664         else if (full_msginfo &&
2665                  full_msginfo->extradata &&
2666                  full_msginfo->extradata->xface) {
2667                 image = xface_get_from_header(full_msginfo->extradata->xface,
2668                                 &(gtk_widget_get_style(messageview->mainwin->summaryview->ctree)->white),
2669                                 gtk_widget_get_window(messageview->window));
2670         }
2671 #endif
2672         procmsg_msginfo_free(full_msginfo);
2673         if (image)
2674                 picture = gtk_image_get_pixbuf(GTK_IMAGE(image));
2675
2676         addressbook_add_contact(msginfo->fromname, from, NULL, picture);
2677
2678         if (image)
2679                 gtk_widget_destroy(image);
2680 }
2681
2682 static void create_filter_cb(GtkAction *gaction, gpointer data)
2683 {
2684         MessageView *messageview = (MessageView *)data;
2685         FolderItem * item;
2686         gint action = -1;
2687         const gchar *a_name = gtk_action_get_name(gaction);
2688
2689         if (!messageview->msginfo) return;
2690
2691         DO_ACTION("Tools/CreateFilterRule/Automatically", FILTER_BY_AUTO);
2692         DO_ACTION("Tools/CreateFilterRule/ByFrom", FILTER_BY_FROM);
2693         DO_ACTION("Tools/CreateFilterRule/ByTo", FILTER_BY_TO);
2694         DO_ACTION("Tools/CreateFilterRule/BySubject", FILTER_BY_SUBJECT);
2695         
2696         item = messageview->msginfo->folder;
2697         summary_msginfo_filter_open(item,  messageview->msginfo,
2698                                     (PrefsFilterType)action, 0);
2699 }
2700
2701 static void create_processing_cb(GtkAction *gaction, gpointer data)
2702 {
2703         MessageView *messageview = (MessageView *)data;
2704         FolderItem * item;
2705         gint action = -1;
2706         const gchar *a_name = gtk_action_get_name(gaction);
2707         
2708         if (!messageview->msginfo) return;
2709         
2710         DO_ACTION("Tools/CreateProcessingRule/Automatically", FILTER_BY_AUTO);
2711         DO_ACTION("Tools/CreateProcessingRule/ByFrom", FILTER_BY_FROM);
2712         DO_ACTION("Tools/CreateProcessingRule/ByTo", FILTER_BY_TO);
2713         DO_ACTION("Tools/CreateProcessingRule/BySubject", FILTER_BY_SUBJECT);
2714
2715         item = messageview->msginfo->folder;
2716         summary_msginfo_filter_open(item,  messageview->msginfo,
2717                                     (PrefsFilterType)action, 1);
2718 }
2719
2720 static void open_urls_cb(GtkAction *action, gpointer data)
2721 {
2722         MessageView *messageview = (MessageView *)data;
2723         messageview_list_urls(messageview);
2724 }
2725
2726 static void about_cb(GtkAction *gaction, gpointer data)
2727 {
2728         about_show();
2729 }
2730
2731 static gboolean messageview_update_msg(gpointer source, gpointer data)
2732 {
2733         MsgInfoUpdate *msginfo_update = (MsgInfoUpdate *) source;
2734         MessageView *messageview = (MessageView *)data;
2735
2736         if (messageview->msginfo != msginfo_update->msginfo)
2737                 return FALSE;
2738
2739         if (msginfo_update->flags & MSGINFO_UPDATE_DELETED) {
2740                 MsgInfo *old_msginfo = messageview->msginfo;
2741                 messageview_clear(messageview);
2742                 messageview_update(messageview, old_msginfo);
2743         }
2744
2745         return FALSE;
2746 }
2747
2748 void messageview_set_menu_sensitive(MessageView *messageview)
2749 {
2750         if (!messageview || !messageview->new_window) 
2751                 return;
2752         /* do some smart things */
2753         if (!messageview->menubar) return;
2754
2755         cm_toggle_menu_set_active_full(messageview->ui_manager, "Menu/View/Quotes/CollapseAll", (prefs_common.hide_quotes == 1));
2756         cm_toggle_menu_set_active_full(messageview->ui_manager, "Menu/View/Quotes/Collapse2", (prefs_common.hide_quotes == 2));
2757         cm_toggle_menu_set_active_full(messageview->ui_manager, "Menu/View/Quotes/Collapse3", (prefs_common.hide_quotes == 3));
2758 }
2759
2760 void messageview_learn (MessageView *msgview, gboolean is_spam)
2761 {
2762         if (is_spam) {
2763                 if (procmsg_spam_learner_learn(msgview->msginfo, NULL, TRUE) == 0)
2764                         procmsg_msginfo_set_flags(msgview->msginfo, MSG_SPAM, 0);
2765                 else
2766                         log_error(LOG_PROTOCOL, _("An error happened while learning.\n"));
2767                 
2768         } else {
2769                 if (procmsg_spam_learner_learn(msgview->msginfo, NULL, FALSE) == 0)
2770                         procmsg_msginfo_unset_flags(msgview->msginfo, MSG_SPAM, 0);
2771                 else
2772                         log_error(LOG_PROTOCOL, _("An error happened while learning.\n"));
2773         }
2774         if (msgview->toolbar)
2775                 toolbar_set_learn_button
2776                         (msgview->toolbar,
2777                          MSG_IS_SPAM(msgview->msginfo->flags)?LEARN_HAM:LEARN_SPAM);
2778         else
2779                 toolbar_set_learn_button
2780                         (msgview->mainwin->toolbar,
2781                          MSG_IS_SPAM(msgview->msginfo->flags)?LEARN_HAM:LEARN_SPAM);
2782 }
2783
2784 void messageview_list_urls (MessageView *msgview)
2785 {
2786         GSList *cur = msgview->mimeview->textview->uri_list;
2787         GSList *newlist = NULL;
2788         GHashTable *uri_hashtable;
2789         gchar *tmp;
2790         
2791         uri_hashtable = g_hash_table_new_full(g_str_hash, g_str_equal,
2792                                          (GDestroyNotify) g_free, NULL);
2793         
2794         for (; cur; cur = cur->next) {
2795                 ClickableText *uri = (ClickableText *)cur->data;
2796                 if (uri->uri &&
2797                     (!g_ascii_strncasecmp(uri->uri, "ftp.", 4) ||
2798                      !g_ascii_strncasecmp(uri->uri, "ftp:", 4) ||
2799                      !g_ascii_strncasecmp(uri->uri, "www.", 4) ||
2800                      !g_ascii_strncasecmp(uri->uri, "http:", 5) ||
2801                      !g_ascii_strncasecmp(uri->uri, "https:", 6)))
2802                 {
2803                         tmp = g_utf8_strdown(uri->uri, -1);
2804                         
2805                         if (g_hash_table_lookup(uri_hashtable, tmp)) {
2806                                 g_free(tmp);
2807                                 continue;
2808                         }
2809                         
2810                         newlist = g_slist_prepend(newlist, uri);
2811                         g_hash_table_insert(uri_hashtable, tmp,
2812                                             GUINT_TO_POINTER(g_str_hash(tmp)));
2813                 }
2814         }
2815         newlist = g_slist_reverse(newlist);
2816         uri_opener_open(msgview, newlist);
2817         g_slist_free(newlist);
2818         g_hash_table_destroy(uri_hashtable);
2819 }