210bb8e377f0e76c02897c5f1aa904d1e581a3b9
[claws.git] / src / compose.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gdk/gdkkeysyms.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkmenu.h>
30 #include <gtk/gtkmenuitem.h>
31 #include <gtk/gtkitemfactory.h>
32 #include <gtk/gtkcheckmenuitem.h>
33 #include <gtk/gtkoptionmenu.h>
34 #include <gtk/gtkwidget.h>
35 #include <gtk/gtkclist.h>
36 #include <gtk/gtkctree.h>
37 #include <gtk/gtkvpaned.h>
38 #include <gtk/gtkentry.h>
39 #include <gtk/gtkeditable.h>
40 #include <gtk/gtkwindow.h>
41 #include <gtk/gtksignal.h>
42 #include <gtk/gtkvbox.h>
43 #include <gtk/gtkcontainer.h>
44 #include <gtk/gtkhandlebox.h>
45 #include <gtk/gtktoolbar.h>
46 #include <gtk/gtktable.h>
47 #include <gtk/gtkhbox.h>
48 #include <gtk/gtklabel.h>
49 #include <gtk/gtkscrolledwindow.h>
50 #include <gtk/gtktreeview.h>
51 #warning FIXME_GTK2
52 /* #include <gtk/gtkthemes.h> */
53 #include <gtk/gtkdnd.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <ctype.h>
58 #include <sys/types.h>
59 #include <sys/stat.h>
60 #include <unistd.h>
61 #include <time.h>
62 /* #include <sys/utsname.h> */
63 #include <stdlib.h>
64 #include <sys/wait.h>
65 #include <signal.h>
66 #include <errno.h>
67 #include <libgen.h>
68
69 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
70 #  include <wchar.h>
71 #  include <wctype.h>
72 #endif
73
74 #include "intl.h"
75 #include "main.h"
76 #include "mainwindow.h"
77 #include "compose.h"
78 #include "addressbook.h"
79 #include "folderview.h"
80 #include "procmsg.h"
81 #include "menu.h"
82 #include "stock_pixmap.h"
83 #include "send_message.h"
84 #include "imap.h"
85 #include "news.h"
86 #include "customheader.h"
87 #include "prefs_common.h"
88 #include "prefs_account.h"
89 #include "action.h"
90 #include "account.h"
91 #include "filesel.h"
92 #include "procheader.h"
93 #include "procmime.h"
94 #include "statusbar.h"
95 #include "about.h"
96 #include "base64.h"
97 #include "quoted-printable.h"
98 #include "codeconv.h"
99 #include "utils.h"
100 #include "gtkutils.h"
101 #include "socket.h"
102 #include "alertpanel.h"
103 #include "manage_window.h"
104 #include "gtkshruler.h"
105 #include "folder.h"
106 #include "addr_compl.h"
107 #include "quote_fmt.h"
108 #include "template.h"
109 #include "undo.h"
110 #include "foldersel.h"
111 #include "toolbar.h"
112
113 #if USE_GPGME
114 #  include "rfc2015.h"
115 #endif
116
117 typedef enum
118 {
119         COL_MIMETYPE = 0,
120         COL_SIZE     = 1,
121         COL_NAME     = 2
122 } AttachColumnPos;
123
124 #define N_ATTACH_COLS           3
125
126 typedef enum
127 {
128         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BEGINNING_OF_LINE,
129         COMPOSE_CALL_ADVANCED_ACTION_MOVE_FORWARD_CHARACTER,
130         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BACKWARD_CHARACTER,
131         COMPOSE_CALL_ADVANCED_ACTION_MOVE_FORWARD_WORD,
132         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BACKWARD_WORD,
133         COMPOSE_CALL_ADVANCED_ACTION_MOVE_END_OF_LINE,
134         COMPOSE_CALL_ADVANCED_ACTION_MOVE_NEXT_LINE,
135         COMPOSE_CALL_ADVANCED_ACTION_MOVE_PREVIOUS_LINE,
136         COMPOSE_CALL_ADVANCED_ACTION_DELETE_FORWARD_CHARACTER,
137         COMPOSE_CALL_ADVANCED_ACTION_DELETE_BACKWARD_CHARACTER,
138         COMPOSE_CALL_ADVANCED_ACTION_DELETE_FORWARD_WORD,
139         COMPOSE_CALL_ADVANCED_ACTION_DELETE_BACKWARD_WORD,
140         COMPOSE_CALL_ADVANCED_ACTION_DELETE_LINE,
141         COMPOSE_CALL_ADVANCED_ACTION_DELETE_LINE_N,
142         COMPOSE_CALL_ADVANCED_ACTION_DELETE_TO_LINE_END
143 } ComposeCallAdvancedAction;
144
145 typedef enum
146 {
147         PRIORITY_HIGHEST = 1,
148         PRIORITY_HIGH,
149         PRIORITY_NORMAL,
150         PRIORITY_LOW,
151         PRIORITY_LOWEST
152 } PriorityLevel;
153
154 typedef enum
155 {
156         COMPOSE_INSERT_SUCCESS,
157         COMPOSE_INSERT_READ_ERROR,
158         COMPOSE_INSERT_INVALID_CHARACTER,
159         COMPOSE_INSERT_NO_FILE
160 } ComposeInsertResult;
161
162 #define B64_LINE_SIZE           57
163 #define B64_BUFFSIZE            77
164
165 #define MAX_REFERENCES_LEN      999
166
167 static GdkColor quote_color = {0, 0, 0, 0xbfff};
168
169 static GList *compose_list = NULL;
170
171 Compose *compose_generic_new                    (PrefsAccount   *account,
172                                                  const gchar    *to,
173                                                  FolderItem     *item,
174                                                  GPtrArray      *attach_files,
175                                                  GList          *listAddress );
176
177 static Compose *compose_create                  (PrefsAccount   *account,
178                                                  ComposeMode     mode);
179
180 static GtkWidget *compose_account_option_menu_create
181                                                 (Compose        *compose);
182 static void compose_set_template_menu           (Compose        *compose);
183 static void compose_template_apply              (Compose        *compose,
184                                                  Template       *tmpl,
185                                                  gboolean        replace);
186 static void compose_destroy                     (Compose        *compose);
187
188 static void compose_entries_set                 (Compose        *compose,
189                                                  const gchar    *mailto);
190 static gint compose_parse_header                (Compose        *compose,
191                                                  MsgInfo        *msginfo);
192 static gchar *compose_parse_references          (const gchar    *ref,
193                                                  const gchar    *msgid);
194
195 static gchar *compose_quote_fmt                 (Compose        *compose,
196                                                  MsgInfo        *msginfo,
197                                                  const gchar    *fmt,
198                                                  const gchar    *qmark,
199                                                  const gchar    *body);
200
201 static void compose_reply_set_entry             (Compose        *compose,
202                                                  MsgInfo        *msginfo,
203                                                  gboolean        to_all,
204                                                  gboolean        to_ml,
205                                                  gboolean        to_sender,
206                                                  gboolean
207                                                  followup_and_reply_to);
208 static void compose_reedit_set_entry            (Compose        *compose,
209                                                  MsgInfo        *msginfo);
210 static void compose_insert_sig                  (Compose        *compose,
211                                                  gboolean        replace);
212 static gchar *compose_get_signature_str         (Compose        *compose);
213 static ComposeInsertResult compose_insert_file  (Compose        *compose,
214                                                  const gchar    *file);
215 static void compose_attach_append               (Compose        *compose,
216                                                  const gchar    *file,
217                                                  const gchar    *type,
218                                                  const gchar    *content_type);
219 static void compose_attach_parts                (Compose        *compose,
220                                                  MsgInfo        *msginfo);
221 static void compose_wrap_line                   (Compose        *compose);
222 static void compose_wrap_line_all               (Compose        *compose);
223 static void compose_wrap_line_all_full          (Compose        *compose,
224                                                  gboolean        autowrap);
225 static void compose_set_title                   (Compose        *compose);
226 static void compose_select_account              (Compose        *compose,
227                                                  PrefsAccount   *account,
228                                                  gboolean        init);
229
230 static PrefsAccount *compose_current_mail_account(void);
231 /* static gint compose_send                     (Compose        *compose); */
232 static gboolean compose_check_for_valid_recipient
233                                                 (Compose        *compose);
234 static gboolean compose_check_entries           (Compose        *compose,
235                                                  gboolean       check_subject);
236 static gint compose_write_to_file               (Compose        *compose,
237                                                  const gchar    *file,
238                                                  gboolean        is_draft);
239 static gint compose_write_body_to_file          (Compose        *compose,
240                                                  const gchar    *file);
241 static gint compose_remove_reedit_target        (Compose        *compose);
242 void compose_remove_draft                       (Compose        *compose);
243 static gint compose_queue                       (Compose        *compose,
244                                                  gint           *msgnum,
245                                                  FolderItem     **item);
246 static gint compose_queue_sub                   (Compose        *compose,
247                                                  gint           *msgnum,
248                                                  FolderItem     **item,
249                                                  gboolean       check_subject);
250 static void compose_write_attach                (Compose        *compose,
251                                                  FILE           *fp);
252 static gint compose_write_headers               (Compose        *compose,
253                                                  FILE           *fp,
254                                                  const gchar    *charset,
255                                                  EncodingType    encoding,
256                                                  gboolean        is_draft);
257
258 static void compose_convert_header              (gchar          *dest,
259                                                  gint            len,
260                                                  gchar          *src,
261                                                  gint            header_len,
262                                                  gboolean        addr_field);
263 static void compose_generate_msgid              (gchar          *buf,
264                                                  gint            len);
265
266 static void compose_attach_info_free            (AttachInfo     *ainfo);
267 static void compose_attach_remove_selected      (Compose        *compose);
268
269 static void compose_attach_property             (Compose        *compose);
270 static void compose_attach_property_create      (gboolean       *cancelled);
271 static void attach_property_ok                  (GtkWidget      *widget,
272                                                  gboolean       *cancelled);
273 static void attach_property_cancel              (GtkWidget      *widget,
274                                                  gboolean       *cancelled);
275 static gint attach_property_delete_event        (GtkWidget      *widget,
276                                                  GdkEventAny    *event,
277                                                  gboolean       *cancelled);
278 static gboolean attach_property_key_pressed     (GtkWidget      *widget,
279                                                  GdkEventKey    *event,
280                                                  gboolean       *cancelled);
281
282 static void compose_exec_ext_editor             (Compose           *compose);
283 static gint compose_exec_ext_editor_real        (const gchar       *file);
284 static gboolean compose_ext_editor_kill         (Compose           *compose);
285 static void compose_input_cb                    (gpointer           data,
286                                                  gint               source,
287                                                  GdkInputCondition  condition);
288 static void compose_set_ext_editor_sensitive    (Compose           *compose,
289                                                  gboolean           sensitive);
290
291 static void compose_undo_state_changed          (UndoMain       *undostruct,
292                                                  gint            undo_state,
293                                                  gint            redo_state,
294                                                  gpointer        data);
295
296 static gint calc_cursor_xpos    (GtkTextView    *text,
297                                  gint            extra,
298                                  gint            char_width);
299
300 static void compose_create_header_entry (Compose *compose);
301 static void compose_add_header_entry    (Compose *compose, gchar *header, gchar *text);
302 static void compose_update_priority_menu_item(Compose * compose);
303
304 static void compose_add_field_list      ( Compose *compose,
305                                           GList *listAddress );
306
307 /* callback functions */
308
309 static gboolean compose_edit_size_alloc (GtkEditable    *widget,
310                                          GtkAllocation  *allocation,
311                                          GtkSHRuler     *shruler);
312 static void account_activated           (GtkMenuItem    *menuitem,
313                                          gpointer        data);
314 static void attach_selected             (GtkCList       *clist,
315                                          gint            row,
316                                          gint            column,
317                                          GdkEvent       *event,
318                                          gpointer        data);
319 static gboolean attach_button_pressed   (GtkWidget      *widget,
320                                          GdkEventButton *event,
321                                          gpointer        data);
322 static gboolean attach_key_pressed      (GtkWidget      *widget,
323                                          GdkEventKey    *event,
324                                          gpointer        data);
325
326 static void compose_send_cb             (gpointer        data,
327                                          guint           action,
328                                          GtkWidget      *widget);
329 static void compose_send_later_cb       (gpointer        data,
330                                          guint           action,
331                                          GtkWidget      *widget);
332
333 static void compose_draft_cb            (gpointer        data,
334                                          guint           action,
335                                          GtkWidget      *widget);
336
337 static void compose_attach_cb           (gpointer        data,
338                                          guint           action,
339                                          GtkWidget      *widget);
340 static void compose_insert_file_cb      (gpointer        data,
341                                          guint           action,
342                                          GtkWidget      *widget);
343 static void compose_insert_sig_cb       (gpointer        data,
344                                          guint           action,
345                                          GtkWidget      *widget);
346
347 static void compose_close_cb            (gpointer        data,
348                                          guint           action,
349                                          GtkWidget      *widget);
350
351 static void compose_address_cb          (gpointer        data,
352                                          guint           action,
353                                          GtkWidget      *widget);
354 static void compose_template_activate_cb(GtkWidget      *widget,
355                                          gpointer        data);
356
357 static void compose_ext_editor_cb       (gpointer        data,
358                                          guint           action,
359                                          GtkWidget      *widget);
360
361 static gint compose_delete_cb           (GtkWidget      *widget,
362                                          GdkEventAny    *event,
363                                          gpointer        data);
364 static void compose_destroy_cb          (GtkWidget      *widget,
365                                          Compose        *compose);
366
367 static void compose_undo_cb             (Compose        *compose);
368 static void compose_redo_cb             (Compose        *compose);
369 static void compose_cut_cb              (Compose        *compose);
370 static void compose_copy_cb             (Compose        *compose);
371 static void compose_paste_cb            (Compose        *compose);
372 static void compose_paste_as_quote_cb   (Compose        *compose);
373 static void compose_allsel_cb           (Compose        *compose);
374
375 static void compose_advanced_action_cb  (Compose                   *compose,
376                                          ComposeCallAdvancedAction  action);
377
378 static void compose_grab_focus_cb       (GtkWidget      *widget,
379                                          Compose        *compose);
380
381 static void compose_changed_cb          (GtkTextBuffer  *textbuf,
382                                          Compose        *compose);
383
384 static void compose_toggle_autowrap_cb  (gpointer        data,
385                                          guint           action,
386                                          GtkWidget      *widget);
387
388 #if 0
389 static void compose_toggle_to_cb        (gpointer        data,
390                                          guint           action,
391                                          GtkWidget      *widget);
392 static void compose_toggle_cc_cb        (gpointer        data,
393                                          guint           action,
394                                          GtkWidget      *widget);
395 static void compose_toggle_bcc_cb       (gpointer        data,
396                                          guint           action,
397                                          GtkWidget      *widget);
398 static void compose_toggle_replyto_cb   (gpointer        data,
399                                          guint           action,
400                                          GtkWidget      *widget);
401 static void compose_toggle_followupto_cb(gpointer        data,
402                                          guint           action,
403                                          GtkWidget      *widget);
404 static void compose_toggle_attach_cb    (gpointer        data,
405                                          guint           action,
406                                          GtkWidget      *widget);
407 #endif
408 static void compose_toggle_ruler_cb     (gpointer        data,
409                                          guint           action,
410                                          GtkWidget      *widget);
411 #if USE_GPGME
412 static void compose_toggle_sign_cb      (gpointer        data,
413                                          guint           action,
414                                          GtkWidget      *widget);
415 static void compose_toggle_encrypt_cb   (gpointer        data,
416                                          guint           action,
417                                          GtkWidget      *widget);
418 static void compose_set_gnupg_mode_cb   (gpointer        data,
419                                          guint           action,
420                                          GtkWidget      *widget);
421 static void compose_update_gnupg_mode_menu_item(Compose * compose);
422 static void activate_gnupg_mode         (Compose *compose, 
423                                          PrefsAccount *account);
424 #endif
425 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
426                                              GtkWidget *widget);
427 static void compose_toggle_remove_refs_cb(gpointer data, guint action,
428                                              GtkWidget *widget);
429 static void compose_set_priority_cb     (gpointer        data,
430                                          guint           action,
431                                          GtkWidget      *widget);
432
433 static void compose_attach_drag_received_cb (GtkWidget          *widget,
434                                              GdkDragContext     *drag_context,
435                                              gint                x,
436                                              gint                y,
437                                              GtkSelectionData   *data,
438                                              guint               info,
439                                              guint               time,
440                                              gpointer            user_data);
441 static void compose_insert_drag_received_cb (GtkWidget          *widget,
442                                              GdkDragContext     *drag_context,
443                                              gint                x,
444                                              gint                y,
445                                              GtkSelectionData   *data,
446                                              guint               info,
447                                              guint               time,
448                                              gpointer            user_data);
449
450 #if 0
451 static void to_activated                (GtkWidget      *widget,
452                                          Compose        *compose);
453 static void newsgroups_activated        (GtkWidget      *widget,
454                                          Compose        *compose);
455 static void cc_activated                (GtkWidget      *widget,
456                                          Compose        *compose);
457 static void bcc_activated               (GtkWidget      *widget,
458                                          Compose        *compose);
459 static void replyto_activated           (GtkWidget      *widget,
460                                          Compose        *compose);
461 static void followupto_activated        (GtkWidget      *widget,
462                                          Compose        *compose);
463 static void subject_activated           (GtkWidget      *widget,
464                                          Compose        *compose);
465 #endif
466
467 static void text_activated              (GtkWidget      *widget,
468                                          Compose        *compose);
469 static void text_inserted               (GtkTextBuffer  *buffer,
470                                          GtkTextIter    *iter,
471                                          const gchar    *text,
472                                          gint            len,
473                                          Compose        *compose);
474 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
475                                   gboolean to_all, gboolean to_ml,
476                                   gboolean to_sender,
477                                   gboolean followup_and_reply_to,
478                                   const gchar *body);
479
480 gboolean compose_headerentry_changed_cb    (GtkWidget          *entry,
481                                             ComposeHeaderEntry *headerentry);
482 gboolean compose_headerentry_key_press_event_cb(GtkWidget              *entry,
483                                             GdkEventKey        *event,
484                                             ComposeHeaderEntry *headerentry);
485 static gboolean compose_headerentry_button_pressed (GtkWidget *entry, 
486                                                     GdkEventButton *event,
487                                                     gpointer data);
488
489 static void compose_show_first_last_header (Compose *compose, gboolean show_first);
490
491 #if USE_ASPELL
492 static void compose_check_all              (Compose *compose);
493 static void compose_highlight_all          (Compose *compose);
494 static void compose_check_backwards        (Compose *compose);
495 static void compose_check_forwards_go      (Compose *compose);
496 #endif
497
498 static gboolean compose_send_control_enter      (Compose        *compose);
499 static gint compose_defer_auto_save_draft       (Compose        *compose);
500 static PrefsAccount *compose_guess_forward_account_from_msginfo (MsgInfo *msginfo);
501
502 static GtkItemFactoryEntry compose_popup_entries[] =
503 {
504         {N_("/_Add..."),        NULL, compose_attach_cb, 0, NULL},
505         {N_("/_Remove"),        NULL, compose_attach_remove_selected, 0, NULL},
506         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
507         {N_("/_Properties..."), NULL, compose_attach_property, 0, NULL}
508 };
509
510 static GtkItemFactoryEntry compose_entries[] =
511 {
512         {N_("/_File"),                          NULL, NULL, 0, "<Branch>"},
513         {N_("/_File/_Attach file"),             "<control>M", compose_attach_cb,      0, NULL},
514         {N_("/_File/_Insert file"),             "<control>I", compose_insert_file_cb, 0, NULL},
515         {N_("/_File/Insert si_gnature"),        "<control>G", compose_insert_sig_cb,  0, NULL},
516         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
517         {N_("/_File/_Close"),                   "<control>W", compose_close_cb, 0, NULL},
518
519         {N_("/_Edit"),                  NULL, NULL, 0, "<Branch>"},
520         {N_("/_Edit/_Undo"),            "<control>Z", compose_undo_cb, 0, NULL},
521         {N_("/_Edit/_Redo"),            "<control>Y", compose_redo_cb, 0, NULL},
522         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
523         {N_("/_Edit/Cu_t"),             "<control>X", compose_cut_cb,    0, NULL},
524         {N_("/_Edit/_Copy"),            "<control>C", compose_copy_cb,   0, NULL},
525         {N_("/_Edit/_Paste"),           "<control>V", compose_paste_cb,  0, NULL},
526         {N_("/_Edit/Paste as _quotation"),
527                                         NULL, compose_paste_as_quote_cb, 0, NULL},
528         {N_("/_Edit/Select _all"),      "<control>A", compose_allsel_cb, 0, NULL},
529         {N_("/_Edit/A_dvanced"),        NULL, NULL, 0, "<Branch>"},
530         {N_("/_Edit/A_dvanced/Move a character backward"),
531                                         "<control>B",
532                                         compose_advanced_action_cb,
533                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BACKWARD_CHARACTER,
534                                         NULL},
535         {N_("/_Edit/A_dvanced/Move a character forward"),
536                                         "<control>F",
537                                         compose_advanced_action_cb,
538                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_FORWARD_CHARACTER,
539                                         NULL},
540         {N_("/_Edit/A_dvanced/Move a word backward"),
541                                         NULL, /* "<alt>B" */
542                                         compose_advanced_action_cb,
543                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BACKWARD_WORD,
544                                         NULL},
545         {N_("/_Edit/A_dvanced/Move a word forward"),
546                                         NULL, /* "<alt>F" */
547                                         compose_advanced_action_cb,
548                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_FORWARD_WORD,
549                                         NULL},
550         {N_("/_Edit/A_dvanced/Move to beginning of line"),
551                                         NULL, /* "<control>A" */
552                                         compose_advanced_action_cb,
553                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_BEGINNING_OF_LINE,
554                                         NULL},
555         {N_("/_Edit/A_dvanced/Move to end of line"),
556                                         "<control>E",
557                                         compose_advanced_action_cb,
558                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_END_OF_LINE,
559                                         NULL},
560         {N_("/_Edit/A_dvanced/Move to previous line"),
561                                         "<control>P",
562                                         compose_advanced_action_cb,
563                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_PREVIOUS_LINE,
564                                         NULL},
565         {N_("/_Edit/A_dvanced/Move to next line"),
566                                         "<control>N",
567                                         compose_advanced_action_cb,
568                                         COMPOSE_CALL_ADVANCED_ACTION_MOVE_NEXT_LINE,
569                                         NULL},
570         {N_("/_Edit/A_dvanced/Delete a character backward"),
571                                         "<control>H",
572                                         compose_advanced_action_cb,
573                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_BACKWARD_CHARACTER,
574                                         NULL},
575         {N_("/_Edit/A_dvanced/Delete a character forward"),
576                                         "<control>D",
577                                         compose_advanced_action_cb,
578                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_FORWARD_CHARACTER,
579                                         NULL},
580         {N_("/_Edit/A_dvanced/Delete a word backward"),
581                                         NULL, /* "<control>W" */
582                                         compose_advanced_action_cb,
583                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_BACKWARD_WORD,
584                                         NULL},
585         {N_("/_Edit/A_dvanced/Delete a word forward"),
586                                         NULL, /* "<alt>D", */
587                                         compose_advanced_action_cb,
588                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_FORWARD_WORD,
589                                         NULL},
590         {N_("/_Edit/A_dvanced/Delete line"),
591                                         "<control>U",
592                                         compose_advanced_action_cb,
593                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_LINE,
594                                         NULL},
595         {N_("/_Edit/A_dvanced/Delete entire line"),
596                                         NULL,
597                                         compose_advanced_action_cb,
598                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_LINE_N,
599                                         NULL},
600         {N_("/_Edit/A_dvanced/Delete to end of line"),
601                                         "<control>K",
602                                         compose_advanced_action_cb,
603                                         COMPOSE_CALL_ADVANCED_ACTION_DELETE_TO_LINE_END,
604                                         NULL},
605         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
606         {N_("/_Edit/_Wrap current paragraph"),
607                                         "<control>L", compose_wrap_line, 0, NULL},
608         {N_("/_Edit/Wrap all long _lines"),
609                                         "<control><alt>L", compose_wrap_line_all, 0, NULL},
610         {N_("/_Edit/Aut_o wrapping"),   "<shift><control>L", compose_toggle_autowrap_cb, 0, "<ToggleItem>"},
611         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
612         {N_("/_Edit/Edit with e_xternal editor"),
613                                         "<shift><control>X", compose_ext_editor_cb, 0, NULL},
614 #if USE_ASPELL
615         {N_("/_Spelling"),              NULL, NULL, 0, "<Branch>"},
616         {N_("/_Spelling/_Check all or check selection"),
617                                         NULL, compose_check_all, 0, NULL},
618         {N_("/_Spelling/_Highlight all misspelled words"),
619                                         NULL, compose_highlight_all, 0, NULL},
620         {N_("/_Spelling/Check _backwards misspelled word"),
621                                         NULL, compose_check_backwards , 0, NULL},
622         {N_("/_Spelling/_Forward to next misspelled word"),
623                                         NULL, compose_check_forwards_go, 0, NULL},
624         {N_("/_Spelling/---"),          NULL, NULL, 0, "<Separator>"},
625         {N_("/_Spelling/_Spelling Configuration"),
626                                         NULL, NULL, 0, "<Branch>"},
627 #endif
628 #if 0 /* NEW COMPOSE GUI */
629         {N_("/_View"),                  NULL, NULL, 0, "<Branch>"},
630         {N_("/_View/_To"),              NULL, compose_toggle_to_cb     , 0, "<ToggleItem>"},
631         {N_("/_View/_Cc"),              NULL, compose_toggle_cc_cb     , 0, "<ToggleItem>"},
632         {N_("/_View/_Bcc"),             NULL, compose_toggle_bcc_cb    , 0, "<ToggleItem>"},
633         {N_("/_View/_Reply to"),        NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
634         {N_("/_View/---"),              NULL, NULL, 0, "<Separator>"},
635         {N_("/_View/_Followup to"),     NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
636         {N_("/_View/---"),              NULL, NULL, 0, "<Separator>"},
637         {N_("/_View/R_uler"),           NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
638         {N_("/_View/---"),              NULL, NULL, 0, "<Separator>"},
639         {N_("/_View/_Attachment"),      NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
640 #endif
641         {N_("/_Message"),               NULL, NULL, 0, "<Branch>"},
642         {N_("/_Message/_Send"),         "<control>Return",
643                                         compose_send_cb, 0, NULL},
644         {N_("/_Message/Send _later"),   "<shift><control>S",
645                                         compose_send_later_cb,  0, NULL},
646         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
647         {N_("/_Message/Save to _draft folder"),
648                                         "<shift><control>D", compose_draft_cb, 0, NULL},
649         {N_("/_Message/Save and _keep editing"),
650                                         "<control>S", compose_draft_cb, 1, NULL},
651 #if 0 /* NEW COMPOSE GUI */
652         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
653         {N_("/_Message/_To"),           NULL, compose_toggle_to_cb     , 0, "<ToggleItem>"},
654         {N_("/_Message/_Cc"),           NULL, compose_toggle_cc_cb     , 0, "<ToggleItem>"},
655         {N_("/_Message/_Bcc"),          NULL, compose_toggle_bcc_cb    , 0, "<ToggleItem>"},
656         {N_("/_Message/_Reply to"),     NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
657         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
658         {N_("/_Message/_Followup to"),  NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
659         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
660         {N_("/_Message/_Attach"),       NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
661 #endif
662 #if USE_GPGME
663         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
664         {N_("/_Message/Si_gn"),         NULL, compose_toggle_sign_cb   , 0, "<ToggleItem>"},
665         {N_("/_Message/_Encrypt"),      NULL, compose_toggle_encrypt_cb, 0, "<ToggleItem>"},
666         {N_("/_Message/Mode"),          NULL, NULL,   0, "<Branch>"},
667         {N_("/_Message/Mode/MIME"),     NULL, compose_set_gnupg_mode_cb,   GNUPG_MODE_DETACH, "<RadioItem>"},   
668         {N_("/_Message/Mode/Inline"),   NULL, compose_set_gnupg_mode_cb,   GNUPG_MODE_INLINE, "/Message/Mode/MIME"},    
669 #endif /* USE_GPGME */
670         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
671         {N_("/_Message/_Priority"),     NULL,           NULL,   0, "<Branch>"},
672         {N_("/_Message/Priority/_Highest"), NULL, compose_set_priority_cb, PRIORITY_HIGHEST, "<RadioItem>"},
673         {N_("/_Message/Priority/Hi_gh"),    NULL, compose_set_priority_cb, PRIORITY_HIGH, "/Message/Priority/Highest"},
674         {N_("/_Message/Priority/_Normal"),  NULL, compose_set_priority_cb, PRIORITY_NORMAL, "/Message/Priority/Highest"},
675         {N_("/_Message/Priority/Lo_w"),    NULL, compose_set_priority_cb, PRIORITY_LOW, "/Message/Priority/Highest"},
676         {N_("/_Message/Priority/_Lowest"),  NULL, compose_set_priority_cb, PRIORITY_LOWEST, "/Message/Priority/Highest"},
677         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
678         {N_("/_Message/_Request Return Receipt"),       NULL, compose_toggle_return_receipt_cb, 0, "<ToggleItem>"},
679         {N_("/_Message/Remo_ve references"),    NULL, compose_toggle_remove_refs_cb, 0, "<ToggleItem>"},
680         {N_("/_Tools"),                 NULL, NULL, 0, "<Branch>"},
681         {N_("/_Tools/Show _ruler"),     NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
682         {N_("/_Tools/_Address book"),   "<shift><control>A", compose_address_cb , 0, NULL},
683         {N_("/_Tools/_Template"),       NULL, NULL, 0, "<Branch>"},
684         {N_("/_Tools/Actio_ns"),        NULL, NULL, 0, "<Branch>"},
685         {N_("/_Help"),                  NULL, NULL, 0, "<Branch>"},
686         {N_("/_Help/_About"),           NULL, about_show, 0, NULL}
687 };
688
689 static GtkTargetEntry compose_mime_types[] =
690 {
691         {"text/uri-list", 0, 0}
692 };
693
694 Compose *compose_new(PrefsAccount *account, const gchar *mailto,
695                      GPtrArray *attach_files)
696 {
697         return compose_generic_new(account, mailto, NULL, attach_files, NULL);
698 }
699
700 Compose *compose_new_with_folderitem(PrefsAccount *account, FolderItem *item)
701 {
702         return compose_generic_new(account, NULL, item, NULL, NULL);
703 }
704
705 Compose *compose_new_with_list( PrefsAccount *account, GList *listAddress )
706 {
707         return compose_generic_new( account, NULL, NULL, NULL, listAddress );
708 }
709
710 Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderItem *item,
711                              GPtrArray *attach_files, GList *listAddress )
712 {
713         Compose *compose;
714         GtkTextView *textview;
715         GtkTextBuffer *textbuf;
716         GtkTextIter iter;
717         GtkItemFactory *ifactory;
718
719         if (item && item->prefs && item->prefs->enable_default_account)
720                 account = account_find_from_id(item->prefs->default_account);
721
722         if (!account) account = cur_account;
723         g_return_val_if_fail(account != NULL, NULL);
724
725         compose = compose_create(account, COMPOSE_NEW);
726         ifactory = gtk_item_factory_from_widget(compose->menubar);
727
728         compose->replyinfo = NULL;
729         compose->fwdinfo   = NULL;
730
731         textview = GTK_TEXT_VIEW(compose->text);
732         textbuf = gtk_text_view_get_buffer(textview);
733
734 #ifdef USE_ASPELL
735         if (item && item->prefs && item->prefs->enable_default_dictionary &&
736             compose->gtkaspell) 
737                 gtkaspell_change_dict(compose->gtkaspell, 
738                     item->prefs->default_dictionary);
739 #endif
740
741         if (account->auto_sig)
742                 compose_insert_sig(compose, FALSE);
743         gtk_text_buffer_get_start_iter(textbuf, &iter);
744         gtk_text_buffer_place_cursor(textbuf, &iter);
745
746         if (account->protocol != A_NNTP) {
747                 if (mailto && *mailto != '\0') {
748                         compose_entries_set(compose, mailto);
749
750                 } else if (item && item->prefs->enable_default_to) {
751                         compose_entry_append(compose, item->prefs->default_to, COMPOSE_TO);
752                         compose_entry_mark_default_to(compose, item->prefs->default_to);
753                 }
754                 if (item && item->ret_rcpt) {
755                         menu_set_active(ifactory, "/Message/Request Return Receipt", TRUE);
756                 }
757         } else {
758                 if (mailto) {
759                         compose_entry_append(compose, mailto, COMPOSE_NEWSGROUPS);
760                 }
761                 /*
762                  * CLAWS: just don't allow return receipt request, even if the user
763                  * may want to send an email. simple but foolproof.
764                  */
765                 menu_set_sensitive(ifactory, "/Message/Request Return Receipt", FALSE); 
766         }
767         compose_add_field_list( compose, listAddress );
768
769         if (attach_files) {
770                 gint i;
771                 gchar *file;
772
773                 for (i = 0; i < attach_files->len; i++) {
774                         file = g_ptr_array_index(attach_files, i);
775                         compose_attach_append(compose, file, file, NULL);
776                 }
777         }
778
779         compose_show_first_last_header(compose, TRUE);
780
781         /* Set save folder */
782         if (item && item->prefs && item->prefs->save_copy_to_folder) {
783                 gchar *folderidentifier;
784
785                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
786                 folderidentifier = folder_item_get_identifier(item);
787                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
788                 g_free(folderidentifier);
789         }
790         
791         gtk_widget_grab_focus(compose->header_last->entry);
792
793         undo_unblock(compose->undostruct);
794
795         if (prefs_common.auto_exteditor)
796                 compose_exec_ext_editor(compose);
797
798         return compose;
799 }
800
801 /*
802 Compose *compose_new_followup_and_replyto(PrefsAccount *account,
803                                            const gchar *followupto, gchar * to)
804 {
805         Compose *compose;
806
807         if (!account) account = cur_account;
808         g_return_val_if_fail(account != NULL, NULL);
809         g_return_val_if_fail(account->protocol != A_NNTP, NULL);
810
811         compose = compose_create(account, COMPOSE_NEW);
812
813         if (prefs_common.auto_sig)
814                 compose_insert_sig(compose);
815         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
816         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
817
818         compose_entry_append(compose, to, COMPOSE_TO);
819         compose_entry_append(compose, followupto, COMPOSE_NEWSGROUPS);
820         gtk_widget_grab_focus(compose->subject_entry);
821
822         return compose;
823 }
824 */
825
826 void compose_reply_mode(ComposeMode mode, GSList *msginfo_list, gchar *body)
827 {
828         MsgInfo *msginfo;
829         guint list_len;
830
831         g_return_if_fail(msginfo_list != NULL);
832
833         msginfo = (MsgInfo*)g_slist_nth_data(msginfo_list, 0);
834         g_return_if_fail(msginfo != NULL);
835
836         list_len = g_slist_length(msginfo_list);
837
838         switch (mode) {
839         case COMPOSE_REPLY:
840                 compose_reply(msginfo, prefs_common.reply_with_quote,
841                               FALSE, prefs_common.default_reply_list, FALSE, body);
842                 break;
843         case COMPOSE_REPLY_WITH_QUOTE:
844                 compose_reply(msginfo, TRUE, FALSE, prefs_common.default_reply_list, FALSE, body);
845                 break;
846         case COMPOSE_REPLY_WITHOUT_QUOTE:
847                 compose_reply(msginfo, FALSE, FALSE, prefs_common.default_reply_list, FALSE, NULL);
848                 break;
849         case COMPOSE_REPLY_TO_SENDER:
850                 compose_reply(msginfo, prefs_common.reply_with_quote,
851                               FALSE, FALSE, TRUE, body);
852                 break;
853         case COMPOSE_FOLLOWUP_AND_REPLY_TO:
854                 compose_followup_and_reply_to(msginfo,
855                                               prefs_common.reply_with_quote,
856                                               FALSE, FALSE, body);
857                 break;
858         case COMPOSE_REPLY_TO_SENDER_WITH_QUOTE:
859                 compose_reply(msginfo, TRUE, FALSE, FALSE, TRUE, body);
860                 break;
861         case COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE:
862                 compose_reply(msginfo, FALSE, FALSE, FALSE, TRUE, NULL);
863                 break;
864         case COMPOSE_REPLY_TO_ALL:
865                 compose_reply(msginfo, prefs_common.reply_with_quote,
866                               TRUE, FALSE, FALSE, body);
867                 break;
868         case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
869                 compose_reply(msginfo, TRUE, TRUE, FALSE, FALSE, body);
870                 break;
871         case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
872                 compose_reply(msginfo, FALSE, TRUE, FALSE, FALSE, NULL);
873                 break;
874         case COMPOSE_REPLY_TO_LIST:
875                 compose_reply(msginfo, prefs_common.reply_with_quote,
876                               FALSE, TRUE, FALSE, body);
877                 break;
878         case COMPOSE_REPLY_TO_LIST_WITH_QUOTE:
879                 compose_reply(msginfo, TRUE, FALSE, TRUE, FALSE, body);
880                 break;
881         case COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE:
882                 compose_reply(msginfo, FALSE, FALSE, TRUE, FALSE, NULL);
883                 break;
884         case COMPOSE_FORWARD:
885                 if (prefs_common.forward_as_attachment) {
886                         compose_reply_mode(COMPOSE_FORWARD_AS_ATTACH, msginfo_list, body);
887                         return;
888                 } else {
889                         compose_reply_mode(COMPOSE_FORWARD_INLINE, msginfo_list, body);
890                         return;
891                 }
892                 break;
893         case COMPOSE_FORWARD_INLINE:
894                 /* check if we reply to more than one Message */
895                 if (list_len == 1) {
896                         compose_forward(NULL, msginfo, FALSE, body, FALSE);
897                         break;
898                 } 
899                 /* more messages FALL THROUGH */
900         case COMPOSE_FORWARD_AS_ATTACH:
901                 compose_forward_multiple(NULL, msginfo_list);
902                 break;
903         case COMPOSE_REDIRECT:
904                 compose_redirect(NULL, msginfo);
905                 break;
906         default:
907                 g_warning("compose_reply(): invalid Compose Mode: %d\n", mode);
908         }
909 }
910
911 void compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
912                    gboolean to_ml, gboolean to_sender, 
913                    const gchar *body)
914 {
915         compose_generic_reply(msginfo, quote, to_all, to_ml, 
916                               to_sender, FALSE, body);
917 }
918
919 void compose_followup_and_reply_to(MsgInfo *msginfo, gboolean quote,
920                                    gboolean to_all,
921                                    gboolean to_sender,
922                                    const gchar *body)
923 {
924         compose_generic_reply(msginfo, quote, to_all, FALSE, 
925                               to_sender, TRUE, body);
926 }
927
928 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
929                                   gboolean to_all, gboolean to_ml,
930                                   gboolean to_sender,
931                                   gboolean followup_and_reply_to,
932                                   const gchar *body)
933 {
934         GtkItemFactory *ifactory;
935         Compose *compose;
936         PrefsAccount *account = NULL;
937         PrefsAccount *reply_account;
938         GtkTextView *textview;
939         GtkTextBuffer *textbuf;
940         GtkTextIter iter;
941         int cursor_pos;
942
943         g_return_if_fail(msginfo != NULL);
944         g_return_if_fail(msginfo->folder != NULL);
945
946         account = account_get_reply_account(msginfo, prefs_common.reply_account_autosel);
947         
948         g_return_if_fail(account != NULL);
949
950         if (to_sender && account->protocol == A_NNTP &&
951             !followup_and_reply_to) {
952                 reply_account =
953                         account_find_from_address(account->address);
954                 if (!reply_account)
955                         reply_account = compose_current_mail_account();
956                 if (!reply_account)
957                         return;
958         } else
959                 reply_account = account;
960
961         compose = compose_create(account, COMPOSE_REPLY);
962         ifactory = gtk_item_factory_from_widget(compose->menubar);
963
964         menu_set_active(ifactory, "/Message/Remove references", FALSE);
965         menu_set_sensitive(ifactory, "/Message/Remove references", TRUE);
966
967         compose->replyinfo = procmsg_msginfo_get_full_info(msginfo);
968         if (!compose->replyinfo)
969                 compose->replyinfo = procmsg_msginfo_copy(msginfo);
970
971         if (msginfo->folder && msginfo->folder->ret_rcpt)
972                 menu_set_active(ifactory, "/Message/Request Return Receipt", TRUE);
973
974         /* Set save folder */
975         if (msginfo->folder && msginfo->folder->prefs && msginfo->folder->prefs->save_copy_to_folder) {
976                 gchar *folderidentifier;
977
978                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
979                 folderidentifier = folder_item_get_identifier(msginfo->folder);
980                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
981                 g_free(folderidentifier);
982         }
983
984         if (compose_parse_header(compose, msginfo) < 0) return;
985         compose_reply_set_entry(compose, msginfo, to_all, to_ml, 
986                                 to_sender, followup_and_reply_to);
987         compose_show_first_last_header(compose, TRUE);
988
989         textview = (GTK_TEXT_VIEW(compose->text));
990         textbuf = gtk_text_view_get_buffer(textview);
991         
992 #ifdef USE_ASPELL
993         if (msginfo->folder && msginfo->folder->prefs && 
994             msginfo->folder->prefs && 
995             msginfo->folder->prefs->enable_default_dictionary &&
996             compose->gtkaspell)
997                 gtkaspell_change_dict(compose->gtkaspell, 
998                     msginfo->folder->prefs->default_dictionary);
999 #endif
1000
1001         if (quote) {
1002                 gchar *qmark;
1003
1004                 if (prefs_common.quotemark && *prefs_common.quotemark)
1005                         qmark = prefs_common.quotemark;
1006                 else
1007                         qmark = "> ";
1008
1009                 compose_quote_fmt(compose, compose->replyinfo,
1010                                   prefs_common.quotefmt,
1011                                   qmark, body);
1012         }
1013
1014         if (account->auto_sig)
1015                 compose_insert_sig(compose, FALSE);
1016
1017         if (quote && prefs_common.linewrap_quote)
1018                 compose_wrap_line_all(compose);
1019
1020         cursor_pos = quote_fmt_get_cursor_pos();
1021         gtk_text_buffer_get_start_iter(textbuf, &iter);
1022         gtk_text_buffer_get_iter_at_offset(textbuf, &iter, cursor_pos);
1023         gtk_text_buffer_place_cursor(textbuf, &iter);
1024
1025         if (quote && prefs_common.linewrap_quote) {
1026                 compose_wrap_line_all(compose);
1027                 gtk_text_view_set_editable(GTK_TEXT_VIEW(compose->text), TRUE);
1028         }
1029
1030         gtk_widget_grab_focus(compose->text);
1031
1032         undo_unblock(compose->undostruct);
1033
1034         if (prefs_common.auto_exteditor)
1035                 compose_exec_ext_editor(compose);
1036 }
1037
1038 #define INSERT_FW_HEADER(var, hdr) \
1039 if (msginfo->var && *msginfo->var) { \
1040         gtk_stext_insert(text, NULL, NULL, NULL, hdr, -1); \
1041         gtk_stext_insert(text, NULL, NULL, NULL, msginfo->var, -1); \
1042         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1); \
1043 }
1044
1045 Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
1046                          gboolean as_attach, const gchar *body,
1047                          gboolean no_extedit)
1048 {
1049         Compose *compose;
1050         GtkTextView *textview;
1051         GtkTextBuffer *textbuf;
1052         GtkTextIter iter;
1053
1054         g_return_val_if_fail(msginfo != NULL, NULL);
1055         g_return_val_if_fail(msginfo->folder != NULL, NULL);
1056
1057         if (!account && 
1058             !(account = compose_guess_forward_account_from_msginfo
1059                                 (msginfo)))
1060                 account = cur_account;
1061
1062         compose = compose_create(account, COMPOSE_FORWARD);
1063
1064         compose->fwdinfo = procmsg_msginfo_get_full_info(msginfo);
1065         if (!compose->fwdinfo)
1066                 compose->fwdinfo = procmsg_msginfo_copy(msginfo);
1067
1068         if (msginfo->subject && *msginfo->subject) {
1069                 gchar *buf, *buf2, *p;
1070
1071                 buf = p = g_strdup(msginfo->subject);
1072                 p += subject_get_prefix_length(p);
1073                 memmove(buf, p, strlen(p) + 1);
1074
1075                 buf2 = g_strdup_printf("Fw: %s", buf);
1076                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1077                 
1078                 g_free(buf);
1079                 g_free(buf2);
1080         }
1081
1082         textview = GTK_TEXT_VIEW(compose->text);
1083         textbuf = gtk_text_view_get_buffer(textview);
1084
1085         if (as_attach) {
1086                 gchar *msgfile;
1087
1088                 msgfile = procmsg_get_message_file_path(msginfo);
1089                 if (!is_file_exist(msgfile))
1090                         g_warning("%s: file not exist\n", msgfile);
1091                 else
1092                         compose_attach_append(compose, msgfile, msgfile,
1093                                               "message/rfc822");
1094
1095                 g_free(msgfile);
1096         } else {
1097                 gchar *qmark;
1098                 MsgInfo *full_msginfo;
1099
1100                 full_msginfo = procmsg_msginfo_get_full_info(msginfo);
1101                 if (!full_msginfo)
1102                         full_msginfo = procmsg_msginfo_copy(msginfo);
1103
1104                 if (prefs_common.fw_quotemark &&
1105                     *prefs_common.fw_quotemark)
1106                         qmark = prefs_common.fw_quotemark;
1107                 else
1108                         qmark = "> ";
1109
1110                 compose_quote_fmt(compose, full_msginfo,
1111                                   prefs_common.fw_quotefmt,
1112                                   qmark, body);
1113                 compose_attach_parts(compose, msginfo);
1114
1115                 procmsg_msginfo_free(full_msginfo);
1116         }
1117
1118         if (account->auto_sig)
1119                 compose_insert_sig(compose, FALSE);
1120
1121         if (prefs_common.linewrap_quote)
1122                 compose_wrap_line_all(compose);
1123
1124         gtk_text_buffer_get_start_iter(textbuf, &iter);
1125         gtk_text_buffer_place_cursor(textbuf, &iter);
1126
1127 #if 0 /* NEW COMPOSE GUI */
1128         if (account->protocol != A_NNTP)
1129                 gtk_widget_grab_focus(compose->to_entry);
1130         else
1131                 gtk_widget_grab_focus(compose->newsgroups_entry);
1132 #endif
1133         gtk_widget_grab_focus(compose->header_last->entry);
1134
1135         if (!no_extedit && prefs_common.auto_exteditor)
1136                 compose_exec_ext_editor(compose);
1137         
1138         /*save folder*/
1139         if (msginfo->folder && msginfo->folder->prefs && msginfo->folder->prefs->save_copy_to_folder) {
1140                 gchar *folderidentifier;
1141
1142                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1143                 folderidentifier = folder_item_get_identifier(msginfo->folder);
1144                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
1145                 g_free(folderidentifier);
1146         }
1147
1148         return compose;
1149 }
1150
1151 #undef INSERT_FW_HEADER
1152
1153 Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_list)
1154 {
1155         Compose *compose;
1156         GtkTextView *textview;
1157         GtkTextBuffer *textbuf;
1158         GtkTextIter iter;
1159         GSList *msginfo;
1160         gchar *msgfile;
1161
1162         g_return_val_if_fail(msginfo_list != NULL, NULL);
1163
1164         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next)
1165                 if (((MsgInfo *)msginfo->data)->folder == NULL)
1166                         return NULL;
1167
1168         /* guess account from first selected message */
1169         if (!account && 
1170             !(account = compose_guess_forward_account_from_msginfo
1171                                 (msginfo_list->data)))
1172                 account = cur_account;
1173
1174         g_return_val_if_fail(account != NULL, NULL);
1175
1176         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1177                 MSG_UNSET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_REPLIED);
1178                 MSG_SET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_FORWARDED);
1179         }
1180
1181         compose = compose_create(account, COMPOSE_FORWARD);
1182
1183         textview = GTK_TEXT_VIEW(compose->text);
1184         textbuf = gtk_text_view_get_buffer(textview);
1185
1186         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1187                 msgfile = procmsg_get_message_file_path((MsgInfo *)msginfo->data);
1188                 if (!is_file_exist(msgfile))
1189                         g_warning("%s: file not exist\n", msgfile);
1190                 else
1191                         compose_attach_append(compose, msgfile, msgfile,
1192                                 "message/rfc822");
1193                 g_free(msgfile);
1194         }
1195
1196         if (account->auto_sig)
1197                 compose_insert_sig(compose, FALSE);
1198
1199         if (prefs_common.linewrap_quote)
1200                 compose_wrap_line_all(compose);
1201
1202         gtk_text_buffer_get_start_iter(textbuf, &iter);
1203         gtk_text_buffer_place_cursor(textbuf, &iter);
1204
1205         gtk_widget_grab_focus(compose->header_last->entry);
1206         
1207 #if 0 /* NEW COMPOSE GUI */
1208         if (account->protocol != A_NNTP)
1209                 gtk_widget_grab_focus(compose->to_entry);
1210         else
1211                 gtk_widget_grab_focus(compose->newsgroups_entry);
1212 #endif
1213
1214         return compose;
1215 }
1216
1217 void compose_reedit(MsgInfo *msginfo)
1218 {
1219         Compose *compose;
1220         PrefsAccount *account = NULL;
1221         GtkTextView *textview;
1222         GtkTextBuffer *textbuf;
1223         GtkTextMark *mark;
1224         GtkTextIter iter;
1225         FILE *fp;
1226         gchar buf[BUFFSIZE];
1227
1228         g_return_if_fail(msginfo != NULL);
1229         g_return_if_fail(msginfo->folder != NULL);
1230
1231         if (msginfo->folder->stype == F_QUEUE || msginfo->folder->stype == F_DRAFT) {
1232                 gchar queueheader_buf[BUFFSIZE];
1233                 gint id;
1234
1235                 /* Select Account from queue headers */
1236                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1237                                              sizeof(queueheader_buf), "X-Sylpheed-Account-Id:")) {
1238                         id = atoi(&queueheader_buf[22]);
1239                         account = account_find_from_id(id);
1240                 }
1241                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1242                                              sizeof(queueheader_buf), "NAID:")) {
1243                         id = atoi(&queueheader_buf[5]);
1244                         account = account_find_from_id(id);
1245                 }
1246                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1247                                                     sizeof(queueheader_buf), "MAID:")) {
1248                         id = atoi(&queueheader_buf[5]);
1249                         account = account_find_from_id(id);
1250                 }
1251                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1252                                                                 sizeof(queueheader_buf), "S:")) {
1253                         account = account_find_from_address(queueheader_buf);
1254                 }
1255         } else 
1256                 account = msginfo->folder->folder->account;
1257
1258         if (!account && prefs_common.reedit_account_autosel) {
1259                 gchar from[BUFFSIZE];
1260                 if (!procheader_get_header_from_msginfo(msginfo, from, sizeof(from), "FROM:")){
1261                         extract_address(from);
1262                         account = account_find_from_address(from);
1263                 }
1264         }
1265         if (!account) account = cur_account;
1266         g_return_if_fail(account != NULL);
1267
1268         compose = compose_create(account, COMPOSE_REEDIT);
1269         compose->targetinfo = procmsg_msginfo_copy(msginfo);
1270
1271         if (msginfo->folder->stype == F_QUEUE
1272         ||  msginfo->folder->stype == F_DRAFT) {
1273                 gchar queueheader_buf[BUFFSIZE];
1274
1275                 /* Set message save folder */
1276                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, sizeof(queueheader_buf), "SCF:")) {
1277                         gint startpos = 0;
1278
1279                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1280                         gtk_editable_delete_text(GTK_EDITABLE(compose->savemsg_entry), 0, -1);
1281                         gtk_editable_insert_text(GTK_EDITABLE(compose->savemsg_entry), &queueheader_buf[4], strlen(&queueheader_buf[4]), &startpos);
1282                 }
1283         }
1284         
1285         if (compose_parse_header(compose, msginfo) < 0) return;
1286         compose_reedit_set_entry(compose, msginfo);
1287
1288         textview = GTK_TEXT_VIEW(compose->text);
1289         textbuf = gtk_text_view_get_buffer(textview);
1290         mark = gtk_text_buffer_get_insert(textbuf);
1291         gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
1292
1293         g_signal_handlers_block_by_func(G_OBJECT(textbuf),
1294                                         G_CALLBACK(compose_changed_cb),
1295                                         compose);
1296
1297         if ((fp = procmime_get_first_text_content(msginfo)) == NULL)
1298                 g_warning("Can't get text part\n");
1299         else {
1300                 while (fgets(buf, sizeof(buf), fp) != NULL) {
1301                         strcrchomp(buf);
1302                         gtk_text_buffer_insert(textbuf, &iter, buf, -1);
1303                 }
1304                 fclose(fp);
1305         }
1306         
1307         compose_attach_parts(compose, msginfo);
1308
1309         g_signal_handlers_unblock_by_func(G_OBJECT(textbuf),
1310                                         G_CALLBACK(compose_changed_cb),
1311                                         compose);
1312
1313         gtk_widget_grab_focus(compose->text);
1314
1315         if (prefs_common.auto_exteditor)
1316                 compose_exec_ext_editor(compose);
1317 }
1318
1319 Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo)
1320 {
1321         Compose *compose;
1322         gchar *filename;
1323         GtkItemFactory *ifactory;
1324         FolderItem *item;
1325
1326         g_return_val_if_fail(msginfo != NULL, NULL);
1327
1328         if (!account)
1329                 account = account_get_reply_account(msginfo,
1330                                         prefs_common.reply_account_autosel);
1331         g_return_val_if_fail(account != NULL, NULL);
1332
1333         compose = compose_create(account, COMPOSE_REDIRECT);
1334         ifactory = gtk_item_factory_from_widget(compose->menubar);
1335
1336         compose->replyinfo = NULL;
1337         compose->fwdinfo = NULL;
1338
1339         compose_show_first_last_header(compose, TRUE);
1340
1341         gtk_widget_grab_focus(compose->header_last->entry);
1342
1343         filename = procmsg_get_message_file(msginfo);
1344         if (filename == NULL)
1345                 return NULL;
1346
1347         compose->redirect_filename = filename;
1348         
1349         /* Set save folder */
1350         item = msginfo->folder;
1351         if (item && item->prefs && item->prefs->save_copy_to_folder) {
1352                 gchar *folderidentifier;
1353
1354                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
1355                 folderidentifier = folder_item_get_identifier(item);
1356                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
1357                 g_free(folderidentifier);
1358         }
1359
1360         compose_attach_parts(compose, msginfo);
1361
1362         if (msginfo->subject)
1363                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry),
1364                                    msginfo->subject);
1365         gtk_editable_set_editable(GTK_EDITABLE(compose->subject_entry), FALSE);
1366
1367         compose_quote_fmt(compose, msginfo, "%M", NULL, NULL);
1368         gtk_text_view_set_editable(GTK_TEXT_VIEW(compose->text), TRUE);
1369
1370         ifactory = gtk_item_factory_from_widget(compose->popupmenu);
1371         menu_set_sensitive(ifactory, "/Add...", FALSE);
1372         menu_set_sensitive(ifactory, "/Remove", FALSE);
1373         menu_set_sensitive(ifactory, "/Property...", FALSE);
1374
1375         ifactory = gtk_item_factory_from_widget(compose->menubar);
1376         menu_set_sensitive(ifactory, "/File/Insert file", FALSE);
1377         menu_set_sensitive(ifactory, "/File/Attach file", FALSE);
1378         menu_set_sensitive(ifactory, "/File/Insert signature", FALSE);
1379         menu_set_sensitive(ifactory, "/Edit", FALSE);
1380         menu_set_sensitive(ifactory, "/Message/Save to draft folder", FALSE);
1381         menu_set_sensitive(ifactory, "/Message/Save and keep editing", FALSE);
1382 #if USE_GPGME
1383         menu_set_sensitive(ifactory, "/Message/Sign", FALSE);
1384         menu_set_sensitive(ifactory, "/Message/Encrypt", FALSE);
1385         menu_set_sensitive(ifactory, "/Message/Mode/MIME", FALSE);
1386         menu_set_sensitive(ifactory, "/Message/Mode/Inline", FALSE);
1387 #endif
1388         menu_set_sensitive(ifactory, "/Message/Priority", FALSE);
1389         menu_set_sensitive(ifactory, "/Message/Request Return Receipt", FALSE);
1390         menu_set_sensitive(ifactory, "/Tools/Show ruler", FALSE);
1391         menu_set_sensitive(ifactory, "/Tools/Actions", FALSE);
1392         
1393         gtk_widget_set_sensitive(compose->toolbar->draft_btn, FALSE);
1394         gtk_widget_set_sensitive(compose->toolbar->insert_btn, FALSE);
1395         gtk_widget_set_sensitive(compose->toolbar->attach_btn, FALSE);
1396         gtk_widget_set_sensitive(compose->toolbar->sig_btn, FALSE);
1397         gtk_widget_set_sensitive(compose->toolbar->exteditor_btn, FALSE);
1398         gtk_widget_set_sensitive(compose->toolbar->linewrap_current_btn, FALSE);
1399         gtk_widget_set_sensitive(compose->toolbar->linewrap_all_btn, FALSE);
1400
1401         return compose;
1402 }
1403
1404 GList *compose_get_compose_list(void)
1405 {
1406         return compose_list;
1407 }
1408
1409 void compose_entry_append(Compose *compose, const gchar *address,
1410                           ComposeEntryType type)
1411 {
1412         gchar *header;
1413
1414         if (!address || *address == '\0') return;
1415
1416 #if 0 /* NEW COMPOSE GUI */
1417         switch (type) {
1418         case COMPOSE_CC:
1419                 entry = GTK_ENTRY(compose->cc_entry);
1420                 break;
1421         case COMPOSE_BCC:
1422                 entry = GTK_ENTRY(compose->bcc_entry);
1423                 break;
1424         case COMPOSE_NEWSGROUPS:
1425                 entry = GTK_ENTRY(compose->newsgroups_entry);
1426                 break;
1427         case COMPOSE_TO:
1428         default:
1429                 entry = GTK_ENTRY(compose->to_entry);
1430                 break;
1431         }
1432
1433         text = gtk_entry_get_text(entry);
1434         if (*text != '\0')
1435                 gtk_entry_append_text(entry, ", ");
1436         gtk_entry_append_text(entry, address);
1437 #endif
1438
1439         switch (type) {
1440         case COMPOSE_CC:
1441                 header = N_("Cc:");
1442                 break;
1443         case COMPOSE_BCC:
1444                 header = N_("Bcc:");
1445                 break;
1446         case COMPOSE_REPLYTO:
1447                 header = N_("Reply-To:");
1448                 break;
1449         case COMPOSE_NEWSGROUPS:
1450                 header = N_("Newsgroups:");
1451                 break;
1452         case COMPOSE_FOLLOWUPTO:
1453                 header = N_( "Followup-To:");
1454                 break;
1455         case COMPOSE_TO:
1456         default:
1457                 header = N_("To:");
1458                 break;
1459         }
1460         header = prefs_common.trans_hdr ? gettext(header) : header;
1461
1462         compose_add_header_entry(compose, header, (gchar *)address);
1463 }
1464
1465 void compose_entry_mark_default_to(Compose *compose, const gchar *mailto)
1466 {
1467         static GtkStyle *bold_style = NULL;
1468         static GdkColor bold_color;
1469         GSList *h_list;
1470         GtkEntry *entry;
1471                 
1472         for (h_list = compose->header_list; h_list != NULL; h_list = h_list->next) {
1473                 entry = GTK_ENTRY(((ComposeHeaderEntry *)h_list->data)->entry);
1474                 if (gtk_entry_get_text(entry) && 
1475                     !g_strcasecmp(gtk_entry_get_text(entry), mailto)) {
1476                         gtk_widget_ensure_style(GTK_WIDGET(entry));
1477                         if (!bold_style) {
1478                                 PangoFontDescription *font_desc = NULL;
1479                                 gtkut_convert_int_to_gdk_color
1480                                         (prefs_common.color_new, &bold_color);
1481                                 bold_style = gtk_style_copy(gtk_widget_get_style
1482                                         (GTK_WIDGET(entry)));
1483                                 if (BOLD_FONT)
1484                                         font_desc = pango_font_description_from_string
1485                                                         (BOLD_FONT);
1486                                 if (font_desc) {
1487                                         if (bold_style->font_desc)
1488                                                 pango_font_description_free
1489                                                         (bold_style->font_desc);
1490                                         bold_style->font_desc = font_desc;
1491                                 }
1492                                 bold_style->fg[GTK_STATE_NORMAL] = bold_color;
1493                         }
1494                         gtk_widget_set_style(GTK_WIDGET(entry), bold_style);
1495                 }
1496         }
1497 }
1498
1499 void compose_toolbar_cb(gint action, gpointer data)
1500 {
1501         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1502         Compose *compose = (Compose*)toolbar_item->parent;
1503         
1504         g_return_if_fail(compose != NULL);
1505
1506         switch(action) {
1507         case A_SEND:
1508                 compose_send_cb(compose, 0, NULL);
1509                 break;
1510         case A_SENDL:
1511                 compose_send_later_cb(compose, 0, NULL);
1512                 break;
1513         case A_DRAFT:
1514                 compose_draft_cb(compose, 0, NULL);
1515                 break;
1516         case A_INSERT:
1517                 compose_insert_file_cb(compose, 0, NULL);
1518                 break;
1519         case A_ATTACH:
1520                 compose_attach_cb(compose, 0, NULL);
1521                 break;
1522         case A_SIG:
1523                 compose_insert_sig(compose, FALSE);
1524                 break;
1525         case A_EXTEDITOR:
1526                 compose_ext_editor_cb(compose, 0, NULL);
1527                 break;
1528         case A_LINEWRAP_CURRENT:
1529                 compose_wrap_line(compose);
1530                 break;
1531         case A_LINEWRAP_ALL:
1532                 compose_wrap_line_all(compose);
1533                 break;
1534         case A_ADDRBOOK:
1535                 compose_address_cb(compose, 0, NULL);
1536                 break;
1537 #ifdef USE_ASPELL
1538         case A_CHECK_SPELLING:
1539                 compose_check_all(compose);
1540                 break;
1541 #endif
1542         default:
1543                 break;
1544         }
1545 }
1546
1547 static void compose_entries_set(Compose *compose, const gchar *mailto)
1548 {
1549         gchar *to = NULL;
1550         gchar *cc = NULL;
1551         gchar *bcc = NULL;
1552         gchar *subject = NULL;
1553         gchar *body = NULL;
1554
1555         scan_mailto_url(mailto, &to, &cc, &bcc, &subject, &body);
1556
1557         if (to)
1558                 compose_entry_append(compose, to, COMPOSE_TO);
1559         if (cc)
1560                 compose_entry_append(compose, cc, COMPOSE_CC);
1561         if (bcc)
1562                 compose_entry_append(compose, bcc, COMPOSE_BCC);
1563         if (subject)
1564                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), subject);
1565         if (body) {
1566                 GtkTextView *text = GTK_TEXT_VIEW(compose->text);
1567                 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
1568                 GtkTextMark *mark;
1569                 GtkTextIter iter;
1570
1571                 mark = gtk_text_buffer_get_insert(buffer);
1572                 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
1573
1574                 gtk_text_buffer_insert(buffer, &iter, body, -1);
1575                 gtk_text_buffer_insert(buffer, &iter, "\n", 1);
1576         }
1577
1578         g_free(to);
1579         g_free(cc);
1580         g_free(bcc);
1581         g_free(subject);
1582         g_free(body);
1583 }
1584
1585 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
1586 {
1587         static HeaderEntry hentry[] = {{"Reply-To:",    NULL, TRUE},
1588                                        {"Cc:",          NULL, TRUE},
1589                                        {"References:",  NULL, FALSE},
1590                                        {"Bcc:",         NULL, TRUE},
1591                                        {"Newsgroups:",  NULL, TRUE},
1592                                        {"Followup-To:", NULL, TRUE},
1593                                        {"List-Post:",   NULL, FALSE},
1594                                        {"X-Priority:",  NULL, FALSE},
1595                                        {NULL,           NULL, FALSE}};
1596
1597         enum
1598         {
1599                 H_REPLY_TO      = 0,
1600                 H_CC            = 1,
1601                 H_REFERENCES    = 2,
1602                 H_BCC           = 3,
1603                 H_NEWSGROUPS    = 4,
1604                 H_FOLLOWUP_TO   = 5,
1605                 H_LIST_POST     = 6,
1606                 H_X_PRIORITY    = 7
1607         };
1608
1609         FILE *fp;
1610
1611         g_return_val_if_fail(msginfo != NULL, -1);
1612
1613         if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
1614         procheader_get_header_fields(fp, hentry);
1615         fclose(fp);
1616
1617         if (hentry[H_REPLY_TO].body != NULL) {
1618                 conv_unmime_header_overwrite(hentry[H_REPLY_TO].body);
1619                 compose->replyto = hentry[H_REPLY_TO].body;
1620                 hentry[H_REPLY_TO].body = NULL;
1621         }
1622         if (hentry[H_CC].body != NULL) {
1623                 conv_unmime_header_overwrite(hentry[H_CC].body);
1624                 compose->cc = hentry[H_CC].body;
1625                 hentry[H_CC].body = NULL;
1626         }
1627         if (hentry[H_REFERENCES].body != NULL) {
1628                 if (compose->mode == COMPOSE_REEDIT)
1629                         compose->references = hentry[H_REFERENCES].body;
1630                 else {
1631                         compose->references = compose_parse_references
1632                                 (hentry[H_REFERENCES].body, msginfo->msgid);
1633                         g_free(hentry[H_REFERENCES].body);
1634                 }
1635                 hentry[H_REFERENCES].body = NULL;
1636         }
1637         if (hentry[H_BCC].body != NULL) {
1638                 if (compose->mode == COMPOSE_REEDIT) {
1639                         conv_unmime_header_overwrite(hentry[H_BCC].body);
1640                         compose->bcc = hentry[H_BCC].body;
1641                 } else
1642                         g_free(hentry[H_BCC].body);
1643                 hentry[H_BCC].body = NULL;
1644         }
1645         if (hentry[H_NEWSGROUPS].body != NULL) {
1646                 conv_unmime_header_overwrite(hentry[H_NEWSGROUPS].body);
1647                 compose->newsgroups = hentry[H_NEWSGROUPS].body;
1648                 hentry[H_NEWSGROUPS].body = NULL;
1649         }
1650         if (hentry[H_FOLLOWUP_TO].body != NULL) {
1651                 conv_unmime_header_overwrite(hentry[H_FOLLOWUP_TO].body);
1652                 compose->followup_to = hentry[H_FOLLOWUP_TO].body;
1653                 hentry[H_FOLLOWUP_TO].body = NULL;
1654         }
1655         if (hentry[H_LIST_POST].body != NULL) {
1656                 gchar *to = NULL;
1657
1658                 extract_address(hentry[H_LIST_POST].body);
1659                 if (hentry[H_LIST_POST].body[0] != '\0') {
1660                         scan_mailto_url(hentry[H_LIST_POST].body,
1661                                         &to, NULL, NULL, NULL, NULL);
1662                         if (to) {
1663                                 g_free(compose->ml_post);
1664                                 compose->ml_post = to;
1665                         }
1666                 }
1667                 g_free(hentry[H_LIST_POST].body);
1668                 hentry[H_LIST_POST].body = NULL;
1669         }
1670
1671         /* CLAWS - X-Priority */
1672         if (compose->mode == COMPOSE_REEDIT)
1673                 if (hentry[H_X_PRIORITY].body != NULL) {
1674                         gint priority;
1675                         
1676                         priority = atoi(hentry[H_X_PRIORITY].body);
1677                         g_free(hentry[H_X_PRIORITY].body);
1678                         
1679                         hentry[H_X_PRIORITY].body = NULL;
1680                         
1681                         if (priority < PRIORITY_HIGHEST || 
1682                             priority > PRIORITY_LOWEST)
1683                                 priority = PRIORITY_NORMAL;
1684                         
1685                         compose->priority =  priority;
1686                 }
1687  
1688         if (compose->mode == COMPOSE_REEDIT && msginfo->inreplyto)
1689                 compose->inreplyto = g_strdup(msginfo->inreplyto);
1690         else if (compose->mode != COMPOSE_REEDIT &&
1691                  msginfo->msgid && *msginfo->msgid) {
1692                 compose->inreplyto = g_strdup(msginfo->msgid);
1693
1694                 if (!compose->references) {
1695                         if (msginfo->inreplyto && *msginfo->inreplyto)
1696                                 compose->references =
1697                                         g_strdup_printf("<%s>\n\t<%s>",
1698                                                         msginfo->inreplyto,
1699                                                         msginfo->msgid);
1700                         else
1701                                 compose->references =
1702                                         g_strconcat("<", msginfo->msgid, ">",
1703                                                     NULL);
1704                 }
1705         }
1706
1707         return 0;
1708 }
1709
1710 static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
1711 {
1712         GSList *ref_id_list, *cur;
1713         GString *new_ref;
1714         gchar *new_ref_str;
1715
1716         ref_id_list = references_list_append(NULL, ref);
1717         if (!ref_id_list) return NULL;
1718         if (msgid && *msgid)
1719                 ref_id_list = g_slist_append(ref_id_list, g_strdup(msgid));
1720
1721         for (;;) {
1722                 gint len = 0;
1723
1724                 for (cur = ref_id_list; cur != NULL; cur = cur->next)
1725                         /* "<" + Message-ID + ">" + CR+LF+TAB */
1726                         len += strlen((gchar *)cur->data) + 5;
1727
1728                 if (len > MAX_REFERENCES_LEN) {
1729                         /* remove second message-ID */
1730                         if (ref_id_list && ref_id_list->next &&
1731                             ref_id_list->next->next) {
1732                                 g_free(ref_id_list->next->data);
1733                                 ref_id_list = g_slist_remove
1734                                         (ref_id_list, ref_id_list->next->data);
1735                         } else {
1736                                 slist_free_strings(ref_id_list);
1737                                 g_slist_free(ref_id_list);
1738                                 return NULL;
1739                         }
1740                 } else
1741                         break;
1742         }
1743
1744         new_ref = g_string_new("");
1745         for (cur = ref_id_list; cur != NULL; cur = cur->next) {
1746                 if (new_ref->len > 0)
1747                         g_string_append(new_ref, "\n\t");
1748                 g_string_sprintfa(new_ref, "<%s>", (gchar *)cur->data);
1749         }
1750
1751         slist_free_strings(ref_id_list);
1752         g_slist_free(ref_id_list);
1753
1754         new_ref_str = new_ref->str;
1755         g_string_free(new_ref, FALSE);
1756
1757         return new_ref_str;
1758 }
1759
1760 static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
1761                                 const gchar *fmt, const gchar *qmark,
1762                                 const gchar *body)
1763 {
1764         static MsgInfo dummyinfo;
1765         gchar *quote_str = NULL;
1766         gchar *buf;
1767         gchar *p, *lastp;
1768         gint len;
1769         const gchar *trimmed_body = body;
1770         
1771         if (!msginfo)
1772                 msginfo = &dummyinfo;
1773
1774         if (qmark != NULL) {
1775                 quote_fmt_init(msginfo, NULL, NULL);
1776                 quote_fmt_scan_string(qmark);
1777                 quote_fmt_parse();
1778
1779                 buf = quote_fmt_get_buffer();
1780                 if (buf == NULL)
1781                         alertpanel_error(_("Quote mark format error."));
1782                 else
1783                         Xstrdup_a(quote_str, buf, return NULL)
1784         }
1785
1786         if (fmt && *fmt != '\0') {
1787                 while (trimmed_body && strlen(trimmed_body) > 1
1788                         && trimmed_body[0]=='\n')
1789                         *trimmed_body++;
1790
1791                 quote_fmt_init(msginfo, quote_str, trimmed_body);
1792                 quote_fmt_scan_string(fmt);
1793                 quote_fmt_parse();
1794
1795                 buf = quote_fmt_get_buffer();
1796                 if (buf == NULL) {
1797                         alertpanel_error(_("Message reply/forward format error."));
1798                         return NULL;
1799                 }
1800         } else
1801                 buf = "";
1802
1803         for (p = buf; *p != '\0'; ) {
1804                 GtkTextView *text = GTK_TEXT_VIEW(compose->text);
1805                 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
1806                 GtkTextMark *mark;
1807                 GtkTextIter iter;
1808
1809                 g_signal_handlers_block_by_func(G_OBJECT(buffer),
1810                                         G_CALLBACK(compose_changed_cb),
1811                                         compose);
1812                 
1813                 mark = gtk_text_buffer_get_insert(buffer);
1814                 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
1815
1816                 lastp = strchr(p, '\n');
1817                 len = lastp ? lastp - p + 1 : -1;
1818                 gtk_text_buffer_insert(buffer, &iter, p, len);
1819
1820                 g_signal_handlers_unblock_by_func(G_OBJECT(buffer),
1821                                         G_CALLBACK(compose_changed_cb),
1822                                         compose);
1823                 
1824                 if (lastp)
1825                         p = lastp + 1;
1826                 else
1827                         break;
1828         }
1829
1830         return buf;
1831 }
1832
1833 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
1834                                     gboolean to_all, gboolean to_ml,
1835                                     gboolean to_sender,
1836                                     gboolean followup_and_reply_to)
1837 {
1838         GSList *cc_list = NULL;
1839         GSList *cur;
1840         gchar *from = NULL;
1841         gchar *replyto = NULL;
1842         GHashTable *to_table;
1843
1844         g_return_if_fail(compose->account != NULL);
1845         g_return_if_fail(msginfo != NULL);
1846
1847         if (compose->account->protocol != A_NNTP) {
1848                 if (!compose->replyto && to_ml && compose->ml_post
1849                     && !(msginfo->folder && msginfo->folder->prefs->enable_default_reply_to))
1850                         compose_entry_append(compose,
1851                                            compose->ml_post,
1852                                            COMPOSE_TO);
1853                 else if (!(to_all || to_sender)
1854                          && msginfo->folder
1855                          && msginfo->folder->prefs->enable_default_reply_to) {
1856                         compose_entry_append(compose,
1857                             msginfo->folder->prefs->default_reply_to,
1858                             COMPOSE_TO);
1859                 } else
1860                         compose_entry_append(compose,
1861                                  (compose->replyto && !to_sender)
1862                                   ? compose->replyto :
1863                                   msginfo->from ? msginfo->from : "",
1864                                   COMPOSE_TO);
1865         } else {
1866                 if (to_sender || (compose->followup_to && 
1867                         !strncmp(compose->followup_to, "poster", 6)))
1868                         compose_entry_append
1869                                 (compose, 
1870                                  (compose->replyto ? compose->replyto :
1871                                         msginfo->from ? msginfo->from : ""),
1872                                  COMPOSE_TO);
1873                                  
1874                 else if (followup_and_reply_to || to_all) {
1875                         compose_entry_append
1876                                 (compose,
1877                                  (compose->replyto ? compose->replyto :
1878                                  msginfo->from ? msginfo->from : ""),
1879                                  COMPOSE_TO);                           
1880                 
1881                         compose_entry_append
1882                                 (compose,
1883                                  compose->followup_to ? compose->followup_to :
1884                                  compose->newsgroups ? compose->newsgroups : "",
1885                                  COMPOSE_NEWSGROUPS);
1886                 } 
1887                 else 
1888                         compose_entry_append
1889                                 (compose,
1890                                  compose->followup_to ? compose->followup_to :
1891                                  compose->newsgroups ? compose->newsgroups : "",
1892                                  COMPOSE_NEWSGROUPS);
1893         }
1894
1895         if (msginfo->subject && *msginfo->subject) {
1896                 gchar *buf, *buf2;
1897                 guchar *p;
1898
1899                 buf = p = g_strdup(msginfo->subject);
1900                 p += subject_get_prefix_length(p);
1901                 memmove(buf, p, strlen(p) + 1);
1902
1903                 buf2 = g_strdup_printf("Re: %s", buf);
1904                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1905
1906                 g_free(buf2);
1907                 g_free(buf);
1908         } else
1909                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
1910
1911         if (to_ml && compose->ml_post) return;
1912         if (!to_all || compose->account->protocol == A_NNTP) return;
1913
1914         if (compose->replyto) {
1915                 Xstrdup_a(replyto, compose->replyto, return);
1916                 extract_address(replyto);
1917         }
1918         if (msginfo->from) {
1919                 Xstrdup_a(from, msginfo->from, return);
1920                 extract_address(from);
1921         }
1922
1923         if (replyto && from)
1924                 cc_list = address_list_append_with_comments(cc_list, from);
1925         if (to_all && msginfo->folder && 
1926             msginfo->folder->prefs->enable_default_reply_to)
1927                 cc_list = address_list_append_with_comments(cc_list,
1928                                 msginfo->folder->prefs->default_reply_to);
1929         cc_list = address_list_append_with_comments(cc_list, msginfo->to);
1930         cc_list = address_list_append_with_comments(cc_list, compose->cc);
1931
1932         to_table = g_hash_table_new(g_str_hash, g_str_equal);
1933         if (replyto)
1934                 g_hash_table_insert(to_table, g_strdup(replyto), GINT_TO_POINTER(1));
1935         if (compose->account)
1936                 g_hash_table_insert(to_table, g_strdup(compose->account->address),
1937                                     GINT_TO_POINTER(1));
1938
1939         /* remove address on To: and that of current account */
1940         for (cur = cc_list; cur != NULL; ) {
1941                 GSList *next = cur->next;
1942                 gchar *addr;
1943
1944                 addr = g_strdup(cur->data);
1945                 extract_address(addr);
1946
1947                 if (GPOINTER_TO_INT(g_hash_table_lookup(to_table, addr)) == 1)
1948                         cc_list = g_slist_remove(cc_list, cur->data);
1949                 else
1950                         g_hash_table_insert(to_table, addr, GINT_TO_POINTER(1));
1951
1952                 cur = next;
1953         }
1954         hash_free_strings(to_table);
1955         g_hash_table_destroy(to_table);
1956
1957         if (cc_list) {
1958                 for (cur = cc_list; cur != NULL; cur = cur->next)
1959                         compose_entry_append(compose, (gchar *)cur->data,
1960                                              COMPOSE_CC);
1961                 slist_free_strings(cc_list);
1962                 g_slist_free(cc_list);
1963         }
1964
1965 }
1966
1967 #define SET_ENTRY(entry, str) \
1968 { \
1969         if (str && *str) \
1970                 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
1971 }
1972
1973 #define SET_ADDRESS(type, str) \
1974 { \
1975         if (str && *str) \
1976                 compose_entry_append(compose, str, type); \
1977 }
1978
1979 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
1980 {
1981         g_return_if_fail(msginfo != NULL);
1982
1983         SET_ENTRY(subject_entry, msginfo->subject);
1984         SET_ADDRESS(COMPOSE_TO, msginfo->to);
1985         SET_ADDRESS(COMPOSE_CC, compose->cc);
1986         SET_ADDRESS(COMPOSE_BCC, compose->bcc);
1987         SET_ADDRESS(COMPOSE_REPLYTO, compose->replyto);
1988         SET_ADDRESS(COMPOSE_NEWSGROUPS, compose->newsgroups);
1989         SET_ADDRESS(COMPOSE_FOLLOWUPTO, compose->followup_to);
1990
1991         compose_update_priority_menu_item(compose);
1992 #if USE_GPGME   
1993         compose_update_gnupg_mode_menu_item(compose);
1994 #endif
1995         compose_show_first_last_header(compose, TRUE);
1996
1997 }
1998
1999 #undef SET_ENTRY
2000 #undef SET_ADDRESS
2001
2002 static void compose_insert_sig(Compose *compose, gboolean replace)
2003 {
2004         GtkTextView *text = GTK_TEXT_VIEW(compose->text);
2005         GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
2006         GtkTextMark *mark;
2007         GtkTextIter iter;
2008         gint cur_pos;
2009
2010         
2011         g_return_if_fail(compose->account != NULL);
2012
2013         g_signal_handlers_block_by_func(G_OBJECT(buffer),
2014                                         G_CALLBACK(compose_changed_cb),
2015                                         compose);
2016         
2017         mark = gtk_text_buffer_get_insert(buffer);
2018         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
2019         cur_pos = gtk_text_iter_get_offset (&iter);
2020
2021         gtk_text_buffer_get_end_iter(buffer, &iter);
2022
2023         if (replace && compose->sig_str) {
2024                 gboolean found;
2025                 GtkTextIter first_iter, start_iter, end_iter;
2026
2027                 gtk_text_buffer_get_start_iter(buffer, &first_iter);
2028
2029                 if (compose->sig_str[0] == '\0')
2030                         found = FALSE;
2031                 else
2032                         found = gtk_text_iter_forward_search(&first_iter,
2033                                                              compose->sig_str,
2034                                                              GTK_TEXT_SEARCH_TEXT_ONLY,
2035                                                              &start_iter, &end_iter,
2036                                                              NULL);
2037
2038                 if (found) {
2039                         gtk_text_buffer_delete(buffer, &start_iter, &end_iter);
2040                         iter = start_iter;
2041                 }
2042         }
2043
2044         g_free(compose->sig_str);
2045         compose->sig_str = compose_get_signature_str(compose);
2046         if (!compose->sig_str || (replace && !compose->account->auto_sig))
2047                 compose->sig_str = g_strdup("");
2048
2049         gtk_text_buffer_insert(buffer, &iter, compose->sig_str, -1);
2050
2051         if (cur_pos > gtk_text_buffer_get_char_count (buffer))
2052                 cur_pos = gtk_text_buffer_get_char_count (buffer);
2053
2054         gtk_text_buffer_get_iter_at_offset (buffer, &iter, cur_pos);
2055         gtk_text_buffer_place_cursor (buffer, &iter);
2056
2057         g_signal_handlers_unblock_by_func(G_OBJECT(buffer),
2058                                         G_CALLBACK(compose_changed_cb),
2059                                         compose);
2060         
2061 }
2062
2063 static gchar *compose_get_signature_str(Compose *compose)
2064 {
2065         gchar *sig_body = NULL;
2066         gchar *sig_str = NULL;
2067
2068         g_return_val_if_fail(compose->account != NULL, NULL);
2069
2070         if (!compose->account->sig_path)
2071                 return NULL;
2072
2073         if (compose->account->sig_type == SIG_FILE) {
2074                 if (!is_file_or_fifo_exist(compose->account->sig_path)) {
2075                         g_warning("can't open signature file: %s\n",
2076                                   compose->account->sig_path);
2077                         return NULL;
2078                 }
2079         }
2080
2081         if (compose->account->sig_type == SIG_COMMAND)
2082                 sig_body = get_command_output(compose->account->sig_path);
2083         else {
2084                 gchar *tmp;
2085
2086                 tmp = file_read_to_str(compose->account->sig_path);
2087                 if (!tmp)
2088                         return NULL;
2089                 sig_body = normalize_newlines(tmp);
2090                 g_free(tmp);
2091         }
2092
2093         if (compose->account->sig_sep)
2094                 sig_str = g_strconcat("\n\n", compose->account->sig_sep, "\n", sig_body,
2095                                       NULL);
2096         else
2097                 sig_str = g_strconcat("\n\n", sig_body, NULL);
2098
2099         g_free(sig_body);
2100         
2101         return sig_str;
2102 }
2103
2104 static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *file)
2105 {
2106         GtkTextView *text;
2107         GtkTextBuffer *buffer;
2108         GtkTextMark *mark;
2109         GtkTextIter iter;
2110         gchar buf[BUFFSIZE];
2111         gint len;
2112         FILE *fp;
2113         gboolean badtxt = FALSE;
2114
2115         g_return_val_if_fail(file != NULL, COMPOSE_INSERT_NO_FILE);
2116
2117         if ((fp = fopen(file, "rb")) == NULL) {
2118                 FILE_OP_ERROR(file, "fopen");
2119                 return COMPOSE_INSERT_READ_ERROR;
2120         }
2121
2122         text = GTK_TEXT_VIEW(compose->text);
2123         buffer = gtk_text_view_get_buffer(text);
2124         mark = gtk_text_buffer_get_insert(buffer);
2125         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
2126
2127         while (fgets(buf, sizeof(buf), fp) != NULL) {
2128 #warning FIXME_GTK2
2129 #if 1 /* FIXME_GTK2 */
2130                 const gchar *cur_encoding = conv_get_current_charset_str();
2131                 gchar *str = conv_codeset_strdup(buf, cur_encoding, CS_UTF_8);
2132
2133                 if (!str) continue;
2134
2135                 /* strip <CR> if DOS/Windows file,
2136                    replace <CR> with <LF> if Macintosh file. */
2137                 strcrchomp(str);
2138                 len = strlen(str);
2139                 if (len > 0 && str[len - 1] != '\n') {
2140                         while (--len >= 0)
2141                                 if (str[len] == '\r') str[len] = '\n';
2142                 }
2143                 gtk_text_buffer_insert(buffer, &iter, str, -1);
2144
2145                 g_free (str);
2146 #endif /* FIXME_GTK2 */
2147         }
2148
2149         fclose(fp);
2150
2151         if (badtxt)
2152                 return COMPOSE_INSERT_INVALID_CHARACTER;
2153         else 
2154                 return COMPOSE_INSERT_SUCCESS;
2155 }
2156
2157 static void compose_attach_append(Compose *compose, const gchar *file,
2158                                   const gchar *filename,
2159                                   const gchar *content_type)
2160 {
2161         AttachInfo *ainfo;
2162         gchar *text[N_ATTACH_COLS];
2163         FILE *fp;
2164         off_t size;
2165         gint row;
2166
2167         if (!is_file_exist(file)) {
2168                 g_warning("File %s doesn't exist\n", file);
2169                 return;
2170         }
2171         if ((size = get_file_size(file)) < 0) {
2172                 g_warning("Can't get file size of %s\n", file);
2173                 return;
2174         }
2175         if (size == 0) {
2176                 alertpanel_notice(_("File %s is empty."), file);
2177                 return;
2178         }
2179         if ((fp = fopen(file, "rb")) == NULL) {
2180                 alertpanel_error(_("Can't read %s."), file);
2181                 return;
2182         }
2183         fclose(fp);
2184
2185 #if 0 /* NEW COMPOSE GUI */
2186         if (!compose->use_attach) {
2187                 GtkItemFactory *ifactory;
2188
2189                 ifactory = gtk_item_factory_from_widget(compose->menubar);
2190                 menu_set_active(ifactory, "/View/Attachment", TRUE);
2191         }
2192 #endif
2193         ainfo = g_new0(AttachInfo, 1);
2194         ainfo->file = g_strdup(file);
2195
2196         if (content_type) {
2197                 ainfo->content_type = g_strdup(content_type);
2198                 if (!strcasecmp(content_type, "message/rfc822")) {
2199                         MsgInfo *msginfo;
2200                         MsgFlags flags = {0, 0};
2201                         const gchar *name;
2202
2203                         if (procmime_get_encoding_for_file(file) == ENC_7BIT)
2204                                 ainfo->encoding = ENC_7BIT;
2205                         else
2206                                 ainfo->encoding = ENC_8BIT;
2207
2208                         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
2209                         if (msginfo && msginfo->subject)
2210                                 name = msginfo->subject;
2211                         else
2212                                 name = g_basename(filename ? filename : file);
2213
2214                         ainfo->name = g_strdup_printf(_("Message: %s"), name);
2215
2216                         procmsg_msginfo_free(msginfo);
2217                 } else {
2218                         if (!g_strncasecmp(content_type, "text", 4))
2219                                 ainfo->encoding =
2220                                         procmime_get_encoding_for_file(file);
2221                         else
2222                                 ainfo->encoding = ENC_BASE64;
2223                         ainfo->name = g_strdup
2224                                 (g_basename(filename ? filename : file));
2225                 }
2226         } else {
2227                 ainfo->content_type = procmime_get_mime_type(file);
2228                 if (!ainfo->content_type) {
2229                         ainfo->content_type =
2230                                 g_strdup("application/octet-stream");
2231                         ainfo->encoding = ENC_BASE64;
2232                 } else if (!g_strncasecmp(ainfo->content_type, "text", 4))
2233                         ainfo->encoding = procmime_get_encoding_for_file(file);
2234                 else
2235                         ainfo->encoding = ENC_BASE64;
2236                 ainfo->name = g_strdup(g_basename(filename ? filename : file)); 
2237         }
2238         ainfo->size = size;
2239
2240         text[COL_MIMETYPE] = ainfo->content_type;
2241         text[COL_SIZE] = to_human_readable(size);
2242         text[COL_NAME] = ainfo->name;
2243
2244         row = gtk_clist_append(GTK_CLIST(compose->attach_clist), text);
2245         gtk_clist_set_row_data(GTK_CLIST(compose->attach_clist), row, ainfo);
2246 }
2247
2248 #ifdef USE_GPGME
2249 static void compose_use_signing(Compose *compose, gboolean use_signing)
2250 {
2251         GtkItemFactory *ifactory;
2252         GtkWidget *menuitem = NULL;
2253
2254         compose->use_signing = use_signing;
2255         ifactory = gtk_item_factory_from_widget(compose->menubar);
2256         menuitem = gtk_item_factory_get_item
2257                 (ifactory, "/Message/Sign");
2258         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), 
2259                                        use_signing);
2260         compose_update_gnupg_mode_menu_item(compose);
2261 }
2262 #endif /* USE_GPGME */
2263
2264 #define NEXT_PART_NOT_CHILD(info)  \
2265 {  \
2266         node = info->node;  \
2267         while (node->children)  \
2268                 node = g_node_last_child(node);  \
2269         info = procmime_mimeinfo_next((MimeInfo *)node->data);  \
2270 }
2271
2272 static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
2273 {
2274         MimeInfo *mimeinfo;
2275         MimeInfo *child;
2276         MimeInfo *firsttext = NULL;
2277         MimeInfo *encrypted = NULL;
2278         GNode    *node;
2279         gchar *outfile;
2280         const gchar *partname = NULL;
2281
2282         mimeinfo = procmime_scan_message(msginfo);
2283         if (!mimeinfo) return;
2284
2285         if (mimeinfo->node->children == NULL) {
2286                 procmime_mimeinfo_free_all(mimeinfo);
2287                 return;
2288         }
2289
2290         /* find first content part */
2291         child = (MimeInfo *) mimeinfo->node->children->data;
2292         while (child && child->node->children && (child->type == MIMETYPE_MULTIPART))
2293                 child = (MimeInfo *)child->node->children->data;
2294
2295         if (child->type == MIMETYPE_TEXT) {
2296                 firsttext = child;
2297                 debug_print("First text part found\n");
2298         } else if (compose->mode == COMPOSE_REEDIT &&
2299                  child->type == MIMETYPE_APPLICATION &&
2300                  !strcasecmp(child->subtype, "pgp-encrypted")) {
2301                 AlertValue val;
2302                 val = alertpanel(_("Encrypted message"),
2303                                  _("Cannot re-edit an encrypted message. \n"
2304                                    "Discard encrypted part?"),
2305                                  _("Yes"), _("No"), NULL);
2306                 if (val == G_ALERTDEFAULT) 
2307                         encrypted = (MimeInfo *)child->node->parent->data;
2308         }
2309      
2310         child = (MimeInfo *) mimeinfo->node->children->data;
2311         while (child != NULL) {
2312                 if (child == encrypted) {
2313                         /* skip this part of tree */
2314                         NEXT_PART_NOT_CHILD(child);
2315                         continue;
2316                 }
2317
2318                 if (child->type == MIMETYPE_MULTIPART) {
2319                         /* get the actual content */
2320                         child = procmime_mimeinfo_next(child);
2321                         continue;
2322                 }
2323                     
2324                 if (child == firsttext) {
2325                         child = procmime_mimeinfo_next(child);
2326                         continue;
2327                 }
2328
2329                 if ((compose->mode == COMPOSE_REEDIT || 
2330                      compose->mode == COMPOSE_FORWARD_INLINE ||
2331                      compose->mode == COMPOSE_FORWARD )  &&
2332                     (child->type == MIMETYPE_APPLICATION) && 
2333                     !strcmp(child->subtype, "pgp-signature")) {
2334 #ifdef USE_GPGME
2335                         if (compose->mode == COMPOSE_REEDIT) {
2336                                 compose->gnupg_mode  = GNUPG_MODE_DETACH;
2337                                 compose_use_signing(compose, TRUE);
2338                         }
2339 #endif
2340                         child = procmime_mimeinfo_next(child);
2341                         continue;
2342                 }
2343                 outfile = procmime_get_tmp_file_name(child);
2344                 if (procmime_get_part(outfile, child) < 0)
2345                         g_warning("Can't get the part of multipart message.");
2346                 else {
2347                         gchar *content_type;
2348
2349                         content_type = g_strdup_printf("%s/%s", procmime_get_type_str(child->type), child->subtype);
2350                         partname = procmime_mimeinfo_get_parameter(child, "name");
2351                         if (partname == NULL)
2352                                 partname = "";
2353                         compose_attach_append(compose, outfile, 
2354                                               partname, content_type);
2355                         g_free(content_type);
2356                 }
2357                 g_free(outfile);
2358                 NEXT_PART_NOT_CHILD(child);
2359         }
2360         procmime_mimeinfo_free_all(mimeinfo);
2361 }
2362
2363   
2364 #define CHAR_BUF_SIZE 8
2365 #undef NEXT_PART_NOT_CHILD
2366
2367 #define GET_CHAR(iter_p, buf, len)                                           \
2368 {                                                                            \
2369         GtkTextIter end_iter;                                                \
2370         gchar *tmp;                                                          \
2371         end_iter = *iter_p;                                                  \
2372         gtk_text_iter_forward_char(&end_iter);                               \
2373         tmp = gtk_text_buffer_get_text(textbuf, iter_p, &end_iter, FALSE);   \
2374         if (tmp) {                                                           \
2375                 glong items_read, items_witten;                              \
2376                 GError *error = NULL;                                        \
2377                 gunichar *wide_char;                                         \
2378                 strncpy2(buf, tmp, CHAR_BUF_SIZE);                           \
2379                 wide_char = g_utf8_to_ucs4(tmp, -1,                          \
2380                                            &items_read, &items_witten,       \
2381                                            &error);                          \
2382                 if (error != NULL) {                                         \
2383                         g_warning("%s\n", error->message);                   \
2384                         g_error_free(error);                                 \
2385                 }                                                            \
2386                 len = wide_char && g_unichar_iswide(*wide_char) ? 2 : 1;     \
2387                 g_free(wide_char);                                           \
2388         } else {                                                             \
2389                 buf[0] = '\0';                                               \
2390                 len = 1;                                                     \
2391         }                                                                    \
2392         g_free(tmp);                                                         \
2393 }
2394
2395 #define DISP_WIDTH(len) \
2396         ((len > 2 && conv_get_current_charset() == C_UTF_8) ? 2 : \
2397          (len == 2 && conv_get_current_charset() == C_UTF_8) ? 1 : len)
2398
2399 #define SPACE_CHARS     " \t"
2400
2401 #warning FIXME_GTK2
2402 static void compose_wrap_line(Compose *compose)
2403 {
2404         GtkTextView *text = GTK_TEXT_VIEW(compose->text);
2405         GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
2406         GtkTextMark *mark;
2407         GtkTextIter insert_iter, iter;
2408         gint ch_len, last_ch_len;
2409         gchar cbuf[CHAR_BUF_SIZE], last_ch;
2410         guint text_len;
2411         gint p_start, p_end;
2412         gint line_pos, cur_pos;
2413         gint line_len, cur_len;
2414         gboolean line_end, quoted;
2415
2416         text_len = gtk_text_buffer_get_char_count(textbuf);
2417         mark = gtk_text_buffer_get_insert(textbuf);
2418         gtk_text_buffer_get_iter_at_mark(textbuf, &insert_iter, mark);
2419         cur_pos = gtk_text_iter_get_offset(&insert_iter);
2420         GET_CHAR(&insert_iter, cbuf, ch_len);
2421         if ((ch_len == 1 && *cbuf == '\n') || cur_pos == text_len) {
2422                 GtkTextIter prev_iter;
2423                 if (cur_pos == 0)
2424                         return; /* on the paragraph mark */
2425                 prev_iter = insert_iter;
2426                 gtk_text_iter_backward_char(&prev_iter);
2427                 GET_CHAR(&prev_iter, cbuf, ch_len);
2428                 if (ch_len == 1 && *cbuf == '\n')
2429                         return; /* on the paragraph mark */
2430         }
2431
2432         /* find paragraph start. */
2433         line_end = quoted = FALSE;
2434         for (iter = insert_iter; gtk_text_iter_backward_char(&iter);) {
2435                 GET_CHAR(&iter, cbuf, ch_len);
2436                 if (ch_len == 1 && *cbuf == '\n') {
2437                         if (quoted)
2438                                 return; /* quoted part */
2439                         if (line_end) {
2440                                 gtk_text_iter_forward_chars(&iter, 2);
2441                                 break;
2442                         }
2443                         line_end = TRUE;
2444                 } else {
2445                         if (ch_len == 1 
2446                             && strchr(prefs_common.quote_chars, *cbuf))
2447                                 quoted = 1;
2448                         else if (ch_len != 1 || !isspace(*(guchar *)cbuf))
2449                                 quoted = 0;
2450
2451                         line_end = FALSE;
2452                 }
2453         }
2454         p_start = gtk_text_iter_get_offset(&iter);
2455
2456         /* find paragraph end. */
2457         line_end = FALSE;
2458         for (iter = insert_iter; gtk_text_iter_forward_char(&iter);) {
2459                 GET_CHAR(&iter, cbuf, ch_len);
2460                 if (ch_len == 1 && *cbuf == '\n') {
2461                         if (line_end) {
2462                                 p_end -= 1;
2463                                 gtk_text_iter_backward_char(&iter);
2464                                 break;
2465                         }
2466                         line_end = TRUE;
2467                 } else {
2468                         if (line_end && ch_len == 1 &&
2469                             strchr(prefs_common.quote_chars, *cbuf))
2470                                 return; /* quoted part */
2471
2472                         line_end = FALSE;
2473                 }
2474         }
2475         p_end = gtk_text_iter_get_offset(&iter);
2476
2477         if (p_end >= text_len)
2478                 p_end = text_len;
2479
2480         if (p_start >= p_end)
2481                 return;
2482
2483         line_len = cur_len = 0;
2484         last_ch_len = 0;
2485         last_ch = '\0';
2486         line_pos = p_start;
2487         for (cur_pos = p_start; cur_pos < p_end; cur_pos++) {
2488                 gboolean space = FALSE;
2489
2490                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter, cur_pos);
2491
2492                 GET_CHAR(&iter, cbuf, ch_len);
2493
2494                 if (ch_len < 0) {
2495                         cbuf[0] = '\0';
2496                         ch_len = 1;
2497                 }
2498
2499                 if (ch_len == 1 && isspace(*(guchar *)cbuf))
2500                         space = 1;
2501
2502                 if (ch_len == 1 && *cbuf == '\n') {
2503                         gboolean replace = FALSE;
2504                         GtkTextIter next_iter = iter;
2505
2506                         gtk_text_iter_forward_char(&next_iter);
2507
2508                         if (last_ch_len == 1 && !isspace((guchar)last_ch)) {
2509                                 if (cur_pos + 1 < p_end) {
2510                                         GET_CHAR(&next_iter, cbuf, ch_len);
2511                                         if (ch_len == 1 &&
2512                                             !isspace(*(guchar *)cbuf))
2513                                                 replace = TRUE;
2514                                 }
2515                         }
2516                         gtk_text_buffer_delete(textbuf, &iter, &next_iter);
2517                         if (replace) {
2518                                 gtk_text_buffer_insert(textbuf, &iter, " ", 1);
2519                                 space = TRUE;
2520                         }
2521                         else {
2522                                 p_end--;
2523                                 cur_pos--;
2524                                 gtk_text_buffer_get_iter_at_offset
2525                                         (textbuf, &iter, cur_pos);
2526                                 continue;
2527                         }
2528                 }
2529
2530                 last_ch_len = ch_len;
2531                 last_ch = *cbuf;
2532
2533                 if (space) {
2534                         line_pos = cur_pos + 1;
2535                         line_len = cur_len + ch_len;
2536                 }
2537
2538                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter, line_pos);
2539
2540                 if (cur_len + ch_len > prefs_common.linewrap_len &&
2541                     line_len > 0) {
2542                         gint tlen = ch_len;
2543                         GtkTextIter prev_iter = iter;
2544
2545                         gtk_text_iter_backward_char(&prev_iter);
2546
2547                         GET_CHAR(&prev_iter, cbuf, ch_len);
2548                         if (ch_len == 1 && isspace(*(guchar *)cbuf)) {
2549                                 gtk_text_buffer_delete(textbuf, &prev_iter, &iter);
2550                                 iter = prev_iter;
2551                                 p_end--;
2552                                 cur_pos--;
2553                                 line_pos--;
2554                                 cur_len--;
2555                                 line_len--;
2556                         }
2557                         ch_len = tlen;
2558
2559                         gtk_text_buffer_insert(textbuf, &iter, "\n", 1);
2560                         p_end++;
2561                         cur_pos++;
2562                         line_pos++;
2563                         cur_len = cur_len - line_len + ch_len;
2564                         line_len = 0;
2565                         continue;
2566                 }
2567
2568                 if (ch_len > 1) {
2569                         line_pos = cur_pos + 1;
2570                         line_len = cur_len + ch_len;
2571                 }
2572                 cur_len += ch_len;
2573         }
2574 }
2575
2576 #undef WRAP_DEBUG
2577 #ifdef WRAP_DEBUG
2578 /* Darko: used when I debug wrapping */
2579 void dump_text(GtkTextBuffer textbuf, int pos, int tlen, int breakoncr)
2580 {
2581         gint i, clen;
2582         gchar cbuf[CHAR_BUF_SIZE];
2583
2584         printf("%d [", pos);
2585         gtk_text_buffer_get_iter_at_offset(textbuf, &iter, pos);
2586         gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter, pos + tlen);
2587         for (; gtk_text_iter_forward_char(&iter) &&
2588                      gtk_text_iter_compare(&iter, &end_iter) < 0;)
2589                 GET_CHAR(&iter, cbuf, clen);
2590                 if (clen < 0) break;
2591                 if (breakoncr && clen == 1 && cbuf[0] == '\n')
2592                         break;
2593                 fwrite(cbuf, clen, 1, stdout);
2594         }
2595         printf("]\n");
2596 }
2597 #endif
2598
2599 typedef enum {
2600         WAIT_FOR_SPACE,
2601         WAIT_FOR_INDENT_CHAR,
2602         WAIT_FOR_INDENT_CHAR_OR_SPACE
2603 } IndentState;
2604
2605 /* return indent length, we allow:
2606    > followed by spaces/tabs
2607    | followed by spaces/tabs
2608    uppercase characters immediately followed by >,
2609    and the repeating sequences of the above */
2610 /* return indent length */
2611 static guint get_indent_length(GtkTextBuffer *textbuf, guint start_pos, guint text_len)
2612 {
2613         guint i_len = 0;
2614         guint i, ch_len, alnum_cnt = 0;
2615         IndentState state = WAIT_FOR_INDENT_CHAR;
2616         gchar cbuf[CHAR_BUF_SIZE];
2617         gboolean is_space;
2618         gboolean is_indent;
2619
2620         if (prefs_common.quote_chars == NULL) {
2621                 return 0 ;
2622         }
2623
2624         for (i = start_pos; i < text_len; i++) {
2625                 GtkTextIter iter;
2626
2627                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter, i);
2628                 GET_CHAR(&iter, cbuf, ch_len);
2629                 if (ch_len > 1)
2630                         break;
2631
2632                 if (cbuf[0] == '\n')
2633                         break;
2634
2635                 is_indent = strchr(prefs_common.quote_chars, cbuf[0]) ? TRUE : FALSE;
2636                 is_space = strchr(SPACE_CHARS, cbuf[0]) ? TRUE : FALSE;
2637
2638                 switch (state) {
2639                 case WAIT_FOR_SPACE:
2640                         if (is_space == FALSE)
2641                                 goto out;
2642                         state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
2643                         break;
2644                 case WAIT_FOR_INDENT_CHAR_OR_SPACE:
2645                         if (is_indent == FALSE && is_space == FALSE &&
2646                             !isupper((guchar)cbuf[0]))
2647                                 goto out;
2648                         if (is_space == TRUE) {
2649                                 alnum_cnt = 0;
2650                                 state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
2651                         } else if (is_indent == TRUE) {
2652                                 alnum_cnt = 0;
2653                                 state = WAIT_FOR_SPACE;
2654                         } else {
2655                                 alnum_cnt++;
2656                                 state = WAIT_FOR_INDENT_CHAR;
2657                         }
2658                         break;
2659                 case WAIT_FOR_INDENT_CHAR:
2660                         if (is_indent == FALSE && !isupper((guchar)cbuf[0]))
2661                                 goto out;
2662                         if (is_indent == TRUE) {
2663                                 if (alnum_cnt > 0 
2664                                     && !strchr(prefs_common.quote_chars, cbuf[0]))
2665                                         goto out;
2666                                 alnum_cnt = 0;
2667                                 state = WAIT_FOR_SPACE;
2668                         } else {
2669                                 alnum_cnt++;
2670                         }
2671                         break;
2672                 }
2673
2674                 i_len++;
2675         }
2676
2677 out:
2678         if ((i_len > 0) && (state == WAIT_FOR_INDENT_CHAR))
2679                 i_len -= alnum_cnt;
2680
2681         return i_len;
2682 }
2683
2684 /* insert quotation string when line was wrapped */
2685 static guint ins_quote(GtkTextBuffer *textbuf, GtkTextIter *iter,
2686                        guint indent_len,
2687                        guint prev_line_pos, guint text_len,
2688                        gchar *quote_fmt)
2689 {
2690         guint ins_len = 0;
2691
2692         if (indent_len) {
2693                 GtkTextIter iter1, iter2;
2694                 gchar *text;
2695
2696                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter1,
2697                                                    prev_line_pos);
2698                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter2,
2699                                                    prev_line_pos + indent_len);
2700                 text = gtk_text_buffer_get_text(textbuf, &iter1, &iter2, FALSE);
2701                 if (!text) return 0;
2702
2703                 gtk_text_buffer_insert(textbuf, iter, text, -1);
2704                 ins_len = g_utf8_strlen(text, -1);
2705
2706                 g_free(text);
2707         }
2708
2709         return ins_len;
2710 }
2711
2712 /* check if we should join the next line */
2713 static gboolean join_next_line_is_needed(GtkTextBuffer *textbuf,
2714                                          guint start_pos, guint tlen,
2715                                          guint prev_ilen, gboolean autowrap)
2716 {
2717         guint indent_len, ch_len;
2718         gboolean do_join = FALSE;
2719         gchar cbuf[CHAR_BUF_SIZE];
2720
2721         indent_len = get_indent_length(textbuf, start_pos, tlen);
2722
2723         if ((autowrap || indent_len > 0) && indent_len == prev_ilen) {
2724                 GtkTextIter iter;
2725                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter,
2726                                                    start_pos + indent_len);
2727                 GET_CHAR(&iter, cbuf, ch_len);
2728                 if (ch_len > 0 && (cbuf[0] != '\n'))
2729                         do_join = TRUE;
2730         }
2731
2732         return do_join;
2733 }
2734
2735 static void compose_wrap_line_all(Compose *compose)
2736 {
2737         compose_wrap_line_all_full(compose, FALSE);
2738 }
2739
2740 static void compose_wrap_line_all_full(Compose *compose, gboolean autowrap)
2741 {
2742         GtkTextView *text = GTK_TEXT_VIEW(compose->text);
2743         GtkTextBuffer *textbuf = gtk_text_view_get_buffer(text);
2744         GtkTextIter iter, end_iter;
2745         guint tlen;
2746         guint line_pos = 0, cur_pos = 0, p_pos = 0;
2747         gint line_len = 0, cur_len = 0;
2748         gint ch_len;
2749         gboolean is_new_line = TRUE, do_delete = FALSE;
2750         guint i_len = 0;
2751         gboolean linewrap_quote = TRUE;
2752         gboolean set_editable_pos = FALSE;
2753         gint editable_pos = 0;
2754         guint linewrap_len = prefs_common.linewrap_len;
2755         gchar *qfmt = prefs_common.quotemark;
2756         gchar cbuf[CHAR_BUF_SIZE];
2757
2758         tlen = gtk_text_buffer_get_char_count(textbuf);
2759
2760         for (; cur_pos < tlen; cur_pos++) {
2761                 /* mark position of new line - needed for quotation wrap */
2762                 if (is_new_line) {
2763                         if (linewrap_quote)
2764                                 i_len = get_indent_length(textbuf, cur_pos, tlen);
2765
2766                         is_new_line = FALSE;
2767                         p_pos = cur_pos;
2768                 }
2769
2770                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter, cur_pos);
2771                 GET_CHAR(&iter, cbuf, ch_len);
2772
2773                 /* fix line length for tabs */
2774                 if (ch_len == 1 && *cbuf == '\t') {
2775 #warning FIXME_GTK2
2776                         /* guint tab_width = text->default_tab_width; */
2777                         guint tab_width = 8;
2778                         guint tab_offset = line_len % tab_width;
2779
2780                         if (tab_offset) {
2781                                 line_len += tab_width - tab_offset - 1;
2782                                 cur_len = line_len;
2783                         }
2784                 }
2785
2786                 /* we have encountered line break */
2787                 if (ch_len == 1 && *cbuf == '\n') {
2788                         gint clen;
2789                         gchar cb[CHAR_BUF_SIZE];
2790
2791                         /* should we join the next line */
2792                         if ((autowrap || i_len != cur_len) && do_delete &&
2793                             join_next_line_is_needed
2794                                 (textbuf, cur_pos + 1, tlen, i_len, autowrap))
2795                                 do_delete = TRUE;
2796                         else
2797                                 do_delete = FALSE;
2798
2799                         /* skip delete if it is continuous URL */
2800                         if (do_delete && (line_pos - p_pos <= i_len) &&
2801                             gtkut_text_buffer_is_uri_string(textbuf, line_pos,
2802                                                             tlen))
2803                                 do_delete = FALSE;
2804
2805                         /* should we delete to perform smart wrapping */
2806                         if (line_len < linewrap_len && do_delete) {
2807                                 /* get rid of newline */
2808                                 gtk_text_buffer_get_iter_at_offset(textbuf,
2809                                                                    &iter,
2810                                                                    cur_pos);
2811                                 end_iter = iter;
2812                                 gtk_text_iter_forward_char(&end_iter);
2813                                 gtk_text_buffer_delete(textbuf, &iter, &end_iter);
2814                                 tlen--;
2815
2816                                 /* if text starts with quote fmt or with
2817                                    indent string, delete them */
2818                                 if (i_len) {
2819                                         guint ilen;
2820                                         ilen =  gtkut_text_buffer_str_compare_n
2821                                                 (textbuf, cur_pos, p_pos, i_len,
2822                                                  tlen);
2823                                         if (ilen) {
2824                                                 end_iter = iter;
2825                                                 gtk_text_iter_forward_chars
2826                                                         (&end_iter, ilen);
2827                                                 gtk_text_buffer_delete(textbuf,
2828                                                                        &iter,
2829                                                                        &end_iter);
2830                                                 tlen -= ilen;
2831                                         }
2832                                 }
2833
2834                                 gtk_text_buffer_get_iter_at_offset(textbuf,
2835                                                                    &iter,
2836                                                                    cur_pos);
2837                                 GET_CHAR(&iter, cb, clen);
2838
2839                                 /* insert space if it's alphanumeric */
2840                                 if ((cur_pos != line_pos) &&
2841                                     ((clen > 1) || isalnum((guchar)cb[0]))) {
2842                                         gtk_text_buffer_insert(textbuf, &iter,
2843                                                                " ", 1);
2844                                         tlen++;
2845                                 }
2846
2847                                 /* and start over with current line */
2848                                 cur_pos = p_pos - 1;
2849                                 line_pos = cur_pos;
2850                                 line_len = cur_len = 0;
2851                                 do_delete = FALSE;
2852                                 is_new_line = TRUE;
2853 #ifdef WRAP_DEBUG
2854                                 g_print("after delete l_pos=");
2855                                 dump_text(text, line_pos, tlen, 1);
2856 #endif
2857                                 /* move beginning of line if we are on LF */
2858                                 gtk_text_buffer_get_iter_at_offset(textbuf,
2859                                                                    &iter,
2860                                                                    line_pos);
2861                                 GET_CHAR(&iter, cb, clen);
2862                                 if (clen == 1 && *cb == '\n')
2863                                         line_pos++;
2864 #ifdef WRAP_DEBUG
2865                                 g_print("new line_pos=%d\n", line_pos);
2866 #endif
2867
2868                                 continue;
2869                         }
2870
2871                         /* mark new line beginning */
2872                         line_pos = cur_pos + 1;
2873                         line_len = cur_len = 0;
2874                         do_delete = FALSE;
2875                         is_new_line = TRUE;
2876                         continue;
2877                 }
2878
2879                 if (ch_len < 0) {
2880                         cbuf[0] = '\0';
2881                         ch_len = 1;
2882                 }
2883
2884                 /* possible line break */
2885                 if (ch_len == 1 && isspace(*(guchar *)cbuf)) {
2886                         line_pos = cur_pos + 1;
2887                         line_len = cur_len + ch_len;
2888                 }
2889
2890                 /* are we over wrapping length set in preferences ? */
2891                 if (cur_len + DISP_WIDTH(ch_len) > linewrap_len) {
2892                         gint clen;
2893
2894 #ifdef WRAP_DEBUG
2895                         g_print("should wrap cur_pos=%d ", cur_pos);
2896                         dump_text(text, p_pos, tlen, 1);
2897                         dump_text(text, line_pos, tlen, 1);
2898 #endif
2899                         /* force wrapping if it is one long word but not URL */
2900                         if (line_pos - p_pos <= i_len)
2901                                 if (!gtkut_text_buffer_is_uri_string
2902                                     (textbuf, line_pos, tlen))
2903                                         line_pos = cur_pos - 1;
2904 #ifdef WRAP_DEBUG
2905                         g_print("new line_pos=%d\n", line_pos);
2906 #endif
2907
2908                         gtk_text_buffer_get_iter_at_offset(textbuf,
2909                                                            &iter,
2910                                                            line_pos - 1);
2911                         GET_CHAR(&iter, cbuf, clen);
2912
2913                         /* if next character is space delete it */
2914                         if (clen == 1 && isspace(*(guchar *)cbuf)) {
2915                                 if (p_pos + i_len != line_pos ||
2916                                     !gtkut_text_buffer_is_uri_string
2917                                         (textbuf, line_pos, tlen)) {
2918                                         /* workaround for correct cursor
2919                                            position */
2920                                         if (set_editable_pos == FALSE) {
2921                                                 GtkTextMark *ins = gtk_text_buffer_get_insert(textbuf);
2922                                                 gtk_text_buffer_get_iter_at_mark(textbuf, &iter, ins);
2923                                                 editable_pos = gtk_text_iter_get_offset(&iter);
2924                                                 if (editable_pos == line_pos)
2925                                                         set_editable_pos = TRUE;
2926                                         }
2927                                         gtk_text_buffer_get_iter_at_offset(textbuf,
2928                                                            &iter,
2929                                                            line_pos-1);
2930                                         gtk_text_buffer_get_iter_at_offset(textbuf,
2931                                                            &end_iter,
2932                                                            line_pos);
2933                                         gtk_text_buffer_delete(textbuf, &iter, &end_iter);
2934                                         //gtk_stext_set_point(text, line_pos);
2935                                         //gtk_stext_backward_delete(text, 1);
2936                                         tlen--;
2937                                         cur_pos--;
2938                                         line_pos--;
2939                                         cur_len--;
2940                                         line_len--;
2941                                 }
2942                         }
2943
2944                         /* if it is URL at beginning of line don't wrap */
2945                         if (p_pos + i_len == line_pos &&
2946                             gtkut_text_buffer_is_uri_string(textbuf, line_pos, tlen)) {
2947 #ifdef WRAP_DEBUG
2948                                 g_print("found URL at ");
2949                                 dump_text(text, line_pos, tlen, 1);
2950 #endif
2951                                 continue;
2952                         }
2953
2954                         /* insert CR */
2955                         gtk_text_buffer_get_iter_at_offset(textbuf,
2956                                                            &iter,
2957                                                            line_pos);
2958                         gtk_text_buffer_insert(textbuf, &iter, "\n", 1);
2959                         //gtk_stext_set_point(text, line_pos);
2960                         //gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2961                         //gtk_stext_compact_buffer(text);
2962                         tlen++;
2963                         line_pos++;
2964                         /* for loop will increase it */
2965                         cur_pos = line_pos - 1;
2966                         /* start over with current line */
2967                         is_new_line = TRUE;
2968                         line_len = cur_len = 0;
2969                         if (autowrap || i_len > 0)
2970                                 do_delete = TRUE;
2971                         else
2972                                 do_delete = FALSE;
2973 #ifdef WRAP_DEBUG
2974                         g_print("after CR insert ");
2975                         dump_text(text, line_pos, tlen, 1);
2976                         dump_text(text, cur_pos, tlen, 1);
2977 #endif
2978
2979                         /* should we insert quotation ? */
2980                         if (linewrap_quote && i_len) {
2981                                 /* only if line is not already quoted  */
2982                                 if (!gtkut_text_buffer_str_compare
2983                                         (textbuf, line_pos, tlen, qfmt)) {
2984                                         guint ins_len;
2985                                         
2986                                         gtk_text_buffer_get_iter_at_offset(textbuf, &iter, line_pos);
2987                                         
2988                                         if (line_pos - p_pos > i_len) {
2989                                                 ins_len = ins_quote
2990                                                         (textbuf, &iter, i_len, p_pos,
2991                                                          tlen, qfmt);
2992                                                 tlen += ins_len;
2993                                         }
2994 #ifdef WRAP_DEBUG
2995                                         g_print("after quote insert ");
2996                                         dump_text(text, line_pos, tlen, 1);
2997 #endif
2998                                 }
2999                         }
3000                         continue;
3001                 }
3002
3003                 if (ch_len > 1) {
3004                         line_pos = cur_pos + 1;
3005                         line_len = cur_len + ch_len;
3006                 }
3007                 /* advance to next character in buffer */
3008                 cur_len += ch_len;
3009         }
3010
3011         if (set_editable_pos && editable_pos <= tlen) {
3012                 gtk_text_buffer_get_iter_at_offset(textbuf, &iter, editable_pos);
3013                 gtk_text_buffer_place_cursor(textbuf, &iter);
3014         }
3015 }
3016
3017 #undef GET_CHAR
3018 #undef CHAR_BUF_SIZE
3019
3020 static void compose_set_title(Compose *compose)
3021 {
3022         gchar *str;
3023         gchar *edited;
3024
3025         edited = compose->modified ? _(" [Edited]") : "";
3026         if (compose->account && compose->account->address)
3027                 str = g_strdup_printf(_("%s - Compose message%s"),
3028                                       compose->account->address, edited);
3029         else
3030                 str = g_strdup_printf(_("Compose message%s"), edited);
3031         gtk_window_set_title(GTK_WINDOW(compose->window), str);
3032         g_free(str);
3033 }
3034
3035 /**
3036  * compose_current_mail_account:
3037  * 
3038  * Find a current mail account (the currently selected account, or the
3039  * default account, if a news account is currently selected).  If a
3040  * mail account cannot be found, display an error message.
3041  * 
3042  * Return value: Mail account, or NULL if not found.
3043  **/
3044 static PrefsAccount *
3045 compose_current_mail_account(void)
3046 {
3047         PrefsAccount *ac;
3048
3049         if (cur_account && cur_account->protocol != A_NNTP)
3050                 ac = cur_account;
3051         else {
3052                 ac = account_get_default();
3053                 if (!ac || ac->protocol == A_NNTP) {
3054                         alertpanel_error(_("Account for sending mail is not specified.\n"
3055                                            "Please select a mail account before sending."));
3056                         return NULL;
3057                 }
3058         }
3059         return ac;
3060 }
3061
3062 static void compose_select_account(Compose *compose, PrefsAccount *account,
3063                                    gboolean init)
3064 {
3065         GtkItemFactory *ifactory;
3066
3067         g_return_if_fail(account != NULL);
3068
3069         compose->account = account;
3070
3071         compose_set_title(compose);
3072
3073         ifactory = gtk_item_factory_from_widget(compose->menubar);
3074 #if 0
3075         if (account->protocol == A_NNTP) {
3076                 gtk_widget_show(compose->newsgroups_hbox);
3077                 gtk_widget_show(compose->newsgroups_entry);
3078                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 2, 4);
3079                 compose->use_newsgroups = TRUE;
3080
3081                 menu_set_active(ifactory, "/View/To", FALSE);
3082                 menu_set_sensitive(ifactory, "/View/To", TRUE);
3083                 menu_set_active(ifactory, "/View/Cc", FALSE);
3084                 menu_set_sensitive(ifactory, "/View/Cc", TRUE);
3085                 menu_set_sensitive(ifactory, "/View/Followup to", TRUE);
3086         } else {
3087                 gtk_widget_hide(compose->newsgroups_hbox);
3088                 gtk_widget_hide(compose->newsgroups_entry);
3089                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 2, 0);
3090                 gtk_widget_queue_resize(compose->table_vbox);
3091                 compose->use_newsgroups = FALSE;
3092
3093                 menu_set_active(ifactory, "/View/To", TRUE);
3094                 menu_set_sensitive(ifactory, "/View/To", FALSE);
3095                 menu_set_active(ifactory, "/View/Cc", TRUE);
3096                 menu_set_sensitive(ifactory, "/View/Cc", FALSE);
3097                 menu_set_active(ifactory, "/View/Followup to", FALSE);
3098                 menu_set_sensitive(ifactory, "/View/Followup to", FALSE);
3099         }
3100
3101         if (account->set_autocc) {
3102                 compose_entry_show(compose, COMPOSE_ENTRY_CC);
3103                 if (account->auto_cc && compose->mode != COMPOSE_REEDIT)
3104                         compose_entry_set(compose, account->auto_cc,
3105                                           COMPOSE_ENTRY_CC);
3106         }
3107         if (account->set_autobcc) {
3108                 compose_entry_show(compose, COMPOSE_ENTRY_BCC);
3109                 if (account->auto_bcc && compose->mode != COMPOSE_REEDIT)
3110                         compose_entry_set(compose, account->auto_bcc,
3111                                           COMPOSE_ENTRY_BCC);
3112         }
3113         if (account->set_autoreplyto) {
3114                 compose_entry_show(compose, COMPOSE_ENTRY_REPLY_TO);
3115                 if (account->auto_replyto && compose->mode != COMPOSE_REEDIT)
3116                         compose_entry_set(compose, account->auto_replyto,
3117                                           COMPOSE_ENTRY_REPLY_TO);
3118         }
3119
3120         menuitem = gtk_item_factory_get_item(ifactory, "/View/Ruler");
3121         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
3122                                        prefs_common.show_ruler);
3123 #endif
3124
3125 #if USE_GPGME
3126         if (account->default_sign)
3127                 menu_set_active(ifactory, "/Message/Sign", TRUE);
3128         if (account->default_encrypt)
3129                 menu_set_active(ifactory, "/Message/Encrypt", TRUE);
3130                                        
3131         activate_gnupg_mode(compose, account);          
3132 #endif /* USE_GPGME */
3133
3134         if (!init && compose->mode != COMPOSE_REDIRECT)
3135                 compose_insert_sig(compose, TRUE);
3136 }
3137
3138 gboolean compose_check_for_valid_recipient(Compose *compose) {
3139         gchar *recipient_headers_mail[] = {"To:", "Cc:", "Bcc:", NULL};
3140         gchar *recipient_headers_news[] = {"Newsgroups:", NULL};
3141         gboolean recipient_found = FALSE;
3142         GSList *list;
3143         gchar **strptr;
3144
3145         /* free to and newsgroup list */
3146         slist_free_strings(compose->to_list);
3147         g_slist_free(compose->to_list);
3148         compose->to_list = NULL;
3149                         
3150         slist_free_strings(compose->newsgroup_list);
3151         g_slist_free(compose->newsgroup_list);
3152         compose->newsgroup_list = NULL;
3153
3154         /* search header entries for to and newsgroup entries */
3155         for (list = compose->header_list; list; list = list->next) {
3156                 gchar *header;
3157                 gchar *entry;
3158                 header = gtk_editable_get_chars(GTK_EDITABLE(GTK_COMBO(((ComposeHeaderEntry *)list->data)->combo)->entry), 0, -1);
3159                 entry = gtk_editable_get_chars(GTK_EDITABLE(((ComposeHeaderEntry *)list->data)->entry), 0, -1);
3160                 g_strstrip(entry);
3161                 if (entry[0] != '\0') {
3162                         for (strptr = recipient_headers_mail; *strptr != NULL; strptr++) {
3163                                 if (!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
3164                                         compose->to_list = address_list_append(compose->to_list, entry);
3165                                         recipient_found = TRUE;
3166                                 }
3167                         }
3168                         for (strptr = recipient_headers_news; *strptr != NULL; strptr++) {
3169                                 if (!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
3170                                         compose->newsgroup_list = newsgroup_list_append(compose->newsgroup_list, entry);
3171                                         recipient_found = TRUE;
3172                                 }
3173                         }
3174                 }
3175                 g_free(header);
3176                 g_free(entry);
3177         }
3178         return recipient_found;
3179 }
3180
3181 static gboolean compose_check_entries(Compose *compose, gboolean check_subject)
3182 {
3183         const gchar *str;
3184
3185         if (compose_check_for_valid_recipient(compose) == FALSE) {
3186                 alertpanel_error(_("Recipient is not specified."));
3187                 return FALSE;
3188         }
3189
3190         str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
3191         if (*str == '\0' && check_subject == TRUE) {
3192                 AlertValue aval;
3193
3194                 aval = alertpanel(_("Send"),
3195                                   _("Subject is empty. Send it anyway?"),
3196                                   _("Yes"), _("No"), NULL);
3197                 if (aval != G_ALERTDEFAULT)
3198                         return FALSE;
3199         }
3200
3201         return TRUE;
3202 }
3203
3204 gint compose_send(Compose *compose)
3205 {
3206         gint msgnum;
3207         FolderItem *folder;
3208         gint val;
3209         gchar *msgpath;
3210
3211         if (compose_check_entries(compose, TRUE) == FALSE)
3212                 return -1;
3213
3214         val = compose_queue(compose, &msgnum, &folder);
3215         if (val) {
3216                 alertpanel_error(_("Could not queue message for sending"));
3217                 return -1;
3218         }
3219
3220         if (msgnum == 0) {
3221                 alertpanel_error(_("The message was queued but could not be sent.\nUse \"Send queued messages\" from the main window to retry."));
3222                 return 0;
3223         }
3224         
3225         msgpath = folder_item_fetch_msg(folder, msgnum);
3226         val = procmsg_send_message_queue(msgpath);
3227         g_free(msgpath);
3228
3229         folder_item_remove_msg(folder, msgnum);
3230         
3231         folder_item_scan(folder);
3232
3233         return val;
3234 }
3235
3236 #if 0 /* compose restructure */
3237 gint compose_send(Compose *compose)
3238 {
3239         gchar tmp[MAXPATHLEN + 1];
3240         gint ok = 0;
3241         static gboolean lock = FALSE;
3242
3243         if (lock) return 1;
3244
3245         g_return_val_if_fail(compose->account != NULL, -1);
3246
3247         lock = TRUE;
3248
3249         if (compose_check_entries(compose, TRUE) == FALSE) {
3250                 lock = FALSE;
3251                 return 1;
3252         }
3253
3254         /* write to temporary file */
3255         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.%p",
3256                    get_tmp_dir(), G_DIR_SEPARATOR, compose);
3257
3258         if (prefs_common.linewrap_at_send)
3259                 compose_wrap_line_all(compose);
3260
3261         if (compose_write_to_file(compose, tmp, FALSE) < 0) {
3262                 lock = FALSE;
3263                 return -1;
3264         }
3265
3266         if (!compose->to_list && !compose->newsgroup_list) {
3267                 g_warning("can't get recipient list.");
3268                 unlink(tmp);
3269                 lock = FALSE;
3270                 return -1;
3271         }
3272
3273         if (compose->to_list) {
3274                 PrefsAccount *ac;
3275
3276 #if 0 /* NEW COMPOSE GUI */
3277                 if (compose->account->protocol != A_NNTP)
3278                         ac = compose->account;
3279                 else {
3280                         ac = account_find_from_address(compose->account->address);
3281                         if (!ac) {
3282                                 if (cur_account && cur_account->protocol != A_NNTP)
3283                                         ac = cur_account;
3284                                 else
3285                                         ac = account_get_default();
3286                         }
3287                         if (!ac || ac->protocol == A_NNTP) {
3288                                 alertpanel_error(_("Account for sending mail is not specified.\n"
3289                                                    "Please select a mail account before sending."));
3290                                 unlink(tmp);
3291                                 lock = FALSE;
3292                                 return -1;
3293                         }
3294                 }
3295 #endif
3296                 ac = compose->account;
3297
3298                 ok = send_message(tmp, ac, compose->to_list);
3299         }
3300
3301         if (ok == 0 && compose->newsgroup_list) {
3302                 ok = news_post(FOLDER(compose->account->folder), tmp);
3303                 if (ok < 0) {
3304                         alertpanel_error(_("Error occurred while posting the message to %s ."),
3305                                          compose->account->nntp_server);
3306                         unlink(tmp);
3307                         lock = FALSE;
3308                         return -1;
3309                 }
3310         }
3311
3312         if (ok == 0) {
3313                 if (compose->mode == COMPOSE_REEDIT) {
3314                         compose_remove_reedit_target(compose);
3315                 }
3316                 /* save message to outbox */
3317                 if (prefs_common.savemsg) {
3318                         FolderItem *outbox;
3319
3320                         outbox = account_get_special_folder
3321                                 (compose->account, F_OUTBOX);
3322                         if (procmsg_save_to_outbox(outbox, tmp, FALSE) < 0)
3323                                 alertpanel_error
3324                                         (_("Can't save the message to Sent."));
3325                 }
3326         }
3327
3328         unlink(tmp);
3329         lock = FALSE;
3330         return ok;
3331 }
3332 #endif
3333
3334 static gboolean compose_use_attach(Compose *compose) 
3335 {
3336         return gtk_clist_get_row_data(GTK_CLIST(compose->attach_clist), 0) != NULL;
3337 }
3338
3339 static gint compose_redirect_write_headers_from_headerlist(Compose *compose, 
3340                                                            FILE *fp)
3341 {
3342         gchar buf[BUFFSIZE];
3343         gchar *str;
3344         gboolean first_to_address;
3345         gboolean first_cc_address;
3346         GSList *list;
3347         ComposeHeaderEntry *headerentry;
3348         const gchar *headerentryname;
3349         gchar *cc_hdr;
3350         gchar *to_hdr;
3351
3352         debug_print("Writing redirect header\n");
3353
3354         cc_hdr = prefs_common.trans_hdr ? _("Cc:") : "Cc:";
3355         to_hdr = prefs_common.trans_hdr ? _("To:") : "To:";
3356
3357         first_to_address = TRUE;
3358         first_cc_address = TRUE;
3359         for (list = compose->header_list; list; list = list->next) {
3360                 headerentry = ((ComposeHeaderEntry *)list->data);
3361                 headerentryname = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(headerentry->combo)->entry));
3362
3363                 if (g_strcasecmp(headerentryname, cc_hdr) == 0 
3364                    || g_strcasecmp(headerentryname, to_hdr) == 0) {
3365                         const gchar *entstr = gtk_entry_get_text(GTK_ENTRY(headerentry->entry));
3366                         Xstrdup_a(str, entstr, return -1);
3367                         g_strstrip(str);
3368                         if (str[0] != '\0') {
3369                                 compose_convert_header
3370                                         (buf, sizeof(buf), str,
3371                                         strlen("Resent-To") + 2, TRUE);
3372                                 if (g_strcasecmp(headerentryname, to_hdr) == 0) {
3373                                         if (first_to_address) {
3374                                                 fprintf(fp, "Resent-To: ");
3375                                                 first_to_address = FALSE;
3376                                         } else {
3377                                                 fprintf(fp, ",");
3378                                         }
3379                                 }
3380                                 if (g_strcasecmp(headerentryname, cc_hdr) == 0) {
3381                                         if (first_cc_address) {
3382                                                 fprintf(fp, "\n");
3383                                                 fprintf(fp, "Resent-Cc: ");
3384                                                 first_cc_address = FALSE;
3385                                         } else {
3386                                                 fprintf(fp, ",");
3387                                         }
3388                                 }
3389                                 
3390                                 fprintf(fp, "%s", buf);
3391                         }
3392                 }
3393         }
3394         /* if (!first_address) { */
3395         fprintf(fp, "\n");
3396         /* } */
3397
3398         return(0);
3399 }
3400
3401 static gint compose_redirect_write_headers(Compose *compose, FILE *fp)
3402 {
3403         gchar buf[BUFFSIZE];
3404         gchar *str;
3405         const gchar *entstr;
3406         /* struct utsname utsbuf; */
3407
3408         g_return_val_if_fail(fp != NULL, -1);
3409         g_return_val_if_fail(compose->account != NULL, -1);
3410         g_return_val_if_fail(compose->account->address != NULL, -1);
3411
3412         /* Resent-Date */
3413         get_rfc822_date(buf, sizeof(buf));
3414         fprintf(fp, "Resent-Date: %s\n", buf);
3415
3416         /* Resent-From */
3417         if (compose->account->name && *compose->account->name) {
3418                 compose_convert_header
3419                         (buf, sizeof(buf), compose->account->name,
3420                          strlen("From: "), TRUE);
3421                 fprintf(fp, "Resent-From: %s <%s>\n",
3422                         buf, compose->account->address);
3423         } else
3424                 fprintf(fp, "Resent-From: %s\n", compose->account->address);
3425
3426         /* Subject */
3427         entstr = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
3428         if (*entstr != '\0') {
3429                 Xstrdup_a(str, entstr, return -1);
3430                 g_strstrip(str);
3431                 if (*str != '\0') {
3432                         compose_convert_header(buf, sizeof(buf), str,
3433                                                strlen("Subject: "), FALSE);
3434                         fprintf(fp, "Subject: %s\n", buf);
3435                 }
3436         }
3437
3438         /* Resent-Message-ID */
3439         if (compose->account->gen_msgid) {
3440                 compose_generate_msgid(buf, sizeof(buf));
3441                 fprintf(fp, "Resent-Message-Id: <%s>\n", buf);
3442                 compose->msgid = g_strdup(buf);
3443         }
3444
3445         compose_redirect_write_headers_from_headerlist(compose, fp);
3446
3447         /* separator between header and body */
3448         fputs("\n", fp);
3449
3450         return 0;
3451 }
3452
3453 static gint compose_redirect_write_to_file(Compose *compose, const gchar *file)
3454 {
3455         FILE *fp;
3456         FILE *fdest;
3457         size_t len;
3458         gchar buf[BUFFSIZE];
3459
3460         if ((fp = fopen(compose->redirect_filename, "rb")) == NULL) {
3461                 FILE_OP_ERROR(file, "fopen");
3462                 return -1;
3463         }
3464
3465         if ((fdest = fopen(file, "wb")) == NULL) {
3466                 FILE_OP_ERROR(file, "fopen");
3467                 fclose(fp);
3468                 return -1;
3469         }
3470
3471         /* chmod for security */
3472         if (change_file_mode_rw(fdest, file) < 0) {
3473                 FILE_OP_ERROR(file, "chmod");
3474                 g_warning("can't change file mode\n");
3475         }
3476
3477         while (procheader_get_one_field_asis(buf, sizeof(buf), fp) != -1) {
3478                 /* should filter returnpath, delivered-to */
3479                 if (g_strncasecmp(buf, "Return-Path:",
3480                                    strlen("Return-Path:")) == 0 ||
3481                     g_strncasecmp(buf, "Delivered-To:",
3482                                   strlen("Delivered-To:")) == 0 ||
3483                     g_strncasecmp(buf, "Received:",
3484                                   strlen("Received:")) == 0 ||
3485                     g_strncasecmp(buf, "Subject:",
3486                                   strlen("Subject:")) == 0 ||
3487                     g_strncasecmp(buf, "X-UIDL:",
3488                                   strlen("X-UIDL:")) == 0)
3489                         continue;
3490
3491                 if (fputs(buf, fdest) == -1)
3492                         goto error;
3493
3494                 if (!prefs_common.redirect_keep_from) {
3495                         if (g_strncasecmp(buf, "From:",
3496                                           strlen("From:")) == 0) {
3497                                 fputs(" (by way of ", fdest);
3498                                 if (compose->account->name
3499                                     && *compose->account->name) {
3500                                         compose_convert_header
3501                                                 (buf, sizeof(buf),
3502                                                  compose->account->name,
3503                                                  strlen("From: "),
3504                                                  FALSE);
3505                                         fprintf(fdest, "%s <%s>",
3506                                                 buf,
3507                                                 compose->account->address);
3508                                 } else
3509                                         fprintf(fdest, "%s",
3510                                                 compose->account->address);
3511                                 fputs(")", fdest);
3512                         }
3513                 }
3514
3515                 if (fputs("\n", fdest) == -1)
3516                         goto error;
3517         }
3518
3519         compose_redirect_write_headers(compose, fdest);
3520
3521         while ((len = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
3522                 if (fwrite(buf, sizeof(gchar), len, fdest) != len) {
3523                         FILE_OP_ERROR(file, "fwrite");
3524                         goto error;
3525                 }
3526         }
3527
3528         fclose(fp);
3529         if (fclose(fdest) == EOF) {
3530                 FILE_OP_ERROR(file, "fclose");
3531                 unlink(file);
3532                 return -1;
3533         }
3534
3535         return 0;
3536  error:
3537         fclose(fp);
3538         fclose(fdest);
3539         unlink(file);
3540
3541         return -1;
3542 }
3543
3544
3545 #if USE_GPGME
3546 /* interfaces to rfc2015 to keep out the prefs stuff there.
3547  * returns 0 on success and -1 on error. */
3548 static gint compose_create_signers_list(Compose *compose, GSList **pkey_list)
3549 {
3550         const gchar *key_id = NULL;
3551         GSList *key_list;
3552
3553         switch (compose->account->sign_key) {
3554         case SIGN_KEY_DEFAULT:
3555                 *pkey_list = NULL;
3556                 return 0;
3557         case SIGN_KEY_BY_FROM:
3558                 key_id = compose->account->address;
3559                 break;
3560         case SIGN_KEY_CUSTOM:
3561                 key_id = compose->account->sign_key_id;
3562                 break;
3563         default:
3564                 break;
3565         }
3566
3567         key_list = rfc2015_create_signers_list(key_id);
3568
3569         if (!key_list) {
3570                 alertpanel_error(_("Could not find any key associated with "
3571                                    "currently selected key id `%s'."), key_id);
3572                 return -1;
3573         }
3574
3575         *pkey_list = key_list;
3576         return 0;
3577 }
3578
3579 /* clearsign message body text */
3580 static gint compose_clearsign_text(Compose *compose, gchar **text)
3581 {
3582         GSList *key_list;
3583         gchar *tmp_file;
3584
3585         tmp_file = get_tmp_file();
3586         if (str_write_to_file(*text, tmp_file) < 0) {
3587                 g_free(tmp_file);
3588                 return -1;
3589         }
3590
3591         if (compose_create_signers_list(compose, &key_list) < 0 ||
3592             rfc2015_clearsign(tmp_file, key_list) < 0) {
3593                 unlink(tmp_file);
3594                 g_free(tmp_file);
3595                 return -1;
3596         }
3597
3598         g_free(*text);
3599         *text = file_read_to_str(tmp_file);
3600         unlink(tmp_file);
3601         g_free(tmp_file);
3602         if (*text == NULL)
3603                 return -1;
3604
3605         return 0;
3606 }
3607 #endif /* USE_GPGME */
3608
3609 static gint compose_write_to_file(Compose *compose, const gchar *file,
3610                                   gboolean is_draft)
3611 {
3612         GtkTextBuffer *buffer;
3613         GtkTextIter start, end;
3614         FILE *fp;
3615         size_t len;
3616         gchar *chars;
3617         gchar *buf;
3618         gchar *canon_buf;
3619         const gchar *out_codeset;
3620         EncodingType encoding;
3621
3622         if ((fp = fopen(file, "wb")) == NULL) {
3623                 FILE_OP_ERROR(file, "fopen");
3624                 return -1;
3625         }
3626
3627         /* chmod for security */
3628         if (change_file_mode_rw(fp, file) < 0) {
3629                 FILE_OP_ERROR(file, "chmod");
3630                 g_warning("can't change file mode\n");
3631         }
3632
3633         /* get all composed text */
3634         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(compose->text));
3635         gtk_text_buffer_get_start_iter(buffer, &start);
3636         gtk_text_buffer_get_end_iter(buffer, &end);
3637         chars = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
3638         if (is_ascii_str(chars)) {
3639                 buf = chars;
3640                 chars = NULL;
3641                 out_codeset = CS_US_ASCII;
3642                 encoding = ENC_7BIT;
3643         } else {
3644                 const gchar *src_codeset;
3645
3646                 out_codeset = conv_get_outgoing_charset_str();
3647                 if (!strcasecmp(out_codeset, CS_US_ASCII))
3648                         out_codeset = CS_ISO_8859_1;
3649
3650                 if (prefs_common.encoding_method == CTE_BASE64)
3651                         encoding = ENC_BASE64;
3652                 else if (prefs_common.encoding_method == CTE_QUOTED_PRINTABLE)
3653                         encoding = ENC_QUOTED_PRINTABLE;
3654                 else if (prefs_common.encoding_method == CTE_8BIT)
3655                         encoding = ENC_8BIT;
3656                 else
3657                         encoding = procmime_get_encoding_for_charset(out_codeset);
3658
3659 #if USE_GPGME
3660                 if (!is_draft &&
3661                     compose->use_signing && !compose->gnupg_mode &&
3662                     encoding == ENC_8BIT)
3663                         encoding = ENC_BASE64;
3664 #endif
3665
3666 #warning FIXME_GTK2
3667 #if 0 /* FIXME_GTK2 */
3668                 src_codeset = conv_get_current_charset_str();
3669                 /* if current encoding is US-ASCII, set it the same as
3670                    outgoing one to prevent code conversion failure */
3671                 if (!strcasecmp(src_codeset, CS_US_ASCII))
3672                         src_codeset = out_codeset;
3673 #else /* FIXME_GTK2 */
3674                 src_codeset = CS_UTF_8;
3675 #endif /* FIXME_GTK2 */
3676
3677                 debug_print("src encoding = %s, out encoding = %s, transfer encoding = %s\n",
3678                             src_codeset, out_codeset, procmime_get_encoding_str(encoding));
3679
3680                 buf = conv_codeset_strdup(