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