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