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