edc5d3d7443b74791c6a157b1b186278dc9b66a2
[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         cm_toggle_menu_set_active_full(msgview->ui_manager, "Menu/View/AllHeaders",
635                                         prefs_common.show_all_headers);
636
637         if (prefs_common.toolbar_detachable) {
638                 handlebox = gtk_handle_box_new();
639         } else {
640                 handlebox = gtk_hbox_new(FALSE, 0);
641         }
642         gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
643         gtk_widget_realize(handlebox);
644 #ifdef MAEMO
645         msgview->toolbar = toolbar_create(TOOLBAR_MSGVIEW, window,
646                                           (gpointer)msgview);
647         msgview->statusbar = NULL;
648         msgview->statusbar_cid = 0;
649 #else
650         msgview->toolbar = toolbar_create(TOOLBAR_MSGVIEW, handlebox,
651                                           (gpointer)msgview);
652 #ifndef GENERIC_UMPC
653         statusbar = gtk_statusbar_new();
654         gtk_widget_show(statusbar);
655         gtk_box_pack_end(GTK_BOX(vbox), statusbar, FALSE, FALSE, 0);
656         msgview->statusbar = statusbar;
657         msgview->statusbar_cid = gtk_statusbar_get_context_id
658                 (GTK_STATUSBAR(statusbar), "Message View");
659 #else
660         msgview->statusbar = NULL;
661         msgview->statusbar_cid = 0;
662 #endif
663 #endif
664
665
666         msgview->handlebox = handlebox;
667         msgview->menubar   = menubar;
668
669         gtk_container_add(GTK_CONTAINER(vbox),
670                           GTK_WIDGET_PTR(msgview));
671
672         messageview_update_actions_menu(msgview);
673
674         msgview_list = g_list_append(msgview_list, msgview);
675 }
676
677 static MessageView *messageview_create_with_new_window_visible(MainWindow *mainwin, gboolean show)
678 {
679         MessageView *msgview;
680         GtkWidget *window;
681         static GdkGeometry geometry;
682
683         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "messageview");
684         gtk_window_set_title(GTK_WINDOW(window), _("Claws Mail - Message View"));
685         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
686
687         if (!geometry.min_height) {
688                 geometry.min_width = 320;
689                 geometry.min_height = 200;
690         }
691         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
692                                       GDK_HINT_MIN_SIZE);
693
694         gtk_widget_set_size_request(window, prefs_common.msgwin_width,
695                                     prefs_common.msgwin_height);
696 #ifdef G_OS_WIN32
697         gtk_window_move(GTK_WINDOW(window), 48, 48);
698 #endif
699
700         msgview = messageview_create(mainwin);
701
702         g_signal_connect(G_OBJECT(window), "size_allocate",
703                          G_CALLBACK(messageview_size_allocate_cb),
704                          msgview);
705         g_signal_connect(G_OBJECT(window), "delete_event",
706                          G_CALLBACK(messageview_delete_cb), msgview);
707 #ifdef MAEMO
708         maemo_connect_key_press_to_mainwindow(GTK_WINDOW(window));
709 #else
710         g_signal_connect(G_OBJECT(window), "key_press_event",
711                          G_CALLBACK(key_pressed), msgview);
712 #endif
713         messageview_add_toolbar(msgview, window);
714
715         if (show) {
716                 gtk_widget_grab_focus(msgview->mimeview->textview->text);
717                 gtk_widget_show(window);
718         } else {
719                 gtk_widget_realize(window);
720         }
721
722         msgview->new_window = TRUE;
723         msgview->window = window;
724         msgview->visible = TRUE;
725
726         toolbar_set_style(msgview->toolbar->toolbar, msgview->handlebox, 
727                           prefs_common.toolbar_style);
728         messageview_init(msgview);
729
730         return msgview;
731 }
732
733 MessageView *messageview_create_with_new_window(MainWindow *mainwin)
734 {
735         return messageview_create_with_new_window_visible(mainwin, TRUE);
736 }
737 void messageview_init(MessageView *messageview)
738 {
739         headerview_init(messageview->headerview);
740         mimeview_init(messageview->mimeview);
741         /*messageview_set_font(messageview);*/
742
743         noticeview_hide(messageview->noticeview);
744 }
745
746 static void notification_convert_header(gchar *dest, gint len, 
747                                         const gchar *src_,
748                                         gint header_len)
749 {
750         char *src;
751
752         cm_return_if_fail(src_ != NULL);
753         cm_return_if_fail(dest != NULL);
754
755         if (len < 1) return;
756
757         Xstrndup_a(src, src_, len, return);
758
759         remove_return(src);
760
761         if (is_ascii_str(src)) {
762                 strncpy2(dest, src, len);
763                 dest[len - 1] = '\0';
764                 return;
765         } else
766                 conv_encode_header(dest, len, src, header_len, FALSE);
767 }
768
769 static gint disposition_notification_send(MsgInfo *msginfo)
770 {
771         gchar buf[BUFFSIZE];
772         gchar tmp[MAXPATHLEN + 1];
773         FILE *fp;
774         GList *ac_list;
775         PrefsAccount *account = NULL;
776         gint ok;
777         gchar *to;
778         FolderItem *queue, *outbox;
779         gint num;
780         gchar *path;
781         gchar *addr;
782         gchar *addrp;
783         gchar *foo = NULL;
784         gboolean queued_removed = FALSE;
785         gchar *boundary = NULL;
786         gchar *date = NULL;
787         gchar *orig_to = NULL;
788         gchar *enc_sub = NULL;
789
790         if (!msginfo->extradata)
791                 return -1;
792         if (!msginfo->extradata->returnreceiptto && 
793             !msginfo->extradata->dispositionnotificationto) 
794                 return -1;
795
796         /* RFC2298: Test for Return-Path */
797         if (msginfo->extradata->dispositionnotificationto)
798                 to = msginfo->extradata->dispositionnotificationto;
799         else
800                 to = msginfo->extradata->returnreceiptto;
801
802         ok = procheader_get_header_from_msginfo(msginfo, buf, sizeof(buf),
803                                 "Return-Path:");
804         if (ok == 0) {
805                 gchar *to_addr = g_strdup(to);
806                 extract_address(to_addr);
807                 extract_address(buf);
808                 ok = strcasecmp(to_addr, buf);
809                 g_free(to_addr);
810         } else {
811                 strncpy(buf, _("<No Return-Path found>"), 
812                                 sizeof(buf));
813         }
814         
815         if (ok != 0) {
816                 AlertValue val;
817                 gchar *message;
818                 message = g_markup_printf_escaped(
819                   _("The notification address to which the return receipt is\n"
820                     "to be sent does not correspond to the return path:\n"
821                     "Notification address: %s\n"
822                     "Return path: %s\n"
823                     "It is advised to not to send the return receipt."),
824                   to, buf);
825                 val = alertpanel_full(_("Warning"), message,
826                                 _("_Don't Send"), _("_Send"), NULL, FALSE,
827                                 NULL, ALERT_WARNING, G_ALERTDEFAULT);
828                 g_free(message);                                
829                 if (val != G_ALERTALTERNATE)
830                         return -1;
831         }
832
833         ac_list = account_find_all_from_address(NULL, msginfo->to);
834         ac_list = account_find_all_from_address(ac_list, msginfo->cc);
835
836         if (ac_list == NULL) {
837                 AlertValue val = 
838                 alertpanel_full(_("Warning"),
839                   _("This message is asking for a return receipt notification\n"
840                     "but according to its 'To:' and 'CC:' headers it was not\n"
841                     "officially addressed to you.\n"
842                     "It is advised to not to send the return receipt."),
843                   _("_Don't Send"), _("_Send"), NULL, FALSE,
844                   NULL, ALERT_WARNING, G_ALERTDEFAULT);
845                 if (val != G_ALERTALTERNATE)
846                         return -1;
847         }
848
849         if (g_list_length(ac_list) > 1) {
850                 if ((account = select_account_from_list(ac_list)) == NULL)
851                         return -1;
852         }
853         else if (ac_list != NULL)
854                 account = (PrefsAccount *) ac_list->data;
855         g_list_free(ac_list);
856
857         if (account == NULL)
858                 account = account_get_default();
859         if (!account || account->protocol == A_NNTP) {
860                 alertpanel_error(_("Account for sending mail is not specified.\n"
861                                    "Please select a mail account before sending."));
862                 return -1;
863         }
864
865         /* write to temporary file */
866         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%p",
867                    get_rc_dir(), G_DIR_SEPARATOR, msginfo);
868
869         if ((fp = g_fopen(tmp, "wb")) == NULL) {
870                 FILE_OP_ERROR(tmp, "fopen");
871                 return -1;
872         }
873
874         /* chmod for security */
875         if (change_file_mode_rw(fp, tmp) < 0) {
876                 FILE_OP_ERROR(tmp, "chmod");
877                 g_warning("can't change file mode\n");
878         }
879         
880         addr = g_strdup(to);
881         
882         extract_address(addr);
883         addrp = addr;
884         
885         /* write queue headers */
886         ok = fprintf(fp, "AF:\n"
887                     "NF:0\n"
888                     "PS:10\n"
889                     "SRH:1\n"
890                     "SFN:\n"
891                     "DSR:\n"
892                     "MID:\n"
893                     "CFG:\n"
894                     "PT:0\n"
895                     "S:%s\n"
896                     "RQ:\n"
897                     "SSV:%s\n"
898                     "SSH:\n"
899                     "R:<%s>\n", 
900                     account->address,
901                     account->smtp_server?account->smtp_server:"",
902                     addrp);
903
904         g_free(addrp);
905         if (ok < 0)
906                 goto FILE_ERROR;
907         
908         /* check whether we need to save the message */
909         outbox = account_get_special_folder(account, F_OUTBOX); 
910         if (folder_get_default_outbox() == outbox && !prefs_common.savemsg)
911                 outbox = NULL;
912         if (outbox) {
913                 path = folder_item_get_identifier(outbox);
914                 ok = fprintf(fp, "SCF:%s\n", path);
915                 g_free(path);
916                 
917                 if (ok < 0)
918                         goto FILE_ERROR;
919         }               
920
921         if (fprintf(fp, "X-Claws-End-Special-Headers: 1\n") < 0)
922                 goto FILE_ERROR;
923
924         /* Date */
925         get_rfc822_date(buf, sizeof(buf));
926         if (fprintf(fp, "Date: %s\n", buf) < 0)
927                 goto FILE_ERROR;
928
929         /* From */
930         if (account->name && *account->name) {
931                 notification_convert_header
932                         (buf, sizeof(buf), account->name,
933                          strlen("From: "));
934                 if (fprintf(fp, "From: %s <%s>\n", buf, account->address) < 0)
935                         goto FILE_ERROR;
936         } else
937                 if (fprintf(fp, "From: %s\n", account->address) < 0)
938                         goto FILE_ERROR;
939
940         if (fprintf(fp, "To: %s\n", to) < 0)
941                 goto FILE_ERROR;
942
943         /* Subject */
944         notification_convert_header(buf, sizeof(buf), msginfo->subject,
945                                     strlen("Subject: "));
946         if (fprintf(fp, "Subject: Disposition notification: %s\n", buf) < 0)
947                 goto FILE_ERROR;
948
949         /* Message ID */
950         if (account->set_domain && account->domain) {
951                 g_snprintf(buf, sizeof(buf), "%s", account->domain); 
952         } else if (!strncmp(get_domain_name(), "localhost", strlen("localhost"))) {
953                 g_snprintf(buf, sizeof(buf), "%s", 
954                         strchr(account->address, '@') ?
955                                 strchr(account->address, '@')+1 :
956                                 account->address);
957         } else {
958                 g_snprintf(buf, sizeof(buf), "%s", "");
959         }
960         
961         if (account->gen_msgid) {
962                 gchar *addr = NULL;
963                 if (account->msgid_with_addr) {
964                         addr = account->address;
965                 }
966                 generate_msgid(buf, sizeof(buf), addr);
967
968                 if (fprintf(fp, "Message-ID: <%s>\n", buf) < 0)
969                         goto FILE_ERROR;
970         }
971
972         boundary = generate_mime_boundary("DN");
973         get_rfc822_date(buf, sizeof(buf));
974         date = g_strdup(buf);
975         if (msginfo->to) {
976                 orig_to = g_strdup(msginfo->to);
977                 extract_address(orig_to);
978         }
979         if (msginfo->subject && *(msginfo->subject)) {
980                 enc_sub = g_malloc0(strlen(msginfo->subject)*8);
981                 qp_encode_line(enc_sub, (const guchar *)msginfo->subject);
982                 g_strstrip(enc_sub);
983         }
984         ok = fprintf(fp,"MIME-Version: 1.0\n"
985                         "Content-Type: multipart/report; report-type=disposition-notification;\n"
986                         "  boundary=\"%s\"\n"
987                         "\n"
988                         "--%s\n"
989                         "Content-Type: text/plain; charset=UTF-8\n"
990                         "Content-Transfer-Encoding: quoted-printable\n"
991                         "\n"
992                         "The message sent on: %s\n"
993                         "To: %s\n"
994                         "With subject: \"%s\"\n"
995                         "has been displayed at %s.\n"
996                         "\n"
997                         "There is no guarantee that the message has been read or understood.\n"
998                         "\n"
999                         "--%s\n"
1000                         "Content-Type: message/disposition-notification\n"
1001                         "\n"
1002                         "Reporting-UA: %s\n"
1003                         "Original-Recipient: rfc822;%s\n"
1004                         "Final-Recipient: rfc822;%s\n"
1005                         "Original-Message-ID: <%s>\n"
1006                         "Disposition: manual-action/MDN-sent-manually; displayed\n"
1007                         "\n"
1008                         "--%s\n"
1009                         "Content-Type: application/octet-stream\n"
1010                         "Reporting-UA: %s\n"
1011                         "Original-Recipient: rfc822;%s\n"
1012                         "Final-Recipient: rfc822;%s\n"
1013                         "Original-Message-ID: <%s>\n"
1014                         "Disposition: manual-action/MDN-sent-manually; displayed\n"
1015                         "\n"
1016                         "--%s--\n", 
1017                         boundary, 
1018                         boundary,
1019                         msginfo->date, 
1020                         orig_to?orig_to:"No To:",
1021                         enc_sub?enc_sub:"No subject",
1022                         date,
1023                         boundary,
1024                         PROG_VERSION,
1025                         orig_to?orig_to:"No To:",
1026                         account->address,
1027                         msginfo->msgid?msginfo->msgid:"NO MESSAGE ID",
1028                         boundary,
1029                         PROG_VERSION,
1030                         orig_to?orig_to:"No To:",
1031                         account->address,
1032                         msginfo->msgid?msginfo->msgid:"NO MESSAGE ID",
1033                         boundary);
1034
1035         g_free(enc_sub);
1036         g_free(orig_to);
1037         g_free(date);
1038         g_free(boundary);
1039
1040         if (ok < 0)
1041                 goto FILE_ERROR;        
1042
1043         if (fclose(fp) == EOF) {
1044                 FILE_OP_ERROR(tmp, "fclose");
1045                 claws_unlink(tmp);
1046                 return -1;
1047         }
1048
1049         /* put it in queue */
1050         queue = account_get_special_folder(account, F_QUEUE);
1051         if (!queue) queue = folder_get_default_queue();
1052         if (!queue) {
1053                 g_warning("can't find queue folder\n");
1054                 claws_unlink(tmp);
1055                 return -1;
1056         }
1057         folder_item_scan(queue);
1058         if ((num = folder_item_add_msg(queue, tmp, NULL, TRUE)) < 0) {
1059                 g_warning("can't queue the message\n");
1060                 claws_unlink(tmp);
1061                 return -1;
1062         }
1063                 
1064         if (prefs_common.work_offline && 
1065             !inc_offline_should_override(TRUE,
1066                 _("Claws Mail needs network access in order "
1067                   "to send this email.")))
1068                 return 0;
1069
1070         /* send it */
1071         path = folder_item_fetch_msg(queue, num);
1072         ok = procmsg_send_message_queue_with_lock(path, &foo, queue, num, &queued_removed);
1073         g_free(path);
1074         g_free(foo);
1075         if (ok == 0 && !queued_removed)
1076                 folder_item_remove_msg(queue, num);
1077
1078         return ok;
1079
1080 FILE_ERROR:
1081         fclose(fp);
1082         claws_unlink(tmp);
1083         return -1;
1084 }
1085
1086 static gboolean find_encrypted_func(GNode *node, gpointer data)
1087 {
1088         MimeInfo *mimeinfo = (MimeInfo *) node->data;
1089         MimeInfo **encinfo = (MimeInfo **) data;
1090         
1091         if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
1092                 *encinfo = mimeinfo;
1093                 return TRUE;
1094         }
1095         
1096         return FALSE;
1097 }
1098
1099 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
1100 {
1101         MimeInfo *encinfo = NULL;
1102
1103         g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
1104                 find_encrypted_func, &encinfo);
1105         
1106         return encinfo;
1107 }
1108
1109 static gboolean find_broken_func(GNode *node, gpointer data)
1110 {
1111         MimeInfo *mimeinfo = (MimeInfo *) node->data;
1112         MimeInfo **brokeninfo = (MimeInfo **) data;
1113         
1114         if (mimeinfo->broken) {
1115                 *brokeninfo = mimeinfo;
1116                 return TRUE;
1117         }
1118         
1119         return FALSE;
1120 }
1121
1122 static MimeInfo *find_broken_part(MimeInfo *rootinfo)
1123 {
1124         MimeInfo *brokeninfo = NULL;
1125
1126         g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
1127                 find_broken_func, &brokeninfo);
1128         
1129         return brokeninfo;
1130 }
1131
1132 gint messageview_show(MessageView *messageview, MsgInfo *msginfo,
1133                       gboolean all_headers)
1134 {
1135         gchar *text = NULL;
1136         gchar *file;
1137         MimeInfo *mimeinfo, *encinfo, *brokeninfo, *root;
1138         gchar *subject = NULL;
1139         cm_return_val_if_fail(msginfo != NULL, -1);
1140
1141         if (msginfo != messageview->msginfo)
1142                 messageview->show_full_text = FALSE;
1143
1144         if (messageview->mimeview->textview &&
1145             messageview->mimeview->textview->loading) {
1146                 messageview->mimeview->textview->stop_loading = TRUE;
1147                 return 0;
1148         }
1149
1150         if (messageview->toolbar)
1151                 toolbar_set_learn_button
1152                         (messageview->toolbar,
1153                          MSG_IS_SPAM(msginfo->flags)?LEARN_HAM:LEARN_SPAM);
1154         else
1155                 toolbar_set_learn_button
1156                         (messageview->mainwin->toolbar,
1157                          MSG_IS_SPAM(msginfo->flags)?LEARN_HAM:LEARN_SPAM);
1158
1159         if (messageview->toolbar) {
1160                 if (messageview->toolbar->learn_spam_btn) {
1161                         gboolean can_learn = FALSE;
1162                         if (procmsg_spam_can_learn() &&
1163                             (msginfo->folder &&
1164                              msginfo->folder->folder->klass->type != F_UNKNOWN &&
1165                              msginfo->folder->folder->klass->type != F_NEWS))
1166                                 can_learn = TRUE;
1167
1168                         gtk_widget_set_sensitive(
1169                                 messageview->toolbar->learn_spam_btn, 
1170                                 can_learn);
1171                 }
1172         }
1173         
1174         noticeview_hide(messageview->noticeview);
1175         mimeview_clear(messageview->mimeview);
1176         messageview->updating = TRUE;
1177
1178         if (msginfo->size > 1024*1024)
1179                 statuswindow_print_all(_("Fetching message (%s)..."),
1180                         to_human_readable(msginfo->size));
1181         
1182         file = procmsg_get_message_file_path(msginfo);
1183
1184         if (msginfo->size > 1024*1024)
1185                 statuswindow_pop_all();
1186
1187         if (!file) {
1188                 g_warning("can't get message file path.\n");
1189                 textview_show_error(messageview->mimeview->textview);
1190                 return -1;
1191         }
1192         
1193         if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
1194             !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
1195                 mimeinfo = procmime_scan_file(file);
1196         else
1197                 mimeinfo = procmime_scan_queue_file(file);
1198
1199         messageview->updating = FALSE;
1200         
1201         if (messageview->deferred_destroy) {
1202                 g_free(file);
1203                 messageview_destroy(messageview);
1204                 return 0;
1205         }
1206
1207         if (!mimeinfo) {
1208                 textview_show_error(messageview->mimeview->textview);
1209                 return -1;
1210         }
1211
1212         while ((encinfo = find_encrypted_part(mimeinfo)) != NULL) {
1213                 debug_print("decrypting message part\n");
1214                 if (privacy_mimeinfo_decrypt(encinfo) < 0) {
1215                         text = g_strdup_printf(_("Couldn't decrypt: %s"),
1216                                                privacy_get_error());
1217                         noticeview_show(messageview->noticeview);
1218                         noticeview_set_icon(messageview->noticeview,
1219                                             STOCK_PIXMAP_NOTICE_WARN);
1220                         noticeview_set_text(messageview->noticeview, text);
1221                         gtk_widget_hide(messageview->noticeview->button);
1222                         gtk_widget_hide(messageview->noticeview->button2);
1223                         g_free(text);
1224                         break;
1225                 }
1226         }
1227                         
1228         if (messageview->msginfo != msginfo) {
1229                 procmsg_msginfo_free(messageview->msginfo);
1230                 messageview->msginfo = NULL;
1231                 messageview_set_menu_sensitive(messageview);
1232                 messageview->msginfo = 
1233                         procmsg_msginfo_get_full_info_from_file(msginfo, file);
1234                 if (!messageview->msginfo)
1235                         messageview->msginfo = procmsg_msginfo_copy(msginfo);
1236         } else {
1237                 messageview->msginfo = NULL;
1238                 messageview_set_menu_sensitive(messageview);
1239                 messageview->msginfo = msginfo;
1240         }
1241         headerview_show(messageview->headerview, messageview->msginfo);
1242
1243         messageview_set_position(messageview, 0);
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         root = mimeinfo;
1314         mimeinfo = procmime_mimeinfo_next(mimeinfo);
1315         if (!all_headers && mimeinfo 
1316                         && (mimeinfo->type != MIMETYPE_TEXT || 
1317             strcasecmp(mimeinfo->subtype, "plain")) 
1318                         && (mimeinfo->type != MIMETYPE_MULTIPART || 
1319             strcasecmp(mimeinfo->subtype, "signed"))) {
1320                 if (strcasecmp(mimeinfo->subtype, "html")) {
1321                         MimeInfo *saved_mimeinfo = mimeinfo;
1322                         MimeInfo *alt_parent = mimeinfo;
1323
1324                         /* if multipart/{related,mixed} part, look inside for a multipart/alternative child */
1325                         if (mimeinfo->type == MIMETYPE_MULTIPART &&
1326                             (!strcasecmp(mimeinfo->subtype, "related") ||
1327                              !strcasecmp(mimeinfo->subtype, "mixed"))) {
1328                                 for (; mimeinfo; mimeinfo = procmime_mimeinfo_next(mimeinfo)) {
1329                                         if (mimeinfo->node->parent != saved_mimeinfo->node) {
1330                                                 /* only consider children of the 
1331                                                  * multipart/{related,mixed} part */
1332                                                 continue;
1333                                         }
1334                                         if (mimeinfo->type == MIMETYPE_MULTIPART && 
1335                                             !strcasecmp(mimeinfo->subtype, "alternative")) {
1336                                                 /* we got an alternative part */
1337                                                 alt_parent = mimeinfo;
1338                                                 break;
1339                                         }
1340                                         if (mimeinfo->type == MIMETYPE_TEXT && 
1341                                             !strcasecmp(mimeinfo->subtype, "calendar") &&
1342                                             mimeview_has_viewer_for_content_type(messageview->mimeview,
1343                                                                                  "text/calendar")) {
1344                                                 mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
1345                                                 goto done;
1346                                         } else if (mimeinfo->type == MIMETYPE_TEXT && 
1347                                             !strcasecmp(mimeinfo->subtype, "html") &&
1348                                             prefs_common.promote_html_part) {
1349                                                 mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
1350                                                 goto done;
1351                                         }
1352                                 }
1353                         }
1354
1355                         /* if we now have a multipart/alternative part (possibly inside a
1356                          * multipart/{related,mixed} part, look for an HTML part inside */
1357                         if (mimeinfo && mimeinfo->type == MIMETYPE_MULTIPART &&
1358                             !strcasecmp(mimeinfo->subtype, "alternative")) {
1359                                 for (; mimeinfo; mimeinfo = procmime_mimeinfo_next(mimeinfo)) {
1360                                         if (mimeinfo->node->parent != alt_parent->node) {
1361                                                 /* only consider children of the 
1362                                                  * multipart/alternative part, so as
1363                                                  * not to show html attachments */
1364                                                 continue;
1365                                         }
1366                                         if (mimeinfo->type == MIMETYPE_TEXT && 
1367                                             !strcasecmp(mimeinfo->subtype, "calendar") &&
1368                                             mimeview_has_viewer_for_content_type(messageview->mimeview,
1369                                                                                  "text/calendar")) {
1370                                                 mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
1371                                                 goto done;
1372                                         } else if (mimeinfo->type == MIMETYPE_TEXT && 
1373                                             !strcasecmp(mimeinfo->subtype, "html") &&
1374                                             prefs_common.promote_html_part) {
1375                                                 mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
1376                                                 goto done;
1377                                         }
1378                                 }
1379                         }
1380                         
1381                         /* if we didn't find anything, go back to start */
1382                         if (!mimeinfo) 
1383                                 mimeinfo = saved_mimeinfo;
1384
1385                         if (!mimeview_show_part(messageview->mimeview, mimeinfo))
1386                                 mimeview_select_mimepart_icon(messageview->mimeview, root);
1387                         goto done;
1388                 } else if (prefs_common.invoke_plugin_on_html) {
1389                         mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
1390                         goto done;
1391                 }
1392         }
1393         if (!all_headers && mimeinfo &&
1394             mimeinfo->type == MIMETYPE_MULTIPART &&
1395             mimeview_has_viewer_for_content_type(messageview->mimeview, "text/calendar")) {
1396                 /* look for a calendar part or it looks really strange */
1397                 while (mimeinfo) {
1398                         if (mimeinfo->type == MIMETYPE_TEXT &&
1399                             !strcasecmp(mimeinfo->subtype, "calendar")) {
1400                                 mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
1401                                 goto done;
1402                         }
1403                         mimeinfo = procmime_mimeinfo_next(mimeinfo);
1404                 }
1405         }
1406
1407         mimeview_select_mimepart_icon(messageview->mimeview, root);
1408 done:
1409         /* plugins may hook in here to work with the message view */
1410         hooks_invoke(MESSAGE_VIEW_SHOW_DONE_HOOKLIST, messageview);
1411
1412         g_free(file);
1413
1414         return 0;
1415 }
1416
1417 void messageview_reflect_prefs_pixmap_theme(void)
1418 {
1419         GList *cur;
1420         MessageView *msgview;
1421
1422         for (cur = msgview_list; cur != NULL; cur = cur->next) {
1423                 msgview = (MessageView*)cur->data;
1424                 toolbar_update(TOOLBAR_MSGVIEW, msgview);
1425                 mimeview_update(msgview->mimeview);
1426         }
1427 }
1428
1429 void messageview_clear(MessageView *messageview)
1430 {
1431         if (!messageview)
1432                 return;
1433         procmsg_msginfo_free(messageview->msginfo);
1434         messageview->msginfo = NULL;
1435         messageview->filtered = FALSE;
1436         mimeview_clear(messageview->mimeview);
1437         headerview_clear(messageview->headerview);
1438         noticeview_hide(messageview->noticeview);
1439 }
1440
1441 void messageview_destroy(MessageView *messageview)
1442 {
1443         debug_print("destroy messageview\n");
1444         messageview_list = g_list_remove(messageview_list, messageview);
1445
1446         if (messageview->mainwin->summaryview->messageview == messageview) {
1447                 messageview->mainwin->summaryview->displayed = NULL;
1448                 messageview->mainwin->summaryview->messageview = NULL;
1449         }
1450         if (messageview->mainwin->summaryview->ext_messageview == messageview) {
1451                 messageview->mainwin->summaryview->displayed = NULL;
1452                 messageview->mainwin->summaryview->ext_messageview = NULL;
1453         }
1454         if (!messageview->deferred_destroy) {
1455                 hooks_unregister_hook(MSGINFO_UPDATE_HOOKLIST,
1456                               messageview->msginfo_update_callback_id);
1457         }
1458
1459         if (messageview->updating) {
1460                 debug_print("uh oh, better not touch that now (fetching)\n");
1461                 messageview->deferred_destroy = TRUE;
1462                 gtk_widget_hide(messageview->window);
1463                 return;
1464         }
1465         
1466         if (messageview->mimeview->textview
1467         &&  messageview->mimeview->textview->loading) {
1468                 debug_print("uh oh, better not touch that now (loading text)\n");
1469                 messageview->deferred_destroy = TRUE;
1470                 messageview->mimeview->textview->stop_loading = TRUE;
1471                 gtk_widget_hide(messageview->window);
1472                 return;
1473         }
1474
1475         headerview_destroy(messageview->headerview);
1476         mimeview_destroy(messageview->mimeview);
1477         noticeview_destroy(messageview->noticeview);
1478
1479         procmsg_msginfo_free(messageview->msginfo);
1480         toolbar_clear_list(TOOLBAR_MSGVIEW);
1481         if (messageview->toolbar) {
1482                 toolbar_destroy(messageview->toolbar);
1483                 g_free(messageview->toolbar);
1484         }
1485         
1486         msgview_list = g_list_remove(msgview_list, messageview); 
1487
1488         if (messageview->window)
1489                 gtk_widget_destroy(messageview->window);
1490         g_free(messageview);
1491 }
1492
1493 void messageview_delete(MessageView *msgview)
1494 {
1495         MsgInfo *msginfo = NULL;
1496         FolderItem *trash = NULL;
1497         PrefsAccount *ac = NULL;
1498
1499         if (msgview->msginfo && msgview->mainwin && msgview->mainwin->summaryview)
1500                 msginfo = summary_get_selected_msg(msgview->mainwin->summaryview);
1501         
1502         /* need a procmsg_msginfo_equal() */
1503         if (msginfo && msgview->msginfo && 
1504             msginfo->msgnum == msgview->msginfo->msgnum && 
1505             msginfo->folder == msgview->msginfo->folder) {
1506                 summary_delete_trash(msgview->mainwin->summaryview);
1507         } else {                
1508                 msginfo = msgview->msginfo;
1509
1510                 cm_return_if_fail(msginfo != NULL);
1511
1512                 /* to get the trash folder, we have to choose either
1513                  * the folder's or account's trash default - we prefer
1514                  * the one in the account prefs */
1515                 if (msginfo->folder) {
1516                         if (NULL != (ac = account_find_from_item(msginfo->folder)))
1517                                 trash = account_get_special_folder(ac, F_TRASH);
1518                         if (!trash && msginfo->folder->folder)  
1519                                 trash = msginfo->folder->folder->trash;
1520                         /* if still not found, use the default */
1521                         if (!trash) 
1522                                 trash = folder_get_default_trash();
1523                 }       
1524
1525                 cm_return_if_fail(trash != NULL);
1526
1527                 if (prefs_common.immediate_exec)
1528                         /* TODO: Delete from trash */
1529                         folder_item_move_msg(trash, msginfo);
1530                 else {
1531                         procmsg_msginfo_set_to_folder(msginfo, trash);
1532                         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
1533                         /* NOTE: does not update to next message in summaryview */
1534                 }
1535         }
1536 #ifdef GENERIC_UMPC
1537         if (msgview->window && !prefs_common.always_show_msg) {
1538                 messageview_destroy(msgview);
1539         }
1540 #endif
1541 }
1542
1543 /* 
1544  * \brief update messageview with currently selected message in summaryview
1545  *        leave unchanged if summaryview is empty
1546  * \param pointer to MessageView
1547  */     
1548 static void messageview_update(MessageView *msgview, MsgInfo *old_msginfo)
1549 {
1550         SummaryView *summaryview = (SummaryView*)msgview->mainwin->summaryview;
1551
1552         cm_return_if_fail(summaryview != NULL);
1553         
1554         if (summaryview->selected) {
1555                 MsgInfo *msginfo = summary_get_selected_msg(summaryview);
1556                 if (msginfo == NULL || msginfo == old_msginfo)
1557                         return;
1558
1559                 messageview_show(msgview, msginfo, 
1560                                  msgview->all_headers);
1561         } 
1562 }
1563
1564 TextView *messageview_get_current_textview(MessageView *messageview)
1565 {
1566         TextView *text = NULL;
1567
1568         text = messageview->mimeview->textview;
1569
1570         return text;
1571 }
1572
1573 MimeInfo *messageview_get_selected_mime_part(MessageView *messageview)
1574 {
1575         return mimeview_get_selected_part(messageview->mimeview);
1576 }
1577
1578 void messageview_copy_clipboard(MessageView *messageview)
1579 {
1580         gchar *text = messageview_get_selection(messageview);
1581         if (text) {
1582                 gtk_clipboard_set_text(
1583                         gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
1584                         text, -1);
1585         }
1586         g_free(text);
1587 }
1588
1589 void messageview_select_all(MessageView *messageview)
1590 {
1591         TextView *text;
1592
1593         text = messageview_get_current_textview(messageview);
1594         if (text) {
1595                 GtkTextView *textview = GTK_TEXT_VIEW(text->text);
1596                 GtkTextBuffer *buffer;
1597                 GtkTextIter start, end;
1598
1599                 buffer = gtk_text_view_get_buffer(textview);
1600                 gtk_text_buffer_get_bounds(buffer, &start, &end);
1601                 gtk_text_buffer_select_range(buffer, &start, &end);
1602         }
1603 }
1604
1605 void messageview_set_position(MessageView *messageview, gint pos)
1606 {
1607         TextView *text;
1608
1609         text = messageview_get_current_textview(messageview);
1610         if (text)
1611                 textview_set_position(text, pos);
1612 }
1613
1614 gboolean messageview_search_string(MessageView *messageview, const gchar *str,
1615                                    gboolean case_sens)
1616 {
1617         TextView *text;
1618
1619         if (messageview->mimeview->type == MIMEVIEW_VIEWER) {
1620                 MimeViewer *viewer = messageview->mimeview->mimeviewer;
1621                 if (viewer && viewer->text_search) {
1622                         return viewer->text_search(viewer, FALSE, str, case_sens);
1623                 }
1624         }
1625
1626         text = messageview_get_current_textview(messageview);
1627         if (text)
1628                 return textview_search_string(text, str, case_sens);
1629         return FALSE;
1630 }
1631
1632 gboolean messageview_search_string_backward(MessageView *messageview,
1633                                             const gchar *str,
1634                                             gboolean case_sens)
1635 {
1636         TextView *text;
1637
1638         if (messageview->mimeview->type == MIMEVIEW_VIEWER) {
1639                 MimeViewer *viewer = messageview->mimeview->mimeviewer;
1640                 if (viewer && viewer->text_search) {
1641                         return viewer->text_search(viewer, TRUE, str, case_sens);
1642                 }
1643         }
1644
1645         text = messageview_get_current_textview(messageview);
1646         if (text)       
1647                 return textview_search_string_backward(text,
1648                                                        str, case_sens);
1649         return FALSE;
1650 }
1651
1652 gboolean messageview_is_visible(MessageView *messageview)
1653 {
1654         if (messageview == NULL)
1655                 return FALSE;
1656         return messageview->visible;
1657 }
1658
1659 static void messageview_save_as(MessageView *messageview)
1660 {
1661         gchar *filename = NULL;
1662         MsgInfo *msginfo;
1663         gchar *src, *dest, *tmp;
1664
1665         if (!messageview->msginfo) return;
1666         msginfo = messageview->msginfo;
1667
1668         if (msginfo->subject) {
1669                 Xstrdup_a(filename, msginfo->subject, return);
1670                 subst_for_filename(filename);
1671         }
1672         if (filename && !g_utf8_validate(filename, -1, NULL)) {
1673                 gchar *oldstr = filename;
1674                 filename = conv_codeset_strdup(filename,
1675                                                conv_get_locale_charset_str(),
1676                                                CS_UTF_8);
1677                 if (!filename) {
1678                         g_warning("messageview_save_as(): failed to convert character set.");
1679                         filename = g_strdup(oldstr);
1680                 }
1681                 dest = filesel_select_file_save(_("Save as"), filename);
1682                 g_free(filename);
1683         } else
1684                 dest = filesel_select_file_save(_("Save as"), filename);
1685         if (!dest) return;
1686         if (is_file_exist(dest)) {
1687                 AlertValue aval;
1688
1689                 aval = alertpanel(_("Overwrite"),
1690                                   _("Overwrite existing file?"),
1691                                   GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL);
1692                 if (G_ALERTALTERNATE != aval) return;
1693         }
1694
1695         src = procmsg_get_message_file(msginfo);
1696         if (copy_file(src, dest, TRUE) < 0) {
1697                 tmp =  g_path_get_basename(dest);
1698                 alertpanel_error(_("Couldn't save the file '%s'."), tmp);
1699                 g_free(tmp);
1700         }
1701         g_free(dest);
1702         g_free(src);
1703 }
1704
1705 static gint messageview_delete_cb(GtkWidget *widget, GdkEventAny *event,
1706                                   MessageView *messageview)
1707 {
1708         messageview_destroy(messageview);
1709         return TRUE;
1710 }
1711
1712 static void messageview_size_allocate_cb(GtkWidget *widget,
1713                                          GtkAllocation *allocation)
1714 {
1715         cm_return_if_fail(allocation != NULL);
1716
1717         prefs_common.msgwin_width  = allocation->width;
1718         prefs_common.msgwin_height = allocation->height;
1719 }
1720 #ifndef MAEMO
1721 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
1722                         MessageView *messageview)
1723 {
1724         if (event && event->keyval == GDK_KEY_Escape && messageview->window) {
1725                 messageview_destroy(messageview);
1726                 return TRUE;
1727         }
1728
1729         if (event && (event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0)
1730                 return FALSE;
1731         if (event && (event->state & GDK_SHIFT_MASK) && event->keyval != GDK_KEY_space) 
1732                 return FALSE;
1733
1734         return mimeview_pass_key_press_event(messageview->mimeview, event);
1735 }
1736 #endif
1737
1738 static void messageview_show_partial_display_cb(NoticeView *noticeview, MessageView *messageview)
1739 {
1740         messageview->show_full_text = TRUE;
1741         main_window_cursor_wait(mainwindow_get_mainwindow());
1742         noticeview_hide(messageview->noticeview);
1743         messageview->partial_display_shown = FALSE;
1744         GTK_EVENTS_FLUSH();
1745         mimeview_handle_cmd(messageview->mimeview, "sc://display_as_text", NULL, NULL);
1746         main_window_cursor_normal(mainwindow_get_mainwindow());
1747 }
1748
1749 void messageview_show_partial_display(MessageView *messageview, MsgInfo *msginfo,
1750                                              size_t length)
1751 {
1752         gchar *msg = g_strdup_printf(_("Show all %s."), to_human_readable((goffset)length));
1753         noticeview_set_icon(messageview->noticeview, STOCK_PIXMAP_NOTICE_WARN);
1754         noticeview_set_text(messageview->noticeview, _("Only the first megabyte of text is shown."));
1755         noticeview_set_button_text(messageview->noticeview, msg);
1756         g_free(msg);
1757         noticeview_set_button_press_callback(messageview->noticeview,
1758                                              G_CALLBACK(messageview_show_partial_display_cb),
1759                                              (gpointer) messageview);
1760         noticeview_show(messageview->noticeview);
1761         messageview->partial_display_shown = TRUE;
1762 }
1763
1764 static void return_receipt_show(NoticeView *noticeview, MsgInfo *msginfo)
1765 {
1766         gchar *addr = NULL;
1767         gboolean from_me = FALSE;
1768         if (msginfo->folder 
1769                 && (folder_has_parent_of_type(msginfo->folder, F_QUEUE)
1770                  || folder_has_parent_of_type(msginfo->folder, F_DRAFT)))
1771                 return;
1772
1773         addr = g_strdup(msginfo->from);
1774         if (addr) {
1775                 extract_address(addr);
1776                 if (account_find_from_address(addr, FALSE)) {
1777                         from_me = TRUE;
1778                 }
1779                 g_free(addr);
1780         }
1781
1782         if (from_me) {
1783                 noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
1784                 if (MSG_IS_RETRCPT_GOT(msginfo->flags)) {
1785                         noticeview_set_text(noticeview, _("You got a return receipt for this message : "
1786                                                           "it has been displayed by the recipient."));
1787                 } else {
1788                         noticeview_set_text(noticeview, _("You asked for a return receipt in this message."));
1789                 }
1790                 noticeview_set_button_text(noticeview, NULL);
1791                 noticeview_set_button_press_callback(noticeview, NULL, NULL);
1792         } else {
1793                 noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
1794                 noticeview_set_text(noticeview, _("This message asks for a return receipt."));
1795                 noticeview_set_button_text(noticeview, _("Send receipt"));
1796                 noticeview_set_button_press_callback(noticeview,
1797                                                      G_CALLBACK(return_receipt_send_clicked),
1798                                                      (gpointer) msginfo);
1799         }
1800         noticeview_show(noticeview);
1801 }
1802
1803 static void return_receipt_send_clicked(NoticeView *noticeview, MsgInfo *msginfo)
1804 {
1805         MsgInfo *tmpmsginfo;
1806         gchar *file;
1807
1808         file = procmsg_get_message_file_path(msginfo);
1809         if (!file) {
1810                 g_warning("can't get message file path.\n");
1811                 return;
1812         }
1813
1814         tmpmsginfo = procheader_parse_file(file, msginfo->flags, TRUE, TRUE);
1815         tmpmsginfo->folder = msginfo->folder;
1816         tmpmsginfo->msgnum = msginfo->msgnum;
1817
1818         if (disposition_notification_send(tmpmsginfo) >= 0) {
1819                 procmsg_msginfo_set_flags(msginfo, MSG_RETRCPT_SENT, 0);
1820                 noticeview_hide(noticeview);
1821         }               
1822
1823         procmsg_msginfo_free(tmpmsginfo);
1824         g_free(file);
1825 }
1826
1827 static void partial_recv_show(NoticeView *noticeview, MsgInfo *msginfo)
1828 {
1829         gchar *text = NULL;
1830         gchar *button1 = NULL;
1831         gchar *button2 = NULL;
1832         void  *button1_cb = NULL;
1833         void  *button2_cb = NULL;
1834
1835         if (!msginfo->extradata)
1836                 return;
1837         if (!partial_msg_in_uidl_list(msginfo)) {
1838                 text = g_strdup_printf(_("This message has been partially "
1839                                 "retrieved,\nand has been deleted from the "
1840                                 "server."));
1841         } else {
1842                 switch (msginfo->planned_download) {
1843                 case POP3_PARTIAL_DLOAD_UNKN:
1844                         text = g_strdup_printf(_("This message has been "
1845                                         "partially retrieved;\nit is %s."),
1846                                         to_human_readable(
1847                                                 (goffset)(msginfo->total_size)));
1848                         button1 = _("Mark for download");
1849                         button2 = _("Mark for deletion");
1850                         button1_cb = partial_recv_dload_clicked;
1851                         button2_cb = partial_recv_del_clicked;
1852                         break;
1853                 case POP3_PARTIAL_DLOAD_DLOAD:
1854                         text = g_strdup_printf(_("This message has been "
1855                                         "partially retrieved;\nit is %s and "
1856                                         "will be downloaded."),
1857                                         to_human_readable(
1858                                                 (goffset)(msginfo->total_size)));
1859                         button1 = _("Unmark");
1860                         button1_cb = partial_recv_unmark_clicked;
1861                         button2 = _("Mark for deletion");
1862                         button2_cb = partial_recv_del_clicked;
1863                         break;
1864                 case POP3_PARTIAL_DLOAD_DELE:
1865                         text = g_strdup_printf(_("This message has been "
1866                                         "partially retrieved;\nit is %s and "
1867                                         "will be deleted."),
1868                                         to_human_readable(
1869                                                 (goffset)(msginfo->total_size)));
1870                         button1 = _("Mark for download");
1871                         button1_cb = partial_recv_dload_clicked;
1872                         button2 = _("Unmark");
1873                         button2_cb = partial_recv_unmark_clicked;
1874                         break;
1875                 default:
1876                         return;
1877                 }
1878         }
1879         
1880         noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
1881         noticeview_set_text(noticeview, text);
1882         g_free(text);
1883         noticeview_set_button_text(noticeview, button1);
1884         noticeview_set_button_press_callback(noticeview,
1885                      G_CALLBACK(button1_cb), (gpointer) msginfo);
1886
1887         noticeview_set_2ndbutton_text(noticeview, button2);
1888         noticeview_set_2ndbutton_press_callback(noticeview,
1889                      G_CALLBACK(button2_cb), (gpointer) msginfo);
1890
1891         noticeview_show(noticeview);
1892 }
1893
1894 static void partial_recv_dload_clicked(NoticeView *noticeview, 
1895                                        MsgInfo *msginfo)
1896 {
1897         if (partial_mark_for_download(msginfo) == 0) {
1898                 partial_recv_show(noticeview, msginfo);
1899         }
1900 }
1901
1902 static void partial_recv_del_clicked(NoticeView *noticeview, 
1903                                        MsgInfo *msginfo)
1904 {
1905         if (partial_mark_for_delete(msginfo) == 0) {
1906                 partial_recv_show(noticeview, msginfo);
1907         }
1908 }
1909
1910 static void partial_recv_unmark_clicked(NoticeView *noticeview, 
1911                                        MsgInfo *msginfo)
1912 {
1913         if (partial_unmark(msginfo) == 0) {
1914                 partial_recv_show(noticeview, msginfo);
1915         }
1916 }
1917
1918 static void select_account_cb(GtkWidget *w, gpointer data)
1919 {
1920         *(gint*)data = combobox_get_active_data(GTK_COMBO_BOX(w));
1921 }
1922
1923 static PrefsAccount *select_account_from_list(GList *ac_list)
1924 {
1925         GtkWidget *optmenu;
1926         gint account_id;
1927
1928         cm_return_val_if_fail(ac_list != NULL, NULL);
1929         cm_return_val_if_fail(ac_list->data != NULL, NULL);
1930         
1931         optmenu = gtkut_account_menu_new(ac_list,
1932                         G_CALLBACK(select_account_cb),
1933                         &account_id);
1934         if (!optmenu)
1935                 return NULL;
1936         account_id = ((PrefsAccount *) ac_list->data)->account_id;
1937         if (alertpanel_with_widget(
1938                                 _("Return Receipt Notification"),
1939                                 _("The message was sent to several of your "
1940                                   "accounts.\n"
1941                                   "Please choose which account do you want to "
1942                                   "use for sending the receipt notification:"),
1943                                 _("_Cancel"), _("_Send Notification"), NULL,
1944                                 FALSE, G_ALERTDEFAULT, optmenu) != G_ALERTALTERNATE)
1945                 return NULL;
1946         return account_find_from_id(account_id);
1947 }
1948
1949 /* 
1950  * \brief return selected messageview text, when nothing is 
1951  *        selected and message was filtered, return complete text
1952  *
1953  * \param  pointer to Messageview 
1954  *
1955  * \return pointer to text (needs to be free'd by calling func)
1956  */
1957 gchar *messageview_get_selection(MessageView *msgview)
1958 {
1959         TextView *textview;
1960         gchar *text = NULL;
1961         GtkTextView *edit = NULL;
1962         GtkTextBuffer *textbuf;
1963         gint body_pos = 0;
1964         
1965         cm_return_val_if_fail(msgview != NULL, NULL);
1966
1967         if (msgview->mimeview->type == MIMEVIEW_VIEWER) {
1968                 MimeViewer *viewer = msgview->mimeview->mimeviewer;
1969                 if (viewer && viewer->get_selection) {
1970                         text = viewer->get_selection(viewer);
1971                         if (text)
1972                                 return text;
1973                 }
1974         }
1975
1976         textview = messageview_get_current_textview(msgview);
1977         cm_return_val_if_fail(textview != NULL, NULL);
1978
1979         edit = GTK_TEXT_VIEW(textview->text);
1980         cm_return_val_if_fail(edit != NULL, NULL);
1981         body_pos = textview->body_pos;
1982
1983         textbuf = gtk_text_view_get_buffer(edit);
1984
1985         if (gtk_text_buffer_get_selection_bounds(textbuf, NULL, NULL))
1986                 return gtkut_text_view_get_selection(edit);
1987         else if (msgview->filtered) {
1988                 GtkTextIter start_iter, end_iter;
1989                 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter, body_pos);
1990                 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
1991                 gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter, FALSE);
1992         } else
1993                 text = NULL;
1994
1995         return text;
1996 }
1997
1998 static void save_as_cb(GtkAction *action, gpointer data)
1999 {
2000         MessageView *messageview = (MessageView *)data;
2001         messageview_save_as(messageview);
2002 }
2003
2004 static void print_mimeview(MimeView *mimeview, gint sel_start, gint sel_end, gint partnum) 
2005 {
2006         MainWindow *mainwin;
2007         if (!mimeview 
2008         ||  !mimeview->textview
2009         ||  !mimeview->textview->text)
2010                 alertpanel_warning(_("Cannot print: the message doesn't "
2011                                      "contain text."));
2012         else {
2013                 gtk_widget_realize(mimeview->textview->text);
2014                 if (partnum > 0) {
2015                         mimeview_select_part_num(mimeview, partnum);
2016                 }
2017                 if (mimeview->type == MIMEVIEW_VIEWER) {
2018                         MimeViewer *viewer = mimeview->mimeviewer;
2019                         if (viewer && viewer->print) {
2020                                 viewer->print(viewer);
2021                                 return;
2022                         }
2023                 }
2024                 if (sel_start != -1 && sel_end != -1) {
2025                         GtkTextIter start, end;
2026                         GtkTextView *text = GTK_TEXT_VIEW(mimeview->textview->text);
2027                         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
2028
2029                         gtk_text_buffer_get_iter_at_offset(buffer, &start, sel_start);
2030                         gtk_text_buffer_get_iter_at_offset(buffer, &end, sel_end);
2031                         gtk_text_buffer_select_range(buffer, &start, &end);
2032                 }
2033                 /* TODO: Get the real parent window, not the main window */
2034                 mainwin = mainwindow_get_mainwindow();
2035                 printing_print(GTK_TEXT_VIEW(mimeview->textview->text),
2036                                mainwin ? GTK_WINDOW(mainwin->window) : NULL,
2037                                 sel_start, sel_end);
2038         }
2039 }
2040
2041 void messageview_print(MsgInfo *msginfo, gboolean all_headers, 
2042                         gint sel_start, gint sel_end, gint partnum) 
2043 {
2044         PangoFontDescription *font_desc = NULL;
2045         MessageView *tmpview = messageview_create_with_new_window_visible(
2046                                 mainwindow_get_mainwindow(), FALSE);
2047
2048         if (prefs_common.use_different_print_font) {
2049                 font_desc = pango_font_description_from_string
2050                                                 (prefs_common.printfont);
2051         } else {
2052                 font_desc = pango_font_description_from_string
2053                                                 (prefs_common.textfont);
2054         }
2055         if (font_desc) {
2056                 gtk_widget_modify_font(tmpview->mimeview->textview->text, 
2057                         font_desc);
2058                 pango_font_description_free(font_desc);
2059         }
2060
2061         tmpview->all_headers = all_headers;
2062         if (msginfo && messageview_show(tmpview, msginfo, 
2063                 tmpview->all_headers) >= 0) {
2064                         print_mimeview(tmpview->mimeview, 
2065                                 sel_start, sel_end, partnum);
2066         }
2067         messageview_destroy(tmpview);
2068 }
2069
2070 static void page_setup_cb(GtkAction *action, gpointer data)
2071 {
2072         MessageView *messageview = (MessageView *)data;
2073         printing_page_setup(messageview ?
2074                             GTK_WINDOW(messageview->window) : NULL);
2075 }
2076
2077 static void print_cb(GtkAction *action, gpointer data)
2078 {
2079         MessageView *messageview = (MessageView *)data;
2080         gint sel_start = -1, sel_end = -1, partnum = 0;
2081
2082         if (!messageview->msginfo) return;
2083
2084         partnum = mimeview_get_selected_part_num(messageview->mimeview);
2085         textview_get_selection_offsets(messageview->mimeview->textview,
2086                 &sel_start, &sel_end);
2087         messageview_print(messageview->msginfo, messageview->all_headers, 
2088                 sel_start, sel_end, partnum);
2089 }
2090
2091 static void close_cb(GtkAction *action, gpointer data)
2092 {
2093         MessageView *messageview = (MessageView *)data;
2094         messageview_destroy(messageview);
2095 }
2096
2097 static void copy_cb(GtkAction *action, gpointer data)
2098 {
2099         MessageView *messageview = (MessageView *)data;
2100         messageview_copy_clipboard(messageview);
2101 }
2102
2103 static void allsel_cb(GtkAction *action, gpointer data)
2104 {
2105         MessageView *messageview = (MessageView *)data;
2106         messageview_select_all(messageview);
2107 }
2108
2109 static void search_cb(GtkAction *action, gpointer data)
2110 {
2111         MessageView *messageview = (MessageView *)data;
2112         message_search(messageview);
2113 }
2114
2115 static void prev_cb(GtkAction *action, gpointer data)
2116 {
2117         MessageView *messageview = (MessageView *)data;
2118         messageview->updating = TRUE;
2119         summary_step(messageview->mainwin->summaryview, GTK_SCROLL_STEP_BACKWARD);
2120         messageview->updating = FALSE;
2121
2122         if (messageview->deferred_destroy) {
2123                 debug_print("messageview got away!\n");
2124                 messageview_destroy(messageview);
2125                 return;
2126         }
2127         if (messageview->mainwin->summaryview->selected) {
2128 #ifndef GENERIC_UMPC
2129                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2130                        
2131                 if (msginfo)
2132                         messageview_show(messageview, msginfo, 
2133                                          messageview->all_headers);
2134 #endif
2135         } else {
2136                 gtk_widget_destroy(messageview->window);
2137         }
2138 }
2139
2140 static void next_cb(GtkAction *action, gpointer data)
2141 {
2142         MessageView *messageview = (MessageView *)data;
2143         messageview->updating = TRUE;
2144         summary_step(messageview->mainwin->summaryview, GTK_SCROLL_STEP_FORWARD);
2145         messageview->updating = FALSE;
2146
2147         if (messageview->deferred_destroy) {
2148                 debug_print("messageview got away!\n");
2149                 messageview_destroy(messageview);
2150                 return;
2151         }
2152         if (messageview->mainwin->summaryview->selected) {
2153 #ifndef GENERIC_UMPC
2154                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2155                        
2156                 if (msginfo)
2157                         messageview_show(messageview, msginfo, 
2158                                          messageview->all_headers);
2159 #endif
2160         } else {
2161                 gtk_widget_destroy(messageview->window);
2162         }
2163 }
2164
2165 static void prev_unread_cb(GtkAction *action, gpointer data)
2166 {
2167         MessageView *messageview = (MessageView *)data;
2168         messageview->updating = TRUE;
2169         summary_select_prev_unread(messageview->mainwin->summaryview);
2170         messageview->updating = FALSE;
2171
2172         if (messageview->deferred_destroy) {
2173                 debug_print("messageview got away!\n");
2174                 messageview_destroy(messageview);
2175                 return;
2176         }
2177         if (messageview->mainwin->summaryview->selected) {
2178 #ifndef GENERIC_UMPC
2179                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2180                        
2181                 if (msginfo)
2182                         messageview_show(messageview, msginfo, 
2183                                          messageview->all_headers);
2184 #endif
2185         } else {
2186                 gtk_widget_destroy(messageview->window);
2187         }
2188 }
2189
2190 static void next_unread_cb(GtkAction *action, gpointer data)
2191 {
2192         MessageView *messageview = (MessageView *)data;
2193         messageview->updating = TRUE;
2194         summary_select_next_unread(messageview->mainwin->summaryview);
2195         messageview->updating = FALSE;
2196
2197         if (messageview->deferred_destroy) {
2198                 debug_print("messageview got away!\n");
2199                 messageview_destroy(messageview);
2200                 return;
2201         }
2202         if (messageview->mainwin->summaryview->selected) {
2203 #ifndef GENERIC_UMPC
2204                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2205                        
2206                 if (msginfo)
2207                         messageview_show(messageview, msginfo, 
2208                                          messageview->all_headers);
2209 #endif
2210         } else {
2211                 gtk_widget_destroy(messageview->window);
2212         }
2213 }
2214
2215 static void prev_new_cb(GtkAction *action, gpointer data)
2216 {
2217         MessageView *messageview = (MessageView *)data;
2218         messageview->updating = TRUE;
2219         summary_select_prev_new(messageview->mainwin->summaryview);
2220         messageview->updating = FALSE;
2221
2222         if (messageview->deferred_destroy) {
2223                 debug_print("messageview got away!\n");
2224                 messageview_destroy(messageview);
2225                 return;
2226         }
2227         if (messageview->mainwin->summaryview->selected) {
2228 #ifndef GENERIC_UMPC
2229                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2230                        
2231                 if (msginfo)
2232                         messageview_show(messageview, msginfo, 
2233                                          messageview->all_headers);
2234 #endif
2235         } else {
2236                 gtk_widget_destroy(messageview->window);
2237         }
2238 }
2239
2240 static void next_new_cb(GtkAction *action, gpointer data)
2241 {
2242         MessageView *messageview = (MessageView *)data;
2243         messageview->updating = TRUE;
2244         summary_select_next_new(messageview->mainwin->summaryview);
2245         messageview->updating = FALSE;
2246
2247         if (messageview->deferred_destroy) {
2248                 debug_print("messageview got away!\n");
2249                 messageview_destroy(messageview);
2250                 return;
2251         }
2252         if (messageview->mainwin->summaryview->selected) {
2253 #ifndef GENERIC_UMPC
2254                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2255                        
2256                 if (msginfo)
2257                         messageview_show(messageview, msginfo, 
2258                                          messageview->all_headers);
2259 #endif
2260         } else {
2261                 gtk_widget_destroy(messageview->window);
2262         }
2263 }
2264
2265 static void prev_marked_cb(GtkAction *action, gpointer data)
2266 {
2267         MessageView *messageview = (MessageView *)data;
2268         messageview->updating = TRUE;
2269         summary_select_prev_marked(messageview->mainwin->summaryview);
2270         messageview->updating = FALSE;
2271
2272         if (messageview->deferred_destroy) {
2273                 debug_print("messageview got away!\n");
2274                 messageview_destroy(messageview);
2275                 return;
2276         }
2277         if (messageview->mainwin->summaryview->selected) {
2278 #ifndef GENERIC_UMPC
2279                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2280                        
2281                 if (msginfo)
2282                         messageview_show(messageview, msginfo, 
2283                                          messageview->all_headers);
2284 #endif
2285         } else {
2286                 gtk_widget_destroy(messageview->window);
2287         }
2288 }
2289
2290 static void next_marked_cb(GtkAction *action, gpointer data)
2291 {
2292         MessageView *messageview = (MessageView *)data;
2293         messageview->updating = TRUE;
2294         summary_select_next_marked(messageview->mainwin->summaryview);
2295         messageview->updating = FALSE;
2296
2297         if (messageview->deferred_destroy) {
2298                 debug_print("messageview got away!\n");
2299                 messageview_destroy(messageview);
2300                 return;
2301         }
2302         if (messageview->mainwin->summaryview->selected) {
2303 #ifndef GENERIC_UMPC
2304                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2305                        
2306                 if (msginfo)
2307                         messageview_show(messageview, msginfo, 
2308                                          messageview->all_headers);
2309 #endif
2310         } else {
2311                 gtk_widget_destroy(messageview->window);
2312         }
2313 }
2314
2315 static void prev_labeled_cb(GtkAction *action, gpointer data)
2316 {
2317         MessageView *messageview = (MessageView *)data;
2318         messageview->updating = TRUE;
2319         summary_select_prev_labeled(messageview->mainwin->summaryview);
2320         messageview->updating = FALSE;
2321
2322         if (messageview->deferred_destroy) {
2323                 debug_print("messageview got away!\n");
2324                 messageview_destroy(messageview);
2325                 return;
2326         }
2327         if (messageview->mainwin->summaryview->selected) {
2328 #ifndef GENERIC_UMPC
2329                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2330                        
2331                 if (msginfo)
2332                         messageview_show(messageview, msginfo, 
2333                                          messageview->all_headers);
2334 #endif
2335         } else {
2336                 gtk_widget_destroy(messageview->window);
2337         }
2338 }
2339
2340 static void next_labeled_cb(GtkAction *action, gpointer data)
2341 {
2342         MessageView *messageview = (MessageView *)data;
2343         messageview->updating = TRUE;
2344         summary_select_next_labeled(messageview->mainwin->summaryview);
2345         messageview->updating = FALSE;
2346
2347         if (messageview->deferred_destroy) {
2348                 debug_print("messageview got away!\n");
2349                 messageview_destroy(messageview);
2350                 return;
2351         }
2352         if (messageview->mainwin->summaryview->selected) {
2353 #ifndef GENERIC_UMPC
2354                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2355                        
2356                 if (msginfo)
2357                         messageview_show(messageview, msginfo, 
2358                                          messageview->all_headers);
2359 #endif
2360         } else {
2361                 gtk_widget_destroy(messageview->window);
2362         }
2363 }
2364
2365 static void last_read_cb(GtkAction *action, gpointer data)
2366 {
2367         MessageView *messageview = (MessageView *)data;
2368         messageview->updating = TRUE;
2369         summary_select_last_read(messageview->mainwin->summaryview);
2370         messageview->updating = FALSE;
2371
2372         if (messageview->deferred_destroy) {
2373                 debug_print("messageview got away!\n");
2374                 messageview_destroy(messageview);
2375                 return;
2376         }
2377         if (messageview->mainwin->summaryview->selected) {
2378 #ifndef GENERIC_UMPC
2379                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2380                        
2381                 if (msginfo)
2382                         messageview_show(messageview, msginfo, 
2383                                          messageview->all_headers);
2384 #endif
2385         } else {
2386                 gtk_widget_destroy(messageview->window);
2387         }
2388 }
2389
2390 static void parent_cb(GtkAction *action, gpointer data)
2391 {
2392         MessageView *messageview = (MessageView *)data;
2393         messageview->updating = TRUE;
2394         summary_select_parent(messageview->mainwin->summaryview);
2395         messageview->updating = FALSE;
2396
2397         if (messageview->deferred_destroy) {
2398                 debug_print("messageview got away!\n");
2399                 messageview_destroy(messageview);
2400                 return;
2401         }
2402         if (messageview->mainwin->summaryview->selected) {
2403 #ifndef GENERIC_UMPC
2404                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2405                        
2406                 if (msginfo)
2407                         messageview_show(messageview, msginfo, 
2408                                          messageview->all_headers);
2409 #endif
2410         } else {
2411                 gtk_widget_destroy(messageview->window);
2412         }
2413 }
2414
2415 static void goto_unread_folder_cb(GtkAction *action, gpointer data)
2416 {
2417         MessageView *messageview = (MessageView *)data;
2418         messageview->updating = TRUE;
2419         folderview_select_next_unread(messageview->mainwin->folderview, FALSE);
2420         messageview->updating = FALSE;
2421
2422         if (messageview->deferred_destroy) {
2423                 debug_print("messageview got away!\n");
2424                 messageview_destroy(messageview);
2425                 return;
2426         }
2427         if (messageview->mainwin->summaryview->selected) {
2428 #ifndef GENERIC_UMPC
2429                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2430                        
2431                 if (msginfo)
2432                         messageview_show(messageview, msginfo, 
2433                                          messageview->all_headers);
2434 #endif
2435         } else {
2436                 gtk_widget_destroy(messageview->window);
2437         }
2438 }
2439
2440 static void goto_folder_cb(GtkAction *action, gpointer data)
2441 {
2442         MessageView *messageview = (MessageView *)data;
2443         messageview->updating = TRUE;
2444         FolderItem *to_folder;
2445         messageview->updating = FALSE;
2446
2447         to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL, FALSE);
2448
2449         if (to_folder) {
2450                 folderview_select(messageview->mainwin->folderview, to_folder);
2451
2452                 if (messageview->deferred_destroy) {
2453                         debug_print("messageview got away!\n");
2454                         messageview_destroy(messageview);
2455                         return;
2456                 }
2457                 if (messageview->mainwin->summaryview->selected) {
2458 #ifndef GENERIC_UMPC
2459                         MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2460                        
2461                         if (msginfo)
2462                                 messageview_show(messageview, msginfo, 
2463                                                  messageview->all_headers);
2464 #endif
2465                 } else {
2466                         gtk_widget_destroy(messageview->window);
2467                 }
2468         }
2469 }
2470
2471 static void scroll_prev_line_cb(GtkAction *action, gpointer data)
2472 {
2473         MessageView *messageview = (MessageView *)data;
2474         mimeview_scroll_one_line(messageview->mimeview,TRUE);
2475 }
2476
2477 static void scroll_next_line_cb(GtkAction *action, gpointer data)
2478 {
2479         MessageView *messageview = (MessageView *)data;
2480         mimeview_scroll_one_line(messageview->mimeview,FALSE);
2481 }
2482
2483 static void scroll_prev_page_cb(GtkAction *action, gpointer data)
2484 {
2485         MessageView *messageview = (MessageView *)data;
2486         mimeview_scroll_page(messageview->mimeview,TRUE);
2487 }
2488
2489 static void scroll_next_page_cb(GtkAction *action, gpointer data)
2490 {
2491         MessageView *messageview = (MessageView *)data;
2492         mimeview_scroll_page(messageview->mimeview,FALSE);
2493 }
2494
2495 static void set_charset_cb(GtkAction *action, GtkRadioAction *current, gpointer data)
2496 {
2497         MessageView *messageview = (MessageView *)data;
2498         gboolean active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (current));
2499         gint value = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (current));
2500         const gchar *charset;
2501
2502         if (active) {
2503                 charset = conv_get_charset_str((CharSet)value);
2504                 g_free(messageview->forced_charset);
2505                 messageview->forced_charset = g_strdup(charset);
2506                 procmime_force_charset(charset);
2507                 
2508                 messageview_show(messageview, messageview->msginfo, FALSE);
2509         }
2510 }
2511
2512 static void set_decode_cb(GtkAction *action, GtkRadioAction *current, gpointer data)
2513 {
2514         MessageView *messageview = (MessageView *)data;
2515         gboolean active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (current));
2516         gint value = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (current));
2517
2518         if (active) {
2519                 messageview->forced_encoding = (EncodingType)value;
2520
2521                 messageview_show(messageview, messageview->msginfo, FALSE);
2522                 debug_print("forced encoding: %d\n", value);
2523         }
2524 }
2525
2526
2527 static void view_source_cb(GtkAction *action, gpointer data)
2528 {
2529         MessageView *messageview = (MessageView *)data;
2530         SourceWindow *srcwin;
2531
2532         if (!messageview->msginfo) return;
2533
2534         srcwin = source_window_create();
2535         source_window_show_msg(srcwin, messageview->msginfo);
2536         source_window_show(srcwin);
2537 }
2538
2539 static void show_all_header_cb(GtkToggleAction *action, gpointer data)
2540 {
2541         MessageView *messageview = (MessageView *)data;
2542         MsgInfo *msginfo = messageview->msginfo;
2543
2544         if (messageview->mimeview->textview &&
2545             messageview->mimeview->textview->loading) {
2546                 return;
2547         }
2548         if (messageview->updating)
2549                 return;
2550
2551         messageview->all_headers = prefs_common.show_all_headers =
2552                         gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
2553         if (!msginfo) return;
2554         messageview->msginfo = NULL;
2555         messageview_show(messageview, msginfo, messageview->all_headers);
2556         procmsg_msginfo_free(msginfo);
2557         main_window_set_menu_sensitive(messageview->mainwin);
2558         summary_redisplay_msg(messageview->mainwin->summaryview);
2559 }
2560
2561 static void msg_hide_quotes_cb(GtkToggleAction *action, gpointer data)
2562 {
2563         MessageView *messageview = (MessageView *)data;
2564         MsgInfo *msginfo = messageview->msginfo;
2565         static gboolean updating_menu = FALSE;
2566
2567         if (updating_menu)
2568                 return;
2569         if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action))) {
2570                 const gchar *a_name = gtk_action_get_name(GTK_ACTION(action));
2571                 if (!strcmp(a_name, "View/Quotes/CollapseAll")) prefs_common.hide_quotes = 1;
2572                 else if (!strcmp(a_name, "View/Quotes/Collapse2")) prefs_common.hide_quotes = 2;
2573                 else if (!strcmp(a_name, "View/Quotes/Collapse3")) prefs_common.hide_quotes = 3;
2574         } else
2575                 prefs_common.hide_quotes = 0;
2576         
2577         updating_menu=TRUE;
2578         
2579         cm_toggle_menu_set_active_full(messageview->ui_manager, "Menu/View/Quotes/CollapseAll", (prefs_common.hide_quotes == 1));
2580         cm_toggle_menu_set_active_full(messageview->ui_manager, "Menu/View/Quotes/Collapse2", (prefs_common.hide_quotes == 2));
2581         cm_toggle_menu_set_active_full(messageview->ui_manager, "Menu/View/Quotes/Collapse3", (prefs_common.hide_quotes == 3));
2582
2583         updating_menu=FALSE;
2584         if (!msginfo) return;
2585         messageview->msginfo = NULL;
2586         messageview_show(messageview, msginfo,
2587                          messageview->all_headers);
2588         procmsg_msginfo_free(msginfo);
2589         
2590         /* update main window */
2591         main_window_set_menu_sensitive(messageview->mainwin);
2592         summary_redisplay_msg(messageview->mainwin->summaryview);
2593 }
2594 #undef SET_CHECK_MENU_ACTIVE
2595
2596 static void compose_cb(GtkAction *action, gpointer data)
2597 {
2598         MessageView *messageview = (MessageView *)data;
2599         PrefsAccount *ac = NULL;
2600         FolderItem *item = NULL;
2601
2602         if (messageview->msginfo)
2603                 item = messageview->msginfo->folder;
2604
2605         if (item) {
2606                 ac = account_find_from_item(item);
2607                 if (ac && ac->protocol == A_NNTP &&
2608                     FOLDER_TYPE(item->folder) == F_NEWS) {
2609                         compose_new(ac, item->path, NULL);
2610                         return;
2611                 }
2612         }
2613
2614         compose_new(ac, NULL, NULL);
2615 }
2616
2617 #define DO_ACTION(name, act)    { if (!strcmp(a_name, name)) action = act; }
2618
2619 static void reply_cb(GtkAction *gaction, gpointer data)
2620 {
2621         MessageView *messageview = (MessageView *)data;
2622         GSList *msginfo_list = NULL;
2623         gint action = COMPOSE_REPLY;
2624         const gchar *a_name = gtk_action_get_name(gaction);
2625         
2626         cm_return_if_fail(messageview->msginfo);
2627
2628         DO_ACTION("Message/Reply", COMPOSE_REPLY);
2629         DO_ACTION("Message/ReplyTo/All", COMPOSE_REPLY_TO_ALL);
2630         DO_ACTION("Message/ReplyTo/Sender", COMPOSE_REPLY_TO_SENDER);
2631         DO_ACTION("Message/ReplyTo/List", COMPOSE_REPLY_TO_LIST);
2632         DO_ACTION("Message/Forward", COMPOSE_FORWARD_INLINE);
2633         DO_ACTION("Message/ForwardAtt", COMPOSE_FORWARD_AS_ATTACH);
2634         DO_ACTION("Message/Redirect", COMPOSE_REDIRECT);
2635
2636         msginfo_list = g_slist_append(msginfo_list, messageview->msginfo);
2637         compose_reply_from_messageview(messageview, msginfo_list, action);
2638         g_slist_free(msginfo_list);
2639 }
2640
2641 static void addressbook_open_cb(GtkAction *action, gpointer data)
2642 {
2643         addressbook_open(NULL);
2644 }
2645
2646 static void add_address_cb(GtkAction *action, gpointer data)
2647 {
2648         MessageView *messageview = (MessageView *)data;
2649         MsgInfo *msginfo, *full_msginfo;
2650         gchar *from;
2651         GtkWidget *image = NULL;
2652         GdkPixbuf *picture = NULL;
2653
2654         if (!messageview->msginfo || !messageview->msginfo->from) 
2655                 return;
2656
2657         msginfo = messageview->msginfo;
2658         Xstrdup_a(from, msginfo->from, return);
2659         eliminate_address_comment(from);
2660         extract_address(from);
2661         
2662         full_msginfo = procmsg_msginfo_get_full_info(msginfo);
2663         if (full_msginfo &&
2664             full_msginfo->extradata &&
2665             full_msginfo->extradata->face) {
2666                 image = face_get_from_header(full_msginfo->extradata->face);
2667         } 
2668 #if HAVE_LIBCOMPFACE
2669         else if (full_msginfo &&
2670                  full_msginfo->extradata &&
2671                  full_msginfo->extradata->xface) {
2672                 image = xface_get_from_header(full_msginfo->extradata->xface,
2673                                 &(gtk_widget_get_style(messageview->mainwin->summaryview->ctree)->white),
2674                                 gtk_widget_get_window(messageview->window));
2675         }
2676 #endif
2677         procmsg_msginfo_free(full_msginfo);
2678         if (image)
2679                 picture = gtk_image_get_pixbuf(GTK_IMAGE(image));
2680
2681         addressbook_add_contact(msginfo->fromname, from, NULL, picture);
2682
2683         if (image)
2684                 gtk_widget_destroy(image);
2685 }
2686
2687 static void create_filter_cb(GtkAction *gaction, gpointer data)
2688 {
2689         MessageView *messageview = (MessageView *)data;
2690         FolderItem * item;
2691         gint action = -1;
2692         const gchar *a_name = gtk_action_get_name(gaction);
2693
2694         if (!messageview->msginfo) return;
2695
2696         DO_ACTION("Tools/CreateFilterRule/Automatically", FILTER_BY_AUTO);
2697         DO_ACTION("Tools/CreateFilterRule/ByFrom", FILTER_BY_FROM);
2698         DO_ACTION("Tools/CreateFilterRule/ByTo", FILTER_BY_TO);
2699         DO_ACTION("Tools/CreateFilterRule/BySubject", FILTER_BY_SUBJECT);
2700         
2701         item = messageview->msginfo->folder;
2702         summary_msginfo_filter_open(item,  messageview->msginfo,
2703                                     (PrefsFilterType)action, 0);
2704 }
2705
2706 static void create_processing_cb(GtkAction *gaction, gpointer data)
2707 {
2708         MessageView *messageview = (MessageView *)data;
2709         FolderItem * item;
2710         gint action = -1;
2711         const gchar *a_name = gtk_action_get_name(gaction);
2712         
2713         if (!messageview->msginfo) return;
2714         
2715         DO_ACTION("Tools/CreateProcessingRule/Automatically", FILTER_BY_AUTO);
2716         DO_ACTION("Tools/CreateProcessingRule/ByFrom", FILTER_BY_FROM);
2717         DO_ACTION("Tools/CreateProcessingRule/ByTo", FILTER_BY_TO);
2718         DO_ACTION("Tools/CreateProcessingRule/BySubject", FILTER_BY_SUBJECT);
2719
2720         item = messageview->msginfo->folder;
2721         summary_msginfo_filter_open(item,  messageview->msginfo,
2722                                     (PrefsFilterType)action, 1);
2723 }
2724
2725 static void open_urls_cb(GtkAction *action, gpointer data)
2726 {
2727         MessageView *messageview = (MessageView *)data;
2728         messageview_list_urls(messageview);
2729 }
2730
2731 static void about_cb(GtkAction *gaction, gpointer data)
2732 {
2733         about_show();
2734 }
2735
2736 static gboolean messageview_update_msg(gpointer source, gpointer data)
2737 {
2738         MsgInfoUpdate *msginfo_update = (MsgInfoUpdate *) source;
2739         MessageView *messageview = (MessageView *)data;
2740
2741         if (messageview->msginfo != msginfo_update->msginfo)
2742                 return FALSE;
2743
2744         if (msginfo_update->flags & MSGINFO_UPDATE_DELETED) {
2745                 MsgInfo *old_msginfo = messageview->msginfo;
2746                 messageview_clear(messageview);
2747                 messageview_update(messageview, old_msginfo);
2748         }
2749
2750         return FALSE;
2751 }
2752
2753 void messageview_set_menu_sensitive(MessageView *messageview)
2754 {
2755         if (!messageview || !messageview->new_window) 
2756                 return;
2757         /* do some smart things */
2758         if (!messageview->menubar) return;
2759
2760         cm_toggle_menu_set_active_full(messageview->ui_manager, "Menu/View/Quotes/CollapseAll", (prefs_common.hide_quotes == 1));
2761         cm_toggle_menu_set_active_full(messageview->ui_manager, "Menu/View/Quotes/Collapse2", (prefs_common.hide_quotes == 2));
2762         cm_toggle_menu_set_active_full(messageview->ui_manager, "Menu/View/Quotes/Collapse3", (prefs_common.hide_quotes == 3));
2763 }
2764
2765 void messageview_learn (MessageView *msgview, gboolean is_spam)
2766 {
2767         if (is_spam) {
2768                 if (procmsg_spam_learner_learn(msgview->msginfo, NULL, TRUE) == 0)
2769                         procmsg_msginfo_set_flags(msgview->msginfo, MSG_SPAM, 0);
2770                 else
2771                         log_error(LOG_PROTOCOL, _("An error happened while learning.\n"));
2772                 
2773         } else {
2774                 if (procmsg_spam_learner_learn(msgview->msginfo, NULL, FALSE) == 0)
2775                         procmsg_msginfo_unset_flags(msgview->msginfo, MSG_SPAM, 0);
2776                 else
2777                         log_error(LOG_PROTOCOL, _("An error happened while learning.\n"));
2778         }
2779         if (msgview->toolbar)
2780                 toolbar_set_learn_button
2781                         (msgview->toolbar,
2782                          MSG_IS_SPAM(msgview->msginfo->flags)?LEARN_HAM:LEARN_SPAM);
2783         else
2784                 toolbar_set_learn_button
2785                         (msgview->mainwin->toolbar,
2786                          MSG_IS_SPAM(msgview->msginfo->flags)?LEARN_HAM:LEARN_SPAM);
2787 }
2788
2789 void messageview_list_urls (MessageView *msgview)
2790 {
2791         GSList *cur = msgview->mimeview->textview->uri_list;
2792         GSList *newlist = NULL;
2793         GHashTable *uri_hashtable;
2794         gchar *tmp;
2795         
2796         uri_hashtable = g_hash_table_new_full(g_str_hash, g_str_equal,
2797                                          (GDestroyNotify) g_free, NULL);
2798         
2799         for (; cur; cur = cur->next) {
2800                 ClickableText *uri = (ClickableText *)cur->data;
2801                 if (uri->uri &&
2802                     (!g_ascii_strncasecmp(uri->uri, "ftp.", 4) ||
2803                      !g_ascii_strncasecmp(uri->uri, "ftp:", 4) ||
2804                      !g_ascii_strncasecmp(uri->uri, "www.", 4) ||
2805                      !g_ascii_strncasecmp(uri->uri, "http:", 5) ||
2806                      !g_ascii_strncasecmp(uri->uri, "https:", 6)))
2807                 {
2808                         tmp = g_utf8_strdown(uri->uri, -1);
2809                         
2810                         if (g_hash_table_lookup(uri_hashtable, tmp)) {
2811                                 g_free(tmp);
2812                                 continue;
2813                         }
2814                         
2815                         newlist = g_slist_prepend(newlist, uri);
2816                         g_hash_table_insert(uri_hashtable, tmp,
2817                                             GUINT_TO_POINTER(g_str_hash(tmp)));
2818                 }
2819         }
2820         newlist = g_slist_reverse(newlist);
2821         uri_opener_open(msgview, newlist);
2822         g_slist_free(newlist);
2823         g_hash_table_destroy(uri_hashtable);
2824 }