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