5d78c36a5f03af7c85b2a19049bc091dbed1b4bf
[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         generate_msgid(buf, sizeof(buf));
844         if (fprintf(fp, "Message-ID: <%s>\n", buf) < 0) {
845                 fclose(fp);
846                 g_unlink(tmp);
847                 return -1;
848         }
849
850
851         if (fclose(fp) == EOF) {
852                 FILE_OP_ERROR(tmp, "fclose");
853                 g_unlink(tmp);
854                 return -1;
855         }
856
857         /* put it in queue */
858         queue = account_get_special_folder(account, F_QUEUE);
859         if (!queue) queue = folder_get_default_queue();
860         if (!queue) {
861                 g_warning("can't find queue folder\n");
862                 g_unlink(tmp);
863                 return -1;
864         }
865         folder_item_scan(queue);
866         if ((num = folder_item_add_msg(queue, tmp, NULL, TRUE)) < 0) {
867                 g_warning("can't queue the message\n");
868                 g_unlink(tmp);
869                 return -1;
870         }
871                 
872         if (prefs_common.work_offline && 
873             !inc_offline_should_override(TRUE,
874                 _("Claws Mail needs network access in order "
875                   "to send this email.")))
876                 return 0;
877
878         /* send it */
879         path = folder_item_fetch_msg(queue, num);
880         ok = procmsg_send_message_queue(path, &foo, queue, num, &queued_removed);
881         g_free(path);
882         g_free(foo);
883         if (ok == 0 && !queued_removed)
884                 folder_item_remove_msg(queue, num);
885
886         return ok;
887 }
888
889 static gboolean find_encrypted_func(GNode *node, gpointer data)
890 {
891         MimeInfo *mimeinfo = (MimeInfo *) node->data;
892         MimeInfo **encinfo = (MimeInfo **) data;
893         
894         if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
895                 *encinfo = mimeinfo;
896                 return TRUE;
897         }
898         
899         return FALSE;
900 }
901
902 static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
903 {
904         MimeInfo *encinfo = NULL;
905
906         g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
907                 find_encrypted_func, &encinfo);
908         
909         return encinfo;
910 }
911
912 gint messageview_show(MessageView *messageview, MsgInfo *msginfo,
913                       gboolean all_headers)
914 {
915         gchar *file;
916         MimeInfo *mimeinfo, *encinfo;
917         gchar *subject = NULL;
918         g_return_val_if_fail(msginfo != NULL, -1);
919
920         if (messageview->mimeview->textview &&
921             messageview->mimeview->textview->loading) {
922                 messageview->mimeview->textview->stop_loading = TRUE;
923                 return 0;
924         }
925
926         if (messageview->toolbar)
927                 toolbar_set_learn_button
928                         (messageview->toolbar,
929                          MSG_IS_SPAM(msginfo->flags)?LEARN_HAM:LEARN_SPAM);
930         else
931                 toolbar_set_learn_button
932                         (messageview->mainwin->toolbar,
933                          MSG_IS_SPAM(msginfo->flags)?LEARN_HAM:LEARN_SPAM);
934
935         if (messageview->toolbar) {
936                 if (messageview->toolbar->learn_spam_btn)
937                         gtk_widget_set_sensitive(
938                                 messageview->toolbar->learn_spam_btn, 
939                                 procmsg_spam_can_learn());
940         }
941         messageview->updating = TRUE;
942
943         file = procmsg_get_message_file_path(msginfo);
944         if (!file) {
945                 g_warning("can't get message file path.\n");
946                 textview_show_error(messageview->mimeview->textview);
947                 return -1;
948         }
949         
950         if (!folder_has_parent_of_type(msginfo->folder, F_QUEUE) &&
951             !folder_has_parent_of_type(msginfo->folder, F_DRAFT))
952                 mimeinfo = procmime_scan_file(file);
953         else
954                 mimeinfo = procmime_scan_queue_file(file);
955
956         messageview->updating = FALSE;
957         
958         if (messageview->deferred_destroy) {
959                 g_free(file);
960                 messageview_destroy(messageview);
961                 return 0;
962         }
963
964         if (!mimeinfo) {
965                 textview_show_error(messageview->mimeview->textview);
966                 return -1;
967         }
968
969         while ((encinfo = find_encrypted_part(mimeinfo)) != NULL) {
970                 debug_print("decrypting message part\n");
971                 if (privacy_mimeinfo_decrypt(encinfo) < 0) {
972                         alertpanel_error(_("Couldn't decrypt: %s"),
973                                 privacy_get_error());
974                         break;
975                 }
976         }
977                         
978         if (messageview->msginfo != msginfo) {
979                 procmsg_msginfo_free(messageview->msginfo);
980                 messageview->msginfo = NULL;
981                 messageview_set_menu_sensitive(messageview);
982                 messageview->msginfo = 
983                         procmsg_msginfo_get_full_info_from_file(msginfo, file);
984                 if (!messageview->msginfo)
985                         messageview->msginfo = procmsg_msginfo_copy(msginfo);
986         } else {
987                 messageview->msginfo = NULL;
988                 messageview_set_menu_sensitive(messageview);
989                 messageview->msginfo = msginfo;
990         }
991         headerview_show(messageview->headerview, messageview->msginfo);
992
993         messageview_set_position(messageview, 0);
994
995         textview_set_all_headers(messageview->mimeview->textview, 
996                         messageview->all_headers);
997
998 #ifdef MAEMO
999         maemo_window_full_screen_if_needed(GTK_WINDOW(messageview->window));
1000 #endif
1001         if (messageview->window) {
1002                 gtk_window_set_title(GTK_WINDOW(messageview->window), 
1003                                 _("Claws Mail - Message View"));
1004                 GTK_EVENTS_FLUSH();
1005         }
1006         mimeview_show_message(messageview->mimeview, mimeinfo, file);
1007         
1008 #ifndef MAEMO
1009         messageview_set_position(messageview, 0);
1010 #endif
1011
1012         if (messageview->window && msginfo->subject) {
1013                 subject = g_strdup(msginfo->subject);
1014                 if (!g_utf8_validate(subject, -1, NULL)) {
1015                         g_free(subject);
1016                         subject = g_malloc(strlen(msginfo->subject)*2 +1);
1017                         conv_localetodisp(subject, strlen(msginfo->subject)*2 +1, 
1018                                 msginfo->subject);
1019                 }
1020                 if (g_utf8_validate(subject, -1, NULL))
1021                         gtk_window_set_title(GTK_WINDOW(messageview->window), 
1022                                 subject);
1023                 g_free(subject);
1024         }
1025
1026         if (msginfo && msginfo->folder) {
1027                 msginfo->folder->last_seen = msginfo->msgnum;   
1028         }
1029
1030         main_create_mailing_list_menu(messageview->mainwin, messageview->msginfo);
1031
1032         if (messageview->msginfo && messageview->msginfo->extradata
1033             && messageview->msginfo->extradata->partial_recv)
1034                 partial_recv_show(messageview->noticeview, 
1035                                   messageview->msginfo);
1036         else if (messageview->msginfo && messageview->msginfo->extradata &&
1037             (messageview->msginfo->extradata->dispositionnotificationto || 
1038              messageview->msginfo->extradata->returnreceiptto) &&
1039             !MSG_IS_RETRCPT_SENT(messageview->msginfo->flags) &&
1040             !prefs_common.never_send_retrcpt)
1041                 return_receipt_show(messageview->noticeview, 
1042                                     messageview->msginfo);
1043         else 
1044                 noticeview_hide(messageview->noticeview);
1045
1046         mimeinfo = procmime_mimeinfo_next(mimeinfo);
1047         if (!all_headers && mimeinfo 
1048                         && (mimeinfo->type != MIMETYPE_TEXT || 
1049             strcasecmp(mimeinfo->subtype, "plain")) 
1050                         && (mimeinfo->type != MIMETYPE_MULTIPART || 
1051             strcasecmp(mimeinfo->subtype, "signed"))) {
1052                 if (strcasecmp(mimeinfo->subtype, "html"))
1053                         mimeview_show_part(messageview->mimeview,mimeinfo);
1054                 else if (prefs_common.invoke_plugin_on_html)
1055                         mimeview_select_mimepart_icon(messageview->mimeview,mimeinfo);
1056         }
1057
1058         g_free(file);
1059
1060         return 0;
1061 }
1062
1063 void messageview_reflect_prefs_pixmap_theme(void)
1064 {
1065         GList *cur;
1066         MessageView *msgview;
1067
1068         for (cur = msgview_list; cur != NULL; cur = cur->next) {
1069                 msgview = (MessageView*)cur->data;
1070                 toolbar_update(TOOLBAR_MSGVIEW, msgview);
1071                 mimeview_update(msgview->mimeview);
1072         }
1073 }
1074
1075 void messageview_clear(MessageView *messageview)
1076 {
1077         if (!messageview)
1078                 return;
1079         procmsg_msginfo_free(messageview->msginfo);
1080         messageview->msginfo = NULL;
1081         messageview->filtered = FALSE;
1082         mimeview_clear(messageview->mimeview);
1083         headerview_clear(messageview->headerview);
1084         noticeview_hide(messageview->noticeview);
1085 }
1086
1087 void messageview_destroy(MessageView *messageview)
1088 {
1089         debug_print("destroy messageview\n");
1090         messageview_list = g_list_remove(messageview_list, messageview);
1091
1092         if (messageview->mainwin->summaryview->messageview == messageview) {
1093                 messageview->mainwin->summaryview->displayed = NULL;
1094                 messageview->mainwin->summaryview->messageview = NULL;
1095         }
1096         if (messageview->mainwin->summaryview->ext_messageview == messageview) {
1097                 messageview->mainwin->summaryview->displayed = NULL;
1098                 messageview->mainwin->summaryview->ext_messageview = NULL;
1099         }
1100         if (!messageview->deferred_destroy) {
1101                 hooks_unregister_hook(MSGINFO_UPDATE_HOOKLIST,
1102                               messageview->msginfo_update_callback_id);
1103         }
1104
1105         if (messageview->updating) {
1106                 debug_print("uh oh, better not touch that now (fetching)\n");
1107                 messageview->deferred_destroy = TRUE;
1108                 gtk_widget_hide(messageview->window);
1109                 return;
1110         }
1111         
1112         if (messageview->mimeview->textview
1113         &&  messageview->mimeview->textview->loading) {
1114                 debug_print("uh oh, better not touch that now (loading text)\n");
1115                 messageview->deferred_destroy = TRUE;
1116                 messageview->mimeview->textview->stop_loading = TRUE;
1117                 gtk_widget_hide(messageview->window);
1118                 return;
1119         }
1120
1121         headerview_destroy(messageview->headerview);
1122         mimeview_destroy(messageview->mimeview);
1123         noticeview_destroy(messageview->noticeview);
1124
1125         procmsg_msginfo_free(messageview->msginfo);
1126         toolbar_clear_list(TOOLBAR_MSGVIEW);
1127         if (messageview->toolbar) {
1128                 toolbar_destroy(messageview->toolbar);
1129                 g_free(messageview->toolbar);
1130         }
1131         
1132         msgview_list = g_list_remove(msgview_list, messageview); 
1133
1134         if (messageview->window)
1135                 gtk_widget_destroy(messageview->window);
1136         g_free(messageview);
1137 }
1138
1139 void messageview_delete(MessageView *msgview)
1140 {
1141         MsgInfo *msginfo = NULL;
1142         FolderItem *trash = NULL;
1143         PrefsAccount *ac = NULL;
1144
1145         if (msgview->msginfo && msgview->mainwin && msgview->mainwin->summaryview)
1146                 msginfo = summary_get_selected_msg(msgview->mainwin->summaryview);
1147         
1148         /* need a procmsg_msginfo_equal() */
1149         if (msginfo && msgview->msginfo && 
1150             msginfo->msgnum == msgview->msginfo->msgnum && 
1151             msginfo->folder == msgview->msginfo->folder) {
1152                 summary_delete_trash(msgview->mainwin->summaryview);
1153         } else {                
1154                 msginfo = msgview->msginfo;
1155
1156                 g_return_if_fail(msginfo != NULL);
1157
1158                 /* to get the trash folder, we have to choose either
1159                  * the folder's or account's trash default - we prefer
1160                  * the one in the account prefs */
1161                 if (msginfo->folder) {
1162                         if (NULL != (ac = account_find_from_item(msginfo->folder)))
1163                                 trash = account_get_special_folder(ac, F_TRASH);
1164                         if (!trash && msginfo->folder->folder)  
1165                                 trash = msginfo->folder->folder->trash;
1166                         /* if still not found, use the default */
1167                         if (!trash) 
1168                                 trash = folder_get_default_trash();
1169                 }       
1170
1171                 g_return_if_fail(trash != NULL);
1172
1173                 if (prefs_common.immediate_exec)
1174                         /* TODO: Delete from trash */
1175                         folder_item_move_msg(trash, msginfo);
1176                 else {
1177                         procmsg_msginfo_set_to_folder(msginfo, trash);
1178                         procmsg_msginfo_set_flags(msginfo, MSG_DELETED, 0);
1179                         /* NOTE: does not update to next message in summaryview */
1180                 }
1181         }
1182 #ifdef MAEMO
1183         if (msgview->window) {
1184                 messageview_destroy(msgview);
1185         }
1186 #endif
1187 }
1188
1189 /* 
1190  * \brief update messageview with currently selected message in summaryview
1191  *        leave unchanged if summaryview is empty
1192  * \param pointer to MessageView
1193  */     
1194 static void messageview_update(MessageView *msgview, MsgInfo *old_msginfo)
1195 {
1196         SummaryView *summaryview = (SummaryView*)msgview->mainwin->summaryview;
1197
1198         g_return_if_fail(summaryview != NULL);
1199         
1200         if (summaryview->selected) {
1201                 MsgInfo *msginfo = summary_get_selected_msg(summaryview);
1202                 if (msginfo == NULL || msginfo == old_msginfo)
1203                         return;
1204
1205                 messageview_show(msgview, msginfo, 
1206                                  msgview->all_headers);
1207         } 
1208 }
1209
1210 TextView *messageview_get_current_textview(MessageView *messageview)
1211 {
1212         TextView *text = NULL;
1213
1214         text = messageview->mimeview->textview;
1215
1216         return text;
1217 }
1218
1219 MimeInfo *messageview_get_selected_mime_part(MessageView *messageview)
1220 {
1221         return mimeview_get_selected_part(messageview->mimeview);
1222 }
1223
1224 void messageview_copy_clipboard(MessageView *messageview)
1225 {
1226         gchar *text = messageview_get_selection(messageview);
1227         if (text) {
1228                 gtk_clipboard_set_text(
1229                         gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
1230                         text, -1);
1231         }
1232         g_free(text);
1233 }
1234
1235 void messageview_select_all(MessageView *messageview)
1236 {
1237         TextView *text;
1238
1239         text = messageview_get_current_textview(messageview);
1240         if (text) {
1241                 GtkTextView *textview = GTK_TEXT_VIEW(text->text);
1242                 GtkTextBuffer *buffer;
1243                 GtkTextIter start, end;
1244
1245                 buffer = gtk_text_view_get_buffer(textview);
1246                 gtk_text_buffer_get_bounds(buffer, &start, &end);
1247                 gtk_text_buffer_select_range(buffer, &start, &end);
1248         }
1249 }
1250
1251 void messageview_set_position(MessageView *messageview, gint pos)
1252 {
1253         TextView *text;
1254
1255         text = messageview_get_current_textview(messageview);
1256         if (text)
1257                 textview_set_position(text, pos);
1258 }
1259
1260 gboolean messageview_search_string(MessageView *messageview, const gchar *str,
1261                                    gboolean case_sens)
1262 {
1263         TextView *text;
1264
1265         if (messageview->mimeview->type == MIMEVIEW_VIEWER) {
1266                 MimeViewer *viewer = messageview->mimeview->mimeviewer;
1267                 if (viewer && viewer->text_search) {
1268                         return viewer->text_search(viewer, FALSE, str, case_sens);
1269                 }
1270         }
1271
1272         text = messageview_get_current_textview(messageview);
1273         if (text)
1274                 return textview_search_string(text, str, case_sens);
1275         return FALSE;
1276 }
1277
1278 gboolean messageview_search_string_backward(MessageView *messageview,
1279                                             const gchar *str,
1280                                             gboolean case_sens)
1281 {
1282         TextView *text;
1283
1284         if (messageview->mimeview->type == MIMEVIEW_VIEWER) {
1285                 MimeViewer *viewer = messageview->mimeview->mimeviewer;
1286                 if (viewer && viewer->text_search) {
1287                         return viewer->text_search(viewer, TRUE, str, case_sens);
1288                 }
1289         }
1290
1291         text = messageview_get_current_textview(messageview);
1292         if (text)       
1293                 return textview_search_string_backward(text,
1294                                                        str, case_sens);
1295         return FALSE;
1296 }
1297
1298 gboolean messageview_is_visible(MessageView *messageview)
1299 {
1300         if (messageview == NULL)
1301                 return FALSE;
1302         return messageview->visible;
1303 }
1304
1305 static void messageview_save_as(MessageView *messageview)
1306 {
1307         gchar *filename = NULL;
1308         MsgInfo *msginfo;
1309         gchar *src, *dest, *tmp;
1310
1311         if (!messageview->msginfo) return;
1312         msginfo = messageview->msginfo;
1313
1314         if (msginfo->subject) {
1315                 Xstrdup_a(filename, msginfo->subject, return);
1316                 subst_for_filename(filename);
1317         }
1318         if (filename && !g_utf8_validate(filename, -1, NULL)) {
1319                 gchar *oldstr = filename;
1320                 filename = conv_codeset_strdup(filename,
1321                                                conv_get_locale_charset_str(),
1322                                                CS_UTF_8);
1323                 if (!filename) {
1324                         g_warning("messageview_save_as(): failed to convert character set.");
1325                         filename = g_strdup(oldstr);
1326                 }
1327                 dest = filesel_select_file_save(_("Save as"), filename);
1328                 g_free(filename);
1329         } else
1330                 dest = filesel_select_file_save(_("Save as"), filename);
1331         if (!dest) return;
1332         if (is_file_exist(dest)) {
1333                 AlertValue aval;
1334
1335                 aval = alertpanel(_("Overwrite"),
1336                                   _("Overwrite existing file?"),
1337                                   GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL);
1338                 if (G_ALERTALTERNATE != aval) return;
1339         }
1340
1341         src = procmsg_get_message_file(msginfo);
1342         if (copy_file(src, dest, TRUE) < 0) {
1343                 tmp =  g_path_get_basename(dest);
1344                 alertpanel_error(_("Couldn't save the file '%s'."), tmp);
1345                 g_free(tmp);
1346         }
1347         g_free(dest);
1348         g_free(src);
1349 }
1350
1351 static gint messageview_delete_cb(GtkWidget *widget, GdkEventAny *event,
1352                                   MessageView *messageview)
1353 {
1354         messageview_destroy(messageview);
1355         return TRUE;
1356 }
1357
1358 static void messageview_size_allocate_cb(GtkWidget *widget,
1359                                          GtkAllocation *allocation)
1360 {
1361         g_return_if_fail(allocation != NULL);
1362
1363         prefs_common.msgwin_width  = allocation->width;
1364         prefs_common.msgwin_height = allocation->height;
1365 }
1366 #ifndef MAEMO
1367 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
1368                         MessageView *messageview)
1369 {
1370         if (event && event->keyval == GDK_Escape && messageview->window) {
1371                 messageview_destroy(messageview);
1372                 return TRUE;
1373         }
1374
1375         if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK|GDK_SHIFT_MASK)) != 0)
1376                 return FALSE;
1377
1378         g_signal_stop_emission_by_name(G_OBJECT(widget),
1379                                         "key_press_event");
1380         mimeview_pass_key_press_event(messageview->mimeview, event);
1381         return FALSE;
1382 }
1383 #endif
1384 static void return_receipt_show(NoticeView *noticeview, MsgInfo *msginfo)
1385 {
1386         gchar *addr = NULL;
1387         gboolean from_me = FALSE;
1388         if (msginfo->folder 
1389                 && (folder_has_parent_of_type(msginfo->folder, F_QUEUE)
1390                  || folder_has_parent_of_type(msginfo->folder, F_DRAFT)))
1391                 return;
1392
1393         addr = g_strdup(msginfo->from);
1394         if (addr) {
1395                 extract_address(addr);
1396                 if (account_find_from_address(addr)) {
1397                         from_me = TRUE;
1398                 }
1399                 g_free(addr);
1400         }
1401
1402         if (from_me) {
1403                 noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
1404                 noticeview_set_text(noticeview, _("You asked for a return receipt in this message."));
1405                 noticeview_set_button_text(noticeview, NULL);
1406                 noticeview_set_button_press_callback(noticeview, NULL, NULL);
1407         } else {
1408                 noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
1409                 noticeview_set_text(noticeview, _("This message asks for a return receipt."));
1410                 noticeview_set_button_text(noticeview, _("Send receipt"));
1411                 noticeview_set_button_press_callback(noticeview,
1412                                                      G_CALLBACK(return_receipt_send_clicked),
1413                                                      (gpointer) msginfo);
1414         }
1415         noticeview_show(noticeview);
1416 }
1417
1418 static void return_receipt_send_clicked(NoticeView *noticeview, MsgInfo *msginfo)
1419 {
1420         MsgInfo *tmpmsginfo;
1421         gchar *file;
1422
1423         file = procmsg_get_message_file_path(msginfo);
1424         if (!file) {
1425                 g_warning("can't get message file path.\n");
1426                 return;
1427         }
1428
1429         tmpmsginfo = procheader_parse_file(file, msginfo->flags, TRUE, TRUE);
1430         tmpmsginfo->folder = msginfo->folder;
1431         tmpmsginfo->msgnum = msginfo->msgnum;
1432
1433         if (disposition_notification_send(tmpmsginfo) >= 0) {
1434                 procmsg_msginfo_set_flags(msginfo, MSG_RETRCPT_SENT, 0);
1435                 noticeview_hide(noticeview);
1436         }               
1437
1438         procmsg_msginfo_free(tmpmsginfo);
1439         g_free(file);
1440 }
1441
1442 static void partial_recv_show(NoticeView *noticeview, MsgInfo *msginfo)
1443 {
1444         gchar *text = NULL;
1445         gchar *button1 = NULL;
1446         gchar *button2 = NULL;
1447         void  *button1_cb = NULL;
1448         void  *button2_cb = NULL;
1449
1450         if (!msginfo->extradata)
1451                 return;
1452         if (!partial_msg_in_uidl_list(msginfo)) {
1453                 text = g_strdup_printf(_("This message has been partially "
1454                                 "retrieved,\nand has been deleted from the "
1455                                 "server."));
1456         } else {
1457                 switch (msginfo->planned_download) {
1458                 case POP3_PARTIAL_DLOAD_UNKN:
1459                         text = g_strdup_printf(_("This message has been "
1460                                         "partially retrieved;\nit is %s."),
1461                                         to_human_readable(
1462                                                 (off_t)(msginfo->total_size)));
1463                         button1 = _("Mark for download");
1464                         button2 = _("Mark for deletion");
1465                         button1_cb = partial_recv_dload_clicked;
1466                         button2_cb = partial_recv_del_clicked;
1467                         break;
1468                 case POP3_PARTIAL_DLOAD_DLOAD:
1469                         text = g_strdup_printf(_("This message has been "
1470                                         "partially retrieved;\nit is %s and "
1471                                         "will be downloaded."),
1472                                         to_human_readable(
1473                                                 (off_t)(msginfo->total_size)));
1474                         button1 = _("Unmark");
1475                         button1_cb = partial_recv_unmark_clicked;
1476                         button2 = _("Mark for deletion");
1477                         button2_cb = partial_recv_del_clicked;
1478                         break;
1479                 case POP3_PARTIAL_DLOAD_DELE:
1480                         text = g_strdup_printf(_("This message has been "
1481                                         "partially retrieved;\nit is %s and "
1482                                         "will be deleted."),
1483                                         to_human_readable(
1484                                                 (off_t)(msginfo->total_size)));
1485                         button1 = _("Mark for download");
1486                         button1_cb = partial_recv_dload_clicked;
1487                         button2 = _("Unmark");
1488                         button2_cb = partial_recv_unmark_clicked;
1489                         break;
1490                 default:
1491                         return;
1492                 }
1493         }
1494         
1495         noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
1496         noticeview_set_text(noticeview, text);
1497         g_free(text);
1498         noticeview_set_button_text(noticeview, button1);
1499         noticeview_set_button_press_callback(noticeview,
1500                      G_CALLBACK(button1_cb), (gpointer) msginfo);
1501
1502         noticeview_set_2ndbutton_text(noticeview, button2);
1503         noticeview_set_2ndbutton_press_callback(noticeview,
1504                      G_CALLBACK(button2_cb), (gpointer) msginfo);
1505
1506         noticeview_show(noticeview);
1507 }
1508
1509 static void partial_recv_dload_clicked(NoticeView *noticeview, 
1510                                        MsgInfo *msginfo)
1511 {
1512         if (partial_mark_for_download(msginfo) == 0) {
1513                 partial_recv_show(noticeview, msginfo);
1514         }
1515 }
1516
1517 static void partial_recv_del_clicked(NoticeView *noticeview, 
1518                                        MsgInfo *msginfo)
1519 {
1520         if (partial_mark_for_delete(msginfo) == 0) {
1521                 partial_recv_show(noticeview, msginfo);
1522         }
1523 }
1524
1525 static void partial_recv_unmark_clicked(NoticeView *noticeview, 
1526                                        MsgInfo *msginfo)
1527 {
1528         if (partial_unmark(msginfo) == 0) {
1529                 partial_recv_show(noticeview, msginfo);
1530         }
1531 }
1532
1533 static void select_account_cb(GtkWidget *w, gpointer data)
1534 {
1535         *(gint*)data = combobox_get_active_data(GTK_COMBO_BOX(w));
1536 }
1537
1538 static PrefsAccount *select_account_from_list(GList *ac_list)
1539 {
1540         GtkWidget *optmenu;
1541         gint account_id;
1542
1543         g_return_val_if_fail(ac_list != NULL, NULL);
1544         g_return_val_if_fail(ac_list->data != NULL, NULL);
1545         
1546         optmenu = gtkut_account_menu_new(ac_list,
1547                         G_CALLBACK(select_account_cb),
1548                         &account_id);
1549         if (!optmenu)
1550                 return NULL;
1551         account_id = ((PrefsAccount *) ac_list->data)->account_id;
1552         if (alertpanel_with_widget(
1553                                 _("Return Receipt Notification"),
1554                                 _("The message was sent to several of your "
1555                                   "accounts.\n"
1556                                   "Please choose which account do you want to "
1557                                   "use for sending the receipt notification:"),
1558                                 _("_Cancel"), _("_Send Notification"), NULL,
1559                                 FALSE, G_ALERTDEFAULT, optmenu) != G_ALERTALTERNATE)
1560                 return NULL;
1561         return account_find_from_id(account_id);
1562 }
1563
1564 /* 
1565  * \brief return selected messageview text, when nothing is 
1566  *        selected and message was filtered, return complete text
1567  *
1568  * \param  pointer to Messageview 
1569  *
1570  * \return pointer to text (needs to be free'd by calling func)
1571  */
1572 gchar *messageview_get_selection(MessageView *msgview)
1573 {
1574         TextView *textview;
1575         gchar *text = NULL;
1576         GtkTextView *edit = NULL;
1577         GtkTextBuffer *textbuf;
1578         gint body_pos = 0;
1579         
1580         g_return_val_if_fail(msgview != NULL, NULL);
1581
1582         if (msgview->mimeview->type == MIMEVIEW_VIEWER) {
1583                 MimeViewer *viewer = msgview->mimeview->mimeviewer;
1584                 if (viewer && viewer->get_selection) {
1585                         text = viewer->get_selection(viewer);
1586                         if (text)
1587                                 return text;
1588                 }
1589         }
1590
1591         textview = messageview_get_current_textview(msgview);
1592         g_return_val_if_fail(textview != NULL, NULL);
1593
1594         edit = GTK_TEXT_VIEW(textview->text);
1595         g_return_val_if_fail(edit != NULL, NULL);
1596         body_pos = textview->body_pos;
1597
1598         textbuf = gtk_text_view_get_buffer(edit);
1599
1600         if (gtk_text_buffer_get_selection_bounds(textbuf, NULL, NULL))
1601                 return gtkut_text_view_get_selection(edit);
1602         else if (msgview->filtered) {
1603                 GtkTextIter start_iter, end_iter;
1604                 gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter, body_pos);
1605                 gtk_text_buffer_get_end_iter(textbuf, &end_iter);
1606                 gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter, FALSE);
1607         } else
1608                 text = NULL;
1609
1610         return text;
1611 }
1612
1613 static void save_as_cb(gpointer data, guint action, GtkWidget *widget)
1614 {
1615         MessageView *messageview = (MessageView *)data;
1616         messageview_save_as(messageview);
1617 }
1618
1619 #if defined(USE_GNOMEPRINT) || GTK_CHECK_VERSION(2,10,0)
1620 static void print_mimeview(MimeView *mimeview, gint sel_start, gint sel_end, gint partnum) 
1621 {
1622 #if !defined(USE_GNOMEPRINT) && GTK_CHECK_VERSION(2,10,0)
1623         MainWindow *mainwin;
1624 #endif
1625         if (!mimeview 
1626         ||  !mimeview->textview
1627         ||  !mimeview->textview->text)
1628                 alertpanel_warning(_("Cannot print: the message doesn't "
1629                                      "contain text."));
1630         else {
1631                 gtk_widget_realize(mimeview->textview->text);
1632                 if (partnum > 0) {
1633                         mimeview_select_part_num(mimeview, partnum);
1634                 }
1635                 if (mimeview->type == MIMEVIEW_VIEWER) {
1636                         MimeViewer *viewer = mimeview->mimeviewer;
1637                         if (viewer && viewer->print) {
1638                                 viewer->print(viewer);
1639                                 return;
1640                         }
1641                 }
1642                 if (sel_start != -1 && sel_end != -1) {
1643                         GtkTextIter start, end;
1644                         GtkTextView *text = GTK_TEXT_VIEW(mimeview->textview->text);
1645                         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
1646
1647                         gtk_text_buffer_get_iter_at_offset(buffer, &start, sel_start);
1648                         gtk_text_buffer_get_iter_at_offset(buffer, &end, sel_end);
1649                         gtk_text_buffer_select_range(buffer, &start, &end);
1650                 }
1651 #if defined(USE_GNOMEPRINT)
1652                 gedit_print(GTK_TEXT_VIEW(mimeview->textview->text));
1653 #else
1654                 /* TODO: Get the real parent window, not the main window */
1655                 mainwin = mainwindow_get_mainwindow();
1656                 printing_print(GTK_TEXT_VIEW(mimeview->textview->text),
1657                                mainwin ? GTK_WINDOW(mainwin->window) : NULL,
1658                                 sel_start, sel_end);
1659 #endif
1660         }
1661 }
1662
1663 void messageview_print(MsgInfo *msginfo, gboolean all_headers, 
1664                         gint sel_start, gint sel_end, gint partnum) 
1665 {
1666         PangoFontDescription *font_desc = NULL;
1667         MessageView *tmpview = messageview_create_with_new_window_visible(
1668                                 mainwindow_get_mainwindow(), FALSE);
1669
1670         if (prefs_common.use_different_print_font) {
1671                 font_desc = pango_font_description_from_string
1672                                                 (prefs_common.printfont);
1673         } else {
1674                 font_desc = pango_font_description_from_string
1675                                                 (prefs_common.textfont);
1676         }
1677         if (font_desc) {
1678                 gtk_widget_modify_font(tmpview->mimeview->textview->text, 
1679                         font_desc);
1680                 pango_font_description_free(font_desc);
1681         }
1682
1683         tmpview->all_headers = all_headers;
1684         if (msginfo && messageview_show(tmpview, msginfo, 
1685                 tmpview->all_headers) >= 0) {
1686                         print_mimeview(tmpview->mimeview, 
1687                                 sel_start, sel_end, partnum);
1688         }
1689         messageview_destroy(tmpview);
1690 }
1691 #endif
1692
1693 #if GTK_CHECK_VERSION(2,10,0) && !defined(USE_GNOMEPRINT)
1694 static void page_setup_cb(gpointer data, guint action, GtkWidget *widget)
1695 {
1696         MessageView *messageview = (MessageView *)data;
1697         printing_page_setup(messageview ?
1698                             GTK_WINDOW(messageview->window) : NULL);
1699 }
1700 #endif
1701
1702 static void print_cb(gpointer data, guint action, GtkWidget *widget)
1703 {
1704         MessageView *messageview = (MessageView *)data;
1705 #if !defined(USE_GNOMEPRINT) && !GTK_CHECK_VERSION(2,10,0)
1706         gchar *cmdline = NULL;
1707         gchar *p;
1708 #else
1709         gint sel_start = -1, sel_end = -1, partnum = 0;
1710 #endif
1711
1712         if (!messageview->msginfo) return;
1713
1714 #if !defined(USE_GNOMEPRINT) && !GTK_CHECK_VERSION(2,10,0)
1715         cmdline = input_dialog(_("Print"),
1716                                _("Enter the print command line:\n"
1717                                  "('%s' will be replaced with file name)"),
1718                                prefs_common.print_cmd);
1719         if (!cmdline) return;
1720         if (!(p = strchr(cmdline, '%')) || *(p + 1) != 's' ||
1721             strchr(p + 2, '%')) {
1722                 alertpanel_error(_("Print command line is invalid:\n'%s'"),
1723                                  cmdline);
1724                 g_free(cmdline);
1725                 return;
1726         }
1727         procmsg_print_message(messageview->msginfo, cmdline);
1728         g_free(cmdline);
1729 #else
1730         partnum = mimeview_get_selected_part_num(messageview->mimeview);
1731         textview_get_selection_offsets(messageview->mimeview->textview,
1732                 &sel_start, &sel_end);
1733         messageview_print(messageview->msginfo, messageview->all_headers, 
1734                 sel_start, sel_end, partnum);
1735 #endif
1736 }
1737
1738 static void close_cb(gpointer data, guint action, GtkWidget *widget)
1739 {
1740         MessageView *messageview = (MessageView *)data;
1741         messageview_destroy(messageview);
1742 }
1743
1744 static void copy_cb(gpointer data, guint action, GtkWidget *widget)
1745 {
1746         MessageView *messageview = (MessageView *)data;
1747         messageview_copy_clipboard(messageview);
1748 }
1749
1750 static void allsel_cb(gpointer data, guint action, GtkWidget *widget)
1751 {
1752         MessageView *messageview = (MessageView *)data;
1753         messageview_select_all(messageview);
1754 }
1755
1756 static void search_cb(gpointer data, guint action, GtkWidget *widget)
1757 {
1758         MessageView *messageview = (MessageView *)data;
1759         message_search(messageview);
1760 }
1761
1762 static void prev_cb(gpointer data, guint action, GtkWidget *widget)
1763 {
1764         MessageView *messageview = (MessageView *)data;
1765         messageview->updating = TRUE;
1766         summary_step(messageview->mainwin->summaryview, GTK_SCROLL_STEP_BACKWARD);
1767         messageview->updating = FALSE;
1768
1769         if (messageview->deferred_destroy) {
1770                 debug_print("messageview got away!\n");
1771                 messageview_destroy(messageview);
1772                 return;
1773         }
1774         if (messageview->mainwin->summaryview->selected) {
1775 #ifndef MAEMO
1776                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
1777                        
1778                 if (msginfo)
1779                         messageview_show(messageview, msginfo, 
1780                                          messageview->all_headers);
1781 #endif
1782         } else {
1783                 gtk_widget_destroy(messageview->window);
1784         }
1785 }
1786
1787 static void next_cb(gpointer data, guint action, GtkWidget *widget)
1788 {
1789         MessageView *messageview = (MessageView *)data;
1790         messageview->updating = TRUE;
1791         summary_step(messageview->mainwin->summaryview, GTK_SCROLL_STEP_FORWARD);
1792         messageview->updating = FALSE;
1793
1794         if (messageview->deferred_destroy) {
1795                 debug_print("messageview got away!\n");
1796                 messageview_destroy(messageview);
1797                 return;
1798         }
1799         if (messageview->mainwin->summaryview->selected) {
1800 #ifndef MAEMO
1801                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
1802                        
1803                 if (msginfo)
1804                         messageview_show(messageview, msginfo, 
1805                                          messageview->all_headers);
1806 #endif
1807         } else {
1808                 gtk_widget_destroy(messageview->window);
1809         }
1810 }
1811
1812 static void prev_unread_cb(gpointer data, guint action, GtkWidget *widget)
1813 {
1814         MessageView *messageview = (MessageView *)data;
1815         messageview->updating = TRUE;
1816         summary_select_prev_unread(messageview->mainwin->summaryview);
1817         messageview->updating = FALSE;
1818
1819         if (messageview->deferred_destroy) {
1820                 debug_print("messageview got away!\n");
1821                 messageview_destroy(messageview);
1822                 return;
1823         }
1824         if (messageview->mainwin->summaryview->selected) {
1825 #ifndef MAEMO
1826                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
1827                        
1828                 if (msginfo)
1829                         messageview_show(messageview, msginfo, 
1830                                          messageview->all_headers);
1831 #endif
1832         } else {
1833                 gtk_widget_destroy(messageview->window);
1834         }
1835 }
1836
1837 static void next_unread_cb(gpointer data, guint action, GtkWidget *widget)
1838 {
1839         MessageView *messageview = (MessageView *)data;
1840         messageview->updating = TRUE;
1841         summary_select_next_unread(messageview->mainwin->summaryview);
1842         messageview->updating = FALSE;
1843
1844         if (messageview->deferred_destroy) {
1845                 debug_print("messageview got away!\n");
1846                 messageview_destroy(messageview);
1847                 return;
1848         }
1849         if (messageview->mainwin->summaryview->selected) {
1850 #ifndef MAEMO
1851                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
1852                        
1853                 if (msginfo)
1854                         messageview_show(messageview, msginfo, 
1855                                          messageview->all_headers);
1856 #endif
1857         } else {
1858                 gtk_widget_destroy(messageview->window);
1859         }
1860 }
1861
1862 static void prev_new_cb(gpointer data, guint action, GtkWidget *widget)
1863 {
1864         MessageView *messageview = (MessageView *)data;
1865         messageview->updating = TRUE;
1866         summary_select_prev_new(messageview->mainwin->summaryview);
1867         messageview->updating = FALSE;
1868
1869         if (messageview->deferred_destroy) {
1870                 debug_print("messageview got away!\n");
1871                 messageview_destroy(messageview);
1872                 return;
1873         }
1874         if (messageview->mainwin->summaryview->selected) {
1875 #ifndef MAEMO
1876                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
1877                        
1878                 if (msginfo)
1879                         messageview_show(messageview, msginfo, 
1880                                          messageview->all_headers);
1881 #endif
1882         } else {
1883                 gtk_widget_destroy(messageview->window);
1884         }
1885 }
1886
1887 static void next_new_cb(gpointer data, guint action, GtkWidget *widget)
1888 {
1889         MessageView *messageview = (MessageView *)data;
1890         messageview->updating = TRUE;
1891         summary_select_next_new(messageview->mainwin->summaryview);
1892         messageview->updating = FALSE;
1893
1894         if (messageview->deferred_destroy) {
1895                 debug_print("messageview got away!\n");
1896                 messageview_destroy(messageview);
1897                 return;
1898         }
1899         if (messageview->mainwin->summaryview->selected) {
1900 #ifndef MAEMO
1901                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
1902                        
1903                 if (msginfo)
1904                         messageview_show(messageview, msginfo, 
1905                                          messageview->all_headers);
1906 #endif
1907         } else {
1908                 gtk_widget_destroy(messageview->window);
1909         }
1910 }
1911
1912 static void prev_marked_cb(gpointer data, guint action, GtkWidget *widget)
1913 {
1914         MessageView *messageview = (MessageView *)data;
1915         messageview->updating = TRUE;
1916         summary_select_prev_marked(messageview->mainwin->summaryview);
1917         messageview->updating = FALSE;
1918
1919         if (messageview->deferred_destroy) {
1920                 debug_print("messageview got away!\n");
1921                 messageview_destroy(messageview);
1922                 return;
1923         }
1924         if (messageview->mainwin->summaryview->selected) {
1925 #ifndef MAEMO
1926                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
1927                        
1928                 if (msginfo)
1929                         messageview_show(messageview, msginfo, 
1930                                          messageview->all_headers);
1931 #endif
1932         } else {
1933                 gtk_widget_destroy(messageview->window);
1934         }
1935 }
1936
1937 static void next_marked_cb(gpointer data, guint action, GtkWidget *widget)
1938 {
1939         MessageView *messageview = (MessageView *)data;
1940         messageview->updating = TRUE;
1941         summary_select_next_marked(messageview->mainwin->summaryview);
1942         messageview->updating = FALSE;
1943
1944         if (messageview->deferred_destroy) {
1945                 debug_print("messageview got away!\n");
1946                 messageview_destroy(messageview);
1947                 return;
1948         }
1949         if (messageview->mainwin->summaryview->selected) {
1950 #ifndef MAEMO
1951                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
1952                        
1953                 if (msginfo)
1954                         messageview_show(messageview, msginfo, 
1955                                          messageview->all_headers);
1956 #endif
1957         } else {
1958                 gtk_widget_destroy(messageview->window);
1959         }
1960 }
1961
1962 static void prev_labeled_cb(gpointer data, guint action, GtkWidget *widget)
1963 {
1964         MessageView *messageview = (MessageView *)data;
1965         messageview->updating = TRUE;
1966         summary_select_prev_labeled(messageview->mainwin->summaryview);
1967         messageview->updating = FALSE;
1968
1969         if (messageview->deferred_destroy) {
1970                 debug_print("messageview got away!\n");
1971                 messageview_destroy(messageview);
1972                 return;
1973         }
1974         if (messageview->mainwin->summaryview->selected) {
1975 #ifndef MAEMO
1976                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
1977                        
1978                 if (msginfo)
1979                         messageview_show(messageview, msginfo, 
1980                                          messageview->all_headers);
1981 #endif
1982         } else {
1983                 gtk_widget_destroy(messageview->window);
1984         }
1985 }
1986
1987 static void next_labeled_cb(gpointer data, guint action, GtkWidget *widget)
1988 {
1989         MessageView *messageview = (MessageView *)data;
1990         messageview->updating = TRUE;
1991         summary_select_next_labeled(messageview->mainwin->summaryview);
1992         messageview->updating = FALSE;
1993
1994         if (messageview->deferred_destroy) {
1995                 debug_print("messageview got away!\n");
1996                 messageview_destroy(messageview);
1997                 return;
1998         }
1999         if (messageview->mainwin->summaryview->selected) {
2000 #ifndef MAEMO
2001                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2002                        
2003                 if (msginfo)
2004                         messageview_show(messageview, msginfo, 
2005                                          messageview->all_headers);
2006 #endif
2007         } else {
2008                 gtk_widget_destroy(messageview->window);
2009         }
2010 }
2011
2012 static void last_read_cb(gpointer data, guint action, GtkWidget *widget)
2013 {
2014         MessageView *messageview = (MessageView *)data;
2015         messageview->updating = TRUE;
2016         summary_select_last_read(messageview->mainwin->summaryview);
2017         messageview->updating = FALSE;
2018
2019         if (messageview->deferred_destroy) {
2020                 debug_print("messageview got away!\n");
2021                 messageview_destroy(messageview);
2022                 return;
2023         }
2024         if (messageview->mainwin->summaryview->selected) {
2025 #ifndef MAEMO
2026                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2027                        
2028                 if (msginfo)
2029                         messageview_show(messageview, msginfo, 
2030                                          messageview->all_headers);
2031 #endif
2032         } else {
2033                 gtk_widget_destroy(messageview->window);
2034         }
2035 }
2036
2037 static void parent_cb(gpointer data, guint action, GtkWidget *widget)
2038 {
2039         MessageView *messageview = (MessageView *)data;
2040         messageview->updating = TRUE;
2041         summary_select_parent(messageview->mainwin->summaryview);
2042         messageview->updating = FALSE;
2043
2044         if (messageview->deferred_destroy) {
2045                 debug_print("messageview got away!\n");
2046                 messageview_destroy(messageview);
2047                 return;
2048         }
2049         if (messageview->mainwin->summaryview->selected) {
2050 #ifndef MAEMO
2051                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2052                        
2053                 if (msginfo)
2054                         messageview_show(messageview, msginfo, 
2055                                          messageview->all_headers);
2056 #endif
2057         } else {
2058                 gtk_widget_destroy(messageview->window);
2059         }
2060 }
2061
2062 static void goto_unread_folder_cb(gpointer data, guint action, GtkWidget *widget)
2063 {
2064         MessageView *messageview = (MessageView *)data;
2065         messageview->updating = TRUE;
2066         folderview_select_next_unread(messageview->mainwin->folderview, FALSE);
2067         messageview->updating = FALSE;
2068
2069         if (messageview->deferred_destroy) {
2070                 debug_print("messageview got away!\n");
2071                 messageview_destroy(messageview);
2072                 return;
2073         }
2074         if (messageview->mainwin->summaryview->selected) {
2075 #ifndef MAEMO
2076                 MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2077                        
2078                 if (msginfo)
2079                         messageview_show(messageview, msginfo, 
2080                                          messageview->all_headers);
2081 #endif
2082         } else {
2083                 gtk_widget_destroy(messageview->window);
2084         }
2085 }
2086
2087 static void goto_folder_cb(gpointer data, guint action, GtkWidget *widget)
2088 {
2089         MessageView *messageview = (MessageView *)data;
2090         messageview->updating = TRUE;
2091         FolderItem *to_folder;
2092         messageview->updating = FALSE;
2093
2094         to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL);
2095
2096         if (to_folder) {
2097                 folderview_select(messageview->mainwin->folderview, to_folder);
2098
2099                 if (messageview->deferred_destroy) {
2100                         debug_print("messageview got away!\n");
2101                         messageview_destroy(messageview);
2102                         return;
2103                 }
2104                 if (messageview->mainwin->summaryview->selected) {
2105 #ifndef MAEMO
2106                         MsgInfo * msginfo = summary_get_selected_msg(messageview->mainwin->summaryview);
2107                        
2108                         if (msginfo)
2109                                 messageview_show(messageview, msginfo, 
2110                                                  messageview->all_headers);
2111 #endif
2112                 } else {
2113                         gtk_widget_destroy(messageview->window);
2114                 }
2115         }
2116 }
2117
2118 static void set_charset_cb(gpointer data, guint action, GtkWidget *widget)
2119 {
2120         MessageView *messageview = (MessageView *)data;
2121         const gchar *charset;
2122
2123         if (GTK_CHECK_MENU_ITEM(widget)->active) {
2124                 charset = conv_get_charset_str((CharSet)action);
2125                 g_free(messageview->forced_charset);
2126                 messageview->forced_charset = g_strdup(charset);
2127                 procmime_force_charset(charset);
2128                 
2129                 messageview_show(messageview, messageview->msginfo, FALSE);
2130         }
2131 }
2132
2133 static void set_decode_cb(gpointer data, guint action, GtkWidget *widget)
2134 {
2135         MessageView *messageview = (MessageView *)data;
2136         if (GTK_CHECK_MENU_ITEM(widget)->active) {
2137                 messageview->forced_encoding = (EncodingType)action;
2138
2139                 messageview_show(messageview, messageview->msginfo, FALSE);
2140                 
2141                 debug_print("forced encoding: %d\n", action);
2142         }
2143 }
2144
2145
2146 static void view_source_cb(gpointer data, guint action, GtkWidget *widget)
2147 {
2148         MessageView *messageview = (MessageView *)data;
2149         SourceWindow *srcwin;
2150
2151         if (!messageview->msginfo) return;
2152
2153         srcwin = source_window_create();
2154         source_window_show_msg(srcwin, messageview->msginfo);
2155         source_window_show(srcwin);
2156 }
2157
2158 static void show_all_header_cb(gpointer data, guint action, GtkWidget *widget)
2159 {
2160         MessageView *messageview = (MessageView *)data;
2161         MsgInfo *msginfo = messageview->msginfo;
2162
2163         if (messageview->mimeview->textview &&
2164             messageview->mimeview->textview->loading) {
2165                 return;
2166         }
2167         if (messageview->updating)
2168                 return;
2169
2170         messageview->all_headers = 
2171                         GTK_CHECK_MENU_ITEM(widget)->active;
2172         if (!msginfo) return;
2173         messageview->msginfo = NULL;
2174         messageview_show(messageview, msginfo,
2175                          GTK_CHECK_MENU_ITEM(widget)->active);
2176         procmsg_msginfo_free(msginfo);
2177         main_window_set_menu_sensitive(messageview->mainwin);
2178 }
2179
2180 #define SET_CHECK_MENU_ACTIVE(path, active) \
2181 { \
2182         menuitem = gtk_item_factory_get_widget(ifactory, path); \
2183         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), active); \
2184 }
2185
2186 static void msg_hide_quotes_cb(gpointer data, guint action, GtkWidget *widget)
2187 {
2188         MessageView *messageview = (MessageView *)data;
2189         MsgInfo *msginfo = messageview->msginfo;
2190         static gboolean updating_menu = FALSE;
2191         GtkItemFactory *ifactory = gtk_item_factory_from_widget(messageview->menubar);
2192         GtkWidget *menuitem;
2193         if (updating_menu)
2194                 return;
2195
2196         prefs_common.hide_quotes = 
2197                         GTK_CHECK_MENU_ITEM(widget)->active ? action : 0;
2198         
2199         updating_menu=TRUE;
2200         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold all", FALSE);
2201         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 2", FALSE);
2202         SET_CHECK_MENU_ACTIVE("/View/Quotes/Fold from level 3", FALSE);
2203         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), prefs_common.hide_quotes > 0);      
2204         updating_menu=FALSE;
2205         if (!msginfo) return;
2206         messageview->msginfo = NULL;
2207         messageview_show(messageview, msginfo,
2208                          messageview->all_headers);
2209         procmsg_msginfo_free(msginfo);
2210         
2211         /* update main window */
2212         main_window_set_menu_sensitive(messageview->mainwin);
2213         summary_redisplay_msg(messageview->mainwin->summaryview);
2214 }
2215 #undef SET_CHECK_MENU_ACTIVE
2216
2217 static void compose_cb(gpointer data, guint action, GtkWidget *widget)
2218 {
2219         MessageView *messageview = (MessageView *)data;
2220         PrefsAccount *ac = NULL;
2221         FolderItem *item = NULL;
2222
2223         if (messageview->msginfo)
2224                 item = messageview->msginfo->folder;
2225
2226         if (item) {
2227                 ac = account_find_from_item(item);
2228                 if (ac && ac->protocol == A_NNTP &&
2229                     item->stype == F_NEWS) {
2230                         compose_new(ac, item->path, NULL);
2231                         return;
2232                 }
2233         }
2234
2235         compose_new(ac, NULL, NULL);
2236 }
2237
2238 static void reply_cb(gpointer data, guint action, GtkWidget *widget)
2239 {
2240         MessageView *messageview = (MessageView *)data;
2241         GSList *msginfo_list = NULL;
2242
2243         g_return_if_fail(messageview->msginfo);
2244
2245         msginfo_list = g_slist_append(msginfo_list, messageview->msginfo);
2246         compose_reply_from_messageview(messageview, msginfo_list, action);
2247         g_slist_free(msginfo_list);
2248 }
2249
2250 static void addressbook_open_cb(gpointer data, guint action, GtkWidget *widget)
2251 {
2252         addressbook_open(NULL);
2253 }
2254
2255 static void add_address_cb(gpointer data, guint action, GtkWidget *widget)
2256 {
2257         MessageView *messageview = (MessageView *)data;
2258         MsgInfo *msginfo, *full_msginfo;
2259         gchar *from;
2260         GtkWidget *image = NULL;
2261         GdkPixbuf *picture = NULL;
2262
2263         if (!messageview->msginfo || !messageview->msginfo->from) 
2264                 return;
2265
2266         msginfo = messageview->msginfo;
2267         Xstrdup_a(from, msginfo->from, return);
2268         eliminate_address_comment(from);
2269         extract_address(from);
2270         
2271         full_msginfo = procmsg_msginfo_get_full_info(msginfo);
2272         if (full_msginfo &&
2273             full_msginfo->extradata &&
2274             full_msginfo->extradata->face) {
2275                 image = face_get_from_header(full_msginfo->extradata->face);
2276         } 
2277 #if HAVE_LIBCOMPFACE
2278         else if (full_msginfo &&
2279                  full_msginfo->extradata &&
2280                  full_msginfo->extradata->xface) {
2281                 image = xface_get_from_header(full_msginfo->extradata->xface,
2282                                 &messageview->mainwin->summaryview->ctree->style->white,
2283                                 messageview->window->window);   
2284         }
2285 #endif
2286         procmsg_msginfo_free(full_msginfo);
2287         if (image)
2288                 picture = gtk_image_get_pixbuf(GTK_IMAGE(image));
2289
2290         addressbook_add_contact(msginfo->fromname, from, NULL, picture);
2291
2292         if (image)
2293                 gtk_widget_destroy(image);
2294 }
2295
2296 static void create_filter_cb(gpointer data, guint action, GtkWidget *widget)
2297 {
2298         MessageView *messageview = (MessageView *)data;
2299         FolderItem * item;
2300         
2301         if (!messageview->msginfo) return;
2302         
2303         item = messageview->msginfo->folder;
2304         summary_msginfo_filter_open(item,  messageview->msginfo,
2305                                     (PrefsFilterType)action, 0);
2306 }
2307
2308 static void create_processing_cb(gpointer data, guint action,
2309                                  GtkWidget *widget)
2310 {
2311         MessageView *messageview = (MessageView *)data;
2312         FolderItem * item;
2313         
2314         if (!messageview->msginfo) return;
2315         
2316         item = messageview->msginfo->folder;
2317         summary_msginfo_filter_open(item,  messageview->msginfo,
2318                                     (PrefsFilterType)action, 1);
2319 }
2320
2321 static void open_urls_cb(gpointer data, guint action, GtkWidget *widget)
2322 {
2323         MessageView *messageview = (MessageView *)data;
2324         messageview_list_urls(messageview);
2325 }
2326
2327 static void about_cb(gpointer data, guint action, GtkWidget *widget)
2328 {
2329         about_show();
2330 }
2331
2332 static gboolean messageview_update_msg(gpointer source, gpointer data)
2333 {
2334         MsgInfoUpdate *msginfo_update = (MsgInfoUpdate *) source;
2335         MessageView *messageview = (MessageView *)data;
2336
2337         if (messageview->msginfo != msginfo_update->msginfo)
2338                 return FALSE;
2339
2340         if (msginfo_update->flags & MSGINFO_UPDATE_DELETED) {
2341                 MsgInfo *old_msginfo = messageview->msginfo;
2342                 messageview_clear(messageview);
2343                 messageview_update(messageview, old_msginfo);
2344         }
2345
2346         return FALSE;
2347 }
2348
2349 void messageview_set_menu_sensitive(MessageView *messageview)
2350 {
2351         GtkItemFactory *ifactory;
2352         GtkWidget *menuitem = NULL;
2353
2354         if (!messageview || !messageview->new_window) 
2355                 return;
2356         /* do some smart things */
2357         if (!messageview->menubar) return;
2358         ifactory = gtk_item_factory_from_widget(messageview->menubar);
2359         if (!ifactory) return;
2360
2361         if (prefs_common.hide_quotes) {
2362                 menuitem = NULL;
2363                 if (prefs_common.hide_quotes == 1)
2364                         menuitem = gtk_item_factory_get_widget(ifactory, 
2365                                         "/View/Quotes/Fold all");
2366                 if (prefs_common.hide_quotes == 2)
2367                         menuitem = gtk_item_factory_get_widget(ifactory, 
2368                                         "/View/Quotes/Fold from level 2");
2369                 if (prefs_common.hide_quotes == 3)
2370                         menuitem = gtk_item_factory_get_widget(ifactory, 
2371                                         "/View/Quotes/Fold from level 3");
2372                 gtk_check_menu_item_set_active
2373                         (GTK_CHECK_MENU_ITEM(menuitem),
2374                          TRUE);
2375         }
2376 }
2377
2378 void messageview_learn (MessageView *msgview, gboolean is_spam)
2379 {
2380         if (is_spam) {
2381                 if (procmsg_spam_learner_learn(msgview->msginfo, NULL, TRUE) == 0)
2382                         procmsg_msginfo_set_flags(msgview->msginfo, MSG_SPAM, 0);
2383                 else
2384                         log_error(LOG_PROTOCOL, _("An error happened while learning.\n"));
2385                 
2386         } else {
2387                 if (procmsg_spam_learner_learn(msgview->msginfo, NULL, FALSE) == 0)
2388                         procmsg_msginfo_unset_flags(msgview->msginfo, MSG_SPAM, 0);
2389                 else
2390                         log_error(LOG_PROTOCOL, _("An error happened while learning.\n"));
2391         }
2392         if (msgview->toolbar)
2393                 toolbar_set_learn_button
2394                         (msgview->toolbar,
2395                          MSG_IS_SPAM(msgview->msginfo->flags)?LEARN_HAM:LEARN_SPAM);
2396         else
2397                 toolbar_set_learn_button
2398                         (msgview->mainwin->toolbar,
2399                          MSG_IS_SPAM(msgview->msginfo->flags)?LEARN_HAM:LEARN_SPAM);
2400 }
2401
2402 void messageview_list_urls (MessageView *msgview)
2403 {
2404         GSList *cur = msgview->mimeview->textview->uri_list;
2405         GSList *newlist = NULL;
2406         for (; cur; cur = cur->next) {
2407                 ClickableText *uri = (ClickableText *)cur->data;
2408                 if (uri->uri &&
2409                     (!g_ascii_strncasecmp(uri->uri, "ftp.", 4) ||
2410                      !g_ascii_strncasecmp(uri->uri, "ftp:", 4) ||
2411                      !g_ascii_strncasecmp(uri->uri, "www.", 4) ||
2412                      !g_ascii_strncasecmp(uri->uri, "http:", 5) ||
2413                      !g_ascii_strncasecmp(uri->uri, "https:", 6)))
2414                         newlist = g_slist_prepend(newlist, uri);
2415         }
2416         newlist = g_slist_reverse(newlist);
2417         uri_opener_open(msgview, newlist);
2418         g_slist_free(newlist);
2419 }