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