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