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