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