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