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