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