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