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