54beb02212a232a12af69b0ac485443eeca44338
[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         gtk_widget_realize(handlebox);
404         msgview->toolbar = toolbar_create(TOOLBAR_MSGVIEW, handlebox,
405                                           (gpointer)msgview);
406
407         statusbar = gtk_statusbar_new();
408         gtk_widget_show(statusbar);
409         gtk_box_pack_end(GTK_BOX(vbox), statusbar, FALSE, FALSE, 0);
410         msgview->statusbar = statusbar;
411         msgview->statusbar_cid = gtk_statusbar_get_context_id
412                 (GTK_STATUSBAR(statusbar), "Message View");
413
414         msgview->handlebox = handlebox;
415         msgview->menubar   = menubar;
416
417         gtk_container_add(GTK_CONTAINER(vbox),
418                           GTK_WIDGET_PTR(msgview));
419
420         messageview_update_actions_menu(msgview);
421
422         msgview_list = g_list_append(msgview_list, msgview);
423 }
424
425 MessageView *messageview_create_with_new_window(MainWindow *mainwin)
426 {
427         MessageView *msgview;
428         GtkWidget *window;
429         static GdkGeometry geometry;
430
431         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
432         gtk_window_set_title(GTK_WINDOW(window), _("Sylpheed - Message View"));
433         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
434
435         if (!geometry.min_height) {
436                 geometry.min_width = 320;
437                 geometry.min_height = 200;
438         }
439         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
440                                       GDK_HINT_MIN_SIZE);
441
442         gtk_widget_set_size_request(window, prefs_common.msgwin_width,
443                                     prefs_common.msgwin_height);
444
445         msgview = messageview_create(mainwin);
446
447         g_signal_connect(G_OBJECT(window), "size_allocate",
448                          G_CALLBACK(messageview_size_allocate_cb),
449                          msgview);
450         g_signal_connect(G_OBJECT(window), "delete_event",
451                          G_CALLBACK(messageview_delete_cb), msgview);
452         g_signal_connect(G_OBJECT(window), "key_press_event",
453                          G_CALLBACK(key_pressed), msgview);
454
455         messageview_add_toolbar(msgview, window);
456
457         gtk_widget_grab_focus(msgview->mimeview->textview->text);
458         gtk_widget_show(window);
459
460         msgview->new_window = TRUE;
461         msgview->window = window;
462         msgview->visible = TRUE;
463
464         toolbar_set_style(msgview->toolbar->toolbar, msgview->handlebox, 
465                           prefs_common.toolbar_style);
466         messageview_init(msgview);
467
468         return msgview;
469 }
470
471 void messageview_init(MessageView *messageview)
472 {
473         headerview_init(messageview->headerview);
474         mimeview_init(messageview->mimeview);
475         /*messageview_set_font(messageview);*/
476
477         noticeview_hide(messageview->noticeview);
478 }
479
480 static void notification_convert_header(gchar *dest, gint len, 
481                                         const gchar *src_,
482                                         gint header_len)
483 {
484         char *src;
485
486         g_return_if_fail(src_ != NULL);
487         g_return_if_fail(dest != NULL);
488
489         if (len < 1) return;
490
491         Xstrndup_a(src, src_, len, return);
492
493         remove_return(src);
494
495         if (is_ascii_str(src)) {
496                 strncpy2(dest, src, len);
497                 dest[len - 1] = '\0';
498                 return;
499         } else
500                 conv_encode_header(dest, len, src, header_len, FALSE);
501 }
502
503 static gint disposition_notification_send(MsgInfo *msginfo)
504 {
505         gchar buf[BUFFSIZE];
506         gchar tmp[MAXPATHLEN + 1];
507         FILE *fp;
508         GList *ac_list;
509         PrefsAccount *account = NULL;
510         gint ok;
511         gchar *to;
512         FolderItem *queue, *outbox;
513         gint num;
514         gchar *path;
515         gchar *addr;
516         gchar *addrp;
517
518         if ((!msginfo->returnreceiptto) && 
519             (!msginfo->dispositionnotificationto)) 
520                 return -1;
521
522         /* RFC2298: Test for Return-Path */
523         if (msginfo->dispositionnotificationto)
524                 to = msginfo->dispositionnotificationto;
525         else
526                 to = msginfo->returnreceiptto;
527
528         ok = procheader_get_header_from_msginfo(msginfo, buf, sizeof(buf),
529                                 "Return-Path:");
530         if (ok == 0) {
531                 gchar *to_addr = g_strdup(to);
532                 extract_address(to_addr);
533                 extract_address(buf);
534                 ok = strcmp(to_addr, buf);
535                 g_free(to_addr);
536         } else {
537                 strncpy(buf, _("<No Return-Path found>"), 
538                                 sizeof(buf));
539         }
540         
541         if (ok != 0) {
542                 AlertValue val;
543                 gchar *message;
544                 message = g_strdup_printf(
545                                  _("The notification address to which the "
546                                    "return receipt is to be sent\n"
547                                    "does not correspond to the return path:\n"
548                                    "Notification address: %s\n"
549                                    "Return path: %s\n"
550                                    "It is advised to not to send the return "
551                                    "receipt."), to, buf);
552                 val = alertpanel_with_type(_("Warning"), message, _("Send"),
553                                 _("+Don't Send"), NULL, NULL, ALERT_WARNING);
554                 g_free(message);                                
555                 if (val != G_ALERTDEFAULT)
556                         return -1;
557         }
558
559         ac_list = account_find_all_from_address(NULL, msginfo->to);
560         ac_list = account_find_all_from_address(ac_list, msginfo->cc);
561
562         if (ac_list == NULL) {
563                 AlertValue val = 
564                 alertpanel_with_type(_("Warning"),
565                                 _("This message is asking for a return "
566                                    "receipt notification\n"
567                                    "but according to its 'To:' and 'CC:' "
568                                    "headers it was not\nofficially addressed "
569                                    "to you.\n"
570                                    "It is advised to not to send the return ."
571                                    "receipt."),
572                                 _("Send"), _("+Don't Send"), NULL, 
573                                 NULL, ALERT_WARNING);
574                 if (val != G_ALERTDEFAULT)
575                         return -1;
576         }
577
578         if (g_list_length(ac_list) > 1)
579                 account = select_account_from_list(ac_list);
580         else if (ac_list != NULL)
581                 account = (PrefsAccount *) ac_list->data;
582         g_list_free(ac_list);
583
584         if (account == NULL)
585                 account = account_get_default();
586         if (!account || account->protocol == A_NNTP) {
587                 alertpanel_error(_("Account for sending mail is not specified.\n"
588                                    "Please select a mail account before sending."));
589                 return -1;
590         }
591
592         /* write to temporary file */
593         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
594                    get_rc_dir(), G_DIR_SEPARATOR, (gint)msginfo);
595
596         if ((fp = fopen(tmp, "wb")) == NULL) {
597                 FILE_OP_ERROR(tmp, "fopen");
598                 return -1;
599         }
600
601         /* chmod for security */
602         if (change_file_mode_rw(fp, tmp) < 0) {
603                 FILE_OP_ERROR(tmp, "chmod");
604                 g_warning("can't change file mode\n");
605         }
606         
607         addr = g_strdup(to);
608         
609         extract_address(addr);
610         addrp = addr;
611         
612         /* write queue headers */
613         fprintf(fp, "AF:\n");
614         fprintf(fp, "NF:0\n");
615         fprintf(fp, "PS:10\n");
616         fprintf(fp, "SRH:1\n");
617         fprintf(fp, "SFN:\n");
618         fprintf(fp, "DSR:\n");
619         fprintf(fp, "MID:\n");
620         fprintf(fp, "CFG:\n");
621         fprintf(fp, "PT:0\n");
622         fprintf(fp, "S:%s\n", account->address);
623         fprintf(fp, "RQ:\n");
624         if (account->smtp_server)
625                 fprintf(fp, "SSV:%s\n", account->smtp_server);
626         else
627                 fprintf(fp, "SSV:\n");
628         fprintf(fp, "SSH:\n");
629         fprintf(fp, "R:<%s>\n", addrp);
630         
631         g_free(addrp);
632         
633         /* check whether we need to save the message */
634         outbox = account_get_special_folder(account, F_OUTBOX); 
635         if (folder_get_default_outbox() == outbox && !prefs_common.savemsg)
636                 outbox = NULL;
637         if (outbox) {
638                 path = folder_item_get_identifier(outbox);
639                 fprintf(fp, "SCF:%s\n", path);
640                 g_free(path);
641         }               
642
643         fprintf(fp, "\n");
644         
645         /* Date */
646         get_rfc822_date(buf, sizeof(buf));
647         fprintf(fp, "Date: %s\n", buf);
648
649         /* From */
650         if (account->name && *account->name) {
651                 notification_convert_header
652                         (buf, sizeof(buf), account->name,
653                          strlen("From: "));
654                 fprintf(fp, "From: %s <%s>\n", buf, account->address);
655         } else
656                 fprintf(fp, "From: %s\n", account->address);
657
658         fprintf(fp, "To: %s\n", to);
659
660         /* Subject */
661         notification_convert_header(buf, sizeof(buf), msginfo->subject,
662                                     strlen("Subject: "));
663         fprintf(fp, "Subject: Disposition notification: %s\n", buf);
664
665         /* Message ID */
666         generate_msgid(buf, sizeof(buf));
667         fprintf(fp, "Message-ID: <%s>\n", buf);
668
669         if (fclose(fp) == EOF) {
670                 FILE_OP_ERROR(tmp, "fclose");
671                 unlink(tmp);
672                 return -1;
673         }
674
675         /* put it in queue */
676         queue = account_get_special_folder(account, F_QUEUE);
677         if (!queue) queue = folder_get_default_queue();
678         if (!queue) {
679                 g_warning("can't find queue folder\n");
680                 unlink(tmp);
681                 return -1;
682         }
683         folder_item_scan(queue);
684         if ((num = folder_item_add_msg(queue, tmp, NULL, TRUE)) < 0) {
685                 g_warning("can't queue the message\n");
686                 unlink(tmp);
687                 return -1;
688         }
689                 
690         if (prefs_common.work_offline)
691                 if (alertpanel(_("Offline warning"), 
692                                _("You're working offline. Override?"),
693                                _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
694                         return 0;
695
696         /* send it */
697         path = folder_item_fetch_msg(queue, num);
698         ok = procmsg_send_message_queue(path);
699         g_free(path);
700         folder_item_remove_msg(queue, num);
701
702         return ok;
703 }
704
705 GList *messageview_get_window_list(void)
706 {
707         return messageview_list;
708 }
709
710 static gboolean find_encrypted_func(GNode *node, gpointer data)
711 {
712         MimeInfo *mimeinfo = (MimeInfo *) node->data;
713         MimeInfo **encinfo = (MimeInfo **) data;
714         
715         if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
716                 *encinfo = mimeinfo;
717                 return TRUE;
718         }
719         
720         return FALSE;
721 }
722
723 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
724 {
725         MimeInfo *encinfo = NULL;
726
727         g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
728                 find_encrypted_func, &encinfo);
729         
730         return encinfo;
731 }
732
733 gint messageview_show(MessageView *messageview, MsgInfo *msginfo,
734                       gboolean all_headers)
735 {
736         gchar *file;
737         MimeInfo *mimeinfo, *encinfo;
738
739         g_return_val_if_fail(msginfo != NULL, -1);
740
741         mimeinfo = procmime_scan_message(msginfo);
742         if (!mimeinfo) {
743                 textview_show_error(messageview->mimeview->textview);
744                 return -1;
745         }
746
747         while ((encinfo = find_encrypted_part(mimeinfo)) != NULL) {
748                 debug_print("decrypting message part\n");
749                 if (privacy_mimeinfo_decrypt(encinfo) < 0)
750                         break;
751         }
752         
753         file = procmsg_get_message_file_path(msginfo);
754         if (!file) {
755                 g_warning("can't get message file path.\n");
756                 procmime_mimeinfo_free_all(mimeinfo);
757                 textview_show_error(messageview->mimeview->textview);
758                 return -1;
759         }
760
761         if (messageview->msginfo != msginfo) {
762                 procmsg_msginfo_free(messageview->msginfo);
763                 messageview->msginfo = procmsg_msginfo_get_full_info(msginfo);
764                 if (!messageview->msginfo)
765                         messageview->msginfo = procmsg_msginfo_copy(msginfo);
766         }
767         headerview_show(messageview->headerview, messageview->msginfo);
768
769         textview_set_all_headers(messageview->mimeview->textview, 
770                         messageview->all_headers);
771
772         mimeview_show_message(messageview->mimeview, mimeinfo, file);
773         messageview_set_position(messageview, 0);
774
775         if (messageview->msginfo->partial_recv)
776                 partial_recv_show(messageview->noticeview, 
777                                   messageview->msginfo);
778         else if ((messageview->msginfo->dispositionnotificationto || 
779              messageview->msginfo->returnreceiptto) &&
780             !MSG_IS_RETRCPT_SENT(messageview->msginfo->flags))
781                 return_receipt_show(messageview->noticeview, 
782                                     messageview->msginfo);
783         else 
784                 noticeview_hide(messageview->noticeview);
785
786         mimeinfo = procmime_mimeinfo_next(mimeinfo);
787         if (mimeinfo && (mimeinfo->type != MIMETYPE_TEXT || 
788             (strcmp(mimeinfo->subtype, "plain") &&
789              strcmp(mimeinfo->subtype, "html")))) {
790                 mimeview_show_part(messageview->mimeview,mimeinfo);
791         }
792
793         g_free(file);
794
795         return 0;
796 }
797
798 void messageview_reflect_prefs_pixmap_theme(void)
799 {
800         GList *cur;
801         MessageView *msgview;
802
803         for (cur = msgview_list; cur != NULL; cur = cur->next) {
804                 msgview = (MessageView*)cur->data;
805                 toolbar_update(TOOLBAR_MSGVIEW, msgview);
806                 mimeview_update(msgview->mimeview);
807         }
808 }
809
810 void messageview_clear(MessageView *messageview)
811 {
812         procmsg_msginfo_free(messageview->msginfo);
813         messageview->msginfo = NULL;
814         messageview->filtered = FALSE;
815         mimeview_clear(messageview->mimeview);
816         headerview_clear(messageview->headerview);
817         noticeview_hide(messageview->noticeview);
818 }
819
820 void messageview_destroy(MessageView *messageview)
821 {
822         debug_print("destroy messageview\n");
823         messageview_list = g_list_remove(messageview_list, messageview);
824
825         hooks_unregister_hook(MSGINFO_UPDATE_HOOKLIST,
826                               messageview->msginfo_update_callback_id);
827
828         headerview_destroy(messageview->headerview);
829         mimeview_destroy(messageview->mimeview);
830         noticeview_destroy(messageview->noticeview);
831
832         procmsg_msginfo_free(messageview->msginfo);
833         toolbar_clear_list(TOOLBAR_MSGVIEW);
834         if (messageview->toolbar) {
835                 toolbar_destroy(messageview->toolbar);
836                 g_free(messageview->toolbar);
837         }
838         
839         msgview_list = g_list_remove(msgview_list, messageview); 
840
841         if (messageview->window)
842                 gtk_widget_destroy(messageview->window);
843         g_free(messageview);
844 }
845
846 void messageview_delete(MessageView *msgview)
847 {
848         MsgInfo *msginfo = NULL;
849         FolderItem *trash = NULL;
850         PrefsAccount *ac = NULL;
851
852         if (msgview->msginfo && msgview->mainwin && msgview->mainwin->summaryview)
853                 msginfo = summary_get_selected_msg(msgview->mainwin->summaryview);
854         
855         /* need a procmsg_msginfo_equal() */
856         if (msginfo && msgview->msginfo && 
857             msginfo->msgnum == msgview->msginfo->msgnum && 
858             msginfo->folder == msgview->msginfo->folder) {
859                 summary_delete_trash(msgview->mainwin->summaryview);
860         } else {                
861                 msginfo = msgview->msginfo;
862
863                 g_return_if_fail(msginfo != NULL);
864
865                 /* to get the trash folder, we have to choose either
866                  * the folder's or account's trash default - we prefer
867                  * the one in the account prefs */
868                 if (msginfo->folder) {
869                         if (NULL != (ac = account_find_from_item(msginfo->folder)))
870                                 trash = account_get_special_folder(ac, F_TRASH);
871                         if (!trash && msginfo->folder->folder)  
872                                 trash = msginfo->folder->folder->trash;
873                         /* if still not found, use the default */
874                         if (!trash) 
875                                 trash = folder_get_default_trash();
876                 }       
877
878                 g_return_if_fail(trash != NULL);
879
880                 if (prefs_common.immediate_exec)
881                         /* TODO: Delete from trash */
882                         folder_item_move_msg(trash, msginfo);
883                 else {
884                         procmsg_msginfo_set_to_folder(msginfo, trash);
885                         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
886                         /* NOTE: does not update to next message in summaryview */
887                 }
888         }               
889 }
890
891 /* 
892  * \brief update messageview with currently selected message in summaryview
893  *        leave unchanged if summaryview is empty
894  * \param pointer to MessageView
895  */     
896 static void messageview_update(MessageView *msgview)
897 {
898         SummaryView *summaryview = (SummaryView*)msgview->mainwin->summaryview;
899
900         g_return_if_fail(summaryview != NULL);
901         
902         if (summaryview->selected) {
903                 GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
904                 MsgInfo *msginfo = gtk_ctree_node_get_row_data(ctree, 
905                                                       summaryview->selected);
906                 g_return_if_fail(msginfo != NULL);
907
908                 messageview_show(msgview, msginfo, 
909                                  msgview->all_headers);
910         } 
911 }
912
913 void messageview_quote_color_set(void)
914 {
915 }
916
917 void messageview_set_font(MessageView *messageview)
918 {
919 }
920
921 TextView *messageview_get_current_textview(MessageView *messageview)
922 {
923         TextView *text = NULL;
924
925         text = messageview->mimeview->textview;
926
927         return text;
928 }
929
930 MimeInfo *messageview_get_selected_mime_part(MessageView *messageview)
931 {
932         return mimeview_get_selected_part(messageview->mimeview);
933 }
934
935 void messageview_copy_clipboard(MessageView *messageview)
936 {
937         TextView *text;
938
939         text = messageview_get_current_textview(messageview);
940         if (text) {
941                 GtkTextView *textview = GTK_TEXT_VIEW(text->text);
942                 GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
943                 GtkClipboard *clipboard
944                         = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
945
946                 gtk_text_buffer_copy_clipboard(buffer, clipboard);
947         }
948 }
949
950 void messageview_select_all(MessageView *messageview)
951 {
952         TextView *text;
953
954         text = messageview_get_current_textview(messageview);
955         if (text) {
956                 GtkTextView *textview = GTK_TEXT_VIEW(text->text);
957                 GtkTextBuffer *buffer;
958                 GtkTextIter start, end;
959
960                 buffer = gtk_text_view_get_buffer(textview);
961                 gtk_text_buffer_get_bounds(buffer, &start, &end);
962                 gtk_text_buffer_select_range(buffer, &start, &end);
963         }
964 }
965
966 void messageview_set_position(MessageView *messageview, gint pos)
967 {
968         TextView *text;
969
970         text = messageview_get_current_textview(messageview);
971         if (text)
972                 textview_set_position(text, pos);
973 }
974
975 gboolean messageview_search_string(MessageView *messageview, const gchar *str,
976                                    gboolean case_sens)
977 {
978         TextView *text;
979
980         text = messageview_get_current_textview(messageview);
981         if (text)
982                 return textview_search_string(text, str, case_sens);
983         return FALSE;
984 }
985
986 gboolean messageview_search_string_backward(MessageView *messageview,
987                                             const gchar *str,
988                                             gboolean case_sens)
989 {
990         TextView *text;
991
992         text = messageview_get_current_textview(messageview);
993         if (text)       
994                 return textview_search_string_backward(text,
995                                                        str, case_sens);
996         return FALSE;
997 }
998
999 gboolean messageview_is_visible(MessageView *messageview)
1000 {
1001         return messageview->visible;
1002 }
1003
1004 void messageview_save_as(MessageView *messageview)
1005 {
1006         gchar *filename = NULL;
1007         MsgInfo *msginfo;
1008         gchar *src, *dest, *tmp;
1009
1010         if (!messageview->msginfo) return;
1011         msginfo = messageview->msginfo;
1012
1013         if (msginfo->subject) {
1014                 Xstrdup_a(filename, msginfo->subject, return);
1015                 subst_for_filename(filename);
1016         }
1017         dest = filesel_select_file_save(_("Save as"), filename);
1018         if (!dest) return;
1019         if (is_file_exist(dest)) {
1020                 AlertValue aval;
1021
1022                 aval = alertpanel(_("Overwrite"),
1023                                   _("Overwrite existing file?"),
1024                                   GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL);
1025                 if (G_ALERTDEFAULT != aval) return;
1026         }
1027
1028         src = procmsg_get_message_file(msginfo);
1029         if (copy_file(src, dest, TRUE) < 0) {
1030                 tmp =  g_path_get_basename(dest);
1031                 alertpanel_error(_("Can't save the file `%s'."), tmp);
1032                 g_free(tmp);
1033         }
1034         g_free(dest);
1035         g_free(src);
1036 }
1037
1038 static gint messageview_delete_cb(GtkWidget *widget, GdkEventAny *event,
1039                                   MessageView *messageview)
1040 {
1041         messageview_destroy(messageview);
1042         return TRUE;
1043 }
1044
1045 static void messageview_size_allocate_cb(GtkWidget *widget,
1046                                          GtkAllocation *allocation)
1047 {
1048         g_return_if_fail(allocation != NULL);
1049
1050         prefs_common.msgwin_width  = allocation->width;
1051         prefs_common.msgwin_height = allocation->height;
1052 }
1053
1054 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
1055                         MessageView *messageview)
1056 {
1057         if (event && event->keyval == GDK_Escape && messageview->window) {
1058                 messageview_destroy(messageview);
1059                 return TRUE;
1060         }
1061         return FALSE;
1062 }
1063
1064 void messageview_toggle_view_real(MessageView *messageview)
1065 {
1066         MainWindow *mainwin = messageview->mainwin;
1067         union CompositeWin *cwin = &mainwin->win;
1068         GtkWidget *vpaned = NULL;
1069         GtkWidget *container = NULL;
1070         GtkItemFactory *ifactory = gtk_item_factory_from_widget(mainwin->menubar);
1071         
1072         switch (mainwin->type) {
1073         case SEPARATE_NONE:
1074                 vpaned = cwin->sep_none.vpaned;
1075                 container = cwin->sep_none.hpaned;
1076                 break;
1077         case SEPARATE_FOLDER:
1078                 vpaned = cwin->sep_folder.vpaned;
1079                 container = mainwin->vbox_body;
1080                 break;
1081         case SEPARATE_MESSAGE:
1082         case SEPARATE_BOTH:
1083                 return;
1084         }
1085
1086         if (vpaned->parent != NULL) {
1087                 gtk_widget_ref(vpaned);
1088                 gtkut_container_remove(GTK_CONTAINER(container), vpaned);
1089                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), container);
1090                 menu_set_sensitive(ifactory, "/View/Expand Summary View", FALSE);
1091         } else {
1092                 gtk_widget_reparent(GTK_WIDGET_PTR(messageview), vpaned);
1093                 gtk_container_add(GTK_CONTAINER(container), vpaned);
1094                 gtk_widget_unref(vpaned);
1095                 menu_set_sensitive(ifactory, "/View/Expand Summary View", TRUE);
1096                 gtk_widget_grab_focus(GTK_WIDGET(mainwin->summaryview->ctree));
1097         }
1098 }
1099
1100 static void return_receipt_show(NoticeView *noticeview, MsgInfo *msginfo)
1101 {
1102         noticeview_set_text(noticeview, _("This message asks for a return receipt."));
1103         noticeview_set_button_text(noticeview, _("Send receipt"));
1104         noticeview_set_button_press_callback(noticeview,
1105                                              G_CALLBACK(return_receipt_send_clicked),
1106                                              (gpointer) msginfo);
1107         noticeview_show(noticeview);
1108 }
1109
1110 static void return_receipt_send_clicked(NoticeView *noticeview, MsgInfo *msginfo)
1111 {
1112         MsgInfo *tmpmsginfo;
1113         gchar *file;
1114
1115         file = procmsg_get_message_file_path(msginfo);
1116         if (!file) {
1117                 g_warning("can't get message file path.\n");
1118                 return;
1119         }
1120
1121         tmpmsginfo = procheader_parse_file(file, msginfo->flags, TRUE, TRUE);
1122         tmpmsginfo->folder = msginfo->folder;
1123         tmpmsginfo->msgnum = msginfo->msgnum;
1124
1125         if (disposition_notification_send(tmpmsginfo) >= 0) {
1126                 procmsg_msginfo_set_flags(msginfo, MSG_RETRCPT_SENT, 0);
1127                 noticeview_hide(noticeview);
1128         }               
1129
1130         procmsg_msginfo_free(tmpmsginfo);
1131         g_free(file);
1132 }
1133
1134 static void partial_recv_show(NoticeView *noticeview, MsgInfo *msginfo)
1135 {
1136         gchar *text = NULL;
1137         gchar *button1 = NULL;
1138         gchar *button2 = NULL;
1139         void  *button1_cb = NULL;
1140         void  *button2_cb = NULL;
1141
1142         if (!partial_msg_in_uidl_list(msginfo)) {
1143                 text = g_strdup_printf(_("This message has been partially "
1144                                 "retrieved,\nand has been deleted from the "
1145                                 "server."));
1146         } else {
1147                 switch (msginfo->planned_download) {
1148                 case POP3_PARTIAL_DLOAD_UNKN:
1149                         text = g_strdup_printf(_("This message has been "
1150                                         "partially retrieved;\nit is %s."),
1151                                         to_human_readable(
1152                                                 (off_t)(msginfo->total_size)));
1153                         button1 = _("Mark for download");
1154                         button2 = _("Mark for deletion");
1155                         button1_cb = partial_recv_dload_clicked;
1156                         button2_cb = partial_recv_del_clicked;
1157                         break;
1158                 case POP3_PARTIAL_DLOAD_DLOAD:
1159                         text = g_strdup_printf(_("This message has been "
1160                                         "partially retrieved;\nit is %s and "
1161                                         "will be downloaded."),
1162                                         to_human_readable(
1163                                                 (off_t)(msginfo->total_size)));
1164                         button1 = _("Unmark");
1165                         button1_cb = partial_recv_unmark_clicked;
1166                         button2 = _("Mark for deletion");
1167                         button2_cb = partial_recv_del_clicked;
1168                         break;
1169                 case POP3_PARTIAL_DLOAD_DELE:
1170                         text = g_strdup_printf(_("This message has been "
1171                                         "partially retrieved;\nit is %s and "
1172                                         "will be deleted."),
1173                                         to_human_readable(
1174                                                 (off_t)(msginfo->total_size)));
1175                         button1 = _("Mark for download");
1176                         button1_cb = partial_recv_dload_clicked;
1177                         button2 = _("Unmark");
1178                         button2_cb = partial_recv_unmark_clicked;
1179                         break;
1180                 default:
1181                         return;
1182                 }
1183         }
1184         
1185         noticeview_set_text(noticeview, text);
1186         g_free(text);
1187         noticeview_set_button_text(noticeview, button1);
1188         noticeview_set_button_press_callback(noticeview,
1189                      G_CALLBACK(button1_cb), (gpointer) msginfo);
1190
1191         noticeview_set_2ndbutton_text(noticeview, button2);
1192         noticeview_set_2ndbutton_press_callback(noticeview,
1193                      G_CALLBACK(button2_cb), (gpointer) msginfo);
1194
1195         noticeview_show(noticeview);
1196 }
1197
1198 static void partial_recv_dload_clicked(NoticeView *noticeview, 
1199                                        MsgInfo *msginfo)
1200 {
1201         if (partial_mark_for_download(msginfo) == 0) {
1202                 partial_recv_show(noticeview, msginfo);
1203         }
1204 }
1205
1206 static void partial_recv_del_clicked(NoticeView *noticeview, 
1207                                        MsgInfo *msginfo)
1208 {
1209         if (partial_mark_for_delete(msginfo) == 0) {
1210                 partial_recv_show(noticeview, msginfo);
1211         }
1212 }
1213
1214 static void partial_recv_unmark_clicked(NoticeView *noticeview, 
1215                                        MsgInfo *msginfo)
1216 {
1217         if (partial_unmark(msginfo) == 0) {
1218                 partial_recv_show(noticeview, msginfo);
1219         }
1220 }
1221
1222 static void select_account_cb(GtkWidget *w, gpointer data)
1223 {
1224         *(gint*)data = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w), MENU_VAL_ID));
1225 }
1226         
1227 static PrefsAccount *select_account_from_list(GList *ac_list)
1228 {
1229         GtkWidget *optmenu;
1230         GtkWidget *menu;
1231         gint account_id;
1232
1233         g_return_val_if_fail(ac_list != NULL, NULL);
1234         g_return_val_if_fail(ac_list->data != NULL, NULL);
1235         
1236         optmenu = gtk_option_menu_new();
1237         menu = gtkut_account_menu_new(ac_list, 
1238                         G_CALLBACK(select_account_cb), 
1239                         &account_id);
1240         if (!menu)
1241                 return NULL;
1242         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), menu);
1243         gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), 0);
1244         account_id = ((PrefsAccount *) ac_list->data)->account_id;
1245         if (alertpanel_with_widget(
1246                                 _("Return Receipt Notification"),
1247                                 _("The message was sent to several of your "
1248                                   "accounts.\n"
1249                                   "Please choose which account do you want to "
1250                                   "use for sending the receipt notification:"),
1251                                 _("Send Notification"), _("+Cancel"), NULL,
1252                                 optmenu) != G_ALERTDEFAULT)
1253                 return NULL;
1254         return account_find_from_id(account_id);
1255 }
1256
1257 /* 
1258  * \brief return selected messageview text, when nothing is 
1259  *        selected and message was filtered, return complete text
1260  *
1261  * \param  pointer to Messageview 
1262  *
1263  * \return pointer to text (needs to be free'd by calling func)
1264  */
1265 gchar *messageview_get_selection(MessageView *msgview)
1266 {
1267         TextView *textview;
1268         gchar *text = NULL;
1269         GtkTextView *edit = NULL;
1270         GtkTextBuffer *textbuf;
1271         gint body_pos = 0;
1272         
1273         g_return_val_if_fail(msgview != NULL, NULL);
1274
1275         textview = messageview_get_current_textview(msgview);
1276         g_return_val_if_fail(textview != NULL, NULL);
1277
1278         edit = GTK_TEXT_VIEW(textview->text);
1279         g_return_val_if_fail(edit != NULL, NULL);
1280         body_pos = textview->body_pos;
1281
1282         textbuf = gtk_text_view_get_buffer(edit);
1283
1284         if (gtk_text_buffer_get_selection_bounds(textbuf, NULL, NULL))
1285                 return gtkut_text_view_get_selection(edit);
1286         else if (msgview->filtered) {
1287                 GtkTextIter start_iter, end_iter;
1288                 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter, body_pos);
1289                 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
1290                 gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter, FALSE);
1291         } else
1292                 text = NULL;
1293
1294         return text;
1295 }
1296
1297 static void save_as_cb(gpointer data, guint action, GtkWidget *widget)
1298 {
1299         MessageView *messageview = (MessageView *)data;
1300         messageview_save_as(messageview);
1301 }
1302
1303 static void print_cb(gpointer data, guint action, GtkWidget *widget)
1304 {
1305         MessageView *messageview = (MessageView *)data;
1306         gchar *cmdline;
1307         gchar *p;
1308
1309         if (!messageview->msginfo) return;
1310
1311         cmdline = input_dialog(_("Print"),
1312                                _("Enter the print command line:\n"
1313                                  "(`%s' will be replaced with file name)"),
1314                                prefs_common.print_cmd);
1315         if (!cmdline) return;
1316         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
1317             strchr(p + 2, '%')) {
1318                 alertpanel_error(_("Print command line is invalid:\n`%s'"),
1319                                  cmdline);
1320                 g_free(cmdline);
1321                 return;
1322         }
1323
1324         procmsg_print_message(messageview->msginfo, cmdline);
1325         g_free(cmdline);
1326 }
1327
1328 static void close_cb(gpointer data, guint action, GtkWidget *widget)
1329 {
1330         MessageView *messageview = (MessageView *)data;
1331         messageview_destroy(messageview);
1332 }
1333
1334 static void copy_cb(gpointer data, guint action, GtkWidget *widget)
1335 {
1336         MessageView *messageview = (MessageView *)data;
1337         messageview_copy_clipboard(messageview);
1338 }
1339
1340 static void allsel_cb(gpointer data, guint action, GtkWidget *widget)
1341 {
1342         MessageView *messageview = (MessageView *)data;
1343         messageview_select_all(messageview);
1344 }
1345
1346 static void search_cb(gpointer data, guint action, GtkWidget *widget)
1347 {
1348         MessageView *messageview = (MessageView *)data;
1349         message_search(messageview);
1350 }
1351
1352 static void set_charset_cb(gpointer data, guint action, GtkWidget *widget)
1353 {
1354         MessageView *messageview = (MessageView *)data;
1355         const gchar *charset;
1356
1357         if (GTK_CHECK_MENU_ITEM(widget)->active) {
1358                 charset = conv_get_charset_str((CharSet)action);
1359                 g_free(messageview->forced_charset);
1360                 messageview->forced_charset = g_strdup(charset);
1361                 procmime_force_charset(charset);
1362                 
1363                 messageview_show(messageview, messageview->msginfo, FALSE);
1364         }
1365 }
1366
1367 static void set_decode_cb(gpointer data, guint action, GtkWidget *widget)
1368 {
1369         MessageView *messageview = (MessageView *)data;
1370         if (GTK_CHECK_MENU_ITEM(widget)->active) {
1371                 messageview->forced_encoding = (EncodingType)action;
1372
1373                 messageview_show(messageview, messageview->msginfo, FALSE);
1374                 
1375                 debug_print("forced encoding: %d\n", action);
1376         }
1377 }
1378
1379
1380 static void view_source_cb(gpointer data, guint action, GtkWidget *widget)
1381 {
1382         MessageView *messageview = (MessageView *)data;
1383         SourceWindow *srcwin;
1384
1385         if (!messageview->msginfo) return;
1386
1387         srcwin = source_window_create();
1388         source_window_show_msg(srcwin, messageview->msginfo);
1389         source_window_show(srcwin);
1390 }
1391
1392 static void show_all_header_cb(gpointer data, guint action, GtkWidget *widget)
1393 {
1394         MessageView *messageview = (MessageView *)data;
1395         MsgInfo *msginfo = messageview->msginfo;
1396
1397         messageview->all_headers = 
1398                         GTK_CHECK_MENU_ITEM(widget)->active;
1399         if (!msginfo) return;
1400         messageview->msginfo = NULL;
1401         messageview_show(messageview, msginfo,
1402                          GTK_CHECK_MENU_ITEM(widget)->active);
1403         procmsg_msginfo_free(msginfo);
1404         main_window_set_menu_sensitive(messageview->mainwin);
1405 }
1406
1407 static void compose_cb(gpointer data, guint action, GtkWidget *widget)
1408 {
1409         MessageView *messageview = (MessageView *)data;
1410         PrefsAccount *ac = NULL;
1411         FolderItem *item = NULL;
1412
1413         if (messageview->msginfo)
1414                 item = messageview->msginfo->folder;
1415
1416         if (item) {
1417                 ac = account_find_from_item(item);
1418                 if (ac && ac->protocol == A_NNTP &&
1419                     item->stype == F_NEWS) {
1420                         compose_new(ac, item->path, NULL);
1421                         return;
1422                 }
1423         }
1424
1425         compose_new(ac, NULL, NULL);
1426 }
1427
1428 static void reply_cb(gpointer data, guint action, GtkWidget *widget)
1429 {
1430         MessageView *messageview = (MessageView *)data;
1431         GSList *msginfo_list = NULL;
1432
1433         g_return_if_fail(messageview->msginfo);
1434
1435         msginfo_list = g_slist_append(msginfo_list, messageview->msginfo);
1436         compose_reply_from_messageview(messageview, msginfo_list, action);
1437         g_slist_free(msginfo_list);
1438 }
1439
1440 static void reedit_cb(gpointer data, guint action, GtkWidget *widget)
1441 {
1442         MessageView *messageview = (MessageView *)data;
1443         MsgInfo *msginfo;
1444
1445         if (!messageview->msginfo) return;
1446         msginfo = messageview->msginfo;
1447         if (!msginfo->folder) return;
1448         if (msginfo->folder->stype != F_OUTBOX &&
1449             msginfo->folder->stype != F_DRAFT &&
1450             msginfo->folder->stype != F_QUEUE) return;
1451
1452         compose_reedit(msginfo);
1453 }
1454
1455 static void addressbook_open_cb(gpointer data, guint action, GtkWidget *widget)
1456 {
1457         addressbook_open(NULL);
1458 }
1459
1460 static void add_address_cb(gpointer data, guint action, GtkWidget *widget)
1461 {
1462         MessageView *messageview = (MessageView *)data;
1463         MsgInfo *msginfo;
1464         gchar *from;
1465
1466         if (!messageview->msginfo) return;
1467         msginfo = messageview->msginfo;
1468         Xstrdup_a(from, msginfo->from, return);
1469         eliminate_address_comment(from);
1470         extract_address(from);
1471         addressbook_add_contact(msginfo->fromname, from, NULL);
1472 }
1473
1474 static void create_filter_cb(gpointer data, guint action, GtkWidget *widget)
1475 {
1476         MessageView *messageview = (MessageView *)data;
1477         FolderItem * item;
1478         
1479         if (!messageview->msginfo) return;
1480         
1481         item = messageview->msginfo->folder;
1482         summary_msginfo_filter_open(item,  messageview->msginfo,
1483                                     (PrefsFilterType)action, 0);
1484 }
1485
1486 static void create_processing_cb(gpointer data, guint action,
1487                                  GtkWidget *widget)
1488 {
1489         MessageView *messageview = (MessageView *)data;
1490         FolderItem * item;
1491         
1492         if (!messageview->msginfo) return;
1493         
1494         item = messageview->msginfo->folder;
1495         summary_msginfo_filter_open(item,  messageview->msginfo,
1496                                     (PrefsFilterType)action, 1);
1497 }
1498
1499 static void about_cb(gpointer data, guint action, GtkWidget *widget)
1500 {
1501         about_show();
1502 }
1503
1504 static gboolean messageview_update_msg(gpointer source, gpointer data)
1505 {
1506         MsgInfoUpdate *msginfo_update = (MsgInfoUpdate *) source;
1507         MessageView *messageview = (MessageView *)data;
1508
1509         if (messageview->msginfo != msginfo_update->msginfo)
1510                 return FALSE;
1511
1512         if (msginfo_update->flags & MSGINFO_UPDATE_DELETED) {
1513                 messageview_clear(messageview);
1514                 messageview_update(messageview);
1515         }
1516
1517         return FALSE;
1518 }
1519
1520 void messageview_set_menu_sensitive(MessageView *messageview)
1521 {
1522         GtkItemFactory *ifactory;
1523         GtkWidget *menuitem;
1524
1525         if (!messageview || !messageview->new_window) 
1526                 return;
1527         /* do some smart things */
1528         if (!messageview->menubar) return;
1529         ifactory = gtk_item_factory_from_widget(messageview->menubar);
1530         if (!ifactory) return;
1531         if (messageview->mainwin->type == SEPARATE_MESSAGE) {
1532                 menuitem = gtk_item_factory_get_widget(ifactory, "/View/Show all headers");
1533                 gtk_check_menu_item_set_active
1534                         (GTK_CHECK_MENU_ITEM(menuitem),
1535                          messageview->mimeview->textview->show_all_headers);
1536         }
1537 }