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