2007-08-09 [colin] 2.10.0cvs107
[claws.git] / src / messageview.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
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 "mainwindow.h"
50 #include "manage_window.h"
51 #include "procmsg.h"
52 #include "procheader.h"
53 #include "procmime.h"
54 #include "account.h"
55 #include "action.h"
56 #include "prefs_common.h"
57 #include "prefs_account.h"
58 #include "gtkutils.h"
59 #include "utils.h"
60 #include "send_message.h"
61 #include "stock_pixmap.h"
62 #include "hooks.h"
63 #include "filtering.h"
64 #include "partial_download.h"
65 #include "gedit-print.h"
66 #include "uri_opener.h"
67 #include "inc.h"
68 #include "log.h"
69
70 static GList *messageview_list = NULL;
71
72 static gint messageview_delete_cb       (GtkWidget              *widget,
73                                          GdkEventAny            *event,
74                                          MessageView            *messageview);
75 static void messageview_size_allocate_cb(GtkWidget      *widget,
76                                          GtkAllocation  *allocation);
77 static gboolean key_pressed             (GtkWidget      *widget,
78                                          GdkEventKey    *event,
79                                          MessageView    *messageview);
80
81 static void return_receipt_show         (NoticeView     *noticeview, 
82                                          MsgInfo        *msginfo);      
83 static void return_receipt_send_clicked (NoticeView     *noticeview, 
84                                          MsgInfo        *msginfo);
85 static void partial_recv_show           (NoticeView     *noticeview, 
86                                          MsgInfo        *msginfo);      
87 static void partial_recv_dload_clicked  (NoticeView     *noticeview, 
88                                          MsgInfo        *msginfo);
89 static void partial_recv_del_clicked    (NoticeView     *noticeview, 
90                                          MsgInfo        *msginfo);
91 static void partial_recv_unmark_clicked (NoticeView     *noticeview, 
92                                          MsgInfo        *msginfo);
93 static void save_as_cb                  (gpointer        data,
94                                          guint           action,
95                                          GtkWidget      *widget);
96 static void print_cb                    (gpointer        data,
97                                          guint           action,
98                                          GtkWidget      *widget);
99 static void close_cb                    (gpointer        data,
100                                          guint           action,
101                                          GtkWidget      *widget);
102 static void copy_cb                     (gpointer        data,
103                                          guint           action,
104                                          GtkWidget      *widget);
105 static void allsel_cb                   (gpointer        data,
106                                          guint           action,
107                                          GtkWidget      *widget);
108 static void search_cb                   (gpointer        data,
109                                          guint           action,
110                                          GtkWidget      *widget);
111
112 static void set_charset_cb              (gpointer        data,
113                                          guint           action,
114                                          GtkWidget      *widget);
115 static void set_decode_cb               (gpointer        data,
116                                          guint           action,
117                                          GtkWidget      *widget);
118 static void view_source_cb              (gpointer        data,
119                                          guint           action,
120                                          GtkWidget      *widget);
121 static void show_all_header_cb          (gpointer        data,
122                                          guint           action,
123                                          GtkWidget      *widget);
124 static void msg_hide_quotes_cb          (gpointer        data,
125                                          guint           action,
126                                          GtkWidget      *widget);
127
128 static void compose_cb                  (gpointer        data,
129                                          guint           action,
130                                          GtkWidget      *widget);
131 static void reply_cb                    (gpointer        data,
132                                          guint           action,
133                                          GtkWidget      *widget);
134
135 static PrefsAccount *select_account_from_list
136                                         (GList          *ac_list);
137 static void addressbook_open_cb         (gpointer        data,
138                                          guint           action,
139                                          GtkWidget      *widget);
140 static void add_address_cb              (gpointer        data,
141                                          guint           action,
142                                          GtkWidget      *widget);
143 static void create_filter_cb            (gpointer        data,
144                                          guint           action,
145                                          GtkWidget      *widget);
146 static void create_processing_cb        (gpointer        data,
147                                          guint           action,
148                                          GtkWidget      *widget);
149 static void open_urls_cb                (gpointer        data,
150                                          guint           action,
151                                          GtkWidget      *widget);
152
153 static void about_cb                    (gpointer        data,
154                                          guint           action,
155                                          GtkWidget      *widget);
156 static void messageview_update          (MessageView    *msgview,
157                                          MsgInfo        *old_msginfo);
158 static gboolean messageview_update_msg  (gpointer source, gpointer data);
159
160 static GList *msgview_list = NULL;
161 static GtkItemFactoryEntry msgview_entries[] =
162 {
163         {N_("/_File"),                  NULL, NULL, 0, "<Branch>"},
164         {N_("/_File/_Save as..."),      "<control>S", save_as_cb, 0, NULL},
165         {N_("/_File/_Print..."),        "<control>P", print_cb, 0, NULL},
166         {N_("/_File/---"),              NULL, NULL, 0, "<Separator>"},
167         {N_("/_File/_Close"),           "<control>W", close_cb, 0, NULL},
168
169         {N_("/_Edit"),                  NULL, NULL, 0, "<Branch>"},
170         {N_("/_Edit/_Copy"),            "<control>C", copy_cb, 0, NULL},
171         {N_("/_Edit/Select _all"),      "<control>A", allsel_cb, 0, NULL},
172         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
173         {N_("/_Edit/_Find in current message..."),
174                                         "<control>F", search_cb, 0, NULL},
175
176         {N_("/_View"),                  NULL, NULL, 0, "<Branch>"},
177
178 #define ENC_SEPARATOR \
179         {N_("/_View/Character _encoding/---"),  NULL, NULL, 0, "<Separator>"}
180 #define ENC_ACTION(action) \
181         NULL, set_charset_cb, action, "/View/Character encoding/Auto detect"
182
183         {N_("/_View/Character _encoding"),      NULL, NULL, 0, "<Branch>"},
184         {N_("/_View/Character _encoding/_Auto detect"),
185                                         NULL, set_charset_cb, C_AUTO, "<RadioItem>"},
186         ENC_SEPARATOR,
187         {N_("/_View/Character _encoding/7bit ascii (US-ASC_II)"),
188          ENC_ACTION(C_US_ASCII)},
189
190         {N_("/_View/Character _encoding/Unicode (_UTF-8)"),
191          ENC_ACTION(C_UTF_8)},
192         ENC_SEPARATOR,
193         {N_("/_View/Character _encoding/Western European (ISO-8859-_1)"),
194          ENC_ACTION(C_ISO_8859_1)},
195         {N_("/_View/Character _encoding/Western European (ISO-8859-15)"),
196          ENC_ACTION(C_ISO_8859_15)},
197         {N_("/_View/Character _encoding/Western European (Windows-1252)"),
198          ENC_ACTION(C_WINDOWS_1252)},
199         ENC_SEPARATOR,
200         {N_("/_View/Character _encoding/Central European (ISO-8859-_2)"),
201          ENC_ACTION(C_ISO_8859_2)},
202         ENC_SEPARATOR,
203         {N_("/_View/Character _encoding/_Baltic (ISO-8859-13)"),
204          ENC_ACTION(C_ISO_8859_13)},
205         {N_("/_View/Character _encoding/Baltic (ISO-8859-_4)"),
206          ENC_ACTION(C_ISO_8859_4)},
207         ENC_SEPARATOR,
208         {N_("/_View/Character _encoding/Greek (ISO-8859-_7)"),
209          ENC_ACTION(C_ISO_8859_7)},
210         ENC_SEPARATOR,
211         {N_("/_View/Character _encoding/Hebrew (ISO-8859-_8)"),
212          ENC_ACTION(C_ISO_8859_8)},
213         {N_("/_View/Character _encoding/Hebrew (Windows-1255)"),
214          ENC_ACTION(C_CP1255)},
215         ENC_SEPARATOR,
216         {N_("/_View/Character _encoding/Arabic (ISO-8859-_6)"),
217          ENC_ACTION(C_ISO_8859_6)},
218         {N_("/_View/Character _encoding/Arabic (Windows-1256)"),
219          ENC_ACTION(C_CP1256)},
220         ENC_SEPARATOR,
221         {N_("/_View/Character _encoding/Turkish (ISO-8859-_9)"),
222          ENC_ACTION(C_ISO_8859_9)},
223         ENC_SEPARATOR,
224         {N_("/_View/Character _encoding/Cyrillic (ISO-8859-_5)"),
225          ENC_ACTION(C_ISO_8859_5)},
226         {N_("/_View/Character _encoding/Cyrillic (KOI8-_R)"),
227          ENC_ACTION(C_KOI8_R)},
228         {N_("/_View/Character _encoding/Cyrillic (KOI8-U)"),
229          ENC_ACTION(C_KOI8_U)},
230         {N_("/_View/Character _encoding/Cyrillic (Windows-1251)"),
231          ENC_ACTION(C_CP1251)},
232         ENC_SEPARATOR,
233         {N_("/_View/Character _encoding/Japanese (ISO-2022-_JP)"),
234          ENC_ACTION(C_ISO_2022_JP)},
235         {N_("/_View/Character _encoding/Japanese (ISO-2022-JP-2)"),
236          ENC_ACTION(C_ISO_2022_JP_2)},
237         {N_("/_View/Character _encoding/Japanese (_EUC-JP)"),
238          ENC_ACTION(C_EUC_JP)},
239         {N_("/_View/Character _encoding/Japanese (_Shift__JIS)"),
240          ENC_ACTION(C_SHIFT_JIS)},
241         ENC_SEPARATOR,
242         {N_("/_View/Character _encoding/Simplified Chinese (_GB2312)"),
243          ENC_ACTION(C_GB2312)},
244         {N_("/_View/Character _encoding/Simplified Chinese (GBK)"),
245          ENC_ACTION(C_GBK)},
246         {N_("/_View/Character _encoding/Traditional Chinese (_Big5)"),
247          ENC_ACTION(C_BIG5)},
248         {N_("/_View/Character _encoding/Traditional Chinese (EUC-_TW)"),
249          ENC_ACTION(C_EUC_TW)},
250         {N_("/_View/Character _encoding/Chinese (ISO-2022-_CN)"),
251          ENC_ACTION(C_ISO_2022_CN)},
252         ENC_SEPARATOR,
253         {N_("/_View/Character _encoding/Korean (EUC-_KR)"),
254          ENC_ACTION(C_EUC_KR)},
255         {N_("/_View/Character _encoding/Korean (ISO-2022-KR)"),
256          ENC_ACTION(C_ISO_2022_KR)},
257         ENC_SEPARATOR,
258         {N_("/_View/Character _encoding/Thai (TIS-620)"),
259          ENC_ACTION(C_TIS_620)},
260         {N_("/_View/Character _encoding/Thai (Windows-874)"),
261          ENC_ACTION(C_WINDOWS_874)},
262
263 #undef ENC_SEPARATOR
264 #undef ENC_ACTION
265
266 #define DEC_SEPARATOR \
267         {N_("/_View/Decode/---"),               NULL, NULL, 0, "<Separator>"}
268 #define DEC_ACTION(action) \
269          NULL, set_decode_cb, action, "/View/Decode/Auto detect"
270         {N_("/_View/Decode"),           NULL, NULL, 0, "<Branch>"},
271         {N_("/_View/Decode/_Auto detect"),
272          NULL, set_decode_cb, 0, "<RadioItem>"},
273         {N_("/_View/Decode/---"),               NULL, NULL, 0, "<Separator>"},
274         {N_("/_View/Decode/_8bit"),             DEC_ACTION(ENC_8BIT)},
275         {N_("/_View/Decode/_Quoted printable"), DEC_ACTION(ENC_QUOTED_PRINTABLE)},
276         {N_("/_View/Decode/_Base64"),           DEC_ACTION(ENC_BASE64)},
277         {N_("/_View/Decode/_Uuencode"),         DEC_ACTION(ENC_X_UUENCODE)},
278
279 #undef DEC_SEPARATOR
280 #undef DEC_ACTION
281
282         {N_("/_View/---"),              NULL, NULL, 0, "<Separator>"},
283         {N_("/_View/Mess_age source"),  "<control>U", view_source_cb, 0, NULL},
284         {N_("/_View/Show all _headers"),"<control>H", show_all_header_cb, 0, "<ToggleItem>"},
285         {N_("/_View/Quotes"),                   NULL, NULL, 0, "<Branch>"},
286         {N_("/_View/Quotes/_Fold all"),         "<control><shift>Q", msg_hide_quotes_cb, 1, "<ToggleItem>"},
287         {N_("/_View/Quotes/Fold from level _2"),NULL, msg_hide_quotes_cb, 2, "<ToggleItem>"},
288         {N_("/_View/Quotes/Fold from level _3"),NULL, msg_hide_quotes_cb, 3, "<ToggleItem>"},
289
290         {N_("/_Message"),               NULL, NULL, 0, "<Branch>"},
291         {N_("/_Message/Compose _new message"),
292                                         "<control>M", compose_cb, 0, NULL},
293         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
294         {N_("/_Message/_Reply"),        "<control>R", reply_cb, COMPOSE_REPLY, NULL},
295         {N_("/_Message/Repl_y to/_all"),
296                                         "<control><shift>R", reply_cb, COMPOSE_REPLY_TO_ALL, NULL},
297         {N_("/_Message/Repl_y to/_sender"),
298                                         NULL, reply_cb, COMPOSE_REPLY_TO_SENDER, NULL},
299         {N_("/_Message/Repl_y to/mailing _list"),
300                                         "<control>L", reply_cb, COMPOSE_REPLY_TO_LIST, NULL},
301         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
302         {N_("/_Message/_Forward"),      "<control><alt>F", reply_cb, COMPOSE_FORWARD_INLINE, NULL},
303         {N_("/_Message/For_ward as attachment"),
304                                         NULL, reply_cb, COMPOSE_FORWARD_AS_ATTACH, NULL},
305         {N_("/_Message/Redirec_t"),     NULL, reply_cb, COMPOSE_REDIRECT, NULL},
306
307         {N_("/_Tools"),                 NULL, NULL, 0, "<Branch>"},
308         {N_("/_Tools/_Address book"),   "<control><shift>A", addressbook_open_cb, 0, NULL},
309         {N_("/_Tools/Add sender to address boo_k"),
310                                         NULL, add_address_cb, 0, NULL},
311         {N_("/_Tools/---"),             NULL, NULL, 0, "<Separator>"},
312         {N_("/_Tools/_Create filter rule"),
313                                         NULL, NULL, 0, "<Branch>"},
314         {N_("/_Tools/_Create filter rule/_Automatically"),
315                                         NULL, create_filter_cb, FILTER_BY_AUTO, NULL},
316         {N_("/_Tools/_Create filter rule/by _From"),
317                                         NULL, create_filter_cb, FILTER_BY_FROM, NULL},
318         {N_("/_Tools/_Create filter rule/by _To"),
319                                         NULL, create_filter_cb, FILTER_BY_TO, NULL},
320         {N_("/_Tools/_Create filter rule/by _Subject"),
321                                         NULL, create_filter_cb, FILTER_BY_SUBJECT, NULL},
322         {N_("/_Tools/Create processing rule"),
323                                         NULL, NULL, 0, "<Branch>"},
324         {N_("/_Tools/Create processing rule/_Automatically"),
325                                         NULL, create_processing_cb, FILTER_BY_AUTO, NULL},
326         {N_("/_Tools/Create processing rule/by _From"),
327                                         NULL, create_processing_cb, FILTER_BY_FROM, NULL},
328         {N_("/_Tools/Create processing rule/by _To"),
329                                         NULL, create_processing_cb, FILTER_BY_TO, NULL},
330         {N_("/_Tools/Create processing rule/by _Subject"),
331                                         NULL, create_processing_cb, FILTER_BY_SUBJECT, NULL},
332         {N_("/_Tools/---"),             NULL, NULL, 0, "<Separator>"},
333         {N_("/_Tools/List _URLs..."),   "<shift><control>U", open_urls_cb, 0, NULL},
334         {N_("/_Tools/---"),             NULL, NULL, 0, "<Separator>"},
335         {N_("/_Tools/Actio_ns"),        NULL, NULL, 0, "<Branch>"},
336
337         {N_("/_Help"),                  NULL, NULL, 0, "<Branch>"},
338         {N_("/_Help/_About"),           NULL, about_cb, 0, NULL}
339 };
340
341 MessageView *messageview_create(MainWindow *mainwin)
342 {
343         MessageView *messageview;
344         GtkWidget *vbox;
345         HeaderView *headerview;
346         MimeView *mimeview;
347         NoticeView *noticeview;
348
349         debug_print("Creating message view...\n");
350         messageview = g_new0(MessageView, 1);
351
352         headerview = headerview_create();
353
354         noticeview = noticeview_create(mainwin);
355
356         mimeview = mimeview_create(mainwin);
357         mimeview->textview = textview_create();
358         mimeview->textview->messageview = messageview;
359         mimeview->messageview = messageview;
360
361         vbox = gtk_vbox_new(FALSE, 0);
362         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(headerview),
363                            FALSE, FALSE, 0);
364         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET_PTR(noticeview),
365                            FALSE, FALSE, 0);
366         gtk_box_pack_start(GTK_BOX(vbox),
367                            GTK_WIDGET_PTR(mimeview), TRUE, TRUE, 0);
368         gtk_widget_show(vbox);
369
370         messageview->vbox        = vbox;
371         messageview->new_window  = FALSE;
372         messageview->window      = NULL;
373         messageview->headerview  = headerview;
374         messageview->mimeview    = mimeview;
375         messageview->noticeview = noticeview;
376         messageview->mainwin    = mainwin;
377
378         messageview->statusbar     = NULL;
379         messageview->statusbar_cid = 0;
380
381         messageview->msginfo_update_callback_id =
382                 hooks_register_hook(MSGINFO_UPDATE_HOOKLIST, messageview_update_msg, (gpointer) messageview);
383
384         return messageview;
385 }
386
387 GList *messageview_get_msgview_list(void)
388 {
389         return msgview_list;
390 }
391
392 void messageview_update_actions_menu(MessageView *msgview)
393 {
394         GtkItemFactory *ifactory;
395
396         /* Messages opened in a new window do not have a menu bar */
397         if (msgview->menubar == NULL)
398                 return;
399         ifactory = gtk_item_factory_from_widget(msgview->menubar);
400         action_update_msgview_menu(ifactory, "/Tools/Actions", msgview);
401 }
402
403 void messageview_add_toolbar(MessageView *msgview, GtkWidget *window) 
404 {
405         GtkWidget *handlebox;
406         GtkWidget *vbox;
407         GtkWidget *menubar;
408         GtkWidget *statusbar = NULL;
409         guint n_menu_entries;
410
411         vbox = gtk_vbox_new(FALSE, 0);
412         gtk_widget_show(vbox);
413         gtk_container_add(GTK_CONTAINER(window), vbox); 
414
415         n_menu_entries = sizeof(msgview_entries) / sizeof(msgview_entries[0]);
416         menubar = menubar_create(window, msgview_entries,
417                                  n_menu_entries, "<MessageView>", msgview);
418         gtk_widget_show(menubar);
419         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
420
421         if (prefs_common.toolbar_detachable) {
422                 handlebox = gtk_handle_box_new();
423         } else {
424                 handlebox = gtk_hbox_new(FALSE, 0);
425         }
426         gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
427         gtk_widget_realize(handlebox);
428 #ifdef MAEMO
429         msgview->toolbar = toolbar_create(TOOLBAR_MSGVIEW, window,
430                                           (gpointer)msgview);
431         msgview->statusbar = NULL;
432         msgview->statusbar_cid = 0;
433 #else
434         msgview->toolbar = toolbar_create(TOOLBAR_MSGVIEW, handlebox,
435                                           (gpointer)msgview);
436         statusbar = gtk_statusbar_new();
437         gtk_widget_show(statusbar);
438         gtk_box_pack_end(GTK_BOX(vbox), statusbar, FALSE, FALSE, 0);
439         msgview->statusbar = statusbar;
440         msgview->statusbar_cid = gtk_statusbar_get_context_id
441                 (GTK_STATUSBAR(statusbar), "Message View");
442 #endif
443
444
445         msgview->handlebox = handlebox;
446         msgview->menubar   = menubar;
447
448         gtk_container_add(GTK_CONTAINER(vbox),
449                           GTK_WIDGET_PTR(msgview));
450
451         messageview_update_actions_menu(msgview);
452
453         msgview_list = g_list_append(msgview_list, msgview);
454 }
455
456 static MessageView *messageview_create_with_new_window_visible(MainWindow *mainwin, gboolean show)
457 {
458         MessageView *msgview;
459         GtkWidget *window;
460         static GdkGeometry geometry;
461
462         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "messageview");
463         gtk_window_set_title(GTK_WINDOW(window), _("Claws Mail - Message View"));
464         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
465
466         if (!geometry.min_height) {
467                 geometry.min_width = 320;
468                 geometry.min_height = 200;
469         }
470         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
471                                       GDK_HINT_MIN_SIZE);
472
473         gtk_widget_set_size_request(window, prefs_common.msgwin_width,
474                                     prefs_common.msgwin_height);
475
476         msgview = messageview_create(mainwin);
477
478         g_signal_connect(G_OBJECT(window), "size_allocate",
479                          G_CALLBACK(messageview_size_allocate_cb),
480                          msgview);
481         g_signal_connect(G_OBJECT(window), "delete_event",
482                          G_CALLBACK(messageview_delete_cb), msgview);
483 #ifdef MAEMO
484         maemo_connect_key_press_to_mainwindow(GTK_WINDOW(window));
485 #else
486         g_signal_connect(G_OBJECT(window), "key_press_event",
487                          G_CALLBACK(key_pressed), msgview);
488 #endif
489         messageview_add_toolbar(msgview, window);
490
491         if (show) {
492                 gtk_widget_grab_focus(msgview->mimeview->textview->text);
493                 gtk_widget_show(window);
494         } else {
495                 gtk_widget_realize(window);
496         }
497
498         msgview->new_window = TRUE;
499         msgview->window = window;
500         msgview->visible = TRUE;
501
502         toolbar_set_style(msgview->toolbar->toolbar, msgview->handlebox, 
503                           prefs_common.toolbar_style);
504         messageview_init(msgview);
505
506         return msgview;
507 }
508
509 MessageView *messageview_create_with_new_window(MainWindow *mainwin)
510 {
511         return messageview_create_with_new_window_visible(mainwin, TRUE);
512 }
513 void messageview_init(MessageView *messageview)
514 {
515         headerview_init(messageview->headerview);
516         mimeview_init(messageview->mimeview);
517         /*messageview_set_font(messageview);*/
518
519         noticeview_hide(messageview->noticeview);
520 }
521
522 static void notification_convert_header(gchar *dest, gint len, 
523                                         const gchar *src_,
524                                         gint header_len)
525 {
526         char *src;
527
528         g_return_if_fail(src_ != NULL);
529         g_return_if_fail(dest != NULL);
530
531         if (len < 1) return;
532
533         Xstrndup_a(src, src_, len, return);
534
535         remove_return(src);
536
537         if (is_ascii_str(src)) {
538                 strncpy2(dest, src, len);
539                 dest[len - 1] = '\0';
540                 return;
541         } else
542                 conv_encode_header(dest, len, src, header_len, FALSE);
543 }
544
545 static gint disposition_notification_send(MsgInfo *msginfo)
546 {
547         gchar buf[BUFFSIZE];
548         gchar tmp[MAXPATHLEN + 1];
549         FILE *fp;
550         GList *ac_list;
551         PrefsAccount *account = NULL;
552         gint ok;
553         gchar *to;
554         FolderItem *queue, *outbox;
555         gint num;
556         gchar *path;
557         gchar *addr;
558         gchar *addrp;
559         gchar *foo = NULL;
560         gboolean queued_removed = FALSE;
561         
562         if (!msginfo->extradata)
563                 return -1;
564         if (!msginfo->extradata->returnreceiptto && 
565             !msginfo->extradata->dispositionnotificationto) 
566                 return -1;
567
568         /* RFC2298: Test for Return-Path */
569         if (msginfo->extradata->dispositionnotificationto)
570                 to = msginfo->extradata->dispositionnotificationto;
571         else
572                 to = msginfo->extradata->returnreceiptto;
573
574         ok = procheader_get_header_from_msginfo(msginfo, buf, sizeof(buf),
575                                 "Return-Path:");
576         if (ok == 0) {
577                 gchar *to_addr = g_strdup(to);
578                 extract_address(to_addr);
579                 extract_address(buf);
580                 ok = strcasecmp(to_addr, buf);
581                 g_free(to_addr);
582         } else {
583                 strncpy(buf, _("<No Return-Path found>"), 
584                                 sizeof(buf));
585         }
586         
587         if (ok != 0) {
588                 AlertValue val;
589                 gchar *message;
590                 message = g_strdup_printf(
591                   _("The notification address to which the return receipt is\n"
592                     "to be sent does not correspond to the return path:\n"
593                     "Notification address: %s\n"
594                     "Return path: %s\n"
595                     "It is advised to not to send the return receipt."),
596                   to, buf);
597                 val = alertpanel_full(_("Warning"), message,
598                                 _("_Don't Send"), _("_Send"), NULL, FALSE,
599                                 NULL, ALERT_WARNING, G_ALERTDEFAULT);
600                 g_free(message);                                
601                 if (val != G_ALERTALTERNATE)
602                         return -1;
603         }
604
605         ac_list = account_find_all_from_address(NULL, msginfo->to);
606         ac_list = account_find_all_from_address(ac_list, msginfo->cc);
607
608         if (ac_list == NULL) {
609                 AlertValue val = 
610                 alertpanel_full(_("Warning"),
611                   _("This message is asking for a return receipt notification\n"
612                     "but according to its 'To:' and 'CC:' headers it was not\n"
613                     "officially addressed to you.\n"
614                     "It is advised to not to send the return receipt."),
615                   _("_Don't Send"), _("_Send"), NULL, FALSE,
616                   NULL, ALERT_WARNING, G_ALERTDEFAULT);
617                 if (val != G_ALERTALTERNATE)
618                         return -1;
619         }
620
621         if (g_list_length(ac_list) > 1) {
622                 if ((account = select_account_from_list(ac_list)) == NULL)
623                         return -1;
624         }
625         else if (ac_list != NULL)
626                 account = (PrefsAccount *) ac_list->data;
627         g_list_free(ac_list);
628
629         if (account == NULL)
630                 account = account_get_default();
631         if (!account || account->protocol == A_NNTP) {
632                 alertpanel_error(_("Account for sending mail is not specified.\n"
633                                    "Please select a mail account before sending."));
634                 return -1;
635         }
636
637         /* write to temporary file */
638         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%p",
639                    get_rc_dir(), G_DIR_SEPARATOR, msginfo);
640
641         if ((fp = g_fopen(tmp, "wb")) == NULL) {
642                 FILE_OP_ERROR(tmp, "fopen");
643                 return -1;
644         }
645
646         /* chmod for security */
647         if (change_file_mode_rw(fp, tmp) < 0) {
648                 FILE_OP_ERROR(tmp, "chmod");
649                 g_warning("can't change file mode\n");
650         }
651         
652         addr = g_strdup(to);
653         
654         extract_address(addr);
655         addrp = addr;
656         
657         /* write queue headers */
658         fprintf(fp, "AF:\n");
659         fprintf(fp, "NF:0\n");
660         fprintf(fp, "PS:10\n");
661         fprintf(fp, "SRH:1\n");
662         fprintf(fp, "SFN:\n");
663         fprintf(fp, "DSR:\n");
664         fprintf(fp, "MID:\n");
665         fprintf(fp, "CFG:\n");
666         fprintf(fp, "PT:0\n");
667         fprintf(fp, "S:%s\n", account->address);
668         fprintf(fp, "RQ:\n");
669         if (account->smtp_server)
670                 fprintf(fp, "SSV:%s\n", account->smtp_server);
671         else
672                 fprintf(fp, "SSV:\n");
673         fprintf(fp, "SSH:\n");
674         fprintf(fp, "R:<%s>\n", addrp);
675         
676         g_free(addrp);
677         
678         /* check whether we need to save the message */
679         outbox = account_get_special_folder(account, F_OUTBOX); 
680         if (folder_get_default_outbox() == outbox && !prefs_common.savemsg)
681                 outbox = NULL;
682         if (outbox) {
683                 path = folder_item_get_identifier(outbox);
684                 fprintf(fp, "SCF:%s\n", path);
685                 g_free(path);
686         }               
687
688         fprintf(fp, "X-Claws-End-Special-Headers: 1\n");
689         
690         /* Date */
691         get_rfc822_date(buf, sizeof(buf));
692         fprintf(fp, "Date: %s\n", buf);
693
694         /* From */
695         if (account->name && *account->name) {
696                 notification_convert_header
697                         (buf, sizeof(buf), account->name,
698                          strlen("From: "));
699                 fprintf(fp, "From: %s <%s>\n", buf, account->address);
700         } else
701                 fprintf(fp, "From: %s\n", account->address);
702
703         fprintf(fp, "To: %s\n", to);
704
705         /* Subject */
706         notification_convert_header(buf, sizeof(buf), msginfo->subject,
707                                     strlen("Subject: "));
708         fprintf(fp, "Subject: Disposition notification: %s\n", buf);
709
710         /* Message ID */
711         generate_msgid(buf, sizeof(buf));
712         fprintf(fp, "Message-ID: <%s>\n", buf);
713
714         if (fclose(fp) == EOF) {
715                 FILE_OP_ERROR(tmp, "fclose");
716                 g_unlink(tmp);
717                 return -1;
718         }
719
720         /* put it in queue */
721         queue = account_get_special_folder(account, F_QUEUE);
722         if (!queue) queue = folder_get_default_queue();
723         if (!queue) {
724                 g_warning("can't find queue folder\n");
725                 g_unlink(tmp);
726                 return -1;
727         }
728         folder_item_scan(queue);
729         if ((num = folder_item_add_msg(queue, tmp, NULL, TRUE)) < 0) {
730                 g_warning("can't queue the message\n");
731                 g_unlink(tmp);
732                 return -1;
733         }
734                 
735         if (prefs_common.work_offline && 
736             !inc_offline_should_override(TRUE,
737                 _("Claws Mail needs network access in order "
738                   "to send this email.")))
739                 return 0;
740
741         /* send it */
742         path = folder_item_fetch_msg(queue, num);
743         ok = procmsg_send_message_queue(path, &foo, queue, num, &queued_removed);
744         g_free(path);
745         g_free(foo);
746         if (ok == 0 && !queued_removed)
747                 folder_item_remove_msg(queue, num);
748
749         return ok;
750 }
751
752 static gboolean find_encrypted_func(GNode *node, gpointer data)
753 {
754         MimeInfo *mimeinfo = (MimeInfo *) node->data;
755         MimeInfo **encinfo = (MimeInfo **) data;
756         
757         if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
758                 *encinfo = mimeinfo;
759                 return TRUE;
760         }
761         
762         return FALSE;
763 }
764
765 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
766 {
767         MimeInfo *encinfo = NULL;
768
769         g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
770                 find_encrypted_func, &encinfo);
771         
772         return encinfo;
773 }
774
775 gint messageview_show(MessageView *messageview, MsgInfo *msginfo,
776                       gboolean all_headers)
777 {
778         gchar *file;
779         MimeInfo *mimeinfo, *encinfo;
780         gchar *subject = NULL;
781         g_return_val_if_fail(msginfo != NULL, -1);
782
783         if (messageview->mimeview->textview &&
784             messageview->mimeview->textview->loading) {
785                 messageview->mimeview->textview->stop_loading = TRUE;
786                 return 0;
787         }
788
789         if (messageview->toolbar)
790                 toolbar_set_learn_button
791                         (messageview->toolbar,
792                          MSG_IS_SPAM(msginfo->flags)?LEARN_HAM:LEARN_SPAM);
793         else
794                 toolbar_set_learn_button
795                         (messageview->mainwin->toolbar,
796                          MSG_IS_SPAM(msginfo->flags)?LEARN_HAM:LEARN_SPAM);
797
798         if (messageview->toolbar) {
799                 if (messageview->toolbar->learn_spam_btn)
800                         gtk_widget_set_sensitive(
801                                 messageview->toolbar->learn_spam_btn, 
802                                 procmsg_spam_can_learn());
803         }
804         messageview->updating = TRUE;
805         mimeinfo = procmime_scan_message(msginfo);
806         messageview->updating = FALSE;
807         
808         if (messageview->deferred_destroy) {
809                 messageview_destroy(messageview);
810                 return 0;
811         }
812
813         if (!mimeinfo) {
814                 textview_show_error(messageview->mimeview->textview);
815                 return -1;
816         }
817
818         while ((encinfo = find_encrypted_part(mimeinfo)) != NULL) {
819                 debug_print("decrypting message part\n");
820                 if (privacy_mimeinfo_decrypt(encinfo) < 0) {
821                         alertpanel_error(_("Couldn't decrypt: %s"),
822                                 privacy_get_error());
823                         break;
824                 }
825         }
826         
827         messageview->updating = TRUE;
828         file = procmsg_get_message_file_path(msginfo);
829         messageview->updating = FALSE;
830         
831         if (messageview->deferred_destroy) {
832                 g_free(file);
833                 messageview_destroy(messageview);
834                 return 0;
835         }
836
837         if (!file) {
838                 g_warning("can't get message file path.\n");
839                 procmime_mimeinfo_free_all(mimeinfo);
840                 textview_show_error(messageview->mimeview->textview);
841                 return -1;
842         }
843         
844         if (messageview->msginfo != msginfo) {
845                 procmsg_msginfo_free(messageview->msginfo);
846                 messageview->msginfo = NULL;
847                 messageview_set_menu_sensitive(messageview);
848                 messageview->msginfo = procmsg_msginfo_get_full_info(msginfo);
849                 if (!messageview->msginfo)
850                         messageview->msginfo = procmsg_msginfo_copy(msginfo);
851         } else {
852                 messageview->msginfo = NULL;
853                 messageview_set_menu_sensitive(messageview);
854                 messageview->msginfo = msginfo;
855         }
856         headerview_show(messageview->headerview, messageview->msginfo);
857
858         messageview_set_position(messageview, 0);
859
860         textview_set_all_headers(messageview->mimeview->textview, 
861                         messageview->all_headers);
862
863 #ifdef MAEMO
864         maemo_window_full_screen_if_needed(GTK_WINDOW(messageview->window));
865 #endif
866         if (messageview->window) {
867                 gtk_window_set_title(GTK_WINDOW(messageview->window), 
868                                 _("Claws Mail - Message View"));
869                 GTK_EVENTS_FLUSH();
870         }
871         mimeview_show_message(messageview->mimeview, mimeinfo, file);
872         
873         messageview_set_position(messageview, 0);
874
875         if (messageview->window && msginfo->subject) {
876                 subject = g_strdup(msginfo->subject);
877                 if (!g_utf8_validate(subject, -1, NULL)) {
878                         g_free(subject);
879                         subject = g_malloc(strlen(msginfo->subject)*2 +1);
880                         conv_localetodisp(subject, strlen(msginfo->subject)*2 +1, 
881                                 msginfo->subject);
882                 }
883                 if (g_utf8_validate(subject, -1, NULL))
884                         gtk_window_set_title(GTK_WINDOW(messageview->window), 
885                                 subject);
886                 g_free(subject);
887         }
888
889         if (msginfo && msginfo->folder) {
890                 msginfo->folder->last_seen = msginfo->msgnum;   
891         }
892
893         main_create_mailing_list_menu(messageview->mainwin, messageview->msginfo);
894
895         if (messageview->msginfo && messageview->msginfo->extradata
896             && messageview->msginfo->extradata->partial_recv)
897                 partial_recv_show(messageview->noticeview, 
898                                   messageview->msginfo);
899         else if (messageview->msginfo && messageview->msginfo->extradata &&
900             (messageview->msginfo->extradata->dispositionnotificationto || 
901              messageview->msginfo->extradata->returnreceiptto) &&
902             !MSG_IS_RETRCPT_SENT(messageview->msginfo->flags) &&
903             !prefs_common.never_send_retrcpt)
904                 return_receipt_show(messageview->noticeview, 
905                                     messageview->msginfo);
906         else 
907                 noticeview_hide(messageview->noticeview);
908
909         mimeinfo = procmime_mimeinfo_next(mimeinfo);
910         if (!all_headers && mimeinfo 
911                         && (mimeinfo->type != MIMETYPE_TEXT || 
912             strcasecmp(mimeinfo->subtype, "plain")) 
913                         && (mimeinfo->type != MIMETYPE_MULTIPART || 
914             strcasecmp(mimeinfo->subtype, "signed"))) {
915                 if (strcasecmp(mimeinfo->subtype, "html"))
916                         mimeview_show_part(messageview->mimeview,mimeinfo);
917                 else if (prefs_common.invoke_plugin_on_html)
918                         mimeview_select_mimepart_icon(messageview->mimeview,mimeinfo);
919         }
920
921         g_free(file);
922
923         return 0;
924 }
925
926 void messageview_reflect_prefs_pixmap_theme(void)
927 {
928         GList *cur;
929         MessageView *msgview;
930
931         for (cur = msgview_list; cur != NULL; cur = cur->next) {
932                 msgview = (MessageView*)cur->data;
933                 toolbar_update(TOOLBAR_MSGVIEW, msgview);
934                 mimeview_update(msgview->mimeview);
935         }
936 }
937
938 void messageview_clear(MessageView *messageview)
939 {
940         if (!messageview)
941                 return;
942         procmsg_msginfo_free(messageview->msginfo);
943         messageview->msginfo = NULL;
944         messageview->filtered = FALSE;
945         mimeview_clear(messageview->mimeview);
946         headerview_clear(messageview->headerview);
947         noticeview_hide(messageview->noticeview);
948 }
949
950 void messageview_destroy(MessageView *messageview)
951 {
952         debug_print("destroy messageview\n");
953         messageview_list = g_list_remove(messageview_list, messageview);
954
955         if (messageview->mainwin->summaryview->messageview == messageview)
956                 messageview->mainwin->summaryview->messageview = NULL;
957
958         if (messageview->mainwin->summaryview->ext_messageview == messageview)
959                 messageview->mainwin->summaryview->ext_messageview = NULL;
960
961         if (!messageview->deferred_destroy) {
962                 hooks_unregister_hook(MSGINFO_UPDATE_HOOKLIST,
963                               messageview->msginfo_update_callback_id);
964         }
965
966         if (messageview->updating) {
967                 debug_print("uh oh, better not touch that now (fetching)\n");
968                 messageview->deferred_destroy = TRUE;
969                 gtk_widget_hide(messageview->window);
970                 return;
971         }
972         
973         if (messageview->mimeview->textview
974         &&  messageview->mimeview->textview->loading) {
975                 debug_print("uh oh, better not touch that now (loading text)\n");
976                 messageview->deferred_destroy = TRUE;
977                 messageview->mimeview->textview->stop_loading = TRUE;
978                 gtk_widget_hide(messageview->window);
979                 return;
980         }
981
982         headerview_destroy(messageview->headerview);
983         mimeview_destroy(messageview->mimeview);
984         noticeview_destroy(messageview->noticeview);
985
986         procmsg_msginfo_free(messageview->msginfo);
987         toolbar_clear_list(TOOLBAR_MSGVIEW);
988         if (messageview->toolbar) {
989                 toolbar_destroy(messageview->toolbar);
990                 g_free(messageview->toolbar);
991         }
992         
993         msgview_list = g_list_remove(msgview_list, messageview); 
994
995         if (messageview->window)
996                 gtk_widget_destroy(messageview->window);
997         g_free(messageview);
998 }
999
1000 void messageview_delete(MessageView *msgview)
1001 {
1002         MsgInfo *msginfo = NULL;
1003         FolderItem *trash = NULL;
1004         PrefsAccount *ac = NULL;
1005
1006         if (msgview->msginfo && msgview->mainwin && msgview->mainwin->summaryview)
1007                 msginfo = summary_get_selected_msg(msgview->mainwin->summaryview);
1008         
1009         /* need a procmsg_msginfo_equal() */
1010         if (msginfo && msgview->msginfo && 
1011             msginfo->msgnum == msgview->msginfo->msgnum && 
1012             msginfo->folder == msgview->msginfo->folder) {
1013                 summary_delete_trash(msgview->mainwin->summaryview);
1014         } else {                
1015                 msginfo = msgview->msginfo;
1016
1017                 g_return_if_fail(msginfo != NULL);
1018
1019                 /* to get the trash folder, we have to choose either
1020                  * the folder's or account's trash default - we prefer
1021                  * the one in the account prefs */
1022                 if (msginfo->folder) {
1023                         if (NULL != (ac = account_find_from_item(msginfo->folder)))
1024                                 trash = account_get_special_folder(ac, F_TRASH);
1025                         if (!trash && msginfo->folder->folder)  
1026                                 trash = msginfo->folder->folder->trash;
1027                         /* if still not found, use the default */
1028                         if (!trash) 
1029                                 trash = folder_get_default_trash();
1030                 }       
1031
1032                 g_return_if_fail(trash != NULL);
1033
1034                 if (prefs_common.immediate_exec)
1035                         /* TODO: Delete from trash */
1036                         folder_item_move_msg(trash, msginfo);
1037                 else {
1038                         procmsg_msginfo_set_to_folder(msginfo, trash);
1039                         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
1040                         /* NOTE: does not update to next message in summaryview */
1041                 }
1042         }
1043 #ifdef MAEMO
1044         if (msgview->window) {
1045                 messageview_destroy(msgview);
1046         }
1047 #endif
1048 }
1049
1050 /* 
1051  * \brief update messageview with currently selected message in summaryview
1052  *        leave unchanged if summaryview is empty
1053  * \param pointer to MessageView
1054  */     
1055 static void messageview_update(MessageView *msgview, MsgInfo *old_msginfo)
1056 {
1057         SummaryView *summaryview = (SummaryView*)msgview->mainwin->summaryview;
1058
1059         g_return_if_fail(summaryview != NULL);
1060         
1061         if (summaryview->selected) {
1062                 MsgInfo *msginfo = summary_get_selected_msg(summaryview);
1063                 if (msginfo == NULL || msginfo == old_msginfo)
1064                         return;
1065
1066                 messageview_show(msgview, msginfo, 
1067                                  msgview->all_headers);
1068         } 
1069 }
1070
1071 void messageview_quote_color_set(void)
1072 {
1073 }
1074
1075 void messageview_set_font(MessageView *messageview)
1076 {
1077 }
1078
1079 TextView *messageview_get_current_textview(MessageView *messageview)
1080 {
1081         TextView *text = NULL;
1082
1083         text = messageview->mimeview->textview;
1084
1085         return text;
1086 }
1087
1088 MimeInfo *messageview_get_selected_mime_part(MessageView *messageview)
1089 {
1090         return mimeview_get_selected_part(messageview->mimeview);
1091 }
1092
1093 void messageview_copy_clipboard(MessageView *messageview)
1094 {
1095         gchar *text = messageview_get_selection(messageview);
1096         if (text) {
1097                 gtk_clipboard_set_text(
1098                         gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
1099                         text, -1);
1100         }
1101         g_free(text);
1102 }
1103
1104 void messageview_select_all(MessageView *messageview)
1105 {
1106         TextView *text;
1107
1108         text = messageview_get_current_textview(messageview);
1109         if (text) {
1110                 GtkTextView *textview = GTK_TEXT_VIEW(text->text);
1111                 GtkTextBuffer *buffer;
1112                 GtkTextIter start, end;
1113
1114                 buffer = gtk_text_view_get_buffer(textview);
1115                 gtk_text_buffer_get_bounds(buffer, &start, &end);
1116                 gtk_text_buffer_select_range(buffer, &start, &end);
1117         }
1118 }
1119
1120 void messageview_set_position(MessageView *messageview, gint pos)
1121 {
1122         TextView *text;
1123
1124         text = messageview_get_current_textview(messageview);
1125         if (text)
1126                 textview_set_position(text, pos);
1127 }
1128
1129 gboolean messageview_search_string(MessageView *messageview, const gchar *str,
1130                                    gboolean case_sens)
1131 {
1132         TextView *text;
1133
1134         if (messageview->mimeview->type == MIMEVIEW_VIEWER) {
1135                 MimeViewer *viewer = messageview->mimeview->mimeviewer;
1136                 if (viewer && viewer->text_search) {
1137                         return viewer->text_search(viewer, FALSE, str, case_sens);
1138                 }
1139         }
1140
1141         text = messageview_get_current_textview(messageview);
1142         if (text)
1143                 return textview_search_string(text, str, case_sens);
1144         return FALSE;
1145 }
1146
1147 gboolean messageview_search_string_backward(MessageView *messageview,
1148                                             const gchar *str,
1149                                             gboolean case_sens)
1150 {
1151         TextView *text;
1152
1153         if (messageview->mimeview->type == MIMEVIEW_VIEWER) {
1154                 MimeViewer *viewer = messageview->mimeview->mimeviewer;
1155                 if (viewer && viewer->text_search) {
1156                         return viewer->text_search(viewer, TRUE, str, case_sens);
1157                 }
1158         }
1159
1160         text = messageview_get_current_textview(messageview);
1161         if (text)       
1162                 return textview_search_string_backward(text,
1163                                                        str, case_sens);
1164         return FALSE;
1165 }
1166
1167 gboolean messageview_is_visible(MessageView *messageview)
1168 {
1169         if (messageview == NULL)
1170                 return FALSE;
1171         return messageview->visible;
1172 }
1173
1174 static void messageview_save_as(MessageView *messageview)
1175 {
1176         gchar *filename = NULL;
1177         MsgInfo *msginfo;
1178         gchar *src, *dest, *tmp;
1179
1180         if (!messageview->msginfo) return;
1181         msginfo = messageview->msginfo;
1182
1183         if (msginfo->subject) {
1184                 Xstrdup_a(filename, msginfo->subject, return);
1185                 subst_for_filename(filename);
1186         }
1187         if (filename && !g_utf8_validate(filename, -1, NULL)) {
1188                 gchar *oldstr = filename;
1189                 filename = conv_codeset_strdup(filename,
1190                                                conv_get_locale_charset_str(),
1191                                                CS_UTF_8);
1192                 if (!filename) {
1193                         g_warning("messageview_save_as(): failed to convert character set.");
1194                         filename = g_strdup(oldstr);
1195                 }
1196                 dest = filesel_select_file_save(_("Save as"), filename);
1197                 g_free(filename);
1198         } else
1199                 dest = filesel_select_file_save(_("Save as"), filename);
1200         if (!dest) return;
1201         if (is_file_exist(dest)) {
1202                 AlertValue aval;
1203
1204                 aval = alertpanel(_("Overwrite"),
1205                                   _("Overwrite existing file?"),
1206                                   GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL);
1207                 if (G_ALERTALTERNATE != aval) return;
1208         }
1209
1210         src = procmsg_get_message_file(msginfo);
1211         if (copy_file(src, dest, TRUE) < 0) {
1212                 tmp =  g_path_get_basename(dest);
1213                 alertpanel_error(_("Couldn't save the file '%s'."), tmp);
1214                 g_free(tmp);
1215         }
1216         g_free(dest);
1217         g_free(src);
1218 }
1219
1220 static gint messageview_delete_cb(GtkWidget *widget, GdkEventAny *event,
1221                                   MessageView *messageview)
1222 {
1223         messageview_destroy(messageview);
1224         return TRUE;
1225 }
1226
1227 static void messageview_size_allocate_cb(GtkWidget *widget,
1228                                          GtkAllocation *allocation)
1229 {
1230         g_return_if_fail(allocation != NULL);
1231
1232         prefs_common.msgwin_width  = allocation->width;
1233         prefs_common.msgwin_height = allocation->height;
1234 }
1235
1236 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
1237                         MessageView *messageview)
1238 {
1239         if (event && event->keyval == GDK_Escape && messageview->window) {
1240                 messageview_destroy(messageview);
1241                 return TRUE;
1242         }
1243
1244         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0)
1245                 return FALSE;
1246
1247         g_signal_stop_emission_by_name(G_OBJECT(widget),
1248                                         "key_press_event");
1249         mimeview_pass_key_press_event(messageview->mimeview, event);
1250         return FALSE;
1251 }
1252
1253 static void return_receipt_show(NoticeView *noticeview, MsgInfo *msginfo)
1254 {
1255         gchar *addr = NULL;
1256         gboolean from_me = FALSE;
1257         if (msginfo->folder 
1258                 && (folder_has_parent_of_type(msginfo->folder, F_QUEUE)
1259                  || folder_has_parent_of_type(msginfo->folder, F_DRAFT)))
1260                 return;
1261
1262         addr = g_strdup(msginfo->from);
1263         if (addr) {
1264                 extract_address(addr);
1265                 if (account_find_from_address(addr)) {
1266                         from_me = TRUE;
1267                 }
1268                 g_free(addr);
1269         }
1270
1271         if (from_me) {
1272                 noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
1273                 noticeview_set_text(noticeview, _("You asked for a return receipt in this message."));
1274                 noticeview_set_button_text(noticeview, NULL);
1275                 noticeview_set_button_press_callback(noticeview, NULL, NULL);
1276         } else {
1277                 noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
1278                 noticeview_set_text(noticeview, _("This message asks for a return receipt."));
1279                 noticeview_set_button_text(noticeview, _("Send receipt"));
1280                 noticeview_set_button_press_callback(noticeview,
1281                                                      G_CALLBACK(return_receipt_send_clicked),
1282                                                      (gpointer) msginfo);
1283         }
1284         noticeview_show(noticeview);
1285 }
1286
1287 static void return_receipt_send_clicked(NoticeView *noticeview, MsgInfo *msginfo)
1288 {
1289         MsgInfo *tmpmsginfo;
1290         gchar *file;
1291
1292         file = procmsg_get_message_file_path(msginfo);
1293         if (!file) {
1294                 g_warning("can't get message file path.\n");
1295                 return;
1296         }
1297
1298         tmpmsginfo = procheader_parse_file(file, msginfo->flags, TRUE, TRUE);
1299         tmpmsginfo->folder = msginfo->folder;
1300         tmpmsginfo->msgnum = msginfo->msgnum;
1301
1302         if (disposition_notification_send(tmpmsginfo) >= 0) {
1303                 procmsg_msginfo_set_flags(msginfo, MSG_RETRCPT_SENT, 0);
1304                 noticeview_hide(noticeview);
1305         }               
1306
1307         procmsg_msginfo_free(tmpmsginfo);
1308         g_free(file);
1309 }
1310
1311 static void partial_recv_show(NoticeView *noticeview, MsgInfo *msginfo)
1312 {
1313         gchar *text = NULL;
1314         gchar *button1 = NULL;
1315         gchar *button2 = NULL;
1316         void  *button1_cb = NULL;
1317         void  *button2_cb = NULL;
1318
1319         if (!msginfo->extradata)
1320                 return;
1321         if (!partial_msg_in_uidl_list(msginfo)) {
1322                 text = g_strdup_printf(_("This message has been partially "
1323                                 "retrieved,\nand has been deleted from the "
1324                                 "server."));
1325         } else {
1326                 switch (msginfo->planned_download) {
1327                 case POP3_PARTIAL_DLOAD_UNKN:
1328                         text = g_strdup_printf(_("This message has been "
1329                                         "partially retrieved;\nit is %s."),
1330                                         to_human_readable(
1331                                                 (off_t)(msginfo->total_size)));
1332                         button1 = _("Mark for download");
1333                         button2 = _("Mark for deletion");
1334                         button1_cb = partial_recv_dload_clicked;
1335                         button2_cb = partial_recv_del_clicked;
1336                         break;
1337                 case POP3_PARTIAL_DLOAD_DLOAD:
1338                         text = g_strdup_printf(_("This message has been "
1339                                         "partially retrieved;\nit is %s and "
1340                                         "will be downloaded."),
1341                                         to_human_readable(
1342                                                 (off_t)(msginfo->total_size)));
1343                         button1 = _("Unmark");
1344                         button1_cb = partial_recv_unmark_clicked;
1345                         button2 = _("Mark for deletion");
1346                         button2_cb = partial_recv_del_clicked;
1347                         break;
1348                 case POP3_PARTIAL_DLOAD_DELE:
1349                         text = g_strdup_printf(_("This message has been "
1350                                         "partially retrieved;\nit is %s and "
1351                                         "will be deleted."),
1352                                         to_human_readable(
1353                                                 (off_t)(msginfo->total_size)));
1354                         button1 = _("Mark for download");
1355                         button1_cb = partial_recv_dload_clicked;
1356                         button2 = _("Unmark");
1357                         button2_cb = partial_recv_unmark_clicked;
1358                         break;
1359                 default:
1360                         return;
1361                 }
1362         }
1363         
1364         noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
1365         noticeview_set_text(noticeview, text);
1366         g_free(text);
1367         noticeview_set_button_text(noticeview, button1);
1368         noticeview_set_button_press_callback(noticeview,
1369                      G_CALLBACK(button1_cb), (gpointer) msginfo);
1370
1371         noticeview_set_2ndbutton_text(noticeview, button2);
1372         noticeview_set_2ndbutton_press_callback(noticeview,
1373                      G_CALLBACK(button2_cb), (gpointer) msginfo);
1374
1375         noticeview_show(noticeview);
1376 }
1377
1378 static void partial_recv_dload_clicked(NoticeView *noticeview, 
1379                                        MsgInfo *msginfo)
1380 {
1381         if (partial_mark_for_download(msginfo) == 0) {
1382                 partial_recv_show(noticeview, msginfo);
1383         }
1384 }
1385
1386 static void partial_recv_del_clicked(NoticeView *noticeview, 
1387                                        MsgInfo *msginfo)
1388 {
1389         if (partial_mark_for_delete(msginfo) == 0) {
1390                 partial_recv_show(noticeview, msginfo);
1391         }
1392 }
1393
1394 static void partial_recv_unmark_clicked(NoticeView *noticeview, 
1395                                        MsgInfo *msginfo)
1396 {
1397         if (partial_unmark(msginfo) == 0) {
1398                 partial_recv_show(noticeview, msginfo);
1399         }
1400 }
1401
1402 static void select_account_cb(GtkWidget *w, gpointer data)
1403 {
1404         *(gint*)data = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w), MENU_VAL_ID));
1405 }
1406         
1407 static PrefsAccount *select_account_from_list(GList *ac_list)
1408 {
1409         GtkWidget *optmenu;
1410         GtkWidget *menu;
1411         gint account_id;
1412
1413         g_return_val_if_fail(ac_list != NULL, NULL);
1414         g_return_val_if_fail(ac_list->data != NULL, NULL);
1415         
1416         optmenu = gtk_option_menu_new();
1417         menu = gtkut_account_menu_new(ac_list, 
1418                         G_CALLBACK(select_account_cb), 
1419                         &account_id);
1420         if (!menu)
1421                 return NULL;
1422         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), menu);
1423         gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), 0);
1424         account_id = ((PrefsAccount *) ac_list->data)->account_id;
1425         if (alertpanel_with_widget(
1426                                 _("Return Receipt Notification"),
1427                                 _("The message was sent to several of your "
1428                                   "accounts.\n"
1429                                   "Please choose which account do you want to "
1430                                   "use for sending the receipt notification:"),
1431                                 _("_Cancel"), _("_Send Notification"), NULL,
1432                                 FALSE, G_ALERTDEFAULT, optmenu) != G_ALERTALTERNATE)
1433                 return NULL;
1434         return account_find_from_id(account_id);
1435 }
1436
1437 /* 
1438  * \brief return selected messageview text, when nothing is 
1439  *        selected and message was filtered, return complete text
1440  *
1441  * \param  pointer to Messageview 
1442  *
1443  * \return pointer to text (needs to be free'd by calling func)
1444  */
1445 gchar *messageview_get_selection(MessageView *msgview)
1446 {
1447         TextView *textview;
1448         gchar *text = NULL;
1449         GtkTextView *edit = NULL;
1450         GtkTextBuffer *textbuf;
1451         gint body_pos = 0;
1452         
1453         g_return_val_if_fail(msgview != NULL, NULL);
1454
1455         if (msgview->mimeview->type == MIMEVIEW_VIEWER) {
1456                 MimeViewer *viewer = msgview->mimeview->mimeviewer;
1457                 if (viewer && viewer->get_selection) {
1458                         text = viewer->get_selection(viewer);
1459                         if (text)
1460                                 return text;
1461                 }
1462         }
1463
1464         textview = messageview_get_current_textview(msgview);
1465         g_return_val_if_fail(textview != NULL, NULL);
1466
1467         edit = GTK_TEXT_VIEW(textview->text);
1468         g_return_val_if_fail(edit != NULL, NULL);
1469         body_pos = textview->body_pos;
1470
1471         textbuf = gtk_text_view_get_buffer(edit);
1472
1473         if (gtk_text_buffer_get_selection_bounds(textbuf, NULL, NULL))
1474                 return gtkut_text_view_get_selection(edit);
1475         else if (msgview->filtered) {
1476                 GtkTextIter start_iter, end_iter;
1477                 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter, body_pos);
1478                 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
1479                 gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter, FALSE);
1480         } else
1481                 text = NULL;
1482
1483         return text;
1484 }
1485
1486 static void save_as_cb(gpointer data, guint action, GtkWidget *widget)
1487 {
1488         MessageView *messageview = (MessageView *)data;
1489         messageview_save_as(messageview);
1490 }
1491
1492 #ifdef USE_GNOMEPRINT
1493 static void print_mimeview(MimeView *mimeview, gint sel_start, gint sel_end, gint partnum) 
1494 {
1495         if (!mimeview 
1496         ||  !mimeview->textview
1497         ||  !mimeview->textview->text)
1498                 alertpanel_warning(_("Cannot print: the message doesn't "
1499                                      "contain text."));
1500         else {
1501                 gtk_widget_realize(mimeview->textview->text);
1502                 if (partnum > 0) {
1503                         mimeview_select_part_num(mimeview, partnum);
1504                 }
1505                 if (mimeview->type == MIMEVIEW_VIEWER) {
1506                         MimeViewer *viewer = mimeview->mimeviewer;
1507                         if (viewer && viewer->print) {
1508                                 viewer->print(viewer);
1509                                 return;
1510                         }
1511                 }
1512                 if (sel_start != -1 && sel_end != -1) {
1513                         GtkTextIter start, end;
1514                         GtkTextView *text = GTK_TEXT_VIEW(mimeview->textview->text);
1515                         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
1516
1517                         gtk_text_buffer_get_iter_at_offset(buffer, &start, sel_start);
1518                         gtk_text_buffer_get_iter_at_offset(buffer, &end, sel_end);
1519                         gtk_text_buffer_select_range(buffer, &start, &end);
1520                 }
1521
1522                 gedit_print(GTK_TEXT_VIEW(mimeview->textview->text));
1523         }
1524 }
1525
1526 void messageview_print(MsgInfo *msginfo, gboolean all_headers, 
1527                         gint sel_start, gint sel_end, gint partnum) 
1528 {
1529         PangoFontDescription *font_desc = NULL;
1530         MessageView *tmpview = messageview_create_with_new_window_visible(
1531                                 mainwindow_get_mainwindow(), FALSE);
1532
1533         if (prefs_common.use_different_print_font) {
1534                 font_desc = pango_font_description_from_string
1535                                                 (prefs_common.printfont);
1536         } else {
1537                 font_desc = pango_font_description_from_string
1538                                                 (prefs_common.textfont);
1539         }
1540         if (font_desc) {
1541                 gtk_widget_modify_font(tmpview->mimeview->textview->text, 
1542                         font_desc);
1543                 pango_font_description_free(font_desc);
1544         }
1545
1546         tmpview->all_headers = all_headers;
1547         if (msginfo && messageview_show(tmpview, msginfo, 
1548                 tmpview->all_headers) >= 0) {
1549                         print_mimeview(tmpview->mimeview, 
1550                                 sel_start, sel_end, partnum);
1551         }
1552         messageview_destroy(tmpview);
1553 }
1554 #endif
1555
1556 static void print_cb(gpointer data, guint action, GtkWidget *widget)
1557 {
1558         MessageView *messageview = (MessageView *)data;
1559 #ifndef USE_GNOMEPRINT
1560         gchar *cmdline = NULL;
1561         gchar *p;
1562 #else
1563         gint sel_start = -1, sel_end = -1, partnum = 0;
1564 #endif
1565
1566         if (!messageview->msginfo) return;
1567 #ifndef USE_GNOMEPRINT
1568         cmdline = input_dialog(_("Print"),
1569                                _("Enter the print command line:\n"
1570                                  "('%s' will be replaced with file name)"),
1571                                prefs_common.print_cmd);
1572         if (!cmdline) return;
1573         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
1574             strchr(p + 2, '%')) {
1575                 alertpanel_error(_("Print command line is invalid:\n'%s'"),
1576                                  cmdline);
1577                 g_free(cmdline);
1578                 return;
1579         }
1580         procmsg_print_message(messageview->msginfo, cmdline);
1581         g_free(cmdline);
1582 #else
1583         partnum = mimeview_get_selected_part_num(messageview->mimeview);
1584         textview_get_selection_offsets(messageview->mimeview->textview,
1585                 &sel_start, &sel_end);
1586         messageview_print(messageview->msginfo, messageview->all_headers, 
1587                 sel_start, sel_end, partnum);
1588 #endif
1589 }
1590
1591 static void close_cb(gpointer data, guint action, GtkWidget *widget)
1592 {
1593         MessageView *messageview = (MessageView *)data;
1594         messageview_destroy(messageview);
1595 }
1596
1597 static void copy_cb(gpointer data, guint action, GtkWidget *widget)
1598 {
1599         MessageView *messageview = (MessageView *)data;
1600         messageview_copy_clipboard(messageview);
1601 }
1602
1603 static void allsel_cb(gpointer data, guint action, GtkWidget *widget)
1604 {
1605         MessageView *messageview = (MessageView *)data;
1606         messageview_select_all(messageview);
1607 }
1608
1609 static void search_cb(gpointer data, guint action, GtkWidget *widget)
1610 {
1611         MessageView *messageview = (MessageView *)data;
1612         message_search(messageview);
1613 }
1614
1615 static void set_charset_cb(gpointer data, guint action, GtkWidget *widget)
1616 {
1617         MessageView *messageview = (MessageView *)data;
1618         const gchar *charset;
1619
1620         if (GTK_CHECK_MENU_ITEM(widget)->active) {
1621                 charset = conv_get_charset_str((CharSet)action);
1622                 g_free(messageview->forced_charset);
1623                 messageview->forced_charset = g_strdup(charset);
1624                 procmime_force_charset(charset);
1625                 
1626                 messageview_show(messageview, messageview->msginfo, FALSE);
1627         }
1628 }
1629
1630 static void set_decode_cb(gpointer data, guint action, GtkWidget *widget)
1631 {
1632         MessageView *messageview = (MessageView *)data;
1633         if (GTK_CHECK_MENU_ITEM(widget)->active) {
1634                 messageview->forced_encoding = (EncodingType)action;
1635
1636                 messageview_show(messageview, messageview->msginfo, FALSE);
1637                 
1638                 debug_print("forced encoding: %d\n", action);
1639         }
1640 }
1641
1642
1643 static void view_source_cb(gpointer data, guint action, GtkWidget *widget)
1644 {
1645         MessageView *messageview = (MessageView *)data;
1646         SourceWindow *srcwin;
1647
1648         if (!messageview->msginfo) return;
1649
1650         srcwin = source_window_create();
1651         source_window_show_msg(srcwin, messageview->msginfo);
1652         source_window_show(srcwin);
1653 }
1654
1655 static void show_all_header_cb(gpointer data, guint action, GtkWidget *widget)
1656 {
1657         MessageView *messageview = (MessageView *)data;
1658         MsgInfo *msginfo = messageview->msginfo;
1659
1660         if (messageview->mimeview->textview &&
1661             messageview->mimeview->textview->loading) {
1662                 return;
1663         }
1664         if (messageview->updating)
1665                 return;
1666
1667         messageview->all_headers = 
1668                         GTK_CHECK_MENU_ITEM(widget)->active;
1669         if (!msginfo) return;
1670         messageview->msginfo = NULL;
1671         messageview_show(messageview, msginfo,
1672                          GTK_CHECK_MENU_ITEM(widget)->active);
1673         procmsg_msginfo_free(msginfo);
1674         main_window_set_menu_sensitive(messageview->mainwin);
1675 }
1676
1677 #define SET_CHECK_MENU_ACTIVE(path, active) \
1678 { \
1679         menuitem = gtk_item_factory_get_widget(ifactory, path); \
1680         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), active); \
1681 }
1682
1683 static void msg_hide_quotes_cb(gpointer data, guint action, GtkWidget *widget)
1684 {
1685         MessageView *messageview = (MessageView *)data;
1686         MsgInfo *msginfo = messageview->msginfo;
1687         static gboolean updating_menu = FALSE;
1688         GtkItemFactory *ifactory = gtk_item_factory_from_widget(messageview->menubar);
1689         GtkWidget *menuitem;
1690         if (updating_menu)
1691                 return;
1692
1693         prefs_common.hide_quotes = 
1694                         GTK_CHECK_MENU_ITEM(widget)->active ? action : 0;
1695         
1696         updating_menu=TRUE;
1697         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold all", FALSE);
1698         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 2", FALSE);
1699         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 3", FALSE);
1700         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), prefs_common.hide_quotes > 0);      
1701         updating_menu=FALSE;
1702         if (!msginfo) return;
1703         messageview->msginfo = NULL;
1704         messageview_show(messageview, msginfo,
1705                          messageview->all_headers);
1706         procmsg_msginfo_free(msginfo);
1707         
1708         /* update main window */
1709         main_window_set_menu_sensitive(messageview->mainwin);
1710         summary_redisplay_msg(messageview->mainwin->summaryview);
1711 }
1712 #undef SET_CHECK_MENU_ACTIVE
1713
1714 static void compose_cb(gpointer data, guint action, GtkWidget *widget)
1715 {
1716         MessageView *messageview = (MessageView *)data;
1717         PrefsAccount *ac = NULL;
1718         FolderItem *item = NULL;
1719
1720         if (messageview->msginfo)
1721                 item = messageview->msginfo->folder;
1722
1723         if (item) {
1724                 ac = account_find_from_item(item);
1725                 if (ac && ac->protocol == A_NNTP &&
1726                     item->stype == F_NEWS) {
1727                         compose_new(ac, item->path, NULL);
1728                         return;
1729                 }
1730         }
1731
1732         compose_new(ac, NULL, NULL);
1733 }
1734
1735 static void reply_cb(gpointer data, guint action, GtkWidget *widget)
1736 {
1737         MessageView *messageview = (MessageView *)data;
1738         GSList *msginfo_list = NULL;
1739
1740         g_return_if_fail(messageview->msginfo);
1741
1742         msginfo_list = g_slist_append(msginfo_list, messageview->msginfo);
1743         compose_reply_from_messageview(messageview, msginfo_list, action);
1744         g_slist_free(msginfo_list);
1745 }
1746
1747 static void addressbook_open_cb(gpointer data, guint action, GtkWidget *widget)
1748 {
1749         addressbook_open(NULL);
1750 }
1751
1752 static void add_address_cb(gpointer data, guint action, GtkWidget *widget)
1753 {
1754         MessageView *messageview = (MessageView *)data;
1755         MsgInfo *msginfo;
1756         gchar *from;
1757
1758         if (!messageview->msginfo) return;
1759         msginfo = messageview->msginfo;
1760         Xstrdup_a(from, msginfo->from, return);
1761         eliminate_address_comment(from);
1762         extract_address(from);
1763         addressbook_add_contact(msginfo->fromname, from, NULL);
1764 }
1765
1766 static void create_filter_cb(gpointer data, guint action, GtkWidget *widget)
1767 {
1768         MessageView *messageview = (MessageView *)data;
1769         FolderItem * item;
1770         
1771         if (!messageview->msginfo) return;
1772         
1773         item = messageview->msginfo->folder;
1774         summary_msginfo_filter_open(item,  messageview->msginfo,
1775                                     (PrefsFilterType)action, 0);
1776 }
1777
1778 static void create_processing_cb(gpointer data, guint action,
1779                                  GtkWidget *widget)
1780 {
1781         MessageView *messageview = (MessageView *)data;
1782         FolderItem * item;
1783         
1784         if (!messageview->msginfo) return;
1785         
1786         item = messageview->msginfo->folder;
1787         summary_msginfo_filter_open(item,  messageview->msginfo,
1788                                     (PrefsFilterType)action, 1);
1789 }
1790
1791 static void open_urls_cb(gpointer data, guint action, GtkWidget *widget)
1792 {
1793         MessageView *messageview = (MessageView *)data;
1794         messageview_list_urls(messageview);
1795 }
1796
1797 static void about_cb(gpointer data, guint action, GtkWidget *widget)
1798 {
1799         about_show();
1800 }
1801
1802 static gboolean messageview_update_msg(gpointer source, gpointer data)
1803 {
1804         MsgInfoUpdate *msginfo_update = (MsgInfoUpdate *) source;
1805         MessageView *messageview = (MessageView *)data;
1806
1807         if (messageview->msginfo != msginfo_update->msginfo)
1808                 return FALSE;
1809
1810         if (msginfo_update->flags & MSGINFO_UPDATE_DELETED) {
1811                 MsgInfo *old_msginfo = messageview->msginfo;
1812                 messageview_clear(messageview);
1813                 messageview_update(messageview, old_msginfo);
1814         }
1815
1816         return FALSE;
1817 }
1818
1819 void messageview_set_menu_sensitive(MessageView *messageview)
1820 {
1821         GtkItemFactory *ifactory;
1822         GtkWidget *menuitem = NULL;
1823
1824         if (!messageview || !messageview->new_window) 
1825                 return;
1826         /* do some smart things */
1827         if (!messageview->menubar) return;
1828         ifactory = gtk_item_factory_from_widget(messageview->menubar);
1829         if (!ifactory) return;
1830
1831         if (prefs_common.hide_quotes) {
1832                 menuitem = NULL;
1833                 if (prefs_common.hide_quotes == 1)
1834                         menuitem = gtk_item_factory_get_widget(ifactory, 
1835                                         "/View/Quotes/Fold all");
1836                 if (prefs_common.hide_quotes == 2)
1837                         menuitem = gtk_item_factory_get_widget(ifactory, 
1838                                         "/View/Quotes/Fold from level 2");
1839                 if (prefs_common.hide_quotes == 3)
1840                         menuitem = gtk_item_factory_get_widget(ifactory, 
1841                                         "/View/Quotes/Fold from level 3");
1842                 gtk_check_menu_item_set_active
1843                         (GTK_CHECK_MENU_ITEM(menuitem),
1844                          TRUE);
1845         }
1846 }
1847
1848 void messageview_learn (MessageView *msgview, gboolean is_spam)
1849 {
1850         if (is_spam) {
1851                 if (procmsg_spam_learner_learn(msgview->msginfo, NULL, TRUE) == 0)
1852                         procmsg_msginfo_set_flags(msgview->msginfo, MSG_SPAM, 0);
1853                 else
1854                         log_error(LOG_PROTOCOL, _("An error happened while learning.\n"));
1855                 
1856         } else {
1857                 if (procmsg_spam_learner_learn(msgview->msginfo, NULL, FALSE) == 0)
1858                         procmsg_msginfo_unset_flags(msgview->msginfo, MSG_SPAM, 0);
1859                 else
1860                         log_error(LOG_PROTOCOL, _("An error happened while learning.\n"));
1861         }
1862         if (msgview->toolbar)
1863                 toolbar_set_learn_button
1864                         (msgview->toolbar,
1865                          MSG_IS_SPAM(msgview->msginfo->flags)?LEARN_HAM:LEARN_SPAM);
1866         else
1867                 toolbar_set_learn_button
1868                         (msgview->mainwin->toolbar,
1869                          MSG_IS_SPAM(msgview->msginfo->flags)?LEARN_HAM:LEARN_SPAM);
1870 }
1871
1872 void messageview_list_urls (MessageView *msgview)
1873 {
1874         GSList *cur = msgview->mimeview->textview->uri_list;
1875         GSList *newlist = NULL;
1876         for (; cur; cur = cur->next) {
1877                 ClickableText *uri = (ClickableText *)cur->data;
1878                 if (!uri->uri || !g_ascii_strncasecmp(uri->uri, "mailto:", 7) 
1879                 ||  uri->is_quote)
1880                         continue;
1881                 newlist = g_slist_prepend(newlist, uri);
1882         }
1883         newlist = g_slist_reverse(newlist);
1884         uri_opener_open(msgview, newlist);
1885         g_slist_free(newlist);
1886 }