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