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