0.8.8claws63
[claws.git] / src / messageview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 <stdio.h>
30 #include <ctype.h>
31 #include <string.h>
32
33 #include "intl.h"
34 #include "main.h"
35 #include "messageview.h"
36 #include "headerview.h"
37 #include "summaryview.h"
38 #include "textview.h"
39 #include "imageview.h"
40 #include "mimeview.h"
41 #include "procmsg.h"
42 #include "procheader.h"
43 #include "procmime.h"
44 #include "prefs_common.h"
45 #include "gtkutils.h"
46 #include "utils.h"
47 #include "rfc2015.h"
48 #include "about.h"
49 #include "account.h"
50 #include "alertpanel.h"
51 #include "send.h"
52 #include "pgptext.h"
53 #include "menu.h"
54 #include "stock_pixmap.h"
55
56 static void messageview_change_view_type(MessageView    *messageview,
57                                          MessageType     type);
58 static void messageview_destroy_cb      (GtkWidget      *widget,
59                                          MessageView    *messageview);
60 static void messageview_size_allocate_cb(GtkWidget      *widget,
61                                          GtkAllocation  *allocation);
62 static void key_pressed                 (GtkWidget      *widget,
63                                          GdkEventKey    *event,
64                                          MessageView    *messageview);
65 static void focus_in                    (GtkWidget      *widget, 
66                                          GdkEventFocus  *event,
67                                          gpointer        data);
68
69 static void return_receipt_show         (NoticeView     *noticeview, 
70                                          MsgInfo        *msginfo);      
71 static void return_receipt_send_clicked (NoticeView     *noticeview, 
72                                          MsgInfo        *msginfo);
73
74 static PrefsAccount *select_account_from_list
75                                         (GList          *ac_list);
76
77 static void messageview_menubar_cb      (gpointer        data, 
78                                          guint           action, 
79                                          GtkWidget      *widget);
80                                          
81 static void messageview_close_cb        (gpointer        data,
82                                          guint           action,
83                                          GtkWidget      *widget);
84 static GList *msgview_list = NULL;
85
86 MessageView *messageview_create(MainWindow *mainwin)
87 {
88         MessageView *messageview;
89         GtkWidget *vbox;
90         HeaderView *headerview;
91         TextView *textview;
92         ImageView *imageview;
93         MimeView *mimeview;
94         NoticeView *noticeview;
95
96         debug_print("Creating message view...\n");
97         messageview = g_new0(MessageView, 1);
98
99         messageview->type = MVIEW_TEXT;
100
101         headerview = headerview_create();
102
103         noticeview = noticeview_create(mainwin);
104
105         textview = textview_create();
106         textview->messageview = messageview;
107
108         imageview = imageview_create();
109         imageview->messageview = messageview;
110
111         mimeview = mimeview_create();
112         mimeview->textview = textview_create();
113         mimeview->textview->messageview = messageview;
114         mimeview->imageview = imageview;
115         mimeview->messageview = messageview;
116
117         vbox = gtk_vbox_new(FALSE, 0);
118         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(headerview),
119                            FALSE, FALSE, 0);
120         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(noticeview),
121                            FALSE, FALSE, 0);
122         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(textview),
123                            TRUE, TRUE, 0);
124
125         /* to remove without destroyed */
126         gtk_widget_ref(GTK_WIDGET_PTR(textview));
127         gtk_widget_ref(GTK_WIDGET_PTR(imageview));
128         gtk_widget_ref(GTK_WIDGET_PTR(mimeview));
129         gtk_widget_ref(GTK_WIDGET_PTR(mimeview->textview));
130
131         messageview->vbox       = vbox;
132         messageview->new_window = FALSE;
133         messageview->window     = NULL;
134         messageview->headerview = headerview;
135         messageview->textview   = textview;
136         messageview->imageview  = imageview;
137         messageview->mimeview   = mimeview;
138         messageview->noticeview = noticeview;
139         messageview->mainwin    = mainwin;
140
141         return messageview;
142 }
143
144 static GtkItemFactoryEntry messageview_entries[] =
145 {
146         {N_("/_File"),                          NULL, NULL, 0, "<Branch>"},
147         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
148         {N_("/_File/_Close"),                   "<control>W", messageview_close_cb, 0, NULL},
149
150         {N_("/_Message"),                       NULL, NULL, 0, "<Branch>"},
151         {N_("/_Message/_Reply"),                "<control>R",   messageview_menubar_cb, COMPOSE_REPLY, NULL},
152         {N_("/_Message/Repl_y to"),             NULL, NULL, 0, "<Branch>"},
153         {N_("/_Message/Repl_y to/_all"),        "<shift><control>R", messageview_menubar_cb, COMPOSE_REPLY_TO_ALL, NULL},
154         {N_("/_Message/Repl_y to/_sender"),     NULL, messageview_menubar_cb, COMPOSE_REPLY_TO_SENDER, NULL},
155         {N_("/_Message/Repl_y to/mailing _list"),
156                                                 "<control>L", messageview_menubar_cb, COMPOSE_REPLY_TO_LIST, NULL},
157         {N_("/_Message/Follow-up and reply to"),NULL, messageview_menubar_cb, COMPOSE_FOLLOWUP_AND_REPLY_TO, NULL},
158         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
159         {N_("/_Message/_Delete"),               "<control>D", delete_msgview_cb,  0, NULL},
160         
161         {N_("/_Help"),                          NULL, NULL, 0, "<Branch>"},
162         {N_("/_Help/_About"),                   NULL, about_show, 0, NULL}
163 };
164
165
166 GList *messageview_get_msgview_list(void)
167 {
168         return msgview_list;
169 }
170
171 void messageview_add_toolbar(MessageView *msgview, GtkWidget *window) 
172 {
173         GtkWidget *handlebox;
174         GtkWidget *vbox;
175         GtkWidget *menubar;
176         guint n_menu_entries;
177
178         vbox = gtk_vbox_new(FALSE, 0);
179         gtk_widget_show(vbox);
180         gtk_container_add(GTK_CONTAINER(window), vbox); 
181         
182         n_menu_entries = sizeof(messageview_entries) / sizeof(messageview_entries[0]);
183         menubar = menubar_create(window, messageview_entries,
184                                  n_menu_entries, "<MessageView>", msgview);
185         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
186
187         handlebox = gtk_handle_box_new();
188         gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
189         msgview->toolbar = toolbar_create(TOOLBAR_MSGVIEW, handlebox,
190                                           (gpointer)msgview);
191         msgview->handlebox = handlebox;
192         msgview->menubar   = menubar;
193
194         gtk_container_add(GTK_CONTAINER(vbox),
195                           GTK_WIDGET_PTR(msgview));
196 }
197
198 MessageView *messageview_create_with_new_window(MainWindow *mainwin)
199 {
200         GtkWidget *window;
201         MessageView *msgview;
202
203         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
204         gtk_window_set_title(GTK_WINDOW(window), _("Sylpheed - Message View"));
205         gtk_window_set_wmclass(GTK_WINDOW(window), "message_view", "Sylpheed");
206         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
207         gtk_widget_set_usize(window, prefs_common.msgwin_width,
208                              prefs_common.msgwin_height);
209
210         msgview = messageview_create(mainwin);
211
212         gtk_signal_connect(GTK_OBJECT(window), "size_allocate",
213                            GTK_SIGNAL_FUNC(messageview_size_allocate_cb),
214                            msgview);
215         gtk_signal_connect(GTK_OBJECT(window), "destroy",
216                            GTK_SIGNAL_FUNC(messageview_destroy_cb), msgview);
217         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
218                            GTK_SIGNAL_FUNC(key_pressed), msgview);
219         gtk_signal_connect(GTK_OBJECT(window), "focus_in_event",
220                            GTK_SIGNAL_FUNC(focus_in), msgview);
221
222         messageview_add_toolbar(msgview, window);
223
224         gtk_widget_grab_focus(msgview->textview->text);
225         gtk_widget_show_all(window);
226
227         msgview->new_window = TRUE;
228         msgview->window = window;
229         msgview->visible = TRUE;
230
231         toolbar_set_style(msgview->toolbar->toolbar, msgview->handlebox, 
232                           prefs_common.toolbar_style);
233         messageview_init(msgview);
234         msgview_list = g_list_append(msgview_list, msgview);
235
236         return msgview;
237 }
238
239 void messageview_init(MessageView *messageview)
240 {
241         headerview_init(messageview->headerview);
242         textview_init(messageview->textview);
243         imageview_init(messageview->imageview);
244         mimeview_init(messageview->mimeview);
245         /*messageview_set_font(messageview);*/
246
247         noticeview_hide(messageview->noticeview);
248 }
249
250 static void notification_convert_header(gchar *dest, gint len, 
251                                         const gchar *src_,
252                                         gint header_len)
253 {
254         char *src;
255
256         g_return_if_fail(src_ != NULL);
257         g_return_if_fail(dest != NULL);
258
259         if (len < 1) return;
260
261         Xstrndup_a(src, src_, len, return);
262
263         remove_return(src);
264
265         if (is_ascii_str(src)) {
266                 strncpy2(dest, src, len);
267                 dest[len - 1] = '\0';
268                 return;
269         } else
270                 conv_encode_header(dest, len, src, header_len);
271 }
272
273 static gint disposition_notification_queue(PrefsAccount * account,
274                                            gchar * to, const gchar *file)
275 {
276         FolderItem *queue;
277         gchar *tmp;
278         FILE *fp, *src_fp;
279         gchar buf[BUFFSIZE];
280         gint num;
281
282         debug_print("queueing message...\n");
283         g_return_val_if_fail(account != NULL, -1);
284
285         tmp = g_strdup_printf("%s%cqueue.%d", g_get_tmp_dir(),
286                               G_DIR_SEPARATOR, (gint)file);
287         if ((fp = fopen(tmp, "wb")) == NULL) {
288                 FILE_OP_ERROR(tmp, "fopen");
289                 g_free(tmp);
290                 return -1;
291         }
292         if ((src_fp = fopen(file, "rb")) == NULL) {
293                 FILE_OP_ERROR(file, "fopen");
294                 fclose(fp);
295                 unlink(tmp);
296                 g_free(tmp);
297                 return -1;
298         }
299         if (change_file_mode_rw(fp, tmp) < 0) {
300                 FILE_OP_ERROR(tmp, "chmod");
301                 g_warning("can't change file mode\n");
302         }
303
304         /* queueing variables */
305         fprintf(fp, "AF:\n");
306         fprintf(fp, "NF:0\n");
307         fprintf(fp, "PS:10\n");
308         fprintf(fp, "SRH:1\n");
309         fprintf(fp, "SFN:\n");
310         fprintf(fp, "DSR:\n");
311         fprintf(fp, "MID:\n");
312         fprintf(fp, "CFG:\n");
313         fprintf(fp, "PT:0\n");
314         fprintf(fp, "S:%s\n", account->address);
315         fprintf(fp, "RQ:\n");
316         if (account->smtp_server)
317                 fprintf(fp, "SSV:%s\n", account->smtp_server);
318         else
319                 fprintf(fp, "SSV:\n");
320         if (account->nntp_server)
321                 fprintf(fp, "NSV:%s\n", account->nntp_server);
322         else
323                 fprintf(fp, "NSV:\n");
324         fprintf(fp, "SSH:\n");
325         fprintf(fp, "R:<%s>", to);
326         fprintf(fp, "\n");
327         fprintf(fp, "\n");
328
329         while (fgets(buf, sizeof(buf), src_fp) != NULL) {
330                 if (fputs(buf, fp) == EOF) {
331                         FILE_OP_ERROR(tmp, "fputs");
332                         fclose(fp);
333                         fclose(src_fp);
334                         unlink(tmp);
335                         g_free(tmp);
336                         return -1;
337                 }
338         }
339
340         fclose(src_fp);
341         if (fclose(fp) == EOF) {
342                 FILE_OP_ERROR(tmp, "fclose");
343                 unlink(tmp);
344                 g_free(tmp);
345                 return -1;
346         }
347
348         queue = folder_get_default_queue();
349         if ((num = folder_item_add_msg(queue, tmp, TRUE)) < 0) {
350                 g_warning("can't queue the message\n");
351                 unlink(tmp);
352                 g_free(tmp);
353                 return -1;
354         }
355         g_free(tmp);
356
357         return 0;
358 }
359
360 static gint disposition_notification_send(MsgInfo *msginfo)
361 {
362         gchar buf[BUFFSIZE];
363         gchar tmp[MAXPATHLEN + 1];
364         FILE *fp;
365         GSList *to_list;
366         GList *ac_list;
367         PrefsAccount *account;
368         gint ok;
369         gchar *to;
370
371         if ((!msginfo->returnreceiptto) && 
372             (!msginfo->dispositionnotificationto)) 
373                 return -1;
374
375         /* RFC2298: Test for Return-Path */
376         if (msginfo->dispositionnotificationto)
377                 to = msginfo->dispositionnotificationto;
378         else
379                 to = msginfo->returnreceiptto;
380
381         ok = get_header_from_msginfo(msginfo, buf, sizeof(buf),
382                                 "Return-Path:");
383         if (ok == 0) {
384                 gchar *to_addr = g_strdup(to);
385                 extract_address(to_addr);
386                 extract_address(buf);
387                 ok = strcmp(to_addr, buf);
388                 g_free(to_addr);
389         } else {
390                 strncpy(buf, _("<No Return-Path found>"), 
391                                 sizeof(buf));
392         }
393         
394         if (ok != 0) {
395                 AlertValue val;
396                 gchar *message;
397                 message = g_strdup_printf(
398                                  _("The notification address to which the "
399                                    "return receipt is to be sent\n"
400                                    "does not correspond to the return path:\n"
401                                    "Notification address: %s\n"
402                                    "Return path: %s\n"
403                                    "It is advised to not to send the return "
404                                    "receipt."), to, buf);
405                 val = alertpanel(_("Warning"), message, _("Send"),
406                                 _("+Don't Send"), NULL);
407                 if (val != G_ALERTDEFAULT)
408                         return -1;
409         }
410
411         ac_list = account_find_all_from_address(NULL, msginfo->to);
412         ac_list = account_find_all_from_address(ac_list, msginfo->cc);
413
414         if (ac_list == NULL) {
415                 alertpanel_error(_("This message is asking for a return "
416                                    "receipt notification\n"
417                                    "but according to its 'To:' and 'CC:' "
418                                    "headers it was not\nofficially addressed "
419                                    "to you.\n"
420                                    "Receipt notification cancelled."));
421                 return -1;
422         }
423
424         if (g_list_length(ac_list) > 1)
425                 account = select_account_from_list(ac_list);
426         else
427                 account = (PrefsAccount *) ac_list->data;
428         g_list_free(ac_list);
429
430         if (account == NULL)
431                 return -1;
432
433         /* write to temporary file */
434         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
435                    get_rc_dir(), G_DIR_SEPARATOR, (gint)msginfo);
436
437         if ((fp = fopen(tmp, "wb")) == NULL) {
438                 FILE_OP_ERROR(tmp, "fopen");
439                 return -1;
440         }
441
442         /* chmod for security */
443         if (change_file_mode_rw(fp, tmp) < 0) {
444                 FILE_OP_ERROR(tmp, "chmod");
445                 g_warning("can't change file mode\n");
446         }
447
448         /* Date */
449         get_rfc822_date(buf, sizeof(buf));
450         fprintf(fp, "Date: %s\n", buf);
451
452         /* From */
453         if (account->name && *account->name) {
454                 notification_convert_header
455                         (buf, sizeof(buf), account->name,
456                          strlen("From: "));
457                 fprintf(fp, "From: %s <%s>\n", buf, account->address);
458         } else
459                 fprintf(fp, "From: %s\n", account->address);
460
461         fprintf(fp, "To: %s\n", to);
462
463         /* Subject */
464         notification_convert_header(buf, sizeof(buf), msginfo->subject,
465                                     strlen("Subject: "));
466         fprintf(fp, "Subject: Disposition notification: %s\n", buf);
467
468         if (fclose(fp) == EOF) {
469                 FILE_OP_ERROR(tmp, "fclose");
470                 unlink(tmp);
471                 return -1;
472         }
473
474         to_list = address_list_append(NULL, to);
475         ok = send_message(tmp, account, to_list);
476         
477         if (ok < 0) {
478                 if (prefs_common.queue_msg) {
479                         AlertValue val;
480                         
481                         val = alertpanel
482                                 (_("Queueing"),
483                                  _("Error occurred while sending the notification.\n"
484                                    "Put this notification into queue folder?"),
485                                  _("OK"), _("Cancel"), NULL);
486                         if (G_ALERTDEFAULT == val) {
487                                 ok = disposition_notification_queue(account, to, tmp);
488                                 if (ok < 0)
489                                         alertpanel_error(_("Can't queue the notification."));
490                         }
491                 } else
492                         alertpanel_error(_("Error occurred while sending the notification."));
493         }
494
495         if (unlink(tmp) < 0) FILE_OP_ERROR(tmp, "unlink");
496
497         return ok;
498 }
499
500 void messageview_show(MessageView *messageview, MsgInfo *msginfo,
501                       gboolean all_headers)
502 {
503         FILE *fp;
504         gchar *file;
505         MimeInfo *mimeinfo;
506         MsgInfo *tmpmsginfo;
507
508         g_return_if_fail(msginfo != NULL);
509         messageview->msginfo = msginfo;
510
511 #if USE_GPGME
512         if ((fp = procmsg_open_message_decrypted(msginfo, &mimeinfo)) == NULL)
513                 return;
514 #else /* !USE_GPGME */
515         if ((fp = procmsg_open_message(msginfo)) == NULL) return;
516         mimeinfo = procmime_scan_mime_header(fp);
517 #endif /* USE_GPGME */
518         fclose(fp);
519         if (!mimeinfo) return;
520
521         file = procmsg_get_message_file_path(msginfo);
522         if (!file) {
523                 g_warning("can't get message file path.\n");
524                 procmime_mimeinfo_free_all(mimeinfo);
525                 return;
526         }
527
528         tmpmsginfo = procheader_parse_file(file, msginfo->flags, TRUE, TRUE);
529
530         headerview_show(messageview->headerview, tmpmsginfo);
531         procmsg_msginfo_free(tmpmsginfo);
532
533         messageview->all_headers = all_headers;
534         textview_set_all_headers(messageview->textview, all_headers);
535         textview_set_all_headers(messageview->mimeview->textview, all_headers);
536
537         if (mimeinfo->mime_type != MIME_TEXT &&
538             mimeinfo->mime_type != MIME_TEXT_HTML) {
539                 messageview_change_view_type(messageview, MVIEW_MIME);
540                 mimeview_show_message(messageview->mimeview, mimeinfo, file);
541         } else {
542                 messageview_change_view_type(messageview, MVIEW_TEXT);
543                 textview_show_message(messageview->textview, mimeinfo, file);
544                 procmime_mimeinfo_free_all(mimeinfo);
545         }
546
547         if (MSG_IS_RETRCPT_PENDING(msginfo->flags))
548                 return_receipt_show(messageview->noticeview, msginfo);
549         else 
550                 noticeview_hide(messageview->noticeview);
551
552         g_free(file);
553 }
554
555 static void messageview_change_view_type(MessageView *messageview,
556                                          MessageType type)
557 {
558         TextView *textview = messageview->textview;
559         MimeView *mimeview = messageview->mimeview;
560
561         if (messageview->type == type) return;
562
563         if (type == MVIEW_MIME) {
564                 gtkut_container_remove
565                         (GTK_CONTAINER(GTK_WIDGET_PTR(messageview)),
566                          GTK_WIDGET_PTR(textview));
567                 gtk_box_pack_start(GTK_BOX(messageview->vbox),
568                                    GTK_WIDGET_PTR(mimeview), TRUE, TRUE, 0);
569                 gtk_container_add(GTK_CONTAINER(mimeview->vbox),
570                                   GTK_WIDGET_PTR(textview));
571         } else if (type == MVIEW_TEXT) {
572                 gtkut_container_remove
573                         (GTK_CONTAINER(GTK_WIDGET_PTR(messageview)),
574                          GTK_WIDGET_PTR(mimeview));
575
576                 if (mimeview->vbox == GTK_WIDGET_PTR(textview)->parent)
577                         gtkut_container_remove(GTK_CONTAINER(mimeview->vbox),
578                                                GTK_WIDGET_PTR(textview));
579
580                 gtk_box_pack_start(GTK_BOX(messageview->vbox),
581                                    GTK_WIDGET_PTR(textview), TRUE, TRUE, 0);
582         } else
583                 return;
584
585         messageview->type = type;
586 }
587
588 void messageview_reflect_prefs_pixmap_theme(void)
589 {
590         GList *cur;
591         MessageView *msgview;
592
593         for (cur = msgview_list; cur != NULL; cur = cur->next) {
594                 msgview = (MessageView*)cur->data;
595                 toolbar_update(TOOLBAR_MSGVIEW, msgview);
596         }
597 }
598
599 void messageview_clear(MessageView *messageview)
600 {
601         messageview_change_view_type(messageview, MVIEW_TEXT);
602         headerview_clear(messageview->headerview);
603         textview_clear(messageview->textview);
604         imageview_clear(messageview->imageview);
605         noticeview_hide(messageview->noticeview);
606 }
607
608 void messageview_destroy(MessageView *messageview)
609 {
610         GtkWidget *textview  = GTK_WIDGET_PTR(messageview->textview);
611         GtkWidget *imageview = GTK_WIDGET_PTR(messageview->imageview);
612         GtkWidget *mimeview  = GTK_WIDGET_PTR(messageview->mimeview);
613
614         debug_print("destroy messageview\n");
615         headerview_destroy(messageview->headerview);
616         textview_destroy(messageview->textview);
617         imageview_destroy(messageview->imageview);
618         mimeview_destroy(messageview->mimeview);
619         noticeview_destroy(messageview->noticeview);
620
621         toolbar_clear_list(TOOLBAR_MSGVIEW);
622         if (messageview->toolbar) {
623                 toolbar_destroy(messageview->toolbar);
624                 g_free(messageview->toolbar);
625         }
626         
627         msgview_list = g_list_remove(msgview_list, messageview); 
628
629         g_free(messageview);
630
631         gtk_widget_unref(textview);
632         gtk_widget_unref(imageview);
633         gtk_widget_unref(mimeview);
634 }
635
636 void messageview_quote_color_set(void)
637 {
638 }
639
640 void messageview_set_font(MessageView *messageview)
641 {
642         textview_set_font(messageview->textview, NULL);
643 }
644
645 TextView *messageview_get_current_textview(MessageView *messageview)
646 {
647         TextView *text = NULL;
648
649         if (messageview->type == MVIEW_TEXT)
650                 text = messageview->textview;
651         else if (messageview->type == MVIEW_MIME) {
652                 if (gtk_notebook_get_current_page
653                         (GTK_NOTEBOOK(messageview->mimeview->notebook)) == 0)
654                         text = messageview->textview;
655                 else if (messageview->mimeview->type == MIMEVIEW_TEXT)
656                         text = messageview->mimeview->textview;
657         }
658
659         return text;
660 }
661
662 void messageview_copy_clipboard(MessageView *messageview)
663 {
664         TextView *text;
665
666         text = messageview_get_current_textview(messageview);
667         if (text)
668                 gtk_editable_copy_clipboard(GTK_EDITABLE(text->text));
669 }
670
671 void messageview_select_all(MessageView *messageview)
672 {
673         TextView *text;
674
675         text = messageview_get_current_textview(messageview);
676         if (text)
677                 gtk_editable_select_region(GTK_EDITABLE(text->text), 0, -1);
678 }
679
680 void messageview_set_position(MessageView *messageview, gint pos)
681 {
682         textview_set_position(messageview->textview, pos);
683 }
684
685 gboolean messageview_search_string(MessageView *messageview, const gchar *str,
686                                    gboolean case_sens)
687 {
688         return textview_search_string(messageview->textview, str, case_sens);
689         return FALSE;
690 }
691
692 gboolean messageview_search_string_backward(MessageView *messageview,
693                                             const gchar *str,
694                                             gboolean case_sens)
695 {
696         return textview_search_string_backward(messageview->textview,
697                                                str, case_sens);
698         return FALSE;
699 }
700
701 gboolean messageview_is_visible(MessageView *messageview)
702 {
703         return messageview->visible;
704 }
705
706 static void messageview_destroy_cb(GtkWidget *widget, MessageView *messageview)
707 {
708         messageview_destroy(messageview);
709 }
710
711 static void messageview_size_allocate_cb(GtkWidget *widget,
712                                          GtkAllocation *allocation)
713 {
714         g_return_if_fail(allocation != NULL);
715
716         prefs_common.msgwin_width  = allocation->width;
717         prefs_common.msgwin_height = allocation->height;
718 }
719
720 static void key_pressed(GtkWidget *widget, GdkEventKey *event,
721                         MessageView *messageview)
722 {
723         if (event && event->keyval == GDK_Escape && messageview->window)
724                 gtk_widget_destroy(messageview->window);
725 }
726
727 static void focus_in(GtkWidget *widget, GdkEventFocus *event,
728                      gpointer data)
729 {
730         MessageView *msgview = (MessageView*)data;
731
732         summary_select_by_msgnum(msgview->mainwin->summaryview, 
733                                  msgview->msginfo->msgnum);
734 }
735
736 void messageview_toggle_view_real(MessageView *messageview)
737 {
738         MainWindow *mainwin = messageview->mainwin;
739         union CompositeWin *cwin = &mainwin->win;
740         GtkWidget *vpaned = NULL;
741         GtkWidget *container = NULL;
742         GtkItemFactory *ifactory = gtk_item_factory_from_widget(mainwin->menubar);
743         
744         switch (mainwin->type) {
745         case SEPARATE_NONE:
746                 vpaned = cwin->sep_none.vpaned;
747                 container = cwin->sep_none.hpaned;
748                 break;
749         case SEPARATE_FOLDER:
750                 vpaned = cwin->sep_folder.vpaned;
751                 container = mainwin->vbox_body;
752                 break;
753         case SEPARATE_MESSAGE:
754         case SEPARATE_BOTH:
755                 return;
756         }
757
758         if (vpaned->parent != NULL) {
759                 gtk_widget_ref(vpaned);
760                 gtkut_container_remove(GTK_CONTAINER(container), vpaned);
761                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), container);
762                 menu_set_sensitive(ifactory, "/View/Expand Summary View", FALSE);
763                 gtk_widget_grab_focus(GTK_WIDGET(messageview->textview->text));
764         } else {
765                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), vpaned);
766                 gtk_container_add(GTK_CONTAINER(container), vpaned);
767                 gtk_widget_unref(vpaned);
768                 menu_set_sensitive(ifactory, "/View/Expand Summary View", TRUE);
769                 gtk_widget_grab_focus(GTK_WIDGET(mainwin->summaryview->ctree));
770         }
771 }
772
773 static void return_receipt_show(NoticeView *noticeview, MsgInfo *msginfo)
774 {
775         noticeview_set_text(noticeview, _("This messages asks for a return receipt."));
776         noticeview_set_button_text(noticeview, _("Send receipt"));
777         noticeview_set_button_press_callback(noticeview,
778                                              GTK_SIGNAL_FUNC(return_receipt_send_clicked),
779                                              (gpointer) msginfo);
780         noticeview_show(noticeview);
781 }
782
783 static void return_receipt_send_clicked(NoticeView *noticeview, MsgInfo *msginfo)
784 {
785         MsgInfo *tmpmsginfo;
786         gchar *file;
787
788         file = procmsg_get_message_file_path(msginfo);
789         if (!file) {
790                 g_warning("can't get message file path.\n");
791                 return;
792         }
793
794         tmpmsginfo = procheader_parse_file(file, msginfo->flags, TRUE, TRUE);
795         tmpmsginfo->folder = msginfo->folder;
796         tmpmsginfo->msgnum = msginfo->msgnum;
797
798         if (disposition_notification_send(tmpmsginfo) >= 0) {
799                 procmsg_msginfo_unset_flags(msginfo, MSG_RETRCPT_PENDING, 0);
800                 noticeview_hide(noticeview);
801         }               
802
803         procmsg_msginfo_free(tmpmsginfo);
804         g_free(file);
805 }
806
807 static void select_account_cb(GtkWidget *w, gpointer data)
808 {
809         *(gint*)data = GPOINTER_TO_INT(gtk_object_get_user_data(GTK_OBJECT(w)));
810 }
811         
812 static PrefsAccount *select_account_from_list(GList *ac_list)
813 {
814         GtkWidget *optmenu;
815         GtkWidget *menu;
816         gint account_id;
817
818         g_return_val_if_fail(ac_list != NULL, NULL);
819         g_return_val_if_fail(ac_list->data != NULL, NULL);
820         
821         optmenu = gtk_option_menu_new();
822         menu = gtkut_account_menu_new(ac_list, select_account_cb, &account_id);
823         if (!menu)
824                 return NULL;
825         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), menu);
826         gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), 0);
827         account_id = ((PrefsAccount *) ac_list->data)->account_id;
828         if (alertpanel_with_widget(
829                                 _("Return Receipt Notification"),
830                                 _("The message was sent to several of your "
831                                   "accounts.\n"
832                                   "Please choose which account do you want to "
833                                   "use for sending the receipt notification:"),
834                                 _("Send Notification"), _("+Cancel"), NULL,
835                                 optmenu) != G_ALERTDEFAULT)
836                 return NULL;
837         return account_find_from_id(account_id);
838 }
839
840 static void messageview_menubar_cb(gpointer data, guint action, GtkWidget *widget)
841 {
842         MessageView *msgview = (MessageView*)data;
843         MainWindow *mainwin = (MainWindow*)msgview->mainwin;
844
845         g_return_if_fail(mainwin != NULL);
846         reply_cb(mainwin, action, widget);
847 }
848
849 static void messageview_close_cb(gpointer data, guint action, GtkWidget *widget)
850 {
851         MessageView *messageview = (MessageView *)data;
852         
853         gtk_widget_destroy(messageview->window);
854 }