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