make notification_convert_header obey const correctness (next step will be fixing...
[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, *queue_path;
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         folder_item_scan(queue);
258         queue_path = folder_item_get_path(queue);
259         if (!is_dir_exist(queue_path))
260                 make_dir_hier(queue_path);
261         if ((num = folder_item_add_msg(queue, tmp, TRUE)) < 0) {
262                 g_warning(_("can't queue the message\n"));
263                 unlink(tmp);
264                 g_free(tmp);
265                 g_free(queue_path);
266                 return -1;
267         }
268         g_free(tmp);
269
270         if ((fp = procmsg_open_mark_file(queue_path, TRUE)) == NULL)
271                 g_warning(_("can't open mark file\n"));
272         else {
273                 MsgInfo newmsginfo;
274
275                 newmsginfo.msgnum = num;
276                 newmsginfo.flags.perm_flags = newmsginfo.flags.tmp_flags = 0;
277                 procmsg_write_flags(&newmsginfo, fp);
278                 fclose(fp);
279         }
280         g_free(queue_path);
281
282         folder_item_scan(queue);
283         folderview_update_item(queue, TRUE);
284
285         return 0;
286 }
287
288 static gint disposition_notification_send(MsgInfo * msginfo)
289 {
290         gchar buf[BUFFSIZE];
291         gchar tmp[MAXPATHLEN + 1];
292         FILE *fp;
293         GSList * to_list;
294         gint ok;
295         gchar * to;
296
297         if ((!msginfo->returnreceiptto) && 
298             (!msginfo->dispositionnotificationto))
299                 return -1;
300
301         /* write to temporary file */
302         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
303                    get_rc_dir(), G_DIR_SEPARATOR, (gint)msginfo);
304
305         if ((fp = fopen(tmp, "wb")) == NULL) {
306                 FILE_OP_ERROR(tmp, "fopen");
307                 return -1;
308         }
309
310         /* chmod for security */
311         if (change_file_mode_rw(fp, tmp) < 0) {
312                 FILE_OP_ERROR(tmp, "chmod");
313                 g_warning(_("can't change file mode\n"));
314         }
315
316         /* Date */
317         get_rfc822_date(buf, sizeof(buf));
318         fprintf(fp, "Date: %s\n", buf);
319
320         /* From */
321         if (cur_account->name && *cur_account->name) {
322                 notification_convert_header
323                         (buf, sizeof(buf), cur_account->name,
324                          strlen("From: "));
325                 fprintf(fp, "From: %s <%s>\n", buf, cur_account->address);
326         } else
327                 fprintf(fp, "From: %s\n", cur_account->address);
328
329         /* To */
330         if (msginfo->dispositionnotificationto)
331                 to = msginfo->dispositionnotificationto;
332         else
333                 to = msginfo->returnreceiptto;
334         fprintf(fp, "To: %s\n", to);
335
336         /* Subject */
337         notification_convert_header(buf, sizeof(buf), msginfo->subject,
338                                     strlen("Subject: "));
339         fprintf(fp, "Subject: Disposition notification: %s\n", buf);
340
341         if (fclose(fp) == EOF) {
342                 FILE_OP_ERROR(tmp, "fclose");
343                 unlink(tmp);
344                 return -1;
345         }
346
347         to_list = address_list_append(NULL, msginfo->dispositionnotificationto);
348         ok = send_message(tmp, cur_account, to_list);
349         
350         if (ok < 0) {
351                 if (prefs_common.queue_msg) {
352                         AlertValue val;
353                         
354                         val = alertpanel
355                                 (_("Queueing"),
356                                  _("Error occurred while sending the notification.\n"
357                                    "Put this notification into queue folder?"),
358                                  _("OK"), _("Cancel"), NULL);
359                         if (G_ALERTDEFAULT == val) {
360                                 ok = disposition_notification_queue(cur_account, to, tmp);
361                                 if (ok < 0)
362                                         alertpanel_error(_("Can't queue the notification."));
363                         }
364                 } else
365                         alertpanel_error(_("Error occurred while sending the notification."));
366         }
367
368         if (unlink(tmp) < 0) FILE_OP_ERROR(tmp, "unlink");
369
370         return ok;
371 }
372
373 void messageview_show(MessageView *messageview, MsgInfo *msginfo,
374                       gboolean all_headers)
375 {
376         FILE *fp;
377         gchar *file;
378         MimeInfo *mimeinfo;
379         MsgInfo *tmpmsginfo;
380
381         g_return_if_fail(msginfo != NULL);
382
383 #if USE_GPGME
384         if ((fp = procmsg_open_message_decrypted(msginfo, &mimeinfo)) == NULL)
385                 return;
386 #else /* !USE_GPGME */
387         if ((fp = procmsg_open_message(msginfo)) == NULL) return;
388         mimeinfo = procmime_scan_mime_header(fp);
389 #endif /* USE_GPGME */
390         fclose(fp);
391         if (!mimeinfo) return;
392
393         file = procmsg_get_message_file_path(msginfo);
394         if (!file) {
395                 g_warning("can't get message file path.\n");
396                 procmime_mimeinfo_free(mimeinfo);
397                 return;
398         }
399
400         /* FIXME - doesn't tmpmsginfo->flags have the value
401          * of msginfo->flags after procheader_parse()???
402          * in any case, checking tmpmsginfo->flags for MSG_UNREAD
403          * fixes the return-receipt-request bug */
404
405         tmpmsginfo = procheader_parse_file(file, msginfo->flags, TRUE, TRUE);
406         if (MSG_IS_MIME(tmpmsginfo->flags))
407                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MIME);
408
409         if (prefs_common.return_receipt
410             && (tmpmsginfo->dispositionnotificationto
411                 || tmpmsginfo->returnreceiptto)
412             && (MSG_IS_UNREAD(tmpmsginfo->flags))
413             && (MSG_IS_RETRCPT_PENDING(tmpmsginfo->flags))) {
414                 gint ok;
415                 
416                 if (alertpanel(_("Return Receipt"), _("Send return receipt ?"),
417                                _("Yes"), _("No"), NULL) == G_ALERTDEFAULT) {
418                         ok = disposition_notification_send(tmpmsginfo);
419                         if (ok < 0)
420                                 alertpanel_error(_("Error occurred while sending notification."));
421                 }
422                 MSG_UNSET_PERM_FLAGS(tmpmsginfo->flags, MSG_RETRCPT_PENDING);   
423         }
424
425         headerview_show(messageview->headerview, tmpmsginfo);
426         procmsg_msginfo_free(tmpmsginfo);
427
428         textview_set_all_headers(messageview->textview, all_headers);
429         textview_set_all_headers(messageview->mimeview->textview, all_headers);
430
431         if (mimeinfo->mime_type != MIME_TEXT &&
432             mimeinfo->mime_type != MIME_TEXT_HTML) {
433                 messageview_change_view_type(messageview, MVIEW_MIME);
434                 mimeview_show_message(messageview->mimeview, mimeinfo, file);
435         } else {
436                 messageview_change_view_type(messageview, MVIEW_TEXT);
437                 textview_show_message(messageview->textview, mimeinfo, file);
438                 procmime_mimeinfo_free(mimeinfo);
439         }
440
441         g_free(file);
442 }
443
444 static void messageview_change_view_type(MessageView *messageview,
445                                          MessageType type)
446 {
447         TextView *textview = messageview->textview;
448         MimeView *mimeview = messageview->mimeview;
449
450         if (messageview->type == type) return;
451
452         if (type == MVIEW_MIME) {
453                 gtkut_container_remove
454                         (GTK_CONTAINER(GTK_WIDGET_PTR(messageview)),
455                          GTK_WIDGET_PTR(textview));
456                 gtk_box_pack_start(GTK_BOX(messageview->vbox),
457                                    GTK_WIDGET_PTR(mimeview), TRUE, TRUE, 0);
458                 gtk_container_add(GTK_CONTAINER(mimeview->vbox),
459                                   GTK_WIDGET_PTR(textview));
460         } else if (type == MVIEW_TEXT) {
461                 gtkut_container_remove
462                         (GTK_CONTAINER(GTK_WIDGET_PTR(messageview)),
463                          GTK_WIDGET_PTR(mimeview));
464
465                 if (mimeview->vbox == GTK_WIDGET_PTR(textview)->parent)
466                         gtkut_container_remove(GTK_CONTAINER(mimeview->vbox),
467                                                GTK_WIDGET_PTR(textview));
468
469                 gtk_box_pack_start(GTK_BOX(messageview->vbox),
470                                    GTK_WIDGET_PTR(textview), TRUE, TRUE, 0);
471         } else
472                 return;
473
474         messageview->type = type;
475 }
476
477 void messageview_clear(MessageView *messageview)
478 {
479         messageview_change_view_type(messageview, MVIEW_TEXT);
480         headerview_clear(messageview->headerview);
481         textview_clear(messageview->textview);
482         imageview_clear(messageview->imageview);
483 }
484
485 void messageview_destroy(MessageView *messageview)
486 {
487         GtkWidget *textview  = GTK_WIDGET_PTR(messageview->textview);
488         GtkWidget *imageview = GTK_WIDGET_PTR(messageview->imageview);
489         GtkWidget *mimeview  = GTK_WIDGET_PTR(messageview->mimeview);
490
491         headerview_destroy(messageview->headerview);
492         textview_destroy(messageview->textview);
493         imageview_destroy(messageview->imageview);
494         mimeview_destroy(messageview->mimeview);
495
496         g_free(messageview);
497
498         gtk_widget_unref(textview);
499         gtk_widget_unref(imageview);
500         gtk_widget_unref(mimeview);
501 }
502
503 void messageview_quote_color_set(void)
504 {
505 }
506
507 void messageview_set_font(MessageView *messageview)
508 {
509         textview_set_font(messageview->textview, NULL);
510 }
511
512 TextView *messageview_get_current_textview(MessageView *messageview)
513 {
514         TextView *text = NULL;
515
516         if (messageview->type == MVIEW_TEXT)
517                 text = messageview->textview;
518         else if (messageview->type == MVIEW_MIME) {
519                 if (gtk_notebook_get_current_page
520                         (GTK_NOTEBOOK(messageview->mimeview->notebook)) == 0)
521                         text = messageview->textview;
522                 else if (messageview->mimeview->type == MIMEVIEW_TEXT)
523                         text = messageview->mimeview->textview;
524         }
525
526         return text;
527 }
528
529 void messageview_copy_clipboard(MessageView *messageview)
530 {
531         TextView *text;
532
533         text = messageview_get_current_textview(messageview);
534         if (text)
535                 gtk_editable_copy_clipboard(GTK_EDITABLE(text->text));
536 }
537
538 void messageview_select_all(MessageView *messageview)
539 {
540         TextView *text;
541
542         text = messageview_get_current_textview(messageview);
543         if (text)
544                 gtk_editable_select_region(GTK_EDITABLE(text->text), 0, -1);
545 }
546
547 void messageview_set_position(MessageView *messageview, gint pos)
548 {
549         textview_set_position(messageview->textview, pos);
550 }
551
552 gboolean messageview_search_string(MessageView *messageview, const gchar *str,
553                                    gboolean case_sens)
554 {
555         return textview_search_string(messageview->textview, str, case_sens);
556         return FALSE;
557 }
558
559 gboolean messageview_search_string_backward(MessageView *messageview,
560                                             const gchar *str,
561                                             gboolean case_sens)
562 {
563         return textview_search_string_backward(messageview->textview,
564                                                str, case_sens);
565         return FALSE;
566 }
567
568 gboolean messageview_is_visible(MessageView *messageview)
569 {
570         return messageview->visible;
571 }
572
573 static void messageview_destroy_cb(GtkWidget *widget, MessageView *messageview)
574 {
575         messageview_destroy(messageview);
576 }
577
578 static void messageview_size_allocate_cb(GtkWidget *widget,
579                                          GtkAllocation *allocation)
580 {
581         g_return_if_fail(allocation != NULL);
582
583         prefs_common.msgwin_width  = allocation->width;
584         prefs_common.msgwin_height = allocation->height;
585 }
586
587 static void key_pressed(GtkWidget *widget, GdkEventKey *event,
588                         MessageView *messageview)
589 {
590         if (event && event->keyval == GDK_Escape && messageview->window)
591                 gtk_widget_destroy(messageview->window);
592 }
593
594 static void messageview_toggle_view(MessageView *messageview)
595 {
596         MainWindow *mainwin = messageview->mainwin;
597         GtkItemFactory *ifactory;
598         
599         if (!mainwin) return;
600         
601         ifactory = gtk_item_factory_from_widget(mainwin->menubar);
602         menu_toggle_toggle(ifactory, "/View/Expand Summary View");
603 }
604
605 void messageview_toggle_view_real(MessageView *messageview)
606 {
607         MainWindow *mainwin = messageview->mainwin;
608         union CompositeWin *cwin = &mainwin->win;
609         GtkWidget *vpaned = NULL;
610         GtkWidget *container = NULL;
611         GtkItemFactory *ifactory =gtk_item_factory_from_widget(mainwin->menubar);
612         
613         switch (mainwin->type) {
614         case SEPARATE_NONE:
615                 vpaned = cwin->sep_none.vpaned;
616                 container = cwin->sep_none.hpaned;
617                 break;
618         case SEPARATE_FOLDER:
619                 vpaned = cwin->sep_folder.vpaned;
620                 container = mainwin->vbox_body;
621                 break;
622         case SEPARATE_MESSAGE:
623         case SEPARATE_BOTH:
624                 return;
625         }
626
627         if (vpaned->parent != NULL) {
628                 gtk_widget_ref(vpaned);
629                 gtkut_container_remove(GTK_CONTAINER(container), vpaned);
630                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), container);
631                 menu_set_sensitive(ifactory, "/View/Expand Summary View", FALSE);
632                 gtk_widget_grab_focus(GTK_WIDGET(messageview->textview->text));
633         } else {
634                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), vpaned);
635                 gtk_container_add(GTK_CONTAINER(container), vpaned);
636                 gtk_widget_unref(vpaned);
637                 menu_set_sensitive(ifactory, "/View/Expand Summary View", TRUE);
638                 gtk_widget_grab_focus(GTK_WIDGET(mainwin->summaryview->ctree));
639         }
640 }