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