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