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