5b50a60199642551d15bb78658d63086a8323072
[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 "partial_download.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 partial_recv_unmark_clicked (NoticeView     *noticeview, 
88                                          MsgInfo        *msginfo);
89 static void save_as_cb                  (gpointer        data,
90                                          guint           action,
91                                          GtkWidget      *widget);
92 static void print_cb                    (gpointer        data,
93                                          guint           action,
94                                          GtkWidget      *widget);
95 static void close_cb                    (gpointer        data,
96                                          guint           action,
97                                          GtkWidget      *widget);
98 static void copy_cb                     (gpointer        data,
99                                          guint           action,
100                                          GtkWidget      *widget);
101 static void allsel_cb                   (gpointer        data,
102                                          guint           action,
103                                          GtkWidget      *widget);
104 static void search_cb                   (gpointer        data,
105                                          guint           action,
106                                          GtkWidget      *widget);
107
108 static void set_charset_cb              (gpointer        data,
109                                          guint           action,
110                                          GtkWidget      *widget);
111 static void view_source_cb              (gpointer        data,
112                                          guint           action,
113                                          GtkWidget      *widget);
114 static void show_all_header_cb          (gpointer        data,
115                                          guint           action,
116                                          GtkWidget      *widget);
117
118 static void compose_cb                  (gpointer        data,
119                                          guint           action,
120                                          GtkWidget      *widget);
121 static void reply_cb                    (gpointer        data,
122                                          guint           action,
123                                          GtkWidget      *widget);
124 static void reedit_cb                   (gpointer        data,
125                                          guint           action,
126                                          GtkWidget      *widget);
127
128 static PrefsAccount *select_account_from_list
129                                         (GList          *ac_list);
130 static void addressbook_open_cb         (gpointer        data,
131                                          guint           action,
132                                          GtkWidget      *widget);
133 static void add_address_cb              (gpointer        data,
134                                          guint           action,
135                                          GtkWidget      *widget);
136 static void create_filter_cb            (gpointer        data,
137                                          guint           action,
138                                          GtkWidget      *widget);
139 static void create_processing_cb        (gpointer        data,
140                                          guint           action,
141                                          GtkWidget      *widget);
142
143 static void about_cb                    (gpointer        data,
144                                          guint           action,
145                                          GtkWidget      *widget);
146 static void messageview_update          (MessageView *msgview);
147 static gboolean messageview_update_msg  (gpointer source, gpointer data);
148
149 static GList *msgview_list = NULL;
150 static GtkItemFactoryEntry msgview_entries[] =
151 {
152         {N_("/_File"),                  NULL, NULL, 0, "<Branch>"},
153         {N_("/_File/_Save as..."),      NULL, save_as_cb, 0, NULL},
154         {N_("/_File/_Print..."),        NULL, print_cb, 0, NULL},
155         {N_("/_File/---"),              NULL, NULL, 0, "<Separator>"},
156         {N_("/_File/_Close"),           NULL, close_cb, 0, NULL},
157
158         {N_("/_Edit"),                  NULL, NULL, 0, "<Branch>"},
159         {N_("/_Edit/_Copy"),            NULL, copy_cb, 0, NULL},
160         {N_("/_Edit/Select _all"),      NULL, allsel_cb, 0, NULL},
161         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
162         {N_("/_Edit/_Find in current message..."),
163                                         NULL, search_cb, 0, NULL},
164
165         {N_("/_View"),                  NULL, NULL, 0, "<Branch>"},
166
167 #define CODESET_SEPARATOR \
168         {N_("/_View/_Code set/---"),    NULL, NULL, 0, "<Separator>"}
169 #define CODESET_ACTION(action) \
170         NULL, set_charset_cb, action, "/View/Code set/Auto detect"
171
172         {N_("/_View/_Code set"),        NULL, NULL, 0, "<Branch>"},
173         {N_("/_View/_Code set/_Auto detect"),
174                                         NULL, set_charset_cb, C_AUTO, "<RadioItem>"},
175         CODESET_SEPARATOR,
176         {N_("/_View/_Code set/7bit ascii (US-ASC_II)"),
177          CODESET_ACTION(C_US_ASCII)},
178
179 #if HAVE_ICONV
180         {N_("/_View/_Code set/Unicode (_UTF-8)"),
181          CODESET_ACTION(C_UTF_8)},
182         CODESET_SEPARATOR,
183 #endif
184         {N_("/_View/_Code set/Western European (ISO-8859-_1)"),
185          CODESET_ACTION(C_ISO_8859_1)},
186         {N_("/_View/_Code set/Western European (ISO-8859-15)"),
187          CODESET_ACTION(C_ISO_8859_15)},
188         CODESET_SEPARATOR,
189 #if HAVE_ICONV
190         {N_("/_View/_Code set/Central European (ISO-8859-_2)"),
191          CODESET_ACTION(C_ISO_8859_2)},
192         CODESET_SEPARATOR,
193         {N_("/_View/_Code set/_Baltic (ISO-8859-13)"),
194          CODESET_ACTION(C_ISO_8859_13)},
195         {N_("/_View/_Code set/Baltic (ISO-8859-_4)"),
196          CODESET_ACTION(C_ISO_8859_4)},
197         CODESET_SEPARATOR,
198         {N_("/_View/_Code set/Greek (ISO-8859-_7)"),
199          CODESET_ACTION(C_ISO_8859_7)},
200         CODESET_SEPARATOR,
201         {N_("/_View/_Code set/Turkish (ISO-8859-_9)"),
202          CODESET_ACTION(C_ISO_8859_9)},
203         CODESET_SEPARATOR,
204         {N_("/_View/_Code set/Cyrillic (ISO-8859-_5)"),
205          CODESET_ACTION(C_ISO_8859_5)},
206         {N_("/_View/_Code set/Cyrillic (KOI8-_R)"),
207          CODESET_ACTION(C_KOI8_R)},
208         {N_("/_View/_Code set/Cyrillic (KOI8-U)"),
209          CODESET_ACTION(C_KOI8_U)},
210         {N_("/_View/_Code set/Cyrillic (Windows-1251)"),
211          CODESET_ACTION(C_CP1251)},
212         CODESET_SEPARATOR,
213 #endif
214         {N_("/_View/_Code set/Japanese (ISO-2022-_JP)"),
215          CODESET_ACTION(C_ISO_2022_JP)},
216 #if HAVE_ICONV
217         {N_("/_View/_Code set/Japanese (ISO-2022-JP-2)"),
218          CODESET_ACTION(C_ISO_2022_JP_2)},
219 #endif
220         {N_("/_View/_Code set/Japanese (_EUC-JP)"),
221          CODESET_ACTION(C_EUC_JP)},
222         {N_("/_View/_Code set/Japanese (_Shift__JIS)"),
223          CODESET_ACTION(C_SHIFT_JIS)},
224 #if HAVE_ICONV
225         CODESET_SEPARATOR,
226         {N_("/_View/_Code set/Simplified Chinese (_GB2312)"),
227          CODESET_ACTION(C_GB2312)},
228         {N_("/_View/_Code set/Traditional Chinese (_Big5)"),
229          CODESET_ACTION(C_BIG5)},
230         {N_("/_View/_Code set/Traditional Chinese (EUC-_TW)"),
231          CODESET_ACTION(C_EUC_TW)},
232         {N_("/_View/_Code set/Chinese (ISO-2022-_CN)"),
233          CODESET_ACTION(C_ISO_2022_CN)},
234         CODESET_SEPARATOR,
235         {N_("/_View/_Code set/Korean (EUC-_KR)"),
236          CODESET_ACTION(C_EUC_KR)},
237         {N_("/_View/_Code set/Korean (ISO-2022-KR)"),
238          CODESET_ACTION(C_ISO_2022_KR)},
239         CODESET_SEPARATOR,
240         {N_("/_View/_Code set/Thai (TIS-620)"),
241          CODESET_ACTION(C_TIS_620)},
242         {N_("/_View/_Code set/Thai (Windows-874)"),
243          CODESET_ACTION(C_WINDOWS_874)},
244 #endif
245
246 #undef CODESET_SEPARATOR
247 #undef CODESET_ACTION
248
249         {N_("/_View/---"),              NULL, NULL, 0, "<Separator>"},
250         {N_("/_View/Mess_age source"),  NULL, view_source_cb, 0, NULL},
251         {N_("/_View/Show all _headers"),NULL, show_all_header_cb, 0, "<ToggleItem>"},
252
253         {N_("/_Message"),               NULL, NULL, 0, "<Branch>"},
254         {N_("/_Message/Compose _new message"),
255                                         NULL, compose_cb, 0, NULL},
256         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
257         {N_("/_Message/_Reply"),        NULL, reply_cb, COMPOSE_REPLY, NULL},
258         {N_("/_Message/Repl_y to/_all"),
259                                         NULL, reply_cb, COMPOSE_REPLY_TO_ALL, NULL},
260         {N_("/_Message/Repl_y to/_sender"),
261                                         NULL, reply_cb, COMPOSE_REPLY_TO_SENDER, NULL},
262         {N_("/_Message/Repl_y to/mailing _list"),
263                                         NULL, reply_cb, COMPOSE_REPLY_TO_LIST, NULL},
264         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
265         {N_("/_Message/_Forward"),      NULL, reply_cb, COMPOSE_FORWARD, NULL},
266         {N_("/_Message/For_ward as attachment"),
267                                         NULL, reply_cb, COMPOSE_FORWARD_AS_ATTACH, NULL},
268         {N_("/_Message/Redirec_t"),     NULL, reply_cb, COMPOSE_REDIRECT, NULL},
269         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
270         {N_("/_Message/Re-_edit"),      NULL, reedit_cb, 0, NULL},
271
272         {N_("/_Tools"),                 NULL, NULL, 0, "<Branch>"},
273         {N_("/_Tools/_Address book"),   NULL, addressbook_open_cb, 0, NULL},
274         {N_("/_Tools/Add sender to address boo_k"),
275                                         NULL, add_address_cb, 0, NULL},
276         {N_("/_Tools/---"),             NULL, NULL, 0, "<Separator>"},
277         {N_("/_Tools/_Create filter rule"),
278                                         NULL, NULL, 0, "<Branch>"},
279         {N_("/_Tools/_Create filter rule/_Automatically"),
280                                         NULL, create_filter_cb, FILTER_BY_AUTO, NULL},
281         {N_("/_Tools/_Create filter rule/by _From"),
282                                         NULL, create_filter_cb, FILTER_BY_FROM, NULL},
283         {N_("/_Tools/_Create filter rule/by _To"),
284                                         NULL, create_filter_cb, FILTER_BY_TO, NULL},
285         {N_("/_Tools/_Create filter rule/by _Subject"),
286                                         NULL, create_filter_cb, FILTER_BY_SUBJECT, NULL},
287         {N_("/_Tools/Create processing rule/"),
288                                         NULL, NULL, 0, "<Branch>"},
289         {N_("/_Tools/Create processing rule/_Automatically"),
290                                         NULL, create_processing_cb, FILTER_BY_AUTO, NULL},
291         {N_("/_Tools/Create processing rule/by _From"),
292                                         NULL, create_processing_cb, FILTER_BY_FROM, NULL},
293         {N_("/_Tools/Create processing rule/by _To"),
294                                         NULL, create_processing_cb, FILTER_BY_TO, NULL},
295         {N_("/_Tools/Create processing rule/by _Subject"),
296                                         NULL, create_processing_cb, FILTER_BY_SUBJECT, NULL},
297         {N_("/_Tools/---"),             NULL, NULL, 0, "<Separator>"},
298         {N_("/_Tools/Actio_ns"),        NULL, NULL, 0, "<Branch>"},
299
300         {N_("/_Help"),                  NULL, NULL, 0, "<Branch>"},
301         {N_("/_Help/_About"),           NULL, about_cb, 0, NULL}
302 };
303
304 MessageView *messageview_create(MainWindow *mainwin)
305 {
306         MessageView *messageview;
307         GtkWidget *vbox;
308         HeaderView *headerview;
309         MimeView *mimeview;
310         NoticeView *noticeview;
311
312         debug_print("Creating message view...\n");
313         messageview = g_new0(MessageView, 1);
314
315         headerview = headerview_create();
316
317         noticeview = noticeview_create(mainwin);
318
319         mimeview = mimeview_create(mainwin);
320         mimeview->textview = textview_create();
321         mimeview->textview->messageview = messageview;
322         mimeview->messageview = messageview;
323
324         vbox = gtk_vbox_new(FALSE, 0);
325         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(headerview),
326                            FALSE, FALSE, 0);
327         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(noticeview),
328                            FALSE, FALSE, 0);
329         gtk_box_pack_start(GTK_BOX(vbox),
330                            GTK_WIDGET_PTR(mimeview), TRUE, TRUE, 0);
331         gtk_widget_show(vbox);
332
333         messageview->vbox        = vbox;
334         messageview->new_window  = FALSE;
335         messageview->window      = NULL;
336         messageview->headerview  = headerview;
337         messageview->mimeview    = mimeview;
338         messageview->noticeview = noticeview;
339         messageview->mainwin    = mainwin;
340
341         messageview->statusbar     = NULL;
342         messageview->statusbar_cid = 0;
343
344         messageview->msginfo_update_callback_id =
345                 hooks_register_hook(MSGINFO_UPDATE_HOOKLIST, messageview_update_msg, (gpointer) messageview);
346
347         return messageview;
348 }
349
350 GList *messageview_get_msgview_list(void)
351 {
352         return msgview_list;
353 }
354
355 void messageview_update_actions_menu(MessageView *msgview)
356 {
357         GtkItemFactory *ifactory;
358
359         /* Messages opened in a new window do not have a menu bar */
360         if (msgview->menubar == NULL)
361                 return;
362         ifactory = gtk_item_factory_from_widget(msgview->menubar);
363         action_update_msgview_menu(ifactory, "/Tools/Actions", msgview);
364 }
365
366 void messageview_add_toolbar(MessageView *msgview, GtkWidget *window) 
367 {
368         GtkWidget *handlebox;
369         GtkWidget *vbox;
370         GtkWidget *menubar;
371         GtkWidget *statusbar;
372         guint n_menu_entries;
373
374         vbox = gtk_vbox_new(FALSE, 0);
375         gtk_widget_show(vbox);
376         gtk_container_add(GTK_CONTAINER(window), vbox); 
377
378         n_menu_entries = sizeof(msgview_entries) / sizeof(msgview_entries[0]);
379         menubar = menubar_create(window, msgview_entries,
380                                  n_menu_entries, "<MessageView>", msgview);
381         gtk_widget_show(menubar);
382         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
383
384         handlebox = gtk_handle_box_new();
385         gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
386         msgview->toolbar = toolbar_create(TOOLBAR_MSGVIEW, handlebox,
387                                           (gpointer)msgview);
388
389         statusbar = gtk_statusbar_new();
390         gtk_widget_show(statusbar);
391         gtk_box_pack_end(GTK_BOX(vbox), statusbar, FALSE, FALSE, 0);
392         msgview->statusbar = statusbar;
393         msgview->statusbar_cid = gtk_statusbar_get_context_id
394                 (GTK_STATUSBAR(statusbar), "Message View");
395
396         msgview->handlebox = handlebox;
397         msgview->menubar   = menubar;
398
399         gtk_container_add(GTK_CONTAINER(vbox),
400                           GTK_WIDGET_PTR(msgview));
401
402         messageview_update_actions_menu(msgview);
403
404         msgview_list = g_list_append(msgview_list, msgview);
405 }
406
407 MessageView *messageview_create_with_new_window(MainWindow *mainwin)
408 {
409         MessageView *msgview;
410         GtkWidget *window;
411
412         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
413         gtk_window_set_title(GTK_WINDOW(window), _("Sylpheed - Message View"));
414         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
415         gtk_widget_set_size_request(window, prefs_common.msgwin_width,
416                                     prefs_common.msgwin_height);
417
418         msgview = messageview_create(mainwin);
419
420         g_signal_connect(G_OBJECT(window), "size_allocate",
421                          G_CALLBACK(messageview_size_allocate_cb),
422                          msgview);
423         g_signal_connect(G_OBJECT(window), "destroy",
424                          G_CALLBACK(messageview_destroy_cb), msgview);
425         g_signal_connect(G_OBJECT(window), "key_press_event",
426                          G_CALLBACK(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         messageview_set_position(messageview, 0);
728
729         if (messageview->msginfo->partial_recv)
730                 partial_recv_show(messageview->noticeview, 
731                                   messageview->msginfo);
732         else if ((messageview->msginfo->dispositionnotificationto || 
733              messageview->msginfo->returnreceiptto) &&
734             !MSG_IS_RETRCPT_SENT(messageview->msginfo->flags))
735                 return_receipt_show(messageview->noticeview, 
736                                     messageview->msginfo);
737         else 
738                 noticeview_hide(messageview->noticeview);
739
740         g_free(file);
741
742         return 0;
743 }
744
745 void messageview_reflect_prefs_pixmap_theme(void)
746 {
747         GList *cur;
748         MessageView *msgview;
749
750         for (cur = msgview_list; cur != NULL; cur = cur->next) {
751                 msgview = (MessageView*)cur->data;
752                 toolbar_update(TOOLBAR_MSGVIEW, msgview);
753                 mimeview_update(msgview->mimeview);
754         }
755 }
756
757 void messageview_clear(MessageView *messageview)
758 {
759         procmsg_msginfo_free(messageview->msginfo);
760         messageview->msginfo = NULL;
761         messageview->filtered = FALSE;
762         mimeview_clear(messageview->mimeview);
763         headerview_clear(messageview->headerview);
764         noticeview_hide(messageview->noticeview);
765 }
766
767 void messageview_destroy(MessageView *messageview)
768 {
769         debug_print("destroy messageview\n");
770         messageview_list = g_list_remove(messageview_list, messageview);
771
772         hooks_unregister_hook(MSGINFO_UPDATE_HOOKLIST,
773                               messageview->msginfo_update_callback_id);
774
775         headerview_destroy(messageview->headerview);
776         mimeview_destroy(messageview->mimeview);
777         noticeview_destroy(messageview->noticeview);
778
779         procmsg_msginfo_free(messageview->msginfo);
780         toolbar_clear_list(TOOLBAR_MSGVIEW);
781         if (messageview->toolbar) {
782                 toolbar_destroy(messageview->toolbar);
783                 g_free(messageview->toolbar);
784         }
785         
786         msgview_list = g_list_remove(msgview_list, messageview); 
787
788         g_free(messageview);
789 }
790
791 void messageview_delete(MessageView *msgview)
792 {
793         MsgInfo *msginfo = NULL;
794         FolderItem *trash = NULL;
795         PrefsAccount *ac = NULL;
796
797         if (msgview->msginfo && msgview->mainwin && msgview->mainwin->summaryview)
798                 msginfo = summary_get_selected_msg(msgview->mainwin->summaryview);
799         
800         /* need a procmsg_msginfo_equal() */
801         if (msginfo && msgview->msginfo && 
802             msginfo->msgnum == msgview->msginfo->msgnum && 
803             msginfo->folder == msgview->msginfo->folder) {
804                 summary_delete(msgview->mainwin->summaryview);
805         } else {                
806                 msginfo = msgview->msginfo;
807
808                 g_return_if_fail(msginfo != NULL);
809
810                 /* to get the trash folder, we have to choose either
811                  * the folder's or account's trash default - we prefer
812                  * the one in the account prefs */
813                 if (msginfo->folder) {
814                         if (NULL != (ac = account_find_from_item(msginfo->folder)))
815                                 trash = account_get_special_folder(ac, F_TRASH);
816                         if (!trash && msginfo->folder->folder)  
817                                 trash = msginfo->folder->folder->trash;
818                         /* if still not found, use the default */
819                         if (!trash) 
820                                 trash = folder_get_default_trash();
821                 }       
822
823                 g_return_if_fail(trash != NULL);
824
825                 if (prefs_common.immediate_exec)
826                         /* TODO: Delete from trash */
827                         folder_item_move_msg(trash, msginfo);
828                 else {
829                         procmsg_msginfo_set_to_folder(msginfo, trash);
830                         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
831                         /* NOTE: does not update to next message in summaryview */
832                 }
833         }               
834 }
835
836 /* 
837  * \brief update messageview with currently selected message in summaryview
838  *        leave unchanged if summaryview is empty
839  * \param pointer to MessageView
840  */     
841 static void messageview_update(MessageView *msgview)
842 {
843         SummaryView *summaryview = (SummaryView*)msgview->mainwin->summaryview;
844
845         g_return_if_fail(summaryview != NULL);
846         
847         if (summaryview->selected) {
848                 GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
849                 MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, 
850                                                       summaryview->selected);
851                 g_return_if_fail(msginfo != NULL);
852
853                 messageview_show(msgview, msginfo, 
854                                  msgview->all_headers);
855         } 
856 }
857
858 void messageview_quote_color_set(void)
859 {
860 }
861
862 void messageview_set_font(MessageView *messageview)
863 {
864 }
865
866 TextView *messageview_get_current_textview(MessageView *messageview)
867 {
868         TextView *text = NULL;
869
870         text = messageview->mimeview->textview;
871
872         return text;
873 }
874
875 MimeInfo *messageview_get_selected_mime_part(MessageView *messageview)
876 {
877         return mimeview_get_selected_part(messageview->mimeview);
878 }
879
880 void messageview_copy_clipboard(MessageView *messageview)
881 {
882         TextView *text;
883
884         text = messageview_get_current_textview(messageview);
885         if (text) {
886                 GtkTextView *textview = GTK_TEXT_VIEW(text->text);
887                 GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
888                 GtkClipboard *clipboard
889                         = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
890
891                 gtk_text_buffer_copy_clipboard(buffer, clipboard);
892         }
893 }
894
895 void messageview_select_all(MessageView *messageview)
896 {
897         TextView *text;
898
899         text = messageview_get_current_textview(messageview);
900         if (text)
901                 gtk_editable_select_region(GTK_EDITABLE(text->text), 0, -1);
902 }
903
904 void messageview_set_position(MessageView *messageview, gint pos)
905 {
906         TextView *text;
907
908         text = messageview_get_current_textview(messageview);
909         if (text)
910                 textview_set_position(text, pos);
911 }
912
913 gboolean messageview_search_string(MessageView *messageview, const gchar *str,
914                                    gboolean case_sens)
915 {
916         TextView *text;
917
918         text = messageview_get_current_textview(messageview);
919         if (text)
920                 return textview_search_string(text, str, case_sens);
921         return FALSE;
922 }
923
924 gboolean messageview_search_string_backward(MessageView *messageview,
925                                             const gchar *str,
926                                             gboolean case_sens)
927 {
928         TextView *text;
929
930         text = messageview_get_current_textview(messageview);
931         if (text)       
932                 return textview_search_string_backward(text,
933                                                        str, case_sens);
934         return FALSE;
935 }
936
937 gboolean messageview_is_visible(MessageView *messageview)
938 {
939         return messageview->visible;
940 }
941
942 void messageview_save_as(MessageView *messageview)
943 {
944         gchar *filename = NULL;
945         MsgInfo *msginfo;
946         gchar *src, *dest;
947
948         if (!messageview->msginfo) return;
949         msginfo = messageview->msginfo;
950
951         if (msginfo->subject) {
952                 Xstrdup_a(filename, msginfo->subject, return);
953                 subst_for_filename(filename);
954         }
955         dest = filesel_select_file_save(_("Save as"), filename);
956         if (!dest) return;
957         if (is_file_exist(dest)) {
958                 AlertValue aval;
959
960                 aval = alertpanel(_("Overwrite"),
961                                   _("Overwrite existing file?"),
962                                   _("OK"), _("Cancel"), NULL);
963                 if (G_ALERTDEFAULT != aval) return;
964         }
965
966         src = procmsg_get_message_file(msginfo);
967         if (copy_file(src, dest, TRUE) < 0) {
968                 alertpanel_error(_("Can't save the file `%s'."),
969                                  g_basename(dest));
970         }
971         g_free(src);
972 }
973
974 static void messageview_destroy_cb(GtkWidget *widget, MessageView *messageview)
975 {
976         messageview_destroy(messageview);
977 }
978
979 static void messageview_size_allocate_cb(GtkWidget *widget,
980                                          GtkAllocation *allocation)
981 {
982         g_return_if_fail(allocation != NULL);
983
984         prefs_common.msgwin_width  = allocation->width;
985         prefs_common.msgwin_height = allocation->height;
986 }
987
988 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
989                         MessageView *messageview)
990 {
991         if (event && event->keyval == GDK_Escape && messageview->window)
992                 gtk_widget_destroy(messageview->window);
993         return FALSE;
994 }
995
996 void messageview_toggle_view_real(MessageView *messageview)
997 {
998         MainWindow *mainwin = messageview->mainwin;
999         union CompositeWin *cwin = &mainwin->win;
1000         GtkWidget *vpaned = NULL;
1001         GtkWidget *container = NULL;
1002         GtkItemFactory *ifactory = gtk_item_factory_from_widget(mainwin->menubar);
1003         
1004         switch (mainwin->type) {
1005         case SEPARATE_NONE:
1006                 vpaned = cwin->sep_none.vpaned;
1007                 container = cwin->sep_none.hpaned;
1008                 break;
1009         case SEPARATE_FOLDER:
1010                 vpaned = cwin->sep_folder.vpaned;
1011                 container = mainwin->vbox_body;
1012                 break;
1013         case SEPARATE_MESSAGE:
1014         case SEPARATE_BOTH:
1015                 return;
1016         }
1017
1018         if (vpaned->parent != NULL) {
1019                 gtk_widget_ref(vpaned);
1020                 gtkut_container_remove(GTK_CONTAINER(container), vpaned);
1021                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), container);
1022                 menu_set_sensitive(ifactory, "/View/Expand Summary View", FALSE);
1023         } else {
1024                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), vpaned);
1025                 gtk_container_add(GTK_CONTAINER(container), vpaned);
1026                 gtk_widget_unref(vpaned);
1027                 menu_set_sensitive(ifactory, "/View/Expand Summary View", TRUE);
1028                 gtk_widget_grab_focus(GTK_WIDGET(mainwin->summaryview->ctree));
1029         }
1030 }
1031
1032 static void return_receipt_show(NoticeView *noticeview, MsgInfo *msginfo)
1033 {
1034         noticeview_set_text(noticeview, _("This message asks for a return receipt."));
1035         noticeview_set_button_text(noticeview, _("Send receipt"));
1036         noticeview_set_button_press_callback(noticeview,
1037                                              GTK_SIGNAL_FUNC(return_receipt_send_clicked),
1038                                              (gpointer) msginfo);
1039         noticeview_show(noticeview);
1040 }
1041
1042 static void return_receipt_send_clicked(NoticeView *noticeview, MsgInfo *msginfo)
1043 {
1044         MsgInfo *tmpmsginfo;
1045         gchar *file;
1046
1047         file = procmsg_get_message_file_path(msginfo);
1048         if (!file) {
1049                 g_warning("can't get message file path.\n");
1050                 return;
1051         }
1052
1053         tmpmsginfo = procheader_parse_file(file, msginfo->flags, TRUE, TRUE);
1054         tmpmsginfo->folder = msginfo->folder;
1055         tmpmsginfo->msgnum = msginfo->msgnum;
1056
1057         if (disposition_notification_send(tmpmsginfo) >= 0) {
1058                 procmsg_msginfo_set_flags(msginfo, MSG_RETRCPT_SENT, 0);
1059                 noticeview_hide(noticeview);
1060         }               
1061
1062         procmsg_msginfo_free(tmpmsginfo);
1063         g_free(file);
1064 }
1065
1066 static void partial_recv_show(NoticeView *noticeview, MsgInfo *msginfo)
1067 {
1068         gchar *text = NULL;
1069         gchar *button1 = NULL;
1070         gchar *button2 = NULL;
1071         void  *button1_cb = NULL;
1072         void  *button2_cb = NULL;
1073
1074         if (!partial_msg_in_uidl_list(msginfo)) {
1075                 text = g_strdup_printf(_("This message has been partially "
1076                                 "retrieved,\nand has been deleted from the "
1077                                 "server."));
1078         } else {
1079                 switch (msginfo->planned_download) {
1080                 case POP3_PARTIAL_DLOAD_UNKN:
1081                         text = g_strdup_printf(_("This message has been "
1082                                         "partially retrieved;\nit is %s."),
1083                                         to_human_readable(
1084                                                 (off_t)(msginfo->total_size)));
1085                         button1 = _("Mark for download");
1086                         button2 = _("Mark for deletion");
1087                         button1_cb = partial_recv_dload_clicked;
1088                         button2_cb = partial_recv_del_clicked;
1089                         break;
1090                 case POP3_PARTIAL_DLOAD_DLOAD:
1091                         text = g_strdup_printf(_("This message has been "
1092                                         "partially retrieved;\nit is %s and "
1093                                         "will be downloaded."),
1094                                         to_human_readable(
1095                                                 (off_t)(msginfo->total_size)));
1096                         button1 = _("Unmark");
1097                         button1_cb = partial_recv_unmark_clicked;
1098                         button2 = _("Mark for deletion");
1099                         button2_cb = partial_recv_del_clicked;
1100                         break;
1101                 case POP3_PARTIAL_DLOAD_DELE:
1102                         text = g_strdup_printf(_("This message has been "
1103                                         "partially retrieved;\nit is %s and "
1104                                         "will be deleted."),
1105                                         to_human_readable(
1106                                                 (off_t)(msginfo->total_size)));
1107                         button1 = _("Mark for download");
1108                         button1_cb = partial_recv_dload_clicked;
1109                         button2 = _("Unmark");
1110                         button2_cb = partial_recv_unmark_clicked;
1111                         break;
1112                 default:
1113                         return;
1114                 }
1115         }
1116         
1117         noticeview_set_text(noticeview, text);
1118         g_free(text);
1119         noticeview_set_button_text(noticeview, button1);
1120         noticeview_set_button_press_callback(noticeview,
1121                      GTK_SIGNAL_FUNC(button1_cb), (gpointer) msginfo);
1122
1123         noticeview_set_2ndbutton_text(noticeview, button2);
1124         noticeview_set_2ndbutton_press_callback(noticeview,
1125                      GTK_SIGNAL_FUNC(button2_cb), (gpointer) msginfo);
1126
1127         noticeview_show(noticeview);
1128 }
1129
1130 static void partial_recv_dload_clicked(NoticeView *noticeview, 
1131                                        MsgInfo *msginfo)
1132 {
1133         if (partial_mark_for_download(msginfo) == 0) {
1134                 partial_recv_show(noticeview, msginfo);
1135         }
1136 }
1137
1138 static void partial_recv_del_clicked(NoticeView *noticeview, 
1139                                        MsgInfo *msginfo)
1140 {
1141         if (partial_mark_for_delete(msginfo) == 0) {
1142                 partial_recv_show(noticeview, msginfo);
1143         }
1144 }
1145
1146 static void partial_recv_unmark_clicked(NoticeView *noticeview, 
1147                                        MsgInfo *msginfo)
1148 {
1149         if (partial_unmark(msginfo) == 0) {
1150                 partial_recv_show(noticeview, msginfo);
1151         }
1152 }
1153
1154 static void select_account_cb(GtkWidget *w, gpointer data)
1155 {
1156         *(gint*)data = GPOINTER_TO_INT(gtk_object_get_user_data(GTK_OBJECT(w)));
1157 }
1158         
1159 static PrefsAccount *select_account_from_list(GList *ac_list)
1160 {
1161         GtkWidget *optmenu;
1162         GtkWidget *menu;
1163         gint account_id;
1164
1165         g_return_val_if_fail(ac_list != NULL, NULL);
1166         g_return_val_if_fail(ac_list->data != NULL, NULL);
1167         
1168         optmenu = gtk_option_menu_new();
1169         menu = gtkut_account_menu_new(ac_list, 
1170                         G_CALLBACK(select_account_cb), 
1171                         &account_id);
1172         if (!menu)
1173                 return NULL;
1174         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), menu);
1175         gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), 0);
1176         account_id = ((PrefsAccount *) ac_list->data)->account_id;
1177         if (alertpanel_with_widget(
1178                                 _("Return Receipt Notification"),
1179                                 _("The message was sent to several of your "
1180                                   "accounts.\n"
1181                                   "Please choose which account do you want to "
1182                                   "use for sending the receipt notification:"),
1183                                 _("Send Notification"), _("+Cancel"), NULL,
1184                                 optmenu) != G_ALERTDEFAULT)
1185                 return NULL;
1186         return account_find_from_id(account_id);
1187 }
1188
1189 /* 
1190  * \brief return selected messageview text, when nothing is 
1191  *        selected and message was filtered, return complete text
1192  *
1193  * \param  pointer to Messageview 
1194  *
1195  * \return pointer to text (needs to be free'd by calling func)
1196  */
1197 gchar *messageview_get_selection(MessageView *msgview)
1198 {
1199         TextView *textview;
1200         gchar *text = NULL;
1201         GtkTextView *edit = NULL;
1202         GtkTextBuffer *textbuf;
1203         gint body_pos = 0;
1204         
1205         g_return_val_if_fail(msgview != NULL, NULL);
1206
1207         textview = messageview_get_current_textview(msgview);
1208         g_return_val_if_fail(textview != NULL, NULL);
1209
1210         edit = GTK_TEXT_VIEW(textview->text);
1211         g_return_val_if_fail(edit != NULL, NULL);
1212         body_pos = textview->body_pos;
1213
1214         textbuf = gtk_text_view_get_buffer(edit);
1215
1216         if (gtk_text_buffer_get_selection_bounds(textbuf, NULL, NULL))
1217                 return gtkut_text_view_get_selection(edit);
1218         else if (msgview->filtered) {
1219                 GtkTextIter start_iter, end_iter;
1220                 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter, body_pos);
1221                 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
1222                 gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter, FALSE);
1223         } else
1224                 text = NULL;
1225
1226         return text;
1227 }
1228
1229 static void save_as_cb(gpointer data, guint action, GtkWidget *widget)
1230 {
1231         MessageView *messageview = (MessageView *)data;
1232         messageview_save_as(messageview);
1233 }
1234
1235 static void print_cb(gpointer data, guint action, GtkWidget *widget)
1236 {
1237         MessageView *messageview = (MessageView *)data;
1238         gchar *cmdline;
1239         gchar *p;
1240
1241         if (!messageview->msginfo) return;
1242
1243         cmdline = input_dialog(_("Print"),
1244                                _("Enter the print command line:\n"
1245                                  "(`%s' will be replaced with file name)"),
1246                                prefs_common.print_cmd);
1247         if (!cmdline) return;
1248         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
1249             strchr(p + 2, '%')) {
1250                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
1251                                  cmdline);
1252                 g_free(cmdline);
1253                 return;
1254         }
1255
1256         procmsg_print_message(messageview->msginfo, cmdline);
1257         g_free(cmdline);
1258 }
1259
1260 static void close_cb(gpointer data, guint action, GtkWidget *widget)
1261 {
1262         MessageView *messageview = (MessageView *)data;
1263         gtk_widget_destroy(messageview->window);
1264 }
1265
1266 static void copy_cb(gpointer data, guint action, GtkWidget *widget)
1267 {
1268         MessageView *messageview = (MessageView *)data;
1269         messageview_copy_clipboard(messageview);
1270 }
1271
1272 static void allsel_cb(gpointer data, guint action, GtkWidget *widget)
1273 {
1274         MessageView *messageview = (MessageView *)data;
1275         messageview_select_all(messageview);
1276 }
1277
1278 static void search_cb(gpointer data, guint action, GtkWidget *widget)
1279 {
1280         MessageView *messageview = (MessageView *)data;
1281         message_search(messageview);
1282 }
1283
1284 static void set_charset_cb(gpointer data, guint action, GtkWidget *widget)
1285 {
1286         MessageView *messageview = (MessageView *)data;
1287         const gchar *charset;
1288
1289         if (GTK_CHECK_MENU_ITEM(widget)->active) {
1290                 charset = conv_get_charset_str((CharSet)action);
1291                 g_free(messageview->forced_charset);
1292                 messageview->forced_charset = g_strdup(charset);
1293                 messageview_show(messageview, messageview->msginfo, FALSE);
1294         }
1295 }
1296
1297 static void view_source_cb(gpointer data, guint action, GtkWidget *widget)
1298 {
1299         MessageView *messageview = (MessageView *)data;
1300         SourceWindow *srcwin;
1301
1302         if (!messageview->msginfo) return;
1303
1304         srcwin = source_window_create();
1305         source_window_show_msg(srcwin, messageview->msginfo);
1306         source_window_show(srcwin);
1307 }
1308
1309 static void show_all_header_cb(gpointer data, guint action, GtkWidget *widget)
1310 {
1311         MessageView *messageview = (MessageView *)data;
1312         MsgInfo *msginfo = messageview->msginfo;
1313
1314         if (!msginfo) return;
1315         messageview->msginfo = NULL;
1316         messageview_show(messageview, msginfo,
1317                          GTK_CHECK_MENU_ITEM(widget)->active);
1318         procmsg_msginfo_free(msginfo);
1319         main_window_set_menu_sensitive(messageview->mainwin);
1320 }
1321
1322 static void compose_cb(gpointer data, guint action, GtkWidget *widget)
1323 {
1324         MessageView *messageview = (MessageView *)data;
1325         PrefsAccount *ac = NULL;
1326         FolderItem *item = NULL;
1327
1328         if (messageview->msginfo)
1329                 item = messageview->msginfo->folder;
1330
1331         if (item) {
1332                 ac = account_find_from_item(item);
1333                 if (ac && ac->protocol == A_NNTP &&
1334                     item->stype == F_NEWS) {
1335                         compose_new(ac, item->path, NULL);
1336                         return;
1337                 }
1338         }
1339
1340         compose_new(ac, NULL, NULL);
1341 }
1342
1343 static void reply_cb(gpointer data, guint action, GtkWidget *widget)
1344 {
1345         MessageView *messageview = (MessageView *)data;
1346         GSList *mlist = NULL;
1347         MsgInfo *msginfo;
1348         gchar *text = NULL;
1349         ComposeMode mode = (ComposeMode)action;
1350         TextView *textview;
1351
1352         msginfo = messageview->msginfo;
1353         mlist = g_slist_append(NULL, msginfo);
1354
1355         textview = messageview_get_current_textview(messageview);
1356         text = gtkut_editable_get_selection
1357                 (GTK_EDITABLE(textview->text));
1358         if (text && *text == '\0') {
1359                 g_free(text);
1360                 text = NULL;
1361         }
1362
1363         switch (mode) {
1364         case COMPOSE_REPLY:
1365                 compose_reply(msginfo, prefs_common.reply_with_quote,
1366                               FALSE, prefs_common.default_reply_list, FALSE, text);
1367                 break;
1368         case COMPOSE_REPLY_WITH_QUOTE:
1369                 compose_reply(msginfo, TRUE, FALSE, prefs_common.default_reply_list, FALSE, text);
1370                 break;
1371         case COMPOSE_REPLY_WITHOUT_QUOTE:
1372                 compose_reply(msginfo, FALSE, FALSE, prefs_common.default_reply_list, FALSE, NULL);
1373                 break;
1374         case COMPOSE_REPLY_TO_SENDER:
1375                 compose_reply(msginfo, prefs_common.reply_with_quote,
1376                               FALSE, FALSE, TRUE, text);
1377                 break;
1378         case COMPOSE_FOLLOWUP_AND_REPLY_TO:
1379                 compose_followup_and_reply_to(msginfo,
1380                                               prefs_common.reply_with_quote,
1381                                               FALSE, FALSE, text);
1382                 break;
1383         case COMPOSE_REPLY_TO_SENDER_WITH_QUOTE:
1384                 compose_reply(msginfo, TRUE, FALSE, FALSE, TRUE, text);
1385                 break;
1386         case COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE:
1387                 compose_reply(msginfo, FALSE, FALSE, FALSE, TRUE, NULL);
1388                 break;
1389         case COMPOSE_REPLY_TO_ALL:
1390                 compose_reply(msginfo, prefs_common.reply_with_quote,
1391                               TRUE, FALSE, FALSE, text);
1392                 break;
1393         case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
1394                 compose_reply(msginfo, TRUE, TRUE, FALSE, FALSE, text);
1395                 break;
1396         case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
1397                 compose_reply(msginfo, FALSE, TRUE, FALSE, FALSE, NULL);
1398                 break;
1399         case COMPOSE_REPLY_TO_LIST:
1400                 compose_reply(msginfo, prefs_common.reply_with_quote,
1401                               FALSE, TRUE, FALSE, text);
1402                 break;
1403         case COMPOSE_REPLY_TO_LIST_WITH_QUOTE:
1404                 compose_reply(msginfo, TRUE, FALSE, TRUE, FALSE, text);
1405                 break;
1406         case COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE:
1407                 compose_reply(msginfo, FALSE, FALSE, TRUE, FALSE, NULL);
1408                 break;
1409         case COMPOSE_FORWARD:
1410                 if (prefs_common.forward_as_attachment) {
1411                         compose_reply_mode(COMPOSE_FORWARD_AS_ATTACH, mlist, text);
1412                         return;
1413                 } else {
1414                         compose_reply_mode(COMPOSE_FORWARD_INLINE, mlist, text);
1415                         return;
1416                 }
1417                 break;
1418         case COMPOSE_FORWARD_INLINE:
1419                 compose_forward(NULL, msginfo, FALSE, text, FALSE);
1420                 break;
1421         case COMPOSE_FORWARD_AS_ATTACH:
1422                 compose_forward_multiple(NULL, mlist);
1423                 break;
1424         case COMPOSE_REDIRECT:
1425                 compose_redirect(NULL, msginfo);
1426                 break;
1427         default:
1428                 g_warning("compose_reply(): invalid Compose Mode: %d\n", mode);
1429         }
1430
1431         /* summary_set_marks_selected(summaryview); */
1432         g_free(text);
1433         g_slist_free(mlist);
1434 }
1435
1436 static void reedit_cb(gpointer data, guint action, GtkWidget *widget)
1437 {
1438         MessageView *messageview = (MessageView *)data;
1439         MsgInfo *msginfo;
1440
1441         if (!messageview->msginfo) return;
1442         msginfo = messageview->msginfo;
1443         if (!msginfo->folder) return;
1444         if (msginfo->folder->stype != F_OUTBOX &&
1445             msginfo->folder->stype != F_DRAFT &&
1446             msginfo->folder->stype != F_QUEUE) return;
1447
1448         compose_reedit(msginfo);
1449 }
1450
1451 static void addressbook_open_cb(gpointer data, guint action, GtkWidget *widget)
1452 {
1453         addressbook_open(NULL);
1454 }
1455
1456 static void add_address_cb(gpointer data, guint action, GtkWidget *widget)
1457 {
1458         MessageView *messageview = (MessageView *)data;
1459         MsgInfo *msginfo;
1460         gchar *from;
1461
1462         if (!messageview->msginfo) return;
1463         msginfo = messageview->msginfo;
1464         Xstrdup_a(from, msginfo->from, return);
1465         eliminate_address_comment(from);
1466         extract_address(from);
1467         addressbook_add_contact(msginfo->fromname, from, NULL);
1468 }
1469
1470 static void create_filter_cb(gpointer data, guint action, GtkWidget *widget)
1471 {
1472         MessageView *messageview = (MessageView *)data;
1473         FolderItem * item;
1474         
1475         if (!messageview->msginfo) return;
1476         
1477         item = messageview->msginfo->folder;
1478         summary_msginfo_filter_open(item,  messageview->msginfo,
1479                                     (PrefsFilterType)action, 0);
1480 }
1481
1482 static void create_processing_cb(gpointer data, guint action,
1483                                  GtkWidget *widget)
1484 {
1485         MessageView *messageview = (MessageView *)data;
1486         FolderItem * item;
1487         
1488         if (!messageview->msginfo) return;
1489         
1490         item = messageview->msginfo->folder;
1491         summary_msginfo_filter_open(item,  messageview->msginfo,
1492                                     (PrefsFilterType)action, 1);
1493 }
1494
1495 static void about_cb(gpointer data, guint action, GtkWidget *widget)
1496 {
1497         about_show();
1498 }
1499
1500 static gboolean messageview_update_msg(gpointer source, gpointer data)
1501 {
1502         MsgInfoUpdate *msginfo_update = (MsgInfoUpdate *) source;
1503         MessageView *messageview = (MessageView *)data;
1504
1505         if (messageview->msginfo != msginfo_update->msginfo)
1506                 return FALSE;
1507
1508         if (msginfo_update->flags & MSGINFO_UPDATE_DELETED) {
1509                 messageview_clear(messageview);
1510                 messageview_update(messageview);
1511         }
1512
1513         return FALSE;
1514 }
1515
1516 void messageview_set_menu_sensitive(MessageView *messageview)
1517 {
1518         GtkItemFactory *ifactory;
1519         GtkWidget *menuitem;
1520
1521         if (!messageview && !messageview->new_window) 
1522                 return;
1523         /* do some smart things */
1524         if (!messageview->menubar) return;
1525         ifactory = gtk_item_factory_from_widget(messageview->menubar);
1526         if (!ifactory) return;
1527         if (messageview->mainwin->type == SEPARATE_MESSAGE) {
1528                 menuitem = gtk_item_factory_get_widget(ifactory, "/View/Show all headers");
1529                 gtk_check_menu_item_set_active
1530                         (GTK_CHECK_MENU_ITEM(menuitem),
1531                          messageview->mimeview->textview->show_all_headers);
1532         }
1533 }