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