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