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