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