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