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