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