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