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