e697167b7bee94f4a2cede016b761588433d2c11
[claws.git] / src / messageview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2005 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 <glib/gi18n.h>
24 #include <gdk/gdkkeysyms.h>
25 #include <gtk/gtkvbox.h>
26 #include <gtk/gtkcontainer.h>
27 #include <gtk/gtkwindow.h>
28 #include <gtk/gtktext.h>
29 #include <gtk/gtkmenu.h>
30 #include <gtk/gtkmenuitem.h>
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <string.h>
34
35 #include "main.h"
36 #include "messageview.h"
37 #include "message_search.h"
38 #include "headerview.h"
39 #include "summaryview.h"
40 #include "textview.h"
41 #include "mimeview.h"
42 #include "menu.h"
43 #include "about.h"
44 #include "filesel.h"
45 #include "sourcewindow.h"
46 #include "addressbook.h"
47 #include "alertpanel.h"
48 #include "inputdialog.h"
49 #include "manage_window.h"
50 #include "procmsg.h"
51 #include "procheader.h"
52 #include "procmime.h"
53 #include "account.h"
54 #include "action.h"
55 #include "prefs_common.h"
56 #include "prefs_account.h"
57 #include "gtkutils.h"
58 #include "utils.h"
59 #include "send_message.h"
60 #include "stock_pixmap.h"
61 #include "hooks.h"
62 #include "filtering.h"
63 #include "partial_download.h"
64
65 static GList *messageview_list = NULL;
66
67 static void messageview_destroy_cb      (GtkWidget      *widget,
68                                          MessageView    *messageview);
69 static void messageview_size_allocate_cb(GtkWidget      *widget,
70                                          GtkAllocation  *allocation);
71 static gboolean key_pressed             (GtkWidget      *widget,
72                                          GdkEventKey    *event,
73                                          MessageView    *messageview);
74
75 static void return_receipt_show         (NoticeView     *noticeview, 
76                                          MsgInfo        *msginfo);      
77 static void return_receipt_send_clicked (NoticeView     *noticeview, 
78                                          MsgInfo        *msginfo);
79 static void partial_recv_show           (NoticeView     *noticeview, 
80                                          MsgInfo        *msginfo);      
81 static void partial_recv_dload_clicked  (NoticeView     *noticeview, 
82                                          MsgInfo        *msginfo);
83 static void partial_recv_del_clicked    (NoticeView     *noticeview, 
84                                          MsgInfo        *msginfo);
85 static void partial_recv_unmark_clicked (NoticeView     *noticeview, 
86                                          MsgInfo        *msginfo);
87 static void save_as_cb                  (gpointer        data,
88                                          guint           action,
89                                          GtkWidget      *widget);
90 static void print_cb                    (gpointer        data,
91                                          guint           action,
92                                          GtkWidget      *widget);
93 static void close_cb                    (gpointer        data,
94                                          guint           action,
95                                          GtkWidget      *widget);
96 static void copy_cb                     (gpointer        data,
97                                          guint           action,
98                                          GtkWidget      *widget);
99 static void allsel_cb                   (gpointer        data,
100                                          guint           action,
101                                          GtkWidget      *widget);
102 static void search_cb                   (gpointer        data,
103                                          guint           action,
104                                          GtkWidget      *widget);
105
106 static void set_charset_cb              (gpointer        data,
107                                          guint           action,
108                                          GtkWidget      *widget);
109 static void set_decode_cb               (gpointer        data,
110                                          guint           action,
111                                          GtkWidget      *widget);
112 static void view_source_cb              (gpointer        data,
113                                          guint           action,
114                                          GtkWidget      *widget);
115 static void show_all_header_cb          (gpointer        data,
116                                          guint           action,
117                                          GtkWidget      *widget);
118
119 static void compose_cb                  (gpointer        data,
120                                          guint           action,
121                                          GtkWidget      *widget);
122 static void reply_cb                    (gpointer        data,
123                                          guint           action,
124                                          GtkWidget      *widget);
125 static void reedit_cb                   (gpointer        data,
126                                          guint           action,
127                                          GtkWidget      *widget);
128
129 static PrefsAccount *select_account_from_list
130                                         (GList          *ac_list);
131 static void addressbook_open_cb         (gpointer        data,
132                                          guint           action,
133                                          GtkWidget      *widget);
134 static void add_address_cb              (gpointer        data,
135                                          guint           action,
136                                          GtkWidget      *widget);
137 static void create_filter_cb            (gpointer        data,
138                                          guint           action,
139                                          GtkWidget      *widget);
140 static void create_processing_cb        (gpointer        data,
141                                          guint           action,
142                                          GtkWidget      *widget);
143
144 static void about_cb                    (gpointer        data,
145                                          guint           action,
146                                          GtkWidget      *widget);
147 static void messageview_update          (MessageView *msgview);
148 static gboolean messageview_update_msg  (gpointer source, gpointer data);
149
150 static GList *msgview_list = NULL;
151 static GtkItemFactoryEntry msgview_entries[] =
152 {
153         {N_("/_File"),                  NULL, NULL, 0, "<Branch>"},
154         {N_("/_File/_Save as..."),      NULL, save_as_cb, 0, NULL},
155         {N_("/_File/_Print..."),        NULL, print_cb, 0, NULL},
156         {N_("/_File/---"),              NULL, NULL, 0, "<Separator>"},
157         {N_("/_File/_Close"),           NULL, close_cb, 0, NULL},
158
159         {N_("/_Edit"),                  NULL, NULL, 0, "<Branch>"},
160         {N_("/_Edit/_Copy"),            NULL, copy_cb, 0, NULL},
161         {N_("/_Edit/Select _all"),      NULL, allsel_cb, 0, NULL},
162         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
163         {N_("/_Edit/_Find in current message..."),
164                                         NULL, search_cb, 0, NULL},
165
166         {N_("/_View"),                  NULL, NULL, 0, "<Branch>"},
167
168 #define ENC_SEPARATOR \
169         {N_("/_View/Character _encoding/---"),  NULL, NULL, 0, "<Separator>"}
170 #define ENC_ACTION(action) \
171         NULL, set_charset_cb, action, "/View/Character encoding/Auto detect"
172
173         {N_("/_View/Character _encoding"),      NULL, NULL, 0, "<Branch>"},
174         {N_("/_View/Character _encoding/_Auto detect"),
175                                         NULL, set_charset_cb, C_AUTO, "<RadioItem>"},
176         ENC_SEPARATOR,
177         {N_("/_View/Character _encoding/7bit ascii (US-ASC_II)"),
178          ENC_ACTION(C_US_ASCII)},
179
180         {N_("/_View/Character _encoding/Unicode (_UTF-8)"),
181          ENC_ACTION(C_UTF_8)},
182         ENC_SEPARATOR,
183         {N_("/_View/Character _encoding/Western European (ISO-8859-_1)"),
184          ENC_ACTION(C_ISO_8859_1)},
185         {N_("/_View/Character _encoding/Western European (ISO-8859-15)"),
186          ENC_ACTION(C_ISO_8859_15)},
187         ENC_SEPARATOR,
188         {N_("/_View/Character _encoding/Central European (ISO-8859-_2)"),
189          ENC_ACTION(C_ISO_8859_2)},
190         ENC_SEPARATOR,
191         {N_("/_View/Character _encoding/_Baltic (ISO-8859-13)"),
192          ENC_ACTION(C_ISO_8859_13)},
193         {N_("/_View/Character _encoding/Baltic (ISO-8859-_4)"),
194          ENC_ACTION(C_ISO_8859_4)},
195         ENC_SEPARATOR,
196         {N_("/_View/Character _encoding/Greek (ISO-8859-_7)"),
197          ENC_ACTION(C_ISO_8859_7)},
198         ENC_SEPARATOR,
199         {N_("/_View/Character _encoding/Turkish (ISO-8859-_9)"),
200          ENC_ACTION(C_ISO_8859_9)},
201         ENC_SEPARATOR,
202         {N_("/_View/Character _encoding/Cyrillic (ISO-8859-_5)"),
203          ENC_ACTION(C_ISO_8859_5)},
204         {N_("/_View/Character _encoding/Cyrillic (KOI8-_R)"),
205          ENC_ACTION(C_KOI8_R)},
206         {N_("/_View/Character _encoding/Cyrillic (KOI8-U)"),
207          ENC_ACTION(C_KOI8_U)},
208         {N_("/_View/Character _encoding/Cyrillic (Windows-1251)"),
209          ENC_ACTION(C_CP1251)},
210         ENC_SEPARATOR,
211         {N_("/_View/Character _encoding/Japanese (ISO-2022-_JP)"),
212          ENC_ACTION(C_ISO_2022_JP)},
213         {N_("/_View/Character _encoding/Japanese (ISO-2022-JP-2)"),
214          ENC_ACTION(C_ISO_2022_JP_2)},
215         {N_("/_View/Character _encoding/Japanese (_EUC-JP)"),
216          ENC_ACTION(C_EUC_JP)},
217         {N_("/_View/Character _encoding/Japanese (_Shift__JIS)"),
218          ENC_ACTION(C_SHIFT_JIS)},
219         ENC_SEPARATOR,
220         {N_("/_View/Character _encoding/Simplified Chinese (_GB2312)"),
221          ENC_ACTION(C_GB2312)},
222         {N_("/_View/Character _encoding/Simplified Chinese (GBK)"),
223          ENC_ACTION(C_GBK)},
224         {N_("/_View/Character _encoding/Traditional Chinese (_Big5)"),
225          ENC_ACTION(C_BIG5)},
226         {N_("/_View/Character _encoding/Traditional Chinese (EUC-_TW)"),
227          ENC_ACTION(C_EUC_TW)},
228         {N_("/_View/Character _encoding/Chinese (ISO-2022-_CN)"),
229          ENC_ACTION(C_ISO_2022_CN)},
230         ENC_SEPARATOR,
231         {N_("/_View/Character _encoding/Korean (EUC-_KR)"),
232          ENC_ACTION(C_EUC_KR)},
233         {N_("/_View/Character _encoding/Korean (ISO-2022-KR)"),
234          ENC_ACTION(C_ISO_2022_KR)},
235         ENC_SEPARATOR,
236         {N_("/_View/Character _encoding/Thai (TIS-620)"),
237          ENC_ACTION(C_TIS_620)},
238         {N_("/_View/Character _encoding/Thai (Windows-874)"),
239          ENC_ACTION(C_WINDOWS_874)},
240
241 #undef ENC_SEPARATOR
242 #undef ENC_ACTION
243
244 #define DEC_SEPARATOR \
245         {N_("/_View/Decode/---"),               NULL, NULL, 0, "<Separator>"}
246 #define DEC_ACTION(action) \
247          NULL, set_decode_cb, action, "/View/Decode/Auto detect"
248         {N_("/_View/Decode"),           NULL, NULL, 0, "<Branch>"},
249         {N_("/_View/Decode/_Auto detect"),
250          NULL, set_decode_cb, 0, "<RadioItem>"},
251         {N_("/_View/Decode/---"),               NULL, NULL, 0, "<Separator>"},
252         {N_("/_View/Decode/_8bit"),             DEC_ACTION(ENC_8BIT)},
253         {N_("/_View/Decode/_Quoted printable"), DEC_ACTION(ENC_QUOTED_PRINTABLE)},
254         {N_("/_View/Decode/_Base64"),           DEC_ACTION(ENC_BASE64)},
255         {N_("/_View/Decode/_Uuencode"),         DEC_ACTION(ENC_X_UUENCODE)},
256
257 #undef DEC_SEPARATOR
258 #undef DEC_ACTION
259
260         {N_("/_View/---"),              NULL, NULL, 0, "<Separator>"},
261         {N_("/_View/Mess_age source"),  NULL, view_source_cb, 0, NULL},
262         {N_("/_View/Show all _headers"),NULL, show_all_header_cb, 0, "<ToggleItem>"},
263
264         {N_("/_Message"),               NULL, NULL, 0, "<Branch>"},
265         {N_("/_Message/Compose _new message"),
266                                         NULL, compose_cb, 0, NULL},
267         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
268         {N_("/_Message/_Reply"),        NULL, reply_cb, COMPOSE_REPLY, NULL},
269         {N_("/_Message/Repl_y to/_all"),
270                                         NULL, reply_cb, COMPOSE_REPLY_TO_ALL, NULL},
271         {N_("/_Message/Repl_y to/_sender"),
272                                         NULL, reply_cb, COMPOSE_REPLY_TO_SENDER, NULL},
273         {N_("/_Message/Repl_y to/mailing _list"),
274                                         NULL, reply_cb, COMPOSE_REPLY_TO_LIST, NULL},
275         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
276         {N_("/_Message/_Forward"),      NULL, reply_cb, COMPOSE_FORWARD, NULL},
277         {N_("/_Message/For_ward as attachment"),
278                                         NULL, reply_cb, COMPOSE_FORWARD_AS_ATTACH, NULL},
279         {N_("/_Message/Redirec_t"),     NULL, reply_cb, COMPOSE_REDIRECT, NULL},
280         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
281         {N_("/_Message/Re-_edit"),      NULL, reedit_cb, 0, NULL},
282
283         {N_("/_Tools"),                 NULL, NULL, 0, "<Branch>"},
284         {N_("/_Tools/_Address book"),   NULL, addressbook_open_cb, 0, NULL},
285         {N_("/_Tools/Add sender to address boo_k"),
286                                         NULL, add_address_cb, 0, NULL},
287         {N_("/_Tools/---"),             NULL, NULL, 0, "<Separator>"},
288         {N_("/_Tools/_Create filter rule"),
289                                         NULL, NULL, 0, "<Branch>"},
290         {N_("/_Tools/_Create filter rule/_Automatically"),
291                                         NULL, create_filter_cb, FILTER_BY_AUTO, NULL},
292         {N_("/_Tools/_Create filter rule/by _From"),
293                                         NULL, create_filter_cb, FILTER_BY_FROM, NULL},
294         {N_("/_Tools/_Create filter rule/by _To"),
295                                         NULL, create_filter_cb, FILTER_BY_TO, NULL},
296         {N_("/_Tools/_Create filter rule/by _Subject"),
297                                         NULL, create_filter_cb, FILTER_BY_SUBJECT, NULL},
298         {N_("/_Tools/Create processing rule"),
299                                         NULL, NULL, 0, "<Branch>"},
300         {N_("/_Tools/Create processing rule/_Automatically"),
301                                         NULL, create_processing_cb, FILTER_BY_AUTO, NULL},
302         {N_("/_Tools/Create processing rule/by _From"),
303                                         NULL, create_processing_cb, FILTER_BY_FROM, NULL},
304         {N_("/_Tools/Create processing rule/by _To"),
305                                         NULL, create_processing_cb, FILTER_BY_TO, NULL},
306         {N_("/_Tools/Create processing rule/by _Subject"),
307                                         NULL, create_processing_cb, FILTER_BY_SUBJECT, NULL},
308         {N_("/_Tools/---"),             NULL, NULL, 0, "<Separator>"},
309         {N_("/_Tools/Actio_ns"),        NULL, NULL, 0, "<Branch>"},
310
311         {N_("/_Help"),                  NULL, NULL, 0, "<Branch>"},
312         {N_("/_Help/_About"),           NULL, about_cb, 0, NULL}
313 };
314
315 MessageView *messageview_create(MainWindow *mainwin)
316 {
317         MessageView *messageview;
318         GtkWidget *vbox;
319         HeaderView *headerview;
320         MimeView *mimeview;
321         NoticeView *noticeview;
322
323         debug_print("Creating message view...\n");
324         messageview = g_new0(MessageView, 1);
325
326         headerview = headerview_create();
327
328         noticeview = noticeview_create(mainwin);
329
330         mimeview = mimeview_create(mainwin);
331         mimeview->textview = textview_create();
332         mimeview->textview->messageview = messageview;
333         mimeview->messageview = messageview;
334
335         vbox = gtk_vbox_new(FALSE, 0);
336         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(headerview),
337                            FALSE, FALSE, 0);
338         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(noticeview),
339                            FALSE, FALSE, 0);
340         gtk_box_pack_start(GTK_BOX(vbox),
341                            GTK_WIDGET_PTR(mimeview), TRUE, TRUE, 0);
342         gtk_widget_show(vbox);
343
344         messageview->vbox        = vbox;
345         messageview->new_window  = FALSE;
346         messageview->window      = NULL;
347         messageview->headerview  = headerview;
348         messageview->mimeview    = mimeview;
349         messageview->noticeview = noticeview;
350         messageview->mainwin    = mainwin;
351
352         messageview->statusbar     = NULL;
353         messageview->statusbar_cid = 0;
354
355         messageview->msginfo_update_callback_id =
356                 hooks_register_hook(MSGINFO_UPDATE_HOOKLIST, messageview_update_msg, (gpointer) messageview);
357
358         return messageview;
359 }
360
361 GList *messageview_get_msgview_list(void)
362 {
363         return msgview_list;
364 }
365
366 void messageview_update_actions_menu(MessageView *msgview)
367 {
368         GtkItemFactory *ifactory;
369
370         /* Messages opened in a new window do not have a menu bar */
371         if (msgview->menubar == NULL)
372                 return;
373         ifactory = gtk_item_factory_from_widget(msgview->menubar);
374         action_update_msgview_menu(ifactory, "/Tools/Actions", msgview);
375 }
376
377 void messageview_add_toolbar(MessageView *msgview, GtkWidget *window) 
378 {
379         GtkWidget *handlebox;
380         GtkWidget *vbox;
381         GtkWidget *menubar;
382         GtkWidget *statusbar;
383         guint n_menu_entries;
384
385         vbox = gtk_vbox_new(FALSE, 0);
386         gtk_widget_show(vbox);
387         gtk_container_add(GTK_CONTAINER(window), vbox); 
388
389         n_menu_entries = sizeof(msgview_entries) / sizeof(msgview_entries[0]);
390         menubar = menubar_create(window, msgview_entries,
391                                  n_menu_entries, "<MessageView>", msgview);
392         gtk_widget_show(menubar);
393         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
394
395         handlebox = gtk_handle_box_new();
396         gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
397         msgview->toolbar = toolbar_create(TOOLBAR_MSGVIEW, handlebox,
398                                           (gpointer)msgview);
399
400         statusbar = gtk_statusbar_new();
401         gtk_widget_show(statusbar);
402         gtk_box_pack_end(GTK_BOX(vbox), statusbar, FALSE, FALSE, 0);
403         msgview->statusbar = statusbar;
404         msgview->statusbar_cid = gtk_statusbar_get_context_id
405                 (GTK_STATUSBAR(statusbar), "Message View");
406
407         msgview->handlebox = handlebox;
408         msgview->menubar   = menubar;
409
410         gtk_container_add(GTK_CONTAINER(vbox),
411                           GTK_WIDGET_PTR(msgview));
412
413         messageview_update_actions_menu(msgview);
414
415         msgview_list = g_list_append(msgview_list, msgview);
416 }
417
418 MessageView *messageview_create_with_new_window(MainWindow *mainwin)
419 {
420         MessageView *msgview;
421         GtkWidget *window;
422
423         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
424         gtk_window_set_title(GTK_WINDOW(window), _("Sylpheed - Message View"));
425         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
426         gtk_widget_set_size_request(window, prefs_common.msgwin_width,
427                                     prefs_common.msgwin_height);
428
429         msgview = messageview_create(mainwin);
430
431         g_signal_connect(G_OBJECT(window), "size_allocate",
432                          G_CALLBACK(messageview_size_allocate_cb),
433                          msgview);
434         g_signal_connect(G_OBJECT(window), "destroy",
435                          G_CALLBACK(messageview_destroy_cb), msgview);
436         g_signal_connect(G_OBJECT(window), "key_press_event",
437                          G_CALLBACK(key_pressed), msgview);
438
439         messageview_add_toolbar(msgview, window);
440
441         gtk_widget_grab_focus(msgview->mimeview->textview->text);
442         gtk_widget_show(window);
443
444         msgview->new_window = TRUE;
445         msgview->window = window;
446         msgview->visible = TRUE;
447
448         toolbar_set_style(msgview->toolbar->toolbar, msgview->handlebox, 
449                           prefs_common.toolbar_style);
450         messageview_init(msgview);
451
452         return msgview;
453 }
454
455 void messageview_init(MessageView *messageview)
456 {
457         headerview_init(messageview->headerview);
458         mimeview_init(messageview->mimeview);
459         /*messageview_set_font(messageview);*/
460
461         noticeview_hide(messageview->noticeview);
462 }
463
464 static void notification_convert_header(gchar *dest, gint len, 
465                                         const gchar *src_,
466                                         gint header_len)
467 {
468         char *src;
469
470         g_return_if_fail(src_ != NULL);
471         g_return_if_fail(dest != NULL);
472
473         if (len < 1) return;
474
475         Xstrndup_a(src, src_, len, return);
476
477         remove_return(src);
478
479         if (is_ascii_str(src)) {
480                 strncpy2(dest, src, len);
481                 dest[len - 1] = '\0';
482                 return;
483         } else
484                 conv_encode_header(dest, len, src, header_len, FALSE);
485 }
486
487 static gint disposition_notification_send(MsgInfo *msginfo)
488 {
489         gchar buf[BUFFSIZE];
490         gchar tmp[MAXPATHLEN + 1];
491         FILE *fp;
492         GList *ac_list;
493         PrefsAccount *account;
494         gint ok;
495         gchar *to;
496         FolderItem *queue, *outbox;
497         gint num;
498         gchar *path;
499         gchar *addr;
500         gchar *addrp;
501
502         if ((!msginfo->returnreceiptto) && 
503             (!msginfo->dispositionnotificationto)) 
504                 return -1;
505
506         /* RFC2298: Test for Return-Path */
507         if (msginfo->dispositionnotificationto)
508                 to = msginfo->dispositionnotificationto;
509         else
510                 to = msginfo->returnreceiptto;
511
512         ok = procheader_get_header_from_msginfo(msginfo, buf, sizeof(buf),
513                                 "Return-Path:");
514         if (ok == 0) {
515                 gchar *to_addr = g_strdup(to);
516                 extract_address(to_addr);
517                 extract_address(buf);
518                 ok = strcmp(to_addr, buf);
519                 g_free(to_addr);
520         } else {
521                 strncpy(buf, _("<No Return-Path found>"), 
522                                 sizeof(buf));
523         }
524         
525         if (ok != 0) {
526                 AlertValue val;
527                 gchar *message;
528                 message = g_strdup_printf(
529                                  _("The notification address to which the "
530                                    "return receipt is to be sent\n"
531                                    "does not correspond to the return path:\n"
532                                    "Notification address: %s\n"
533                                    "Return path: %s\n"
534                                    "It is advised to not to send the return "
535                                    "receipt."), to, buf);
536                 val = alertpanel_with_type(_("Warning"), message, _("Send"),
537                                 _("+Don't Send"), NULL, NULL, ALERT_WARNING);
538                 g_free(message);                                
539                 if (val != G_ALERTDEFAULT)
540                         return -1;
541         }
542
543         ac_list = account_find_all_from_address(NULL, msginfo->to);
544         ac_list = account_find_all_from_address(ac_list, msginfo->cc);
545
546         if (ac_list == NULL) {
547                 alertpanel_error(_("This message is asking for a return "
548                                    "receipt notification\n"
549                                    "but according to its 'To:' and 'CC:' "
550                                    "headers it was not\nofficially addressed "
551                                    "to you.\n"
552                                    "Receipt notification cancelled."));
553                 return -1;
554         }
555
556         if (g_list_length(ac_list) > 1)
557                 account = select_account_from_list(ac_list);
558         else
559                 account = (PrefsAccount *) ac_list->data;
560         g_list_free(ac_list);
561
562         if (account == NULL)
563                 return -1;
564
565         /* write to temporary file */
566         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
567                    get_rc_dir(), G_DIR_SEPARATOR, (gint)msginfo);
568
569         if ((fp = fopen(tmp, "wb")) == NULL) {
570                 FILE_OP_ERROR(tmp, "fopen");
571                 return -1;
572         }
573
574         /* chmod for security */
575         if (change_file_mode_rw(fp, tmp) < 0) {
576                 FILE_OP_ERROR(tmp, "chmod");
577                 g_warning("can't change file mode\n");
578         }
579         
580         addr = g_strdup(to);
581         
582         extract_address(addr);
583         addrp = addr;
584         
585         /* write queue headers */
586         fprintf(fp, "AF:\n");
587         fprintf(fp, "NF:0\n");
588         fprintf(fp, "PS:10\n");
589         fprintf(fp, "SRH:1\n");
590         fprintf(fp, "SFN:\n");
591         fprintf(fp, "DSR:\n");
592         fprintf(fp, "MID:\n");
593         fprintf(fp, "CFG:\n");
594         fprintf(fp, "PT:0\n");
595         fprintf(fp, "S:%s\n", account->address);
596         fprintf(fp, "RQ:\n");
597         if (account->smtp_server)
598                 fprintf(fp, "SSV:%s\n", account->smtp_server);
599         else
600                 fprintf(fp, "SSV:\n");
601         fprintf(fp, "SSH:\n");
602         fprintf(fp, "R:<%s>\n", addrp);
603         
604         g_free(addrp);
605         
606         /* check whether we need to save the message */
607         outbox = account_get_special_folder(account, F_OUTBOX); 
608         if (folder_get_default_outbox() == outbox && !prefs_common.savemsg)
609                 outbox = NULL;
610         if (outbox) {
611                 path = folder_item_get_identifier(outbox);
612                 fprintf(fp, "SCF:%s\n", path);
613                 g_free(path);
614         }               
615
616         fprintf(fp, "\n");
617         
618         /* Date */
619         get_rfc822_date(buf, sizeof(buf));
620         fprintf(fp, "Date: %s\n", buf);
621
622         /* From */
623         if (account->name && *account->name) {
624                 notification_convert_header
625                         (buf, sizeof(buf), account->name,
626                          strlen("From: "));
627                 fprintf(fp, "From: %s <%s>\n", buf, account->address);
628         } else
629                 fprintf(fp, "From: %s\n", account->address);
630
631         fprintf(fp, "To: %s\n", to);
632
633         /* Subject */
634         notification_convert_header(buf, sizeof(buf), msginfo->subject,
635                                     strlen("Subject: "));
636         fprintf(fp, "Subject: Disposition notification: %s\n", buf);
637
638         /* Message ID */
639         generate_msgid(buf, sizeof(buf));
640         fprintf(fp, "Message-ID: <%s>\n", buf);
641
642         if (fclose(fp) == EOF) {
643                 FILE_OP_ERROR(tmp, "fclose");
644                 unlink(tmp);
645                 return -1;
646         }
647
648         /* put it in queue */
649         queue = account_get_special_folder(account, F_QUEUE);
650         if (!queue) queue = folder_get_default_queue();
651         if (!queue) {
652                 g_warning("can't find queue folder\n");
653                 unlink(tmp);
654                 return -1;
655         }
656         folder_item_scan(queue);
657         if ((num = folder_item_add_msg(queue, tmp, NULL, TRUE)) < 0) {
658                 g_warning("can't queue the message\n");
659                 unlink(tmp);
660                 return -1;
661         }
662                 
663         if (prefs_common.work_offline)
664                 if (alertpanel(_("Offline warning"), 
665                                _("You're working offline. Override?"),
666                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
667                         return 0;
668
669         /* send it */
670         path = folder_item_fetch_msg(queue, num);
671         ok = procmsg_send_message_queue(path);
672         g_free(path);
673         folder_item_remove_msg(queue, num);
674
675         return ok;
676 }
677
678 GList *messageview_get_window_list(void)
679 {
680         return messageview_list;
681 }
682
683 static gboolean find_encrypted_func(GNode *node, gpointer data)
684 {
685         MimeInfo *mimeinfo = (MimeInfo *) node->data;
686         MimeInfo **encinfo = (MimeInfo **) data;
687         
688         if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
689                 *encinfo = mimeinfo;
690                 return TRUE;
691         }
692         
693         return FALSE;
694 }
695
696 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
697 {
698         MimeInfo *encinfo = NULL;
699
700         g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
701                 find_encrypted_func, &encinfo);
702         
703         return encinfo;
704 }
705
706 gint messageview_show(MessageView *messageview, MsgInfo *msginfo,
707                       gboolean all_headers)
708 {
709         gchar *file;
710         MimeInfo *mimeinfo, *encinfo;
711
712         g_return_val_if_fail(msginfo != NULL, -1);
713
714         mimeinfo = procmime_scan_message(msginfo);
715         if (!mimeinfo) {
716                 textview_show_error(messageview->mimeview->textview);
717                 return -1;
718         }
719
720         while ((encinfo = find_encrypted_part(mimeinfo)) != NULL) {
721                 debug_print("decrypting message part\n");
722                 if (privacy_mimeinfo_decrypt(encinfo) < 0)
723                         break;
724         }
725         
726         file = procmsg_get_message_file_path(msginfo);
727         if (!file) {
728                 g_warning("can't get message file path.\n");
729                 procmime_mimeinfo_free_all(mimeinfo);
730                 textview_show_error(messageview->mimeview->textview);
731                 return -1;
732         }
733
734         if (messageview->msginfo != msginfo) {
735                 procmsg_msginfo_free(messageview->msginfo);
736                 messageview->msginfo = procmsg_msginfo_get_full_info(msginfo);
737                 if (!messageview->msginfo)
738                         messageview->msginfo = procmsg_msginfo_copy(msginfo);
739         }
740         headerview_show(messageview->headerview, messageview->msginfo);
741
742         messageview->all_headers = all_headers;
743         textview_set_all_headers(messageview->mimeview->textview, all_headers);
744
745         mimeview_show_message(messageview->mimeview, mimeinfo, file);
746         messageview_set_position(messageview, 0);
747
748         if (messageview->msginfo->partial_recv)
749                 partial_recv_show(messageview->noticeview, 
750                                   messageview->msginfo);
751         else if ((messageview->msginfo->dispositionnotificationto || 
752              messageview->msginfo->returnreceiptto) &&
753             !MSG_IS_RETRCPT_SENT(messageview->msginfo->flags))
754                 return_receipt_show(messageview->noticeview, 
755                                     messageview->msginfo);
756         else 
757                 noticeview_hide(messageview->noticeview);
758
759         mimeinfo = procmime_mimeinfo_next(mimeinfo);
760         if (mimeinfo && (mimeinfo->type != MIMETYPE_TEXT || 
761             (strcmp(mimeinfo->subtype, "plain") &&
762              strcmp(mimeinfo->subtype, "html")))) {
763                 mimeview_show_part(messageview->mimeview,mimeinfo);
764         }
765
766         g_free(file);
767
768         return 0;
769 }
770
771 void messageview_reflect_prefs_pixmap_theme(void)
772 {
773         GList *cur;
774         MessageView *msgview;
775
776         for (cur = msgview_list; cur != NULL; cur = cur->next) {
777                 msgview = (MessageView*)cur->data;
778                 toolbar_update(TOOLBAR_MSGVIEW, msgview);
779                 mimeview_update(msgview->mimeview);
780         }
781 }
782
783 void messageview_clear(MessageView *messageview)
784 {
785         procmsg_msginfo_free(messageview->msginfo);
786         messageview->msginfo = NULL;
787         messageview->filtered = FALSE;
788         mimeview_clear(messageview->mimeview);
789         headerview_clear(messageview->headerview);
790         noticeview_hide(messageview->noticeview);
791 }
792
793 void messageview_destroy(MessageView *messageview)
794 {
795         debug_print("destroy messageview\n");
796         messageview_list = g_list_remove(messageview_list, messageview);
797
798         hooks_unregister_hook(MSGINFO_UPDATE_HOOKLIST,
799                               messageview->msginfo_update_callback_id);
800
801         headerview_destroy(messageview->headerview);
802         mimeview_destroy(messageview->mimeview);
803         noticeview_destroy(messageview->noticeview);
804
805         procmsg_msginfo_free(messageview->msginfo);
806         toolbar_clear_list(TOOLBAR_MSGVIEW);
807         if (messageview->toolbar) {
808                 toolbar_destroy(messageview->toolbar);
809                 g_free(messageview->toolbar);
810         }
811         
812         msgview_list = g_list_remove(msgview_list, messageview); 
813
814         g_free(messageview);
815 }
816
817 void messageview_delete(MessageView *msgview)
818 {
819         MsgInfo *msginfo = NULL;
820         FolderItem *trash = NULL;
821         PrefsAccount *ac = NULL;
822
823         if (msgview->msginfo && msgview->mainwin && msgview->mainwin->summaryview)
824                 msginfo = summary_get_selected_msg(msgview->mainwin->summaryview);
825         
826         /* need a procmsg_msginfo_equal() */
827         if (msginfo && msgview->msginfo && 
828             msginfo->msgnum == msgview->msginfo->msgnum && 
829             msginfo->folder == msgview->msginfo->folder) {
830                 summary_delete(msgview->mainwin->summaryview);
831         } else {                
832                 msginfo = msgview->msginfo;
833
834                 g_return_if_fail(msginfo != NULL);
835
836                 /* to get the trash folder, we have to choose either
837                  * the folder's or account's trash default - we prefer
838                  * the one in the account prefs */
839                 if (msginfo->folder) {
840                         if (NULL != (ac = account_find_from_item(msginfo->folder)))
841                                 trash = account_get_special_folder(ac, F_TRASH);
842                         if (!trash && msginfo->folder->folder)  
843                                 trash = msginfo->folder->folder->trash;
844                         /* if still not found, use the default */
845                         if (!trash) 
846                                 trash = folder_get_default_trash();
847                 }       
848
849                 g_return_if_fail(trash != NULL);
850
851                 if (prefs_common.immediate_exec)
852                         /* TODO: Delete from trash */
853                         folder_item_move_msg(trash, msginfo);
854                 else {
855                         procmsg_msginfo_set_to_folder(msginfo, trash);
856                         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
857                         /* NOTE: does not update to next message in summaryview */
858                 }
859         }               
860 }
861
862 /* 
863  * \brief update messageview with currently selected message in summaryview
864  *        leave unchanged if summaryview is empty
865  * \param pointer to MessageView
866  */     
867 static void messageview_update(MessageView *msgview)
868 {
869         SummaryView *summaryview = (SummaryView*)msgview->mainwin->summaryview;
870
871         g_return_if_fail(summaryview != NULL);
872         
873         if (summaryview->selected) {
874                 GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
875                 MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, 
876                                                       summaryview->selected);
877                 g_return_if_fail(msginfo != NULL);
878
879                 messageview_show(msgview, msginfo, 
880                                  msgview->all_headers);
881         } 
882 }
883
884 void messageview_quote_color_set(void)
885 {
886 }
887
888 void messageview_set_font(MessageView *messageview)
889 {
890 }
891
892 TextView *messageview_get_current_textview(MessageView *messageview)
893 {
894         TextView *text = NULL;
895
896         text = messageview->mimeview->textview;
897
898         return text;
899 }
900
901 MimeInfo *messageview_get_selected_mime_part(MessageView *messageview)
902 {
903         return mimeview_get_selected_part(messageview->mimeview);
904 }
905
906 void messageview_copy_clipboard(MessageView *messageview)
907 {
908         TextView *text;
909
910         text = messageview_get_current_textview(messageview);
911         if (text) {
912                 GtkTextView *textview = GTK_TEXT_VIEW(text->text);
913                 GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
914                 GtkClipboard *clipboard
915                         = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
916
917                 gtk_text_buffer_copy_clipboard(buffer, clipboard);
918         }
919 }
920
921 void messageview_select_all(MessageView *messageview)
922 {
923         TextView *text;
924
925         text = messageview_get_current_textview(messageview);
926         if (text) {
927                 GtkTextView *textview = GTK_TEXT_VIEW(text->text);
928                 GtkTextBuffer *buffer;
929                 GtkTextIter start, end;
930
931                 buffer = gtk_text_view_get_buffer(textview);
932                 gtk_text_buffer_get_bounds(buffer, &start, &end);
933                 gtk_text_buffer_select_range(buffer, &start, &end);
934         }
935 }
936
937 void messageview_set_position(MessageView *messageview, gint pos)
938 {
939         TextView *text;
940
941         text = messageview_get_current_textview(messageview);
942         if (text)
943                 textview_set_position(text, pos);
944 }
945
946 gboolean messageview_search_string(MessageView *messageview, const gchar *str,
947                                    gboolean case_sens)
948 {
949         TextView *text;
950
951         text = messageview_get_current_textview(messageview);
952         if (text)
953                 return textview_search_string(text, str, case_sens);
954         return FALSE;
955 }
956
957 gboolean messageview_search_string_backward(MessageView *messageview,
958                                             const gchar *str,
959                                             gboolean case_sens)
960 {
961         TextView *text;
962
963         text = messageview_get_current_textview(messageview);
964         if (text)       
965                 return textview_search_string_backward(text,
966                                                        str, case_sens);
967         return FALSE;
968 }
969
970 gboolean messageview_is_visible(MessageView *messageview)
971 {
972         return messageview->visible;
973 }
974
975 void messageview_save_as(MessageView *messageview)
976 {
977         gchar *filename = NULL;
978         MsgInfo *msginfo;
979         gchar *src, *dest, *tmp;
980
981         if (!messageview->msginfo) return;
982         msginfo = messageview->msginfo;
983
984         if (msginfo->subject) {
985                 Xstrdup_a(filename, msginfo->subject, return);
986                 subst_for_filename(filename);
987         }
988         dest = filesel_select_file_save(_("Save as"), filename);
989         if (!dest) return;
990         if (is_file_exist(dest)) {
991                 AlertValue aval;
992
993                 aval = alertpanel(_("Overwrite"),
994                                   _("Overwrite existing file?"),
995                                   GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL);
996                 if (G_ALERTDEFAULT != aval) return;
997         }
998
999         src = procmsg_get_message_file(msginfo);
1000         if (copy_file(src, dest, TRUE) < 0) {
1001                 tmp =  g_path_get_basename(dest);
1002                 alertpanel_error(_("Can't save the file `%s'."), tmp);
1003                 g_free(tmp);
1004         }
1005         g_free(dest);
1006         g_free(src);
1007 }
1008
1009 static void messageview_destroy_cb(GtkWidget *widget, MessageView *messageview)
1010 {
1011         messageview_destroy(messageview);
1012 }
1013
1014 static void messageview_size_allocate_cb(GtkWidget *widget,
1015                                          GtkAllocation *allocation)
1016 {
1017         g_return_if_fail(allocation != NULL);
1018
1019         prefs_common.msgwin_width  = allocation->width;
1020         prefs_common.msgwin_height = allocation->height;
1021 }
1022
1023 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
1024                         MessageView *messageview)
1025 {
1026         if (event && event->keyval == GDK_Escape && messageview->window)
1027                 gtk_widget_destroy(messageview->window);
1028         return FALSE;
1029 }
1030
1031 void messageview_toggle_view_real(MessageView *messageview)
1032 {
1033         MainWindow *mainwin = messageview->mainwin;
1034         union CompositeWin *cwin = &mainwin->win;
1035         GtkWidget *vpaned = NULL;
1036         GtkWidget *container = NULL;
1037         GtkItemFactory *ifactory = gtk_item_factory_from_widget(mainwin->menubar);
1038         
1039         switch (mainwin->type) {
1040         case SEPARATE_NONE:
1041                 vpaned = cwin->sep_none.vpaned;
1042                 container = cwin->sep_none.hpaned;
1043                 break;
1044         case SEPARATE_FOLDER:
1045                 vpaned = cwin->sep_folder.vpaned;
1046                 container = mainwin->vbox_body;
1047                 break;
1048         case SEPARATE_MESSAGE:
1049         case SEPARATE_BOTH:
1050                 return;
1051         }
1052
1053         if (vpaned->parent != NULL) {
1054                 gtk_widget_ref(vpaned);
1055                 gtkut_container_remove(GTK_CONTAINER(container), vpaned);
1056                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), container);
1057                 menu_set_sensitive(ifactory, "/View/Expand Summary View", FALSE);
1058         } else {
1059                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), vpaned);
1060                 gtk_container_add(GTK_CONTAINER(container), vpaned);
1061                 gtk_widget_unref(vpaned);
1062                 menu_set_sensitive(ifactory, "/View/Expand Summary View", TRUE);
1063                 gtk_widget_grab_focus(GTK_WIDGET(mainwin->summaryview->ctree));
1064         }
1065 }
1066
1067 static void return_receipt_show(NoticeView *noticeview, MsgInfo *msginfo)
1068 {
1069         noticeview_set_text(noticeview, _("This message asks for a return receipt."));
1070         noticeview_set_button_text(noticeview, _("Send receipt"));
1071         noticeview_set_button_press_callback(noticeview,
1072                                              G_CALLBACK(return_receipt_send_clicked),
1073                                              (gpointer) msginfo);
1074         noticeview_show(noticeview);
1075 }
1076
1077 static void return_receipt_send_clicked(NoticeView *noticeview, MsgInfo *msginfo)
1078 {
1079         MsgInfo *tmpmsginfo;
1080         gchar *file;
1081
1082         file = procmsg_get_message_file_path(msginfo);
1083         if (!file) {
1084                 g_warning("can't get message file path.\n");
1085                 return;
1086         }
1087
1088         tmpmsginfo = procheader_parse_file(file, msginfo->flags, TRUE, TRUE);
1089         tmpmsginfo->folder = msginfo->folder;
1090         tmpmsginfo->msgnum = msginfo->msgnum;
1091
1092         if (disposition_notification_send(tmpmsginfo) >= 0) {
1093                 procmsg_msginfo_set_flags(msginfo, MSG_RETRCPT_SENT, 0);
1094                 noticeview_hide(noticeview);
1095         }               
1096
1097         procmsg_msginfo_free(tmpmsginfo);
1098         g_free(file);
1099 }
1100
1101 static void partial_recv_show(NoticeView *noticeview, MsgInfo *msginfo)
1102 {
1103         gchar *text = NULL;
1104         gchar *button1 = NULL;
1105         gchar *button2 = NULL;
1106         void  *button1_cb = NULL;
1107         void  *button2_cb = NULL;
1108
1109         if (!partial_msg_in_uidl_list(msginfo)) {
1110                 text = g_strdup_printf(_("This message has been partially "
1111                                 "retrieved,\nand has been deleted from the "
1112                                 "server."));
1113         } else {
1114                 switch (msginfo->planned_download) {
1115                 case POP3_PARTIAL_DLOAD_UNKN:
1116                         text = g_strdup_printf(_("This message has been "
1117                                         "partially retrieved;\nit is %s."),
1118                                         to_human_readable(
1119                                                 (off_t)(msginfo->total_size)));
1120                         button1 = _("Mark for download");
1121                         button2 = _("Mark for deletion");
1122                         button1_cb = partial_recv_dload_clicked;
1123                         button2_cb = partial_recv_del_clicked;
1124                         break;
1125                 case POP3_PARTIAL_DLOAD_DLOAD:
1126                         text = g_strdup_printf(_("This message has been "
1127                                         "partially retrieved;\nit is %s and "
1128                                         "will be downloaded."),
1129                                         to_human_readable(
1130                                                 (off_t)(msginfo->total_size)));
1131                         button1 = _("Unmark");
1132                         button1_cb = partial_recv_unmark_clicked;
1133                         button2 = _("Mark for deletion");
1134                         button2_cb = partial_recv_del_clicked;
1135                         break;
1136                 case POP3_PARTIAL_DLOAD_DELE:
1137                         text = g_strdup_printf(_("This message has been "
1138                                         "partially retrieved;\nit is %s and "
1139                                         "will be deleted."),
1140                                         to_human_readable(
1141                                                 (off_t)(msginfo->total_size)));
1142                         button1 = _("Mark for download");
1143                         button1_cb = partial_recv_dload_clicked;
1144                         button2 = _("Unmark");
1145                         button2_cb = partial_recv_unmark_clicked;
1146                         break;
1147                 default:
1148                         return;
1149                 }
1150         }
1151         
1152         noticeview_set_text(noticeview, text);
1153         g_free(text);
1154         noticeview_set_button_text(noticeview, button1);
1155         noticeview_set_button_press_callback(noticeview,
1156                      G_CALLBACK(button1_cb), (gpointer) msginfo);
1157
1158         noticeview_set_2ndbutton_text(noticeview, button2);
1159         noticeview_set_2ndbutton_press_callback(noticeview,
1160                      G_CALLBACK(button2_cb), (gpointer) msginfo);
1161
1162         noticeview_show(noticeview);
1163 }
1164
1165 static void partial_recv_dload_clicked(NoticeView *noticeview, 
1166                                        MsgInfo *msginfo)
1167 {
1168         if (partial_mark_for_download(msginfo) == 0) {
1169                 partial_recv_show(noticeview, msginfo);
1170         }
1171 }
1172
1173 static void partial_recv_del_clicked(NoticeView *noticeview, 
1174                                        MsgInfo *msginfo)
1175 {
1176         if (partial_mark_for_delete(msginfo) == 0) {
1177                 partial_recv_show(noticeview, msginfo);
1178         }
1179 }
1180
1181 static void partial_recv_unmark_clicked(NoticeView *noticeview, 
1182                                        MsgInfo *msginfo)
1183 {
1184         if (partial_unmark(msginfo) == 0) {
1185                 partial_recv_show(noticeview, msginfo);
1186         }
1187 }
1188
1189 static void select_account_cb(GtkWidget *w, gpointer data)
1190 {
1191         *(gint*)data = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w), MENU_VAL_ID));
1192 }
1193         
1194 static PrefsAccount *select_account_from_list(GList *ac_list)
1195 {
1196         GtkWidget *optmenu;
1197         GtkWidget *menu;
1198         gint account_id;
1199
1200         g_return_val_if_fail(ac_list != NULL, NULL);
1201         g_return_val_if_fail(ac_list->data != NULL, NULL);
1202         
1203         optmenu = gtk_option_menu_new();
1204         menu = gtkut_account_menu_new(ac_list, 
1205                         G_CALLBACK(select_account_cb), 
1206                         &account_id);
1207         if (!menu)
1208                 return NULL;
1209         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), menu);
1210         gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), 0);
1211         account_id = ((PrefsAccount *) ac_list->data)->account_id;
1212         if (alertpanel_with_widget(
1213                                 _("Return Receipt Notification"),
1214                                 _("The message was sent to several of your "
1215                                   "accounts.\n"
1216                                   "Please choose which account do you want to "
1217                                   "use for sending the receipt notification:"),
1218                                 _("Send Notification"), _("+Cancel"), NULL,
1219                                 optmenu) != G_ALERTDEFAULT)
1220                 return NULL;
1221         return account_find_from_id(account_id);
1222 }
1223
1224 /* 
1225  * \brief return selected messageview text, when nothing is 
1226  *        selected and message was filtered, return complete text
1227  *
1228  * \param  pointer to Messageview 
1229  *
1230  * \return pointer to text (needs to be free'd by calling func)
1231  */
1232 gchar *messageview_get_selection(MessageView *msgview)
1233 {
1234         TextView *textview;
1235         gchar *text = NULL;
1236         GtkTextView *edit = NULL;
1237         GtkTextBuffer *textbuf;
1238         gint body_pos = 0;
1239         
1240         g_return_val_if_fail(msgview != NULL, NULL);
1241
1242         textview = messageview_get_current_textview(msgview);
1243         g_return_val_if_fail(textview != NULL, NULL);
1244
1245         edit = GTK_TEXT_VIEW(textview->text);
1246         g_return_val_if_fail(edit != NULL, NULL);
1247         body_pos = textview->body_pos;
1248
1249         textbuf = gtk_text_view_get_buffer(edit);
1250
1251         if (gtk_text_buffer_get_selection_bounds(textbuf, NULL, NULL))
1252                 return gtkut_text_view_get_selection(edit);
1253         else if (msgview->filtered) {
1254                 GtkTextIter start_iter, end_iter;
1255                 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter, body_pos);
1256                 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
1257                 gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter, FALSE);
1258         } else
1259                 text = NULL;
1260
1261         return text;
1262 }
1263
1264 static void save_as_cb(gpointer data, guint action, GtkWidget *widget)
1265 {
1266         MessageView *messageview = (MessageView *)data;
1267         messageview_save_as(messageview);
1268 }
1269
1270 static void print_cb(gpointer data, guint action, GtkWidget *widget)
1271 {
1272         MessageView *messageview = (MessageView *)data;
1273         gchar *cmdline;
1274         gchar *p;
1275
1276         if (!messageview->msginfo) return;
1277
1278         cmdline = input_dialog(_("Print"),
1279                                _("Enter the print command line:\n"
1280                                  "(`%s' will be replaced with file name)"),
1281                                prefs_common.print_cmd);
1282         if (!cmdline) return;
1283         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
1284             strchr(p + 2, '%')) {
1285                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
1286                                  cmdline);
1287                 g_free(cmdline);
1288                 return;
1289         }
1290
1291         procmsg_print_message(messageview->msginfo, cmdline);
1292         g_free(cmdline);
1293 }
1294
1295 static void close_cb(gpointer data, guint action, GtkWidget *widget)
1296 {
1297         MessageView *messageview = (MessageView *)data;
1298         gtk_widget_destroy(messageview->window);
1299 }
1300
1301 static void copy_cb(gpointer data, guint action, GtkWidget *widget)
1302 {
1303         MessageView *messageview = (MessageView *)data;
1304         messageview_copy_clipboard(messageview);
1305 }
1306
1307 static void allsel_cb(gpointer data, guint action, GtkWidget *widget)
1308 {
1309         MessageView *messageview = (MessageView *)data;
1310         messageview_select_all(messageview);
1311 }
1312
1313 static void search_cb(gpointer data, guint action, GtkWidget *widget)
1314 {
1315         MessageView *messageview = (MessageView *)data;
1316         message_search(messageview);
1317 }
1318
1319 static void set_charset_cb(gpointer data, guint action, GtkWidget *widget)
1320 {
1321         MessageView *messageview = (MessageView *)data;
1322         const gchar *charset;
1323
1324         if (GTK_CHECK_MENU_ITEM(widget)->active) {
1325                 charset = conv_get_charset_str((CharSet)action);
1326                 g_free(messageview->forced_charset);
1327                 messageview->forced_charset = g_strdup(charset);
1328                 procmime_force_charset(charset);
1329                 
1330                 messageview_show(messageview, messageview->msginfo, FALSE);
1331         }
1332 }
1333
1334 static void set_decode_cb(gpointer data, guint action, GtkWidget *widget)
1335 {
1336         MessageView *messageview = (MessageView *)data;
1337         if (GTK_CHECK_MENU_ITEM(widget)->active) {
1338                 messageview->forced_encoding = (EncodingType)action;
1339
1340                 messageview_show(messageview, messageview->msginfo, FALSE);
1341                 
1342                 debug_print("forced encoding: %d\n", action);
1343         }
1344 }
1345
1346
1347 static void view_source_cb(gpointer data, guint action, GtkWidget *widget)
1348 {
1349         MessageView *messageview = (MessageView *)data;
1350         SourceWindow *srcwin;
1351
1352         if (!messageview->msginfo) return;
1353
1354         srcwin = source_window_create();
1355         source_window_show_msg(srcwin, messageview->msginfo);
1356         source_window_show(srcwin);
1357 }
1358
1359 static void show_all_header_cb(gpointer data, guint action, GtkWidget *widget)
1360 {
1361         MessageView *messageview = (MessageView *)data;
1362         MsgInfo *msginfo = messageview->msginfo;
1363
1364         if (!msginfo) return;
1365         messageview->msginfo = NULL;
1366         messageview_show(messageview, msginfo,
1367                          GTK_CHECK_MENU_ITEM(widget)->active);
1368         procmsg_msginfo_free(msginfo);
1369         main_window_set_menu_sensitive(messageview->mainwin);
1370 }
1371
1372 static void compose_cb(gpointer data, guint action, GtkWidget *widget)
1373 {
1374         MessageView *messageview = (MessageView *)data;
1375         PrefsAccount *ac = NULL;
1376         FolderItem *item = NULL;
1377
1378         if (messageview->msginfo)
1379                 item = messageview->msginfo->folder;
1380
1381         if (item) {
1382                 ac = account_find_from_item(item);
1383                 if (ac && ac->protocol == A_NNTP &&
1384                     item->stype == F_NEWS) {
1385                         compose_new(ac, item->path, NULL);
1386                         return;
1387                 }
1388         }
1389
1390         compose_new(ac, NULL, NULL);
1391 }
1392
1393 static void reply_cb(gpointer data, guint action, GtkWidget *widget)
1394 {
1395         MessageView *messageview = (MessageView *)data;
1396         GSList *msginfo_list = NULL;
1397         MsgInfo *msginfo;
1398
1399         g_return_if_fail(messageview->msginfo);
1400
1401         msginfo_list = g_slist_append(msginfo_list, messageview->msginfo);
1402         compose_reply_from_messageview(messageview, msginfo_list, action);
1403         g_slist_free(msginfo_list);
1404 }
1405
1406 static void reedit_cb(gpointer data, guint action, GtkWidget *widget)
1407 {
1408         MessageView *messageview = (MessageView *)data;
1409         MsgInfo *msginfo;
1410
1411         if (!messageview->msginfo) return;
1412         msginfo = messageview->msginfo;
1413         if (!msginfo->folder) return;
1414         if (msginfo->folder->stype != F_OUTBOX &&
1415             msginfo->folder->stype != F_DRAFT &&
1416             msginfo->folder->stype != F_QUEUE) return;
1417
1418         compose_reedit(msginfo);
1419 }
1420
1421 static void addressbook_open_cb(gpointer data, guint action, GtkWidget *widget)
1422 {
1423         addressbook_open(NULL);
1424 }
1425
1426 static void add_address_cb(gpointer data, guint action, GtkWidget *widget)
1427 {
1428         MessageView *messageview = (MessageView *)data;
1429         MsgInfo *msginfo;
1430         gchar *from;
1431
1432         if (!messageview->msginfo) return;
1433         msginfo = messageview->msginfo;
1434         Xstrdup_a(from, msginfo->from, return);
1435         eliminate_address_comment(from);
1436         extract_address(from);
1437         addressbook_add_contact(msginfo->fromname, from, NULL);
1438 }
1439
1440 static void create_filter_cb(gpointer data, guint action, GtkWidget *widget)
1441 {
1442         MessageView *messageview = (MessageView *)data;
1443         FolderItem * item;
1444         
1445         if (!messageview->msginfo) return;
1446         
1447         item = messageview->msginfo->folder;
1448         summary_msginfo_filter_open(item,  messageview->msginfo,
1449                                     (PrefsFilterType)action, 0);
1450 }
1451
1452 static void create_processing_cb(gpointer data, guint action,
1453                                  GtkWidget *widget)
1454 {
1455         MessageView *messageview = (MessageView *)data;
1456         FolderItem * item;
1457         
1458         if (!messageview->msginfo) return;
1459         
1460         item = messageview->msginfo->folder;
1461         summary_msginfo_filter_open(item,  messageview->msginfo,
1462                                     (PrefsFilterType)action, 1);
1463 }
1464
1465 static void about_cb(gpointer data, guint action, GtkWidget *widget)
1466 {
1467         about_show();
1468 }
1469
1470 static gboolean messageview_update_msg(gpointer source, gpointer data)
1471 {
1472         MsgInfoUpdate *msginfo_update = (MsgInfoUpdate *) source;
1473         MessageView *messageview = (MessageView *)data;
1474
1475         if (messageview->msginfo != msginfo_update->msginfo)
1476                 return FALSE;
1477
1478         if (msginfo_update->flags & MSGINFO_UPDATE_DELETED) {
1479                 messageview_clear(messageview);
1480                 messageview_update(messageview);
1481         }
1482
1483         return FALSE;
1484 }
1485
1486 void messageview_set_menu_sensitive(MessageView *messageview)
1487 {
1488         GtkItemFactory *ifactory;
1489         GtkWidget *menuitem;
1490
1491         if (!messageview || !messageview->new_window) 
1492                 return;
1493         /* do some smart things */
1494         if (!messageview->menubar) return;
1495         ifactory = gtk_item_factory_from_widget(messageview->menubar);
1496         if (!ifactory) return;
1497         if (messageview->mainwin->type == SEPARATE_MESSAGE) {
1498                 menuitem = gtk_item_factory_get_widget(ifactory, "/View/Show all headers");
1499                 gtk_check_menu_item_set_active
1500                         (GTK_CHECK_MENU_ITEM(menuitem),
1501                          messageview->mimeview->textview->show_all_headers);
1502         }
1503 }