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