2006-11-09 [wwp] 2.6.0cvs21
[claws.git] / src / compose.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 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 2 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #ifndef PANGO_ENABLE_ENGINE
27 #  define PANGO_ENABLE_ENGINE
28 #endif
29
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <gtk/gtkmain.h>
34 #include <gtk/gtkmenu.h>
35 #include <gtk/gtkmenuitem.h>
36 #include <gtk/gtkitemfactory.h>
37 #include <gtk/gtkcheckmenuitem.h>
38 #include <gtk/gtkoptionmenu.h>
39 #include <gtk/gtkwidget.h>
40 #include <gtk/gtkvpaned.h>
41 #include <gtk/gtkentry.h>
42 #include <gtk/gtkeditable.h>
43 #include <gtk/gtkwindow.h>
44 #include <gtk/gtksignal.h>
45 #include <gtk/gtkvbox.h>
46 #include <gtk/gtkcontainer.h>
47 #include <gtk/gtkhandlebox.h>
48 #include <gtk/gtktoolbar.h>
49 #include <gtk/gtktable.h>
50 #include <gtk/gtkhbox.h>
51 #include <gtk/gtklabel.h>
52 #include <gtk/gtkscrolledwindow.h>
53 #include <gtk/gtktreeview.h>
54 #include <gtk/gtkliststore.h>
55 #include <gtk/gtktreeselection.h>
56 #include <gtk/gtktreemodel.h>
57
58 #include <gtk/gtkdnd.h>
59 #include <gtk/gtkclipboard.h>
60 #include <pango/pango-break.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <ctype.h>
65 #include <sys/types.h>
66 #include <sys/stat.h>
67 #include <unistd.h>
68 #include <time.h>
69 #include <stdlib.h>
70 #if HAVE_SYS_WAIT_H
71 #  include <sys/wait.h>
72 #endif
73 #include <signal.h>
74 #include <errno.h>
75 #ifndef G_OS_WIN32  /* fixme we should have a configure test. */
76 #include <libgen.h>
77 #endif
78
79 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
80 #  include <wchar.h>
81 #  include <wctype.h>
82 #endif
83
84 #include "sylpheed.h"
85 #include "main.h"
86 #include "mainwindow.h"
87 #include "compose.h"
88 #include "addressbook.h"
89 #include "folderview.h"
90 #include "procmsg.h"
91 #include "menu.h"
92 #include "stock_pixmap.h"
93 #include "send_message.h"
94 #include "imap.h"
95 #include "news.h"
96 #include "customheader.h"
97 #include "prefs_common.h"
98 #include "prefs_account.h"
99 #include "action.h"
100 #include "account.h"
101 #include "filesel.h"
102 #include "procheader.h"
103 #include "procmime.h"
104 #include "statusbar.h"
105 #include "about.h"
106 #include "base64.h"
107 #include "quoted-printable.h"
108 #include "codeconv.h"
109 #include "utils.h"
110 #include "gtkutils.h"
111 #include "socket.h"
112 #include "alertpanel.h"
113 #include "manage_window.h"
114 #include "gtkshruler.h"
115 #include "folder.h"
116 #include "addr_compl.h"
117 #include "quote_fmt.h"
118 #include "undo.h"
119 #include "foldersel.h"
120 #include "toolbar.h"
121 #include "inc.h"
122 #include "message_search.h"
123 #include "combobox.h"
124
125 enum
126 {
127         COL_MIMETYPE = 0,
128         COL_SIZE     = 1,
129         COL_NAME     = 2,
130         COL_DATA     = 3,
131         COL_AUTODATA = 4,
132         N_COL_COLUMNS
133 };
134
135 #define N_ATTACH_COLS   (N_COL_COLUMNS)
136
137 typedef enum
138 {
139         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BEGINNING_OF_LINE,
140         COMPOSE_CALL_ADVANCED_ACTION_MOVE_FORWARD_CHARACTER,
141         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BACKWARD_CHARACTER,
142         COMPOSE_CALL_ADVANCED_ACTION_MOVE_FORWARD_WORD,
143         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BACKWARD_WORD,
144         COMPOSE_CALL_ADVANCED_ACTION_MOVE_END_OF_LINE,
145         COMPOSE_CALL_ADVANCED_ACTION_MOVE_NEXT_LINE,
146         COMPOSE_CALL_ADVANCED_ACTION_MOVE_PREVIOUS_LINE,
147         COMPOSE_CALL_ADVANCED_ACTION_DELETE_FORWARD_CHARACTER,
148         COMPOSE_CALL_ADVANCED_ACTION_DELETE_BACKWARD_CHARACTER,
149         COMPOSE_CALL_ADVANCED_ACTION_DELETE_FORWARD_WORD,
150         COMPOSE_CALL_ADVANCED_ACTION_DELETE_BACKWARD_WORD,
151         COMPOSE_CALL_ADVANCED_ACTION_DELETE_LINE,
152         COMPOSE_CALL_ADVANCED_ACTION_DELETE_LINE_N,
153         COMPOSE_CALL_ADVANCED_ACTION_DELETE_TO_LINE_END
154 } ComposeCallAdvancedAction;
155
156 typedef enum
157 {
158         PRIORITY_HIGHEST = 1,
159         PRIORITY_HIGH,
160         PRIORITY_NORMAL,
161         PRIORITY_LOW,
162         PRIORITY_LOWEST
163 } PriorityLevel;
164
165 typedef enum
166 {
167         COMPOSE_INSERT_SUCCESS,
168         COMPOSE_INSERT_READ_ERROR,
169         COMPOSE_INSERT_INVALID_CHARACTER,
170         COMPOSE_INSERT_NO_FILE
171 } ComposeInsertResult;
172
173 typedef enum
174 {
175         COMPOSE_QUIT_EDITING,
176         COMPOSE_KEEP_EDITING,
177         COMPOSE_AUTO_SAVE
178 } ComposeDraftAction;
179
180 typedef enum
181 {
182         COMPOSE_WRITE_FOR_SEND,
183         COMPOSE_WRITE_FOR_STORE
184 } ComposeWriteType;
185
186 #define B64_LINE_SIZE           57
187 #define B64_BUFFSIZE            77
188
189 #define MAX_REFERENCES_LEN      999
190
191 static GList *compose_list = NULL;
192
193 Compose *compose_generic_new                    (PrefsAccount   *account,
194                                                  const gchar    *to,
195                                                  FolderItem     *item,
196                                                  GPtrArray      *attach_files,
197                                                  GList          *listAddress );
198
199 static Compose *compose_create                  (PrefsAccount   *account,
200                                                  ComposeMode     mode,
201                                                  gboolean batch);
202
203 static GtkWidget *compose_account_option_menu_create
204                                                 (Compose        *compose);
205 static void compose_set_out_encoding            (Compose        *compose);
206 static void compose_set_template_menu           (Compose        *compose);
207 static void compose_template_apply              (Compose        *compose,
208                                                  Template       *tmpl,
209                                                  gboolean        replace);
210 static void compose_destroy                     (Compose        *compose);
211
212 static void compose_entries_set                 (Compose        *compose,
213                                                  const gchar    *mailto);
214 static gint compose_parse_header                (Compose        *compose,
215                                                  MsgInfo        *msginfo);
216 static gchar *compose_parse_references          (const gchar    *ref,
217                                                  const gchar    *msgid);
218
219 static gchar *compose_quote_fmt                 (Compose        *compose,
220                                                  MsgInfo        *msginfo,
221                                                  const gchar    *fmt,
222                                                  const gchar    *qmark,
223                                                  const gchar    *body,
224                                                  gboolean        rewrap,
225                                                  const gchar *err_msg);
226
227 static void compose_reply_set_entry             (Compose        *compose,
228                                                  MsgInfo        *msginfo,
229                                                  gboolean        to_all,
230                                                  gboolean        to_ml,
231                                                  gboolean        to_sender,
232                                                  gboolean
233                                                  followup_and_reply_to);
234 static void compose_reedit_set_entry            (Compose        *compose,
235                                                  MsgInfo        *msginfo);
236
237 static void compose_insert_sig                  (Compose        *compose,
238                                                  gboolean        replace);
239 static gchar *compose_get_signature_str         (Compose        *compose);
240 static ComposeInsertResult compose_insert_file  (Compose        *compose,
241                                                  const gchar    *file);
242
243 static gboolean compose_attach_append           (Compose        *compose,
244                                                  const gchar    *file,
245                                                  const gchar    *type,
246                                                  const gchar    *content_type);
247 static void compose_attach_parts                (Compose        *compose,
248                                                  MsgInfo        *msginfo);
249
250 static void compose_beautify_paragraph          (Compose        *compose,
251                                                  GtkTextIter    *par_iter,
252                                                  gboolean        force);
253 static void compose_wrap_all                    (Compose        *compose);
254 static void compose_wrap_all_full               (Compose        *compose,
255                                                  gboolean        autowrap);
256
257 static void compose_set_title                   (Compose        *compose);
258 static void compose_select_account              (Compose        *compose,
259                                                  PrefsAccount   *account,
260                                                  gboolean        init);
261
262 static PrefsAccount *compose_current_mail_account(void);
263 /* static gint compose_send                     (Compose        *compose); */
264 static gboolean compose_check_for_valid_recipient
265                                                 (Compose        *compose);
266 static gboolean compose_check_entries           (Compose        *compose,
267                                                  gboolean       check_subject);
268 static gint compose_write_to_file               (Compose        *compose,
269                                                  FILE           *fp,
270                                                  gint            action,
271                                                  gboolean        attach_parts);
272 static gint compose_write_body_to_file          (Compose        *compose,
273                                                  const gchar    *file);
274 static gint compose_remove_reedit_target        (Compose        *compose,
275                                                  gboolean        force);
276 void compose_remove_draft                       (Compose        *compose);
277 static gint compose_queue_sub                   (Compose        *compose,
278                                                  gint           *msgnum,
279                                                  FolderItem     **item,
280                                                  gchar          **msgpath,
281                                                  gboolean       check_subject,
282                                                  gboolean       remove_reedit_target);
283 static void compose_add_attachments             (Compose        *compose,
284                                                  MimeInfo       *parent);
285 static gchar *compose_get_header                (Compose        *compose);
286
287 static void compose_convert_header              (Compose        *compose,
288                                                  gchar          *dest,
289                                                  gint            len,
290                                                  gchar          *src,
291                                                  gint            header_len,
292                                                  gboolean        addr_field);
293
294 static void compose_attach_info_free            (AttachInfo     *ainfo);
295 static void compose_attach_remove_selected      (Compose        *compose);
296
297 static void compose_attach_property             (Compose        *compose);
298 static void compose_attach_property_create      (gboolean       *cancelled);
299 static void attach_property_ok                  (GtkWidget      *widget,
300                                                  gboolean       *cancelled);
301 static void attach_property_cancel              (GtkWidget      *widget,
302                                                  gboolean       *cancelled);
303 static gint attach_property_delete_event        (GtkWidget      *widget,
304                                                  GdkEventAny    *event,
305                                                  gboolean       *cancelled);
306 static gboolean attach_property_key_pressed     (GtkWidget      *widget,
307                                                  GdkEventKey    *event,
308                                                  gboolean       *cancelled);
309
310 static void compose_exec_ext_editor             (Compose        *compose);
311 #ifdef G_OS_UNIX
312 static gint compose_exec_ext_editor_real        (const gchar    *file);
313 static gboolean compose_ext_editor_kill         (Compose        *compose);
314 static gboolean compose_input_cb                (GIOChannel     *source,
315                                                  GIOCondition    condition,
316                                                  gpointer        data);
317 static void compose_set_ext_editor_sensitive    (Compose        *compose,
318                                                  gboolean        sensitive);
319 #endif /* G_OS_UNIX */
320
321 static void compose_undo_state_changed          (UndoMain       *undostruct,
322                                                  gint            undo_state,
323                                                  gint            redo_state,
324                                                  gpointer        data);
325
326 static void compose_create_header_entry (Compose *compose);
327 static void compose_add_header_entry    (Compose *compose, gchar *header, gchar *text);
328 static void compose_remove_header_entries(Compose *compose);
329
330 static void compose_update_priority_menu_item(Compose * compose);
331 #if USE_ASPELL
332 static void compose_spell_menu_changed  (void *data);
333 #endif
334 static void compose_add_field_list      ( Compose *compose,
335                                           GList *listAddress );
336
337 /* callback functions */
338
339 static gboolean compose_edit_size_alloc (GtkEditable    *widget,
340                                          GtkAllocation  *allocation,
341                                          GtkSHRuler     *shruler);
342 static void account_activated           (GtkComboBox *optmenu,
343                                          gpointer        data);
344 static void attach_selected             (GtkTreeView    *tree_view, 
345                                          GtkTreePath    *tree_path,
346                                          GtkTreeViewColumn *column, 
347                                          Compose *compose);
348 static gboolean attach_button_pressed   (GtkWidget      *widget,
349                                          GdkEventButton *event,
350                                          gpointer        data);
351 static gboolean attach_key_pressed      (GtkWidget      *widget,
352                                          GdkEventKey    *event,
353                                          gpointer        data);
354
355 static void compose_send_cb             (gpointer        data,
356                                          guint           action,
357                                          GtkWidget      *widget);
358 static void compose_send_later_cb       (gpointer        data,
359                                          guint           action,
360                                          GtkWidget      *widget);
361
362 static void compose_draft_cb            (gpointer        data,
363                                          guint           action,
364                                          GtkWidget      *widget);
365
366 static void compose_attach_cb           (gpointer        data,
367                                          guint           action,
368                                          GtkWidget      *widget);
369 static void compose_insert_file_cb      (gpointer        data,
370                                          guint           action,
371                                          GtkWidget      *widget);
372 static void compose_insert_sig_cb       (gpointer        data,
373                                          guint           action,
374                                          GtkWidget      *widget);
375
376 static void compose_close_cb            (gpointer        data,
377                                          guint           action,
378                                          GtkWidget      *widget);
379
380 static void compose_set_encoding_cb     (gpointer        data,
381                                          guint           action,
382                                          GtkWidget      *widget);
383
384 static void compose_address_cb          (gpointer        data,
385                                          guint           action,
386                                          GtkWidget      *widget);
387 static void compose_template_activate_cb(GtkWidget      *widget,
388                                          gpointer        data);
389
390 static void compose_ext_editor_cb       (gpointer        data,
391                                          guint           action,
392                                          GtkWidget      *widget);
393
394 static gint compose_delete_cb           (GtkWidget      *widget,
395                                          GdkEventAny    *event,
396                                          gpointer        data);
397
398 static void compose_undo_cb             (Compose        *compose);
399 static void compose_redo_cb             (Compose        *compose);
400 static void compose_cut_cb              (Compose        *compose);
401 static void compose_copy_cb             (Compose        *compose);
402 static void compose_paste_cb            (Compose        *compose);
403 static void compose_paste_as_quote_cb   (Compose        *compose);
404 static void compose_paste_no_wrap_cb    (Compose        *compose);
405 static void compose_paste_wrap_cb       (Compose        *compose);
406 static void compose_allsel_cb           (Compose        *compose);
407
408 static void compose_advanced_action_cb  (Compose                   *compose,
409                                          ComposeCallAdvancedAction  action);
410
411 static void compose_grab_focus_cb       (GtkWidget      *widget,
412                                          Compose        *compose);
413
414 static void compose_changed_cb          (GtkTextBuffer  *textbuf,
415                                          Compose        *compose);
416
417 static void compose_wrap_cb             (gpointer        data,
418                                          guint           action,
419                                          GtkWidget      *widget);
420 static void compose_find_cb             (gpointer        data,
421                                          guint           action,
422                                          GtkWidget      *widget);
423 static void compose_toggle_autowrap_cb  (gpointer        data,
424                                          guint           action,
425                                          GtkWidget      *widget);
426
427 static void compose_toggle_ruler_cb     (gpointer        data,
428                                          guint           action,
429                                          GtkWidget      *widget);
430 static void compose_toggle_sign_cb      (gpointer        data,
431                                          guint           action,
432                                          GtkWidget      *widget);
433 static void compose_toggle_encrypt_cb   (gpointer        data,
434                                          guint           action,
435                                          GtkWidget      *widget);
436 static void compose_set_privacy_system_cb(GtkWidget      *widget,
437                                           gpointer        data);
438 static void compose_update_privacy_system_menu_item(Compose * compose, gboolean warn);
439 static void activate_privacy_system     (Compose *compose, 
440                                          PrefsAccount *account,
441                                          gboolean warn);
442 static void compose_use_signing(Compose *compose, gboolean use_signing);
443 static void compose_use_encryption(Compose *compose, gboolean use_encryption);
444 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
445                                              GtkWidget *widget);
446 static void compose_toggle_remove_refs_cb(gpointer data, guint action,
447                                              GtkWidget *widget);
448 static void compose_set_priority_cb     (gpointer        data,
449                                          guint           action,
450                                          GtkWidget      *widget);
451 static void compose_reply_change_mode   (gpointer        data,
452                                          guint           action,
453                                          GtkWidget      *widget);
454
455 static void compose_attach_drag_received_cb (GtkWidget          *widget,
456                                              GdkDragContext     *drag_context,
457                                              gint                x,
458                                              gint                y,
459                                              GtkSelectionData   *data,
460                                              guint               info,
461                                              guint               time,
462                                              gpointer            user_data);
463 static void compose_insert_drag_received_cb (GtkWidget          *widget,
464                                              GdkDragContext     *drag_context,
465                                              gint                x,
466                                              gint                y,
467                                              GtkSelectionData   *data,
468                                              guint               info,
469                                              guint               time,
470                                              gpointer            user_data);
471 static void compose_header_drag_received_cb (GtkWidget          *widget,
472                                              GdkDragContext     *drag_context,
473                                              gint                x,
474                                              gint                y,
475                                              GtkSelectionData   *data,
476                                              guint               info,
477                                              guint               time,
478                                              gpointer            user_data);
479
480 static gboolean compose_drag_drop           (GtkWidget *widget,
481                                              GdkDragContext *drag_context,
482                                              gint x, gint y,
483                                              guint time, gpointer user_data);
484
485 static void text_inserted               (GtkTextBuffer  *buffer,
486                                          GtkTextIter    *iter,
487                                          const gchar    *text,
488                                          gint            len,
489                                          Compose        *compose);
490 static Compose *compose_generic_reply(MsgInfo *msginfo, gboolean quote,
491                                   gboolean to_all, gboolean to_ml,
492                                   gboolean to_sender,
493                                   gboolean followup_and_reply_to,
494                                   const gchar *body);
495
496 gboolean compose_headerentry_changed_cb    (GtkWidget          *entry,
497                                             ComposeHeaderEntry *headerentry);
498 gboolean compose_headerentry_key_press_event_cb(GtkWidget              *entry,
499                                             GdkEventKey        *event,
500                                             ComposeHeaderEntry *headerentry);
501
502 static void compose_show_first_last_header (Compose *compose, gboolean show_first);
503
504 static void compose_allow_user_actions (Compose *compose, gboolean allow);
505
506 #if USE_ASPELL
507 static void compose_check_all              (Compose *compose);
508 static void compose_highlight_all          (Compose *compose);
509 static void compose_check_backwards        (Compose *compose);
510 static void compose_check_forwards_go      (Compose *compose);
511 #endif
512
513 static gint compose_defer_auto_save_draft       (Compose        *compose);
514 static PrefsAccount *compose_guess_forward_account_from_msginfo (MsgInfo *msginfo);
515
516 static MsgInfo *compose_msginfo_new_from_compose(Compose *compose);
517
518 static GtkItemFactoryEntry compose_popup_entries[] =
519 {
520         {N_("/_Add..."),        NULL, compose_attach_cb, 0, NULL},
521         {N_("/_Remove"),        NULL, compose_attach_remove_selected, 0, NULL},
522         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
523         {N_("/_Properties..."), NULL, compose_attach_property, 0, NULL}
524 };
525
526 static GtkItemFactoryEntry compose_entries[] =
527 {
528         {N_("/_Message"),                               NULL, NULL, 0, "<Branch>"},
529         {N_("/_Message/_Send"),         "<control>Return",
530                                         compose_send_cb, 0, NULL},
531         {N_("/_Message/Send _later"),   "<shift><control>S",
532                                         compose_send_later_cb,  0, NULL},
533         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
534         {N_("/_Message/_Attach file"),          "<control>M", compose_attach_cb,      0, NULL},
535         {N_("/_Message/_Insert file"),          "<control>I", compose_insert_file_cb, 0, NULL},
536         {N_("/_Message/Insert si_gnature"),     "<control>G", compose_insert_sig_cb,  0, NULL},
537         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
538         {N_("/_Message/_Save"),
539                                                 "<control>S", compose_draft_cb, COMPOSE_KEEP_EDITING, NULL},
540         {N_("/_Message/---"),                   NULL, NULL, 0, "<Separator>"},
541         {N_("/_Message/_Close"),                        "<control>W", compose_close_cb, 0, NULL},
542
543         {N_("/_Edit"),                  NULL, NULL, 0, "<Branch>"},
544         {N_("/_Edit/_Undo"),            "<control>Z", compose_undo_cb, 0, NULL},
545         {N_("/_Edit/_Redo"),            "<control>Y", compose_redo_cb, 0, NULL},
546         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
547         {N_("/_Edit/Cu_t"),             "<control>X", compose_cut_cb,    0, NULL},
548         {N_("/_Edit/_Copy"),            "<control>C", compose_copy_cb,   0, NULL},
549         {N_("/_Edit/_Paste"),           "<control>V", compose_paste_cb,  0, NULL},
550         {N_("/_Edit/Special paste"),    NULL, NULL, 0, "<Branch>"},
551         {N_("/_Edit/Special paste/as _quotation"),
552                                         NULL, compose_paste_as_quote_cb, 0, NULL},
553         {N_("/_Edit/Special paste/_wrapped"),
554                                         NULL, compose_paste_wrap_cb, 0, NULL},
555         {N_("/_Edit/Special paste/_unwrapped"),
556                                         NULL, compose_paste_no_wrap_cb, 0, NULL},
557         {N_("/_Edit/Select _all"),      "<control>A", compose_allsel_cb, 0, NULL},
558         {N_("/_Edit/A_dvanced"),        NULL, NULL, 0, "<Branch>"},
559         {N_("/_Edit/A_dvanced/Move a character backward"),
560                                         "<shift><control>B",
561                                         compose_advanced_action_cb,
562                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BACKWARD_CHARACTER,
563                                         NULL},
564         {N_("/_Edit/A_dvanced/Move a character forward"),
565                                         "<shift><control>F",
566                                         compose_advanced_action_cb,
567                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_FORWARD_CHARACTER,
568                                         NULL},
569         {N_("/_Edit/A_dvanced/Move a word backward"),
570                                         NULL, /* "<alt>B" */
571                                         compose_advanced_action_cb,
572                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BACKWARD_WORD,
573                                         NULL},
574         {N_("/_Edit/A_dvanced/Move a word forward"),
575                                         NULL, /* "<alt>F" */
576                                         compose_advanced_action_cb,
577                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_FORWARD_WORD,
578                                         NULL},
579         {N_("/_Edit/A_dvanced/Move to beginning of line"),
580                                         NULL, /* "<control>A" */
581                                         compose_advanced_action_cb,
582                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BEGINNING_OF_LINE,
583                                         NULL},
584         {N_("/_Edit/A_dvanced/Move to end of line"),
585                                         "<control>E",
586                                         compose_advanced_action_cb,
587                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_END_OF_LINE,
588                                         NULL},
589         {N_("/_Edit/A_dvanced/Move to previous line"),
590                                         "<control>P",
591                                         compose_advanced_action_cb,
592                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_PREVIOUS_LINE,
593                                         NULL},
594         {N_("/_Edit/A_dvanced/Move to next line"),
595                                         "<control>N",
596                                         compose_advanced_action_cb,
597                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_NEXT_LINE,
598                                         NULL},
599         {N_("/_Edit/A_dvanced/Delete a character backward"),
600                                         "<control>H",
601                                         compose_advanced_action_cb,
602                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_BACKWARD_CHARACTER,
603                                         NULL},
604         {N_("/_Edit/A_dvanced/Delete a character forward"),
605                                         "<control>D",
606                                         compose_advanced_action_cb,
607                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_FORWARD_CHARACTER,
608                                         NULL},
609         {N_("/_Edit/A_dvanced/Delete a word backward"),
610                                         NULL, /* "<control>W" */
611                                         compose_advanced_action_cb,
612                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_BACKWARD_WORD,
613                                         NULL},
614         {N_("/_Edit/A_dvanced/Delete a word forward"),
615                                         NULL, /* "<alt>D", */
616                                         compose_advanced_action_cb,
617                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_FORWARD_WORD,
618                                         NULL},
619         {N_("/_Edit/A_dvanced/Delete line"),
620                                         "<control>U",
621                                         compose_advanced_action_cb,
622                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_LINE,
623                                         NULL},
624         {N_("/_Edit/A_dvanced/Delete entire line"),
625                                         NULL,
626                                         compose_advanced_action_cb,
627                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_LINE_N,
628                                         NULL},
629         {N_("/_Edit/A_dvanced/Delete to end of line"),
630                                         "<control>K",
631                                         compose_advanced_action_cb,
632                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_TO_LINE_END,
633                                         NULL},
634         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
635         {N_("/_Edit/_Find"),
636                                         "<control>F", compose_find_cb, 0, NULL},
637         {N_("/_Edit/---"),                      NULL, NULL, 0, "<Separator>"},
638         {N_("/_Edit/_Wrap current paragraph"),
639                                         "<control>L", compose_wrap_cb, 0, NULL},
640         {N_("/_Edit/Wrap all long _lines"),
641                                         "<control><alt>L", compose_wrap_cb, 1, NULL},
642         {N_("/_Edit/Aut_o wrapping"),   "<shift><control>L", compose_toggle_autowrap_cb, 0, "<ToggleItem>"},
643         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
644         {N_("/_Edit/Edit with e_xternal editor"),
645                                         "<shift><control>X", compose_ext_editor_cb, 0, NULL},
646 #if USE_ASPELL
647         {N_("/_Spelling"),              NULL, NULL, 0, "<Branch>"},
648         {N_("/_Spelling/_Check all or check selection"),
649                                         NULL, compose_check_all, 0, NULL},
650         {N_("/_Spelling/_Highlight all misspelled words"),
651                                         NULL, compose_highlight_all, 0, NULL},
652         {N_("/_Spelling/Check _backwards misspelled word"),
653                                         NULL, compose_check_backwards , 0, NULL},
654         {N_("/_Spelling/_Forward to next misspelled word"),
655                                         NULL, compose_check_forwards_go, 0, NULL},
656         {N_("/_Spelling/---"),          NULL, NULL, 0, "<Separator>"},
657         {N_("/_Spelling/Options"),
658                                         NULL, NULL, 0, "<Branch>"},
659 #endif
660         {N_("/_Options"),               NULL, NULL, 0, "<Branch>"},
661         {N_("/_Options/Reply _mode"),           NULL, NULL,   0, "<Branch>"},
662         {N_("/_Options/Reply _mode/_Normal"),   NULL, compose_reply_change_mode,   COMPOSE_REPLY, "<RadioItem>"},
663         {N_("/_Options/Reply _mode/_All"),              NULL, compose_reply_change_mode,   COMPOSE_REPLY_TO_ALL, "/Options/Reply mode/Normal"},
664         {N_("/_Options/Reply _mode/_Sender"),           NULL, compose_reply_change_mode,   COMPOSE_REPLY_TO_SENDER, "/Options/Reply mode/Normal"},
665         {N_("/_Options/Reply _mode/_Mailing-list"),     NULL, compose_reply_change_mode,   COMPOSE_REPLY_TO_LIST, "/Options/Reply mode/Normal"},
666         {N_("/_Options/---"),           NULL,           NULL,   0, "<Separator>"},
667         {N_("/_Options/Privacy _System"),               NULL, NULL,   0, "<Branch>"},
668         {N_("/_Options/Privacy _System/None"),  NULL, NULL,   0, "<RadioItem>"},
669         {N_("/_Options/Si_gn"),         NULL, compose_toggle_sign_cb   , 0, "<ToggleItem>"},
670         {N_("/_Options/_Encrypt"),      NULL, compose_toggle_encrypt_cb, 0, "<ToggleItem>"},
671         {N_("/_Options/---"),           NULL,           NULL,   0, "<Separator>"},
672         {N_("/_Options/_Priority"),     NULL,           NULL,   0, "<Branch>"},
673         {N_("/_Options/Priority/_Highest"), NULL, compose_set_priority_cb, PRIORITY_HIGHEST, "<RadioItem>"},
674         {N_("/_Options/Priority/Hi_gh"),    NULL, compose_set_priority_cb, PRIORITY_HIGH, "/Options/Priority/Highest"},
675         {N_("/_Options/Priority/_Normal"),  NULL, compose_set_priority_cb, PRIORITY_NORMAL, "/Options/Priority/Highest"},
676         {N_("/_Options/Priority/Lo_w"),    NULL, compose_set_priority_cb, PRIORITY_LOW, "/Options/Priority/Highest"},
677         {N_("/_Options/Priority/_Lowest"),  NULL, compose_set_priority_cb, PRIORITY_LOWEST, "/Options/Priority/Highest"},
678         {N_("/_Options/---"),           NULL,           NULL,   0, "<Separator>"},
679         {N_("/_Options/_Request Return Receipt"),       NULL, compose_toggle_return_receipt_cb, 0, "<ToggleItem>"},
680         {N_("/_Options/---"),           NULL,           NULL,   0, "<Separator>"},
681         {N_("/_Options/Remo_ve references"),    NULL, compose_toggle_remove_refs_cb, 0, "<ToggleItem>"},
682         {N_("/_Options/---"),           NULL,           NULL,   0, "<Separator>"},
683
684 #define ENC_ACTION(action) \
685         NULL, compose_set_encoding_cb, action, \
686         "/Options/Character encoding/Automatic"
687
688         {N_("/_Options/Character _encoding"), NULL, NULL, 0, "<Branch>"},
689         {N_("/_Options/Character _encoding/_Automatic"),
690                         NULL, compose_set_encoding_cb, C_AUTO, "<RadioItem>"},
691         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
692
693         {N_("/_Options/Character _encoding/7bit ascii (US-ASC_II)"),
694          ENC_ACTION(C_US_ASCII)},
695         {N_("/_Options/Character _encoding/Unicode (_UTF-8)"),
696          ENC_ACTION(C_UTF_8)},
697         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
698
699         {N_("/_Options/Character _encoding/Western European (ISO-8859-_1)"),
700          ENC_ACTION(C_ISO_8859_1)},
701         {N_("/_Options/Character _encoding/Western European (ISO-8859-15)"),
702          ENC_ACTION(C_ISO_8859_15)},
703         {N_("/_Options/Character _encoding/Western European (Windows-1252)"),
704          ENC_ACTION(C_WINDOWS_1252)},
705         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
706
707         {N_("/_Options/Character _encoding/Central European (ISO-8859-_2)"),
708          ENC_ACTION(C_ISO_8859_2)},
709         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
710
711         {N_("/_Options/Character _encoding/_Baltic (ISO-8859-13)"),
712          ENC_ACTION(C_ISO_8859_13)},
713         {N_("/_Options/Character _encoding/Baltic (ISO-8859-_4)"),
714          ENC_ACTION(C_ISO_8859_4)},
715         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
716
717         {N_("/_Options/Character _encoding/Greek (ISO-8859-_7)"),
718          ENC_ACTION(C_ISO_8859_7)},
719         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
720
721         {N_("/_Options/Character _encoding/Hebrew (ISO-8859-_8)"),
722          ENC_ACTION(C_ISO_8859_8)},
723         {N_("/_Options/Character _encoding/Hebrew (Windows-1255)"),
724          ENC_ACTION(C_WINDOWS_1255)},
725         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
726
727         {N_("/_Options/Character _encoding/Arabic (ISO-8859-_6)"),
728          ENC_ACTION(C_ISO_8859_6)},
729         {N_("/_Options/Character _encoding/Arabic (Windows-1256)"),
730          ENC_ACTION(C_CP1256)},
731         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
732
733         {N_("/_Options/Character _encoding/Turkish (ISO-8859-_9)"),
734          ENC_ACTION(C_ISO_8859_9)},
735         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
736
737         {N_("/_Options/Character _encoding/Cyrillic (ISO-8859-_5)"),
738          ENC_ACTION(C_ISO_8859_5)},
739         {N_("/_Options/Character _encoding/Cyrillic (KOI8-_R)"),
740          ENC_ACTION(C_KOI8_R)},
741         {N_("/_Options/Character _encoding/Cyrillic (KOI8-U)"),
742          ENC_ACTION(C_KOI8_U)},
743         {N_("/_Options/Character _encoding/Cyrillic (Windows-1251)"),
744          ENC_ACTION(C_WINDOWS_1251)},
745         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
746
747         {N_("/_Options/Character _encoding/Japanese (ISO-2022-_JP)"),
748          ENC_ACTION(C_ISO_2022_JP)},
749         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
750
751         {N_("/_Options/Character _encoding/Simplified Chinese (_GB2312)"),
752          ENC_ACTION(C_GB2312)},
753         {N_("/_Options/Character _encoding/Simplified Chinese (GBK)"),
754          ENC_ACTION(C_GBK)},
755         {N_("/_Options/Character _encoding/Traditional Chinese (_Big5)"),
756          ENC_ACTION(C_BIG5)},
757         {N_("/_Options/Character _encoding/Traditional Chinese (EUC-_TW)"),
758          ENC_ACTION(C_EUC_TW)},
759         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
760
761         {N_("/_Options/Character _encoding/Korean (EUC-_KR)"),
762          ENC_ACTION(C_EUC_KR)},
763         {N_("/_Options/Character _encoding/---"), NULL, NULL, 0, "<Separator>"},
764
765         {N_("/_Options/Character _encoding/Thai (TIS-620)"),
766          ENC_ACTION(C_TIS_620)},
767         {N_("/_Options/Character _encoding/Thai (Windows-874)"),
768          ENC_ACTION(C_WINDOWS_874)},
769
770         {N_("/_Tools"),                 NULL, NULL, 0, "<Branch>"},
771         {N_("/_Tools/Show _ruler"),     NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
772         {N_("/_Tools/_Address book"),   "<shift><control>A", compose_address_cb , 0, NULL},
773         {N_("/_Tools/_Template"),       NULL, NULL, 0, "<Branch>"},
774         {N_("/_Tools/Actio_ns"),        NULL, NULL, 0, "<Branch>"},
775         {N_("/_Help"),                  NULL, NULL, 0, "<Branch>"},
776         {N_("/_Help/_About"),           NULL, about_show, 0, NULL}
777 };
778
779 static GtkTargetEntry compose_mime_types[] =
780 {
781         {"text/uri-list", 0, 0},
782         {"UTF8_STRING", 0, 0},
783         {"text/plain", 0, 0}
784 };
785
786 static gboolean compose_put_existing_to_front(MsgInfo *info)
787 {
788         GList *compose_list = compose_get_compose_list();
789         GList *elem = NULL;
790         
791         if (compose_list) {
792                 for (elem = compose_list; elem != NULL && elem->data != NULL; 
793                      elem = elem->next) {
794                         Compose *c = (Compose*)elem->data;
795
796                         if (!c->targetinfo || !c->targetinfo->msgid ||
797                             !info->msgid)
798                                 continue;
799
800                         if (!strcmp(c->targetinfo->msgid, info->msgid)) {
801                                 gtkut_window_popup(c->window);
802                                 return TRUE;
803                         }
804                 }
805         }
806         return FALSE;
807 }
808
809 static GdkColor quote_color1 = 
810         {(gulong)0, (gushort)0, (gushort)0, (gushort)0};
811 static GdkColor quote_color2 = 
812         {(gulong)0, (gushort)0, (gushort)0, (gushort)0};
813 static GdkColor quote_color3 = 
814         {(gulong)0, (gushort)0, (gushort)0, (gushort)0};
815
816 static GdkColor quote_bgcolor1 = 
817         {(gulong)0, (gushort)0, (gushort)0, (gushort)0};
818 static GdkColor quote_bgcolor2 = 
819         {(gulong)0, (gushort)0, (gushort)0, (gushort)0};
820 static GdkColor quote_bgcolor3 = 
821         {(gulong)0, (gushort)0, (gushort)0, (gushort)0};
822
823 static GdkColor signature_color = {
824         (gulong)0,
825         (gushort)0x7fff,
826         (gushort)0x7fff,
827         (gushort)0x7fff
828 };
829
830 static GdkColor uri_color = {
831         (gulong)0,
832         (gushort)0,
833         (gushort)0,
834         (gushort)0
835 };
836
837 static void compose_create_tags(GtkTextView *text, Compose *compose)
838 {
839         GtkTextBuffer *buffer;
840         GdkColor black = {(gulong)0, (gushort)0, (gushort)0, (gushort)0};
841         GdkColormap *cmap;
842         GdkColor color[8];
843         gboolean success[8];
844         int i;
845
846         buffer = gtk_text_view_get_buffer(text);
847
848         if (prefs_common.enable_color) {
849                 /* grab the quote colors, converting from an int to a GdkColor */
850                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_col,
851                                                &quote_color1);
852                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_col,
853                                                &quote_color2);
854                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_col,
855                                                &quote_color3);
856                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_bgcol,
857                                                &quote_bgcolor1);
858                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_bgcol,
859                                                &quote_bgcolor2);
860                 gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_bgcol,
861                                                &quote_bgcolor3);
862                 gtkut_convert_int_to_gdk_color(prefs_common.signature_col,
863                                                &signature_color);
864                 gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
865                                                &uri_color);
866         } else {
867                 signature_color = quote_color1 = quote_color2 = quote_color3 = 
868                         quote_bgcolor1 = quote_bgcolor2 = quote_bgcolor3 = uri_color = black;
869         }
870
871         if (prefs_common.enable_color && prefs_common.enable_bgcolor) {
872                 gtk_text_buffer_create_tag(buffer, "quote0",
873                                            "foreground-gdk", &quote_color1,
874                                            "paragraph-background-gdk", &quote_bgcolor1,
875                                            NULL);
876                 gtk_text_buffer_create_tag(buffer, "quote1",
877                                            "foreground-gdk", &quote_color2,
878                                            "paragraph-background-gdk", &quote_bgcolor2,
879                                            NULL);
880                 gtk_text_buffer_create_tag(buffer, "quote2",
881                                            "foreground-gdk", &quote_color3,
882                                            "paragraph-background-gdk", &quote_bgcolor3,
883                                            NULL);
884         } else {
885                 gtk_text_buffer_create_tag(buffer, "quote0",
886                                            "foreground-gdk", &quote_color1,
887                                            NULL);
888                 gtk_text_buffer_create_tag(buffer, "quote1",
889                                            "foreground-gdk", &quote_color2,
890                                            NULL);
891                 gtk_text_buffer_create_tag(buffer, "quote2",
892                                            "foreground-gdk", &quote_color3,
893                                            NULL);
894         }
895         
896         gtk_text_buffer_create_tag(buffer, "signature",
897                                    "foreground-gdk", &signature_color,
898                                    NULL);
899         gtk_text_buffer_create_tag(buffer, "link",
900                                         "foreground-gdk", &uri_color,
901                                          NULL);
902         compose->no_wrap_tag = gtk_text_buffer_create_tag(buffer, "no_wrap", NULL);
903         compose->no_join_tag = gtk_text_buffer_create_tag(buffer, "no_join", NULL);
904
905         color[0] = quote_color1;
906         color[1] = quote_color2;
907         color[2] = quote_color3;
908         color[3] = quote_bgcolor1;
909         color[4] = quote_bgcolor2;
910         color[5] = quote_bgcolor3;
911         color[6] = signature_color;
912         color[7] = uri_color;
913         cmap = gdk_drawable_get_colormap(compose->window->window);
914         gdk_colormap_alloc_colors(cmap, color, 8, FALSE, TRUE, success);
915
916         for (i = 0; i < 8; i++) {
917                 if (success[i] == FALSE) {
918                         GtkStyle *style;
919
920                         g_warning("Compose: color allocation failed.\n");
921                         style = gtk_widget_get_style(GTK_WIDGET(text));
922                         quote_color1 = quote_color2 = quote_color3 = 
923                                 quote_bgcolor1 = quote_bgcolor2 = quote_bgcolor3 = 
924                                 signature_color = uri_color = black;
925                 }
926         }
927 }
928
929 Compose *compose_new(PrefsAccount *account, const gchar *mailto,
930                      GPtrArray *attach_files)
931 {
932         return compose_generic_new(account, mailto, NULL, attach_files, NULL);
933 }
934
935 Compose *compose_new_with_folderitem(PrefsAccount *account, FolderItem *item, const gchar *mailto)
936 {
937         return compose_generic_new(account, mailto, item, NULL, NULL);
938 }
939
940 Compose *compose_new_with_list( PrefsAccount *account, GList *listAddress )
941 {
942         return compose_generic_new( account, NULL, NULL, NULL, listAddress );
943 }
944
945 Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderItem *item,
946                              GPtrArray *attach_files, GList *listAddress )
947 {
948         Compose *compose;
949         GtkTextView *textview;
950         GtkTextBuffer *textbuf;
951         GtkTextIter iter;
952         GtkItemFactory *ifactory;
953
954         if (item && item->prefs && item->prefs->enable_default_account)
955                 account = account_find_from_id(item->prefs->default_account);
956
957         if (!account) account = cur_account;
958         g_return_val_if_fail(account != NULL, NULL);
959
960         compose = compose_create(account, COMPOSE_NEW, FALSE);
961
962         ifactory = gtk_item_factory_from_widget(compose->menubar);
963
964         compose->replyinfo = NULL;
965         compose->fwdinfo   = NULL;
966
967         textview = GTK_TEXT_VIEW(compose->text);
968         textbuf = gtk_text_view_get_buffer(textview);
969         compose_create_tags(textview, compose);
970
971         undo_block(compose->undostruct);
972 #ifdef USE_ASPELL
973         if (item && item->prefs && item->prefs->enable_default_dictionary &&
974             compose->gtkaspell) 
975                 gtkaspell_change_dict(compose->gtkaspell, 
976                     item->prefs->default_dictionary);
977         compose_spell_menu_changed(compose);
978 #endif
979
980         if (account->auto_sig)
981                 compose_insert_sig(compose, FALSE);
982         gtk_text_buffer_get_start_iter(textbuf, &iter);
983         gtk_text_buffer_place_cursor(textbuf, &iter);
984
985         if (account->protocol != A_NNTP) {
986                 if (mailto && *mailto != '\0') {
987                         compose_entries_set(compose, mailto);
988
989                 } else if (item && item->prefs->enable_default_to) {
990                         compose_entry_append(compose, item->prefs->default_to, COMPOSE_TO);
991                         compose_entry_mark_default_to(compose, item->prefs->default_to);
992                 }
993                 if (item && item->ret_rcpt) {
994                         menu_set_active(ifactory, "/Options/Request Return Receipt", TRUE);
995                 }
996         } else {
997                 if (mailto) {
998                         compose_entry_append(compose, mailto, COMPOSE_NEWSGROUPS);
999                 } else if (item && FOLDER_CLASS(item->folder) == news_get_class()) {
1000                         compose_entry_append(compose, item->path, COMPOSE_NEWSGROUPS);
1001                 }
1002                 /*
1003                  * CLAWS: just don't allow return receipt request, even if the user
1004                  * may want to send an email. simple but foolproof.
1005                  */
1006                 menu_set_sensitive(ifactory, "/Options/Request Return Receipt", FALSE); 
1007         }
1008         compose_add_field_list( compose, listAddress );
1009
1010         if (prefs_common.compose_with_format) {
1011                 MsgInfo* dummyinfo = NULL;
1012
1013                 if ( prefs_common.compose_subject_format
1014                          && *prefs_common.compose_subject_format != '\0' )
1015                 {
1016                         gchar *subject = NULL;
1017                         gchar *tmp = NULL;
1018                         gchar *buf = NULL;
1019
1020                         dummyinfo = compose_msginfo_new_from_compose(compose);
1021
1022                         /* decode \-escape sequences in the internal representation of the quote format */
1023                         tmp = malloc(strlen(prefs_common.compose_subject_format)+1);
1024                         pref_get_unescaped_pref(tmp, prefs_common.compose_subject_format);
1025
1026                         subject = gtk_editable_get_chars(GTK_EDITABLE(compose->subject_entry), 0, -1);
1027                         quote_fmt_init(dummyinfo, NULL, subject, FALSE, compose->account);
1028                         quote_fmt_scan_string(tmp);
1029                         quote_fmt_parse();
1030
1031                         buf = quote_fmt_get_buffer();
1032                         if (buf == NULL)
1033                                 alertpanel_error(_("New message subject format error."));
1034                         else
1035                                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf);
1036                         quote_fmt_reset_vartable();
1037
1038                         g_free(subject);
1039                         g_free(tmp);
1040                 }
1041
1042                 if ( prefs_common.compose_body_format
1043                          && *prefs_common.compose_body_format != '\0' )
1044                 {
1045                         GtkTextView *text;
1046                         GtkTextBuffer *buffer;
1047                         GtkTextIter start, end;
1048                         gchar *tmp = NULL;
1049
1050                         if ( dummyinfo == NULL )
1051                                 dummyinfo = compose_msginfo_new_from_compose(compose);
1052
1053                         text = GTK_TEXT_VIEW(compose->text);
1054                         buffer = gtk_text_view_get_buffer(text);
1055                         gtk_text_buffer_get_start_iter(buffer, &start);
1056                         gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
1057                         tmp = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1058
1059                         compose_quote_fmt(compose, dummyinfo,
1060                                           prefs_common.compose_body_format,
1061                                           NULL, tmp, FALSE,
1062                                                   _("New message body format error."));
1063                         quote_fmt_reset_vartable();
1064
1065                         g_free(tmp);
1066                 }
1067
1068                 procmsg_msginfo_free( dummyinfo );
1069         }
1070
1071         if (attach_files) {
1072                 gint i;
1073                 gchar *file;
1074
1075                 for (i = 0; i < attach_files->len; i++) {
1076                         file = g_ptr_array_index(attach_files, i);
1077                         compose_attach_append(compose, file, file, NULL);
1078                 }
1079         }
1080
1081         compose_show_first_last_header(compose, TRUE);
1082
1083         /* Set save folder */
1084         if (item && item->prefs && item->prefs->save_copy_to_folder) {
1085                 gchar *folderidentifier;
1086
1087                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
1088                 folderidentifier = folder_item_get_identifier(item);
1089                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
1090                 g_free(folderidentifier);
1091         }
1092         
1093         gtk_widget_grab_focus(compose->header_last->entry);
1094
1095         undo_unblock(compose->undostruct);
1096
1097         if (prefs_common.auto_exteditor)
1098                 compose_exec_ext_editor(compose);
1099
1100         compose->modified = FALSE;
1101         compose_set_title(compose);
1102         return compose;
1103 }
1104
1105 static void compose_force_encryption(Compose *compose, PrefsAccount *account,
1106                 gboolean override_pref)
1107 {
1108         gchar *privacy = NULL;
1109
1110         g_return_if_fail(compose != NULL);
1111         g_return_if_fail(account != NULL);
1112
1113         if (override_pref == FALSE && account->default_encrypt_reply == FALSE)
1114                 return;
1115
1116         if (account->default_privacy_system
1117         &&  strlen(account->default_privacy_system)) {
1118                 privacy = account->default_privacy_system;
1119         } else {
1120                 GSList *privacy_avail = privacy_get_system_ids();
1121                 if (privacy_avail && g_slist_length(privacy_avail)) {
1122                         privacy = (gchar *)(privacy_avail->data);
1123                 }
1124         }
1125         if (privacy != NULL) {
1126                 compose->privacy_system = g_strdup(privacy);
1127                 compose_update_privacy_system_menu_item(compose, FALSE);
1128                 compose_use_encryption(compose, TRUE);
1129         }
1130 }       
1131
1132 static void compose_force_signing(Compose *compose, PrefsAccount *account)
1133 {
1134         gchar *privacy = NULL;
1135
1136         if (account->default_privacy_system
1137         &&  strlen(account->default_privacy_system)) {
1138                 privacy = account->default_privacy_system;
1139         } else {
1140                 GSList *privacy_avail = privacy_get_system_ids();
1141                 if (privacy_avail && g_slist_length(privacy_avail)) {
1142                         privacy = (gchar *)(privacy_avail->data);
1143                 }
1144         }
1145         if (privacy != NULL) {
1146                 compose->privacy_system = g_strdup(privacy);
1147                 compose_update_privacy_system_menu_item(compose, FALSE);
1148                 compose_use_signing(compose, TRUE);
1149         }
1150 }       
1151
1152 Compose *compose_reply_mode(ComposeMode mode, GSList *msginfo_list, gchar *body)
1153 {
1154         MsgInfo *msginfo;
1155         guint list_len;
1156         Compose *compose = NULL;
1157         GtkItemFactory *ifactory = NULL;
1158         
1159         g_return_val_if_fail(msginfo_list != NULL, NULL);
1160
1161         msginfo = (MsgInfo*)g_slist_nth_data(msginfo_list, 0);
1162         g_return_val_if_fail(msginfo != NULL, NULL);
1163
1164         list_len = g_slist_length(msginfo_list);
1165
1166         switch (mode) {
1167         case COMPOSE_REPLY:
1168                 compose = compose_reply(msginfo, prefs_common.reply_with_quote,
1169                               FALSE, prefs_common.default_reply_list, FALSE, body);
1170                 break;
1171         case COMPOSE_REPLY_WITH_QUOTE:
1172                 compose = compose_reply(msginfo, TRUE, 
1173                         FALSE, prefs_common.default_reply_list, FALSE, body);
1174                 break;
1175         case COMPOSE_REPLY_WITHOUT_QUOTE:
1176                 compose = compose_reply(msginfo, FALSE, 
1177                         FALSE, prefs_common.default_reply_list, FALSE, NULL);
1178                 break;
1179         case COMPOSE_REPLY_TO_SENDER:
1180                 compose = compose_reply(msginfo, prefs_common.reply_with_quote,
1181                               FALSE, FALSE, TRUE, body);
1182                 break;
1183         case COMPOSE_FOLLOWUP_AND_REPLY_TO:
1184                 compose = compose_followup_and_reply_to(msginfo,
1185                                               prefs_common.reply_with_quote,
1186                                               FALSE, FALSE, body);
1187                 break;
1188         case COMPOSE_REPLY_TO_SENDER_WITH_QUOTE:
1189                 compose = compose_reply(msginfo, TRUE, 
1190                         FALSE, FALSE, TRUE, body);
1191                 break;
1192         case COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE:
1193                 compose = compose_reply(msginfo, FALSE, 
1194                         FALSE, FALSE, TRUE, NULL);
1195                 break;
1196         case COMPOSE_REPLY_TO_ALL:
1197                 compose = compose_reply(msginfo, prefs_common.reply_with_quote,
1198                         TRUE, FALSE, FALSE, body);
1199                 break;
1200         case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
1201                 compose = compose_reply(msginfo, TRUE, 
1202                         TRUE, FALSE, FALSE, body);
1203                 break;
1204         case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
1205                 compose = compose_reply(msginfo, FALSE, 
1206                         TRUE, FALSE, FALSE, NULL);
1207                 break;
1208         case COMPOSE_REPLY_TO_LIST:
1209                 compose = compose_reply(msginfo, prefs_common.reply_with_quote,
1210                         FALSE, TRUE, FALSE, body);
1211                 break;
1212         case COMPOSE_REPLY_TO_LIST_WITH_QUOTE:
1213                 compose = compose_reply(msginfo, TRUE, 
1214                         FALSE, TRUE, FALSE, body);
1215                 break;
1216         case COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE:
1217                 compose = compose_reply(msginfo, FALSE, 
1218                         FALSE, TRUE, FALSE, NULL);
1219                 break;
1220         case COMPOSE_FORWARD:
1221                 if (prefs_common.forward_as_attachment) {
1222                         compose = compose_reply_mode(COMPOSE_FORWARD_AS_ATTACH, msginfo_list, body);
1223                         return compose;
1224                 } else {
1225                         compose = compose_reply_mode(COMPOSE_FORWARD_INLINE, msginfo_list, body);
1226                         return compose;
1227                 }
1228                 break;
1229         case COMPOSE_FORWARD_INLINE:
1230                 /* check if we reply to more than one Message */
1231                 if (list_len == 1) {
1232                         compose = compose_forward(NULL, msginfo, FALSE, body, FALSE, FALSE);
1233                         break;
1234                 } 
1235                 /* more messages FALL THROUGH */
1236         case COMPOSE_FORWARD_AS_ATTACH:
1237                 compose = compose_forward_multiple(NULL, msginfo_list);
1238                 break;
1239         case COMPOSE_REDIRECT:
1240                 compose = compose_redirect(NULL, msginfo, FALSE);
1241                 break;
1242         default:
1243                 g_warning("compose_reply(): invalid Compose Mode: %d\n", mode);
1244         }
1245         
1246         ifactory = gtk_item_factory_from_widget(compose->menubar);
1247
1248         compose->rmode = mode;
1249         switch (compose->rmode) {
1250         case COMPOSE_REPLY:
1251         case COMPOSE_REPLY_WITH_QUOTE:
1252         case COMPOSE_REPLY_WITHOUT_QUOTE:
1253         case COMPOSE_FOLLOWUP_AND_REPLY_TO:
1254                 debug_print("reply mode Normal\n");
1255                 menu_set_active(ifactory, "/Options/Reply mode/Normal", TRUE);
1256                 compose_reply_change_mode(compose, COMPOSE_REPLY, NULL); /* force update */
1257                 break;
1258         case COMPOSE_REPLY_TO_SENDER:
1259         case COMPOSE_REPLY_TO_SENDER_WITH_QUOTE:
1260         case COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE:
1261                 debug_print("reply mode Sender\n");
1262                 menu_set_active(ifactory, "/Options/Reply mode/Sender", TRUE);
1263                 break;
1264         case COMPOSE_REPLY_TO_ALL:
1265         case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
1266         case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
1267                 debug_print("reply mode All\n");
1268                 menu_set_active(ifactory, "/Options/Reply mode/All", TRUE);
1269                 break;
1270         case COMPOSE_REPLY_TO_LIST:
1271         case COMPOSE_REPLY_TO_LIST_WITH_QUOTE:
1272         case COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE:
1273                 debug_print("reply mode List\n");
1274                 menu_set_active(ifactory, "/Options/Reply mode/Mailing-list", TRUE);
1275                 break;
1276         default:
1277                 break;
1278         }
1279         return compose;
1280 }
1281
1282 Compose *compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
1283                    gboolean to_ml, gboolean to_sender, 
1284                    const gchar *body)
1285 {
1286         return compose_generic_reply(msginfo, quote, to_all, to_ml, 
1287                               to_sender, FALSE, body);
1288 }
1289
1290 Compose *compose_followup_and_reply_to(MsgInfo *msginfo, gboolean quote,
1291                                    gboolean to_all,
1292                                    gboolean to_sender,
1293                                    const gchar *body)
1294 {
1295         return compose_generic_reply(msginfo, quote, to_all, FALSE, 
1296                               to_sender, TRUE, body);
1297 }
1298
1299 static void compose_extract_original_charset(Compose *compose)
1300 {
1301         MsgInfo *info = NULL;
1302         if (compose->replyinfo) {
1303                 info = compose->replyinfo;
1304         } else if (compose->fwdinfo) {
1305                 info = compose->fwdinfo;
1306         } else if (compose->targetinfo) {
1307                 info = compose->targetinfo;
1308         }
1309         if (info) {
1310                 MimeInfo *mimeinfo = procmime_scan_message(info);
1311                 MimeInfo *partinfo = mimeinfo;
1312                 while (partinfo && partinfo->type != MIMETYPE_TEXT)
1313                         partinfo = procmime_mimeinfo_next(partinfo);
1314                 if (partinfo) {
1315                         compose->orig_charset = 
1316                                 g_strdup(procmime_mimeinfo_get_parameter(
1317                                                 partinfo, "charset"));
1318                 }
1319                 procmime_mimeinfo_free_all(mimeinfo);
1320         }
1321 }
1322
1323 #define SIGNAL_BLOCK(buffer) {                                  \
1324         g_signal_handlers_block_by_func(G_OBJECT(buffer),       \
1325                                 G_CALLBACK(compose_changed_cb), \
1326                                 compose);                       \
1327         g_signal_handlers_block_by_func(G_OBJECT(buffer),       \
1328                                 G_CALLBACK(text_inserted),      \
1329                                 compose);                       \
1330 }
1331
1332 #define SIGNAL_UNBLOCK(buffer) {                                \
1333         g_signal_handlers_unblock_by_func(G_OBJECT(buffer),     \
1334                                 G_CALLBACK(compose_changed_cb), \
1335                                 compose);                       \
1336         g_signal_handlers_unblock_by_func(G_OBJECT(buffer),     \
1337                                 G_CALLBACK(text_inserted),      \
1338                                 compose);                       \
1339 }
1340
1341 static Compose *compose_generic_reply(MsgInfo *msginfo, gboolean quote,
1342                                   gboolean to_all, gboolean to_ml,
1343                                   gboolean to_sender,
1344                                   gboolean followup_and_reply_to,
1345                                   const gchar *body)
1346 {
1347         GtkItemFactory *ifactory;
1348         Compose *compose;
1349         PrefsAccount *account = NULL;
1350         PrefsAccount *reply_account;
1351         GtkTextView *textview;
1352         GtkTextBuffer *textbuf;
1353
1354         g_return_val_if_fail(msginfo != NULL, NULL);
1355         g_return_val_if_fail(msginfo->folder != NULL, NULL);
1356
1357         account = account_get_reply_account(msginfo, prefs_common.reply_account_autosel);
1358         
1359         g_return_val_if_fail(account != NULL, NULL);
1360
1361         if (to_sender && account->protocol == A_NNTP &&
1362             !followup_and_reply_to) {
1363                 reply_account =
1364                         account_find_from_address(account->address);
1365                 if (!reply_account)
1366                         reply_account = compose_current_mail_account();
1367                 if (!reply_account)
1368                         return NULL;
1369         } else
1370                 reply_account = account;
1371
1372         compose = compose_create(account, COMPOSE_REPLY, FALSE);
1373
1374         compose->updating = TRUE;
1375
1376         ifactory = gtk_item_factory_from_widget(compose->menubar);
1377
1378         menu_set_active(ifactory, "/Options/Remove references", FALSE);
1379         menu_set_sensitive(ifactory, "/Options/Remove references", TRUE);
1380
1381         compose->replyinfo = procmsg_msginfo_get_full_info(msginfo);
1382         if (!compose->replyinfo)
1383                 compose->replyinfo = procmsg_msginfo_copy(msginfo);
1384
1385         compose_extract_original_charset(compose);
1386         
1387         if (msginfo->folder && msginfo->folder->ret_rcpt)
1388                 menu_set_active(ifactory, "/Options/Request Return Receipt", TRUE);
1389
1390         /* Set save folder */
1391         if (msginfo->folder && msginfo->folder->prefs && msginfo->folder->prefs->save_copy_to_folder) {
1392                 gchar *folderidentifier;
1393
1394                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1395                 folderidentifier = folder_item_get_identifier(msginfo->folder);
1396                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
1397                 g_free(folderidentifier);
1398         }
1399
1400         if (compose_parse_header(compose, msginfo) < 0) return NULL;
1401
1402         textview = (GTK_TEXT_VIEW(compose->text));
1403         textbuf = gtk_text_view_get_buffer(textview);
1404         compose_create_tags(textview, compose);
1405
1406         undo_block(compose->undostruct);
1407 #ifdef USE_ASPELL
1408         if (msginfo->folder && msginfo->folder->prefs && 
1409             msginfo->folder->prefs && 
1410             msginfo->folder->prefs->enable_default_dictionary &&
1411             compose->gtkaspell)
1412                 gtkaspell_change_dict(compose->gtkaspell, 
1413                     msginfo->folder->prefs->default_dictionary);
1414 #endif
1415
1416         if (quote) {
1417                 gchar *qmark;
1418
1419                 if (prefs_common.quotemark && *prefs_common.quotemark)
1420                         qmark = prefs_common.quotemark;
1421                 else
1422                         qmark = "> ";
1423
1424                 compose_quote_fmt(compose, compose->replyinfo,
1425                                   prefs_common.quotefmt,
1426                                   qmark, body, FALSE,
1427                                           _("Message reply format error."));
1428                 quote_fmt_reset_vartable();
1429         }
1430         if (procmime_msginfo_is_encrypted(compose->replyinfo)) {
1431                 compose_force_encryption(compose, account, FALSE);
1432         }
1433
1434         SIGNAL_BLOCK(textbuf);
1435         
1436         if (account->auto_sig)
1437                 compose_insert_sig(compose, FALSE);
1438
1439         compose_wrap_all(compose);
1440
1441         SIGNAL_UNBLOCK(textbuf);
1442         
1443         gtk_widget_grab_focus(compose->text);
1444
1445         undo_unblock(compose->undostruct);
1446
1447         if (prefs_common.auto_exteditor)
1448                 compose_exec_ext_editor(compose);
1449                 
1450         compose->modified = FALSE;
1451         compose_set_title(compose);
1452
1453         compose->updating = FALSE;
1454
1455         if (compose->deferred_destroy) {
1456                 compose_destroy(compose);
1457                 return NULL;
1458         }
1459
1460         return compose;
1461 }
1462
1463 #define INSERT_FW_HEADER(var, hdr) \
1464 if (msginfo->var && *msginfo->var) { \
1465         gtk_stext_insert(text, NULL, NULL, NULL, hdr, -1); \
1466         gtk_stext_insert(text, NULL, NULL, NULL, msginfo->var, -1); \
1467         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1); \
1468 }
1469
1470 Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
1471                          gboolean as_attach, const gchar *body,
1472                          gboolean no_extedit,
1473                          gboolean batch)
1474 {
1475         Compose *compose;
1476         GtkTextView *textview;
1477         GtkTextBuffer *textbuf;
1478         GtkTextIter iter;
1479
1480         g_return_val_if_fail(msginfo != NULL, NULL);
1481         g_return_val_if_fail(msginfo->folder != NULL, NULL);
1482
1483         if (!account && 
1484             !(account = compose_guess_forward_account_from_msginfo
1485                                 (msginfo)))
1486                 account = cur_account;
1487
1488         compose = compose_create(account, COMPOSE_FORWARD, batch);
1489
1490         compose->updating = TRUE;
1491         compose->fwdinfo = procmsg_msginfo_get_full_info(msginfo);
1492         if (!compose->fwdinfo)
1493                 compose->fwdinfo = procmsg_msginfo_copy(msginfo);
1494
1495         compose_extract_original_charset(compose);
1496
1497         if (msginfo->subject && *msginfo->subject) {
1498                 gchar *buf, *buf2, *p;
1499
1500                 buf = p = g_strdup(msginfo->subject);
1501                 p += subject_get_prefix_length(p);
1502                 memmove(buf, p, strlen(p) + 1);
1503
1504                 buf2 = g_strdup_printf("Fw: %s", buf);
1505                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1506                 
1507                 g_free(buf);
1508                 g_free(buf2);
1509         }
1510
1511         textview = GTK_TEXT_VIEW(compose->text);
1512         textbuf = gtk_text_view_get_buffer(textview);
1513         compose_create_tags(textview, compose);
1514         
1515         undo_block(compose->undostruct);
1516         if (as_attach) {
1517                 gchar *msgfile;
1518
1519                 msgfile = procmsg_get_message_file_path(msginfo);
1520                 if (!is_file_exist(msgfile))
1521                         g_warning("%s: file not exist\n", msgfile);
1522                 else
1523                         compose_attach_append(compose, msgfile, msgfile,
1524                                               "message/rfc822");
1525
1526                 g_free(msgfile);
1527         } else {
1528                 gchar *qmark;
1529                 MsgInfo *full_msginfo;
1530
1531                 full_msginfo = procmsg_msginfo_get_full_info(msginfo);
1532                 if (!full_msginfo)
1533                         full_msginfo = procmsg_msginfo_copy(msginfo);
1534
1535                 if (prefs_common.fw_quotemark &&
1536                     *prefs_common.fw_quotemark)
1537                         qmark = prefs_common.fw_quotemark;
1538                 else
1539                         qmark = "> ";
1540
1541                 compose_quote_fmt(compose, full_msginfo,
1542                                   prefs_common.fw_quotefmt,
1543                                   qmark, body, FALSE,
1544                                           _("Message forward format error."));
1545                 quote_fmt_reset_vartable();
1546                 compose_attach_parts(compose, msginfo);
1547
1548                 procmsg_msginfo_free(full_msginfo);
1549         }
1550
1551         SIGNAL_BLOCK(textbuf);
1552
1553         if (account->auto_sig)
1554                 compose_insert_sig(compose, FALSE);
1555
1556         compose_wrap_all(compose);
1557
1558         SIGNAL_UNBLOCK(textbuf);
1559         
1560         gtk_text_buffer_get_start_iter(textbuf, &iter);
1561         gtk_text_buffer_place_cursor(textbuf, &iter);
1562
1563         gtk_widget_grab_focus(compose->header_last->entry);
1564
1565         if (!no_extedit && prefs_common.auto_exteditor)
1566                 compose_exec_ext_editor(compose);
1567         
1568         /*save folder*/
1569         if (msginfo->folder && msginfo->folder->prefs && msginfo->folder->prefs->save_copy_to_folder) {
1570                 gchar *folderidentifier;
1571
1572                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1573                 folderidentifier = folder_item_get_identifier(msginfo->folder);
1574                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
1575                 g_free(folderidentifier);
1576         }
1577
1578         undo_unblock(compose->undostruct);
1579         
1580         compose->modified = FALSE;
1581         compose_set_title(compose);
1582
1583         compose->updating = FALSE;
1584
1585         if (compose->deferred_destroy) {
1586                 compose_destroy(compose);
1587                 return NULL;
1588         }
1589
1590         return compose;
1591 }
1592
1593 #undef INSERT_FW_HEADER
1594
1595 Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_list)
1596 {
1597         Compose *compose;
1598         GtkTextView *textview;
1599         GtkTextBuffer *textbuf;
1600         GtkTextIter iter;
1601         GSList *msginfo;
1602         gchar *msgfile;
1603         gboolean single_mail = TRUE;
1604         
1605         g_return_val_if_fail(msginfo_list != NULL, NULL);
1606
1607         if (g_slist_length(msginfo_list) > 1)
1608                 single_mail = FALSE;
1609
1610         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next)
1611                 if (((MsgInfo *)msginfo->data)->folder == NULL)
1612                         return NULL;
1613
1614         /* guess account from first selected message */
1615         if (!account && 
1616             !(account = compose_guess_forward_account_from_msginfo
1617                                 (msginfo_list->data)))
1618                 account = cur_account;
1619
1620         g_return_val_if_fail(account != NULL, NULL);
1621
1622         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1623                 MSG_UNSET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_REPLIED);
1624                 MSG_SET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_FORWARDED);
1625         }
1626
1627         compose = compose_create(account, COMPOSE_FORWARD, FALSE);
1628
1629         compose->updating = TRUE;
1630
1631         textview = GTK_TEXT_VIEW(compose->text);
1632         textbuf = gtk_text_view_get_buffer(textview);
1633         compose_create_tags(textview, compose);
1634         
1635         undo_block(compose->undostruct);
1636         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1637                 msgfile = procmsg_get_message_file_path((MsgInfo *)msginfo->data);
1638
1639                 if (!is_file_exist(msgfile))
1640                         g_warning("%s: file not exist\n", msgfile);
1641                 else
1642                         compose_attach_append(compose, msgfile, msgfile,
1643                                 "message/rfc822");
1644                 g_free(msgfile);
1645         }
1646         
1647         if (single_mail) {
1648                 MsgInfo *info = (MsgInfo *)msginfo_list->data;
1649                 if (info->subject && *info->subject) {
1650                         gchar *buf, *buf2, *p;
1651
1652                         buf = p = g_strdup(info->subject);
1653                         p += subject_get_prefix_length(p);
1654                         memmove(buf, p, strlen(p) + 1);
1655
1656                         buf2 = g_strdup_printf("Fw: %s", buf);
1657                         gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1658
1659                         g_free(buf);
1660                         g_free(buf2);
1661                 }
1662         } else {
1663                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry),
1664                         _("Fw: multiple emails"));
1665         }
1666
1667         SIGNAL_BLOCK(textbuf);
1668         
1669         if (account->auto_sig)
1670                 compose_insert_sig(compose, FALSE);
1671
1672         compose_wrap_all(compose);
1673
1674         SIGNAL_UNBLOCK(textbuf);
1675         
1676         gtk_text_buffer_get_start_iter(textbuf, &iter);
1677         gtk_text_buffer_place_cursor(textbuf, &iter);
1678
1679         gtk_widget_grab_focus(compose->header_last->entry);
1680         undo_unblock(compose->undostruct);
1681         compose->modified = FALSE;
1682         compose_set_title(compose);
1683
1684         compose->updating = FALSE;
1685
1686         if (compose->deferred_destroy) {
1687                 compose_destroy(compose);
1688                 return NULL;
1689         }
1690
1691         return compose;
1692 }
1693
1694 static gboolean compose_is_sig_separator(Compose *compose, GtkTextBuffer *textbuf, GtkTextIter *iter) 
1695 {
1696         GtkTextIter start = *iter;
1697         GtkTextIter end_iter;
1698         int start_pos = gtk_text_iter_get_offset(&start);
1699         gchar *str = NULL;
1700         if (!compose->account->sig_sep)
1701                 return FALSE;
1702         
1703         gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
1704                 start_pos+strlen(compose->account->sig_sep));
1705
1706         /* check sig separator */
1707         str = gtk_text_iter_get_text(&start, &end_iter);
1708         if (!strcmp(str, compose->account->sig_sep)) {
1709                 gchar *tmp = NULL;
1710                 /* check end of line (\n) */
1711                 gtk_text_buffer_get_iter_at_offset(textbuf, &start,
1712                         start_pos+strlen(compose->account->sig_sep));
1713                 gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
1714                         start_pos+strlen(compose->account->sig_sep)+1);
1715                 tmp = gtk_text_iter_get_text(&start, &end_iter);
1716                 if (!strcmp(tmp,"\n")) {
1717                         g_free(str);
1718                         g_free(tmp);
1719                         return TRUE;
1720                 }
1721                 g_free(tmp);    
1722         }
1723         g_free(str);
1724
1725         return FALSE;
1726 }
1727
1728 static void compose_colorize_signature(Compose *compose)
1729 {
1730         GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(compose->text));
1731         GtkTextIter iter;
1732         GtkTextIter end_iter;
1733         gtk_text_buffer_get_start_iter(buffer, &iter);
1734         while (gtk_text_iter_forward_line(&iter))
1735                 if (compose_is_sig_separator(compose, buffer, &iter)) {
1736                         gtk_text_buffer_get_end_iter(buffer, &end_iter);
1737                         gtk_text_buffer_apply_tag_by_name(buffer,"signature",&iter, &end_iter);
1738                 }
1739 }
1740
1741 Compose *compose_reedit(MsgInfo *msginfo, gboolean batch)
1742 {
1743         Compose *compose = NULL;
1744         PrefsAccount *account = NULL;
1745         GtkTextView *textview;
1746         GtkTextBuffer *textbuf;
1747         GtkTextMark *mark;
1748         GtkTextIter iter;
1749         FILE *fp;
1750         gchar buf[BUFFSIZE];
1751         gboolean use_signing = FALSE;
1752         gboolean use_encryption = FALSE;
1753         gchar *privacy_system = NULL;
1754         int priority = PRIORITY_NORMAL;
1755         MsgInfo *replyinfo = NULL, *fwdinfo = NULL;
1756
1757         g_return_val_if_fail(msginfo != NULL, NULL);
1758         g_return_val_if_fail(msginfo->folder != NULL, NULL);
1759
1760         if (compose_put_existing_to_front(msginfo)) {
1761                 return NULL;
1762         }
1763
1764         if (folder_has_parent_of_type(msginfo->folder, F_QUEUE) ||
1765             folder_has_parent_of_type(msginfo->folder, F_DRAFT)) {
1766                 gchar queueheader_buf[BUFFSIZE];
1767                 gint id, param;
1768
1769                 /* Select Account from queue headers */
1770                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1771                                              sizeof(queueheader_buf), "X-Sylpheed-Account-Id:")) {
1772                         id = atoi(&queueheader_buf[strlen("X-Sylpheed-Account-Id:")]);
1773                         account = account_find_from_id(id);
1774                 }
1775                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1776                                              sizeof(queueheader_buf), "NAID:")) {
1777                         id = atoi(&queueheader_buf[strlen("NAID:")]);
1778                         account = account_find_from_id(id);
1779                 }
1780                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1781                                                     sizeof(queueheader_buf), "MAID:")) {
1782                         id = atoi(&queueheader_buf[strlen("MAID:")]);
1783                         account = account_find_from_id(id);
1784                 }
1785                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1786                                                                 sizeof(queueheader_buf), "S:")) {
1787                         account = account_find_from_address(queueheader_buf);
1788                 }
1789                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1790                                              sizeof(queueheader_buf), "X-Sylpheed-Sign:")) {
1791                         param = atoi(&queueheader_buf[strlen("X-Sylpheed-Sign:")]);
1792                         use_signing = param;
1793                         
1794                 }
1795                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1796                                              sizeof(queueheader_buf), "X-Sylpheed-Encrypt:")) {
1797                         param = atoi(&queueheader_buf[strlen("X-Sylpheed-Encrypt:")]);
1798                         use_encryption = param;
1799                 }
1800                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1801                                             sizeof(queueheader_buf), "X-Sylpheed-Privacy-System:")) {
1802                         privacy_system = g_strdup(&queueheader_buf[strlen("X-Sylpheed-Privacy-System:")]);
1803                 }
1804                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1805                                              sizeof(queueheader_buf), "X-Priority: ")) {
1806                         param = atoi(&queueheader_buf[strlen("X-Priority: ")]); /* mind the space */
1807                         priority = param;
1808                 }
1809                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1810                                              sizeof(queueheader_buf), "RMID:")) {
1811                         gchar **tokens = g_strsplit(&queueheader_buf[strlen("RMID:")], "\t", 0);
1812                         if (tokens[0] && tokens[1] && tokens[2]) {
1813                                 FolderItem *orig_item = folder_find_item_from_identifier(tokens[0]);
1814                                 if (orig_item != NULL) {
1815                                         replyinfo = folder_item_get_msginfo_by_msgid(orig_item, tokens[2]);
1816                                 }
1817                         }
1818                         g_strfreev(tokens);
1819                 }
1820                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1821                                              sizeof(queueheader_buf), "FMID:")) {
1822                         gchar **tokens = g_strsplit(&queueheader_buf[strlen("FMID:")], "\t", 0);
1823                         if (tokens[0] && tokens[1] && tokens[2]) {
1824                                 FolderItem *orig_item = folder_find_item_from_identifier(tokens[0]);
1825                                 if (orig_item != NULL) {
1826                                         fwdinfo = folder_item_get_msginfo_by_msgid(orig_item, tokens[2]);
1827                                 }
1828                         }
1829                         g_strfreev(tokens);
1830                 }
1831         } else {
1832                 account = msginfo->folder->folder->account;
1833         }
1834
1835         if (!account && prefs_common.reedit_account_autosel) {
1836                 gchar from[BUFFSIZE];
1837                 if (!procheader_get_header_from_msginfo(msginfo, from, sizeof(from), "FROM:")) {
1838                         extract_address(from);
1839                         account = account_find_from_address(from);
1840                 }
1841         }
1842         if (!account) {
1843                 account = cur_account;
1844         }
1845         g_return_val_if_fail(account != NULL, NULL);
1846
1847         compose = compose_create(account, COMPOSE_REEDIT, batch);
1848         
1849         compose->replyinfo = replyinfo;
1850         compose->fwdinfo = fwdinfo;
1851
1852         compose->updating = TRUE;
1853         compose->priority = priority;
1854
1855         if (privacy_system != NULL) {
1856                 compose->privacy_system = privacy_system;
1857                 compose_use_signing(compose, use_signing);
1858                 compose_use_encryption(compose, use_encryption);
1859                 compose_update_privacy_system_menu_item(compose, FALSE);
1860         } else {
1861                 activate_privacy_system(compose, account, FALSE);
1862         }
1863
1864         compose->targetinfo = procmsg_msginfo_copy(msginfo);
1865
1866         compose_extract_original_charset(compose);
1867
1868         if (folder_has_parent_of_type(msginfo->folder, F_QUEUE) ||
1869             folder_has_parent_of_type(msginfo->folder, F_DRAFT)) {
1870                 gchar queueheader_buf[BUFFSIZE];
1871
1872                 /* Set message save folder */
1873                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, sizeof(queueheader_buf), "SCF:")) {
1874                         gint startpos = 0;
1875
1876                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1877                         gtk_editable_delete_text(GTK_EDITABLE(compose->savemsg_entry), 0, -1);
1878                         gtk_editable_insert_text(GTK_EDITABLE(compose->savemsg_entry), &queueheader_buf[4], strlen(&queueheader_buf[4]), &startpos);
1879                 }
1880                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, sizeof(queueheader_buf), "RRCPT:")) {
1881                         gint active = atoi(&queueheader_buf[strlen("RRCPT:")]);
1882                         if (active) {
1883                                 GtkItemFactory *ifactory;
1884                                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1885                                 menu_set_active(ifactory, "/Options/Request Return Receipt", TRUE);
1886                         }
1887                 }
1888         }
1889         
1890         if (compose_parse_header(compose, msginfo) < 0) {
1891                 compose->updating = FALSE;
1892                 compose_destroy(compose);
1893                 return NULL;
1894         }
1895         compose_reedit_set_entry(compose, msginfo);
1896
1897         textview = GTK_TEXT_VIEW(compose->text);
1898         textbuf = gtk_text_view_get_buffer(textview);
1899         compose_create_tags(textview, compose);
1900
1901         mark = gtk_text_buffer_get_insert(textbuf);
1902         gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1903
1904         g_signal_handlers_block_by_func(G_OBJECT(textbuf),
1905                                         G_CALLBACK(compose_changed_cb),
1906                                         compose);
1907         
1908         if (procmime_msginfo_is_encrypted(msginfo)) {
1909                 fp = procmime_get_first_encrypted_text_content(msginfo);
1910                 if (fp) {
1911                         compose_force_encryption(compose, account, TRUE);
1912                 }
1913         } else {
1914                 fp = procmime_get_first_text_content(msginfo);
1915         }
1916         if (fp == NULL) {
1917                 g_warning("Can't get text part\n");
1918         }
1919
1920         if (fp != NULL) {
1921                 gboolean prev_autowrap = compose->autowrap;
1922
1923                 compose->autowrap = FALSE;
1924                 while (fgets(buf, sizeof(buf), fp) != NULL) {
1925                         strcrchomp(buf);
1926                         gtk_text_buffer_insert(textbuf, &iter, buf, -1);
1927                 }
1928                 compose_wrap_all_full(compose, FALSE);
1929                 compose->autowrap = prev_autowrap;
1930                 fclose(fp);
1931         }
1932         
1933         compose_attach_parts(compose, msginfo);
1934
1935         compose_colorize_signature(compose);
1936
1937         g_signal_handlers_unblock_by_func(G_OBJECT(textbuf),
1938                                         G_CALLBACK(compose_changed_cb),
1939                                         compose);
1940
1941         gtk_widget_grab_focus(compose->text);
1942
1943         if (prefs_common.auto_exteditor) {
1944                 compose_exec_ext_editor(compose);
1945         }
1946         compose->modified = FALSE;
1947         compose_set_title(compose);
1948
1949         compose->updating = FALSE;
1950
1951         if (compose->deferred_destroy) {
1952                 compose_destroy(compose);
1953                 return NULL;
1954         }
1955         
1956         compose->sig_str = compose_get_signature_str(compose);
1957         
1958         return compose;
1959 }
1960
1961 Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo,
1962                                                  gboolean batch)
1963 {
1964         Compose *compose;
1965         gchar *filename;
1966         GtkItemFactory *ifactory;
1967         FolderItem *item;
1968
1969         g_return_val_if_fail(msginfo != NULL, NULL);
1970
1971         if (!account)
1972                 account = account_get_reply_account(msginfo,
1973                                         prefs_common.reply_account_autosel);
1974         g_return_val_if_fail(account != NULL, NULL);
1975
1976         compose = compose_create(account, COMPOSE_REDIRECT, batch);
1977
1978         compose->updating = TRUE;
1979
1980         ifactory = gtk_item_factory_from_widget(compose->menubar);
1981         compose_create_tags(GTK_TEXT_VIEW(compose->text), compose);
1982         compose->replyinfo = NULL;
1983         compose->fwdinfo = NULL;
1984
1985         compose_show_first_last_header(compose, TRUE);
1986
1987         gtk_widget_grab_focus(compose->header_last->entry);
1988
1989         filename = procmsg_get_message_file_path(msginfo);
1990
1991         if (filename == NULL) {
1992                 compose->updating = FALSE;
1993                 compose_destroy(compose);
1994
1995                 return NULL;
1996         }
1997
1998         compose->redirect_filename = filename;
1999         
2000         /* Set save folder */
2001         item = msginfo->folder;
2002         if (item && item->prefs && item->prefs->save_copy_to_folder) {
2003                 gchar *folderidentifier;
2004
2005                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
2006                 folderidentifier = folder_item_get_identifier(item);
2007                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
2008                 g_free(folderidentifier);
2009         }
2010
2011         compose_attach_parts(compose, msginfo);
2012
2013         if (msginfo->subject)
2014                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry),
2015                                    msginfo->subject);
2016         gtk_editable_set_editable(GTK_EDITABLE(compose->subject_entry), FALSE);
2017
2018         compose_quote_fmt(compose, msginfo, "%M", NULL, NULL, FALSE,
2019                                           _("Message redirect format error."));
2020         quote_fmt_reset_vartable();
2021         gtk_text_view_set_editable(GTK_TEXT_VIEW(compose->text), FALSE);
2022
2023         compose_colorize_signature(compose);
2024
2025         ifactory = gtk_item_factory_from_widget(compose->popupmenu);
2026         menu_set_sensitive(ifactory, "/Add...", FALSE);
2027         menu_set_sensitive(ifactory, "/Remove", FALSE);
2028         menu_set_sensitive(ifactory, "/Properties...", FALSE);
2029
2030         ifactory = gtk_item_factory_from_widget(compose->menubar);
2031         menu_set_sensitive(ifactory, "/Message/Save", FALSE);
2032         menu_set_sensitive(ifactory, "/Message/Insert file", FALSE);
2033         menu_set_sensitive(ifactory, "/Message/Attach file", FALSE);
2034         menu_set_sensitive(ifactory, "/Message/Insert signature", FALSE);
2035         menu_set_sensitive(ifactory, "/Edit", FALSE);
2036         menu_set_sensitive(ifactory, "/Options", FALSE);
2037         menu_set_sensitive(ifactory, "/Tools/Show ruler", FALSE);
2038         menu_set_sensitive(ifactory, "/Tools/Actions", FALSE);
2039         
2040         if (compose->toolbar->draft_btn)
2041                 gtk_widget_set_sensitive(compose->toolbar->draft_btn, FALSE);
2042         if (compose->toolbar->insert_btn)
2043                 gtk_widget_set_sensitive(compose->toolbar->insert_btn, FALSE);
2044         if (compose->toolbar->attach_btn)
2045                 gtk_widget_set_sensitive(compose->toolbar->attach_btn, FALSE);
2046         if (compose->toolbar->sig_btn)
2047                 gtk_widget_set_sensitive(compose->toolbar->sig_btn, FALSE);
2048         if (compose->toolbar->exteditor_btn)
2049                 gtk_widget_set_sensitive(compose->toolbar->exteditor_btn, FALSE);
2050         if (compose->toolbar->linewrap_current_btn)
2051                 gtk_widget_set_sensitive(compose->toolbar->linewrap_current_btn, FALSE);
2052         if (compose->toolbar->linewrap_all_btn)
2053                 gtk_widget_set_sensitive(compose->toolbar->linewrap_all_btn, FALSE);
2054
2055         compose->modified = FALSE;
2056         compose_set_title(compose);
2057         compose->updating = FALSE;
2058
2059         if (compose->deferred_destroy) {
2060                 compose_destroy(compose);
2061                 return NULL;
2062         }
2063         
2064         return compose;
2065 }
2066
2067 GList *compose_get_compose_list(void)
2068 {
2069         return compose_list;
2070 }
2071
2072 void compose_entry_append(Compose *compose, const gchar *address,
2073                           ComposeEntryType type)
2074 {
2075         gchar *header;
2076         gchar *cur, *begin;
2077         gboolean in_quote = FALSE;
2078         if (!address || *address == '\0') return;
2079
2080         switch (type) {
2081         case COMPOSE_CC:
2082                 header = N_("Cc:");
2083                 break;
2084         case COMPOSE_BCC:
2085                 header = N_("Bcc:");
2086                 break;
2087         case COMPOSE_REPLYTO:
2088                 header = N_("Reply-To:");
2089                 break;
2090         case COMPOSE_NEWSGROUPS:
2091                 header = N_("Newsgroups:");
2092                 break;
2093         case COMPOSE_FOLLOWUPTO:
2094                 header = N_( "Followup-To:");
2095                 break;
2096         case COMPOSE_TO:
2097         default:
2098                 header = N_("To:");
2099                 break;
2100         }
2101         header = prefs_common.trans_hdr ? gettext(header) : header;
2102         
2103         cur = begin = (gchar *)address;
2104         
2105         /* we separate the line by commas, but not if we're inside a quoted
2106          * string */
2107         while (*cur != '\0') {
2108                 if (*cur == '"') 
2109                         in_quote = !in_quote;
2110                 if (*cur == ',' && !in_quote) {
2111                         gchar *tmp = g_strdup(begin);
2112                         gchar *o_tmp = tmp;
2113                         tmp[cur-begin]='\0';
2114                         cur++;
2115                         begin = cur;
2116                         while (*tmp == ' ' || *tmp == '\t')
2117                                 tmp++;
2118                         compose_add_header_entry(compose, header, tmp);
2119                         g_free(o_tmp);
2120                         continue;
2121                 }
2122                 cur++;
2123         }
2124         if (begin < cur) {
2125                 gchar *tmp = g_strdup(begin);
2126                 gchar *o_tmp = tmp;
2127                 tmp[cur-begin]='\0';
2128                 cur++;
2129                 begin = cur;
2130                 while (*tmp == ' ' || *tmp == '\t')
2131                         tmp++;
2132                 compose_add_header_entry(compose, header, tmp);
2133                 g_free(o_tmp);          
2134         }
2135 }
2136
2137 void compose_entry_mark_default_to(Compose *compose, const gchar *mailto)
2138 {
2139         static GdkColor yellow;
2140         static GdkColor black;
2141         static gboolean yellow_initialised = FALSE;
2142         GSList *h_list;
2143         GtkEntry *entry;
2144                 
2145         if (!yellow_initialised) {
2146                 gdk_color_parse("#f5f6be", &yellow);
2147                 gdk_color_parse("#000000", &black);
2148                 yellow_initialised = gdk_colormap_alloc_color(
2149                         gdk_colormap_get_system(), &yellow, FALSE, TRUE);
2150                 yellow_initialised &= gdk_colormap_alloc_color(
2151                         gdk_colormap_get_system(), &black, FALSE, TRUE);
2152         }
2153
2154         for (h_list = compose->header_list; h_list != NULL; h_list = h_list->next) {
2155                 entry = GTK_ENTRY(((ComposeHeaderEntry *)h_list->data)->entry);
2156                 if (gtk_entry_get_text(entry) && 
2157                     !g_utf8_collate(gtk_entry_get_text(entry), mailto)) {
2158                         if (yellow_initialised) {
2159                                 gtk_widget_modify_base(
2160                                         GTK_WIDGET(((ComposeHeaderEntry *)h_list->data)->entry),
2161                                         GTK_STATE_NORMAL, &yellow);
2162                                 gtk_widget_modify_text(
2163                                         GTK_WIDGET(((ComposeHeaderEntry *)h_list->data)->entry),
2164                                         GTK_STATE_NORMAL, &black);
2165                         }
2166                 }
2167         }
2168 }
2169
2170 void compose_toolbar_cb(gint action, gpointer data)
2171 {
2172         ToolbarItem *toolbar_item = (ToolbarItem*)data;
2173         Compose *compose = (Compose*)toolbar_item->parent;
2174         
2175         g_return_if_fail(compose != NULL);
2176
2177         switch(action) {
2178         case A_SEND:
2179                 compose_send_cb(compose, 0, NULL);
2180                 break;
2181         case A_SENDL:
2182                 compose_send_later_cb(compose, 0, NULL);
2183                 break;
2184         case A_DRAFT:
2185                 compose_draft_cb(compose, COMPOSE_QUIT_EDITING, NULL);
2186                 break;
2187         case A_INSERT:
2188                 compose_insert_file_cb(compose, 0, NULL);
2189                 break;
2190         case A_ATTACH:
2191                 compose_attach_cb(compose, 0, NULL);
2192                 break;
2193         case A_SIG:
2194                 compose_insert_sig(compose, FALSE);
2195                 break;
2196         case A_EXTEDITOR:
2197                 compose_ext_editor_cb(compose, 0, NULL);
2198                 break;
2199         case A_LINEWRAP_CURRENT:
2200                 compose_beautify_paragraph(compose, NULL, TRUE);
2201                 break;
2202         case A_LINEWRAP_ALL:
2203                 compose_wrap_all_full(compose, TRUE);
2204                 break;
2205         case A_ADDRBOOK:
2206                 compose_address_cb(compose, 0, NULL);
2207                 break;
2208 #ifdef USE_ASPELL
2209         case A_CHECK_SPELLING:
2210                 compose_check_all(compose);
2211                 break;
2212 #endif
2213         default:
2214                 break;
2215         }
2216 }
2217
2218 static void compose_entries_set(Compose *compose, const gchar *mailto)
2219 {
2220         gchar *to = NULL;
2221         gchar *cc = NULL;
2222         gchar *subject = NULL;
2223         gchar *body = NULL;
2224         gchar *temp = NULL;
2225         guint  len = 0;
2226         gchar *attach = NULL;
2227         
2228         scan_mailto_url(mailto, &to, &cc, NULL, &subject, &body, &attach);
2229
2230         if (to)
2231                 compose_entry_append(compose, to, COMPOSE_TO);
2232         if (cc)
2233                 compose_entry_append(compose, cc, COMPOSE_CC);
2234         if (subject) {
2235                 if (!g_utf8_validate (subject, -1, NULL)) {
2236                         temp = g_locale_to_utf8 (subject, -1, NULL, &len, NULL);
2237                         gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), temp);
2238                         g_free(temp);
2239                 } else {
2240                         gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), subject);
2241                 }
2242         }
2243         if (body) {
2244                 GtkTextView *text = GTK_TEXT_VIEW(compose->text);
2245                 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
2246                 GtkTextMark *mark;
2247                 GtkTextIter iter;
2248                 gboolean prev_autowrap = compose->autowrap;
2249
2250                 compose->autowrap = FALSE;
2251
2252                 mark = gtk_text_buffer_get_insert(buffer);
2253                 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
2254
2255                 if (!g_utf8_validate (body, -1, NULL)) {
2256                         temp = g_locale_to_utf8 (body, -1, NULL, &len, NULL);
2257                         gtk_text_buffer_insert(buffer, &iter, temp, -1);
2258                         g_free(temp);
2259                 } else {
2260                         gtk_text_buffer_insert(buffer, &iter, body, -1);
2261                 }
2262                 gtk_text_buffer_insert(buffer, &iter, "\n", 1);
2263
2264                 compose->autowrap = prev_autowrap;
2265                 if (compose->autowrap)
2266                         compose_wrap_all(compose);
2267         }
2268
2269         if (attach) {
2270                 gchar *utf8_filename = conv_filename_to_utf8(attach);
2271                 if (utf8_filename) {
2272                         if (compose_attach_append(compose, attach, utf8_filename, NULL)) {
2273                                 alertpanel_notice(_("The file '%s' has been attached."), attach);
2274                         } 
2275                         g_free(utf8_filename);
2276                 } else {
2277                         alertpanel_error(_("Couldn't attach a file (charset conversion failed)."));
2278                 }
2279         }
2280         g_free(to);
2281         g_free(cc);
2282         g_free(subject);
2283         g_free(body);
2284         g_free(attach);
2285 }
2286
2287 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
2288 {
2289         static HeaderEntry hentry[] = {{"Reply-To:",    NULL, TRUE},
2290                                        {"Cc:",          NULL, TRUE},
2291                                        {"References:",  NULL, FALSE},
2292                                        {"Bcc:",         NULL, TRUE},
2293                                        {"Newsgroups:",  NULL, TRUE},
2294                                        {"Followup-To:", NULL, TRUE},
2295                                        {"List-Post:",   NULL, FALSE},
2296                                        {"X-Priority:",  NULL, FALSE},
2297                                        {NULL,           NULL, FALSE}};
2298
2299         enum
2300         {
2301                 H_REPLY_TO      = 0,
2302                 H_CC            = 1,
2303                 H_REFERENCES    = 2,
2304                 H_BCC           = 3,
2305                 H_NEWSGROUPS    = 4,
2306                 H_FOLLOWUP_TO   = 5,
2307                 H_LIST_POST     = 6,
2308                 H_X_PRIORITY    = 7
2309         };
2310
2311         FILE *fp;
2312
2313         g_return_val_if_fail(msginfo != NULL, -1);
2314
2315         if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
2316         procheader_get_header_fields(fp, hentry);
2317         fclose(fp);
2318
2319         if (hentry[H_REPLY_TO].body != NULL) {
2320                 if (hentry[H_REPLY_TO].body[0] != '\0') {
2321                         compose->replyto =
2322                                 conv_unmime_header(hentry[H_REPLY_TO].body,
2323                                                    NULL);
2324                 }
2325                 g_free(hentry[H_REPLY_TO].body);
2326                 hentry[H_REPLY_TO].body = NULL;
2327         }
2328         if (hentry[H_CC].body != NULL) {
2329                 compose->cc = conv_unmime_header(hentry[H_CC].body, NULL);
2330                 g_free(hentry[H_CC].body);
2331                 hentry[H_CC].body = NULL;
2332         }
2333         if (hentry[H_REFERENCES].body != NULL) {
2334                 if (compose->mode == COMPOSE_REEDIT)
2335                         compose->references = hentry[H_REFERENCES].body;
2336                 else {
2337                         compose->references = compose_parse_references
2338                                 (hentry[H_REFERENCES].body, msginfo->msgid);
2339                         g_free(hentry[H_REFERENCES].body);
2340                 }
2341                 hentry[H_REFERENCES].body = NULL;
2342         }
2343         if (hentry[H_BCC].body != NULL) {
2344                 if (compose->mode == COMPOSE_REEDIT)
2345                         compose->bcc =
2346                                 conv_unmime_header(hentry[H_BCC].body, NULL);
2347                 g_free(hentry[H_BCC].body);
2348                 hentry[H_BCC].body = NULL;
2349         }
2350         if (hentry[H_NEWSGROUPS].body != NULL) {
2351                 compose->newsgroups = hentry[H_NEWSGROUPS].body;
2352                 hentry[H_NEWSGROUPS].body = NULL;
2353         }
2354         if (hentry[H_FOLLOWUP_TO].body != NULL) {
2355                 if (hentry[H_FOLLOWUP_TO].body[0] != '\0') {
2356                         compose->followup_to =
2357                                 conv_unmime_header(hentry[H_FOLLOWUP_TO].body,
2358                                                    NULL);
2359                 }
2360                 g_free(hentry[H_FOLLOWUP_TO].body);
2361                 hentry[H_FOLLOWUP_TO].body = NULL;
2362         }
2363         if (hentry[H_LIST_POST].body != NULL) {
2364                 gchar *to = NULL;
2365
2366                 extract_address(hentry[H_LIST_POST].body);
2367                 if (hentry[H_LIST_POST].body[0] != '\0') {
2368                         scan_mailto_url(hentry[H_LIST_POST].body,
2369                                         &to, NULL, NULL, NULL, NULL, NULL);
2370                         if (to) {
2371                                 g_free(compose->ml_post);
2372                                 compose->ml_post = to;
2373                         }
2374                 }
2375                 g_free(hentry[H_LIST_POST].body);
2376                 hentry[H_LIST_POST].body = NULL;
2377         }
2378
2379         /* CLAWS - X-Priority */
2380         if (compose->mode == COMPOSE_REEDIT)
2381                 if (hentry[H_X_PRIORITY].body != NULL) {
2382                         gint priority;
2383                         
2384                         priority = atoi(hentry[H_X_PRIORITY].body);
2385                         g_free(hentry[H_X_PRIORITY].body);
2386                         
2387                         hentry[H_X_PRIORITY].body = NULL;
2388                         
2389                         if (priority < PRIORITY_HIGHEST || 
2390                             priority > PRIORITY_LOWEST)
2391                                 priority = PRIORITY_NORMAL;
2392                         
2393                         compose->priority =  priority;
2394                 }
2395  
2396         if (compose->mode == COMPOSE_REEDIT) {
2397                 if (msginfo->inreplyto && *msginfo->inreplyto)
2398                         compose->inreplyto = g_strdup(msginfo->inreplyto);
2399                 return 0;
2400         }
2401
2402         if (msginfo->msgid && *msginfo->msgid)
2403                 compose->inreplyto = g_strdup(msginfo->msgid);
2404
2405         if (!compose->references) {
2406                 if (msginfo->msgid && *msginfo->msgid) {
2407                         if (msginfo->inreplyto && *msginfo->inreplyto)
2408                                 compose->references =
2409                                         g_strdup_printf("<%s>\n\t<%s>",
2410                                                         msginfo->inreplyto,
2411                                                         msginfo->msgid);
2412                         else
2413                                 compose->references =
2414                                         g_strconcat("<", msginfo->msgid, ">",
2415                                                     NULL);
2416                 } else if (msginfo->inreplyto && *msginfo->inreplyto) {
2417                         compose->references =
2418                                 g_strconcat("<", msginfo->inreplyto, ">",
2419                                             NULL);
2420                 }
2421         }
2422
2423         return 0;
2424 }
2425
2426 static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
2427 {
2428         GSList *ref_id_list, *cur;
2429         GString *new_ref;
2430         gchar *new_ref_str;
2431
2432         ref_id_list = references_list_append(NULL, ref);
2433         if (!ref_id_list) return NULL;
2434         if (msgid && *msgid)
2435                 ref_id_list = g_slist_append(ref_id_list, g_strdup(msgid));
2436
2437         for (;;) {
2438                 gint len = 0;
2439
2440                 for (cur = ref_id_list; cur != NULL; cur = cur->next)
2441                         /* "<" + Message-ID + ">" + CR+LF+TAB */
2442                         len += strlen((gchar *)cur->data) + 5;
2443
2444                 if (len > MAX_REFERENCES_LEN) {
2445                         /* remove second message-ID */
2446                         if (ref_id_list && ref_id_list->next &&
2447                             ref_id_list->next->next) {
2448                                 g_free(ref_id_list->next->data);
2449                                 ref_id_list = g_slist_remove
2450                                         (ref_id_list, ref_id_list->next->data);
2451                         } else {
2452                                 slist_free_strings(ref_id_list);
2453                                 g_slist_free(ref_id_list);
2454                                 return NULL;
2455                         }
2456                 } else
2457                         break;
2458         }
2459
2460         new_ref = g_string_new("");
2461         for (cur = ref_id_list; cur != NULL; cur = cur->next) {
2462                 if (new_ref->len > 0)
2463                         g_string_append(new_ref, "\n\t");
2464                 g_string_append_printf(new_ref, "<%s>", (gchar *)cur->data);
2465         }
2466
2467         slist_free_strings(ref_id_list);
2468         g_slist_free(ref_id_list);
2469
2470         new_ref_str = new_ref->str;
2471         g_string_free(new_ref, FALSE);
2472
2473         return new_ref_str;
2474 }
2475
2476 static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
2477                                 const gchar *fmt, const gchar *qmark,
2478                                 const gchar *body, gboolean rewrap,
2479                                 const gchar *err_msg)
2480 {
2481         MsgInfo* dummyinfo = NULL;
2482         gchar *quote_str = NULL;
2483         gchar *buf;
2484         gboolean prev_autowrap;
2485         const gchar *trimmed_body = body;
2486         gint cursor_pos = -1;
2487         GtkTextView *text = GTK_TEXT_VIEW(compose->text);
2488         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
2489         GtkTextIter iter;
2490         GtkTextMark *mark;
2491         
2492
2493         SIGNAL_BLOCK(buffer);
2494
2495         if (!msginfo) {
2496                 dummyinfo = compose_msginfo_new_from_compose(compose);
2497                 msginfo = dummyinfo;
2498         }
2499
2500         if (qmark != NULL) {
2501                 quote_fmt_init(msginfo, NULL, NULL, FALSE, compose->account);
2502                 quote_fmt_scan_string(qmark);
2503                 quote_fmt_parse();
2504
2505                 buf = quote_fmt_get_buffer();
2506                 if (buf == NULL)
2507                         alertpanel_error(_("Quote mark format error."));
2508                 else
2509                         Xstrdup_a(quote_str, buf, goto error)
2510         }
2511
2512         if (fmt && *fmt != '\0') {
2513                 gchar *tmp = NULL;
2514
2515                 if (trimmed_body)
2516                         while (*trimmed_body == '\n')
2517                                 trimmed_body++;
2518
2519                 /* decode \-escape sequences in the internal representation of the quote format */
2520                 tmp = malloc(strlen(fmt)+1);
2521                 pref_get_unescaped_pref(tmp, fmt);
2522
2523                 quote_fmt_init(msginfo, quote_str, trimmed_body, FALSE, compose->account);
2524                 quote_fmt_scan_string(tmp);
2525                 quote_fmt_parse();
2526                 g_free(tmp);
2527
2528                 buf = quote_fmt_get_buffer();
2529                 if (buf == NULL) {
2530                         alertpanel_error(err_msg);
2531                         goto error;
2532                 }
2533         } else
2534                 buf = "";
2535
2536         prev_autowrap = compose->autowrap;
2537         compose->autowrap = FALSE;
2538
2539         mark = gtk_text_buffer_get_insert(buffer);
2540         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
2541         if (g_utf8_validate(buf, -1, NULL)) { 
2542                 gtk_text_buffer_insert(buffer, &iter, buf, -1);
2543         } else {
2544                 gchar *tmpout = NULL;
2545                 tmpout = conv_codeset_strdup
2546                         (buf, conv_get_locale_charset_str_no_utf8(),
2547                          CS_INTERNAL);
2548                 if (!tmpout || !g_utf8_validate(tmpout, -1, NULL)) {
2549                         g_free(tmpout);
2550                         tmpout = g_malloc(strlen(buf)*2+1);
2551                         conv_localetodisp(tmpout, strlen(buf)*2+1, buf);
2552                 }
2553                 gtk_text_buffer_insert(buffer, &iter, tmpout, -1);
2554                 g_free(tmpout);
2555         }
2556
2557         cursor_pos = quote_fmt_get_cursor_pos();
2558         compose->set_cursor_pos = cursor_pos;
2559         if (cursor_pos == -1) {
2560                 cursor_pos = 0;
2561         }
2562         gtk_text_buffer_get_start_iter(buffer, &iter);
2563         gtk_text_buffer_get_iter_at_offset(buffer, &iter, cursor_pos);
2564         gtk_text_buffer_place_cursor(buffer, &iter);
2565
2566         compose->autowrap = prev_autowrap;
2567         if (compose->autowrap && rewrap)
2568                 compose_wrap_all(compose);
2569
2570         goto ok;
2571
2572 error:
2573         buf = NULL;
2574 ok:
2575         SIGNAL_UNBLOCK(buffer);
2576
2577         procmsg_msginfo_free( dummyinfo );
2578
2579         return buf;
2580 }
2581
2582 /* if ml_post is of type addr@host and from is of type
2583  * addr-anything@host, return TRUE
2584  */
2585 static gboolean is_subscription(const gchar *ml_post, const gchar *from)
2586 {
2587         gchar *left_ml = NULL;
2588         gchar *right_ml = NULL;
2589         gchar *left_from = NULL;
2590         gchar *right_from = NULL;
2591         gboolean result = FALSE;
2592         
2593         if (!ml_post || !from)
2594                 return FALSE;
2595         
2596         left_ml = g_strdup(ml_post);
2597         if (strstr(left_ml, "@")) {
2598                 right_ml = strstr(left_ml, "@")+1;
2599                 *(strstr(left_ml, "@")) = '\0';
2600         }
2601         
2602         left_from = g_strdup(from);
2603         if (strstr(left_from, "@")) {
2604                 right_from = strstr(left_from, "@")+1;
2605                 *(strstr(left_from, "@")) = '\0';
2606         }
2607         
2608         if (left_ml && left_from && right_ml && right_from
2609         &&  !strncmp(left_from, left_ml, strlen(left_ml))
2610         &&  !strcmp(right_from, right_ml)) {
2611                 result = TRUE;
2612         }
2613         g_free(left_ml);
2614         g_free(left_from);
2615         
2616         return result;
2617 }
2618
2619 static gboolean same_address(const gchar *addr1, const gchar *addr2)
2620 {
2621         gchar *my_addr1, *my_addr2;
2622         
2623         if (!addr1 || !addr2)
2624                 return FALSE;
2625
2626         Xstrdup_a(my_addr1, addr1, return FALSE);
2627         Xstrdup_a(my_addr2, addr2, return FALSE);
2628         
2629         extract_address(my_addr1);
2630         extract_address(my_addr2);
2631         
2632         return !strcasecmp(my_addr1, my_addr2);
2633 }
2634
2635 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
2636                                     gboolean to_all, gboolean to_ml,
2637                                     gboolean to_sender,
2638                                     gboolean followup_and_reply_to)
2639 {
2640         GSList *cc_list = NULL;
2641         GSList *cur;
2642         gchar *from = NULL;
2643         gchar *replyto = NULL;
2644         GHashTable *to_table;
2645
2646         gboolean reply_to_ml = FALSE;
2647         gboolean default_reply_to = FALSE;
2648
2649         g_return_if_fail(compose->account != NULL);
2650         g_return_if_fail(msginfo != NULL);
2651
2652         reply_to_ml = to_ml && compose->ml_post;
2653
2654         default_reply_to = msginfo->folder && 
2655                 msginfo->folder->prefs->enable_default_reply_to;
2656
2657         if (compose->account->protocol != A_NNTP) {
2658                 if (reply_to_ml && !default_reply_to) {
2659                         
2660                         gboolean is_subscr = is_subscription(compose->ml_post,
2661                                                              msginfo->from);
2662                         if (!is_subscr) {
2663                                 /* normal answer to ml post with a reply-to */
2664                                 compose_entry_append(compose,
2665                                            compose->ml_post,
2666                                            COMPOSE_TO);
2667                                 if (compose->replyto
2668                                 &&  !same_address(compose->ml_post, compose->replyto))
2669                                         compose_entry_append(compose,
2670                                                 compose->replyto,
2671                                                 COMPOSE_CC);
2672                         } else {
2673                                 /* answer to subscription confirmation */
2674                                 if (compose->replyto)
2675                                         compose_entry_append(compose,
2676                                                 compose->replyto,
2677                                                 COMPOSE_TO);
2678                                 else if (msginfo->from)
2679                                         compose_entry_append(compose,
2680                                                 msginfo->from,
2681                                                 COMPOSE_TO);
2682                         }
2683                 }
2684                 else if (!(to_all || to_sender) && default_reply_to) {
2685                         compose_entry_append(compose,
2686                             msginfo->folder->prefs->default_reply_to,
2687                             COMPOSE_TO);
2688                         compose_entry_mark_default_to(compose,
2689                                 msginfo->folder->prefs->default_reply_to);
2690                 } else {
2691                         gchar *tmp1 = NULL;
2692                         if (!msginfo->from)
2693                                 return;
2694                         Xstrdup_a(tmp1, msginfo->from, return);
2695                         extract_address(tmp1);
2696                         if (to_all || to_sender ||
2697                             !account_find_from_address(tmp1))
2698                                 compose_entry_append(compose,
2699                                  (compose->replyto && !to_sender)
2700                                           ? compose->replyto :
2701                                           msginfo->from ? msginfo->from : "",
2702                                           COMPOSE_TO);
2703                         else if (!to_all && !to_sender) {
2704                                 /* reply to the last list of recipients */
2705                                 compose_entry_append(compose,
2706                                           msginfo->to ? msginfo->to : "",
2707                                           COMPOSE_TO);
2708                                 compose_entry_append(compose,
2709                                           msginfo->cc ? msginfo->cc : "",
2710                                           COMPOSE_CC);
2711                         }
2712                 }
2713         } else {
2714                 if (to_sender || (compose->followup_to && 
2715                         !strncmp(compose->followup_to, "poster", 6)))
2716                         compose_entry_append
2717                                 (compose, 
2718                                  (compose->replyto ? compose->replyto :
2719                                         msginfo->from ? msginfo->from : ""),
2720                                  COMPOSE_TO);
2721                                  
2722                 else if (followup_and_reply_to || to_all) {
2723                         compose_entry_append
2724                                 (compose,
2725                                  (compose->replyto ? compose->replyto :
2726                                  msginfo->from ? msginfo->from : ""),
2727                                  COMPOSE_TO);                           
2728                 
2729                         compose_entry_append
2730                                 (compose,
2731                                  compose->followup_to ? compose->followup_to :
2732                                  compose->newsgroups ? compose->newsgroups : "",
2733                                  COMPOSE_NEWSGROUPS);
2734                 } 
2735                 else 
2736                         compose_entry_append
2737                                 (compose,
2738                                  compose->followup_to ? compose->followup_to :
2739                                  compose->newsgroups ? compose->newsgroups : "",
2740                                  COMPOSE_NEWSGROUPS);
2741         }
2742
2743         if (msginfo->subject && *msginfo->subject) {
2744                 gchar *buf, *buf2;
2745                 gchar *p;
2746
2747                 buf = p = g_strdup(msginfo->subject);
2748                 p += subject_get_prefix_length(p);
2749                 memmove(buf, p, strlen(p) + 1);
2750
2751                 buf2 = g_strdup_printf("Re: %s", buf);
2752                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
2753
2754                 g_free(buf2);
2755                 g_free(buf);
2756         } else
2757                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
2758
2759         if (to_ml && compose->ml_post) return;
2760         if (!to_all || compose->account->protocol == A_NNTP) return;
2761
2762         if (compose->replyto) {
2763                 Xstrdup_a(replyto, compose->replyto, return);
2764                 extract_address(replyto);
2765         }
2766         if (msginfo->from) {
2767                 Xstrdup_a(from, msginfo->from, return);
2768                 extract_address(from);
2769         }
2770
2771         if (replyto && from)
2772                 cc_list = address_list_append_with_comments(cc_list, from);
2773         if (to_all && msginfo->folder && 
2774             msginfo->folder->prefs->enable_default_reply_to)
2775                 cc_list = address_list_append_with_comments(cc_list,
2776                                 msginfo->folder->prefs->default_reply_to);
2777         cc_list = address_list_append_with_comments(cc_list, msginfo->to);
2778         cc_list = address_list_append_with_comments(cc_list, compose->cc);
2779
2780         to_table = g_hash_table_new(g_str_hash, g_str_equal);
2781         if (replyto)
2782                 g_hash_table_insert(to_table, g_strdup(replyto), GINT_TO_POINTER(1));
2783         if (compose->account)
2784                 g_hash_table_insert(to_table, g_strdup(compose->account->address),
2785                                     GINT_TO_POINTER(1));
2786
2787         /* remove address on To: and that of current account */
2788         for (cur = cc_list; cur != NULL; ) {
2789                 GSList *next = cur->next;
2790                 gchar *addr;
2791
2792                 addr = g_strdup(cur->data);
2793                 extract_address(addr);
2794
2795                 if (GPOINTER_TO_INT(g_hash_table_lookup(to_table, addr)) == 1)
2796                         cc_list = g_slist_remove(cc_list, cur->data);
2797                 else
2798                         g_hash_table_insert(to_table, addr, GINT_TO_POINTER(1));
2799
2800                 cur = next;
2801         }
2802         hash_free_strings(to_table);
2803         g_hash_table_destroy(to_table);
2804
2805         if (cc_list) {
2806                 for (cur = cc_list; cur != NULL; cur = cur->next)
2807                         compose_entry_append(compose, (gchar *)cur->data,
2808                                              COMPOSE_CC);
2809                 slist_free_strings(cc_list);
2810                 g_slist_free(cc_list);
2811         }
2812
2813 }
2814
2815 #define SET_ENTRY(entry, str) \
2816 { \
2817         if (str && *str) \
2818                 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
2819 }
2820
2821 #define SET_ADDRESS(type, str) \
2822 { \
2823         if (str && *str) \
2824                 compose_entry_append(compose, str, type); \
2825 }
2826
2827 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
2828 {
2829         g_return_if_fail(msginfo != NULL);
2830
2831         SET_ENTRY(subject_entry, msginfo->subject);
2832         SET_ENTRY(from_name, msginfo->from);
2833         SET_ADDRESS(COMPOSE_TO, msginfo->to);
2834         SET_ADDRESS(COMPOSE_CC, compose->cc);
2835         SET_ADDRESS(COMPOSE_BCC, compose->bcc);
2836         SET_ADDRESS(COMPOSE_REPLYTO, compose->replyto);
2837         SET_ADDRESS(COMPOSE_NEWSGROUPS, compose->newsgroups);
2838         SET_ADDRESS(COMPOSE_FOLLOWUPTO, compose->followup_to);
2839
2840         compose_update_priority_menu_item(compose);
2841         compose_update_privacy_system_menu_item(compose, FALSE);
2842         compose_show_first_last_header(compose, TRUE);
2843 }
2844
2845 #undef SET_ENTRY
2846 #undef SET_ADDRESS
2847
2848 static void compose_insert_sig(Compose *compose, gboolean replace)
2849 {
2850         GtkTextView *text = GTK_TEXT_VIEW(compose->text);
2851         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
2852         GtkTextMark *mark;
2853         GtkTextIter iter, iter_end;
2854         gint cur_pos;
2855         gchar *search = NULL;
2856         gboolean prev_autowrap;
2857         gboolean found = FALSE, shift = FALSE;
2858
2859         
2860         g_return_if_fail(compose->account != NULL);
2861
2862         prev_autowrap = compose->autowrap;
2863         compose->autowrap = FALSE;
2864
2865         g_signal_handlers_block_by_func(G_OBJECT(buffer),
2866                                         G_CALLBACK(compose_changed_cb),
2867                                         compose);
2868         
2869         mark = gtk_text_buffer_get_insert(buffer);
2870         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
2871         cur_pos = gtk_text_iter_get_offset (&iter);
2872
2873         gtk_text_buffer_get_end_iter(buffer, &iter);
2874
2875         search = compose->sig_str;
2876 again:
2877         if (replace && search) {
2878                 GtkTextIter first_iter, start_iter, end_iter;
2879
2880                 gtk_text_buffer_get_start_iter(buffer, &first_iter);
2881
2882                 if (compose->sig_str[0] == '\0')
2883                         found = FALSE;
2884                 else
2885                         found = gtk_text_iter_forward_search(&first_iter,
2886                                                              search,
2887                                                              GTK_TEXT_SEARCH_TEXT_ONLY,
2888                                                              &start_iter, &end_iter,
2889                                                              NULL);
2890
2891                 if (found) {
2892                         gtk_text_buffer_delete(buffer, &start_iter, &end_iter);
2893                         iter = start_iter;
2894                 }
2895         } 
2896         if (replace && !found && search && strlen(search) > 2
2897         &&  search[0] == '\n' && search[1] == '\n') {
2898                 search ++;
2899                 shift = TRUE;
2900                 goto again;
2901         }
2902
2903         g_free(compose->sig_str);
2904         compose->sig_str = compose_get_signature_str(compose);
2905         if (!compose->sig_str || (replace && !compose->account->auto_sig))
2906                 compose->sig_str = g_strdup("");
2907
2908         cur_pos = gtk_text_iter_get_offset(&iter);
2909         if (shift && found)
2910                 gtk_text_buffer_insert(buffer, &iter, compose->sig_str + 1, -1);
2911         else
2912                 gtk_text_buffer_insert(buffer, &iter, compose->sig_str, -1);
2913         /* skip \n\n */
2914         gtk_text_buffer_get_iter_at_offset(buffer, &iter, cur_pos);
2915         gtk_text_iter_forward_char(&iter);
2916         gtk_text_iter_forward_char(&iter);
2917         gtk_text_buffer_get_end_iter(buffer, &iter_end);
2918         gtk_text_buffer_apply_tag_by_name(buffer,"signature",&iter, &iter_end);
2919
2920         if (cur_pos > gtk_text_buffer_get_char_count (buffer))
2921                 cur_pos = gtk_text_buffer_get_char_count (buffer);
2922
2923         /* put the cursor where it should be 
2924          * either where the quote_fmt says, either before the signature */
2925         if (compose->set_cursor_pos < 0)
2926                 gtk_text_buffer_get_iter_at_offset(buffer, &iter, cur_pos);
2927         else
2928                 gtk_text_buffer_get_iter_at_offset(buffer, &iter, 
2929                         compose->set_cursor_pos);
2930                 
2931         gtk_text_buffer_place_cursor(buffer, &iter);
2932         g_signal_handlers_unblock_by_func(G_OBJECT(buffer),
2933                                         G_CALLBACK(compose_changed_cb),
2934                                         compose);
2935                 
2936         compose->autowrap = prev_autowrap;
2937         if (compose->autowrap)
2938                 compose_wrap_all(compose);
2939 }
2940
2941 static gchar *compose_get_signature_str(Compose *compose)
2942 {
2943         gchar *sig_body = NULL;
2944         gchar *sig_str = NULL;
2945         gchar *utf8_sig_str = NULL;
2946
2947         g_return_val_if_fail(compose->account != NULL, NULL);
2948
2949         if (!compose->account->sig_path)
2950                 return NULL;
2951
2952         if (compose->account->sig_type == SIG_FILE) {
2953                 if (!is_file_or_fifo_exist(compose->account->sig_path)) {
2954                         g_warning("can't open signature file: %s\n",
2955                                   compose->account->sig_path);
2956                         return NULL;
2957                 }
2958         }
2959
2960         if (compose->account->sig_type == SIG_COMMAND)
2961                 sig_body = get_command_output(compose->account->sig_path);
2962         else {
2963                 gchar *tmp;
2964
2965                 tmp = file_read_to_str(compose->account->sig_path);
2966                 if (!tmp)
2967                         return NULL;
2968                 sig_body = normalize_newlines(tmp);
2969                 g_free(tmp);
2970         }
2971
2972         if (compose->account->sig_sep) {
2973                 sig_str = g_strconcat("\n\n", compose->account->sig_sep, "\n", sig_body,
2974                                       NULL);
2975                 g_free(sig_body);
2976         } else
2977                 sig_str = g_strconcat("\n\n", sig_body, NULL);
2978
2979         if (sig_str) {
2980                 if (g_utf8_validate(sig_str, -1, NULL) == TRUE)
2981                         utf8_sig_str = sig_str;
2982                 else {
2983                         utf8_sig_str = conv_codeset_strdup
2984                                 (sig_str, conv_get_locale_charset_str_no_utf8(),
2985                                  CS_INTERNAL);
2986                         g_free(sig_str);
2987                 }
2988         }
2989
2990         return utf8_sig_str;
2991 }
2992
2993 static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *file)
2994 {
2995         GtkTextView *text;
2996         GtkTextBuffer *buffer;
2997         GtkTextMark *mark;
2998         GtkTextIter iter;
2999         const gchar *cur_encoding;
3000         gchar buf[BUFFSIZE];
3001         gint len;
3002         FILE *fp;
3003         gboolean prev_autowrap;
3004         gboolean badtxt = FALSE;
3005
3006         g_return_val_if_fail(file != NULL, COMPOSE_INSERT_NO_FILE);
3007
3008         if ((fp = g_fopen(file, "rb")) == NULL) {
3009                 FILE_OP_ERROR(file, "fopen");
3010                 return COMPOSE_INSERT_READ_ERROR;
3011         }
3012
3013         prev_autowrap = compose->autowrap;
3014         compose->autowrap = FALSE;
3015
3016         text = GTK_TEXT_VIEW(compose->text);
3017         buffer = gtk_text_view_get_buffer(text);
3018         mark = gtk_text_buffer_get_insert(buffer);
3019         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
3020
3021         g_signal_handlers_block_by_func(G_OBJECT(buffer),
3022                                         G_CALLBACK(text_inserted),
3023                                         compose);
3024
3025         cur_encoding = conv_get_locale_charset_str_no_utf8();
3026
3027         while (fgets(buf, sizeof(buf), fp) != NULL) {
3028                 gchar *str;
3029
3030                 if (g_utf8_validate(buf, -1, NULL) == TRUE)
3031                         str = g_strdup(buf);
3032                 else
3033                         str = conv_codeset_strdup
3034                                 (buf, cur_encoding, CS_INTERNAL);
3035                 if (!str) continue;
3036
3037                 /* strip <CR> if DOS/Windows file,
3038                    replace <CR> with <LF> if Macintosh file. */
3039                 strcrchomp(str);
3040                 len = strlen(str);
3041                 if (len > 0 && str[len - 1] != '\n') {
3042                         while (--len >= 0)
3043                                 if (str[len] == '\r') str[len] = '\n';
3044                 }
3045
3046                 gtk_text_buffer_insert(buffer, &iter, str, -1);
3047                 g_free(str);
3048         }
3049
3050         g_signal_handlers_unblock_by_func(G_OBJECT(buffer),
3051                                           G_CALLBACK(text_inserted),
3052                                           compose);
3053         compose->autowrap = prev_autowrap;
3054         if (compose->autowrap)
3055                 compose_wrap_all(compose);
3056
3057         fclose(fp);
3058
3059         if (badtxt)
3060                 return COMPOSE_INSERT_INVALID_CHARACTER;
3061         else 
3062                 return COMPOSE_INSERT_SUCCESS;
3063 }
3064
3065 static gboolean compose_attach_append(Compose *compose, const gchar *file,
3066                                   const gchar *filename,
3067                                   const gchar *content_type)
3068 {
3069         AttachInfo *ainfo;
3070         GtkTreeIter iter;
3071         FILE *fp;
3072         off_t size;
3073         GAuto *auto_ainfo;
3074         gchar *size_text;
3075         GtkListStore *store;
3076         gchar *name;
3077         gboolean has_binary = FALSE;
3078
3079         if (!is_file_exist(file)) {
3080                 alertpanel_error("File %s doesn't exist\n", filename);
3081                 return FALSE;
3082         }
3083         if ((size = get_file_size(file)) < 0) {
3084                 alertpanel_error("Can't get file size of %s\n", filename);
3085                 return FALSE;
3086         }
3087         if (size == 0) {
3088                 alertpanel_error(_("File %s is empty."), filename);
3089                 return FALSE;
3090         }
3091         if ((fp = g_fopen(file, "rb")) == NULL) {
3092                 alertpanel_error(_("Can't read %s."), filename);
3093                 return FALSE;
3094         }
3095         fclose(fp);
3096
3097         ainfo = g_new0(AttachInfo, 1);
3098         auto_ainfo = g_auto_pointer_new_with_free
3099                         (ainfo, (GFreeFunc) compose_attach_info_free); 
3100         ainfo->file = g_strdup(file);
3101
3102         if (content_type) {
3103                 ainfo->content_type = g_strdup(content_type);
3104                 if (!g_ascii_strcasecmp(content_type, "message/rfc822")) {
3105                         MsgInfo *msginfo;
3106                         MsgFlags flags = {0, 0};
3107
3108                         if (procmime_get_encoding_for_text_file(file, &has_binary) == ENC_7BIT)
3109                                 ainfo->encoding = ENC_7BIT;
3110                         else
3111                                 ainfo->encoding = ENC_8BIT;
3112
3113                         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
3114                         if (msginfo && msginfo->subject)
3115                                 name = g_strdup(msginfo->subject);
3116                         else
3117                                 name = g_path_get_basename(filename ? filename : file);
3118
3119                         ainfo->name = g_strdup_printf(_("Message: %s"), name);
3120
3121                         procmsg_msginfo_free(msginfo);
3122                 } else {
3123                         if (!g_ascii_strncasecmp(content_type, "text", 4))
3124                                 ainfo->encoding = procmime_get_encoding_for_text_file(file, &has_binary);
3125                         else
3126                                 ainfo->encoding = ENC_BASE64;
3127                         name = g_path_get_basename(filename ? filename : file);
3128                         ainfo->name = g_strdup(name);
3129                 }
3130                 g_free(name);
3131         } else {
3132                 ainfo->content_type = procmime_get_mime_type(file);
3133                 if (!ainfo->content_type) {
3134                         ainfo->content_type =
3135                                 g_strdup("application/octet-stream");
3136                         ainfo->encoding = ENC_BASE64;
3137                 } else if (!g_ascii_strncasecmp(ainfo->content_type, "text", 4))
3138                         ainfo->encoding =
3139                                 procmime_get_encoding_for_text_file(file, &has_binary);
3140                 else
3141                         ainfo->encoding = ENC_BASE64;
3142                 name = g_path_get_basename(filename ? filename : file);
3143                 ainfo->name = g_strdup(name);   
3144                 g_free(name);
3145         }
3146
3147         if (ainfo->name != NULL
3148         &&  !strcmp(ainfo->name, ".")) {
3149                 g_free(ainfo->name);
3150                 ainfo->name = NULL;
3151         }
3152
3153         if (!strcmp(ainfo->content_type, "unknown") || has_binary) {
3154                 g_free(ainfo->content_type);
3155                 ainfo->content_type = g_strdup("application/octet-stream");
3156         }
3157
3158         ainfo->size = size;
3159         size_text = to_human_readable(size);
3160
3161         store = GTK_LIST_STORE(gtk_tree_view_get_model
3162                         (GTK_TREE_VIEW(compose->attach_clist)));
3163                 
3164         gtk_list_store_append(store, &iter);
3165         gtk_list_store_set(store, &iter, 
3166                            COL_MIMETYPE, ainfo->content_type,
3167                            COL_SIZE, size_text,
3168                            COL_NAME, ainfo->name,
3169                            COL_DATA, ainfo,
3170                            COL_AUTODATA, auto_ainfo,
3171                            -1);
3172         
3173         g_auto_pointer_free(auto_ainfo);
3174         return TRUE;
3175 }
3176
3177 static void compose_use_signing(Compose *compose, gboolean use_signing)
3178 {
3179         GtkItemFactory *ifactory;
3180         GtkWidget *menuitem = NULL;
3181
3182         compose->use_signing = use_signing;
3183         ifactory = gtk_item_factory_from_widget(compose->menubar);
3184         menuitem = gtk_item_factory_get_item
3185                 (ifactory, "/Options/Sign");
3186         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), 
3187                                        use_signing);
3188 }
3189
3190 static void compose_use_encryption(Compose *compose, gboolean use_encryption)
3191 {
3192         GtkItemFactory *ifactory;
3193         GtkWidget *menuitem = NULL;
3194
3195         compose->use_encryption = use_encryption;
3196         ifactory = gtk_item_factory_from_widget(compose->menubar);
3197         menuitem = gtk_item_factory_get_item
3198                 (ifactory, "/Options/Encrypt");
3199
3200         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), 
3201                                        use_encryption);
3202 }
3203
3204 #define NEXT_PART_NOT_CHILD(info)  \
3205 {  \
3206         node = info->node;  \
3207         while (node->children)  \
3208                 node = g_node_last_child(node);  \
3209         info = procmime_mimeinfo_next((MimeInfo *)node->data);  \
3210 }
3211
3212 static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
3213 {
3214         MimeInfo *mimeinfo;
3215         MimeInfo *child;
3216         MimeInfo *firsttext = NULL;
3217         MimeInfo *encrypted = NULL;
3218         GNode    *node;
3219         gchar *outfile;
3220         const gchar *partname = NULL;
3221
3222         mimeinfo = procmime_scan_message(msginfo);
3223         if (!mimeinfo) return;
3224
3225         if (mimeinfo->node->children == NULL) {
3226                 procmime_mimeinfo_free_all(mimeinfo);
3227                 return;
3228         }
3229
3230         /* find first content part */
3231         child = (MimeInfo *) mimeinfo->node->children->data;
3232         while (child && child->node->children && (child->type == MIMETYPE_MULTIPART))
3233                 child = (MimeInfo *)child->node->children->data;