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