1b3701e0f1a326d8e534daa2f483893c46407093
[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 static void messageview_toolbar_create  (MessageView    *messageview,
69                                          GtkWidget      *container);
70 static void toolbar_messageview_buttons_cb  (GtkWidget      *widget, 
71                                              ToolbarItem    *item);
72
73 static void return_receipt_show         (NoticeView     *noticeview, 
74                                          MsgInfo        *msginfo);      
75 static void return_receipt_send_clicked (NoticeView     *noticeview, 
76                                          MsgInfo        *msginfo);
77
78 static PrefsAccount *select_account_from_list
79                                         (GList          *ac_list);
80
81 static void messageview_reply_cb        (gpointer        data,
82                                          guint           action,
83                                          GtkWidget      *widget);
84                                          
85 static void messageview_delete_cb       (gpointer        data,
86                                          guint           action,
87                                          GtkWidget      *widget);
88                                          
89 static void messageview_close_cb        (gpointer        data,
90                                          guint           action,
91                                          GtkWidget      *widget);
92
93 MessageView *messageview_create(MainWindow *mainwin)
94 {
95         MessageView *messageview;
96         GtkWidget *vbox;
97         HeaderView *headerview;
98         TextView *textview;
99         ImageView *imageview;
100         MimeView *mimeview;
101         NoticeView *noticeview;
102
103         debug_print("Creating message view...\n");
104         messageview = g_new0(MessageView, 1);
105
106         messageview->type = MVIEW_TEXT;
107
108         headerview = headerview_create();
109
110         noticeview = noticeview_create(mainwin);
111
112         textview = textview_create();
113         textview->messageview = messageview;
114
115         imageview = imageview_create();
116         imageview->messageview = messageview;
117
118         mimeview = mimeview_create();
119         mimeview->textview = textview_create();
120         mimeview->textview->messageview = messageview;
121         mimeview->imageview = imageview;
122         mimeview->messageview = messageview;
123
124         vbox = gtk_vbox_new(FALSE, 0);
125         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(headerview),
126                            FALSE, FALSE, 0);
127         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(noticeview),
128                            FALSE, FALSE, 0);
129         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(textview),
130                            TRUE, TRUE, 0);
131
132         /* to remove without destroyed */
133         gtk_widget_ref(GTK_WIDGET_PTR(textview));
134         gtk_widget_ref(GTK_WIDGET_PTR(imageview));
135         gtk_widget_ref(GTK_WIDGET_PTR(mimeview));
136         gtk_widget_ref(GTK_WIDGET_PTR(mimeview->textview));
137
138         messageview->vbox       = vbox;
139         messageview->new_window = FALSE;
140         messageview->window     = NULL;
141         messageview->headerview = headerview;
142         messageview->textview   = textview;
143         messageview->imageview  = imageview;
144         messageview->mimeview   = mimeview;
145         messageview->noticeview = noticeview;
146         messageview->mainwin    = mainwin;
147
148         return messageview;
149 }
150
151 static GtkItemFactoryEntry messageview_entries[] =
152 {
153         {N_("/_File"),                          NULL, NULL, 0, "<Branch>"},
154         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
155         {N_("/_File/_Close"),                   "<control>W", messageview_close_cb, 0, NULL},
156
157         {N_("/_Message"),               NULL, NULL, 0, "<Branch>"},
158         {N_("/_Message/_Reply"),                "<control>R",   messageview_reply_cb, COMPOSE_REPLY, NULL},
159         {N_("/_Message/Repl_y to"),             NULL, NULL, 0, "<Branch>"},
160         {N_("/_Message/Repl_y to/_all"),        "<shift><control>R", messageview_reply_cb, COMPOSE_REPLY_TO_ALL, NULL},
161         {N_("/_Message/Repl_y to/_sender"),     NULL, messageview_reply_cb, COMPOSE_REPLY_TO_SENDER, NULL},
162         {N_("/_Message/Repl_y to/mailing _list"),
163                                                 "<control>L", messageview_reply_cb, COMPOSE_REPLY_TO_LIST, NULL},
164         {N_("/_Message/Follow-up and reply to"),NULL, messageview_reply_cb, COMPOSE_FOLLOWUP_AND_REPLY_TO, NULL},
165         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
166         {N_("/_Message/_Delete"),               "<control>D", messageview_delete_cb,  0, NULL},
167         
168         {N_("/_Help"),                  NULL, NULL, 0, "<Branch>"},
169         {N_("/_Help/_About"),           NULL, about_show, 0, NULL}
170 };
171
172
173
174 MessageView *messageview_create_with_new_window(MainWindow *mainwin)
175 {
176         GtkWidget *window;
177         MessageView *msgview;
178         GtkWidget *vbox;
179         GtkWidget *menubar;
180         GtkWidget *handlebox;
181
182         guint n_menu_entries;
183
184         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
185         gtk_window_set_title(GTK_WINDOW(window), _("Sylpheed - Message View"));
186         gtk_window_set_wmclass(GTK_WINDOW(window), "message_view", "Sylpheed");
187         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
188         gtk_widget_set_usize(window, prefs_common.msgwin_width,
189                              prefs_common.msgwin_height);
190
191         vbox = gtk_vbox_new(FALSE, 0);
192         gtk_widget_show(vbox);
193         gtk_container_add(GTK_CONTAINER(window), vbox);
194         
195         msgview = messageview_create(mainwin);
196
197         gtk_signal_connect(GTK_OBJECT(window), "size_allocate",
198                            GTK_SIGNAL_FUNC(messageview_size_allocate_cb),
199                            msgview);
200         gtk_signal_connect(GTK_OBJECT(window), "destroy",
201                            GTK_SIGNAL_FUNC(messageview_destroy_cb), msgview);
202         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
203                            GTK_SIGNAL_FUNC(key_pressed), msgview);
204         gtk_signal_connect(GTK_OBJECT(window), "focus_in_event",
205                            GTK_SIGNAL_FUNC(focus_in), msgview);
206
207         n_menu_entries = sizeof(messageview_entries) / sizeof(messageview_entries[0]);
208         menubar = menubar_create(window, messageview_entries,
209                                  n_menu_entries, "<MessageView>", msgview);
210         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
211
212         handlebox = gtk_handle_box_new();
213         gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
214         messageview_toolbar_create(msgview,handlebox);
215
216         gtk_container_add(GTK_CONTAINER(vbox), GTK_WIDGET_PTR(msgview));
217         gtk_widget_grab_focus(msgview->textview->text);
218         gtk_widget_show_all(window);
219
220         msgview->new_window = TRUE;
221         msgview->window = window;
222         msgview->visible = TRUE;
223         msgview->menubar = menubar; 
224         msgview->handlebox = handlebox;
225
226         
227         messageview_init(msgview);
228         common_toolbar_set_style(msgview, TOOLBAR_MSGVIEW);
229
230         return msgview;
231 }
232
233 void messageview_init(MessageView *messageview)
234 {
235         headerview_init(messageview->headerview);
236         textview_init(messageview->textview);
237         imageview_init(messageview->imageview);
238         mimeview_init(messageview->mimeview);
239         /*messageview_set_font(messageview);*/
240
241         noticeview_hide(messageview->noticeview);
242 }
243
244 static void notification_convert_header(gchar *dest, gint len, 
245                                         const gchar *src_,
246                                         gint header_len)
247 {
248         char *src;
249
250         g_return_if_fail(src_ != NULL);
251         g_return_if_fail(dest != NULL);
252
253         if (len < 1) return;
254
255         Xstrndup_a(src, src_, len, return);
256
257         remove_return(src);
258
259         if (is_ascii_str(src)) {
260                 strncpy2(dest, src, len);
261                 dest[len - 1] = '\0';
262                 return;
263         } else
264                 conv_encode_header(dest, len, src, header_len);
265 }
266
267 static gint disposition_notification_queue(PrefsAccount * account,
268                                            gchar * to, const gchar *file)
269 {
270         FolderItem *queue;
271         gchar *tmp;
272         FILE *fp, *src_fp;
273         gchar buf[BUFFSIZE];
274         gint num;
275
276         debug_print("queueing message...\n");
277         g_return_val_if_fail(account != NULL, -1);
278
279         tmp = g_strdup_printf("%s%cqueue.%d", g_get_tmp_dir(),
280                               G_DIR_SEPARATOR, (gint)file);
281         if ((fp = fopen(tmp, "wb")) == NULL) {
282                 FILE_OP_ERROR(tmp, "fopen");
283                 g_free(tmp);
284                 return -1;
285         }
286         if ((src_fp = fopen(file, "rb")) == NULL) {
287                 FILE_OP_ERROR(file, "fopen");
288                 fclose(fp);
289                 unlink(tmp);
290                 g_free(tmp);
291                 return -1;
292         }
293         if (change_file_mode_rw(fp, tmp) < 0) {
294                 FILE_OP_ERROR(tmp, "chmod");
295                 g_warning(_("can't change file mode\n"));
296         }
297
298         /* queueing variables */
299         fprintf(fp, "AF:\n");
300         fprintf(fp, "NF:0\n");
301         fprintf(fp, "PS:10\n");
302         fprintf(fp, "SRH:1\n");
303         fprintf(fp, "SFN:\n");
304         fprintf(fp, "DSR:\n");
305         fprintf(fp, "MID:\n");
306         fprintf(fp, "CFG:\n");
307         fprintf(fp, "PT:0\n");
308         fprintf(fp, "S:%s\n", account->address);
309         fprintf(fp, "RQ:\n");
310         if (account->smtp_server)
311                 fprintf(fp, "SSV:%s\n", account->smtp_server);
312         else
313                 fprintf(fp, "SSV:\n");
314         if (account->nntp_server)
315                 fprintf(fp, "NSV:%s\n", account->nntp_server);
316         else
317                 fprintf(fp, "NSV:\n");
318         fprintf(fp, "SSH:\n");
319         fprintf(fp, "R:<%s>", to);
320         fprintf(fp, "\n");
321         fprintf(fp, "\n");
322
323         while (fgets(buf, sizeof(buf), src_fp) != NULL) {
324                 if (fputs(buf, fp) == EOF) {
325                         FILE_OP_ERROR(tmp, "fputs");
326                         fclose(fp);
327                         fclose(src_fp);
328                         unlink(tmp);
329                         g_free(tmp);
330                         return -1;
331                 }
332         }
333
334         fclose(src_fp);
335         if (fclose(fp) == EOF) {
336                 FILE_OP_ERROR(tmp, "fclose");
337                 unlink(tmp);
338                 g_free(tmp);
339                 return -1;
340         }
341
342         queue = folder_get_default_queue();
343         if ((num = folder_item_add_msg(queue, tmp, TRUE)) < 0) {
344                 g_warning(_("can't queue the message\n"));
345                 unlink(tmp);
346                 g_free(tmp);
347                 return -1;
348         }
349         g_free(tmp);
350
351         folder_update_item(queue, TRUE);
352
353         return 0;
354 }
355
356 static gint disposition_notification_send(MsgInfo *msginfo)
357 {
358         gchar buf[BUFFSIZE];
359         gchar tmp[MAXPATHLEN + 1];
360         FILE *fp;
361         GSList *to_list;
362         GList *ac_list;
363         PrefsAccount *account;
364         gint ok;
365         gchar *to;
366
367         if ((!msginfo->returnreceiptto) && 
368             (!msginfo->dispositionnotificationto)) 
369                 return -1;
370
371         /* RFC2298: Test for Return-Path */
372         if (msginfo->dispositionnotificationto)
373                 to = msginfo->dispositionnotificationto;
374         else
375                 to = msginfo->returnreceiptto;
376
377         ok = get_header_from_msginfo(msginfo, buf, sizeof(buf),
378                                 "Return-Path:");
379         if (ok == 0) {
380                 gchar *to_addr = g_strdup(to);
381                 extract_address(to_addr);
382                 extract_address(buf);
383                 ok = strcmp(to_addr, buf);
384                 g_free(to_addr);
385         } else {
386                 strncpy(buf, _("<No Return-Path found>"), 
387                                 sizeof(buf));
388         }
389         
390         if (ok != 0) {
391                 AlertValue val;
392                 gchar *message;
393                 message = g_strdup_printf(
394                                  _("The notification address to which the "
395                                    "return receipt is to be sent\n"
396                                    "does not correspond to the return path:\n"
397                                    "Notification address: %s\n"
398                                    "Return path: %s\n"
399                                    "It is advised to not to send the return "
400                                    "receipt."), to, buf);
401                 val = alertpanel(_("Warning"), message, _("Send"),
402                                 _("+Don't Send"), NULL);
403                 if (val != G_ALERTDEFAULT)
404                         return -1;
405         }
406
407         ac_list = account_find_all_from_address(NULL, msginfo->to);
408         ac_list = account_find_all_from_address(ac_list, msginfo->cc);
409
410         if (ac_list == NULL) {
411                 alertpanel_error(_("This message is asking for a return "
412                                    "receipt notification\n"
413                                    "but according to its 'To:' and 'CC:' "
414                                    "headers it was not\nofficially addressed "
415                                    "to you.\n"
416                                    "Receipt notification cancelled."));
417                 return -1;
418         }
419
420         if (g_list_length(ac_list) > 1)
421                 account = select_account_from_list(ac_list);
422         else
423                 account = (PrefsAccount *) ac_list->data;
424         g_list_free(ac_list);
425
426         if (account == NULL)
427                 return -1;
428
429         /* write to temporary file */
430         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
431                    get_rc_dir(), G_DIR_SEPARATOR, (gint)msginfo);
432
433         if ((fp = fopen(tmp, "wb")) == NULL) {
434                 FILE_OP_ERROR(tmp, "fopen");
435                 return -1;
436         }
437
438         /* chmod for security */
439         if (change_file_mode_rw(fp, tmp) < 0) {
440                 FILE_OP_ERROR(tmp, "chmod");
441                 g_warning(_("can't change file mode\n"));
442         }
443
444         /* Date */
445         get_rfc822_date(buf, sizeof(buf));
446         fprintf(fp, "Date: %s\n", buf);
447
448         /* From */
449         if (account->name && *account->name) {
450                 notification_convert_header
451                         (buf, sizeof(buf), account->name,
452                          strlen("From: "));
453                 fprintf(fp, "From: %s <%s>\n", buf, account->address);
454         } else
455                 fprintf(fp, "From: %s\n", account->address);
456
457         fprintf(fp, "To: %s\n", to);
458
459         /* Subject */
460         notification_convert_header(buf, sizeof(buf), msginfo->subject,
461                                     strlen("Subject: "));
462         fprintf(fp, "Subject: Disposition notification: %s\n", buf);
463
464         if (fclose(fp) == EOF) {
465                 FILE_OP_ERROR(tmp, "fclose");
466                 unlink(tmp);
467                 return -1;
468         }
469
470         to_list = address_list_append(NULL, to);
471         ok = send_message(tmp, account, to_list);
472         
473         if (ok < 0) {
474                 if (prefs_common.queue_msg) {
475                         AlertValue val;
476                         
477                         val = alertpanel
478                                 (_("Queueing"),
479                                  _("Error occurred while sending the notification.\n"
480                                    "Put this notification into queue folder?"),
481                                  _("OK"), _("Cancel"), NULL);
482                         if (G_ALERTDEFAULT == val) {
483                                 ok = disposition_notification_queue(account, to, tmp);
484                                 if (ok < 0)
485                                         alertpanel_error(_("Can't queue the notification."));
486                         }
487                 } else
488                         alertpanel_error(_("Error occurred while sending the notification."));
489         }
490
491         if (unlink(tmp) < 0) FILE_OP_ERROR(tmp, "unlink");
492
493         return ok;
494 }
495
496 void messageview_show(MessageView *messageview, MsgInfo *msginfo,
497                       gboolean all_headers)
498 {
499         FILE *fp;
500         gchar *file;
501         MimeInfo *mimeinfo;
502         MsgInfo *tmpmsginfo;
503
504         g_return_if_fail(msginfo != NULL);
505         messageview->msginfo = msginfo;
506
507 #if USE_GPGME
508         if ((fp = procmsg_open_message_decrypted(msginfo, &mimeinfo)) == NULL)
509                 return;
510 #else /* !USE_GPGME */
511         if ((fp = procmsg_open_message(msginfo)) == NULL) return;
512         mimeinfo = procmime_scan_mime_header(fp);
513 #endif /* USE_GPGME */
514         fclose(fp);
515         if (!mimeinfo) return;
516
517         file = procmsg_get_message_file_path(msginfo);
518         if (!file) {
519                 g_warning(_("can't get message file path.\n"));
520                 procmime_mimeinfo_free_all(mimeinfo);
521                 return;
522         }
523
524         tmpmsginfo = procheader_parse_file(file, msginfo->flags, TRUE, TRUE);
525
526         headerview_show(messageview->headerview, tmpmsginfo);
527         procmsg_msginfo_free(tmpmsginfo);
528
529         messageview->all_headers = all_headers;
530         textview_set_all_headers(messageview->textview, all_headers);
531         textview_set_all_headers(messageview->mimeview->textview, all_headers);
532
533         if (mimeinfo->mime_type != MIME_TEXT &&
534             mimeinfo->mime_type != MIME_TEXT_HTML) {
535                 messageview_change_view_type(messageview, MVIEW_MIME);
536                 mimeview_show_message(messageview->mimeview, mimeinfo, file);
537         } else {
538                 messageview_change_view_type(messageview, MVIEW_TEXT);
539                 textview_show_message(messageview->textview, mimeinfo, file);
540                 procmime_mimeinfo_free_all(mimeinfo);
541         }
542
543         if (MSG_IS_RETRCPT_PENDING(msginfo->flags))
544                 return_receipt_show(messageview->noticeview, msginfo);
545         else 
546                 noticeview_hide(messageview->noticeview);
547
548         g_free(file);
549 }
550
551 static void messageview_change_view_type(MessageView *messageview,
552                                          MessageType type)
553 {
554         TextView *textview = messageview->textview;
555         MimeView *mimeview = messageview->mimeview;
556
557         if (messageview->type == type) return;
558
559         if (type == MVIEW_MIME) {
560                 gtkut_container_remove
561                         (GTK_CONTAINER(GTK_WIDGET_PTR(messageview)),
562                          GTK_WIDGET_PTR(textview));
563                 gtk_box_pack_start(GTK_BOX(messageview->vbox),
564                                    GTK_WIDGET_PTR(mimeview), TRUE, TRUE, 0);
565                 gtk_container_add(GTK_CONTAINER(mimeview->vbox),
566                                   GTK_WIDGET_PTR(textview));
567         } else if (type == MVIEW_TEXT) {
568                 gtkut_container_remove
569                         (GTK_CONTAINER(GTK_WIDGET_PTR(messageview)),
570                          GTK_WIDGET_PTR(mimeview));
571
572                 if (mimeview->vbox == GTK_WIDGET_PTR(textview)->parent)
573                         gtkut_container_remove(GTK_CONTAINER(mimeview->vbox),
574                                                GTK_WIDGET_PTR(textview));
575
576                 gtk_box_pack_start(GTK_BOX(messageview->vbox),
577                                    GTK_WIDGET_PTR(textview), TRUE, TRUE, 0);
578         } else
579                 return;
580
581         messageview->type = type;
582 }
583
584 void messageview_clear(MessageView *messageview)
585 {
586         messageview_change_view_type(messageview, MVIEW_TEXT);
587         headerview_clear(messageview->headerview);
588         textview_clear(messageview->textview);
589         imageview_clear(messageview->imageview);
590         noticeview_hide(messageview->noticeview);
591 }
592
593 void messageview_destroy(MessageView *messageview)
594 {
595         GtkWidget *textview  = GTK_WIDGET_PTR(messageview->textview);
596         GtkWidget *imageview = GTK_WIDGET_PTR(messageview->imageview);
597         GtkWidget *mimeview  = GTK_WIDGET_PTR(messageview->mimeview);
598
599         headerview_destroy(messageview->headerview);
600         textview_destroy(messageview->textview);
601         imageview_destroy(messageview->imageview);
602         mimeview_destroy(messageview->mimeview);
603         noticeview_destroy(messageview->noticeview);
604
605         toolbar_clear_list(TOOLBAR_MSGVIEW);
606         TOOLBAR_DESTROY_ITEMS(messageview->toolbar->item_list); 
607         TOOLBAR_DESTROY_ACTIONS(messageview->toolbar->action_list);
608
609         g_free(messageview);
610
611         gtk_widget_unref(textview);
612         gtk_widget_unref(imageview);
613         gtk_widget_unref(mimeview);
614 }
615
616 void messageview_quote_color_set(void)
617 {
618 }
619
620 void messageview_set_font(MessageView *messageview)
621 {
622         textview_set_font(messageview->textview, NULL);
623 }
624
625 TextView *messageview_get_current_textview(MessageView *messageview)
626 {
627         TextView *text = NULL;
628
629         if (messageview->type == MVIEW_TEXT)
630                 text = messageview->textview;
631         else if (messageview->type == MVIEW_MIME) {
632                 if (gtk_notebook_get_current_page
633                         (GTK_NOTEBOOK(messageview->mimeview->notebook)) == 0)
634                         text = messageview->textview;
635                 else if (messageview->mimeview->type == MIMEVIEW_TEXT)
636                         text = messageview->mimeview->textview;
637         }
638
639         return text;
640 }
641
642 void messageview_copy_clipboard(MessageView *messageview)
643 {
644         TextView *text;
645
646         text = messageview_get_current_textview(messageview);
647         if (text)
648                 gtk_editable_copy_clipboard(GTK_EDITABLE(text->text));
649 }
650
651 void messageview_select_all(MessageView *messageview)
652 {
653         TextView *text;
654
655         text = messageview_get_current_textview(messageview);
656         if (text)
657                 gtk_editable_select_region(GTK_EDITABLE(text->text), 0, -1);
658 }
659
660 void messageview_set_position(MessageView *messageview, gint pos)
661 {
662         textview_set_position(messageview->textview, pos);
663 }
664
665 gboolean messageview_search_string(MessageView *messageview, const gchar *str,
666                                    gboolean case_sens)
667 {
668         return textview_search_string(messageview->textview, str, case_sens);
669         return FALSE;
670 }
671
672 gboolean messageview_search_string_backward(MessageView *messageview,
673                                             const gchar *str,
674                                             gboolean case_sens)
675 {
676         return textview_search_string_backward(messageview->textview,
677                                                str, case_sens);
678         return FALSE;
679 }
680
681 gboolean messageview_is_visible(MessageView *messageview)
682 {
683         return messageview->visible;
684 }
685
686 static void messageview_destroy_cb(GtkWidget *widget, MessageView *messageview)
687 {
688         messageview_destroy(messageview);
689 }
690
691 static void messageview_size_allocate_cb(GtkWidget *widget,
692                                          GtkAllocation *allocation)
693 {
694         g_return_if_fail(allocation != NULL);
695
696         prefs_common.msgwin_width  = allocation->width;
697         prefs_common.msgwin_height = allocation->height;
698 }
699
700 static void key_pressed(GtkWidget *widget, GdkEventKey *event,
701                         MessageView *messageview)
702 {
703         if (event && event->keyval == GDK_Escape && messageview->window)
704                 gtk_widget_destroy(messageview->window);
705 }
706
707 static void focus_in(GtkWidget *widget, GdkEventFocus *event,
708                      gpointer data)
709 {
710         MessageView *msgview = (MessageView*)data;
711
712         summary_select_by_msgnum(msgview->mainwin->summaryview, 
713                                  msgview->msginfo->msgnum);
714 }
715
716 void messageview_toggle_view_real(MessageView *messageview)
717 {
718         MainWindow *mainwin = messageview->mainwin;
719         union CompositeWin *cwin = &mainwin->win;
720         GtkWidget *vpaned = NULL;
721         GtkWidget *container = NULL;
722         GtkItemFactory *ifactory = gtk_item_factory_from_widget(mainwin->menubar);
723         
724         switch (mainwin->type) {
725         case SEPARATE_NONE:
726                 vpaned = cwin->sep_none.vpaned;
727                 container = cwin->sep_none.hpaned;
728                 break;
729         case SEPARATE_FOLDER:
730                 vpaned = cwin->sep_folder.vpaned;
731                 container = mainwin->vbox_body;
732                 break;
733         case SEPARATE_MESSAGE:
734         case SEPARATE_BOTH:
735                 return;
736         }
737
738         if (vpaned->parent != NULL) {
739                 gtk_widget_ref(vpaned);
740                 gtkut_container_remove(GTK_CONTAINER(container), vpaned);
741                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), container);
742                 menu_set_sensitive(ifactory, "/View/Expand Summary View", FALSE);
743                 gtk_widget_grab_focus(GTK_WIDGET(messageview->textview->text));
744         } else {
745                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), vpaned);
746                 gtk_container_add(GTK_CONTAINER(container), vpaned);
747                 gtk_widget_unref(vpaned);
748                 menu_set_sensitive(ifactory, "/View/Expand Summary View", TRUE);
749                 gtk_widget_grab_focus(GTK_WIDGET(mainwin->summaryview->ctree));
750         }
751 }
752
753 static void return_receipt_show(NoticeView *noticeview, MsgInfo *msginfo)
754 {
755         noticeview_set_text(noticeview, _("This messages asks for a return receipt."));
756         noticeview_set_button_text(noticeview, _("Send receipt"));
757         noticeview_set_button_press_callback(noticeview,
758                                              GTK_SIGNAL_FUNC(return_receipt_send_clicked),
759                                              (gpointer) msginfo);
760         noticeview_show(noticeview);
761 }
762
763 static void return_receipt_send_clicked(NoticeView *noticeview, MsgInfo *msginfo)
764 {
765         MsgInfo *tmpmsginfo;
766         gchar *file;
767
768         file = procmsg_get_message_file_path(msginfo);
769         if (!file) {
770                 g_warning(_("can't get message file path.\n"));
771                 return;
772         }
773
774         tmpmsginfo = procheader_parse_file(file, msginfo->flags, TRUE, TRUE);
775         tmpmsginfo->folder = msginfo->folder;
776         tmpmsginfo->msgnum = msginfo->msgnum;
777
778         if (disposition_notification_send(tmpmsginfo) >= 0) {
779                 procmsg_msginfo_unset_flags(msginfo, MSG_RETRCPT_PENDING, 0);
780                 noticeview_hide(noticeview);
781         }               
782
783         procmsg_msginfo_free(tmpmsginfo);
784         g_free(file);
785 }
786
787 static void select_account_cb(GtkWidget *w, gpointer data)
788 {
789         *(gint*)data = GPOINTER_TO_INT(gtk_object_get_user_data(GTK_OBJECT(w)));
790 }
791         
792 static PrefsAccount *select_account_from_list(GList *ac_list)
793 {
794         GtkWidget *optmenu;
795         GtkWidget *menu;
796         gint account_id;
797
798         g_return_val_if_fail(ac_list != NULL, NULL);
799         g_return_val_if_fail(ac_list->data != NULL, NULL);
800         
801         optmenu = gtk_option_menu_new();
802         menu = gtkut_account_menu_new(ac_list, select_account_cb, &account_id);
803         if (!menu)
804                 return NULL;
805         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), menu);
806         gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), 0);
807         account_id = ((PrefsAccount *) ac_list->data)->account_id;
808         if (alertpanel_with_widget(
809                                 _("Return Receipt Notification"),
810                                 _("The message was sent to several of your "
811                                   "accounts.\n"
812                                   "Please choose which account do you want to "
813                                   "use for sending the receipt notification:"),
814                                 _("Send Notification"), _("+Cancel"), NULL,
815                                 optmenu) != G_ALERTDEFAULT)
816                 return NULL;
817         return account_find_from_id(account_id);
818 }
819
820 static void messageview_toolbar_create(MessageView   *messageview, 
821                                        GtkWidget *container)
822 {
823         ToolbarItem *toolbar_item;
824
825         GtkWidget *toolbar;
826         GtkWidget *icon_wid = NULL;
827         GtkWidget *item;
828         GtkTooltips *toolbar_tips;
829         ToolbarSylpheedActions *action_item;
830         GSList *cur;
831         GSList *toolbar_list;
832         toolbar_tips = gtk_tooltips_new();
833         
834         toolbar_read_config_file(TOOLBAR_MSGVIEW);
835         toolbar_list = toolbar_get_list(TOOLBAR_MSGVIEW);
836
837         messageview->toolbar = g_new0(Toolbar, 1); 
838
839         toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
840                                   GTK_TOOLBAR_BOTH);
841         gtk_container_add(GTK_CONTAINER(container), toolbar);
842         gtk_container_set_border_width(GTK_CONTAINER(container), 2);
843         gtk_toolbar_set_button_relief(GTK_TOOLBAR(toolbar), GTK_RELIEF_NONE);
844         gtk_toolbar_set_space_style(GTK_TOOLBAR(toolbar),
845                                     GTK_TOOLBAR_SPACE_LINE);
846         
847         for (cur = toolbar_list; cur != NULL; cur = cur->next) {
848
849                 if (g_strcasecmp(((ToolbarItem*)cur->data)->file, SEPARATOR) == 0) {
850                         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
851                         continue;
852                 }
853                 
854                 toolbar_item = g_new0(ToolbarItem, 1); 
855                 toolbar_item->file = g_strdup(((ToolbarItem*)cur->data)->file);
856                 toolbar_item->text = g_strdup(((ToolbarItem*)cur->data)->text);
857                 toolbar_item->index = ((ToolbarItem*)cur->data)->index;
858
859                 toolbar_item->parent = g_new0(ToolbarParent, 1);
860                 toolbar_item->parent->data = (gpointer)messageview;
861                 toolbar_item->parent->type = TOOLBAR_MSGVIEW;
862
863                 /* collect toolbar items in list to keep track */
864                 messageview->toolbar->item_list = 
865                         g_slist_append(messageview->toolbar->item_list, 
866                                        toolbar_item);
867
868                 icon_wid = stock_pixmap_widget(container, stock_pixmap_get_icon(toolbar_item->file));
869                 item  = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
870                                                 toolbar_item->text,
871                                                 (""),
872                                                 (""),
873                                                 icon_wid, toolbar_messageview_buttons_cb, 
874                                                 toolbar_item);
875                 
876                 switch (toolbar_item->index) {
877                 case A_COMPOSE_EMAIL:
878                         messageview->toolbar->compose_mail_btn = item;
879                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
880                                              messageview->toolbar->compose_mail_btn,
881                                            _("Compose Email"), NULL);
882                         break;
883                 case A_REPLY_MESSAGE:
884                         messageview->toolbar->reply_btn = item;
885                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
886                                              messageview->toolbar->reply_btn,
887                                            _("Reply to Message"), NULL);
888                         break;
889                 case A_REPLY_SENDER:
890                         messageview->toolbar->replysender_btn = item;
891                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
892                                              messageview->toolbar->replysender_btn,
893                                            _("Reply to Sender"), NULL);
894                         break;
895                 case A_REPLY_ALL:
896                         messageview->toolbar->replyall_btn = item;
897                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
898                                              messageview->toolbar->replyall_btn,
899                                            _("Reply to All"), NULL);
900                         break;
901                 case A_REPLY_ML:
902                         messageview->toolbar->replylist_btn = item;
903                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
904                                              messageview->toolbar->replylist_btn,
905                                            _("Reply to Mailing-list"), NULL);
906                         break;
907                 case A_FORWARD:
908                         messageview->toolbar->fwd_btn = item;
909                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
910                                              messageview->toolbar->fwd_btn,
911                                            _("Forward Message"), NULL);
912                         break;
913                 case A_DELETE:
914                         messageview->toolbar->delete_btn = item;
915                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
916                                              messageview->toolbar->delete_btn,
917                                            _("Delete Message"), NULL);
918                         break;
919                 case A_GOTO_NEXT:
920                         messageview->toolbar->next_btn = item;
921                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
922                                              messageview->toolbar->next_btn,
923                                            _("Goto Next Message"), NULL);
924                         break;
925                 case A_SYL_ACTIONS:
926                         action_item = g_new0(ToolbarSylpheedActions, 1);
927                         action_item->widget = item;
928                         action_item->name   = g_strdup(toolbar_item->text);
929
930                         messageview->toolbar->action_list = 
931                                 g_slist_append(messageview->toolbar->action_list,
932                                                action_item);
933
934                         gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tips), 
935                                              item,
936                                              action_item->name, NULL);
937
938                         gtk_widget_show(item);
939                         break;
940                 default:
941                         break;
942                 }
943
944         }
945         messageview->toolbar->toolbar = toolbar;
946
947         gtk_widget_show_all(toolbar);
948 }
949
950 static void toolbar_messageview_buttons_cb(GtkWidget   *widget, 
951                                            ToolbarItem *item)
952 {
953
954         struct {
955                 gint   index;
956                 void (*func)(GtkWidget *widget, gpointer data);
957         } messageview_action[] = {
958                 { A_COMPOSE_EMAIL,      common_toolbar_compose_cb               },
959                 { A_REPLY_MESSAGE,      common_toolbar_reply_cb                 },
960                 { A_REPLY_SENDER,       common_toolbar_reply_to_sender_cb       },
961                 { A_REPLY_ALL,          common_toolbar_reply_to_all_cb          },
962                 { A_REPLY_ML,           common_toolbar_reply_to_list_cb         },
963                 { A_FORWARD,            common_toolbar_forward_cb               },
964                 { A_DELETE,             common_toolbar_delete_cb                },
965                 { A_GOTO_NEXT,          common_toolbar_next_unread_cb           },
966                 { A_SYL_ACTIONS,        common_toolbar_actions_execute_cb       },
967                 };
968
969
970         gint num_items = sizeof(messageview_action)/sizeof(messageview_action[0]);
971         gint i;
972         for (i = 0; i < num_items; i++) {
973                 
974                 if (messageview_action[i].index == item->index) {
975                         messageview_action[i].func(widget, (gpointer)item->parent);
976                         break;
977                 }
978         }
979 }
980
981 void messageview_reply_cb(gpointer data, guint action, GtkWidget *widget)
982 {
983         MessageView *messageview = (MessageView *)data;
984         
985         switch (action) {
986         case COMPOSE_REPLY:
987                 common_toolbar_reply_cb(NULL,messageview);
988                 break;
989         case COMPOSE_REPLY_TO_ALL:
990                 common_toolbar_reply_to_all_cb(NULL,messageview);
991                 break;
992         case COMPOSE_REPLY_TO_SENDER:
993                 common_toolbar_reply_to_sender_cb(NULL,messageview);
994                 break;
995         case COMPOSE_REPLY_TO_LIST:
996                 common_toolbar_reply_to_list_cb(NULL,messageview);
997                 break;
998         }
999 }
1000
1001 static void messageview_delete_cb(gpointer data, guint action, GtkWidget *widget)
1002 {
1003         MessageView *messageview = (MessageView *)data;
1004
1005         common_toolbar_delete_cb(NULL,messageview);
1006 }
1007
1008 static void messageview_close_cb(gpointer data, guint action, GtkWidget *widget)
1009 {
1010         MessageView *messageview = (MessageView *)data;
1011
1012         gtk_widget_destroy(messageview->window);
1013 }