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