bb5b648cbdf5ed7efe92c30664d8ea77d14fceb2
[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                         gboolean can_learn = FALSE;
1031                         if (procmsg_spam_can_learn() &&
1032                             (msginfo->folder &&
1033                              msginfo->folder->folder->klass->type != F_UNKNOWN &&
1034                              msginfo->folder->folder->klass->type != F_NEWS))
1035                                 can_learn = TRUE;
1036
1037                         gtk_widget_set_sensitive(
1038                                 messageview->toolbar->learn_spam_btn, 
1039                                 can_learn);
1040                 }
1041         }
1042         
1043         noticeview_hide(messageview->noticeview);
1044         mimeview_clear(messageview->mimeview);
1045         messageview->updating = TRUE;
1046
1047         if (msginfo->size > 1024*1024)
1048                 statuswindow_print_all(_("Fetching message (%s)..."),
1049                         to_human_readable(msginfo->size));
1050         
1051         file = procmsg_get_message_file_path(msginfo);
1052
1053         if (msginfo->size > 1024*1024)
1054                 statuswindow_pop_all();
1055
1056         if (!file) {
1057                 g_warning("can't get message file path.\n");
1058                 textview_show_error(messageview->mimeview->textview);
1059                 return -1;
1060         }
1061         
1062         if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
1063             !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
1064                 mimeinfo = procmime_scan_file(file);
1065         else
1066                 mimeinfo = procmime_scan_queue_file(file);
1067
1068         messageview->updating = FALSE;
1069         
1070         if (messageview->deferred_destroy) {
1071                 g_free(file);
1072                 messageview_destroy(messageview);
1073                 return 0;
1074         }
1075
1076         if (!mimeinfo) {
1077                 textview_show_error(messageview->mimeview->textview);
1078                 return -1;
1079         }
1080
1081         while ((encinfo = find_encrypted_part(mimeinfo)) != NULL) {
1082                 debug_print("decrypting message part\n");
1083                 if (privacy_mimeinfo_decrypt(encinfo) < 0) {
1084                         alertpanel_error(_("Couldn't decrypt: %s"),
1085                                 privacy_get_error());
1086                         break;
1087                 }
1088         }
1089                         
1090         if (messageview->msginfo != msginfo) {
1091                 procmsg_msginfo_free(messageview->msginfo);
1092                 messageview->msginfo = NULL;
1093                 messageview_set_menu_sensitive(messageview);
1094                 messageview->msginfo = 
1095                         procmsg_msginfo_get_full_info_from_file(msginfo, file);
1096                 if (!messageview->msginfo)
1097                         messageview->msginfo = procmsg_msginfo_copy(msginfo);
1098         } else {
1099                 messageview->msginfo = NULL;
1100                 messageview_set_menu_sensitive(messageview);
1101                 messageview->msginfo = msginfo;
1102         }
1103         headerview_show(messageview->headerview, messageview->msginfo);
1104
1105         messageview_set_position(messageview, 0);
1106
1107         textview_set_all_headers(messageview->mimeview->textview, 
1108                         messageview->all_headers);
1109
1110 #ifdef MAEMO
1111         maemo_window_full_screen_if_needed(GTK_WINDOW(messageview->window));
1112 #endif
1113         if (messageview->window) {
1114                 gtk_window_set_title(GTK_WINDOW(messageview->window), 
1115                                 _("Claws Mail - Message View"));
1116                 GTK_EVENTS_FLUSH();
1117         }
1118         mimeview_show_message(messageview->mimeview, mimeinfo, file);
1119         
1120 #ifndef GENERIC_UMPC
1121         messageview_set_position(messageview, 0);
1122 #endif
1123
1124         if (messageview->window && msginfo->subject) {
1125                 subject = g_strdup(msginfo->subject);
1126                 if (!g_utf8_validate(subject, -1, NULL)) {
1127                         g_free(subject);
1128                         subject = g_malloc(strlen(msginfo->subject)*2 +1);
1129                         conv_localetodisp(subject, strlen(msginfo->subject)*2 +1, 
1130                                 msginfo->subject);
1131                 }
1132                 if (g_utf8_validate(subject, -1, NULL))
1133                         gtk_window_set_title(GTK_WINDOW(messageview->window), 
1134                                 subject);
1135                 g_free(subject);
1136         }
1137
1138         if (msginfo && msginfo->folder) {
1139                 msginfo->folder->last_seen = msginfo->msgnum;   
1140         }
1141
1142         main_create_mailing_list_menu(messageview->mainwin, messageview->msginfo);
1143
1144         if (messageview->msginfo && messageview->msginfo->extradata
1145             && messageview->msginfo->extradata->partial_recv
1146             && !noticeview_is_visible(messageview->noticeview))
1147                 partial_recv_show(messageview->noticeview, 
1148                                   messageview->msginfo);
1149         else if (messageview->msginfo && messageview->msginfo->extradata &&
1150             (messageview->msginfo->extradata->dispositionnotificationto || 
1151              messageview->msginfo->extradata->returnreceiptto) &&
1152             !MSG_IS_RETRCPT_SENT(messageview->msginfo->flags) &&
1153             !prefs_common.never_send_retrcpt &&
1154             !noticeview_is_visible(messageview->noticeview))
1155                 return_receipt_show(messageview->noticeview, 
1156                                     messageview->msginfo);
1157
1158         mimeinfo = procmime_mimeinfo_next(mimeinfo);
1159         if (!all_headers && mimeinfo 
1160                         && (mimeinfo->type != MIMETYPE_TEXT || 
1161             strcasecmp(mimeinfo->subtype, "plain")) 
1162                         && (mimeinfo->type != MIMETYPE_MULTIPART || 
1163             strcasecmp(mimeinfo->subtype, "signed"))) {
1164                 if (strcasecmp(mimeinfo->subtype, "html")) {
1165                         MimeInfo *saved_mimeinfo = mimeinfo;
1166                         MimeInfo *alt_parent = mimeinfo;
1167
1168                         /* if multipart/{related,mixed} part, look inside for a multipart/alternative child */
1169                         if (prefs_common.promote_html_part &&
1170                             mimeinfo->type == MIMETYPE_MULTIPART &&
1171                             (!strcasecmp(mimeinfo->subtype, "related") ||
1172                              !strcasecmp(mimeinfo->subtype, "mixed"))) {
1173                                 for (; mimeinfo; mimeinfo = procmime_mimeinfo_next(mimeinfo)) {
1174                                         if (mimeinfo->node->parent != saved_mimeinfo->node) {
1175                                                 /* only consider children of the 
1176                                                  * multipart/{related,mixed} part */
1177                                                 continue;
1178                                         }
1179                                         if (mimeinfo->type == MIMETYPE_MULTIPART && 
1180                                             !strcasecmp(mimeinfo->subtype, "alternative")) {
1181                                                 /* we got an alternative part */
1182                                                 alt_parent = mimeinfo;
1183                                                 break;
1184                                         }
1185                                 }
1186                         }
1187
1188                         /* if we now have a multipart/alternative part (possibly inside a
1189                          * multipart/{related,mixed} part, look for an HTML part inside */
1190                         if (prefs_common.promote_html_part && mimeinfo && 
1191                             mimeinfo->type == MIMETYPE_MULTIPART &&
1192                             !strcasecmp(mimeinfo->subtype, "alternative")) {
1193                                 for (; mimeinfo; mimeinfo = procmime_mimeinfo_next(mimeinfo)) {
1194                                         if (mimeinfo->node->parent != alt_parent->node) {
1195                                                 /* only consider children of the 
1196                                                  * multipart/alternative part, so as
1197                                                  * not to show html attachments */
1198                                                 continue;
1199                                         }
1200                                         if (mimeinfo->type == MIMETYPE_TEXT && 
1201                                             !strcasecmp(mimeinfo->subtype, "html")) {
1202                                                 /* we got it */
1203                                                 mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
1204                                                 goto done;
1205                                         }
1206                                 }
1207                         }
1208                         
1209                         /* if we didn't find anything, go back to start */
1210                         if (!mimeinfo) 
1211                                 mimeinfo = saved_mimeinfo;
1212
1213                         mimeview_show_part(messageview->mimeview,mimeinfo);
1214                         goto done;
1215                 } else if (prefs_common.invoke_plugin_on_html) {
1216                         mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
1217                         goto done;
1218                 }
1219         }
1220         if (!all_headers && mimeinfo &&
1221             mimeinfo->type == MIMETYPE_MULTIPART &&
1222             mimeview_has_viewer_for_content_type(messageview->mimeview, "text/calendar")) {
1223                 /* look for a calendar part or it looks really strange */
1224                 while (mimeinfo) {
1225                         if (mimeinfo->type == MIMETYPE_TEXT &&
1226                             !strcasecmp(mimeinfo->subtype, "calendar")) {
1227                                 mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
1228                                 goto done;
1229                         }
1230                         mimeinfo = procmime_mimeinfo_next(mimeinfo);
1231                 }
1232         }
1233 done:
1234         g_free(file);
1235
1236         return 0;
1237 }
1238
1239 void messageview_reflect_prefs_pixmap_theme(void)
1240 {
1241         GList *cur;
1242         MessageView *msgview;
1243
1244         for (cur = msgview_list; cur != NULL; cur = cur->next) {
1245                 msgview = (MessageView*)cur->data;
1246                 toolbar_update(TOOLBAR_MSGVIEW, msgview);
1247                 mimeview_update(msgview->mimeview);
1248         }
1249 }
1250
1251 void messageview_clear(MessageView *messageview)
1252 {
1253         if (!messageview)
1254                 return;
1255         procmsg_msginfo_free(messageview->msginfo);
1256         messageview->msginfo = NULL;
1257         messageview->filtered = FALSE;
1258         mimeview_clear(messageview->mimeview);
1259         headerview_clear(messageview->headerview);
1260         noticeview_hide(messageview->noticeview);
1261 }
1262
1263 void messageview_destroy(MessageView *messageview)
1264 {
1265         debug_print("destroy messageview\n");
1266         messageview_list = g_list_remove(messageview_list, messageview);
1267
1268         if (messageview->mainwin->summaryview->messageview == messageview) {
1269                 messageview->mainwin->summaryview->displayed = NULL;
1270                 messageview->mainwin->summaryview->messageview = NULL;
1271         }
1272         if (messageview->mainwin->summaryview->ext_messageview == messageview) {
1273                 messageview->mainwin->summaryview->displayed = NULL;
1274                 messageview->mainwin->summaryview->ext_messageview = NULL;
1275         }
1276         if (!messageview->deferred_destroy) {
1277                 hooks_unregister_hook(MSGINFO_UPDATE_HOOKLIST,
1278                               messageview->msginfo_update_callback_id);
1279         }
1280
1281         if (messageview->updating) {
1282                 debug_print("uh oh, better not touch that now (fetching)\n");
1283                 messageview->deferred_destroy = TRUE;
1284                 gtk_widget_hide(messageview->window);
1285                 return;
1286         }
1287         
1288         if (messageview->mimeview->textview
1289         &&  messageview->mimeview->textview->loading) {
1290                 debug_print("uh oh, better not touch that now (loading text)\n");
1291                 messageview->deferred_destroy = TRUE;
1292                 messageview->mimeview->textview->stop_loading = TRUE;
1293                 gtk_widget_hide(messageview->window);
1294                 return;
1295         }
1296
1297         headerview_destroy(messageview->headerview);
1298         mimeview_destroy(messageview->mimeview);
1299         noticeview_destroy(messageview->noticeview);
1300
1301         procmsg_msginfo_free(messageview->msginfo);
1302         toolbar_clear_list(TOOLBAR_MSGVIEW);
1303         if (messageview->toolbar) {
1304                 toolbar_destroy(messageview->toolbar);
1305                 g_free(messageview->toolbar);
1306         }
1307         
1308         msgview_list = g_list_remove(msgview_list, messageview); 
1309
1310         if (messageview->window)
1311                 gtk_widget_destroy(messageview->window);
1312         g_free(messageview);
1313 }
1314
1315 void messageview_delete(MessageView *msgview)
1316 {
1317         MsgInfo *msginfo = NULL;
1318         FolderItem *trash = NULL;
1319         PrefsAccount *ac = NULL;
1320
1321         if (msgview->msginfo && msgview->mainwin && msgview->mainwin->summaryview)
1322                 msginfo = summary_get_selected_msg(msgview->mainwin->summaryview);
1323         
1324         /* need a procmsg_msginfo_equal() */
1325         if (msginfo && msgview->msginfo && 
1326             msginfo->msgnum == msgview->msginfo->msgnum && 
1327             msginfo->folder == msgview->msginfo->folder) {
1328                 summary_delete_trash(msgview->mainwin->summaryview);
1329         } else {                
1330                 msginfo = msgview->msginfo;
1331
1332                 g_return_if_fail(msginfo != NULL);
1333
1334                 /* to get the trash folder, we have to choose either
1335                  * the folder's or account's trash default - we prefer
1336                  * the one in the account prefs */
1337                 if (msginfo->folder) {
1338                         if (NULL != (ac = account_find_from_item(msginfo->folder)))
1339                                 trash = account_get_special_folder(ac, F_TRASH);
1340                         if (!trash && msginfo->folder->folder)  
1341                                 trash = msginfo->folder->folder->trash;
1342                         /* if still not found, use the default */
1343                         if (!trash) 
1344                                 trash = folder_get_default_trash();
1345                 }       
1346
1347                 g_return_if_fail(trash != NULL);
1348
1349                 if (prefs_common.immediate_exec)
1350                         /* TODO: Delete from trash */
1351                         folder_item_move_msg(trash, msginfo);
1352                 else {
1353                         procmsg_msginfo_set_to_folder(msginfo, trash);
1354                         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
1355                         /* NOTE: does not update to next message in summaryview */
1356                 }
1357         }
1358 #ifdef GENERIC_UMPC
1359         if (msgview->window && !prefs_common.always_show_msg) {
1360                 messageview_destroy(msgview);
1361         }
1362 #endif
1363 }
1364
1365 /* 
1366  * \brief update messageview with currently selected message in summaryview
1367  *        leave unchanged if summaryview is empty
1368  * \param pointer to MessageView
1369  */     
1370 static void messageview_update(MessageView *msgview, MsgInfo *old_msginfo)
1371 {
1372         SummaryView *summaryview = (SummaryView*)msgview->mainwin->summaryview;
1373
1374         g_return_if_fail(summaryview != NULL);
1375         
1376         if (summaryview->selected) {
1377                 MsgInfo *msginfo = summary_get_selected_msg(summaryview);
1378                 if (msginfo == NULL || msginfo == old_msginfo)
1379                         return;
1380
1381                 messageview_show(msgview, msginfo, 
1382                                  msgview->all_headers);
1383         } 
1384 }
1385
1386 TextView *messageview_get_current_textview(MessageView *messageview)
1387 {
1388         TextView *text = NULL;
1389
1390         text = messageview->mimeview->textview;
1391
1392         return text;
1393 }
1394
1395 MimeInfo *messageview_get_selected_mime_part(MessageView *messageview)
1396 {
1397         return mimeview_get_selected_part(messageview->mimeview);
1398 }
1399
1400 void messageview_copy_clipboard(MessageView *messageview)
1401 {
1402         gchar *text = messageview_get_selection(messageview);
1403         if (text) {
1404                 gtk_clipboard_set_text(
1405                         gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
1406                         text, -1);
1407         }
1408         g_free(text);
1409 }
1410
1411 void messageview_select_all(MessageView *messageview)
1412 {
1413         TextView *text;
1414
1415         text = messageview_get_current_textview(messageview);
1416         if (text) {
1417                 GtkTextView *textview = GTK_TEXT_VIEW(text->text);
1418                 GtkTextBuffer *buffer;
1419                 GtkTextIter start, end;
1420
1421                 buffer = gtk_text_view_get_buffer(textview);
1422                 gtk_text_buffer_get_bounds(buffer, &start, &end);
1423                 gtk_text_buffer_select_range(buffer, &start, &end);
1424         }
1425 }
1426
1427 void messageview_set_position(MessageView *messageview, gint pos)
1428 {
1429         TextView *text;
1430
1431         text = messageview_get_current_textview(messageview);
1432         if (text)
1433                 textview_set_position(text, pos);
1434 }
1435
1436 gboolean messageview_search_string(MessageView *messageview, const gchar *str,
1437                                    gboolean case_sens)
1438 {
1439         TextView *text;
1440
1441         if (messageview->mimeview->type == MIMEVIEW_VIEWER) {
1442                 MimeViewer *viewer = messageview->mimeview->mimeviewer;
1443                 if (viewer && viewer->text_search) {
1444                         return viewer->text_search(viewer, FALSE, str, case_sens);
1445                 }
1446         }
1447
1448         text = messageview_get_current_textview(messageview);
1449         if (text)
1450                 return textview_search_string(text, str, case_sens);
1451         return FALSE;
1452 }
1453
1454 gboolean messageview_search_string_backward(MessageView *messageview,
1455                                             const gchar *str,
1456                                             gboolean case_sens)
1457 {
1458         TextView *text;
1459
1460         if (messageview->mimeview->type == MIMEVIEW_VIEWER) {
1461                 MimeViewer *viewer = messageview->mimeview->mimeviewer;
1462                 if (viewer && viewer->text_search) {
1463                         return viewer->text_search(viewer, TRUE, str, case_sens);
1464                 }
1465         }
1466
1467         text = messageview_get_current_textview(messageview);
1468         if (text)       
1469                 return textview_search_string_backward(text,
1470                                                        str, case_sens);
1471         return FALSE;
1472 }
1473
1474 gboolean messageview_is_visible(MessageView *messageview)
1475 {
1476         if (messageview == NULL)
1477                 return FALSE;
1478         return messageview->visible;
1479 }
1480
1481 static void messageview_save_as(MessageView *messageview)
1482 {
1483         gchar *filename = NULL;
1484         MsgInfo *msginfo;
1485         gchar *src, *dest, *tmp;
1486
1487         if (!messageview->msginfo) return;
1488         msginfo = messageview->msginfo;
1489
1490         if (msginfo->subject) {
1491                 Xstrdup_a(filename, msginfo->subject, return);
1492                 subst_for_filename(filename);
1493         }
1494         if (filename && !g_utf8_validate(filename, -1, NULL)) {
1495                 gchar *oldstr = filename;
1496                 filename = conv_codeset_strdup(filename,
1497                                                conv_get_locale_charset_str(),
1498                                                CS_UTF_8);
1499                 if (!filename) {
1500                         g_warning("messageview_save_as(): failed to convert character set.");
1501                         filename = g_strdup(oldstr);
1502                 }
1503                 dest = filesel_select_file_save(_("Save as"), filename);
1504                 g_free(filename);
1505         } else
1506                 dest = filesel_select_file_save(_("Save as"), filename);
1507         if (!dest) return;
1508         if (is_file_exist(dest)) {
1509                 AlertValue aval;
1510
1511                 aval = alertpanel(_("Overwrite"),
1512                                   _("Overwrite existing file?"),
1513                                   GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL);
1514                 if (G_ALERTALTERNATE != aval) return;
1515         }
1516
1517         src = procmsg_get_message_file(msginfo);
1518         if (copy_file(src, dest, TRUE) < 0) {
1519                 tmp =  g_path_get_basename(dest);
1520                 alertpanel_error(_("Couldn't save the file '%s'."), tmp);
1521                 g_free(tmp);
1522         }
1523         g_free(dest);
1524         g_free(src);
1525 }
1526
1527 static gint messageview_delete_cb(GtkWidget *widget, GdkEventAny *event,
1528                                   MessageView *messageview)
1529 {
1530         messageview_destroy(messageview);
1531         return TRUE;
1532 }
1533
1534 static void messageview_size_allocate_cb(GtkWidget *widget,
1535                                          GtkAllocation *allocation)
1536 {
1537         g_return_if_fail(allocation != NULL);
1538
1539         prefs_common.msgwin_width  = allocation->width;
1540         prefs_common.msgwin_height = allocation->height;
1541 }
1542 #ifndef MAEMO
1543 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
1544                         MessageView *messageview)
1545 {
1546         if (event && event->keyval == GDK_Escape && messageview->window) {
1547                 messageview_destroy(messageview);
1548                 return TRUE;
1549         }
1550
1551         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK|GDK_SHIFT_MASK)) != 0)
1552                 return FALSE;
1553
1554         g_signal_stop_emission_by_name(G_OBJECT(widget),
1555                                         "key_press_event");
1556         mimeview_pass_key_press_event(messageview->mimeview, event);
1557         return FALSE;
1558 }
1559 #endif
1560
1561 static void messageview_show_partial_display_cb(NoticeView *noticeview, MessageView *messageview)
1562 {
1563         messageview->show_full_text = TRUE;
1564         main_window_cursor_wait(mainwindow_get_mainwindow());
1565         GTK_EVENTS_FLUSH();
1566         messageview_show(messageview, messageview->msginfo, messageview->all_headers);
1567         main_window_cursor_normal(mainwindow_get_mainwindow());
1568 }
1569
1570 void messageview_show_partial_display(MessageView *messageview, MsgInfo *msginfo,
1571                                              size_t length)
1572 {
1573         gchar *msg = g_strdup_printf(_("Show all %s."), to_human_readable((goffset)length));
1574         noticeview_set_icon(messageview->noticeview, STOCK_PIXMAP_NOTICE_WARN);
1575         noticeview_set_text(messageview->noticeview, _("Only the first megabyte of text is shown."));
1576         noticeview_set_button_text(messageview->noticeview, msg);
1577         g_free(msg);
1578         noticeview_set_button_press_callback(messageview->noticeview,
1579                                              G_CALLBACK(messageview_show_partial_display_cb),
1580                                              (gpointer) messageview);
1581         noticeview_show(messageview->noticeview);
1582 }
1583
1584 static void return_receipt_show(NoticeView *noticeview, MsgInfo *msginfo)
1585 {
1586         gchar *addr = NULL;
1587         gboolean from_me = FALSE;
1588         if (msginfo->folder 
1589                 && (folder_has_parent_of_type(msginfo->folder, F_QUEUE)
1590                  || folder_has_parent_of_type(msginfo->folder, F_DRAFT)))
1591                 return;
1592
1593         addr = g_strdup(msginfo->from);
1594         if (addr) {
1595                 extract_address(addr);
1596                 if (account_find_from_address(addr, FALSE)) {
1597                         from_me = TRUE;
1598                 }
1599                 g_free(addr);
1600         }
1601
1602         if (from_me) {
1603                 noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
1604                 if (MSG_IS_RETRCPT_GOT(msginfo->flags)) {
1605                         noticeview_set_text(noticeview, _("You got a return receipt in this message : "
1606                                                           "it has been displayed by the recipient."));
1607                 } else {
1608                         noticeview_set_text(noticeview, _("You asked for a return receipt in this message."));
1609                 }
1610                 noticeview_set_button_text(noticeview, NULL);
1611                 noticeview_set_button_press_callback(noticeview, NULL, NULL);
1612         } else {
1613                 noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
1614                 noticeview_set_text(noticeview, _("This message asks for a return receipt."));
1615                 noticeview_set_button_text(noticeview, _("Send receipt"));
1616                 noticeview_set_button_press_callback(noticeview,
1617                                                      G_CALLBACK(return_receipt_send_clicked),
1618                                                      (gpointer) msginfo);
1619         }
1620         noticeview_show(noticeview);
1621 }
1622
1623 static void return_receipt_send_clicked(NoticeView *noticeview, MsgInfo *msginfo)
1624 {
1625         MsgInfo *tmpmsginfo;
1626         gchar *file;
1627
1628         file = procmsg_get_message_file_path(msginfo);
1629         if (!file) {
1630                 g_warning("can't get message file path.\n");
1631                 return;
1632         }
1633
1634         tmpmsginfo = procheader_parse_file(file, msginfo->flags, TRUE, TRUE);
1635         tmpmsginfo->folder = msginfo->folder;
1636         tmpmsginfo->msgnum = msginfo->msgnum;
1637
1638         if (disposition_notification_send(tmpmsginfo) >= 0) {
1639                 procmsg_msginfo_set_flags(msginfo, MSG_RETRCPT_SENT, 0);
1640                 noticeview_hide(noticeview);
1641         }               
1642
1643         procmsg_msginfo_free(tmpmsginfo);
1644         g_free(file);
1645 }
1646
1647 static void partial_recv_show(NoticeView *noticeview, MsgInfo *msginfo)
1648 {
1649         gchar *text = NULL;
1650         gchar *button1 = NULL;
1651         gchar *button2 = NULL;
1652         void  *button1_cb = NULL;
1653         void  *button2_cb = NULL;
1654
1655         if (!msginfo->extradata)
1656                 return;
1657         if (!partial_msg_in_uidl_list(msginfo)) {
1658                 text = g_strdup_printf(_("This message has been partially "
1659                                 "retrieved,\nand has been deleted from the "
1660                                 "server."));
1661         } else {
1662                 switch (msginfo->planned_download) {
1663                 case POP3_PARTIAL_DLOAD_UNKN:
1664                         text = g_strdup_printf(_("This message has been "
1665                                         "partially retrieved;\nit is %s."),
1666                                         to_human_readable(
1667                                                 (goffset)(msginfo->total_size)));
1668                         button1 = _("Mark for download");
1669                         button2 = _("Mark for deletion");
1670                         button1_cb = partial_recv_dload_clicked;
1671                         button2_cb = partial_recv_del_clicked;
1672                         break;
1673                 case POP3_PARTIAL_DLOAD_DLOAD:
1674                         text = g_strdup_printf(_("This message has been "
1675                                         "partially retrieved;\nit is %s and "
1676                                         "will be downloaded."),
1677                                         to_human_readable(
1678                                                 (goffset)(msginfo->total_size)));
1679                         button1 = _("Unmark");
1680                         button1_cb = partial_recv_unmark_clicked;
1681                         button2 = _("Mark for deletion");
1682                         button2_cb = partial_recv_del_clicked;
1683                         break;
1684                 case POP3_PARTIAL_DLOAD_DELE:
1685                         text = g_strdup_printf(_("This message has been "
1686                                         "partially retrieved;\nit is %s and "
1687                                         "will be deleted."),
1688                                         to_human_readable(
1689                                                 (goffset)(msginfo->total_size)));
1690                         button1 = _("Mark for download");
1691                         button1_cb = partial_recv_dload_clicked;
1692                         button2 = _("Unmark");
1693                         button2_cb = partial_recv_unmark_clicked;
1694                         break;
1695                 default:
1696                         return;
1697                 }
1698         }
1699         
1700         noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
1701         noticeview_set_text(noticeview, text);
1702         g_free(text);
1703         noticeview_set_button_text(noticeview, button1);
1704         noticeview_set_button_press_callback(noticeview,
1705                      G_CALLBACK(button1_cb), (gpointer) msginfo);
1706
1707         noticeview_set_2ndbutton_text(noticeview, button2);
1708         noticeview_set_2ndbutton_press_callback(noticeview,
1709                      G_CALLBACK(button2_cb), (gpointer) msginfo);
1710
1711         noticeview_show(noticeview);
1712 }
1713
1714 static void partial_recv_dload_clicked(NoticeView *noticeview, 
1715                                        MsgInfo *msginfo)
1716 {
1717         if (partial_mark_for_download(msginfo) == 0) {
1718                 partial_recv_show(noticeview, msginfo);
1719         }
1720 }
1721
1722 static void partial_recv_del_clicked(NoticeView *noticeview, 
1723                                        MsgInfo *msginfo)
1724 {
1725         if (partial_mark_for_delete(msginfo) == 0) {
1726                 partial_recv_show(noticeview, msginfo);
1727         }
1728 }
1729
1730 static void partial_recv_unmark_clicked(NoticeView *noticeview, 
1731                                        MsgInfo *msginfo)
1732 {
1733         if (partial_unmark(msginfo) == 0) {
1734                 partial_recv_show(noticeview, msginfo);
1735         }
1736 }
1737
1738 static void select_account_cb(GtkWidget *w, gpointer data)
1739 {
1740         *(gint*)data = combobox_get_active_data(GTK_COMBO_BOX(w));
1741 }
1742
1743 static PrefsAccount *select_account_from_list(GList *ac_list)
1744 {
1745         GtkWidget *optmenu;
1746         gint account_id;
1747
1748         g_return_val_if_fail(ac_list != NULL, NULL);
1749         g_return_val_if_fail(ac_list->data != NULL, NULL);
1750         
1751         optmenu = gtkut_account_menu_new(ac_list,
1752                         G_CALLBACK(select_account_cb),
1753                         &account_id);
1754         if (!optmenu)
1755                 return NULL;
1756         account_id = ((PrefsAccount *) ac_list->data)->account_id;
1757         if (alertpanel_with_widget(
1758                                 _("Return Receipt Notification"),
1759                                 _("The message was sent to several of your "
1760                                   "accounts.\n"
1761                                   "Please choose which account do you want to "
1762                                   "use for sending the receipt notification:"),
1763                                 _("_Cancel"), _("_Send Notification"), NULL,
1764                                 FALSE, G_ALERTDEFAULT, optmenu) != G_ALERTALTERNATE)
1765                 return NULL;
1766         return account_find_from_id(account_id);
1767 }
1768
1769 /* 
1770  * \brief return selected messageview text, when nothing is 
1771  *        selected and message was filtered, return complete text
1772  *
1773  * \param  pointer to Messageview 
1774  *
1775  * \return pointer to text (needs to be free'd by calling func)
1776  */
1777 gchar *messageview_get_selection(MessageView *msgview)
1778 {
1779         TextView *textview;
1780         gchar *text = NULL;
1781         GtkTextView *edit = NULL;
1782         GtkTextBuffer *textbuf;
1783         gint body_pos = 0;
1784         
1785         g_return_val_if_fail(msgview != NULL, NULL);
1786
1787         if (msgview->mimeview->type == MIMEVIEW_VIEWER) {
1788                 MimeViewer *viewer = msgview->mimeview->mimeviewer;
1789                 if (viewer && viewer->get_selection) {
1790                         text = viewer->get_selection(viewer);
1791                         if (text)
1792                                 return text;
1793                 }
1794         }
1795
1796         textview = messageview_get_current_textview(msgview);
1797         g_return_val_if_fail(textview != NULL, NULL);
1798
1799         edit = GTK_TEXT_VIEW(textview->text);
1800         g_return_val_if_fail(edit != NULL, NULL);
1801         body_pos = textview->body_pos;
1802
1803         textbuf = gtk_text_view_get_buffer(edit);
1804
1805         if (gtk_text_buffer_get_selection_bounds(textbuf, NULL, NULL))
1806                 return gtkut_text_view_get_selection(edit);
1807         else if (msgview->filtered) {
1808                 GtkTextIter start_iter, end_iter;
1809                 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter, body_pos);
1810                 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
1811                 gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter, FALSE);
1812         } else
1813                 text = NULL;
1814
1815         return text;
1816 }
1817
1818 static void save_as_cb(gpointer data, guint action, GtkWidget *widget)
1819 {
1820         MessageView *messageview = (MessageView *)data;
1821         messageview_save_as(messageview);
1822 }
1823
1824 #if defined(USE_GNOMEPRINT) || GTK_CHECK_VERSION(2,10,0)
1825 static void print_mimeview(MimeView *mimeview, gint sel_start, gint sel_end, gint partnum) 
1826 {
1827 #if !defined(USE_GNOMEPRINT) && GTK_CHECK_VERSION(2,10,0)
1828         MainWindow *mainwin;
1829 #endif
1830         if (!mimeview 
1831         ||  !mimeview->textview
1832         ||  !mimeview->textview->text)
1833                 alertpanel_warning(_("Cannot print: the message doesn't "
1834                                      "contain text."));
1835         else {
1836                 gtk_widget_realize(mimeview->textview->text);
1837                 if (partnum > 0) {
1838                         mimeview_select_part_num(mimeview, partnum);
1839                 }
1840                 if (mimeview->type == MIMEVIEW_VIEWER) {
1841                         MimeViewer *viewer = mimeview->mimeviewer;
1842                         if (viewer && viewer->print) {
1843                                 viewer->print(viewer);
1844                                 return;
1845                         }
1846                 }
1847                 if (sel_start != -1 && sel_end != -1) {
1848                         GtkTextIter start, end;
1849                         GtkTextView *text = GTK_TEXT_VIEW(mimeview->textview->text);
1850                         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
1851
1852                         gtk_text_buffer_get_iter_at_offset(buffer, &start, sel_start);
1853                         gtk_text_buffer_get_iter_at_offset(buffer, &end, sel_end);
1854                         gtk_text_buffer_select_range(buffer, &start, &end);
1855                 }
1856 #if defined(USE_GNOMEPRINT)
1857                 gedit_print(GTK_TEXT_VIEW(mimeview->textview->text));
1858 #else
1859                 /* TODO: Get the real parent window, not the main window */
1860                 mainwin = mainwindow_get_mainwindow();
1861                 printing_print(GTK_TEXT_VIEW(mimeview->textview->text),
1862                                mainwin ? GTK_WINDOW(mainwin->window) : NULL,
1863                                 sel_start, sel_end);
1864 #endif
1865         }
1866 }
1867
1868 void messageview_print(MsgInfo *msginfo, gboolean all_headers, 
1869                         gint sel_start, gint sel_end, gint partnum) 
1870 {
1871         PangoFontDescription *font_desc = NULL;
1872         MessageView *tmpview = messageview_create_with_new_window_visible(
1873                                 mainwindow_get_mainwindow(), FALSE);
1874
1875         if (prefs_common.use_different_print_font) {
1876                 font_desc = pango_font_description_from_string
1877                                                 (prefs_common.printfont);
1878         } else {
1879                 font_desc = pango_font_description_from_string
1880                                                 (prefs_common.textfont);
1881         }
1882         if (font_desc) {
1883                 gtk_widget_modify_font(tmpview->mimeview->textview->text, 
1884                         font_desc);
1885                 pango_font_description_free(font_desc);
1886         }
1887
1888         tmpview->all_headers = all_headers;
1889         if (msginfo && messageview_show(tmpview, msginfo, 
1890                 tmpview->all_headers) >= 0) {
1891                         print_mimeview(tmpview->mimeview, 
1892                                 sel_start, sel_end, partnum);
1893         }
1894         messageview_destroy(tmpview);
1895 }
1896 #endif
1897
1898 #if GTK_CHECK_VERSION(2,10,0) && !defined(USE_GNOMEPRINT)
1899 static void page_setup_cb(gpointer data, guint action, GtkWidget *widget)
1900 {
1901         MessageView *messageview = (MessageView *)data;
1902         printing_page_setup(messageview ?
1903                             GTK_WINDOW(messageview->window) : NULL);
1904 }
1905 #endif
1906
1907 static void print_cb(gpointer data, guint action, GtkWidget *widget)
1908 {
1909         MessageView *messageview = (MessageView *)data;
1910 #if !defined(USE_GNOMEPRINT) && !GTK_CHECK_VERSION(2,10,0)
1911         gchar *cmdline = NULL;
1912         gchar *p;
1913 #else
1914         gint sel_start = -1, sel_end = -1, partnum = 0;
1915 #endif
1916
1917         if (!messageview->msginfo) return;
1918
1919 #if !defined(USE_GNOMEPRINT) && !GTK_CHECK_VERSION(2,10,0)
1920         cmdline = input_dialog(_("Print"),
1921                                _("Enter the print command line:\n"
1922                                  "('%s' will be replaced with file name)"),
1923                                prefs_common.print_cmd);
1924         if (!cmdline) return;
1925         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
1926             strchr(p + 2, '%')) {
1927                 alertpanel_error(_("Print command line is invalid:\n'%s'"),
1928                                  cmdline);
1929                 g_free(cmdline);
1930                 return;
1931         }
1932         procmsg_print_message(messageview->msginfo, cmdline);
1933         g_free(cmdline);
1934 #else
1935         partnum = mimeview_get_selected_part_num(messageview->mimeview);
1936         textview_get_selection_offsets(messageview->mimeview->textview,
1937                 &sel_start, &sel_end);
1938         messageview_print(messageview->msginfo, messageview->all_headers, 
1939                 sel_start, sel_end, partnum);
1940 #endif
1941 }
1942
1943 static void close_cb(gpointer data, guint action, GtkWidget *widget)
1944 {
1945         MessageView *messageview = (MessageView *)data;
1946         messageview_destroy(messageview);
1947 }
1948
1949 static void copy_cb(gpointer data, guint action, GtkWidget *widget)
1950 {
1951         MessageView *messageview = (MessageView *)data;
1952         messageview_copy_clipboard(messageview);
1953 }
1954
1955 static void allsel_cb(gpointer data, guint action, GtkWidget *widget)
1956 {
1957         MessageView *messageview = (MessageView *)data;
1958         messageview_select_all(messageview);
1959 }
1960
1961 static void search_cb(gpointer data, guint action, GtkWidget *widget)
1962 {
1963         MessageView *messageview = (MessageView *)data;
1964         message_search(messageview);
1965 }
1966
1967 static void prev_cb(gpointer data, guint action, GtkWidget *widget)
1968 {
1969         MessageView *messageview = (MessageView *)data;
1970         messageview->updating = TRUE;
1971         summary_step(messageview->mainwin->summaryview, GTK_SCROLL_STEP_BACKWARD);
1972         messageview->updating = FALSE;
1973
1974         if (messageview->deferred_destroy) {
1975                 debug_print("messageview got away!\n");
1976                 messageview_destroy(messageview);
1977                 return;
1978         }
1979         if (messageview->mainwin->summaryview->selected) {
1980 #ifndef GENERIC_UMPC
1981                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
1982                        
1983                 if (msginfo)
1984                         messageview_show(messageview, msginfo, 
1985                                          messageview->all_headers);
1986 #endif
1987         } else {
1988                 gtk_widget_destroy(messageview->window);
1989         }
1990 }
1991
1992 static void next_cb(gpointer data, guint action, GtkWidget *widget)
1993 {
1994         MessageView *messageview = (MessageView *)data;
1995         messageview->updating = TRUE;
1996         summary_step(messageview->mainwin->summaryview, GTK_SCROLL_STEP_FORWARD);
1997         messageview->updating = FALSE;
1998
1999         if (messageview->deferred_destroy) {
2000                 debug_print("messageview got away!\n");
2001                 messageview_destroy(messageview);
2002                 return;
2003         }
2004         if (messageview->mainwin->summaryview->selected) {
2005 #ifndef GENERIC_UMPC
2006                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2007                        
2008                 if (msginfo)
2009                         messageview_show(messageview, msginfo, 
2010                                          messageview->all_headers);
2011 #endif
2012         } else {
2013                 gtk_widget_destroy(messageview->window);
2014         }
2015 }
2016
2017 static void prev_unread_cb(gpointer data, guint action, GtkWidget *widget)
2018 {
2019         MessageView *messageview = (MessageView *)data;
2020         messageview->updating = TRUE;
2021         summary_select_prev_unread(messageview->mainwin->summaryview);
2022         messageview->updating = FALSE;
2023
2024         if (messageview->deferred_destroy) {
2025                 debug_print("messageview got away!\n");
2026                 messageview_destroy(messageview);
2027                 return;
2028         }
2029         if (messageview->mainwin->summaryview->selected) {
2030 #ifndef GENERIC_UMPC
2031                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2032                        
2033                 if (msginfo)
2034                         messageview_show(messageview, msginfo, 
2035                                          messageview->all_headers);
2036 #endif
2037         } else {
2038                 gtk_widget_destroy(messageview->window);
2039         }
2040 }
2041
2042 static void next_unread_cb(gpointer data, guint action, GtkWidget *widget)
2043 {
2044         MessageView *messageview = (MessageView *)data;
2045         messageview->updating = TRUE;
2046         summary_select_next_unread(messageview->mainwin->summaryview);
2047         messageview->updating = FALSE;
2048
2049         if (messageview->deferred_destroy) {
2050                 debug_print("messageview got away!\n");
2051                 messageview_destroy(messageview);
2052                 return;
2053         }
2054         if (messageview->mainwin->summaryview->selected) {
2055 #ifndef GENERIC_UMPC
2056                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2057                        
2058                 if (msginfo)
2059                         messageview_show(messageview, msginfo, 
2060                                          messageview->all_headers);
2061 #endif
2062         } else {
2063                 gtk_widget_destroy(messageview->window);
2064         }
2065 }
2066
2067 static void prev_new_cb(gpointer data, guint action, GtkWidget *widget)
2068 {
2069         MessageView *messageview = (MessageView *)data;
2070         messageview->updating = TRUE;
2071         summary_select_prev_new(messageview->mainwin->summaryview);
2072         messageview->updating = FALSE;
2073
2074         if (messageview->deferred_destroy) {
2075                 debug_print("messageview got away!\n");
2076                 messageview_destroy(messageview);
2077                 return;
2078         }
2079         if (messageview->mainwin->summaryview->selected) {
2080 #ifndef GENERIC_UMPC
2081                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2082                        
2083                 if (msginfo)
2084                         messageview_show(messageview, msginfo, 
2085                                          messageview->all_headers);
2086 #endif
2087         } else {
2088                 gtk_widget_destroy(messageview->window);
2089         }
2090 }
2091
2092 static void next_new_cb(gpointer data, guint action, GtkWidget *widget)
2093 {
2094         MessageView *messageview = (MessageView *)data;
2095         messageview->updating = TRUE;
2096         summary_select_next_new(messageview->mainwin->summaryview);
2097         messageview->updating = FALSE;
2098
2099         if (messageview->deferred_destroy) {
2100                 debug_print("messageview got away!\n");
2101                 messageview_destroy(messageview);
2102                 return;
2103         }
2104         if (messageview->mainwin->summaryview->selected) {
2105 #ifndef GENERIC_UMPC
2106                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2107                        
2108                 if (msginfo)
2109                         messageview_show(messageview, msginfo, 
2110                                          messageview->all_headers);
2111 #endif
2112         } else {
2113                 gtk_widget_destroy(messageview->window);
2114         }
2115 }
2116
2117 static void prev_marked_cb(gpointer data, guint action, GtkWidget *widget)
2118 {
2119         MessageView *messageview = (MessageView *)data;
2120         messageview->updating = TRUE;
2121         summary_select_prev_marked(messageview->mainwin->summaryview);
2122         messageview->updating = FALSE;
2123
2124         if (messageview->deferred_destroy) {
2125                 debug_print("messageview got away!\n");
2126                 messageview_destroy(messageview);
2127                 return;
2128         }
2129         if (messageview->mainwin->summaryview->selected) {
2130 #ifndef GENERIC_UMPC
2131                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2132                        
2133                 if (msginfo)
2134                         messageview_show(messageview, msginfo, 
2135                                          messageview->all_headers);
2136 #endif
2137         } else {
2138                 gtk_widget_destroy(messageview->window);
2139         }
2140 }
2141
2142 static void next_marked_cb(gpointer data, guint action, GtkWidget *widget)
2143 {
2144         MessageView *messageview = (MessageView *)data;
2145         messageview->updating = TRUE;
2146         summary_select_next_marked(messageview->mainwin->summaryview);
2147         messageview->updating = FALSE;
2148
2149         if (messageview->deferred_destroy) {
2150                 debug_print("messageview got away!\n");
2151                 messageview_destroy(messageview);
2152                 return;
2153         }
2154         if (messageview->mainwin->summaryview->selected) {
2155 #ifndef GENERIC_UMPC
2156                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2157                        
2158                 if (msginfo)
2159                         messageview_show(messageview, msginfo, 
2160                                          messageview->all_headers);
2161 #endif
2162         } else {
2163                 gtk_widget_destroy(messageview->window);
2164         }
2165 }
2166
2167 static void prev_labeled_cb(gpointer data, guint action, GtkWidget *widget)
2168 {
2169         MessageView *messageview = (MessageView *)data;
2170         messageview->updating = TRUE;
2171         summary_select_prev_labeled(messageview->mainwin->summaryview);
2172         messageview->updating = FALSE;
2173
2174         if (messageview->deferred_destroy) {
2175                 debug_print("messageview got away!\n");
2176                 messageview_destroy(messageview);
2177                 return;
2178         }
2179         if (messageview->mainwin->summaryview->selected) {
2180 #ifndef GENERIC_UMPC
2181                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2182                        
2183                 if (msginfo)
2184                         messageview_show(messageview, msginfo, 
2185                                          messageview->all_headers);
2186 #endif
2187         } else {
2188                 gtk_widget_destroy(messageview->window);
2189         }
2190 }
2191
2192 static void next_labeled_cb(gpointer data, guint action, GtkWidget *widget)
2193 {
2194         MessageView *messageview = (MessageView *)data;
2195         messageview->updating = TRUE;
2196         summary_select_next_labeled(messageview->mainwin->summaryview);
2197         messageview->updating = FALSE;
2198
2199         if (messageview->deferred_destroy) {
2200                 debug_print("messageview got away!\n");
2201                 messageview_destroy(messageview);
2202                 return;
2203         }
2204         if (messageview->mainwin->summaryview->selected) {
2205 #ifndef GENERIC_UMPC
2206                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2207                        
2208                 if (msginfo)
2209                         messageview_show(messageview, msginfo, 
2210                                          messageview->all_headers);
2211 #endif
2212         } else {
2213                 gtk_widget_destroy(messageview->window);
2214         }
2215 }
2216
2217 static void last_read_cb(gpointer data, guint action, GtkWidget *widget)
2218 {
2219         MessageView *messageview = (MessageView *)data;
2220         messageview->updating = TRUE;
2221         summary_select_last_read(messageview->mainwin->summaryview);
2222         messageview->updating = FALSE;
2223
2224         if (messageview->deferred_destroy) {
2225                 debug_print("messageview got away!\n");
2226                 messageview_destroy(messageview);
2227                 return;
2228         }
2229         if (messageview->mainwin->summaryview->selected) {
2230 #ifndef GENERIC_UMPC
2231                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2232                        
2233                 if (msginfo)
2234                         messageview_show(messageview, msginfo, 
2235                                          messageview->all_headers);
2236 #endif
2237         } else {
2238                 gtk_widget_destroy(messageview->window);
2239         }
2240 }
2241
2242 static void parent_cb(gpointer data, guint action, GtkWidget *widget)
2243 {
2244         MessageView *messageview = (MessageView *)data;
2245         messageview->updating = TRUE;
2246         summary_select_parent(messageview->mainwin->summaryview);
2247         messageview->updating = FALSE;
2248
2249         if (messageview->deferred_destroy) {
2250                 debug_print("messageview got away!\n");
2251                 messageview_destroy(messageview);
2252                 return;
2253         }
2254         if (messageview->mainwin->summaryview->selected) {
2255 #ifndef GENERIC_UMPC
2256                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2257                        
2258                 if (msginfo)
2259                         messageview_show(messageview, msginfo, 
2260                                          messageview->all_headers);
2261 #endif
2262         } else {
2263                 gtk_widget_destroy(messageview->window);
2264         }
2265 }
2266
2267 static void goto_unread_folder_cb(gpointer data, guint action, GtkWidget *widget)
2268 {
2269         MessageView *messageview = (MessageView *)data;
2270         messageview->updating = TRUE;
2271         folderview_select_next_unread(messageview->mainwin->folderview, FALSE);
2272         messageview->updating = FALSE;
2273
2274         if (messageview->deferred_destroy) {
2275                 debug_print("messageview got away!\n");
2276                 messageview_destroy(messageview);
2277                 return;
2278         }
2279         if (messageview->mainwin->summaryview->selected) {
2280 #ifndef GENERIC_UMPC
2281                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2282                        
2283                 if (msginfo)
2284                         messageview_show(messageview, msginfo, 
2285                                          messageview->all_headers);
2286 #endif
2287         } else {
2288                 gtk_widget_destroy(messageview->window);
2289         }
2290 }
2291
2292 static void goto_folder_cb(gpointer data, guint action, GtkWidget *widget)
2293 {
2294         MessageView *messageview = (MessageView *)data;
2295         messageview->updating = TRUE;
2296         FolderItem *to_folder;
2297         messageview->updating = FALSE;
2298
2299         to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL, FALSE);
2300
2301         if (to_folder) {
2302                 folderview_select(messageview->mainwin->folderview, to_folder);
2303
2304                 if (messageview->deferred_destroy) {
2305                         debug_print("messageview got away!\n");
2306                         messageview_destroy(messageview);
2307                         return;
2308                 }
2309                 if (messageview->mainwin->summaryview->selected) {
2310 #ifndef GENERIC_UMPC
2311                         MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2312                        
2313                         if (msginfo)
2314                                 messageview_show(messageview, msginfo, 
2315                                                  messageview->all_headers);
2316 #endif
2317                 } else {
2318                         gtk_widget_destroy(messageview->window);
2319                 }
2320         }
2321 }
2322
2323 static void set_charset_cb(gpointer data, guint action, GtkWidget *widget)
2324 {
2325         MessageView *messageview = (MessageView *)data;
2326         const gchar *charset;
2327
2328         if (GTK_CHECK_MENU_ITEM(widget)->active) {
2329                 charset = conv_get_charset_str((CharSet)action);
2330                 g_free(messageview->forced_charset);
2331                 messageview->forced_charset = g_strdup(charset);
2332                 procmime_force_charset(charset);
2333                 
2334                 messageview_show(messageview, messageview->msginfo, FALSE);
2335         }
2336 }
2337
2338 static void set_decode_cb(gpointer data, guint action, GtkWidget *widget)
2339 {
2340         MessageView *messageview = (MessageView *)data;
2341         if (GTK_CHECK_MENU_ITEM(widget)->active) {
2342                 messageview->forced_encoding = (EncodingType)action;
2343
2344                 messageview_show(messageview, messageview->msginfo, FALSE);
2345                 
2346                 debug_print("forced encoding: %d\n", action);
2347         }
2348 }
2349
2350
2351 static void view_source_cb(gpointer data, guint action, GtkWidget *widget)
2352 {
2353         MessageView *messageview = (MessageView *)data;
2354         SourceWindow *srcwin;
2355
2356         if (!messageview->msginfo) return;
2357
2358         srcwin = source_window_create();
2359         source_window_show_msg(srcwin, messageview->msginfo);
2360         source_window_show(srcwin);
2361 }
2362
2363 static void show_all_header_cb(gpointer data, guint action, GtkWidget *widget)
2364 {
2365         MessageView *messageview = (MessageView *)data;
2366         MsgInfo *msginfo = messageview->msginfo;
2367
2368         if (messageview->mimeview->textview &&
2369             messageview->mimeview->textview->loading) {
2370                 return;
2371         }
2372         if (messageview->updating)
2373                 return;
2374
2375         messageview->all_headers = 
2376                         GTK_CHECK_MENU_ITEM(widget)->active;
2377         if (!msginfo) return;
2378         messageview->msginfo = NULL;
2379         messageview_show(messageview, msginfo,
2380                          GTK_CHECK_MENU_ITEM(widget)->active);
2381         procmsg_msginfo_free(msginfo);
2382         main_window_set_menu_sensitive(messageview->mainwin);
2383 }
2384
2385 #define SET_CHECK_MENU_ACTIVE(path, active) \
2386 { \
2387         menuitem = gtk_item_factory_get_widget(ifactory, path); \
2388         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), active); \
2389 }
2390
2391 static void msg_hide_quotes_cb(gpointer data, guint action, GtkWidget *widget)
2392 {
2393         MessageView *messageview = (MessageView *)data;
2394         MsgInfo *msginfo = messageview->msginfo;
2395         static gboolean updating_menu = FALSE;
2396         GtkItemFactory *ifactory = gtk_item_factory_from_widget(messageview->menubar);
2397         GtkWidget *menuitem;
2398         if (updating_menu)
2399                 return;
2400
2401         prefs_common.hide_quotes = 
2402                         GTK_CHECK_MENU_ITEM(widget)->active ? action : 0;
2403         
2404         updating_menu=TRUE;
2405         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold all", FALSE);
2406         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 2", FALSE);
2407         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 3", FALSE);
2408         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), prefs_common.hide_quotes > 0);      
2409         updating_menu=FALSE;
2410         if (!msginfo) return;
2411         messageview->msginfo = NULL;
2412         messageview_show(messageview, msginfo,
2413                          messageview->all_headers);
2414         procmsg_msginfo_free(msginfo);
2415         
2416         /* update main window */
2417         main_window_set_menu_sensitive(messageview->mainwin);
2418         summary_redisplay_msg(messageview->mainwin->summaryview);
2419 }
2420 #undef SET_CHECK_MENU_ACTIVE
2421
2422 static void compose_cb(gpointer data, guint action, GtkWidget *widget)
2423 {
2424         MessageView *messageview = (MessageView *)data;
2425         PrefsAccount *ac = NULL;
2426         FolderItem *item = NULL;
2427
2428         if (messageview->msginfo)
2429                 item = messageview->msginfo->folder;
2430
2431         if (item) {
2432                 ac = account_find_from_item(item);
2433                 if (ac && ac->protocol == A_NNTP &&
2434                     item->stype == F_NEWS) {
2435                         compose_new(ac, item->path, NULL);
2436                         return;
2437                 }
2438         }
2439
2440         compose_new(ac, NULL, NULL);
2441 }
2442
2443 static void reply_cb(gpointer data, guint action, GtkWidget *widget)
2444 {
2445         MessageView *messageview = (MessageView *)data;
2446         GSList *msginfo_list = NULL;
2447
2448         g_return_if_fail(messageview->msginfo);
2449
2450         msginfo_list = g_slist_append(msginfo_list, messageview->msginfo);
2451         compose_reply_from_messageview(messageview, msginfo_list, action);
2452         g_slist_free(msginfo_list);
2453 }
2454
2455 static void addressbook_open_cb(gpointer data, guint action, GtkWidget *widget)
2456 {
2457         addressbook_open(NULL);
2458 }
2459
2460 static void add_address_cb(gpointer data, guint action, GtkWidget *widget)
2461 {
2462         MessageView *messageview = (MessageView *)data;
2463         MsgInfo *msginfo, *full_msginfo;
2464         gchar *from;
2465         GtkWidget *image = NULL;
2466         GdkPixbuf *picture = NULL;
2467
2468         if (!messageview->msginfo || !messageview->msginfo->from) 
2469                 return;
2470
2471         msginfo = messageview->msginfo;
2472         Xstrdup_a(from, msginfo->from, return);
2473         eliminate_address_comment(from);
2474         extract_address(from);
2475         
2476         full_msginfo = procmsg_msginfo_get_full_info(msginfo);
2477         if (full_msginfo &&
2478             full_msginfo->extradata &&
2479             full_msginfo->extradata->face) {
2480                 image = face_get_from_header(full_msginfo->extradata->face);
2481         } 
2482 #if HAVE_LIBCOMPFACE
2483         else if (full_msginfo &&
2484                  full_msginfo->extradata &&
2485                  full_msginfo->extradata->xface) {
2486                 image = xface_get_from_header(full_msginfo->extradata->xface,
2487                                 &messageview->mainwin->summaryview->ctree->style->white,
2488                                 messageview->window->window);   
2489         }
2490 #endif
2491         procmsg_msginfo_free(full_msginfo);
2492         if (image)
2493                 picture = gtk_image_get_pixbuf(GTK_IMAGE(image));
2494
2495         addressbook_add_contact(msginfo->fromname, from, NULL, picture);
2496
2497         if (image)
2498                 gtk_widget_destroy(image);
2499 }
2500
2501 static void create_filter_cb(gpointer data, guint action, GtkWidget *widget)
2502 {
2503         MessageView *messageview = (MessageView *)data;
2504         FolderItem * item;
2505         
2506         if (!messageview->msginfo) return;
2507         
2508         item = messageview->msginfo->folder;
2509         summary_msginfo_filter_open(item,  messageview->msginfo,
2510                                     (PrefsFilterType)action, 0);
2511 }
2512
2513 static void create_processing_cb(gpointer data, guint action,
2514                                  GtkWidget *widget)
2515 {
2516         MessageView *messageview = (MessageView *)data;
2517         FolderItem * item;
2518         
2519         if (!messageview->msginfo) return;
2520         
2521         item = messageview->msginfo->folder;
2522         summary_msginfo_filter_open(item,  messageview->msginfo,
2523                                     (PrefsFilterType)action, 1);
2524 }
2525
2526 static void open_urls_cb(gpointer data, guint action, GtkWidget *widget)
2527 {
2528         MessageView *messageview = (MessageView *)data;
2529         messageview_list_urls(messageview);
2530 }
2531
2532 static void about_cb(gpointer data, guint action, GtkWidget *widget)
2533 {
2534         about_show();
2535 }
2536
2537 static gboolean messageview_update_msg(gpointer source, gpointer data)
2538 {
2539         MsgInfoUpdate *msginfo_update = (MsgInfoUpdate *) source;
2540         MessageView *messageview = (MessageView *)data;
2541
2542         if (messageview->msginfo != msginfo_update->msginfo)
2543                 return FALSE;
2544
2545         if (msginfo_update->flags & MSGINFO_UPDATE_DELETED) {
2546                 MsgInfo *old_msginfo = messageview->msginfo;
2547                 messageview_clear(messageview);
2548                 messageview_update(messageview, old_msginfo);
2549         }
2550
2551         return FALSE;
2552 }
2553
2554 void messageview_set_menu_sensitive(MessageView *messageview)
2555 {
2556         GtkItemFactory *ifactory;
2557         GtkWidget *menuitem = NULL;
2558
2559         if (!messageview || !messageview->new_window) 
2560                 return;
2561         /* do some smart things */
2562         if (!messageview->menubar) return;
2563         ifactory = gtk_item_factory_from_widget(messageview->menubar);
2564         if (!ifactory) return;
2565
2566         if (prefs_common.hide_quotes) {
2567                 menuitem = NULL;
2568                 if (prefs_common.hide_quotes == 1)
2569                         menuitem = gtk_item_factory_get_widget(ifactory, 
2570                                         "/View/Quotes/Fold all");
2571                 if (prefs_common.hide_quotes == 2)
2572                         menuitem = gtk_item_factory_get_widget(ifactory, 
2573                                         "/View/Quotes/Fold from level 2");
2574                 if (prefs_common.hide_quotes == 3)
2575                         menuitem = gtk_item_factory_get_widget(ifactory, 
2576                                         "/View/Quotes/Fold from level 3");
2577                 gtk_check_menu_item_set_active
2578                         (GTK_CHECK_MENU_ITEM(menuitem),
2579                          TRUE);
2580         }
2581 }
2582
2583 void messageview_learn (MessageView *msgview, gboolean is_spam)
2584 {
2585         if (is_spam) {
2586                 if (procmsg_spam_learner_learn(msgview->msginfo, NULL, TRUE) == 0)
2587                         procmsg_msginfo_set_flags(msgview->msginfo, MSG_SPAM, 0);
2588                 else
2589                         log_error(LOG_PROTOCOL, _("An error happened while learning.\n"));
2590                 
2591         } else {
2592                 if (procmsg_spam_learner_learn(msgview->msginfo, NULL, FALSE) == 0)
2593                         procmsg_msginfo_unset_flags(msgview->msginfo, MSG_SPAM, 0);
2594                 else
2595                         log_error(LOG_PROTOCOL, _("An error happened while learning.\n"));
2596         }
2597         if (msgview->toolbar)
2598                 toolbar_set_learn_button
2599                         (msgview->toolbar,
2600                          MSG_IS_SPAM(msgview->msginfo->flags)?LEARN_HAM:LEARN_SPAM);
2601         else
2602                 toolbar_set_learn_button
2603                         (msgview->mainwin->toolbar,
2604                          MSG_IS_SPAM(msgview->msginfo->flags)?LEARN_HAM:LEARN_SPAM);
2605 }
2606
2607 void messageview_list_urls (MessageView *msgview)
2608 {
2609         GSList *cur = msgview->mimeview->textview->uri_list;
2610         GSList *newlist = NULL;
2611         GHashTable *uri_hashtable = g_hash_table_new(g_str_hash, g_str_equal); 
2612         for (; cur; cur = cur->next) {
2613                 ClickableText *uri = (ClickableText *)cur->data;
2614                 if (uri->uri &&
2615                     (!g_ascii_strncasecmp(uri->uri, "ftp.", 4) ||
2616                      !g_ascii_strncasecmp(uri->uri, "ftp:", 4) ||
2617                      !g_ascii_strncasecmp(uri->uri, "www.", 4) ||
2618                      !g_ascii_strncasecmp(uri->uri, "http:", 5) ||
2619                      !g_ascii_strncasecmp(uri->uri, "https:", 6)))
2620                 {
2621                         if(g_hash_table_lookup(uri_hashtable, uri->uri))
2622                                 continue;
2623                         
2624                         newlist = g_slist_prepend(newlist, uri);
2625                         g_hash_table_insert(uri_hashtable, uri->uri,
2626                                             GUINT_TO_POINTER(g_str_hash(uri->uri)));
2627                 }
2628         }
2629         newlist = g_slist_reverse(newlist);
2630         uri_opener_open(msgview, newlist);
2631         g_slist_free(newlist);
2632         g_hash_table_destroy(uri_hashtable);
2633 }