c42e0c4b9e0a1cb5c256b138fe78c787a458dd19
[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
102 #if USE_GPGME
103 #  include "rfc2015.h"
104 #endif
105
106 #include "quote_fmt.h"
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 static void compose_toolbar_create              (Compose        *compose,
128                                                  GtkWidget      *container);
129 static GtkWidget *compose_account_option_menu_create
130                                                 (Compose        *compose);
131 static void compose_destroy                     (Compose        *compose);
132
133 static gint compose_parse_header                (Compose        *compose,
134                                                  MsgInfo        *msginfo);
135 static gchar *compose_parse_references          (const gchar    *ref,
136                                                  const gchar    *msgid);
137 static void compose_quote_file                  (Compose        *compose,
138                                                  MsgInfo        *msginfo,
139                                                  FILE           *fp);
140 static gchar *compose_quote_parse_fmt           (Compose        *compose,
141                                                  MsgInfo        *msginfo,
142                                                  const gchar    *fmt);
143 static void compose_reply_set_entry             (Compose        *compose,
144                                                  MsgInfo        *msginfo,
145                                                  gboolean        to_all,
146                                                  gboolean        to_sender,
147                                                  gboolean
148                                                  followup_and_reply_to);
149 static void compose_reedit_set_entry            (Compose        *compose,
150                                                  MsgInfo        *msginfo);
151 static void compose_insert_sig                  (Compose        *compose);
152 static void compose_insert_file                 (Compose        *compose,
153                                                  const gchar    *file);
154 static void compose_attach_append               (Compose        *compose,
155                                                  const gchar    *file,
156                                                  ContentType     cnttype);
157 static void compose_attach_append_with_type(Compose *compose,
158                                             const gchar *file,
159                                             const gchar *type,
160                                             ContentType cnttype);
161 static void compose_wrap_line                   (Compose        *compose);
162 static void compose_set_title                   (Compose        *compose);
163
164 static PrefsAccount *compose_current_mail_account(void);
165 /* static gint compose_send                     (Compose        *compose); */
166 static gint compose_write_to_file               (Compose        *compose,
167                                                  const gchar    *file,
168                                                  gboolean        is_draft);
169 static gint compose_write_body_to_file          (Compose        *compose,
170                                                  const gchar    *file);
171 static gint compose_save_to_outbox              (Compose        *compose,
172                                                  const gchar    *file);
173 static gint compose_queue                       (Compose        *compose,
174                                                  const gchar    *file);
175 static void compose_write_attach                (Compose        *compose,
176                                                  FILE           *fp);
177 static gint compose_write_headers               (Compose        *compose,
178                                                  FILE           *fp,
179                                                  const gchar    *charset,
180                                                  EncodingType    encoding,
181                                                  gboolean        is_draft);
182
183 static void compose_convert_header              (gchar          *dest,
184                                                  gint            len,
185                                                  gchar          *src,
186                                                  gint            header_len);
187 static void compose_generate_msgid              (Compose        *compose,
188                                                  gchar          *buf,
189                                                  gint            len);
190
191 static void compose_attach_info_free            (AttachInfo     *ainfo);
192 static void compose_attach_remove_selected      (Compose        *compose);
193
194 static void compose_attach_property             (Compose        *compose);
195 static void compose_attach_property_create      (gboolean       *cancelled);
196 static void attach_property_ok                  (GtkWidget      *widget,
197                                                  gboolean       *cancelled);
198 static void attach_property_cancel              (GtkWidget      *widget,
199                                                  gboolean       *cancelled);
200 static gint attach_property_delete_event        (GtkWidget      *widget,
201                                                  GdkEventAny    *event,
202                                                  gboolean       *cancelled);
203 static void attach_property_key_pressed         (GtkWidget      *widget,
204                                                  GdkEventKey    *event,
205                                                  gboolean       *cancelled);
206
207 static void compose_exec_ext_editor             (Compose           *compose);
208 static gint compose_exec_ext_editor_real        (const gchar       *file);
209 static gboolean compose_ext_editor_kill         (Compose           *compose);
210 static void compose_input_cb                    (gpointer           data,
211                                                  gint               source,
212                                                  GdkInputCondition  condition);
213 static void compose_set_ext_editor_sensitive    (Compose           *compose,
214                                                  gboolean           sensitive);
215
216 static gint calc_cursor_xpos    (GtkSText       *text,
217                                  gint            extra,
218                                  gint            char_width);
219
220 /* callback functions */
221
222 static gboolean compose_edit_size_alloc (GtkEditable    *widget,
223                                          GtkAllocation  *allocation,
224                                          GtkSHRuler     *shruler);
225
226 static void toolbar_send_cb             (GtkWidget      *widget,
227                                          gpointer        data);
228 static void toolbar_send_later_cb       (GtkWidget      *widget,
229                                          gpointer        data);
230 static void toolbar_draft_cb            (GtkWidget      *widget,
231                                          gpointer        data);
232 static void toolbar_insert_cb           (GtkWidget      *widget,
233                                          gpointer        data);
234 static void toolbar_attach_cb           (GtkWidget      *widget,
235                                          gpointer        data);
236 static void toolbar_sig_cb              (GtkWidget      *widget,
237                                          gpointer        data);
238 static void toolbar_ext_editor_cb       (GtkWidget      *widget,
239                                          gpointer        data);
240 static void toolbar_linewrap_cb         (GtkWidget      *widget,
241                                          gpointer        data);
242 static void toolbar_address_cb          (GtkWidget      *widget,
243                                          gpointer        data);
244
245 static void select_account(Compose * compose, PrefsAccount * ac);
246 static void account_activated           (GtkMenuItem    *menuitem,
247                                          gpointer        data);
248
249 static void attach_selected             (GtkCList       *clist,
250                                          gint            row,
251                                          gint            column,
252                                          GdkEvent       *event,
253                                          gpointer        data);
254 static void attach_button_pressed       (GtkWidget      *widget,
255                                          GdkEventButton *event,
256                                          gpointer        data);
257 static void attach_key_pressed          (GtkWidget      *widget,
258                                          GdkEventKey    *event,
259                                          gpointer        data);
260
261 static void compose_send_cb             (gpointer        data,
262                                          guint           action,
263                                          GtkWidget      *widget);
264 static void compose_send_later_cb       (gpointer        data,
265                                          guint           action,
266                                          GtkWidget      *widget);
267
268 static void compose_draft_cb            (gpointer        data,
269                                          guint           action,
270                                          GtkWidget      *widget);
271
272 static void compose_attach_cb           (gpointer        data,
273                                          guint           action,
274                                          GtkWidget      *widget);
275 static void compose_insert_file_cb      (gpointer        data,
276                                          guint           action,
277                                          GtkWidget      *widget);
278
279 static void compose_close_cb            (gpointer        data,
280                                          guint           action,
281                                          GtkWidget      *widget);
282 static void compose_address_cb          (gpointer        data,
283                                          guint           action,
284                                          GtkWidget      *widget);
285
286 static void compose_ext_editor_cb       (gpointer        data,
287                                          guint           action,
288                                          GtkWidget      *widget);
289
290 static gint compose_delete_cb           (GtkWidget      *widget,
291                                          GdkEventAny    *event,
292                                          gpointer        data);
293 static void compose_destroy_cb          (GtkWidget      *widget,
294                                          Compose        *compose);
295
296 static void compose_cut_cb              (Compose        *compose);
297 static void compose_copy_cb             (Compose        *compose);
298 static void compose_paste_cb            (Compose        *compose);
299 static void compose_allsel_cb           (Compose        *compose);
300
301 static void compose_grab_focus_cb       (GtkWidget      *widget,
302                                          Compose        *compose);
303
304 static void compose_changed_cb          (GtkEditable    *editable,
305                                          Compose        *compose);
306 static void compose_button_press_cb     (GtkWidget      *widget,
307                                          GdkEventButton *event,
308                                          Compose        *compose);
309 #if 0
310 static void compose_key_press_cb        (GtkWidget      *widget,
311                                          GdkEventKey    *event,
312                                          Compose        *compose);
313 #endif
314
315 static void compose_toggle_to_cb        (gpointer        data,
316                                          guint           action,
317                                          GtkWidget      *widget);
318 static void compose_toggle_cc_cb        (gpointer        data,
319                                          guint           action,
320                                          GtkWidget      *widget);
321 static void compose_toggle_bcc_cb       (gpointer        data,
322                                          guint           action,
323                                          GtkWidget      *widget);
324 static void compose_toggle_replyto_cb   (gpointer        data,
325                                          guint           action,
326                                          GtkWidget      *widget);
327 static void compose_toggle_followupto_cb(gpointer        data,
328                                          guint           action,
329                                          GtkWidget      *widget);
330 static void compose_toggle_attach_cb    (gpointer        data,
331                                          guint           action,
332                                          GtkWidget      *widget);
333 static void compose_toggle_ruler_cb     (gpointer        data,
334                                          guint           action,
335                                          GtkWidget      *widget);
336 #if USE_GPGME
337 static void compose_toggle_sign_cb      (gpointer        data,
338                                          guint           action,
339                                          GtkWidget      *widget);
340 static void compose_toggle_encrypt_cb   (gpointer        data,
341                                          guint           action,
342                                          GtkWidget      *widget);
343 #endif
344 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
345                                              GtkWidget *widget);
346
347 static void compose_attach_drag_received_cb (GtkWidget          *widget,
348                                              GdkDragContext     *drag_context,
349                                              gint                x,
350                                              gint                y,
351                                              GtkSelectionData   *data,
352                                              guint               info,
353                                              guint               time,
354                                              gpointer            user_data);
355 static void compose_insert_drag_received_cb (GtkWidget          *widget,
356                                              GdkDragContext     *drag_context,
357                                              gint                x,
358                                              gint                y,
359                                              GtkSelectionData   *data,
360                                              guint               info,
361                                              guint               time,
362                                              gpointer            user_data);
363
364 static void to_activated                (GtkWidget      *widget,
365                                          Compose        *compose);
366 static void newsgroups_activated        (GtkWidget      *widget,
367                                          Compose        *compose);
368 static void subject_activated           (GtkWidget      *widget,
369                                          Compose        *compose);
370 static void cc_activated                (GtkWidget      *widget,
371                                          Compose        *compose);
372 static void bcc_activated               (GtkWidget      *widget,
373                                          Compose        *compose);
374 static void replyto_activated           (GtkWidget      *widget,
375                                          Compose        *compose);
376 static void followupto_activated        (GtkWidget      *widget,
377                                          Compose        *compose);
378 static void compose_attach_parts(Compose * compose,
379                                  MsgInfo * msginfo);
380
381 static gchar *compose_quote_fmt         (Compose        *compose,
382                                          MsgInfo        *msginfo,
383                                          const gchar    *fmt,
384                                          const gchar    * qmark);
385
386 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
387                                   gboolean to_all,
388                                   gboolean ignore_replyto,
389                                   gboolean followup_and_reply_to);
390
391 Compose * compose_generic_new (PrefsAccount     *account,
392                                const gchar      *to,
393                                FolderItem       *item);
394
395 static GtkItemFactoryEntry compose_popup_entries[] =
396 {
397         {N_("/_Add..."),        NULL, compose_attach_cb, 0, NULL},
398         {N_("/_Remove"),        NULL, compose_attach_remove_selected, 0, NULL},
399         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
400         {N_("/_Property..."),   NULL, compose_attach_property, 0, NULL}
401 };
402
403 static GtkItemFactoryEntry compose_entries[] =
404 {
405         {N_("/_File"),                          NULL, NULL, 0, "<Branch>"},
406         {N_("/_File/_Attach file"),             "<control>M", compose_attach_cb, 0, NULL},
407         {N_("/_File/_Insert file"),             "<control>I", compose_insert_file_cb, 0, NULL},
408         {N_("/_File/Insert si_gnature"),        "<control>G", compose_insert_sig, 0, NULL},
409         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
410         {N_("/_File/_Close"),                   "<alt>W", compose_close_cb, 0, NULL},
411
412         {N_("/_Edit"),             NULL,         NULL,  0, "<Branch>"},
413         {N_("/_Edit/_Undo"),       "<control>Z", NULL,  0, NULL},
414         {N_("/_Edit/_Redo"),       "<control>Y", NULL,  0, NULL},
415         {N_("/_Edit/---"),         NULL,         NULL,  0, "<Separator>"},
416         {N_("/_Edit/Cu_t"),        "<control>X", compose_cut_cb,    0, NULL},
417         {N_("/_Edit/_Copy"),       "<control>C", compose_copy_cb,   0, NULL},
418         {N_("/_Edit/_Paste"),      "<control>V", compose_paste_cb,  0, NULL},
419         {N_("/_Edit/Select _all"), "<control>A", compose_allsel_cb, 0, NULL},
420         {N_("/_Edit/---"),         NULL,         NULL,  0, "<Separator>"},
421         {N_("/_Edit/Wrap long _lines"), "<alt>L", compose_wrap_line, 0, NULL},
422         {N_("/_Edit/Edit with e_xternal editor"), "<alt>X",
423                                         compose_ext_editor_cb,  0, NULL},
424
425         {N_("/_Message"),               NULL,           NULL,   0, "<Branch>"},
426         {N_("/_Message/_Send"),         "<shift><control>S",
427                                         compose_send_cb, 0, NULL},
428         {N_("/_Message/Send _later"),   "<shift><alt>S",
429                                         compose_send_later_cb,  0, NULL},
430         {N_("/_Message/Save to _draft folder"),
431                                         "<alt>D",       compose_draft_cb, 0, NULL},
432         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
433         {N_("/_Message/_To"),           NULL, compose_toggle_to_cb, 0, "<ToggleItem>"},
434         {N_("/_Message/_Cc"),           NULL, compose_toggle_cc_cb, 0, "<ToggleItem>"},
435         {N_("/_Message/_Bcc"),          NULL, compose_toggle_bcc_cb, 0, "<ToggleItem>"},
436         {N_("/_Message/_Reply to"),     NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
437         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
438         {N_("/_Message/_Followup to"),  NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
439         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
440         {N_("/_Message/_Attach"),       NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
441 #if USE_GPGME
442         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
443         {N_("/_Message/Si_gn"),         NULL, compose_toggle_sign_cb, 0, "<ToggleItem>"},
444         {N_("/_Message/_Encrypt"),      NULL, compose_toggle_encrypt_cb, 0, "<ToggleItem>"},
445 #endif /* USE_GPGME */
446         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
447         {N_("/_Message/_Request Return Receipt"),       NULL, compose_toggle_return_receipt_cb, 0, "<ToggleItem>"},
448         {N_("/_Tool"),                  NULL, NULL,     0, "<Branch>"},
449         {N_("/_Tool/Show _ruler"),      NULL, compose_toggle_ruler_cb,  0, "<ToggleItem>"},
450         {N_("/_Tool/_Address book"),    "<alt>A",       compose_address_cb, 0, NULL},
451         {N_("/_Help"),                  NULL, NULL,     0, "<LastBranch>"},
452         {N_("/_Help/_About"),           NULL,           about_show,     0, NULL}
453 };
454
455 static GtkTargetEntry compose_mime_types[] =
456 {
457         {"text/uri-list", 0, 0}
458 };
459
460 Compose * compose_new(PrefsAccount *account)
461 {
462         return compose_generic_new(account, NULL, NULL);
463 }
464
465 Compose * compose_new_with_recipient(PrefsAccount *account, const gchar *to)
466 {
467         return compose_generic_new(account, to, NULL);
468 }
469
470 Compose * compose_new_with_folderitem(PrefsAccount *account, FolderItem *item)
471 {
472         return compose_generic_new(account, NULL, item);
473 }
474
475 Compose * compose_generic_new(PrefsAccount *account, const gchar *to, FolderItem *item)
476 {
477         Compose *compose;
478
479         if (!account) account = cur_account;
480         g_return_val_if_fail(account != NULL, NULL);
481
482         compose = compose_create(account);
483         compose->mode = COMPOSE_NEW;
484
485         if (prefs_common.auto_sig)
486                 compose_insert_sig(compose);
487         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
488         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
489
490         if (account->protocol != A_NNTP) {
491                 if (to) {
492                         compose_entry_append(compose, to, COMPOSE_TO);
493                         gtk_widget_grab_focus(compose->subject_entry);
494                 } else {
495                         if(item && item->prefs->enable_default_to) {
496                                 compose_entry_append(compose, item->prefs->default_to, COMPOSE_TO);
497                         } else {
498                                 gtk_widget_grab_focus(compose->to_entry);
499                         }
500                 }
501                 if(item && item->prefs->request_return_receipt) {
502                         GtkItemFactory *ifactory;
503                 
504                         ifactory = gtk_item_factory_from_widget(compose->menubar);
505                         menu_set_toggle(ifactory, "/Message/Request Return Receipt", TRUE);
506                 }
507         } else {
508                 if (to) {
509                         compose_entry_append(compose, to, COMPOSE_NEWSGROUPS);
510                         gtk_widget_grab_focus(compose->subject_entry);
511                 } else
512                         gtk_widget_grab_focus(compose->newsgroups_entry);
513         }
514
515         return compose;
516 }
517
518 #define CHANGE_FLAGS(msginfo) \
519 { \
520 if (msginfo->folder->folder->change_flags != NULL) \
521 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
522                                       msginfo->folder, \
523                                       msginfo); \
524 }
525
526 /*
527 Compose * compose_new_followup_and_replyto(PrefsAccount *account,
528                                            const gchar *followupto, gchar * to)
529 {
530         Compose *compose;
531
532         if (!account) account = cur_account;
533         g_return_val_if_fail(account != NULL, NULL);
534         g_return_val_if_fail(account->protocol != A_NNTP, NULL);
535
536         compose = compose_create(account);
537         compose->mode = COMPOSE_NEW;
538
539         if (prefs_common.auto_sig)
540                 compose_insert_sig(compose);
541         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
542         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
543
544         compose_entry_append(compose, to, COMPOSE_TO);
545         compose_entry_append(compose, followupto, COMPOSE_NEWSGROUPS);
546         gtk_widget_grab_focus(compose->subject_entry);
547
548         return compose;
549 }
550 */
551
552 void compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
553                    gboolean ignore_replyto)
554 {
555         compose_generic_reply(msginfo, quote, to_all, ignore_replyto, FALSE);
556 }
557
558 void compose_followup_and_reply_to(MsgInfo *msginfo, gboolean quote,
559                                    gboolean to_all,
560                                    gboolean ignore_replyto)
561 {
562         compose_generic_reply(msginfo, quote, to_all, ignore_replyto, TRUE);
563 }
564
565 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
566                                   gboolean to_all,
567                                   gboolean ignore_replyto,
568                                   gboolean followup_and_reply_to)
569 {
570         Compose *compose;
571         PrefsAccount *account;
572         PrefsAccount *reply_account;
573         GtkSText *text;
574
575         g_return_if_fail(msginfo != NULL);
576         g_return_if_fail(msginfo->folder != NULL);
577
578         account = msginfo->folder->folder->account;
579         if (!account && msginfo->to && prefs_common.reply_account_autosel) {
580                 gchar *to;
581                 Xstrdup_a(to, msginfo->to, return);
582                 extract_address(to);
583                 account = account_find_mail_from_address(to);
584         }
585         if (!account) account = cur_account;
586         g_return_if_fail(account != NULL);
587
588         if (ignore_replyto && account->protocol == A_NNTP &&
589             !followup_and_reply_to) {
590                 reply_account =
591                         account_find_mail_from_address(account->address);
592                 if (!reply_account)
593                         reply_account = compose_current_mail_account();
594                 if (!reply_account)
595                         return;
596         } else
597                 reply_account = account;
598         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
599         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
600
601         CHANGE_FLAGS(msginfo);
602
603         compose = compose_create(reply_account);
604         compose->mode = COMPOSE_REPLY;
605
606
607         if (followup_and_reply_to) {
608                 gtk_widget_show(compose->to_hbox);
609                 gtk_widget_show(compose->to_entry);
610                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 1, 4);
611                 compose->use_to = TRUE;
612         }
613
614         if (compose_parse_header(compose, msginfo) < 0) return;
615         compose_reply_set_entry(compose, msginfo, to_all, ignore_replyto,
616                                 followup_and_reply_to);
617
618         text = GTK_STEXT(compose->text);
619         gtk_stext_freeze(text);
620
621         if (quote) {
622                 FILE *fp;
623                 gchar *quote_str;
624
625                 if ((fp = procmime_get_text_part(msginfo)) == NULL)
626                         g_warning(_("Can't get text part\n"));
627                 else {
628                         gchar * qmark;
629
630                         if (prefs_common.quotemark && *prefs_common.quotemark)
631                                 qmark = prefs_common.quotemark;
632                         else
633                                 qmark = "> ";
634
635                         quote_str = compose_quote_fmt(compose, msginfo,
636                                                       prefs_common.quotefmt,
637                                                       qmark);
638
639                         /*
640                         quote_str = compose_quote_parse_fmt
641                                 (compose, msginfo, prefs_common.quotefmt);
642                         */
643
644                         if (quote_str != NULL)
645                                 gtk_stext_insert(text, NULL, NULL, NULL,
646                                                  quote_str, -1);
647                         /*                      g_free(quote_str); */
648                         /* compose_quote_file(compose, msginfo, fp); */
649                         fclose(fp);
650                 }
651         }
652
653         if (prefs_common.auto_sig)
654                 compose_insert_sig(compose);
655         gtk_editable_set_position(GTK_EDITABLE(text), 0);
656         gtk_stext_set_point(text, 0);
657
658         gtk_stext_thaw(text);
659         gtk_widget_grab_focus(compose->text);
660 }
661
662
663 static gchar *procmime_get_file_name(MimeInfo *mimeinfo)
664 {
665         gchar *base;
666         gchar *filename;
667
668         g_return_val_if_fail(mimeinfo != NULL, NULL);
669
670         base = mimeinfo->filename ? mimeinfo->filename
671                 : mimeinfo->name ? mimeinfo->name : NULL;
672
673         if (MIME_TEXT_HTML == mimeinfo->mime_type && base == NULL){
674                 filename = g_strdup_printf("%s%smimetmp%i.html",
675                                            get_mime_tmp_dir(),
676                                            G_DIR_SEPARATOR_S,
677                                            mimeinfo);
678                 return filename;
679         }
680         else {
681                 base = base ? base : "";
682                 base = g_basename(base);
683                 if (*base == '\0') {
684                         filename = g_strdup_printf("%s%smimetmp%i",
685                                                    get_mime_tmp_dir(),
686                                                    G_DIR_SEPARATOR_S,
687                                                    mimeinfo);
688                         return filename;
689                 }
690         }
691
692         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
693                                base, NULL);
694
695         return filename;
696 }
697
698 static gchar * mime_extract_file(gchar * source, MimeInfo *partinfo)
699 {
700         gchar *filename;
701
702         if (!partinfo) return;
703
704         filename = procmime_get_file_name(partinfo);
705
706         if (procmime_get_part(filename, source, partinfo) < 0)
707                 alertpanel_error
708                         (_("Can't get the part of multipart message."));
709
710         return filename;
711 }
712
713 static void compose_attach_parts(Compose * compose,
714                                  MsgInfo * msginfo)
715 {
716
717         FILE *fp;
718         gchar *file;
719         MimeInfo *mimeinfo;
720         MsgInfo *tmpmsginfo;
721         gchar *p;
722         gchar *boundary;
723         gint boundary_len = 0;
724         gchar buf[BUFFSIZE];
725         glong fpos, prev_fpos;
726         gint npart;
727         gchar * source;
728         gchar * filename;
729
730         g_return_if_fail(msginfo != NULL);
731         
732 #if USE_GPGME
733         for (;;) {
734                 if ((fp = procmsg_open_message(msginfo)) == NULL) return;
735                 mimeinfo = procmime_scan_mime_header(fp);
736                 if (!mimeinfo) break;
737
738                 if (!MSG_IS_ENCRYPTED(msginfo->flags) &&
739                     rfc2015_is_encrypted(mimeinfo)) {
740                         MSG_SET_TMP_FLAGS(msginfo->flags, MSG_ENCRYPTED);
741                 }
742                 if (MSG_IS_ENCRYPTED(msginfo->flags) &&
743                     !msginfo->plaintext_file  &&
744                     !msginfo->decryption_failed) {
745                         rfc2015_decrypt_message(msginfo, mimeinfo, fp);
746                         if (msginfo->plaintext_file &&
747                             !msginfo->decryption_failed) {
748                                 fclose(fp);
749                                 continue;
750                         }
751                 }
752                 
753                 break;
754         }
755 #else /* !USE_GPGME */
756         if ((fp = procmsg_open_message(msginfo)) == NULL) return;
757         mimeinfo = procmime_scan_mime_header(fp);
758 #endif /* USE_GPGME */
759
760         fclose(fp);
761         if (!mimeinfo) return;
762         if (mimeinfo->mime_type == MIME_TEXT)
763                 return;
764
765         if ((fp = procmsg_open_message(msginfo)) == NULL) return;
766
767         g_return_if_fail(mimeinfo != NULL);
768         g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
769
770         if (mimeinfo->mime_type == MIME_MULTIPART) {
771                 g_return_if_fail(mimeinfo->boundary != NULL);
772                 g_return_if_fail(mimeinfo->sub == NULL);
773         }
774         g_return_if_fail(fp != NULL);
775
776         boundary = mimeinfo->boundary;
777
778         if (boundary) {
779                 boundary_len = strlen(boundary);
780
781                 /* look for first boundary */
782                 while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
783                         if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
784                 if (!p) {
785                         fclose(fp);
786                         return;
787                 }
788         }
789
790         if ((fpos = ftell(fp)) < 0) {
791                 perror("ftell");
792                 fclose(fp);
793                 return;
794         }
795
796         for (npart = 0;; npart++) {
797                 MimeInfo *partinfo;
798                 gboolean eom = FALSE;
799
800                 prev_fpos = fpos;
801
802                 partinfo = procmime_scan_mime_header(fp);
803                 if (!partinfo) break;
804
805                 if (npart != 0)
806                         procmime_mimeinfo_insert(mimeinfo, partinfo);
807                 else
808                         procmime_mimeinfo_free(partinfo);
809
810                 /* look for next boundary */
811                 buf[0] = '\0';
812                 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
813                         if (IS_BOUNDARY(buf, boundary, boundary_len)) {
814                                 if (buf[2 + boundary_len]     == '-' &&
815                                     buf[2 + boundary_len + 1] == '-')
816                                         eom = TRUE;
817                                 break;
818                         }
819                 }
820                 if (p == NULL)
821                         eom = TRUE;     /* broken MIME message */
822                 fpos = ftell(fp);
823
824                 partinfo->size = fpos - prev_fpos - strlen(buf);
825
826                 if (eom) break;
827         }
828
829         source = procmsg_get_message_file_path(msginfo);
830
831         g_return_if_fail(mimeinfo != NULL);
832
833         if (!mimeinfo->main && mimeinfo->parent)
834                 {
835                         filename = mime_extract_file(source, mimeinfo);
836
837                         compose_attach_append_with_type(compose, filename,
838                                                         mimeinfo->content_type,
839                                                         mimeinfo->mime_type);
840
841                         g_free(filename);
842                 }
843
844         if (mimeinfo->sub && mimeinfo->sub->children)
845                 {
846                         filename = mime_extract_file(source, mimeinfo->sub);
847
848                         compose_attach_append_with_type(compose, filename,
849                                                         mimeinfo->content_type,
850                                                         mimeinfo->sub->mime_type);
851
852                         g_free(filename);
853                 }
854
855         if (mimeinfo->children) {
856                 MimeInfo *child;
857
858                 child = mimeinfo->children;
859                 while (child) {
860                         filename = mime_extract_file(source, child);
861
862                         compose_attach_append_with_type(compose, filename,
863                                                         child->content_type,
864                                                         child->mime_type);
865
866                         g_free(filename);
867
868                         child = child->next;
869                 }
870         }
871
872         fclose(fp);
873
874         procmime_mimeinfo_free_all(mimeinfo);
875 }
876
877
878 #define INSERT_FW_HEADER(var, hdr) \
879 if (msginfo->var && *msginfo->var) { \
880         gtk_stext_insert(text, NULL, NULL, NULL, hdr, -1); \
881         gtk_stext_insert(text, NULL, NULL, NULL, msginfo->var, -1); \
882         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1); \
883 }
884
885 Compose * compose_forward(PrefsAccount * account, MsgInfo *msginfo,
886                           gboolean as_attach)
887 {
888         Compose *compose;
889         /*      PrefsAccount *account; */
890         GtkSText *text;
891         FILE *fp;
892         gchar buf[BUFFSIZE];
893
894         g_return_val_if_fail(msginfo != NULL, NULL);
895         g_return_val_if_fail(msginfo->folder != NULL, NULL);
896
897         if (account == NULL) {
898                 account = cur_account;
899                 /*
900                 account = msginfo->folder->folder->account;
901                 if (!account) account = cur_account;
902                 */
903         }
904         g_return_val_if_fail(account != NULL, NULL);
905
906         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
907         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
908         CHANGE_FLAGS(msginfo);
909
910         compose = compose_create(account);
911         compose->mode = COMPOSE_FORWARD;
912
913         if (msginfo->subject && *msginfo->subject) {
914                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Fw: ");
915                 gtk_entry_append_text(GTK_ENTRY(compose->subject_entry),
916                                       msginfo->subject);
917         }
918
919         text = GTK_STEXT(compose->text);
920         gtk_stext_freeze(text);
921
922         if (as_attach) {
923                 gchar *msgfile;
924
925                 msgfile = procmsg_get_message_file_path(msginfo);
926                 if (!is_file_exist(msgfile))
927                         g_warning(_("%s: file not exist\n"), msgfile);
928                 else
929                         compose_attach_append(compose, msgfile,
930                                               MIME_MESSAGE_RFC822);
931
932                 g_free(msgfile);
933         } else {
934                 FILE *fp;
935                 gchar *quote_str;
936
937                 if ((fp = procmime_get_text_part(msginfo)) == NULL)
938                         g_warning(_("Can't get text part\n"));
939                 else {
940                         gchar * qmark;
941
942                         if (prefs_common.fw_quotemark &&
943                             *prefs_common.fw_quotemark)
944                                 qmark = prefs_common.fw_quotemark;
945                         else
946                                 qmark = "> ";
947
948                         quote_str = compose_quote_fmt(compose, msginfo,
949                                                       prefs_common.fw_quotefmt,
950                                                       qmark);
951
952                         if (quote_str != NULL)
953                                 gtk_stext_insert(text, NULL, NULL, NULL,
954                                                  quote_str, -1);
955
956                         fclose(fp);
957                 }
958
959                 compose_attach_parts(compose, msginfo);
960         }
961
962         if (prefs_common.auto_sig)
963                 compose_insert_sig(compose);
964         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
965         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
966
967         gtk_stext_thaw(text);
968         if (account->protocol != A_NNTP)
969                 gtk_widget_grab_focus(compose->to_entry);
970         else
971                 gtk_widget_grab_focus(compose->newsgroups_entry);
972
973         return compose;
974 }
975
976 #undef INSERT_FW_HEADER
977
978 Compose * compose_forward_multiple(PrefsAccount * account, 
979                           GSList *msginfo_list) 
980 {
981         Compose *compose;
982         GtkSText *text;
983         GSList *msginfo;
984         gchar *msgfile;
985
986         g_return_val_if_fail(msginfo_list != NULL, NULL);
987         
988         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
989                 if ( ((MsgInfo *)msginfo->data)->folder == NULL )
990                         return NULL;
991         }
992         
993         if (account == NULL) {
994                 account = cur_account;
995                 /*
996                 account = msginfo->folder->folder->account;
997                 if (!account) account = cur_account;
998                 */
999         }
1000         g_return_val_if_fail(account != NULL, NULL);
1001
1002         compose = compose_create(account);
1003         compose->mode = COMPOSE_FORWARD;
1004
1005         text = GTK_STEXT(compose->text);
1006         gtk_stext_freeze(text);
1007
1008         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1009                 msgfile = procmsg_get_message_file_path((MsgInfo *)msginfo->data);
1010                 if (!is_file_exist(msgfile))
1011                         g_warning(_("%s: file not exist\n"), msgfile);
1012                 else
1013                         compose_attach_append(compose, msgfile,
1014                                 MIME_MESSAGE_RFC822);
1015                 g_free(msgfile);
1016         }
1017
1018         if (prefs_common.auto_sig)
1019                 compose_insert_sig(compose);
1020         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
1021         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
1022
1023         gtk_stext_thaw(text);
1024         if (account->protocol != A_NNTP)
1025                 gtk_widget_grab_focus(compose->to_entry);
1026         else
1027                 gtk_widget_grab_focus(compose->newsgroups_entry);
1028
1029         return compose;
1030 }
1031
1032 void compose_reedit(MsgInfo *msginfo)
1033 {
1034         Compose *compose;
1035         PrefsAccount *account;
1036         GtkSText *text;
1037         FILE *fp;
1038         gchar buf[BUFFSIZE];
1039
1040         g_return_if_fail(msginfo != NULL);
1041         g_return_if_fail(msginfo->folder != NULL);
1042
1043         account = msginfo->folder->folder->account;
1044         if (!account) account = cur_account;
1045         g_return_if_fail(account != NULL);
1046
1047         compose = compose_create(account);
1048         compose->mode = COMPOSE_REEDIT_DRAFT;
1049         compose->targetinfo = procmsg_msginfo_copy(msginfo);
1050
1051         if (compose_parse_header(compose, msginfo) < 0) return;
1052         compose_reedit_set_entry(compose, msginfo);
1053
1054         text = GTK_STEXT(compose->text);
1055         gtk_stext_freeze(text);
1056
1057         if ((fp = procmime_get_text_part(msginfo)) == NULL)
1058                 g_warning(_("Can't get text part\n"));
1059         else {
1060                 while (fgets(buf, sizeof(buf), fp) != NULL)
1061                         gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1062                 fclose(fp);
1063         }
1064         compose_attach_parts(compose, msginfo);
1065
1066         gtk_stext_thaw(text);
1067         gtk_widget_grab_focus(compose->text);
1068 }
1069
1070 GList *compose_get_compose_list(void)
1071 {
1072         return compose_list;
1073 }
1074
1075 void compose_entry_append(Compose *compose, const gchar *address,
1076                           ComposeEntryType type)
1077 {
1078         GtkEntry *entry;
1079         const gchar *text;
1080
1081         if (!address || *address == '\0') return;
1082
1083         switch (type) {
1084         case COMPOSE_CC:
1085                 entry = GTK_ENTRY(compose->cc_entry);
1086                 break;
1087         case COMPOSE_BCC:
1088                 entry = GTK_ENTRY(compose->bcc_entry);
1089                 break;
1090         case COMPOSE_NEWSGROUPS:
1091                 entry = GTK_ENTRY(compose->newsgroups_entry);
1092                 break;
1093         case COMPOSE_TO:
1094         default:
1095                 entry = GTK_ENTRY(compose->to_entry);
1096                 break;
1097         }
1098
1099         text = gtk_entry_get_text(entry);
1100         if (*text != '\0')
1101                 gtk_entry_append_text(entry, ", ");
1102         gtk_entry_append_text(entry, address);
1103 }
1104
1105 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
1106 {
1107         static HeaderEntry hentry[] = {{"Reply-To:",    NULL, TRUE},
1108                                        {"Cc:",          NULL, FALSE},
1109                                        {"References:",  NULL, FALSE},
1110                                        {"Bcc:",         NULL, FALSE},
1111                                        {"Newsgroups:",  NULL, FALSE},
1112                                        {"Followup-To:", NULL, FALSE},
1113                                        {NULL,           NULL, FALSE}};
1114
1115         enum
1116         {
1117                 H_REPLY_TO      = 0,
1118                 H_CC            = 1,
1119                 H_REFERENCES    = 2,
1120                 H_BCC           = 3,
1121                 H_NEWSGROUPS    = 4,
1122                 H_FOLLOWUP_TO   = 5
1123         };
1124
1125         FILE *fp;
1126
1127         g_return_val_if_fail(msginfo != NULL, -1);
1128
1129         if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
1130         procheader_get_header_fields(fp, hentry);
1131         fclose(fp);
1132
1133         if (hentry[H_REPLY_TO].body != NULL) {
1134                 conv_unmime_header_overwrite(hentry[H_REPLY_TO].body);
1135                 compose->replyto = hentry[H_REPLY_TO].body;
1136                 hentry[H_REPLY_TO].body = NULL;
1137         }
1138         if (hentry[H_CC].body != NULL) {
1139                 conv_unmime_header_overwrite(hentry[H_CC].body);
1140                 compose->cc = hentry[H_CC].body;
1141                 hentry[H_CC].body = NULL;
1142         }
1143         if (hentry[H_REFERENCES].body != NULL) {
1144                 if (compose->mode == COMPOSE_REEDIT_DRAFT)
1145                         compose->references = hentry[H_REFERENCES].body;
1146                 else {
1147                         compose->references = compose_parse_references
1148                                 (hentry[H_REFERENCES].body, msginfo->msgid);
1149                         g_free(hentry[H_REFERENCES].body);
1150                 }
1151                 hentry[H_REFERENCES].body = NULL;
1152         }
1153         if (hentry[H_BCC].body != NULL) {
1154                 if (compose->mode == COMPOSE_REEDIT_DRAFT) {
1155                         conv_unmime_header_overwrite(hentry[H_BCC].body);
1156                         compose->bcc = hentry[H_BCC].body;
1157                 } else
1158                         g_free(hentry[H_BCC].body);
1159                 hentry[H_BCC].body = NULL;
1160         }
1161         if (hentry[H_NEWSGROUPS].body != NULL) {
1162                 compose->newsgroups = hentry[H_NEWSGROUPS].body;
1163                 hentry[H_NEWSGROUPS].body = NULL;
1164         }
1165         if (hentry[H_FOLLOWUP_TO].body != NULL) {
1166                 conv_unmime_header_overwrite(hentry[H_FOLLOWUP_TO].body);
1167                 compose->followup_to = hentry[H_FOLLOWUP_TO].body;
1168                 hentry[H_FOLLOWUP_TO].body = NULL;
1169         }
1170
1171         if (compose->mode == COMPOSE_REEDIT_DRAFT && msginfo->inreplyto)
1172                 compose->inreplyto = g_strdup(msginfo->inreplyto);
1173         else if (compose->mode != COMPOSE_REEDIT_DRAFT &&
1174                  msginfo->msgid && *msginfo->msgid) {
1175                 compose->inreplyto = g_strdup(msginfo->msgid);
1176
1177                 if (!compose->references) {
1178                         if (msginfo->inreplyto && *msginfo->inreplyto)
1179                                 compose->references =
1180                                         g_strdup_printf("<%s>\n\t<%s>",
1181                                                         msginfo->inreplyto,
1182                                                         msginfo->msgid);
1183                         else
1184                                 compose->references =
1185                                         g_strconcat("<", msginfo->msgid, ">",
1186                                                     NULL);
1187                 }
1188         }
1189
1190         return 0;
1191 }
1192
1193 static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
1194 {
1195         GSList *ref_id_list, *cur;
1196         GString *new_ref;
1197         gchar *new_ref_str;
1198
1199         ref_id_list = references_list_append(NULL, ref);
1200         if (!ref_id_list) return NULL;
1201         if (msgid && *msgid)
1202                 ref_id_list = g_slist_append(ref_id_list, g_strdup(msgid));
1203
1204         for (;;) {
1205                 gint len = 0;
1206
1207                 for (cur = ref_id_list; cur != NULL; cur = cur->next)
1208                         /* "<" + Message-ID + ">" + CR+LF+TAB */
1209                         len += strlen((gchar *)cur->data) + 5;
1210
1211                 if (len > MAX_REFERENCES_LEN) {
1212                         /* remove second message-ID */
1213                         if (ref_id_list && ref_id_list->next &&
1214                             ref_id_list->next->next) {
1215                                 g_free(ref_id_list->next->data);
1216                                 ref_id_list = g_slist_remove
1217                                         (ref_id_list, ref_id_list->next->data);
1218                         } else {
1219                                 slist_free_strings(ref_id_list);
1220                                 g_slist_free(ref_id_list);
1221                                 return NULL;
1222                         }
1223                 } else
1224                         break;
1225         }
1226
1227         new_ref = g_string_new("");
1228         for (cur = ref_id_list; cur != NULL; cur = cur->next) {
1229                 if (new_ref->len > 0)
1230                         g_string_append(new_ref, "\n\t");
1231                 g_string_sprintfa(new_ref, "<%s>", (gchar *)cur->data);
1232         }
1233
1234         slist_free_strings(ref_id_list);
1235         g_slist_free(ref_id_list);
1236
1237         new_ref_str = new_ref->str;
1238         g_string_free(new_ref, FALSE);
1239
1240         return new_ref_str;
1241 }
1242
1243 /*
1244 static void compose_quote_file(Compose *compose, MsgInfo *msginfo, FILE *fp)
1245 {
1246         GtkSText *text = GTK_STEXT(compose->text);
1247         gchar *qmark;
1248         gchar *quote_str;
1249         GdkColor *qcolor = NULL;
1250         gchar buf[BUFFSIZE];
1251         gint qlen;
1252         gchar *linep, *cur, *leftp;
1253         gint line_len, cur_len;
1254         gint wrap_len;
1255         gint str_len;
1256         gint ch_len;
1257
1258         // if (prefs_common.enable_color) qcolor = &quote_color;
1259         if (prefs_common.quotemark && *prefs_common.quotemark)
1260                 qmark = prefs_common.quotemark;
1261         else
1262                 qmark = "> ";
1263         quote_str = compose_quote_parse_fmt(compose, msginfo, qmark);
1264         g_return_if_fail(quote_str != NULL);
1265         qlen = strlen(quote_str);
1266
1267         if (!prefs_common.linewrap_quote ||
1268             prefs_common.linewrap_len <= qlen) {
1269                 while (fgets(buf, sizeof(buf), fp) != NULL) {
1270                         gtk_stext_insert(text, NULL, qcolor, NULL,
1271                                         quote_str, -1);
1272                         gtk_stext_insert(text, NULL, qcolor, NULL, buf, -1);
1273                 }
1274                 g_free(quote_str);
1275                 return;
1276         }
1277
1278         wrap_len = prefs_common.linewrap_len - qlen;
1279
1280         while (fgets(buf, sizeof(buf), fp) != NULL) {
1281                 strretchomp(buf);
1282                 str_len = strlen(buf);
1283
1284                 if (str_len <= wrap_len) {
1285                         gtk_stext_insert(text, NULL, qcolor, NULL,
1286                                         quote_str, -1);
1287                         gtk_stext_insert(text, NULL, qcolor, NULL, buf, -1);
1288                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
1289                         continue;
1290                 }
1291
1292                 linep = cur = leftp = buf;
1293                 line_len = cur_len = 0;
1294
1295                 while (*cur != '\0') {
1296                         ch_len = mblen(cur, MB_CUR_MAX);
1297                         if (ch_len < 0) ch_len = 1;
1298
1299                         if (ch_len == 1 && isspace(*cur)) {
1300                                 linep = cur + ch_len;
1301                                 line_len = cur_len + ch_len;
1302                         }
1303
1304                         if (cur_len + ch_len > wrap_len && line_len > 0) {
1305                                 gtk_stext_insert(text, NULL, qcolor, NULL,
1306                                                 quote_str, -1);
1307
1308                                 if (isspace(*(linep - 1)))
1309                                         gtk_stext_insert(text, NULL,
1310                                                         qcolor, NULL,
1311                                                         leftp, line_len - 1);
1312                                 else
1313                                         gtk_stext_insert(text, NULL,
1314                                                         qcolor, NULL,
1315                                                         leftp, line_len);
1316                                 gtk_stext_insert(text, NULL, NULL, NULL,
1317                                                 "\n", 1);
1318
1319                                 leftp = linep;
1320                                 cur_len = cur_len - line_len + ch_len;
1321                                 line_len = 0;
1322                                 cur += ch_len;
1323                                 continue;
1324                         }
1325
1326                         if (ch_len > 1) {
1327                                 linep = cur + ch_len;
1328                                 line_len = cur_len + ch_len;
1329                         }
1330                         cur_len += ch_len;
1331                         cur += ch_len;
1332                 }
1333
1334                 if (*leftp) {
1335                         gtk_stext_insert(text, NULL, qcolor, NULL,
1336                                         quote_str, -1);
1337                         gtk_stext_insert(text, NULL, qcolor, NULL, leftp, -1);
1338                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
1339                 }
1340         }
1341
1342         g_free(quote_str);
1343 }
1344 */
1345
1346 /*
1347 static gchar *compose_quote_parse_fmt(Compose *compose, MsgInfo *msginfo,
1348                                       const gchar *fmt)
1349 {
1350         gchar *ext_str;
1351         size_t buf_len = 1024;
1352         size_t ext_len = 0;
1353         gchar *str;
1354         gchar *mbs;
1355         wchar_t *wcsfmt;
1356         wchar_t *sp;
1357         gchar tmp[3];
1358
1359         if (!fmt || *fmt == '\0') return 0;
1360
1361         Xalloca(mbs, sizeof(wchar_t) + 1, return 0);
1362         Xalloca(wcsfmt, (strlen(fmt) + 1) * sizeof(wchar_t), return 0);
1363         mbstowcs(wcsfmt, fmt, strlen(fmt) + 1);
1364         sp = wcsfmt;
1365
1366         ext_str = g_malloc(sizeof(gchar) * buf_len);
1367         g_return_val_if_fail(ext_str != NULL, NULL);
1368
1369         while (*sp) {
1370                 gint len;
1371
1372                 len = wctomb(mbs, *sp);
1373                 mbs[len] = '\0';
1374
1375                 if (*mbs == '%') {
1376                         gchar *p;
1377
1378                         wctomb(mbs, *(++sp));
1379                         str = NULL;
1380
1381                         switch (*mbs) {
1382                         case 'd':
1383                                 str = msginfo->date;
1384                                 sp++;
1385                                 break;
1386                         case 'f':
1387                                 str = msginfo->from;
1388                                 sp++;
1389                                 break;
1390                         case 'I':
1391                                 if (!msginfo->fromname) {sp++; break;}
1392                                 p = msginfo->fromname;
1393                                 tmp[0] = tmp[1] = tmp[2] = '\0';
1394
1395                                 if (*p && isalnum(*p))
1396                                         tmp[0] = toupper(*p);
1397                                 else {
1398                                         sp++;
1399                                         break;
1400                                 }
1401
1402                                 while (*p) {
1403                                         while (*p && !isspace(*p)) p++;
1404                                         while (*p && isspace(*p)) p++;
1405                                         if (*p && isalnum(*p))
1406                                                 tmp[1] = toupper(*p);
1407                                 }
1408
1409                                 if (tmp[1]) str = tmp;
1410                                 sp++;
1411                                 break;
1412                         case 'n':
1413                                 str = msginfo->fromname;
1414                                 sp++;
1415                                 break;
1416                         case 'N':
1417                                 if (!msginfo->fromname) {sp++; break;}
1418                                 Xstrdup_a(str, msginfo->fromname,
1419                                           {sp++; break;});
1420                                 p = str;
1421                                 while (*p && !isspace(*p)) p++;
1422                                 *p = '\0';
1423                                 sp++;
1424                                 break;
1425                         case 's':
1426                                 str = msginfo->subject;
1427                                 sp++;
1428                                 break;
1429                         case 't':
1430                                 str = msginfo->to;
1431                                 sp++;
1432                                 break;
1433                         case 'c':
1434                                 str = msginfo->cc;
1435                                 sp++;
1436                                 break;
1437                         case 'i':
1438                                 if (!msginfo->msgid) {sp++; break;}
1439                                 Xalloca(str, strlen(msginfo->msgid) + 3,
1440                                         {sp++; break;});
1441                                 g_snprintf(str, strlen(msginfo->msgid) + 3,
1442                                            "<%s>", msginfo->msgid);
1443                                 sp++;
1444                                 break;
1445                         case '%':
1446                                 str = "%";
1447                                 sp++;
1448                                 break;
1449                         default:
1450                                 break;
1451                         }
1452
1453                         if (str) {
1454                                 while (ext_len + strlen(str) + 1 > buf_len)
1455                                         buf_len += 1024;
1456                                 ext_str = g_realloc(ext_str,
1457                                                     sizeof(gchar) * buf_len);
1458                                 g_return_val_if_fail(ext_str != NULL, NULL);
1459                                 strcpy(ext_str + ext_len, str);
1460                                 ext_len += strlen(str);
1461                         }
1462                 } else if (*mbs == '\\') {
1463                         wctomb(mbs, *(++sp));
1464                         str = NULL;
1465
1466                         switch (*mbs) {
1467                         case 'n':
1468                                 str = "\n";
1469                                 break;
1470                         case 't':
1471                                 str = "\t";
1472                                 break;
1473                         case '\\':
1474                                 str = "\\";
1475                                 break;
1476                         default:
1477                                 break;
1478                         }
1479
1480                         if (str) {
1481                                 while (ext_len + strlen(str) + 1 > buf_len)
1482                                         buf_len += 1024;
1483                                 ext_str = g_realloc(ext_str,
1484                                                     sizeof(gchar) * buf_len);
1485                                 g_return_val_if_fail(ext_str != NULL, NULL);
1486                                 strcpy(ext_str + ext_len, str);
1487                                 ext_len += strlen(str);
1488                                 sp++;
1489                         }
1490                 } else {
1491                         while (ext_len + len + 1 > buf_len) buf_len += 1024;
1492                         ext_str = g_realloc(ext_str, sizeof(gchar) * buf_len);
1493                         g_return_val_if_fail(ext_str != NULL, NULL);
1494                         strcpy(ext_str + ext_len, mbs);
1495                         ext_len += len;
1496                         sp++;
1497                 }
1498         }
1499
1500         if (ext_str)
1501                 ext_str = g_realloc(ext_str, strlen(ext_str) + 1);
1502
1503         return ext_str;
1504 }
1505 */
1506
1507 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
1508                                     gboolean to_all, gboolean ignore_replyto,
1509                                     gboolean followup_and_reply_to)
1510 {
1511         GSList *cc_list;
1512         GSList *cur;
1513         gchar *from;
1514         GHashTable *to_table;
1515
1516         g_return_if_fail(compose->account != NULL);
1517         g_return_if_fail(msginfo != NULL);
1518
1519         if ((compose->account->protocol != A_NNTP) || followup_and_reply_to)
1520                 gtk_entry_set_text(GTK_ENTRY(compose->to_entry),
1521                                    ( (compose->replyto && !ignore_replyto) 
1522                                      ? compose->replyto
1523                                      : msginfo->from ? msginfo->from : ""));
1524         if (compose->account->protocol == A_NNTP)
1525                 gtk_entry_set_text(GTK_ENTRY(compose->newsgroups_entry),
1526                                    compose->followup_to ? compose->followup_to
1527                                    : compose->newsgroups ? compose->newsgroups
1528                                    : "");
1529
1530         if (msginfo->subject && *msginfo->subject) {
1531                 gchar *buf, *buf2, *p;
1532
1533                 buf = g_strdup(msginfo->subject);
1534                 while (!strncasecmp(buf, "Re:", 3)) {
1535                         p = buf + 3;
1536                         while (isspace(*p)) p++;
1537                         memmove(buf, p, strlen(p) + 1);
1538                 }
1539
1540                 buf2 = g_strdup_printf("Re: %s", buf);
1541                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1542                 g_free(buf2);
1543                 g_free(buf);
1544         } else
1545                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
1546
1547         if (!to_all || compose->account->protocol == A_NNTP) return;
1548
1549         from = g_strdup(compose->replyto ? compose->replyto :
1550                         msginfo->from ? msginfo->from : "");
1551         extract_address(from);
1552
1553         cc_list = address_list_append(NULL, msginfo->to);
1554         cc_list = address_list_append(cc_list, compose->cc);
1555
1556         to_table = g_hash_table_new(g_str_hash, g_str_equal);
1557         g_hash_table_insert(to_table, from, GINT_TO_POINTER(1));
1558         if (compose->account)
1559                 g_hash_table_insert(to_table, compose->account->address,
1560                                     GINT_TO_POINTER(1));
1561
1562         /* remove address on To: and that of current account */
1563         for (cur = cc_list; cur != NULL; ) {
1564                 GSList *next = cur->next;
1565
1566                 if (g_hash_table_lookup(to_table, cur->data) != NULL)
1567                         cc_list = g_slist_remove(cc_list, cur->data);
1568                 else
1569                         g_hash_table_insert(to_table, cur->data, cur);
1570
1571                 cur = next;
1572         }
1573         g_hash_table_destroy(to_table);
1574         g_free(from);
1575
1576         if (cc_list) {
1577                 for (cur = cc_list; cur != NULL; cur = cur->next)
1578                         compose_entry_append(compose, (gchar *)cur->data,
1579                                              COMPOSE_CC);
1580                 slist_free_strings(cc_list);
1581                 g_slist_free(cc_list);
1582         }
1583 }
1584
1585 #define SET_ENTRY(entry, str) \
1586 { \
1587         if (str && *str) \
1588                 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
1589 }
1590
1591 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
1592 {
1593         g_return_if_fail(msginfo != NULL);
1594
1595         SET_ENTRY(to_entry, msginfo->to);
1596         SET_ENTRY(subject_entry, msginfo->subject);
1597         SET_ENTRY(cc_entry, compose->cc);
1598         SET_ENTRY(bcc_entry, compose->bcc);
1599         SET_ENTRY(reply_entry, compose->replyto);
1600
1601         if (compose->bcc) {
1602                 GtkItemFactory *ifactory;
1603                 GtkWidget *menuitem;
1604
1605                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1606                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Bcc");
1607                 gtk_check_menu_item_set_active
1608                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1609         }
1610         if (compose->replyto) {
1611                 GtkItemFactory *ifactory;
1612                 GtkWidget *menuitem;
1613
1614                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1615                 menuitem = gtk_item_factory_get_item
1616                         (ifactory, "/Message/Reply to");
1617                 gtk_check_menu_item_set_active
1618                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1619         }
1620 }
1621
1622 #undef SET_ENTRY
1623
1624 static void compose_insert_sig(Compose *compose)
1625 {
1626         gchar *sigfile;
1627
1628         if (compose->account && compose->account->sig_path)
1629                 sigfile = g_strdup(compose->account->sig_path);
1630         else {
1631                 sigfile = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1632                                       DEFAULT_SIGNATURE, NULL);
1633         }
1634
1635         if (!is_file_or_fifo_exist(sigfile)) {
1636                 g_free(sigfile);
1637                 return;
1638         }
1639
1640         gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, "\n\n", 2);
1641         if (prefs_common.sig_sep) {
1642                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1643                                 prefs_common.sig_sep, -1);
1644                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1645                                 "\n", 1);
1646         }
1647
1648         compose_insert_file(compose, sigfile);
1649         g_free(sigfile);
1650 }
1651
1652 static void compose_insert_file(Compose *compose, const gchar *file)
1653 {
1654         GtkSText *text = GTK_STEXT(compose->text);
1655         gchar buf[BUFFSIZE];
1656         FILE *fp;
1657
1658         g_return_if_fail(file != NULL);
1659
1660         if ((fp = fopen(file, "r")) == NULL) {
1661                 FILE_OP_ERROR(file, "fopen");
1662                 return;
1663         }
1664
1665         gtk_stext_freeze(text);
1666
1667         while (fgets(buf, sizeof(buf), fp) != NULL)
1668                 gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1669
1670         gtk_stext_thaw(text);
1671
1672         fclose(fp);
1673 }
1674
1675 static void compose_attach_info(Compose * compose, AttachInfo * ainfo,
1676                                 ContentType cnttype)
1677 {
1678         gchar *text[N_ATTACH_COLS];
1679         gint row;
1680
1681         text[COL_MIMETYPE] = ainfo->content_type;
1682         text[COL_SIZE] = to_human_readable(ainfo->size);
1683         text[COL_NAME] = ainfo->name;
1684
1685         row = gtk_clist_append(GTK_CLIST(compose->attach_clist), text);
1686         gtk_clist_set_row_data(GTK_CLIST(compose->attach_clist), row, ainfo);
1687
1688         if (cnttype != MIME_MESSAGE_RFC822)
1689                 compose_changed_cb(NULL, compose);
1690 }
1691
1692 static void compose_attach_append_with_type(Compose *compose,
1693                                             const gchar *file,
1694                                             const gchar *type,
1695                                             ContentType cnttype)
1696 {
1697         AttachInfo *ainfo;
1698         off_t size;
1699
1700         if (!is_file_exist(file)) {
1701                 g_warning(_("File %s doesn't exist\n"), file);
1702                 return;
1703         }
1704         if ((size = get_file_size(file)) < 0) {
1705                 g_warning(_("Can't get file size of %s\n"), file);
1706                 return;
1707         }
1708         if (size == 0) {
1709                 alertpanel_notice(_("File %s is empty\n"), file);
1710                 return;
1711         }
1712
1713         if (!compose->use_attach) {
1714                 GtkItemFactory *ifactory;
1715                 GtkWidget *menuitem;
1716
1717                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1718                 menuitem = gtk_item_factory_get_item(ifactory,
1719                                                      "/Message/Attach");
1720                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1721                                                TRUE);
1722         }
1723
1724         ainfo = g_new0(AttachInfo, 1);
1725         ainfo->file = g_strdup(file);
1726
1727         if (cnttype == MIME_MESSAGE_RFC822) {
1728                 ainfo->encoding = ENC_7BIT;
1729                 ainfo->name = g_strdup_printf(_("Message: %s"),
1730                                               g_basename(file));
1731         } else {
1732                 ainfo->encoding = ENC_BASE64;
1733                 ainfo->name = g_strdup(g_basename(file));
1734         }
1735
1736         ainfo->content_type = g_strdup(type);
1737         ainfo->size = size;
1738
1739         compose_attach_info(compose, ainfo, cnttype);
1740 }
1741
1742 static void compose_attach_append(Compose *compose, const gchar *file,
1743                                   ContentType cnttype)
1744 {
1745         AttachInfo *ainfo;
1746         gchar *text[N_ATTACH_COLS];
1747         off_t size;
1748         gint row;
1749
1750         if (!is_file_exist(file)) {
1751                 g_warning(_("File %s doesn't exist\n"), file);
1752                 return;
1753         }
1754         if ((size = get_file_size(file)) < 0) {
1755                 g_warning(_("Can't get file size of %s\n"), file);
1756                 return;
1757         }
1758         if (size == 0) {
1759                 alertpanel_notice(_("File %s is empty\n"), file);
1760                 return;
1761         }
1762
1763         if (!compose->use_attach) {
1764                 GtkItemFactory *ifactory;
1765                 GtkWidget *menuitem;
1766
1767                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1768                 menuitem = gtk_item_factory_get_item(ifactory,
1769                                                      "/Message/Attach");
1770                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1771                                                TRUE);
1772         }
1773
1774         ainfo = g_new0(AttachInfo, 1);
1775         ainfo->file = g_strdup(file);
1776
1777         if (cnttype == MIME_MESSAGE_RFC822) {
1778                 ainfo->content_type = g_strdup("message/rfc822");
1779                 ainfo->encoding = ENC_7BIT;
1780                 ainfo->name = g_strdup_printf(_("Message: %s"),
1781                                               g_basename(file));
1782         } else {
1783                 ainfo->content_type = procmime_get_mime_type(file);
1784                 if (!ainfo->content_type)
1785                         ainfo->content_type =
1786                                 g_strdup("application/octet-stream");
1787                 ainfo->encoding = ENC_BASE64;
1788                 ainfo->name = g_strdup(g_basename(file));
1789         }
1790         ainfo->size = size;
1791
1792         compose_attach_info(compose, ainfo, cnttype);
1793 }
1794
1795 static void compose_wrap_line(Compose *compose)
1796 {
1797         GtkSText *text = GTK_STEXT(compose->text);
1798         guint text_len;
1799         guint line_pos = 0, cur_pos = 0;
1800         gint line_len = 0, cur_len = 0;
1801         gint ch_len;
1802         gchar cbuf[MB_CUR_MAX];
1803
1804         gtk_stext_freeze(text);
1805
1806         text_len = gtk_stext_get_length(text);
1807
1808         for (; cur_pos < text_len; cur_pos++) {
1809                 if (text->use_wchar)
1810                         ch_len = wctomb
1811                                 (cbuf, (wchar_t)GTK_STEXT_INDEX(text, cur_pos));
1812                 else {
1813                         cbuf[0] = GTK_STEXT_INDEX(text, cur_pos);
1814                         ch_len = 1;
1815                 }
1816
1817                 if (ch_len == 1 && *cbuf == '\n') {
1818                         line_pos = cur_pos + 1;
1819                         line_len = cur_len = 0;
1820                         continue;
1821                 }
1822
1823                 if (ch_len < 0) {
1824                         cbuf[0] = '\0';
1825                         ch_len = 1;
1826                 }
1827
1828                 if (ch_len == 1 && isspace(*cbuf)) {
1829                         line_pos = cur_pos + 1;
1830                         line_len = cur_len + ch_len;
1831                 }
1832
1833                 if (cur_len + ch_len > prefs_common.linewrap_len &&
1834                     line_len > 0) {
1835                         gint tlen;
1836
1837                         if (text->use_wchar)
1838                                 tlen = wctomb(cbuf, (wchar_t)GTK_STEXT_INDEX(text, line_pos - 1));
1839                         else {
1840                                 cbuf[0] = GTK_STEXT_INDEX(text, line_pos - 1);
1841                                 tlen = 1;
1842                         }
1843                         if (tlen == 1 && isspace(*cbuf)) {
1844                                 gtk_stext_set_point(text, line_pos);
1845                                 gtk_stext_backward_delete(text, 1);
1846                                 text_len--;
1847                                 cur_pos--;
1848                                 line_pos--;
1849                                 cur_len--;
1850                                 line_len--;
1851                         }
1852
1853                         gtk_stext_set_point(text, line_pos);
1854                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
1855                         text_len++;
1856                         cur_pos++;
1857                         line_pos++;
1858                         cur_len = cur_len - line_len + ch_len;
1859                         line_len = 0;
1860                         continue;
1861                 }
1862
1863                 if (ch_len > 1) {
1864                         line_pos = cur_pos + 1;
1865                         line_len = cur_len + ch_len;
1866                 }
1867                 cur_len += ch_len;
1868         }
1869
1870         gtk_stext_thaw(text);
1871 }
1872
1873 static void compose_set_title(Compose *compose)
1874 {
1875         gchar *str;
1876         gchar *edited;
1877
1878         edited = compose->modified ? _(" [Edited]") : "";
1879         if (compose->account && compose->account->address)
1880                 str = g_strdup_printf(_("%s - Compose message%s"),
1881                                       compose->account->address, edited);
1882         else
1883                 str = g_strdup_printf(_("Compose message%s"), edited);
1884         gtk_window_set_title(GTK_WINDOW(compose->window), str);
1885         g_free(str);
1886 }
1887
1888 /**
1889  * compose_current_mail_account:
1890  * 
1891  * Find a current mail account (the currently selected account, or the
1892  * default account, if a news account is currently selected).  If a
1893  * mail account cannot be found, display an error message.
1894  * 
1895  * Return value: Mail account, or NULL if not found.
1896  **/
1897 static PrefsAccount *
1898 compose_current_mail_account(void)
1899 {
1900         PrefsAccount *ac;
1901
1902         if (cur_account && cur_account->protocol != A_NNTP)
1903                 ac = cur_account;
1904         else {
1905                 ac = account_get_default();
1906                 if (!ac || ac->protocol == A_NNTP) {
1907                         alertpanel_error(_("Account for sending mail is not specified.\n"
1908                                            "Please select a mail account before sending."));
1909                         return NULL;
1910                 }
1911         }
1912         return ac;
1913 }
1914
1915 gint compose_send(Compose *compose)
1916 {
1917         gchar tmp[MAXPATHLEN + 1];
1918         gchar *to, *newsgroups;
1919         gint ok = 0;
1920         static gboolean lock = FALSE;
1921
1922         if (lock) return 1;
1923
1924         g_return_val_if_fail(compose->account != NULL, -1);
1925         g_return_val_if_fail(compose->orig_account != NULL, -1);
1926
1927         lock = TRUE;
1928
1929         to = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
1930         newsgroups = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
1931         if (*to == '\0' && *newsgroups == '\0') {
1932                 alertpanel_error(_("Recipient is not specified."));
1933                 lock = FALSE;
1934                 return 1;
1935         }
1936
1937         /* write to temporary file */
1938         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
1939                    get_rc_dir(), G_DIR_SEPARATOR, (gint)compose);
1940
1941         if (prefs_common.linewrap_at_send)
1942                 compose_wrap_line(compose);
1943
1944         if (compose_write_to_file(compose, tmp, FALSE) < 0) {
1945                 lock = FALSE;
1946                 return -1;
1947         }
1948
1949         if (!compose->to_list && !compose->newsgroup_list) {
1950                 g_warning(_("can't get recipient list."));
1951                 unlink(tmp);
1952                 lock = FALSE;
1953                 return -1;
1954         }
1955
1956         if (compose->to_list) {
1957                 PrefsAccount *ac;
1958
1959                 /*
1960                 if (compose->account->protocol != A_NNTP)
1961                         ac = compose->account;
1962                 else if (compose->orig_account->protocol != A_NNTP)
1963                         ac = compose->orig_account;
1964                 else if (cur_account && cur_account->protocol != A_NNTP)
1965                         ac = cur_account;
1966                 else {
1967                         ac = compose_current_mail_account();
1968                         if (!ac) {
1969                                 unlink(tmp);
1970                                 lock = FALSE;
1971                                 return -1;
1972                         }
1973                 }
1974                 */
1975                 ac = compose->account;
1976
1977                 ok = send_message(tmp, ac, compose->to_list);
1978                 statusbar_pop_all();
1979         }
1980
1981         if (ok == 0 && compose->newsgroup_list) {
1982                 Folder *folder;
1983
1984                 if (compose->account->protocol == A_NNTP)
1985                         folder = FOLDER(compose->account->folder);
1986                 else
1987                         folder = FOLDER(compose->orig_account->folder);
1988
1989                 ok = news_post(folder, tmp);
1990                 if (ok < 0) {
1991                         alertpanel_error(_("Error occurred while posting the message to %s ."),
1992                                          compose->account->nntp_server);
1993                         unlink(tmp);
1994                         lock = FALSE;
1995                         return -1;
1996                 }
1997         }
1998
1999         /* queue message if failed to send */
2000         if (ok < 0) {
2001                 if (prefs_common.queue_msg) {
2002                         AlertValue val;
2003
2004                         val = alertpanel
2005                                 (_("Queueing"),
2006                                  _("Error occurred while sending the message.\n"
2007                                    "Put this message into queue folder?"),
2008                                  _("OK"), _("Cancel"), NULL);
2009                         if (G_ALERTDEFAULT == val) {
2010                                 ok = compose_queue(compose, tmp);
2011                                 if (ok < 0)
2012                                         alertpanel_error(_("Can't queue the message."));
2013                         }
2014                 } else
2015                         alertpanel_error(_("Error occurred while sending the message."));
2016         }
2017
2018         /* save message to outbox */
2019         if (ok == 0 && prefs_common.savemsg) {
2020                 if (compose_save_to_outbox(compose, tmp) < 0)
2021                         alertpanel_error
2022                                 (_("Can't save the message to outbox."));
2023         }
2024
2025         unlink(tmp);
2026         lock = FALSE;
2027         return ok;
2028 }
2029
2030 static gint compose_write_to_file(Compose *compose, const gchar *file,
2031                                   gboolean is_draft)
2032 {
2033         FILE *fp;
2034         size_t len;
2035         gchar *chars;
2036         gchar *buf;
2037         const gchar *out_codeset;
2038         EncodingType encoding;
2039
2040         if ((fp = fopen(file, "w")) == NULL) {
2041                 FILE_OP_ERROR(file, "fopen");
2042                 return -1;
2043         }
2044
2045         /* chmod for security */
2046         if (change_file_mode_rw(fp, file) < 0) {
2047                 FILE_OP_ERROR(file, "chmod");
2048                 g_warning(_("can't change file mode\n"));
2049         }
2050
2051         /* get all composed text */
2052         chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
2053         len = strlen(chars);
2054         if (is_ascii_str(chars)) {
2055                 buf = g_strdup(chars);
2056                 out_codeset = "US-ASCII";
2057                 encoding = ENC_7BIT;
2058         } else {
2059                 const gchar *src_codeset;
2060
2061                 out_codeset = conv_get_outgoing_charset_str();
2062                 if (!strcasecmp(out_codeset, "US-ASCII"))
2063                         out_codeset = "ISO-8859-1";
2064                 encoding = procmime_get_encoding_for_charset(out_codeset);
2065                 debug_print("charset = %s, encoding = %s\n",
2066                             out_codeset, procmime_get_encoding_str(encoding));
2067
2068                 src_codeset = conv_get_current_charset_str();
2069                 buf = conv_codeset_strdup(chars, src_codeset, out_codeset);
2070                 if (!buf) {
2071                         g_free(chars);
2072                         fclose(fp);
2073                         unlink(file);
2074                         alertpanel_error(_("Can't convert the codeset of the message."));
2075                         return -1;
2076                 }
2077         }
2078         g_free(chars);
2079
2080         /* write headers */
2081         if (compose_write_headers
2082                 (compose, fp, out_codeset, encoding, is_draft) < 0) {
2083                 g_warning(_("can't write headers\n"));
2084                 fclose(fp);
2085                 /* unlink(file); */
2086                 g_free(buf);
2087                 return -1;
2088         }
2089
2090         if (compose->use_attach) {
2091 #if USE_GPGME
2092             /* This prolog message is ignored by mime software and
2093              * because it would make our signing/encryption task
2094              * tougher, we don't emit it in that case */
2095             if (!compose->use_signing && !compose->use_encryption)
2096 #endif
2097                 fputs("This is a multi-part message in MIME format.\n", fp);
2098
2099                 fprintf(fp, "\n--%s\n", compose->boundary);
2100                 fprintf(fp, "Content-Type: text/plain; charset=%s\n",
2101                         out_codeset);
2102                 fprintf(fp, "Content-Transfer-Encoding: %s\n",
2103                         procmime_get_encoding_str(encoding));
2104                 fputc('\n', fp);
2105         }
2106
2107         /* write body */
2108         len = strlen(buf);
2109         if (encoding == ENC_BASE64) {
2110                 gchar outbuf[B64_BUFFSIZE];
2111                 gint i, l;
2112
2113                 for (i = 0; i < len; i += B64_LINE_SIZE) {
2114                         l = MIN(B64_LINE_SIZE, len - i);
2115                         to64frombits(outbuf, buf + i, l);
2116                         fputs(outbuf, fp);
2117                         fputc('\n', fp);
2118                 }
2119         } else if (fwrite(buf, sizeof(gchar), len, fp) != len) {
2120                 FILE_OP_ERROR(file, "fwrite");
2121                 fclose(fp);
2122                 unlink(file);
2123                 g_free(buf);
2124                 return -1;
2125         }
2126         g_free(buf);
2127
2128         if (compose->use_attach)
2129                 compose_write_attach(compose, fp);
2130
2131         if (fclose(fp) == EOF) {
2132                 FILE_OP_ERROR(file, "fclose");
2133                 unlink(file);
2134                 return -1;
2135         }
2136
2137 #if USE_GPGME
2138         if (compose->use_signing) {
2139                 if (rfc2015_sign(file, compose->account) < 0) {
2140                         unlink(file);
2141                         return -1;
2142                 }
2143         }
2144         if (compose->use_encryption) {
2145                 if (rfc2015_encrypt(file, compose->to_list) < 0) {
2146                         unlink(file);
2147                         return -1;
2148                 }
2149         }
2150 #endif /* USE_GPGME */
2151
2152         return 0;
2153 }
2154
2155 static gint compose_write_body_to_file(Compose *compose, const gchar *file)
2156 {
2157         FILE *fp;
2158         size_t len;
2159         gchar *chars;
2160
2161         if ((fp = fopen(file, "w")) == NULL) {
2162                 FILE_OP_ERROR(file, "fopen");
2163                 return -1;
2164         }
2165
2166         /* chmod for security */
2167         if (change_file_mode_rw(fp, file) < 0) {
2168                 FILE_OP_ERROR(file, "chmod");
2169                 g_warning(_("can't change file mode\n"));
2170         }
2171
2172         chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
2173
2174         /* write body */
2175         len = strlen(chars);
2176         if (fwrite(chars, sizeof(gchar), len, fp) != len) {
2177                 FILE_OP_ERROR(file, "fwrite");
2178                 g_free(chars);
2179                 fclose(fp);
2180                 unlink(file);
2181                 return -1;
2182         }
2183
2184         g_free(chars);
2185
2186         if (fclose(fp) == EOF) {
2187                 FILE_OP_ERROR(file, "fclose");
2188                 unlink(file);
2189                 return -1;
2190         }
2191         return 0;
2192 }
2193
2194 static gint compose_save_to_outbox(Compose *compose, const gchar *file)
2195 {
2196         FolderItem *outbox;
2197         gchar *path;
2198         gint num;
2199         FILE *fp;
2200
2201         debug_print(_("saving sent message...\n"));
2202
2203         outbox = folder_get_default_outbox();
2204         path = folder_item_get_path(outbox);
2205         if (!is_dir_exist(path))
2206                 make_dir_hier(path);
2207
2208         folder_item_scan(outbox);
2209         if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
2210                 g_free(path);
2211                 g_warning(_("can't save message\n"));
2212                 return -1;
2213         }
2214
2215         if ((fp = procmsg_open_mark_file(path, TRUE)) == NULL)
2216                 g_warning(_("can't open mark file\n"));
2217         else {
2218                 MsgInfo newmsginfo;
2219
2220                 newmsginfo.msgnum = num;
2221                 newmsginfo.flags.perm_flags = 0;
2222                 newmsginfo.flags.tmp_flags = 0;
2223                 procmsg_write_flags(&newmsginfo, fp);
2224                 fclose(fp);
2225         }
2226         g_free(path);
2227
2228         return 0;
2229 }
2230
2231 static gint compose_queue(Compose *compose, const gchar *file)
2232 {
2233         FolderItem *queue;
2234         gchar *tmp, *queue_path;
2235         FILE *fp, *src_fp;
2236         GSList *cur;
2237         gchar buf[BUFFSIZE];
2238         gint num;
2239
2240         debug_print(_("queueing message...\n"));
2241         g_return_val_if_fail(compose->to_list != NULL, -1);
2242         g_return_val_if_fail(compose->account != NULL, -1);
2243
2244         tmp = g_strdup_printf("%s%cqueue.%d", g_get_tmp_dir(),
2245                               G_DIR_SEPARATOR, (gint)compose);
2246         if ((fp = fopen(tmp, "w")) == NULL) {
2247                 FILE_OP_ERROR(tmp, "fopen");
2248                 g_free(tmp);
2249                 return -1;
2250         }
2251         if ((src_fp = fopen(file, "r")) == NULL) {
2252                 FILE_OP_ERROR(file, "fopen");
2253                 fclose(fp);
2254                 unlink(tmp);
2255                 g_free(tmp);
2256                 return -1;
2257         }
2258         if (change_file_mode_rw(fp, tmp) < 0) {
2259                 FILE_OP_ERROR(tmp, "chmod");
2260                 g_warning(_("can't change file mode\n"));
2261         }
2262
2263         /* queueing variables */
2264         fprintf(fp, "AF:\n");
2265         fprintf(fp, "NF:0\n");
2266         fprintf(fp, "PS:10\n");
2267         fprintf(fp, "SRH:1\n");
2268         fprintf(fp, "SFN:\n");
2269         fprintf(fp, "DSR:\n");
2270         if (compose->msgid)
2271                 fprintf(fp, "MID:<%s>\n", compose->msgid);
2272         else
2273                 fprintf(fp, "MID:\n");
2274         fprintf(fp, "CFG:\n");
2275         fprintf(fp, "PT:0\n");
2276         fprintf(fp, "S:%s\n", compose->account->address);
2277         fprintf(fp, "RQ:\n");
2278         if (compose->account->smtp_server)
2279                 fprintf(fp, "SSV:%s\n", compose->account->smtp_server);
2280         else
2281                 fprintf(fp, "SSV:\n");
2282         if (compose->account->nntp_server)
2283                 fprintf(fp, "NSV:%s\n", compose->account->nntp_server);
2284         else
2285                 fprintf(fp, "NSV:\n");
2286         fprintf(fp, "SSH:\n");
2287         fprintf(fp, "R:<%s>", (gchar *)compose->to_list->data);
2288         for (cur = compose->to_list->next; cur != NULL; cur = cur->next)
2289                 fprintf(fp, ",<%s>", (gchar *)cur->data);
2290         fprintf(fp, "\n");
2291         fprintf(fp, "\n");
2292
2293         while (fgets(buf, sizeof(buf), src_fp) != NULL) {
2294                 if (fputs(buf, fp) == EOF) {
2295                         FILE_OP_ERROR(tmp, "fputs");
2296                         fclose(fp);
2297                         fclose(src_fp);
2298                         unlink(tmp);
2299                         g_free(tmp);
2300                         return -1;
2301                 }
2302         }
2303
2304         fclose(src_fp);
2305         if (fclose(fp) == EOF) {
2306                 FILE_OP_ERROR(tmp, "fclose");
2307                 unlink(tmp);
2308                 g_free(tmp);
2309                 return -1;
2310         }
2311
2312         queue = folder_get_default_queue();
2313         folder_item_scan(queue);
2314         queue_path = folder_item_get_path(queue);
2315         if (!is_dir_exist(queue_path))
2316                 make_dir_hier(queue_path);
2317         if ((num = folder_item_add_msg(queue, tmp, TRUE)) < 0) {
2318                 g_warning(_("can't queue the message\n"));
2319                 unlink(tmp);
2320                 g_free(tmp);
2321                 g_free(queue_path);
2322                 return -1;
2323         }
2324         g_free(tmp);
2325
2326         if ((fp = procmsg_open_mark_file(queue_path, TRUE)) == NULL)
2327                 g_warning(_("can't open mark file\n"));
2328         else {
2329                 MsgInfo newmsginfo;
2330
2331                 newmsginfo.msgnum = num;
2332                 newmsginfo.flags.perm_flags = 0;
2333                 newmsginfo.flags.tmp_flags = 0;
2334                 procmsg_write_flags(&newmsginfo, fp);
2335                 fclose(fp);
2336         }
2337         g_free(queue_path);
2338
2339         folder_item_scan(queue);
2340         folderview_update_item(queue, TRUE);
2341
2342         return 0;
2343 }
2344
2345 static void compose_write_attach(Compose *compose, FILE *fp)
2346 {
2347         AttachInfo *ainfo;
2348         GtkCList *clist = GTK_CLIST(compose->attach_clist);
2349         gint row;
2350         FILE *attach_fp;
2351         gchar filename[BUFFSIZE];
2352         gint len;
2353
2354         for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
2355              row++) {
2356                 if ((attach_fp = fopen(ainfo->file, "r")) == NULL) {
2357                         g_warning(_("Can't open file %s\n"), ainfo->file);
2358                         continue;
2359                 }
2360
2361                 fprintf(fp, "\n--%s\n", compose->boundary);
2362
2363                 if (!strcmp2(ainfo->content_type, "message/rfc822")) {
2364                         fprintf(fp, "Content-Type: %s\n", ainfo->content_type);
2365                         fprintf(fp, "Content-Disposition: inline\n");
2366                 } else {
2367                         conv_encode_header(filename, sizeof(filename),
2368                                            ainfo->name, 12);
2369                         fprintf(fp, "Content-Type: %s;\n"
2370                                     " name=\"%s\"\n",
2371                                 ainfo->content_type, filename);
2372                         fprintf(fp, "Content-Disposition: attachment;\n"
2373                                     " filename=\"%s\"\n", filename);
2374                 }
2375
2376                 fprintf(fp, "Content-Transfer-Encoding: %s\n\n",
2377                         procmime_get_encoding_str(ainfo->encoding));
2378
2379                 if (ainfo->encoding == ENC_7BIT) {
2380                         gchar buf[BUFFSIZE];
2381
2382                         while (fgets(buf, sizeof(buf), attach_fp) != NULL) {
2383                                 len = strlen(buf);
2384                                 if (len > 1 && buf[len - 1] == '\n' &&
2385                                     buf[len - 2] == '\r') {
2386                                         buf[len - 2] = '\n';
2387                                         buf[len - 1] = '\0';
2388                                 }
2389                                 fputs(buf, fp);
2390                         }
2391                 } else {
2392                         gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
2393
2394                         while ((len = fread(inbuf, sizeof(gchar),
2395                                             B64_LINE_SIZE, attach_fp))
2396                                == B64_LINE_SIZE) {
2397                                 to64frombits(outbuf, inbuf, B64_LINE_SIZE);
2398                                 fputs(outbuf, fp);
2399                                 fputc('\n', fp);
2400                         }
2401                         if (len > 0 && feof(attach_fp)) {
2402                                 to64frombits(outbuf, inbuf, len);
2403                                 fputs(outbuf, fp);
2404                                 fputc('\n', fp);
2405                         }
2406                 }
2407
2408                 fclose(attach_fp);
2409         }
2410
2411         fprintf(fp, "\n--%s--\n", compose->boundary);
2412 }
2413
2414 #define IS_IN_CUSTOM_HEADER(header) \
2415         (compose->account->add_customhdr && \
2416          custom_header_find(compose->account->customhdr_list, header) != NULL)
2417
2418 static gint compose_write_headers(Compose *compose, FILE *fp,
2419                                   const gchar *charset, EncodingType encoding,
2420                                   gboolean is_draft)
2421 {
2422         gchar buf[BUFFSIZE];
2423         gchar *str;
2424         /* struct utsname utsbuf; */
2425
2426         g_return_val_if_fail(fp != NULL, -1);
2427         g_return_val_if_fail(charset != NULL, -1);
2428         g_return_val_if_fail(compose->account != NULL, -1);
2429         g_return_val_if_fail(compose->account->address != NULL, -1);
2430
2431         /* Date */
2432         if (compose->account->add_date) {
2433                 get_rfc822_date(buf, sizeof(buf));
2434                 fprintf(fp, "Date: %s\n", buf);
2435         }
2436
2437         /* From */
2438         if (!IS_IN_CUSTOM_HEADER("From")) {
2439                 if (compose->account->name && *compose->account->name) {
2440                         compose_convert_header
2441                                 (buf, sizeof(buf), compose->account->name,
2442                                  strlen("From: "));
2443                         fprintf(fp, "From: %s <%s>\n",
2444                                 buf, compose->account->address);
2445                 } else
2446                         fprintf(fp, "From: %s\n", compose->account->address);
2447         }
2448         
2449         slist_free_strings(compose->to_list);
2450         g_slist_free(compose->to_list);
2451         compose->to_list = NULL;
2452
2453         /* To */
2454         if (compose->use_to) {
2455                 str = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
2456                 if (*str != '\0') {
2457                         Xstrdup_a(str, str, return -1);
2458                         g_strstrip(str);
2459                         if (*str != '\0') {
2460                                 compose->to_list = address_list_append
2461                                         (compose->to_list, str);
2462                                 if (!IS_IN_CUSTOM_HEADER("To")) {
2463                                         compose_convert_header
2464                                                 (buf, sizeof(buf), str,
2465                                                  strlen("To: "));
2466                                         fprintf(fp, "To: %s\n", buf);
2467                                 }
2468                         }
2469                 }
2470         }
2471
2472         slist_free_strings(compose->newsgroup_list);
2473         g_slist_free(compose->newsgroup_list);
2474         compose->newsgroup_list = NULL;
2475
2476         /* Newsgroups */
2477         str = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
2478         if (*str != '\0') {
2479                 Xstrdup_a(str, str, return -1);
2480                 g_strstrip(str);
2481                 remove_space(str);
2482                 if (*str != '\0') {
2483                         compose->newsgroup_list =
2484                                 newsgroup_list_append(compose->newsgroup_list,
2485                                                       str);
2486                         if (!IS_IN_CUSTOM_HEADER("Newsgroups")) {
2487                                 compose_convert_header(buf, sizeof(buf), str,
2488                                                        strlen("Newsgroups: "));
2489                                 fprintf(fp, "Newsgroups: %s\n", buf);
2490                         }
2491                 }
2492         }
2493
2494         if (!is_draft && !compose->to_list && !compose->newsgroup_list)
2495                 return -1;
2496
2497         /* Cc */
2498         if (compose->use_cc) {
2499                 str = gtk_entry_get_text(GTK_ENTRY(compose->cc_entry));
2500                 if (*str != '\0') {
2501                         Xstrdup_a(str, str, return -1);
2502                         g_strstrip(str);
2503                         if (*str != '\0') {
2504                                 compose->to_list = address_list_append
2505                                         (compose->to_list, str);
2506                                 if (!IS_IN_CUSTOM_HEADER("Cc")) {
2507                                         compose_convert_header
2508                                                 (buf, sizeof(buf), str,
2509                                                  strlen("Cc: "));
2510                                         fprintf(fp, "Cc: %s\n", buf);
2511                                 }
2512                         }
2513                 }
2514         }
2515
2516         /* Bcc */
2517         if (compose->use_bcc) {
2518                 str = gtk_entry_get_text(GTK_ENTRY(compose->bcc_entry));
2519                 if (*str != '\0') {
2520                         Xstrdup_a(str, str, return -1);
2521                         g_strstrip(str);
2522                         if (*str != '\0') {
2523                                 compose->to_list = address_list_append
2524                                         (compose->to_list, str);
2525                                 if (is_draft) {
2526                                         compose_convert_header
2527                                                 (buf, sizeof(buf), str,
2528                                                  strlen("Bcc: "));
2529                                         fprintf(fp, "Bcc: %s\n", buf);
2530                                 }
2531                         }
2532                 }
2533         }
2534
2535         /* Subject */
2536         str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
2537         if (*str != '\0' && !IS_IN_CUSTOM_HEADER("Subject")) {
2538                 Xstrdup_a(str, str, return -1);
2539                 g_strstrip(str);
2540                 if (*str != '\0') {
2541                         compose_convert_header(buf, sizeof(buf), str,
2542                                                strlen("Subject: "));
2543                         fprintf(fp, "Subject: %s\n", buf);
2544                 }
2545         }
2546
2547         /* Message-ID */
2548         if (compose->account->gen_msgid) {
2549                 compose_generate_msgid(compose, buf, sizeof(buf));
2550                 fprintf(fp, "Message-Id: <%s>\n", buf);
2551                 compose->msgid = g_strdup(buf);
2552         }
2553
2554         /* In-Reply-To */
2555         if (compose->inreplyto && compose->to_list)
2556                 fprintf(fp, "In-Reply-To: <%s>\n", compose->inreplyto);
2557
2558         /* References */
2559         if (compose->references)
2560                 fprintf(fp, "References: %s\n", compose->references);
2561
2562         /* Followup-To */
2563         if (compose->use_followupto && !IS_IN_CUSTOM_HEADER("Followup-To")) {
2564                 str = gtk_entry_get_text(GTK_ENTRY(compose->followup_entry));
2565                 if (*str != '\0') {
2566                         Xstrdup_a(str, str, return -1);
2567                         g_strstrip(str);
2568                         remove_space(str);
2569                         if (*str != '\0') {
2570                                 compose_convert_header(buf, sizeof(buf), str,
2571                                                        strlen("Followup-To: "));
2572                                 fprintf(fp, "Followup-To: %s\n", buf);
2573                         }
2574                 }
2575         }
2576
2577         /* Reply-To */
2578         if (compose->use_replyto && !IS_IN_CUSTOM_HEADER("Reply-To")) {
2579                 str = gtk_entry_get_text(GTK_ENTRY(compose->reply_entry));
2580                 if (*str != '\0') {
2581                         Xstrdup_a(str, str, return -1);
2582                         g_strstrip(str);
2583                         if (*str != '\0') {
2584                                 compose_convert_header(buf, sizeof(buf), str,
2585                                                        strlen("Reply-To: "));
2586                                 fprintf(fp, "Reply-To: %s\n", buf);
2587                         }
2588                 }
2589         }
2590
2591         /* Organization */
2592         if (compose->account->organization &&
2593             !IS_IN_CUSTOM_HEADER("Organization")) {
2594                 compose_convert_header(buf, sizeof(buf),
2595                                        compose->account->organization,
2596                                        strlen("Organization: "));
2597                 fprintf(fp, "Organization: %s\n", buf);
2598         }
2599
2600         /* Program version and system info */
2601         /* uname(&utsbuf); */
2602         str = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
2603         if (*str != '\0' && !IS_IN_CUSTOM_HEADER("X-Mailer")) {
2604                 fprintf(fp, "X-Mailer: %s (GTK+ %d.%d.%d; %s)\n",
2605                         prog_version,
2606                         gtk_major_version, gtk_minor_version, gtk_micro_version,
2607                         HOST_ALIAS);
2608                         /* utsbuf.sysname, utsbuf.release, utsbuf.machine); */
2609         }
2610         str = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
2611         if (*str != '\0' && !IS_IN_CUSTOM_HEADER("X-Newsreader")) {
2612                 fprintf(fp, "X-Newsreader: %s (GTK+ %d.%d.%d; %s)\n",
2613                         prog_version,
2614                         gtk_major_version, gtk_minor_version, gtk_micro_version,
2615                         HOST_ALIAS);
2616                         /* utsbuf.sysname, utsbuf.release, utsbuf.machine); */
2617         }
2618
2619         /* custom headers */
2620         if (compose->account->add_customhdr) {
2621                 GSList *cur;
2622
2623                 for (cur = compose->account->customhdr_list; cur != NULL;
2624                      cur = cur->next) {
2625                         CustomHeader *chdr = (CustomHeader *)cur->data;
2626
2627                         if (strcasecmp(chdr->name, "Date")         != 0 &&
2628                             strcasecmp(chdr->name, "From")         != 0 &&
2629                             strcasecmp(chdr->name, "To")           != 0 &&
2630                             strcasecmp(chdr->name, "Sender")       != 0 &&
2631                             strcasecmp(chdr->name, "Message-Id")   != 0 &&
2632                             strcasecmp(chdr->name, "In-Reply-To")  != 0 &&
2633                             strcasecmp(chdr->name, "References")   != 0 &&
2634                             strcasecmp(chdr->name, "Mime-Version") != 0 &&
2635                             strcasecmp(chdr->name, "Content-Type") != 0 &&
2636                             strcasecmp(chdr->name, "Content-Transfer-Encoding")
2637                             != 0)
2638                                 compose_convert_header
2639                                         (buf, sizeof(buf),
2640                                          chdr->value ? chdr->value : "",
2641                                          strlen(chdr->name) + 2);
2642                                 fprintf(fp, "%s: %s\n", chdr->name, buf);
2643                 }
2644         }
2645
2646         /* MIME */
2647         fprintf(fp, "Mime-Version: 1.0\n");
2648         if (compose->use_attach) {
2649                 get_rfc822_date(buf, sizeof(buf));
2650                 subst_char(buf, ' ', '_');
2651                 subst_char(buf, ',', '_');
2652                 compose->boundary = g_strdup_printf("Multipart_%s_%08x",
2653                                                     buf, (guint)compose);
2654                 fprintf(fp,
2655                         "Content-Type: multipart/mixed;\n"
2656                         " boundary=\"%s\"\n", compose->boundary);
2657         } else {
2658                 fprintf(fp, "Content-Type: text/plain; charset=%s\n", charset);
2659                 fprintf(fp, "Content-Transfer-Encoding: %s\n",
2660                         procmime_get_encoding_str(encoding));
2661         }
2662
2663         /* Request Return Receipt */
2664         if (!IS_IN_CUSTOM_HEADER("Disposition-Notification-To")) {
2665                 if (compose->return_receipt) {
2666                         if (compose->account->name
2667                             && *compose->account->name) {
2668                                 compose_convert_header(buf, sizeof(buf), compose->account->name, strlen("Disposition-Notification-To: "));
2669                                 fprintf(fp, "Disposition-Notification-To: %s <%s>\n", buf, compose->account->address);
2670                         } else
2671                                 fprintf(fp, "Disposition-Notification-To: %s\n", compose->account->address);
2672                 }
2673         }
2674
2675         /* separator between header and body */
2676         fputs("\n", fp);
2677
2678         return 0;
2679 }
2680
2681 #undef IS_IN_CUSTOM_HEADER
2682
2683 static void compose_convert_header(gchar *dest, gint len, gchar *src,
2684                                    gint header_len)
2685 {
2686         g_return_if_fail(src != NULL);
2687         g_return_if_fail(dest != NULL);
2688
2689         if (len < 1) return;
2690
2691         remove_return(src);
2692
2693         if (is_ascii_str(src)) {
2694                 strncpy2(dest, src, len);
2695                 dest[len - 1] = '\0';
2696                 return;
2697         } else
2698                 conv_encode_header(dest, len, src, header_len);
2699 }
2700
2701 static void compose_generate_msgid(Compose *compose, gchar *buf, gint len)
2702 {
2703         struct tm *lt;
2704         time_t t;
2705         gchar *addr;
2706
2707         t = time(NULL);
2708         lt = localtime(&t);
2709
2710         if (compose->account && compose->account->address &&
2711             *compose->account->address) {
2712                 if (strchr(compose->account->address, '@'))
2713                         addr = g_strdup(compose->account->address);
2714                 else
2715                         addr = g_strconcat(compose->account->address, "@",
2716                                            get_domain_name(), NULL);
2717         } else
2718                 addr = g_strconcat(g_get_user_name(), "@", get_domain_name(),
2719                                    NULL);
2720
2721         g_snprintf(buf, len, "%04d%02d%02d%02d%02d%02d.%08x.%s",
2722                    lt->tm_year + 1900, lt->tm_mon + 1,
2723                    lt->tm_mday, lt->tm_hour,
2724                    lt->tm_min, lt->tm_sec,
2725                    (guint)random(), addr);
2726
2727         debug_print(_("generated Message-ID: %s\n"), buf);
2728
2729         g_free(addr);
2730 }
2731
2732 static void compose_add_entry_field(GtkWidget *table, GtkWidget **hbox,
2733                                     GtkWidget **entry, gint *count,
2734                                     const gchar *label_str,
2735                                     gboolean is_addr_entry)
2736 {
2737         GtkWidget *label;
2738
2739         if (GTK_TABLE(table)->nrows < (*count) + 1)
2740                 gtk_table_resize(GTK_TABLE(table), (*count) + 1, 2);
2741
2742         *hbox = gtk_hbox_new(FALSE, 0);
2743         label = gtk_label_new
2744                 (prefs_common.trans_hdr ? gettext(label_str) : label_str);
2745         gtk_box_pack_end(GTK_BOX(*hbox), label, FALSE, FALSE, 0);
2746         gtk_table_attach(GTK_TABLE(table), *hbox, 0, 1, *count, (*count) + 1,
2747                          GTK_FILL, 0, 2, 0);
2748         *entry = gtk_entry_new();
2749         gtk_table_attach_defaults
2750                 (GTK_TABLE(table), *entry, 1, 2, *count, (*count) + 1);
2751         if (GTK_TABLE(table)->nrows > (*count) + 1)
2752                 gtk_table_set_row_spacing(GTK_TABLE(table), *count, 4);
2753
2754         if (is_addr_entry)
2755                 address_completion_register_entry(GTK_ENTRY(*entry));
2756
2757         (*count)++;
2758 }
2759
2760 static Compose *compose_create(PrefsAccount *account)
2761 {
2762         Compose   *compose;
2763         GtkWidget *window;
2764         GtkWidget *vbox;
2765         GtkWidget *menubar;
2766         GtkWidget *handlebox;
2767
2768         GtkWidget *vbox2;
2769
2770         GtkWidget *table_vbox;
2771         GtkWidget *label;
2772         GtkWidget *from_optmenu_hbox;
2773         GtkWidget *to_entry;
2774         GtkWidget *to_hbox;
2775         GtkWidget *newsgroups_entry;
2776         GtkWidget *newsgroups_hbox;
2777         GtkWidget *subject_entry;
2778         GtkWidget *cc_entry;
2779         GtkWidget *cc_hbox;
2780         GtkWidget *bcc_entry;
2781         GtkWidget *bcc_hbox;
2782         GtkWidget *reply_entry;
2783         GtkWidget *reply_hbox;
2784         GtkWidget *followup_entry;
2785         GtkWidget *followup_hbox;
2786
2787         GtkWidget *paned;
2788
2789         GtkWidget *attach_scrwin;
2790         GtkWidget *attach_clist;
2791
2792         GtkWidget *edit_vbox;
2793         GtkWidget *ruler_hbox;
2794         GtkWidget *ruler;
2795         GtkWidget *scrolledwin;
2796         GtkWidget *text;
2797
2798         GtkWidget *table;
2799         GtkWidget *hbox;
2800
2801         gchar *titles[] = {_("MIME type"), _("Size"), _("Name")};
2802         guint n_menu_entries;
2803         GtkStyle  *style, *new_style;
2804         GdkColormap *cmap;
2805         GdkColor color[1];
2806         gboolean success[1];
2807         GdkFont   *font;
2808         GtkWidget *popupmenu;
2809         GtkWidget *menuitem;
2810         GtkItemFactory *popupfactory;
2811         GtkItemFactory *ifactory;
2812         gint n_entries;
2813         gint count = 0;
2814         gint i;
2815
2816         g_return_val_if_fail(account != NULL, NULL);
2817
2818         debug_print(_("Creating compose window...\n"));
2819         compose = g_new0(Compose, 1);
2820
2821         compose->account = account;
2822         compose->orig_account = account;
2823
2824         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2825         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
2826         gtk_widget_set_usize(window, -1, prefs_common.compose_height);
2827         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
2828                            GTK_SIGNAL_FUNC(compose_delete_cb), compose);
2829         gtk_signal_connect(GTK_OBJECT(window), "destroy",
2830                            GTK_SIGNAL_FUNC(compose_destroy_cb), compose);
2831         gtk_signal_connect(GTK_OBJECT(window), "focus_in_event",
2832                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
2833         gtk_signal_connect(GTK_OBJECT(window), "focus_out_event",
2834                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
2835         gtk_widget_realize(window);
2836
2837         vbox = gtk_vbox_new(FALSE, 0);
2838         gtk_container_add(GTK_CONTAINER(window), vbox);
2839
2840         n_menu_entries = sizeof(compose_entries) / sizeof(compose_entries[0]);
2841         menubar = menubar_create(window, compose_entries,
2842                                  n_menu_entries, "<Compose>", compose);
2843         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
2844
2845         handlebox = gtk_handle_box_new();
2846         gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
2847
2848         compose_toolbar_create(compose, handlebox);
2849
2850         vbox2 = gtk_vbox_new(FALSE, 2);
2851         gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
2852         gtk_container_set_border_width(GTK_CONTAINER(vbox2), BORDER_WIDTH);
2853
2854         table_vbox = gtk_vbox_new(FALSE, 0);
2855         gtk_box_pack_start(GTK_BOX(vbox2), table_vbox, FALSE, TRUE, 0);
2856         gtk_container_set_border_width(GTK_CONTAINER(table_vbox),
2857                                        BORDER_WIDTH * 2);
2858
2859         table = gtk_table_new(8, 2, FALSE);
2860         gtk_box_pack_start(GTK_BOX(table_vbox), table, FALSE, TRUE, 0);
2861
2862         /* option menu for selecting accounts */
2863         hbox = gtk_hbox_new(FALSE, 0);
2864         label = gtk_label_new(prefs_common.trans_hdr ? _("From:") : "From:");
2865         gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
2866         gtk_table_attach(GTK_TABLE(table), hbox, 0, 1, count, count + 1,
2867                          GTK_FILL, 0, 2, 0);
2868         from_optmenu_hbox = compose_account_option_menu_create(compose);
2869         gtk_table_attach_defaults(GTK_TABLE(table), from_optmenu_hbox,
2870                                   1, 2, count, count + 1);
2871         gtk_table_set_row_spacing(GTK_TABLE(table), 0, 4);
2872         count++;
2873
2874         /* header labels and entries */
2875         compose_add_entry_field(table, &to_hbox, &to_entry, &count,
2876                                 "To:", TRUE); 
2877         gtk_table_set_row_spacing(GTK_TABLE(table), 0, 4);
2878         compose_add_entry_field(table, &newsgroups_hbox, &newsgroups_entry,
2879                                 &count, "Newsgroups:", FALSE);
2880         gtk_table_set_row_spacing(GTK_TABLE(table), 1, 4);
2881         compose_add_entry_field(table, &hbox, &subject_entry, &count,
2882                                 "Subject:", FALSE);
2883         gtk_table_set_row_spacing(GTK_TABLE(table), 2, 4);
2884         compose_add_entry_field(table, &cc_hbox, &cc_entry, &count,
2885                                 "Cc:", TRUE);
2886         gtk_table_set_row_spacing(GTK_TABLE(table), 3, 4);
2887         compose_add_entry_field(table, &bcc_hbox, &bcc_entry, &count,
2888                                 "Bcc:", TRUE);
2889         gtk_table_set_row_spacing(GTK_TABLE(table), 4, 4);
2890         compose_add_entry_field(table, &reply_hbox, &reply_entry, &count,
2891                                 "Reply-To:", TRUE);
2892         gtk_table_set_row_spacing(GTK_TABLE(table), 5, 4);
2893         compose_add_entry_field(table, &followup_hbox, &followup_entry, &count,
2894                                 "Followup-To:", FALSE);
2895         gtk_table_set_row_spacing(GTK_TABLE(table), 6, 4);
2896
2897         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
2898
2899         gtk_signal_connect(GTK_OBJECT(to_entry), "activate",
2900                            GTK_SIGNAL_FUNC(to_activated), compose);
2901         gtk_signal_connect(GTK_OBJECT(newsgroups_entry), "activate",
2902                            GTK_SIGNAL_FUNC(newsgroups_activated), compose);
2903         gtk_signal_connect(GTK_OBJECT(subject_entry), "activate",
2904                            GTK_SIGNAL_FUNC(subject_activated), compose);
2905         gtk_signal_connect(GTK_OBJECT(cc_entry), "activate",
2906                            GTK_SIGNAL_FUNC(cc_activated), compose);
2907         gtk_signal_connect(GTK_OBJECT(bcc_entry), "activate",
2908                            GTK_SIGNAL_FUNC(bcc_activated), compose);
2909         gtk_signal_connect(GTK_OBJECT(reply_entry), "activate",
2910                            GTK_SIGNAL_FUNC(replyto_activated), compose);
2911         gtk_signal_connect(GTK_OBJECT(followup_entry), "activate",
2912                            GTK_SIGNAL_FUNC(followupto_activated), compose);
2913
2914         gtk_signal_connect(GTK_OBJECT(subject_entry), "grab_focus",
2915                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2916         gtk_signal_connect(GTK_OBJECT(to_entry), "grab_focus",
2917                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2918         gtk_signal_connect(GTK_OBJECT(newsgroups_entry), "grab_focus",
2919                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2920         gtk_signal_connect(GTK_OBJECT(cc_entry), "grab_focus",
2921                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2922         gtk_signal_connect(GTK_OBJECT(bcc_entry), "grab_focus",
2923                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2924         gtk_signal_connect(GTK_OBJECT(reply_entry), "grab_focus",
2925                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2926         gtk_signal_connect(GTK_OBJECT(followup_entry), "grab_focus",
2927                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2928
2929         /* attachment list */
2930         attach_scrwin = gtk_scrolled_window_new(NULL, NULL);
2931         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(attach_scrwin),
2932                                        GTK_POLICY_AUTOMATIC,
2933                                        GTK_POLICY_ALWAYS);
2934         gtk_widget_set_usize(attach_scrwin, -1, 80);
2935
2936         attach_clist = gtk_clist_new_with_titles(N_ATTACH_COLS, titles);
2937         gtk_clist_set_column_justification(GTK_CLIST(attach_clist), COL_SIZE,
2938                                            GTK_JUSTIFY_RIGHT);
2939         gtk_clist_set_column_width(GTK_CLIST(attach_clist), COL_MIMETYPE, 240);
2940         gtk_clist_set_column_width(GTK_CLIST(attach_clist), COL_SIZE, 64);
2941         gtk_clist_set_selection_mode(GTK_CLIST(attach_clist),
2942                                      GTK_SELECTION_EXTENDED);
2943         for (i = 0; i < N_ATTACH_COLS; i++)
2944                 GTK_WIDGET_UNSET_FLAGS
2945                         (GTK_CLIST(attach_clist)->column[i].button,
2946                          GTK_CAN_FOCUS);
2947         gtk_container_add(GTK_CONTAINER(attach_scrwin), attach_clist);
2948
2949         gtk_signal_connect(GTK_OBJECT(attach_clist), "select_row",
2950                            GTK_SIGNAL_FUNC(attach_selected), compose);
2951         gtk_signal_connect(GTK_OBJECT(attach_clist), "button_press_event",
2952                            GTK_SIGNAL_FUNC(attach_button_pressed), compose);
2953         gtk_signal_connect(GTK_OBJECT(attach_clist), "key_press_event",
2954                            GTK_SIGNAL_FUNC(attach_key_pressed), compose);
2955
2956         /* drag and drop */
2957         gtk_drag_dest_set(attach_clist,
2958                           GTK_DEST_DEFAULT_ALL, compose_mime_types, 1,
2959                           GDK_ACTION_COPY);
2960         gtk_signal_connect(GTK_OBJECT(attach_clist), "drag_data_received",
2961                            GTK_SIGNAL_FUNC(compose_attach_drag_received_cb),
2962                            compose);
2963
2964         /* pane between attach clist and text */
2965         paned = gtk_vpaned_new();
2966         gtk_paned_add1(GTK_PANED(paned), attach_scrwin);
2967         gtk_widget_ref(paned);
2968         gtk_widget_show_all(paned);
2969
2970         edit_vbox = gtk_vbox_new(FALSE, 0);
2971         gtk_box_pack_start(GTK_BOX(vbox2), edit_vbox, TRUE, TRUE, 0);
2972
2973         /* ruler */
2974         ruler_hbox = gtk_hbox_new(FALSE, 0);
2975         gtk_box_pack_start(GTK_BOX(edit_vbox), ruler_hbox, FALSE, FALSE, 0);
2976
2977         ruler = gtk_shruler_new();
2978         gtk_ruler_set_range(GTK_RULER(ruler), 0.0, 100.0, 1.0, 100.0);
2979         gtk_box_pack_start(GTK_BOX(ruler_hbox), ruler, TRUE, TRUE,
2980                            BORDER_WIDTH + 1);
2981         gtk_widget_set_usize(ruler_hbox, 1, -1);
2982
2983         /* text widget */
2984         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
2985         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
2986                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
2987         gtk_box_pack_start(GTK_BOX(edit_vbox), scrolledwin, TRUE, TRUE, 0);
2988         gtk_widget_set_usize(scrolledwin, prefs_common.compose_width, -1);
2989
2990         text = gtk_stext_new(gtk_scrolled_window_get_hadjustment
2991                             (GTK_SCROLLED_WINDOW(scrolledwin)),
2992                             gtk_scrolled_window_get_vadjustment
2993                             (GTK_SCROLLED_WINDOW(scrolledwin)));
2994         gtk_stext_set_editable(GTK_STEXT(text), TRUE);
2995         if (prefs_common.smart_wrapping) {      
2996                 gtk_stext_set_word_wrap(GTK_STEXT(text), TRUE);
2997                 gtk_stext_set_wrap_rmargin(GTK_STEXT(text), prefs_common.linewrap_len);
2998         }               
2999
3000         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
3001
3002         gtk_signal_connect(GTK_OBJECT(text), "changed",
3003                            GTK_SIGNAL_FUNC(compose_changed_cb), compose);
3004         gtk_signal_connect(GTK_OBJECT(text), "grab_focus",
3005                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
3006         gtk_signal_connect_after(GTK_OBJECT(text), "button_press_event",
3007                                  GTK_SIGNAL_FUNC(compose_button_press_cb),
3008                                  compose);
3009 #if 0
3010         gtk_signal_connect_after(GTK_OBJECT(text), "key_press_event",
3011                                  GTK_SIGNAL_FUNC(compose_key_press_cb),
3012                                  compose);
3013 #endif
3014         gtk_signal_connect_after(GTK_OBJECT(text), "size_allocate",
3015                                  GTK_SIGNAL_FUNC(compose_edit_size_alloc),
3016                                  ruler);
3017
3018         /* drag and drop */
3019         gtk_drag_dest_set(text, GTK_DEST_DEFAULT_ALL, compose_mime_types, 1,
3020                           GDK_ACTION_COPY);
3021         gtk_signal_connect(GTK_OBJECT(text), "drag_data_received",
3022                            GTK_SIGNAL_FUNC(compose_insert_drag_received_cb),
3023                            compose);
3024
3025         gtk_widget_show_all(vbox);
3026
3027         style = gtk_widget_get_style(text);
3028
3029         /* workaround for the slow down of GtkText when using Pixmap theme */
3030         if (style->engine) {
3031                 GtkThemeEngine *engine;
3032
3033                 engine = style->engine;
3034                 style->engine = NULL;
3035                 new_style = gtk_style_copy(style);
3036                 style->engine = engine;
3037         } else
3038                 new_style = gtk_style_copy(style);
3039
3040         if (prefs_common.textfont) {
3041                 CharSet charset;
3042
3043                 charset = conv_get_current_charset();
3044                 if (MB_CUR_MAX == 1) {
3045                         gchar *fontstr, *p;
3046
3047                         Xstrdup_a(fontstr, prefs_common.textfont, );
3048                         if (fontstr && (p = strchr(fontstr, ',')) != NULL)
3049                                 *p = '\0';
3050                         font = gdk_font_load(fontstr);
3051                 } else
3052                         font = gdk_fontset_load(prefs_common.textfont);
3053                 if (font) {
3054                         gdk_font_unref(new_style->font);
3055                         new_style->font = font;
3056                 }
3057         }
3058
3059         gtk_widget_set_style(text, new_style);
3060
3061         color[0] = quote_color;
3062         cmap = gdk_window_get_colormap(window->window);
3063         gdk_colormap_alloc_colors(cmap, color, 1, FALSE, TRUE, success);
3064         if (success[0] == FALSE) {
3065                 g_warning("Compose: color allocation failed.\n");
3066                 style = gtk_widget_get_style(text);
3067                 quote_color = style->black;
3068         }
3069
3070         n_entries = sizeof(compose_popup_entries) /
3071                 sizeof(compose_popup_entries[0]);
3072         popupmenu = menu_create_items(compose_popup_entries, n_entries,
3073                                       "<Compose>", &popupfactory,
3074                                       compose);
3075
3076         ifactory = gtk_item_factory_from_widget(menubar);
3077         menu_set_sensitive(ifactory, "/Edit/Undo", FALSE);
3078         menu_set_sensitive(ifactory, "/Edit/Redo", FALSE);
3079
3080         /*
3081         if (account->protocol == A_NNTP) {
3082                 gtk_widget_hide(to_hbox);
3083                 gtk_widget_hide(to_entry);
3084                 gtk_widget_hide(cc_hbox);
3085                 gtk_widget_hide(cc_entry);
3086                 gtk_table_set_row_spacing(GTK_TABLE(table), 1, 0);
3087                 gtk_table_set_row_spacing(GTK_TABLE(table), 3, 0);
3088         } else {
3089                 gtk_widget_hide(newsgroups_hbox);
3090                 gtk_widget_hide(newsgroups_entry);
3091                 gtk_table_set_row_spacing(GTK_TABLE(table), 2, 0);
3092
3093                 menu_set_sensitive(ifactory, "/Message/Followup to", FALSE);
3094         }
3095         */
3096
3097         switch (prefs_common.toolbar_style) {
3098         case TOOLBAR_NONE:
3099                 gtk_widget_hide(handlebox);
3100                 break;
3101         case TOOLBAR_ICON:
3102                 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
3103                                       GTK_TOOLBAR_ICONS);
3104                 break;
3105         case TOOLBAR_TEXT:
3106                 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
3107                                       GTK_TOOLBAR_TEXT);
3108                 break;
3109         case TOOLBAR_BOTH:
3110                 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
3111                                       GTK_TOOLBAR_BOTH);
3112                 break;
3113         }
3114
3115         gtk_widget_show(window);
3116
3117         address_completion_start(window);
3118
3119         compose->window        = window;
3120         compose->vbox          = vbox;
3121         compose->menubar       = menubar;
3122         compose->handlebox     = handlebox;
3123
3124         compose->vbox2         = vbox2;
3125
3126         compose->table_vbox       = table_vbox;
3127         compose->table            = table;
3128         compose->to_hbox          = to_hbox;
3129         compose->to_entry         = to_entry;
3130         compose->newsgroups_hbox  = newsgroups_hbox;
3131         compose->newsgroups_entry = newsgroups_entry;
3132         compose->subject_entry    = subject_entry;
3133         compose->cc_hbox          = cc_hbox;
3134         compose->cc_entry         = cc_entry;
3135         compose->bcc_hbox         = bcc_hbox;
3136         compose->bcc_entry        = bcc_entry;
3137         compose->reply_hbox       = reply_hbox;
3138         compose->reply_entry      = reply_entry;
3139         compose->followup_hbox    = followup_hbox;
3140         compose->followup_entry   = followup_entry;
3141
3142         compose->paned = paned;
3143
3144         compose->attach_scrwin = attach_scrwin;
3145         compose->attach_clist  = attach_clist;
3146
3147         compose->edit_vbox     = edit_vbox;
3148         compose->ruler_hbox    = ruler_hbox;
3149         compose->ruler         = ruler;
3150         compose->scrolledwin   = scrolledwin;
3151         compose->text          = text;
3152
3153         compose->focused_editable = NULL;
3154
3155         compose->popupmenu    = popupmenu;
3156         compose->popupfactory = popupfactory;
3157
3158         compose->mode = COMPOSE_NEW;
3159
3160         compose->replyto     = NULL;
3161         compose->cc          = NULL;
3162         compose->bcc         = NULL;
3163         compose->followup_to = NULL;
3164         compose->inreplyto   = NULL;
3165         compose->references  = NULL;
3166         compose->msgid       = NULL;
3167         compose->boundary    = NULL;
3168
3169 #if USE_GPGME
3170         compose->use_signing    = FALSE;
3171         compose->use_encryption = FALSE;
3172 #endif /* USE_GPGME */
3173
3174         compose->modified = FALSE;
3175
3176         compose->return_receipt = FALSE;
3177
3178         compose->to_list        = NULL;
3179         compose->newsgroup_list = NULL;
3180
3181         compose->exteditor_file    = NULL;
3182         compose->exteditor_pid     = -1;
3183         compose->exteditor_readdes = -1;
3184         compose->exteditor_tag     = -1;
3185
3186         compose_set_title(compose);
3187
3188         compose->use_bcc        = FALSE;
3189         compose->use_replyto    = FALSE;
3190         compose->use_followupto = FALSE;
3191
3192         /*
3193         if (account->protocol != A_NNTP) {
3194                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/To");
3195                 gtk_check_menu_item_set_active
3196                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
3197                 gtk_widget_set_sensitive(menuitem, FALSE);
3198                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Cc");
3199                 gtk_check_menu_item_set_active
3200                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
3201                 gtk_widget_set_sensitive(menuitem, FALSE);
3202         }
3203         */
3204         if (account->set_autocc && account->auto_cc) {
3205                 compose->use_cc = TRUE;
3206                 gtk_entry_set_text(GTK_ENTRY(cc_entry), account->auto_cc);
3207                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Cc");
3208                 gtk_check_menu_item_set_active
3209                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
3210         }
3211
3212         if (account->set_autobcc) {
3213                 compose->use_bcc = TRUE;
3214                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Bcc");
3215                 gtk_check_menu_item_set_active
3216                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
3217                 if (account->auto_bcc)
3218                         gtk_entry_set_text(GTK_ENTRY(bcc_entry),
3219                                            account->auto_bcc);
3220         }
3221         if (account->set_autoreplyto) {
3222                 compose->use_replyto = TRUE;
3223                 menuitem = gtk_item_factory_get_item(ifactory,
3224                                                      "/Message/Reply to");
3225                 gtk_check_menu_item_set_active
3226                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
3227                 if (account->auto_replyto)
3228                         gtk_entry_set_text(GTK_ENTRY(reply_entry),
3229                                            account->auto_replyto);
3230         }
3231
3232         menuitem = gtk_item_factory_get_item(ifactory, "/Tool/Show ruler");
3233         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
3234                                        prefs_common.show_ruler);
3235
3236 #if USE_GPGME
3237         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Sign");
3238         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
3239                                        prefs_common.default_sign);
3240         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Encrypt");
3241         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
3242                                        prefs_common.default_encrypt);
3243 #endif /* USE_GPGME */
3244
3245         addressbook_set_target_compose(compose);
3246
3247         compose_list = g_list_append(compose_list, compose);
3248
3249         /*
3250         compose->use_to         = FALSE;
3251         compose->use_cc         = FALSE;
3252         */
3253         compose->use_attach     = FALSE;
3254
3255         if (!compose->use_bcc) {
3256                 gtk_widget_hide(bcc_hbox);
3257                 gtk_widget_hide(bcc_entry);
3258                 gtk_table_set_row_spacing(GTK_TABLE(table), 4, 0);
3259         }
3260         if (!compose->use_replyto) {
3261                 gtk_widget_hide(reply_hbox);
3262                 gtk_widget_hide(reply_entry);
3263                 gtk_table_set_row_spacing(GTK_TABLE(table), 5, 0);
3264         }
3265         if (!compose->use_followupto) {
3266                 gtk_widget_hide(followup_hbox);
3267                 gtk_widget_hide(followup_entry);
3268                 gtk_table_set_row_spacing(GTK_TABLE(table), 6, 0);
3269         }
3270
3271         if (!prefs_common.show_ruler)
3272                 gtk_widget_hide(ruler_hbox);
3273
3274         select_account(compose, account);
3275
3276         return compose;
3277 }
3278
3279 #include "pixmaps/stock_mail_send.xpm"
3280 #include "pixmaps/stock_mail.xpm"
3281 #include "pixmaps/stock_paste.xpm"
3282 #include "pixmaps/stock_mail_attach.xpm"
3283 #include "pixmaps/stock_mail_compose.xpm"
3284 #include "pixmaps/linewrap.xpm"
3285 /*#include "pixmaps/tb_mail_queue_send.xpm"*/
3286 #include "pixmaps/tb_address_book.xpm"
3287
3288 #define CREATE_TOOLBAR_ICON(xpm_d) \
3289 { \
3290         icon = gdk_pixmap_create_from_xpm_d(container->window, &mask, \
3291                                             &container->style->white, \
3292                                             xpm_d); \
3293         icon_wid = gtk_pixmap_new(icon, mask); \
3294 }
3295
3296 static void compose_toolbar_create(Compose *compose, GtkWidget *container)
3297 {
3298         GtkWidget *toolbar;
3299         GdkPixmap *icon;
3300         GdkBitmap *mask;
3301         GtkWidget *icon_wid;
3302         GtkWidget *send_btn;
3303         GtkWidget *sendl_btn;
3304         GtkWidget *draft_btn;
3305         GtkWidget *insert_btn;
3306         GtkWidget *attach_btn;
3307         GtkWidget *sig_btn;
3308         GtkWidget *exteditor_btn;
3309         GtkWidget *linewrap_btn;
3310         GtkWidget *addrbook_btn;
3311
3312         toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
3313                                   GTK_TOOLBAR_BOTH);
3314         gtk_container_add(GTK_CONTAINER(container), toolbar);
3315         gtk_container_set_border_width(GTK_CONTAINER(container), 2);
3316         gtk_toolbar_set_button_relief(GTK_TOOLBAR(toolbar), GTK_RELIEF_NONE);
3317         gtk_toolbar_set_space_style(GTK_TOOLBAR(toolbar),
3318                                     GTK_TOOLBAR_SPACE_LINE);
3319
3320         CREATE_TOOLBAR_ICON(stock_mail_send_xpm);
3321         send_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3322                                            _("Send"),
3323                                            _("Send message"),
3324                                            "Send",
3325                                            icon_wid, toolbar_send_cb, compose);
3326
3327         CREATE_TOOLBAR_ICON(stock_mail_send_xpm);
3328         /* CREATE_TOOLBAR_ICON(tb_mail_queue_send_xpm); */
3329         sendl_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3330                                            _("Send later"),
3331                                            _("Put into queue folder and send later"),
3332                                            "Send later",
3333                                            icon_wid, toolbar_send_later_cb,
3334                                            compose);
3335
3336         CREATE_TOOLBAR_ICON(stock_mail_xpm);
3337         draft_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3338                                             _("Draft"),
3339                                             _("Save to draft folder"),
3340                                             "Draft",
3341                                             icon_wid, toolbar_draft_cb,
3342                                             compose);
3343
3344         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
3345
3346         CREATE_TOOLBAR_ICON(stock_paste_xpm);
3347         insert_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3348                                              _("Insert"),
3349                                              _("Insert file"),
3350                                              "Insert",
3351                                              icon_wid, toolbar_insert_cb,
3352                                              compose);
3353
3354         CREATE_TOOLBAR_ICON(stock_mail_attach_xpm);
3355         attach_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3356                                              _("Attach"),
3357                                              _("Attach file"),
3358                                              "Attach",
3359                                              icon_wid, toolbar_attach_cb,
3360                                              compose);
3361
3362         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
3363
3364         CREATE_TOOLBAR_ICON(stock_mail_xpm);
3365         sig_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3366                                           _("Signature"),
3367                                           _("Insert signature"),
3368                                           "Signature",
3369                                           icon_wid, toolbar_sig_cb, compose);
3370
3371         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
3372
3373         CREATE_TOOLBAR_ICON(stock_mail_compose_xpm);
3374         exteditor_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3375                                                 _("Editor"),
3376                                                 _("Edit with external editor"),
3377                                                 "Editor",
3378                                                 icon_wid,
3379                                                 toolbar_ext_editor_cb,
3380                                                 compose);
3381
3382         CREATE_TOOLBAR_ICON(linewrap_xpm);
3383         linewrap_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3384                                                _("Linewrap"),
3385                                                _("Wrap long lines"),
3386                                                "Linewrap",
3387                                                icon_wid,
3388                                                toolbar_linewrap_cb,
3389                                                compose);
3390
3391         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
3392
3393         CREATE_TOOLBAR_ICON(tb_address_book_xpm);
3394         addrbook_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3395                                                _("Address"),
3396                                                _("Address book"),
3397                                                "Address",
3398                                                icon_wid, toolbar_address_cb,
3399                                                compose);
3400
3401         compose->toolbar       = toolbar;
3402         compose->send_btn      = send_btn;
3403         compose->sendl_btn     = sendl_btn;
3404         compose->draft_btn     = draft_btn;
3405         compose->insert_btn    = insert_btn;
3406         compose->attach_btn    = attach_btn;
3407         compose->sig_btn       = sig_btn;
3408         compose->exteditor_btn = exteditor_btn;
3409         compose->linewrap_btn  = linewrap_btn;
3410         compose->addrbook_btn  = addrbook_btn;
3411
3412         gtk_widget_show_all(toolbar);
3413 }
3414
3415 #undef CREATE_TOOLBAR_ICON
3416
3417 static GtkWidget *compose_account_option_menu_create(Compose *compose)
3418 {
3419         GList *accounts;
3420         GtkWidget *hbox;
3421         GtkWidget *optmenu;
3422         GtkWidget *menu;
3423         gint num = 0, def_menu = 0;
3424
3425         accounts = account_get_list();
3426         g_return_val_if_fail(accounts != NULL, NULL);
3427
3428         hbox = gtk_hbox_new(FALSE, 0);
3429         optmenu = gtk_option_menu_new();
3430         gtk_box_pack_start(GTK_BOX(hbox), optmenu, FALSE, FALSE, 0);
3431         menu = gtk_menu_new();
3432
3433         for (; accounts != NULL; accounts = accounts->next, num++) {
3434                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
3435                 GtkWidget *menuitem;
3436                 gchar *name;
3437
3438                 if (ac == compose->account) def_menu = num;
3439
3440                 name = g_strdup_printf("%s: %s <%s>",
3441                                        ac->account_name, ac->name, ac->address);
3442                 MENUITEM_ADD(menu, menuitem, name, ac);
3443                 g_free(name);
3444                 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3445                                    GTK_SIGNAL_FUNC(account_activated),
3446                                    compose);
3447         }
3448
3449         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), menu);
3450         gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), def_menu);
3451
3452         return hbox;
3453 }
3454
3455 static void compose_destroy(Compose *compose)
3456 {
3457         gint row;
3458         GtkCList *clist = GTK_CLIST(compose->attach_clist);
3459         AttachInfo *ainfo;
3460
3461         /* NOTE: address_completion_end() does nothing with the window
3462          * however this may change. */
3463         address_completion_end(compose->window);
3464
3465         slist_free_strings(compose->to_list);
3466         g_slist_free(compose->to_list);
3467         slist_free_strings(compose->newsgroup_list);
3468         g_slist_free(compose->newsgroup_list);
3469
3470         procmsg_msginfo_free(compose->targetinfo);
3471
3472         g_free(compose->replyto);
3473         g_free(compose->cc);
3474         g_free(compose->bcc);
3475         g_free(compose->newsgroups);
3476         g_free(compose->followup_to);
3477
3478         g_free(compose->inreplyto);
3479         g_free(compose->references);
3480         g_free(compose->msgid);
3481         g_free(compose->boundary);
3482
3483         g_free(compose->exteditor_file);
3484
3485         for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
3486              row++)
3487                 compose_attach_info_free(ainfo);
3488
3489         if (addressbook_get_target_compose() == compose)
3490                 addressbook_set_target_compose(NULL);
3491
3492         prefs_common.compose_width = compose->scrolledwin->allocation.width;
3493         prefs_common.compose_height = compose->window->allocation.height;
3494
3495         gtk_widget_destroy(compose->paned);
3496
3497         g_free(compose);
3498
3499         compose_list = g_list_remove(compose_list, compose);
3500 }
3501
3502 static void compose_attach_info_free(AttachInfo *ainfo)
3503 {
3504         g_free(ainfo->file);
3505         g_free(ainfo->content_type);
3506         g_free(ainfo->name);
3507         g_free(ainfo);
3508 }
3509
3510 static void compose_attach_remove_selected(Compose *compose)
3511 {
3512         GtkCList *clist = GTK_CLIST(compose->attach_clist);
3513         AttachInfo *ainfo;
3514         gint row;
3515
3516         while (clist->selection != NULL) {
3517                 row = GPOINTER_TO_INT(clist->selection->data);
3518                 ainfo = gtk_clist_get_row_data(clist, row);
3519                 compose_attach_info_free(ainfo);
3520                 gtk_clist_remove(clist, row);
3521         }
3522 }
3523
3524 static struct _AttachProperty
3525 {
3526         GtkWidget *window;
3527         GtkWidget *mimetype_entry;
3528         GtkWidget *encoding_optmenu;
3529         GtkWidget *path_entry;
3530         GtkWidget *filename_entry;
3531         GtkWidget *ok_btn;
3532         GtkWidget *cancel_btn;
3533 } attach_prop;
3534
3535 static void compose_attach_property(Compose *compose)
3536 {
3537         GtkCList *clist = GTK_CLIST(compose->attach_clist);
3538         AttachInfo *ainfo;
3539         gint row;
3540         gboolean cancelled;
3541         GtkOptionMenu *optmenu;
3542
3543         if (!clist->selection) return;
3544         row = GPOINTER_TO_INT(clist->selection->data);
3545
3546         ainfo = gtk_clist_get_row_data(clist, row);
3547         if (!ainfo) return;
3548
3549         if (!attach_prop.window)
3550                 compose_attach_property_create(&cancelled);
3551         gtk_widget_grab_focus(attach_prop.ok_btn);
3552         gtk_widget_show(attach_prop.window);
3553         manage_window_set_transient(GTK_WINDOW(attach_prop.window));
3554
3555         optmenu = GTK_OPTION_MENU(attach_prop.encoding_optmenu);
3556         if (ainfo->encoding == ENC_UNKNOWN)
3557                 gtk_option_menu_set_history(optmenu, ENC_BASE64);
3558         else
3559                 gtk_option_menu_set_history(optmenu, ainfo->encoding);
3560
3561         gtk_entry_set_text(GTK_ENTRY(attach_prop.mimetype_entry),
3562                            ainfo->content_type ? ainfo->content_type : "");
3563         gtk_entry_set_text(GTK_ENTRY(attach_prop.path_entry),
3564                            ainfo->file ? ainfo->file : "");
3565         gtk_entry_set_text(GTK_ENTRY(attach_prop.filename_entry),
3566                            ainfo->name ? ainfo->name : "");
3567
3568         for (;;) {
3569                 gchar *text;
3570                 gchar *cnttype = NULL;
3571                 gchar *file = NULL;
3572                 off_t size = 0;
3573                 GtkWidget *menu;
3574                 GtkWidget *menuitem;
3575
3576                 gtk_main();
3577
3578                 if (cancelled == TRUE) {
3579                         gtk_widget_hide(attach_prop.window);
3580                         break;
3581                 }
3582
3583                 text = gtk_entry_get_text(GTK_ENTRY(attach_prop.mimetype_entry));
3584                 if (*text != '\0') {
3585                         gchar *p;
3586
3587                         text = g_strstrip(g_strdup(text));
3588                         if ((p = strchr(text, '/')) && !strchr(p + 1, '/')) {
3589                                 cnttype = g_strdup(text);
3590                                 g_free(text);
3591                         } else {
3592                                 alertpanel_error(_("Invalid MIME type."));
3593                                 g_free(text);
3594                                 continue;
3595                         }
3596                 }
3597
3598                 menu = gtk_option_menu_get_menu(optmenu);
3599                 menuitem = gtk_menu_get_active(GTK_MENU(menu));
3600                 ainfo->encoding = GPOINTER_TO_INT
3601                         (gtk_object_get_user_data(GTK_OBJECT(menuitem)));
3602
3603                 text = gtk_entry_get_text(GTK_ENTRY(attach_prop.path_entry));
3604                 if (*text != '\0') {
3605                         if (is_file_exist(text) &&
3606                             (size = get_file_size(text)) > 0)
3607                                 file = g_strdup(text);
3608                         else {
3609                                 alertpanel_error
3610                                         (_("File doesn't exist or is empty."));
3611                                 g_free(cnttype);
3612                                 continue;
3613                         }
3614                 }
3615
3616                 text = gtk_entry_get_text(GTK_ENTRY(attach_prop.filename_entry));
3617                 if (*text != '\0') {
3618                         g_free(ainfo->name);
3619                         ainfo->name = g_strdup(text);
3620                 }
3621
3622                 if (cnttype) {
3623                         g_free(ainfo->content_type);
3624                         ainfo->content_type = cnttype;
3625                 }
3626                 if (file) {
3627                         g_free(ainfo->file);
3628                         ainfo->file = file;
3629                 }
3630                 if (size)
3631                         ainfo->size = size;
3632
3633                 gtk_clist_set_text(clist, row, COL_MIMETYPE,
3634                                    ainfo->content_type);
3635                 gtk_clist_set_text(clist, row, COL_SIZE,
3636                                    to_human_readable(ainfo->size));
3637                 gtk_clist_set_text(clist, row, COL_NAME, ainfo->name);
3638
3639                 gtk_widget_hide(attach_prop.window);
3640                 break;
3641         }
3642 }
3643
3644 #define SET_LABEL_AND_ENTRY(str, entry, top) \
3645 { \
3646         label = gtk_label_new(str); \
3647         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), \
3648                          GTK_FILL, 0, 0, 0); \
3649         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); \
3650  \
3651         entry = gtk_entry_new(); \
3652         gtk_table_attach(GTK_TABLE(table), entry, 1, 2, top, (top + 1), \
3653                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); \
3654 }
3655
3656 static void compose_attach_property_create(gboolean *cancelled)
3657 {
3658         GtkWidget *window;
3659         GtkWidget *vbox;
3660         GtkWidget *table;
3661         GtkWidget *label;
3662         GtkWidget *mimetype_entry;
3663         GtkWidget *hbox;
3664         GtkWidget *optmenu;
3665         GtkWidget *optmenu_menu;
3666         GtkWidget *menuitem;
3667         GtkWidget *path_entry;
3668         GtkWidget *filename_entry;
3669         GtkWidget *hbbox;
3670         GtkWidget *ok_btn;
3671         GtkWidget *cancel_btn;
3672         GList     *mime_type_list, *strlist;
3673
3674         debug_print("Creating attach_property window...\n");
3675
3676         window = gtk_window_new(GTK_WINDOW_DIALOG);
3677         gtk_widget_set_usize(window, 480, -1);
3678         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
3679         gtk_window_set_title(GTK_WINDOW(window), _("Property"));
3680         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3681         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
3682         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
3683                            GTK_SIGNAL_FUNC(attach_property_delete_event),
3684                            cancelled);
3685         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
3686                            GTK_SIGNAL_FUNC(attach_property_key_pressed),
3687                            cancelled);
3688
3689         vbox = gtk_vbox_new(FALSE, 8);
3690         gtk_container_add(GTK_CONTAINER(window), vbox);
3691
3692         table = gtk_table_new(4, 2, FALSE);
3693         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
3694         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
3695         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
3696
3697         label = gtk_label_new(_("MIME type")); 
3698         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, (0 + 1), 
3699                          GTK_FILL, 0, 0, 0); 
3700         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); 
3701         mimetype_entry = gtk_combo_new(); 
3702         gtk_table_attach(GTK_TABLE(table), mimetype_entry, 1, 2, 0, (0 + 1), 
3703                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
3704                          
3705         /* stuff with list */
3706         mime_type_list = procmime_get_mime_type_list();
3707         strlist = NULL;
3708         for (; mime_type_list != NULL; mime_type_list = mime_type_list->next) {
3709                 MimeType *type = (MimeType *) mime_type_list->data;
3710                 strlist = g_list_append(strlist, 
3711                                 g_strdup_printf("%s/%s",
3712                                         type->type, type->sub_type));
3713         }
3714         
3715         gtk_combo_set_popdown_strings(GTK_COMBO(mimetype_entry), strlist);
3716
3717         for (mime_type_list = strlist; mime_type_list != NULL; 
3718                 mime_type_list = mime_type_list->next)
3719                 g_free(mime_type_list->data);
3720         g_list_free(strlist);
3721                          
3722         mimetype_entry = GTK_COMBO(mimetype_entry)->entry;                       
3723
3724         label = gtk_label_new(_("Encoding"));
3725         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
3726                          GTK_FILL, 0, 0, 0);
3727         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
3728
3729         hbox = gtk_hbox_new(FALSE, 0);
3730         gtk_table_attach(GTK_TABLE(table), hbox, 1, 2, 1, 2,
3731                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
3732
3733         optmenu = gtk_option_menu_new();
3734         gtk_box_pack_start(GTK_BOX(hbox), optmenu, FALSE, FALSE, 0);
3735
3736         optmenu_menu = gtk_menu_new();
3737         MENUITEM_ADD(optmenu_menu, menuitem, "7bit", ENC_7BIT);
3738         gtk_widget_set_sensitive(menuitem, FALSE);
3739         MENUITEM_ADD(optmenu_menu, menuitem, "8bit", ENC_8BIT);
3740         gtk_widget_set_sensitive(menuitem, FALSE);
3741         MENUITEM_ADD(optmenu_menu, menuitem, "quoted-printable", ENC_QUOTED_PRINTABLE);
3742         gtk_widget_set_sensitive(menuitem, FALSE);
3743         MENUITEM_ADD(optmenu_menu, menuitem, "base64", ENC_BASE64);
3744
3745         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), optmenu_menu);
3746
3747         SET_LABEL_AND_ENTRY(_("Path"),      path_entry,     2);
3748         SET_LABEL_AND_ENTRY(_("File name"), filename_entry, 3);
3749
3750         gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
3751                                 &cancel_btn, _("Cancel"), NULL, NULL);
3752         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
3753         gtk_widget_grab_default(ok_btn);
3754
3755         gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
3756                            GTK_SIGNAL_FUNC(attach_property_ok),
3757                            cancelled);
3758         gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
3759                            GTK_SIGNAL_FUNC(attach_property_cancel),
3760                            cancelled);
3761
3762         gtk_widget_show_all(vbox);
3763
3764         attach_prop.window           = window;
3765         attach_prop.mimetype_entry   = mimetype_entry;
3766         attach_prop.encoding_optmenu = optmenu;
3767         attach_prop.path_entry       = path_entry;
3768         attach_prop.filename_entry   = filename_entry;
3769         attach_prop.ok_btn           = ok_btn;
3770         attach_prop.cancel_btn       = cancel_btn;
3771 }
3772
3773 #undef SET_LABEL_AND_ENTRY
3774
3775 static void attach_property_ok(GtkWidget *widget, gboolean *cancelled)
3776 {
3777         *cancelled = FALSE;
3778         gtk_main_quit();
3779 }
3780
3781 static void attach_property_cancel(GtkWidget *widget, gboolean *cancelled)
3782 {
3783         *cancelled = TRUE;
3784         gtk_main_quit();
3785 }
3786
3787 static gint attach_property_delete_event(GtkWidget *widget, GdkEventAny *event,
3788                                          gboolean *cancelled)
3789 {
3790         *cancelled = TRUE;
3791         gtk_main_quit();
3792
3793         return TRUE;
3794 }
3795
3796 static void attach_property_key_pressed(GtkWidget *widget, GdkEventKey *event,
3797                                         gboolean *cancelled)
3798 {
3799         if (event && event->keyval == GDK_Escape) {
3800                 *cancelled = TRUE;
3801                 gtk_main_quit();
3802         }
3803 }
3804
3805 static void compose_exec_ext_editor(Compose *compose)
3806 {
3807         gchar tmp[64];
3808         pid_t pid;
3809         gint pipe_fds[2];
3810
3811         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.%08x",
3812                    g_get_tmp_dir(), G_DIR_SEPARATOR, (gint)compose);
3813
3814         if (pipe(pipe_fds) < 0) {
3815                 perror("pipe");
3816                 return;
3817         }
3818
3819         if ((pid = fork()) < 0) {
3820                 perror("fork");
3821                 return;
3822         }
3823
3824         if (pid != 0) {