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