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