e31cb11c5979e9138af7a366d9a0371d585776d1
[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 "textview.h"
38 #include "imageview.h"
39 #include "mimeview.h"
40 #include "procmsg.h"
41 #include "procheader.h"
42 #include "procmime.h"
43 #include "prefs_common.h"
44 #include "gtkutils.h"
45 #include "utils.h"
46 #include "rfc2015.h"
47 #include "account.h"
48 #include "alertpanel.h"
49 #include "send.h"
50 #include "pgptext.h"
51 #include "menu.h"
52
53 static void messageview_change_view_type(MessageView    *messageview,
54                                          MessageType     type);
55 static void messageview_destroy_cb      (GtkWidget      *widget,
56                                          MessageView    *messageview);
57 static void messageview_size_allocate_cb(GtkWidget      *widget,
58                                          GtkAllocation  *allocation);
59 static void key_pressed                 (GtkWidget      *widget,
60                                          GdkEventKey    *event,
61                                          MessageView    *messageview);
62
63 static void return_receipt_show         (NoticeView     *noticeview, 
64                                          MsgInfo        *msginfo);      
65 static void return_receipt_send_clicked (NoticeView     *noticeview, 
66                                          MsgInfo        *msginfo);
67
68 MessageView *messageview_create(void)
69 {
70         MessageView *messageview;
71         GtkWidget *vbox;
72         HeaderView *headerview;
73         TextView *textview;
74         ImageView *imageview;
75         MimeView *mimeview;
76         NoticeView *noticeview;
77
78         debug_print(_("Creating message view...\n"));
79         messageview = g_new0(MessageView, 1);
80
81         messageview->type = MVIEW_TEXT;
82
83         headerview = headerview_create();
84
85         noticeview = noticeview_create();
86
87         textview = textview_create();
88         textview->messageview = messageview;
89
90         imageview = imageview_create();
91         imageview->messageview = messageview;
92
93         mimeview = mimeview_create();
94         mimeview->textview = textview_create();
95         mimeview->textview->messageview = messageview;
96         mimeview->imageview = imageview;
97         mimeview->messageview = messageview;
98
99         vbox = gtk_vbox_new(FALSE, 0);
100         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(headerview),
101                            FALSE, FALSE, 0);
102         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(noticeview),
103                            FALSE, FALSE, 0);
104         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(textview),
105                            TRUE, TRUE, 0);
106
107         /* to remove without destroyed */
108         gtk_widget_ref(GTK_WIDGET_PTR(textview));
109         gtk_widget_ref(GTK_WIDGET_PTR(imageview));
110         gtk_widget_ref(GTK_WIDGET_PTR(mimeview));
111         gtk_widget_ref(GTK_WIDGET_PTR(mimeview->textview));
112
113         messageview->vbox       = vbox;
114         messageview->new_window = FALSE;
115         messageview->window     = NULL;
116         messageview->headerview = headerview;
117         messageview->textview   = textview;
118         messageview->imageview  = imageview;
119         messageview->mimeview   = mimeview;
120         messageview->noticeview = noticeview;
121
122         return messageview;
123 }
124
125 MessageView *messageview_create_with_new_window(void)
126 {
127         GtkWidget *window;
128         MessageView *msgview;
129
130         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
131         gtk_window_set_title(GTK_WINDOW(window), _("Sylpheed - Message View"));
132         gtk_window_set_wmclass(GTK_WINDOW(window), "message_view", "Sylpheed");
133         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
134         gtk_widget_set_usize(window, prefs_common.msgwin_width,
135                              prefs_common.msgwin_height);
136
137         msgview = messageview_create();
138
139         gtk_signal_connect(GTK_OBJECT(window), "size_allocate",
140                            GTK_SIGNAL_FUNC(messageview_size_allocate_cb),
141                            msgview);
142         gtk_signal_connect(GTK_OBJECT(window), "destroy",
143                            GTK_SIGNAL_FUNC(messageview_destroy_cb), msgview);
144         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
145                            GTK_SIGNAL_FUNC(key_pressed), msgview);
146
147         gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET_PTR(msgview));
148         gtk_widget_grab_focus(msgview->textview->text);
149         gtk_widget_show_all(window);
150
151         msgview->new_window = TRUE;
152         msgview->window = window;
153         msgview->visible = TRUE;
154
155         messageview_init(msgview);
156
157         return msgview;
158 }
159
160 void messageview_init(MessageView *messageview)
161 {
162         headerview_init(messageview->headerview);
163         textview_init(messageview->textview);
164         imageview_init(messageview->imageview);
165         mimeview_init(messageview->mimeview);
166         /*messageview_set_font(messageview);*/
167
168         noticeview_hide(messageview->noticeview);
169 }
170
171 static void notification_convert_header(gchar *dest, gint len, 
172                                         const gchar *src_,
173                                         gint header_len)
174 {
175         char *src;
176
177         g_return_if_fail(src_ != NULL);
178         g_return_if_fail(dest != NULL);
179
180         if (len < 1) return;
181
182         Xstrndup_a(src, src_, len, return);
183
184         remove_return(src);
185
186         if (is_ascii_str(src)) {
187                 strncpy2(dest, src, len);
188                 dest[len - 1] = '\0';
189                 return;
190         } else
191                 conv_encode_header(dest, len, src, header_len);
192 }
193
194 static gint disposition_notification_queue(PrefsAccount * account,
195                                            gchar * to, const gchar *file)
196 {
197         FolderItem *queue;
198         gchar *tmp;
199         FILE *fp, *src_fp;
200         gchar buf[BUFFSIZE];
201         gint num;
202
203         debug_print(_("queueing message...\n"));
204         g_return_val_if_fail(account != NULL, -1);
205
206         tmp = g_strdup_printf("%s%cqueue.%d", g_get_tmp_dir(),
207                               G_DIR_SEPARATOR, (gint)file);
208         if ((fp = fopen(tmp, "wb")) == NULL) {
209                 FILE_OP_ERROR(tmp, "fopen");
210                 g_free(tmp);
211                 return -1;
212         }
213         if ((src_fp = fopen(file, "rb")) == NULL) {
214                 FILE_OP_ERROR(file, "fopen");
215                 fclose(fp);
216                 unlink(tmp);
217                 g_free(tmp);
218                 return -1;
219         }
220         if (change_file_mode_rw(fp, tmp) < 0) {
221                 FILE_OP_ERROR(tmp, "chmod");
222                 g_warning(_("can't change file mode\n"));
223         }
224
225         /* queueing variables */
226         fprintf(fp, "AF:\n");
227         fprintf(fp, "NF:0\n");
228         fprintf(fp, "PS:10\n");
229         fprintf(fp, "SRH:1\n");
230         fprintf(fp, "SFN:\n");
231         fprintf(fp, "DSR:\n");
232         fprintf(fp, "MID:\n");
233         fprintf(fp, "CFG:\n");
234         fprintf(fp, "PT:0\n");
235         fprintf(fp, "S:%s\n", account->address);
236         fprintf(fp, "RQ:\n");
237         if (account->smtp_server)
238                 fprintf(fp, "SSV:%s\n", account->smtp_server);
239         else
240                 fprintf(fp, "SSV:\n");
241         if (account->nntp_server)
242                 fprintf(fp, "NSV:%s\n", account->nntp_server);
243         else
244                 fprintf(fp, "NSV:\n");
245         fprintf(fp, "SSH:\n");
246         fprintf(fp, "R:<%s>", to);
247         fprintf(fp, "\n");
248         fprintf(fp, "\n");
249
250         while (fgets(buf, sizeof(buf), src_fp) != NULL) {
251                 if (fputs(buf, fp) == EOF) {
252                         FILE_OP_ERROR(tmp, "fputs");
253                         fclose(fp);
254                         fclose(src_fp);
255                         unlink(tmp);
256                         g_free(tmp);
257                         return -1;
258                 }
259         }
260
261         fclose(src_fp);
262         if (fclose(fp) == EOF) {
263                 FILE_OP_ERROR(tmp, "fclose");
264                 unlink(tmp);
265                 g_free(tmp);
266                 return -1;
267         }
268
269         queue = folder_get_default_queue();
270         if ((num = folder_item_add_msg(queue, tmp, TRUE)) < 0) {
271                 g_warning(_("can't queue the message\n"));
272                 unlink(tmp);
273                 g_free(tmp);
274                 return -1;
275         }
276         g_free(tmp);
277
278         folderview_update_item(queue, TRUE);
279
280         return 0;
281 }
282
283 static gint disposition_notification_send(MsgInfo * msginfo)
284 {
285         gchar buf[BUFFSIZE];
286         gchar tmp[MAXPATHLEN + 1];
287         FILE *fp;
288         GSList * to_list;
289         gint ok;
290         gchar * to;
291
292         if ((!msginfo->returnreceiptto) && 
293             (!msginfo->dispositionnotificationto)) 
294                 return -1;
295
296         /* write to temporary file */
297         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
298                    get_rc_dir(), G_DIR_SEPARATOR, (gint)msginfo);
299
300         if ((fp = fopen(tmp, "wb")) == NULL) {
301                 FILE_OP_ERROR(tmp, "fopen");
302                 return -1;
303         }
304
305         /* chmod for security */
306         if (change_file_mode_rw(fp, tmp) < 0) {
307                 FILE_OP_ERROR(tmp, "chmod");
308                 g_warning(_("can't change file mode\n"));
309         }
310
311         /* Date */
312         get_rfc822_date(buf, sizeof(buf));
313         fprintf(fp, "Date: %s\n", buf);
314
315         /* From */
316         if (cur_account->name && *cur_account->name) {
317                 notification_convert_header
318                         (buf, sizeof(buf), cur_account->name,
319                          strlen("From: "));
320                 fprintf(fp, "From: %s <%s>\n", buf, cur_account->address);
321         } else
322                 fprintf(fp, "From: %s\n", cur_account->address);
323
324         /* To */
325         if (msginfo->dispositionnotificationto)
326                 to = msginfo->dispositionnotificationto;
327         else
328                 to = msginfo->returnreceiptto;
329         fprintf(fp, "To: %s\n", to);
330
331         /* Subject */
332         notification_convert_header(buf, sizeof(buf), msginfo->subject,
333                                     strlen("Subject: "));
334         fprintf(fp, "Subject: Disposition notification: %s\n", buf);
335
336         if (fclose(fp) == EOF) {
337                 FILE_OP_ERROR(tmp, "fclose");
338                 unlink(tmp);
339                 return -1;
340         }
341
342         to_list = address_list_append(NULL, to);
343         ok = send_message(tmp, cur_account, to_list);
344         
345         if (ok < 0) {
346                 if (prefs_common.queue_msg) {
347                         AlertValue val;
348                         
349                         val = alertpanel
350                                 (_("Queueing"),
351                                  _("Error occurred while sending the notification.\n"
352                                    "Put this notification into queue folder?"),
353                                  _("OK"), _("Cancel"), NULL);
354                         if (G_ALERTDEFAULT == val) {
355                                 ok = disposition_notification_queue(cur_account, to, tmp);
356                                 if (ok < 0)
357                                         alertpanel_error(_("Can't queue the notification."));
358                         }
359                 } else
360                         alertpanel_error(_("Error occurred while sending the notification."));
361         }
362
363         if (unlink(tmp) < 0) FILE_OP_ERROR(tmp, "unlink");
364
365         return ok;
366 }
367
368 void messageview_show(MessageView *messageview, MsgInfo *msginfo,
369                       gboolean all_headers)
370 {
371         FILE *fp;
372         gchar *file;
373         MimeInfo *mimeinfo;
374         MsgInfo *tmpmsginfo;
375
376         g_return_if_fail(msginfo != NULL);
377
378 #if USE_GPGME
379         if ((fp = procmsg_open_message_decrypted(msginfo, &mimeinfo)) == NULL)
380                 return;
381 #else /* !USE_GPGME */
382         if ((fp = procmsg_open_message(msginfo)) == NULL) return;
383         mimeinfo = procmime_scan_mime_header(fp);
384 #endif /* USE_GPGME */
385         fclose(fp);
386         if (!mimeinfo) return;
387
388         file = procmsg_get_message_file_path(msginfo);
389         if (!file) {
390                 g_warning(_("can't get message file path.\n"));
391                 procmime_mimeinfo_free(mimeinfo);
392                 return;
393         }
394
395         tmpmsginfo = procheader_parse_file(file, msginfo->flags, TRUE, TRUE);
396
397         headerview_show(messageview->headerview, tmpmsginfo);
398         procmsg_msginfo_free(tmpmsginfo);
399
400         textview_set_all_headers(messageview->textview, all_headers);
401         textview_set_all_headers(messageview->mimeview->textview, all_headers);
402
403         if (mimeinfo->mime_type != MIME_TEXT &&
404             mimeinfo->mime_type != MIME_TEXT_HTML) {
405                 messageview_change_view_type(messageview, MVIEW_MIME);
406                 mimeview_show_message(messageview->mimeview, mimeinfo, file);
407         } else {
408                 messageview_change_view_type(messageview, MVIEW_TEXT);
409                 textview_show_message(messageview->textview, mimeinfo, file);
410                 procmime_mimeinfo_free(mimeinfo);
411         }
412
413         if (MSG_IS_RETRCPT_PENDING(msginfo->flags))
414                 return_receipt_show(messageview->noticeview, msginfo);
415         else 
416                 noticeview_hide(messageview->noticeview);
417
418         g_free(file);
419 }
420
421 static void messageview_change_view_type(MessageView *messageview,
422                                          MessageType type)
423 {
424         TextView *textview = messageview->textview;
425         MimeView *mimeview = messageview->mimeview;
426
427         if (messageview->type == type) return;
428
429         if (type == MVIEW_MIME) {
430                 gtkut_container_remove
431                         (GTK_CONTAINER(GTK_WIDGET_PTR(messageview)),
432                          GTK_WIDGET_PTR(textview));
433                 gtk_box_pack_start(GTK_BOX(messageview->vbox),
434                                    GTK_WIDGET_PTR(mimeview), TRUE, TRUE, 0);
435                 gtk_container_add(GTK_CONTAINER(mimeview->vbox),
436                                   GTK_WIDGET_PTR(textview));
437         } else if (type == MVIEW_TEXT) {
438                 gtkut_container_remove
439                         (GTK_CONTAINER(GTK_WIDGET_PTR(messageview)),
440                          GTK_WIDGET_PTR(mimeview));
441
442                 if (mimeview->vbox == GTK_WIDGET_PTR(textview)->parent)
443                         gtkut_container_remove(GTK_CONTAINER(mimeview->vbox),
444                                                GTK_WIDGET_PTR(textview));
445
446                 gtk_box_pack_start(GTK_BOX(messageview->vbox),
447                                    GTK_WIDGET_PTR(textview), TRUE, TRUE, 0);
448         } else
449                 return;
450
451         messageview->type = type;
452 }
453
454 void messageview_clear(MessageView *messageview)
455 {
456         messageview_change_view_type(messageview, MVIEW_TEXT);
457         headerview_clear(messageview->headerview);
458         textview_clear(messageview->textview);
459         imageview_clear(messageview->imageview);
460         noticeview_hide(messageview->noticeview);
461 }
462
463 void messageview_destroy(MessageView *messageview)
464 {
465         GtkWidget *textview  = GTK_WIDGET_PTR(messageview->textview);
466         GtkWidget *imageview = GTK_WIDGET_PTR(messageview->imageview);
467         GtkWidget *mimeview  = GTK_WIDGET_PTR(messageview->mimeview);
468
469         headerview_destroy(messageview->headerview);
470         textview_destroy(messageview->textview);
471         imageview_destroy(messageview->imageview);
472         mimeview_destroy(messageview->mimeview);
473         noticeview_destroy(messageview->noticeview);
474
475         g_free(messageview);
476
477         gtk_widget_unref(textview);
478         gtk_widget_unref(imageview);
479         gtk_widget_unref(mimeview);
480 }
481
482 void messageview_quote_color_set(void)
483 {
484 }
485
486 void messageview_set_font(MessageView *messageview)
487 {
488         textview_set_font(messageview->textview, NULL);
489 }
490
491 TextView *messageview_get_current_textview(MessageView *messageview)
492 {
493         TextView *text = NULL;
494
495         if (messageview->type == MVIEW_TEXT)
496                 text = messageview->textview;
497         else if (messageview->type == MVIEW_MIME) {
498                 if (gtk_notebook_get_current_page
499                         (GTK_NOTEBOOK(messageview->mimeview->notebook)) == 0)
500                         text = messageview->textview;
501                 else if (messageview->mimeview->type == MIMEVIEW_TEXT)
502                         text = messageview->mimeview->textview;
503         }
504
505         return text;
506 }
507
508 void messageview_copy_clipboard(MessageView *messageview)
509 {
510         TextView *text;
511
512         text = messageview_get_current_textview(messageview);
513         if (text)
514                 gtk_editable_copy_clipboard(GTK_EDITABLE(text->text));
515 }
516
517 void messageview_select_all(MessageView *messageview)
518 {
519         TextView *text;
520
521         text = messageview_get_current_textview(messageview);
522         if (text)
523                 gtk_editable_select_region(GTK_EDITABLE(text->text), 0, -1);
524 }
525
526 void messageview_set_position(MessageView *messageview, gint pos)
527 {
528         textview_set_position(messageview->textview, pos);
529 }
530
531 gboolean messageview_search_string(MessageView *messageview, const gchar *str,
532                                    gboolean case_sens)
533 {
534         return textview_search_string(messageview->textview, str, case_sens);
535         return FALSE;
536 }
537
538 gboolean messageview_search_string_backward(MessageView *messageview,
539                                             const gchar *str,
540                                             gboolean case_sens)
541 {
542         return textview_search_string_backward(messageview->textview,
543                                                str, case_sens);
544         return FALSE;
545 }
546
547 gboolean messageview_is_visible(MessageView *messageview)
548 {
549         return messageview->visible;
550 }
551
552 static void messageview_destroy_cb(GtkWidget *widget, MessageView *messageview)
553 {
554         messageview_destroy(messageview);
555 }
556
557 static void messageview_size_allocate_cb(GtkWidget *widget,
558                                          GtkAllocation *allocation)
559 {
560         g_return_if_fail(allocation != NULL);
561
562         prefs_common.msgwin_width  = allocation->width;
563         prefs_common.msgwin_height = allocation->height;
564 }
565
566 static void key_pressed(GtkWidget *widget, GdkEventKey *event,
567                         MessageView *messageview)
568 {
569         if (event && event->keyval == GDK_Escape && messageview->window)
570                 gtk_widget_destroy(messageview->window);
571 }
572
573 void messageview_toggle_view_real(MessageView *messageview)
574 {
575         MainWindow *mainwin = messageview->mainwin;
576         union CompositeWin *cwin = &mainwin->win;
577         GtkWidget *vpaned = NULL;
578         GtkWidget *container = NULL;
579         GtkItemFactory *ifactory =gtk_item_factory_from_widget(mainwin->menubar);
580         
581         switch (mainwin->type) {
582         case SEPARATE_NONE:
583                 vpaned = cwin->sep_none.vpaned;
584                 container = cwin->sep_none.hpaned;
585                 break;
586         case SEPARATE_FOLDER:
587                 vpaned = cwin->sep_folder.vpaned;
588                 container = mainwin->vbox_body;
589                 break;
590         case SEPARATE_MESSAGE:
591         case SEPARATE_BOTH:
592                 return;
593         }
594
595         if (vpaned->parent != NULL) {
596                 gtk_widget_ref(vpaned);
597                 gtkut_container_remove(GTK_CONTAINER(container), vpaned);
598                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), container);
599                 menu_set_sensitive(ifactory, "/View/Expand Summary View", FALSE);
600                 gtk_widget_grab_focus(GTK_WIDGET(messageview->textview->text));
601         } else {
602                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), vpaned);
603                 gtk_container_add(GTK_CONTAINER(container), vpaned);
604                 gtk_widget_unref(vpaned);
605                 menu_set_sensitive(ifactory, "/View/Expand Summary View", TRUE);
606                 gtk_widget_grab_focus(GTK_WIDGET(mainwin->summaryview->ctree));
607         }
608 }
609
610 static void return_receipt_show(NoticeView *noticeview, MsgInfo *msginfo)
611 {
612         noticeview_set_text(noticeview, _("This messages asks for a return receipt."));
613         noticeview_set_button_text(noticeview, _("Send receipt"));
614         noticeview_set_button_press_callback(noticeview,
615                                              GTK_SIGNAL_FUNC(return_receipt_send_clicked),
616                                              (gpointer) msginfo);
617         noticeview_show(noticeview);
618 }
619
620 static void return_receipt_send_clicked(NoticeView *noticeview, MsgInfo *msginfo)
621 {
622         MsgInfo *tmpmsginfo;
623         gchar *file;
624
625         file = procmsg_get_message_file_path(msginfo);
626         if (!file) {
627                 g_warning(_("can't get message file path.\n"));
628                 return;
629         }
630
631         tmpmsginfo = procheader_parse_file(file, msginfo->flags, TRUE, TRUE);
632         if (disposition_notification_send(tmpmsginfo) >= 0) {
633                 procmsg_msginfo_unset_flags(msginfo, MSG_RETRCPT_PENDING, 0);
634                 noticeview_hide(noticeview);
635         }               
636
637         procmsg_msginfo_free(tmpmsginfo);
638         g_free(file);
639 }
640
641