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