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