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