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