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