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