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