2007-09-18 [colin] 3.0.1cvs2
[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         fprintf(fp, "AF:\n");
669         fprintf(fp, "NF:0\n");
670         fprintf(fp, "PS:10\n");
671         fprintf(fp, "SRH:1\n");
672         fprintf(fp, "SFN:\n");
673         fprintf(fp, "DSR:\n");
674         fprintf(fp, "MID:\n");
675         fprintf(fp, "CFG:\n");
676         fprintf(fp, "PT:0\n");
677         fprintf(fp, "S:%s\n", account->address);
678         fprintf(fp, "RQ:\n");
679         if (account->smtp_server)
680                 fprintf(fp, "SSV:%s\n", account->smtp_server);
681         else
682                 fprintf(fp, "SSV:\n");
683         fprintf(fp, "SSH:\n");
684         fprintf(fp, "R:<%s>\n", addrp);
685         
686         g_free(addrp);
687         
688         /* check whether we need to save the message */
689         outbox = account_get_special_folder(account, F_OUTBOX); 
690         if (folder_get_default_outbox() == outbox && !prefs_common.savemsg)
691                 outbox = NULL;
692         if (outbox) {
693                 path = folder_item_get_identifier(outbox);
694                 fprintf(fp, "SCF:%s\n", path);
695                 g_free(path);
696         }               
697
698         fprintf(fp, "X-Claws-End-Special-Headers: 1\n");
699         
700         /* Date */
701         get_rfc822_date(buf, sizeof(buf));
702         fprintf(fp, "Date: %s\n", buf);
703
704         /* From */
705         if (account->name && *account->name) {
706                 notification_convert_header
707                         (buf, sizeof(buf), account->name,
708                          strlen("From: "));
709                 fprintf(fp, "From: %s <%s>\n", buf, account->address);
710         } else
711                 fprintf(fp, "From: %s\n", account->address);
712
713         fprintf(fp, "To: %s\n", to);
714
715         /* Subject */
716         notification_convert_header(buf, sizeof(buf), msginfo->subject,
717                                     strlen("Subject: "));
718         fprintf(fp, "Subject: Disposition notification: %s\n", buf);
719
720         /* Message ID */
721         if (account->set_domain && account->domain) {
722                 g_snprintf(buf, sizeof(buf), "%s", account->domain); 
723         } else if (!strncmp(get_domain_name(), "localhost", strlen("localhost"))) {
724                 g_snprintf(buf, sizeof(buf), "%s", 
725                         strchr(account->address, '@') ?
726                                 strchr(account->address, '@')+1 :
727                                 account->address);
728         } else {
729                 g_snprintf(buf, sizeof(buf), "%s", "");
730         }
731         generate_msgid(buf, sizeof(buf));
732         fprintf(fp, "Message-ID: <%s>\n", buf);
733
734         if (fclose(fp) == EOF) {
735                 FILE_OP_ERROR(tmp, "fclose");
736                 g_unlink(tmp);
737                 return -1;
738         }
739
740         /* put it in queue */
741         queue = account_get_special_folder(account, F_QUEUE);
742         if (!queue) queue = folder_get_default_queue();
743         if (!queue) {
744                 g_warning("can't find queue folder\n");
745                 g_unlink(tmp);
746                 return -1;
747         }
748         folder_item_scan(queue);
749         if ((num = folder_item_add_msg(queue, tmp, NULL, TRUE)) < 0) {
750                 g_warning("can't queue the message\n");
751                 g_unlink(tmp);
752                 return -1;
753         }
754                 
755         if (prefs_common.work_offline && 
756             !inc_offline_should_override(TRUE,
757                 _("Claws Mail needs network access in order "
758                   "to send this email.")))
759                 return 0;
760
761         /* send it */
762         path = folder_item_fetch_msg(queue, num);
763         ok = procmsg_send_message_queue(path, &foo, queue, num, &queued_removed);
764         g_free(path);
765         g_free(foo);
766         if (ok == 0 && !queued_removed)
767                 folder_item_remove_msg(queue, num);
768
769         return ok;
770 }
771
772 static gboolean find_encrypted_func(GNode *node, gpointer data)
773 {
774         MimeInfo *mimeinfo = (MimeInfo *) node->data;
775         MimeInfo **encinfo = (MimeInfo **) data;
776         
777         if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
778                 *encinfo = mimeinfo;
779                 return TRUE;
780         }
781         
782         return FALSE;
783 }
784
785 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
786 {
787         MimeInfo *encinfo = NULL;
788
789         g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
790                 find_encrypted_func, &encinfo);
791         
792         return encinfo;
793 }
794
795 gint messageview_show(MessageView *messageview, MsgInfo *msginfo,
796                       gboolean all_headers)
797 {
798         gchar *file;
799         MimeInfo *mimeinfo, *encinfo;
800         gchar *subject = NULL;
801         g_return_val_if_fail(msginfo != NULL, -1);
802
803         if (messageview->mimeview->textview &&
804             messageview->mimeview->textview->loading) {
805                 messageview->mimeview->textview->stop_loading = TRUE;
806                 return 0;
807         }
808
809         if (messageview->toolbar)
810                 toolbar_set_learn_button
811                         (messageview->toolbar,
812                          MSG_IS_SPAM(msginfo->flags)?LEARN_HAM:LEARN_SPAM);
813         else
814                 toolbar_set_learn_button
815                         (messageview->mainwin->toolbar,
816                          MSG_IS_SPAM(msginfo->flags)?LEARN_HAM:LEARN_SPAM);
817
818         if (messageview->toolbar) {
819                 if (messageview->toolbar->learn_spam_btn)
820                         gtk_widget_set_sensitive(
821                                 messageview->toolbar->learn_spam_btn, 
822                                 procmsg_spam_can_learn());
823         }
824         messageview->updating = TRUE;
825         mimeinfo = procmime_scan_message(msginfo);
826         messageview->updating = FALSE;
827         
828         if (messageview->deferred_destroy) {
829                 messageview_destroy(messageview);
830                 return 0;
831         }
832
833         if (!mimeinfo) {
834                 textview_show_error(messageview->mimeview->textview);
835                 return -1;
836         }
837
838         while ((encinfo = find_encrypted_part(mimeinfo)) != NULL) {
839                 debug_print("decrypting message part\n");
840                 if (privacy_mimeinfo_decrypt(encinfo) < 0) {
841                         alertpanel_error(_("Couldn't decrypt: %s"),
842                                 privacy_get_error());
843                         break;
844                 }
845         }
846         
847         messageview->updating = TRUE;
848         file = procmsg_get_message_file_path(msginfo);
849         messageview->updating = FALSE;
850         
851         if (messageview->deferred_destroy) {
852                 g_free(file);
853                 messageview_destroy(messageview);
854                 return 0;
855         }
856
857         if (!file) {
858                 g_warning("can't get message file path.\n");
859                 procmime_mimeinfo_free_all(mimeinfo);
860                 textview_show_error(messageview->mimeview->textview);
861                 return -1;
862         }
863         
864         if (messageview->msginfo != msginfo) {
865                 procmsg_msginfo_free(messageview->msginfo);
866                 messageview->msginfo = NULL;
867                 messageview_set_menu_sensitive(messageview);
868                 messageview->msginfo = procmsg_msginfo_get_full_info(msginfo);
869                 if (!messageview->msginfo)
870                         messageview->msginfo = procmsg_msginfo_copy(msginfo);
871         } else {
872                 messageview->msginfo = NULL;
873                 messageview_set_menu_sensitive(messageview);
874                 messageview->msginfo = msginfo;
875         }
876         headerview_show(messageview->headerview, messageview->msginfo);
877
878         messageview_set_position(messageview, 0);
879
880         textview_set_all_headers(messageview->mimeview->textview, 
881                         messageview->all_headers);
882
883 #ifdef MAEMO
884         maemo_window_full_screen_if_needed(GTK_WINDOW(messageview->window));
885 #endif
886         if (messageview->window) {
887                 gtk_window_set_title(GTK_WINDOW(messageview->window), 
888                                 _("Claws Mail - Message View"));
889                 GTK_EVENTS_FLUSH();
890         }
891         mimeview_show_message(messageview->mimeview, mimeinfo, file);
892         
893 #ifndef MAEMO
894         messageview_set_position(messageview, 0);
895 #endif
896
897         if (messageview->window && msginfo->subject) {
898                 subject = g_strdup(msginfo->subject);
899                 if (!g_utf8_validate(subject, -1, NULL)) {
900                         g_free(subject);
901                         subject = g_malloc(strlen(msginfo->subject)*2 +1);
902                         conv_localetodisp(subject, strlen(msginfo->subject)*2 +1, 
903                                 msginfo->subject);
904                 }
905                 if (g_utf8_validate(subject, -1, NULL))
906                         gtk_window_set_title(GTK_WINDOW(messageview->window), 
907                                 subject);
908                 g_free(subject);
909         }
910
911         if (msginfo && msginfo->folder) {
912                 msginfo->folder->last_seen = msginfo->msgnum;   
913         }
914
915         main_create_mailing_list_menu(messageview->mainwin, messageview->msginfo);
916
917         if (messageview->msginfo && messageview->msginfo->extradata
918             && messageview->msginfo->extradata->partial_recv)
919                 partial_recv_show(messageview->noticeview, 
920                                   messageview->msginfo);
921         else if (messageview->msginfo && messageview->msginfo->extradata &&
922             (messageview->msginfo->extradata->dispositionnotificationto || 
923              messageview->msginfo->extradata->returnreceiptto) &&
924             !MSG_IS_RETRCPT_SENT(messageview->msginfo->flags) &&
925             !prefs_common.never_send_retrcpt)
926                 return_receipt_show(messageview->noticeview, 
927                                     messageview->msginfo);
928         else 
929                 noticeview_hide(messageview->noticeview);
930
931         mimeinfo = procmime_mimeinfo_next(mimeinfo);
932         if (!all_headers && mimeinfo 
933                         && (mimeinfo->type != MIMETYPE_TEXT || 
934             strcasecmp(mimeinfo->subtype, "plain")) 
935                         && (mimeinfo->type != MIMETYPE_MULTIPART || 
936             strcasecmp(mimeinfo->subtype, "signed"))) {
937                 if (strcasecmp(mimeinfo->subtype, "html"))
938                         mimeview_show_part(messageview->mimeview,mimeinfo);
939                 else if (prefs_common.invoke_plugin_on_html)
940                         mimeview_select_mimepart_icon(messageview->mimeview,mimeinfo);
941         }
942
943         g_free(file);
944
945         return 0;
946 }
947
948 void messageview_reflect_prefs_pixmap_theme(void)
949 {
950         GList *cur;
951         MessageView *msgview;
952
953         for (cur = msgview_list; cur != NULL; cur = cur->next) {
954                 msgview = (MessageView*)cur->data;
955                 toolbar_update(TOOLBAR_MSGVIEW, msgview);
956                 mimeview_update(msgview->mimeview);
957         }
958 }
959
960 void messageview_clear(MessageView *messageview)
961 {
962         if (!messageview)
963                 return;
964         procmsg_msginfo_free(messageview->msginfo);
965         messageview->msginfo = NULL;
966         messageview->filtered = FALSE;
967         mimeview_clear(messageview->mimeview);
968         headerview_clear(messageview->headerview);
969         noticeview_hide(messageview->noticeview);
970 }
971
972 void messageview_destroy(MessageView *messageview)
973 {
974         debug_print("destroy messageview\n");
975         messageview_list = g_list_remove(messageview_list, messageview);
976
977         if (messageview->mainwin->summaryview->messageview == messageview) {
978                 messageview->mainwin->summaryview->displayed = NULL;
979                 messageview->mainwin->summaryview->messageview = NULL;
980         }
981         if (messageview->mainwin->summaryview->ext_messageview == messageview) {
982                 messageview->mainwin->summaryview->displayed = NULL;
983                 messageview->mainwin->summaryview->ext_messageview = NULL;
984         }
985         if (!messageview->deferred_destroy) {
986                 hooks_unregister_hook(MSGINFO_UPDATE_HOOKLIST,
987                               messageview->msginfo_update_callback_id);
988         }
989
990         if (messageview->updating) {
991                 debug_print("uh oh, better not touch that now (fetching)\n");
992                 messageview->deferred_destroy = TRUE;
993                 gtk_widget_hide(messageview->window);
994                 return;
995         }
996         
997         if (messageview->mimeview->textview
998         &&  messageview->mimeview->textview->loading) {
999                 debug_print("uh oh, better not touch that now (loading text)\n");
1000                 messageview->deferred_destroy = TRUE;
1001                 messageview->mimeview->textview->stop_loading = TRUE;
1002                 gtk_widget_hide(messageview->window);
1003                 return;
1004         }
1005
1006         headerview_destroy(messageview->headerview);
1007         mimeview_destroy(messageview->mimeview);
1008         noticeview_destroy(messageview->noticeview);
1009
1010         procmsg_msginfo_free(messageview->msginfo);
1011         toolbar_clear_list(TOOLBAR_MSGVIEW);
1012         if (messageview->toolbar) {
1013                 toolbar_destroy(messageview->toolbar);
1014                 g_free(messageview->toolbar);
1015         }
1016         
1017         msgview_list = g_list_remove(msgview_list, messageview); 
1018
1019         if (messageview->window)
1020                 gtk_widget_destroy(messageview->window);
1021         g_free(messageview);
1022 }
1023
1024 void messageview_delete(MessageView *msgview)
1025 {
1026         MsgInfo *msginfo = NULL;
1027         FolderItem *trash = NULL;
1028         PrefsAccount *ac = NULL;
1029
1030         if (msgview->msginfo && msgview->mainwin && msgview->mainwin->summaryview)
1031                 msginfo = summary_get_selected_msg(msgview->mainwin->summaryview);
1032         
1033         /* need a procmsg_msginfo_equal() */
1034         if (msginfo && msgview->msginfo && 
1035             msginfo->msgnum == msgview->msginfo->msgnum && 
1036             msginfo->folder == msgview->msginfo->folder) {
1037                 summary_delete_trash(msgview->mainwin->summaryview);
1038         } else {                
1039                 msginfo = msgview->msginfo;
1040
1041                 g_return_if_fail(msginfo != NULL);
1042
1043                 /* to get the trash folder, we have to choose either
1044                  * the folder's or account's trash default - we prefer
1045                  * the one in the account prefs */
1046                 if (msginfo->folder) {
1047                         if (NULL != (ac = account_find_from_item(msginfo->folder)))
1048                                 trash = account_get_special_folder(ac, F_TRASH);
1049                         if (!trash && msginfo->folder->folder)  
1050                                 trash = msginfo->folder->folder->trash;
1051                         /* if still not found, use the default */
1052                         if (!trash) 
1053                                 trash = folder_get_default_trash();
1054                 }       
1055
1056                 g_return_if_fail(trash != NULL);
1057
1058                 if (prefs_common.immediate_exec)
1059                         /* TODO: Delete from trash */
1060                         folder_item_move_msg(trash, msginfo);
1061                 else {
1062                         procmsg_msginfo_set_to_folder(msginfo, trash);
1063                         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
1064                         /* NOTE: does not update to next message in summaryview */
1065                 }
1066         }
1067 #ifdef MAEMO
1068         if (msgview->window) {
1069                 messageview_destroy(msgview);
1070         }
1071 #endif
1072 }
1073
1074 /* 
1075  * \brief update messageview with currently selected message in summaryview
1076  *        leave unchanged if summaryview is empty
1077  * \param pointer to MessageView
1078  */     
1079 static void messageview_update(MessageView *msgview, MsgInfo *old_msginfo)
1080 {
1081         SummaryView *summaryview = (SummaryView*)msgview->mainwin->summaryview;
1082
1083         g_return_if_fail(summaryview != NULL);
1084         
1085         if (summaryview->selected) {
1086                 MsgInfo *msginfo = summary_get_selected_msg(summaryview);
1087                 if (msginfo == NULL || msginfo == old_msginfo)
1088                         return;
1089
1090                 messageview_show(msgview, msginfo, 
1091                                  msgview->all_headers);
1092         } 
1093 }
1094
1095 void messageview_quote_color_set(void)
1096 {
1097 }
1098
1099 void messageview_set_font(MessageView *messageview)
1100 {
1101 }
1102
1103 TextView *messageview_get_current_textview(MessageView *messageview)
1104 {
1105         TextView *text = NULL;
1106
1107         text = messageview->mimeview->textview;
1108
1109         return text;
1110 }
1111
1112 MimeInfo *messageview_get_selected_mime_part(MessageView *messageview)
1113 {
1114         return mimeview_get_selected_part(messageview->mimeview);
1115 }
1116
1117 void messageview_copy_clipboard(MessageView *messageview)
1118 {
1119         gchar *text = messageview_get_selection(messageview);
1120         if (text) {
1121                 gtk_clipboard_set_text(
1122                         gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
1123                         text, -1);
1124         }
1125         g_free(text);
1126 }
1127
1128 void messageview_select_all(MessageView *messageview)
1129 {
1130         TextView *text;
1131
1132         text = messageview_get_current_textview(messageview);
1133         if (text) {
1134                 GtkTextView *textview = GTK_TEXT_VIEW(text->text);
1135                 GtkTextBuffer *buffer;
1136                 GtkTextIter start, end;
1137
1138                 buffer = gtk_text_view_get_buffer(textview);
1139                 gtk_text_buffer_get_bounds(buffer, &start, &end);
1140                 gtk_text_buffer_select_range(buffer, &start, &end);
1141         }
1142 }
1143
1144 void messageview_set_position(MessageView *messageview, gint pos)
1145 {
1146         TextView *text;
1147
1148         text = messageview_get_current_textview(messageview);
1149         if (text)
1150                 textview_set_position(text, pos);
1151 }
1152
1153 gboolean messageview_search_string(MessageView *messageview, const gchar *str,
1154                                    gboolean case_sens)
1155 {
1156         TextView *text;
1157
1158         if (messageview->mimeview->type == MIMEVIEW_VIEWER) {
1159                 MimeViewer *viewer = messageview->mimeview->mimeviewer;
1160                 if (viewer && viewer->text_search) {
1161                         return viewer->text_search(viewer, FALSE, str, case_sens);
1162                 }
1163         }
1164
1165         text = messageview_get_current_textview(messageview);
1166         if (text)
1167                 return textview_search_string(text, str, case_sens);
1168         return FALSE;
1169 }
1170
1171 gboolean messageview_search_string_backward(MessageView *messageview,
1172                                             const gchar *str,
1173                                             gboolean case_sens)
1174 {
1175         TextView *text;
1176
1177         if (messageview->mimeview->type == MIMEVIEW_VIEWER) {
1178                 MimeViewer *viewer = messageview->mimeview->mimeviewer;
1179                 if (viewer && viewer->text_search) {
1180                         return viewer->text_search(viewer, TRUE, str, case_sens);
1181                 }
1182         }
1183
1184         text = messageview_get_current_textview(messageview);
1185         if (text)       
1186                 return textview_search_string_backward(text,
1187                                                        str, case_sens);
1188         return FALSE;
1189 }
1190
1191 gboolean messageview_is_visible(MessageView *messageview)
1192 {
1193         if (messageview == NULL)
1194                 return FALSE;
1195         return messageview->visible;
1196 }
1197
1198 static void messageview_save_as(MessageView *messageview)
1199 {
1200         gchar *filename = NULL;
1201         MsgInfo *msginfo;
1202         gchar *src, *dest, *tmp;
1203
1204         if (!messageview->msginfo) return;
1205         msginfo = messageview->msginfo;
1206
1207         if (msginfo->subject) {
1208                 Xstrdup_a(filename, msginfo->subject, return);
1209                 subst_for_filename(filename);
1210         }
1211         if (filename && !g_utf8_validate(filename, -1, NULL)) {
1212                 gchar *oldstr = filename;
1213                 filename = conv_codeset_strdup(filename,
1214                                                conv_get_locale_charset_str(),
1215                                                CS_UTF_8);
1216                 if (!filename) {
1217                         g_warning("messageview_save_as(): failed to convert character set.");
1218                         filename = g_strdup(oldstr);
1219                 }
1220                 dest = filesel_select_file_save(_("Save as"), filename);
1221                 g_free(filename);
1222         } else
1223                 dest = filesel_select_file_save(_("Save as"), filename);
1224         if (!dest) return;
1225         if (is_file_exist(dest)) {
1226                 AlertValue aval;
1227
1228                 aval = alertpanel(_("Overwrite"),
1229                                   _("Overwrite existing file?"),
1230                                   GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL);
1231                 if (G_ALERTALTERNATE != aval) return;
1232         }
1233
1234         src = procmsg_get_message_file(msginfo);
1235         if (copy_file(src, dest, TRUE) < 0) {
1236                 tmp =  g_path_get_basename(dest);
1237                 alertpanel_error(_("Couldn't save the file '%s'."), tmp);
1238                 g_free(tmp);
1239         }
1240         g_free(dest);
1241         g_free(src);
1242 }
1243
1244 static gint messageview_delete_cb(GtkWidget *widget, GdkEventAny *event,
1245                                   MessageView *messageview)
1246 {
1247         messageview_destroy(messageview);
1248         return TRUE;
1249 }
1250
1251 static void messageview_size_allocate_cb(GtkWidget *widget,
1252                                          GtkAllocation *allocation)
1253 {
1254         g_return_if_fail(allocation != NULL);
1255
1256         prefs_common.msgwin_width  = allocation->width;
1257         prefs_common.msgwin_height = allocation->height;
1258 }
1259
1260 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
1261                         MessageView *messageview)
1262 {
1263         if (event && event->keyval == GDK_Escape && messageview->window) {
1264                 messageview_destroy(messageview);
1265                 return TRUE;
1266         }
1267
1268         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0)
1269                 return FALSE;
1270
1271         g_signal_stop_emission_by_name(G_OBJECT(widget),
1272                                         "key_press_event");
1273         mimeview_pass_key_press_event(messageview->mimeview, event);
1274         return FALSE;
1275 }
1276
1277 static void return_receipt_show(NoticeView *noticeview, MsgInfo *msginfo)
1278 {
1279         gchar *addr = NULL;
1280         gboolean from_me = FALSE;
1281         if (msginfo->folder 
1282                 && (folder_has_parent_of_type(msginfo->folder, F_QUEUE)
1283                  || folder_has_parent_of_type(msginfo->folder, F_DRAFT)))
1284                 return;
1285
1286         addr = g_strdup(msginfo->from);
1287         if (addr) {
1288                 extract_address(addr);
1289                 if (account_find_from_address(addr)) {
1290                         from_me = TRUE;
1291                 }
1292                 g_free(addr);
1293         }
1294
1295         if (from_me) {
1296                 noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
1297                 noticeview_set_text(noticeview, _("You asked for a return receipt in this message."));
1298                 noticeview_set_button_text(noticeview, NULL);
1299                 noticeview_set_button_press_callback(noticeview, NULL, NULL);
1300         } else {
1301                 noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
1302                 noticeview_set_text(noticeview, _("This message asks for a return receipt."));
1303                 noticeview_set_button_text(noticeview, _("Send receipt"));
1304                 noticeview_set_button_press_callback(noticeview,
1305                                                      G_CALLBACK(return_receipt_send_clicked),
1306                                                      (gpointer) msginfo);
1307         }
1308         noticeview_show(noticeview);
1309 }
1310
1311 static void return_receipt_send_clicked(NoticeView *noticeview, MsgInfo *msginfo)
1312 {
1313         MsgInfo *tmpmsginfo;
1314         gchar *file;
1315
1316         file = procmsg_get_message_file_path(msginfo);
1317         if (!file) {
1318                 g_warning("can't get message file path.\n");
1319                 return;
1320         }
1321
1322         tmpmsginfo = procheader_parse_file(file, msginfo->flags, TRUE, TRUE);
1323         tmpmsginfo->folder = msginfo->folder;
1324         tmpmsginfo->msgnum = msginfo->msgnum;
1325
1326         if (disposition_notification_send(tmpmsginfo) >= 0) {
1327                 procmsg_msginfo_set_flags(msginfo, MSG_RETRCPT_SENT, 0);
1328                 noticeview_hide(noticeview);
1329         }               
1330
1331         procmsg_msginfo_free(tmpmsginfo);
1332         g_free(file);
1333 }
1334
1335 static void partial_recv_show(NoticeView *noticeview, MsgInfo *msginfo)
1336 {
1337         gchar *text = NULL;
1338         gchar *button1 = NULL;
1339         gchar *button2 = NULL;
1340         void  *button1_cb = NULL;
1341         void  *button2_cb = NULL;
1342
1343         if (!msginfo->extradata)
1344                 return;
1345         if (!partial_msg_in_uidl_list(msginfo)) {
1346                 text = g_strdup_printf(_("This message has been partially "
1347                                 "retrieved,\nand has been deleted from the "
1348                                 "server."));
1349         } else {
1350                 switch (msginfo->planned_download) {
1351                 case POP3_PARTIAL_DLOAD_UNKN:
1352                         text = g_strdup_printf(_("This message has been "
1353                                         "partially retrieved;\nit is %s."),
1354                                         to_human_readable(
1355                                                 (off_t)(msginfo->total_size)));
1356                         button1 = _("Mark for download");
1357                         button2 = _("Mark for deletion");
1358                         button1_cb = partial_recv_dload_clicked;
1359                         button2_cb = partial_recv_del_clicked;
1360                         break;
1361                 case POP3_PARTIAL_DLOAD_DLOAD:
1362                         text = g_strdup_printf(_("This message has been "
1363                                         "partially retrieved;\nit is %s and "
1364                                         "will be downloaded."),
1365                                         to_human_readable(
1366                                                 (off_t)(msginfo->total_size)));
1367                         button1 = _("Unmark");
1368                         button1_cb = partial_recv_unmark_clicked;
1369                         button2 = _("Mark for deletion");
1370                         button2_cb = partial_recv_del_clicked;
1371                         break;
1372                 case POP3_PARTIAL_DLOAD_DELE:
1373                         text = g_strdup_printf(_("This message has been "
1374                                         "partially retrieved;\nit is %s and "
1375                                         "will be deleted."),
1376                                         to_human_readable(
1377                                                 (off_t)(msginfo->total_size)));
1378                         button1 = _("Mark for download");
1379                         button1_cb = partial_recv_dload_clicked;
1380                         button2 = _("Unmark");
1381                         button2_cb = partial_recv_unmark_clicked;
1382                         break;
1383                 default:
1384                         return;
1385                 }
1386         }
1387         
1388         noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
1389         noticeview_set_text(noticeview, text);
1390         g_free(text);
1391         noticeview_set_button_text(noticeview, button1);
1392         noticeview_set_button_press_callback(noticeview,
1393                      G_CALLBACK(button1_cb), (gpointer) msginfo);
1394
1395         noticeview_set_2ndbutton_text(noticeview, button2);
1396         noticeview_set_2ndbutton_press_callback(noticeview,
1397                      G_CALLBACK(button2_cb), (gpointer) msginfo);
1398
1399         noticeview_show(noticeview);
1400 }
1401
1402 static void partial_recv_dload_clicked(NoticeView *noticeview, 
1403                                        MsgInfo *msginfo)
1404 {
1405         if (partial_mark_for_download(msginfo) == 0) {
1406                 partial_recv_show(noticeview, msginfo);
1407         }
1408 }
1409
1410 static void partial_recv_del_clicked(NoticeView *noticeview, 
1411                                        MsgInfo *msginfo)
1412 {
1413         if (partial_mark_for_delete(msginfo) == 0) {
1414                 partial_recv_show(noticeview, msginfo);
1415         }
1416 }
1417
1418 static void partial_recv_unmark_clicked(NoticeView *noticeview, 
1419                                        MsgInfo *msginfo)
1420 {
1421         if (partial_unmark(msginfo) == 0) {
1422                 partial_recv_show(noticeview, msginfo);
1423         }
1424 }
1425
1426 static void select_account_cb(GtkWidget *w, gpointer data)
1427 {
1428         *(gint*)data = combobox_get_active_data(GTK_COMBO_BOX(w));
1429 }
1430
1431 static PrefsAccount *select_account_from_list(GList *ac_list)
1432 {
1433         GtkWidget *optmenu;
1434         gint account_id;
1435
1436         g_return_val_if_fail(ac_list != NULL, NULL);
1437         g_return_val_if_fail(ac_list->data != NULL, NULL);
1438         
1439         optmenu = gtkut_account_menu_new(ac_list,
1440                         G_CALLBACK(select_account_cb),
1441                         &account_id);
1442         if (!optmenu)
1443                 return NULL;
1444         account_id = ((PrefsAccount *) ac_list->data)->account_id;
1445         if (alertpanel_with_widget(
1446                                 _("Return Receipt Notification"),
1447                                 _("The message was sent to several of your "
1448                                   "accounts.\n"
1449                                   "Please choose which account do you want to "
1450                                   "use for sending the receipt notification:"),
1451                                 _("_Cancel"), _("_Send Notification"), NULL,
1452                                 FALSE, G_ALERTDEFAULT, optmenu) != G_ALERTALTERNATE)
1453                 return NULL;
1454         return account_find_from_id(account_id);
1455 }
1456
1457 /* 
1458  * \brief return selected messageview text, when nothing is 
1459  *        selected and message was filtered, return complete text
1460  *
1461  * \param  pointer to Messageview 
1462  *
1463  * \return pointer to text (needs to be free'd by calling func)
1464  */
1465 gchar *messageview_get_selection(MessageView *msgview)
1466 {
1467         TextView *textview;
1468         gchar *text = NULL;
1469         GtkTextView *edit = NULL;
1470         GtkTextBuffer *textbuf;
1471         gint body_pos = 0;
1472         
1473         g_return_val_if_fail(msgview != NULL, NULL);
1474
1475         if (msgview->mimeview->type == MIMEVIEW_VIEWER) {
1476                 MimeViewer *viewer = msgview->mimeview->mimeviewer;
1477                 if (viewer && viewer->get_selection) {
1478                         text = viewer->get_selection(viewer);
1479                         if (text)
1480                                 return text;
1481                 }
1482         }
1483
1484         textview = messageview_get_current_textview(msgview);
1485         g_return_val_if_fail(textview != NULL, NULL);
1486
1487         edit = GTK_TEXT_VIEW(textview->text);
1488         g_return_val_if_fail(edit != NULL, NULL);
1489         body_pos = textview->body_pos;
1490
1491         textbuf = gtk_text_view_get_buffer(edit);
1492
1493         if (gtk_text_buffer_get_selection_bounds(textbuf, NULL, NULL))
1494                 return gtkut_text_view_get_selection(edit);
1495         else if (msgview->filtered) {
1496                 GtkTextIter start_iter, end_iter;
1497                 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter, body_pos);
1498                 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
1499                 gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter, FALSE);
1500         } else
1501                 text = NULL;
1502
1503         return text;
1504 }
1505
1506 static void save_as_cb(gpointer data, guint action, GtkWidget *widget)
1507 {
1508         MessageView *messageview = (MessageView *)data;
1509         messageview_save_as(messageview);
1510 }
1511
1512 #if defined(USE_GNOMEPRINT) || GTK_CHECK_VERSION(2,10,0)
1513 static void print_mimeview(MimeView *mimeview, gint sel_start, gint sel_end, gint partnum) 
1514 {
1515 #if !defined(USE_GNOMEPRINT) && GTK_CHECK_VERSION(2,10,0)
1516         MainWindow *mainwin;
1517 #endif
1518         if (!mimeview 
1519         ||  !mimeview->textview
1520         ||  !mimeview->textview->text)
1521                 alertpanel_warning(_("Cannot print: the message doesn't "
1522                                      "contain text."));
1523         else {
1524                 gtk_widget_realize(mimeview->textview->text);
1525                 if (partnum > 0) {
1526                         mimeview_select_part_num(mimeview, partnum);
1527                 }
1528                 if (mimeview->type == MIMEVIEW_VIEWER) {
1529                         MimeViewer *viewer = mimeview->mimeviewer;
1530                         if (viewer && viewer->print) {
1531                                 viewer->print(viewer);
1532                                 return;
1533                         }
1534                 }
1535                 if (sel_start != -1 && sel_end != -1) {
1536                         GtkTextIter start, end;
1537                         GtkTextView *text = GTK_TEXT_VIEW(mimeview->textview->text);
1538                         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
1539
1540                         gtk_text_buffer_get_iter_at_offset(buffer, &start, sel_start);
1541                         gtk_text_buffer_get_iter_at_offset(buffer, &end, sel_end);
1542                         gtk_text_buffer_select_range(buffer, &start, &end);
1543                 }
1544 #if defined(USE_GNOMEPRINT)
1545                 gedit_print(GTK_TEXT_VIEW(mimeview->textview->text));
1546 #else
1547                 /* TODO: Get the real parent window, not the main window */
1548                 mainwin = mainwindow_get_mainwindow();
1549                 printing_print(GTK_TEXT_VIEW(mimeview->textview->text),
1550                                mainwin ? GTK_WINDOW(mainwin->window) : NULL,
1551                                 sel_start, sel_end);
1552 #endif
1553         }
1554 }
1555
1556 void messageview_print(MsgInfo *msginfo, gboolean all_headers, 
1557                         gint sel_start, gint sel_end, gint partnum) 
1558 {
1559         PangoFontDescription *font_desc = NULL;
1560         MessageView *tmpview = messageview_create_with_new_window_visible(
1561                                 mainwindow_get_mainwindow(), FALSE);
1562
1563         if (prefs_common.use_different_print_font) {
1564                 font_desc = pango_font_description_from_string
1565                                                 (prefs_common.printfont);
1566         } else {
1567                 font_desc = pango_font_description_from_string
1568                                                 (prefs_common.textfont);
1569         }
1570         if (font_desc) {
1571                 gtk_widget_modify_font(tmpview->mimeview->textview->text, 
1572                         font_desc);
1573                 pango_font_description_free(font_desc);
1574         }
1575
1576         tmpview->all_headers = all_headers;
1577         if (msginfo && messageview_show(tmpview, msginfo, 
1578                 tmpview->all_headers) >= 0) {
1579                         print_mimeview(tmpview->mimeview, 
1580                                 sel_start, sel_end, partnum);
1581         }
1582         messageview_destroy(tmpview);
1583 }
1584 #endif
1585
1586 #if GTK_CHECK_VERSION(2,10,0) && !defined(USE_GNOMEPRINT)
1587 static void page_setup_cb(gpointer data, guint action, GtkWidget *widget)
1588 {
1589         MessageView *messageview = (MessageView *)data;
1590         printing_page_setup(messageview ?
1591                             GTK_WINDOW(messageview->window) : NULL);
1592 }
1593 #endif
1594
1595 static void print_cb(gpointer data, guint action, GtkWidget *widget)
1596 {
1597         MessageView *messageview = (MessageView *)data;
1598 #if !defined(USE_GNOMEPRINT) && !GTK_CHECK_VERSION(2,10,0)
1599         gchar *cmdline = NULL;
1600         gchar *p;
1601 #else
1602         gint sel_start = -1, sel_end = -1, partnum = 0;
1603 #endif
1604
1605         if (!messageview->msginfo) return;
1606
1607 #if !defined(USE_GNOMEPRINT) && !GTK_CHECK_VERSION(2,10,0)
1608         cmdline = input_dialog(_("Print"),
1609                                _("Ente the print command line:\n"
1610                                  "('%s' will be replaced with file name)"),
1611                                prefs_common.print_cmd);
1612         if (!cmdline) return;
1613         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
1614             strchr(p + 2, '%')) {
1615                 alertpanel_error(_("Print command line is invalid:\n'%s'"),
1616                                  cmdline);
1617                 g_free(cmdline);
1618                 return;
1619         }
1620         procmsg_print_message(messageview->msginfo, cmdline);
1621         g_free(cmdline);
1622 #else
1623         partnum = mimeview_get_selected_part_num(messageview->mimeview);
1624         textview_get_selection_offsets(messageview->mimeview->textview,
1625                 &sel_start, &sel_end);
1626         messageview_print(messageview->msginfo, messageview->all_headers, 
1627                 sel_start, sel_end, partnum);
1628 #endif
1629 }
1630
1631 static void close_cb(gpointer data, guint action, GtkWidget *widget)
1632 {
1633         MessageView *messageview = (MessageView *)data;
1634         messageview_destroy(messageview);
1635 }
1636
1637 static void copy_cb(gpointer data, guint action, GtkWidget *widget)
1638 {
1639         MessageView *messageview = (MessageView *)data;
1640         messageview_copy_clipboard(messageview);
1641 }
1642
1643 static void allsel_cb(gpointer data, guint action, GtkWidget *widget)
1644 {
1645         MessageView *messageview = (MessageView *)data;
1646         messageview_select_all(messageview);
1647 }
1648
1649 static void search_cb(gpointer data, guint action, GtkWidget *widget)
1650 {
1651         MessageView *messageview = (MessageView *)data;
1652         message_search(messageview);
1653 }
1654
1655 static void set_charset_cb(gpointer data, guint action, GtkWidget *widget)
1656 {
1657         MessageView *messageview = (MessageView *)data;
1658         const gchar *charset;
1659
1660         if (GTK_CHECK_MENU_ITEM(widget)->active) {
1661                 charset = conv_get_charset_str((CharSet)action);
1662                 g_free(messageview->forced_charset);
1663                 messageview->forced_charset = g_strdup(charset);
1664                 procmime_force_charset(charset);
1665                 
1666                 messageview_show(messageview, messageview->msginfo, FALSE);
1667         }
1668 }
1669
1670 static void set_decode_cb(gpointer data, guint action, GtkWidget *widget)
1671 {
1672         MessageView *messageview = (MessageView *)data;
1673         if (GTK_CHECK_MENU_ITEM(widget)->active) {
1674                 messageview->forced_encoding = (EncodingType)action;
1675
1676                 messageview_show(messageview, messageview->msginfo, FALSE);
1677                 
1678                 debug_print("forced encoding: %d\n", action);
1679         }
1680 }
1681
1682
1683 static void view_source_cb(gpointer data, guint action, GtkWidget *widget)
1684 {
1685         MessageView *messageview = (MessageView *)data;
1686         SourceWindow *srcwin;
1687
1688         if (!messageview->msginfo) return;
1689
1690         srcwin = source_window_create();
1691         source_window_show_msg(srcwin, messageview->msginfo);
1692         source_window_show(srcwin);
1693 }
1694
1695 static void show_all_header_cb(gpointer data, guint action, GtkWidget *widget)
1696 {
1697         MessageView *messageview = (MessageView *)data;
1698         MsgInfo *msginfo = messageview->msginfo;
1699
1700         if (messageview->mimeview->textview &&
1701             messageview->mimeview->textview->loading) {
1702                 return;
1703         }
1704         if (messageview->updating)
1705                 return;
1706
1707         messageview->all_headers = 
1708                         GTK_CHECK_MENU_ITEM(widget)->active;
1709         if (!msginfo) return;
1710         messageview->msginfo = NULL;
1711         messageview_show(messageview, msginfo,
1712                          GTK_CHECK_MENU_ITEM(widget)->active);
1713         procmsg_msginfo_free(msginfo);
1714         main_window_set_menu_sensitive(messageview->mainwin);
1715 }
1716
1717 #define SET_CHECK_MENU_ACTIVE(path, active) \
1718 { \
1719         menuitem = gtk_item_factory_get_widget(ifactory, path); \
1720         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), active); \
1721 }
1722
1723 static void msg_hide_quotes_cb(gpointer data, guint action, GtkWidget *widget)
1724 {
1725         MessageView *messageview = (MessageView *)data;
1726         MsgInfo *msginfo = messageview->msginfo;
1727         static gboolean updating_menu = FALSE;
1728         GtkItemFactory *ifactory = gtk_item_factory_from_widget(messageview->menubar);
1729         GtkWidget *menuitem;
1730         if (updating_menu)
1731                 return;
1732
1733         prefs_common.hide_quotes = 
1734                         GTK_CHECK_MENU_ITEM(widget)->active ? action : 0;
1735         
1736         updating_menu=TRUE;
1737         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold all", FALSE);
1738         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 2", FALSE);
1739         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 3", FALSE);
1740         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), prefs_common.hide_quotes > 0);      
1741         updating_menu=FALSE;
1742         if (!msginfo) return;
1743         messageview->msginfo = NULL;
1744         messageview_show(messageview, msginfo,
1745                          messageview->all_headers);
1746         procmsg_msginfo_free(msginfo);
1747         
1748         /* update main window */
1749         main_window_set_menu_sensitive(messageview->mainwin);
1750         summary_redisplay_msg(messageview->mainwin->summaryview);
1751 }
1752 #undef SET_CHECK_MENU_ACTIVE
1753
1754 static void compose_cb(gpointer data, guint action, GtkWidget *widget)
1755 {
1756         MessageView *messageview = (MessageView *)data;
1757         PrefsAccount *ac = NULL;
1758         FolderItem *item = NULL;
1759
1760         if (messageview->msginfo)
1761                 item = messageview->msginfo->folder;
1762
1763         if (item) {
1764                 ac = account_find_from_item(item);
1765                 if (ac && ac->protocol == A_NNTP &&
1766                     item->stype == F_NEWS) {
1767                         compose_new(ac, item->path, NULL);
1768                         return;
1769                 }
1770         }
1771
1772         compose_new(ac, NULL, NULL);
1773 }
1774
1775 static void reply_cb(gpointer data, guint action, GtkWidget *widget)
1776 {
1777         MessageView *messageview = (MessageView *)data;
1778         GSList *msginfo_list = NULL;
1779
1780         g_return_if_fail(messageview->msginfo);
1781
1782         msginfo_list = g_slist_append(msginfo_list, messageview->msginfo);
1783         compose_reply_from_messageview(messageview, msginfo_list, action);
1784         g_slist_free(msginfo_list);
1785 }
1786
1787 static void addressbook_open_cb(gpointer data, guint action, GtkWidget *widget)
1788 {
1789         addressbook_open(NULL);
1790 }
1791
1792 static void add_address_cb(gpointer data, guint action, GtkWidget *widget)
1793 {
1794         MessageView *messageview = (MessageView *)data;
1795         MsgInfo *msginfo;
1796         gchar *from;
1797
1798         if (!messageview->msginfo) return;
1799         msginfo = messageview->msginfo;
1800         Xstrdup_a(from, msginfo->from, return);
1801         eliminate_address_comment(from);
1802         extract_address(from);
1803         addressbook_add_contact(msginfo->fromname, from, NULL);
1804 }
1805
1806 static void create_filter_cb(gpointer data, guint action, GtkWidget *widget)
1807 {
1808         MessageView *messageview = (MessageView *)data;
1809         FolderItem * item;
1810         
1811         if (!messageview->msginfo) return;
1812         
1813         item = messageview->msginfo->folder;
1814         summary_msginfo_filter_open(item,  messageview->msginfo,
1815                                     (PrefsFilterType)action, 0);
1816 }
1817
1818 static void create_processing_cb(gpointer data, guint action,
1819                                  GtkWidget *widget)
1820 {
1821         MessageView *messageview = (MessageView *)data;
1822         FolderItem * item;
1823         
1824         if (!messageview->msginfo) return;
1825         
1826         item = messageview->msginfo->folder;
1827         summary_msginfo_filter_open(item,  messageview->msginfo,
1828                                     (PrefsFilterType)action, 1);
1829 }
1830
1831 static void open_urls_cb(gpointer data, guint action, GtkWidget *widget)
1832 {
1833         MessageView *messageview = (MessageView *)data;
1834         messageview_list_urls(messageview);
1835 }
1836
1837 static void about_cb(gpointer data, guint action, GtkWidget *widget)
1838 {
1839         about_show();
1840 }
1841
1842 static gboolean messageview_update_msg(gpointer source, gpointer data)
1843 {
1844         MsgInfoUpdate *msginfo_update = (MsgInfoUpdate *) source;
1845         MessageView *messageview = (MessageView *)data;
1846
1847         if (messageview->msginfo != msginfo_update->msginfo)
1848                 return FALSE;
1849
1850         if (msginfo_update->flags & MSGINFO_UPDATE_DELETED) {
1851                 MsgInfo *old_msginfo = messageview->msginfo;
1852                 messageview_clear(messageview);
1853                 messageview_update(messageview, old_msginfo);
1854         }
1855
1856         return FALSE;
1857 }
1858
1859 void messageview_set_menu_sensitive(MessageView *messageview)
1860 {
1861         GtkItemFactory *ifactory;
1862         GtkWidget *menuitem = NULL;
1863
1864         if (!messageview || !messageview->new_window) 
1865                 return;
1866         /* do some smart things */
1867         if (!messageview->menubar) return;
1868         ifactory = gtk_item_factory_from_widget(messageview->menubar);
1869         if (!ifactory) return;
1870
1871         if (prefs_common.hide_quotes) {
1872                 menuitem = NULL;
1873                 if (prefs_common.hide_quotes == 1)
1874                         menuitem = gtk_item_factory_get_widget(ifactory, 
1875                                         "/View/Quotes/Fold all");
1876                 if (prefs_common.hide_quotes == 2)
1877                         menuitem = gtk_item_factory_get_widget(ifactory, 
1878                                         "/View/Quotes/Fold from level 2");
1879                 if (prefs_common.hide_quotes == 3)
1880                         menuitem = gtk_item_factory_get_widget(ifactory, 
1881                                         "/View/Quotes/Fold from level 3");
1882                 gtk_check_menu_item_set_active
1883                         (GTK_CHECK_MENU_ITEM(menuitem),
1884                          TRUE);
1885         }
1886 }
1887
1888 void messageview_learn (MessageView *msgview, gboolean is_spam)
1889 {
1890         if (is_spam) {
1891                 if (procmsg_spam_learner_learn(msgview->msginfo, NULL, TRUE) == 0)
1892                         procmsg_msginfo_set_flags(msgview->msginfo, MSG_SPAM, 0);
1893                 else
1894                         log_error(LOG_PROTOCOL, _("An error happened while learning.\n"));
1895                 
1896         } else {
1897                 if (procmsg_spam_learner_learn(msgview->msginfo, NULL, FALSE) == 0)
1898                         procmsg_msginfo_unset_flags(msgview->msginfo, MSG_SPAM, 0);
1899                 else
1900                         log_error(LOG_PROTOCOL, _("An error happened while learning.\n"));
1901         }
1902         if (msgview->toolbar)
1903                 toolbar_set_learn_button
1904                         (msgview->toolbar,
1905                          MSG_IS_SPAM(msgview->msginfo->flags)?LEARN_HAM:LEARN_SPAM);
1906         else
1907                 toolbar_set_learn_button
1908                         (msgview->mainwin->toolbar,
1909                          MSG_IS_SPAM(msgview->msginfo->flags)?LEARN_HAM:LEARN_SPAM);
1910 }
1911
1912 void messageview_list_urls (MessageView *msgview)
1913 {
1914         GSList *cur = msgview->mimeview->textview->uri_list;
1915         GSList *newlist = NULL;
1916         for (; cur; cur = cur->next) {
1917                 ClickableText *uri = (ClickableText *)cur->data;
1918                 if (uri->uri &&
1919                     (!g_ascii_strncasecmp(uri->uri, "ftp.", 4) ||
1920                      !g_ascii_strncasecmp(uri->uri, "ftp:", 4) ||
1921                      !g_ascii_strncasecmp(uri->uri, "www.", 4) ||
1922                      !g_ascii_strncasecmp(uri->uri, "http:", 5) ||
1923                      !g_ascii_strncasecmp(uri->uri, "https:", 6)))
1924                         newlist = g_slist_prepend(newlist, uri);
1925         }
1926         newlist = g_slist_reverse(newlist);
1927         uri_opener_open(msgview, newlist);
1928         g_slist_free(newlist);
1929 }