2006-11-13 [wwp] 2.6.0cvs39
[claws.git] / src / compose.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #ifndef PANGO_ENABLE_ENGINE
27 #  define PANGO_ENABLE_ENGINE
28 #endif
29
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <gtk/gtkmain.h>
34 #include <gtk/gtkmenu.h>
35 #include <gtk/gtkmenuitem.h>
36 #include <gtk/gtkitemfactory.h>
37 #include <gtk/gtkcheckmenuitem.h>
38 #include <gtk/gtkoptionmenu.h>
39 #include <gtk/gtkwidget.h>
40 #include <gtk/gtkvpaned.h>
41 #include <gtk/gtkentry.h>
42 #include <gtk/gtkeditable.h>
43 #include <gtk/gtkwindow.h>
44 #include <gtk/gtksignal.h>
45 #include <gtk/gtkvbox.h>
46 #include <gtk/gtkcontainer.h>
47 #include <gtk/gtkhandlebox.h>
48 #include <gtk/gtktoolbar.h>
49 #include <gtk/gtktable.h>
50 #include <gtk/gtkhbox.h>
51 #include <gtk/gtklabel.h>
52 #include <gtk/gtkscrolledwindow.h>
53 #include <gtk/gtktreeview.h>
54 #include <gtk/gtkliststore.h>
55 #include <gtk/gtktreeselection.h>
56 #include <gtk/gtktreemodel.h>
57
58 #include <gtk/gtkdnd.h>
59 #include <gtk/gtkclipboard.h>
60 #include <pango/pango-break.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <ctype.h>
65 #include <sys/types.h>
66 #include <sys/stat.h>
67 #include <unistd.h>
68 #include <time.h>
69 #include <stdlib.h>
70 #if HAVE_SYS_WAIT_H
71 #  include <sys/wait.h>
72 #endif
73 #include <signal.h>
74 #include <errno.h>
75 #ifndef G_OS_WIN32  /* fixme we should have a configure test. */
76 #include <libgen.h>
77 #endif
78
79 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
80 #  include <wchar.h>
81 #  include <wctype.h>
82 #endif
83
84 #include "sylpheed.h"
85 #include "main.h"
86 #include "mainwindow.h"
87 #include "compose.h"
88 #include "addressbook.h"
89 #include "folderview.h"
90 #include "procmsg.h"
91 #include "menu.h"
92 #include "stock_pixmap.h"
93 #include "send_message.h"
94 #include "imap.h"
95 #include "news.h"
96 #include "customheader.h"
97 #include "prefs_common.h"
98 #include "prefs_account.h"
99 #include "action.h"
100 #include "account.h"
101 #include "filesel.h"
102 #include "procheader.h"
103 #include "procmime.h"
104 #include "statusbar.h"
105 #include "about.h"
106 #include "base64.h"
107 #include "quoted-printable.h"
108 #include "codeconv.h"
109 #include "utils.h"
110 #include "gtkutils.h"
111 #include "socket.h"
112 #include "alertpanel.h"
113 #include "manage_window.h"
114 #include "gtkshruler.h"
115 #include "folder.h"
116 #include "addr_compl.h"
117 #include "quote_fmt.h"
118 #include "undo.h"
119 #include "foldersel.h"
120 #include "toolbar.h"
121 #include "inc.h"
122 #include "message_search.h"
123 #include "combobox.h"
124 #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_subject);
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         {N_("/---"),            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 && item->prefs->enable_default_dictionary &&
976             compose->gtkaspell) 
977                 gtkaspell_change_dict(compose->gtkaspell, 
978                     item->prefs->default_dictionary);
979         compose_spell_menu_changed(compose);
980 #endif
981
982         if (account->auto_sig)
983                 compose_insert_sig(compose, FALSE);
984         gtk_text_buffer_get_start_iter(textbuf, &iter);
985         gtk_text_buffer_place_cursor(textbuf, &iter);
986
987         if (account->protocol != A_NNTP) {
988                 if (mailto && *mailto != '\0') {
989                         compose_entries_set(compose, mailto);
990
991                 } else if (item && item->prefs->enable_default_to) {
992                         compose_entry_append(compose, item->prefs->default_to, COMPOSE_TO);
993                         compose_entry_mark_default_to(compose, item->prefs->default_to);
994                 }
995                 if (item && item->ret_rcpt) {
996                         menu_set_active(ifactory, "/Options/Request Return Receipt", TRUE);
997                 }
998         } else {
999                 if (mailto) {
1000                         compose_entry_append(compose, mailto, COMPOSE_NEWSGROUPS);
1001                 } else if (item && FOLDER_CLASS(item->folder) == news_get_class()) {
1002                         compose_entry_append(compose, item->path, COMPOSE_NEWSGROUPS);
1003                 }
1004                 /*
1005                  * CLAWS: just don't allow return receipt request, even if the user
1006                  * may want to send an email. simple but foolproof.
1007                  */
1008                 menu_set_sensitive(ifactory, "/Options/Request Return Receipt", FALSE); 
1009         }
1010         compose_add_field_list( compose, listAddress );
1011
1012         if (prefs_common.compose_with_format) {
1013                 MsgInfo* dummyinfo = NULL;
1014
1015                 if ( prefs_common.compose_subject_format
1016                          && *prefs_common.compose_subject_format != '\0' )
1017                 {
1018                         gchar *subject = NULL;
1019                         gchar *tmp = NULL;
1020                         gchar *buf = NULL;
1021
1022                         dummyinfo = compose_msginfo_new_from_compose(compose);
1023
1024                         /* decode \-escape sequences in the internal representation of the quote format */
1025                         tmp = malloc(strlen(prefs_common.compose_subject_format)+1);
1026                         pref_get_unescaped_pref(tmp, prefs_common.compose_subject_format);
1027
1028                         subject = gtk_editable_get_chars(GTK_EDITABLE(compose->subject_entry), 0, -1);
1029                         quote_fmt_init(dummyinfo, NULL, subject, FALSE, compose->account);
1030                         quote_fmt_scan_string(tmp);
1031                         quote_fmt_parse();
1032
1033                         buf = quote_fmt_get_buffer();
1034                         if (buf == NULL)
1035                                 alertpanel_error(_("New message subject format error."));
1036                         else
1037                                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf);
1038                         quote_fmt_reset_vartable();
1039
1040                         g_free(subject);
1041                         g_free(tmp);
1042                 }
1043
1044                 if ( prefs_common.compose_body_format
1045                          && *prefs_common.compose_body_format != '\0' )
1046                 {
1047                         GtkTextView *text;
1048                         GtkTextBuffer *buffer;
1049                         GtkTextIter start, end;
1050                         gchar *tmp = NULL;
1051
1052                         if ( dummyinfo == NULL )
1053                                 dummyinfo = compose_msginfo_new_from_compose(compose);
1054
1055                         text = GTK_TEXT_VIEW(compose->text);
1056                         buffer = gtk_text_view_get_buffer(text);
1057                         gtk_text_buffer_get_start_iter(buffer, &start);
1058                         gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
1059                         tmp = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1060
1061                         compose_quote_fmt(compose, dummyinfo,
1062                                           prefs_common.compose_body_format,
1063                                           NULL, tmp, FALSE, TRUE,
1064                                                   _("New message body format error."));
1065                         quote_fmt_reset_vartable();
1066
1067                         g_free(tmp);
1068                 }
1069
1070                 procmsg_msginfo_free( dummyinfo );
1071         }
1072
1073         if (attach_files) {
1074                 gint i;
1075                 gchar *file;
1076
1077                 for (i = 0; i < attach_files->len; i++) {
1078                         file = g_ptr_array_index(attach_files, i);
1079                         compose_attach_append(compose, file, file, NULL);
1080                 }
1081         }
1082
1083         compose_show_first_last_header(compose, TRUE);
1084
1085         /* Set save folder */
1086         if (item && item->prefs && item->prefs->save_copy_to_folder) {
1087                 gchar *folderidentifier;
1088
1089                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
1090                 folderidentifier = folder_item_get_identifier(item);
1091                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
1092                 g_free(folderidentifier);
1093         }
1094         
1095         gtk_widget_grab_focus(compose->header_last->entry);
1096
1097         undo_unblock(compose->undostruct);
1098
1099         if (prefs_common.auto_exteditor)
1100                 compose_exec_ext_editor(compose);
1101
1102         compose->modified = FALSE;
1103         compose_set_title(compose);
1104         return compose;
1105 }
1106
1107 static void compose_force_encryption(Compose *compose, PrefsAccount *account,
1108                 gboolean override_pref)
1109 {
1110         gchar *privacy = NULL;
1111
1112         g_return_if_fail(compose != NULL);
1113         g_return_if_fail(account != NULL);
1114
1115         if (override_pref == FALSE && account->default_encrypt_reply == FALSE)
1116                 return;
1117
1118         if (account->default_privacy_system
1119         &&  strlen(account->default_privacy_system)) {
1120                 privacy = account->default_privacy_system;
1121         } else {
1122                 GSList *privacy_avail = privacy_get_system_ids();
1123                 if (privacy_avail && g_slist_length(privacy_avail)) {
1124                         privacy = (gchar *)(privacy_avail->data);
1125                 }
1126         }
1127         if (privacy != NULL) {
1128                 compose->privacy_system = g_strdup(privacy);
1129                 compose_update_privacy_system_menu_item(compose, FALSE);
1130                 compose_use_encryption(compose, TRUE);
1131         }
1132 }       
1133
1134 static void compose_force_signing(Compose *compose, PrefsAccount *account)
1135 {
1136         gchar *privacy = NULL;
1137
1138         if (account->default_privacy_system
1139         &&  strlen(account->default_privacy_system)) {
1140                 privacy = account->default_privacy_system;
1141         } else {
1142                 GSList *privacy_avail = privacy_get_system_ids();
1143                 if (privacy_avail && g_slist_length(privacy_avail)) {
1144                         privacy = (gchar *)(privacy_avail->data);
1145                 }
1146         }
1147         if (privacy != NULL) {
1148                 compose->privacy_system = g_strdup(privacy);
1149                 compose_update_privacy_system_menu_item(compose, FALSE);
1150                 compose_use_signing(compose, TRUE);
1151         }
1152 }       
1153
1154 Compose *compose_reply_mode(ComposeMode mode, GSList *msginfo_list, gchar *body)
1155 {
1156         MsgInfo *msginfo;
1157         guint list_len;
1158         Compose *compose = NULL;
1159         GtkItemFactory *ifactory = NULL;
1160         
1161         g_return_val_if_fail(msginfo_list != NULL, NULL);
1162
1163         msginfo = (MsgInfo*)g_slist_nth_data(msginfo_list, 0);
1164         g_return_val_if_fail(msginfo != NULL, NULL);
1165
1166         list_len = g_slist_length(msginfo_list);
1167
1168         switch (mode) {
1169         case COMPOSE_REPLY:
1170                 compose = compose_reply(msginfo, prefs_common.reply_with_quote,
1171                               FALSE, prefs_common.default_reply_list, FALSE, body);
1172                 break;
1173         case COMPOSE_REPLY_WITH_QUOTE:
1174                 compose = compose_reply(msginfo, TRUE, 
1175                         FALSE, prefs_common.default_reply_list, FALSE, body);
1176                 break;
1177         case COMPOSE_REPLY_WITHOUT_QUOTE:
1178                 compose = compose_reply(msginfo, FALSE, 
1179                         FALSE, prefs_common.default_reply_list, FALSE, NULL);
1180                 break;
1181         case COMPOSE_REPLY_TO_SENDER:
1182                 compose = compose_reply(msginfo, prefs_common.reply_with_quote,
1183                               FALSE, FALSE, TRUE, body);
1184                 break;
1185         case COMPOSE_FOLLOWUP_AND_REPLY_TO:
1186                 compose = compose_followup_and_reply_to(msginfo,
1187                                               prefs_common.reply_with_quote,
1188                                               FALSE, FALSE, body);
1189                 break;
1190         case COMPOSE_REPLY_TO_SENDER_WITH_QUOTE:
1191                 compose = compose_reply(msginfo, TRUE, 
1192                         FALSE, FALSE, TRUE, body);
1193                 break;
1194         case COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE:
1195                 compose = compose_reply(msginfo, FALSE, 
1196                         FALSE, FALSE, TRUE, NULL);
1197                 break;
1198         case COMPOSE_REPLY_TO_ALL:
1199                 compose = compose_reply(msginfo, prefs_common.reply_with_quote,
1200                         TRUE, FALSE, FALSE, body);
1201                 break;
1202         case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
1203                 compose = compose_reply(msginfo, TRUE, 
1204                         TRUE, FALSE, FALSE, body);
1205                 break;
1206         case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
1207                 compose = compose_reply(msginfo, FALSE, 
1208                         TRUE, FALSE, FALSE, NULL);
1209                 break;
1210         case COMPOSE_REPLY_TO_LIST:
1211                 compose = compose_reply(msginfo, prefs_common.reply_with_quote,
1212                         FALSE, TRUE, FALSE, body);
1213                 break;
1214         case COMPOSE_REPLY_TO_LIST_WITH_QUOTE:
1215                 compose = compose_reply(msginfo, TRUE, 
1216                         FALSE, TRUE, FALSE, body);
1217                 break;
1218         case COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE:
1219                 compose = compose_reply(msginfo, FALSE, 
1220                         FALSE, TRUE, FALSE, NULL);
1221                 break;
1222         case COMPOSE_FORWARD:
1223                 if (prefs_common.forward_as_attachment) {
1224                         compose = compose_reply_mode(COMPOSE_FORWARD_AS_ATTACH, msginfo_list, body);
1225                         return compose;
1226                 } else {
1227                         compose = compose_reply_mode(COMPOSE_FORWARD_INLINE, msginfo_list, body);
1228                         return compose;
1229                 }
1230                 break;
1231         case COMPOSE_FORWARD_INLINE:
1232                 /* check if we reply to more than one Message */
1233                 if (list_len == 1) {
1234                         compose = compose_forward(NULL, msginfo, FALSE, body, FALSE, FALSE);
1235                         break;
1236                 } 
1237                 /* more messages FALL THROUGH */
1238         case COMPOSE_FORWARD_AS_ATTACH:
1239                 compose = compose_forward_multiple(NULL, msginfo_list);
1240                 break;
1241         case COMPOSE_REDIRECT:
1242                 compose = compose_redirect(NULL, msginfo, FALSE);
1243                 break;
1244         default:
1245                 g_warning("compose_reply(): invalid Compose Mode: %d\n", mode);
1246         }
1247         
1248         ifactory = gtk_item_factory_from_widget(compose->menubar);
1249
1250         compose->rmode = mode;
1251         switch (compose->rmode) {
1252         case COMPOSE_REPLY:
1253         case COMPOSE_REPLY_WITH_QUOTE:
1254         case COMPOSE_REPLY_WITHOUT_QUOTE:
1255         case COMPOSE_FOLLOWUP_AND_REPLY_TO:
1256                 debug_print("reply mode Normal\n");
1257                 menu_set_active(ifactory, "/Options/Reply mode/Normal", TRUE);
1258                 compose_reply_change_mode(compose, COMPOSE_REPLY, NULL); /* force update */
1259                 break;
1260         case COMPOSE_REPLY_TO_SENDER:
1261         case COMPOSE_REPLY_TO_SENDER_WITH_QUOTE:
1262         case COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE:
1263                 debug_print("reply mode Sender\n");
1264                 menu_set_active(ifactory, "/Options/Reply mode/Sender", TRUE);
1265                 break;
1266         case COMPOSE_REPLY_TO_ALL:
1267         case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
1268         case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
1269                 debug_print("reply mode All\n");
1270                 menu_set_active(ifactory, "/Options/Reply mode/All", TRUE);
1271                 break;
1272         case COMPOSE_REPLY_TO_LIST:
1273         case COMPOSE_REPLY_TO_LIST_WITH_QUOTE:
1274         case COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE:
1275                 debug_print("reply mode List\n");
1276                 menu_set_active(ifactory, "/Options/Reply mode/Mailing-list", TRUE);
1277                 break;
1278         default:
1279                 break;
1280         }
1281         return compose;
1282 }
1283
1284 Compose *compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
1285                    gboolean to_ml, gboolean to_sender, 
1286                    const gchar *body)
1287 {
1288         return compose_generic_reply(msginfo, quote, to_all, to_ml, 
1289                               to_sender, FALSE, body);
1290 }
1291
1292 Compose *compose_followup_and_reply_to(MsgInfo *msginfo, gboolean quote,
1293                                    gboolean to_all,
1294                                    gboolean to_sender,
1295                                    const gchar *body)
1296 {
1297         return compose_generic_reply(msginfo, quote, to_all, FALSE, 
1298                               to_sender, TRUE, body);
1299 }
1300
1301 static void compose_extract_original_charset(Compose *compose)
1302 {
1303         MsgInfo *info = NULL;
1304         if (compose->replyinfo) {
1305                 info = compose->replyinfo;
1306         } else if (compose->fwdinfo) {
1307                 info = compose->fwdinfo;
1308         } else if (compose->targetinfo) {
1309                 info = compose->targetinfo;
1310         }
1311         if (info) {
1312                 MimeInfo *mimeinfo = procmime_scan_message(info);
1313                 MimeInfo *partinfo = mimeinfo;
1314                 while (partinfo && partinfo->type != MIMETYPE_TEXT)
1315                         partinfo = procmime_mimeinfo_next(partinfo);
1316                 if (partinfo) {
1317                         compose->orig_charset = 
1318                                 g_strdup(procmime_mimeinfo_get_parameter(
1319                                                 partinfo, "charset"));
1320                 }
1321                 procmime_mimeinfo_free_all(mimeinfo);
1322         }
1323 }
1324
1325 #define SIGNAL_BLOCK(buffer) {                                  \
1326         g_signal_handlers_block_by_func(G_OBJECT(buffer),       \
1327                                 G_CALLBACK(compose_changed_cb), \
1328                                 compose);                       \
1329         g_signal_handlers_block_by_func(G_OBJECT(buffer),       \
1330                                 G_CALLBACK(text_inserted),      \
1331                                 compose);                       \
1332 }
1333
1334 #define SIGNAL_UNBLOCK(buffer) {                                \
1335         g_signal_handlers_unblock_by_func(G_OBJECT(buffer),     \
1336                                 G_CALLBACK(compose_changed_cb), \
1337                                 compose);                       \
1338         g_signal_handlers_unblock_by_func(G_OBJECT(buffer),     \
1339                                 G_CALLBACK(text_inserted),      \
1340                                 compose);                       \
1341 }
1342
1343 static Compose *compose_generic_reply(MsgInfo *msginfo, gboolean quote,
1344                                   gboolean to_all, gboolean to_ml,
1345                                   gboolean to_sender,
1346                                   gboolean followup_and_reply_to,
1347                                   const gchar *body)
1348 {
1349         GtkItemFactory *ifactory;
1350         Compose *compose;
1351         PrefsAccount *account = NULL;
1352         PrefsAccount *reply_account;
1353         GtkTextView *textview;
1354         GtkTextBuffer *textbuf;
1355
1356         g_return_val_if_fail(msginfo != NULL, NULL);
1357         g_return_val_if_fail(msginfo->folder != NULL, NULL);
1358
1359         account = account_get_reply_account(msginfo, prefs_common.reply_account_autosel);
1360         
1361         g_return_val_if_fail(account != NULL, NULL);
1362
1363         if (to_sender && account->protocol == A_NNTP &&
1364             !followup_and_reply_to) {
1365                 reply_account =
1366                         account_find_from_address(account->address);
1367                 if (!reply_account)
1368                         reply_account = compose_current_mail_account();
1369                 if (!reply_account)
1370                         return NULL;
1371         } else
1372                 reply_account = account;
1373
1374         compose = compose_create(account, COMPOSE_REPLY, FALSE);
1375
1376         compose->updating = TRUE;
1377
1378         ifactory = gtk_item_factory_from_widget(compose->menubar);
1379
1380         menu_set_active(ifactory, "/Options/Remove references", FALSE);
1381         menu_set_sensitive(ifactory, "/Options/Remove references", TRUE);
1382
1383         compose->replyinfo = procmsg_msginfo_get_full_info(msginfo);
1384         if (!compose->replyinfo)
1385                 compose->replyinfo = procmsg_msginfo_copy(msginfo);
1386
1387         compose_extract_original_charset(compose);
1388         
1389         if (msginfo->folder && msginfo->folder->ret_rcpt)
1390                 menu_set_active(ifactory, "/Options/Request Return Receipt", TRUE);
1391
1392         /* Set save folder */
1393         if (msginfo->folder && msginfo->folder->prefs && msginfo->folder->prefs->save_copy_to_folder) {
1394                 gchar *folderidentifier;
1395
1396                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1397                 folderidentifier = folder_item_get_identifier(msginfo->folder);
1398                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
1399                 g_free(folderidentifier);
1400         }
1401
1402         if (compose_parse_header(compose, msginfo) < 0) return NULL;
1403
1404         textview = (GTK_TEXT_VIEW(compose->text));
1405         textbuf = gtk_text_view_get_buffer(textview);
1406         compose_create_tags(textview, compose);
1407
1408         undo_block(compose->undostruct);
1409 #ifdef USE_ASPELL
1410         if (msginfo->folder && msginfo->folder->prefs && 
1411             msginfo->folder->prefs && 
1412             msginfo->folder->prefs->enable_default_dictionary &&
1413             compose->gtkaspell)
1414                 gtkaspell_change_dict(compose->gtkaspell, 
1415                     msginfo->folder->prefs->default_dictionary);
1416 #endif
1417
1418         if (quote) {
1419                 gchar *qmark;
1420
1421                 if (prefs_common.quotemark && *prefs_common.quotemark)
1422                         qmark = prefs_common.quotemark;
1423                 else
1424                         qmark = "> ";
1425
1426                 compose_quote_fmt(compose, compose->replyinfo,
1427                                   prefs_common.quotefmt,
1428                                   qmark, body, FALSE, TRUE,
1429                                           _("Message reply format error."));
1430                 quote_fmt_reset_vartable();
1431         }
1432         if (procmime_msginfo_is_encrypted(compose->replyinfo)) {
1433                 compose_force_encryption(compose, account, FALSE);
1434         }
1435
1436         SIGNAL_BLOCK(textbuf);
1437         
1438         if (account->auto_sig)
1439                 compose_insert_sig(compose, FALSE);
1440
1441         compose_wrap_all(compose);
1442
1443         SIGNAL_UNBLOCK(textbuf);
1444         
1445         gtk_widget_grab_focus(compose->text);
1446
1447         undo_unblock(compose->undostruct);
1448
1449         if (prefs_common.auto_exteditor)
1450                 compose_exec_ext_editor(compose);
1451                 
1452         compose->modified = FALSE;
1453         compose_set_title(compose);
1454
1455         compose->updating = FALSE;
1456
1457         if (compose->deferred_destroy) {
1458                 compose_destroy(compose);
1459                 return NULL;
1460         }
1461
1462         return compose;
1463 }
1464
1465 #define INSERT_FW_HEADER(var, hdr) \
1466 if (msginfo->var && *msginfo->var) { \
1467         gtk_stext_insert(text, NULL, NULL, NULL, hdr, -1); \
1468         gtk_stext_insert(text, NULL, NULL, NULL, msginfo->var, -1); \
1469         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1); \
1470 }
1471
1472 Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
1473                          gboolean as_attach, const gchar *body,
1474                          gboolean no_extedit,
1475                          gboolean batch)
1476 {
1477         Compose *compose;
1478         GtkTextView *textview;
1479         GtkTextBuffer *textbuf;
1480         GtkTextIter iter;
1481
1482         g_return_val_if_fail(msginfo != NULL, NULL);
1483         g_return_val_if_fail(msginfo->folder != NULL, NULL);
1484
1485         if (!account && 
1486             !(account = compose_guess_forward_account_from_msginfo
1487                                 (msginfo)))
1488                 account = cur_account;
1489
1490         compose = compose_create(account, COMPOSE_FORWARD, batch);
1491
1492         compose->updating = TRUE;
1493         compose->fwdinfo = procmsg_msginfo_get_full_info(msginfo);
1494         if (!compose->fwdinfo)
1495                 compose->fwdinfo = procmsg_msginfo_copy(msginfo);
1496
1497         compose_extract_original_charset(compose);
1498
1499         if (msginfo->subject && *msginfo->subject) {
1500                 gchar *buf, *buf2, *p;
1501
1502                 buf = p = g_strdup(msginfo->subject);
1503                 p += subject_get_prefix_length(p);
1504                 memmove(buf, p, strlen(p) + 1);
1505
1506                 buf2 = g_strdup_printf("Fw: %s", buf);
1507                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1508                 
1509                 g_free(buf);
1510                 g_free(buf2);
1511         }
1512
1513         textview = GTK_TEXT_VIEW(compose->text);
1514         textbuf = gtk_text_view_get_buffer(textview);
1515         compose_create_tags(textview, compose);
1516         
1517         undo_block(compose->undostruct);
1518         if (as_attach) {
1519                 gchar *msgfile;
1520
1521                 msgfile = procmsg_get_message_file_path(msginfo);
1522                 if (!is_file_exist(msgfile))
1523                         g_warning("%s: file not exist\n", msgfile);
1524                 else
1525                         compose_attach_append(compose, msgfile, msgfile,
1526                                               "message/rfc822");
1527
1528                 g_free(msgfile);
1529         } else {
1530                 gchar *qmark;
1531                 MsgInfo *full_msginfo;
1532
1533                 full_msginfo = procmsg_msginfo_get_full_info(msginfo);
1534                 if (!full_msginfo)
1535                         full_msginfo = procmsg_msginfo_copy(msginfo);
1536
1537                 if (prefs_common.fw_quotemark &&
1538                     *prefs_common.fw_quotemark)
1539                         qmark = prefs_common.fw_quotemark;
1540                 else
1541                         qmark = "> ";
1542
1543                 compose_quote_fmt(compose, full_msginfo,
1544                                   prefs_common.fw_quotefmt,
1545                                   qmark, body, FALSE, TRUE,
1546                                           _("Message forward format error."));
1547                 quote_fmt_reset_vartable();
1548                 compose_attach_parts(compose, msginfo);
1549
1550                 procmsg_msginfo_free(full_msginfo);
1551         }
1552
1553         SIGNAL_BLOCK(textbuf);
1554
1555         if (account->auto_sig)
1556                 compose_insert_sig(compose, FALSE);
1557
1558         compose_wrap_all(compose);
1559
1560         SIGNAL_UNBLOCK(textbuf);
1561         
1562         gtk_text_buffer_get_start_iter(textbuf, &iter);
1563         gtk_text_buffer_place_cursor(textbuf, &iter);
1564
1565         gtk_widget_grab_focus(compose->header_last->entry);
1566
1567         if (!no_extedit && prefs_common.auto_exteditor)
1568                 compose_exec_ext_editor(compose);
1569         
1570         /*save folder*/
1571         if (msginfo->folder && msginfo->folder->prefs && msginfo->folder->prefs->save_copy_to_folder) {
1572                 gchar *folderidentifier;
1573
1574                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1575                 folderidentifier = folder_item_get_identifier(msginfo->folder);
1576                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
1577                 g_free(folderidentifier);
1578         }
1579
1580         undo_unblock(compose->undostruct);
1581         
1582         compose->modified = FALSE;
1583         compose_set_title(compose);
1584
1585         compose->updating = FALSE;
1586
1587         if (compose->deferred_destroy) {
1588                 compose_destroy(compose);
1589                 return NULL;
1590         }
1591
1592         return compose;
1593 }
1594
1595 #undef INSERT_FW_HEADER
1596
1597 Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_list)
1598 {
1599         Compose *compose;
1600         GtkTextView *textview;
1601         GtkTextBuffer *textbuf;
1602         GtkTextIter iter;
1603         GSList *msginfo;
1604         gchar *msgfile;
1605         gboolean single_mail = TRUE;
1606         
1607         g_return_val_if_fail(msginfo_list != NULL, NULL);
1608
1609         if (g_slist_length(msginfo_list) > 1)
1610                 single_mail = FALSE;
1611
1612         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next)
1613                 if (((MsgInfo *)msginfo->data)->folder == NULL)
1614                         return NULL;
1615
1616         /* guess account from first selected message */
1617         if (!account && 
1618             !(account = compose_guess_forward_account_from_msginfo
1619                                 (msginfo_list->data)))
1620                 account = cur_account;
1621
1622         g_return_val_if_fail(account != NULL, NULL);
1623
1624         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1625                 MSG_UNSET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_REPLIED);
1626                 MSG_SET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_FORWARDED);
1627         }
1628
1629         compose = compose_create(account, COMPOSE_FORWARD, FALSE);
1630
1631         compose->updating = TRUE;
1632
1633         textview = GTK_TEXT_VIEW(compose->text);
1634         textbuf = gtk_text_view_get_buffer(textview);
1635         compose_create_tags(textview, compose);
1636         
1637         undo_block(compose->undostruct);
1638         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1639                 msgfile = procmsg_get_message_file_path((MsgInfo *)msginfo->data);
1640
1641                 if (!is_file_exist(msgfile))
1642                         g_warning("%s: file not exist\n", msgfile);
1643                 else
1644                         compose_attach_append(compose, msgfile, msgfile,
1645                                 "message/rfc822");
1646                 g_free(msgfile);
1647         }
1648         
1649         if (single_mail) {
1650                 MsgInfo *info = (MsgInfo *)msginfo_list->data;
1651                 if (info->subject && *info->subject) {
1652                         gchar *buf, *buf2, *p;
1653
1654                         buf = p = g_strdup(info->subject);
1655                         p += subject_get_prefix_length(p);
1656                         memmove(buf, p, strlen(p) + 1);
1657
1658                         buf2 = g_strdup_printf("Fw: %s", buf);
1659                         gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1660
1661                         g_free(buf);
1662                         g_free(buf2);
1663                 }
1664         } else {
1665                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry),
1666                         _("Fw: multiple emails"));
1667         }
1668
1669         SIGNAL_BLOCK(textbuf);
1670         
1671         if (account->auto_sig)
1672                 compose_insert_sig(compose, FALSE);
1673
1674         compose_wrap_all(compose);
1675
1676         SIGNAL_UNBLOCK(textbuf);
1677         
1678         gtk_text_buffer_get_start_iter(textbuf, &iter);
1679         gtk_text_buffer_place_cursor(textbuf, &iter);
1680
1681         gtk_widget_grab_focus(compose->header_last->entry);
1682         undo_unblock(compose->undostruct);
1683         compose->modified = FALSE;
1684         compose_set_title(compose);
1685
1686         compose->updating = FALSE;
1687
1688         if (compose->deferred_destroy) {
1689                 compose_destroy(compose);
1690                 return NULL;
1691         }
1692
1693         return compose;
1694 }
1695
1696 static gboolean compose_is_sig_separator(Compose *compose, GtkTextBuffer *textbuf, GtkTextIter *iter) 
1697 {
1698         GtkTextIter start = *iter;
1699         GtkTextIter end_iter;
1700         int start_pos = gtk_text_iter_get_offset(&start);
1701         gchar *str = NULL;
1702         if (!compose->account->sig_sep)
1703                 return FALSE;
1704         
1705         gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
1706                 start_pos+strlen(compose->account->sig_sep));
1707
1708         /* check sig separator */
1709         str = gtk_text_iter_get_text(&start, &end_iter);
1710         if (!strcmp(str, compose->account->sig_sep)) {
1711                 gchar *tmp = NULL;
1712                 /* check end of line (\n) */
1713                 gtk_text_buffer_get_iter_at_offset(textbuf, &start,
1714                         start_pos+strlen(compose->account->sig_sep));
1715                 gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
1716                         start_pos+strlen(compose->account->sig_sep)+1);
1717                 tmp = gtk_text_iter_get_text(&start, &end_iter);
1718                 if (!strcmp(tmp,"\n")) {
1719                         g_free(str);
1720                         g_free(tmp);
1721                         return TRUE;
1722                 }
1723                 g_free(tmp);    
1724         }
1725         g_free(str);
1726
1727         return FALSE;
1728 }
1729
1730 static void compose_colorize_signature(Compose *compose)
1731 {
1732         GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(compose->text));
1733         GtkTextIter iter;
1734         GtkTextIter end_iter;
1735         gtk_text_buffer_get_start_iter(buffer, &iter);
1736         while (gtk_text_iter_forward_line(&iter))
1737                 if (compose_is_sig_separator(compose, buffer, &iter)) {
1738                         gtk_text_buffer_get_end_iter(buffer, &end_iter);
1739                         gtk_text_buffer_apply_tag_by_name(buffer,"signature",&iter, &end_iter);
1740                 }
1741 }
1742
1743 Compose *compose_reedit(MsgInfo *msginfo, gboolean batch)
1744 {
1745         Compose *compose = NULL;
1746         PrefsAccount *account = NULL;
1747         GtkTextView *textview;
1748         GtkTextBuffer *textbuf;
1749         GtkTextMark *mark;
1750         GtkTextIter iter;
1751         FILE *fp;
1752         gchar buf[BUFFSIZE];
1753         gboolean use_signing = FALSE;
1754         gboolean use_encryption = FALSE;
1755         gchar *privacy_system = NULL;
1756         int priority = PRIORITY_NORMAL;
1757         MsgInfo *replyinfo = NULL, *fwdinfo = NULL;
1758
1759         g_return_val_if_fail(msginfo != NULL, NULL);
1760         g_return_val_if_fail(msginfo->folder != NULL, NULL);
1761
1762         if (compose_put_existing_to_front(msginfo)) {
1763                 return NULL;
1764         }
1765
1766         if (folder_has_parent_of_type(msginfo->folder, F_QUEUE) ||
1767             folder_has_parent_of_type(msginfo->folder, F_DRAFT)) {
1768                 gchar queueheader_buf[BUFFSIZE];
1769                 gint id, param;
1770
1771                 /* Select Account from queue headers */
1772                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1773                                              sizeof(queueheader_buf), "X-Sylpheed-Account-Id:")) {
1774                         id = atoi(&queueheader_buf[strlen("X-Sylpheed-Account-Id:")]);
1775                         account = account_find_from_id(id);
1776                 }
1777                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1778                                              sizeof(queueheader_buf), "NAID:")) {
1779                         id = atoi(&queueheader_buf[strlen("NAID:")]);
1780                         account = account_find_from_id(id);
1781                 }
1782                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1783                                                     sizeof(queueheader_buf), "MAID:")) {
1784                         id = atoi(&queueheader_buf[strlen("MAID:")]);
1785                         account = account_find_from_id(id);
1786                 }
1787                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1788                                                                 sizeof(queueheader_buf), "S:")) {
1789                         account = account_find_from_address(queueheader_buf);
1790                 }
1791                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1792                                              sizeof(queueheader_buf), "X-Sylpheed-Sign:")) {
1793                         param = atoi(&queueheader_buf[strlen("X-Sylpheed-Sign:")]);
1794                         use_signing = param;
1795                         
1796                 }
1797                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1798                                              sizeof(queueheader_buf), "X-Sylpheed-Encrypt:")) {
1799                         param = atoi(&queueheader_buf[strlen("X-Sylpheed-Encrypt:")]);
1800                         use_encryption = param;
1801                 }
1802                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1803                                             sizeof(queueheader_buf), "X-Sylpheed-Privacy-System:")) {
1804                         privacy_system = g_strdup(&queueheader_buf[strlen("X-Sylpheed-Privacy-System:")]);
1805                 }
1806                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1807                                              sizeof(queueheader_buf), "X-Priority: ")) {
1808                         param = atoi(&queueheader_buf[strlen("X-Priority: ")]); /* mind the space */
1809                         priority = param;
1810                 }
1811                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1812                                              sizeof(queueheader_buf), "RMID:")) {
1813                         gchar **tokens = g_strsplit(&queueheader_buf[strlen("RMID:")], "\t", 0);
1814                         if (tokens[0] && tokens[1] && tokens[2]) {
1815                                 FolderItem *orig_item = folder_find_item_from_identifier(tokens[0]);
1816                                 if (orig_item != NULL) {
1817                                         replyinfo = folder_item_get_msginfo_by_msgid(orig_item, tokens[2]);
1818                                 }
1819                         }
1820                         g_strfreev(tokens);
1821                 }
1822                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1823                                              sizeof(queueheader_buf), "FMID:")) {
1824                         gchar **tokens = g_strsplit(&queueheader_buf[strlen("FMID:")], "\t", 0);
1825                         if (tokens[0] && tokens[1] && tokens[2]) {
1826                                 FolderItem *orig_item = folder_find_item_from_identifier(tokens[0]);
1827                                 if (orig_item != NULL) {
1828                                         fwdinfo = folder_item_get_msginfo_by_msgid(orig_item, tokens[2]);
1829                                 }
1830                         }
1831                         g_strfreev(tokens);
1832                 }
1833         } else {
1834                 account = msginfo->folder->folder->account;
1835         }
1836
1837         if (!account && prefs_common.reedit_account_autosel) {
1838                 gchar from[BUFFSIZE];
1839                 if (!procheader_get_header_from_msginfo(msginfo, from, sizeof(from), "FROM:")) {
1840                         extract_address(from);
1841                         account = account_find_from_address(from);
1842                 }
1843         }
1844         if (!account) {
1845                 account = cur_account;
1846         }
1847         g_return_val_if_fail(account != NULL, NULL);
1848
1849         compose = compose_create(account, COMPOSE_REEDIT, batch);
1850         
1851         compose->replyinfo = replyinfo;
1852         compose->fwdinfo = fwdinfo;
1853
1854         compose->updating = TRUE;
1855         compose->priority = priority;
1856
1857         if (privacy_system != NULL) {
1858                 compose->privacy_system = privacy_system;
1859                 compose_use_signing(compose, use_signing);
1860                 compose_use_encryption(compose, use_encryption);
1861                 compose_update_privacy_system_menu_item(compose, FALSE);
1862         } else {
1863                 activate_privacy_system(compose, account, FALSE);
1864         }
1865
1866         compose->targetinfo = procmsg_msginfo_copy(msginfo);
1867
1868         compose_extract_original_charset(compose);
1869
1870         if (folder_has_parent_of_type(msginfo->folder, F_QUEUE) ||
1871             folder_has_parent_of_type(msginfo->folder, F_DRAFT)) {
1872                 gchar queueheader_buf[BUFFSIZE];
1873
1874                 /* Set message save folder */
1875                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, sizeof(queueheader_buf), "SCF:")) {
1876                         gint startpos = 0;
1877
1878                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1879                         gtk_editable_delete_text(GTK_EDITABLE(compose->savemsg_entry), 0, -1);
1880                         gtk_editable_insert_text(GTK_EDITABLE(compose->savemsg_entry), &queueheader_buf[4], strlen(&queueheader_buf[4]), &startpos);
1881                 }
1882                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, sizeof(queueheader_buf), "RRCPT:")) {
1883                         gint active = atoi(&queueheader_buf[strlen("RRCPT:")]);
1884                         if (active) {
1885                                 GtkItemFactory *ifactory;
1886                                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1887                                 menu_set_active(ifactory, "/Options/Request Return Receipt", TRUE);
1888                         }
1889                 }
1890         }
1891         
1892         if (compose_parse_header(compose, msginfo) < 0) {
1893                 compose->updating = FALSE;
1894                 compose_destroy(compose);
1895                 return NULL;
1896         }
1897         compose_reedit_set_entry(compose, msginfo);
1898
1899         textview = GTK_TEXT_VIEW(compose->text);
1900         textbuf = gtk_text_view_get_buffer(textview);
1901         compose_create_tags(textview, compose);
1902
1903         mark = gtk_text_buffer_get_insert(textbuf);
1904         gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1905
1906         g_signal_handlers_block_by_func(G_OBJECT(textbuf),
1907                                         G_CALLBACK(compose_changed_cb),
1908                                         compose);
1909         
1910         if (procmime_msginfo_is_encrypted(msginfo)) {
1911                 fp = procmime_get_first_encrypted_text_content(msginfo);
1912                 if (fp) {
1913                         compose_force_encryption(compose, account, TRUE);
1914                 }
1915         } else {
1916                 fp = procmime_get_first_text_content(msginfo);
1917         }
1918         if (fp == NULL) {
1919                 g_warning("Can't get text part\n");
1920         }
1921
1922         if (fp != NULL) {
1923                 gboolean prev_autowrap = compose->autowrap;
1924
1925                 compose->autowrap = FALSE;
1926                 while (fgets(buf, sizeof(buf), fp) != NULL) {
1927                         strcrchomp(buf);
1928                         gtk_text_buffer_insert(textbuf, &iter, buf, -1);
1929                 }
1930                 compose_wrap_all_full(compose, FALSE);
1931                 compose->autowrap = prev_autowrap;
1932                 fclose(fp);
1933         }
1934         
1935         compose_attach_parts(compose, msginfo);
1936
1937         compose_colorize_signature(compose);
1938
1939         g_signal_handlers_unblock_by_func(G_OBJECT(textbuf),
1940                                         G_CALLBACK(compose_changed_cb),
1941                                         compose);
1942
1943         gtk_widget_grab_focus(compose->text);
1944
1945         if (prefs_common.auto_exteditor) {
1946                 compose_exec_ext_editor(compose);
1947         }
1948         compose->modified = FALSE;
1949         compose_set_title(compose);
1950
1951         compose->updating = FALSE;
1952
1953         if (compose->deferred_destroy) {
1954                 compose_destroy(compose);
1955                 return NULL;
1956         }
1957         
1958         compose->sig_str = compose_get_signature_str(compose);
1959         
1960         return compose;
1961 }
1962
1963 Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo,
1964                                                  gboolean batch)
1965 {
1966         Compose *compose;
1967         gchar *filename;
1968         GtkItemFactory *ifactory;
1969         FolderItem *item;
1970
1971         g_return_val_if_fail(msginfo != NULL, NULL);
1972
1973         if (!account)
1974                 account = account_get_reply_account(msginfo,
1975                                         prefs_common.reply_account_autosel);
1976         g_return_val_if_fail(account != NULL, NULL);
1977
1978         compose = compose_create(account, COMPOSE_REDIRECT, batch);
1979
1980         compose->updating = TRUE;
1981
1982         ifactory = gtk_item_factory_from_widget(compose->menubar);
1983         compose_create_tags(GTK_TEXT_VIEW(compose->text), compose);
1984         compose->replyinfo = NULL;
1985         compose->fwdinfo = NULL;
1986
1987         compose_show_first_last_header(compose, TRUE);
1988
1989         gtk_widget_grab_focus(compose->header_last->entry);
1990
1991         filename = procmsg_get_message_file_path(msginfo);
1992
1993         if (filename == NULL) {
1994                 compose->updating = FALSE;
1995                 compose_destroy(compose);
1996
1997                 return NULL;
1998         }
1999
2000         compose->redirect_filename = filename;
2001         
2002         /* Set save folder */
2003         item = msginfo->folder;
2004         if (item && item->prefs && item->prefs->save_copy_to_folder) {
2005                 gchar *folderidentifier;
2006
2007                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
2008                 folderidentifier = folder_item_get_identifier(item);
2009                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
2010                 g_free(folderidentifier);
2011         }
2012
2013         compose_attach_parts(compose, msginfo);
2014
2015         if (msginfo->subject)
2016                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry),
2017                                    msginfo->subject);
2018         gtk_editable_set_editable(GTK_EDITABLE(compose->subject_entry), FALSE);
2019
2020         compose_quote_fmt(compose, msginfo, "%M", NULL, NULL, FALSE, FALSE,
2021                                           _("Message redirect format error."));
2022         quote_fmt_reset_vartable();
2023         gtk_text_view_set_editable(GTK_TEXT_VIEW(compose->text), FALSE);
2024
2025         compose_colorize_signature(compose);
2026
2027         ifactory = gtk_item_factory_from_widget(compose->popupmenu);
2028         menu_set_sensitive(ifactory, "/Add...", FALSE);
2029         menu_set_sensitive(ifactory, "/Remove", FALSE);
2030         menu_set_sensitive(ifactory, "/Properties...", FALSE);
2031
2032         ifactory = gtk_item_factory_from_widget(compose->menubar);
2033         menu_set_sensitive(ifactory, "/Message/Save", FALSE);
2034         menu_set_sensitive(ifactory, "/Message/Insert file", FALSE);
2035         menu_set_sensitive(ifactory, "/Message/Attach file", FALSE);
2036         menu_set_sensitive(ifactory, "/Message/Insert signature", FALSE);
2037         menu_set_sensitive(ifactory, "/Edit", FALSE);
2038         menu_set_sensitive(ifactory, "/Options", FALSE);
2039         menu_set_sensitive(ifactory, "/Tools/Show ruler", FALSE);
2040         menu_set_sensitive(ifactory, "/Tools/Actions", FALSE);
2041         
2042         if (compose->toolbar->draft_btn)
2043                 gtk_widget_set_sensitive(compose->toolbar->draft_btn, FALSE);
2044         if (compose->toolbar->insert_btn)
2045                 gtk_widget_set_sensitive(compose->toolbar->insert_btn, FALSE);
2046         if (compose->toolbar->attach_btn)
2047                 gtk_widget_set_sensitive(compose->toolbar->attach_btn, FALSE);
2048         if (compose->toolbar->sig_btn)
2049                 gtk_widget_set_sensitive(compose->toolbar->sig_btn, FALSE);
2050         if (compose->toolbar->exteditor_btn)
2051                 gtk_widget_set_sensitive(compose->toolbar->exteditor_btn, FALSE);
2052         if (compose->toolbar->linewrap_current_btn)
2053                 gtk_widget_set_sensitive(compose->toolbar->linewrap_current_btn, FALSE);
2054         if (compose->toolbar->linewrap_all_btn)
2055                 gtk_widget_set_sensitive(compose->toolbar->linewrap_all_btn, FALSE);
2056
2057         compose->modified = FALSE;
2058         compose_set_title(compose);
2059         compose->updating = FALSE;
2060
2061         if (compose->deferred_destroy) {
2062                 compose_destroy(compose);
2063                 return NULL;
2064         }
2065         
2066         return compose;
2067 }
2068
2069 GList *compose_get_compose_list(void)
2070 {
2071         return compose_list;
2072 }
2073
2074 void compose_entry_append(Compose *compose, const gchar *address,
2075                           ComposeEntryType type)
2076 {
2077         gchar *header;
2078         gchar *cur, *begin;
2079         gboolean in_quote = FALSE;
2080         if (!address || *address == '\0') return;
2081
2082         switch (type) {
2083         case COMPOSE_CC:
2084                 header = N_("Cc:");
2085                 break;
2086         case COMPOSE_BCC:
2087                 header = N_("Bcc:");
2088                 break;
2089         case COMPOSE_REPLYTO:
2090                 header = N_("Reply-To:");
2091                 break;
2092         case COMPOSE_NEWSGROUPS:
2093                 header = N_("Newsgroups:");
2094                 break;
2095         case COMPOSE_FOLLOWUPTO:
2096                 header = N_( "Followup-To:");
2097                 break;
2098         case COMPOSE_TO:
2099         default:
2100                 header = N_("To:");
2101                 break;
2102         }
2103         header = prefs_common.trans_hdr ? gettext(header) : header;
2104         
2105         cur = begin = (gchar *)address;
2106         
2107         /* we separate the line by commas, but not if we're inside a quoted
2108          * string */
2109         while (*cur != '\0') {
2110                 if (*cur == '"') 
2111                         in_quote = !in_quote;
2112                 if (*cur == ',' && !in_quote) {
2113                         gchar *tmp = g_strdup(begin);
2114                         gchar *o_tmp = tmp;
2115                         tmp[cur-begin]='\0';
2116                         cur++;
2117                         begin = cur;
2118                         while (*tmp == ' ' || *tmp == '\t')
2119                                 tmp++;
2120                         compose_add_header_entry(compose, header, tmp);
2121                         g_free(o_tmp);
2122                         continue;
2123                 }
2124                 cur++;
2125         }
2126         if (begin < cur) {
2127                 gchar *tmp = g_strdup(begin);
2128                 gchar *o_tmp = tmp;
2129                 tmp[cur-begin]='\0';
2130                 cur++;
2131                 begin = cur;
2132                 while (*tmp == ' ' || *tmp == '\t')
2133                         tmp++;
2134                 compose_add_header_entry(compose, header, tmp);
2135                 g_free(o_tmp);          
2136         }
2137 }
2138
2139 void compose_entry_mark_default_to(Compose *compose, const gchar *mailto)
2140 {
2141         static GdkColor yellow;
2142         static GdkColor black;
2143         static gboolean yellow_initialised = FALSE;
2144         GSList *h_list;
2145         GtkEntry *entry;
2146                 
2147         if (!yellow_initialised) {
2148                 gdk_color_parse("#f5f6be", &yellow);
2149                 gdk_color_parse("#000000", &black);
2150                 yellow_initialised = gdk_colormap_alloc_color(
2151                         gdk_colormap_get_system(), &yellow, FALSE, TRUE);
2152                 yellow_initialised &= gdk_colormap_alloc_color(
2153                         gdk_colormap_get_system(), &black, FALSE, TRUE);
2154         }
2155
2156         for (h_list = compose->header_list; h_list != NULL; h_list = h_list->next) {
2157                 entry = GTK_ENTRY(((ComposeHeaderEntry *)h_list->data)->entry);
2158                 if (gtk_entry_get_text(entry) && 
2159                     !g_utf8_collate(gtk_entry_get_text(entry), mailto)) {
2160                         if (yellow_initialised) {
2161                                 gtk_widget_modify_base(
2162                                         GTK_WIDGET(((ComposeHeaderEntry *)h_list->data)->entry),
2163                                         GTK_STATE_NORMAL, &yellow);
2164                                 gtk_widget_modify_text(
2165                                         GTK_WIDGET(((ComposeHeaderEntry *)h_list->data)->entry),
2166                                         GTK_STATE_NORMAL, &black);
2167                         }
2168                 }
2169         }
2170 }
2171
2172 void compose_toolbar_cb(gint action, gpointer data)
2173 {
2174         ToolbarItem *toolbar_item = (ToolbarItem*)data;
2175         Compose *compose = (Compose*)toolbar_item->parent;
2176         
2177         g_return_if_fail(compose != NULL);
2178
2179         switch(action) {
2180         case A_SEND:
2181                 compose_send_cb(compose, 0, NULL);
2182                 break;
2183         case A_SENDL:
2184                 compose_send_later_cb(compose, 0, NULL);
2185                 break;
2186         case A_DRAFT:
2187                 compose_draft_cb(compose, COMPOSE_QUIT_EDITING, NULL);
2188                 break;
2189         case A_INSERT:
2190                 compose_insert_file_cb(compose, 0, NULL);
2191                 break;
2192         case A_ATTACH:
2193                 compose_attach_cb(compose, 0, NULL);
2194                 break;
2195         case A_SIG:
2196                 compose_insert_sig(compose, FALSE);
2197                 break;
2198         case A_EXTEDITOR:
2199                 compose_ext_editor_cb(compose, 0, NULL);
2200                 break;
2201         case A_LINEWRAP_CURRENT:
2202                 compose_beautify_paragraph(compose, NULL, TRUE);
2203                 break;
2204         case A_LINEWRAP_ALL:
2205                 compose_wrap_all_full(compose, TRUE);
2206                 break;
2207         case A_ADDRBOOK:
2208                 compose_address_cb(compose, 0, NULL);
2209                 break;
2210 #ifdef USE_ASPELL
2211         case A_CHECK_SPELLING:
2212                 compose_check_all(compose);
2213                 break;
2214 #endif
2215         default:
2216                 break;
2217         }
2218 }
2219
2220 static void compose_entries_set(Compose *compose, const gchar *mailto)
2221 {
2222         gchar *to = NULL;
2223         gchar *cc = NULL;
2224         gchar *subject = NULL;
2225         gchar *body = NULL;
2226         gchar *temp = NULL;
2227         guint  len = 0;
2228         gchar *attach = NULL;
2229         
2230         scan_mailto_url(mailto, &to, &cc, NULL, &subject, &body, &attach);
2231
2232         if (to)
2233                 compose_entry_append(compose, to, COMPOSE_TO);
2234         if (cc)
2235                 compose_entry_append(compose, cc, COMPOSE_CC);
2236         if (subject) {
2237                 if (!g_utf8_validate (subject, -1, NULL)) {
2238                         temp = g_locale_to_utf8 (subject, -1, NULL, &len, NULL);
2239                         gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), temp);
2240                         g_free(temp);
2241                 } else {
2242                         gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), subject);
2243                 }
2244         }
2245         if (body) {
2246                 GtkTextView *text = GTK_TEXT_VIEW(compose->text);
2247                 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
2248                 GtkTextMark *mark;
2249                 GtkTextIter iter;
2250                 gboolean prev_autowrap = compose->autowrap;
2251
2252                 compose->autowrap = FALSE;
2253
2254                 mark = gtk_text_buffer_get_insert(buffer);
2255                 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
2256
2257                 if (!g_utf8_validate (body, -1, NULL)) {
2258                         temp = g_locale_to_utf8 (body, -1, NULL, &len, NULL);
2259                         gtk_text_buffer_insert(buffer, &iter, temp, -1);
2260                         g_free(temp);
2261                 } else {
2262                         gtk_text_buffer_insert(buffer, &iter, body, -1);
2263                 }
2264                 gtk_text_buffer_insert(buffer, &iter, "\n", 1);
2265
2266                 compose->autowrap = prev_autowrap;
2267                 if (compose->autowrap)
2268                         compose_wrap_all(compose);
2269         }
2270
2271         if (attach) {
2272                 gchar *utf8_filename = conv_filename_to_utf8(attach);
2273                 if (utf8_filename) {
2274                         if (compose_attach_append(compose, attach, utf8_filename, NULL)) {
2275                                 alertpanel_notice(_("The file '%s' has been attached."), attach);
2276                         } 
2277                         g_free(utf8_filename);
2278                 } else {
2279                         alertpanel_error(_("Couldn't attach a file (charset conversion failed)."));
2280                 }
2281         }
2282         g_free(to);
2283         g_free(cc);
2284         g_free(subject);
2285         g_free(body);
2286         g_free(attach);
2287 }
2288
2289 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
2290 {
2291         static HeaderEntry hentry[] = {{"Reply-To:",    NULL, TRUE},
2292                                        {"Cc:",          NULL, TRUE},
2293                                        {"References:",  NULL, FALSE},
2294                                        {"Bcc:",         NULL, TRUE},
2295                                        {"Newsgroups:",  NULL, TRUE},
2296                                        {"Followup-To:", NULL, TRUE},
2297                                        {"List-Post:",   NULL, FALSE},
2298                                        {"X-Priority:",  NULL, FALSE},
2299                                        {NULL,           NULL, FALSE}};
2300
2301         enum
2302         {
2303                 H_REPLY_TO      = 0,
2304                 H_CC            = 1,
2305                 H_REFERENCES    = 2,
2306                 H_BCC           = 3,
2307                 H_NEWSGROUPS    = 4,
2308                 H_FOLLOWUP_TO   = 5,
2309                 H_LIST_POST     = 6,
2310                 H_X_PRIORITY    = 7
2311         };
2312
2313         FILE *fp;
2314
2315         g_return_val_if_fail(msginfo != NULL, -1);
2316
2317         if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
2318         procheader_get_header_fields(fp, hentry);
2319         fclose(fp);
2320
2321         if (hentry[H_REPLY_TO].body != NULL) {
2322                 if (hentry[H_REPLY_TO].body[0] != '\0') {
2323                         compose->replyto =
2324                                 conv_unmime_header(hentry[H_REPLY_TO].body,
2325                                                    NULL);
2326                 }
2327                 g_free(hentry[H_REPLY_TO].body);
2328                 hentry[H_REPLY_TO].body = NULL;
2329         }
2330         if (hentry[H_CC].body != NULL) {
2331                 compose->cc = conv_unmime_header(hentry[H_CC].body, NULL);
2332                 g_free(hentry[H_CC].body);
2333                 hentry[H_CC].body = NULL;
2334         }
2335         if (hentry[H_REFERENCES].body != NULL) {
2336                 if (compose->mode == COMPOSE_REEDIT)
2337                         compose->references = hentry[H_REFERENCES].body;
2338                 else {
2339                         compose->references = compose_parse_references
2340                                 (hentry[H_REFERENCES].body, msginfo->msgid);
2341                         g_free(hentry[H_REFERENCES].body);
2342                 }
2343                 hentry[H_REFERENCES].body = NULL;
2344         }
2345         if (hentry[H_BCC].body != NULL) {
2346                 if (compose->mode == COMPOSE_REEDIT)
2347                         compose->bcc =
2348                                 conv_unmime_header(hentry[H_BCC].body, NULL);
2349                 g_free(hentry[H_BCC].body);
2350                 hentry[H_BCC].body = NULL;
2351         }
2352         if (hentry[H_NEWSGROUPS].body != NULL) {
2353                 compose->newsgroups = hentry[H_NEWSGROUPS].body;
2354                 hentry[H_NEWSGROUPS].body = NULL;
2355         }
2356         if (hentry[H_FOLLOWUP_TO].body != NULL) {
2357                 if (hentry[H_FOLLOWUP_TO].body[0] != '\0') {
2358                         compose->followup_to =
2359                                 conv_unmime_header(hentry[H_FOLLOWUP_TO].body,
2360                                                    NULL);
2361                 }
2362                 g_free(hentry[H_FOLLOWUP_TO].body);
2363                 hentry[H_FOLLOWUP_TO].body = NULL;
2364         }
2365         if (hentry[H_LIST_POST].body != NULL) {
2366                 gchar *to = NULL;
2367
2368                 extract_address(hentry[H_LIST_POST].body);
2369                 if (hentry[H_LIST_POST].body[0] != '\0') {
2370                         scan_mailto_url(hentry[H_LIST_POST].body,
2371                                         &to, NULL, NULL, NULL, NULL, NULL);
2372                         if (to) {
2373                                 g_free(compose->ml_post);
2374                                 compose->ml_post = to;
2375                         }
2376                 }
2377                 g_free(hentry[H_LIST_POST].body);
2378                 hentry[H_LIST_POST].body = NULL;
2379         }
2380
2381         /* CLAWS - X-Priority */
2382         if (compose->mode == COMPOSE_REEDIT)
2383                 if (hentry[H_X_PRIORITY].body != NULL) {
2384                         gint priority;
2385                         
2386                         priority = atoi(hentry[H_X_PRIORITY].body);
2387                         g_free(hentry[H_X_PRIORITY].body);
2388                         
2389                         hentry[H_X_PRIORITY].body = NULL;
2390                         
2391                         if (priority < PRIORITY_HIGHEST || 
2392                             priority > PRIORITY_LOWEST)
2393                                 priority = PRIORITY_NORMAL;
2394                         
2395                         compose->priority =  priority;
2396                 }
2397  
2398         if (compose->mode == COMPOSE_REEDIT) {
2399                 if (msginfo->inreplyto && *msginfo->inreplyto)
2400                         compose->inreplyto = g_strdup(msginfo->inreplyto);
2401                 return 0;
2402         }
2403
2404         if (msginfo->msgid && *msginfo->msgid)
2405                 compose->inreplyto = g_strdup(msginfo->msgid);
2406
2407         if (!compose->references) {
2408                 if (msginfo->msgid && *msginfo->msgid) {
2409                         if (msginfo->inreplyto && *msginfo->inreplyto)
2410                                 compose->references =
2411                                         g_strdup_printf("<%s>\n\t<%s>",
2412                                                         msginfo->inreplyto,
2413                                                         msginfo->msgid);
2414                         else
2415                                 compose->references =
2416                                         g_strconcat("<", msginfo->msgid, ">",
2417                                                     NULL);
2418                 } else if (msginfo->inreplyto && *msginfo->inreplyto) {
2419                         compose->references =
2420                                 g_strconcat("<", msginfo->inreplyto, ">",
2421                                             NULL);
2422                 }
2423         }
2424
2425         return 0;
2426 }
2427
2428 static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
2429 {
2430         GSList *ref_id_list, *cur;
2431         GString *new_ref;
2432         gchar *new_ref_str;
2433
2434         ref_id_list = references_list_append(NULL, ref);
2435         if (!ref_id_list) return NULL;
2436         if (msgid && *msgid)
2437                 ref_id_list = g_slist_append(ref_id_list, g_strdup(msgid));
2438
2439         for (;;) {
2440                 gint len = 0;
2441
2442                 for (cur = ref_id_list; cur != NULL; cur = cur->next)
2443                         /* "<" + Message-ID + ">" + CR+LF+TAB */
2444                         len += strlen((gchar *)cur->data) + 5;
2445
2446                 if (len > MAX_REFERENCES_LEN) {
2447                         /* remove second message-ID */
2448                         if (ref_id_list && ref_id_list->next &&
2449                             ref_id_list->next->next) {
2450                                 g_free(ref_id_list->next->data);
2451                                 ref_id_list = g_slist_remove
2452                                         (ref_id_list, ref_id_list->next->data);
2453                         } else {
2454                                 slist_free_strings(ref_id_list);
2455                                 g_slist_free(ref_id_list);
2456                                 return NULL;
2457                         }
2458                 } else
2459                         break;
2460         }
2461
2462         new_ref = g_string_new("");
2463         for (cur = ref_id_list; cur != NULL; cur = cur->next) {
2464                 if (new_ref->len > 0)
2465                         g_string_append(new_ref, "\n\t");
2466                 g_string_append_printf(new_ref, "<%s>", (gchar *)cur->data);
2467         }
2468
2469         slist_free_strings(ref_id_list);
2470         g_slist_free(ref_id_list);
2471
2472         new_ref_str = new_ref->str;
2473         g_string_free(new_ref, FALSE);
2474
2475         return new_ref_str;
2476 }
2477
2478 static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
2479                                 const gchar *fmt, const gchar *qmark,
2480                                 const gchar *body, gboolean rewrap,
2481                                 gboolean need_unescape,
2482                                 const gchar *err_msg)
2483 {
2484         MsgInfo* dummyinfo = NULL;
2485         gchar *quote_str = NULL;
2486         gchar *buf;
2487         gboolean prev_autowrap;
2488         const gchar *trimmed_body = body;
2489         gint cursor_pos = -1;
2490         GtkTextView *text = GTK_TEXT_VIEW(compose->text);
2491         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
2492         GtkTextIter iter;
2493         GtkTextMark *mark;
2494         
2495
2496         SIGNAL_BLOCK(buffer);
2497
2498         if (!msginfo) {
2499                 dummyinfo = compose_msginfo_new_from_compose(compose);
2500                 msginfo = dummyinfo;
2501         }
2502
2503         if (qmark != NULL) {
2504                 quote_fmt_init(msginfo, NULL, NULL, FALSE, compose->account);
2505                 quote_fmt_scan_string(qmark);
2506                 quote_fmt_parse();
2507
2508                 buf = quote_fmt_get_buffer();
2509                 if (buf == NULL)
2510                         alertpanel_error(_("Quote mark format error."));
2511                 else
2512                         Xstrdup_a(quote_str, buf, goto error)
2513         }
2514
2515         if (fmt && *fmt != '\0') {
2516
2517                 if (trimmed_body)
2518                         while (*trimmed_body == '\n')
2519                                 trimmed_body++;
2520
2521                 quote_fmt_init(msginfo, quote_str, trimmed_body, FALSE, compose->account);
2522                 if (need_unescape) {
2523                         gchar *tmp = NULL;
2524
2525                         /* decode \-escape sequences in the internal representation of the quote format */
2526                         tmp = malloc(strlen(fmt)+1);
2527                         pref_get_unescaped_pref(tmp, fmt);
2528                         quote_fmt_scan_string(tmp);
2529                         quote_fmt_parse();
2530                         g_free(tmp);
2531                 } else {
2532                         quote_fmt_scan_string(fmt);
2533                         quote_fmt_parse();
2534                 }
2535
2536                 buf = quote_fmt_get_buffer();
2537                 if (buf == NULL) {
2538                         alertpanel_error(err_msg);
2539                         goto error;
2540                 }
2541         } else
2542                 buf = "";
2543
2544         prev_autowrap = compose->autowrap;
2545         compose->autowrap = FALSE;
2546
2547         mark = gtk_text_buffer_get_insert(buffer);
2548         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
2549         if (g_utf8_validate(buf, -1, NULL)) { 
2550                 gtk_text_buffer_insert(buffer, &iter, buf, -1);
2551         } else {
2552                 gchar *tmpout = NULL;
2553                 tmpout = conv_codeset_strdup
2554                         (buf, conv_get_locale_charset_str_no_utf8(),
2555                          CS_INTERNAL);
2556                 if (!tmpout || !g_utf8_validate(tmpout, -1, NULL)) {
2557                         g_free(tmpout);
2558                         tmpout = g_malloc(strlen(buf)*2+1);
2559                         conv_localetodisp(tmpout, strlen(buf)*2+1, buf);
2560                 }
2561                 gtk_text_buffer_insert(buffer, &iter, tmpout, -1);
2562                 g_free(tmpout);
2563         }
2564
2565         cursor_pos = quote_fmt_get_cursor_pos();
2566         compose->set_cursor_pos = cursor_pos;
2567         if (cursor_pos == -1) {
2568                 cursor_pos = 0;
2569         }
2570         gtk_text_buffer_get_start_iter(buffer, &iter);
2571         gtk_text_buffer_get_iter_at_offset(buffer, &iter, cursor_pos);
2572         gtk_text_buffer_place_cursor(buffer, &iter);
2573
2574         compose->autowrap = prev_autowrap;
2575         if (compose->autowrap && rewrap)
2576                 compose_wrap_all(compose);
2577
2578         goto ok;
2579
2580 error:
2581         buf = NULL;
2582 ok:
2583         SIGNAL_UNBLOCK(buffer);
2584
2585         procmsg_msginfo_free( dummyinfo );
2586
2587         return buf;
2588 }
2589
2590 /* if ml_post is of type addr@host and from is of type
2591  * addr-anything@host, return TRUE
2592  */
2593 static gboolean is_subscription(const gchar *ml_post, const gchar *from)
2594 {
2595         gchar *left_ml = NULL;
2596         gchar *right_ml = NULL;
2597         gchar *left_from = NULL;
2598         gchar *right_from = NULL;
2599         gboolean result = FALSE;
2600         
2601         if (!ml_post || !from)
2602                 return FALSE;
2603         
2604         left_ml = g_strdup(ml_post);
2605         if (strstr(left_ml, "@")) {
2606                 right_ml = strstr(left_ml, "@")+1;
2607                 *(strstr(left_ml, "@")) = '\0';
2608         }
2609         
2610         left_from = g_strdup(from);
2611         if (strstr(left_from, "@")) {
2612                 right_from = strstr(left_from, "@")+1;
2613                 *(strstr(left_from, "@")) = '\0';
2614         }
2615         
2616         if (left_ml && left_from && right_ml && right_from
2617         &&  !strncmp(left_from, left_ml, strlen(left_ml))
2618         &&  !strcmp(right_from, right_ml)) {
2619                 result = TRUE;
2620         }
2621         g_free(left_ml);
2622         g_free(left_from);
2623         
2624         return result;
2625 }
2626
2627 static gboolean same_address(const gchar *addr1, const gchar *addr2)
2628 {
2629         gchar *my_addr1, *my_addr2;
2630         
2631         if (!addr1 || !addr2)
2632                 return FALSE;
2633
2634         Xstrdup_a(my_addr1, addr1, return FALSE);
2635         Xstrdup_a(my_addr2, addr2, return FALSE);
2636         
2637         extract_address(my_addr1);
2638         extract_address(my_addr2);
2639         
2640         return !strcasecmp(my_addr1, my_addr2);
2641 }
2642
2643 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
2644                                     gboolean to_all, gboolean to_ml,
2645                                     gboolean to_sender,
2646                                     gboolean followup_and_reply_to)
2647 {
2648         GSList *cc_list = NULL;
2649         GSList *cur;
2650         gchar *from = NULL;
2651         gchar *replyto = NULL;
2652         GHashTable *to_table;
2653
2654         gboolean reply_to_ml = FALSE;
2655         gboolean default_reply_to = FALSE;
2656
2657         g_return_if_fail(compose->account != NULL);
2658         g_return_if_fail(msginfo != NULL);
2659
2660         reply_to_ml = to_ml && compose->ml_post;
2661
2662         default_reply_to = msginfo->folder && 
2663                 msginfo->folder->prefs->enable_default_reply_to;
2664
2665         if (compose->account->protocol != A_NNTP) {
2666                 if (reply_to_ml && !default_reply_to) {
2667                         
2668                         gboolean is_subscr = is_subscription(compose->ml_post,
2669                                                              msginfo->from);
2670                         if (!is_subscr) {
2671                                 /* normal answer to ml post with a reply-to */
2672                                 compose_entry_append(compose,
2673                                            compose->ml_post,
2674                                            COMPOSE_TO);
2675                                 if (compose->replyto
2676                                 &&  !same_address(compose->ml_post, compose->replyto))
2677                                         compose_entry_append(compose,
2678                                                 compose->replyto,
2679                                                 COMPOSE_CC);
2680                         } else {
2681                                 /* answer to subscription confirmation */
2682                                 if (compose->replyto)
2683                                         compose_entry_append(compose,
2684                                                 compose->replyto,
2685                                                 COMPOSE_TO);
2686                                 else if (msginfo->from)
2687                                         compose_entry_append(compose,
2688                                                 msginfo->from,
2689                                                 COMPOSE_TO);
2690                         }
2691                 }
2692                 else if (!(to_all || to_sender) && default_reply_to) {
2693                         compose_entry_append(compose,
2694                             msginfo->folder->prefs->default_reply_to,
2695                             COMPOSE_TO);
2696                         compose_entry_mark_default_to(compose,
2697                                 msginfo->folder->prefs->default_reply_to);
2698                 } else {
2699                         gchar *tmp1 = NULL;
2700                         if (!msginfo->from)
2701                                 return;
2702                         Xstrdup_a(tmp1, msginfo->from, return);
2703                         extract_address(tmp1);
2704                         if (to_all || to_sender ||
2705                             !account_find_from_address(tmp1))
2706                                 compose_entry_append(compose,
2707                                  (compose->replyto && !to_sender)
2708                                           ? compose->replyto :
2709                                           msginfo->from ? msginfo->from : "",
2710                                           COMPOSE_TO);
2711                         else if (!to_all && !to_sender) {
2712                                 /* reply to the last list of recipients */
2713                                 compose_entry_append(compose,
2714                                           msginfo->to ? msginfo->to : "",
2715                                           COMPOSE_TO);
2716                                 compose_entry_append(compose,
2717                                           msginfo->cc ? msginfo->cc : "",
2718                                           COMPOSE_CC);
2719                         }
2720                 }
2721         } else {
2722                 if (to_sender || (compose->followup_to && 
2723                         !strncmp(compose->followup_to, "poster", 6)))
2724                         compose_entry_append
2725                                 (compose, 
2726                                  (compose->replyto ? compose->replyto :
2727                                         msginfo->from ? msginfo->from : ""),
2728                                  COMPOSE_TO);
2729                                  
2730                 else if (followup_and_reply_to || to_all) {
2731                         compose_entry_append
2732                                 (compose,
2733                                  (compose->replyto ? compose->replyto :
2734                                  msginfo->from ? msginfo->from : ""),
2735                                  COMPOSE_TO);                           
2736                 
2737                         compose_entry_append
2738                                 (compose,
2739                                  compose->followup_to ? compose->followup_to :
2740                                  compose->newsgroups ? compose->newsgroups : "",
2741                                  COMPOSE_NEWSGROUPS);
2742                 } 
2743                 else 
2744                         compose_entry_append
2745                                 (compose,
2746                                  compose->followup_to ? compose->followup_to :
2747                                  compose->newsgroups ? compose->newsgroups : "",
2748                                  COMPOSE_NEWSGROUPS);
2749         }
2750
2751         if (msginfo->subject && *msginfo->subject) {
2752                 gchar *buf, *buf2;
2753                 gchar *p;
2754
2755                 buf = p = g_strdup(msginfo->subject);
2756                 p += subject_get_prefix_length(p);
2757                 memmove(buf, p, strlen(p) + 1);
2758
2759                 buf2 = g_strdup_printf("Re: %s", buf);
2760                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
2761
2762                 g_free(buf2);
2763                 g_free(buf);
2764         } else
2765                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
2766
2767         if (to_ml && compose->ml_post) return;
2768         if (!to_all || compose->account->protocol == A_NNTP) return;
2769
2770         if (compose->replyto) {
2771                 Xstrdup_a(replyto, compose->replyto, return);
2772                 extract_address(replyto);
2773         }
2774         if (msginfo->from) {
2775                 Xstrdup_a(from, msginfo->from, return);
2776                 extract_address(from);
2777         }
2778
2779         if (replyto && from)
2780                 cc_list = address_list_append_with_comments(cc_list, from);
2781         if (to_all && msginfo->folder && 
2782             msginfo->folder->prefs->enable_default_reply_to)
2783                 cc_list = address_list_append_with_comments(cc_list,
2784                                 msginfo->folder->prefs->default_reply_to);
2785         cc_list = address_list_append_with_comments(cc_list, msginfo->to);
2786         cc_list = address_list_append_with_comments(cc_list, compose->cc);
2787
2788         to_table = g_hash_table_new(g_str_hash, g_str_equal);
2789         if (replyto)
2790                 g_hash_table_insert(to_table, g_strdup(replyto), GINT_TO_POINTER(1));
2791         if (compose->account)
2792                 g_hash_table_insert(to_table, g_strdup(compose->account->address),
2793                                     GINT_TO_POINTER(1));
2794
2795         /* remove address on To: and that of current account */
2796         for (cur = cc_list; cur != NULL; ) {
2797                 GSList *next = cur->next;
2798                 gchar *addr;
2799
2800                 addr = g_strdup(cur->data);
2801                 extract_address(addr);
2802
2803                 if (GPOINTER_TO_INT(g_hash_table_lookup(to_table, addr)) == 1)
2804                         cc_list = g_slist_remove(cc_list, cur->data);
2805                 else
2806                         g_hash_table_insert(to_table, addr, GINT_TO_POINTER(1));
2807
2808                 cur = next;
2809         }
2810         hash_free_strings(to_table);
2811         g_hash_table_destroy(to_table);
2812
2813         if (cc_list) {
2814                 for (cur = cc_list; cur != NULL; cur = cur->next)
2815                         compose_entry_append(compose, (gchar *)cur->data,
2816                                              COMPOSE_CC);
2817                 slist_free_strings(cc_list);
2818                 g_slist_free(cc_list);
2819         }
2820
2821 }
2822
2823 #define SET_ENTRY(entry, str) \
2824 { \
2825         if (str && *str) \
2826                 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
2827 }
2828
2829 #define SET_ADDRESS(type, str) \
2830 { \
2831         if (str && *str) \
2832                 compose_entry_append(compose, str, type); \
2833 }
2834
2835 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
2836 {
2837         g_return_if_fail(msginfo != NULL);
2838
2839         SET_ENTRY(subject_entry, msginfo->subject);
2840         SET_ENTRY(from_name, msginfo->from);
2841         SET_ADDRESS(COMPOSE_TO, msginfo->to);
2842         SET_ADDRESS(COMPOSE_CC, compose->cc);
2843         SET_ADDRESS(COMPOSE_BCC, compose->bcc);
2844         SET_ADDRESS(COMPOSE_REPLYTO, compose->replyto);
2845         SET_ADDRESS(COMPOSE_NEWSGROUPS, compose->newsgroups);
2846         SET_ADDRESS(COMPOSE_FOLLOWUPTO, compose->followup_to);
2847
2848         compose_update_priority_menu_item(compose);
2849         compose_update_privacy_system_menu_item(compose, FALSE);
2850         compose_show_first_last_header(compose, TRUE);
2851 }
2852
2853 #undef SET_ENTRY
2854 #undef SET_ADDRESS
2855
2856 static void compose_insert_sig(Compose *compose, gboolean replace)
2857 {
2858         GtkTextView *text = GTK_TEXT_VIEW(compose->text);
2859         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
2860         GtkTextMark *mark;
2861         GtkTextIter iter, iter_end;
2862         gint cur_pos;
2863         gchar *search = NULL;
2864         gboolean prev_autowrap;
2865         gboolean found = FALSE, shift = FALSE;
2866
2867         
2868         g_return_if_fail(compose->account != NULL);
2869
2870         prev_autowrap = compose->autowrap;
2871         compose->autowrap = FALSE;
2872
2873         g_signal_handlers_block_by_func(G_OBJECT(buffer),
2874                                         G_CALLBACK(compose_changed_cb),
2875                                         compose);
2876         
2877         mark = gtk_text_buffer_get_insert(buffer);
2878         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
2879         cur_pos = gtk_text_iter_get_offset (&iter);
2880
2881         gtk_text_buffer_get_end_iter(buffer, &iter);
2882
2883         search = compose->sig_str;
2884 again:
2885         if (replace && search) {
2886                 GtkTextIter first_iter, start_iter, end_iter;
2887
2888                 gtk_text_buffer_get_start_iter(buffer, &first_iter);
2889
2890                 if (compose->sig_str[0] == '\0')
2891                         found = FALSE;
2892                 else
2893                         found = gtk_text_iter_forward_search(&first_iter,
2894                                                              search,
2895                                                              GTK_TEXT_SEARCH_TEXT_ONLY,
2896                                                              &start_iter, &end_iter,
2897                                                              NULL);
2898
2899                 if (found) {
2900                         gtk_text_buffer_delete(buffer, &start_iter, &end_iter);
2901                         iter = start_iter;
2902                 }
2903         } 
2904         if (replace && !found && search && strlen(search) > 2
2905         &&  search[0] == '\n' && search[1] == '\n') {
2906                 search ++;
2907                 shift = TRUE;
2908                 goto again;
2909         }
2910
2911         g_free(compose->sig_str);
2912         compose->sig_str = compose_get_signature_str(compose);
2913         if (!compose->sig_str || (replace && !compose->account->auto_sig))
2914                 compose->sig_str = g_strdup("");
2915
2916         cur_pos = gtk_text_iter_get_offset(&iter);
2917         if (shift && found)
2918                 gtk_text_buffer_insert(buffer, &iter, compose->sig_str + 1, -1);
2919         else
2920                 gtk_text_buffer_insert(buffer, &iter, compose->sig_str, -1);
2921         /* skip \n\n */
2922         gtk_text_buffer_get_iter_at_offset(buffer, &iter, cur_pos);
2923         gtk_text_iter_forward_char(&iter);
2924         gtk_text_iter_forward_char(&iter);
2925         gtk_text_buffer_get_end_iter(buffer, &iter_end);
2926         gtk_text_buffer_apply_tag_by_name(buffer,"signature",&iter, &iter_end);
2927
2928         if (cur_pos > gtk_text_buffer_get_char_count (buffer))
2929                 cur_pos = gtk_text_buffer_get_char_count (buffer);
2930
2931         /* put the cursor where it should be 
2932          * either where the quote_fmt says, either before the signature */
2933         if (compose->set_cursor_pos < 0)
2934                 gtk_text_buffer_get_iter_at_offset(buffer, &iter, cur_pos);
2935         else
2936                 gtk_text_buffer_get_iter_at_offset(buffer, &iter, 
2937                         compose->set_cursor_pos);
2938                 
2939         gtk_text_buffer_place_cursor(buffer, &iter);
2940         g_signal_handlers_unblock_by_func(G_OBJECT(buffer),
2941                                         G_CALLBACK(compose_changed_cb),
2942                                         compose);
2943                 
2944         compose->autowrap = prev_autowrap;
2945         if (compose->autowrap)
2946                 compose_wrap_all(compose);
2947 }
2948
2949 static gchar *compose_get_signature_str(Compose *compose)
2950 {
2951         gchar *sig_body = NULL;
2952         gchar *sig_str = NULL;
2953         gchar *utf8_sig_str = NULL;
2954
2955         g_return_val_if_fail(compose->account != NULL, NULL);
2956
2957         if (!compose->account->sig_path)
2958                 return NULL;
2959
2960         if (compose->account->sig_type == SIG_FILE) {
2961                 if (!is_file_or_fifo_exist(compose->account->sig_path)) {
2962                         g_warning("can't open signature file: %s\n",
2963                                   compose->account->sig_path);
2964                         return NULL;
2965                 }
2966         }
2967
2968         if (compose->account->sig_type == SIG_COMMAND)
2969                 sig_body = get_command_output(compose->account->sig_path);
2970         else {
2971                 gchar *tmp;
2972
2973                 tmp = file_read_to_str(compose->account->sig_path);
2974                 if (!tmp)
2975                         return NULL;
2976                 sig_body = normalize_newlines(tmp);
2977                 g_free(tmp);
2978         }
2979
2980         if (compose->account->sig_sep) {
2981                 sig_str = g_strconcat("\n\n", compose->account->sig_sep, "\n", sig_body,
2982                                       NULL);
2983                 g_free(sig_body);
2984         } else
2985                 sig_str = g_strconcat("\n\n", sig_body, NULL);
2986
2987         if (sig_str) {
2988                 if (g_utf8_validate(sig_str, -1, NULL) == TRUE)
2989                         utf8_sig_str = sig_str;
2990                 else {
2991                         utf8_sig_str = conv_codeset_strdup
2992                                 (sig_str, conv_get_locale_charset_str_no_utf8(),
2993                                  CS_INTERNAL);
2994                         g_free(sig_str);
2995                 }
2996         }
2997
2998         return utf8_sig_str;
2999 }
3000
3001 static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *file)
3002 {
3003         GtkTextView *text;
3004         GtkTextBuffer *buffer;
3005         GtkTextMark *mark;
3006         GtkTextIter iter;
3007         const gchar *cur_encoding;
3008         gchar buf[BUFFSIZE];
3009         gint len;
3010         FILE *fp;
3011         gboolean prev_autowrap;
3012         gboolean badtxt = FALSE;
3013
3014         g_return_val_if_fail(file != NULL, COMPOSE_INSERT_NO_FILE);
3015
3016         if ((fp = g_fopen(file, "rb")) == NULL) {
3017                 FILE_OP_ERROR(file, "fopen");
3018                 return COMPOSE_INSERT_READ_ERROR;
3019         }
3020
3021         prev_autowrap = compose->autowrap;
3022         compose->autowrap = FALSE;
3023
3024         text = GTK_TEXT_VIEW(compose->text);
3025         buffer = gtk_text_view_get_buffer(text);
3026         mark = gtk_text_buffer_get_insert(buffer);
3027         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
3028
3029         g_signal_handlers_block_by_func(G_OBJECT(buffer),
3030                                         G_CALLBACK(text_inserted),
3031                                         compose);
3032
3033         cur_encoding = conv_get_locale_charset_str_no_utf8();
3034
3035         while (fgets(buf, sizeof(buf), fp) != NULL) {
3036                 gchar *str;
3037
3038                 if (g_utf8_validate(buf, -1, NULL) == TRUE)
3039                         str = g_strdup(buf);
3040                 else
3041                         str = conv_codeset_strdup
3042                                 (buf, cur_encoding, CS_INTERNAL);
3043                 if (!str) continue;
3044
3045                 /* strip <CR> if DOS/Windows file,
3046                    replace <CR> with <LF> if Macintosh file. */
3047                 strcrchomp(str);
3048                 len = strlen(str);
3049                 if (len > 0 && str[len - 1] != '\n') {
3050                         while (--len >= 0)
3051                                 if (str[len] == '\r') str[len] = '\n';
3052                 }
3053
3054                 gtk_text_buffer_insert(buffer, &iter, str, -1);
3055                 g_free(str);
3056         }
3057
3058         g_signal_handlers_unblock_by_func(G_OBJECT(buffer),
3059                                           G_CALLBACK(text_inserted),
3060                                           compose);
3061         compose->autowrap = prev_autowrap;
3062         if (compose->autowrap)
3063                 compose_wrap_all(compose);
3064
3065         fclose(fp);
3066
3067         if (badtxt)
3068                 return COMPOSE_INSERT_INVALID_CHARACTER;
3069         else 
3070                 return COMPOSE_INSERT_SUCCESS;
3071 }
3072
3073 static gboolean compose_attach_append(Compose *compose, const gchar *file,
3074                                   const gchar *filename,
3075                                   const gchar *content_type)
3076 {
3077         AttachInfo *ainfo;
3078         GtkTreeIter iter;
3079         FILE *fp;
3080         off_t size;
3081         GAuto *auto_ainfo;
3082         gchar *size_text;
3083         GtkListStore *store;
3084         gchar *name;
3085         gboolean has_binary = FALSE;
3086
3087         if (!is_file_exist(file)) {
3088                 alertpanel_error("File %s doesn't exist\n", filename);
3089                 return FALSE;
3090         }
3091         if ((size = get_file_size(file)) < 0) {
3092                 alertpanel_error("Can't get file size of %s\n", filename);
3093                 return FALSE;
3094         }
3095         if (size == 0) {
3096                 alertpanel_error(_("File %s is empty."), filename);
3097                 return FALSE;
3098         }
3099         if ((fp = g_fopen(file, "rb")) == NULL) {
3100                 alertpanel_error(_("Can't read %s."), filename);
3101                 return FALSE;
3102         }
3103         fclose(fp);
3104
3105         ainfo = g_new0(AttachInfo, 1);
3106         auto_ainfo = g_auto_pointer_new_with_free
3107                         (ainfo, (GFreeFunc) compose_attach_info_free); 
3108         ainfo->file = g_strdup(file);
3109
3110         if (content_type) {
3111                 ainfo->content_type = g_strdup(content_type);
3112                 if (!g_ascii_strcasecmp(content_type, "message/rfc822")) {
3113                         MsgInfo *msginfo;
3114                         MsgFlags flags = {0, 0};
3115
3116                         if (procmime_get_encoding_for_text_file(file, &has_binary) == ENC_7BIT)
3117                                 ainfo->encoding = ENC_7BIT;
3118                         else
3119                                 ainfo->encoding = ENC_8BIT;
3120
3121                         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
3122                         if (msginfo && msginfo->subject)
3123                                 name = g_strdup(msginfo->subject);
3124                         else
3125                                 name = g_path_get_basename(filename ? filename : file);
3126
3127                         ainfo->name = g_strdup_printf(_("Message: %s"), name);
3128
3129                         procmsg_msginfo_free(msginfo);
3130                 } else {
3131                         if (!g_ascii_strncasecmp(content_type, "text", 4))
3132                                 ainfo->encoding = procmime_get_encoding_for_text_file(file, &has_binary);
3133                         else
3134                                 ainfo->encoding = ENC_BASE64;
3135                         name = g_path_get_basename(filename ? filename : file);
3136                         ainfo->name = g_strdup(name);
3137                 }
3138                 g_free(name);
3139         } else {
3140                 ainfo->content_type = procmime_get_mime_type(file);
3141                 if (!ainfo->content_type) {
3142                         ainfo->content_type =
3143                                 g_strdup("application/octet-stream");
3144                         ainfo->encoding = ENC_BASE64;
3145                 } else if (!g_ascii_strncasecmp(ainfo->content_type, "text", 4))
3146                         ainfo->encoding =
3147                                 procmime_get_encoding_for_text_file(file, &has_binary);
3148                 else
3149                         ainfo->encoding = ENC_BASE64;
3150                 name = g_path_get_basename(filename ? filename : file);
3151                 ainfo->name = g_strdup(name);   
3152                 g_free(name);
3153         }
3154
3155         if (ainfo->name != NULL
3156         &&  !strcmp(ainfo->name, ".")) {
3157                 g_free(ainfo->name);
3158                 ainfo->name = NULL;
3159         }
3160
3161         if (!strcmp(ainfo->content_type, "unknown") || has_binary) {
3162                 g_free(ainfo->content_type);
3163                 ainfo->content_type = g_strdup("application/octet-stream");
3164         }
3165
3166         ainfo->size = size;
3167         size_text = to_human_readable(size);
3168
3169         store = GTK_LIST_STORE(gtk_tree_view_get_model
3170                         (GTK_TREE_VIEW(compose->attach_clist)));
3171                 
3172         gtk_list_store_append(store, &iter);
3173         gtk_list_store_set(store, &iter, 
3174                            COL_MIMETYPE, ainfo->content_type,
3175                            COL_SIZE, size_text,
3176                            COL_NAME, ainfo->name,
3177                            COL_DATA, ainfo,
3178                            COL_AUTODATA, auto_ainfo,
3179                            -1);
3180         
3181         g_auto_pointer_free(auto_ainfo);
3182         return TRUE;
3183 }
3184
3185 static void compose_use_signing(Compose *compose, gboolean use_signing)
3186 {
3187         GtkItemFactory *ifactory;
3188         GtkWidget *menuitem = NULL;
3189
3190         compose->use_signing = use_signing;
3191         ifactory = gtk_item_factory_from_widget(compose->menubar);
3192         menuitem = gtk_item_factory_get_item
3193                 (ifactory, "/Options/Sign");
3194         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), 
3195                                        use_signing);
3196 }
3197
3198 static void compose_use_encryption(Compose *compose, gboolean use_encryption)
3199 {
3200         GtkItemFactory *ifactory;
3201         GtkWidget *menuitem = NULL;
3202
3203         compose->use_encryption = use_encryption;
3204         ifactory = gtk_item_factory_from_widget(compose->menubar);
3205         menuitem = gtk_item_factory_get_item
3206                 (ifactory, "/Options/Encrypt");
3207
3208         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), 
3209                                        use_encryption);
3210 }
3211
3212 #define NEXT_PART_NOT_CHILD(info)  \
3213 {  \
3214         node = info->node;  \
3215         while (node->children)  \
3216                 node = g_node_last_child(node);  \
3217         info = procmime_mimeinfo_next((MimeInfo *)node->data);  \
3218 }
3219
3220 static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
3221 {
3222         MimeInfo *mimeinfo;
3223         MimeInfo *child;
3224         MimeInfo *firsttext = NULL;
3225         MimeInfo *encrypted = NULL;
3226         GNode    *node;
3227         gchar *outfile;
3228         const gchar *partname = NULL;
3229
3230         mimeinfo = procmime_scan_message(msginfo);