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