ef3ac3c92b26862875e66fd737b8a0f23b6c8182
[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(mainwin);
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_send(MsgInfo *msginfo)
445 {
446         gchar buf[BUFFSIZE];
447         gchar tmp[MAXPATHLEN + 1];
448         FILE *fp;
449         GList *ac_list;
450         PrefsAccount *account;
451         gint ok;
452         gchar *to;
453         FolderItem *queue, *outbox;
454         gint num;
455         gchar *path;
456
457         if ((!msginfo->returnreceiptto) && 
458             (!msginfo->dispositionnotificationto)) 
459                 return -1;
460
461         /* RFC2298: Test for Return-Path */
462         if (msginfo->dispositionnotificationto)
463                 to = msginfo->dispositionnotificationto;
464         else
465                 to = msginfo->returnreceiptto;
466
467         ok = get_header_from_msginfo(msginfo, buf, sizeof(buf),
468                                 "Return-Path:");
469         if (ok == 0) {
470                 gchar *to_addr = g_strdup(to);
471                 extract_address(to_addr);
472                 extract_address(buf);
473                 ok = strcmp(to_addr, buf);
474                 g_free(to_addr);
475         } else {
476                 strncpy(buf, _("<No Return-Path found>"), 
477                                 sizeof(buf));
478         }
479         
480         if (ok != 0) {
481                 AlertValue val;
482                 gchar *message;
483                 message = g_strdup_printf(
484                                  _("The notification address to which the "
485                                    "return receipt is to be sent\n"
486                                    "does not correspond to the return path:\n"
487                                    "Notification address: %s\n"
488                                    "Return path: %s\n"
489                                    "It is advised to not to send the return "
490                                    "receipt."), to, buf);
491                 val = alertpanel(_("Warning"), message, _("Send"),
492                                 _("+Don't Send"), NULL);
493                 g_free(message);                                
494                 if (val != G_ALERTDEFAULT)
495                         return -1;
496         }
497
498         ac_list = account_find_all_from_address(NULL, msginfo->to);
499         ac_list = account_find_all_from_address(ac_list, msginfo->cc);
500
501         if (ac_list == NULL) {
502                 alertpanel_error(_("This message is asking for a return "
503                                    "receipt notification\n"
504                                    "but according to its 'To:' and 'CC:' "
505                                    "headers it was not\nofficially addressed "
506                                    "to you.\n"
507                                    "Receipt notification cancelled."));
508                 return -1;
509         }
510
511         if (g_list_length(ac_list) > 1)
512                 account = select_account_from_list(ac_list);
513         else
514                 account = (PrefsAccount *) ac_list->data;
515         g_list_free(ac_list);
516
517         if (account == NULL)
518                 return -1;
519
520         /* write to temporary file */
521         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
522                    get_rc_dir(), G_DIR_SEPARATOR, (gint)msginfo);
523
524         if ((fp = fopen(tmp, "wb")) == NULL) {
525                 FILE_OP_ERROR(tmp, "fopen");
526                 return -1;
527         }
528
529         /* chmod for security */
530         if (change_file_mode_rw(fp, tmp) < 0) {
531                 FILE_OP_ERROR(tmp, "chmod");
532                 g_warning("can't change file mode\n");
533         }
534
535         /* write queue headers */
536         fprintf(fp, "AF:\n");
537         fprintf(fp, "NF:0\n");
538         fprintf(fp, "PS:10\n");
539         fprintf(fp, "SRH:1\n");
540         fprintf(fp, "SFN:\n");
541         fprintf(fp, "DSR:\n");
542         fprintf(fp, "MID:\n");
543         fprintf(fp, "CFG:\n");
544         fprintf(fp, "PT:0\n");
545         fprintf(fp, "S:%s\n", account->address);
546         fprintf(fp, "RQ:\n");
547         if (account->smtp_server)
548                 fprintf(fp, "SSV:%s\n", account->smtp_server);
549         else
550                 fprintf(fp, "SSV:\n");
551         fprintf(fp, "SSH:\n");
552         fprintf(fp, "R:<%s>\n", to);
553         
554         /* check whether we need to save the message */
555         outbox = account_get_special_folder(account, F_OUTBOX); 
556         if (folder_get_default_outbox() == outbox && !prefs_common.savemsg)
557                 outbox = NULL;
558         if (outbox) {
559                 path = folder_item_get_identifier(outbox);
560                 fprintf(fp, "SCF:%s\n", path);
561                 g_free(path);
562         }               
563
564         fprintf(fp, "\n");
565         
566         /* Date */
567         get_rfc822_date(buf, sizeof(buf));
568         fprintf(fp, "Date: %s\n", buf);
569
570         /* From */
571         if (account->name && *account->name) {
572                 notification_convert_header
573                         (buf, sizeof(buf), account->name,
574                          strlen("From: "));
575                 fprintf(fp, "From: %s <%s>\n", buf, account->address);
576         } else
577                 fprintf(fp, "From: %s\n", account->address);
578
579         fprintf(fp, "To: %s\n", to);
580
581         /* Subject */
582         notification_convert_header(buf, sizeof(buf), msginfo->subject,
583                                     strlen("Subject: "));
584         fprintf(fp, "Subject: Disposition notification: %s\n", buf);
585
586         /* Message ID */
587         generate_msgid(account->address, buf, sizeof buf);
588         fprintf(fp, "Message-Id: <%s>\n", buf);
589
590         if (fclose(fp) == EOF) {
591                 FILE_OP_ERROR(tmp, "fclose");
592                 unlink(tmp);
593                 return -1;
594         }
595
596         /* put it in queue */
597         queue = account_get_special_folder(account, F_QUEUE);
598         if (!queue) queue = folder_get_default_queue();
599         if (!queue) {
600                 g_warning("can't find queue folder\n");
601                 unlink(tmp);
602                 return -1;
603         }
604         folder_item_scan(queue);
605         if ((num = folder_item_add_msg(queue, tmp, NULL, TRUE)) < 0) {
606                 g_warning("can't queue the message\n");
607                 unlink(tmp);
608                 return -1;
609         }
610         
611         /* send it */
612         path = folder_item_fetch_msg(queue, num);
613         ok = procmsg_send_message_queue(path);
614         g_free(path);
615         folder_item_remove_msg(queue, num);
616
617         return ok;
618 }
619
620 GList *messageview_get_window_list(void)
621 {
622         return messageview_list;
623 }
624
625 void messageview_show(MessageView *messageview, MsgInfo *msginfo,
626                       gboolean all_headers)
627 {
628         gchar *file;
629         MimeInfo *mimeinfo;
630
631         g_return_if_fail(msginfo != NULL);
632
633         mimeinfo = procmime_scan_message(msginfo);
634         g_return_if_fail(mimeinfo != NULL);
635
636         file = procmsg_get_message_file_path(msginfo);
637         if (!file) {
638                 g_warning("can't get message file path.\n");
639                 procmime_mimeinfo_free_all(mimeinfo);
640                 return;
641         }
642
643         if (messageview->msginfo != msginfo) {
644                 procmsg_msginfo_free(messageview->msginfo);
645                 messageview->msginfo = procmsg_msginfo_get_full_info(msginfo);
646         }
647         headerview_show(messageview->headerview, messageview->msginfo);
648
649         messageview->all_headers = all_headers;
650         textview_set_all_headers(messageview->textview, all_headers);
651         textview_set_all_headers(messageview->mimeview->textview, all_headers);
652
653         if (mimeinfo->mime_type != MIME_TEXT) {
654                 messageview_change_view_type(messageview, MVIEW_MIME);
655                 mimeview_show_message(messageview->mimeview, mimeinfo, file);
656         } else {
657                 messageview_change_view_type(messageview, MVIEW_TEXT);
658                 textview_show_message(messageview->textview, mimeinfo, file);
659                 procmime_mimeinfo_free_all(mimeinfo);
660         }
661
662         if ((messageview->msginfo->dispositionnotificationto || 
663              messageview->msginfo->returnreceiptto) &&
664             !MSG_IS_RETRCPT_SENT(messageview->msginfo->flags))
665                 return_receipt_show(messageview->noticeview, messageview->msginfo);
666         else 
667                 noticeview_hide(messageview->noticeview);
668
669         g_free(file);
670 }
671
672 static void messageview_change_view_type(MessageView *messageview,
673                                          MessageType type)
674 {
675         TextView *textview = messageview->textview;
676         MimeView *mimeview = messageview->mimeview;
677         gboolean hadfocus  = GTK_WIDGET_HAS_FOCUS(textview->text);
678
679         if (messageview->type == type) return;
680
681         if (type == MVIEW_MIME) {
682                 gtkut_container_remove
683                         (GTK_CONTAINER(GTK_WIDGET_PTR(messageview)),
684                          GTK_WIDGET_PTR(textview));
685                 gtk_box_pack_start(GTK_BOX(messageview->vbox),
686                                    GTK_WIDGET_PTR(mimeview), TRUE, TRUE, 0);
687                 gtk_container_add(GTK_CONTAINER(mimeview->vbox),
688                                   GTK_WIDGET_PTR(textview));
689         } else if (type == MVIEW_TEXT) {
690                 gtkut_container_remove
691                         (GTK_CONTAINER(GTK_WIDGET_PTR(messageview)),
692                          GTK_WIDGET_PTR(mimeview));
693
694                 if (mimeview->vbox == GTK_WIDGET_PTR(textview)->parent)
695                         gtkut_container_remove(GTK_CONTAINER(mimeview->vbox),
696                                                GTK_WIDGET_PTR(textview));
697
698                 gtk_box_pack_start(GTK_BOX(messageview->vbox),
699                                    GTK_WIDGET_PTR(textview), TRUE, TRUE, 0);
700         } else
701                 return;
702         if (hadfocus) gtk_widget_grab_focus(textview->text);
703
704         messageview->type = type;
705 }
706
707 void messageview_reflect_prefs_pixmap_theme(void)
708 {
709         GList *cur;
710         MessageView *msgview;
711
712         for (cur = msgview_list; cur != NULL; cur = cur->next) {
713                 msgview = (MessageView*)cur->data;
714                 toolbar_update(TOOLBAR_MSGVIEW, msgview);
715                 mimeview_update(msgview->mimeview);
716         }
717 }
718
719 void messageview_clear(MessageView *messageview)
720 {
721         procmsg_msginfo_free(messageview->msginfo);
722         messageview->msginfo = NULL;
723         messageview_change_view_type(messageview, MVIEW_TEXT);
724         messageview->filtered = FALSE;
725         headerview_clear(messageview->headerview);
726         textview_clear(messageview->textview);
727         noticeview_hide(messageview->noticeview);
728 }
729
730 void messageview_destroy(MessageView *messageview)
731 {
732         GtkWidget *textview  = GTK_WIDGET_PTR(messageview->textview);
733         GtkWidget *mimeview  = GTK_WIDGET_PTR(messageview->mimeview);
734
735         debug_print("destroy messageview\n");
736         messageview_list = g_list_remove(messageview_list, messageview);
737
738         headerview_destroy(messageview->headerview);
739         textview_destroy(messageview->textview);
740         mimeview_destroy(messageview->mimeview);
741         noticeview_destroy(messageview->noticeview);
742
743         procmsg_msginfo_free(messageview->msginfo);
744         toolbar_clear_list(TOOLBAR_MSGVIEW);
745         if (messageview->toolbar) {
746                 toolbar_destroy(messageview->toolbar);
747                 g_free(messageview->toolbar);
748         }
749         
750         msgview_list = g_list_remove(msgview_list, messageview); 
751
752         g_free(messageview);
753
754         gtk_widget_unref(textview);
755         gtk_widget_unref(mimeview);
756 }
757
758 void messageview_delete(MessageView *msgview)
759 {
760         MsgInfo *msginfo = (MsgInfo*)msgview->msginfo;
761         FolderItem *trash = folder_get_default_trash();
762         GSList *msg_list;
763
764         g_return_if_fail(msginfo != NULL);
765         g_return_if_fail(trash   != NULL);
766         
767         msg_list = folder_item_get_msg_list(msginfo->folder);
768         
769         if (msg_list == NULL) {
770                 alertpanel_error(_("Message already removed from folder."));
771                 return;
772         }
773         
774         for (; msg_list != NULL; msg_list = msg_list->next) {
775                 MsgInfo *msginfo_list = (MsgInfo*)msg_list->data;
776                 
777                 if (msginfo->msgnum == msginfo_list->msgnum) {
778
779                         if (prefs_common.immediate_exec)
780                                 folder_item_move_msg(trash, msginfo);
781                         else {
782                                 procmsg_msginfo_set_to_folder(msginfo, trash);
783                                 procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
784                                 /* NOTE: does not update to next message in summaryview
785                                  */
786                         }
787                                 
788                         messageview_update_all(msgview);
789                         break;
790                 }
791         }
792 }
793
794 /*      
795  * scan List of MessageViews checking whether there are any Views holding messages 
796  * which need to be updated (another view might have deleted the one this MessagView holds)
797  */
798 static void messageview_update_all(MessageView *msgview)
799 {
800         MsgInfo *msginfo = (MsgInfo*)msgview->msginfo;
801         GList *cur;
802         
803         g_return_if_fail(msginfo != NULL);
804
805         for (cur = msgview_list; cur != NULL; cur = cur->next) {
806                 MessageView *msgview = (MessageView*)cur->data;
807                 MsgInfo *msginfo_list = (MsgInfo*)msgview->msginfo;
808                 
809                 g_return_if_fail(msginfo != NULL);
810
811                 if (msginfo->msgnum == msginfo_list->msgnum)
812                         messageview_update(msgview);
813         }
814 }
815
816 /* 
817  * \brief update messageview with currently selected message in summaryview
818  *        leave unchanged if summaryview is empty
819  * \param pointer to MessageView
820  */     
821 static void messageview_update(MessageView *msgview)
822 {
823         SummaryView *summaryview = (SummaryView*)msgview->mainwin->summaryview;
824
825         g_return_if_fail(summaryview != NULL);
826         
827         if (summaryview->selected) {
828                 GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
829                 MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, 
830                                                       summaryview->selected);
831                 g_return_if_fail(msginfo != NULL);
832
833                 messageview_show(msgview, msginfo, 
834                                  msgview->all_headers);
835         } 
836 }
837
838 void messageview_quote_color_set(void)
839 {
840 }
841
842 void messageview_set_font(MessageView *messageview)
843 {
844         textview_set_font(messageview->textview, NULL);
845 }
846
847 TextView *messageview_get_current_textview(MessageView *messageview)
848 {
849         TextView *text = NULL;
850
851         if (messageview->type == MVIEW_TEXT)
852                 text = messageview->textview;
853         else if (messageview->type == MVIEW_MIME) {
854                 if (gtk_notebook_get_current_page
855                         (GTK_NOTEBOOK(messageview->mimeview->notebook)) == 0)
856                         text = messageview->textview;
857                 else if (messageview->mimeview->type == MIMEVIEW_TEXT)
858                         text = messageview->mimeview->textview;
859         }
860
861         return text;
862 }
863
864 MimeInfo *messageview_get_selected_mime_part(MessageView *messageview)
865 {
866         if (messageview->type == MVIEW_MIME)
867                 return mimeview_get_selected_part(messageview->mimeview);
868
869         return NULL;
870 }
871
872 void messageview_copy_clipboard(MessageView *messageview)
873 {
874         TextView *text;
875
876         text = messageview_get_current_textview(messageview);
877         if (text)
878                 gtk_editable_copy_clipboard(GTK_EDITABLE(text->text));
879 }
880
881 void messageview_select_all(MessageView *messageview)
882 {
883         TextView *text;
884
885         text = messageview_get_current_textview(messageview);
886         if (text)
887                 gtk_editable_select_region(GTK_EDITABLE(text->text), 0, -1);
888 }
889
890 void messageview_set_position(MessageView *messageview, gint pos)
891 {
892         textview_set_position(messageview->textview, pos);
893 }
894
895 gboolean messageview_search_string(MessageView *messageview, const gchar *str,
896                                    gboolean case_sens)
897 {
898         return textview_search_string(messageview->textview, str, case_sens);
899         return FALSE;
900 }
901
902 gboolean messageview_search_string_backward(MessageView *messageview,
903                                             const gchar *str,
904                                             gboolean case_sens)
905 {
906         return textview_search_string_backward(messageview->textview,
907                                                str, case_sens);
908         return FALSE;
909 }
910
911 gboolean messageview_is_visible(MessageView *messageview)
912 {
913         return messageview->visible;
914 }
915
916 void messageview_save_as(MessageView *messageview)
917 {
918         gchar *filename = NULL;
919         MsgInfo *msginfo;
920         gchar *src, *dest;
921
922         if (!messageview->msginfo) return;
923         msginfo = messageview->msginfo;
924
925         if (msginfo->subject) {
926                 Xstrdup_a(filename, msginfo->subject, return);
927                 subst_for_filename(filename);
928         }
929         dest = filesel_select_file(_("Save as"), filename);
930         if (!dest) return;
931         if (is_file_exist(dest)) {
932                 AlertValue aval;
933
934                 aval = alertpanel(_("Overwrite"),
935                                   _("Overwrite existing file?"),
936                                   _("OK"), _("Cancel"), NULL);
937                 if (G_ALERTDEFAULT != aval) return;
938         }
939
940         src = procmsg_get_message_file(msginfo);
941         if (copy_file(src, dest, TRUE) < 0) {
942                 alertpanel_error(_("Can't save the file `%s'."),
943                                  g_basename(dest));
944         }
945         g_free(src);
946 }
947
948 static void messageview_destroy_cb(GtkWidget *widget, MessageView *messageview)
949 {
950         messageview_destroy(messageview);
951 }
952
953 static void messageview_size_allocate_cb(GtkWidget *widget,
954                                          GtkAllocation *allocation)
955 {
956         g_return_if_fail(allocation != NULL);
957
958         prefs_common.msgwin_width  = allocation->width;
959         prefs_common.msgwin_height = allocation->height;
960 }
961
962 static void key_pressed(GtkWidget *widget, GdkEventKey *event,
963                         MessageView *messageview)
964 {
965         if (event && event->keyval == GDK_Escape && messageview->window)
966                 gtk_widget_destroy(messageview->window);
967 }
968
969 void messageview_toggle_view_real(MessageView *messageview)
970 {
971         MainWindow *mainwin = messageview->mainwin;
972         union CompositeWin *cwin = &mainwin->win;
973         GtkWidget *vpaned = NULL;
974         GtkWidget *container = NULL;
975         GtkItemFactory *ifactory = gtk_item_factory_from_widget(mainwin->menubar);
976         
977         switch (mainwin->type) {
978         case SEPARATE_NONE:
979                 vpaned = cwin->sep_none.vpaned;
980                 container = cwin->sep_none.hpaned;
981                 break;
982         case SEPARATE_FOLDER:
983                 vpaned = cwin->sep_folder.vpaned;
984                 container = mainwin->vbox_body;
985                 break;
986         case SEPARATE_MESSAGE:
987         case SEPARATE_BOTH:
988                 return;
989         }
990
991         if (vpaned->parent != NULL) {
992                 gtk_widget_ref(vpaned);
993                 gtkut_container_remove(GTK_CONTAINER(container), vpaned);
994                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), container);
995                 menu_set_sensitive(ifactory, "/View/Expand Summary View", FALSE);
996                 gtk_widget_grab_focus(GTK_WIDGET(messageview->textview->text));
997         } else {
998                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), vpaned);
999                 gtk_container_add(GTK_CONTAINER(container), vpaned);
1000                 gtk_widget_unref(vpaned);
1001                 menu_set_sensitive(ifactory, "/View/Expand Summary View", TRUE);
1002                 gtk_widget_grab_focus(GTK_WIDGET(mainwin->summaryview->ctree));
1003         }
1004 }
1005
1006 static void return_receipt_show(NoticeView *noticeview, MsgInfo *msginfo)
1007 {
1008         noticeview_set_text(noticeview, _("This message asks for a return receipt"));
1009         noticeview_set_button_text(noticeview, _("Send receipt"));
1010         noticeview_set_button_press_callback(noticeview,
1011                                              GTK_SIGNAL_FUNC(return_receipt_send_clicked),
1012                                              (gpointer) msginfo);
1013         noticeview_show(noticeview);
1014 }
1015
1016 static void return_receipt_send_clicked(NoticeView *noticeview, MsgInfo *msginfo)
1017 {
1018         MsgInfo *tmpmsginfo;
1019         gchar *file;
1020
1021         file = procmsg_get_message_file_path(msginfo);
1022         if (!file) {
1023                 g_warning("can't get message file path.\n");
1024                 return;
1025         }
1026
1027         tmpmsginfo = procheader_parse_file(file, msginfo->flags, TRUE, TRUE);
1028         tmpmsginfo->folder = msginfo->folder;
1029         tmpmsginfo->msgnum = msginfo->msgnum;
1030
1031         if (disposition_notification_send(tmpmsginfo) >= 0) {
1032                 procmsg_msginfo_set_flags(msginfo, MSG_RETRCPT_SENT, 0);
1033                 noticeview_hide(noticeview);
1034         }               
1035
1036         procmsg_msginfo_free(tmpmsginfo);
1037         g_free(file);
1038 }
1039
1040 static void select_account_cb(GtkWidget *w, gpointer data)
1041 {
1042         *(gint*)data = GPOINTER_TO_INT(gtk_object_get_user_data(GTK_OBJECT(w)));
1043 }
1044         
1045 static PrefsAccount *select_account_from_list(GList *ac_list)
1046 {
1047         GtkWidget *optmenu;
1048         GtkWidget *menu;
1049         gint account_id;
1050
1051         g_return_val_if_fail(ac_list != NULL, NULL);
1052         g_return_val_if_fail(ac_list->data != NULL, NULL);
1053         
1054         optmenu = gtk_option_menu_new();
1055         menu = gtkut_account_menu_new(ac_list, select_account_cb, &account_id);
1056         if (!menu)
1057                 return NULL;
1058         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), menu);
1059         gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), 0);
1060         account_id = ((PrefsAccount *) ac_list->data)->account_id;
1061         if (alertpanel_with_widget(
1062                                 _("Return Receipt Notification"),
1063                                 _("The message was sent to several of your "
1064                                   "accounts.\n"
1065                                   "Please choose which account do you want to "
1066                                   "use for sending the receipt notification:"),
1067                                 _("Send Notification"), _("+Cancel"), NULL,
1068                                 optmenu) != G_ALERTDEFAULT)
1069                 return NULL;
1070         return account_find_from_id(account_id);
1071 }
1072
1073 /* 
1074  * \brief return selected messageview text, when nothing is 
1075  *        selected and message was filtered, return complete text
1076  *
1077  * \param  pointer to Messageview 
1078  *
1079  * \return pointer to text (needs to be free'd by calling func)
1080  */
1081 gchar *messageview_get_selection(MessageView *msgview)
1082 {
1083         TextView *textview;
1084         gchar *text = NULL;
1085         GtkEditable *edit = NULL;
1086         gint body_pos = 0;
1087         
1088         g_return_val_if_fail(msgview != NULL, NULL);
1089
1090         textview = messageview_get_current_textview(msgview);
1091         if (textview) {
1092                 edit = GTK_EDITABLE(textview->text);
1093                 body_pos = textview->body_pos;
1094         } else {
1095                 edit = GTK_EDITABLE(msgview->mimeview->textview->text);
1096                 body_pos = msgview->mimeview->textview->body_pos;
1097         }
1098
1099         g_return_val_if_fail(edit != NULL, NULL);
1100
1101         if (edit->has_selection)
1102                 text = gtkut_editable_get_selection(edit);
1103         else if (msgview->filtered) 
1104                 text = gtk_editable_get_chars (edit, body_pos, -1);
1105         else
1106                 text = NULL;
1107         
1108         return text;
1109 }
1110
1111 static void messageview_delete_cb(MessageView *msgview, guint action, GtkWidget *widget)
1112 {
1113         messageview_delete(msgview);
1114 }
1115
1116 static void messageview_menubar_cb(MessageView *msgview, guint action, GtkWidget *widget)
1117 {
1118         GSList *msginfo_list = NULL;
1119         gchar *body;
1120         MsgInfo *msginfo;
1121
1122         g_return_if_fail(msgview != NULL);
1123
1124         msginfo = (MsgInfo*)msgview->msginfo;
1125         g_return_if_fail(msginfo != NULL);
1126
1127         msginfo_list = g_slist_append(msginfo_list, msginfo);
1128         g_return_if_fail(msginfo_list);
1129
1130         body =  messageview_get_selection(msgview);
1131         compose_reply_mode((ComposeMode)action, msginfo_list, body);
1132         g_free(body);
1133         g_slist_free(msginfo_list);
1134 }
1135
1136 static void save_as_cb(gpointer data, guint action, GtkWidget *widget)
1137 {
1138         MessageView *messageview = (MessageView *)data;
1139         messageview_save_as(messageview);
1140 }
1141
1142 static void print_cb(gpointer data, guint action, GtkWidget *widget)
1143 {
1144         MessageView *messageview = (MessageView *)data;
1145         gchar *cmdline;
1146         gchar *p;
1147
1148         if (!messageview->msginfo) return;
1149
1150         cmdline = input_dialog(_("Print"),
1151                                _("Enter the print command line:\n"
1152                                  "(`%s' will be replaced with file name)"),
1153                                prefs_common.print_cmd);
1154         if (!cmdline) return;
1155         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
1156             strchr(p + 2, '%')) {
1157                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
1158                                  cmdline);
1159                 g_free(cmdline);
1160                 return;
1161         }
1162
1163         procmsg_print_message(messageview->msginfo, cmdline);
1164         g_free(cmdline);
1165 }
1166
1167 static void close_cb(gpointer data, guint action, GtkWidget *widget)
1168 {
1169         MessageView *messageview = (MessageView *)data;
1170         gtk_widget_destroy(messageview->window);
1171 }
1172
1173 static void copy_cb(gpointer data, guint action, GtkWidget *widget)
1174 {
1175         MessageView *messageview = (MessageView *)data;
1176         messageview_copy_clipboard(messageview);
1177 }
1178
1179 static void allsel_cb(gpointer data, guint action, GtkWidget *widget)
1180 {
1181         MessageView *messageview = (MessageView *)data;
1182         messageview_select_all(messageview);
1183 }
1184
1185 static void search_cb(gpointer data, guint action, GtkWidget *widget)
1186 {
1187         MessageView *messageview = (MessageView *)data;
1188         message_search(messageview);
1189 }
1190
1191 static void set_charset_cb(gpointer data, guint action, GtkWidget *widget)
1192 {
1193         MessageView *messageview = (MessageView *)data;
1194         const gchar *charset;
1195
1196         if (GTK_CHECK_MENU_ITEM(widget)->active) {
1197                 charset = conv_get_charset_str((CharSet)action);
1198                 g_free(messageview->forced_charset);
1199                 messageview->forced_charset = g_strdup(charset);
1200                 messageview_show(messageview, messageview->msginfo, FALSE);
1201         }
1202 }
1203
1204 static void view_source_cb(gpointer data, guint action, GtkWidget *widget)
1205 {
1206         MessageView *messageview = (MessageView *)data;
1207         SourceWindow *srcwin;
1208
1209         if (!messageview->msginfo) return;
1210
1211         srcwin = source_window_create();
1212         source_window_show_msg(srcwin, messageview->msginfo);
1213         source_window_show(srcwin);
1214 }
1215
1216 static void show_all_header_cb(gpointer data, guint action, GtkWidget *widget)
1217 {
1218         MessageView *messageview = (MessageView *)data;
1219         MsgInfo *msginfo = messageview->msginfo;
1220
1221         if (!msginfo) return;
1222         messageview->msginfo = NULL;
1223         messageview_show(messageview, msginfo,
1224                          GTK_CHECK_MENU_ITEM(widget)->active);
1225         procmsg_msginfo_free(msginfo);
1226 }
1227
1228 static void compose_cb(gpointer data, guint action, GtkWidget *widget)
1229 {
1230         MessageView *messageview = (MessageView *)data;
1231         PrefsAccount *ac = NULL;
1232         FolderItem *item = NULL;
1233
1234         if (messageview->msginfo)
1235                 item = messageview->msginfo->folder;
1236
1237         if (item) {
1238                 ac = account_find_from_item(item);
1239                 if (ac && ac->protocol == A_NNTP &&
1240                     item->stype == F_NEWS) {
1241                         compose_new(ac, item->path, NULL);
1242                         return;
1243                 }
1244         }
1245
1246         compose_new(ac, NULL, NULL);
1247 }
1248
1249 static void reply_cb(gpointer data, guint action, GtkWidget *widget)
1250 {
1251         MessageView *messageview = (MessageView *)data;
1252         GSList *mlist = NULL;
1253         MsgInfo *msginfo;
1254         gchar *text = NULL;
1255         ComposeMode mode = (ComposeMode)action;
1256
1257         msginfo = messageview->msginfo;
1258         mlist = g_slist_append(NULL, msginfo);
1259
1260         text = gtkut_editable_get_selection
1261                 (GTK_EDITABLE(messageview->textview->text));
1262         if (text && *text == '\0') {
1263                 g_free(text);
1264                 text = NULL;
1265         }
1266
1267         switch (mode) {
1268         case COMPOSE_REPLY:
1269                 compose_reply(msginfo, prefs_common.reply_with_quote,
1270                               FALSE, prefs_common.default_reply_list, FALSE, text);
1271                 break;
1272         case COMPOSE_REPLY_WITH_QUOTE:
1273                 compose_reply(msginfo, TRUE, FALSE, prefs_common.default_reply_list, FALSE, text);
1274                 break;
1275         case COMPOSE_REPLY_WITHOUT_QUOTE:
1276                 compose_reply(msginfo, FALSE, FALSE, prefs_common.default_reply_list, FALSE, NULL);
1277                 break;
1278         case COMPOSE_REPLY_TO_SENDER:
1279                 compose_reply(msginfo, prefs_common.reply_with_quote,
1280                               FALSE, FALSE, TRUE, text);
1281                 break;
1282         case COMPOSE_FOLLOWUP_AND_REPLY_TO:
1283                 compose_followup_and_reply_to(msginfo,
1284                                               prefs_common.reply_with_quote,
1285                                               FALSE, FALSE, text);
1286                 break;
1287         case COMPOSE_REPLY_TO_SENDER_WITH_QUOTE:
1288                 compose_reply(msginfo, TRUE, FALSE, FALSE, TRUE, text);
1289                 break;
1290         case COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE:
1291                 compose_reply(msginfo, FALSE, FALSE, FALSE, TRUE, NULL);
1292                 break;
1293         case COMPOSE_REPLY_TO_ALL:
1294                 compose_reply(msginfo, prefs_common.reply_with_quote,
1295                               TRUE, FALSE, FALSE, text);
1296                 break;
1297         case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
1298                 compose_reply(msginfo, TRUE, TRUE, FALSE, FALSE, text);
1299                 break;
1300         case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
1301                 compose_reply(msginfo, FALSE, TRUE, FALSE, FALSE, NULL);
1302                 break;
1303         case COMPOSE_REPLY_TO_LIST:
1304                 compose_reply(msginfo, prefs_common.reply_with_quote,
1305                               FALSE, TRUE, FALSE, text);
1306                 break;
1307         case COMPOSE_REPLY_TO_LIST_WITH_QUOTE:
1308                 compose_reply(msginfo, TRUE, FALSE, TRUE, FALSE, text);
1309                 break;
1310         case COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE:
1311                 compose_reply(msginfo, FALSE, FALSE, TRUE, FALSE, NULL);
1312                 break;
1313         case COMPOSE_FORWARD:
1314                 if (prefs_common.forward_as_attachment) {
1315                         compose_reply_mode(COMPOSE_FORWARD_AS_ATTACH, mlist, text);
1316                         return;
1317                 } else {
1318                         compose_reply_mode(COMPOSE_FORWARD_INLINE, mlist, text);
1319                         return;
1320                 }
1321                 break;
1322         case COMPOSE_FORWARD_INLINE:
1323                 compose_forward(NULL, msginfo, FALSE, text);
1324                 break;
1325         case COMPOSE_FORWARD_AS_ATTACH:
1326                 compose_forward_multiple(NULL, mlist);
1327                 break;
1328         case COMPOSE_REDIRECT:
1329                 compose_redirect(NULL, msginfo);
1330                 break;
1331         default:
1332                 g_warning("compose_reply(): invalid Compose Mode: %d\n", mode);
1333         }
1334
1335         /* summary_set_marks_selected(summaryview); */
1336         g_free(text);
1337         g_slist_free(mlist);
1338 }
1339
1340 static void reedit_cb(gpointer data, guint action, GtkWidget *widget)
1341 {
1342         MessageView *messageview = (MessageView *)data;
1343         MsgInfo *msginfo;
1344
1345         if (!messageview->msginfo) return;
1346         msginfo = messageview->msginfo;
1347         if (!msginfo->folder) return;
1348         if (msginfo->folder->stype != F_OUTBOX &&
1349             msginfo->folder->stype != F_DRAFT &&
1350             msginfo->folder->stype != F_QUEUE) return;
1351
1352         compose_reedit(msginfo);
1353 }
1354
1355 static void addressbook_open_cb(gpointer data, guint action, GtkWidget *widget)
1356 {
1357         addressbook_open(NULL);
1358 }
1359
1360 static void add_address_cb(gpointer data, guint action, GtkWidget *widget)
1361 {
1362         MessageView *messageview = (MessageView *)data;
1363         MsgInfo *msginfo;
1364         gchar *from;
1365
1366         if (!messageview->msginfo) return;
1367         msginfo = messageview->msginfo;
1368         Xstrdup_a(from, msginfo->from, return);
1369         eliminate_address_comment(from);
1370         extract_address(from);
1371         addressbook_add_contact(msginfo->fromname, from, NULL);
1372 }
1373
1374 static void create_filter_cb(gpointer data, guint action, GtkWidget *widget)
1375 {
1376         MessageView *messageview = (MessageView *)data;
1377         gchar *header = NULL;
1378         gchar *key = NULL;
1379
1380         if (!messageview->msginfo) return;
1381
1382         procmsg_get_filter_keyword(messageview->msginfo, &header, &key,
1383                                    (PrefsFilterType)action);
1384         prefs_filtering_open(NULL, header, key);
1385
1386         g_free(header);
1387         g_free(key);
1388 }
1389
1390 static void about_cb(gpointer data, guint action, GtkWidget *widget)
1391 {
1392         about_show();
1393 }