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