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