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