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