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