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