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