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