* src/messageview.[ch]
[claws.git] / src / messageview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto
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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <gdk/gdkkeysyms.h>
24 #include <gtk/gtkvbox.h>
25 #include <gtk/gtkcontainer.h>
26 #include <gtk/gtkeditable.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 "intl.h"
36 #include "main.h"
37 #include "messageview.h"
38 #include "message_search.h"
39 #include "headerview.h"
40 #include "summaryview.h"
41 #include "textview.h"
42 #include "mimeview.h"
43 #include "menu.h"
44 #include "about.h"
45 #include "filesel.h"
46 #include "sourcewindow.h"
47 #include "addressbook.h"
48 #include "alertpanel.h"
49 #include "inputdialog.h"
50 #include "manage_window.h"
51 #include "procmsg.h"
52 #include "procheader.h"
53 #include "procmime.h"
54 #include "account.h"
55 #include "action.h"
56 #include "prefs_common.h"
57 #include "prefs_account.h"
58 #include "gtkutils.h"
59 #include "utils.h"
60 #include "rfc2015.h"
61 #include "send_message.h"
62 #include "stock_pixmap.h"
63 #include "hooks.h"
64 #include "filtering.h"
65
66 static GList *messageview_list = NULL;
67
68 static void messageview_destroy_cb      (GtkWidget      *widget,
69                                          MessageView    *messageview);
70 static void messageview_size_allocate_cb(GtkWidget      *widget,
71                                          GtkAllocation  *allocation);
72 static void key_pressed                 (GtkWidget      *widget,
73                                          GdkEventKey    *event,
74                                          MessageView    *messageview);
75
76 static void return_receipt_show         (NoticeView     *noticeview, 
77                                          MsgInfo        *msginfo);      
78 static void return_receipt_send_clicked (NoticeView     *noticeview, 
79                                          MsgInfo        *msginfo);
80 static void save_as_cb                  (gpointer        data,
81                                          guint           action,
82                                          GtkWidget      *widget);
83 static void print_cb                    (gpointer        data,
84                                          guint           action,
85                                          GtkWidget      *widget);
86 static void close_cb                    (gpointer        data,
87                                          guint           action,
88                                          GtkWidget      *widget);
89 static void copy_cb                     (gpointer        data,
90                                          guint           action,
91                                          GtkWidget      *widget);
92 static void allsel_cb                   (gpointer        data,
93                                          guint           action,
94                                          GtkWidget      *widget);
95 static void search_cb                   (gpointer        data,
96                                          guint           action,
97                                          GtkWidget      *widget);
98
99 static void set_charset_cb              (gpointer        data,
100                                          guint           action,
101                                          GtkWidget      *widget);
102 static void view_source_cb              (gpointer        data,
103                                          guint           action,
104                                          GtkWidget      *widget);
105 static void show_all_header_cb          (gpointer        data,
106                                          guint           action,
107                                          GtkWidget      *widget);
108
109 static void compose_cb                  (gpointer        data,
110                                          guint           action,
111                                          GtkWidget      *widget);
112 static void reply_cb                    (gpointer        data,
113                                          guint           action,
114                                          GtkWidget      *widget);
115 static void reedit_cb                   (gpointer        data,
116                                          guint           action,
117                                          GtkWidget      *widget);
118
119 static PrefsAccount *select_account_from_list
120                                         (GList          *ac_list);
121 static void addressbook_open_cb         (gpointer        data,
122                                          guint           action,
123                                          GtkWidget      *widget);
124 static void add_address_cb              (gpointer        data,
125                                          guint           action,
126                                          GtkWidget      *widget);
127 static void create_filter_cb            (gpointer        data,
128                                          guint           action,
129                                          GtkWidget      *widget);
130 static void create_processing_cb        (gpointer        data,
131                                          guint           action,
132                                          GtkWidget      *widget);
133
134 static void about_cb                    (gpointer        data,
135                                          guint           action,
136                                          GtkWidget      *widget);
137 static void messageview_update          (MessageView *msgview);
138 static gboolean messageview_update_msg  (gpointer source, gpointer data);
139
140 static GList *msgview_list = NULL;
141 static GtkItemFactoryEntry msgview_entries[] =
142 {
143         {N_("/_File"),                  NULL, NULL, 0, "<Branch>"},
144         {N_("/_File/_Save as..."),      NULL, save_as_cb, 0, NULL},
145         {N_("/_File/_Print..."),        NULL, print_cb, 0, NULL},
146         {N_("/_File/---"),              NULL, NULL, 0, "<Separator>"},
147         {N_("/_File/_Close"),           NULL, close_cb, 0, NULL},
148
149         {N_("/_Edit"),                  NULL, NULL, 0, "<Branch>"},
150         {N_("/_Edit/_Copy"),            NULL, copy_cb, 0, NULL},
151         {N_("/_Edit/Select _all"),      NULL, allsel_cb, 0, NULL},
152         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
153         {N_("/_Edit/_Find in current message..."),
154                                         NULL, search_cb, 0, NULL},
155
156         {N_("/_View"),                  NULL, NULL, 0, "<Branch>"},
157
158 #define CODESET_SEPARATOR \
159         {N_("/_View/_Code set/---"),    NULL, NULL, 0, "<Separator>"}
160 #define CODESET_ACTION(action) \
161         NULL, set_charset_cb, action, "/View/Code set/Auto detect"
162
163         {N_("/_View/_Code set"),        NULL, NULL, 0, "<Branch>"},
164         {N_("/_View/_Code set/_Auto detect"),
165                                         NULL, set_charset_cb, C_AUTO, "<RadioItem>"},
166         CODESET_SEPARATOR,
167         {N_("/_View/_Code set/7bit ascii (US-ASC_II)"),
168          CODESET_ACTION(C_US_ASCII)},
169
170 #if HAVE_ICONV
171         {N_("/_View/_Code set/Unicode (_UTF-8)"),
172          CODESET_ACTION(C_UTF_8)},
173         CODESET_SEPARATOR,
174 #endif
175         {N_("/_View/_Code set/Western European (ISO-8859-_1)"),
176          CODESET_ACTION(C_ISO_8859_1)},
177         {N_("/_View/_Code set/Western European (ISO-8859-15)"),
178          CODESET_ACTION(C_ISO_8859_15)},
179         CODESET_SEPARATOR,
180 #if HAVE_ICONV
181         {N_("/_View/_Code set/Central European (ISO-8859-_2)"),
182          CODESET_ACTION(C_ISO_8859_2)},
183         CODESET_SEPARATOR,
184         {N_("/_View/_Code set/_Baltic (ISO-8859-13)"),
185          CODESET_ACTION(C_ISO_8859_13)},
186         {N_("/_View/_Code set/Baltic (ISO-8859-_4)"),
187          CODESET_ACTION(C_ISO_8859_4)},
188         CODESET_SEPARATOR,
189         {N_("/_View/_Code set/Greek (ISO-8859-_7)"),
190          CODESET_ACTION(C_ISO_8859_7)},
191         CODESET_SEPARATOR,
192         {N_("/_View/_Code set/Turkish (ISO-8859-_9)"),
193          CODESET_ACTION(C_ISO_8859_9)},
194         CODESET_SEPARATOR,
195         {N_("/_View/_Code set/Cyrillic (ISO-8859-_5)"),
196          CODESET_ACTION(C_ISO_8859_5)},
197         {N_("/_View/_Code set/Cyrillic (KOI8-_R)"),
198          CODESET_ACTION(C_KOI8_R)},
199         {N_("/_View/_Code set/Cyrillic (KOI8-U)"),
200          CODESET_ACTION(C_KOI8_U)},
201         {N_("/_View/_Code set/Cyrillic (Windows-1251)"),
202          CODESET_ACTION(C_CP1251)},
203         CODESET_SEPARATOR,
204 #endif
205         {N_("/_View/_Code set/Japanese (ISO-2022-_JP)"),
206          CODESET_ACTION(C_ISO_2022_JP)},
207 #if HAVE_ICONV
208         {N_("/_View/_Code set/Japanese (ISO-2022-JP-2)"),
209          CODESET_ACTION(C_ISO_2022_JP_2)},
210 #endif
211         {N_("/_View/_Code set/Japanese (_EUC-JP)"),
212          CODESET_ACTION(C_EUC_JP)},
213         {N_("/_View/_Code set/Japanese (_Shift__JIS)"),
214          CODESET_ACTION(C_SHIFT_JIS)},
215 #if HAVE_ICONV
216         CODESET_SEPARATOR,
217         {N_("/_View/_Code set/Simplified Chinese (_GB2312)"),
218          CODESET_ACTION(C_GB2312)},
219         {N_("/_View/_Code set/Traditional Chinese (_Big5)"),
220          CODESET_ACTION(C_BIG5)},
221         {N_("/_View/_Code set/Traditional Chinese (EUC-_TW)"),
222          CODESET_ACTION(C_EUC_TW)},
223         {N_("/_View/_Code set/Chinese (ISO-2022-_CN)"),
224          CODESET_ACTION(C_ISO_2022_CN)},
225         CODESET_SEPARATOR,
226         {N_("/_View/_Code set/Korean (EUC-_KR)"),
227          CODESET_ACTION(C_EUC_KR)},
228         {N_("/_View/_Code set/Korean (ISO-2022-KR)"),
229          CODESET_ACTION(C_ISO_2022_KR)},
230         CODESET_SEPARATOR,
231         {N_("/_View/_Code set/Thai (TIS-620)"),
232          CODESET_ACTION(C_TIS_620)},
233         {N_("/_View/_Code set/Thai (Windows-874)"),
234          CODESET_ACTION(C_WINDOWS_874)},
235 #endif
236
237 #undef CODESET_SEPARATOR
238 #undef CODESET_ACTION
239
240         {N_("/_View/---"),              NULL, NULL, 0, "<Separator>"},
241         {N_("/_View/Mess_age source"),  NULL, view_source_cb, 0, NULL},
242         {N_("/_View/Show all _headers"),NULL, show_all_header_cb, 0, "<ToggleItem>"},
243
244         {N_("/_Message"),               NULL, NULL, 0, "<Branch>"},
245         {N_("/_Message/Compose _new message"),
246                                         NULL, compose_cb, 0, NULL},
247         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
248         {N_("/_Message/_Reply"),        NULL, reply_cb, COMPOSE_REPLY, NULL},
249         {N_("/_Message/Repl_y to/_all"),
250                                         NULL, reply_cb, COMPOSE_REPLY_TO_ALL, NULL},
251         {N_("/_Message/Repl_y to/_sender"),
252                                         NULL, reply_cb, COMPOSE_REPLY_TO_SENDER, NULL},
253         {N_("/_Message/Repl_y to/mailing _list"),
254                                         NULL, reply_cb, COMPOSE_REPLY_TO_LIST, NULL},
255         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
256         {N_("/_Message/_Forward"),      NULL, reply_cb, COMPOSE_FORWARD, NULL},
257         {N_("/_Message/For_ward as attachment"),
258                                         NULL, reply_cb, COMPOSE_FORWARD_AS_ATTACH, NULL},
259         {N_("/_Message/Redirec_t"),     NULL, reply_cb, COMPOSE_REDIRECT, NULL},
260         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
261         {N_("/_Message/Re-_edit"),      NULL, reedit_cb, 0, NULL},
262
263         {N_("/_Tools"),                 NULL, NULL, 0, "<Branch>"},
264         {N_("/_Tools/_Address book"),   NULL, addressbook_open_cb, 0, NULL},
265         {N_("/_Tools/Add sender to address boo_k"),
266                                         NULL, add_address_cb, 0, NULL},
267         {N_("/_Tools/---"),             NULL, NULL, 0, "<Separator>"},
268         {N_("/_Tools/_Create filter rule"),
269                                         NULL, NULL, 0, "<Branch>"},
270         {N_("/_Tools/_Create filter rule/_Automatically"),
271                                         NULL, create_filter_cb, FILTER_BY_AUTO, NULL},
272         {N_("/_Tools/_Create filter rule/by _From"),
273                                         NULL, create_filter_cb, FILTER_BY_FROM, NULL},
274         {N_("/_Tools/_Create filter rule/by _To"),
275                                         NULL, create_filter_cb, FILTER_BY_TO, NULL},
276         {N_("/_Tools/_Create filter rule/by _Subject"),
277                                         NULL, create_filter_cb, FILTER_BY_SUBJECT, NULL},
278         {N_("/_Tools/Create processing rule/"),
279                                         NULL, NULL, 0, "<Branch>"},
280         {N_("/_Tools/Create processing rule/_Automatically"),
281                                         NULL, create_processing_cb, FILTER_BY_AUTO, NULL},
282         {N_("/_Tools/Create processing rule/by _From"),
283                                         NULL, create_processing_cb, FILTER_BY_FROM, NULL},
284         {N_("/_Tools/Create processing rule/by _To"),
285                                         NULL, create_processing_cb, FILTER_BY_TO, NULL},
286         {N_("/_Tools/Create processing rule/by _Subject"),
287                                         NULL, create_processing_cb, FILTER_BY_SUBJECT, NULL},
288         {N_("/_Tools/---"),             NULL, NULL, 0, "<Separator>"},
289         {N_("/_Tools/Actio_ns"),        NULL, NULL, 0, "<Branch>"},
290
291         {N_("/_Help"),                  NULL, NULL, 0, "<Branch>"},
292         {N_("/_Help/_About"),           NULL, about_cb, 0, NULL}
293 };
294
295 MessageView *messageview_create(MainWindow *mainwin)
296 {
297         MessageView *messageview;
298         GtkWidget *vbox;
299         HeaderView *headerview;
300         MimeView *mimeview;
301         NoticeView *noticeview;
302
303         debug_print("Creating message view...\n");
304         messageview = g_new0(MessageView, 1);
305
306         headerview = headerview_create();
307
308         noticeview = noticeview_create(mainwin);
309
310         mimeview = mimeview_create(mainwin);
311         mimeview->textview = textview_create();
312         mimeview->textview->messageview = messageview;
313         mimeview->messageview = messageview;
314
315         vbox = gtk_vbox_new(FALSE, 0);
316         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(headerview),
317                            FALSE, FALSE, 0);
318         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(noticeview),
319                            FALSE, FALSE, 0);
320         gtk_box_pack_start(GTK_BOX(vbox),
321                            GTK_WIDGET_PTR(mimeview), TRUE, TRUE, 0);
322         gtk_widget_show(vbox);
323
324         messageview->vbox        = vbox;
325         messageview->new_window  = FALSE;
326         messageview->window      = NULL;
327         messageview->headerview  = headerview;
328         messageview->mimeview    = mimeview;
329         messageview->noticeview = noticeview;
330         messageview->mainwin    = mainwin;
331
332         messageview->statusbar     = NULL;
333         messageview->statusbar_cid = 0;
334
335         messageview->msginfo_update_callback_id =
336                 hooks_register_hook(MSGINFO_UPDATE_HOOKLIST, messageview_update_msg, (gpointer) messageview);
337
338         return messageview;
339 }
340
341 GList *messageview_get_msgview_list(void)
342 {
343         return msgview_list;
344 }
345
346 void messageview_update_actions_menu(MessageView *msgview)
347 {
348         GtkItemFactory *ifactory;
349
350         /* Messages opened in a new window do not have a menu bar */
351         if (msgview->menubar == NULL)
352                 return;
353         ifactory = gtk_item_factory_from_widget(msgview->menubar);
354         action_update_msgview_menu(ifactory, "/Tools/Actions", msgview);
355 }
356
357 void messageview_add_toolbar(MessageView *msgview, GtkWidget *window) 
358 {
359         GtkWidget *handlebox;
360         GtkWidget *vbox;
361         GtkWidget *menubar;
362         GtkWidget *statusbar;
363         GtkItemFactory *ifactory;
364         guint n_menu_entries;
365
366         vbox = gtk_vbox_new(FALSE, 0);
367         gtk_widget_show(vbox);
368         gtk_container_add(GTK_CONTAINER(window), vbox); 
369
370         n_menu_entries = sizeof(msgview_entries) / sizeof(msgview_entries[0]);
371         menubar = menubar_create(window, msgview_entries,
372                                  n_menu_entries, "<MessageView>", msgview);
373         gtk_widget_show(menubar);
374         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
375
376         handlebox = gtk_handle_box_new();
377         gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
378         msgview->toolbar = toolbar_create(TOOLBAR_MSGVIEW, handlebox,
379                                           (gpointer)msgview);
380
381         statusbar = gtk_statusbar_new();
382         gtk_widget_show(statusbar);
383         gtk_box_pack_end(GTK_BOX(vbox), statusbar, FALSE, FALSE, 0);
384         msgview->statusbar = statusbar;
385         msgview->statusbar_cid = gtk_statusbar_get_context_id
386                 (GTK_STATUSBAR(statusbar), "Message View");
387
388         msgview->handlebox = handlebox;
389         msgview->menubar   = menubar;
390
391         gtk_container_add(GTK_CONTAINER(vbox),
392                           GTK_WIDGET_PTR(msgview));
393
394         messageview_update_actions_menu(msgview);
395
396         msgview_list = g_list_append(msgview_list, msgview);
397 }
398
399 MessageView *messageview_create_with_new_window(MainWindow *mainwin)
400 {
401         MessageView *msgview;
402         GtkWidget *window;
403
404         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
405         gtk_window_set_title(GTK_WINDOW(window), _("Sylpheed - Message View"));
406         gtk_window_set_wmclass(GTK_WINDOW(window), "message_view", "Sylpheed");
407         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
408         gtk_widget_set_usize(window, prefs_common.msgwin_width,
409                              prefs_common.msgwin_height);
410
411         msgview = messageview_create(mainwin);
412
413         gtk_signal_connect(GTK_OBJECT(window), "size_allocate",
414                            GTK_SIGNAL_FUNC(messageview_size_allocate_cb),
415                            msgview);
416         gtk_signal_connect(GTK_OBJECT(window), "destroy",
417                            GTK_SIGNAL_FUNC(messageview_destroy_cb), msgview);
418         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
419                            GTK_SIGNAL_FUNC(key_pressed), msgview);
420
421         messageview_add_toolbar(msgview, window);
422
423         gtk_widget_grab_focus(msgview->mimeview->textview->text);
424         gtk_widget_show(window);
425
426         msgview->new_window = TRUE;
427         msgview->window = window;
428         msgview->visible = TRUE;
429
430         toolbar_set_style(msgview->toolbar->toolbar, msgview->handlebox, 
431                           prefs_common.toolbar_style);
432         messageview_init(msgview);
433
434         return msgview;
435 }
436
437 void messageview_init(MessageView *messageview)
438 {
439         headerview_init(messageview->headerview);
440         mimeview_init(messageview->mimeview);
441         /*messageview_set_font(messageview);*/
442
443         noticeview_hide(messageview->noticeview);
444 }
445
446 static void notification_convert_header(gchar *dest, gint len, 
447                                         const gchar *src_,
448                                         gint header_len)
449 {
450         char *src;
451
452         g_return_if_fail(src_ != NULL);
453         g_return_if_fail(dest != NULL);
454
455         if (len < 1) return;
456
457         Xstrndup_a(src, src_, len, return);
458
459         remove_return(src);
460
461         if (is_ascii_str(src)) {
462                 strncpy2(dest, src, len);
463                 dest[len - 1] = '\0';
464                 return;
465         } else
466                 conv_encode_header(dest, len, src, header_len, FALSE);
467 }
468
469 static gint disposition_notification_send(MsgInfo *msginfo)
470 {
471         gchar buf[BUFFSIZE];
472         gchar tmp[MAXPATHLEN + 1];
473         FILE *fp;
474         GList *ac_list;
475         PrefsAccount *account;
476         gint ok;
477         gchar *to;
478         FolderItem *queue, *outbox;
479         gint num;
480         gchar *path;
481         gchar *addr;
482         gchar *addrp;
483
484         if ((!msginfo->returnreceiptto) && 
485             (!msginfo->dispositionnotificationto)) 
486                 return -1;
487
488         /* RFC2298: Test for Return-Path */
489         if (msginfo->dispositionnotificationto)
490                 to = msginfo->dispositionnotificationto;
491         else
492                 to = msginfo->returnreceiptto;
493
494         ok = procheader_get_header_from_msginfo(msginfo, buf, sizeof(buf),
495                                 "Return-Path:");
496         if (ok == 0) {
497                 gchar *to_addr = g_strdup(to);
498                 extract_address(to_addr);
499                 extract_address(buf);
500                 ok = strcmp(to_addr, buf);
501                 g_free(to_addr);
502         } else {
503                 strncpy(buf, _("<No Return-Path found>"), 
504                                 sizeof(buf));
505         }
506         
507         if (ok != 0) {
508                 AlertValue val;
509                 gchar *message;
510                 message = g_strdup_printf(
511                                  _("The notification address to which the "
512                                    "return receipt is to be sent\n"
513                                    "does not correspond to the return path:\n"
514                                    "Notification address: %s\n"
515                                    "Return path: %s\n"
516                                    "It is advised to not to send the return "
517                                    "receipt."), to, buf);
518                 val = alertpanel(_("Warning"), message, _("Send"),
519                                 _("+Don't Send"), NULL);
520                 g_free(message);                                
521                 if (val != G_ALERTDEFAULT)
522                         return -1;
523         }
524
525         ac_list = account_find_all_from_address(NULL, msginfo->to);
526         ac_list = account_find_all_from_address(ac_list, msginfo->cc);
527
528         if (ac_list == NULL) {
529                 alertpanel_error(_("This message is asking for a return "
530                                    "receipt notification\n"
531                                    "but according to its 'To:' and 'CC:' "
532                                    "headers it was not\nofficially addressed "
533                                    "to you.\n"
534                                    "Receipt notification cancelled."));
535                 return -1;
536         }
537
538         if (g_list_length(ac_list) > 1)
539                 account = select_account_from_list(ac_list);
540         else
541                 account = (PrefsAccount *) ac_list->data;
542         g_list_free(ac_list);
543
544         if (account == NULL)
545                 return -1;
546
547         /* write to temporary file */
548         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
549                    get_rc_dir(), G_DIR_SEPARATOR, (gint)msginfo);
550
551         if ((fp = fopen(tmp, "wb")) == NULL) {
552                 FILE_OP_ERROR(tmp, "fopen");
553                 return -1;
554         }
555
556         /* chmod for security */
557         if (change_file_mode_rw(fp, tmp) < 0) {
558                 FILE_OP_ERROR(tmp, "chmod");
559                 g_warning("can't change file mode\n");
560         }
561         
562         addr = g_strdup(to);
563         
564         extract_address(addr);
565         addrp = addr;
566         
567         /* write queue headers */
568         fprintf(fp, "AF:\n");
569         fprintf(fp, "NF:0\n");
570         fprintf(fp, "PS:10\n");
571         fprintf(fp, "SRH:1\n");
572         fprintf(fp, "SFN:\n");
573         fprintf(fp, "DSR:\n");
574         fprintf(fp, "MID:\n");
575         fprintf(fp, "CFG:\n");
576         fprintf(fp, "PT:0\n");
577         fprintf(fp, "S:%s\n", account->address);
578         fprintf(fp, "RQ:\n");
579         if (account->smtp_server)
580                 fprintf(fp, "SSV:%s\n", account->smtp_server);
581         else
582                 fprintf(fp, "SSV:\n");
583         fprintf(fp, "SSH:\n");
584         fprintf(fp, "R:<%s>\n", addrp);
585         
586         g_free(addrp);
587         
588         /* check whether we need to save the message */
589         outbox = account_get_special_folder(account, F_OUTBOX); 
590         if (folder_get_default_outbox() == outbox && !prefs_common.savemsg)
591                 outbox = NULL;
592         if (outbox) {
593                 path = folder_item_get_identifier(outbox);
594                 fprintf(fp, "SCF:%s\n", path);
595                 g_free(path);
596         }               
597
598         fprintf(fp, "\n");
599         
600         /* Date */
601         get_rfc822_date(buf, sizeof(buf));
602         fprintf(fp, "Date: %s\n", buf);
603
604         /* From */
605         if (account->name && *account->name) {
606                 notification_convert_header
607                         (buf, sizeof(buf), account->name,
608                          strlen("From: "));
609                 fprintf(fp, "From: %s <%s>\n", buf, account->address);
610         } else
611                 fprintf(fp, "From: %s\n", account->address);
612
613         fprintf(fp, "To: %s\n", to);
614
615         /* Subject */
616         notification_convert_header(buf, sizeof(buf), msginfo->subject,
617                                     strlen("Subject: "));
618         fprintf(fp, "Subject: Disposition notification: %s\n", buf);
619
620         /* Message ID */
621         generate_msgid(account->address, buf, sizeof buf);
622         fprintf(fp, "Message-Id: <%s>\n", buf);
623
624         if (fclose(fp) == EOF) {
625                 FILE_OP_ERROR(tmp, "fclose");
626                 unlink(tmp);
627                 return -1;
628         }
629
630         /* put it in queue */
631         queue = account_get_special_folder(account, F_QUEUE);
632         if (!queue) queue = folder_get_default_queue();
633         if (!queue) {
634                 g_warning("can't find queue folder\n");
635                 unlink(tmp);
636                 return -1;
637         }
638         folder_item_scan(queue);
639         if ((num = folder_item_add_msg(queue, tmp, NULL, TRUE)) < 0) {
640                 g_warning("can't queue the message\n");
641                 unlink(tmp);
642                 return -1;
643         }
644         
645         /* send it */
646         path = folder_item_fetch_msg(queue, num);
647         ok = procmsg_send_message_queue(path);
648         g_free(path);
649         folder_item_remove_msg(queue, num);
650
651         return ok;
652 }
653
654 GList *messageview_get_window_list(void)
655 {
656         return messageview_list;
657 }
658
659 static gboolean find_encrypted_func(GNode *node, gpointer data)
660 {
661         MimeInfo *mimeinfo = (MimeInfo *) node->data;
662         MimeInfo **encinfo = (MimeInfo **) data;
663         
664         if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
665                 *encinfo = mimeinfo;
666                 return TRUE;
667         }
668         
669         return FALSE;
670 }
671
672 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
673 {
674         MimeInfo *encinfo = NULL;
675
676         g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
677                 find_encrypted_func, &encinfo);
678         
679         return encinfo;
680 }
681
682 gint messageview_show(MessageView *messageview, MsgInfo *msginfo,
683                       gboolean all_headers)
684 {
685         gchar *file;
686         MimeInfo *mimeinfo, *encinfo;
687
688         g_return_val_if_fail(msginfo != NULL, -1);
689
690         mimeinfo = procmime_scan_message(msginfo);
691         if (!mimeinfo) {
692                 textview_show_error(messageview->mimeview->textview);
693                 return -1;
694         }
695
696         while ((encinfo = find_encrypted_part(mimeinfo)) != NULL) {
697                 debug_print("decrypting message part\n");
698                 if (privacy_mimeinfo_decrypt(encinfo) < 0)
699                         break;
700         }
701         
702         file = procmsg_get_message_file_path(msginfo);
703         if (!file) {
704                 g_warning("can't get message file path.\n");
705                 procmime_mimeinfo_free_all(mimeinfo);
706                 textview_show_error(messageview->mimeview->textview);
707                 return -1;
708         }
709
710         if (messageview->msginfo != msginfo) {
711                 procmsg_msginfo_free(messageview->msginfo);
712                 messageview->msginfo = procmsg_msginfo_get_full_info(msginfo);
713         }
714         headerview_show(messageview->headerview, messageview->msginfo);
715
716         messageview->all_headers = all_headers;
717         textview_set_all_headers(messageview->mimeview->textview, all_headers);
718
719         mimeview_show_message(messageview->mimeview, mimeinfo, file);
720
721         if ((messageview->msginfo->dispositionnotificationto || 
722              messageview->msginfo->returnreceiptto) &&
723             !MSG_IS_RETRCPT_SENT(messageview->msginfo->flags))
724                 return_receipt_show(messageview->noticeview, messageview->msginfo);
725         else 
726                 noticeview_hide(messageview->noticeview);
727
728         g_free(file);
729
730         return 0;
731 }
732
733 void messageview_reflect_prefs_pixmap_theme(void)
734 {
735         GList *cur;
736         MessageView *msgview;
737
738         for (cur = msgview_list; cur != NULL; cur = cur->next) {
739                 msgview = (MessageView*)cur->data;
740                 toolbar_update(TOOLBAR_MSGVIEW, msgview);
741                 mimeview_update(msgview->mimeview);
742         }
743 }
744
745 void messageview_clear(MessageView *messageview)
746 {
747         procmsg_msginfo_free(messageview->msginfo);
748         messageview->msginfo = NULL;
749         messageview->filtered = FALSE;
750         mimeview_clear(messageview->mimeview);
751         headerview_clear(messageview->headerview);
752         noticeview_hide(messageview->noticeview);
753 }
754
755 void messageview_destroy(MessageView *messageview)
756 {
757         GtkWidget *mimeview  = GTK_WIDGET_PTR(messageview->mimeview);
758
759         debug_print("destroy messageview\n");
760         messageview_list = g_list_remove(messageview_list, messageview);
761
762         hooks_unregister_hook(MSGINFO_UPDATE_HOOKLIST,
763                               messageview->msginfo_update_callback_id);
764
765         headerview_destroy(messageview->headerview);
766         mimeview_destroy(messageview->mimeview);
767         noticeview_destroy(messageview->noticeview);
768
769         procmsg_msginfo_free(messageview->msginfo);
770         toolbar_clear_list(TOOLBAR_MSGVIEW);
771         if (messageview->toolbar) {
772                 toolbar_destroy(messageview->toolbar);
773                 g_free(messageview->toolbar);
774         }
775         
776         msgview_list = g_list_remove(msgview_list, messageview); 
777
778         g_free(messageview);
779 }
780
781 void messageview_delete(MessageView *msgview)
782 {
783         MsgInfo *msginfo = NULL;
784         FolderItem *trash = NULL;
785         PrefsAccount *ac = NULL;
786
787         if (msgview->msginfo && msgview->mainwin && msgview->mainwin->summaryview)
788                 msginfo = summary_get_selected_msg(msgview->mainwin->summaryview);
789         
790         /* need a procmsg_msginfo_equal() */
791         if (msginfo && msgview->msginfo && 
792             msginfo->msgnum == msgview->msginfo->msgnum && 
793             msginfo->folder == msgview->msginfo->folder) {
794                 summary_delete(msgview->mainwin->summaryview);
795         } else {                
796                 msginfo = msgview->msginfo;
797
798                 g_return_if_fail(msginfo != NULL);
799
800                 /* to get the trash folder, we have to choose either
801                  * the folder's or account's trash default - we prefer
802                  * the one in the account prefs */
803                 if (msginfo->folder) {
804                         if (NULL != (ac = account_find_from_item(msginfo->folder)))
805                                 trash = account_get_special_folder(ac, F_TRASH);
806                         if (!trash && msginfo->folder->folder)  
807                                 trash = msginfo->folder->folder->trash;
808                         /* if still not found, use the default */
809                         if (!trash) 
810                                 trash = folder_get_default_trash();
811                 }       
812
813                 g_return_if_fail(trash != NULL);
814
815                 if (prefs_common.immediate_exec)
816                         /* TODO: Delete from trash */
817                         folder_item_move_msg(trash, msginfo);
818                 else {
819                         procmsg_msginfo_set_to_folder(msginfo, trash);
820                         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
821                         /* NOTE: does not update to next message in summaryview */
822                 }
823         }               
824 }
825
826 /* 
827  * \brief update messageview with currently selected message in summaryview
828  *        leave unchanged if summaryview is empty
829  * \param pointer to MessageView
830  */     
831 static void messageview_update(MessageView *msgview)
832 {
833         SummaryView *summaryview = (SummaryView*)msgview->mainwin->summaryview;
834
835         g_return_if_fail(summaryview != NULL);
836         
837         if (summaryview->selected) {
838                 GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
839                 MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, 
840                                                       summaryview->selected);
841                 g_return_if_fail(msginfo != NULL);
842
843                 messageview_show(msgview, msginfo, 
844                                  msgview->all_headers);
845         } 
846 }
847
848 void messageview_quote_color_set(void)
849 {
850 }
851
852 void messageview_set_font(MessageView *messageview)
853 {
854 }
855
856 TextView *messageview_get_current_textview(MessageView *messageview)
857 {
858         TextView *text = NULL;
859
860         text = messageview->mimeview->textview;
861
862         return text;
863 }
864
865 MimeInfo *messageview_get_selected_mime_part(MessageView *messageview)
866 {
867         return mimeview_get_selected_part(messageview->mimeview);
868 }
869
870 void messageview_copy_clipboard(MessageView *messageview)
871 {
872         TextView *text;
873
874         text = messageview_get_current_textview(messageview);
875         if (text)
876                 gtk_editable_copy_clipboard(GTK_EDITABLE(text->text));
877 }
878
879 void messageview_select_all(MessageView *messageview)
880 {
881         TextView *text;
882
883         text = messageview_get_current_textview(messageview);
884         if (text)
885                 gtk_editable_select_region(GTK_EDITABLE(text->text), 0, -1);
886 }
887
888 void messageview_set_position(MessageView *messageview, gint pos)
889 {
890         TextView *text;
891
892         text = messageview_get_current_textview(messageview);
893         if (text)
894                 textview_set_position(text, pos);
895 }
896
897 gboolean messageview_search_string(MessageView *messageview, const gchar *str,
898                                    gboolean case_sens)
899 {
900         TextView *text;
901
902         text = messageview_get_current_textview(messageview);
903         if (text)
904                 return textview_search_string(text, str, case_sens);
905         return FALSE;
906 }
907
908 gboolean messageview_search_string_backward(MessageView *messageview,
909                                             const gchar *str,
910                                             gboolean case_sens)
911 {
912         TextView *text;
913
914         text = messageview_get_current_textview(messageview);
915         if (text)       
916                 return textview_search_string_backward(text,
917                                                        str, case_sens);
918         return FALSE;
919 }
920
921 gboolean messageview_is_visible(MessageView *messageview)
922 {
923         return messageview->visible;
924 }
925
926 void messageview_save_as(MessageView *messageview)
927 {
928         gchar *filename = NULL;
929         MsgInfo *msginfo;
930         gchar *src, *dest;
931
932         if (!messageview->msginfo) return;
933         msginfo = messageview->msginfo;
934
935         if (msginfo->subject) {
936                 Xstrdup_a(filename, msginfo->subject, return);
937                 subst_for_filename(filename);
938         }
939         dest = filesel_select_file(_("Save as"), filename);
940         if (!dest) return;
941         if (is_file_exist(dest)) {
942                 AlertValue aval;
943
944                 aval = alertpanel(_("Overwrite"),
945                                   _("Overwrite existing file?"),
946                                   _("OK"), _("Cancel"), NULL);
947                 if (G_ALERTDEFAULT != aval) return;
948         }
949
950         src = procmsg_get_message_file(msginfo);
951         if (copy_file(src, dest, TRUE) < 0) {
952                 alertpanel_error(_("Can't save the file `%s'."),
953                                  g_basename(dest));
954         }
955         g_free(src);
956 }
957
958 static void messageview_destroy_cb(GtkWidget *widget, MessageView *messageview)
959 {
960         messageview_destroy(messageview);
961 }
962
963 static void messageview_size_allocate_cb(GtkWidget *widget,
964                                          GtkAllocation *allocation)
965 {
966         g_return_if_fail(allocation != NULL);
967
968         prefs_common.msgwin_width  = allocation->width;
969         prefs_common.msgwin_height = allocation->height;
970 }
971
972 static void key_pressed(GtkWidget *widget, GdkEventKey *event,
973                         MessageView *messageview)
974 {
975         if (event && event->keyval == GDK_Escape && messageview->window)
976                 gtk_widget_destroy(messageview->window);
977 }
978
979 void messageview_toggle_view_real(MessageView *messageview)
980 {
981         MainWindow *mainwin = messageview->mainwin;
982         union CompositeWin *cwin = &mainwin->win;
983         GtkWidget *vpaned = NULL;
984         GtkWidget *container = NULL;
985         GtkItemFactory *ifactory = gtk_item_factory_from_widget(mainwin->menubar);
986         
987         switch (mainwin->type) {
988         case SEPARATE_NONE:
989                 vpaned = cwin->sep_none.vpaned;
990                 container = cwin->sep_none.hpaned;
991                 break;
992         case SEPARATE_FOLDER:
993                 vpaned = cwin->sep_folder.vpaned;
994                 container = mainwin->vbox_body;
995                 break;
996         case SEPARATE_MESSAGE:
997         case SEPARATE_BOTH:
998                 return;
999         }
1000
1001         if (vpaned->parent != NULL) {
1002                 gtk_widget_ref(vpaned);
1003                 gtkut_container_remove(GTK_CONTAINER(container), vpaned);
1004                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), container);
1005                 menu_set_sensitive(ifactory, "/View/Expand Summary View", FALSE);
1006         } else {
1007                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), vpaned);
1008                 gtk_container_add(GTK_CONTAINER(container), vpaned);
1009                 gtk_widget_unref(vpaned);
1010                 menu_set_sensitive(ifactory, "/View/Expand Summary View", TRUE);
1011                 gtk_widget_grab_focus(GTK_WIDGET(mainwin->summaryview->ctree));
1012         }
1013 }
1014
1015 static void return_receipt_show(NoticeView *noticeview, MsgInfo *msginfo)
1016 {
1017         noticeview_set_text(noticeview, _("This message asks for a return receipt"));
1018         noticeview_set_button_text(noticeview, _("Send receipt"));
1019         noticeview_set_button_press_callback(noticeview,
1020                                              GTK_SIGNAL_FUNC(return_receipt_send_clicked),
1021                                              (gpointer) msginfo);
1022         noticeview_show(noticeview);
1023 }
1024
1025 static void return_receipt_send_clicked(NoticeView *noticeview, MsgInfo *msginfo)
1026 {
1027         MsgInfo *tmpmsginfo;
1028         gchar *file;
1029
1030         file = procmsg_get_message_file_path(msginfo);
1031         if (!file) {
1032                 g_warning("can't get message file path.\n");
1033                 return;
1034         }
1035
1036         tmpmsginfo = procheader_parse_file(file, msginfo->flags, TRUE, TRUE);
1037         tmpmsginfo->folder = msginfo->folder;
1038         tmpmsginfo->msgnum = msginfo->msgnum;
1039
1040         if (disposition_notification_send(tmpmsginfo) >= 0) {
1041                 procmsg_msginfo_set_flags(msginfo, MSG_RETRCPT_SENT, 0);
1042                 noticeview_hide(noticeview);
1043         }               
1044
1045         procmsg_msginfo_free(tmpmsginfo);
1046         g_free(file);
1047 }
1048
1049 static void select_account_cb(GtkWidget *w, gpointer data)
1050 {
1051         *(gint*)data = GPOINTER_TO_INT(gtk_object_get_user_data(GTK_OBJECT(w)));
1052 }
1053         
1054 static PrefsAccount *select_account_from_list(GList *ac_list)
1055 {
1056         GtkWidget *optmenu;
1057         GtkWidget *menu;
1058         gint account_id;
1059
1060         g_return_val_if_fail(ac_list != NULL, NULL);
1061         g_return_val_if_fail(ac_list->data != NULL, NULL);
1062         
1063         optmenu = gtk_option_menu_new();
1064         menu = gtkut_account_menu_new(ac_list, select_account_cb, &account_id);
1065         if (!menu)
1066                 return NULL;
1067         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), menu);
1068         gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), 0);
1069         account_id = ((PrefsAccount *) ac_list->data)->account_id;
1070         if (alertpanel_with_widget(
1071                                 _("Return Receipt Notification"),
1072                                 _("The message was sent to several of your "
1073                                   "accounts.\n"
1074                                   "Please choose which account do you want to "
1075                                   "use for sending the receipt notification:"),
1076                                 _("Send Notification"), _("+Cancel"), NULL,
1077                                 optmenu) != G_ALERTDEFAULT)
1078                 return NULL;
1079         return account_find_from_id(account_id);
1080 }
1081
1082 /* 
1083  * \brief return selected messageview text, when nothing is 
1084  *        selected and message was filtered, return complete text
1085  *
1086  * \param  pointer to Messageview 
1087  *
1088  * \return pointer to text (needs to be free'd by calling func)
1089  */
1090 gchar *messageview_get_selection(MessageView *msgview)
1091 {
1092         TextView *textview;
1093         gchar *text = NULL;
1094         GtkEditable *edit = NULL;
1095         gint body_pos = 0;
1096         
1097         g_return_val_if_fail(msgview != NULL, NULL);
1098
1099         textview = messageview_get_current_textview(msgview);
1100         g_return_val_if_fail(textview != NULL, NULL);
1101
1102         edit = GTK_EDITABLE(textview->text);
1103         g_return_val_if_fail(edit != NULL, NULL);
1104         body_pos = textview->body_pos;
1105
1106         if (edit->has_selection)
1107                 text = gtkut_editable_get_selection(edit);
1108         else if (msgview->filtered) 
1109                 text = gtk_editable_get_chars (edit, body_pos, -1);
1110         else
1111                 text = NULL;
1112         
1113         return text;
1114 }
1115
1116 static void save_as_cb(gpointer data, guint action, GtkWidget *widget)
1117 {
1118         MessageView *messageview = (MessageView *)data;
1119         messageview_save_as(messageview);
1120 }
1121
1122 static void print_cb(gpointer data, guint action, GtkWidget *widget)
1123 {
1124         MessageView *messageview = (MessageView *)data;
1125         gchar *cmdline;
1126         gchar *p;
1127
1128         if (!messageview->msginfo) return;
1129
1130         cmdline = input_dialog(_("Print"),
1131                                _("Enter the print command line:\n"
1132                                  "(`%s' will be replaced with file name)"),
1133                                prefs_common.print_cmd);
1134         if (!cmdline) return;
1135         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
1136             strchr(p + 2, '%')) {
1137                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
1138                                  cmdline);
1139                 g_free(cmdline);
1140                 return;
1141         }
1142
1143         procmsg_print_message(messageview->msginfo, cmdline);
1144         g_free(cmdline);
1145 }
1146
1147 static void close_cb(gpointer data, guint action, GtkWidget *widget)
1148 {
1149         MessageView *messageview = (MessageView *)data;
1150         gtk_widget_destroy(messageview->window);
1151 }
1152
1153 static void copy_cb(gpointer data, guint action, GtkWidget *widget)
1154 {
1155         MessageView *messageview = (MessageView *)data;
1156         messageview_copy_clipboard(messageview);
1157 }
1158
1159 static void allsel_cb(gpointer data, guint action, GtkWidget *widget)
1160 {
1161         MessageView *messageview = (MessageView *)data;
1162         messageview_select_all(messageview);
1163 }
1164
1165 static void search_cb(gpointer data, guint action, GtkWidget *widget)
1166 {
1167         MessageView *messageview = (MessageView *)data;
1168         message_search(messageview);
1169 }
1170
1171 static void set_charset_cb(gpointer data, guint action, GtkWidget *widget)
1172 {
1173         MessageView *messageview = (MessageView *)data;
1174         const gchar *charset;
1175
1176         if (GTK_CHECK_MENU_ITEM(widget)->active) {
1177                 charset = conv_get_charset_str((CharSet)action);
1178                 g_free(messageview->forced_charset);
1179                 messageview->forced_charset = g_strdup(charset);
1180                 messageview_show(messageview, messageview->msginfo, FALSE);
1181         }
1182 }
1183
1184 static void view_source_cb(gpointer data, guint action, GtkWidget *widget)
1185 {
1186         MessageView *messageview = (MessageView *)data;
1187         SourceWindow *srcwin;
1188
1189         if (!messageview->msginfo) return;
1190
1191         srcwin = source_window_create();
1192         source_window_show_msg(srcwin, messageview->msginfo);
1193         source_window_show(srcwin);
1194 }
1195
1196 static void show_all_header_cb(gpointer data, guint action, GtkWidget *widget)
1197 {
1198         MessageView *messageview = (MessageView *)data;
1199         MsgInfo *msginfo = messageview->msginfo;
1200
1201         if (!msginfo) return;
1202         messageview->msginfo = NULL;
1203         messageview_show(messageview, msginfo,
1204                          GTK_CHECK_MENU_ITEM(widget)->active);
1205         procmsg_msginfo_free(msginfo);
1206         main_window_set_menu_sensitive(messageview->mainwin);
1207 }
1208
1209 static void compose_cb(gpointer data, guint action, GtkWidget *widget)
1210 {
1211         MessageView *messageview = (MessageView *)data;
1212         PrefsAccount *ac = NULL;
1213         FolderItem *item = NULL;
1214
1215         if (messageview->msginfo)
1216                 item = messageview->msginfo->folder;
1217
1218         if (item) {
1219                 ac = account_find_from_item(item);
1220                 if (ac && ac->protocol == A_NNTP &&
1221                     item->stype == F_NEWS) {
1222                         compose_new(ac, item->path, NULL);
1223                         return;
1224                 }
1225         }
1226
1227         compose_new(ac, NULL, NULL);
1228 }
1229
1230 static void reply_cb(gpointer data, guint action, GtkWidget *widget)
1231 {
1232         MessageView *messageview = (MessageView *)data;
1233         GSList *mlist = NULL;
1234         MsgInfo *msginfo;
1235         gchar *text = NULL;
1236         ComposeMode mode = (ComposeMode)action;
1237         TextView *textview;
1238
1239         msginfo = messageview->msginfo;
1240         mlist = g_slist_append(NULL, msginfo);
1241
1242         textview = messageview_get_current_textview(messageview);
1243         text = gtkut_editable_get_selection
1244                 (GTK_EDITABLE(textview->text));
1245         if (text && *text == '\0') {
1246                 g_free(text);
1247                 text = NULL;
1248         }
1249
1250         switch (mode) {
1251         case COMPOSE_REPLY:
1252                 compose_reply(msginfo, prefs_common.reply_with_quote,
1253                               FALSE, prefs_common.default_reply_list, FALSE, text);
1254                 break;
1255         case COMPOSE_REPLY_WITH_QUOTE:
1256                 compose_reply(msginfo, TRUE, FALSE, prefs_common.default_reply_list, FALSE, text);
1257                 break;
1258         case COMPOSE_REPLY_WITHOUT_QUOTE:
1259                 compose_reply(msginfo, FALSE, FALSE, prefs_common.default_reply_list, FALSE, NULL);
1260                 break;
1261         case COMPOSE_REPLY_TO_SENDER:
1262                 compose_reply(msginfo, prefs_common.reply_with_quote,
1263                               FALSE, FALSE, TRUE, text);
1264                 break;
1265         case COMPOSE_FOLLOWUP_AND_REPLY_TO:
1266                 compose_followup_and_reply_to(msginfo,
1267                                               prefs_common.reply_with_quote,
1268                                               FALSE, FALSE, text);
1269                 break;
1270         case COMPOSE_REPLY_TO_SENDER_WITH_QUOTE:
1271                 compose_reply(msginfo, TRUE, FALSE, FALSE, TRUE, text);
1272                 break;
1273         case COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE:
1274                 compose_reply(msginfo, FALSE, FALSE, FALSE, TRUE, NULL);
1275                 break;
1276         case COMPOSE_REPLY_TO_ALL:
1277                 compose_reply(msginfo, prefs_common.reply_with_quote,
1278                               TRUE, FALSE, FALSE, text);
1279                 break;
1280         case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
1281                 compose_reply(msginfo, TRUE, TRUE, FALSE, FALSE, text);
1282                 break;
1283         case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
1284                 compose_reply(msginfo, FALSE, TRUE, FALSE, FALSE, NULL);
1285                 break;
1286         case COMPOSE_REPLY_TO_LIST:
1287                 compose_reply(msginfo, prefs_common.reply_with_quote,
1288                               FALSE, TRUE, FALSE, text);
1289                 break;
1290         case COMPOSE_REPLY_TO_LIST_WITH_QUOTE:
1291                 compose_reply(msginfo, TRUE, FALSE, TRUE, FALSE, text);
1292                 break;
1293         case COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE:
1294                 compose_reply(msginfo, FALSE, FALSE, TRUE, FALSE, NULL);
1295                 break;
1296         case COMPOSE_FORWARD:
1297                 if (prefs_common.forward_as_attachment) {
1298                         compose_reply_mode(COMPOSE_FORWARD_AS_ATTACH, mlist, text);
1299                         return;
1300                 } else {
1301                         compose_reply_mode(COMPOSE_FORWARD_INLINE, mlist, text);
1302                         return;
1303                 }
1304                 break;
1305         case COMPOSE_FORWARD_INLINE:
1306                 compose_forward(NULL, msginfo, FALSE, text, FALSE);
1307                 break;
1308         case COMPOSE_FORWARD_AS_ATTACH:
1309                 compose_forward_multiple(NULL, mlist);
1310                 break;
1311         case COMPOSE_REDIRECT:
1312                 compose_redirect(NULL, msginfo);
1313                 break;
1314         default:
1315                 g_warning("compose_reply(): invalid Compose Mode: %d\n", mode);
1316         }
1317
1318         /* summary_set_marks_selected(summaryview); */
1319         g_free(text);
1320         g_slist_free(mlist);
1321 }
1322
1323 static void reedit_cb(gpointer data, guint action, GtkWidget *widget)
1324 {
1325         MessageView *messageview = (MessageView *)data;
1326         MsgInfo *msginfo;
1327
1328         if (!messageview->msginfo) return;
1329         msginfo = messageview->msginfo;
1330         if (!msginfo->folder) return;
1331         if (msginfo->folder->stype != F_OUTBOX &&
1332             msginfo->folder->stype != F_DRAFT &&
1333             msginfo->folder->stype != F_QUEUE) return;
1334
1335         compose_reedit(msginfo);
1336 }
1337
1338 static void addressbook_open_cb(gpointer data, guint action, GtkWidget *widget)
1339 {
1340         addressbook_open(NULL);
1341 }
1342
1343 static void add_address_cb(gpointer data, guint action, GtkWidget *widget)
1344 {
1345         MessageView *messageview = (MessageView *)data;
1346         MsgInfo *msginfo;
1347         gchar *from;
1348
1349         if (!messageview->msginfo) return;
1350         msginfo = messageview->msginfo;
1351         Xstrdup_a(from, msginfo->from, return);
1352         eliminate_address_comment(from);
1353         extract_address(from);
1354         addressbook_add_contact(msginfo->fromname, from, NULL);
1355 }
1356
1357 static void create_filter_cb(gpointer data, guint action, GtkWidget *widget)
1358 {
1359         MessageView *messageview = (MessageView *)data;
1360         FolderItem * item;
1361         
1362         if (!messageview->msginfo) return;
1363         
1364         item = messageview->msginfo->folder;
1365         summary_msginfo_filter_open(item,  messageview->msginfo,
1366                                     (PrefsFilterType)action, 0);
1367 }
1368
1369 static void create_processing_cb(gpointer data, guint action,
1370                                  GtkWidget *widget)
1371 {
1372         MessageView *messageview = (MessageView *)data;
1373         FolderItem * item;
1374         
1375         if (!messageview->msginfo) return;
1376         
1377         item = messageview->msginfo->folder;
1378         summary_msginfo_filter_open(item,  messageview->msginfo,
1379                                     (PrefsFilterType)action, 1);
1380 }
1381
1382 static void about_cb(gpointer data, guint action, GtkWidget *widget)
1383 {
1384         about_show();
1385 }
1386
1387 static gboolean messageview_update_msg(gpointer source, gpointer data)
1388 {
1389         MsgInfoUpdate *msginfo_update = (MsgInfoUpdate *) source;
1390         MessageView *messageview = (MessageView *)data;
1391
1392         if (messageview->msginfo != msginfo_update->msginfo)
1393                 return FALSE;
1394
1395         if (msginfo_update->flags & MSGINFO_UPDATE_DELETED) {
1396                 messageview_clear(messageview);
1397                 messageview_update(messageview);
1398         }
1399
1400         return FALSE;
1401 }
1402
1403 void messageview_set_menu_sensitive(MessageView *messageview)
1404 {
1405         GtkItemFactory *ifactory;
1406         GtkWidget *menuitem;
1407
1408         if (!messageview && !messageview->new_window) 
1409                 return;
1410         /* do some smart things */
1411         if (!messageview->menubar) return;
1412         ifactory = gtk_item_factory_from_widget(messageview->menubar);
1413         if (!ifactory) return;
1414         if (messageview->mainwin->type == SEPARATE_MESSAGE) {
1415                 menuitem = gtk_item_factory_get_widget(ifactory, "/View/Show all headers");
1416                 gtk_check_menu_item_set_active
1417                         (GTK_CHECK_MENU_ITEM(menuitem),
1418                          messageview->mimeview->textview->show_all_headers);
1419         }
1420 }