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