perform a sync to bring in Hiro's separate view implementation
[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, gchar *src,
158                                         gint header_len)
159 {
160         g_return_if_fail(src != NULL);
161         g_return_if_fail(dest != NULL);
162
163         if (len < 1) return;
164
165         remove_return(src);
166
167         if (is_ascii_str(src)) {
168                 strncpy2(dest, src, len);
169                 dest[len - 1] = '\0';
170                 return;
171         } else
172                 conv_encode_header(dest, len, src, header_len);
173 }
174
175 static gint disposition_notification_queue(PrefsAccount * account,
176                                            gchar * to, const gchar *file)
177 {
178         FolderItem *queue;
179         gchar *tmp, *queue_path;
180         FILE *fp, *src_fp;
181         GSList *cur;
182         gchar buf[BUFFSIZE];
183         gint num;
184
185         debug_print(_("queueing message...\n"));
186         g_return_val_if_fail(account != NULL, -1);
187
188         tmp = g_strdup_printf("%s%cqueue.%d", g_get_tmp_dir(),
189                               G_DIR_SEPARATOR, (gint)file);
190         if ((fp = fopen(tmp, "wb")) == NULL) {
191                 FILE_OP_ERROR(tmp, "fopen");
192                 g_free(tmp);
193                 return -1;
194         }
195         if ((src_fp = fopen(file, "rb")) == NULL) {
196                 FILE_OP_ERROR(file, "fopen");
197                 fclose(fp);
198                 unlink(tmp);
199                 g_free(tmp);
200                 return -1;
201         }
202         if (change_file_mode_rw(fp, tmp) < 0) {
203                 FILE_OP_ERROR(tmp, "chmod");
204                 g_warning(_("can't change file mode\n"));
205         }
206
207         /* queueing variables */
208         fprintf(fp, "AF:\n");
209         fprintf(fp, "NF:0\n");
210         fprintf(fp, "PS:10\n");
211         fprintf(fp, "SRH:1\n");
212         fprintf(fp, "SFN:\n");
213         fprintf(fp, "DSR:\n");
214         fprintf(fp, "MID:\n");
215         fprintf(fp, "CFG:\n");
216         fprintf(fp, "PT:0\n");
217         fprintf(fp, "S:%s\n", account->address);
218         fprintf(fp, "RQ:\n");
219         if (account->smtp_server)
220                 fprintf(fp, "SSV:%s\n", account->smtp_server);
221         else
222                 fprintf(fp, "SSV:\n");
223         if (account->nntp_server)
224                 fprintf(fp, "NSV:%s\n", account->nntp_server);
225         else
226                 fprintf(fp, "NSV:\n");
227         fprintf(fp, "SSH:\n");
228         fprintf(fp, "R:<%s>", to);
229         fprintf(fp, "\n");
230         fprintf(fp, "\n");
231
232         while (fgets(buf, sizeof(buf), src_fp) != NULL) {
233                 if (fputs(buf, fp) == EOF) {
234                         FILE_OP_ERROR(tmp, "fputs");
235                         fclose(fp);
236                         fclose(src_fp);
237                         unlink(tmp);
238                         g_free(tmp);
239                         return -1;
240                 }
241         }
242
243         fclose(src_fp);
244         if (fclose(fp) == EOF) {
245                 FILE_OP_ERROR(tmp, "fclose");
246                 unlink(tmp);
247                 g_free(tmp);
248                 return -1;
249         }
250
251         queue = folder_get_default_queue();
252         folder_item_scan(queue);
253         queue_path = folder_item_get_path(queue);
254         if (!is_dir_exist(queue_path))
255                 make_dir_hier(queue_path);
256         if ((num = folder_item_add_msg(queue, tmp, TRUE)) < 0) {
257                 g_warning(_("can't queue the message\n"));
258                 unlink(tmp);
259                 g_free(tmp);
260                 g_free(queue_path);
261                 return -1;
262         }
263         g_free(tmp);
264
265         if ((fp = procmsg_open_mark_file(queue_path, TRUE)) == NULL)
266                 g_warning(_("can't open mark file\n"));
267         else {
268                 MsgInfo newmsginfo;
269
270                 newmsginfo.msgnum = num;
271                 newmsginfo.flags.perm_flags = newmsginfo.flags.tmp_flags = 0;
272                 procmsg_write_flags(&newmsginfo, fp);
273                 fclose(fp);
274         }
275         g_free(queue_path);
276
277         folder_item_scan(queue);
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, msginfo->dispositionnotificationto);
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         /* FIXME - doesn't tmpmsginfo->flags have the value
396          * of msginfo->flags after procheader_parse()???
397          * in any case, checking tmpmsginfo->flags for MSG_UNREAD
398          * fixes the return-receipt-request bug */
399
400         tmpmsginfo = procheader_parse(file, msginfo->flags, TRUE, TRUE);
401         if (MSG_IS_MIME(tmpmsginfo->flags))
402                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MIME);
403
404         if (prefs_common.return_receipt
405             && (tmpmsginfo->dispositionnotificationto
406                 || tmpmsginfo->returnreceiptto)
407             && (MSG_IS_UNREAD(tmpmsginfo->flags))
408             && (MSG_IS_RETRCPT_PENDING(tmpmsginfo->flags))) {
409                 gint ok;
410                 
411                 if (alertpanel(_("Return Receipt"), _("Send return receipt ?"),
412                                _("Yes"), _("No"), NULL) == G_ALERTDEFAULT) {
413                         ok = disposition_notification_send(tmpmsginfo);
414                         if (ok < 0)
415                                 alertpanel_error(_("Error occurred while sending notification."));
416                 }
417                 MSG_UNSET_PERM_FLAGS(tmpmsginfo->flags, MSG_RETRCPT_PENDING);   
418         }
419
420         headerview_show(messageview->headerview, tmpmsginfo);
421         procmsg_msginfo_free(tmpmsginfo);
422
423         textview_set_all_headers(messageview->textview, all_headers);
424         textview_set_all_headers(messageview->mimeview->textview, all_headers);
425
426         if (mimeinfo->mime_type != MIME_TEXT &&
427             mimeinfo->mime_type != MIME_TEXT_HTML) {
428                 messageview_change_view_type(messageview, MVIEW_MIME);
429                 mimeview_show_message(messageview->mimeview, mimeinfo, file);
430         } else {
431                 messageview_change_view_type(messageview, MVIEW_TEXT);
432                 textview_show_message(messageview->textview, mimeinfo, file);
433                 procmime_mimeinfo_free(mimeinfo);
434         }
435
436         g_free(file);
437 }
438
439 static void messageview_change_view_type(MessageView *messageview,
440                                          MessageType type)
441 {
442         TextView *textview = messageview->textview;
443         MimeView *mimeview = messageview->mimeview;
444
445         if (messageview->type == type) return;
446
447         if (type == MVIEW_MIME) {
448                 gtkut_container_remove
449                         (GTK_CONTAINER(GTK_WIDGET_PTR(messageview)),
450                          GTK_WIDGET_PTR(textview));
451                 gtk_box_pack_start(GTK_BOX(messageview->vbox),
452                                    GTK_WIDGET_PTR(mimeview), TRUE, TRUE, 0);
453                 gtk_container_add(GTK_CONTAINER(mimeview->vbox),
454                                   GTK_WIDGET_PTR(textview));
455         } else if (type == MVIEW_TEXT) {
456                 gtkut_container_remove
457                         (GTK_CONTAINER(GTK_WIDGET_PTR(messageview)),
458                          GTK_WIDGET_PTR(mimeview));
459
460                 if (mimeview->vbox == GTK_WIDGET_PTR(textview)->parent)
461                         gtkut_container_remove(GTK_CONTAINER(mimeview->vbox),
462                                                GTK_WIDGET_PTR(textview));
463
464                 gtk_box_pack_start(GTK_BOX(messageview->vbox),
465                                    GTK_WIDGET_PTR(textview), TRUE, TRUE, 0);
466         } else
467                 return;
468
469         messageview->type = type;
470 }
471
472 void messageview_clear(MessageView *messageview)
473 {
474         messageview_change_view_type(messageview, MVIEW_TEXT);
475         headerview_clear(messageview->headerview);
476         textview_clear(messageview->textview);
477         imageview_clear(messageview->imageview);
478 }
479
480 void messageview_destroy(MessageView *messageview)
481 {
482         GtkWidget *textview  = GTK_WIDGET_PTR(messageview->textview);
483         GtkWidget *imageview = GTK_WIDGET_PTR(messageview->imageview);
484         GtkWidget *mimeview  = GTK_WIDGET_PTR(messageview->mimeview);
485
486         headerview_destroy(messageview->headerview);
487         textview_destroy(messageview->textview);
488         imageview_destroy(messageview->imageview);
489         mimeview_destroy(messageview->mimeview);
490
491         g_free(messageview);
492
493         gtk_widget_unref(textview);
494         gtk_widget_unref(imageview);
495         gtk_widget_unref(mimeview);
496 }
497
498 void messageview_quote_color_set(void)
499 {
500 }
501
502 void messageview_set_font(MessageView *messageview)
503 {
504         textview_set_font(messageview->textview, NULL);
505 }
506
507 void messageview_copy_clipboard(MessageView *messageview)
508 {
509         gint displaytype = /* force MVIEV_TEXT on first page */
510                 ((messageview->type == MVIEW_MIME)
511                 && (gtk_notebook_get_current_page(GTK_NOTEBOOK(
512                                 messageview->mimeview->notebook)) > 0))
513                 ? MVIEW_MIME
514                 : MVIEW_TEXT;
515
516         switch (displaytype) {
517         case MVIEW_TEXT:
518                 gtk_editable_copy_clipboard(GTK_EDITABLE(messageview->textview->text));
519                 break;
520         case MVIEW_MIME:
521                 if (messageview->mimeview->type == MIMEVIEW_TEXT)
522                         gtk_editable_copy_clipboard(GTK_EDITABLE(messageview->mimeview->textview->text));
523         default:
524                 break;
525         }
526 }
527
528 void messageview_select_all(MessageView *messageview)
529 {
530         GtkWidget *text = NULL;
531
532         if (messageview->type == MVIEW_TEXT)
533                 text = messageview->textview->text;
534         else if (messageview->type == MVIEW_MIME) {
535                 if (gtk_notebook_get_current_page
536                         (GTK_NOTEBOOK(messageview->mimeview->notebook)) == 0)
537                         text = messageview->textview->text;
538                 else if (messageview->mimeview->type == MIMEVIEW_TEXT)
539                         text = messageview->mimeview->textview->text;
540         }
541
542         if (text)
543                 gtk_editable_select_region(GTK_EDITABLE(text), 0, -1);
544 }
545
546 void messageview_set_position(MessageView *messageview, gint pos)
547 {
548         textview_set_position(messageview->textview, pos);
549 }
550
551 gboolean messageview_search_string(MessageView *messageview, const gchar *str,
552                                    gboolean case_sens)
553 {
554         return textview_search_string(messageview->textview, str, case_sens);
555         return FALSE;
556 }
557
558 gboolean messageview_search_string_backward(MessageView *messageview,
559                                             const gchar *str,
560                                             gboolean case_sens)
561 {
562         return textview_search_string_backward(messageview->textview,
563                                                str, case_sens);
564         return FALSE;
565 }
566
567 GtkWidget *messageview_get_text_widget(MessageView *messageview)
568 {
569         return messageview->textview->text;
570 }
571
572 gboolean messageview_is_visible(MessageView *messageview)
573 {
574         return messageview->visible;
575 }
576
577 static void messageview_destroy_cb(GtkWidget *widget, MessageView *messageview)
578 {
579         messageview_destroy(messageview);
580 }
581
582 static void messageview_size_allocate_cb(GtkWidget *widget,
583                                          GtkAllocation *allocation)
584 {
585         g_return_if_fail(allocation != NULL);
586
587         prefs_common.msgwin_width  = allocation->width;
588         prefs_common.msgwin_height = allocation->height;
589 }
590
591 static void key_pressed(GtkWidget *widget, GdkEventKey *event,
592                         MessageView *messageview)
593 {
594         if (event && event->keyval == GDK_Escape && messageview->window)
595                 gtk_widget_destroy(messageview->window);
596 }
597
598 static void messageview_toggle_view(MessageView *messageview)
599 {
600         MainWindow *mainwin = messageview->mainwin;
601         GtkItemFactory *ifactory;
602         
603         if (!mainwin) return;
604         
605         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
606         menu_toggle_toggle(ifactory, "/View/Expand Summary View");
607 }
608
609 void messageview_toggle_view_real(MessageView *messageview)
610 {
611         MainWindow *mainwin = messageview->mainwin;
612         union CompositeWin *cwin = &mainwin->win;
613         GtkWidget *vpaned = NULL;
614         GtkWidget *container = NULL;
615         GtkItemFactory *ifactory =gtk_item_factory_from_widget(mainwin->menubar);
616         
617         switch (mainwin->type) {
618         case SEPARATE_NONE:
619                 vpaned = cwin->sep_none.vpaned;
620                 container = cwin->sep_none.hpaned;
621                 break;
622         case SEPARATE_FOLDER:
623                 vpaned = cwin->sep_folder.vpaned;
624                 container = mainwin->vbox_body;
625                 break;
626         case SEPARATE_MESSAGE:
627         case SEPARATE_BOTH:
628                 return;
629         }
630
631         if (vpaned->parent != NULL) {
632                 gtk_widget_ref(vpaned);
633                 gtkut_container_remove(GTK_CONTAINER(container), vpaned);
634                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), container);
635                 menu_set_sensitive(ifactory, "/View/Expand Summary View", FALSE);
636                 gtk_widget_grab_focus(GTK_WIDGET(messageview->textview->text));
637         } else {
638                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), vpaned);
639                 gtk_container_add(GTK_CONTAINER(container), vpaned);
640                 gtk_widget_unref(vpaned);
641                 menu_set_sensitive(ifactory, "/View/Expand Summary View", TRUE);
642                 gtk_widget_grab_focus(GTK_WIDGET(mainwin->summaryview->ctree));
643         }
644 }