Added user definable actions on message body/file
[claws.git] / src / compose.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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/gtkthemes.h>
51 #include <gtk/gtkdnd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <ctype.h>
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <unistd.h>
59 #include <time.h>
60 /* #include <sys/utsname.h> */
61 #include <stdlib.h>
62 #include <sys/wait.h>
63 #include <signal.h>
64 #include <errno.h>
65
66 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
67 #  include <wchar.h>
68 #  include <wctype.h>
69 #endif
70
71 #include "intl.h"
72 #include "main.h"
73 #include "mainwindow.h"
74 #include "compose.h"
75 #include "gtkstext.h"
76 #include "addressbook.h"
77 #include "folderview.h"
78 #include "procmsg.h"
79 #include "menu.h"
80 #include "stock_pixmap.h"
81 #include "send.h"
82 #include "imap.h"
83 #include "news.h"
84 #include "customheader.h"
85 #include "prefs_common.h"
86 #include "prefs_account.h"
87 #include "account.h"
88 #include "filesel.h"
89 #include "procheader.h"
90 #include "procmime.h"
91 #include "statusbar.h"
92 #include "about.h"
93 #include "base64.h"
94 #include "codeconv.h"
95 #include "utils.h"
96 #include "gtkutils.h"
97 #include "socket.h"
98 #include "alertpanel.h"
99 #include "manage_window.h"
100 #include "gtkshruler.h"
101 #include "folder.h"
102 #include "addr_compl.h"
103 #include "quote_fmt.h"
104 #include "template.h"
105 #include "undo.h"
106 #include "foldersel.h"
107 #include "prefs_actions.h"
108
109 #if USE_GPGME
110 #  include "rfc2015.h"
111 #endif
112
113 typedef enum
114 {
115         COL_MIMETYPE = 0,
116         COL_SIZE     = 1,
117         COL_NAME     = 2
118 } AttachColumnPos;
119
120 #define N_ATTACH_COLS           3
121
122 typedef enum
123 {
124         COMPOSE_CALL_GTK_STEXT_MOVE_BEGINNING_OF_LINE,
125         COMPOSE_CALL_GTK_STEXT_MOVE_FORWARD_CHARACTER,
126         COMPOSE_CALL_GTK_STEXT_MOVE_BACKWARD_CHARACTER,
127         COMPOSE_CALL_GTK_STEXT_MOVE_FORWARD_WORD,
128         COMPOSE_CALL_GTK_STEXT_MOVE_BACKWARD_WORD,
129         COMPOSE_CALL_GTK_STEXT_MOVE_END_OF_LINE,
130         COMPOSE_CALL_GTK_STEXT_MOVE_NEXT_LINE,
131         COMPOSE_CALL_GTK_STEXT_MOVE_PREVIOUS_LINE,
132         COMPOSE_CALL_GTK_STEXT_DELETE_FORWARD_CHARACTER,
133         COMPOSE_CALL_GTK_STEXT_DELETE_BACKWARD_CHARACTER,
134         COMPOSE_CALL_GTK_STEXT_DELETE_FORWARD_WORD,
135         COMPOSE_CALL_GTK_STEXT_DELETE_BACKWARD_WORD,
136         COMPOSE_CALL_GTK_STEXT_DELETE_LINE,
137         COMPOSE_CALL_GTK_STEXT_DELETE_LINE_N,
138         COMPOSE_CALL_GTK_STEXT_DELETE_TO_LINE_END
139 } ComposeCallGtkSTextAction;
140
141 #define B64_LINE_SIZE           57
142 #define B64_BUFFSIZE            77
143
144 #define MAX_REFERENCES_LEN      999
145
146 static GdkColor quote_color = {0, 0, 0, 0xbfff};
147
148 static GList *compose_list = NULL;
149
150 Compose *compose_generic_new                    (PrefsAccount   *account,
151                                                  const gchar    *to,
152                                                  FolderItem     *item);
153
154 static Compose *compose_create                  (PrefsAccount   *account,
155                                                  ComposeMode     mode);
156 static void compose_toolbar_create              (Compose        *compose,
157                                                  GtkWidget      *container);
158 static GtkWidget *compose_account_option_menu_create
159                                                 (Compose        *compose);
160 static void compose_set_template_menu           (Compose        *compose);
161 static void compose_template_apply              (Compose        *compose,
162                                                  Template       *tmpl);
163 static void compose_destroy                     (Compose        *compose);
164
165 static void compose_entries_set                 (Compose        *compose,
166                                                  const gchar    *mailto);
167 static gint compose_parse_header                (Compose        *compose,
168                                                  MsgInfo        *msginfo);
169 static gchar *compose_parse_references          (const gchar    *ref,
170                                                  const gchar    *msgid);
171
172 static gchar *compose_quote_fmt                 (Compose        *compose,
173                                                  MsgInfo        *msginfo,
174                                                  const gchar    *fmt,
175                                                  const gchar    *qmark);
176
177 static void compose_reply_set_entry             (Compose        *compose,
178                                                  MsgInfo        *msginfo,
179                                                  gboolean        to_all,
180                                                  gboolean        to_sender,
181                                                  gboolean
182                                                  followup_and_reply_to);
183 static void compose_reedit_set_entry            (Compose        *compose,
184                                                  MsgInfo        *msginfo);
185 static void compose_insert_sig                  (Compose        *compose);
186 static void compose_insert_file                 (Compose        *compose,
187                                                  const gchar    *file);
188 static void compose_attach_append               (Compose        *compose,
189                                                  const gchar    *file,
190                                                  ContentType     cnttype);
191 static void compose_attach_append_with_type(Compose *compose,
192                                             const gchar *file,
193                                             const gchar *type,
194                                             ContentType cnttype);
195 static void compose_wrap_line                   (Compose        *compose);
196 static void compose_wrap_line_all               (Compose        *compose);
197 static void compose_set_title                   (Compose        *compose);
198
199 static PrefsAccount *compose_current_mail_account(void);
200 /* static gint compose_send                     (Compose        *compose); */
201 static gint compose_write_to_file               (Compose        *compose,
202                                                  const gchar    *file,
203                                                  gboolean        is_draft);
204 static gint compose_write_body_to_file          (Compose        *compose,
205                                                  const gchar    *file);
206 static gint compose_save_to_outbox              (Compose        *compose,
207                                                  const gchar    *file);
208 static gint compose_remove_reedit_target        (Compose        *compose);
209 static gint compose_queue                       (Compose        *compose,
210                                                  gint           *msgnum,
211                                                  FolderItem     **item);
212 static void compose_write_attach                (Compose        *compose,
213                                                  FILE           *fp);
214 static gint compose_write_headers               (Compose        *compose,
215                                                  FILE           *fp,
216                                                  const gchar    *charset,
217                                                  EncodingType    encoding,
218                                                  gboolean        is_draft);
219
220 static void compose_convert_header              (gchar          *dest,
221                                                  gint            len,
222                                                  gchar          *src,
223                                                  gint            header_len);
224 static void compose_generate_msgid              (Compose        *compose,
225                                                  gchar          *buf,
226                                                  gint            len);
227
228 static void compose_attach_info_free            (AttachInfo     *ainfo);
229 static void compose_attach_remove_selected      (Compose        *compose);
230
231 static void compose_attach_property             (Compose        *compose);
232 static void compose_attach_property_create      (gboolean       *cancelled);
233 static void attach_property_ok                  (GtkWidget      *widget,
234                                                  gboolean       *cancelled);
235 static void attach_property_cancel              (GtkWidget      *widget,
236                                                  gboolean       *cancelled);
237 static gint attach_property_delete_event        (GtkWidget      *widget,
238                                                  GdkEventAny    *event,
239                                                  gboolean       *cancelled);
240 static void attach_property_key_pressed         (GtkWidget      *widget,
241                                                  GdkEventKey    *event,
242                                                  gboolean       *cancelled);
243
244 static void compose_exec_ext_editor             (Compose           *compose);
245 static gint compose_exec_ext_editor_real        (const gchar       *file);
246 static gboolean compose_ext_editor_kill         (Compose           *compose);
247 static void compose_input_cb                    (gpointer           data,
248                                                  gint               source,
249                                                  GdkInputCondition  condition);
250 static void compose_set_ext_editor_sensitive    (Compose           *compose,
251                                                  gboolean           sensitive);
252
253 static void compose_undo_state_changed          (UndoMain       *undostruct,
254                                                  gint            undo_state,
255                                                  gint            redo_state,
256                                                  gpointer        data);
257
258 static gint calc_cursor_xpos    (GtkSText       *text,
259                                  gint            extra,
260                                  gint            char_width);
261
262 static void compose_create_header_entry (Compose *compose);
263 static void compose_add_header_entry    (Compose *compose, gchar *header, gchar *text);
264
265 /* callback functions */
266
267 static gboolean compose_edit_size_alloc (GtkEditable    *widget,
268                                          GtkAllocation  *allocation,
269                                          GtkSHRuler     *shruler);
270
271 static void toolbar_send_cb             (GtkWidget      *widget,
272                                          gpointer        data);
273 static void toolbar_send_later_cb       (GtkWidget      *widget,
274                                          gpointer        data);
275 static void toolbar_draft_cb            (GtkWidget      *widget,
276                                          gpointer        data);
277 static void toolbar_insert_cb           (GtkWidget      *widget,
278                                          gpointer        data);
279 static void toolbar_attach_cb           (GtkWidget      *widget,
280                                          gpointer        data);
281 static void toolbar_sig_cb              (GtkWidget      *widget,
282                                          gpointer        data);
283 static void toolbar_ext_editor_cb       (GtkWidget      *widget,
284                                          gpointer        data);
285 static void toolbar_linewrap_cb         (GtkWidget      *widget,
286                                          gpointer        data);
287 static void toolbar_address_cb          (GtkWidget      *widget,
288                                          gpointer        data);
289
290 static void select_account              (Compose        *compose,
291                                          PrefsAccount   *ac);
292
293 static void account_activated           (GtkMenuItem    *menuitem,
294                                          gpointer        data);
295
296 static void attach_selected             (GtkCList       *clist,
297                                          gint            row,
298                                          gint            column,
299                                          GdkEvent       *event,
300                                          gpointer        data);
301 static void attach_button_pressed       (GtkWidget      *widget,
302                                          GdkEventButton *event,
303                                          gpointer        data);
304 static void attach_key_pressed          (GtkWidget      *widget,
305                                          GdkEventKey    *event,
306                                          gpointer        data);
307
308 static void compose_send_cb             (gpointer        data,
309                                          guint           action,
310                                          GtkWidget      *widget);
311 static void compose_send_later_cb       (gpointer        data,
312                                          guint           action,
313                                          GtkWidget      *widget);
314
315 static void compose_draft_cb            (gpointer        data,
316                                          guint           action,
317                                          GtkWidget      *widget);
318
319 static void compose_attach_cb           (gpointer        data,
320                                          guint           action,
321                                          GtkWidget      *widget);
322 static void compose_insert_file_cb      (gpointer        data,
323                                          guint           action,
324                                          GtkWidget      *widget);
325
326 static void compose_close_cb            (gpointer        data,
327                                          guint           action,
328                                          GtkWidget      *widget);
329
330 static void compose_address_cb          (gpointer        data,
331                                          guint           action,
332                                          GtkWidget      *widget);
333 static void compose_template_activate_cb(GtkWidget      *widget,
334                                          gpointer        data);
335
336 static void compose_ext_editor_cb       (gpointer        data,
337                                          guint           action,
338                                          GtkWidget      *widget);
339
340 static gint compose_delete_cb           (GtkWidget      *widget,
341                                          GdkEventAny    *event,
342                                          gpointer        data);
343 static void compose_destroy_cb          (GtkWidget      *widget,
344                                          Compose        *compose);
345
346 static void compose_undo_cb             (Compose        *compose);
347 static void compose_redo_cb             (Compose        *compose);
348 static void compose_cut_cb              (Compose        *compose);
349 static void compose_copy_cb             (Compose        *compose);
350 static void compose_paste_cb            (Compose        *compose);
351 static void compose_allsel_cb           (Compose        *compose);
352
353 static void compose_gtk_stext_action_cb (Compose                   *compose,
354                                          ComposeCallGtkSTextAction  action);
355
356 static void compose_grab_focus_cb       (GtkWidget      *widget,
357                                          Compose        *compose);
358
359 static void compose_changed_cb          (GtkEditable    *editable,
360                                          Compose        *compose);
361 static void compose_button_press_cb     (GtkWidget      *widget,
362                                          GdkEventButton *event,
363                                          Compose        *compose);
364 #if 0
365 static void compose_key_press_cb        (GtkWidget      *widget,
366                                          GdkEventKey    *event,
367                                          Compose        *compose);
368 #endif
369
370 #if 0
371 static void compose_toggle_to_cb        (gpointer        data,
372                                          guint           action,
373                                          GtkWidget      *widget);
374 static void compose_toggle_cc_cb        (gpointer        data,
375                                          guint           action,
376                                          GtkWidget      *widget);
377 static void compose_toggle_bcc_cb       (gpointer        data,
378                                          guint           action,
379                                          GtkWidget      *widget);
380 static void compose_toggle_replyto_cb   (gpointer        data,
381                                          guint           action,
382                                          GtkWidget      *widget);
383 static void compose_toggle_followupto_cb(gpointer        data,
384                                          guint           action,
385                                          GtkWidget      *widget);
386 static void compose_toggle_attach_cb    (gpointer        data,
387                                          guint           action,
388                                          GtkWidget      *widget);
389 #endif
390 static void compose_toggle_ruler_cb     (gpointer        data,
391                                          guint           action,
392                                          GtkWidget      *widget);
393 #if USE_GPGME
394 static void compose_toggle_sign_cb      (gpointer        data,
395                                          guint           action,
396                                          GtkWidget      *widget);
397 static void compose_toggle_encrypt_cb   (gpointer        data,
398                                          guint           action,
399                                          GtkWidget      *widget);
400 #endif
401 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
402                                              GtkWidget *widget);
403
404 static void compose_attach_drag_received_cb (GtkWidget          *widget,
405                                              GdkDragContext     *drag_context,
406                                              gint                x,
407                                              gint                y,
408                                              GtkSelectionData   *data,
409                                              guint               info,
410                                              guint               time,
411                                              gpointer            user_data);
412 static void compose_insert_drag_received_cb (GtkWidget          *widget,
413                                              GdkDragContext     *drag_context,
414                                              gint                x,
415                                              gint                y,
416                                              GtkSelectionData   *data,
417                                              guint               info,
418                                              guint               time,
419                                              gpointer            user_data);
420
421 #if 0
422 static void to_activated                (GtkWidget      *widget,
423                                          Compose        *compose);
424 static void newsgroups_activated        (GtkWidget      *widget,
425                                          Compose        *compose);
426 static void subject_activated           (GtkWidget      *widget,
427                                          Compose        *compose);
428 static void cc_activated                (GtkWidget      *widget,
429                                          Compose        *compose);
430 static void bcc_activated               (GtkWidget      *widget,
431                                          Compose        *compose);
432 static void replyto_activated           (GtkWidget      *widget,
433                                          Compose        *compose);
434 static void followupto_activated        (GtkWidget      *widget,
435                                          Compose        *compose);
436 #endif
437
438 static void compose_attach_parts        (Compose        *compose,
439                                          MsgInfo        *msginfo);
440
441 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
442                                   gboolean to_all,
443                                   gboolean ignore_replyto,
444                                   gboolean followup_and_reply_to);
445
446 void compose_headerentry_changed_cb        (GtkWidget          *entry,
447                                             ComposeHeaderEntry *headerentry);
448 void compose_headerentry_key_press_event_cb(GtkWidget          *entry,
449                                             GdkEventKey        *event,
450                                             ComposeHeaderEntry *headerentry);
451
452 static void compose_show_first_last_header (Compose *compose, gboolean show_first);
453
454 #if USE_PSPELL
455 static void compose_check_all              (Compose *compose);
456 static void compose_highlight_all          (Compose *compose);
457 static void compose_check_backwards        (Compose *compose);
458 static void compose_check_forwards_go      (Compose *compose);
459 #endif
460
461 static gboolean compose_send_control_enter (Compose *compose);
462 static void text_activated                 (GtkWidget   *widget,
463                                             Compose     *compose);
464
465 static GtkItemFactoryEntry compose_popup_entries[] =
466 {
467         {N_("/_Add..."),        NULL, compose_attach_cb, 0, NULL},
468         {N_("/_Remove"),        NULL, compose_attach_remove_selected, 0, NULL},
469         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
470         {N_("/_Property..."),   NULL, compose_attach_property, 0, NULL}
471 };
472
473 static GtkItemFactoryEntry compose_entries[] =
474 {
475         {N_("/_File"),                          NULL, NULL, 0, "<Branch>"},
476         {N_("/_File/_Attach file"),             "<control>M", compose_attach_cb,      0, NULL},
477         {N_("/_File/_Insert file"),             "<control>I", compose_insert_file_cb, 0, NULL},
478         {N_("/_File/Insert si_gnature"),        "<control>G", compose_insert_sig,     0, NULL},
479         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
480         {N_("/_File/_Close"),                   "<control>W", compose_close_cb, 0, NULL},
481
482         {N_("/_Edit"),                  NULL, NULL, 0, "<Branch>"},
483         {N_("/_Edit/_Undo"),            "<control>Z", compose_undo_cb, 0, NULL},
484         {N_("/_Edit/_Redo"),            "<control>Y", compose_redo_cb, 0, NULL},
485         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
486         {N_("/_Edit/Cu_t"),             "<control>X", compose_cut_cb,    0, NULL},
487         {N_("/_Edit/_Copy"),            "<control>C", compose_copy_cb,   0, NULL},
488         {N_("/_Edit/_Paste"),           "<control>V", compose_paste_cb,  0, NULL},
489         {N_("/_Edit/Select _all"),      "<control>A", compose_allsel_cb, 0, NULL},
490         {N_("/_Edit/A_dvanced"),        NULL, NULL, 0, "<Branch>"},
491         {N_("/_Edit/A_dvanced/Move a character backward"),
492                                         "<control>B",
493                                         compose_gtk_stext_action_cb,
494                                         COMPOSE_CALL_GTK_STEXT_MOVE_BACKWARD_CHARACTER,
495                                         NULL},
496         {N_("/_Edit/A_dvanced/Move a character forward"),
497                                         "<control>F",
498                                         compose_gtk_stext_action_cb,
499                                         COMPOSE_CALL_GTK_STEXT_MOVE_FORWARD_CHARACTER,
500                                         NULL},
501         {N_("/_Edit/A_dvanced/Move a word backward"),
502                                         NULL, /* "<alt>B" */
503                                         compose_gtk_stext_action_cb,
504                                         COMPOSE_CALL_GTK_STEXT_MOVE_BACKWARD_WORD,
505                                         NULL},
506         {N_("/_Edit/A_dvanced/Move a word forward"),
507                                         NULL, /* "<alt>F" */
508                                         compose_gtk_stext_action_cb,
509                                         COMPOSE_CALL_GTK_STEXT_MOVE_FORWARD_WORD,
510                                         NULL},
511         {N_("/_Edit/A_dvanced/Move to beginning of line"),
512                                         NULL, /* "<control>A" */
513                                         compose_gtk_stext_action_cb,
514                                         COMPOSE_CALL_GTK_STEXT_MOVE_BEGINNING_OF_LINE,
515                                         NULL},
516         {N_("/_Edit/A_dvanced/Move to end of line"),
517                                         "<control>E",
518                                         compose_gtk_stext_action_cb,
519                                         COMPOSE_CALL_GTK_STEXT_MOVE_END_OF_LINE,
520                                         NULL},
521         {N_("/_Edit/A_dvanced/Move to previous line"),
522                                         "<control>P",
523                                         compose_gtk_stext_action_cb,
524                                         COMPOSE_CALL_GTK_STEXT_MOVE_PREVIOUS_LINE,
525                                         NULL},
526         {N_("/_Edit/A_dvanced/Move to next line"),
527                                         "<control>N",
528                                         compose_gtk_stext_action_cb,
529                                         COMPOSE_CALL_GTK_STEXT_MOVE_NEXT_LINE,
530                                         NULL},
531         {N_("/_Edit/A_dvanced/Delete a character backward"),
532                                         "<control>H",
533                                         compose_gtk_stext_action_cb,
534                                         COMPOSE_CALL_GTK_STEXT_DELETE_BACKWARD_CHARACTER,
535                                         NULL},
536         {N_("/_Edit/A_dvanced/Delete a character forward"),
537                                         "<control>D",
538                                         compose_gtk_stext_action_cb,
539                                         COMPOSE_CALL_GTK_STEXT_DELETE_FORWARD_CHARACTER,
540                                         NULL},
541         {N_("/_Edit/A_dvanced/Delete a word backward"),
542                                         NULL, /* "<control>W" */
543                                         compose_gtk_stext_action_cb,
544                                         COMPOSE_CALL_GTK_STEXT_DELETE_BACKWARD_WORD,
545                                         NULL},
546         {N_("/_Edit/A_dvanced/Delete a word forward"),
547                                         NULL, /* "<alt>D", */
548                                         compose_gtk_stext_action_cb,
549                                         COMPOSE_CALL_GTK_STEXT_DELETE_FORWARD_WORD,
550                                         NULL},
551         {N_("/_Edit/A_dvanced/Delete line"),
552                                         "<control>U",
553                                         compose_gtk_stext_action_cb,
554                                         COMPOSE_CALL_GTK_STEXT_DELETE_LINE,
555                                         NULL},
556         {N_("/_Edit/A_dvanced/Delete entire line"),
557                                         NULL,
558                                         compose_gtk_stext_action_cb,
559                                         COMPOSE_CALL_GTK_STEXT_DELETE_LINE_N,
560                                         NULL},
561         {N_("/_Edit/A_dvanced/Delete to end of line"),
562                                         "<control>K",
563                                         compose_gtk_stext_action_cb,
564                                         COMPOSE_CALL_GTK_STEXT_DELETE_TO_LINE_END,
565                                         NULL},
566         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
567         {N_("/_Edit/_Wrap current paragraph"),
568                                         "<control>L", compose_wrap_line, 0, NULL},
569         {N_("/_Edit/Wrap all long _lines"),
570                                         "<control><alt>L", compose_wrap_line_all, 0, NULL},
571         {N_("/_Edit/Edit with e_xternal editor"),
572                                         "<shift><control>X", compose_ext_editor_cb, 0, NULL},
573         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
574         {N_("/_Edit/Actio_ns"),         NULL, NULL, 0, "<Branch>"},
575 #if USE_PSPELL
576         {N_("/_Spelling"),              NULL, NULL, 0, "<Branch>"},
577         {N_("/_Spelling/_Check all or check selection"),
578                                         NULL, compose_check_all, 0, NULL},
579         {N_("/_Spelling/_Highlight all misspelled words"),
580                                         NULL, compose_highlight_all, 0, NULL},
581         {N_("/_Spelling/Check _backwards misspelled word"),
582                                         NULL, compose_check_backwards , 0, NULL},
583         {N_("/_Spelling/_Forward to next misspelled word"),
584                                         NULL, compose_check_forwards_go, 0, NULL},
585         {N_("/_Spelling/---"),          NULL, NULL, 0, "<Separator>"},
586         {N_("/_Spelling/_Spelling Configuration"),
587                                         NULL, NULL, 0, "<Branch>"},
588 #endif
589 #if 0 /* NEW COMPOSE GUI */
590         {N_("/_View"),                  NULL, NULL, 0, "<Branch>"},
591         {N_("/_View/_To"),              NULL, compose_toggle_to_cb     , 0, "<ToggleItem>"},
592         {N_("/_View/_Cc"),              NULL, compose_toggle_cc_cb     , 0, "<ToggleItem>"},
593         {N_("/_View/_Bcc"),             NULL, compose_toggle_bcc_cb    , 0, "<ToggleItem>"},
594         {N_("/_View/_Reply to"),        NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
595         {N_("/_View/---"),              NULL, NULL, 0, "<Separator>"},
596         {N_("/_View/_Followup to"),     NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
597         {N_("/_View/---"),              NULL, NULL, 0, "<Separator>"},
598         {N_("/_View/R_uler"),           NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
599         {N_("/_View/---"),              NULL, NULL, 0, "<Separator>"},
600         {N_("/_View/_Attachment"),      NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
601 #endif
602         {N_("/_Message"),               NULL, NULL, 0, "<Branch>"},
603         {N_("/_Message/_Send"),         "<control>Return",
604                                         compose_send_cb, 0, NULL},
605         {N_("/_Message/Send _later"),   "<shift><control>S",
606                                         compose_send_later_cb,  0, NULL},
607         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
608         {N_("/_Message/Save to _draft folder"),
609                                         "<shift><control>D", compose_draft_cb, 0, NULL},
610         {N_("/_Message/Save and _keep editing"),
611                                         "<control>S", compose_draft_cb, 1, NULL},
612 #if 0 /* NEW COMPOSE GUI */
613         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
614         {N_("/_Message/_To"),           NULL, compose_toggle_to_cb     , 0, "<ToggleItem>"},
615         {N_("/_Message/_Cc"),           NULL, compose_toggle_cc_cb     , 0, "<ToggleItem>"},
616         {N_("/_Message/_Bcc"),          NULL, compose_toggle_bcc_cb    , 0, "<ToggleItem>"},
617         {N_("/_Message/_Reply to"),     NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
618         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
619         {N_("/_Message/_Followup to"),  NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
620         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
621         {N_("/_Message/_Attach"),       NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
622 #endif
623 #if USE_GPGME
624         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
625         {N_("/_Message/Si_gn"),         NULL, compose_toggle_sign_cb   , 0, "<ToggleItem>"},
626         {N_("/_Message/_Encrypt"),      NULL, compose_toggle_encrypt_cb, 0, "<ToggleItem>"},
627 #endif /* USE_GPGME */
628         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
629         {N_("/_Message/_Request Return Receipt"),       NULL, compose_toggle_return_receipt_cb, 0, "<ToggleItem>"},
630         {N_("/_Tool"),                  NULL, NULL, 0, "<Branch>"},
631         {N_("/_Tool/Show _ruler"),      NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
632         {N_("/_Tool/_Address book"),    "<shift><control>A", compose_address_cb , 0, NULL},
633         {N_("/_Tool/_Template"),        NULL, NULL, 0, "<Branch>"},
634         {N_("/_Help"),                  NULL, NULL, 0, "<Branch>"},
635         {N_("/_Help/_About"),           NULL, about_show, 0, NULL}
636 };
637
638 static GtkTargetEntry compose_mime_types[] =
639 {
640         {"text/uri-list", 0, 0}
641 };
642
643 Compose *compose_new(PrefsAccount *account)
644 {
645         return compose_generic_new(account, NULL, NULL);
646 }
647
648 Compose *compose_bounce(PrefsAccount *account, MsgInfo *msginfo)
649 {
650         Compose *c;
651         gchar *filename;
652         GtkItemFactory *ifactory;
653         
654         c = compose_generic_new(account, NULL, NULL);
655
656         filename = procmsg_get_message_file(msginfo);
657         if (filename == NULL)
658                 return NULL;
659
660         c->bounce_filename = filename;
661
662         if (msginfo->subject)
663                 gtk_entry_set_text(GTK_ENTRY(c->subject_entry),
664                                    msginfo->subject);
665         gtk_editable_set_editable(GTK_EDITABLE(c->subject_entry), FALSE);
666
667         compose_quote_fmt(c, msginfo, "%M", NULL);
668         gtk_editable_set_editable(GTK_EDITABLE(c->text), FALSE);
669
670         ifactory = gtk_item_factory_from_widget(c->popupmenu);
671         menu_set_sensitive(ifactory, "/Add...", FALSE);
672         menu_set_sensitive(ifactory, "/Remove", FALSE);
673         menu_set_sensitive(ifactory, "/Property...", FALSE);
674
675         ifactory = gtk_item_factory_from_widget(c->menubar);
676         menu_set_sensitive(ifactory, "/File/Insert file", FALSE);
677         menu_set_sensitive(ifactory, "/File/Attach file", FALSE);
678         menu_set_sensitive(ifactory, "/File/Insert signature", FALSE);
679         menu_set_sensitive(ifactory, "/Edit/Paste", FALSE);
680         menu_set_sensitive(ifactory, "/Edit/Wrap current paragraph", FALSE);
681         menu_set_sensitive(ifactory, "/Edit/Wrap all long lines", FALSE);
682         menu_set_sensitive(ifactory, "/Edit/Edit with external editor", FALSE);
683         menu_set_sensitive(ifactory, "/Message/Attach", FALSE);
684 #if USE_GPGME
685         menu_set_sensitive(ifactory, "/Message/Sign", FALSE);
686         menu_set_sensitive(ifactory, "/Message/Encrypt", FALSE);
687 #endif
688         menu_set_sensitive(ifactory, "/Message/Request Return Receipt", FALSE);
689         menu_set_sensitive(ifactory, "/Tool/Template", FALSE);
690         
691         gtk_widget_set_sensitive(c->insert_btn, FALSE);
692         gtk_widget_set_sensitive(c->attach_btn, FALSE);
693         gtk_widget_set_sensitive(c->sig_btn, FALSE);
694         gtk_widget_set_sensitive(c->exteditor_btn, FALSE);
695         gtk_widget_set_sensitive(c->linewrap_btn, FALSE);
696
697         return c;
698 }
699
700 Compose *compose_new_with_recipient(PrefsAccount *account, const gchar *mailto)
701 {
702         return compose_generic_new(account, mailto, NULL);
703 }
704
705 Compose *compose_new_with_folderitem(PrefsAccount *account, FolderItem *item)
706 {
707         return compose_generic_new(account, NULL, item);
708 }
709
710 Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderItem *item)
711 {
712         Compose *compose;
713
714         if (item && item->prefs && item->prefs->enable_default_account)
715                 account = account_find_from_id(item->prefs->default_account);
716
717         if (!account) account = cur_account;
718         g_return_val_if_fail(account != NULL, NULL);
719
720         compose = compose_create(account, COMPOSE_NEW);
721         compose->replyinfo = NULL;
722
723         if (prefs_common.auto_sig)
724                 compose_insert_sig(compose);
725         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
726         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
727
728         if (account->protocol != A_NNTP) {
729                 if (mailto) {
730                         compose_entries_set(compose, mailto);
731
732                 } else if(item && item->prefs->enable_default_to) {
733                         compose_entry_append(compose, item->prefs->default_to, COMPOSE_TO);
734                 }
735                 if (item && item->ret_rcpt) {
736                         GtkItemFactory *ifactory;
737                 
738                         ifactory = gtk_item_factory_from_widget(compose->menubar);
739                         menu_set_toggle(ifactory, "/Message/Request Return Receipt", TRUE);
740                 }
741         } else {
742                 if (mailto) {
743                         compose_entry_append(compose, mailto, COMPOSE_NEWSGROUPS);
744                 }
745         }
746         compose_show_first_last_header(compose, TRUE);
747
748         /* Set save folder */
749         if(item && item->prefs && item->prefs->save_copy_to_folder) {
750                 gchar *folderidentifier;
751
752                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
753                 folderidentifier = folder_item_get_identifier(item);
754                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
755                 g_free(folderidentifier);
756         }
757
758         gtk_widget_grab_focus(compose->header_last->entry);
759
760         if (prefs_common.auto_exteditor)
761                 compose_exec_ext_editor(compose);
762
763         return compose;
764 }
765
766 #define CHANGE_FLAGS(msginfo) \
767 { \
768 if (msginfo->folder->folder->change_flags != NULL) \
769 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
770                                       msginfo->folder, \
771                                       msginfo); \
772 }
773
774 /*
775 Compose *compose_new_followup_and_replyto(PrefsAccount *account,
776                                            const gchar *followupto, gchar * to)
777 {
778         Compose *compose;
779
780         if (!account) account = cur_account;
781         g_return_val_if_fail(account != NULL, NULL);
782         g_return_val_if_fail(account->protocol != A_NNTP, NULL);
783
784         compose = compose_create(account, COMPOSE_NEW);
785
786         if (prefs_common.auto_sig)
787                 compose_insert_sig(compose);
788         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
789         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
790
791         compose_entry_append(compose, to, COMPOSE_TO);
792         compose_entry_append(compose, followupto, COMPOSE_NEWSGROUPS);
793         gtk_widget_grab_focus(compose->subject_entry);
794
795         return compose;
796 }
797 */
798
799 void compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
800                    gboolean ignore_replyto)
801 {
802         compose_generic_reply(msginfo, quote, to_all, ignore_replyto, FALSE);
803 }
804
805 void compose_followup_and_reply_to(MsgInfo *msginfo, gboolean quote,
806                                    gboolean to_all,
807                                    gboolean ignore_replyto)
808 {
809         compose_generic_reply(msginfo, quote, to_all, ignore_replyto, TRUE);
810 }
811
812 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
813                                   gboolean to_all,
814                                   gboolean ignore_replyto,
815                                   gboolean followup_and_reply_to)
816 {
817         Compose *compose;
818         PrefsAccount *account;
819         PrefsAccount *reply_account;
820         GtkSText *text;
821
822         g_return_if_fail(msginfo != NULL);
823         g_return_if_fail(msginfo->folder != NULL);
824
825         account = NULL;
826         /* select the account set in folderitem's property (if enabled) */
827         if (msginfo->folder->prefs && msginfo->folder->prefs->enable_default_account)
828                 account = account_find_from_id(msginfo->folder->prefs->default_account);
829         
830         /* select the account for the whole folder (IMAP / NNTP) */
831         if (!account)
832                 /* FIXME: this is not right, because folder may be nested. we should
833                  * ascend the tree until we find a parent with proper account 
834                  * information */
835                 account = msginfo->folder->folder->account;
836
837         /* select account by to: and cc: header if enabled */
838         if (prefs_common.reply_account_autosel) {
839                 if (!account && msginfo->to) {
840                         gchar *to;
841                         Xstrdup_a(to, msginfo->to, return);
842                         extract_address(to);
843                         account = account_find_from_address(to);
844                 }
845                 if (!account) {
846                         gchar cc[BUFFSIZE];
847                         if(!get_header_from_msginfo(msginfo, cc, sizeof(cc), "CC:")) { /* Found a CC header */
848                                 extract_address(cc);
849                                 account = account_find_from_address(cc);
850                         }        
851                 }
852         }
853
854         /* select current account */
855         if (!account) account = cur_account;
856         g_return_if_fail(account != NULL);
857
858         if (ignore_replyto && account->protocol == A_NNTP &&
859             !followup_and_reply_to) {
860                 reply_account =
861                         account_find_from_address(account->address);
862                 if (!reply_account)
863                         reply_account = compose_current_mail_account();
864                 if (!reply_account)
865                         return;
866         } else
867                 reply_account = account;
868
869         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
870         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
871         if (MSG_IS_IMAP(msginfo->flags))
872                 imap_msg_set_perm_flags(msginfo, MSG_REPLIED);
873         CHANGE_FLAGS(msginfo);
874
875         compose = compose_create(account, COMPOSE_REPLY);
876         compose->replyinfo = procmsg_msginfo_copy(msginfo);
877
878 #if 0 /* NEW COMPOSE GUI */
879         if (followup_and_reply_to) {
880                 gtk_widget_show(compose->to_hbox);
881                 gtk_widget_show(compose->to_entry);
882                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 1, 4);
883                 compose->use_to = TRUE;
884         }
885 #endif
886         if (msginfo->folder && msginfo->folder->ret_rcpt) {
887                 GtkItemFactory *ifactory;
888         
889                 ifactory = gtk_item_factory_from_widget(compose->menubar);
890                 menu_set_toggle(ifactory, "/Message/Request Return Receipt", TRUE);
891         }
892
893         /* Set save folder */
894         if(msginfo->folder && msginfo->folder->prefs && msginfo->folder->prefs->save_copy_to_folder) {
895                 gchar *folderidentifier;
896
897                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
898                 folderidentifier = folder_item_get_identifier(msginfo->folder);
899                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
900                 g_free(folderidentifier);
901         }
902
903         if (compose_parse_header(compose, msginfo) < 0) return;
904         compose_reply_set_entry(compose, msginfo, to_all, ignore_replyto,
905                                 followup_and_reply_to);
906         compose_show_first_last_header(compose, TRUE);
907
908         text = GTK_STEXT(compose->text);
909         gtk_stext_freeze(text);
910
911         if (quote) {
912                 gchar *qmark;
913                 gchar *quote_str;
914
915                 if (prefs_common.quotemark && *prefs_common.quotemark)
916                         qmark = prefs_common.quotemark;
917                 else
918                         qmark = "> ";
919
920                 quote_str = compose_quote_fmt(compose, msginfo,
921                                               prefs_common.quotefmt,
922                                               qmark);
923         }
924
925         if (prefs_common.auto_sig)
926                 compose_insert_sig(compose);
927
928         if (quote && prefs_common.linewrap_quote)
929                 compose_wrap_line_all(compose);
930
931         gtk_editable_set_position(GTK_EDITABLE(text), 0);
932         gtk_stext_set_point(text, 0);
933
934         gtk_stext_thaw(text);
935         gtk_widget_grab_focus(compose->text);
936
937         if (prefs_common.auto_exteditor)
938                 compose_exec_ext_editor(compose);
939 }
940
941
942 static gchar *procmime_get_file_name(MimeInfo *mimeinfo)
943 {
944         gchar *base;
945         gchar *filename;
946
947         g_return_val_if_fail(mimeinfo != NULL, NULL);
948
949         base = mimeinfo->filename ? mimeinfo->filename
950                 : mimeinfo->name ? mimeinfo->name : NULL;
951
952         if (MIME_TEXT_HTML == mimeinfo->mime_type && base == NULL){
953                 filename = g_strdup_printf("%s%smimetmp.%08x.html",
954                                            get_mime_tmp_dir(),
955                                            G_DIR_SEPARATOR_S,
956                                            (gint)mimeinfo);
957                 return filename;
958         }
959         else {
960                 base = base ? base : "";
961                 base = g_basename(base);
962                 if (*base == '\0') {
963                         filename = g_strdup_printf("%s%smimetmp.%08x",
964                                                    get_mime_tmp_dir(),
965                                                    G_DIR_SEPARATOR_S,
966                                                    (gint)mimeinfo);
967                         return filename;
968                 }
969         }
970
971         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
972                                base, NULL);
973
974         return filename;
975 }
976
977 static gchar *mime_extract_file(gchar *source, MimeInfo *partinfo)
978 {
979         gchar *filename;
980
981         if (!partinfo) return NULL;
982
983         filename = procmime_get_file_name(partinfo);
984
985         if (procmime_get_part(filename, source, partinfo) < 0)
986                 alertpanel_error
987                         (_("Can't get the part of multipart message."));
988
989         return filename;
990 }
991
992 static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
993 {
994         FILE *fp;
995         gchar *file;
996         MimeInfo *mimeinfo;
997         MsgInfo *tmpmsginfo;
998         gchar *p;
999         gchar *boundary;
1000         gint boundary_len = 0;
1001         gchar buf[BUFFSIZE];
1002         glong fpos, prev_fpos;
1003         gint npart;
1004         gchar *source;
1005         gchar *filename;
1006
1007         g_return_if_fail(msginfo != NULL);
1008         
1009 #if USE_GPGME
1010         for (;;) {
1011                 if ((fp = procmsg_open_message(msginfo)) == NULL) return;
1012                 mimeinfo = procmime_scan_mime_header(fp, MIME_TEXT);
1013                 if (!mimeinfo) break;
1014
1015                 if (!MSG_IS_ENCRYPTED(msginfo->flags) &&
1016                     rfc2015_is_encrypted(mimeinfo)) {
1017                         MSG_SET_TMP_FLAGS(msginfo->flags, MSG_ENCRYPTED);
1018                 }
1019                 if (MSG_IS_ENCRYPTED(msginfo->flags) &&
1020                     !msginfo->plaintext_file  &&
1021                     !msginfo->decryption_failed) {
1022                         rfc2015_decrypt_message(msginfo, mimeinfo, fp);
1023                         if (msginfo->plaintext_file &&
1024                             !msginfo->decryption_failed) {
1025                                 fclose(fp);
1026                                 continue;
1027                         }
1028                 }
1029                 
1030                 break;
1031         }
1032 #else /* !USE_GPGME */
1033         if ((fp = procmsg_open_message(msginfo)) == NULL) return;
1034         mimeinfo = procmime_scan_mime_header(fp, MIME_TEXT);
1035 #endif /* USE_GPGME */
1036
1037         fclose(fp);
1038         if (!mimeinfo) return;
1039         if (mimeinfo->mime_type == MIME_TEXT)
1040                 return;
1041
1042         if ((fp = procmsg_open_message(msginfo)) == NULL) return;
1043
1044         g_return_if_fail(mimeinfo != NULL);
1045         g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
1046
1047         if (mimeinfo->mime_type == MIME_MULTIPART) {
1048                 g_return_if_fail(mimeinfo->boundary != NULL);
1049                 g_return_if_fail(mimeinfo->sub == NULL);
1050         }
1051         g_return_if_fail(fp != NULL);
1052
1053         boundary = mimeinfo->boundary;
1054
1055         if (boundary) {
1056                 boundary_len = strlen(boundary);
1057
1058                 /* look for first boundary */
1059                 while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
1060                         if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
1061                 if (!p) {
1062                         fclose(fp);
1063                         return;
1064                 }
1065         }
1066
1067         if ((fpos = ftell(fp)) < 0) {
1068                 perror("ftell");
1069                 fclose(fp);
1070                 return;
1071         }
1072
1073         for (npart = 0;; npart++) {
1074                 MimeInfo *partinfo;
1075                 gboolean eom = FALSE;
1076
1077                 prev_fpos = fpos;
1078
1079                 partinfo = procmime_scan_mime_header(fp, MIME_TEXT);
1080                 if (!partinfo) break;
1081
1082                 if (npart != 0)
1083                         procmime_mimeinfo_insert(mimeinfo, partinfo);
1084                 else
1085                         procmime_mimeinfo_free(partinfo);
1086
1087                 /* look for next boundary */
1088                 buf[0] = '\0';
1089                 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
1090                         if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1091                                 if (buf[2 + boundary_len]     == '-' &&
1092                                     buf[2 + boundary_len + 1] == '-')
1093                                         eom = TRUE;
1094                                 break;
1095                         }
1096                 }
1097                 if (p == NULL)
1098                         eom = TRUE;     /* broken MIME message */
1099                 fpos = ftell(fp);
1100
1101                 partinfo->size = fpos - prev_fpos - strlen(buf);
1102
1103                 if (eom) break;
1104         }
1105
1106         source = procmsg_get_message_file_path(msginfo);
1107
1108         g_return_if_fail(mimeinfo != NULL);
1109
1110         if (!mimeinfo->main && mimeinfo->parent)
1111                 {
1112                         filename = mime_extract_file(source, mimeinfo);
1113
1114                         compose_attach_append_with_type(compose, filename,
1115                                                         mimeinfo->content_type,
1116                                                         mimeinfo->mime_type);
1117
1118                         g_free(filename);
1119                 }
1120
1121         if (mimeinfo->sub && mimeinfo->sub->children)
1122                 {
1123                         filename = mime_extract_file(source, mimeinfo->sub);
1124
1125                         compose_attach_append_with_type(compose, filename,
1126                                                         mimeinfo->content_type,
1127                                                         mimeinfo->sub->mime_type);
1128
1129                         g_free(filename);
1130                 }
1131
1132         if (mimeinfo->children) {
1133                 MimeInfo *child;
1134
1135                 child = mimeinfo->children;
1136                 while (child) {
1137                         filename = mime_extract_file(source, child);
1138
1139                         compose_attach_append_with_type(compose, filename,
1140                                                         child->content_type,
1141                                                         child->mime_type);
1142
1143                         g_free(filename);
1144
1145                         child = child->next;
1146                 }
1147         }
1148
1149         fclose(fp);
1150
1151         procmime_mimeinfo_free_all(mimeinfo);
1152 }
1153
1154
1155 #define INSERT_FW_HEADER(var, hdr) \
1156 if (msginfo->var && *msginfo->var) { \
1157         gtk_stext_insert(text, NULL, NULL, NULL, hdr, -1); \
1158         gtk_stext_insert(text, NULL, NULL, NULL, msginfo->var, -1); \
1159         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1); \
1160 }
1161
1162 Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
1163                          gboolean as_attach)
1164 {
1165         Compose *compose;
1166         /*      PrefsAccount *account; */
1167         GtkSText *text;
1168
1169         g_return_val_if_fail(msginfo != NULL, NULL);
1170         g_return_val_if_fail(msginfo->folder != NULL, NULL);
1171
1172         account = msginfo->folder->folder->account;
1173         if (!account && msginfo->to && prefs_common.forward_account_autosel) {
1174                 gchar *to;
1175                 Xstrdup_a(to, msginfo->to, return NULL);
1176                 extract_address(to);
1177                 account = account_find_from_address(to);
1178         }
1179
1180         if(!account && prefs_common.forward_account_autosel) {
1181                 gchar cc[BUFFSIZE];
1182                 if(!get_header_from_msginfo(msginfo,cc,sizeof(cc),"CC:")){ /* Found a CC header */
1183                         extract_address(cc);
1184                         account = account_find_from_address(cc);
1185                 }
1186         }
1187
1188         if (account == NULL) {
1189                 account = cur_account;
1190                 /*
1191                 account = msginfo->folder->folder->account;
1192                 if (!account) account = cur_account;
1193                 */
1194         }
1195         g_return_val_if_fail(account != NULL, NULL);
1196
1197         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
1198         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
1199         if (MSG_IS_IMAP(msginfo->flags))
1200                 imap_msg_unset_perm_flags(msginfo, MSG_REPLIED);
1201         CHANGE_FLAGS(msginfo);
1202
1203         compose = compose_create(account, COMPOSE_FORWARD);
1204
1205         if (msginfo->subject && *msginfo->subject) {
1206                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Fw: ");
1207                 gtk_entry_append_text(GTK_ENTRY(compose->subject_entry),
1208                                       msginfo->subject);
1209         }
1210
1211         text = GTK_STEXT(compose->text);
1212         gtk_stext_freeze(text);
1213
1214         if (as_attach) {
1215                 gchar *msgfile;
1216
1217                 msgfile = procmsg_get_message_file_path(msginfo);
1218                 if (!is_file_exist(msgfile))
1219                         g_warning(_("%s: file not exist\n"), msgfile);
1220                 else
1221                         compose_attach_append(compose, msgfile,
1222                                               MIME_MESSAGE_RFC822);
1223
1224                 g_free(msgfile);
1225         } else {
1226                 gchar *qmark;
1227                 gchar *quote_str;
1228
1229                 if (prefs_common.fw_quotemark && *prefs_common.fw_quotemark)
1230                         qmark = prefs_common.fw_quotemark;
1231                 else
1232                         qmark = "> ";
1233
1234                 quote_str = compose_quote_fmt(compose, msginfo,
1235                                               prefs_common.fw_quotefmt, qmark);
1236                 compose_attach_parts(compose, msginfo);
1237         }
1238
1239         if (prefs_common.auto_sig)
1240                 compose_insert_sig(compose);
1241
1242         if (prefs_common.linewrap_quote)
1243                 compose_wrap_line_all(compose);
1244
1245         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
1246         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
1247
1248         gtk_stext_thaw(text);
1249 #if 0 /* NEW COMPOSE GUI */
1250         if (account->protocol != A_NNTP)
1251                 gtk_widget_grab_focus(compose->to_entry);
1252         else
1253                 gtk_widget_grab_focus(compose->newsgroups_entry);
1254 #endif
1255         gtk_widget_grab_focus(compose->header_last->entry);
1256
1257         if (prefs_common.auto_exteditor)
1258                 compose_exec_ext_editor(compose);
1259
1260         return compose;
1261 }
1262
1263 #undef INSERT_FW_HEADER
1264
1265 Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_list)
1266 {
1267         Compose *compose;
1268         GtkSText *text;
1269         GSList *msginfo;
1270         gchar *msgfile;
1271
1272         g_return_val_if_fail(msginfo_list != NULL, NULL);
1273         
1274         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1275                 if ( ((MsgInfo *)msginfo->data)->folder == NULL )
1276                         return NULL;
1277         }
1278
1279         if (account == NULL) {
1280                 account = cur_account;
1281                 /*
1282                 account = msginfo->folder->folder->account;
1283                 if (!account) account = cur_account;
1284                 */
1285         }
1286         g_return_val_if_fail(account != NULL, NULL);
1287
1288         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1289                 MSG_UNSET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_REPLIED);
1290                 MSG_SET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_FORWARDED);
1291                 CHANGE_FLAGS(((MsgInfo *)msginfo->data));
1292         }
1293
1294         compose = compose_create(account, COMPOSE_FORWARD);
1295
1296         text = GTK_STEXT(compose->text);
1297         gtk_stext_freeze(text);
1298
1299         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1300                 msgfile = procmsg_get_message_file_path((MsgInfo *)msginfo->data);
1301                 if (!is_file_exist(msgfile))
1302                         g_warning(_("%s: file not exist\n"), msgfile);
1303                 else
1304                         compose_attach_append(compose, msgfile,
1305                                 MIME_MESSAGE_RFC822);
1306                 g_free(msgfile);
1307         }
1308
1309         if (prefs_common.auto_sig)
1310                 compose_insert_sig(compose);
1311
1312         if (prefs_common.linewrap_quote)
1313                 compose_wrap_line_all(compose);
1314
1315         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
1316         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
1317
1318         gtk_stext_thaw(text);
1319 #if 0 /* NEW COMPOSE GUI */
1320         if (account->protocol != A_NNTP)
1321                 gtk_widget_grab_focus(compose->to_entry);
1322         else
1323                 gtk_widget_grab_focus(compose->newsgroups_entry);
1324 #endif
1325
1326         return compose;
1327 }
1328
1329 void compose_reedit(MsgInfo *msginfo)
1330 {
1331         Compose *compose;
1332         PrefsAccount *account = NULL;
1333         GtkSText *text;
1334         FILE *fp;
1335         gchar buf[BUFFSIZE];
1336
1337         g_return_if_fail(msginfo != NULL);
1338         g_return_if_fail(msginfo->folder != NULL);
1339
1340         if (msginfo->folder->stype == F_QUEUE) {
1341                 gchar queueheader_buf[BUFFSIZE];
1342                 gint id;
1343
1344                 /* Select Account from queue headers */
1345                 if (!get_header_from_msginfo(msginfo, queueheader_buf, 
1346                                              sizeof(queueheader_buf), "NAID:")) {
1347                         id = atoi(&queueheader_buf[5]);
1348                         account = account_find_from_id(id);
1349                 }
1350                 if (!account && !get_header_from_msginfo(msginfo, queueheader_buf, 
1351                                                     sizeof(queueheader_buf), "MAID:")) {
1352                         id = atoi(&queueheader_buf[5]);
1353                         account = account_find_from_id(id);
1354                 }
1355                 if (!account && !get_header_from_msginfo(msginfo, queueheader_buf, 
1356                                                                 sizeof(queueheader_buf), "S:")) {
1357                         account = account_find_from_address(queueheader_buf);
1358                 }
1359         } else 
1360                 account = msginfo->folder->folder->account;
1361
1362         if (!account && prefs_common.reedit_account_autosel) {
1363                 gchar from[BUFFSIZE];
1364                 if (!get_header_from_msginfo(msginfo, from, sizeof(from), "FROM:")){
1365                         extract_address(from);
1366                         account = account_find_from_address(from);
1367                 }
1368         }
1369         if (!account) account = cur_account;
1370         g_return_if_fail(account != NULL);
1371
1372         compose = compose_create(account, COMPOSE_REEDIT);
1373         compose->targetinfo = procmsg_msginfo_copy(msginfo);
1374
1375         if (msginfo->folder->stype == F_QUEUE) {
1376                 gchar queueheader_buf[BUFFSIZE];
1377
1378                 /* Set message save folder */
1379                 if (!get_header_from_msginfo(msginfo, queueheader_buf, sizeof(queueheader_buf), "SCF:")) {
1380                         gint startpos = 0;
1381
1382                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1383                         gtk_editable_delete_text(GTK_EDITABLE(compose->savemsg_entry), 0, -1);
1384                         gtk_editable_insert_text(GTK_EDITABLE(compose->savemsg_entry), &queueheader_buf[4], strlen(&queueheader_buf[4]), &startpos);
1385                 }
1386         }
1387         
1388         if (compose_parse_header(compose, msginfo) < 0) return;
1389         compose_reedit_set_entry(compose, msginfo);
1390
1391         text = GTK_STEXT(compose->text);
1392         gtk_stext_freeze(text);
1393
1394         if ((fp = procmime_get_first_text_content(msginfo)) == NULL)
1395                 g_warning(_("Can't get text part\n"));
1396         else {
1397                 while (fgets(buf, sizeof(buf), fp) != NULL)
1398                         gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1399                 fclose(fp);
1400         }
1401         compose_attach_parts(compose, msginfo);
1402
1403         gtk_stext_thaw(text);
1404         gtk_widget_grab_focus(compose->text);
1405
1406         if (prefs_common.auto_exteditor)
1407                 compose_exec_ext_editor(compose);
1408 }
1409
1410 GList *compose_get_compose_list(void)
1411 {
1412         return compose_list;
1413 }
1414
1415 void compose_entry_append(Compose *compose, const gchar *address,
1416                           ComposeEntryType type)
1417 {
1418         GtkEntry *entry;
1419         const gchar *text;
1420         gchar *header;
1421
1422         if (!address || *address == '\0') return;
1423
1424 #if 0 /* NEW COMPOSE GUI */
1425         switch (type) {
1426         case COMPOSE_CC:
1427                 entry = GTK_ENTRY(compose->cc_entry);
1428                 break;
1429         case COMPOSE_BCC:
1430                 entry = GTK_ENTRY(compose->bcc_entry);
1431                 break;
1432         case COMPOSE_NEWSGROUPS:
1433                 entry = GTK_ENTRY(compose->newsgroups_entry);
1434                 break;
1435         case COMPOSE_TO:
1436         default:
1437                 entry = GTK_ENTRY(compose->to_entry);
1438                 break;
1439         }
1440
1441         text = gtk_entry_get_text(entry);
1442         if (*text != '\0')
1443                 gtk_entry_append_text(entry, ", ");
1444         gtk_entry_append_text(entry, address);
1445 #endif
1446
1447         switch (type) {
1448         case COMPOSE_CC:
1449                 header = N_("Cc:");
1450                 break;
1451         case COMPOSE_BCC:
1452                 header = N_("Bcc:");
1453                 break;
1454         case COMPOSE_REPLYTO:
1455                 header = N_("Reply-To:");
1456                 break;
1457         case COMPOSE_NEWSGROUPS:
1458                 header = N_("Newsgroups:");
1459                 break;
1460         case COMPOSE_FOLLOWUPTO:
1461                 header = N_( "Followup-To:");
1462                 break;
1463         case COMPOSE_TO:
1464         default:
1465                 header = N_("To:");
1466                 break;
1467         }
1468         header = prefs_common.trans_hdr ? gettext(header) : header;
1469
1470         compose_add_header_entry(compose, header, (gchar *)address);
1471 }
1472
1473 static void compose_entries_set(Compose *compose, const gchar *mailto)
1474 {
1475         gchar *subject = NULL;
1476         gchar *to = NULL;
1477         gchar *cc = NULL;
1478         gchar *bcc = NULL;
1479         gchar *body = NULL;
1480         gchar *p;
1481         gchar *tmp_mailto;
1482
1483         Xstrdup_a(tmp_mailto, mailto, return);
1484
1485         to = tmp_mailto;
1486
1487         p = strchr(tmp_mailto, '?');
1488         if (p) {
1489                 *p = '\0';
1490                 p++;
1491         }
1492
1493         while (p) {
1494                 gchar *field, *value;
1495
1496                 field = p;
1497
1498                 p = strchr(p, '=');
1499                 if (!p) break;
1500                 *p = '\0';
1501                 p++;
1502
1503                 value = p;
1504
1505                 p = strchr(p, '&');
1506                 if (p) {
1507                         *p = '\0';
1508                         p++;
1509                 }
1510
1511                 if (*value == '\0') continue;
1512
1513                 if (!g_strcasecmp(field, "subject")) {
1514                         Xalloca(subject, strlen(value) + 1, return);
1515                         decode_uri(subject, value);
1516                 } else if (!g_strcasecmp(field, "cc")) {
1517                         cc = value;
1518                 } else if (!g_strcasecmp(field, "bcc")) {
1519                         bcc = value;
1520                 } else if (!g_strcasecmp(field, "body")) {
1521                         Xalloca(body, strlen(value) + 1, return);
1522                         decode_uri(body, value);
1523                 }
1524         }
1525
1526         if (to)
1527                 compose_entry_append(compose, to, COMPOSE_TO);
1528         if (subject)
1529                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), subject);
1530         if (cc)
1531                 compose_entry_append(compose, cc, COMPOSE_CC);
1532         if (bcc)
1533                 compose_entry_append(compose, bcc, COMPOSE_BCC);
1534         if (body) {
1535                 gtk_stext_insert(GTK_STEXT(compose->text),
1536                                 NULL, NULL, NULL, body, -1);
1537                 gtk_stext_insert(GTK_STEXT(compose->text),
1538                                 NULL, NULL, NULL, "\n", 1);
1539         }
1540 }
1541
1542 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
1543 {
1544         static HeaderEntry hentry[] = {{"Reply-To:",       NULL, TRUE},
1545                                        {"Cc:",             NULL, FALSE},
1546                                        {"References:",     NULL, FALSE},
1547                                        {"Bcc:",            NULL, FALSE},
1548                                        {"Newsgroups:",     NULL, FALSE},
1549                                        {"Followup-To:",    NULL, FALSE},
1550                                        {"X-Mailing-List:", NULL, FALSE},
1551                                        {"X-BeenThere:",    NULL, FALSE},
1552                                        {NULL,              NULL, FALSE}};
1553
1554         enum
1555         {
1556                 H_REPLY_TO       = 0,
1557                 H_CC             = 1,
1558                 H_REFERENCES     = 2,
1559                 H_BCC            = 3,
1560                 H_NEWSGROUPS     = 4,
1561                 H_FOLLOWUP_TO    = 5,
1562                 H_X_MAILING_LIST = 6,
1563                 H_X_BEENTHERE    = 7
1564         };
1565
1566         FILE *fp;
1567
1568         g_return_val_if_fail(msginfo != NULL, -1);
1569
1570         if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
1571         procheader_get_header_fields(fp, hentry);
1572         fclose(fp);
1573
1574         if (hentry[H_REPLY_TO].body != NULL) {
1575                 conv_unmime_header_overwrite(hentry[H_REPLY_TO].body);
1576                 compose->replyto = hentry[H_REPLY_TO].body;
1577                 hentry[H_REPLY_TO].body = NULL;
1578         }
1579         if (hentry[H_CC].body != NULL) {
1580                 conv_unmime_header_overwrite(hentry[H_CC].body);
1581                 compose->cc = hentry[H_CC].body;
1582                 hentry[H_CC].body = NULL;
1583         }
1584         if (hentry[H_X_MAILING_LIST].body != NULL) {
1585                 /* this is good enough to parse debian-devel */
1586                 gchar *buf = g_malloc(strlen(hentry[H_X_MAILING_LIST].body) + 1);
1587                 g_return_val_if_fail(buf != NULL, -1 );
1588                 if (1 == sscanf(hentry[H_X_MAILING_LIST].body, "<%[^>]>", buf))
1589                         compose->mailinglist = g_strdup(buf);
1590                 g_free(buf);
1591                 g_free(hentry[H_X_MAILING_LIST].body);
1592                 hentry[H_X_MAILING_LIST].body = NULL ;
1593         }
1594         if (hentry[H_X_BEENTHERE].body != NULL) {
1595                 /* this is good enough to parse the sylpheed-claws lists */
1596                 gchar *buf = g_malloc(strlen(hentry[H_X_BEENTHERE].body) + 1);
1597                 g_return_val_if_fail(buf != NULL, -1 );
1598                 if (1 == sscanf(hentry[H_X_BEENTHERE].body, "%[^>]", buf))
1599                         compose->mailinglist = g_strdup(buf);
1600                 g_free(buf);
1601                 g_free(hentry[H_X_BEENTHERE].body);
1602                 hentry[H_X_BEENTHERE].body = NULL ;
1603         }
1604         if (hentry[H_REFERENCES].body != NULL) {
1605                 if (compose->mode == COMPOSE_REEDIT)
1606                         compose->references = hentry[H_REFERENCES].body;
1607                 else {
1608                         compose->references = compose_parse_references
1609                                 (hentry[H_REFERENCES].body, msginfo->msgid);
1610                         g_free(hentry[H_REFERENCES].body);
1611                 }
1612                 hentry[H_REFERENCES].body = NULL;
1613         }
1614         if (hentry[H_BCC].body != NULL) {
1615                 if (compose->mode == COMPOSE_REEDIT) {
1616                         conv_unmime_header_overwrite(hentry[H_BCC].body);
1617                         compose->bcc = hentry[H_BCC].body;
1618                 } else
1619                         g_free(hentry[H_BCC].body);
1620                 hentry[H_BCC].body = NULL;
1621         }
1622         if (hentry[H_NEWSGROUPS].body != NULL) {
1623                 compose->newsgroups = hentry[H_NEWSGROUPS].body;
1624                 hentry[H_NEWSGROUPS].body = NULL;
1625         }
1626         if (hentry[H_FOLLOWUP_TO].body != NULL) {
1627                 conv_unmime_header_overwrite(hentry[H_FOLLOWUP_TO].body);
1628                 compose->followup_to = hentry[H_FOLLOWUP_TO].body;
1629                 hentry[H_FOLLOWUP_TO].body = NULL;
1630         }
1631
1632         if (compose->mode == COMPOSE_REEDIT && msginfo->inreplyto)
1633                 compose->inreplyto = g_strdup(msginfo->inreplyto);
1634         else if (compose->mode != COMPOSE_REEDIT &&
1635                  msginfo->msgid && *msginfo->msgid) {
1636                 compose->inreplyto = g_strdup(msginfo->msgid);
1637
1638                 if (!compose->references) {
1639                         if (msginfo->inreplyto && *msginfo->inreplyto)
1640                                 compose->references =
1641                                         g_strdup_printf("<%s>\n\t<%s>",
1642                                                         msginfo->inreplyto,
1643                                                         msginfo->msgid);
1644                         else
1645                                 compose->references =
1646                                         g_strconcat("<", msginfo->msgid, ">",
1647                                                     NULL);
1648                 }
1649         }
1650
1651         return 0;
1652 }
1653
1654 static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
1655 {
1656         GSList *ref_id_list, *cur;
1657         GString *new_ref;
1658         gchar *new_ref_str;
1659
1660         ref_id_list = references_list_append(NULL, ref);
1661         if (!ref_id_list) return NULL;
1662         if (msgid && *msgid)
1663                 ref_id_list = g_slist_append(ref_id_list, g_strdup(msgid));
1664
1665         for (;;) {
1666                 gint len = 0;
1667
1668                 for (cur = ref_id_list; cur != NULL; cur = cur->next)
1669                         /* "<" + Message-ID + ">" + CR+LF+TAB */
1670                         len += strlen((gchar *)cur->data) + 5;
1671
1672                 if (len > MAX_REFERENCES_LEN) {
1673                         /* remove second message-ID */
1674                         if (ref_id_list && ref_id_list->next &&
1675                             ref_id_list->next->next) {
1676                                 g_free(ref_id_list->next->data);
1677                                 ref_id_list = g_slist_remove
1678                                         (ref_id_list, ref_id_list->next->data);
1679                         } else {
1680                                 slist_free_strings(ref_id_list);
1681                                 g_slist_free(ref_id_list);
1682                                 return NULL;
1683                         }
1684                 } else
1685                         break;
1686         }
1687
1688         new_ref = g_string_new("");
1689         for (cur = ref_id_list; cur != NULL; cur = cur->next) {
1690                 if (new_ref->len > 0)
1691                         g_string_append(new_ref, "\n\t");
1692                 g_string_sprintfa(new_ref, "<%s>", (gchar *)cur->data);
1693         }
1694
1695         slist_free_strings(ref_id_list);
1696         g_slist_free(ref_id_list);
1697
1698         new_ref_str = new_ref->str;
1699         g_string_free(new_ref, FALSE);
1700
1701         return new_ref_str;
1702 }
1703
1704 static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
1705                                 const gchar *fmt, const gchar *qmark)
1706 {
1707         GtkSText *text = GTK_STEXT(compose->text);
1708         gchar *quote_str = NULL;
1709         gchar *buf;
1710         gchar *p, *lastp;
1711         gint len;
1712
1713         if (qmark != NULL) {
1714                 quote_fmt_init(msginfo, NULL);
1715                 quote_fmt_scan_string(qmark);
1716                 quote_fmt_parse();
1717
1718                 buf = quote_fmt_get_buffer();
1719                 if (buf == NULL)
1720                         alertpanel_error(_("Quote mark format error."));
1721                 else
1722                         Xstrdup_a(quote_str, buf, return NULL)
1723         }
1724
1725         if (fmt && *fmt != '\0') {
1726                 quote_fmt_init(msginfo, quote_str);
1727                 quote_fmt_scan_string(fmt);
1728                 quote_fmt_parse();
1729
1730                 buf = quote_fmt_get_buffer();
1731                 if (buf == NULL) {
1732                         alertpanel_error(_("Message reply/forward format error."));
1733                         return NULL;
1734                 }
1735         } else
1736                 buf = "";
1737
1738         gtk_stext_freeze(text);
1739
1740         for (p = buf; *p != '\0'; ) {
1741                 lastp = strchr(p, '\n');
1742                 len = lastp ? lastp - p + 1 : -1;
1743                 gtk_stext_insert(text, NULL, NULL, NULL, p, len);
1744                 if (lastp)
1745                         p = lastp + 1;
1746                 else
1747                         break;
1748         }
1749
1750         gtk_stext_thaw(text);
1751
1752         return buf;
1753 }
1754
1755 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
1756                                     gboolean to_all, gboolean ignore_replyto,
1757                                     gboolean followup_and_reply_to)
1758 {
1759         GSList *cc_list;
1760         GSList *cur;
1761         gchar *from;
1762         GHashTable *to_table;
1763
1764         g_return_if_fail(compose->account != NULL);
1765         g_return_if_fail(msginfo != NULL);
1766
1767         if ((compose->account->protocol != A_NNTP) || followup_and_reply_to)
1768                 compose_entry_append(compose,
1769                                     ((compose->replyto && !ignore_replyto)
1770                                      ? compose->replyto
1771                                      : (compose->mailinglist && !ignore_replyto)
1772                                      ? compose->mailinglist
1773                                      : msginfo->from ? msginfo->from : ""),
1774                                      COMPOSE_TO);
1775
1776         if (compose->replyto && to_all)
1777                 compose_entry_append
1778                         (compose, compose->replyto, COMPOSE_CC);
1779
1780
1781         if (compose->account->protocol == A_NNTP) {
1782                 if (ignore_replyto)
1783                         compose_entry_append
1784                                 (compose, msginfo->from ? msginfo->from : "",
1785                                  COMPOSE_TO);
1786                 else
1787                         compose_entry_append
1788                                 (compose,
1789                                  compose->followup_to ? compose->followup_to
1790                                  : compose->newsgroups ? compose->newsgroups
1791                                  : "",
1792                                  COMPOSE_NEWSGROUPS);
1793         }
1794
1795         if (msginfo->subject && *msginfo->subject) {
1796                 gchar *buf, *buf2, *p;
1797
1798                 buf = g_strdup(msginfo->subject);
1799                 while (!strncasecmp(buf, "Re:", 3)) {
1800                         p = buf + 3;
1801                         while (isspace(*p)) p++;
1802                         memmove(buf, p, strlen(p) + 1);
1803                 }
1804
1805                 buf2 = g_strdup_printf("Re: %s", buf);
1806                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1807                 g_free(buf2);
1808                 g_free(buf);
1809         } else
1810                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
1811
1812         if (!to_all || compose->account->protocol == A_NNTP) return;
1813
1814         from = g_strdup(compose->replyto ? compose->replyto :
1815                         msginfo->from ? msginfo->from : "");
1816         extract_address(from);
1817
1818         cc_list = address_list_append(NULL, msginfo->to);
1819         cc_list = address_list_append(cc_list, compose->cc);
1820
1821         to_table = g_hash_table_new(g_str_hash, g_str_equal);
1822         g_hash_table_insert(to_table, from, GINT_TO_POINTER(1));
1823         if (compose->account)
1824                 g_hash_table_insert(to_table, compose->account->address,
1825                                     GINT_TO_POINTER(1));
1826
1827         /* remove address on To: and that of current account */
1828         for (cur = cc_list; cur != NULL; ) {
1829                 GSList *next = cur->next;
1830
1831                 if (g_hash_table_lookup(to_table, cur->data) != NULL)
1832                         cc_list = g_slist_remove(cc_list, cur->data);
1833                 else
1834                         g_hash_table_insert(to_table, cur->data, cur);
1835
1836                 cur = next;
1837         }
1838         g_hash_table_destroy(to_table);
1839         g_free(from);
1840
1841         if (cc_list) {
1842                 for (cur = cc_list; cur != NULL; cur = cur->next)
1843                         compose_entry_append(compose, (gchar *)cur->data,
1844                                              COMPOSE_CC);
1845                 slist_free_strings(cc_list);
1846                 g_slist_free(cc_list);
1847         }
1848 }
1849
1850 #define SET_ENTRY(entry, str) \
1851 { \
1852         if (str && *str) \
1853                 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
1854 }
1855
1856 #define SET_ADDRESS(type, str) \
1857 { \
1858         if (str && *str) \
1859                 compose_entry_append(compose, str, type); \
1860 }
1861
1862 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
1863 {
1864         g_return_if_fail(msginfo != NULL);
1865
1866         SET_ENTRY(subject_entry, msginfo->subject);
1867         SET_ADDRESS(COMPOSE_TO, msginfo->to);
1868         SET_ADDRESS(COMPOSE_CC, compose->cc);
1869         SET_ADDRESS(COMPOSE_BCC, compose->bcc);
1870         SET_ADDRESS(COMPOSE_REPLYTO, compose->replyto);
1871
1872         compose_show_first_last_header(compose, TRUE);
1873
1874 #if 0 /* NEW COMPOSE GUI */
1875         if (compose->bcc) {
1876                 GtkItemFactory *ifactory;
1877                 GtkWidget *menuitem;
1878
1879                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1880                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Bcc");
1881                 gtk_check_menu_item_set_active
1882                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1883         }
1884         if (compose->replyto) {
1885                 GtkItemFactory *ifactory;
1886                 GtkWidget *menuitem;
1887
1888                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1889                 menuitem = gtk_item_factory_get_item
1890                         (ifactory, "/View/Reply to");
1891                 gtk_check_menu_item_set_active
1892                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1893         }
1894 #endif
1895 }
1896
1897 #undef SET_ENTRY
1898 #undef SET_ADDRESS
1899
1900 static void compose_exec_sig(Compose *compose, gchar *sigfile)
1901 {
1902         FILE *tmpfp;
1903         pid_t thepid;
1904         gchar *sigtext;
1905         FILE  *sigprg;
1906         gchar  *buf;
1907         size_t buf_len = 128;
1908  
1909         if (strlen(sigfile) < 2)
1910           return;
1911  
1912         sigprg = popen(sigfile+1, "r");
1913         if (sigprg) {
1914
1915                 buf = g_malloc(buf_len);
1916
1917                 if (!buf) {
1918                         gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1919                         "Unable to insert signature (malloc failed)\n", -1);
1920
1921                         pclose(sigprg);
1922                         return;
1923                 }
1924
1925                 while (!feof(sigprg)) {
1926                         bzero(buf, buf_len);
1927                         fread(buf, buf_len-1, 1, sigprg);
1928                         gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, buf, -1);
1929                 }
1930
1931                 g_free(buf);
1932                 pclose(sigprg);
1933         }
1934         else
1935         {
1936                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1937                 "Can't exec file: ", -1);
1938                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, \
1939                 sigfile+1, -1);
1940         }
1941 }
1942
1943 static void compose_insert_sig(Compose *compose)
1944 {
1945         gchar *sigfile;
1946
1947         if (compose->account && compose->account->sig_path)
1948                 sigfile = g_strdup(compose->account->sig_path);
1949         else
1950                 sigfile = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1951                                       DEFAULT_SIGNATURE, NULL);
1952
1953         if (!is_file_or_fifo_exist(sigfile) && sigfile[0] != '|') {
1954                 g_free(sigfile);
1955                 return;
1956         }
1957
1958         gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, "\n\n", 2);
1959         if (prefs_common.sig_sep) {
1960                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1961                                 prefs_common.sig_sep, -1);
1962                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1963                                 "\n", 1);
1964         }
1965
1966         if (sigfile[0] == '|')
1967                 compose_exec_sig(compose, sigfile);
1968         else
1969                 compose_insert_file(compose, sigfile);
1970         g_free(sigfile);
1971 }
1972
1973 static void compose_insert_file(Compose *compose, const gchar *file)
1974 {
1975         GtkSText *text = GTK_STEXT(compose->text);
1976         gchar buf[BUFFSIZE];
1977         gint len;
1978         FILE *fp;
1979
1980         g_return_if_fail(file != NULL);
1981
1982         if ((fp = fopen(file, "r")) == NULL) {
1983                 FILE_OP_ERROR(file, "fopen");
1984                 return;
1985         }
1986
1987         gtk_stext_freeze(text);
1988
1989         while (fgets(buf, sizeof(buf), fp) != NULL) {
1990                 /* strip <CR> if DOS/Windows file,
1991                    replace <CR> with <LF> if Macintosh file. */
1992                 strcrchomp(buf);
1993                 len = strlen(buf);
1994                 if (len > 0 && buf[len - 1] != '\n') {
1995                         while (--len >= 0)
1996                                 if (buf[len] == '\r') buf[len] = '\n';
1997                 }
1998                 gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1999         }
2000
2001         gtk_stext_thaw(text);
2002
2003         fclose(fp);
2004 }
2005
2006 static void compose_attach_info(Compose * compose, AttachInfo * ainfo,
2007                                 ContentType cnttype)
2008 {
2009         gchar *text[N_ATTACH_COLS];
2010         gint row;
2011
2012         text[COL_MIMETYPE] = ainfo->content_type;
2013         text[COL_SIZE] = to_human_readable(ainfo->size);
2014         text[COL_NAME] = ainfo->name;
2015
2016         row = gtk_clist_append(GTK_CLIST(compose->attach_clist), text);
2017         gtk_clist_set_row_data(GTK_CLIST(compose->attach_clist), row, ainfo);
2018
2019         if (cnttype != MIME_MESSAGE_RFC822)
2020                 compose_changed_cb(NULL, compose);
2021 }
2022
2023 static void compose_attach_append_with_type(Compose *compose,
2024                                             const gchar *file,
2025                                             const gchar *type,
2026                                             ContentType cnttype)
2027 {
2028         AttachInfo *ainfo;
2029         off_t size;
2030
2031         if (!is_file_exist(file)) {
2032                 g_warning(_("File %s doesn't exist\n"), file);
2033                 return;
2034         }
2035         if ((size = get_file_size(file)) < 0) {
2036                 g_warning(_("Can't get file size of %s\n"), file);
2037                 return;
2038         }
2039         if (size == 0) {
2040                 alertpanel_notice(_("File %s is empty\n"), file);
2041                 return;
2042         }
2043 #if 0 /* NEW COMPOSE GUI */
2044         if (!compose->use_attach) {
2045                 GtkItemFactory *ifactory;
2046                 GtkWidget *menuitem;
2047
2048                 ifactory = gtk_item_factory_from_widget(compose->menubar);
2049                 menuitem = gtk_item_factory_get_item(ifactory,
2050                                                      "/Message/Attach");
2051                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2052                                                TRUE);
2053         }
2054 #endif
2055         ainfo = g_new0(AttachInfo, 1);
2056         ainfo->file = g_strdup(file);
2057
2058         if (cnttype == MIME_MESSAGE_RFC822) {
2059                 ainfo->encoding = ENC_7BIT;
2060                 ainfo->name = g_strdup_printf(_("Message: %s"),
2061                                               g_basename(file));
2062         } else {
2063                 ainfo->encoding = ENC_BASE64;
2064                 ainfo->name = g_strdup(g_basename(file));
2065         }
2066
2067         ainfo->content_type = g_strdup(type);
2068         ainfo->size = size;
2069
2070         compose_attach_info(compose, ainfo, cnttype);
2071 }
2072
2073 static void compose_attach_append(Compose *compose, const gchar *file,
2074                                   ContentType cnttype)
2075 {
2076         AttachInfo *ainfo;
2077         gchar *text[N_ATTACH_COLS];
2078         off_t size;
2079         gint row;
2080
2081         if (!is_file_exist(file)) {
2082                 g_warning(_("File %s doesn't exist\n"), file);
2083                 return;
2084         }
2085         if ((size = get_file_size(file)) < 0) {
2086                 g_warning(_("Can't get file size of %s\n"), file);
2087                 return;
2088         }
2089         if (size == 0) {
2090                 alertpanel_notice(_("File %s is empty\n"), file);
2091                 return;
2092         }
2093 #if 0 /* NEW COMPOSE GUI */
2094         if (!compose->use_attach) {
2095                 GtkItemFactory *ifactory;
2096                 GtkWidget *menuitem;
2097
2098                 ifactory = gtk_item_factory_from_widget(compose->menubar);
2099                 menuitem = gtk_item_factory_get_item(ifactory,
2100                                                      "/View/Attachment");
2101                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2102                                                TRUE);
2103         }
2104 #endif
2105         ainfo = g_new0(AttachInfo, 1);
2106         ainfo->file = g_strdup(file);
2107
2108         if (cnttype == MIME_MESSAGE_RFC822) {
2109                 ainfo->content_type = g_strdup("message/rfc822");
2110                 ainfo->encoding = ENC_7BIT;
2111                 ainfo->name = g_strdup_printf(_("Message: %s"),
2112                                               g_basename(file));
2113         } else {
2114                 ainfo->content_type = procmime_get_mime_type(file);
2115                 if (!ainfo->content_type)
2116                         ainfo->content_type =
2117                                 g_strdup("application/octet-stream");
2118                 ainfo->encoding = ENC_BASE64;
2119                 ainfo->name = g_strdup(g_basename(file));
2120         }
2121         ainfo->size = size;
2122
2123         compose_attach_info(compose, ainfo, cnttype);
2124 }
2125
2126 #define GET_CHAR(pos, buf, len)                                              \
2127 {                                                                            \
2128         if (text->use_wchar)                                                 \
2129                 len = wctomb(buf, (wchar_t)GTK_STEXT_INDEX(text, (pos)));    \
2130         else {                                                               \
2131                 buf[0] = GTK_STEXT_INDEX(text, (pos));                       \
2132                 len = 1;                                                     \
2133         }                                                                    \
2134 }
2135
2136 static void compose_wrap_line(Compose *compose)
2137 {
2138         GtkSText *text = GTK_STEXT(compose->text);
2139         gint ch_len, last_ch_len;
2140         gchar cbuf[MB_LEN_MAX], last_ch;
2141         guint text_len;
2142         guint line_end;
2143         guint quoted;
2144         gint p_start, p_end;
2145         gint line_pos, cur_pos;
2146         gint line_len, cur_len;
2147
2148         gtk_stext_freeze(text);
2149
2150         text_len = gtk_stext_get_length(text);
2151
2152         /* check to see if the point is on the paragraph mark (empty line). */
2153         cur_pos = gtk_stext_get_point(text);
2154         GET_CHAR(cur_pos, cbuf, ch_len);
2155         if ((ch_len == 1 && *cbuf == '\n') || cur_pos == text_len) {
2156                 if (cur_pos == 0)
2157                         goto compose_end; /* on the paragraph mark */
2158                 GET_CHAR(cur_pos - 1, cbuf, ch_len);
2159                 if (ch_len == 1 && *cbuf == '\n')
2160                         goto compose_end; /* on the paragraph mark */
2161         }
2162
2163         /* find paragraph start. */
2164         line_end = quoted = 0;
2165         for (p_start = cur_pos; p_start >= 0; --p_start) {
2166                 GET_CHAR(p_start, cbuf, ch_len);
2167                 if (ch_len == 1 && *cbuf == '\n') {
2168                         if (quoted)
2169                                 goto compose_end; /* quoted part */
2170                         if (line_end) {
2171                                 p_start += 2;
2172                                 break;
2173                         }
2174                         line_end = 1;
2175                 } else {
2176                         if (ch_len == 1 && strchr(">:#", *cbuf))
2177                                 quoted = 1;
2178                         else if (ch_len != 1 || !isspace(*cbuf))
2179                                 quoted = 0;
2180
2181                         line_end = 0;
2182                 }
2183         }
2184         if (p_start < 0)
2185                 p_start = 0;
2186
2187         /* find paragraph end. */
2188         line_end = 0;
2189         for (p_end = cur_pos; p_end < text_len; p_end++) {
2190                 GET_CHAR(p_end, cbuf, ch_len);
2191                 if (ch_len == 1 && *cbuf == '\n') {
2192                         if (line_end) {
2193                                 p_end -= 1;
2194                                 break;
2195                         }
2196                         line_end = 1;
2197                 } else {
2198                         if (line_end && ch_len == 1 && strchr(">:#", *cbuf))
2199                                 goto compose_end; /* quoted part */
2200
2201                         line_end = 0;
2202                 }
2203         }
2204         if (p_end >= text_len)
2205                 p_end = text_len;
2206
2207         if (p_start >= p_end)
2208                 goto compose_end;
2209
2210         line_len = cur_len = 0;
2211         last_ch_len = 0;
2212         last_ch = '\0';
2213         line_pos = p_start;
2214         for (cur_pos = p_start; cur_pos < p_end; cur_pos++) {
2215                 guint space = 0;
2216
2217                 GET_CHAR(cur_pos, cbuf, ch_len);
2218
2219                 if (ch_len < 0) {
2220                         cbuf[0] = '\0';
2221                         ch_len = 1;
2222                 }
2223
2224                 if (ch_len == 1 && isspace(*cbuf))
2225                         space = 1;
2226
2227                 if (ch_len == 1 && *cbuf == '\n') {
2228                         guint replace = 0;
2229                         if (last_ch_len == 1 && !isspace(last_ch)) {
2230                                 if (cur_pos + 1 < p_end) {
2231                                         GET_CHAR(cur_pos + 1, cbuf, ch_len);
2232                                         if (ch_len == 1 && !isspace(*cbuf))
2233                                                 replace = 1;
2234                                 }
2235                         }
2236                         gtk_stext_set_point(text, cur_pos + 1);
2237                         gtk_stext_backward_delete(text, 1);
2238                         if (replace) {
2239                                 gtk_stext_set_point(text, cur_pos);
2240                                 gtk_stext_insert(text, NULL, NULL, NULL, " ", 1);
2241                                 space = 1;
2242                         }
2243                         else {
2244                                 p_end--;
2245                                 cur_pos--;
2246                                 continue;
2247                         }
2248                 }
2249
2250                 last_ch_len = ch_len;
2251                 last_ch = *cbuf;
2252
2253                 if (space) {
2254                         line_pos = cur_pos + 1;
2255                         line_len = cur_len + ch_len;
2256                 }
2257
2258                 if (cur_len + ch_len > prefs_common.linewrap_len &&
2259                     line_len > 0) {
2260                         gint tlen = ch_len;
2261
2262                         GET_CHAR(line_pos - 1, cbuf, ch_len);
2263                         if (ch_len == 1 && isspace(*cbuf)) {
2264                                 gtk_stext_set_point(text, line_pos);
2265                                 gtk_stext_backward_delete(text, 1);
2266                                 p_end--;
2267                                 cur_pos--;
2268                                 line_pos--;
2269                                 cur_len--;
2270                                 line_len--;
2271                         }
2272                         ch_len = tlen;
2273
2274                         gtk_stext_set_point(text, line_pos);
2275                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2276                         p_end++;
2277                         cur_pos++;
2278                         line_pos++;
2279                         cur_len = cur_len - line_len + ch_len;
2280                         line_len = 0;
2281                         continue;
2282                 }
2283
2284                 if (ch_len > 1) {
2285                         line_pos = cur_pos + 1;
2286                         line_len = cur_len + ch_len;
2287                 }
2288                 cur_len += ch_len;
2289         }
2290
2291 compose_end:
2292         gtk_stext_thaw(text);
2293 }
2294
2295 /* return indent length */
2296 static guint get_indent_length(GtkSText *text, guint start_pos, guint text_len)
2297 {
2298         gint indent_len = 0;
2299         gint i, ch_len;
2300         gchar cbuf[MB_LEN_MAX];
2301
2302         for (i = start_pos; i < text_len; i++) {
2303                 GET_CHAR(i, cbuf, ch_len);
2304                 if (ch_len > 1)
2305                         break;
2306                 /* allow space, tab, > or | */
2307                 if (cbuf[0] != ' ' && cbuf[0] != '\t' &&
2308                     cbuf[0] != '>' && cbuf[0] != '|')
2309                         break;
2310                 indent_len++;
2311         }
2312
2313         return indent_len;
2314 }
2315
2316 /* insert quotation string when line was wrapped */
2317 static guint ins_quote(GtkSText *text, guint quote_len, guint indent_len,
2318                        guint prev_line_pos, guint text_len,
2319                        gchar *quote_fmt)
2320 {
2321         guint i, ins_len;
2322         gchar ch;
2323
2324         if (indent_len) {
2325                 for (i = 0; i < indent_len; i++) {
2326                         ch = GTK_STEXT_INDEX(text, prev_line_pos + i);
2327                         gtk_stext_insert(text, NULL, NULL, NULL, &ch, 1);
2328                 }
2329                 ins_len = indent_len;
2330         } else {
2331                 gtk_stext_insert(text, NULL, NULL, NULL, quote_fmt, quote_len);
2332                 ins_len = quote_len;
2333         }
2334
2335         return ins_len;
2336 }
2337
2338 #undef WRAP_DEBUG
2339 #ifdef WRAP_DEBUG
2340 /* Darko: used when I debug wrapping */
2341 void dump_text(GtkSText *text, int pos, int tlen, int breakoncr)
2342 {
2343         gint i;
2344         gchar ch;
2345
2346         printf("%d [", pos);
2347         for (i = pos; i < tlen; i++) {
2348                 ch = GTK_STEXT_INDEX(text, i);
2349                 if (breakoncr && ch == '\n')
2350                         break;
2351                 printf("%c", ch);
2352         }
2353         printf("]\n");
2354 }
2355 #endif
2356
2357 static void compose_wrap_line_all(Compose *compose)
2358 {
2359         GtkSText *text = GTK_STEXT(compose->text);
2360         guint tlen;
2361         guint line_pos = 0, cur_pos = 0, p_pos = 0;
2362         gint line_len = 0, cur_len = 0;
2363         gint ch_len;
2364         gboolean is_new_line = TRUE, do_delete = FALSE;
2365         guint qlen = 0, i_len = 0;
2366         gboolean linewrap_quote = TRUE;
2367         guint linewrap_len = prefs_common.linewrap_len;
2368         gchar *qfmt = prefs_common.quotemark;
2369         gchar cbuf[MB_LEN_MAX];
2370
2371         gtk_stext_freeze(text);
2372
2373         /* make text buffer contiguous */
2374         /* gtk_stext_compact_buffer(text); */
2375
2376         tlen = gtk_stext_get_length(text);
2377
2378         for (; cur_pos < tlen; cur_pos++) {
2379                 /* mark position of new line - needed for quotation wrap */
2380                 if (is_new_line) {
2381                         if (linewrap_quote) {
2382                                 qlen = gtkut_stext_str_compare
2383                                         (text, cur_pos, tlen, qfmt);
2384                                 if (qlen)
2385                                         i_len = get_indent_length
2386                                                 (text, cur_pos, tlen);
2387                                 else
2388                                         i_len = 0;
2389                         }
2390                         is_new_line = FALSE;
2391                         p_pos = cur_pos;
2392 #ifdef WRAP_DEBUG
2393                         printf("new line i_len=%d qlen=%d p_pos=", i_len, qlen);
2394                         dump_text(text, p_pos, tlen, 1);
2395 #endif
2396                 }
2397
2398                 GET_CHAR(cur_pos, cbuf, ch_len);
2399
2400                 /* fix line length for tabs */
2401                 if (ch_len == 1 && *cbuf == '\t') {
2402                         guint tab_width = text->default_tab_width;
2403                         guint tab_offset = line_len % tab_width;
2404
2405 #ifdef WRAP_DEBUG
2406                         printf("found tab at pos=%d line_len=%d ", cur_pos,
2407                                 line_len);
2408 #endif
2409                         if (tab_offset) {
2410                                 line_len += tab_width - tab_offset - 1;
2411                                 cur_len = line_len;
2412                         }
2413 #ifdef WRAP_DEBUG
2414                         printf("new_len=%d\n", line_len);
2415 #endif
2416                 }
2417
2418                 /* we have encountered line break */
2419                 if (ch_len == 1 && *cbuf == '\n') {
2420                         gint clen;
2421                         guint ilen;
2422                         gchar cb[MB_CUR_MAX];
2423
2424 #ifdef WRAP_DEBUG
2425                         printf("found CR at %d next line is ", cur_pos);
2426                         dump_text(text, cur_pos + 1, tlen, 1);
2427 #endif
2428                         /* if it's just quotation + newline skip it */
2429                         if (i_len && (cur_pos + 1 < tlen)) {
2430                                 /* check if text at new line matches indent */
2431                                 ilen =  gtkut_stext_str_compare_n
2432                                         (text, cur_pos + 1, p_pos, i_len, tlen);
2433                                 if (cur_pos + ilen < tlen) {
2434                                         GET_CHAR(cur_pos + ilen + 1, cb, clen);
2435                                         /* no need to join the lines */
2436                                         if (clen == 1 && cb[0] == '\n')
2437                                                 do_delete = FALSE;
2438                                 }
2439                         /* if it's just newline skip it */
2440                         } else if (do_delete && (cur_pos + 1 < tlen)) {
2441                                 GET_CHAR(cur_pos + 1, cb, clen);
2442                                 /* no need to join the lines */
2443                                 if (clen == 1 && cb[0] == '\n')
2444                                         do_delete = FALSE;
2445                         }
2446
2447                         /* skip delete if it is continuous URL */
2448                         if (do_delete && (line_pos - p_pos <= i_len) &&
2449                             gtkut_stext_is_uri_string(text, line_pos, tlen))
2450                                 do_delete = FALSE;
2451
2452 #ifdef WRAP_DEBUG
2453                         printf("qlen=%d l_len=%d wrap_len=%d do_del=%d\n",
2454                                 qlen, line_len, linewrap_len, do_delete);
2455 #endif
2456                         /* should we delete to perform smart wrapping */
2457                         if (line_len < linewrap_len && do_delete) {
2458                                 /* get rid of newline */
2459                                 gtk_stext_set_point(text, cur_pos);
2460                                 gtk_stext_forward_delete(text, 1);
2461                                 tlen--;
2462
2463                                 /* if text starts with quote fmt or with
2464                                    indent string, delete them */
2465                                 if (i_len) {
2466                                         ilen =  gtkut_stext_str_compare_n
2467                                                 (text, cur_pos, p_pos, i_len,
2468                                                  tlen);
2469                                         if (ilen) {
2470                                                 gtk_stext_forward_delete
2471                                                         (text, ilen);
2472                                                 tlen -= ilen;
2473                                         }
2474                                 } else if (qlen) {
2475                                         if (gtkut_stext_str_compare
2476                                             (text, cur_pos, tlen, qfmt)) {
2477                                                 gtk_stext_forward_delete
2478                                                         (text, qlen);
2479                                                 tlen -= qlen;
2480                                         }
2481                                 }
2482
2483                                 GET_CHAR(cur_pos, cb, clen);
2484
2485                                 /* insert space if it's alphanumeric */
2486                                 if ((cur_pos != line_pos) &&
2487                                     ((clen > 1) || isalnum(cb[0]))) {
2488                                         gtk_stext_insert(text, NULL, NULL,
2489                                                         NULL, " ", 1);
2490                                         /* gtk_stext_compact_buffer(text); */
2491                                         tlen++;
2492                                 }
2493
2494                                 /* and start over with current line */
2495                                 cur_pos = p_pos - 1;
2496                                 line_pos = cur_pos;
2497                                 line_len = cur_len = 0;
2498                                 qlen = 0;
2499                                 do_delete = FALSE;
2500                                 is_new_line = TRUE;
2501 #ifdef WRAP_DEBUG
2502                                 printf("after delete l_pos=");
2503                                 dump_text(text, line_pos, tlen, 1);
2504 #endif
2505                                 continue;
2506                         }
2507
2508                         /* mark new line beginning */
2509                         line_pos = cur_pos + 1;
2510                         line_len = cur_len = 0;
2511                         qlen = 0;
2512                         do_delete = FALSE;
2513                         is_new_line = TRUE;
2514                         continue;
2515                 }
2516
2517                 if (ch_len < 0) {
2518                         cbuf[0] = '\0';
2519                         ch_len = 1;
2520                 }
2521
2522                 /* possible line break */
2523                 if (ch_len == 1 && isspace(*cbuf)) {
2524                         line_pos = cur_pos + 1;
2525                         line_len = cur_len + ch_len;
2526                 }
2527
2528                 /* are we over wrapping length set in preferences ? */
2529                 if (cur_len + ch_len > linewrap_len) {
2530                         gint clen;
2531
2532 #ifdef WRAP_DEBUG
2533                         printf("should wrap cur_pos=%d ", cur_pos);
2534                         dump_text(text, p_pos, tlen, 1);
2535                         dump_text(text, line_pos, tlen, 1);
2536 #endif
2537                         /* force wrapping if it is one long word but not URL */
2538                         if (line_pos - p_pos <= i_len)
2539                                 if (!gtkut_stext_is_uri_string
2540                                     (text, line_pos, tlen))
2541                                         line_pos = cur_pos - 1;
2542 #ifdef WRAP_DEBUG
2543                         printf("new line_pos=%d\n", line_pos);
2544 #endif
2545
2546                         GET_CHAR(line_pos - 1, cbuf, clen);
2547
2548                         /* if next character is space delete it */
2549                         if (clen == 1 && isspace(*cbuf)) {
2550                                 if (p_pos + i_len != line_pos ||
2551                                     !gtkut_stext_is_uri_string
2552                                         (text, line_pos, tlen)) {
2553                                         gtk_stext_set_point(text, line_pos);
2554                                         gtk_stext_backward_delete(text, 1);
2555                                         tlen--;
2556                                         cur_pos--;
2557                                         line_pos--;
2558                                         cur_len--;
2559                                         line_len--;
2560                                 }
2561                         }
2562
2563                         /* if it is URL at beginning of line don't wrap */
2564                         if (p_pos + i_len == line_pos &&
2565                             gtkut_stext_is_uri_string(text, line_pos, tlen)) {
2566 #ifdef WRAP_DEBUG
2567                                 printf("found URL at ");
2568                                 dump_text(text, line_pos, tlen, 1);
2569 #endif
2570                                 continue;
2571                         }
2572
2573                         /* insert CR */
2574                         gtk_stext_set_point(text, line_pos);
2575                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2576                         /* gtk_stext_compact_buffer(text); */
2577                         tlen++;
2578                         line_pos++;
2579                         /* for loop will increase it */
2580                         cur_pos = line_pos - 1;
2581                         /* start over with current line */
2582                         is_new_line = TRUE;
2583                         line_len = 0;
2584                         cur_len = 0;
2585                         do_delete = TRUE;
2586 #ifdef WRAP_DEBUG
2587                         printf("after CR insert ");
2588                         dump_text(text, line_pos, tlen, 1);
2589                         dump_text(text, cur_pos, tlen, 1);
2590 #endif
2591
2592                         /* should we insert quotation ? */
2593                         if (linewrap_quote && qlen) {
2594                                 /* only if line is not already quoted  */
2595                                 if (!gtkut_stext_str_compare
2596                                         (text, line_pos, tlen, qfmt)) {
2597                                         guint ins_len;
2598
2599                                         if (line_pos - p_pos > i_len) {
2600                                                 ins_len = ins_quote
2601                                                         (text, qlen, i_len,
2602                                                          p_pos, tlen, qfmt);
2603
2604                                                 /* gtk_stext_compact_buffer(text); */
2605                                                 tlen += ins_len;
2606                                         }
2607 #ifdef WRAP_DEBUG
2608                                         printf("after quote insert ");
2609                                         dump_text(text, line_pos, tlen, 1);
2610 #endif
2611                                 }
2612                         }
2613                         continue;
2614                 }
2615
2616                 if (ch_len > 1) {
2617                         line_pos = cur_pos + 1;
2618                         line_len = cur_len + ch_len;
2619                 }
2620                 /* advance to next character in buffer */
2621                 cur_len += ch_len;
2622         }
2623
2624         gtk_stext_thaw(text);
2625 }
2626
2627 #undef GET_CHAR
2628
2629 static void compose_set_title(Compose *compose)
2630 {
2631         gchar *str;
2632         gchar *edited;
2633
2634         edited = compose->modified ? _(" [Edited]") : "";
2635         if (compose->account && compose->account->address)
2636                 str = g_strdup_printf(_("%s - Compose message%s"),
2637                                       compose->account->address, edited);
2638         else
2639                 str = g_strdup_printf(_("Compose message%s"), edited);
2640         gtk_window_set_title(GTK_WINDOW(compose->window), str);
2641         g_free(str);
2642 }
2643
2644 /**
2645  * compose_current_mail_account:
2646  * 
2647  * Find a current mail account (the currently selected account, or the
2648  * default account, if a news account is currently selected).  If a
2649  * mail account cannot be found, display an error message.
2650  * 
2651  * Return value: Mail account, or NULL if not found.
2652  **/
2653 static PrefsAccount *
2654 compose_current_mail_account(void)
2655 {
2656         PrefsAccount *ac;
2657
2658         if (cur_account && cur_account->protocol != A_NNTP)
2659                 ac = cur_account;
2660         else {
2661                 ac = account_get_default();
2662                 if (!ac || ac->protocol == A_NNTP) {
2663                         alertpanel_error(_("Account for sending mail is not specified.\n"
2664                                            "Please select a mail account before sending."));
2665                         return NULL;
2666                 }
2667         }
2668         return ac;
2669 }
2670
2671 gboolean compose_check_for_valid_recipient(Compose *compose) {
2672         gchar *recipient_headers_mail[] = {"To:", "Cc:", "Bcc:", NULL};
2673         gchar *recipient_headers_news[] = {"Newsgroups:", NULL};
2674         gboolean recipient_found = FALSE;
2675         GSList *list;
2676         gchar **strptr;
2677
2678         /* free to and newsgroup list */
2679         slist_free_strings(compose->to_list);
2680         g_slist_free(compose->to_list);
2681         compose->to_list = NULL;
2682                         
2683         slist_free_strings(compose->newsgroup_list);
2684         g_slist_free(compose->newsgroup_list);
2685         compose->newsgroup_list = NULL;
2686
2687         /* search header entries for to and newsgroup entries */
2688         for(list = compose->header_list; list; list = list->next) {
2689                 gchar *header;
2690                 gchar *entry;
2691                 header = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(((ComposeHeaderEntry *)list->data)->combo)->entry));
2692                 entry = gtk_editable_get_chars(GTK_EDITABLE(((ComposeHeaderEntry *)list->data)->entry), 0, -1);
2693                 g_strstrip(entry);
2694                 if(entry[0] != '\0') {
2695                         for(strptr = recipient_headers_mail; *strptr != NULL; strptr++) {
2696                                 if(!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
2697                                         compose->to_list = address_list_append(compose->to_list, entry);
2698                                         recipient_found = TRUE;
2699                                 }
2700                         }
2701                         for(strptr = recipient_headers_news; *strptr != NULL; strptr++) {
2702                                 if(!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
2703                                         compose->newsgroup_list = newsgroup_list_append(compose->newsgroup_list, entry);
2704                                         recipient_found = TRUE;
2705                                 }
2706                         }
2707                 }
2708                 g_free(entry);
2709         }
2710         return recipient_found;
2711 }
2712
2713 gint compose_send(Compose *compose)
2714 {
2715         gint msgnum;
2716         FolderItem *folder;
2717         gint val;
2718
2719         val = compose_queue(compose, &msgnum, &folder);
2720         if (val) {
2721                 alertpanel_error(_("Could not queue message for sending"));
2722                 return -1;
2723         }
2724         
2725         val = procmsg_send_message_queue(folder_item_fetch_msg(folder, msgnum));
2726         if(!val) {
2727                 folder_item_remove_msg(folder, msgnum);
2728                 folderview_update_item(folder, TRUE);
2729         }
2730
2731         return val;
2732 }
2733
2734 #if 0 /* compose restructure */
2735 gint compose_send(Compose *compose)
2736 {
2737         gchar tmp[MAXPATHLEN + 1];
2738         gint ok = 0;
2739         static gboolean lock = FALSE;
2740
2741         if (lock) return 1;
2742
2743         g_return_val_if_fail(compose->account != NULL, -1);
2744         g_return_val_if_fail(compose->orig_account != NULL, -1);
2745
2746         lock = TRUE;
2747
2748         if(!compose_check_for_valid_recipient(compose)) {
2749                 alertpanel_error(_("Recipient is not specified."));
2750                 lock = FALSE;
2751                 return 1;
2752         }
2753
2754         /* write to temporary file */
2755         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
2756                    get_rc_dir(), G_DIR_SEPARATOR, (gint)compose);
2757
2758         if (prefs_common.linewrap_at_send)
2759                 compose_wrap_line_all(compose);
2760
2761         if (compose_write_to_file(compose, tmp, FALSE) < 0) {
2762                 lock = FALSE;
2763                 return -1;
2764         }
2765
2766         if (!compose->to_list && !compose->newsgroup_list) {
2767                 g_warning(_("can't get recipient list."));
2768                 unlink(tmp);
2769                 lock = FALSE;
2770                 return -1;
2771         }
2772
2773         if (compose->to_list) {
2774                 PrefsAccount *ac;
2775
2776 #if 0 /* NEW COMPOSE GUI */
2777                 if (compose->account->protocol != A_NNTP)
2778                         ac = compose->account;
2779                 else if (compose->orig_account->protocol != A_NNTP)
2780                         ac = compose->orig_account;
2781                 else if (cur_account && cur_account->protocol != A_NNTP)
2782                         ac = cur_account;
2783                 else {
2784                         ac = compose_current_mail_account();
2785                         if (!ac) {
2786                                 unlink(tmp);
2787                                 lock = FALSE;
2788                                 return -1;
2789                         }
2790                 }
2791 #endif
2792                 ac = compose->account;
2793
2794                 ok = send_message(tmp, ac, compose->to_list);
2795                 statusbar_pop_all();
2796         }
2797
2798         if (ok == 0 && compose->newsgroup_list) {
2799                 Folder *folder;
2800
2801                 if (compose->account->protocol == A_NNTP)
2802                         folder = FOLDER(compose->account->folder);
2803                 else
2804                         folder = FOLDER(compose->orig_account->folder);
2805
2806                 ok = news_post(folder, tmp);
2807                 if (ok < 0) {
2808                         alertpanel_error(_("Error occurred while posting the message to %s ."),
2809                                          compose->account->nntp_server);
2810                         unlink(tmp);
2811                         lock = FALSE;
2812                         return -1;
2813                 }
2814         }
2815
2816         /* queue message if failed to send */
2817         if (ok < 0) {
2818                 if (prefs_common.queue_msg) {
2819                         AlertValue val;
2820
2821                         val = alertpanel
2822                                 (_("Queueing"),
2823                                  _("Error occurred while sending the message.\n"
2824                                    "Put this message into queue folder?"),
2825                                  _("OK"), _("Cancel"), NULL);
2826                         if (G_ALERTDEFAULT == val) {
2827                                 ok = compose_queue(compose, tmp);
2828                                 if (ok < 0)
2829                                         alertpanel_error(_("Can't queue the message."));
2830                         }
2831                 } else
2832                         alertpanel_error(_("Error occurred while sending the message."));
2833         } else {
2834                 if (compose->mode == COMPOSE_REEDIT) {
2835                         compose_remove_reedit_target(compose);
2836                         if (compose->targetinfo)
2837                                 folderview_update_item
2838                                         (compose->targetinfo->folder, TRUE);
2839                 }
2840         }
2841
2842         /* save message to outbox */
2843         if (ok == 0 && prefs_common.savemsg) {
2844                 if (compose_save_to_outbox(compose, tmp) < 0)
2845                         alertpanel_error
2846                                 (_("Can't save the message to outbox."));
2847         }
2848
2849         unlink(tmp);
2850         lock = FALSE;
2851         return ok;
2852 }
2853 #endif
2854
2855 static gboolean compose_use_attach(Compose *compose) {
2856     return(gtk_clist_get_row_data(GTK_CLIST(compose->attach_clist), 0) != NULL);
2857 }
2858
2859 static gint compose_bounce_write_headers_from_headerlist(Compose *compose, 
2860                                                          FILE *fp)
2861 {
2862         gchar buf[BUFFSIZE];
2863         gchar *str;
2864         gboolean first_address;
2865         GSList *list;
2866         ComposeHeaderEntry *headerentry;
2867         gchar *headerentryname;
2868         gchar *header_w_colon;
2869         gchar *cc_hdr;
2870         gchar *to_hdr;
2871
2872         debug_print(_("Writing bounce header\n"));
2873
2874         header_w_colon = g_strconcat("To:", NULL);
2875         to_hdr = (prefs_common.trans_hdr ? gettext(header_w_colon) : header_w_colon);
2876         header_w_colon = g_strconcat("Cc:", NULL);
2877         cc_hdr = (prefs_common.trans_hdr ? gettext(header_w_colon) : header_w_colon);
2878         
2879         first_address = TRUE;
2880         for(list = compose->header_list; list; list = list->next) {
2881                 headerentry = ((ComposeHeaderEntry *)list->data);
2882                 headerentryname = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(headerentry->combo)->entry));
2883
2884                 if(g_strcasecmp(headerentryname, cc_hdr) == 0 
2885                    || g_strcasecmp(headerentryname, to_hdr) == 0) {
2886                         str = gtk_entry_get_text(GTK_ENTRY(headerentry->entry));
2887                         Xstrdup_a(str, str, return -1);
2888                         g_strstrip(str);
2889                         if(str[0] != '\0') {
2890                                 compose_convert_header
2891                                         (buf, sizeof(buf), str,
2892                                         strlen("Resent-To") + 2);
2893                                 if(first_address) {
2894                                         fprintf(fp, "Resent-To: ");
2895                                         first_address = FALSE;
2896                                 } else {
2897                                         fprintf(fp, ",");
2898                                 }
2899                                 fprintf(fp, "%s", buf);
2900                         }
2901                 }
2902         }
2903         /* if(!first_address) { */
2904         fprintf(fp, "\n");
2905         /* } */
2906
2907         return(0);
2908 }
2909
2910 static gint compose_bounce_write_headers(Compose *compose, FILE *fp)
2911 {
2912         gchar buf[BUFFSIZE];
2913         gchar *str;
2914         /* struct utsname utsbuf; */
2915
2916         g_return_val_if_fail(fp != NULL, -1);
2917         g_return_val_if_fail(compose->account != NULL, -1);
2918         g_return_val_if_fail(compose->account->address != NULL, -1);
2919
2920         /* Date */
2921         get_rfc822_date(buf, sizeof(buf));
2922         fprintf(fp, "Resent-Date: %s\n", buf);
2923
2924         /* From */
2925         if (compose->account->name && *compose->account->name) {
2926                 compose_convert_header
2927                         (buf, sizeof(buf), compose->account->name,
2928                          strlen("From: "));
2929                 fprintf(fp, "Resent-From: %s <%s>\n",
2930                         buf, compose->account->address);
2931         } else
2932                 fprintf(fp, "Resent-From: %s\n", compose->account->address);
2933
2934         /* To */
2935         compose_bounce_write_headers_from_headerlist(compose, fp);
2936
2937         /* separator between header and body */
2938         fputs("\n", fp);
2939
2940         return 0;
2941 }
2942
2943 static gint compose_bounce_write_to_file(Compose *compose, const gchar *file)
2944 {
2945         FILE *fp;
2946         FILE *fdest;
2947         size_t len;
2948         gchar buf[BUFFSIZE];
2949
2950         if ((fp = fopen(compose->bounce_filename, "r")) == NULL) {
2951                 FILE_OP_ERROR(file, "fopen");
2952                 return -1;
2953         }
2954
2955         if ((fdest = fopen(file, "w")) == NULL) {
2956                 FILE_OP_ERROR(file, "fopen");
2957                 fclose(fp);
2958                 return -1;
2959         }
2960
2961         /* chmod for security */
2962         if (change_file_mode_rw(fdest, file) < 0) {
2963                 FILE_OP_ERROR(file, "chmod");
2964                 g_warning(_("can't change file mode\n"));
2965         }
2966
2967         while (procheader_get_unfolded_line(buf, sizeof(buf), fp)) {
2968                 /* should filter returnpath, delivered-to */
2969                 if ((g_strncasecmp(buf, "Return-Path:",
2970                                    strlen("Return-Path:")) == 0)
2971                     || (g_strncasecmp(buf, "Delivered-To:",
2972                                       strlen("Delivered-To:")) == 0))
2973                         continue;
2974
2975                 if (fputs(buf, fdest) == -1)
2976                         goto error;
2977
2978                 if (g_strncasecmp(buf, "From:", strlen("From:")) == 0) {
2979                         fputs(" (by way of ", fdest);
2980                         if (compose->account->name
2981                             && *compose->account->name) {
2982                                 compose_convert_header
2983                                         (buf, sizeof(buf),
2984                                          compose->account->name,
2985                                          strlen("From: "));
2986                                 fprintf(fdest, "%s <%s>",
2987                                         buf, compose->account->address);
2988                         } else
2989                                 fprintf(fdest, "%s",
2990                                         compose->account->address);
2991                         fputs(")", fdest);
2992                 }
2993
2994                 if (fputs("\n", fdest) == -1)
2995                         goto error;
2996         }
2997
2998         compose_bounce_write_headers(compose, fdest);
2999
3000         while ((len = fread(buf, sizeof(gchar), BUFFSIZE, fp)) > 0) {
3001                 if (fwrite(buf, sizeof(gchar), len, fdest) == -1)
3002                         goto error;
3003         }
3004
3005         fclose(fdest);
3006         fclose(fp);
3007
3008         return 0;
3009  error:
3010         fclose(fdest);
3011         fclose(fp);
3012
3013         return -1;
3014 }
3015
3016
3017 #ifdef USE_GPGME
3018 /*
3019  * interfaces to rfc2015 to keep out the prefs stuff there.
3020  * returns 0 on success and -1 on error. */
3021 static int compose_create_signers_list (Compose *compose, GSList **pkey_list)
3022 {
3023         const char *keyid = NULL;
3024         GSList *key_list;
3025
3026         switch (compose->account->sign_key) {
3027         case SIGN_KEY_DEFAULT:
3028                 *pkey_list = NULL;
3029                 return 0;               /* nothing to do */
3030
3031         case SIGN_KEY_BY_FROM:
3032                 keyid = compose->account->address;
3033                 break;
3034
3035         case SIGN_KEY_CUSTOM:
3036                 keyid = compose->account->sign_key_id;
3037                 break;
3038
3039         default:
3040                 g_assert_not_reached ();
3041         }
3042
3043         key_list = rfc2015_create_signers_list(keyid);
3044
3045         if (!key_list) {
3046             alertpanel_error("Could not find any key associated with currently "
3047                                 "selected keyid `%s'!", keyid);
3048             return -1;
3049         }
3050         
3051         *pkey_list = key_list;
3052         return 0;
3053 }
3054 #endif /* USE_GPGME */
3055
3056 static gint compose_write_to_file(Compose *compose, const gchar *file,
3057                                   gboolean is_draft)
3058 {
3059         FILE *fp;
3060         size_t len;
3061         gchar *chars;
3062         gchar *buf;
3063         const gchar *out_codeset;
3064         EncodingType encoding;
3065
3066         if ((fp = fopen(file, "w")) == NULL) {
3067                 FILE_OP_ERROR(file, "fopen");
3068                 return -1;
3069         }
3070
3071         /* chmod for security */
3072         if (change_file_mode_rw(fp, file) < 0) {
3073                 FILE_OP_ERROR(file, "chmod");
3074                 g_warning(_("can't change file mode\n"));
3075         }
3076
3077         /* get all composed text */
3078         chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
3079         len = strlen(chars);
3080         if (is_ascii_str(chars)) {
3081                 buf = g_strdup(chars);
3082                 out_codeset = CS_US_ASCII;
3083                 encoding = ENC_7BIT;
3084         } else {
3085                 const gchar *src_codeset;
3086
3087                 out_codeset = conv_get_outgoing_charset_str();
3088                 if (!strcasecmp(out_codeset, CS_US_ASCII))
3089                         out_codeset = CS_ISO_8859_1;
3090                 encoding = procmime_get_encoding_for_charset(out_codeset);
3091
3092                 src_codeset = conv_get_current_charset_str();
3093                 /* if current encoding is US-ASCII, set it the same as
3094                    outgoing one to prevent code conversion failure */
3095                 if (!strcasecmp(src_codeset, CS_US_ASCII))
3096                         src_codeset = out_codeset;
3097
3098                 debug_print("src encoding = %s, out encoding = %s, transfer encoding = %s\n",
3099                             src_codeset, out_codeset, procmime_get_encoding_str(encoding));
3100
3101                 buf = conv_codeset_strdup(chars, src_codeset, out_codeset);
3102                 if (!buf) {
3103                         g_free(chars);
3104                         fclose(fp);
3105                         unlink(file);
3106                         alertpanel_error(_("Can't convert the codeset of the message."));
3107                         return -1;
3108                 }
3109         }
3110         g_free(chars);
3111
3112         /* write headers */
3113         if (compose_write_headers
3114                 (compose, fp, out_codeset, encoding, is_draft) < 0) {
3115                 g_warning(_("can't write headers\n"));
3116                 fclose(fp);
3117                 /* unlink(file); */
3118                 g_free(buf);
3119                 return -1;
3120         }
3121
3122         if (compose_use_attach(compose)) {
3123 #if USE_GPGME
3124             /* This prolog message is ignored by mime software and
3125              * because it would make our signing/encryption task
3126              * tougher, we don't emit it in that case */
3127             if (!compose->use_signing && !compose->use_encryption)
3128 #endif
3129                 fputs("This is a multi-part message in MIME format.\n", fp);
3130
3131                 fprintf(fp, "\n--%s\n", compose->boundary);
3132                 fprintf(fp, "Content-Type: text/plain; charset=%s\n",
3133                         out_codeset);
3134                 fprintf(fp, "Content-Transfer-Encoding: %s\n",
3135                         procmime_get_encoding_str(encoding));
3136                 fputc('\n', fp);
3137         }
3138
3139         /* write body */
3140         len = strlen(buf);
3141         if (encoding == ENC_BASE64) {
3142                 gchar outbuf[B64_BUFFSIZE];
3143                 gint i, l;
3144
3145                 for (i = 0; i < len; i += B64_LINE_SIZE) {
3146                         l = MIN(B64_LINE_SIZE, len - i);
3147                         to64frombits(outbuf, buf + i, l);
3148                         fputs(outbuf, fp);
3149                         fputc('\n', fp);
3150                 }
3151         } else if (fwrite(buf, sizeof(gchar), len, fp) != len) {
3152                 FILE_OP_ERROR(file, "fwrite");
3153                 fclose(fp);
3154                 unlink(file);
3155                 g_free(buf);
3156                 return -1;
3157         }
3158         g_free(buf);
3159
3160         if (compose_use_attach(compose))
3161                 compose_write_attach(compose, fp);
3162
3163         if (fclose(fp) == EOF) {
3164                 FILE_OP_ERROR(file, "fclose");
3165                 unlink(file);
3166                 return -1;
3167         }
3168
3169 #if USE_GPGME
3170         if (compose->use_signing) {
3171                 GSList *key_list;
3172                 
3173                 if (compose_create_signers_list(compose, &key_list) == -1 ||
3174                         rfc2015_sign(file, key_list) < 0) {
3175                     
3176                         unlink(file);
3177                         return -1;
3178                 }
3179         }
3180         if (compose->use_encryption) {
3181                 if (rfc2015_encrypt(file, compose->to_list, compose->account->ascii_armored) < 0) {
3182                         unlink(file);
3183                         return -1;
3184                 }
3185         }
3186 #endif /* USE_GPGME */
3187
3188         return 0;
3189 }
3190
3191 static gint compose_write_body_to_file(Compose *compose, const gchar *file)
3192 {
3193         FILE *fp;
3194         size_t len;
3195         gchar *chars;
3196
3197         if ((fp = fopen(file, "w")) == NULL) {
3198                 FILE_OP_ERROR(file, "fopen");
3199                 return -1;
3200         }
3201
3202         /* chmod for security */
3203         if (change_file_mode_rw(fp, file) < 0) {
3204                 FILE_OP_ERROR(file, "chmod");
3205                 g_warning(_("can't change file mode\n"));
3206         }
3207
3208         chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
3209
3210         /* write body */
3211         len = strlen(chars);
3212         if (fwrite(chars, sizeof(gchar), len, fp) != len) {
3213                 FILE_OP_ERROR(file, "fwrite");
3214                 g_free(chars);
3215                 fclose(fp);
3216                 unlink(file);
3217                 return -1;
3218         }
3219
3220         g_free(chars);
3221
3222         if (fclose(fp) == EOF) {
3223                 FILE_OP_ERROR(file, "fclose");
3224                 unlink(file);
3225                 return -1;
3226         }
3227         return 0;
3228 }
3229
3230 static gint compose_save_to_outbox(Compose *compose, const gchar *file)
3231 {
3232         FolderItem *outbox;
3233         gchar *path;
3234         gint num;
3235         FILE *fp;
3236
3237         debug_print(_("saving sent message...\n"));
3238
3239         outbox = folder_get_default_outbox();
3240         path = folder_item_get_path(outbox);
3241         if (!is_dir_exist(path))
3242                 make_dir_hier(path);
3243
3244         folder_item_scan(outbox);
3245         if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
3246                 g_free(path);
3247                 g_warning(_("can't save message\n"));
3248                 return -1;
3249         }
3250
3251         if ((fp = procmsg_open_mark_file(path, TRUE)) == NULL)
3252                 g_warning(_("can't open mark file\n"));
3253         else {
3254                 MsgInfo newmsginfo;
3255
3256                 newmsginfo.msgnum = num;
3257                 newmsginfo.flags.perm_flags = 0;
3258                 newmsginfo.flags.tmp_flags = 0;
3259                 procmsg_write_flags(&newmsginfo, fp);
3260                 fclose(fp);
3261         }
3262         g_free(path);
3263
3264         return 0;
3265 }
3266
3267 static gint compose_remove_reedit_target(Compose *compose)
3268 {
3269         FolderItem *item;
3270         MsgInfo *msginfo = compose->targetinfo;
3271
3272         g_return_val_if_fail(compose->mode == COMPOSE_REEDIT, -1);
3273         if (!msginfo) return -1;
3274
3275         item = msginfo->folder;
3276         g_return_val_if_fail(item != NULL, -1);
3277
3278         folder_item_scan(item);
3279         if (procmsg_msg_exist(msginfo) &&
3280             (item->stype == F_DRAFT || item->stype == F_QUEUE)) {
3281                 if (folder_item_remove_msg(item, msginfo->msgnum) < 0) {
3282                         g_warning(_("can't remove the old message\n"));
3283                         return -1;
3284                 }
3285         }
3286
3287         return 0;
3288 }
3289
3290
3291 static gint compose_queue(Compose *compose, gint *msgnum, FolderItem **item)
3292 {
3293         FolderItem *queue;
3294         gchar *tmp, *tmp2, *queue_path;
3295         FILE *fp, *src_fp;
3296         GSList *cur;
3297         gchar buf[BUFFSIZE];
3298         gint num;
3299         static gboolean lock = FALSE;
3300         PrefsAccount *mailac = NULL, *newsac = NULL;
3301         
3302         debug_print(_("queueing message...\n"));
3303         g_return_val_if_fail(compose->account != NULL, -1);
3304         g_return_val_if_fail(compose->orig_account != NULL, -1);
3305
3306         lock = TRUE;
3307         
3308         if(!compose_check_for_valid_recipient(compose)) {
3309                 alertpanel_error(_("Recipient is not specified."));
3310                 lock = FALSE;
3311                 return -1;
3312         }
3313                                                                         
3314         if (!compose->to_list && !compose->newsgroup_list) {
3315                 g_warning(_("can't get recipient list."));
3316                 lock = FALSE;
3317                 return -1;
3318         }
3319
3320         if(compose->to_list) {
3321                 if (compose->account->protocol != A_NNTP)
3322                         mailac = compose->account;
3323                 else if (compose->orig_account->protocol != A_NNTP)
3324                         mailac = compose->orig_account;
3325                 else if (cur_account && cur_account->protocol != A_NNTP)
3326                         mailac = cur_account;
3327                 else if (!(mailac = compose_current_mail_account())) {
3328                         lock = FALSE;
3329                         alertpanel_error(_("No account for sending mails available!"));
3330                         return -1;
3331                 }
3332         }
3333
3334         if(compose->newsgroup_list) {
3335                 if (compose->account->protocol == A_NNTP)
3336                         newsac = compose->account;
3337                 else if(!(newsac = compose->orig_account) || (newsac->protocol != A_NNTP)) {
3338                         lock = FALSE;
3339                         alertpanel_error(_("No account for posting news available!"));
3340                         return -1;
3341                 }                       
3342         }
3343
3344         if (prefs_common.linewrap_at_send)
3345                 compose_wrap_line_all(compose);
3346                         
3347         /* write to temporary file */
3348         tmp2 = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
3349                                       G_DIR_SEPARATOR, (gint)compose);
3350
3351         if (compose->bounce_filename != NULL) {
3352                 if (compose_bounce_write_to_file(compose, tmp2) < 0) {
3353                         unlink(tmp2);
3354                         lock = FALSE;
3355                         return -1;
3356                 }
3357         }
3358         else {
3359                 if (compose_write_to_file(compose, tmp2, FALSE) < 0) {
3360                         unlink(tmp2);
3361                         lock = FALSE;
3362                         return -1;
3363                 }
3364         }
3365
3366         /* add queue header */
3367         tmp = g_strdup_printf("%s%cqueue.%d", g_get_tmp_dir(),
3368                                       G_DIR_SEPARATOR, (gint)compose);
3369         if ((fp = fopen(tmp, "w")) == NULL) {
3370                 FILE_OP_ERROR(tmp, "fopen");
3371                 g_free(tmp);
3372                 return -1;
3373         }
3374         if ((src_fp = fopen(tmp2, "r")) == NULL) {
3375                 FILE_OP_ERROR(tmp2, "fopen");
3376                 fclose(fp);
3377                 unlink(tmp);
3378                 g_free(tmp);
3379                 unlink(tmp2);
3380                 g_free(tmp2);
3381                 return -1;
3382         }
3383         if (change_file_mode_rw(fp, tmp) < 0) {
3384                 FILE_OP_ERROR(tmp, "chmod");
3385                 g_warning(_("can't change file mode\n"));
3386         }
3387
3388         /* queueing variables */
3389         fprintf(fp, "AF:\n");
3390         fprintf(fp, "NF:0\n");
3391         fprintf(fp, "PS:10\n");
3392         fprintf(fp, "SRH:1\n");
3393         fprintf(fp, "SFN:\n");
3394         fprintf(fp, "DSR:\n");
3395         if (compose->msgid)
3396                 fprintf(fp, "MID:<%s>\n", compose->msgid);
3397         else
3398                 fprintf(fp, "MID:\n");
3399         fprintf(fp, "CFG:\n");
3400         fprintf(fp, "PT:0\n");
3401         fprintf(fp, "S:%s\n", compose->account->address);
3402         fprintf(fp, "RQ:\n");
3403         if (mailac)
3404                 fprintf(fp, "SSV:%s\n", mailac->smtp_server);
3405         else
3406                 fprintf(fp, "SSV:\n");
3407         if (newsac)
3408                 fprintf(fp, "NSV:%s\n", newsac->nntp_server);
3409         else
3410                 fprintf(fp, "NSV:\n");
3411         fprintf(fp, "SSH:\n");
3412         /* write recepient list */
3413         fprintf(fp, "R:");
3414         if(compose->to_list) {
3415                 fprintf(fp, "<%s>", (gchar *)compose->to_list->data);
3416                 for (cur = compose->to_list->next; cur != NULL; cur = cur->next)
3417                         fprintf(fp, ",<%s>", (gchar *)cur->data);
3418         }
3419         fprintf(fp, "\n");
3420         /* write newsgroup list */
3421         fprintf(fp, "NG:");
3422         if(compose->newsgroup_list) {
3423                 fprintf(fp, "%s", (gchar *)compose->newsgroup_list->data);
3424                 for (cur = compose->newsgroup_list->next; cur != NULL; cur = cur->next)
3425                         fprintf(fp, ",%s", (gchar *)cur->data);
3426         }
3427         fprintf(fp, "\n");
3428         /* Sylpheed account IDs */
3429         if(mailac) {
3430                 fprintf(fp, "MAID:%d\n", mailac->account_id);
3431         }
3432         if(newsac) {
3433                 fprintf(fp, "NAID:%d\n", newsac->account_id);
3434         }
3435         /* Save copy folder */
3436         if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn))) {
3437                 gchar *str;
3438                 
3439                 str = gtk_editable_get_chars(GTK_EDITABLE(compose->savemsg_entry), 0, -1);
3440                 fprintf(fp, "SCF:%s\n", str);
3441                 g_free(str);
3442         }
3443         fprintf(fp, "\n");
3444
3445         while (fgets(buf, sizeof(buf), src_fp) != NULL) {
3446                 if (fputs(buf, fp) == EOF) {
3447                         FILE_OP_ERROR(tmp, "fputs");
3448                         fclose(fp);
3449                         fclose(src_fp);
3450                         unlink(tmp);
3451                         g_free(tmp);
3452                         unlink(tmp2);
3453                         g_free(tmp2);
3454                         return -1;
3455                 }
3456         }
3457         fclose(src_fp);
3458         if (fclose(fp) == EOF) {
3459                 FILE_OP_ERROR(tmp, "fclose");
3460                 unlink(tmp);
3461                 g_free(tmp);
3462                 unlink(tmp2);
3463                 g_free(tmp2);
3464                 return -1;
3465         }
3466                                                 
3467         /* queue message */
3468         queue = folder_get_default_queue();
3469
3470         folder_item_scan(queue);
3471         queue_path = folder_item_get_path(queue);
3472         if (!is_dir_exist(queue_path))
3473                 make_dir_hier(queue_path);
3474         if ((num = folder_item_add_msg(queue, tmp, TRUE)) < 0) {
3475                 g_warning(_("can't queue the message\n"));
3476                 unlink(tmp);
3477                 g_free(tmp);
3478                 g_free(queue_path);
3479                 return -1;
3480         }
3481         unlink(tmp);
3482         g_free(tmp);
3483         unlink(tmp2);
3484         g_free(tmp2);
3485
3486         if (compose->mode == COMPOSE_REEDIT) {
3487                 compose_remove_reedit_target(compose);
3488                 if (compose->targetinfo &&
3489                     compose->targetinfo->folder != queue)
3490                         folderview_update_item
3491                                 (compose->targetinfo->folder, TRUE);
3492         }
3493
3494         if ((fp = procmsg_open_mark_file(queue_path, TRUE)) == NULL)
3495                 g_warning(_("can't open mark file\n"));
3496         else {
3497                 MsgInfo newmsginfo;
3498
3499                 newmsginfo.msgnum = num;
3500                 newmsginfo.flags.perm_flags = 0;
3501                 newmsginfo.flags.tmp_flags = 0;
3502                 procmsg_write_flags(&newmsginfo, fp);
3503                 fclose(fp);
3504         }
3505         g_free(queue_path);
3506
3507         folder_item_scan(queue);
3508         folderview_update_item(queue, TRUE);
3509
3510         if((msgnum != NULL) && (item != NULL)) {
3511                 *msgnum = num;
3512                 *item = queue;
3513         }
3514
3515         return 0;
3516 }
3517
3518 static void compose_write_attach(Compose *compose, FILE *fp)
3519 {
3520         AttachInfo *ainfo;
3521         GtkCList *clist = GTK_CLIST(compose->attach_clist);
3522         gint row;
3523         FILE *attach_fp;
3524         gchar filename[BUFFSIZE];
3525         gint len;
3526
3527         for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
3528              row++) {
3529                 if ((attach_fp = fopen(ainfo->file, "r")) == NULL) {
3530                         g_warning(_("Can't open file %s\n"), ainfo->file);
3531                         continue;
3532                 }
3533
3534                 fprintf(fp, "\n--%s\n", compose->boundary);
3535
3536                 if (!strcmp2(ainfo->content_type, "message/rfc822")) {
3537                         fprintf(fp, "Content-Type: %s\n", ainfo->content_type);
3538                         fprintf(fp, "Content-Disposition: inline\n");
3539                 } else {
3540                         conv_encode_header(filename, sizeof(filename),
3541                                            ainfo->name, 12);
3542                         fprintf(fp, "Content-Type: %s;\n"
3543                                     " name=\"%s\"\n",
3544                                 ainfo->content_type, filename);
3545                         fprintf(fp, "Content-Disposition: attachment;\n"
3546                                     " filename=\"%s\"\n", filename);
3547                 }
3548
3549                 fprintf(fp, "Content-Transfer-Encoding: %s\n\n",
3550                         procmime_get_encoding_str(ainfo->encoding));
3551
3552                 if (ainfo->encoding == ENC_7BIT) {
3553                         gchar buf[BUFFSIZE];
3554
3555                         while (fgets(buf, sizeof(buf), attach_fp) != NULL) {
3556                                 strcrchomp(buf);
3557                                 fputs(buf, fp);
3558                         }
3559                 } else {
3560                         gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
3561
3562                         while ((len = fread(inbuf, sizeof(gchar),
3563                                             B64_LINE_SIZE, attach_fp))
3564                                == B64_LINE_SIZE) {
3565                                 to64frombits(outbuf, inbuf, B64_LINE_SIZE);
3566                                 fputs(outbuf, fp);
3567                                 fputc('\n', fp);
3568                         }
3569                         if (len > 0 && feof(attach_fp)) {
3570                                 to64frombits(outbuf, inbuf, len);
3571                                 fputs(outbuf, fp);
3572                                 fputc('\n', fp);
3573                         }
3574                 }
3575
3576                 fclose(attach_fp);
3577         }
3578
3579         fprintf(fp, "\n--%s--\n", compose->boundary);
3580 }
3581
3582 #define IS_IN_CUSTOM_HEADER(header) \
3583         (compose->account->add_customhdr && \
3584          custom_header_find(compose->account->customhdr_list, header) != NULL)
3585
3586 static gint compose_write_headers_from_headerlist(Compose *compose, 
3587                                                   FILE *fp, 
3588                                                   gchar *header)
3589 {
3590         gchar buf[BUFFSIZE];
3591         gchar *str, *header_w_colon, *trans_hdr;
3592         gboolean first_address;
3593         GSList *list;
3594         ComposeHeaderEntry *headerentry;
3595         gchar * headerentryname;
3596
3597         if (IS_IN_CUSTOM_HEADER(header)) {
3598                 return 0;
3599         }
3600
3601         debug_print(_("Writing %s-header\n"), header);
3602
3603         header_w_colon = g_strconcat(header, ":", NULL);
3604         trans_hdr = (prefs_common.trans_hdr ? gettext(header_w_colon) : header_w_colon);
3605
3606         first_address = TRUE;
3607         for(list = compose->header_list; list; list = list->next) {
3608                 headerentry = ((ComposeHeaderEntry *)list->data);
3609                 headerentryname = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(headerentry->combo)->entry));
3610
3611                 if(!g_strcasecmp(trans_hdr, headerentryname)) {
3612                         str = gtk_entry_get_text(GTK_ENTRY(headerentry->entry));
3613                         Xstrdup_a(str, str, return -1);
3614                         g_strstrip(str);
3615                         if(str[0] != '\0') {
3616                                 compose_convert_header
3617                                         (buf, sizeof(buf), str,
3618                                         strlen(header) + 2);
3619                                 if(first_address) {
3620                                         fprintf(fp, "%s: ", header);
3621                                         first_address = FALSE;
3622                                 } else {
3623                                         fprintf(fp, ",");
3624                                 }
3625                                 fprintf(fp, "%s", buf);
3626                         }
3627                 }
3628         }
3629         if(!first_address) {
3630                 fprintf(fp, "\n");
3631         }
3632
3633         g_free(header_w_colon);
3634
3635         return(0);
3636 }
3637
3638 static gint compose_write_headers(Compose *compose, FILE *fp,
3639                                   const gchar *charset, EncodingType encoding,
3640                                   gboolean is_draft)
3641 {
3642         gchar buf[BUFFSIZE];
3643         gchar *str;
3644         /* struct utsname utsbuf; */
3645
3646         g_return_val_if_fail(fp != NULL, -1);
3647         g_return_val_if_fail(charset != NULL, -1);
3648         g_return_val_if_fail(compose->account != NULL, -1);
3649         g_return_val_if_fail(compose->account->address != NULL, -1);
3650
3651         /* Date */
3652         if (compose->account->add_date) {
3653                 get_rfc822_date(buf, sizeof(buf));
3654                 fprintf(fp, "Date: %s\n", buf);
3655         }
3656
3657         /* From */
3658         if (!IS_IN_CUSTOM_HEADER("From")) {
3659                 if (compose->account->name && *compose->account->name) {
3660                         compose_convert_header
3661                                 (buf, sizeof(buf), compose->account->name,
3662                                  strlen("From: "));
3663                         fprintf(fp, "From: %s <%s>\n",
3664                                 buf, compose->account->address);
3665                 } else
3666                         fprintf(fp, "From: %s\n", compose->account->address);
3667         }
3668         
3669         /* To */
3670         compose_write_headers_from_headerlist(compose, fp, "To");
3671 #if 0 /* NEW COMPOSE GUI */
3672         if (compose->use_to) {
3673                 str = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
3674                 if (*str != '\0') {
3675                         Xstrdup_a(str, str, return -1);
3676                         g_strstrip(str);
3677                         if (*str != '\0') {
3678                                 compose->to_list = address_list_append
3679                                         (compose->to_list, str);
3680                                 if (!IS_IN_CUSTOM_HEADER("To")) {
3681                                         compose_convert_header
3682                                                 (buf, sizeof(buf), str,
3683                                                  strlen("To: "));
3684                                         fprintf(fp, "To: %s\n", buf);
3685                                 }
3686                         }
3687                 }
3688         }
3689 #endif
3690
3691         /* Newsgroups */
3692         compose_write_headers_from_headerlist(compose, fp, "Newsgroups");
3693 #if 0 /* NEW COMPOSE GUI */
3694         str = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
3695         if (*str != '\0') {
3696                 Xstrdup_a(str, str, return -1);
3697                 g_strstrip(str);
3698                 remove_space(str);
3699                 if (*str != '\0') {
3700                         compose->newsgroup_list =
3701                                 newsgroup_list_append(compose->newsgroup_list,
3702                                                       str);
3703                         if (!IS_IN_CUSTOM_HEADER("Newsgroups")) {
3704                                 compose_convert_header(buf, sizeof(buf), str,
3705                                                        strlen("Newsgroups: "));
3706                                 fprintf(fp, "Newsgroups: %s\n", buf);
3707                         }
3708                 }
3709         }
3710 #endif
3711         /* Cc */
3712         compose_write_headers_from_headerlist(compose, fp, "Cc");
3713 #if 0 /* NEW COMPOSE GUI */
3714         if (compose->use_cc) {
3715                 str = gtk_entry_get_text(GTK_ENTRY(compose->cc_entry));
3716                 if (*str != '\0') {
3717                         Xstrdup_a(str, str, return -1);
3718                         g_strstrip(str);
3719                         if (*str != '\0') {
3720                                 compose->to_list = address_list_append
3721                                         (compose->to_list, str);
3722                                 if (!IS_IN_CUSTOM_HEADER("Cc")) {
3723                                         compose_convert_header
3724                                                 (buf, sizeof(buf), str,
3725                                                  strlen("Cc: "));
3726                                         fprintf(fp, "Cc: %s\n", buf);
3727                                 }
3728                         }
3729                 }
3730         }
3731 #endif
3732         /* Bcc */
3733         compose_write_headers_from_headerlist(compose, fp, "Bcc");
3734 #if 0 /* NEW COMPOSE GUI */
3735         if (compose->use_bcc) {
3736                 str = gtk_entry_get_text(GTK_ENTRY(compose->bcc_entry));
3737                 if (*str != '\0') {
3738                         Xstrdup_a(str, str, return -1);
3739                         g_strstrip(str);
3740                         if (*str != '\0') {
3741                                 compose->to_list = address_list_append
3742                                         (compose->to_list, str);
3743                                 compose_convert_header(buf, sizeof(buf), str,
3744                                                        strlen("Bcc: "));
3745                                 fprintf(fp, "Bcc: %s\n", buf);
3746                         }
3747                 }
3748         }
3749 #endif
3750
3751         /* Subject */
3752         str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
3753         if (*str != '\0' && !IS_IN_CUSTOM_HEADER("Subject")) {
3754                 Xstrdup_a(str, str, return -1);
3755                 g_strstrip(str);
3756                 if (*str != '\0') {
3757                         compose_convert_header(buf, sizeof(buf), str,
3758                                                strlen("Subject: "));
3759                         fprintf(fp, "Subject: %s\n", buf);
3760                 }
3761         }
3762
3763         /* Message-ID */
3764         if (compose->account->gen_msgid) {
3765                 compose_generate_msgid(compose, buf, sizeof(buf));
3766                 fprintf(fp, "Message-Id: <%s>\n", buf);
3767                 compose->msgid = g_strdup(buf);
3768         }
3769
3770         /* In-Reply-To */
3771         if (compose->inreplyto && compose->to_list)
3772                 fprintf(fp, "In-Reply-To: <%s>\n", compose->inreplyto);
3773
3774         /* References */
3775         if (compose->references)
3776                 fprintf(fp, "References: %s\n", compose->references);
3777
3778         /* Followup-To */
3779         compose_write_headers_from_headerlist(compose, fp, "Followup-To");
3780 #if 0 /* NEW COMPOSE GUI */
3781         if (compose->use_followupto && !IS_IN_CUSTOM_HEADER("Followup-To")) {
3782                 str = gtk_entry_get_text(GTK_ENTRY(compose->followup_entry));
3783                 if (*str != '\0') {
3784                         Xstrdup_a(str, str, return -1);
3785                         g_strstrip(str);
3786                         remove_space(str);
3787                         if (*str != '\0') {
3788                                 compose_convert_header(buf, sizeof(buf), str,
3789                                                        strlen("Followup-To: "));
3790                                 fprintf(fp, "Followup-To: %s\n", buf);
3791                         }
3792                 }
3793         }
3794 #endif
3795         /* Reply-To */
3796         compose_write_headers_from_headerlist(compose, fp, "Reply-To");
3797 #if 0 /* NEW COMPOSE GUI */
3798         if (compose->use_replyto && !IS_IN_CUSTOM_HEADER("Reply-To")) {
3799                 str = gtk_entry_get_text(GTK_ENTRY(compose->reply_entry));
3800                 if (*str != '\0') {
3801                         Xstrdup_a(str, str, return -1);
3802                         g_strstrip(str);
3803                         if (*str != '\0') {
3804                                 compose_convert_header(buf, sizeof(buf), str,
3805                                                        strlen("Reply-To: "));
3806                                 fprintf(fp, "Reply-To: %s\n", buf);
3807                         }
3808                 }
3809         }
3810 #endif
3811         /* Organization */
3812         if (compose->account->organization &&
3813             !IS_IN_CUSTOM_HEADER("Organization")) {
3814                 compose_convert_header(buf, sizeof(buf),
3815                                        compose->account->organization,
3816                                        strlen("Organization: "));
3817                 fprintf(fp, "Organization: %s\n", buf);
3818         }
3819
3820         /* Program version and system info */
3821         /* uname(&utsbuf); */
3822         if (g_slist_length(compose->to_list) && !IS_IN_CUSTOM_HEADER("X-Mailer")) {
3823                 fprintf(fp, "X-Mailer: %s (GTK+ %d.%d.%d; %s)\n",
3824                         prog_version,
3825                         gtk_major_version, gtk_minor_version, gtk_micro_version,
3826                         HOST_ALIAS);
3827                         /* utsbuf.sysname, utsbuf.release, utsbuf.machine); */
3828         }
3829         if (g_slist_length(compose->newsgroup_list) && !IS_IN_CUSTOM_HEADER("X-Newsreader")) {
3830                 fprintf(fp, "X-Newsreader: %s (GTK+ %d.%d.%d; %s)\n",
3831                         prog_version,
3832                         gtk_major_version, gtk_minor_version, gtk_micro_version,
3833                         HOST_ALIAS);
3834                         /* utsbuf.sysname, utsbuf.release, utsbuf.machine); */
3835         }
3836
3837         /* custom headers */
3838         if (compose->account->add_customhdr) {
3839                 GSList *cur;
3840
3841                 for (cur = compose->account->customhdr_list; cur != NULL;
3842                      cur = cur->next) {
3843                         CustomHeader *chdr = (CustomHeader *)cur->data;
3844
3845                         if (strcasecmp(chdr->name, "Date")         != 0 &&
3846                             strcasecmp(chdr->name, "From")         != 0 &&
3847                             strcasecmp(chdr->name, "To")           != 0 &&
3848                          /* strcasecmp(chdr->name, "Sender")       != 0 && */
3849                             strcasecmp(chdr->name, "Message-Id")   != 0 &&
3850                             strcasecmp(chdr->name, "In-Reply-To")  != 0 &&
3851                             strcasecmp(chdr->name, "References")   != 0 &&
3852                             strcasecmp(chdr->name, "Mime-Version") != 0 &&
3853                             strcasecmp(chdr->name, "Content-Type") != 0 &&
3854                             strcasecmp(chdr->name, "Content-Transfer-Encoding")
3855                             != 0) {
3856                                 compose_convert_header
3857                                         (buf, sizeof(buf),
3858                                          chdr->value ? chdr->value : "",
3859                                          strlen(chdr->name) + 2);
3860                                 fprintf(fp, "%s: %s\n", chdr->name, buf);
3861                         }
3862                 }
3863         }
3864
3865         /* MIME */
3866         fprintf(fp, "Mime-Version: 1.0\n");
3867         if (compose_use_attach(compose)) {
3868                 get_rfc822_date(buf, sizeof(buf));
3869                 subst_char(buf, ' ', '_');
3870                 subst_char(buf, ',', '_');
3871                 compose->boundary = g_strdup_printf("Multipart_%s_%08x",
3872                                                     buf, (guint)compose);
3873                 fprintf(fp,
3874                         "Content-Type: multipart/mixed;\n"
3875                         " boundary=\"%s\"\n", compose->boundary);
3876         } else {
3877                 fprintf(fp, "Content-Type: text/plain; charset=%s\n", charset);
3878                 fprintf(fp, "Content-Transfer-Encoding: %s\n",
3879                         procmime_get_encoding_str(encoding));
3880         }
3881
3882         /* Request Return Receipt */
3883         if (!IS_IN_CUSTOM_HEADER("Disposition-Notification-To")) {
3884                 if (compose->return_receipt) {
3885                         if (compose->account->name
3886                             && *compose->account->name) {
3887                                 compose_convert_header(buf, sizeof(buf), compose->account->name, strlen("Disposition-Notification-To: "));
3888                                 fprintf(fp, "Disposition-Notification-To: %s <%s>\n", buf, compose->account->address);
3889                         } else
3890                                 fprintf(fp, "Disposition-Notification-To: %s\n", compose->account->address);
3891                 }
3892         }
3893
3894         /* separator between header and body */
3895         fputs("\n", fp);
3896
3897         return 0;
3898 }
3899
3900 #undef IS_IN_CUSTOM_HEADER
3901
3902 static void compose_convert_header(gchar *dest, gint len, gchar *src,
3903                                    gint header_len)
3904 {
3905         g_return_if_fail(src != NULL);
3906         g_return_if_fail(dest != NULL);
3907
3908         if (len < 1) return;
3909
3910         remove_return(src);
3911
3912         if (is_ascii_str(src)) {
3913                 strncpy2(dest, src, len);
3914                 dest[len - 1] = '\0';
3915                 return;
3916         } else
3917                 conv_encode_header(dest, len, src, header_len);
3918 }
3919
3920 static void compose_generate_msgid(Compose *compose, gchar *buf, gint len)
3921 {
3922         struct tm *lt;
3923         time_t t;
3924         gchar *addr;
3925
3926         t = time(NULL);
3927         lt = localtime(&t);
3928
3929         if (compose->account && compose->account->address &&
3930             *compose->account->address) {
3931                 if (strchr(compose->account->address, '@'))
3932                         addr = g_strdup(compose->account->address);
3933                 else
3934                         addr = g_strconcat(compose->account->address, "@",
3935                                            get_domain_name(), NULL);
3936         } else
3937                 addr = g_strconcat(g_get_user_name(), "@", get_domain_name(),
3938                                    NULL);
3939
3940         g_snprintf(buf, len, "%04d%02d%02d%02d%02d%02d.%08x.%s",
3941                    lt->tm_year + 1900, lt->tm_mon + 1,
3942                    lt->tm_mday, lt->tm_hour,
3943                    lt->tm_min, lt->tm_sec,
3944                    (guint)random(), addr);
3945
3946         debug_print(_("generated Message-ID: %s\n"), buf);
3947
3948         g_free(addr);
3949 }
3950
3951 static void compose_add_entry_field(GtkWidget *table, GtkWidget **hbox,
3952                                     GtkWidget **entry, gint *count,
3953                                     const gchar *label_str,
3954                                     gboolean is_addr_entry)
3955 {
3956         GtkWidget *label;
3957
3958         if (GTK_TABLE(table)->nrows < (*count) + 1)
3959                 gtk_table_resize(GTK_TABLE(table), (*count) + 1, 2);
3960
3961         *hbox = gtk_hbox_new(FALSE, 0);
3962         label = gtk_label_new
3963                 (prefs_common.trans_hdr ? gettext(label_str) : label_str);
3964         gtk_box_pack_end(GTK_BOX(*hbox), label, FALSE, FALSE, 0);
3965         gtk_table_attach(GTK_TABLE(table), *hbox, 0, 1, *count, (*count) + 1,
3966                          GTK_FILL, 0, 2, 0);
3967         *entry = gtk_entry_new_with_max_length(MAX_ENTRY_LENGTH);
3968         gtk_table_attach
3969                 (GTK_TABLE(table), *entry, 1, 2, *count, (*count) + 1, GTK_FILL | GTK_EXPAND, GTK_SHRINK, 0, 0);
3970 #if 0 /* NEW COMPOSE GUI */
3971         if (GTK_TABLE(table)->nrows > (*count) + 1)
3972                 gtk_table_set_row_spacing(GTK_TABLE(table), *count, 4);
3973 #endif
3974
3975         if (is_addr_entry)
3976                 address_completion_register_entry(GTK_ENTRY(*entry));
3977
3978         (*count)++;
3979 }
3980
3981 static void compose_create_header_entry(Compose *compose) 
3982 {
3983         gchar *headers[] = {"To:", "Cc:", "Bcc:", "Newsgroups:", "Reply-To:", "Followup-To:", NULL};
3984
3985         GtkWidget *combo;
3986         GtkWidget *entry;
3987         GList *combo_list = NULL;
3988         gchar **string, *header;
3989         ComposeHeaderEntry *headerentry;
3990
3991         headerentry = g_new0(ComposeHeaderEntry, 1);
3992
3993         /* Combo box */
3994         combo = gtk_combo_new();
3995         string = headers; 
3996         while(*string != NULL) {
3997             combo_list = g_list_append(combo_list, (prefs_common.trans_hdr ? gettext(*string) : *string));
3998             string++;
3999         }
4000         gtk_combo_set_popdown_strings(GTK_COMBO(combo), combo_list);
4001         g_list_free(combo_list);
4002         gtk_editable_set_editable(GTK_EDITABLE(GTK_COMBO(combo)->entry), FALSE);
4003         gtk_widget_show(combo);
4004         gtk_table_attach(GTK_TABLE(compose->header_table), combo, 0, 1, compose->header_nextrow, compose->header_nextrow+1, GTK_SHRINK, GTK_FILL, 0, 0);
4005         if(compose->header_last) {      
4006                 header = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(compose->header_last->combo)->entry));
4007         } else {
4008                 switch(compose->account->protocol) {
4009                         case A_NNTP:
4010                                 header = prefs_common.trans_hdr ? _("Newsgroups:") : "Newsgroups:";
4011                                 break;
4012                         default:
4013                                 header = prefs_common.trans_hdr ? _("To:") : "To:";
4014                                 break;
4015                 }                                                                   
4016         }
4017         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo)->entry), header);
4018
4019         /* Entry field */
4020         entry = gtk_entry_new(); 
4021         gtk_widget_show(entry);
4022         gtk_table_attach(GTK_TABLE(compose->header_table), entry, 1, 2, compose->header_nextrow, compose->header_nextrow+1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
4023
4024         gtk_signal_connect(GTK_OBJECT(entry), "key-press-event", GTK_SIGNAL_FUNC(compose_headerentry_key_press_event_cb), headerentry);
4025         gtk_signal_connect(GTK_OBJECT(entry), "changed", GTK_SIGNAL_FUNC(compose_headerentry_changed_cb), headerentry);
4026         gtk_signal_connect(GTK_OBJECT(entry), "activate", GTK_SIGNAL_FUNC(text_activated), compose);
4027
4028         address_completion_register_entry(GTK_ENTRY(entry));
4029
4030         headerentry->compose = compose;
4031         headerentry->combo = combo;
4032         headerentry->entry = entry;
4033         headerentry->headernum = compose->header_nextrow;
4034
4035         compose->header_nextrow++;
4036         compose->header_last = headerentry;
4037 }
4038
4039 static void compose_add_header_entry(Compose *compose, gchar *header, gchar *text) 
4040 {
4041         ComposeHeaderEntry *last_header;
4042         
4043         last_header = compose->header_last;
4044         
4045         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(last_header->combo)->entry), header);
4046         gtk_entry_set_text(GTK_ENTRY(last_header->entry), text);
4047 }
4048
4049 static GtkWidget *compose_create_header(Compose *compose) 
4050 {
4051         GtkWidget *label;
4052         GtkWidget *hbox;
4053         GtkWidget *from_optmenu_hbox;
4054 #if 0 /* NEW COMPOSE GUI */
4055         GtkWidget *to_entry;
4056         GtkWidget *to_hbox;
4057         GtkWidget *newsgroups_entry;
4058         GtkWidget *newsgroups_hbox;
4059 #endif
4060         GtkWidget *header_scrolledwin;
4061         GtkWidget *header_table;
4062 #if 0 /* NEW COMPOSE GUI */
4063         GtkWidget *cc_entry;
4064         GtkWidget *cc_hbox;
4065         GtkWidget *bcc_entry;
4066         GtkWidget *bcc_hbox;
4067         GtkWidget *reply_entry;
4068         GtkWidget *reply_hbox;
4069         GtkWidget *followup_entry;
4070         GtkWidget *followup_hbox;
4071 #endif
4072
4073         gint count = 0;
4074
4075         /* header labels and entries */
4076         header_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
4077         gtk_widget_show(header_scrolledwin);
4078         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(header_scrolledwin), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
4079
4080         header_table = gtk_table_new(2, 2, FALSE);
4081         gtk_widget_show(header_table);
4082         gtk_container_set_border_width(GTK_CONTAINER(header_table), 2);
4083         gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(header_scrolledwin), header_table);
4084         gtk_viewport_set_shadow_type(GTK_VIEWPORT(GTK_BIN(header_scrolledwin)->child), GTK_SHADOW_ETCHED_IN);
4085         count = 0;
4086
4087         /* option menu for selecting accounts */
4088         hbox = gtk_hbox_new(FALSE, 0);
4089         label = gtk_label_new(prefs_common.trans_hdr ? _("From:") : "From:");
4090         gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
4091         gtk_table_attach(GTK_TABLE(header_table), hbox, 0, 1, count, count + 1,
4092                          GTK_FILL, 0, 2, 0);
4093         from_optmenu_hbox = compose_account_option_menu_create(compose);
4094         gtk_table_attach(GTK_TABLE(header_table), from_optmenu_hbox,
4095                                   1, 2, count, count + 1, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
4096 #if 0 /* NEW COMPOSE GUI */
4097         gtk_table_set_row_spacing(GTK_TABLE(table), 0, 4);
4098 #endif
4099         count++;
4100
4101         compose->header_table = header_table;
4102         compose->header_list = NULL;
4103         compose->header_nextrow = count;
4104
4105         compose_create_header_entry(compose);
4106
4107 #if 0 /* NEW COMPOSE GUI */
4108         compose_add_entry_field(table, &to_hbox, &to_entry, &count,
4109                                 "To:", TRUE); 
4110         gtk_table_set_row_spacing(GTK_TABLE(table), 0, 4);
4111         compose_add_entry_field(table, &newsgroups_hbox, &newsgroups_entry,
4112                                 &count, "Newsgroups:", FALSE);
4113         gtk_table_set_row_spacing(GTK_TABLE(table), 1, 4);
4114
4115         gtk_table_set_row_spacing(GTK_TABLE(table), 2, 4);
4116
4117         compose_add_entry_field(table, &cc_hbox, &cc_entry, &count,
4118                                 "Cc:", TRUE);
4119         gtk_table_set_row_spacing(GTK_TABLE(table), 3, 4);
4120         compose_add_entry_field(table, &bcc_hbox, &bcc_entry, &count,
4121                                 "Bcc:", TRUE);
4122         gtk_table_set_row_spacing(GTK_TABLE(table), 4, 4);
4123         compose_add_entry_field(table, &reply_hbox, &reply_entry, &count,
4124                                 "Reply-To:", TRUE);
4125         gtk_table_set_row_spacing(GTK_TABLE(table), 5, 4);
4126         compose_add_entry_field(table, &followup_hbox, &followup_entry, &count,
4127                                 "Followup-To:", FALSE);
4128         gtk_table_set_row_spacing(GTK_TABLE(table), 6, 4);
4129
4130         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
4131
4132         gtk_signal_connect(GTK_OBJECT(to_entry), "activate",
4133                            GTK_SIGNAL_FUNC(to_activated), compose);
4134         gtk_signal_connect(GTK_OBJECT(newsgroups_entry), "activate",
4135                            GTK_SIGNAL_FUNC(newsgroups_activated), compose);
4136         gtk_signal_connect(GTK_OBJECT(subject_entry), "activate",
4137                            GTK_SIGNAL_FUNC(subject_activated), compose);
4138         gtk_signal_connect(GTK_OBJECT(cc_entry), "activate",
4139                            GTK_SIGNAL_FUNC(cc_activated), compose);
4140         gtk_signal_connect(GTK_OBJECT(bcc_entry), "activate",
4141                            GTK_SIGNAL_FUNC(bcc_activated), compose);
4142         gtk_signal_connect(GTK_OBJECT(reply_entry), "activate",
4143                            GTK_SIGNAL_FUNC(replyto_activated), compose);
4144         gtk_signal_connect(GTK_OBJECT(followup_entry), "activate",
4145                            GTK_SIGNAL_FUNC(followupto_activated), compose);
4146
4147         gtk_signal_connect(GTK_OBJECT(subject_entry), "grab_focus",
4148                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
4149         gtk_signal_connect(GTK_OBJECT(to_entry), "grab_focus",
4150                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
4151         gtk_signal_connect(GTK_OBJECT(newsgroups_entry), "grab_focus",
4152                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
4153         gtk_signal_connect(GTK_OBJECT(cc_entry), "grab_focus",
4154                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
4155         gtk_signal_connect(GTK_OBJECT(bcc_entry), "grab_focus",
4156                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
4157         gtk_signal_connect(GTK_OBJECT(reply_entry), "grab_focus",
4158                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
4159         gtk_signal_connect(GTK_OBJECT(followup_entry), "grab_focus",
4160                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
4161 #endif
4162
4163         compose->table            = NULL;
4164 #if 0 /* NEW COMPOSE GUI */
4165         compose->table            = table;
4166         compose->to_hbox          = to_hbox;
4167         compose->to_entry         = to_entry;
4168         compose->newsgroups_hbox  = newsgroups_hbox;
4169         compose->newsgroups_entry = newsgroups_entry;
4170 #endif
4171 #if 0 /* NEW COMPOSE GUI */
4172         compose->cc_hbox          = cc_hbox;
4173         compose->cc_entry         = cc_entry;
4174         compose->bcc_hbox         = bcc_hbox;
4175         compose->bcc_entry        = bcc_entry;
4176         compose->reply_hbox       = reply_hbox;
4177         compose->reply_entry      = reply_entry;
4178         compose->followup_hbox    = followup_hbox;
4179         compose->followup_entry   = followup_entry;
4180 #endif
4181
4182         return header_scrolledwin ;
4183 }
4184
4185 GtkWidget *compose_create_attach(Compose *compose)
4186 {
4187         gchar *titles[] = {_("MIME type"), _("Size"), _("Name")};
4188         gint i;
4189
4190         GtkWidget *attach_scrwin;
4191         GtkWidget *attach_clist;
4192
4193         /* attachment list */
4194         attach_scrwin = gtk_scrolled_window_new(NULL, NULL);
4195         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(attach_scrwin),
4196                                        GTK_POLICY_AUTOMATIC,
4197                                        GTK_POLICY_ALWAYS);
4198         gtk_widget_set_usize(attach_scrwin, -1, 80);
4199
4200         attach_clist = gtk_clist_new_with_titles(N_ATTACH_COLS, titles);
4201         gtk_clist_set_column_justification(GTK_CLIST(attach_clist), COL_SIZE,
4202                                            GTK_JUSTIFY_RIGHT);
4203         gtk_clist_set_column_width(GTK_CLIST(attach_clist), COL_MIMETYPE, 240);
4204         gtk_clist_set_column_width(GTK_CLIST(attach_clist), COL_SIZE, 64);
4205         gtk_clist_set_selection_mode(GTK_CLIST(attach_clist),
4206                                      GTK_SELECTION_EXTENDED);
4207         for (i = 0; i < N_ATTACH_COLS; i++)
4208                 GTK_WIDGET_UNSET_FLAGS
4209                         (GTK_CLIST(attach_clist)->column[i].button,
4210                          GTK_CAN_FOCUS);
4211         gtk_container_add(GTK_CONTAINER(attach_scrwin), attach_clist);
4212
4213         gtk_signal_connect(GTK_OBJECT(attach_clist), "select_row",
4214                            GTK_SIGNAL_FUNC(attach_selected), compose);
4215         gtk_signal_connect(GTK_OBJECT(attach_clist), "button_press_event",
4216                            GTK_SIGNAL_FUNC(attach_button_pressed), compose);
4217         gtk_signal_connect(GTK_OBJECT(attach_clist), "key_press_event",
4218                            GTK_SIGNAL_FUNC(attach_key_pressed), compose);
4219
4220         /* drag and drop */
4221         gtk_drag_dest_set(attach_clist,
4222                           GTK_DEST_DEFAULT_ALL, compose_mime_types, 1,
4223                           GDK_ACTION_COPY);
4224         gtk_signal_connect(GTK_OBJECT(attach_clist), "drag_data_received",
4225                            GTK_SIGNAL_FUNC(compose_attach_drag_received_cb),
4226                            compose);
4227
4228         compose->attach_scrwin = attach_scrwin;
4229         compose->attach_clist  = attach_clist;
4230
4231         return attach_scrwin;
4232 }
4233
4234 static void compose_savemsg_checkbtn_cb(GtkWidget *widget, Compose *compose);
4235 static void compose_savemsg_select_cb(GtkWidget *widget, Compose *compose);
4236
4237 static GtkWidget *compose_create_others(Compose *compose)
4238 {
4239         GtkWidget *table;
4240         GtkWidget *savemsg_checkbtn;
4241         GtkWidget *savemsg_entry;
4242         GtkWidget *savemsg_select;
4243         
4244         guint rowcount = 0;
4245         gchar *folderidentifier;
4246
4247         /* Table for settings */
4248         table = gtk_table_new(3, 1, FALSE);
4249         gtk_widget_show(table);
4250         gtk_table_set_row_spacings(GTK_TABLE(table), VSPACING_NARROW);
4251         rowcount = 0;
4252
4253         /* Save Message to folder */
4254         savemsg_checkbtn = gtk_check_button_new_with_label(_("Save Message to "));
4255         gtk_widget_show(savemsg_checkbtn);
4256         gtk_table_attach(GTK_TABLE(table), savemsg_checkbtn, 0, 1, rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
4257         if(folder_get_default_outbox()) {
4258                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(savemsg_checkbtn), prefs_common.savemsg);
4259         }
4260         gtk_signal_connect(GTK_OBJECT(savemsg_checkbtn), "toggled",
4261                             GTK_SIGNAL_FUNC(compose_savemsg_checkbtn_cb), compose);
4262
4263         savemsg_entry = gtk_entry_new();
4264         gtk_widget_show(savemsg_entry);
4265         gtk_table_attach_defaults(GTK_TABLE(table), savemsg_entry, 1, 2, rowcount, rowcount + 1);
4266         gtk_editable_set_editable(GTK_EDITABLE(savemsg_entry), prefs_common.savemsg);
4267         if(folder_get_default_outbox()) {
4268                 folderidentifier = folder_item_get_identifier(folder_get_default_outbox());
4269                 gtk_entry_set_text(GTK_ENTRY(savemsg_entry), folderidentifier);
4270                 g_free(folderidentifier);
4271         }
4272
4273         savemsg_select = gtk_button_new_with_label (_("Select ..."));
4274         gtk_widget_show (savemsg_select);
4275         gtk_table_attach(GTK_TABLE(table), savemsg_select, 2, 3, rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
4276         gtk_signal_connect (GTK_OBJECT (savemsg_select), "clicked",
4277                             GTK_SIGNAL_FUNC (compose_savemsg_select_cb),
4278                             compose);
4279
4280         rowcount++;
4281
4282         compose->savemsg_checkbtn = savemsg_checkbtn;
4283         compose->savemsg_entry = savemsg_entry;
4284
4285         return table;   
4286 }
4287
4288 static void compose_savemsg_checkbtn_cb(GtkWidget *widget, Compose *compose) 
4289 {
4290         gtk_editable_set_editable(GTK_EDITABLE(compose->savemsg_entry),
4291                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn)));
4292 }
4293
4294 static void compose_savemsg_select_cb(GtkWidget *widget, Compose *compose)
4295 {
4296         FolderItem *dest;
4297         gchar * path;
4298
4299         dest = foldersel_folder_sel(NULL, NULL);
4300         if (!dest) return;
4301
4302         path = folder_item_get_identifier(dest);
4303
4304         gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), path);
4305         g_free(path);
4306 }
4307
4308 static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
4309 {
4310         Compose   *compose;
4311         GtkWidget *window;
4312         GtkWidget *vbox;
4313         GtkWidget *menubar;
4314         GtkWidget *handlebox;
4315
4316         GtkWidget *notebook;
4317
4318         GtkWidget *vbox2;
4319
4320         GtkWidget *label;
4321         GtkWidget *subject_hbox;
4322         GtkWidget *subject_frame;
4323         GtkWidget *subject_entry;
4324         GtkWidget *subject;
4325         GtkWidget *paned;
4326
4327         GtkWidget *edit_vbox;
4328         GtkWidget *ruler_hbox;
4329         GtkWidget *ruler;
4330         GtkWidget *scrolledwin;
4331         GtkWidget *text;
4332
4333         GtkWidget *table;
4334
4335         UndoMain *undostruct;
4336
4337         guint n_menu_entries;
4338         GtkStyle  *style, *new_style;
4339         GdkColormap *cmap;
4340         GdkColor color[1];
4341         gboolean success[1];
4342         GdkFont   *font;
4343         GtkWidget *popupmenu;
4344         GtkWidget *menuitem;
4345         GtkItemFactory *popupfactory;
4346         GtkItemFactory *ifactory;
4347         GtkWidget *tmpl_menu;
4348         gint n_entries;
4349
4350 #if USE_PSPELL
4351         GtkPspell * gtkpspell = NULL;
4352 #endif
4353
4354         g_return_val_if_fail(account != NULL, NULL);
4355
4356         debug_print(_("Creating compose window...\n"));
4357         compose = g_new0(Compose, 1);
4358
4359         compose->account = account;
4360         compose->orig_account = account;
4361
4362         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
4363         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
4364         gtk_widget_set_usize(window, -1, prefs_common.compose_height);
4365         gtk_window_set_wmclass(GTK_WINDOW(window), "compose window", "Sylpheed");
4366         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
4367                            GTK_SIGNAL_FUNC(compose_delete_cb), compose);
4368         gtk_signal_connect(GTK_OBJECT(window), "destroy",
4369                            GTK_SIGNAL_FUNC(compose_destroy_cb), compose);
4370         gtk_signal_connect(GTK_OBJECT(window), "focus_in_event",
4371                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
4372         gtk_signal_connect(GTK_OBJECT(window), "focus_out_event",
4373                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
4374         gtk_widget_realize(window);
4375
4376         gtkut_widget_set_composer_icon(window);
4377
4378         vbox = gtk_vbox_new(FALSE, 0);
4379         gtk_container_add(GTK_CONTAINER(window), vbox);
4380
4381         n_menu_entries = sizeof(compose_entries) / sizeof(compose_entries[0]);
4382         menubar = menubar_create(window, compose_entries,
4383                                  n_menu_entries, "<Compose>", compose);
4384         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
4385
4386         handlebox = gtk_handle_box_new();
4387         gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
4388
4389         compose_toolbar_create(compose, handlebox);
4390
4391         vbox2 = gtk_vbox_new(FALSE, 2);
4392         gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
4393         gtk_container_set_border_width(GTK_CONTAINER(vbox2), BORDER_WIDTH);
4394         
4395         /* Notebook */
4396         notebook = gtk_notebook_new();
4397         gtk_widget_set_usize(notebook, -1, 130);
4398         gtk_widget_show(notebook);
4399
4400         /* header labels and entries */
4401         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), compose_create_header(compose), gtk_label_new(_("Header")));
4402         /* attachment list */
4403         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), compose_create_attach(compose), gtk_label_new(_("Attachments")));
4404         /* Others Tab */
4405         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), compose_create_others(compose), gtk_label_new(_("Others")));
4406
4407         /* Subject */
4408         subject_hbox = gtk_hbox_new(FALSE, 0);
4409         gtk_widget_show(subject_hbox);
4410
4411         subject_frame = gtk_frame_new(NULL);
4412         gtk_frame_set_shadow_type(GTK_FRAME(subject_frame), GTK_SHADOW_OUT);
4413         gtk_box_pack_start(GTK_BOX(subject_hbox), subject_frame, TRUE, TRUE, BORDER_WIDTH+1);
4414         gtk_widget_show(subject_frame);
4415
4416         subject = gtk_hbox_new(FALSE, 0);
4417         gtk_container_set_border_width(GTK_CONTAINER(subject), BORDER_WIDTH);
4418         gtk_widget_show(subject);
4419
4420         label = gtk_label_new(_("Subject:"));
4421         gtk_box_pack_start(GTK_BOX(subject), label, FALSE, FALSE, 4);
4422         gtk_widget_show(label);
4423
4424         subject_entry = gtk_entry_new();
4425         gtk_box_pack_start(GTK_BOX(subject), subject_entry, TRUE, TRUE, 2);
4426         gtk_signal_connect(GTK_OBJECT(subject_entry), "activate", GTK_SIGNAL_FUNC(text_activated), compose);
4427         gtk_widget_show(subject_entry);
4428         compose->subject_entry = subject_entry;
4429         gtk_container_add(GTK_CONTAINER(subject_frame), subject);
4430         
4431         edit_vbox = gtk_vbox_new(FALSE, 0);
4432 #if 0 /* NEW COMPOSE GUI */
4433         gtk_box_pack_start(GTK_BOX(vbox2), edit_vbox, TRUE, TRUE, 0);
4434 #endif
4435
4436         gtk_box_pack_start(GTK_BOX(edit_vbox), subject_hbox, FALSE, FALSE, 0);
4437
4438         /* ruler */
4439         ruler_hbox = gtk_hbox_new(FALSE, 0);
4440         gtk_box_pack_start(GTK_BOX(edit_vbox), ruler_hbox, FALSE, FALSE, 0);
4441
4442         ruler = gtk_shruler_new();
4443         gtk_ruler_set_range(GTK_RULER(ruler), 0.0, 100.0, 1.0, 100.0);
4444         gtk_box_pack_start(GTK_BOX(ruler_hbox), ruler, TRUE, TRUE,
4445                            BORDER_WIDTH + 1);
4446         gtk_widget_set_usize(ruler_hbox, 1, -1);
4447
4448         /* text widget */
4449         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
4450         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
4451                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
4452         gtk_box_pack_start(GTK_BOX(edit_vbox), scrolledwin, TRUE, TRUE, 0);
4453         gtk_widget_set_usize(scrolledwin, prefs_common.compose_width, -1);
4454
4455         text = gtk_stext_new(gtk_scrolled_window_get_hadjustment
4456                             (GTK_SCROLLED_WINDOW(scrolledwin)),
4457                             gtk_scrolled_window_get_vadjustment
4458                             (GTK_SCROLLED_WINDOW(scrolledwin)));
4459         GTK_STEXT(text)->default_tab_width = 8;
4460         gtk_stext_set_editable(GTK_STEXT(text), TRUE);
4461
4462         if (prefs_common.block_cursor) {
4463                 GTK_STEXT(text)->cursor_type = GTK_STEXT_CURSOR_BLOCK;
4464         }
4465         
4466         if (prefs_common.smart_wrapping) {      
4467                 gtk_stext_set_word_wrap(GTK_STEXT(text), TRUE);
4468                 gtk_stext_set_wrap_rmargin(GTK_STEXT(text), prefs_common.linewrap_len);
4469         }               
4470
4471         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
4472
4473         gtk_signal_connect(GTK_OBJECT(text), "changed",
4474                            GTK_SIGNAL_FUNC(compose_changed_cb), compose);
4475         gtk_signal_connect(GTK_OBJECT(text), "grab_focus",
4476                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
4477         gtk_signal_connect(GTK_OBJECT(text), "activate",
4478                            GTK_SIGNAL_FUNC(text_activated), compose);
4479         gtk_signal_connect_after(GTK_OBJECT(text), "button_press_event",
4480                                  GTK_SIGNAL_FUNC(compose_button_press_cb),
4481                                  edit_vbox);
4482 #if 0
4483         gtk_signal_connect_after(GTK_OBJECT(text), "key_press_event",
4484                                  GTK_SIGNAL_FUNC(compose_key_press_cb),
4485                                  compose);
4486 #endif
4487         gtk_signal_connect_after(GTK_OBJECT(text), "size_allocate",
4488                                  GTK_SIGNAL_FUNC(compose_edit_size_alloc),
4489                                  ruler);
4490
4491         /* drag and drop */
4492         gtk_drag_dest_set(text, GTK_DEST_DEFAULT_ALL, compose_mime_types, 1,
4493                           GDK_ACTION_COPY);
4494         gtk_signal_connect(GTK_OBJECT(text), "drag_data_received",
4495                            GTK_SIGNAL_FUNC(compose_insert_drag_received_cb),
4496                            compose);
4497         gtk_widget_show_all(vbox);
4498
4499         /* pane between attach clist and text */
4500         paned = gtk_vpaned_new();
4501         gtk_paned_set_gutter_size(GTK_PANED(paned), 12);
4502         gtk_paned_set_handle_size(GTK_PANED(paned), 12);
4503         gtk_container_add(GTK_CONTAINER(vbox2), paned);
4504         gtk_paned_add1(GTK_PANED(paned), notebook);
4505         gtk_paned_add2(GTK_PANED(paned), edit_vbox);
4506         gtk_widget_show_all(paned);
4507
4508         style = gtk_widget_get_style(text);
4509
4510         /* workaround for the slow down of GtkSText when using Pixmap theme */
4511         if (style->engine) {
4512                 GtkThemeEngine *engine;
4513
4514                 engine = style->engine;
4515                 style->engine = NULL;
4516                 new_style = gtk_style_copy(style);
4517                 style->engine = engine;
4518         } else
4519                 new_style = gtk_style_copy(style);
4520
4521         if (prefs_common.textfont) {
4522                 CharSet charset;
4523
4524                 charset = conv_get_current_charset();
4525                 if (MB_CUR_MAX == 1) {
4526                         gchar *fontstr, *p;
4527
4528                         Xstrdup_a(fontstr, prefs_common.textfont, );
4529                         if (fontstr && (p = strchr(fontstr, ',')) != NULL)
4530                                 *p = '\0';
4531                         font = gdk_font_load(fontstr);
4532                 } else
4533                         font = gdk_fontset_load(prefs_common.textfont);
4534                 if (font) {
4535                         gdk_font_unref(new_style->font);
4536                         new_style->font = font;
4537                 }
4538         }
4539
4540         gtk_widget_set_style(text, new_style);
4541
4542         color[0] = quote_color;
4543         cmap = gdk_window_get_colormap(window->window);
4544         gdk_colormap_alloc_colors(cmap, color, 1, FALSE, TRUE, success);
4545         if (success[0] == FALSE) {
4546                 g_warning("Compose: color allocation failed.\n");
4547                 style = gtk_widget_get_style(text);
4548                 quote_color = style->black;
4549         }
4550
4551         n_entries = sizeof(compose_popup_entries) /
4552                 sizeof(compose_popup_entries[0]);
4553         popupmenu = menu_create_items(compose_popup_entries, n_entries,
4554                                       "<Compose>", &popupfactory,
4555                                       compose);
4556
4557         ifactory = gtk_item_factory_from_widget(menubar);
4558         menu_set_sensitive(ifactory, "/Edit/Undo", FALSE);
4559         menu_set_sensitive(ifactory, "/Edit/Redo", FALSE);
4560
4561         tmpl_menu = gtk_item_factory_get_item(ifactory, "/Tool/Template");
4562 #if 0 /* NEW COMPOSE GUI */
4563         gtk_widget_hide(bcc_hbox);
4564         gtk_widget_hide(bcc_entry);
4565         gtk_widget_hide(reply_hbox);
4566         gtk_widget_hide(reply_entry);
4567         gtk_widget_hide(followup_hbox);
4568         gtk_widget_hide(followup_entry);
4569         gtk_widget_hide(ruler_hbox);
4570         gtk_table_set_row_spacing(GTK_TABLE(table), 4, 0);
4571         gtk_table_set_row_spacing(GTK_TABLE(table), 5, 0);
4572         gtk_table_set_row_spacing(GTK_TABLE(table), 6, 0);
4573
4574         if (account->protocol == A_NNTP) {
4575                 gtk_widget_hide(to_hbox);
4576                 gtk_widget_hide(to_entry);
4577                 gtk_widget_hide(cc_hbox);
4578                 gtk_widget_hide(cc_entry);
4579                 gtk_table_set_row_spacing(GTK_TABLE(table), 1, 0);
4580                 gtk_table_set_row_spacing(GTK_TABLE(table), 3, 0);
4581         } else {
4582                 gtk_widget_hide(newsgroups_hbox);
4583                 gtk_widget_hide(newsgroups_entry);
4584                 gtk_table_set_row_spacing(GTK_TABLE(table), 2, 0);
4585                 menu_set_sensitive(ifactory, "/View/Followup to", FALSE);
4586         }
4587 #endif
4588
4589         update_compose_actions_menu(ifactory, "/Edit/Actions", compose);
4590
4591         switch (prefs_common.toolbar_style) {
4592         case TOOLBAR_NONE:
4593                 gtk_widget_hide(handlebox);
4594                 break;
4595         case TOOLBAR_ICON:
4596                 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
4597                                       GTK_TOOLBAR_ICONS);
4598                 break;
4599         case TOOLBAR_TEXT:
4600                 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
4601                                       GTK_TOOLBAR_TEXT);
4602                 break;
4603         case TOOLBAR_BOTH:
4604                 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
4605                                       GTK_TOOLBAR_BOTH);
4606                 break;
4607         }
4608
4609         undostruct = undo_init(text);
4610         undo_set_change_state_func(undostruct, &compose_undo_state_changed,
4611                                    menubar);
4612
4613         gtk_widget_show(window);
4614
4615         address_completion_start(window);
4616
4617         compose->window        = window;
4618         compose->vbox          = vbox;
4619         compose->menubar       = menubar;
4620         compose->handlebox     = handlebox;
4621
4622         compose->vbox2         = vbox2;
4623
4624         compose->paned = paned;
4625
4626         compose->edit_vbox     = edit_vbox;
4627         compose->ruler_hbox    = ruler_hbox;
4628         compose->ruler         = ruler;
4629         compose->scrolledwin   = scrolledwin;
4630         compose->text          = text;
4631
4632         compose->focused_editable = NULL;
4633
4634         compose->popupmenu    = popupmenu;
4635         compose->popupfactory = popupfactory;
4636
4637         compose->tmpl_menu = tmpl_menu;
4638
4639         compose->mode = mode;
4640
4641         compose->targetinfo = NULL;
4642         compose->replyinfo  = NULL;
4643
4644         compose->replyto     = NULL;
4645         compose->mailinglist = NULL;
4646         compose->cc          = NULL;
4647         compose->bcc         = NULL;
4648         compose->followup_to = NULL;
4649         compose->inreplyto   = NULL;
4650         compose->references  = NULL;
4651         compose->msgid       = NULL;
4652         compose->boundary    = NULL;
4653
4654 #if USE_GPGME
4655         compose->use_signing    = FALSE;
4656         compose->use_encryption = FALSE;
4657 #endif /* USE_GPGME */
4658
4659         compose->modified = FALSE;
4660
4661         compose->return_receipt = FALSE;
4662
4663         compose->to_list        = NULL;
4664         compose->newsgroup_list = NULL;
4665
4666         compose->exteditor_file    = NULL;
4667         compose->exteditor_pid     = -1;
4668         compose->exteditor_readdes = -1;
4669         compose->exteditor_tag     = -1;
4670
4671         compose->bounce_filename = NULL;
4672         compose->undostruct = undostruct;
4673 #if USE_PSPELL
4674         
4675         menu_set_sensitive(ifactory, "/Spelling", FALSE);
4676         if (prefs_common.enable_pspell) {
4677                 gtkpspell = gtkpspell_new((const gchar*)prefs_common.dictionary,
4678                                           conv_get_current_charset_str(),
4679                                           prefs_common.misspelled_col,
4680                                           prefs_common.check_while_typing,
4681                                           GTK_STEXT(text));
4682                 if (!gtkpspell) {
4683                         alertpanel_error(_("Spell checker could not be started.\n%s"), gtkpspellcheckers->error_message);
4684                         gtkpspell_checkers_reset_error();
4685                 } else {
4686
4687                         GtkWidget *menuitem;
4688
4689                         if (!gtkpspell_set_sug_mode(gtkpspell, prefs_common.pspell_sugmode)) {
4690                                 debug_print(_("Pspell: could not set suggestion mode %s"),
4691                                     gtkpspellcheckers->error_message);
4692                                 gtkpspell_checkers_reset_error();
4693                         }
4694
4695                         menuitem = gtk_item_factory_get_item(ifactory, "/Spelling/Spelling Configuration");
4696                         gtkpspell_populate_submenu(gtkpspell, menuitem);
4697                         menu_set_sensitive(ifactory, "/Spelling", TRUE);
4698                         }
4699         }
4700 #endif
4701
4702         compose_set_title(compose);
4703
4704 #if 0 /* NEW COMPOSE GUI */
4705         compose->use_bcc        = FALSE;
4706         compose->use_replyto    = FALSE;
4707         compose->use_followupto = FALSE;
4708 #endif
4709
4710 #if USE_PSPELL
4711         compose->gtkpspell      = gtkpspell;
4712 #endif
4713
4714 #if 0 /* NEW COMPOSE GUI */
4715         if (account->protocol != A_NNTP) {
4716                 menuitem = gtk_item_factory_get_item(ifactory, "/View/To");
4717                 gtk_check_menu_item_set_active
4718                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
4719                 gtk_widget_set_sensitive(menuitem, FALSE);
4720                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Cc");
4721                 gtk_check_menu_item_set_active
4722                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
4723                 gtk_widget_set_sensitive(menuitem, FALSE);
4724         }
4725 #endif
4726         if (account->set_autocc && account->auto_cc && mode != COMPOSE_REEDIT) {
4727                 compose_entry_append(compose, account->auto_cc, COMPOSE_CC);
4728 #if 0 /* NEW COMPOSE GUI */
4729                 compose->use_cc = TRUE;
4730                 gtk_entry_set_text(GTK_ENTRY(cc_entry), account->auto_cc);
4731                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Cc");
4732                 gtk_check_menu_item_set_active
4733                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
4734 #endif
4735         }
4736         if (account->set_autobcc) {
4737                 compose_entry_append(compose, account->auto_bcc, COMPOSE_BCC);
4738 #if 0 /* NEW COMPOSE GUI */
4739                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Bcc");
4740                 gtk_check_menu_item_set_active
4741                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
4742                 if (account->auto_bcc && mode != COMPOSE_REEDIT)
4743                         gtk_entry_set_text(GTK_ENTRY(bcc_entry),
4744                                            account->auto_bcc);
4745 #endif
4746         }
4747         if (account->set_autoreplyto && account->auto_replyto && mode != COMPOSE_REEDIT) {
4748                 compose_entry_append(compose, account->auto_replyto, COMPOSE_REPLYTO);
4749 #if 0 /* NEW COMPOSE GUI */
4750                 compose->use_replyto = TRUE;
4751                 menuitem = gtk_item_factory_get_item(ifactory,
4752                                                      "/View/Reply to");
4753                 gtk_check_menu_item_set_active
4754                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
4755                 gtk_entry_set_text(GTK_ENTRY(reply_entry),
4756                                    account->auto_replyto);
4757 #endif
4758         }
4759
4760         if (account->protocol != A_NNTP) {
4761                 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(compose->header_last->combo)->entry), prefs_common.trans_hdr ? _("To:") : "To:");
4762         } else {
4763                 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(compose->header_last->combo)->entry), prefs_common.trans_hdr ? _("Newsgroups:") : "Newsgroups:");
4764         }
4765
4766 #if 0 /* NEW COMPOSE GUI */
4767         menuitem = gtk_item_factory_get_item(ifactory, "/View/Ruler");
4768         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
4769                                        prefs_common.show_ruler);
4770 #endif                                 
4771
4772 #if USE_GPGME
4773         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Sign");
4774         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
4775                                        account->default_sign);
4776         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Encrypt");
4777         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
4778                                        account->default_encrypt);
4779 #endif /* USE_GPGME */
4780
4781         addressbook_set_target_compose(compose);
4782         compose_set_template_menu(compose);
4783
4784         compose_list = g_list_append(compose_list, compose);
4785
4786 #if 0 /* NEW COMPOSE GUI */
4787         compose->use_to         = FALSE;
4788         compose->use_cc         = FALSE;
4789         compose->use_attach     = TRUE;
4790 #endif
4791
4792 #if 0 /* NEW COMPOSE GUI */
4793         if (!compose->use_bcc) {
4794                 gtk_widget_hide(bcc_hbox);
4795                 gtk_widget_hide(bcc_entry);
4796                 gtk_table_set_row_spacing(GTK_TABLE(table), 4, 0);
4797         }
4798         if (!compose->use_replyto) {
4799                 gtk_widget_hide(reply_hbox);
4800                 gtk_widget_hide(reply_entry);
4801                 gtk_table_set_row_spacing(GTK_TABLE(table), 5, 0);
4802         }
4803         if (!compose->use_followupto) {
4804                 gtk_widget_hide(followup_hbox);
4805                 gtk_widget_hide(followup_entry);
4806                 gtk_table_set_row_spacing(GTK_TABLE(table), 6, 0);
4807         }
4808 #endif
4809         if (!prefs_common.show_ruler)
4810                 gtk_widget_hide(ruler_hbox);
4811
4812         select_account(compose, account);
4813
4814         return compose;
4815 }
4816
4817 static void compose_toolbar_create(Compose *compose, GtkWidget *container)
4818 {
4819         GtkWidget *toolbar;
4820         GtkWidget *icon_wid;
4821         GtkWidget *send_btn;
4822         GtkWidget *sendl_btn;
4823         GtkWidget *draft_btn;
4824         GtkWidget *insert_btn;
4825         GtkWidget *attach_btn;
4826         GtkWidget *sig_btn;
4827         GtkWidget *exteditor_btn;
4828         GtkWidget *linewrap_btn;
4829         GtkWidget *addrbook_btn;
4830
4831         toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
4832                                   GTK_TOOLBAR_BOTH);
4833         gtk_container_add(GTK_CONTAINER(container), toolbar);
4834         gtk_container_set_border_width(GTK_CONTAINER(container), 2);
4835         gtk_toolbar_set_button_relief(GTK_TOOLBAR(toolbar), GTK_RELIEF_NONE);
4836         gtk_toolbar_set_space_style(GTK_TOOLBAR(toolbar),
4837                                     GTK_TOOLBAR_SPACE_LINE);
4838
4839         icon_wid = stock_pixmap_widget(container, STOCK_PIXMAP_MAIL_SEND);
4840         send_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
4841                                            _("Send"),
4842                                            _("Send message"),
4843                                            "Send",
4844                                            icon_wid, toolbar_send_cb, compose);
4845
4846         icon_wid = stock_pixmap_widget(container, STOCK_PIXMAP_MAIL_SEND_QUEUE);
4847         sendl_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
4848                                            _("Send later"),
4849                                            _("Put into queue folder and send later"),
4850                                            "Send later",
4851                                            icon_wid, toolbar_send_later_cb,
4852                                            compose);
4853
4854         icon_wid = stock_pixmap_widget(container, STOCK_PIXMAP_MAIL);
4855         draft_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
4856                                             _("Draft"),
4857                                             _("Save to draft folder"),
4858                                             "Draft",
4859                                             icon_wid, toolbar_draft_cb,
4860                                             compose);
4861
4862         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
4863
4864         icon_wid = stock_pixmap_widget(container, STOCK_PIXMAP_PASTE);
4865         insert_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
4866                                              _("Insert"),
4867                                              _("Insert file"),
4868                                              "Insert",
4869                                              icon_wid, toolbar_insert_cb,
4870                                              compose);
4871
4872         icon_wid = stock_pixmap_widget(container, STOCK_PIXMAP_MAIL_ATTACH);
4873         attach_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
4874                                              _("Attach"),
4875                                              _("Attach file"),
4876                                              "Attach",
4877                                              icon_wid, toolbar_attach_cb,
4878                                              compose);
4879
4880         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
4881
4882         icon_wid = stock_pixmap_widget(container, STOCK_PIXMAP_MAIL);
4883         sig_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
4884                                           _("Signature"),
4885                                           _("Insert signature"),
4886                                           "Signature",
4887                                           icon_wid, toolbar_sig_cb, compose);
4888
4889         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
4890
4891         icon_wid = stock_pixmap_widget(container, STOCK_PIXMAP_MAIL_COMPOSE);
4892         exteditor_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
4893                                                 _("Editor"),
4894                                                 _("Edit with external editor"),
4895                                                 "Editor",
4896                                                 icon_wid,
4897                                                 toolbar_ext_editor_cb,
4898                                                 compose);
4899
4900         icon_wid = stock_pixmap_widget(container, STOCK_PIXMAP_LINEWRAP);
4901         linewrap_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
4902                                                _("Linewrap"),
4903                                                _("Wrap all long lines"),
4904                                                "Linewrap",
4905                                                icon_wid,
4906                                                toolbar_linewrap_cb,
4907                                                compose);
4908
4909         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
4910
4911         icon_wid = stock_pixmap_widget(container, STOCK_PIXMAP_ADDRESS_BOOK);
4912         addrbook_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
4913                                                _("Address"),
4914                                                _("Address book"),
4915                                                "Address",
4916                                                icon_wid, toolbar_address_cb,
4917                                                compose);
4918
4919         compose->toolbar       = toolbar;
4920         compose->send_btn      = send_btn;
4921         compose->sendl_btn     = sendl_btn;
4922         compose->draft_btn     = draft_btn;
4923         compose->insert_btn    = insert_btn;
4924         compose->attach_btn    = attach_btn;
4925         compose->sig_btn       = sig_btn;
4926         compose->exteditor_btn = exteditor_btn;
4927         compose->linewrap_btn  = linewrap_btn;
4928         compose->addrbook_btn  = addrbook_btn;
4929
4930         gtk_widget_show_all(toolbar);
4931 }
4932
4933 static GtkWidget *compose_account_option_menu_create(Compose *compose)
4934 {
4935         GList *accounts;
4936         GtkWidget *hbox;
4937         GtkWidget *optmenu;
4938         GtkWidget *menu;
4939         gint num = 0, def_menu = 0;
4940
4941         accounts = account_get_list();
4942         g_return_val_if_fail(accounts != NULL, NULL);
4943
4944         hbox = gtk_hbox_new(FALSE, 0);
4945         optmenu = gtk_option_menu_new();
4946         gtk_box_pack_start(GTK_BOX(hbox), optmenu, FALSE, FALSE, 0);
4947         menu = gtk_menu_new();
4948
4949         for (; accounts != NULL; accounts = accounts->next, num++) {
4950                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
4951                 GtkWidget *menuitem;
4952                 gchar *name;
4953
4954                 if (ac == compose->account) def_menu = num;
4955
4956                 name = g_strdup_printf("%s: %s <%s>",
4957                                        ac->account_name, ac->name, ac->address);
4958                 MENUITEM_ADD(menu, menuitem, name, ac);
4959                 g_free(name);
4960                 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
4961                                    GTK_SIGNAL_FUNC(account_activated),
4962                                    compose);
4963         }
4964
4965         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), menu);
4966         gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), def_menu);
4967
4968         return hbox;
4969 }
4970
4971 static void compose_set_template_menu(Compose *compose)
4972 {
4973         GSList *tmpl_list, *cur;
4974         GtkWidget *menu;
4975         GtkWidget *item;
4976
4977         tmpl_list = template_get_config();
4978
4979         menu = gtk_menu_new();
4980
4981         for (cur = tmpl_list; cur != NULL; cur = cur->next) {
4982                 Template *tmpl = (Template *)cur->data;
4983
4984                 item = gtk_menu_item_new_with_label(tmpl->name);
4985                 gtk_menu_append(GTK_MENU(menu), item);
4986                 gtk_signal_connect(GTK_OBJECT(item), "activate",
4987                                    GTK_SIGNAL_FUNC(compose_template_activate_cb),
4988                                    compose);
4989                 gtk_object_set_data(GTK_OBJECT(item), "template", tmpl);
4990                 gtk_widget_show(item);
4991         }
4992
4993         gtk_widget_show(menu);
4994         gtk_menu_item_set_submenu(GTK_MENU_ITEM(compose->tmpl_menu), menu);
4995 }
4996
4997 void compose_reflect_prefs_all(void)
4998 {
4999         GList *cur;
5000         Compose *compose;
5001
5002         for (cur = compose_list; cur != NULL; cur = cur->next) {
5003                 compose = (Compose *)cur->data;
5004                 compose_set_template_menu(compose);
5005         }
5006 }
5007
5008 static void compose_template_apply(Compose *compose, Template *tmpl)
5009 {
5010         gchar *qmark;
5011         gchar *parsed_str;
5012
5013         if (!tmpl || !tmpl->value) return;
5014
5015         gtk_stext_freeze(GTK_STEXT(compose->text));
5016
5017         if (tmpl->subject && *tmpl->subject != '\0')
5018                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry),
5019                                    tmpl->subject);
5020         if (tmpl->to && *tmpl->to != '\0')
5021                 compose_entry_append(compose, tmpl->to, COMPOSE_TO);
5022
5023         gtk_stext_clear(GTK_STEXT(compose->text));
5024
5025         if (compose->replyinfo == NULL) {
5026                 MsgInfo dummyinfo;
5027
5028                 memset(&dummyinfo, 0, sizeof(MsgInfo));
5029                 parsed_str = compose_quote_fmt(compose, &dummyinfo,
5030                                                tmpl->value, NULL);
5031         } else {
5032                 if (prefs_common.quotemark && *prefs_common.quotemark)
5033                         qmark = prefs_common.quotemark;
5034                 else
5035                         qmark = "> ";
5036
5037                 parsed_str = compose_quote_fmt(compose, compose->replyinfo,
5038                                                tmpl->value, qmark);
5039         }
5040
5041         if (parsed_str && prefs_common.auto_sig)
5042                 compose_insert_sig(compose);
5043
5044         gtk_stext_thaw(GTK_STEXT(compose->text));
5045 }
5046
5047 static void compose_destroy(Compose *compose)
5048 {
5049         gint row;
5050         GtkCList *clist = GTK_CLIST(compose->attach_clist);
5051         AttachInfo *ainfo;
5052
5053         /* NOTE: address_completion_end() does nothing with the window
5054          * however this may change. */
5055         address_completion_end(compose->window);
5056
5057         slist_free_strings(compose->to_list);
5058         g_slist_free(compose->to_list);
5059         slist_free_strings(compose->newsgroup_list);
5060         g_slist_free(compose->newsgroup_list);
5061         slist_free_strings(compose->header_list);
5062         g_slist_free(compose->header_list);
5063
5064         procmsg_msginfo_free(compose->targetinfo);
5065         procmsg_msginfo_free(compose->replyinfo);
5066
5067         g_free(compose->replyto);
5068         g_free(compose->cc);
5069         g_free(compose->bcc);
5070         g_free(compose->newsgroups);
5071         g_free(compose->followup_to);
5072
5073         g_free(compose->inreplyto);
5074         g_free(compose->references);
5075         g_free(compose->msgid);
5076         g_free(compose->boundary);
5077
5078         if (compose->bounce_filename)
5079                 g_free(compose->bounce_filename);
5080
5081         g_free(compose->exteditor_file);
5082
5083         for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
5084              row++)
5085                 compose_attach_info_free(ainfo);
5086
5087         if (addressbook_get_target_compose() == compose)
5088                 addressbook_set_target_compose(NULL);
5089
5090 #if USE_PSPELL
5091         if (compose->gtkpspell) {
5092                 gtkpspell_delete(compose->gtkpspell);
5093         }
5094 #endif
5095
5096         prefs_common.compose_width = compose->scrolledwin->allocation.width;
5097         prefs_common.compose_height = compose->window->allocation.height;
5098
5099         gtk_widget_destroy(compose->paned);
5100
5101         g_free(compose);
5102
5103         compose_list = g_list_remove(compose_list, compose);
5104 }
5105
5106 static void compose_attach_info_free(AttachInfo *ainfo)
5107 {
5108         g_free(ainfo->file);
5109         g_free(ainfo->content_type);
5110         g_free(ainfo->name);
5111         g_free(ainfo);
5112 }
5113
5114 static void compose_attach_remove_selected(Compose *compose)
5115 {
5116         GtkCList *clist = GTK_CLIST(compose->attach_clist);
5117         AttachInfo *ainfo;
5118         gint row;
5119
5120         while (clist->selection != NULL) {
5121                 row = GPOINTER_TO_INT(clist->selection->data);
5122                 ainfo = gtk_clist_get_row_data(clist, row);
5123                 compose_attach_info_free(ainfo);
5124                 gtk_clist_remove(clist, row);
5125         }
5126 }
5127
5128 static struct _AttachProperty
5129 {
5130         GtkWidget *window;
5131         GtkWidget *mimetype_entry;
5132         GtkWidget *encoding_optmenu;
5133         GtkWidget *path_entry;
5134         GtkWidget *filename_entry;
5135         GtkWidget *ok_btn;
5136         GtkWidget *cancel_btn;
5137 } attach_prop;
5138
5139 static void compose_attach_property(Compose *compose)
5140 {
5141         GtkCList *clist = GTK_CLIST(compose->attach_clist);
5142         AttachInfo *ainfo;
5143         gint row;
5144         GtkOptionMenu *optmenu;
5145         static gboolean cancelled;
5146
5147         if (!clist->selection) return;
5148         row = GPOINTER_TO_INT(clist->selection->data);
5149
5150         ainfo = gtk_clist_get_row_data(clist, row);
5151         if (!ainfo) return;
5152
5153         if (!attach_prop.window)
5154                 compose_attach_property_create(&cancelled);
5155         gtk_widget_grab_focus(attach_prop.ok_btn);
5156         gtk_widget_show(attach_prop.window);
5157         manage_window_set_transient(GTK_WINDOW(attach_prop.window));
5158
5159         optmenu = GTK_OPTION_MENU(attach_prop.encoding_optmenu);
5160         if (ainfo->encoding == ENC_UNKNOWN)
5161                 gtk_option_menu_set_history(optmenu, ENC_BASE64);
5162         else
5163                 gtk_option_menu_set_history(optmenu, ainfo->encoding);
5164
5165         gtk_entry_set_text(GTK_ENTRY(attach_prop.mimetype_entry),
5166                            ainfo->content_type ? ainfo->content_type : "");
5167         gtk_entry_set_text(GTK_ENTRY(attach_prop.path_entry),
5168                            ainfo->file ? ainfo->file : "");
5169         gtk_entry_set_text(GTK_ENTRY(attach_prop.filename_entry),
5170                            ainfo->name ? ainfo->name : "");
5171
5172         for (;;) {
5173                 gchar *text;
5174                 gchar *cnttype = NULL;
5175                 gchar *file = NULL;
5176                 off_t size = 0;
5177                 GtkWidget *menu;
5178                 GtkWidget *menuitem;
5179
5180                 cancelled = FALSE;
5181                 gtk_main();
5182
5183                 if (cancelled == TRUE) {
5184                         gtk_widget_hide(attach_prop.window);
5185                         break;
5186                 }
5187
5188                 text = gtk_entry_get_text(GTK_ENTRY(attach_prop.mimetype_entry));
5189                 if (*text != '\0') {
5190                         gchar *p;
5191
5192                         text = g_strstrip(g_strdup(text));
5193                         if ((p = strchr(text, '/')) && !strchr(p + 1, '/')) {
5194                                 cnttype = g_strdup(text);
5195                                 g_free(text);
5196                         } else {
5197                                 alertpanel_error(_("Invalid MIME type."));
5198                                 g_free(text);
5199                                 continue;
5200                         }
5201                 }
5202
5203                 menu = gtk_option_menu_get_menu(optmenu);
5204                 menuitem = gtk_menu_get_active(GTK_MENU(menu));
5205                 ainfo->encoding = GPOINTER_TO_INT
5206                         (gtk_object_get_user_data(GTK_OBJECT(menuitem)));
5207
5208                 text = gtk_entry_get_text(GTK_ENTRY(attach_prop.path_entry));
5209                 if (*text != '\0') {
5210                         if (is_file_exist(text) &&
5211                             (size = get_file_size(text)) > 0)
5212                                 file = g_strdup(text);
5213                         else {
5214                                 alertpanel_error
5215                                         (_("File doesn't exist or is empty."));
5216                                 g_free(cnttype);
5217                                 continue;
5218                         }
5219                 }
5220
5221                 text = gtk_entry_get_text(GTK_ENTRY(attach_prop.filename_entry));
5222                 if (*text != '\0') {
5223                         g_free(ainfo->name);
5224                         ainfo->name = g_strdup(text);
5225                 }
5226
5227                 if (cnttype) {
5228                         g_free(ainfo->content_type);
5229                         ainfo->content_type = cnttype;
5230                 }
5231                 if (file) {
5232                         g_free(ainfo->file);
5233                         ainfo->file = file;
5234                 }
5235                 if (size)
5236                         ainfo->size = size;
5237
5238                 gtk_clist_set_text(clist, row, COL_MIMETYPE,
5239                                    ainfo->content_type);
5240                 gtk_clist_set_text(clist, row, COL_SIZE,
5241                                    to_human_readable(ainfo->size));
5242                 gtk_clist_set_text(clist, row, COL_NAME, ainfo->name);
5243
5244                 gtk_widget_hide(attach_prop.window);
5245                 break;
5246         }
5247 }
5248
5249 #define SET_LABEL_AND_ENTRY(str, entry, top) \
5250 { \
5251         label = gtk_label_new(str); \
5252         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), \
5253                          GTK_FILL, 0, 0, 0); \
5254         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); \
5255  \
5256         entry = gtk_entry_new(); \
5257         gtk_table_attach(GTK_TABLE(table), entry, 1, 2, top, (top + 1), \
5258                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); \
5259 }
5260
5261 static void compose_attach_property_create(gboolean *cancelled)
5262 {
5263         GtkWidget *window;
5264         GtkWidget *vbox;
5265         GtkWidget *table;
5266         GtkWidget *label;
5267         GtkWidget *mimetype_entry;
5268         GtkWidget *hbox;
5269         GtkWidget *optmenu;
5270         GtkWidget *optmenu_menu;
5271         GtkWidget *menuitem;
5272         GtkWidget *path_entry;
5273         GtkWidget *filename_entry;
5274         GtkWidget *hbbox;
5275         GtkWidget *ok_btn;
5276         GtkWidget *cancel_btn;
5277         GList     *mime_type_list, *strlist;
5278
5279         debug_print("Creating attach_property window...\n");
5280
5281         window = gtk_window_new(GTK_WINDOW_DIALOG);
5282         gtk_widget_set_usize(window, 480, -1);
5283         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
5284         gtk_window_set_title(GTK_WINDOW(window), _("Property"));
5285         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
5286         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
5287         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
5288                            GTK_SIGNAL_FUNC(attach_property_delete_event),
5289                            cancelled);
5290         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
5291                            GTK_SIGNAL_FUNC(attach_property_key_pressed),
5292                            cancelled);
5293
5294         vbox = gtk_vbox_new(FALSE, 8);
5295         gtk_container_add(GTK_CONTAINER(window), vbox);
5296
5297         table = gtk_table_new(4, 2, FALSE);
5298         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
5299         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
5300         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
5301
5302         label = gtk_label_new(_("MIME type")); 
5303         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, (0 + 1), 
5304                          GTK_FILL, 0, 0, 0); 
5305         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); 
5306         mimetype_entry = gtk_combo_new(); 
5307         gtk_table_attach(GTK_TABLE(table), mimetype_entry, 1, 2, 0, (0 + 1), 
5308                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
5309                          
5310         /* stuff with list */
5311         mime_type_list = procmime_get_mime_type_list();
5312         strlist = NULL;
5313         for (; mime_type_list != NULL; mime_type_list = mime_type_list->next) {
5314                 MimeType *type = (MimeType *) mime_type_list->data;
5315                 strlist = g_list_append(strlist, 
5316                                 g_strdup_printf("%s/%s",
5317                                         type->type, type->sub_type));
5318         }
5319         
5320         gtk_combo_set_popdown_strings(GTK_COMBO(mimetype_entry), strlist);
5321
5322         for (mime_type_list = strlist; mime_type_list != NULL; 
5323                 mime_type_list = mime_type_list->next)
5324                 g_free(mime_type_list->data);
5325         g_list_free(strlist);
5326                          
5327         mimetype_entry = GTK_COMBO(mimetype_entry)->entry;                       
5328
5329         label = gtk_label_new(_("Encoding"));
5330         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
5331                          GTK_FILL, 0, 0, 0);
5332         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
5333
5334         hbox = gtk_hbox_new(FALSE, 0);
5335         gtk_table_attach(GTK_TABLE(table), hbox, 1, 2, 1, 2,
5336                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
5337
5338         optmenu = gtk_option_menu_new();
5339         gtk_box_pack_start(GTK_BOX(hbox), optmenu, FALSE, FALSE, 0);
5340
5341         optmenu_menu = gtk_menu_new();
5342         MENUITEM_ADD(optmenu_menu, menuitem, "7bit", ENC_7BIT);
5343         gtk_widget_set_sensitive(menuitem, FALSE);
5344         MENUITEM_ADD(optmenu_menu, menuitem, "8bit", ENC_8BIT);
5345         gtk_widget_set_sensitive(menuitem, FALSE);
5346         MENUITEM_ADD(optmenu_menu, menuitem, "quoted-printable", ENC_QUOTED_PRINTABLE);
5347         gtk_widget_set_sensitive(menuitem, FALSE);
5348         MENUITEM_ADD(optmenu_menu, menuitem, "base64", ENC_BASE64);
5349
5350         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), optmenu_menu);
5351
5352         SET_LABEL_AND_ENTRY(_("Path"),      path_entry,     2);
5353         SET_LABEL_AND_ENTRY(_("File name"), filename_entry, 3);
5354
5355         gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
5356                                 &cancel_btn, _("Cancel"), NULL, NULL);
5357         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
5358         gtk_widget_grab_default(ok_btn);
5359
5360         gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
5361                            GTK_SIGNAL_FUNC(attach_property_ok),
5362                            cancelled);
5363         gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
5364                            GTK_SIGNAL_FUNC(attach_property_cancel),
5365                            cancelled);
5366
5367         gtk_widget_show_all(vbox);
5368
5369         attach_prop.window           = window;
5370         attach_prop.mimetype_entry   = mimetype_entry;
5371         attach_prop.encoding_optmenu = optmenu;
5372         attach_prop.path_entry       = path_entry;
5373         attach_prop.filename_entry   = filename_entry;
5374         attach_prop.ok_btn           = ok_btn;
5375         attach_prop.cancel_btn       = cancel_btn;
5376 }
5377
5378 #undef SET_LABEL_AND_ENTRY
5379
5380 static void attach_property_ok(GtkWidget *widget, gboolean *cancelled)
5381 {
5382         *cancelled = FALSE;
5383         gtk_main_quit();
5384 }
5385
5386 static void attach_property_cancel(GtkWidget *widget, gboolean *cancelled)
5387 {
5388         *cancelled = TRUE;
5389         gtk_main_quit();
5390 }
5391
5392 static gint attach_property_delete_event(GtkWidget *widget, GdkEventAny *event,
5393                                          gboolean *cancelled)
5394 {
5395         *cancelled = TRUE;
5396         gtk_main_quit();
5397
5398         return TRUE;
5399 }
5400
5401 static void attach_property_key_pressed(GtkWidget *widget, GdkEventKey *event,
5402                                         gboolean *cancelled)
5403 {
5404         if (event && event->keyval == GDK_Escape) {
5405                 *cancelled = TRUE;
5406                 gtk_main_quit();
5407         }
5408 }
5409
5410 static void compose_exec_ext_editor(Compose *compose)
5411 {
5412         gchar tmp[64];
5413         pid_t pid;
5414         gint pipe_fds[2];
5415
5416         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.%08x",
5417                    g_get_tmp_dir(), G_DIR_SEPARATOR, (gint)compose);
5418
5419         if (pipe(pipe_fds) < 0) {
5420                 perror("pipe");
5421                 return;
5422         }
5423
5424         if ((pid = fork()) < 0) {
5425                 perror("fork");
5426                 return;
5427         }
5428
5429         if (pid != 0) {
5430                 /* close the write side of the pipe */
5431                 close(pipe_fds[1]);
5432
5433                 compose->exteditor_file    = g_strdup(tmp);
5434                 compose->exteditor_pid     = pid;
5435                 compose->exteditor_readdes = pipe_fds[0];
5436
5437                 compose_set_ext_editor_sensitive(compose, FALSE);
5438
5439                 compose->exteditor_tag =
5440                         gdk_input_add(pipe_fds[0], GDK_INPUT_READ,
5441                                       compose_input_cb, compose);
5442         } else {        /* process-monitoring process */
5443                 pid_t pid_ed;
5444
5445                 if (setpgid(0, 0))
5446                         perror("setpgid");
5447
5448                 /* close the read side of the pipe */
5449                 close(pipe_fds[0]);
5450
5451                 if (compose_write_body_to_file(compose, tmp) < 0) {
5452                         fd_write(pipe_fds[1], "2\n", 2);
5453                         _exit(1);
5454                 }
5455
5456                 pid_ed = compose_exec_ext_editor_real(tmp);
5457                 if (pid_ed < 0) {
5458                         fd_write(pipe_fds[1], "1\n", 2);
5459                         _exit(1);
5460                 }
5461
5462                 /* wait until editor is terminated */
5463                 waitpid(pid_ed, NULL, 0);
5464
5465                 fd_write(pipe_fds[1], "0\n", 2);
5466
5467                 close(pipe_fds[1]);
5468                 _exit(0);
5469         }
5470 }
5471
5472 static gint compose_exec_ext_editor_real(const gchar *file)
5473 {
5474         static gchar *def_cmd = "emacs %s";
5475         gchar buf[1024];
5476         gchar *p;
5477         gchar **cmdline;
5478         pid_t pid;
5479
5480         g_return_val_if_fail(file != NULL, -1);
5481
5482         if ((pid = fork()) < 0) {
5483                 perror("fork");
5484                 return -1;
5485         }
5486
5487         if (pid != 0) return pid;
5488
5489         /* grandchild process */
5490
5491         if (setpgid(0, getppid()))
5492                 perror("setpgid");
5493
5494         if (prefs_common.ext_editor_cmd &&
5495             (p = strchr(prefs_common.ext_editor_cmd, '%')) &&
5496             *(p + 1) == 's' && !strchr(p + 2, '%')) {
5497                 g_snprintf(buf, sizeof(buf), prefs_common.ext_editor_cmd, file);
5498         } else {
5499                 if (prefs_common.ext_editor_cmd)
5500                         g_warning(_("External editor command line is invalid: `%s'\n"),
5501                                   prefs_common.ext_editor_cmd);
5502                 g_snprintf(buf, sizeof(buf), def_cmd, file);
5503         }
5504
5505         cmdline = strsplit_with_quote(buf, " ", 1024);
5506         execvp(cmdline[0], cmdline);
5507
5508         perror("execvp");
5509         g_strfreev(cmdline);
5510
5511         _exit(1);
5512 }
5513
5514 static gboolean compose_ext_editor_kill(Compose *compose)
5515 {
5516         pid_t pgid = compose->exteditor_pid * -1;
5517         gint ret;
5518
5519         ret = kill(pgid, 0);
5520
5521         if (ret == 0 || (ret == -1 && EPERM == errno)) {
5522                 AlertValue val;
5523                 gchar *msg;
5524
5525                 msg = g_strdup_printf
5526                         (_("The external editor is still working.\n"
5527                            "Force terminating the process?\n"
5528                            "process group id: %d"), -pgid);
5529                 val = alertpanel(_("Notice"), msg, _("Yes"), _("+No"), NULL);
5530                 g_free(msg);
5531
5532                 if (val == G_ALERTDEFAULT) {
5533                         gdk_input_remove(compose->exteditor_tag);
5534                         close(compose->exteditor_readdes);
5535
5536                         if (kill(pgid, SIGTERM) < 0) perror("kill");
5537                         waitpid(compose->exteditor_pid, NULL, 0);
5538
5539                         g_warning(_("Terminated process group id: %d"), -pgid);
5540                         g_warning(_("Temporary file: %s"),
5541                                   compose->exteditor_file);
5542
5543                         compose_set_ext_editor_sensitive(compose, TRUE);
5544
5545                         g_free(compose->exteditor_file);
5546                         compose->exteditor_file    = NULL;
5547                         compose->exteditor_pid     = -1;
5548                         compose->exteditor_readdes = -1;
5549                         compose->exteditor_tag     = -1;
5550                 } else
5551                         return FALSE;
5552         }
5553
5554         return TRUE;
5555 }
5556
5557 static void compose_input_cb(gpointer data, gint source,
5558                              GdkInputCondition condition)
5559 {
5560         gchar buf[3];
5561         Compose *compose = (Compose *)data;
5562         gint i = 0;
5563
5564         debug_print(_("Compose: input from monitoring process\n"));
5565
5566         gdk_input_remove(compose->exteditor_tag);
5567
5568         for (;;) {
5569                 if (read(source, &buf[i], 1) < 1) {
5570                         buf[0] = '3';
5571                         break;
5572                 }
5573                 if (buf[i] == '\n') {
5574                         buf[i] = '\0';
5575                         break;
5576                 }
5577                 i++;
5578                 if (i == sizeof(buf) - 1)
5579                         break;
5580         }
5581
5582         waitpid(compose->exteditor_pid, NULL, 0);
5583
5584         if (buf[0] == '0') {            /* success */
5585                 GtkSText *text = GTK_STEXT(compose->text);
5586
5587                 gtk_stext_freeze(text);
5588                 gtk_stext_set_point(text, 0);
5589                 gtk_stext_forward_delete(text, gtk_stext_get_length(text));
5590                 compose_insert_file(compose, compose->exteditor_file);
5591                 compose_changed_cb(NULL, compose);
5592                 gtk_stext_thaw(text);
5593
5594                 if (unlink(compose->exteditor_file) < 0)
5595                         FILE_OP_ERROR(compose->exteditor_file, "unlink");
5596         } else if (buf[0] == '1') {     /* failed */
5597                 g_warning(_("Couldn't exec external editor\n"));
5598                 if (unlink(compose->exteditor_file) < 0)
5599                         FILE_OP_ERROR(compose->exteditor_file, "unlink");
5600         } else if (buf[0] == '2') {
5601                 g_warning(_("Couldn't write to file\n"));
5602         } else if (buf[0] == '3') {
5603                 g_warning(_("Pipe read failed\n"));
5604         }
5605
5606         close(source);
5607
5608         compose_set_ext_editor_sensitive(compose, TRUE);
5609
5610         g_free(compose->exteditor_file);
5611         compose->exteditor_file    = NULL;
5612         compose->exteditor_pid     = -1;
5613         compose->exteditor_readdes = -1;
5614         compose->exteditor_tag     = -1;
5615 }
5616
5617 static void compose_set_ext_editor_sensitive(Compose *compose,
5618                                              gboolean sensitive)
5619 {
5620         GtkItemFactory *ifactory;
5621
5622         ifactory = gtk_item_factory_from_widget(compose->menubar);
5623
5624         menu_set_sensitive(ifactory, "/Message/Send", sensitive);
5625         menu_set_sensitive(ifactory, "/Message/Send later", sensitive);
5626         menu_set_sensitive(ifactory, "/Message/Save to draft folder",
5627                            sensitive);
5628         menu_set_sensitive(ifactory, "/File/Insert file", sensitive);
5629         menu_set_sensitive(ifactory, "/File/Insert signature", sensitive);
5630         menu_set_sensitive(ifactory, "/Edit/Wrap current paragraph", sensitive);
5631         menu_set_sensitive(ifactory, "/Edit/Wrap all long lines", sensitive);
5632         menu_set_sensitive(ifactory, "/Edit/Edit with external editor",
5633                            sensitive);
5634
5635         gtk_widget_set_sensitive(compose->text,          sensitive);
5636         gtk_widget_set_sensitive(compose->send_btn,      sensitive);
5637         gtk_widget_set_sensitive(compose->sendl_btn,     sensitive);
5638         gtk_widget_set_sensitive(compose->draft_btn,     sensitive);
5639         gtk_widget_set_sensitive(compose->insert_btn,    sensitive);
5640         gtk_widget_set_sensitive(compose->sig_btn,       sensitive);
5641         gtk_widget_set_sensitive(compose->exteditor_btn, sensitive);
5642         gtk_widget_set_sensitive(compose->linewrap_btn,  sensitive);
5643 }
5644
5645 /**
5646  * compose_undo_state_changed:
5647  *
5648  * Change the sensivity of the menuentries undo and redo
5649  **/
5650 static void compose_undo_state_changed(UndoMain *undostruct, gint undo_state,
5651                                        gint redo_state, gpointer data)
5652 {
5653         GtkWidget *widget = GTK_WIDGET(data);
5654         GtkItemFactory *ifactory;
5655
5656         g_return_if_fail(widget != NULL);
5657
5658         debug_print("Set_undo.  UNDO:%i  REDO:%i\n", undo_state, redo_state);
5659
5660         ifactory = gtk_item_factory_from_widget(widget);
5661
5662         switch (undo_state) {
5663         case UNDO_STATE_TRUE:
5664                 if (!undostruct->undo_state) {
5665                         debug_print ("Set_undo - Testpoint\n");
5666                         undostruct->undo_state = TRUE;
5667                         menu_set_sensitive(ifactory, "/Edit/Undo", TRUE);
5668                 }
5669                 break;
5670         case UNDO_STATE_FALSE:
5671                 if (undostruct->undo_state) {
5672                         undostruct->undo_state = FALSE;
5673                         menu_set_sensitive(ifactory, "/Edit/Undo", FALSE);
5674                 }
5675                 break;
5676         case UNDO_STATE_UNCHANGED:
5677                 break;
5678         case UNDO_STATE_REFRESH:
5679                 menu_set_sensitive(ifactory, "/Edit/Undo",
5680                                    undostruct->undo_state);
5681                 break;
5682         default:
5683                 g_warning("Undo state not recognized");
5684                 break;
5685         }
5686
5687         switch (redo_state) {
5688         case UNDO_STATE_TRUE:
5689                 if (!undostruct->redo_state) {
5690                         undostruct->redo_state = TRUE;
5691                         menu_set_sensitive(ifactory, "/Edit/Redo", TRUE);
5692                 }
5693                 break;
5694         case UNDO_STATE_FALSE:
5695                 if (undostruct->redo_state) {
5696                         undostruct->redo_state = FALSE;
5697                         menu_set_sensitive(ifactory, "/Edit/Redo", FALSE);
5698                 }
5699                 break;
5700         case UNDO_STATE_UNCHANGED:
5701                 break;
5702         case UNDO_STATE_REFRESH:
5703                 menu_set_sensitive(ifactory, "/Edit/Redo",
5704                                    undostruct->redo_state);
5705                 break;
5706         default:
5707                 g_warning("Redo state not recognized");
5708                 break;
5709         }
5710 }
5711
5712 static gint calc_cursor_xpos(GtkSText *text, gint extra, gint char_width)
5713 {
5714         gint cursor_pos;
5715
5716         cursor_pos = (text->cursor_pos_x - extra) / char_width;
5717         cursor_pos = MAX(cursor_pos, 0);
5718
5719         return cursor_pos;
5720 }
5721
5722 /* callback functions */
5723
5724 /* compose_edit_size_alloc() - called when resized. don't know whether Gtk
5725  * includes "non-client" (windows-izm) in calculation, so this calculation
5726  * may not be accurate.
5727  */
5728 static gboolean compose_edit_size_alloc(GtkEditable *widget,
5729                                         GtkAllocation *allocation,
5730                                         GtkSHRuler *shruler)
5731 {
5732         if (prefs_common.show_ruler) {
5733                 gint char_width;
5734                 gint line_width_in_chars;
5735
5736                 char_width = gtkut_get_font_width
5737                         (GTK_WIDGET(widget)->style->font);
5738                 line_width_in_chars =
5739                         (allocation->width - allocation->x) / char_width;
5740
5741                 /* got the maximum */
5742                 gtk_ruler_set_range(GTK_RULER(shruler),
5743                                     0.0, line_width_in_chars,
5744                                     calc_cursor_xpos(GTK_STEXT(widget),
5745                                                      allocation->x,
5746                                                      char_width),
5747                                     /*line_width_in_chars*/ char_width);
5748         }
5749
5750         return TRUE;
5751 }
5752
5753 static void toolbar_send_cb(GtkWidget *widget, gpointer data)
5754 {
5755         compose_send_cb(data, 0, NULL);
5756 }
5757
5758 static void toolbar_send_later_cb(GtkWidget *widget, gpointer data)
5759 {
5760         compose_send_later_cb(data, 0, NULL);
5761 }
5762
5763 static void toolbar_draft_cb(GtkWidget *widget, gpointer data)
5764 {
5765         compose_draft_cb(data, 0, NULL);
5766 }
5767
5768 static void toolbar_insert_cb(GtkWidget *widget, gpointer data)
5769 {
5770         compose_insert_file_cb(data, 0, NULL);
5771 }
5772
5773 static void toolbar_attach_cb(GtkWidget *widget, gpointer data)
5774 {
5775         compose_attach_cb(data, 0, NULL);
5776 }
5777
5778 static void toolbar_sig_cb(GtkWidget *widget, gpointer data)
5779 {
5780         Compose *compose = (Compose *)data;
5781
5782         compose_insert_sig(compose);
5783 }
5784
5785 static void toolbar_ext_editor_cb(GtkWidget *widget, gpointer data)
5786 {
5787         Compose *compose = (Compose *)data;
5788
5789         compose_exec_ext_editor(compose);
5790 }
5791
5792 static void toolbar_linewrap_cb(GtkWidget *widget, gpointer data)
5793 {
5794         Compose *compose = (Compose *)data;
5795
5796         compose_wrap_line(compose);
5797 }
5798
5799 static void toolbar_address_cb(GtkWidget *widget, gpointer data)
5800 {
5801         compose_address_cb(data, 0, NULL);
5802 }
5803
5804 static void select_account(Compose * compose, PrefsAccount * ac)
5805 {
5806         compose->account = ac;
5807         compose_set_title(compose);
5808
5809 #if 0 /* NEW COMPOSE GUI */
5810                 if (ac->protocol == A_NNTP) {
5811                         GtkItemFactory *ifactory;
5812                         GtkWidget *menuitem;
5813
5814                         ifactory = gtk_item_factory_from_widget(compose->menubar);
5815                         menu_set_sensitive(ifactory,
5816                                            "/Message/Followup to", TRUE);
5817                         gtk_widget_show(compose->newsgroups_hbox);
5818                         gtk_widget_show(compose->newsgroups_entry);
5819                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
5820                                                   1, 4);
5821
5822                         compose->use_to = FALSE;
5823                         compose->use_cc = FALSE;
5824
5825                         menuitem = gtk_item_factory_get_item(ifactory, "/Message/To");
5826                         gtk_check_menu_item_set_active
5827                                 (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
5828
5829                         menu_set_sensitive(ifactory,
5830                                            "/Message/To", TRUE);
5831                         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Cc");
5832                         gtk_check_menu_item_set_active
5833                                 (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
5834
5835                         gtk_widget_hide(compose->to_hbox);
5836                         gtk_widget_hide(compose->to_entry);
5837                         gtk_widget_hide(compose->cc_hbox);
5838                         gtk_widget_hide(compose->cc_entry);
5839                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
5840                                                   0, 0);
5841                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
5842                                                   3, 0);
5843                 }
5844                 else {
5845                         GtkItemFactory *ifactory;
5846                         GtkWidget *menuitem;
5847
5848                         ifactory = gtk_item_factory_from_widget(compose->menubar);
5849                         menu_set_sensitive(ifactory,
5850                                            "/Message/Followup to", FALSE);
5851                         gtk_entry_set_text(GTK_ENTRY(compose->newsgroups_entry), "");
5852                         gtk_widget_hide(compose->newsgroups_hbox);
5853                         gtk_widget_hide(compose->newsgroups_entry);
5854                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
5855                                                   1, 0);
5856
5857                         compose->use_to = TRUE;
5858                         compose->use_cc = TRUE;
5859
5860                         menuitem = gtk_item_factory_get_item(ifactory, "/Message/To");
5861                         gtk_check_menu_item_set_active
5862                                 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
5863                         menu_set_sensitive(ifactory,
5864                                            "/Message/To", FALSE);
5865                         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Cc");
5866                         gtk_check_menu_item_set_active
5867                                 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
5868                         gtk_widget_show(compose->to_hbox);
5869                         gtk_widget_show(compose->to_entry);
5870                         gtk_widget_show(compose->cc_hbox);
5871                         gtk_widget_show(compose->cc_entry);
5872
5873                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
5874                                                   0, 4);
5875                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
5876                                                   3, 4);
5877                 }
5878                 gtk_widget_queue_resize(compose->table_vbox);
5879 #endif
5880 }
5881
5882 static void account_activated(GtkMenuItem *menuitem, gpointer data)
5883 {
5884         Compose *compose = (Compose *)data;
5885
5886         PrefsAccount *ac;
5887
5888         ac = (PrefsAccount *)gtk_object_get_user_data(GTK_OBJECT(menuitem));
5889         g_return_if_fail(ac != NULL);
5890
5891         if (ac != compose->account)
5892                 select_account(compose, ac);
5893 }
5894
5895 static void attach_selected(GtkCList *clist, gint row, gint column,
5896                             GdkEvent *event, gpointer data)
5897 {
5898         Compose *compose = (Compose *)data;
5899
5900         if (event && event->type == GDK_2BUTTON_PRESS)
5901                 compose_attach_property(compose);
5902 }
5903
5904 static void attach_button_pressed(GtkWidget *widget, GdkEventButton *event,
5905                                   gpointer data)
5906 {
5907         Compose *compose = (Compose *)data;
5908         GtkCList *clist = GTK_CLIST(compose->attach_clist);
5909         gint row, column;
5910
5911         if (!event) return;
5912
5913         if (event->button == 3) {
5914                 if ((clist->selection && !clist->selection->next) ||
5915                     !clist->selection) {
5916                         gtk_clist_unselect_all(clist);
5917                         if (gtk_clist_get_selection_info(clist,
5918                                                          event->x, event->y,
5919                                                          &row, &column)) {
5920                                 gtk_clist_select_row(clist, row, column);
5921                                 gtkut_clist_set_focus_row(clist, row);
5922                         }
5923                 }
5924                 gtk_menu_popup(GTK_MENU(compose->popupmenu), NULL, NULL,
5925                                NULL, NULL, event->button, event->time);
5926         }
5927 }
5928
5929 static void attach_key_pressed(GtkWidget *widget, GdkEventKey *event,
5930                                gpointer data)
5931 {
5932         Compose *compose = (Compose *)data;
5933
5934         if (!event) return;
5935
5936         switch (event->keyval) {
5937         case GDK_Delete:
5938                 compose_attach_remove_selected(compose);
5939                 break;
5940         }
5941 }
5942
5943 static void compose_send_cb(gpointer data, guint action, GtkWidget *widget)
5944 {
5945         Compose *compose = (Compose *)data;
5946         gint val;
5947
5948         val = compose_send(compose);
5949
5950         if (val == 0) gtk_widget_destroy(compose->window);
5951 }
5952
5953 static void compose_send_later_cb(gpointer data, guint action,
5954                                   GtkWidget *widget)
5955 {
5956         Compose *compose = (Compose *)data;
5957         gint val;
5958
5959         val = compose_queue(compose, NULL, NULL);
5960         if (!val) gtk_widget_destroy(compose->window);
5961 }
5962
5963 static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
5964 {
5965         Compose *compose = (Compose *)data;
5966         FolderItem *draft;
5967         gchar *tmp;
5968         gint msgnum;
5969         static gboolean lock = FALSE;
5970
5971         if (lock) return;
5972
5973         draft = folder_get_default_draft();
5974         g_return_if_fail(draft != NULL);
5975
5976         lock = TRUE;
5977
5978         tmp = g_strdup_printf("%s%cdraft.%d", g_get_tmp_dir(),
5979                               G_DIR_SEPARATOR, (gint)compose);
5980
5981         if (compose_write_to_file(compose, tmp, TRUE) < 0) {
5982                 g_free(tmp);
5983                 lock = FALSE;
5984                 return;
5985         }
5986
5987         folder_item_scan(draft);
5988         if ((msgnum = folder_item_add_msg(draft, tmp, TRUE)) <= 0) {
5989                 unlink(tmp);
5990                 g_free(tmp);
5991                 lock = FALSE;
5992                 return;
5993         }
5994         g_free(tmp);
5995         draft->mtime = 0;       /* force updating */
5996
5997         if (compose->mode == COMPOSE_REEDIT) {
5998                 compose_remove_reedit_target(compose);
5999                 if (compose->targetinfo &&
6000                     compose->targetinfo->folder != draft)
6001                         folderview_update_item(compose->targetinfo->folder,
6002                                                TRUE);
6003         }
6004
6005         folder_item_scan(draft);
6006         folderview_update_item(draft, TRUE);
6007
6008         lock = FALSE;
6009
6010         /* 0: quit editing  1: keep editing */
6011         if (action == 0)
6012                 gtk_widget_destroy(compose->window);
6013         else {
6014                 struct stat s;
6015                 gchar *path;
6016
6017                 path = folder_item_fetch_msg(draft, msgnum);
6018                 g_return_if_fail(path != NULL);
6019                 if (stat(path, &s) < 0) {
6020                         FILE_OP_ERROR(path, "stat");
6021                         g_free(path);
6022                         lock = FALSE;
6023                         return;
6024                 }
6025                 g_free(path);
6026
6027                 procmsg_msginfo_free(compose->targetinfo);
6028                 compose->targetinfo = g_new0(MsgInfo, 1);
6029                 compose->targetinfo->msgnum = msgnum;
6030                 compose->targetinfo->size = s.st_size;
6031                 compose->targetinfo->mtime = s.st_mtime;
6032                 compose->targetinfo->folder = draft;
6033                 compose->mode = COMPOSE_REEDIT;
6034         }
6035 }
6036
6037 static void compose_attach_cb(gpointer data, guint action, GtkWidget *widget)
6038 {
6039         Compose *compose = (Compose *)data;
6040         GList *file_list;
6041
6042         if (compose->bounce_filename != NULL)
6043                 return;
6044
6045         file_list = filesel_select_multiple_files(_("Select file"), NULL);
6046
6047         if (file_list) {
6048                 GList *tmp;
6049
6050                 for ( tmp = file_list; tmp; tmp = tmp->next) {
6051                         gchar *file = (gchar *) tmp->data;
6052                         compose_attach_append(compose, file, MIME_UNKNOWN);
6053                         g_free(file);
6054                 }
6055                 g_list_free(file_list);
6056         }               
6057 }
6058
6059 static void compose_insert_file_cb(gpointer data, guint action,
6060                                    GtkWidget *widget)
6061 {
6062         Compose *compose = (Compose *)data;
6063         GList *file_list;
6064
6065         file_list = filesel_select_multiple_files(_("Select file"), NULL);
6066
6067         if (file_list) {
6068                 GList *tmp;
6069
6070                 for ( tmp = file_list; tmp; tmp = tmp->next) {
6071                         gchar *file = (gchar *) tmp->data;
6072                         compose_insert_file(compose, file);
6073                         g_free(file);
6074                 }
6075                 g_list_free(file_list);
6076         }
6077 }
6078
6079 static gint compose_delete_cb(GtkWidget *widget, GdkEventAny *event,
6080                               gpointer data)
6081 {
6082         compose_close_cb(data, 0, NULL);
6083         return TRUE;
6084 }
6085
6086 static void compose_close_cb(gpointer data, guint action, GtkWidget *widget)
6087 {
6088         Compose *compose = (Compose *)data;
6089         AlertValue val;
6090
6091         if (compose->exteditor_tag != -1) {
6092                 if (!compose_ext_editor_kill(compose))
6093                         return;
6094         }
6095
6096         if (compose->modified) {
6097                 val = alertpanel(_("Discard message"),
6098                                  _("This message has been modified. discard it?"),
6099                                  _("Discard"), _("to Draft"), _("Cancel"));
6100
6101                 switch (val) {
6102                 case G_ALERTDEFAULT:
6103                         break;
6104                 case G_ALERTALTERNATE:
6105                         compose_draft_cb(data, 0, NULL);
6106                         return;
6107                 default:
6108                         return;
6109                 }
6110         }
6111         gtk_widget_destroy(compose->window);
6112 }
6113
6114 static void compose_address_cb(gpointer data, guint action, GtkWidget *widget)
6115 {
6116         Compose *compose = (Compose *)data;
6117
6118         addressbook_open(compose);
6119 }
6120
6121 static void compose_template_activate_cb(GtkWidget *widget, gpointer data)
6122 {
6123         Compose *compose = (Compose *)data;
6124         Template *tmpl;
6125
6126         tmpl = gtk_object_get_data(GTK_OBJECT(widget), "template");
6127         g_return_if_fail(tmpl != NULL);
6128
6129         compose_template_apply(compose, tmpl);
6130 }
6131
6132 static void compose_ext_editor_cb(gpointer data, guint action,
6133                                   GtkWidget *widget)
6134 {
6135         Compose *compose = (Compose *)data;
6136
6137         compose_exec_ext_editor(compose);
6138 }
6139
6140 static void compose_destroy_cb(GtkWidget *widget, Compose *compose)
6141 {
6142         compose_destroy(compose);
6143 }
6144
6145 static void compose_undo_cb(Compose *compose)
6146 {
6147         undo_undo(compose->undostruct);
6148 }
6149
6150 static void compose_redo_cb(Compose *compose)
6151 {
6152         undo_redo(compose->undostruct);
6153 }
6154
6155 static void compose_cut_cb(Compose *compose)
6156 {
6157         if (compose->focused_editable &&
6158             GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
6159                 gtk_editable_cut_clipboard
6160                         (GTK_EDITABLE(compose->focused_editable));
6161 }
6162
6163 static void compose_copy_cb(Compose *compose)
6164 {
6165         if (compose->focused_editable &&
6166             GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
6167                 gtk_editable_copy_clipboard
6168                         (GTK_EDITABLE(compose->focused_editable));
6169 }
6170
6171 static void compose_paste_cb(Compose *compose)
6172 {
6173         if (compose->focused_editable &&
6174             GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
6175                 gtk_editable_paste_clipboard
6176                         (GTK_EDITABLE(compose->focused_editable));
6177 }
6178
6179 static void compose_allsel_cb(Compose *compose)
6180 {
6181         if (compose->focused_editable &&
6182             GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
6183                 gtk_editable_select_region
6184                         (GTK_EDITABLE(compose->focused_editable), 0, -1);
6185 }
6186
6187 static void compose_move_beginning_of_line_cb(Compose *compose)
6188 {
6189         if (compose->focused_editable &&
6190                 GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
6191                 gtk_stext_move_beginning_of_line(GTK_STEXT(compose->focused_editable));
6192 }
6193
6194 static void compose_gtk_stext_action_cb(Compose *compose, ComposeCallGtkSTextAction action)
6195 {
6196         if (!(compose->focused_editable && GTK_WIDGET_HAS_FOCUS(compose->focused_editable))) return;
6197                 
6198         switch (action) {
6199                 case COMPOSE_CALL_GTK_STEXT_MOVE_BEGINNING_OF_LINE:
6200                         gtk_stext_move_beginning_of_line(GTK_STEXT(compose->focused_editable));
6201                         break;
6202                 case COMPOSE_CALL_GTK_STEXT_MOVE_FORWARD_CHARACTER:
6203                         gtk_stext_move_forward_character(GTK_STEXT(compose->focused_editable));
6204                         break;
6205                 case COMPOSE_CALL_GTK_STEXT_MOVE_BACKWARD_CHARACTER:
6206                         gtk_stext_move_backward_character(GTK_STEXT(compose->focused_editable));
6207                         break;
6208                 case COMPOSE_CALL_GTK_STEXT_MOVE_FORWARD_WORD:
6209                         gtk_stext_move_forward_word(GTK_STEXT(compose->focused_editable));
6210                         break;
6211                 case COMPOSE_CALL_GTK_STEXT_MOVE_BACKWARD_WORD:
6212                         gtk_stext_move_backward_word(GTK_STEXT(compose->focused_editable));
6213                         break;
6214                 case COMPOSE_CALL_GTK_STEXT_MOVE_END_OF_LINE:
6215                         gtk_stext_move_end_of_line(GTK_STEXT(compose->focused_editable));
6216                         break;
6217                 case COMPOSE_CALL_GTK_STEXT_MOVE_NEXT_LINE:
6218                         gtk_stext_move_next_line(GTK_STEXT(compose->focused_editable));
6219                         break;
6220                 case COMPOSE_CALL_GTK_STEXT_MOVE_PREVIOUS_LINE:
6221                         gtk_stext_move_previous_line(GTK_STEXT(compose->focused_editable));
6222                         break;
6223                 case COMPOSE_CALL_GTK_STEXT_DELETE_FORWARD_CHARACTER:
6224                         gtk_stext_delete_forward_character(GTK_STEXT(compose->focused_editable));
6225                         break;
6226                 case COMPOSE_CALL_GTK_STEXT_DELETE_BACKWARD_CHARACTER:
6227                         gtk_stext_delete_backward_character(GTK_STEXT(compose->focused_editable));
6228                         break;
6229                 case COMPOSE_CALL_GTK_STEXT_DELETE_FORWARD_WORD:
6230                         gtk_stext_delete_forward_word(GTK_STEXT(compose->focused_editable));
6231                         break;
6232                 case COMPOSE_CALL_GTK_STEXT_DELETE_BACKWARD_WORD:
6233                         gtk_stext_delete_backward_word(GTK_STEXT(compose->focused_editable));
6234                         break;
6235                 case COMPOSE_CALL_GTK_STEXT_DELETE_LINE:
6236                         gtk_stext_delete_line(GTK_STEXT(compose->focused_editable));
6237                         break;
6238                 case COMPOSE_CALL_GTK_STEXT_DELETE_LINE_N:
6239                         gtk_stext_delete_line(GTK_STEXT(compose->focused_editable));
6240                         gtk_stext_delete_forward_character(GTK_STEXT(compose->focused_editable));
6241                         break;
6242                 case COMPOSE_CALL_GTK_STEXT_DELETE_TO_LINE_END:
6243                         gtk_stext_delete_to_line_end(GTK_STEXT(compose->focused_editable));
6244                         break;
6245                 default:
6246                         break;
6247         }
6248 }
6249
6250 static void compose_grab_focus_cb(GtkWidget *widget, Compose *compose)
6251 {
6252         if (GTK_IS_EDITABLE(widget))
6253                 compose->focused_editable = widget;
6254 }
6255
6256 static void compose_changed_cb(GtkEditable *editable, Compose *compose)
6257 {
6258         if (compose->modified == FALSE) {
6259                 compose->modified = TRUE;
6260                 compose_set_title(compose);
6261         }
6262 }
6263
6264 static void compose_button_press_cb(GtkWidget *widget, GdkEventButton *event,
6265                                     Compose *compose)
6266 {
6267         gtk_stext_set_point(GTK_STEXT(widget),
6268                            gtk_editable_get_position(GTK_EDITABLE(widget)));
6269 }
6270
6271 #if 0
6272 static void compose_key_press_cb(GtkWidget *widget, GdkEventKey *event,
6273                                  Compose *compose)
6274 {
6275         gtk_stext_set_point(GTK_STEXT(widget),
6276                            gtk_editable_get_position(GTK_EDITABLE(widget)));
6277 }
6278 #endif
6279
6280 #if 0 /* NEW COMPOSE GUI */
6281 static void compose_toggle_to_cb(gpointer data, guint action,
6282                                  GtkWidget *widget)
6283 {
6284         Compose *compose = (Compose *)data;
6285
6286         if (GTK_CHECK_MENU_ITEM(widget)->active) {
6287                 gtk_widget_show(compose->to_hbox);
6288                 gtk_widget_show(compose->to_entry);
6289                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 1, 4);
6290                 compose->use_to = TRUE;
6291         } else {
6292                 gtk_widget_hide(compose->to_hbox);
6293                 gtk_widget_hide(compose->to_entry);
6294                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 1, 0);
6295                 gtk_widget_queue_resize(compose->table_vbox);
6296                 compose->use_to = FALSE;
6297         }
6298
6299         if (addressbook_get_target_compose() == compose)
6300                 addressbook_set_target_compose(compose);
6301 }
6302
6303 static void compose_toggle_cc_cb(gpointer data, guint action,
6304                                  GtkWidget *widget)
6305 {
6306         Compose *compose = (Compose *)data;
6307
6308         if (GTK_CHECK_MENU_ITEM(widget)->active) {
6309                 gtk_widget_show(compose->cc_hbox);
6310                 gtk_widget_show(compose->cc_entry);
6311                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 3, 4);
6312                 compose->use_cc = TRUE;
6313         } else {
6314                 gtk_widget_hide(compose->cc_hbox);
6315                 gtk_widget_hide(compose->cc_entry);
6316                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 3, 0);
6317                 gtk_widget_queue_resize(compose->table_vbox);
6318                 compose->use_cc = FALSE;
6319         }
6320
6321         if (addressbook_get_target_compose() == compose)
6322                 addressbook_set_target_compose(compose);
6323 }
6324
6325 static void compose_toggle_bcc_cb(gpointer data, guint action,
6326                                   GtkWidget *widget)
6327 {
6328         Compose *compose = (Compose *)data;
6329
6330         if (GTK_CHECK_MENU_ITEM(widget)->active) {
6331                 gtk_widget_show(compose->bcc_hbox);
6332                 gtk_widget_show(compose->bcc_entry);
6333                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 4, 4);
6334                 compose->use_bcc = TRUE;
6335         } else {
6336                 gtk_widget_hide(compose->bcc_hbox);
6337                 gtk_widget_hide(compose->bcc_entry);
6338                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 4, 0);
6339                 gtk_widget_queue_resize(compose->table_vbox);
6340                 compose->use_bcc = FALSE;
6341         }
6342
6343         if (addressbook_get_target_compose() == compose)
6344                 addressbook_set_target_compose(compose);
6345 }
6346
6347 static void compose_toggle_replyto_cb(gpointer data, guint action,
6348                                       GtkWidget *widget)
6349 {
6350         Compose *compose = (Compose *)data;
6351
6352         if (GTK_CHECK_MENU_ITEM(widget)->active) {
6353                 gtk_widget_show(compose->reply_hbox);
6354                 gtk_widget_show(compose->reply_entry);
6355                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 5, 4);
6356                 compose->use_replyto = TRUE;
6357         } else {
6358                 gtk_widget_hide(compose->reply_hbox);
6359                 gtk_widget_hide(compose->reply_entry);
6360                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 5, 0);
6361                 gtk_widget_queue_resize(compose->table_vbox);
6362                 compose->use_replyto = FALSE;
6363         }
6364 }
6365
6366 static void compose_toggle_followupto_cb(gpointer data, guint action,
6367                                          GtkWidget *widget)
6368 {
6369         Compose *compose = (Compose *)data;
6370
6371         if (GTK_CHECK_MENU_ITEM(widget)->active) {
6372                 gtk_widget_show(compose->followup_hbox);
6373                 gtk_widget_show(compose->followup_entry);
6374                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 6, 4);
6375                 compose->use_followupto = TRUE;
6376         } else {
6377                 gtk_widget_hide(compose->followup_hbox);
6378                 gtk_widget_hide(compose->followup_entry);
6379                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 6, 0);
6380                 gtk_widget_queue_resize(compose->table_vbox);
6381                 compose->use_followupto = FALSE;
6382         }
6383 }
6384
6385 static void compose_toggle_attach_cb(gpointer data, guint action,
6386                                      GtkWidget *widget)
6387 {
6388         Compose *compose = (Compose *)data;
6389
6390         if (GTK_CHECK_MENU_ITEM(widget)->active) {
6391                 gtk_widget_ref(compose->edit_vbox);
6392
6393                 gtkut_container_remove(GTK_CONTAINER(compose->vbox2),
6394                                        compose->edit_vbox);
6395                 gtk_paned_add2(GTK_PANED(compose->paned), compose->edit_vbox);
6396                 gtk_box_pack_start(GTK_BOX(compose->vbox2), compose->paned,
6397                                    TRUE, TRUE, 0);
6398                 gtk_widget_show(compose->paned);
6399
6400                 gtk_widget_unref(compose->edit_vbox);
6401                 gtk_widget_unref(compose->paned);
6402
6403                 compose->use_attach = TRUE;
6404         } else {
6405                 gtk_widget_ref(compose->paned);
6406                 gtk_widget_ref(compose->edit_vbox);
6407
6408                 gtkut_container_remove(GTK_CONTAINER(compose->vbox2),
6409                                        compose->paned);
6410                 gtkut_container_remove(GTK_CONTAINER(compose->paned),
6411                                        compose->edit_vbox);
6412                 gtk_box_pack_start(GTK_BOX(compose->vbox2),
6413                                    compose->edit_vbox, TRUE, TRUE, 0);
6414
6415                 gtk_widget_unref(compose->edit_vbox);
6416
6417                 compose->use_attach = FALSE;
6418         }
6419 }
6420 #endif
6421
6422 #if USE_GPGME
6423 static void compose_toggle_sign_cb(gpointer data, guint action,
6424                                    GtkWidget *widget)
6425 {
6426         Compose *compose = (Compose *)data;
6427
6428         if (GTK_CHECK_MENU_ITEM(widget)->active)
6429                 compose->use_signing = TRUE;
6430         else
6431                 compose->use_signing = FALSE;
6432 }
6433
6434 static void compose_toggle_encrypt_cb(gpointer data, guint action,
6435                                       GtkWidget *widget)
6436 {
6437         Compose *compose = (Compose *)data;
6438
6439         if (GTK_CHECK_MENU_ITEM(widget)->active)
6440                 compose->use_encryption = TRUE;
6441         else
6442                 compose->use_encryption = FALSE;
6443 }
6444 #endif /* USE_GPGME */
6445
6446 static void compose_toggle_ruler_cb(gpointer data, guint action,
6447                                     GtkWidget *widget)
6448 {
6449         Compose *compose = (Compose *)data;
6450
6451         if (GTK_CHECK_MENU_ITEM(widget)->active) {
6452                 gtk_widget_show(compose->ruler_hbox);
6453                 prefs_common.show_ruler = TRUE;
6454         } else {
6455                 gtk_widget_hide(compose->ruler_hbox);
6456                 gtk_widget_queue_resize(compose->edit_vbox);
6457                 prefs_common.show_ruler = FALSE;
6458         }
6459 }
6460
6461 static void compose_attach_drag_received_cb (GtkWidget          *widget,
6462                                              GdkDragContext     *drag_context,
6463                                              gint                x,
6464                                              gint                y,
6465                                              GtkSelectionData   *data,
6466                                              guint               info,
6467                                              guint               time,
6468                                              gpointer            user_data)
6469 {
6470         Compose *compose = (Compose *)user_data;
6471         GList *list, *tmp;
6472
6473         list = uri_list_extract_filenames((const gchar *)data->data);
6474         for (tmp = list; tmp != NULL; tmp = tmp->next)
6475                 compose_attach_append(compose, (const gchar *)tmp->data,
6476                                       MIME_UNKNOWN);
6477         list_free_strings(list);
6478         g_list_free(list);
6479 }
6480
6481 static void compose_insert_drag_received_cb (GtkWidget          *widget,
6482                                              GdkDragContext     *drag_context,
6483                                              gint                x,
6484                                              gint                y,
6485                                              GtkSelectionData   *data,
6486                                              guint               info,
6487                                              guint               time,
6488                                              gpointer            user_data)
6489 {
6490         Compose *compose = (Compose *)user_data;
6491         GList *list, *tmp;
6492
6493         list = uri_list_extract_filenames((const gchar *)data->data);
6494         for (tmp = list; tmp != NULL; tmp = tmp->next)
6495                 compose_insert_file(compose, (const gchar *)tmp->data);
6496         list_free_strings(list);
6497         g_list_free(list);
6498 }
6499
6500 #if 0 /* NEW COMPOSE GUI */
6501 static void to_activated(GtkWidget *widget, Compose *compose)
6502 {
6503         if (GTK_WIDGET_VISIBLE(compose->newsgroups_entry))
6504                 gtk_widget_grab_focus(compose->newsgroups_entry);
6505         else
6506                 gtk_widget_grab_focus(compose->subject_entry);
6507 }
6508
6509 static void newsgroups_activated(GtkWidget *widget, Compose *compose)
6510 {
6511         gtk_widget_grab_focus(compose->subject_entry);
6512 }
6513
6514 static void subject_activated(GtkWidget *widget, Compose *compose)
6515 {
6516         if (GTK_WIDGET_VISIBLE(compose->cc_entry))
6517                 gtk_widget_grab_focus(compose->cc_entry);
6518         else if (GTK_WIDGET_VISIBLE(compose->bcc_entry))
6519                 gtk_widget_grab_focus(compose->bcc_entry);
6520         else if (GTK_WIDGET_VISIBLE(compose->reply_entry))
6521                 gtk_widget_grab_focus(compose->reply_entry);
6522         else if (GTK_WIDGET_VISIBLE(compose->followup_entry))
6523                 gtk_widget_grab_focus(compose->followup_entry);
6524         else
6525                 gtk_widget_grab_focus(compose->text);
6526 }
6527
6528 static void cc_activated(GtkWidget *widget, Compose *compose)
6529 {
6530         if (GTK_WIDGET_VISIBLE(compose->bcc_entry))
6531                 gtk_widget_grab_focus(compose->bcc_entry);
6532         else if (GTK_WIDGET_VISIBLE(compose->reply_entry))
6533                 gtk_widget_grab_focus(compose->reply_entry);
6534         else if (GTK_WIDGET_VISIBLE(compose->followup_entry))
6535                 gtk_widget_grab_focus(compose->followup_entry);
6536         else
6537                 gtk_widget_grab_focus(compose->text);
6538 }
6539
6540 static void bcc_activated(GtkWidget *widget, Compose *compose)
6541 {
6542         if (GTK_WIDGET_VISIBLE(compose->reply_entry))
6543                 gtk_widget_grab_focus(compose->reply_entry);
6544         else if (GTK_WIDGET_VISIBLE(compose->followup_entry))
6545                 gtk_widget_grab_focus(compose->followup_entry);
6546         else
6547                 gtk_widget_grab_focus(compose->text);
6548 }
6549
6550 static void replyto_activated(GtkWidget *widget, Compose *compose)
6551 {
6552         if (GTK_WIDGET_VISIBLE(compose->followup_entry))
6553                 gtk_widget_grab_focus(compose->followup_entry);
6554         else
6555                 gtk_widget_grab_focus(compose->text);
6556 }
6557
6558 static void followupto_activated(GtkWidget *widget, Compose *compose)
6559 {
6560         gtk_widget_grab_focus(compose->text);
6561 }
6562 #endif
6563
6564 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
6565                                              GtkWidget *widget)
6566 {
6567         Compose *compose = (Compose *)data;
6568
6569         if (GTK_CHECK_MENU_ITEM(widget)->active)
6570                 compose->return_receipt = TRUE;
6571         else
6572                 compose->return_receipt = FALSE;
6573 }
6574
6575 void compose_headerentry_key_press_event_cb(GtkWidget *entry,
6576                                             GdkEventKey *event,
6577                                             ComposeHeaderEntry *headerentry)
6578 {
6579         if ((g_slist_length(headerentry->compose->header_list) > 0) &&
6580             ((headerentry->headernum + 1) != headerentry->compose->header_nextrow) &&
6581             !(event->state & GDK_MODIFIER_MASK) &&
6582             (event->keyval == GDK_BackSpace) &&
6583             (strlen(gtk_entry_get_text(GTK_ENTRY(entry))) == 0)) {
6584                 gtk_container_remove
6585                         (GTK_CONTAINER(headerentry->compose->header_table),
6586                          headerentry->combo);
6587                 gtk_container_remove
6588                         (GTK_CONTAINER(headerentry->compose->header_table),
6589                          headerentry->entry);
6590                 headerentry->compose->header_list =
6591                         g_slist_remove(headerentry->compose->header_list,
6592                                        headerentry);
6593                 g_free(headerentry);
6594         } else  if (event->keyval == GDK_Tab) {
6595                 if (headerentry->compose->header_last == headerentry) {
6596                         /* Override default next focus, and give it to subject_entry
6597                          * instead of notebook tabs
6598                          */
6599                         gtk_signal_emit_stop_by_name(GTK_OBJECT(entry), "key-press-event"); 
6600                         gtk_widget_grab_focus(headerentry->compose->subject_entry);
6601                 }
6602         }
6603
6604 }
6605
6606 void compose_headerentry_changed_cb(GtkWidget *entry,
6607                                     ComposeHeaderEntry *headerentry)
6608 {
6609         if (strlen(gtk_entry_get_text(GTK_ENTRY(entry))) != 0) {
6610                 headerentry->compose->header_list =
6611                         g_slist_append(headerentry->compose->header_list,
6612                                        headerentry);
6613                 compose_create_header_entry(headerentry->compose);
6614                 gtk_signal_disconnect_by_func
6615                         (GTK_OBJECT(entry),
6616                          GTK_SIGNAL_FUNC(compose_headerentry_changed_cb),
6617                          headerentry);
6618                 /* Automatically scroll down */
6619                 compose_show_first_last_header(headerentry->compose, FALSE);
6620                 
6621         }
6622 }
6623
6624 static void compose_show_first_last_header(Compose *compose, gboolean show_first)
6625 {
6626         GtkAdjustment *vadj;
6627
6628         g_return_if_fail(compose);
6629         g_return_if_fail(GTK_IS_WIDGET(compose->header_table));
6630         g_return_if_fail(GTK_IS_VIEWPORT(compose->header_table->parent));
6631
6632         vadj = gtk_viewport_get_vadjustment(GTK_VIEWPORT(compose->header_table->parent));
6633         gtk_adjustment_set_value(vadj, (show_first ? vadj->lower : vadj->upper));
6634 }
6635
6636 static void text_activated(GtkWidget *widget, Compose *compose)
6637 {
6638         compose_send_control_enter(compose);
6639 }
6640
6641 static gboolean compose_send_control_enter(Compose *compose)
6642 {
6643         GdkEvent *ev;
6644         GdkEventKey *kev;
6645         GtkItemFactory *ifactory;
6646         GtkAccelEntry *accel;
6647         GtkWidget *send_menu;
6648         GSList *list;
6649
6650         ev = gtk_get_current_event();
6651         if (ev->type != GDK_KEY_PRESS) return FALSE;
6652
6653         kev = (GdkEventKey *)ev;
6654         if (!(kev->keyval == GDK_Return && (kev->state & GDK_CONTROL_MASK)))
6655                 return FALSE;
6656
6657         ifactory = gtk_item_factory_from_widget(compose->menubar);
6658         send_menu = gtk_item_factory_get_widget(ifactory, "/Message/Send");
6659         list = gtk_accel_group_entries_from_object(GTK_OBJECT(send_menu));
6660         accel = (GtkAccelEntry *)list->data;
6661         if (accel->accelerator_key == kev->keyval &&
6662             accel->accelerator_mods == kev->state) {
6663                 compose_send_cb(compose, 0, NULL);
6664                 return TRUE;
6665         }
6666
6667         return FALSE;
6668 }
6669
6670 #if USE_PSPELL
6671 static void compose_check_all(Compose *compose)
6672 {
6673         if (compose->gtkpspell)
6674                 gtkpspell_check_all(compose->gtkpspell);
6675 }
6676
6677 static void compose_highlight_all(Compose *compose)
6678 {
6679         if (compose->gtkpspell)
6680                 gtkpspell_highlight_all(compose->gtkpspell);
6681 }
6682
6683 static void compose_check_backwards(Compose *compose)
6684 {
6685         if (compose->gtkpspell) 
6686                 gtkpspell_check_backwards(compose->gtkpspell);
6687         else {
6688                 GtkItemFactory *ifactory;
6689                 ifactory = gtk_item_factory_from_widget(compose->popupmenu);
6690                 menu_set_sensitive(ifactory, "/Edit/Check backwards misspelled word", FALSE);
6691                 menu_set_sensitive(ifactory, "/Edit/Forward to next misspelled word", FALSE);
6692         }
6693 }
6694
6695 static void compose_check_forwards_go(Compose *compose)
6696 {
6697         if (compose->gtkpspell) 
6698                 gtkpspell_check_forwards_go(compose->gtkpspell);
6699         else {
6700                 GtkItemFactory *ifactory;
6701                 ifactory = gtk_item_factory_from_widget(compose->popupmenu);
6702                 menu_set_sensitive(ifactory, "/Edit/Check backwards misspelled word", FALSE);
6703                 menu_set_sensitive(ifactory, "/Edit/Forward to next misspelled word", FALSE);
6704         }
6705 }
6706 #endif