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