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