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