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