sync with sylpheed 0.5.3 plus button
[claws.git] / src / compose.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gdk/gdkkeysyms.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkmenu.h>
30 #include <gtk/gtkmenuitem.h>
31 #include <gtk/gtkitemfactory.h>
32 #include <gtk/gtkcheckmenuitem.h>
33 #include <gtk/gtkoptionmenu.h>
34 #include <gtk/gtkwidget.h>
35 #include <gtk/gtkclist.h>
36 #include <gtk/gtkctree.h>
37 #include <gtk/gtkvpaned.h>
38 #include <gtk/gtkentry.h>
39 #include <gtk/gtkeditable.h>
40 #include <gtk/gtkwindow.h>
41 #include <gtk/gtksignal.h>
42 #include <gtk/gtkvbox.h>
43 #include <gtk/gtkcontainer.h>
44 #include <gtk/gtkhandlebox.h>
45 #include <gtk/gtktoolbar.h>
46 #include <gtk/gtktable.h>
47 #include <gtk/gtkhbox.h>
48 #include <gtk/gtklabel.h>
49 #include <gtk/gtkscrolledwindow.h>
50 #include <gtk/gtkthemes.h>
51 #include <gtk/gtkdnd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <ctype.h>
56 #include <unistd.h>
57 #include <time.h>
58 /* #include <sys/utsname.h> */
59 #include <stdlib.h>
60 #include <sys/wait.h>
61 #include <signal.h>
62 #include <errno.h>
63
64 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
65 #  include <wchar.h>
66 #  include <wctype.h>
67 #endif
68
69
70 #include "gtkstext.h"
71
72 #include "intl.h"
73 #include "main.h"
74 #include "mainwindow.h"
75 #include "compose.h"
76 #include "addressbook.h"
77 #include "folderview.h"
78 #include "procmsg.h"
79 #include "menu.h"
80 #include "send.h"
81 #include "news.h"
82 #include "customheader.h"
83 #include "prefs_common.h"
84 #include "prefs_account.h"
85 #include "account.h"
86 #include "filesel.h"
87 #include "procheader.h"
88 #include "procmime.h"
89 #include "statusbar.h"
90 #include "about.h"
91 #include "base64.h"
92 #include "codeconv.h"
93 #include "utils.h"
94 #include "gtkutils.h"
95 #include "socket.h"
96 #include "alertpanel.h"
97 #include "manage_window.h"
98 #include "gtkshruler.h"
99 #include "folder.h"
100 #include "addr_compl.h"
101
102 #if USE_GPGME
103 #  include "rfc2015.h"
104 #endif
105
106 #include "quote_fmt.h"
107
108 typedef enum
109 {
110         COL_MIMETYPE = 0,
111         COL_SIZE     = 1,
112         COL_NAME     = 2
113 } AttachColumnPos;
114
115 #define N_ATTACH_COLS           3
116
117 #define B64_LINE_SIZE           57
118 #define B64_BUFFSIZE            77
119
120 #define MAX_REFERENCES_LEN      999
121
122 static GdkColor quote_color = {0, 0, 0, 0xbfff};
123
124 static GList *compose_list = NULL;
125
126 static Compose *compose_create                  (PrefsAccount   *account);
127 static void compose_toolbar_create              (Compose        *compose,
128                                                  GtkWidget      *container);
129 static GtkWidget *compose_account_option_menu_create
130                                                 (Compose        *compose);
131 static void compose_destroy                     (Compose        *compose);
132
133 static gint compose_parse_header                (Compose        *compose,
134                                                  MsgInfo        *msginfo);
135 static gchar *compose_parse_references          (const gchar    *ref,
136                                                  const gchar    *msgid);
137 static void compose_quote_file                  (Compose        *compose,
138                                                  MsgInfo        *msginfo,
139                                                  FILE           *fp);
140 static gchar *compose_quote_parse_fmt           (Compose        *compose,
141                                                  MsgInfo        *msginfo,
142                                                  const gchar    *fmt);
143 static void compose_reply_set_entry             (Compose        *compose,
144                                                  MsgInfo        *msginfo,
145                                                  gboolean        to_all,
146                                                  gboolean        to_sender,
147                                                  gboolean
148                                                  followup_and_reply_to);
149 static void compose_reedit_set_entry            (Compose        *compose,
150                                                  MsgInfo        *msginfo);
151 static void compose_insert_sig                  (Compose        *compose);
152 static void compose_insert_file                 (Compose        *compose,
153                                                  const gchar    *file);
154 static void compose_attach_append               (Compose        *compose,
155                                                  const gchar    *file,
156                                                  ContentType     cnttype);
157 static void compose_attach_append_with_type(Compose *compose,
158                                             const gchar *file,
159                                             const gchar *type,
160                                             ContentType cnttype);
161 static void compose_wrap_line                   (Compose        *compose);
162 static void compose_set_title                   (Compose        *compose);
163
164 static PrefsAccount *compose_current_mail_account(void);
165 /* static gint compose_send                     (Compose        *compose); */
166 static gint compose_write_to_file               (Compose        *compose,
167                                                  const gchar    *file,
168                                                  gboolean        is_draft);
169 static gint compose_write_body_to_file          (Compose        *compose,
170                                                  const gchar    *file);
171 static gint compose_save_to_outbox              (Compose        *compose,
172                                                  const gchar    *file);
173 static gint compose_queue                       (Compose        *compose,
174                                                  const gchar    *file);
175 static void compose_write_attach                (Compose        *compose,
176                                                  FILE           *fp);
177 static gint compose_write_headers               (Compose        *compose,
178                                                  FILE           *fp,
179                                                  const gchar    *charset,
180                                                  EncodingType    encoding,
181                                                  gboolean        is_draft);
182
183 static void compose_convert_header              (gchar          *dest,
184                                                  gint            len,
185                                                  gchar          *src,
186                                                  gint            header_len);
187 static void compose_generate_msgid              (Compose        *compose,
188                                                  gchar          *buf,
189                                                  gint            len);
190
191 static void compose_attach_info_free            (AttachInfo     *ainfo);
192 static void compose_attach_remove_selected      (Compose        *compose);
193
194 static void compose_attach_property             (Compose        *compose);
195 static void compose_attach_property_create      (gboolean       *cancelled);
196 static void attach_property_ok                  (GtkWidget      *widget,
197                                                  gboolean       *cancelled);
198 static void attach_property_cancel              (GtkWidget      *widget,
199                                                  gboolean       *cancelled);
200 static gint attach_property_delete_event        (GtkWidget      *widget,
201                                                  GdkEventAny    *event,
202                                                  gboolean       *cancelled);
203 static void attach_property_key_pressed         (GtkWidget      *widget,
204                                                  GdkEventKey    *event,
205                                                  gboolean       *cancelled);
206
207 static void compose_exec_ext_editor             (Compose           *compose);
208 static gint compose_exec_ext_editor_real        (const gchar       *file);
209 static gboolean compose_ext_editor_kill         (Compose           *compose);
210 static void compose_input_cb                    (gpointer           data,
211                                                  gint               source,
212                                                  GdkInputCondition  condition);
213 static void compose_set_ext_editor_sensitive    (Compose           *compose,
214                                                  gboolean           sensitive);
215
216 static gint calc_cursor_xpos    (GtkSText       *text,
217                                  gint            extra,
218                                  gint            char_width);
219
220 /* callback functions */
221
222 static gboolean compose_edit_size_alloc (GtkEditable    *widget,
223                                          GtkAllocation  *allocation,
224                                          GtkSHRuler     *shruler);
225
226 static void toolbar_send_cb             (GtkWidget      *widget,
227                                          gpointer        data);
228 static void toolbar_send_later_cb       (GtkWidget      *widget,
229                                          gpointer        data);
230 static void toolbar_draft_cb            (GtkWidget      *widget,
231                                          gpointer        data);
232 static void toolbar_insert_cb           (GtkWidget      *widget,
233                                          gpointer        data);
234 static void toolbar_attach_cb           (GtkWidget      *widget,
235                                          gpointer        data);
236 static void toolbar_sig_cb              (GtkWidget      *widget,
237                                          gpointer        data);
238 static void toolbar_ext_editor_cb       (GtkWidget      *widget,
239                                          gpointer        data);
240 static void toolbar_linewrap_cb         (GtkWidget      *widget,
241                                          gpointer        data);
242 static void toolbar_address_cb          (GtkWidget      *widget,
243                                          gpointer        data);
244
245 static void select_account(Compose * compose, PrefsAccount * ac);
246 static void account_activated           (GtkMenuItem    *menuitem,
247                                          gpointer        data);
248
249 static void attach_selected             (GtkCList       *clist,
250                                          gint            row,
251                                          gint            column,
252                                          GdkEvent       *event,
253                                          gpointer        data);
254 static void attach_button_pressed       (GtkWidget      *widget,
255                                          GdkEventButton *event,
256                                          gpointer        data);
257 static void attach_key_pressed          (GtkWidget      *widget,
258                                          GdkEventKey    *event,
259                                          gpointer        data);
260
261 static void compose_send_cb             (gpointer        data,
262                                          guint           action,
263                                          GtkWidget      *widget);
264 static void compose_send_later_cb       (gpointer        data,
265                                          guint           action,
266                                          GtkWidget      *widget);
267
268 static void compose_draft_cb            (gpointer        data,
269                                          guint           action,
270                                          GtkWidget      *widget);
271
272 static void compose_attach_cb           (gpointer        data,
273                                          guint           action,
274                                          GtkWidget      *widget);
275 static void compose_insert_file_cb      (gpointer        data,
276                                          guint           action,
277                                          GtkWidget      *widget);
278
279 static void compose_close_cb            (gpointer        data,
280                                          guint           action,
281                                          GtkWidget      *widget);
282 static void compose_address_cb          (gpointer        data,
283                                          guint           action,
284                                          GtkWidget      *widget);
285
286 static void compose_ext_editor_cb       (gpointer        data,
287                                          guint           action,
288                                          GtkWidget      *widget);
289
290 static gint compose_delete_cb           (GtkWidget      *widget,
291                                          GdkEventAny    *event,
292                                          gpointer        data);
293 static void compose_destroy_cb          (GtkWidget      *widget,
294                                          Compose        *compose);
295
296 static void compose_cut_cb              (Compose        *compose);
297 static void compose_copy_cb             (Compose        *compose);
298 static void compose_paste_cb            (Compose        *compose);
299 static void compose_allsel_cb           (Compose        *compose);
300
301 static void compose_grab_focus_cb       (GtkWidget      *widget,
302                                          Compose        *compose);
303
304 static void compose_changed_cb          (GtkEditable    *editable,
305                                          Compose        *compose);
306 static void compose_button_press_cb     (GtkWidget      *widget,
307                                          GdkEventButton *event,
308                                          Compose        *compose);
309 #if 0
310 static void compose_key_press_cb        (GtkWidget      *widget,
311                                          GdkEventKey    *event,
312                                          Compose        *compose);
313 #endif
314
315 static void compose_toggle_to_cb        (gpointer        data,
316                                          guint           action,
317                                          GtkWidget      *widget);
318 static void compose_toggle_cc_cb        (gpointer        data,
319                                          guint           action,
320                                          GtkWidget      *widget);
321 static void compose_toggle_bcc_cb       (gpointer        data,
322                                          guint           action,
323                                          GtkWidget      *widget);
324 static void compose_toggle_replyto_cb   (gpointer        data,
325                                          guint           action,
326                                          GtkWidget      *widget);
327 static void compose_toggle_followupto_cb(gpointer        data,
328                                          guint           action,
329                                          GtkWidget      *widget);
330 static void compose_toggle_attach_cb    (gpointer        data,
331                                          guint           action,
332                                          GtkWidget      *widget);
333 static void compose_toggle_ruler_cb     (gpointer        data,
334                                          guint           action,
335                                          GtkWidget      *widget);
336 #if USE_GPGME
337 static void compose_toggle_sign_cb      (gpointer        data,
338                                          guint           action,
339                                          GtkWidget      *widget);
340 static void compose_toggle_encrypt_cb   (gpointer        data,
341                                          guint           action,
342                                          GtkWidget      *widget);
343 #endif
344 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
345                                              GtkWidget *widget);
346
347 static void compose_attach_drag_received_cb (GtkWidget          *widget,
348                                              GdkDragContext     *drag_context,
349                                              gint                x,
350                                              gint                y,
351                                              GtkSelectionData   *data,
352                                              guint               info,
353                                              guint               time,
354                                              gpointer            user_data);
355 static void compose_insert_drag_received_cb (GtkWidget          *widget,
356                                              GdkDragContext     *drag_context,
357                                              gint                x,
358                                              gint                y,
359                                              GtkSelectionData   *data,
360                                              guint               info,
361                                              guint               time,
362                                              gpointer            user_data);
363
364 static void to_activated                (GtkWidget      *widget,
365                                          Compose        *compose);
366 static void newsgroups_activated        (GtkWidget      *widget,
367                                          Compose        *compose);
368 static void subject_activated           (GtkWidget      *widget,
369                                          Compose        *compose);
370 static void cc_activated                (GtkWidget      *widget,
371                                          Compose        *compose);
372 static void bcc_activated               (GtkWidget      *widget,
373                                          Compose        *compose);
374 static void replyto_activated           (GtkWidget      *widget,
375                                          Compose        *compose);
376 static void followupto_activated        (GtkWidget      *widget,
377                                          Compose        *compose);
378 static void compose_attach_parts(Compose * compose,
379                                  MsgInfo * msginfo);
380
381 static gchar *compose_quote_fmt         (Compose        *compose,
382                                          MsgInfo        *msginfo,
383                                          const gchar    *fmt,
384                                          const gchar    * qmark);
385
386 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
387                                   gboolean to_all,
388                                   gboolean ignore_replyto,
389                                   gboolean followup_and_reply_to);
390
391 Compose * compose_generic_new (PrefsAccount     *account,
392                                const gchar      *to,
393                                FolderItem       *item);
394
395 static GtkItemFactoryEntry compose_popup_entries[] =
396 {
397         {N_("/_Add..."),        NULL, compose_attach_cb, 0, NULL},
398         {N_("/_Remove"),        NULL, compose_attach_remove_selected, 0, NULL},
399         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
400         {N_("/_Property..."),   NULL, compose_attach_property, 0, NULL}
401 };
402
403 static GtkItemFactoryEntry compose_entries[] =
404 {
405         {N_("/_File"),                          NULL, NULL, 0, "<Branch>"},
406         {N_("/_File/_Attach file"),             "<control>M", compose_attach_cb, 0, NULL},
407         {N_("/_File/_Insert file"),             "<control>I", compose_insert_file_cb, 0, NULL},
408         {N_("/_File/Insert si_gnature"),        "<control>G", compose_insert_sig, 0, NULL},
409         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
410         {N_("/_File/_Close"),                   "<alt>W", compose_close_cb, 0, NULL},
411
412         {N_("/_Edit"),             NULL,         NULL,  0, "<Branch>"},
413         {N_("/_Edit/_Undo"),       "<control>Z", NULL,  0, NULL},
414         {N_("/_Edit/_Redo"),       "<control>Y", NULL,  0, NULL},
415         {N_("/_Edit/---"),         NULL,         NULL,  0, "<Separator>"},
416         {N_("/_Edit/Cu_t"),        "<control>X", compose_cut_cb,    0, NULL},
417         {N_("/_Edit/_Copy"),       "<control>C", compose_copy_cb,   0, NULL},
418         {N_("/_Edit/_Paste"),      "<control>V", compose_paste_cb,  0, NULL},
419         {N_("/_Edit/Select _all"), "<control>A", compose_allsel_cb, 0, NULL},
420         {N_("/_Edit/---"),         NULL,         NULL,  0, "<Separator>"},
421         {N_("/_Edit/Wrap long _lines"), "<alt>L", compose_wrap_line, 0, NULL},
422         {N_("/_Edit/Edit with e_xternal editor"), "<alt>X",
423                                         compose_ext_editor_cb,  0, NULL},
424
425         {N_("/_Message"),               NULL,           NULL,   0, "<Branch>"},
426         {N_("/_Message/_Send"),         "<shift><control>S",
427                                         compose_send_cb, 0, NULL},
428         {N_("/_Message/Send _later"),   "<shift><alt>S",
429                                         compose_send_later_cb,  0, NULL},
430         {N_("/_Message/Save to _draft folder"),
431                                         "<alt>D",       compose_draft_cb, 0, NULL},
432         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
433         {N_("/_Message/_To"),           NULL, compose_toggle_to_cb, 0, "<ToggleItem>"},
434         {N_("/_Message/_Cc"),           NULL, compose_toggle_cc_cb, 0, "<ToggleItem>"},
435         {N_("/_Message/_Bcc"),          NULL, compose_toggle_bcc_cb, 0, "<ToggleItem>"},
436         {N_("/_Message/_Reply to"),     NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
437         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
438         {N_("/_Message/_Followup to"),  NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
439         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
440         {N_("/_Message/_Attach"),       NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
441 #if USE_GPGME
442         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
443         {N_("/_Message/Si_gn"),         NULL, compose_toggle_sign_cb, 0, "<ToggleItem>"},
444         {N_("/_Message/_Encrypt"),      NULL, compose_toggle_encrypt_cb, 0, "<ToggleItem>"},
445 #endif /* USE_GPGME */
446         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
447         {N_("/_Message/_Request Return Receipt"),       NULL, compose_toggle_return_receipt_cb, 0, "<ToggleItem>"},
448         {N_("/_Tool"),                  NULL, NULL,     0, "<Branch>"},
449         {N_("/_Tool/Show _ruler"),      NULL, compose_toggle_ruler_cb,  0, "<ToggleItem>"},
450         {N_("/_Tool/_Address book"),    "<alt>A",       compose_address_cb, 0, NULL},
451         {N_("/_Help"),                  NULL, NULL,     0, "<LastBranch>"},
452         {N_("/_Help/_About"),           NULL,           about_show,     0, NULL}
453 };
454
455 static GtkTargetEntry compose_mime_types[] =
456 {
457         {"text/uri-list", 0, 0}
458 };
459
460 Compose * compose_new(PrefsAccount *account)
461 {
462         return compose_generic_new(account, NULL, NULL);
463 }
464
465 Compose * compose_new_with_recipient(PrefsAccount *account, const gchar *to)
466 {
467         return compose_generic_new(account, to, NULL);
468 }
469
470 Compose * compose_new_with_folderitem(PrefsAccount *account, FolderItem *item)
471 {
472         return compose_generic_new(account, NULL, item);
473 }
474
475 Compose * compose_generic_new(PrefsAccount *account, const gchar *to, FolderItem *item)
476 {
477         Compose *compose;
478
479         if (!account) account = cur_account;
480         g_return_val_if_fail(account != NULL, NULL);
481
482         compose = compose_create(account);
483         compose->mode = COMPOSE_NEW;
484
485         if (prefs_common.auto_sig)
486                 compose_insert_sig(compose);
487         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
488         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
489
490         if (account->protocol != A_NNTP) {
491                 if (to) {
492                         compose_entry_append(compose, to, COMPOSE_TO);
493                         gtk_widget_grab_focus(compose->subject_entry);
494                 } else {
495                         if(item && item->prefs->enable_default_to) {
496                                 compose_entry_append(compose, item->prefs->default_to, COMPOSE_TO);
497                         } else {
498                                 gtk_widget_grab_focus(compose->to_entry);
499                         }
500                 }
501                 if(item && item->prefs->request_return_receipt) {
502                         GtkItemFactory *ifactory;
503                 
504                         ifactory = gtk_item_factory_from_widget(compose->menubar);
505                         menu_set_toggle(ifactory, "/Message/Request Return Receipt", TRUE);
506                 }
507         } else {
508                 if (to) {
509                         compose_entry_append(compose, to, COMPOSE_NEWSGROUPS);
510                         gtk_widget_grab_focus(compose->subject_entry);
511                 } else
512                         gtk_widget_grab_focus(compose->newsgroups_entry);
513         }
514
515         return compose;
516 }
517
518 #define CHANGE_FLAGS(msginfo) \
519 { \
520 if (msginfo->folder->folder->change_flags != NULL) \
521 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
522                                       msginfo->folder, \
523                                       msginfo); \
524 }
525
526 /*
527 Compose * compose_new_followup_and_replyto(PrefsAccount *account,
528                                            const gchar *followupto, gchar * to)
529 {
530         Compose *compose;
531
532         if (!account) account = cur_account;
533         g_return_val_if_fail(account != NULL, NULL);
534         g_return_val_if_fail(account->protocol != A_NNTP, NULL);
535
536         compose = compose_create(account);
537         compose->mode = COMPOSE_NEW;
538
539         if (prefs_common.auto_sig)
540                 compose_insert_sig(compose);
541         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
542         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
543
544         compose_entry_append(compose, to, COMPOSE_TO);
545         compose_entry_append(compose, followupto, COMPOSE_NEWSGROUPS);
546         gtk_widget_grab_focus(compose->subject_entry);
547
548         return compose;
549 }
550 */
551
552 void compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
553                    gboolean ignore_replyto)
554 {
555         compose_generic_reply(msginfo, quote, to_all, ignore_replyto, FALSE);
556 }
557
558 void compose_followup_and_reply_to(MsgInfo *msginfo, gboolean quote,
559                                    gboolean to_all,
560                                    gboolean ignore_replyto)
561 {
562         compose_generic_reply(msginfo, quote, to_all, ignore_replyto, TRUE);
563 }
564
565 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
566                                   gboolean to_all,
567                                   gboolean ignore_replyto,
568                                   gboolean followup_and_reply_to)
569 {
570         Compose *compose;
571         PrefsAccount *account;
572         PrefsAccount *reply_account;
573         GtkSText *text;
574
575         g_return_if_fail(msginfo != NULL);
576         g_return_if_fail(msginfo->folder != NULL);
577
578         account = msginfo->folder->folder->account;
579         if (!account && msginfo->to && prefs_common.reply_account_autosel) {
580                 gchar *to;
581                 Xstrdup_a(to, msginfo->to, return);
582                 extract_address(to);
583                 account = account_find_from_address(to);
584         }
585         if (!account) account = cur_account;
586         g_return_if_fail(account != NULL);
587
588         if (ignore_replyto && account->protocol == A_NNTP &&
589             !followup_and_reply_to) {
590                 reply_account =
591                         account_find_from_address(account->address);
592                 if (!reply_account)
593                         reply_account = compose_current_mail_account();
594                 if (!reply_account)
595                         return;
596         } else
597                 reply_account = account;
598         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
599         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
600
601         CHANGE_FLAGS(msginfo);
602
603         compose = compose_create(reply_account);
604         compose->mode = COMPOSE_REPLY;
605
606
607         if (followup_and_reply_to) {
608                 gtk_widget_show(compose->to_hbox);
609                 gtk_widget_show(compose->to_entry);
610                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 1, 4);
611                 compose->use_to = TRUE;
612         }
613
614         if (compose_parse_header(compose, msginfo) < 0) return;
615         compose_reply_set_entry(compose, msginfo, to_all, ignore_replyto,
616                                 followup_and_reply_to);
617
618         text = GTK_STEXT(compose->text);
619         gtk_stext_freeze(text);
620
621         if (quote) {
622                 FILE *fp;
623                 gchar *quote_str;
624
625                 if ((fp = procmime_get_text_part(msginfo)) == NULL)
626                         g_warning(_("Can't get text part\n"));
627                 else {
628                         gchar * qmark;
629
630                         if (prefs_common.quotemark && *prefs_common.quotemark)
631                                 qmark = prefs_common.quotemark;
632                         else
633                                 qmark = "> ";
634
635                         quote_str = compose_quote_fmt(compose, msginfo,
636                                                       prefs_common.quotefmt,
637                                                       qmark);
638
639                         /*
640                         quote_str = compose_quote_parse_fmt
641                                 (compose, msginfo, prefs_common.quotefmt);
642                         */
643
644                         if (quote_str != NULL)
645                                 gtk_stext_insert(text, NULL, NULL, NULL,
646                                                  quote_str, -1);
647                         /*                      g_free(quote_str); */
648                         /* compose_quote_file(compose, msginfo, fp); */
649                         fclose(fp);
650                 }
651         }
652
653         if (prefs_common.auto_sig)
654                 compose_insert_sig(compose);
655         gtk_editable_set_position(GTK_EDITABLE(text), 0);
656         gtk_stext_set_point(text, 0);
657
658         gtk_stext_thaw(text);
659         gtk_widget_grab_focus(compose->text);
660 }
661
662
663 static gchar *procmime_get_file_name(MimeInfo *mimeinfo)
664 {
665         gchar *base;
666         gchar *filename;
667
668         g_return_val_if_fail(mimeinfo != NULL, NULL);
669
670         base = mimeinfo->filename ? mimeinfo->filename
671                 : mimeinfo->name ? mimeinfo->name : NULL;
672
673         if (MIME_TEXT_HTML == mimeinfo->mime_type && base == NULL){
674                 filename = g_strdup_printf("%s%smimetmp%i.html",
675                                            get_mime_tmp_dir(),
676                                            G_DIR_SEPARATOR_S,
677                                            mimeinfo);
678                 return filename;
679         }
680         else {
681                 base = base ? base : "";
682                 base = g_basename(base);
683                 if (*base == '\0') {
684                         filename = g_strdup_printf("%s%smimetmp%i",
685                                                    get_mime_tmp_dir(),
686                                                    G_DIR_SEPARATOR_S,
687                                                    mimeinfo);
688                         return filename;
689                 }
690         }
691
692         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
693                                base, NULL);
694
695         return filename;
696 }
697
698 static gchar * mime_extract_file(gchar * source, MimeInfo *partinfo)
699 {
700         gchar *filename;
701
702         if (!partinfo) return;
703
704         filename = procmime_get_file_name(partinfo);
705
706         if (procmime_get_part(filename, source, partinfo) < 0)
707                 alertpanel_error
708                         (_("Can't get the part of multipart message."));
709
710         return filename;
711 }
712
713 static void compose_attach_parts(Compose * compose,
714                                  MsgInfo * msginfo)
715 {
716
717         FILE *fp;
718         gchar *file;
719         MimeInfo *mimeinfo;
720         MsgInfo *tmpmsginfo;
721         gchar *p;
722         gchar *boundary;
723         gint boundary_len = 0;
724         gchar buf[BUFFSIZE];
725         glong fpos, prev_fpos;
726         gint npart;
727         gchar * source;
728         gchar * filename;
729
730         g_return_if_fail(msginfo != NULL);
731         
732 #if USE_GPGME
733         for (;;) {
734                 if ((fp = procmsg_open_message(msginfo)) == NULL) return;
735                 mimeinfo = procmime_scan_mime_header(fp);
736                 if (!mimeinfo) break;
737
738                 if (!MSG_IS_ENCRYPTED(msginfo->flags) &&
739                     rfc2015_is_encrypted(mimeinfo)) {
740                         MSG_SET_TMP_FLAGS(msginfo->flags, MSG_ENCRYPTED);
741                 }
742                 if (MSG_IS_ENCRYPTED(msginfo->flags) &&
743                     !msginfo->plaintext_file  &&
744                     !msginfo->decryption_failed) {
745                         rfc2015_decrypt_message(msginfo, mimeinfo, fp);
746                         if (msginfo->plaintext_file &&
747                             !msginfo->decryption_failed) {
748                                 fclose(fp);
749                                 continue;
750                         }
751                 }
752                 
753                 break;
754         }
755 #else /* !USE_GPGME */
756         if ((fp = procmsg_open_message(msginfo)) == NULL) return;
757         mimeinfo = procmime_scan_mime_header(fp);
758 #endif /* USE_GPGME */
759
760         fclose(fp);
761         if (!mimeinfo) return;
762         if (mimeinfo->mime_type == MIME_TEXT)
763                 return;
764
765         if ((fp = procmsg_open_message(msginfo)) == NULL) return;
766
767         g_return_if_fail(mimeinfo != NULL);
768         g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
769
770         if (mimeinfo->mime_type == MIME_MULTIPART) {
771                 g_return_if_fail(mimeinfo->boundary != NULL);
772                 g_return_if_fail(mimeinfo->sub == NULL);
773         }
774         g_return_if_fail(fp != NULL);
775
776         boundary = mimeinfo->boundary;
777
778         if (boundary) {
779                 boundary_len = strlen(boundary);
780
781                 /* look for first boundary */
782                 while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
783                         if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
784                 if (!p) {
785                         fclose(fp);
786                         return;
787                 }
788         }
789
790         if ((fpos = ftell(fp)) < 0) {
791                 perror("ftell");
792                 fclose(fp);
793                 return;
794         }
795
796         for (npart = 0;; npart++) {
797                 MimeInfo *partinfo;
798                 gboolean eom = FALSE;
799
800                 prev_fpos = fpos;
801
802                 partinfo = procmime_scan_mime_header(fp);
803                 if (!partinfo) break;
804
805                 if (npart != 0)
806                         procmime_mimeinfo_insert(mimeinfo, partinfo);
807                 else
808                         procmime_mimeinfo_free(partinfo);
809
810                 /* look for next boundary */
811                 buf[0] = '\0';
812                 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
813                         if (IS_BOUNDARY(buf, boundary, boundary_len)) {
814                                 if (buf[2 + boundary_len]     == '-' &&
815                                     buf[2 + boundary_len + 1] == '-')
816                                         eom = TRUE;
817                                 break;
818                         }
819                 }
820                 if (p == NULL)
821                         eom = TRUE;     /* broken MIME message */
822                 fpos = ftell(fp);
823
824                 partinfo->size = fpos - prev_fpos - strlen(buf);
825
826                 if (eom) break;
827         }
828
829         source = procmsg_get_message_file_path(msginfo);
830
831         g_return_if_fail(mimeinfo != NULL);
832
833         if (!mimeinfo->main && mimeinfo->parent)
834                 {
835                         filename = mime_extract_file(source, mimeinfo);
836
837                         compose_attach_append_with_type(compose, filename,
838                                                         mimeinfo->content_type,
839                                                         mimeinfo->mime_type);
840
841                         g_free(filename);
842                 }
843
844         if (mimeinfo->sub && mimeinfo->sub->children)
845                 {
846                         filename = mime_extract_file(source, mimeinfo->sub);
847
848                         compose_attach_append_with_type(compose, filename,
849                                                         mimeinfo->content_type,
850                                                         mimeinfo->sub->mime_type);
851
852                         g_free(filename);
853                 }
854
855         if (mimeinfo->children) {
856                 MimeInfo *child;
857
858                 child = mimeinfo->children;
859                 while (child) {
860                         filename = mime_extract_file(source, child);
861
862                         compose_attach_append_with_type(compose, filename,
863                                                         child->content_type,
864                                                         child->mime_type);
865
866                         g_free(filename);
867
868                         child = child->next;
869                 }
870         }
871
872         fclose(fp);
873
874         procmime_mimeinfo_free_all(mimeinfo);
875 }
876
877
878 #define INSERT_FW_HEADER(var, hdr) \
879 if (msginfo->var && *msginfo->var) { \
880         gtk_stext_insert(text, NULL, NULL, NULL, hdr, -1); \
881         gtk_stext_insert(text, NULL, NULL, NULL, msginfo->var, -1); \
882         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1); \
883 }
884
885 Compose * compose_forward(PrefsAccount * account, MsgInfo *msginfo,
886                           gboolean as_attach)
887 {
888         Compose *compose;
889         /*      PrefsAccount *account; */
890         GtkSText *text;
891         FILE *fp;
892         gchar buf[BUFFSIZE];
893
894         g_return_val_if_fail(msginfo != NULL, NULL);
895         g_return_val_if_fail(msginfo->folder != NULL, NULL);
896
897         if (account == NULL) {
898                 account = cur_account;
899                 /*
900                 account = msginfo->folder->folder->account;
901                 if (!account) account = cur_account;
902                 */
903         }
904         g_return_val_if_fail(account != NULL, NULL);
905
906         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
907         MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
908         CHANGE_FLAGS(msginfo);
909
910         compose = compose_create(account);
911         compose->mode = COMPOSE_FORWARD;
912
913         if (msginfo->subject && *msginfo->subject) {
914                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Fw: ");
915                 gtk_entry_append_text(GTK_ENTRY(compose->subject_entry),
916                                       msginfo->subject);
917         }
918
919         text = GTK_STEXT(compose->text);
920         gtk_stext_freeze(text);
921
922         if (as_attach) {
923                 gchar *msgfile;
924
925                 msgfile = procmsg_get_message_file_path(msginfo);
926                 if (!is_file_exist(msgfile))
927                         g_warning(_("%s: file not exist\n"), msgfile);
928                 else
929                         compose_attach_append(compose, msgfile,
930                                               MIME_MESSAGE_RFC822);
931
932                 g_free(msgfile);
933         } else {
934                 FILE *fp;
935                 gchar *quote_str;
936
937                 if ((fp = procmime_get_text_part(msginfo)) == NULL)
938                         g_warning(_("Can't get text part\n"));
939                 else {
940                         gchar * qmark;
941
942                         if (prefs_common.fw_quotemark &&
943                             *prefs_common.fw_quotemark)
944                                 qmark = prefs_common.fw_quotemark;
945                         else
946                                 qmark = "> ";
947
948                         quote_str = compose_quote_fmt(compose, msginfo,
949                                                       prefs_common.fw_quotefmt,
950                                                       qmark);
951
952                         if (quote_str != NULL)
953                                 gtk_stext_insert(text, NULL, NULL, NULL,
954                                                  quote_str, -1);
955
956                         fclose(fp);
957                 }
958
959                 compose_attach_parts(compose, msginfo);
960         }
961
962         if (prefs_common.auto_sig)
963                 compose_insert_sig(compose);
964         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
965         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
966
967         gtk_stext_thaw(text);
968         if (account->protocol != A_NNTP)
969                 gtk_widget_grab_focus(compose->to_entry);
970         else
971                 gtk_widget_grab_focus(compose->newsgroups_entry);
972
973         return compose;
974 }
975
976 #undef INSERT_FW_HEADER
977
978 Compose * compose_forward_multiple(PrefsAccount * account, 
979                           GSList *msginfo_list) 
980 {
981         Compose *compose;
982         GtkSText *text;
983         GSList *msginfo;
984         gchar *msgfile;
985
986         g_return_val_if_fail(msginfo_list != NULL, NULL);
987         
988         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
989                 if ( ((MsgInfo *)msginfo->data)->folder == NULL )
990                         return NULL;
991         }
992         
993         if (account == NULL) {
994                 account = cur_account;
995                 /*
996                 account = msginfo->folder->folder->account;
997                 if (!account) account = cur_account;
998                 */
999         }
1000         g_return_val_if_fail(account != NULL, NULL);
1001
1002         compose = compose_create(account);
1003         compose->mode = COMPOSE_FORWARD;
1004
1005         text = GTK_STEXT(compose->text);
1006         gtk_stext_freeze(text);
1007
1008         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1009                 msgfile = procmsg_get_message_file_path((MsgInfo *)msginfo->data);
1010                 if (!is_file_exist(msgfile))
1011                         g_warning(_("%s: file not exist\n"), msgfile);
1012                 else
1013                         compose_attach_append(compose, msgfile,
1014                                 MIME_MESSAGE_RFC822);
1015                 g_free(msgfile);
1016         }
1017
1018         if (prefs_common.auto_sig)
1019                 compose_insert_sig(compose);
1020         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
1021         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
1022
1023         gtk_stext_thaw(text);
1024         if (account->protocol != A_NNTP)
1025                 gtk_widget_grab_focus(compose->to_entry);
1026         else
1027                 gtk_widget_grab_focus(compose->newsgroups_entry);
1028
1029         return compose;
1030 }
1031
1032 void compose_reedit(MsgInfo *msginfo)
1033 {
1034         Compose *compose;
1035         PrefsAccount *account;
1036         GtkSText *text;
1037         FILE *fp;
1038         gchar buf[BUFFSIZE];
1039
1040         g_return_if_fail(msginfo != NULL);
1041         g_return_if_fail(msginfo->folder != NULL);
1042
1043         account = msginfo->folder->folder->account;
1044         if (!account) account = cur_account;
1045         g_return_if_fail(account != NULL);
1046
1047         compose = compose_create(account);
1048         compose->mode = COMPOSE_REEDIT_DRAFT;
1049         compose->targetinfo = procmsg_msginfo_copy(msginfo);
1050
1051         if (compose_parse_header(compose, msginfo) < 0) return;
1052         compose_reedit_set_entry(compose, msginfo);
1053
1054         text = GTK_STEXT(compose->text);
1055         gtk_stext_freeze(text);
1056
1057         if ((fp = procmime_get_text_part(msginfo)) == NULL)
1058                 g_warning(_("Can't get text part\n"));
1059         else {
1060                 while (fgets(buf, sizeof(buf), fp) != NULL)
1061                         gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1062                 fclose(fp);
1063         }
1064         compose_attach_parts(compose, msginfo);
1065
1066         gtk_stext_thaw(text);
1067         gtk_widget_grab_focus(compose->text);
1068 }
1069
1070 GList *compose_get_compose_list(void)
1071 {
1072         return compose_list;
1073 }
1074
1075 void compose_entry_append(Compose *compose, const gchar *address,
1076                           ComposeEntryType type)
1077 {
1078         GtkEntry *entry;
1079         const gchar *text;
1080
1081         if (!address || *address == '\0') return;
1082
1083         switch (type) {
1084         case COMPOSE_CC:
1085                 entry = GTK_ENTRY(compose->cc_entry);
1086                 break;
1087         case COMPOSE_BCC:
1088                 entry = GTK_ENTRY(compose->bcc_entry);
1089                 break;
1090         case COMPOSE_NEWSGROUPS:
1091                 entry = GTK_ENTRY(compose->newsgroups_entry);
1092                 break;
1093         case COMPOSE_TO:
1094         default:
1095                 entry = GTK_ENTRY(compose->to_entry);
1096                 break;
1097         }
1098
1099         text = gtk_entry_get_text(entry);
1100         if (*text != '\0')
1101                 gtk_entry_append_text(entry, ", ");
1102         gtk_entry_append_text(entry, address);
1103 }
1104
1105 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
1106 {
1107         static HeaderEntry hentry[] = {{"Reply-To:",    NULL, TRUE},
1108                                        {"Cc:",          NULL, FALSE},
1109                                        {"References:",  NULL, FALSE},
1110                                        {"Bcc:",         NULL, FALSE},
1111                                        {"Newsgroups:",  NULL, FALSE},
1112                                        {"Followup-To:", NULL, FALSE},
1113                                        {NULL,           NULL, FALSE}};
1114
1115         enum
1116         {
1117                 H_REPLY_TO      = 0,
1118                 H_CC            = 1,
1119                 H_REFERENCES    = 2,
1120                 H_BCC           = 3,
1121                 H_NEWSGROUPS    = 4,
1122                 H_FOLLOWUP_TO   = 5
1123         };
1124
1125         FILE *fp;
1126
1127         g_return_val_if_fail(msginfo != NULL, -1);
1128
1129         if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
1130         procheader_get_header_fields(fp, hentry);
1131         fclose(fp);
1132
1133         if (hentry[H_REPLY_TO].body != NULL) {
1134                 conv_unmime_header_overwrite(hentry[H_REPLY_TO].body);
1135                 compose->replyto = hentry[H_REPLY_TO].body;
1136                 hentry[H_REPLY_TO].body = NULL;
1137         }
1138         if (hentry[H_CC].body != NULL) {
1139                 conv_unmime_header_overwrite(hentry[H_CC].body);
1140                 compose->cc = hentry[H_CC].body;
1141                 hentry[H_CC].body = NULL;
1142         }
1143         if (hentry[H_REFERENCES].body != NULL) {
1144                 if (compose->mode == COMPOSE_REEDIT_DRAFT)
1145                         compose->references = hentry[H_REFERENCES].body;
1146                 else {
1147                         compose->references = compose_parse_references
1148                                 (hentry[H_REFERENCES].body, msginfo->msgid);
1149                         g_free(hentry[H_REFERENCES].body);
1150                 }
1151                 hentry[H_REFERENCES].body = NULL;
1152         }
1153         if (hentry[H_BCC].body != NULL) {
1154                 if (compose->mode == COMPOSE_REEDIT_DRAFT) {
1155                         conv_unmime_header_overwrite(hentry[H_BCC].body);
1156                         compose->bcc = hentry[H_BCC].body;
1157                 } else
1158                         g_free(hentry[H_BCC].body);
1159                 hentry[H_BCC].body = NULL;
1160         }
1161         if (hentry[H_NEWSGROUPS].body != NULL) {
1162                 compose->newsgroups = hentry[H_NEWSGROUPS].body;
1163                 hentry[H_NEWSGROUPS].body = NULL;
1164         }
1165         if (hentry[H_FOLLOWUP_TO].body != NULL) {
1166                 conv_unmime_header_overwrite(hentry[H_FOLLOWUP_TO].body);
1167                 compose->followup_to = hentry[H_FOLLOWUP_TO].body;
1168                 hentry[H_FOLLOWUP_TO].body = NULL;
1169         }
1170
1171         if (compose->mode == COMPOSE_REEDIT_DRAFT && msginfo->inreplyto)
1172                 compose->inreplyto = g_strdup(msginfo->inreplyto);
1173         else if (compose->mode != COMPOSE_REEDIT_DRAFT &&
1174                  msginfo->msgid && *msginfo->msgid) {
1175                 compose->inreplyto = g_strdup(msginfo->msgid);
1176
1177                 if (!compose->references) {
1178                         if (msginfo->inreplyto && *msginfo->inreplyto)
1179                                 compose->references =
1180                                         g_strdup_printf("<%s>\n\t<%s>",
1181                                                         msginfo->inreplyto,
1182                                                         msginfo->msgid);
1183                         else
1184                                 compose->references =
1185                                         g_strconcat("<", msginfo->msgid, ">",
1186                                                     NULL);
1187                 }
1188         }
1189
1190         return 0;
1191 }
1192
1193 static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
1194 {
1195         GSList *ref_id_list, *cur;
1196         GString *new_ref;
1197         gchar *new_ref_str;
1198
1199         ref_id_list = references_list_append(NULL, ref);
1200         if (!ref_id_list) return NULL;
1201         if (msgid && *msgid)
1202                 ref_id_list = g_slist_append(ref_id_list, g_strdup(msgid));
1203
1204         for (;;) {
1205                 gint len = 0;
1206
1207                 for (cur = ref_id_list; cur != NULL; cur = cur->next)
1208                         /* "<" + Message-ID + ">" + CR+LF+TAB */
1209                         len += strlen((gchar *)cur->data) + 5;
1210
1211                 if (len > MAX_REFERENCES_LEN) {
1212                         /* remove second message-ID */
1213                         if (ref_id_list && ref_id_list->next &&
1214                             ref_id_list->next->next) {
1215                                 g_free(ref_id_list->next->data);
1216                                 ref_id_list = g_slist_remove
1217                                         (ref_id_list, ref_id_list->next->data);
1218                         } else {
1219                                 slist_free_strings(ref_id_list);
1220                                 g_slist_free(ref_id_list);
1221                                 return NULL;
1222                         }
1223                 } else
1224                         break;
1225         }
1226
1227         new_ref = g_string_new("");
1228         for (cur = ref_id_list; cur != NULL; cur = cur->next) {
1229                 if (new_ref->len > 0)
1230                         g_string_append(new_ref, "\n\t");
1231                 g_string_sprintfa(new_ref, "<%s>", (gchar *)cur->data);
1232         }
1233
1234         slist_free_strings(ref_id_list);
1235         g_slist_free(ref_id_list);
1236
1237         new_ref_str = new_ref->str;
1238         g_string_free(new_ref, FALSE);
1239
1240         return new_ref_str;
1241 }
1242
1243 /*
1244 static void compose_quote_file(Compose *compose, MsgInfo *msginfo, FILE *fp)
1245 {
1246         GtkSText *text = GTK_STEXT(compose->text);
1247         gchar *qmark;
1248         gchar *quote_str;
1249         GdkColor *qcolor = NULL;
1250         gchar buf[BUFFSIZE];
1251         gint qlen;
1252         gchar *linep, *cur, *leftp;
1253         gint line_len, cur_len;
1254         gint wrap_len;
1255         gint str_len;
1256         gint ch_len;
1257
1258         // if (prefs_common.enable_color) qcolor = &quote_color;
1259         if (prefs_common.quotemark && *prefs_common.quotemark)
1260                 qmark = prefs_common.quotemark;
1261         else
1262                 qmark = "> ";
1263         quote_str = compose_quote_parse_fmt(compose, msginfo, qmark);
1264         g_return_if_fail(quote_str != NULL);
1265         qlen = strlen(quote_str);
1266
1267         if (!prefs_common.linewrap_quote ||
1268             prefs_common.linewrap_len <= qlen) {
1269                 while (fgets(buf, sizeof(buf), fp) != NULL) {
1270                         gtk_stext_insert(text, NULL, qcolor, NULL,
1271                                         quote_str, -1);
1272                         gtk_stext_insert(text, NULL, qcolor, NULL, buf, -1);
1273                 }
1274                 g_free(quote_str);
1275                 return;
1276         }
1277
1278         wrap_len = prefs_common.linewrap_len - qlen;
1279
1280         while (fgets(buf, sizeof(buf), fp) != NULL) {
1281                 strretchomp(buf);
1282                 str_len = strlen(buf);
1283
1284                 if (str_len <= wrap_len) {
1285                         gtk_stext_insert(text, NULL, qcolor, NULL,
1286                                         quote_str, -1);
1287                         gtk_stext_insert(text, NULL, qcolor, NULL, buf, -1);
1288                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
1289                         continue;
1290                 }
1291
1292                 linep = cur = leftp = buf;
1293                 line_len = cur_len = 0;
1294
1295                 while (*cur != '\0') {
1296                         ch_len = mblen(cur, MB_CUR_MAX);
1297                         if (ch_len < 0) ch_len = 1;
1298
1299                         if (ch_len == 1 && isspace(*cur)) {
1300                                 linep = cur + ch_len;
1301                                 line_len = cur_len + ch_len;
1302                         }
1303
1304                         if (cur_len + ch_len > wrap_len && line_len > 0) {
1305                                 gtk_stext_insert(text, NULL, qcolor, NULL,
1306                                                 quote_str, -1);
1307
1308                                 if (isspace(*(linep - 1)))
1309                                         gtk_stext_insert(text, NULL,
1310                                                         qcolor, NULL,
1311                                                         leftp, line_len - 1);
1312                                 else
1313                                         gtk_stext_insert(text, NULL,
1314                                                         qcolor, NULL,
1315                                                         leftp, line_len);
1316                                 gtk_stext_insert(text, NULL, NULL, NULL,
1317                                                 "\n", 1);
1318
1319                                 leftp = linep;
1320                                 cur_len = cur_len - line_len + ch_len;
1321                                 line_len = 0;
1322                                 cur += ch_len;
1323                                 continue;
1324                         }
1325
1326                         if (ch_len > 1) {
1327                                 linep = cur + ch_len;
1328                                 line_len = cur_len + ch_len;
1329                         }
1330                         cur_len += ch_len;
1331                         cur += ch_len;
1332                 }
1333
1334                 if (*leftp) {
1335                         gtk_stext_insert(text, NULL, qcolor, NULL,
1336                                         quote_str, -1);
1337                         gtk_stext_insert(text, NULL, qcolor, NULL, leftp, -1);
1338                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
1339                 }
1340         }
1341
1342         g_free(quote_str);
1343 }
1344 */
1345
1346 /*
1347 static gchar *compose_quote_parse_fmt(Compose *compose, MsgInfo *msginfo,
1348                                       const gchar *fmt)
1349 {
1350         gchar *ext_str;
1351         size_t buf_len = 1024;
1352         size_t ext_len = 0;
1353         gchar *str;
1354         gchar *mbs;
1355         wchar_t *wcsfmt;
1356         wchar_t *sp;
1357         gchar tmp[3];
1358
1359         if (!fmt || *fmt == '\0') return 0;
1360
1361         Xalloca(mbs, sizeof(wchar_t) + 1, return 0);
1362         Xalloca(wcsfmt, (strlen(fmt) + 1) * sizeof(wchar_t), return 0);
1363         mbstowcs(wcsfmt, fmt, strlen(fmt) + 1);
1364         sp = wcsfmt;
1365
1366         ext_str = g_malloc(sizeof(gchar) * buf_len);
1367         g_return_val_if_fail(ext_str != NULL, NULL);
1368
1369         while (*sp) {
1370                 gint len;
1371
1372                 len = wctomb(mbs, *sp);
1373                 mbs[len] = '\0';
1374
1375                 if (*mbs == '%') {
1376                         gchar *p;
1377
1378                         wctomb(mbs, *(++sp));
1379                         str = NULL;
1380
1381                         switch (*mbs) {
1382                         case 'd':
1383                                 str = msginfo->date;
1384                                 sp++;
1385                                 break;
1386                         case 'f':
1387                                 str = msginfo->from;
1388                                 sp++;
1389                                 break;
1390                         case 'I':
1391                                 if (!msginfo->fromname) {sp++; break;}
1392                                 p = msginfo->fromname;
1393                                 tmp[0] = tmp[1] = tmp[2] = '\0';
1394
1395                                 if (*p && isalnum(*p))
1396                                         tmp[0] = toupper(*p);
1397                                 else {
1398                                         sp++;
1399                                         break;
1400                                 }
1401
1402                                 while (*p) {
1403                                         while (*p && !isspace(*p)) p++;
1404                                         while (*p && isspace(*p)) p++;
1405                                         if (*p && isalnum(*p))
1406                                                 tmp[1] = toupper(*p);
1407                                 }
1408
1409                                 if (tmp[1]) str = tmp;
1410                                 sp++;
1411                                 break;
1412                         case 'n':
1413                                 str = msginfo->fromname;
1414                                 sp++;
1415                                 break;
1416                         case 'N':
1417                                 if (!msginfo->fromname) {sp++; break;}
1418                                 Xstrdup_a(str, msginfo->fromname,
1419                                           {sp++; break;});
1420                                 p = str;
1421                                 while (*p && !isspace(*p)) p++;
1422                                 *p = '\0';
1423                                 sp++;
1424                                 break;
1425                         case 's':
1426                                 str = msginfo->subject;
1427                                 sp++;
1428                                 break;
1429                         case 't':
1430                                 str = msginfo->to;
1431                                 sp++;
1432                                 break;
1433                         case 'c':
1434                                 str = msginfo->cc;
1435                                 sp++;
1436                                 break;
1437                         case 'i':
1438                                 if (!msginfo->msgid) {sp++; break;}
1439                                 Xalloca(str, strlen(msginfo->msgid) + 3,
1440                                         {sp++; break;});
1441                                 g_snprintf(str, strlen(msginfo->msgid) + 3,
1442                                            "<%s>", msginfo->msgid);
1443                                 sp++;
1444                                 break;
1445                         case '%':
1446                                 str = "%";
1447                                 sp++;
1448                                 break;
1449                         default:
1450                                 break;
1451                         }
1452
1453                         if (str) {
1454                                 while (ext_len + strlen(str) + 1 > buf_len)
1455                                         buf_len += 1024;
1456                                 ext_str = g_realloc(ext_str,
1457                                                     sizeof(gchar) * buf_len);
1458                                 g_return_val_if_fail(ext_str != NULL, NULL);
1459                                 strcpy(ext_str + ext_len, str);
1460                                 ext_len += strlen(str);
1461                         }
1462                 } else if (*mbs == '\\') {
1463                         wctomb(mbs, *(++sp));
1464                         str = NULL;
1465
1466                         switch (*mbs) {
1467                         case 'n':
1468                                 str = "\n";
1469                                 break;
1470                         case 't':
1471                                 str = "\t";
1472                                 break;
1473                         case '\\':
1474                                 str = "\\";
1475                                 break;
1476                         default:
1477                                 break;
1478                         }
1479
1480                         if (str) {
1481                                 while (ext_len + strlen(str) + 1 > buf_len)
1482                                         buf_len += 1024;
1483                                 ext_str = g_realloc(ext_str,
1484                                                     sizeof(gchar) * buf_len);
1485                                 g_return_val_if_fail(ext_str != NULL, NULL);
1486                                 strcpy(ext_str + ext_len, str);
1487                                 ext_len += strlen(str);
1488                                 sp++;
1489                         }
1490                 } else {
1491                         while (ext_len + len + 1 > buf_len) buf_len += 1024;
1492                         ext_str = g_realloc(ext_str, sizeof(gchar) * buf_len);
1493                         g_return_val_if_fail(ext_str != NULL, NULL);
1494                         strcpy(ext_str + ext_len, mbs);
1495                         ext_len += len;
1496                         sp++;
1497                 }
1498         }
1499
1500         if (ext_str)
1501                 ext_str = g_realloc(ext_str, strlen(ext_str) + 1);
1502
1503         return ext_str;
1504 }
1505 */
1506
1507 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
1508                                     gboolean to_all, gboolean ignore_replyto,
1509                                     gboolean followup_and_reply_to)
1510 {
1511         GSList *cc_list;
1512         GSList *cur;
1513         gchar *from;
1514         GHashTable *to_table;
1515
1516         g_return_if_fail(compose->account != NULL);
1517         g_return_if_fail(msginfo != NULL);
1518
1519         if ((compose->account->protocol != A_NNTP) || followup_and_reply_to)
1520                 gtk_entry_set_text(GTK_ENTRY(compose->to_entry),
1521                                    ( (compose->replyto && !ignore_replyto) 
1522                                      ? compose->replyto
1523                                      : msginfo->from ? msginfo->from : ""));
1524         if (compose->account->protocol == A_NNTP)
1525                 gtk_entry_set_text(GTK_ENTRY(compose->newsgroups_entry),
1526                                    compose->followup_to ? compose->followup_to
1527                                    : compose->newsgroups ? compose->newsgroups
1528                                    : "");
1529
1530         if (msginfo->subject && *msginfo->subject) {
1531                 gchar *buf, *buf2, *p;
1532
1533                 buf = g_strdup(msginfo->subject);
1534                 while (!strncasecmp(buf, "Re:", 3)) {
1535                         p = buf + 3;
1536                         while (isspace(*p)) p++;
1537                         memmove(buf, p, strlen(p) + 1);
1538                 }
1539
1540                 buf2 = g_strdup_printf("Re: %s", buf);
1541                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1542                 g_free(buf2);
1543                 g_free(buf);
1544         } else
1545                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
1546
1547         if (!to_all || compose->account->protocol == A_NNTP) return;
1548
1549         from = g_strdup(compose->replyto ? compose->replyto :
1550                         msginfo->from ? msginfo->from : "");
1551         extract_address(from);
1552
1553         cc_list = address_list_append(NULL, msginfo->to);
1554         cc_list = address_list_append(cc_list, compose->cc);
1555
1556         to_table = g_hash_table_new(g_str_hash, g_str_equal);
1557         g_hash_table_insert(to_table, from, GINT_TO_POINTER(1));
1558         if (compose->account)
1559                 g_hash_table_insert(to_table, compose->account->address,
1560                                     GINT_TO_POINTER(1));
1561
1562         /* remove address on To: and that of current account */
1563         for (cur = cc_list; cur != NULL; ) {
1564                 GSList *next = cur->next;
1565
1566                 if (g_hash_table_lookup(to_table, cur->data) != NULL)
1567                         cc_list = g_slist_remove(cc_list, cur->data);
1568                 else
1569                         g_hash_table_insert(to_table, cur->data, cur);
1570
1571                 cur = next;
1572         }
1573         g_hash_table_destroy(to_table);
1574         g_free(from);
1575
1576         if (cc_list) {
1577                 for (cur = cc_list; cur != NULL; cur = cur->next)
1578                         compose_entry_append(compose, (gchar *)cur->data,
1579                                              COMPOSE_CC);
1580                 slist_free_strings(cc_list);
1581                 g_slist_free(cc_list);
1582         }
1583 }
1584
1585 #define SET_ENTRY(entry, str) \
1586 { \
1587         if (str && *str) \
1588                 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
1589 }
1590
1591 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
1592 {
1593         g_return_if_fail(msginfo != NULL);
1594
1595         SET_ENTRY(to_entry, msginfo->to);
1596         SET_ENTRY(subject_entry, msginfo->subject);
1597         SET_ENTRY(cc_entry, compose->cc);
1598         SET_ENTRY(bcc_entry, compose->bcc);
1599         SET_ENTRY(reply_entry, compose->replyto);
1600
1601         if (compose->bcc) {
1602                 GtkItemFactory *ifactory;
1603                 GtkWidget *menuitem;
1604
1605                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1606                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Bcc");
1607                 gtk_check_menu_item_set_active
1608                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1609         }
1610         if (compose->replyto) {
1611                 GtkItemFactory *ifactory;
1612                 GtkWidget *menuitem;
1613
1614                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1615                 menuitem = gtk_item_factory_get_item
1616                         (ifactory, "/Message/Reply to");
1617                 gtk_check_menu_item_set_active
1618                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1619         }
1620 }
1621
1622 #undef SET_ENTRY
1623
1624 static void compose_insert_sig(Compose *compose)
1625 {
1626         gchar *sigfile;
1627
1628         if (compose->account && compose->account->sig_path)
1629                 sigfile = g_strdup(compose->account->sig_path);
1630         else {
1631                 sigfile = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1632                                       DEFAULT_SIGNATURE, NULL);
1633         }
1634
1635         if (!is_file_or_fifo_exist(sigfile)) {
1636                 g_free(sigfile);
1637                 return;
1638         }
1639
1640         gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, "\n\n", 2);
1641         if (prefs_common.sig_sep) {
1642                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1643                                 prefs_common.sig_sep, -1);
1644                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1645                                 "\n", 1);
1646         }
1647
1648         compose_insert_file(compose, sigfile);
1649         g_free(sigfile);
1650 }
1651
1652 static void compose_insert_file(Compose *compose, const gchar *file)
1653 {
1654         GtkSText *text = GTK_STEXT(compose->text);
1655         gchar buf[BUFFSIZE];
1656         FILE *fp;
1657
1658         g_return_if_fail(file != NULL);
1659
1660         if ((fp = fopen(file, "r")) == NULL) {
1661                 FILE_OP_ERROR(file, "fopen");
1662                 return;
1663         }
1664
1665         gtk_stext_freeze(text);
1666
1667         while (fgets(buf, sizeof(buf), fp) != NULL)
1668                 gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1669
1670         gtk_stext_thaw(text);
1671
1672         fclose(fp);
1673 }
1674
1675 static void compose_attach_info(Compose * compose, AttachInfo * ainfo,
1676                                 ContentType cnttype)
1677 {
1678         gchar *text[N_ATTACH_COLS];
1679         gint row;
1680
1681         text[COL_MIMETYPE] = ainfo->content_type;
1682         text[COL_SIZE] = to_human_readable(ainfo->size);
1683         text[COL_NAME] = ainfo->name;
1684
1685         row = gtk_clist_append(GTK_CLIST(compose->attach_clist), text);
1686         gtk_clist_set_row_data(GTK_CLIST(compose->attach_clist), row, ainfo);
1687
1688         if (cnttype != MIME_MESSAGE_RFC822)
1689                 compose_changed_cb(NULL, compose);
1690 }
1691
1692 static void compose_attach_append_with_type(Compose *compose,
1693                                             const gchar *file,
1694                                             const gchar *type,
1695                                             ContentType cnttype)
1696 {
1697         AttachInfo *ainfo;
1698         off_t size;
1699
1700         if (!is_file_exist(file)) {
1701                 g_warning(_("File %s doesn't exist\n"), file);
1702                 return;
1703         }
1704         if ((size = get_file_size(file)) < 0) {
1705                 g_warning(_("Can't get file size of %s\n"), file);
1706                 return;
1707         }
1708         if (size == 0) {
1709                 alertpanel_notice(_("File %s is empty\n"), file);
1710                 return;
1711         }
1712
1713         if (!compose->use_attach) {
1714                 GtkItemFactory *ifactory;
1715                 GtkWidget *menuitem;
1716
1717                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1718                 menuitem = gtk_item_factory_get_item(ifactory,
1719                                                      "/Message/Attach");
1720                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1721                                                TRUE);
1722         }
1723
1724         ainfo = g_new0(AttachInfo, 1);
1725         ainfo->file = g_strdup(file);
1726
1727         if (cnttype == MIME_MESSAGE_RFC822) {
1728                 ainfo->encoding = ENC_7BIT;
1729                 ainfo->name = g_strdup_printf(_("Message: %s"),
1730                                               g_basename(file));
1731         } else {
1732                 ainfo->encoding = ENC_BASE64;
1733                 ainfo->name = g_strdup(g_basename(file));
1734         }
1735
1736         ainfo->content_type = g_strdup(type);
1737         ainfo->size = size;
1738
1739         compose_attach_info(compose, ainfo, cnttype);
1740 }
1741
1742 static void compose_attach_append(Compose *compose, const gchar *file,
1743                                   ContentType cnttype)
1744 {
1745         AttachInfo *ainfo;
1746         gchar *text[N_ATTACH_COLS];
1747         off_t size;
1748         gint row;
1749
1750         if (!is_file_exist(file)) {
1751                 g_warning(_("File %s doesn't exist\n"), file);
1752                 return;
1753         }
1754         if ((size = get_file_size(file)) < 0) {
1755                 g_warning(_("Can't get file size of %s\n"), file);
1756                 return;
1757         }
1758         if (size == 0) {
1759                 alertpanel_notice(_("File %s is empty\n"), file);
1760                 return;
1761         }
1762
1763         if (!compose->use_attach) {
1764                 GtkItemFactory *ifactory;
1765                 GtkWidget *menuitem;
1766
1767                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1768                 menuitem = gtk_item_factory_get_item(ifactory,
1769                                                      "/Message/Attach");
1770                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1771                                                TRUE);
1772         }
1773
1774         ainfo = g_new0(AttachInfo, 1);
1775         ainfo->file = g_strdup(file);
1776
1777         if (cnttype == MIME_MESSAGE_RFC822) {
1778                 ainfo->content_type = g_strdup("message/rfc822");
1779                 ainfo->encoding = ENC_7BIT;
1780                 ainfo->name = g_strdup_printf(_("Message: %s"),
1781                                               g_basename(file));
1782         } else {
1783                 ainfo->content_type = procmime_get_mime_type(file);
1784                 if (!ainfo->content_type)
1785                         ainfo->content_type =
1786                                 g_strdup("application/octet-stream");
1787                 ainfo->encoding = ENC_BASE64;
1788                 ainfo->name = g_strdup(g_basename(file));
1789         }
1790         ainfo->size = size;
1791
1792         compose_attach_info(compose, ainfo, cnttype);
1793 }
1794
1795 static void compose_wrap_line(Compose *compose)
1796 {
1797         GtkSText *text = GTK_STEXT(compose->text);
1798         guint text_len;
1799         guint line_pos = 0, cur_pos = 0;
1800         gint line_len = 0, cur_len = 0;
1801         gint ch_len;
1802         gchar cbuf[MB_CUR_MAX];
1803
1804         gtk_stext_freeze(text);
1805
1806         text_len = gtk_stext_get_length(text);
1807
1808         for (; cur_pos < text_len; cur_pos++) {
1809                 if (text->use_wchar)
1810                         ch_len = wctomb
1811                                 (cbuf, (wchar_t)GTK_STEXT_INDEX(text, cur_pos));
1812                 else {
1813                         cbuf[0] = GTK_STEXT_INDEX(text, cur_pos);
1814                         ch_len = 1;
1815                 }
1816
1817                 if (ch_len == 1 && *cbuf == '\n') {
1818                         line_pos = cur_pos + 1;
1819                         line_len = cur_len = 0;
1820                         continue;
1821                 }
1822
1823                 if (ch_len < 0) {
1824                         cbuf[0] = '\0';
1825                         ch_len = 1;
1826                 }
1827
1828                 if (ch_len == 1 && isspace(*cbuf)) {
1829                         line_pos = cur_pos + 1;
1830                         line_len = cur_len + ch_len;
1831                 }
1832
1833                 if (cur_len + ch_len > prefs_common.linewrap_len &&
1834                     line_len > 0) {
1835                         gint tlen;
1836
1837                         if (text->use_wchar)
1838                                 tlen = wctomb(cbuf, (wchar_t)GTK_STEXT_INDEX(text, line_pos - 1));
1839                         else {
1840                                 cbuf[0] = GTK_STEXT_INDEX(text, line_pos - 1);
1841                                 tlen = 1;
1842                         }
1843                         if (tlen == 1 && isspace(*cbuf)) {
1844                                 gtk_stext_set_point(text, line_pos);
1845                                 gtk_stext_backward_delete(text, 1);
1846                                 text_len--;
1847                                 cur_pos--;
1848                                 line_pos--;
1849                                 cur_len--;
1850                                 line_len--;
1851                         }
1852
1853                         gtk_stext_set_point(text, line_pos);
1854                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
1855                         text_len++;
1856                         cur_pos++;
1857                         line_pos++;
1858                         cur_len = cur_len - line_len + ch_len;
1859                         line_len = 0;
1860                         continue;
1861                 }
1862
1863                 if (ch_len > 1) {
1864                         line_pos = cur_pos + 1;
1865                         line_len = cur_len + ch_len;
1866                 }
1867                 cur_len += ch_len;
1868         }
1869
1870         gtk_stext_thaw(text);
1871 }
1872
1873 static void compose_set_title(Compose *compose)
1874 {
1875         gchar *str;
1876         gchar *edited;
1877
1878         edited = compose->modified ? _(" [Edited]") : "";
1879         if (compose->account && compose->account->address)
1880                 str = g_strdup_printf(_("%s - Compose message%s"),
1881                                       compose->account->address, edited);
1882         else
1883                 str = g_strdup_printf(_("Compose message%s"), edited);
1884         gtk_window_set_title(GTK_WINDOW(compose->window), str);
1885         g_free(str);
1886 }
1887
1888 /**
1889  * compose_current_mail_account:
1890  * 
1891  * Find a current mail account (the currently selected account, or the
1892  * default account, if a news account is currently selected).  If a
1893  * mail account cannot be found, display an error message.
1894  * 
1895  * Return value: Mail account, or NULL if not found.
1896  **/
1897 static PrefsAccount *
1898 compose_current_mail_account(void)
1899 {
1900         PrefsAccount *ac;
1901
1902         if (cur_account && cur_account->protocol != A_NNTP)
1903                 ac = cur_account;
1904         else {
1905                 ac = account_get_default();
1906                 if (!ac || ac->protocol == A_NNTP) {
1907                         alertpanel_error(_("Account for sending mail is not specified.\n"
1908                                            "Please select a mail account before sending."));
1909                         return NULL;
1910                 }
1911         }
1912         return ac;
1913 }
1914
1915 gint compose_send(Compose *compose)
1916 {
1917         gchar tmp[MAXPATHLEN + 1];
1918         gchar *to, *newsgroups;
1919         gint ok = 0;
1920         static gboolean lock = FALSE;
1921
1922         if (lock) return 1;
1923
1924         g_return_val_if_fail(compose->account != NULL, -1);
1925         g_return_val_if_fail(compose->orig_account != NULL, -1);
1926
1927         lock = TRUE;
1928
1929         to = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
1930         newsgroups = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
1931         if (*to == '\0' && *newsgroups == '\0') {
1932                 alertpanel_error(_("Recipient is not specified."));
1933                 lock = FALSE;
1934                 return 1;
1935         }
1936
1937         /* write to temporary file */
1938         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
1939                    get_rc_dir(), G_DIR_SEPARATOR, (gint)compose);
1940
1941         if (prefs_common.linewrap_at_send)
1942                 compose_wrap_line(compose);
1943
1944         if (compose_write_to_file(compose, tmp, FALSE) < 0) {
1945                 lock = FALSE;
1946                 return -1;
1947         }
1948
1949         if (!compose->to_list && !compose->newsgroup_list) {
1950                 g_warning(_("can't get recipient list."));
1951                 unlink(tmp);
1952                 lock = FALSE;
1953                 return -1;
1954         }
1955
1956         if (compose->to_list) {
1957                 PrefsAccount *ac;
1958
1959                 /*
1960                 if (compose->account->protocol != A_NNTP)
1961                         ac = compose->account;
1962                 else if (compose->orig_account->protocol != A_NNTP)
1963                         ac = compose->orig_account;
1964                 else if (cur_account && cur_account->protocol != A_NNTP)
1965                         ac = cur_account;
1966                 else {
1967                         ac = compose_current_mail_account();
1968                         if (!ac) {
1969                                 unlink(tmp);
1970                                 lock = FALSE;
1971                                 return -1;
1972                         }
1973                 }
1974                 */
1975                 ac = compose->account;
1976
1977                 ok = send_message(tmp, ac, compose->to_list);
1978                 statusbar_pop_all();
1979         }
1980
1981         if (ok == 0 && compose->newsgroup_list) {
1982                 Folder *folder;
1983
1984                 if (compose->account->protocol == A_NNTP)
1985                         folder = FOLDER(compose->account->folder);
1986                 else
1987                         folder = FOLDER(compose->orig_account->folder);
1988
1989                 ok = news_post(folder, tmp);
1990                 if (ok < 0) {
1991                         alertpanel_error(_("Error occurred while posting the message to %s ."),
1992                                          compose->account->nntp_server);
1993                         unlink(tmp);
1994                         lock = FALSE;
1995                         return -1;
1996                 }
1997         }
1998
1999         /* queue message if failed to send */
2000         if (ok < 0) {
2001                 if (prefs_common.queue_msg) {
2002                         AlertValue val;
2003
2004                         val = alertpanel
2005                                 (_("Queueing"),
2006                                  _("Error occurred while sending the message.\n"
2007                                    "Put this message into queue folder?"),
2008                                  _("OK"), _("Cancel"), NULL);
2009                         if (G_ALERTDEFAULT == val) {
2010                                 ok = compose_queue(compose, tmp);
2011                                 if (ok < 0)
2012                                         alertpanel_error(_("Can't queue the message."));
2013                         }
2014                 } else
2015                         alertpanel_error(_("Error occurred while sending the message."));
2016         }
2017
2018         /* save message to outbox */
2019         if (ok == 0 && prefs_common.savemsg) {
2020                 if (compose_save_to_outbox(compose, tmp) < 0)
2021                         alertpanel_error
2022                                 (_("Can't save the message to outbox."));
2023         }
2024
2025         unlink(tmp);
2026         lock = FALSE;
2027         return ok;
2028 }
2029
2030 static gint compose_write_to_file(Compose *compose, const gchar *file,
2031                                   gboolean is_draft)
2032 {
2033         FILE *fp;
2034         size_t len;
2035         gchar *chars;
2036         gchar *buf;
2037         const gchar *out_codeset;
2038         EncodingType encoding;
2039
2040         if ((fp = fopen(file, "w")) == NULL) {
2041                 FILE_OP_ERROR(file, "fopen");
2042                 return -1;
2043         }
2044
2045         /* chmod for security */
2046         if (change_file_mode_rw(fp, file) < 0) {
2047                 FILE_OP_ERROR(file, "chmod");
2048                 g_warning(_("can't change file mode\n"));
2049         }
2050
2051         /* get all composed text */
2052         chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
2053         len = strlen(chars);
2054         if (is_ascii_str(chars)) {
2055                 buf = g_strdup(chars);
2056                 out_codeset = "US-ASCII";
2057                 encoding = ENC_7BIT;
2058         } else {
2059                 const gchar *src_codeset;
2060
2061                 out_codeset = conv_get_outgoing_charset_str();
2062                 if (!strcasecmp(out_codeset, "US-ASCII"))
2063                         out_codeset = "ISO-8859-1";
2064                 encoding = procmime_get_encoding_for_charset(out_codeset);
2065                 debug_print("charset = %s, encoding = %s\n",
2066                             out_codeset, procmime_get_encoding_str(encoding));
2067
2068                 src_codeset = conv_get_current_charset_str();
2069                 buf = conv_codeset_strdup(chars, src_codeset, out_codeset);
2070                 if (!buf) {
2071                         g_free(chars);
2072                         fclose(fp);
2073                         unlink(file);
2074                         alertpanel_error(_("Can't convert the codeset of the message."));
2075                         return -1;
2076                 }
2077         }
2078         g_free(chars);
2079
2080         /* write headers */
2081         if (compose_write_headers
2082                 (compose, fp, out_codeset, encoding, is_draft) < 0) {
2083                 g_warning(_("can't write headers\n"));
2084                 fclose(fp);
2085                 /* unlink(file); */
2086                 g_free(buf);
2087                 return -1;
2088         }
2089
2090         if (compose->use_attach) {
2091 #if USE_GPGME
2092             /* This prolog message is ignored by mime software and
2093              * because it would make our signing/encryption task
2094              * tougher, we don't emit it in that case */
2095             if (!compose->use_signing && !compose->use_encryption)
2096 #endif
2097                 fputs("This is a multi-part message in MIME format.\n", fp);
2098
2099                 fprintf(fp, "\n--%s\n", compose->boundary);
2100                 fprintf(fp, "Content-Type: text/plain; charset=%s\n",
2101                         out_codeset);
2102                 fprintf(fp, "Content-Transfer-Encoding: %s\n",
2103                         procmime_get_encoding_str(encoding));
2104                 fputc('\n', fp);
2105         }
2106
2107         /* write body */
2108         len = strlen(buf);
2109         if (encoding == ENC_BASE64) {
2110                 gchar outbuf[B64_BUFFSIZE];
2111                 gint i, l;
2112
2113                 for (i = 0; i < len; i += B64_LINE_SIZE) {
2114                         l = MIN(B64_LINE_SIZE, len - i);
2115                         to64frombits(outbuf, buf + i, l);
2116                         fputs(outbuf, fp);
2117                         fputc('\n', fp);
2118                 }
2119         } else if (fwrite(buf, sizeof(gchar), len, fp) != len) {
2120                 FILE_OP_ERROR(file, "fwrite");
2121                 fclose(fp);
2122                 unlink(file);
2123                 g_free(buf);
2124                 return -1;
2125         }
2126         g_free(buf);
2127
2128         if (compose->use_attach)
2129                 compose_write_attach(compose, fp);
2130
2131         if (fclose(fp) == EOF) {
2132                 FILE_OP_ERROR(file, "fclose");
2133                 unlink(file);
2134                 return -1;
2135         }
2136
2137 #if USE_GPGME
2138         if (compose->use_signing) {
2139                 if (rfc2015_sign(file, compose->account) < 0) {
2140                         unlink(file);
2141                         return -1;
2142                 }
2143         }
2144         if (compose->use_encryption) {
2145                 if (rfc2015_encrypt(file, compose->to_list) < 0) {
2146                         unlink(file);
2147                         return -1;
2148                 }
2149         }
2150 #endif /* USE_GPGME */
2151
2152         return 0;
2153 }
2154
2155 static gint compose_write_body_to_file(Compose *compose, const gchar *file)
2156 {
2157         FILE *fp;
2158         size_t len;
2159         gchar *chars;
2160
2161         if ((fp = fopen(file, "w")) == NULL) {
2162                 FILE_OP_ERROR(file, "fopen");
2163                 return -1;
2164         }
2165
2166         /* chmod for security */
2167         if (change_file_mode_rw(fp, file) < 0) {
2168                 FILE_OP_ERROR(file, "chmod");
2169                 g_warning(_("can't change file mode\n"));
2170         }
2171
2172         chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
2173
2174         /* write body */
2175         len = strlen(chars);
2176         if (fwrite(chars, sizeof(gchar), len, fp) != len) {
2177                 FILE_OP_ERROR(file, "fwrite");
2178                 g_free(chars);
2179                 fclose(fp);
2180                 unlink(file);
2181                 return -1;
2182         }
2183
2184         g_free(chars);
2185
2186         if (fclose(fp) == EOF) {
2187                 FILE_OP_ERROR(file, "fclose");
2188                 unlink(file);
2189                 return -1;
2190         }
2191         return 0;
2192 }
2193
2194 static gint compose_save_to_outbox(Compose *compose, const gchar *file)
2195 {
2196         FolderItem *outbox;
2197         gchar *path;
2198         gint num;
2199         FILE *fp;
2200
2201         debug_print(_("saving sent message...\n"));
2202
2203         outbox = folder_get_default_outbox();
2204         path = folder_item_get_path(outbox);
2205         if (!is_dir_exist(path))
2206                 make_dir_hier(path);
2207
2208         folder_item_scan(outbox);
2209         if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
2210                 g_free(path);
2211                 g_warning(_("can't save message\n"));
2212                 return -1;
2213         }
2214
2215         if ((fp = procmsg_open_mark_file(path, TRUE)) == NULL)
2216                 g_warning(_("can't open mark file\n"));
2217         else {
2218                 MsgInfo newmsginfo;
2219
2220                 newmsginfo.msgnum = num;
2221                 newmsginfo.flags.perm_flags = 0;
2222                 newmsginfo.flags.tmp_flags = 0;
2223                 procmsg_write_flags(&newmsginfo, fp);
2224                 fclose(fp);
2225         }
2226         g_free(path);
2227
2228         return 0;
2229 }
2230
2231 static gint compose_queue(Compose *compose, const gchar *file)
2232 {
2233         FolderItem *queue;
2234         gchar *tmp, *queue_path;
2235         FILE *fp, *src_fp;
2236         GSList *cur;
2237         gchar buf[BUFFSIZE];
2238         gint num;
2239
2240         debug_print(_("queueing message...\n"));
2241         g_return_val_if_fail(compose->to_list != NULL, -1);
2242         g_return_val_if_fail(compose->account != NULL, -1);
2243
2244         tmp = g_strdup_printf("%s%cqueue.%d", g_get_tmp_dir(),
2245                               G_DIR_SEPARATOR, (gint)compose);
2246         if ((fp = fopen(tmp, "w")) == NULL) {
2247                 FILE_OP_ERROR(tmp, "fopen");
2248                 g_free(tmp);
2249                 return -1;
2250         }
2251         if ((src_fp = fopen(file, "r")) == NULL) {
2252                 FILE_OP_ERROR(file, "fopen");
2253                 fclose(fp);
2254                 unlink(tmp);
2255                 g_free(tmp);
2256                 return -1;
2257         }
2258         if (change_file_mode_rw(fp, tmp) < 0) {
2259                 FILE_OP_ERROR(tmp, "chmod");
2260                 g_warning(_("can't change file mode\n"));
2261         }
2262
2263         /* queueing variables */
2264         fprintf(fp, "AF:\n");
2265         fprintf(fp, "NF:0\n");
2266         fprintf(fp, "PS:10\n");
2267         fprintf(fp, "SRH:1\n");
2268         fprintf(fp, "SFN:\n");
2269         fprintf(fp, "DSR:\n");
2270         if (compose->msgid)
2271                 fprintf(fp, "MID:<%s>\n", compose->msgid);
2272         else
2273                 fprintf(fp, "MID:\n");
2274         fprintf(fp, "CFG:\n");
2275         fprintf(fp, "PT:0\n");
2276         fprintf(fp, "S:%s\n", compose->account->address);
2277         fprintf(fp, "RQ:\n");
2278         if (compose->account->smtp_server)
2279                 fprintf(fp, "SSV:%s\n", compose->account->smtp_server);
2280         else
2281                 fprintf(fp, "SSV:\n");
2282         if (compose->account->nntp_server)
2283                 fprintf(fp, "NSV:%s\n", compose->account->nntp_server);
2284         else
2285                 fprintf(fp, "NSV:\n");
2286         fprintf(fp, "SSH:\n");
2287         fprintf(fp, "R:<%s>", (gchar *)compose->to_list->data);
2288         for (cur = compose->to_list->next; cur != NULL; cur = cur->next)
2289                 fprintf(fp, ",<%s>", (gchar *)cur->data);
2290         fprintf(fp, "\n");
2291         fprintf(fp, "\n");
2292
2293         while (fgets(buf, sizeof(buf), src_fp) != NULL) {
2294                 if (fputs(buf, fp) == EOF) {
2295                         FILE_OP_ERROR(tmp, "fputs");
2296                         fclose(fp);
2297                         fclose(src_fp);
2298                         unlink(tmp);
2299                         g_free(tmp);
2300                         return -1;
2301                 }
2302         }
2303
2304         fclose(src_fp);
2305         if (fclose(fp) == EOF) {
2306                 FILE_OP_ERROR(tmp, "fclose");
2307                 unlink(tmp);
2308                 g_free(tmp);
2309                 return -1;
2310         }
2311
2312         queue = folder_get_default_queue();
2313         folder_item_scan(queue);
2314         queue_path = folder_item_get_path(queue);
2315         if (!is_dir_exist(queue_path))
2316                 make_dir_hier(queue_path);
2317         if ((num = folder_item_add_msg(queue, tmp, TRUE)) < 0) {
2318                 g_warning(_("can't queue the message\n"));
2319                 unlink(tmp);
2320                 g_free(tmp);
2321                 g_free(queue_path);
2322                 return -1;
2323         }
2324         g_free(tmp);
2325
2326         if ((fp = procmsg_open_mark_file(queue_path, TRUE)) == NULL)
2327                 g_warning(_("can't open mark file\n"));
2328         else {
2329                 MsgInfo newmsginfo;
2330
2331                 newmsginfo.msgnum = num;
2332                 newmsginfo.flags.perm_flags = 0;
2333                 newmsginfo.flags.tmp_flags = 0;
2334                 procmsg_write_flags(&newmsginfo, fp);
2335                 fclose(fp);
2336         }
2337         g_free(queue_path);
2338
2339         folder_item_scan(queue);
2340         folderview_update_item(queue, TRUE);
2341
2342         return 0;
2343 }
2344
2345 static void compose_write_attach(Compose *compose, FILE *fp)
2346 {
2347         AttachInfo *ainfo;
2348         GtkCList *clist = GTK_CLIST(compose->attach_clist);
2349         gint row;
2350         FILE *attach_fp;
2351         gchar filename[BUFFSIZE];
2352         gint len;
2353
2354         for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
2355              row++) {
2356                 if ((attach_fp = fopen(ainfo->file, "r")) == NULL) {
2357                         g_warning(_("Can't open file %s\n"), ainfo->file);
2358                         continue;
2359                 }
2360
2361                 fprintf(fp, "\n--%s\n", compose->boundary);
2362
2363                 if (!strcmp2(ainfo->content_type, "message/rfc822")) {
2364                         fprintf(fp, "Content-Type: %s\n", ainfo->content_type);
2365                         fprintf(fp, "Content-Disposition: inline\n");
2366                 } else {
2367                         conv_encode_header(filename, sizeof(filename),
2368                                            ainfo->name, 12);
2369                         fprintf(fp, "Content-Type: %s;\n"
2370                                     " name=\"%s\"\n",
2371                                 ainfo->content_type, filename);
2372                         fprintf(fp, "Content-Disposition: attachment;\n"
2373                                     " filename=\"%s\"\n", filename);
2374                 }
2375
2376                 fprintf(fp, "Content-Transfer-Encoding: %s\n\n",
2377                         procmime_get_encoding_str(ainfo->encoding));
2378
2379                 if (ainfo->encoding == ENC_7BIT) {
2380                         gchar buf[BUFFSIZE];
2381
2382                         while (fgets(buf, sizeof(buf), attach_fp) != NULL) {
2383                                 len = strlen(buf);
2384                                 if (len > 1 && buf[len - 1] == '\n' &&
2385                                     buf[len - 2] == '\r') {
2386                                         buf[len - 2] = '\n';
2387                                         buf[len - 1] = '\0';
2388                                 }
2389                                 fputs(buf, fp);
2390                         }
2391                 } else {
2392                         gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
2393
2394                         while ((len = fread(inbuf, sizeof(gchar),
2395                                             B64_LINE_SIZE, attach_fp))
2396                                == B64_LINE_SIZE) {
2397                                 to64frombits(outbuf, inbuf, B64_LINE_SIZE);
2398                                 fputs(outbuf, fp);
2399                                 fputc('\n', fp);
2400                         }
2401                         if (len > 0 && feof(attach_fp)) {
2402                                 to64frombits(outbuf, inbuf, len);
2403                                 fputs(outbuf, fp);
2404                                 fputc('\n', fp);
2405                         }
2406                 }
2407
2408                 fclose(attach_fp);
2409         }
2410
2411         fprintf(fp, "\n--%s--\n", compose->boundary);
2412 }
2413
2414 #define IS_IN_CUSTOM_HEADER(header) \
2415         (compose->account->add_customhdr && \
2416          custom_header_find(compose->account->customhdr_list, header) != NULL)
2417
2418 static gint compose_write_headers(Compose *compose, FILE *fp,
2419                                   const gchar *charset, EncodingType encoding,
2420                                   gboolean is_draft)
2421 {
2422         gchar buf[BUFFSIZE];
2423         gchar *str;
2424         /* struct utsname utsbuf; */
2425
2426         g_return_val_if_fail(fp != NULL, -1);
2427         g_return_val_if_fail(charset != NULL, -1);
2428         g_return_val_if_fail(compose->account != NULL, -1);
2429         g_return_val_if_fail(compose->account->address != NULL, -1);
2430
2431         /* Date */
2432         if (compose->account->add_date) {
2433                 get_rfc822_date(buf, sizeof(buf));
2434                 fprintf(fp, "Date: %s\n", buf);
2435         }
2436
2437         /* From */
2438         if (!IS_IN_CUSTOM_HEADER("From")) {
2439                 if (compose->account->name && *compose->account->name) {
2440                         compose_convert_header
2441                                 (buf, sizeof(buf), compose->account->name,
2442                                  strlen("From: "));
2443                         fprintf(fp, "From: %s <%s>\n",
2444                                 buf, compose->account->address);
2445                 } else
2446                         fprintf(fp, "From: %s\n", compose->account->address);
2447         }
2448         
2449         slist_free_strings(compose->to_list);
2450         g_slist_free(compose->to_list);
2451         compose->to_list = NULL;
2452
2453         /* To */
2454         if (compose->use_to) {
2455                 str = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
2456                 if (*str != '\0') {
2457                         Xstrdup_a(str, str, return -1);
2458                         g_strstrip(str);
2459                         if (*str != '\0') {
2460                                 compose->to_list = address_list_append
2461                                         (compose->to_list, str);
2462                                 if (!IS_IN_CUSTOM_HEADER("To")) {
2463                                         compose_convert_header
2464                                                 (buf, sizeof(buf), str,
2465                                                  strlen("To: "));
2466                                         fprintf(fp, "To: %s\n", buf);
2467                                 }
2468                         }
2469                 }
2470         }
2471
2472         slist_free_strings(compose->newsgroup_list);
2473         g_slist_free(compose->newsgroup_list);
2474         compose->newsgroup_list = NULL;
2475
2476         /* Newsgroups */
2477         str = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
2478         if (*str != '\0') {
2479                 Xstrdup_a(str, str, return -1);
2480                 g_strstrip(str);
2481                 remove_space(str);
2482                 if (*str != '\0') {
2483                         compose->newsgroup_list =
2484                                 newsgroup_list_append(compose->newsgroup_list,
2485                                                       str);
2486                         if (!IS_IN_CUSTOM_HEADER("Newsgroups")) {
2487                                 compose_convert_header(buf, sizeof(buf), str,
2488                                                        strlen("Newsgroups: "));
2489                                 fprintf(fp, "Newsgroups: %s\n", buf);
2490                         }
2491                 }
2492         }
2493
2494         if (!is_draft && !compose->to_list && !compose->newsgroup_list)
2495                 return -1;
2496
2497         /* Cc */
2498         if (compose->use_cc) {
2499                 str = gtk_entry_get_text(GTK_ENTRY(compose->cc_entry));
2500                 if (*str != '\0') {
2501                         Xstrdup_a(str, str, return -1);
2502                         g_strstrip(str);
2503                         if (*str != '\0') {
2504                                 compose->to_list = address_list_append
2505                                         (compose->to_list, str);
2506                                 if (!IS_IN_CUSTOM_HEADER("Cc")) {
2507                                         compose_convert_header
2508                                                 (buf, sizeof(buf), str,
2509                                                  strlen("Cc: "));
2510                                         fprintf(fp, "Cc: %s\n", buf);
2511                                 }
2512                         }
2513                 }
2514         }
2515
2516         /* Bcc */
2517         if (compose->use_bcc) {
2518                 str = gtk_entry_get_text(GTK_ENTRY(compose->bcc_entry));
2519                 if (*str != '\0') {
2520                         Xstrdup_a(str, str, return -1);
2521                         g_strstrip(str);
2522                         if (*str != '\0') {
2523                                 compose->to_list = address_list_append
2524                                         (compose->to_list, str);
2525                                 if (is_draft) {
2526                                         compose_convert_header
2527                                                 (buf, sizeof(buf), str,
2528                                                  strlen("Bcc: "));
2529                                         fprintf(fp, "Bcc: %s\n", buf);
2530                                 }
2531                         }
2532                 }
2533         }
2534
2535         /* Subject */
2536         str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
2537         if (*str != '\0' && !IS_IN_CUSTOM_HEADER("Subject")) {
2538                 Xstrdup_a(str, str, return -1);
2539                 g_strstrip(str);
2540                 if (*str != '\0') {
2541                         compose_convert_header(buf, sizeof(buf), str,
2542                                                strlen("Subject: "));
2543                         fprintf(fp, "Subject: %s\n", buf);
2544                 }
2545         }
2546
2547         /* Message-ID */
2548         if (compose->account->gen_msgid) {
2549                 compose_generate_msgid(compose, buf, sizeof(buf));
2550                 fprintf(fp, "Message-Id: <%s>\n", buf);
2551                 compose->msgid = g_strdup(buf);
2552         }
2553
2554         /* In-Reply-To */
2555         if (compose->inreplyto && compose->to_list)
2556                 fprintf(fp, "In-Reply-To: <%s>\n", compose->inreplyto);
2557
2558         /* References */
2559         if (compose->references)
2560                 fprintf(fp, "References: %s\n", compose->references);
2561
2562         /* Followup-To */
2563         if (compose->use_followupto && !IS_IN_CUSTOM_HEADER("Followup-To")) {
2564                 str = gtk_entry_get_text(GTK_ENTRY(compose->followup_entry));
2565                 if (*str != '\0') {
2566                         Xstrdup_a(str, str, return -1);
2567                         g_strstrip(str);
2568                         remove_space(str);
2569                         if (*str != '\0') {
2570                                 compose_convert_header(buf, sizeof(buf), str,
2571                                                        strlen("Followup-To: "));
2572                                 fprintf(fp, "Followup-To: %s\n", buf);
2573                         }
2574                 }
2575         }
2576
2577         /* Reply-To */
2578         if (compose->use_replyto && !IS_IN_CUSTOM_HEADER("Reply-To")) {
2579                 str = gtk_entry_get_text(GTK_ENTRY(compose->reply_entry));
2580                 if (*str != '\0') {
2581                         Xstrdup_a(str, str, return -1);
2582                         g_strstrip(str);
2583                         if (*str != '\0') {
2584                                 compose_convert_header(buf, sizeof(buf), str,
2585                                                        strlen("Reply-To: "));
2586                                 fprintf(fp, "Reply-To: %s\n", buf);
2587                         }
2588                 }
2589         }
2590
2591         /* Organization */
2592         if (compose->account->organization &&
2593             !IS_IN_CUSTOM_HEADER("Organization")) {
2594                 compose_convert_header(buf, sizeof(buf),
2595                                        compose->account->organization,
2596                                        strlen("Organization: "));
2597                 fprintf(fp, "Organization: %s\n", buf);
2598         }
2599
2600         /* Program version and system info */
2601         /* uname(&utsbuf); */
2602         str = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
2603         if (*str != '\0' && !IS_IN_CUSTOM_HEADER("X-Mailer")) {
2604                 fprintf(fp, "X-Mailer: %s (GTK+ %d.%d.%d; %s)\n",
2605                         prog_version,
2606                         gtk_major_version, gtk_minor_version, gtk_micro_version,
2607                         HOST_ALIAS);
2608                         /* utsbuf.sysname, utsbuf.release, utsbuf.machine); */
2609         }
2610         str = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
2611         if (*str != '\0' && !IS_IN_CUSTOM_HEADER("X-Newsreader")) {
2612                 fprintf(fp, "X-Newsreader: %s (GTK+ %d.%d.%d; %s)\n",
2613                         prog_version,
2614                         gtk_major_version, gtk_minor_version, gtk_micro_version,
2615                         HOST_ALIAS);
2616                         /* utsbuf.sysname, utsbuf.release, utsbuf.machine); */
2617         }
2618
2619         /* custom headers */
2620         if (compose->account->add_customhdr) {
2621                 GSList *cur;
2622
2623                 for (cur = compose->account->customhdr_list; cur != NULL;
2624                      cur = cur->next) {
2625                         CustomHeader *chdr = (CustomHeader *)cur->data;
2626
2627                         if (strcasecmp(chdr->name, "Date")         != 0 &&
2628                             strcasecmp(chdr->name, "From")         != 0 &&
2629                             strcasecmp(chdr->name, "To")           != 0 &&
2630                             strcasecmp(chdr->name, "Sender")       != 0 &&
2631                             strcasecmp(chdr->name, "Message-Id")   != 0 &&
2632                             strcasecmp(chdr->name, "In-Reply-To")  != 0 &&
2633                             strcasecmp(chdr->name, "References")   != 0 &&
2634                             strcasecmp(chdr->name, "Mime-Version") != 0 &&
2635                             strcasecmp(chdr->name, "Content-Type") != 0 &&
2636                             strcasecmp(chdr->name, "Content-Transfer-Encoding")
2637                             != 0)
2638                                 compose_convert_header
2639                                         (buf, sizeof(buf),
2640                                          chdr->value ? chdr->value : "",
2641                                          strlen(chdr->name) + 2);
2642                                 fprintf(fp, "%s: %s\n", chdr->name, buf);
2643                 }
2644         }
2645
2646         /* MIME */
2647         fprintf(fp, "Mime-Version: 1.0\n");
2648         if (compose->use_attach) {
2649                 get_rfc822_date(buf, sizeof(buf));
2650                 subst_char(buf, ' ', '_');
2651                 subst_char(buf, ',', '_');
2652                 compose->boundary = g_strdup_printf("Multipart_%s_%08x",
2653                                                     buf, (guint)compose);
2654                 fprintf(fp,
2655                         "Content-Type: multipart/mixed;\n"
2656                         " boundary=\"%s\"\n", compose->boundary);
2657         } else {
2658                 fprintf(fp, "Content-Type: text/plain; charset=%s\n", charset);
2659                 fprintf(fp, "Content-Transfer-Encoding: %s\n",
2660                         procmime_get_encoding_str(encoding));
2661         }
2662
2663         /* Request Return Receipt */
2664         if (!IS_IN_CUSTOM_HEADER("Disposition-Notification-To")) {
2665                 if (compose->return_receipt) {
2666                         if (compose->account->name
2667                             && *compose->account->name) {
2668                                 compose_convert_header(buf, sizeof(buf), compose->account->name, strlen("Disposition-Notification-To: "));
2669                                 fprintf(fp, "Disposition-Notification-To: %s <%s>\n", buf, compose->account->address);
2670                         } else
2671                                 fprintf(fp, "Disposition-Notification-To: %s\n", compose->account->address);
2672                 }
2673         }
2674
2675         /* separator between header and body */
2676         fputs("\n", fp);
2677
2678         return 0;
2679 }
2680
2681 #undef IS_IN_CUSTOM_HEADER
2682
2683 static void compose_convert_header(gchar *dest, gint len, gchar *src,
2684                                    gint header_len)
2685 {
2686         g_return_if_fail(src != NULL);
2687         g_return_if_fail(dest != NULL);
2688
2689         if (len < 1) return;
2690
2691         remove_return(src);
2692
2693         if (is_ascii_str(src)) {
2694                 strncpy2(dest, src, len);
2695                 dest[len - 1] = '\0';
2696                 return;
2697         } else
2698                 conv_encode_header(dest, len, src, header_len);
2699 }
2700
2701 static void compose_generate_msgid(Compose *compose, gchar *buf, gint len)
2702 {
2703         struct tm *lt;
2704         time_t t;
2705         gchar *addr;
2706
2707         t = time(NULL);
2708         lt = localtime(&t);
2709
2710         if (compose->account && compose->account->address &&
2711             *compose->account->address) {
2712                 if (strchr(compose->account->address, '@'))
2713                         addr = g_strdup(compose->account->address);
2714                 else
2715                         addr = g_strconcat(compose->account->address, "@",
2716                                            get_domain_name(), NULL);
2717         } else
2718                 addr = g_strconcat(g_get_user_name(), "@", get_domain_name(),
2719                                    NULL);
2720
2721         g_snprintf(buf, len, "%04d%02d%02d%02d%02d%02d.%08x.%s",
2722                    lt->tm_year + 1900, lt->tm_mon + 1,
2723                    lt->tm_mday, lt->tm_hour,
2724                    lt->tm_min, lt->tm_sec,
2725                    (guint)random(), addr);
2726
2727         debug_print(_("generated Message-ID: %s\n"), buf);
2728
2729         g_free(addr);
2730 }
2731
2732 static void compose_add_entry_field(GtkWidget *table, GtkWidget **hbox,
2733                                     GtkWidget **entry, gint *count,
2734                                     const gchar *label_str,
2735                                     gboolean is_addr_entry)
2736 {
2737         GtkWidget *label;
2738
2739         if (GTK_TABLE(table)->nrows < (*count) + 1)
2740                 gtk_table_resize(GTK_TABLE(table), (*count) + 1, 2);
2741
2742         *hbox = gtk_hbox_new(FALSE, 0);
2743         label = gtk_label_new
2744                 (prefs_common.trans_hdr ? gettext(label_str) : label_str);
2745         gtk_box_pack_end(GTK_BOX(*hbox), label, FALSE, FALSE, 0);
2746         gtk_table_attach(GTK_TABLE(table), *hbox, 0, 1, *count, (*count) + 1,
2747                          GTK_FILL, 0, 2, 0);
2748         *entry = gtk_entry_new();
2749         gtk_table_attach_defaults
2750                 (GTK_TABLE(table), *entry, 1, 2, *count, (*count) + 1);
2751         if (GTK_TABLE(table)->nrows > (*count) + 1)
2752                 gtk_table_set_row_spacing(GTK_TABLE(table), *count, 4);
2753
2754         if (is_addr_entry)
2755                 address_completion_register_entry(GTK_ENTRY(*entry));
2756
2757         (*count)++;
2758 }
2759
2760 static Compose *compose_create(PrefsAccount *account)
2761 {
2762         Compose   *compose;
2763         GtkWidget *window;
2764         GtkWidget *vbox;
2765         GtkWidget *menubar;
2766         GtkWidget *handlebox;
2767
2768         GtkWidget *vbox2;
2769
2770         GtkWidget *table_vbox;
2771         GtkWidget *label;
2772         GtkWidget *from_optmenu_hbox;
2773         GtkWidget *to_entry;
2774         GtkWidget *to_hbox;
2775         GtkWidget *newsgroups_entry;
2776         GtkWidget *newsgroups_hbox;
2777         GtkWidget *subject_entry;
2778         GtkWidget *cc_entry;
2779         GtkWidget *cc_hbox;
2780         GtkWidget *bcc_entry;
2781         GtkWidget *bcc_hbox;
2782         GtkWidget *reply_entry;
2783         GtkWidget *reply_hbox;
2784         GtkWidget *followup_entry;
2785         GtkWidget *followup_hbox;
2786
2787         GtkWidget *paned;
2788
2789         GtkWidget *attach_scrwin;
2790         GtkWidget *attach_clist;
2791
2792         GtkWidget *edit_vbox;
2793         GtkWidget *ruler_hbox;
2794         GtkWidget *ruler;
2795         GtkWidget *scrolledwin;
2796         GtkWidget *text;
2797
2798         GtkWidget *table;
2799         GtkWidget *hbox;
2800
2801         gchar *titles[] = {_("MIME type"), _("Size"), _("Name")};
2802         guint n_menu_entries;
2803         GtkStyle  *style, *new_style;
2804         GdkColormap *cmap;
2805         GdkColor color[1];
2806         gboolean success[1];
2807         GdkFont   *font;
2808         GtkWidget *popupmenu;
2809         GtkWidget *menuitem;
2810         GtkItemFactory *popupfactory;
2811         GtkItemFactory *ifactory;
2812         gint n_entries;
2813         gint count = 0;
2814         gint i;
2815
2816         g_return_val_if_fail(account != NULL, NULL);
2817
2818         debug_print(_("Creating compose window...\n"));
2819         compose = g_new0(Compose, 1);
2820
2821         compose->account = account;
2822         compose->orig_account = account;
2823
2824         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2825         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
2826         gtk_widget_set_usize(window, -1, prefs_common.compose_height);
2827         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
2828                            GTK_SIGNAL_FUNC(compose_delete_cb), compose);
2829         gtk_signal_connect(GTK_OBJECT(window), "destroy",
2830                            GTK_SIGNAL_FUNC(compose_destroy_cb), compose);
2831         gtk_signal_connect(GTK_OBJECT(window), "focus_in_event",
2832                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
2833         gtk_signal_connect(GTK_OBJECT(window), "focus_out_event",
2834                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
2835         gtk_widget_realize(window);
2836
2837         vbox = gtk_vbox_new(FALSE, 0);
2838         gtk_container_add(GTK_CONTAINER(window), vbox);
2839
2840         n_menu_entries = sizeof(compose_entries) / sizeof(compose_entries[0]);
2841         menubar = menubar_create(window, compose_entries,
2842                                  n_menu_entries, "<Compose>", compose);
2843         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
2844
2845         handlebox = gtk_handle_box_new();
2846         gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
2847
2848         compose_toolbar_create(compose, handlebox);
2849
2850         vbox2 = gtk_vbox_new(FALSE, 2);
2851         gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
2852         gtk_container_set_border_width(GTK_CONTAINER(vbox2), BORDER_WIDTH);
2853
2854         table_vbox = gtk_vbox_new(FALSE, 0);
2855         gtk_box_pack_start(GTK_BOX(vbox2), table_vbox, FALSE, TRUE, 0);
2856         gtk_container_set_border_width(GTK_CONTAINER(table_vbox),
2857                                        BORDER_WIDTH * 2);
2858
2859         table = gtk_table_new(8, 2, FALSE);
2860         gtk_box_pack_start(GTK_BOX(table_vbox), table, FALSE, TRUE, 0);
2861
2862         /* option menu for selecting accounts */
2863         hbox = gtk_hbox_new(FALSE, 0);
2864         label = gtk_label_new(prefs_common.trans_hdr ? _("From:") : "From:");
2865         gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
2866         gtk_table_attach(GTK_TABLE(table), hbox, 0, 1, count, count + 1,
2867                          GTK_FILL, 0, 2, 0);
2868         from_optmenu_hbox = compose_account_option_menu_create(compose);
2869         gtk_table_attach_defaults(GTK_TABLE(table), from_optmenu_hbox,
2870                                   1, 2, count, count + 1);
2871         gtk_table_set_row_spacing(GTK_TABLE(table), 0, 4);
2872         count++;
2873
2874         /* header labels and entries */
2875         compose_add_entry_field(table, &to_hbox, &to_entry, &count,
2876                                 "To:", TRUE); 
2877         gtk_table_set_row_spacing(GTK_TABLE(table), 0, 4);
2878         compose_add_entry_field(table, &newsgroups_hbox, &newsgroups_entry,
2879                                 &count, "Newsgroups:", FALSE);
2880         gtk_table_set_row_spacing(GTK_TABLE(table), 1, 4);
2881         compose_add_entry_field(table, &hbox, &subject_entry, &count,
2882                                 "Subject:", FALSE);
2883         gtk_table_set_row_spacing(GTK_TABLE(table), 2, 4);
2884         compose_add_entry_field(table, &cc_hbox, &cc_entry, &count,
2885                                 "Cc:", TRUE);
2886         gtk_table_set_row_spacing(GTK_TABLE(table), 3, 4);
2887         compose_add_entry_field(table, &bcc_hbox, &bcc_entry, &count,
2888                                 "Bcc:", TRUE);
2889         gtk_table_set_row_spacing(GTK_TABLE(table), 4, 4);
2890         compose_add_entry_field(table, &reply_hbox, &reply_entry, &count,
2891                                 "Reply-To:", TRUE);
2892         gtk_table_set_row_spacing(GTK_TABLE(table), 5, 4);
2893         compose_add_entry_field(table, &followup_hbox, &followup_entry, &count,
2894                                 "Followup-To:", FALSE);
2895         gtk_table_set_row_spacing(GTK_TABLE(table), 6, 4);
2896
2897         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
2898
2899         gtk_signal_connect(GTK_OBJECT(to_entry), "activate",
2900                            GTK_SIGNAL_FUNC(to_activated), compose);
2901         gtk_signal_connect(GTK_OBJECT(newsgroups_entry), "activate",
2902                            GTK_SIGNAL_FUNC(newsgroups_activated), compose);
2903         gtk_signal_connect(GTK_OBJECT(subject_entry), "activate",
2904                            GTK_SIGNAL_FUNC(subject_activated), compose);
2905         gtk_signal_connect(GTK_OBJECT(cc_entry), "activate",
2906                            GTK_SIGNAL_FUNC(cc_activated), compose);
2907         gtk_signal_connect(GTK_OBJECT(bcc_entry), "activate",
2908                            GTK_SIGNAL_FUNC(bcc_activated), compose);
2909         gtk_signal_connect(GTK_OBJECT(reply_entry), "activate",
2910                            GTK_SIGNAL_FUNC(replyto_activated), compose);
2911         gtk_signal_connect(GTK_OBJECT(followup_entry), "activate",
2912                            GTK_SIGNAL_FUNC(followupto_activated), compose);
2913
2914         gtk_signal_connect(GTK_OBJECT(subject_entry), "grab_focus",
2915                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2916         gtk_signal_connect(GTK_OBJECT(to_entry), "grab_focus",
2917                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2918         gtk_signal_connect(GTK_OBJECT(newsgroups_entry), "grab_focus",
2919                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2920         gtk_signal_connect(GTK_OBJECT(cc_entry), "grab_focus",
2921                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2922         gtk_signal_connect(GTK_OBJECT(bcc_entry), "grab_focus",
2923                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2924         gtk_signal_connect(GTK_OBJECT(reply_entry), "grab_focus",
2925                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2926         gtk_signal_connect(GTK_OBJECT(followup_entry), "grab_focus",
2927                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2928
2929         /* attachment list */
2930         attach_scrwin = gtk_scrolled_window_new(NULL, NULL);
2931         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(attach_scrwin),
2932                                        GTK_POLICY_AUTOMATIC,
2933                                        GTK_POLICY_ALWAYS);
2934         gtk_widget_set_usize(attach_scrwin, -1, 80);
2935
2936         attach_clist = gtk_clist_new_with_titles(N_ATTACH_COLS, titles);
2937         gtk_clist_set_column_justification(GTK_CLIST(attach_clist), COL_SIZE,
2938                                            GTK_JUSTIFY_RIGHT);
2939         gtk_clist_set_column_width(GTK_CLIST(attach_clist), COL_MIMETYPE, 240);
2940         gtk_clist_set_column_width(GTK_CLIST(attach_clist), COL_SIZE, 64);
2941         gtk_clist_set_selection_mode(GTK_CLIST(attach_clist),
2942                                      GTK_SELECTION_EXTENDED);
2943         for (i = 0; i < N_ATTACH_COLS; i++)
2944                 GTK_WIDGET_UNSET_FLAGS
2945                         (GTK_CLIST(attach_clist)->column[i].button,
2946                          GTK_CAN_FOCUS);
2947         gtk_container_add(GTK_CONTAINER(attach_scrwin), attach_clist);
2948
2949         gtk_signal_connect(GTK_OBJECT(attach_clist), "select_row",
2950                            GTK_SIGNAL_FUNC(attach_selected), compose);
2951         gtk_signal_connect(GTK_OBJECT(attach_clist), "button_press_event",
2952                            GTK_SIGNAL_FUNC(attach_button_pressed), compose);
2953         gtk_signal_connect(GTK_OBJECT(attach_clist), "key_press_event",
2954                            GTK_SIGNAL_FUNC(attach_key_pressed), compose);
2955
2956         /* drag and drop */
2957         gtk_drag_dest_set(attach_clist,
2958                           GTK_DEST_DEFAULT_ALL, compose_mime_types, 1,
2959                           GDK_ACTION_COPY);
2960         gtk_signal_connect(GTK_OBJECT(attach_clist), "drag_data_received",
2961                            GTK_SIGNAL_FUNC(compose_attach_drag_received_cb),
2962                            compose);
2963
2964         /* pane between attach clist and text */
2965         paned = gtk_vpaned_new();
2966         gtk_paned_add1(GTK_PANED(paned), attach_scrwin);
2967         gtk_widget_ref(paned);
2968         gtk_widget_show_all(paned);
2969
2970         edit_vbox = gtk_vbox_new(FALSE, 0);
2971         gtk_box_pack_start(GTK_BOX(vbox2), edit_vbox, TRUE, TRUE, 0);
2972
2973         /* ruler */
2974         ruler_hbox = gtk_hbox_new(FALSE, 0);
2975         gtk_box_pack_start(GTK_BOX(edit_vbox), ruler_hbox, FALSE, FALSE, 0);
2976
2977         ruler = gtk_shruler_new();
2978         gtk_ruler_set_range(GTK_RULER(ruler), 0.0, 100.0, 1.0, 100.0);
2979         gtk_box_pack_start(GTK_BOX(ruler_hbox), ruler, TRUE, TRUE,
2980                            BORDER_WIDTH + 1);
2981         gtk_widget_set_usize(ruler_hbox, 1, -1);
2982
2983         /* text widget */
2984         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
2985         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
2986                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
2987         gtk_box_pack_start(GTK_BOX(edit_vbox), scrolledwin, TRUE, TRUE, 0);
2988         gtk_widget_set_usize(scrolledwin, prefs_common.compose_width, -1);
2989
2990         text = gtk_stext_new(gtk_scrolled_window_get_hadjustment
2991                             (GTK_SCROLLED_WINDOW(scrolledwin)),
2992                             gtk_scrolled_window_get_vadjustment
2993                             (GTK_SCROLLED_WINDOW(scrolledwin)));
2994         GTK_STEXT(text)->default_tab_width = 8;
2995         gtk_stext_set_editable(GTK_STEXT(text), TRUE);
2996         if (prefs_common.smart_wrapping) {      
2997                 gtk_stext_set_word_wrap(GTK_STEXT(text), TRUE);
2998                 gtk_stext_set_wrap_rmargin(GTK_STEXT(text), prefs_common.linewrap_len);
2999         }               
3000
3001         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
3002
3003         gtk_signal_connect(GTK_OBJECT(text), "changed",
3004                            GTK_SIGNAL_FUNC(compose_changed_cb), compose);
3005         gtk_signal_connect(GTK_OBJECT(text), "grab_focus",
3006                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
3007         gtk_signal_connect_after(GTK_OBJECT(text), "button_press_event",
3008                                  GTK_SIGNAL_FUNC(compose_button_press_cb),
3009                                  compose);
3010 #if 0
3011         gtk_signal_connect_after(GTK_OBJECT(text), "key_press_event",
3012                                  GTK_SIGNAL_FUNC(compose_key_press_cb),
3013                                  compose);
3014 #endif
3015         gtk_signal_connect_after(GTK_OBJECT(text), "size_allocate",
3016                                  GTK_SIGNAL_FUNC(compose_edit_size_alloc),
3017                                  ruler);
3018
3019         /* drag and drop */
3020         gtk_drag_dest_set(text, GTK_DEST_DEFAULT_ALL, compose_mime_types, 1,
3021                           GDK_ACTION_COPY);
3022         gtk_signal_connect(GTK_OBJECT(text), "drag_data_received",
3023                            GTK_SIGNAL_FUNC(compose_insert_drag_received_cb),
3024                            compose);
3025
3026         gtk_widget_show_all(vbox);
3027
3028         style = gtk_widget_get_style(text);
3029
3030         /* workaround for the slow down of GtkText when using Pixmap theme */
3031         if (style->engine) {
3032                 GtkThemeEngine *engine;
3033
3034                 engine = style->engine;
3035                 style->engine = NULL;
3036                 new_style = gtk_style_copy(style);
3037                 style->engine = engine;
3038         } else
3039                 new_style = gtk_style_copy(style);
3040
3041         if (prefs_common.textfont) {
3042                 CharSet charset;
3043
3044                 charset = conv_get_current_charset();
3045                 if (MB_CUR_MAX == 1) {
3046                         gchar *fontstr, *p;
3047
3048                         Xstrdup_a(fontstr, prefs_common.textfont, );
3049                         if (fontstr && (p = strchr(fontstr, ',')) != NULL)
3050                                 *p = '\0';
3051                         font = gdk_font_load(fontstr);
3052                 } else
3053                         font = gdk_fontset_load(prefs_common.textfont);
3054                 if (font) {
3055                         gdk_font_unref(new_style->font);
3056                         new_style->font = font;
3057                 }
3058         }
3059
3060         gtk_widget_set_style(text, new_style);
3061
3062         color[0] = quote_color;
3063         cmap = gdk_window_get_colormap(window->window);
3064         gdk_colormap_alloc_colors(cmap, color, 1, FALSE, TRUE, success);
3065         if (success[0] == FALSE) {
3066                 g_warning("Compose: color allocation failed.\n");
3067                 style = gtk_widget_get_style(text);
3068                 quote_color = style->black;
3069         }
3070
3071         n_entries = sizeof(compose_popup_entries) /
3072                 sizeof(compose_popup_entries[0]);
3073         popupmenu = menu_create_items(compose_popup_entries, n_entries,
3074                                       "<Compose>", &popupfactory,
3075                                       compose);
3076
3077         ifactory = gtk_item_factory_from_widget(menubar);
3078         menu_set_sensitive(ifactory, "/Edit/Undo", FALSE);
3079         menu_set_sensitive(ifactory, "/Edit/Redo", FALSE);
3080
3081         /*
3082         if (account->protocol == A_NNTP) {
3083                 gtk_widget_hide(to_hbox);
3084                 gtk_widget_hide(to_entry);
3085                 gtk_widget_hide(cc_hbox);
3086                 gtk_widget_hide(cc_entry);
3087                 gtk_table_set_row_spacing(GTK_TABLE(table), 1, 0);
3088                 gtk_table_set_row_spacing(GTK_TABLE(table), 3, 0);
3089         } else {
3090                 gtk_widget_hide(newsgroups_hbox);
3091                 gtk_widget_hide(newsgroups_entry);
3092                 gtk_table_set_row_spacing(GTK_TABLE(table), 2, 0);
3093
3094                 menu_set_sensitive(ifactory, "/Message/Followup to", FALSE);
3095         }
3096         */
3097
3098         switch (prefs_common.toolbar_style) {
3099         case TOOLBAR_NONE:
3100                 gtk_widget_hide(handlebox);
3101                 break;
3102         case TOOLBAR_ICON:
3103                 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
3104                                       GTK_TOOLBAR_ICONS);
3105                 break;
3106         case TOOLBAR_TEXT:
3107                 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
3108                                       GTK_TOOLBAR_TEXT);
3109                 break;
3110         case TOOLBAR_BOTH:
3111                 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
3112                                       GTK_TOOLBAR_BOTH);
3113                 break;
3114         }
3115
3116         gtk_widget_show(window);
3117
3118         address_completion_start(window);
3119
3120         compose->window        = window;
3121         compose->vbox          = vbox;
3122         compose->menubar       = menubar;
3123         compose->handlebox     = handlebox;
3124
3125         compose->vbox2         = vbox2;
3126
3127         compose->table_vbox       = table_vbox;
3128         compose->table            = table;
3129         compose->to_hbox          = to_hbox;
3130         compose->to_entry         = to_entry;
3131         compose->newsgroups_hbox  = newsgroups_hbox;
3132         compose->newsgroups_entry = newsgroups_entry;
3133         compose->subject_entry    = subject_entry;
3134         compose->cc_hbox          = cc_hbox;
3135         compose->cc_entry         = cc_entry;
3136         compose->bcc_hbox         = bcc_hbox;
3137         compose->bcc_entry        = bcc_entry;
3138         compose->reply_hbox       = reply_hbox;
3139         compose->reply_entry      = reply_entry;
3140         compose->followup_hbox    = followup_hbox;
3141         compose->followup_entry   = followup_entry;
3142
3143         compose->paned = paned;
3144
3145         compose->attach_scrwin = attach_scrwin;
3146         compose->attach_clist  = attach_clist;
3147
3148         compose->edit_vbox     = edit_vbox;
3149         compose->ruler_hbox    = ruler_hbox;
3150         compose->ruler         = ruler;
3151         compose->scrolledwin   = scrolledwin;
3152         compose->text          = text;
3153
3154         compose->focused_editable = NULL;
3155
3156         compose->popupmenu    = popupmenu;
3157         compose->popupfactory = popupfactory;
3158
3159         compose->mode = COMPOSE_NEW;
3160
3161         compose->replyto     = NULL;
3162         compose->cc          = NULL;
3163         compose->bcc         = NULL;
3164         compose->followup_to = NULL;
3165         compose->inreplyto   = NULL;
3166         compose->references  = NULL;
3167         compose->msgid       = NULL;
3168         compose->boundary    = NULL;
3169
3170 #if USE_GPGME
3171         compose->use_signing    = FALSE;
3172         compose->use_encryption = FALSE;
3173 #endif /* USE_GPGME */
3174
3175         compose->modified = FALSE;
3176
3177         compose->return_receipt = FALSE;
3178
3179         compose->to_list        = NULL;
3180         compose->newsgroup_list = NULL;
3181
3182         compose->exteditor_file    = NULL;
3183         compose->exteditor_pid     = -1;
3184         compose->exteditor_readdes = -1;
3185         compose->exteditor_tag     = -1;
3186
3187         compose_set_title(compose);
3188
3189         compose->use_bcc        = FALSE;
3190         compose->use_replyto    = FALSE;
3191         compose->use_followupto = FALSE;
3192
3193         /*
3194         if (account->protocol != A_NNTP) {
3195                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/To");
3196                 gtk_check_menu_item_set_active
3197                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
3198                 gtk_widget_set_sensitive(menuitem, FALSE);
3199                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Cc");
3200                 gtk_check_menu_item_set_active
3201                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
3202                 gtk_widget_set_sensitive(menuitem, FALSE);
3203         }
3204         */
3205         if (account->set_autocc && account->auto_cc) {
3206                 compose->use_cc = TRUE;
3207                 gtk_entry_set_text(GTK_ENTRY(cc_entry), account->auto_cc);
3208                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Cc");
3209                 gtk_check_menu_item_set_active
3210                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
3211         }
3212
3213         if (account->set_autobcc) {
3214                 compose->use_bcc = TRUE;
3215                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Bcc");
3216                 gtk_check_menu_item_set_active
3217                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
3218                 if (account->auto_bcc)
3219                         gtk_entry_set_text(GTK_ENTRY(bcc_entry),
3220                                            account->auto_bcc);
3221         }
3222         if (account->set_autoreplyto) {
3223                 compose->use_replyto = TRUE;
3224                 menuitem = gtk_item_factory_get_item(ifactory,
3225                                                      "/Message/Reply to");
3226                 gtk_check_menu_item_set_active
3227                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
3228                 if (account->auto_replyto)
3229                         gtk_entry_set_text(GTK_ENTRY(reply_entry),
3230                                            account->auto_replyto);
3231         }
3232
3233         menuitem = gtk_item_factory_get_item(ifactory, "/Tool/Show ruler");
3234         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
3235                                        prefs_common.show_ruler);
3236
3237 #if USE_GPGME
3238         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Sign");
3239         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
3240                                        prefs_common.default_sign);
3241         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Encrypt");
3242         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
3243                                        prefs_common.default_encrypt);
3244 #endif /* USE_GPGME */
3245
3246         addressbook_set_target_compose(compose);
3247
3248         compose_list = g_list_append(compose_list, compose);
3249
3250         /*
3251         compose->use_to         = FALSE;
3252         compose->use_cc         = FALSE;
3253         */
3254         compose->use_attach     = FALSE;
3255
3256         if (!compose->use_bcc) {
3257                 gtk_widget_hide(bcc_hbox);
3258                 gtk_widget_hide(bcc_entry);
3259                 gtk_table_set_row_spacing(GTK_TABLE(table), 4, 0);
3260         }
3261         if (!compose->use_replyto) {
3262                 gtk_widget_hide(reply_hbox);
3263                 gtk_widget_hide(reply_entry);
3264                 gtk_table_set_row_spacing(GTK_TABLE(table), 5, 0);
3265         }
3266         if (!compose->use_followupto) {
3267                 gtk_widget_hide(followup_hbox);
3268                 gtk_widget_hide(followup_entry);
3269                 gtk_table_set_row_spacing(GTK_TABLE(table), 6, 0);
3270         }
3271
3272         if (!prefs_common.show_ruler)
3273                 gtk_widget_hide(ruler_hbox);
3274
3275         select_account(compose, account);
3276
3277         return compose;
3278 }
3279
3280 #include "pixmaps/stock_mail_send.xpm"
3281 #include "pixmaps/stock_mail_send_queue.xpm"
3282 #include "pixmaps/stock_mail.xpm"
3283 #include "pixmaps/stock_paste.xpm"
3284 #include "pixmaps/stock_mail_attach.xpm"
3285 #include "pixmaps/stock_mail_compose.xpm"
3286 #include "pixmaps/linewrap.xpm"
3287 #include "pixmaps/tb_address_book.xpm"
3288
3289 #define CREATE_TOOLBAR_ICON(xpm_d) \
3290 { \
3291         icon = gdk_pixmap_create_from_xpm_d(container->window, &mask, \
3292                                             &container->style->white, \
3293                                             xpm_d); \
3294         icon_wid = gtk_pixmap_new(icon, mask); \
3295 }
3296
3297 static void compose_toolbar_create(Compose *compose, GtkWidget *container)
3298 {
3299         GtkWidget *toolbar;
3300         GdkPixmap *icon;
3301         GdkBitmap *mask;
3302         GtkWidget *icon_wid;
3303         GtkWidget *send_btn;
3304         GtkWidget *sendl_btn;
3305         GtkWidget *draft_btn;
3306         GtkWidget *insert_btn;
3307         GtkWidget *attach_btn;
3308         GtkWidget *sig_btn;
3309         GtkWidget *exteditor_btn;
3310         GtkWidget *linewrap_btn;
3311         GtkWidget *addrbook_btn;
3312
3313         toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
3314                                   GTK_TOOLBAR_BOTH);
3315         gtk_container_add(GTK_CONTAINER(container), toolbar);
3316         gtk_container_set_border_width(GTK_CONTAINER(container), 2);
3317         gtk_toolbar_set_button_relief(GTK_TOOLBAR(toolbar), GTK_RELIEF_NONE);
3318         gtk_toolbar_set_space_style(GTK_TOOLBAR(toolbar),
3319                                     GTK_TOOLBAR_SPACE_LINE);
3320
3321         CREATE_TOOLBAR_ICON(stock_mail_send_xpm);
3322         send_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3323                                            _("Send"),
3324                                            _("Send message"),
3325                                            "Send",
3326                                            icon_wid, toolbar_send_cb, compose);
3327
3328         CREATE_TOOLBAR_ICON(stock_mail_send_queue_xpm);
3329         /* CREATE_TOOLBAR_ICON(tb_mail_queue_send_xpm); */
3330         sendl_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3331                                            _("Send later"),
3332                                            _("Put into queue folder and send later"),
3333                                            "Send later",
3334                                            icon_wid, toolbar_send_later_cb,
3335                                            compose);
3336
3337         CREATE_TOOLBAR_ICON(stock_mail_xpm);
3338         draft_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3339                                             _("Draft"),
3340                                             _("Save to draft folder"),
3341                                             "Draft",
3342                                             icon_wid, toolbar_draft_cb,
3343                                             compose);
3344
3345         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
3346
3347         CREATE_TOOLBAR_ICON(stock_paste_xpm);
3348         insert_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3349                                              _("Insert"),
3350                                              _("Insert file"),
3351                                              "Insert",
3352                                              icon_wid, toolbar_insert_cb,
3353                                              compose);
3354
3355         CREATE_TOOLBAR_ICON(stock_mail_attach_xpm);
3356         attach_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3357                                              _("Attach"),
3358                                              _("Attach file"),
3359                                              "Attach",
3360                                              icon_wid, toolbar_attach_cb,
3361                                              compose);
3362
3363         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
3364
3365         CREATE_TOOLBAR_ICON(stock_mail_xpm);
3366         sig_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3367                                           _("Signature"),
3368                                           _("Insert signature"),
3369                                           "Signature",
3370                                           icon_wid, toolbar_sig_cb, compose);
3371
3372         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
3373
3374         CREATE_TOOLBAR_ICON(stock_mail_compose_xpm);
3375         exteditor_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3376                                                 _("Editor"),
3377                                                 _("Edit with external editor"),
3378                                                 "Editor",
3379                                                 icon_wid,
3380                                                 toolbar_ext_editor_cb,
3381                                                 compose);
3382
3383         CREATE_TOOLBAR_ICON(linewrap_xpm);
3384         linewrap_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3385                                                _("Linewrap"),
3386                                                _("Wrap long lines"),
3387                                                "Linewrap",
3388                                                icon_wid,
3389                                                toolbar_linewrap_cb,
3390                                                compose);
3391
3392         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
3393
3394         CREATE_TOOLBAR_ICON(tb_address_book_xpm);
3395         addrbook_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3396                                                _("Address"),
3397                                                _("Address book"),
3398                                                "Address",
3399                                                icon_wid, toolbar_address_cb,
3400                                                compose);
3401
3402         compose->toolbar       = toolbar;
3403         compose->send_btn      = send_btn;
3404         compose->sendl_btn     = sendl_btn;
3405         compose->draft_btn     = draft_btn;
3406         compose->insert_btn    = insert_btn;
3407         compose->attach_btn    = attach_btn;
3408         compose->sig_btn       = sig_btn;
3409         compose->exteditor_btn = exteditor_btn;
3410         compose->linewrap_btn  = linewrap_btn;
3411         compose->addrbook_btn  = addrbook_btn;
3412
3413         gtk_widget_show_all(toolbar);
3414 }
3415
3416 #undef CREATE_TOOLBAR_ICON
3417
3418 static GtkWidget *compose_account_option_menu_create(Compose *compose)
3419 {
3420         GList *accounts;
3421         GtkWidget *hbox;
3422         GtkWidget *optmenu;
3423         GtkWidget *menu;
3424         gint num = 0, def_menu = 0;
3425
3426         accounts = account_get_list();
3427         g_return_val_if_fail(accounts != NULL, NULL);
3428
3429         hbox = gtk_hbox_new(FALSE, 0);
3430         optmenu = gtk_option_menu_new();
3431         gtk_box_pack_start(GTK_BOX(hbox), optmenu, FALSE, FALSE, 0);
3432         menu = gtk_menu_new();
3433
3434         for (; accounts != NULL; accounts = accounts->next, num++) {
3435                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
3436                 GtkWidget *menuitem;
3437                 gchar *name;
3438
3439                 if (ac == compose->account) def_menu = num;
3440
3441                 name = g_strdup_printf("%s: %s <%s>",
3442                                        ac->account_name, ac->name, ac->address);
3443                 MENUITEM_ADD(menu, menuitem, name, ac);
3444                 g_free(name);
3445                 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3446                                    GTK_SIGNAL_FUNC(account_activated),
3447                                    compose);
3448         }
3449
3450         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), menu);
3451         gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), def_menu);
3452
3453         return hbox;
3454 }
3455
3456 static void compose_destroy(Compose *compose)
3457 {
3458         gint row;
3459         GtkCList *clist = GTK_CLIST(compose->attach_clist);
3460         AttachInfo *ainfo;
3461
3462         /* NOTE: address_completion_end() does nothing with the window
3463          * however this may change. */
3464         address_completion_end(compose->window);
3465
3466         slist_free_strings(compose->to_list);
3467         g_slist_free(compose->to_list);
3468         slist_free_strings(compose->newsgroup_list);
3469         g_slist_free(compose->newsgroup_list);
3470
3471         procmsg_msginfo_free(compose->targetinfo);
3472
3473         g_free(compose->replyto);
3474         g_free(compose->cc);
3475         g_free(compose->bcc);
3476         g_free(compose->newsgroups);
3477         g_free(compose->followup_to);
3478
3479         g_free(compose->inreplyto);
3480         g_free(compose->references);
3481         g_free(compose->msgid);
3482         g_free(compose->boundary);
3483
3484         g_free(compose->exteditor_file);
3485
3486         for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
3487              row++)
3488                 compose_attach_info_free(ainfo);
3489
3490         if (addressbook_get_target_compose() == compose)
3491                 addressbook_set_target_compose(NULL);
3492
3493         prefs_common.compose_width = compose->scrolledwin->allocation.width;
3494         prefs_common.compose_height = compose->window->allocation.height;
3495
3496         gtk_widget_destroy(compose->paned);
3497
3498         g_free(compose);
3499
3500         compose_list = g_list_remove(compose_list, compose);
3501 }
3502
3503 static void compose_attach_info_free(AttachInfo *ainfo)
3504 {
3505         g_free(ainfo->file);
3506         g_free(ainfo->content_type);
3507         g_free(ainfo->name);
3508         g_free(ainfo);
3509 }
3510
3511 static void compose_attach_remove_selected(Compose *compose)
3512 {
3513         GtkCList *clist = GTK_CLIST(compose->attach_clist);
3514         AttachInfo *ainfo;
3515         gint row;
3516
3517         while (clist->selection != NULL) {
3518                 row = GPOINTER_TO_INT(clist->selection->data);
3519                 ainfo = gtk_clist_get_row_data(clist, row);
3520                 compose_attach_info_free(ainfo);
3521                 gtk_clist_remove(clist, row);
3522         }
3523 }
3524
3525 static struct _AttachProperty
3526 {
3527         GtkWidget *window;
3528         GtkWidget *mimetype_entry;
3529         GtkWidget *encoding_optmenu;
3530         GtkWidget *path_entry;
3531         GtkWidget *filename_entry;
3532         GtkWidget *ok_btn;
3533         GtkWidget *cancel_btn;
3534 } attach_prop;
3535
3536 static void compose_attach_property(Compose *compose)
3537 {
3538         GtkCList *clist = GTK_CLIST(compose->attach_clist);
3539         AttachInfo *ainfo;
3540         gint row;
3541         gboolean cancelled;
3542         GtkOptionMenu *optmenu;
3543
3544         if (!clist->selection) return;
3545         row = GPOINTER_TO_INT(clist->selection->data);
3546
3547         ainfo = gtk_clist_get_row_data(clist, row);
3548         if (!ainfo) return;
3549
3550         if (!attach_prop.window)
3551                 compose_attach_property_create(&cancelled);
3552         gtk_widget_grab_focus(attach_prop.ok_btn);
3553         gtk_widget_show(attach_prop.window);
3554         manage_window_set_transient(GTK_WINDOW(attach_prop.window));
3555
3556         optmenu = GTK_OPTION_MENU(attach_prop.encoding_optmenu);
3557         if (ainfo->encoding == ENC_UNKNOWN)
3558                 gtk_option_menu_set_history(optmenu, ENC_BASE64);
3559         else
3560                 gtk_option_menu_set_history(optmenu, ainfo->encoding);
3561
3562         gtk_entry_set_text(GTK_ENTRY(attach_prop.mimetype_entry),
3563                            ainfo->content_type ? ainfo->content_type : "");
3564         gtk_entry_set_text(GTK_ENTRY(attach_prop.path_entry),
3565                            ainfo->file ? ainfo->file : "");
3566         gtk_entry_set_text(GTK_ENTRY(attach_prop.filename_entry),
3567                            ainfo->name ? ainfo->name : "");
3568
3569         for (;;) {
3570                 gchar *text;
3571                 gchar *cnttype = NULL;
3572                 gchar *file = NULL;
3573                 off_t size = 0;
3574                 GtkWidget *menu;
3575                 GtkWidget *menuitem;
3576
3577                 gtk_main();
3578
3579                 if (cancelled == TRUE) {
3580                         gtk_widget_hide(attach_prop.window);
3581                         break;
3582                 }
3583
3584                 text = gtk_entry_get_text(GTK_ENTRY(attach_prop.mimetype_entry));
3585                 if (*text != '\0') {
3586                         gchar *p;
3587
3588                         text = g_strstrip(g_strdup(text));
3589                         if ((p = strchr(text, '/')) && !strchr(p + 1, '/')) {
3590                                 cnttype = g_strdup(text);
3591                                 g_free(text);
3592                         } else {
3593                                 alertpanel_error(_("Invalid MIME type."));
3594                                 g_free(text);
3595                                 continue;
3596                         }
3597                 }
3598
3599                 menu = gtk_option_menu_get_menu(optmenu);
3600                 menuitem = gtk_menu_get_active(GTK_MENU(menu));
3601                 ainfo->encoding = GPOINTER_TO_INT
3602                         (gtk_object_get_user_data(GTK_OBJECT(menuitem)));
3603
3604                 text = gtk_entry_get_text(GTK_ENTRY(attach_prop.path_entry));
3605                 if (*text != '\0') {
3606                         if (is_file_exist(text) &&
3607                             (size = get_file_size(text)) > 0)
3608                                 file = g_strdup(text);
3609                         else {
3610                                 alertpanel_error
3611                                         (_("File doesn't exist or is empty."));
3612                                 g_free(cnttype);
3613                                 continue;
3614                         }
3615                 }
3616
3617                 text = gtk_entry_get_text(GTK_ENTRY(attach_prop.filename_entry));
3618                 if (*text != '\0') {
3619                         g_free(ainfo->name);
3620                         ainfo->name = g_strdup(text);
3621                 }
3622
3623                 if (cnttype) {
3624                         g_free(ainfo->content_type);
3625                         ainfo->content_type = cnttype;
3626                 }
3627                 if (file) {
3628                         g_free(ainfo->file);
3629                         ainfo->file = file;
3630                 }
3631                 if (size)
3632                         ainfo->size = size;
3633
3634                 gtk_clist_set_text(clist, row, COL_MIMETYPE,
3635                                    ainfo->content_type);
3636                 gtk_clist_set_text(clist, row, COL_SIZE,
3637                                    to_human_readable(ainfo->size));
3638                 gtk_clist_set_text(clist, row, COL_NAME, ainfo->name);
3639
3640                 gtk_widget_hide(attach_prop.window);
3641                 break;
3642         }
3643 }
3644
3645 #define SET_LABEL_AND_ENTRY(str, entry, top) \
3646 { \
3647         label = gtk_label_new(str); \
3648         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), \
3649                          GTK_FILL, 0, 0, 0); \
3650         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); \
3651  \
3652         entry = gtk_entry_new(); \
3653         gtk_table_attach(GTK_TABLE(table), entry, 1, 2, top, (top + 1), \
3654                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); \
3655 }
3656
3657 static void compose_attach_property_create(gboolean *cancelled)
3658 {
3659         GtkWidget *window;
3660         GtkWidget *vbox;
3661         GtkWidget *table;
3662         GtkWidget *label;
3663         GtkWidget *mimetype_entry;
3664         GtkWidget *hbox;
3665         GtkWidget *optmenu;
3666         GtkWidget *optmenu_menu;
3667         GtkWidget *menuitem;
3668         GtkWidget *path_entry;
3669         GtkWidget *filename_entry;
3670         GtkWidget *hbbox;
3671         GtkWidget *ok_btn;
3672         GtkWidget *cancel_btn;
3673         GList     *mime_type_list, *strlist;
3674
3675         debug_print("Creating attach_property window...\n");
3676
3677         window = gtk_window_new(GTK_WINDOW_DIALOG);
3678         gtk_widget_set_usize(window, 480, -1);
3679         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
3680         gtk_window_set_title(GTK_WINDOW(window), _("Property"));
3681         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3682         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
3683         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
3684                            GTK_SIGNAL_FUNC(attach_property_delete_event),
3685                            cancelled);
3686         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
3687                            GTK_SIGNAL_FUNC(attach_property_key_pressed),
3688                            cancelled);
3689
3690         vbox = gtk_vbox_new(FALSE, 8);
3691         gtk_container_add(GTK_CONTAINER(window), vbox);
3692
3693         table = gtk_table_new(4, 2, FALSE);
3694         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
3695         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
3696         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
3697
3698         label = gtk_label_new(_("MIME type")); 
3699         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, (0 + 1), 
3700                          GTK_FILL, 0, 0, 0); 
3701         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); 
3702         mimetype_entry = gtk_combo_new(); 
3703         gtk_table_attach(GTK_TABLE(table), mimetype_entry, 1, 2, 0, (0 + 1), 
3704                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
3705                          
3706         /* stuff with list */
3707         mime_type_list = procmime_get_mime_type_list();
3708         strlist = NULL;
3709         for (; mime_type_list != NULL; mime_type_list = mime_type_list->next) {
3710                 MimeType *type = (MimeType *) mime_type_list->data;
3711                 strlist = g_list_append(strlist, 
3712                                 g_strdup_printf("%s/%s",
3713                                         type->type, type->sub_type));
3714         }
3715         
3716         gtk_combo_set_popdown_strings(GTK_COMBO(mimetype_entry), strlist);
3717
3718         for (mime_type_list = strlist; mime_type_list != NULL; 
3719                 mime_type_list = mime_type_list->next)
3720                 g_free(mime_type_list->data);
3721         g_list_free(strlist);
3722                          
3723         mimetype_entry = GTK_COMBO(mimetype_entry)->entry;                       
3724
3725         label = gtk_label_new(_("Encoding"));
3726         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
3727                          GTK_FILL, 0, 0, 0);
3728         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
3729
3730         hbox = gtk_hbox_new(FALSE, 0);
3731         gtk_table_attach(GTK_TABLE(table), hbox, 1, 2, 1, 2,
3732                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
3733
3734         optmenu = gtk_option_menu_new();
3735         gtk_box_pack_start(GTK_BOX(hbox), optmenu, FALSE, FALSE, 0);
3736
3737         optmenu_menu = gtk_menu_new();
3738         MENUITEM_ADD(optmenu_menu, menuitem, "7bit", ENC_7BIT);
3739         gtk_widget_set_sensitive(menuitem, FALSE);
3740         MENUITEM_ADD(optmenu_menu, menuitem, "8bit", ENC_8BIT);
3741         gtk_widget_set_sensitive(menuitem, FALSE);
3742         MENUITEM_ADD(optmenu_menu, menuitem, "quoted-printable", ENC_QUOTED_PRINTABLE);
3743         gtk_widget_set_sensitive(menuitem, FALSE);
3744         MENUITEM_ADD(optmenu_menu, menuitem, "base64", ENC_BASE64);
3745
3746         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), optmenu_menu);
3747
3748         SET_LABEL_AND_ENTRY(_("Path"),      path_entry,     2);
3749         SET_LABEL_AND_ENTRY(_("File name"), filename_entry, 3);
3750
3751         gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
3752                                 &cancel_btn, _("Cancel"), NULL, NULL);
3753         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
3754         gtk_widget_grab_default(ok_btn);
3755
3756         gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
3757                            GTK_SIGNAL_FUNC(attach_property_ok),
3758                            cancelled);
3759         gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
3760                            GTK_SIGNAL_FUNC(attach_property_cancel),
3761                            cancelled);
3762
3763         gtk_widget_show_all(vbox);
3764
3765         attach_prop.window           = window;
3766         attach_prop.mimetype_entry   = mimetype_entry;
3767         attach_prop.encoding_optmenu = optmenu;
3768         attach_prop.path_entry       = path_entry;
3769         attach_prop.filename_entry   = filename_entry;
3770         attach_prop.ok_btn           = ok_btn;
3771         attach_prop.cancel_btn       = cancel_btn;
3772 }
3773
3774 #undef SET_LABEL_AND_ENTRY
3775
3776 static void attach_property_ok(GtkWidget *widget, gboolean *cancelled)
3777 {
3778         *cancelled = FALSE;
3779         gtk_main_quit();
3780 }
3781
3782 static void attach_property_cancel(GtkWidget *widget, gboolean *cancelled)
3783 {
3784         *cancelled = TRUE;
3785         gtk_main_quit();
3786 }
3787
3788 static gint attach_property_delete_event(GtkWidget *widget, GdkEventAny *event,
3789                                          gboolean *cancelled)
3790 {
3791         *cancelled = TRUE;
3792         gtk_main_quit();
3793
3794         return TRUE;
3795 }
3796
3797 static void attach_property_key_pressed(GtkWidget *widget, GdkEventKey *event,
3798                                         gboolean *cancelled)
3799 {
3800         if (event && event->keyval == GDK_Escape) {
3801                 *cancelled = TRUE;
3802                 gtk_main_quit();
3803         }
3804 }
3805
3806 static void compose_exec_ext_editor(Compose *compose)
3807 {
3808         gchar tmp[64];
3809         pid_t pid;
3810         gint pipe_fds[2];
3811
3812         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.%08x",
3813                    g_get_tmp_dir(), G_DIR_SEPARATOR, (gint)compose);
3814
3815         if (pipe(pipe_fds) < 0) {
3816                 perror("pipe");
3817                 return;
3818         }
3819
3820         if ((pid = fork()) < 0) {
3821                 perror("fork");
3822                 return;
3823         }
3824
3825         if (pid != 0) {
3826                 /* close the write side of the pipe */
3827                 close(pipe_fds[1]);
3828
3829                 compose->exteditor_file    = g_strdup(tmp);
3830                 compose->exteditor_pid     = pid;
3831                 compose->exteditor_readdes = pipe_fds[0];
3832
3833                 compose_set_ext_editor_sensitive(compose, FALSE);
3834
3835                 compose->exteditor_tag =
3836                         gdk_input_add(pipe_fds[0], GDK_INPUT_READ,
3837                                       compose_input_cb, compose);
3838         } else {        /* process-monitoring process */
3839                 pid_t pid_ed;
3840
3841                 if (setpgid(0, 0))
3842                         perror("setpgid");
3843
3844                 /* close the read side of the pipe */
3845                 close(pipe_fds[0]);
3846
3847                 if (compose_write_body_to_file(compose, tmp) < 0) {
3848                         fd_write(pipe_fds[1], "2\n", 2);
3849                         _exit(1);
3850                 }
3851
3852                 pid_ed = compose_exec_ext_editor_real(tmp);
3853                 if (pid_ed < 0) {
3854                         fd_write(pipe_fds[1], "1\n", 2);
3855                         _exit(1);
3856                 }
3857
3858                 /* wait until editor is terminated */
3859                 waitpid(pid_ed, NULL, 0);
3860
3861                 fd_write(pipe_fds[1], "0\n", 2);
3862
3863                 close(pipe_fds[1]);
3864                 _exit(0);
3865         }
3866 }
3867
3868 static gint compose_exec_ext_editor_real(const gchar *file)
3869 {
3870         static gchar *def_cmd = "emacs %s";
3871         gchar buf[1024];
3872         gchar *p;
3873         gchar **cmdline;
3874         pid_t pid;
3875
3876         g_return_val_if_fail(file != NULL, -1);
3877
3878         if ((pid = fork()) < 0) {
3879                 perror("fork");
3880                 return -1;
3881         }
3882
3883         if (pid != 0) return pid;
3884
3885         /* grandchild process */
3886
3887         if (setpgid(0, getppid()))
3888                 perror("setpgid");
3889
3890         if (prefs_common.ext_editor_cmd &&
3891             (p = strchr(prefs_common.ext_editor_cmd, '%')) &&
3892             *(p + 1) == 's' && !strchr(p + 2, '%')) {
3893                 g_snprintf(buf, sizeof(buf), prefs_common.ext_editor_cmd, file);
3894         } else {
3895                 if (prefs_common.ext_editor_cmd)
3896                         g_warning(_("External editor command line is invalid: `%s'\n"),
3897                                   prefs_common.ext_editor_cmd);
3898                 g_snprintf(buf, sizeof(buf), def_cmd, file);
3899         }
3900
3901         cmdline = g_strsplit(buf, " ", 1024);
3902         execvp(cmdline[0], cmdline);
3903
3904         perror("execvp");
3905         g_strfreev(cmdline);
3906
3907         _exit(1);
3908 }
3909
3910 static gboolean compose_ext_editor_kill(Compose *compose)
3911 {
3912         pid_t pgid = compose->exteditor_pid * -1;
3913         gint ret;
3914
3915         ret = kill(pgid, 0);
3916
3917         if (ret == 0 || (ret == -1 && EPERM == errno)) {
3918                 AlertValue val;
3919                 gchar *msg;
3920
3921                 msg = g_strdup_printf
3922                         (_("The external editor is still working.\n"
3923                            "Force terminating the process?\n"
3924                            "process group id: %d"), -pgid);
3925                 val = alertpanel(_("Notice"), msg, _("Yes"), _("+No"), NULL);
3926                 g_free(msg);
3927
3928                 if (val == G_ALERTDEFAULT) {
3929                         gdk_input_remove(compose->exteditor_tag);
3930                         close(compose->exteditor_readdes);
3931
3932                         if (kill(pgid, SIGTERM) < 0) perror("kill");
3933                         waitpid(compose->exteditor_pid, NULL, 0);
3934
3935                         g_warning(_("Terminated process group id: %d"), -pgid);
3936                         g_warning(_("Temporary file: %s"),
3937                                   compose->exteditor_file);
3938
3939                         compose_set_ext_editor_sensitive(compose, TRUE);
3940
3941                         g_free(compose->exteditor_file);
3942                         compose->exteditor_file    = NULL;
3943                         compose->exteditor_pid     = -1;
3944                         compose->exteditor_readdes = -1;
3945                         compose->exteditor_tag     = -1;
3946                 } else
3947                         return FALSE;
3948         }
3949
3950         return TRUE;
3951 }
3952
3953 static void compose_input_cb(gpointer data, gint source,
3954                              GdkInputCondition condition)
3955 {
3956         gchar buf[3];
3957         Compose *compose = (Compose *)data;
3958         gint i = 0;
3959
3960         debug_print(_("Compose: input from monitoring process\n"));
3961
3962         gdk_input_remove(compose->exteditor_tag);
3963
3964         for (;;) {
3965                 if (read(source, &buf[i], 1) < 1) {
3966                         buf[0] = '3';
3967                         break;
3968                 }
3969                 if (buf[i] == '\n') {
3970                         buf[i] = '\0';
3971                         break;
3972                 }
3973                 i++;
3974                 if (i == sizeof(buf) - 1)
3975                         break;
3976         }
3977
3978         waitpid(compose->exteditor_pid, NULL, 0);
3979
3980         if (buf[0] == '0') {            /* success */
3981                 GtkSText *text = GTK_STEXT(compose->text);
3982
3983                 gtk_stext_freeze(text);
3984                 gtk_stext_set_point(text, 0);
3985                 gtk_stext_forward_delete(text, gtk_stext_get_length(text));
3986                 compose_insert_file(compose, compose->exteditor_file);
3987                 compose_changed_cb(NULL, compose);
3988                 gtk_stext_thaw(text);
3989
3990                 if (unlink(compose->exteditor_file) < 0)
3991                         FILE_OP_ERROR(compose->exteditor_file, "unlink");
3992         } else if (buf[0] == '1') {     /* failed */
3993                 g_warning(_("Couldn't exec external editor\n"));
3994                 if (unlink(compose->exteditor_file) < 0)
3995                         FILE_OP_ERROR(compose->exteditor_file, "unlink");
3996         } else if (buf[0] == '2') {
3997                 g_warning(_("Couldn't write to file\n"));
3998         } else if (buf[0] == '3') {
3999                 g_warning(_("Pipe read failed\n"));
4000         }
4001
4002         close(source);
4003
4004         compose_set_ext_editor_sensitive(compose, TRUE);
4005
4006         g_free(compose->exteditor_file);
4007         compose->exteditor_file    = NULL;
4008         compose->exteditor_pid     = -1;
4009         compose->exteditor_readdes = -1;
4010         compose->exteditor_tag     = -1;
4011 }
4012
4013 static void compose_set_ext_editor_sensitive(Compose *compose,
4014                                              gboolean sensitive)
4015 {
4016         GtkItemFactory *ifactory;
4017
4018         ifactory = gtk_item_factory_from_widget(compose->menubar);
4019
4020         menu_set_sensitive(ifactory, "/Message/Send", sensitive);
4021         menu_set_sensitive(ifactory, "/Message/Send later", sensitive);
4022         menu_set_sensitive(ifactory, "/Message/Save to draft folder",
4023                            sensitive);
4024         menu_set_sensitive(ifactory, "/File/Insert file", sensitive);
4025         menu_set_sensitive(ifactory, "/File/Insert signature", sensitive);
4026         menu_set_sensitive(ifactory, "/Edit/Wrap long lines", sensitive);
4027         menu_set_sensitive(ifactory, "/Edit/Edit with external editor",
4028                            sensitive);
4029
4030         gtk_widget_set_sensitive(compose->text,          sensitive);
4031         gtk_widget_set_sensitive(compose->send_btn,      sensitive);
4032         gtk_widget_set_sensitive(compose->sendl_btn,     sensitive);
4033         gtk_widget_set_sensitive(compose->draft_btn,     sensitive);
4034         gtk_widget_set_sensitive(compose->insert_btn,    sensitive);
4035         gtk_widget_set_sensitive(compose->sig_btn,       sensitive);
4036         gtk_widget_set_sensitive(compose->exteditor_btn, sensitive);
4037         gtk_widget_set_sensitive(compose->linewrap_btn,  sensitive);
4038 }
4039
4040 static gint calc_cursor_xpos(GtkSText *text, gint extra, gint char_width)
4041 {
4042         gint cursor_pos;
4043
4044         cursor_pos = (text->cursor_pos_x - extra) / char_width;
4045         cursor_pos = MAX(cursor_pos, 0);
4046
4047         return cursor_pos;
4048 }
4049
4050 /* callback functions */
4051
4052 /* compose_edit_size_alloc() - called when resized. don't know whether Gtk
4053  * includes "non-client" (windows-izm) in calculation, so this calculation
4054  * may not be accurate.
4055  */
4056 static gboolean compose_edit_size_alloc(GtkEditable *widget,
4057                                         GtkAllocation *allocation,
4058                                         GtkSHRuler *shruler)
4059 {
4060         if (prefs_common.show_ruler) {
4061                 gint char_width;
4062                 gint line_width_in_chars;
4063
4064                 char_width = gtkut_get_font_width
4065                         (GTK_WIDGET(widget)->style->font);
4066                 line_width_in_chars =
4067                         (allocation->width - allocation->x) / char_width;
4068
4069                 /* got the maximum */
4070                 gtk_ruler_set_range(GTK_RULER(shruler),
4071                                     0.0, line_width_in_chars,
4072                                     calc_cursor_xpos(GTK_STEXT(widget),
4073                                                      allocation->x,
4074                                                      char_width),
4075                                     /*line_width_in_chars*/ char_width);
4076         }
4077
4078         return TRUE;
4079 }
4080
4081 static void toolbar_send_cb(GtkWidget *widget, gpointer data)
4082 {
4083         compose_send_cb(data, 0, NULL);
4084 }
4085
4086 static void toolbar_send_later_cb(GtkWidget *widget, gpointer data)
4087 {
4088         compose_send_later_cb(data, 0, NULL);
4089 }
4090
4091 static void toolbar_draft_cb(GtkWidget *widget, gpointer data)
4092 {
4093         compose_draft_cb(data, 0, NULL);
4094 }
4095
4096 static void toolbar_insert_cb(GtkWidget *widget, gpointer data)
4097 {
4098         compose_insert_file_cb(data, 0, NULL);
4099 }
4100
4101 static void toolbar_attach_cb(GtkWidget *widget, gpointer data)
4102 {
4103         compose_attach_cb(data, 0, NULL);
4104 }
4105
4106 static void toolbar_sig_cb(GtkWidget *widget, gpointer data)
4107 {
4108         Compose *compose = (Compose *)data;
4109
4110         compose_insert_sig(compose);
4111 }
4112
4113 static void toolbar_ext_editor_cb(GtkWidget *widget, gpointer data)
4114 {
4115         Compose *compose = (Compose *)data;
4116
4117         compose_exec_ext_editor(compose);
4118 }
4119
4120 static void toolbar_linewrap_cb(GtkWidget *widget, gpointer data)
4121 {
4122         Compose *compose = (Compose *)data;
4123
4124         compose_wrap_line(compose);
4125 }
4126
4127 static void toolbar_address_cb(GtkWidget *widget, gpointer data)
4128 {
4129         compose_address_cb(data, 0, NULL);
4130 }
4131
4132 static void select_account(Compose * compose, PrefsAccount * ac)
4133 {
4134                 compose->account = ac;
4135                 compose_set_title(compose);
4136
4137                 if (ac->protocol == A_NNTP) {
4138                         GtkItemFactory *ifactory;
4139                         GtkWidget *menuitem;
4140
4141                         ifactory = gtk_item_factory_from_widget(compose->menubar);
4142                         menu_set_sensitive(ifactory,
4143                                            "/Message/Followup to", TRUE);
4144
4145                         gtk_widget_show(compose->newsgroups_hbox);
4146                         gtk_widget_show(compose->newsgroups_entry);
4147                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
4148                                                   1, 4);
4149
4150                         compose->use_to = FALSE;
4151                         compose->use_cc = FALSE;
4152
4153                         menuitem = gtk_item_factory_get_item(ifactory, "/Message/To");
4154                         gtk_check_menu_item_set_active
4155                                 (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
4156                         menu_set_sensitive(ifactory,
4157                                            "/Message/To", TRUE);
4158                         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Cc");
4159                         gtk_check_menu_item_set_active
4160                                 (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
4161
4162                         gtk_widget_hide(compose->to_hbox);
4163                         gtk_widget_hide(compose->to_entry);
4164                         gtk_widget_hide(compose->cc_hbox);
4165                         gtk_widget_hide(compose->cc_entry);
4166
4167                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
4168                                                   0, 0);
4169                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
4170                                                   3, 0);
4171                 }
4172                 else {
4173                         GtkItemFactory *ifactory;
4174                         GtkWidget *menuitem;
4175
4176                         ifactory = gtk_item_factory_from_widget(compose->menubar);
4177                         menu_set_sensitive(ifactory,
4178                                            "/Message/Followup to", FALSE);
4179
4180                         gtk_entry_set_text(GTK_ENTRY(compose->newsgroups_entry), "");
4181                         gtk_widget_hide(compose->newsgroups_hbox);
4182                         gtk_widget_hide(compose->newsgroups_entry);
4183                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
4184                                                   1, 0);
4185
4186                         compose->use_to = TRUE;
4187                         compose->use_cc = TRUE;
4188
4189                         menuitem = gtk_item_factory_get_item(ifactory, "/Message/To");
4190                         gtk_check_menu_item_set_active
4191                                 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
4192                         menu_set_sensitive(ifactory,
4193                                            "/Message/To", FALSE);
4194                         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Cc");
4195                         gtk_check_menu_item_set_active
4196                                 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
4197
4198                         gtk_widget_show(compose->to_hbox);
4199                         gtk_widget_show(compose->to_entry);
4200                         gtk_widget_show(compose->cc_hbox);
4201                         gtk_widget_show(compose->cc_entry);
4202
4203                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
4204                                                   0, 4);
4205                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
4206                                                   3, 4);
4207                 }
4208                 gtk_widget_queue_resize(compose->table_vbox);
4209 }
4210
4211 static void account_activated(GtkMenuItem *menuitem, gpointer data)
4212 {
4213         Compose *compose = (Compose *)data;
4214
4215         PrefsAccount *ac;
4216
4217         ac = (PrefsAccount *)gtk_object_get_user_data(GTK_OBJECT(menuitem));
4218         g_return_if_fail(ac != NULL);
4219
4220         if (ac != compose->account)
4221                 select_account(compose, ac);
4222 }
4223
4224 static void attach_selected(GtkCList *clist, gint row, gint column,
4225                             GdkEvent *event, gpointer data)
4226 {
4227         Compose *compose = (Compose *)data;
4228
4229         if (event && event->type == GDK_2BUTTON_PRESS)
4230                 compose_attach_property(compose);
4231 }
4232
4233 static void attach_button_pressed(GtkWidget *widget, GdkEventButton *event,
4234                                   gpointer data)
4235 {
4236         Compose *compose = (Compose *)data;
4237         GtkCList *clist = GTK_CLIST(compose->attach_clist);
4238         gint row, column;
4239
4240         if (!event) return;
4241
4242         if (event->button == 3) {
4243                 if ((clist->selection && !clist->selection->next) ||
4244                     !clist->selection) {
4245                         gtk_clist_unselect_all(clist);
4246                         if (gtk_clist_get_selection_info(clist,
4247                                                          event->x, event->y,
4248                                                          &row, &column)) {
4249                                 gtk_clist_select_row(clist, row, column);
4250                                 gtkut_clist_set_focus_row(clist, row);
4251                         }
4252                 }
4253                 gtk_menu_popup(GTK_MENU(compose->popupmenu), NULL, NULL,
4254                                NULL, NULL, event->button, event->time);
4255         }
4256 }
4257
4258 static void attach_key_pressed(GtkWidget *widget, GdkEventKey *event,
4259                                gpointer data)
4260 {
4261         Compose *compose = (Compose *)data;
4262
4263         if (!event) return;
4264
4265         switch (event->keyval) {
4266         case GDK_Delete:
4267                 compose_attach_remove_selected(compose);
4268                 break;
4269         }
4270 }
4271
4272 static void compose_send_cb(gpointer data, guint action, GtkWidget *widget)
4273 {
4274         Compose *compose = (Compose *)data;
4275         gint val;
4276
4277         val = compose_send(compose);
4278
4279         if (val == 0) gtk_widget_destroy(compose->window);
4280 }
4281
4282 static void compose_send_later_cb(gpointer data, guint action,
4283                                   GtkWidget *widget)
4284 {
4285         Compose *compose = (Compose *)data;
4286         gchar tmp[22];
4287         gchar *to, *newsgroups;
4288
4289         to = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
4290         newsgroups = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
4291         if (*to == '\0' && *newsgroups == '\0') {
4292                 alertpanel_error(_("Recipient is not specified."));
4293                 return;
4294         }
4295
4296         g_snprintf(tmp, 22, "%s%ctmpmsg%d",
4297                    g_get_tmp_dir(), G_DIR_SEPARATOR, (gint)compose);
4298
4299         if (prefs_common.linewrap_at_send)
4300                 compose_wrap_line(compose);
4301
4302         if (compose_write_to_file(compose, tmp, FALSE) < 0 ||
4303             compose_queue(compose, tmp) < 0) {
4304                 alertpanel_error(_("Can't queue the message."));
4305                 return;
4306         }
4307
4308         if (prefs_common.savemsg) {
4309                 if (compose_save_to_outbox(compose, tmp) < 0)
4310                         alertpanel_error
4311                                 (_("Can't save the message to outbox."));
4312         }
4313
4314         if (unlink(tmp) < 0)
4315                 FILE_OP_ERROR(tmp, "unlink");
4316
4317         gtk_widget_destroy(compose->window);
4318 }
4319
4320 static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
4321 {
4322         Compose *compose = (Compose *)data;
4323         FolderItem *draft;
4324         gchar *tmp;
4325
4326         draft = folder_get_default_draft();
4327         folder_item_scan(draft);
4328
4329         if (procmsg_msg_exist(compose->targetinfo) &&
4330             compose->targetinfo->folder == draft) {
4331                 if (folder_item_remove_msg(draft,
4332                                            compose->targetinfo->msgnum) < 0)
4333                         g_warning(_("can't remove the old draft message\n"));
4334         }
4335         tmp = g_strdup_printf("%s%cdraft.%d", g_get_tmp_dir(),
4336                               G_DIR_SEPARATOR, (gint)compose);
4337
4338         if (compose_write_to_file(compose, tmp, TRUE) < 0) {
4339                 g_free(tmp);
4340                 return;
4341         }
4342
4343         if (folder_item_add_msg(draft, tmp, TRUE) < 0) {
4344                 unlink(tmp);
4345                 g_free(tmp);
4346                 return;
4347         }
4348
4349         g_free(tmp);
4350
4351         /* folderview_scan_folder_a(DRAFT_DIR, TRUE); */
4352
4353         gtk_widget_destroy(compose->window);
4354 }
4355
4356 static void compose_attach_cb(gpointer data, guint action, GtkWidget *widget)
4357 {
4358         Compose *compose = (Compose *)data;
4359         GList *file_list;
4360
4361         file_list = filesel_select_multiple_files(_("Select file"), NULL);
4362
4363         if (file_list) {
4364                 GList *tmp;
4365
4366                 for ( tmp = file_list; tmp; tmp = tmp->next) {
4367                         gchar *file = (gchar *) tmp->data;
4368                         compose_attach_append(compose, file, MIME_UNKNOWN);
4369                         g_free(file);
4370                 }
4371                 g_list_free(file_list);
4372         }               
4373 }
4374
4375 static void compose_insert_file_cb(gpointer data, guint action,
4376                                    GtkWidget *widget)
4377 {
4378         Compose *compose = (Compose *)data;
4379         GList *file_list;
4380
4381         file_list = filesel_select_multiple_files(_("Select file"), NULL);
4382
4383         if (file_list) {
4384                 GList *tmp;
4385
4386                 for ( tmp = file_list; tmp; tmp = tmp->next) {
4387                         gchar *file = (gchar *) tmp->data;
4388                         compose_insert_file(compose, file);
4389                         g_free(file);
4390                 }
4391                 g_list_free(file_list);
4392         }
4393 }
4394
4395 static gint compose_delete_cb(GtkWidget *widget, GdkEventAny *event,
4396                               gpointer data)
4397 {
4398         compose_close_cb(data, 0, NULL);
4399         return TRUE;
4400 }
4401
4402 static void compose_close_cb(gpointer data, guint action, GtkWidget *widget)
4403 {
4404         Compose *compose = (Compose *)data;
4405         AlertValue val;
4406
4407         if (compose->exteditor_tag != -1) {
4408                 if (!compose_ext_editor_kill(compose))
4409                         return;
4410         }
4411
4412         if (compose->modified) {
4413                 val = alertpanel(_("Discard message"),
4414                                  _("This message has been modified. discard it?"),
4415                                  _("Discard"), _("to Draft"), _("Cancel"));
4416
4417                 switch (val) {
4418                 case G_ALERTDEFAULT:
4419                         break;
4420                 case G_ALERTALTERNATE:
4421                         compose_draft_cb(data, 0, NULL);
4422                         return;
4423                 default:
4424                         return;
4425                 }
4426         }
4427
4428         gtk_widget_destroy(compose->window);
4429 }
4430
4431 static void compose_address_cb(gpointer data, guint action, GtkWidget *widget)
4432 {
4433         Compose *compose = (Compose *)data;
4434
4435         addressbook_open(compose);
4436 }
4437
4438 static void compose_ext_editor_cb(gpointer data, guint action,
4439                                   GtkWidget *widget)
4440 {
4441         Compose *compose = (Compose *)data;
4442
4443         compose_exec_ext_editor(compose);
4444 }
4445
4446 static void compose_destroy_cb(GtkWidget *widget, Compose *compose)
4447 {
4448         compose_destroy(compose);
4449 }
4450
4451 static void compose_cut_cb(Compose *compose)
4452 {
4453         if (compose->focused_editable &&
4454             GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
4455                 gtk_editable_cut_clipboard
4456                         (GTK_EDITABLE(compose->focused_editable));
4457 }
4458
4459 static void compose_copy_cb(Compose *compose)
4460 {
4461         if (compose->focused_editable &&
4462             GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
4463                 gtk_editable_copy_clipboard
4464                         (GTK_EDITABLE(compose->focused_editable));
4465 }
4466
4467 static void compose_paste_cb(Compose *compose)
4468 {
4469         if (compose->focused_editable &&
4470             GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
4471                 gtk_editable_paste_clipboard
4472                         (GTK_EDITABLE(compose->focused_editable));
4473 }
4474
4475 static void compose_allsel_cb(Compose *compose)
4476 {
4477         if (compose->focused_editable &&
4478             GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
4479                 gtk_editable_select_region
4480                         (GTK_EDITABLE(compose->focused_editable), 0, -1);
4481 }
4482
4483 static void compose_grab_focus_cb(GtkWidget *widget, Compose *compose)
4484 {
4485         if (GTK_IS_EDITABLE(widget))
4486                 compose->focused_editable = widget;
4487 }
4488
4489 static void compose_changed_cb(GtkEditable *editable, Compose *compose)
4490 {
4491         if (compose->modified == FALSE) {
4492                 compose->modified = TRUE;
4493                 compose_set_title(compose);
4494         }
4495 }
4496
4497 static void compose_button_press_cb(GtkWidget *widget, GdkEventButton *event,
4498                                     Compose *compose)
4499 {
4500         gtk_stext_set_point(GTK_STEXT(widget),
4501                            gtk_editable_get_position(GTK_EDITABLE(widget)));
4502 }
4503
4504 #if 0
4505 static void compose_key_press_cb(GtkWidget *widget, GdkEventKey *event,
4506                                  Compose *compose)
4507 {
4508         gtk_stext_set_point(GTK_STEXT(widget),
4509                            gtk_editable_get_position(GTK_EDITABLE(widget)));
4510 }
4511 #endif
4512
4513 static void compose_toggle_to_cb(gpointer data, guint action,
4514                                  GtkWidget *widget)
4515 {
4516         Compose *compose = (Compose *)data;
4517
4518         if (GTK_CHECK_MENU_ITEM(widget)->active) {
4519                 gtk_widget_show(compose->to_hbox);
4520                 gtk_widget_show(compose->to_entry);
4521                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 1, 4);
4522                 compose->use_to = TRUE;
4523         } else {
4524                 gtk_widget_hide(compose->to_hbox);
4525                 gtk_widget_hide(compose->to_entry);
4526                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 1, 0);
4527                 gtk_widget_queue_resize(compose->table_vbox);
4528                 compose->use_to = FALSE;
4529         }
4530
4531         if (addressbook_get_target_compose() == compose)
4532                 addressbook_set_target_compose(compose);
4533 }
4534
4535 static void compose_toggle_cc_cb(gpointer data, guint action,
4536                                  GtkWidget *widget)
4537 {
4538         Compose *compose = (Compose *)data;
4539
4540         if (GTK_CHECK_MENU_ITEM(widget)->active) {
4541                 gtk_widget_show(compose->cc_hbox);
4542                 gtk_widget_show(compose->cc_entry);
4543                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 3, 4);
4544                 compose->use_cc = TRUE;
4545         } else {
4546                 gtk_widget_hide(compose->cc_hbox);
4547                 gtk_widget_hide(compose->cc_entry);
4548                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 3, 0);
4549                 gtk_widget_queue_resize(compose->table_vbox);
4550                 compose->use_cc = FALSE;
4551         }
4552
4553         if (addressbook_get_target_compose() == compose)
4554                 addressbook_set_target_compose(compose);
4555 }
4556
4557 static void compose_toggle_bcc_cb(gpointer data, guint action,
4558                                   GtkWidget *widget)
4559 {
4560         Compose *compose = (Compose *)data;
4561
4562         if (GTK_CHECK_MENU_ITEM(widget)->active) {
4563                 gtk_widget_show(compose->bcc_hbox);
4564                 gtk_widget_show(compose->bcc_entry);
4565                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 4, 4);
4566                 compose->use_bcc = TRUE;
4567         } else {
4568                 gtk_widget_hide(compose->bcc_hbox);
4569                 gtk_widget_hide(compose->bcc_entry);
4570                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 4, 0);
4571                 gtk_widget_queue_resize(compose->table_vbox);
4572                 compose->use_bcc = FALSE;
4573         }
4574
4575         if (addressbook_get_target_compose() == compose)
4576                 addressbook_set_target_compose(compose);
4577 }
4578
4579 static void compose_toggle_replyto_cb(gpointer data, guint action,
4580                                       GtkWidget *widget)
4581 {
4582         Compose *compose = (Compose *)data;
4583
4584         if (GTK_CHECK_MENU_ITEM(widget)->active) {
4585                 gtk_widget_show(compose->reply_hbox);
4586                 gtk_widget_show(compose->reply_entry);
4587                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 5, 4);
4588                 compose->use_replyto = TRUE;
4589         } else {
4590                 gtk_widget_hide(compose->reply_hbox);
4591                 gtk_widget_hide(compose->reply_entry);
4592                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 5, 0);
4593                 gtk_widget_queue_resize(compose->table_vbox);
4594                 compose->use_replyto = FALSE;
4595         }
4596 }
4597
4598 static void compose_toggle_followupto_cb(gpointer data, guint action,
4599                                          GtkWidget *widget)
4600 {
4601         Compose *compose = (Compose *)data;
4602
4603         if (GTK_CHECK_MENU_ITEM(widget)->active) {
4604                 gtk_widget_show(compose->followup_hbox);
4605                 gtk_widget_show(compose->followup_entry);
4606                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 6, 4);
4607                 compose->use_followupto = TRUE;
4608         } else {
4609                 gtk_widget_hide(compose->followup_hbox);
4610                 gtk_widget_hide(compose->followup_entry);
4611                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 6, 0);
4612                 gtk_widget_queue_resize(compose->table_vbox);
4613                 compose->use_followupto = FALSE;
4614         }
4615 }
4616
4617 static void compose_toggle_attach_cb(gpointer data, guint action,
4618                                      GtkWidget *widget)
4619 {
4620         Compose *compose = (Compose *)data;
4621
4622         if (GTK_CHECK_MENU_ITEM(widget)->active) {
4623                 gtk_widget_ref(compose->edit_vbox);
4624
4625                 gtk_container_remove(GTK_CONTAINER(compose->vbox2),
4626                                      compose->edit_vbox);
4627                 gtk_paned_add2(GTK_PANED(compose->paned), compose->edit_vbox);
4628                 gtk_box_pack_start(GTK_BOX(compose->vbox2), compose->paned,
4629                                    TRUE, TRUE, 0);
4630                 gtk_widget_show(compose->paned);
4631
4632                 gtk_widget_unref(compose->edit_vbox);
4633                 gtk_widget_unref(compose->paned);
4634
4635                 compose->use_attach = TRUE;
4636         } else {
4637                 gtk_widget_ref(compose->paned);
4638                 gtk_widget_ref(compose->edit_vbox);
4639
4640                 gtk_container_remove(GTK_CONTAINER(compose->vbox2),
4641                                      compose->paned);
4642                 gtk_container_remove(GTK_CONTAINER(compose->paned),
4643                                      compose->edit_vbox);
4644                 gtk_box_pack_start(GTK_BOX(compose->vbox2),
4645                                    compose->edit_vbox, TRUE, TRUE, 0);
4646
4647                 gtk_widget_unref(compose->edit_vbox);
4648
4649                 compose->use_attach = FALSE;
4650         }
4651 }
4652
4653 #if USE_GPGME
4654 static void compose_toggle_sign_cb(gpointer data, guint action,
4655                                    GtkWidget *widget)
4656 {
4657         Compose *compose = (Compose *)data;
4658
4659         if (GTK_CHECK_MENU_ITEM(widget)->active)
4660                 compose->use_signing = TRUE;
4661         else
4662                 compose->use_signing = FALSE;
4663 }
4664
4665 static void compose_toggle_encrypt_cb(gpointer data, guint action,
4666                                       GtkWidget *widget)
4667 {
4668         Compose *compose = (Compose *)data;
4669
4670         if (GTK_CHECK_MENU_ITEM(widget)->active)
4671                 compose->use_encryption = TRUE;
4672         else
4673                 compose->use_encryption = FALSE;
4674 }
4675 #endif /* USE_GPGME */
4676
4677 static void compose_toggle_ruler_cb(gpointer data, guint action,
4678                                     GtkWidget *widget)
4679 {
4680         Compose *compose = (Compose *)data;
4681
4682         if (GTK_CHECK_MENU_ITEM(widget)->active) {
4683                 gtk_widget_show(compose->ruler_hbox);
4684                 prefs_common.show_ruler = TRUE;
4685         } else {
4686                 gtk_widget_hide(compose->ruler_hbox);
4687                 gtk_widget_queue_resize(compose->edit_vbox);
4688                 prefs_common.show_ruler = FALSE;
4689         }
4690 }
4691
4692 static void compose_attach_drag_received_cb (GtkWidget          *widget,
4693                                              GdkDragContext     *drag_context,
4694                                              gint                x,
4695                                              gint                y,
4696                                              GtkSelectionData   *data,
4697                                              guint               info,
4698                                              guint               time,
4699                                              gpointer            user_data)
4700 {
4701         Compose *compose = (Compose *)user_data;
4702         GList *list, *tmp;
4703
4704         list = uri_list_extract_filenames((const gchar *)data->data);
4705         for (tmp = list; tmp != NULL; tmp = tmp->next)
4706                 compose_attach_append(compose, (const gchar *)tmp->data,
4707                                       MIME_UNKNOWN);
4708         list_free_strings(list);
4709         g_list_free(list);
4710 }
4711
4712 static void compose_insert_drag_received_cb (GtkWidget          *widget,
4713                                              GdkDragContext     *drag_context,
4714                                              gint                x,
4715                                              gint                y,
4716                                              GtkSelectionData   *data,
4717                                              guint               info,
4718                                              guint               time,
4719                                              gpointer            user_data)
4720 {
4721         Compose *compose = (Compose *)user_data;
4722         GList *list, *tmp;
4723
4724         list = uri_list_extract_filenames((const gchar *)data->data);
4725         for (tmp = list; tmp != NULL; tmp = tmp->next)
4726                 compose_insert_file(compose, (const gchar *)tmp->data);
4727         list_free_strings(list);
4728         g_list_free(list);
4729 }
4730
4731 static void to_activated(GtkWidget *widget, Compose *compose)
4732 {
4733         if (GTK_WIDGET_VISIBLE(compose->newsgroups_entry))
4734                 gtk_widget_grab_focus(compose->newsgroups_entry);
4735         else
4736                 gtk_widget_grab_focus(compose->subject_entry);
4737 }
4738
4739 static void newsgroups_activated(GtkWidget *widget, Compose *compose)
4740 {
4741         gtk_widget_grab_focus(compose->subject_entry);
4742 }
4743
4744 static void subject_activated(GtkWidget *widget, Compose *compose)
4745 {
4746         if (GTK_WIDGET_VISIBLE(compose->cc_entry))
4747                 gtk_widget_grab_focus(compose->cc_entry);
4748         else if (GTK_WIDGET_VISIBLE(compose->bcc_entry))
4749                 gtk_widget_grab_focus(compose->bcc_entry);
4750         else if (GTK_WIDGET_VISIBLE(compose->reply_entry))
4751                 gtk_widget_grab_focus(compose->reply_entry);
4752         else if (GTK_WIDGET_VISIBLE(compose->followup_entry))
4753                 gtk_widget_grab_focus(compose->followup_entry);
4754         else
4755                 gtk_widget_grab_focus(compose->text);
4756 }
4757
4758 static void cc_activated(GtkWidget *widget, Compose *compose)
4759 {
4760         if (GTK_WIDGET_VISIBLE(compose->bcc_entry))
4761                 gtk_widget_grab_focus(compose->bcc_entry);
4762         else if (GTK_WIDGET_VISIBLE(compose->reply_entry))
4763                 gtk_widget_grab_focus(compose->reply_entry);
4764         else if (GTK_WIDGET_VISIBLE(compose->followup_entry))
4765                 gtk_widget_grab_focus(compose->followup_entry);
4766         else
4767                 gtk_widget_grab_focus(compose->text);
4768 }
4769
4770 static void bcc_activated(GtkWidget *widget, Compose *compose)
4771 {
4772         if (GTK_WIDGET_VISIBLE(compose->reply_entry))
4773                 gtk_widget_grab_focus(compose->reply_entry);
4774         else if (GTK_WIDGET_VISIBLE(compose->followup_entry))
4775                 gtk_widget_grab_focus(compose->followup_entry);
4776         else
4777                 gtk_widget_grab_focus(compose->text);
4778 }
4779
4780 static void replyto_activated(GtkWidget *widget, Compose *compose)
4781 {
4782         if (GTK_WIDGET_VISIBLE(compose->followup_entry))
4783                 gtk_widget_grab_focus(compose->followup_entry);
4784         else
4785                 gtk_widget_grab_focus(compose->text);
4786 }
4787
4788 static void followupto_activated(GtkWidget *widget, Compose *compose)
4789 {
4790         gtk_widget_grab_focus(compose->text);
4791 }
4792
4793 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
4794                                              GtkWidget *widget)
4795 {
4796         Compose *compose = (Compose *)data;
4797
4798         if (GTK_CHECK_MENU_ITEM(widget)->active)
4799                 compose->return_receipt = TRUE;
4800         else
4801                 compose->return_receipt = FALSE;
4802 }
4803
4804 static gchar *compose_quote_fmt         (Compose        *compose,
4805                                          MsgInfo        *msginfo,
4806                                          const gchar    *fmt,
4807                                          const gchar    *qmark)
4808 {
4809         gchar * quote_str = NULL;
4810
4811         if (qmark != NULL) {
4812                 gchar * p;
4813
4814                 quote_fmt_init(msginfo, NULL);
4815                 quote_fmt_scan_string(qmark);
4816                 quote_fmtparse();
4817
4818                 p = quote_fmt_get_buffer();
4819                 if (p == NULL) {
4820                         alertpanel_error
4821                                 (_("Quote mark format error."));
4822                 }
4823                 else {
4824                         quote_str = alloca(strlen(p) + 1);
4825                         strcpy(quote_str, p);
4826                 }
4827         }
4828
4829         quote_fmt_init(msginfo, quote_str);
4830         quote_fmt_scan_string(fmt);
4831         quote_fmtparse();
4832
4833         if (quote_fmt_get_buffer() == NULL)
4834                 alertpanel_error
4835                         (_("Message reply/forward format error."));
4836
4837         return quote_fmt_get_buffer();
4838 }