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