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