re-arrange the common prefs compose tab
[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 (g_strncasecmp(buf, "From:", strlen("From:")) == 0) {
2842                         fputs(" (by way of ", fdest);
2843                         if (compose->account->name
2844                             && *compose->account->name) {
2845                                 compose_convert_header
2846                                         (buf, sizeof(buf),
2847                                          compose->account->name,
2848                                          strlen("From: "));
2849                                 fprintf(fdest, "%s <%s>",
2850                                         buf, compose->account->address);
2851                         } else
2852                                 fprintf(fdest, "%s",
2853                                         compose->account->address);
2854                         fputs(")", fdest);
2855                 }
2856
2857                 if (fputs("\n", fdest) == -1)
2858                         goto error;
2859         }
2860
2861         compose_bounce_write_headers(compose, fdest);
2862
2863         while ((len = fread(buf, sizeof(gchar), BUFFSIZE, fp)) > 0) {
2864                 if (fwrite(buf, sizeof(gchar), len, fdest) == -1)
2865                         goto error;
2866         }
2867
2868         fclose(fdest);
2869         fclose(fp);
2870
2871         return 0;
2872  error:
2873         fclose(fdest);
2874         fclose(fp);
2875
2876         return -1;
2877 }
2878
2879
2880 #ifdef USE_GPGME
2881 /*
2882  * interfaces to rfc2015 to keep out the prefs stuff there.
2883  * returns 0 on success and -1 on error. */
2884 static int compose_create_signers_list (Compose *compose, GSList **pkey_list)
2885 {
2886         const char *keyid = NULL;
2887         GSList *key_list;
2888
2889         switch (compose->account->sign_key) {
2890         case SIGN_KEY_DEFAULT:
2891                 *pkey_list = NULL;
2892                 return 0;               /* nothing to do */
2893
2894         case SIGN_KEY_BY_FROM:
2895                 keyid = compose->account->address;
2896                 break;
2897
2898         case SIGN_KEY_CUSTOM:
2899                 keyid = compose->account->sign_key_id;
2900                 break;
2901
2902         default:
2903                 g_assert_not_reached ();
2904         }
2905
2906         key_list = rfc2015_create_signers_list(keyid);
2907
2908         if (!key_list) {
2909             alertpanel_error("Could not find any key associated with currently "
2910                                 "selected keyid `%s'!", keyid);
2911             return -1;
2912         }
2913         
2914         *pkey_list = key_list;
2915         return 0;
2916 }
2917 #endif /* USE_GPGME */
2918
2919 static gint compose_write_to_file(Compose *compose, const gchar *file,
2920                                   gboolean is_draft)
2921 {
2922         FILE *fp;
2923         size_t len;
2924         gchar *chars;
2925         gchar *buf;
2926         const gchar *out_codeset;
2927         EncodingType encoding;
2928
2929         if ((fp = fopen(file, "wb")) == NULL) {
2930                 FILE_OP_ERROR(file, "fopen");
2931                 return -1;
2932         }
2933
2934         /* chmod for security */
2935         if (change_file_mode_rw(fp, file) < 0) {
2936                 FILE_OP_ERROR(file, "chmod");
2937                 g_warning(_("can't change file mode\n"));
2938         }
2939
2940         /* get all composed text */
2941         chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
2942         len = strlen(chars);
2943         if (is_ascii_str(chars)) {
2944                 buf = g_strdup(chars);
2945                 out_codeset = CS_US_ASCII;
2946                 encoding = ENC_7BIT;
2947         } else {
2948                 const gchar *src_codeset;
2949
2950                 out_codeset = conv_get_outgoing_charset_str();
2951                 if (!strcasecmp(out_codeset, CS_US_ASCII))
2952                         out_codeset = CS_ISO_8859_1;
2953                 encoding = procmime_get_encoding_for_charset(out_codeset);
2954
2955                 src_codeset = conv_get_current_charset_str();
2956                 /* if current encoding is US-ASCII, set it the same as
2957                    outgoing one to prevent code conversion failure */
2958                 if (!strcasecmp(src_codeset, CS_US_ASCII))
2959                         src_codeset = out_codeset;
2960
2961                 debug_print("src encoding = %s, out encoding = %s, transfer encoding = %s\n",
2962                             src_codeset, out_codeset, procmime_get_encoding_str(encoding));
2963
2964                 buf = conv_codeset_strdup(chars, src_codeset, out_codeset);
2965                 if (!buf) {
2966                         AlertValue aval;
2967
2968                         aval = alertpanel
2969                                 (_("Error"),
2970                                  _("Can't convert the character encoding of the message.\n"
2971                                    "Send it anyway?"), _("Yes"), _("+No"), NULL);
2972                         if (aval != G_ALERTDEFAULT) {
2973                                 g_free(chars);
2974                                 fclose(fp);
2975                                 unlink(file);
2976                                 return -1;
2977                         } else {
2978                                 buf = g_strdup(chars);
2979                         }
2980                 }
2981         }
2982         g_free(chars);
2983
2984         /* write headers */
2985         if (compose_write_headers
2986                 (compose, fp, out_codeset, encoding, is_draft) < 0) {
2987                 g_warning(_("can't write headers\n"));
2988                 fclose(fp);
2989                 /* unlink(file); */
2990                 g_free(buf);
2991                 return -1;
2992         }
2993
2994         if (compose_use_attach(compose)) {
2995 #if USE_GPGME
2996             /* This prolog message is ignored by mime software and
2997              * because it would make our signing/encryption task
2998              * tougher, we don't emit it in that case */
2999             if (!compose->use_signing && !compose->use_encryption)
3000 #endif
3001                 fputs("This is a multi-part message in MIME format.\n", fp);
3002
3003                 fprintf(fp, "\n--%s\n", compose->boundary);
3004                 fprintf(fp, "Content-Type: text/plain; charset=%s\n",
3005                         out_codeset);
3006                 fprintf(fp, "Content-Transfer-Encoding: %s\n",
3007                         procmime_get_encoding_str(encoding));
3008                 fputc('\n', fp);
3009         }
3010
3011         /* write body */
3012         len = strlen(buf);
3013         if (encoding == ENC_BASE64) {
3014                 gchar outbuf[B64_BUFFSIZE];
3015                 gint i, l;
3016
3017                 for (i = 0; i < len; i += B64_LINE_SIZE) {
3018                         l = MIN(B64_LINE_SIZE, len - i);
3019                         to64frombits(outbuf, buf + i, l);
3020                         fputs(outbuf, fp);
3021                         fputc('\n', fp);
3022                 }
3023         } else if (fwrite(buf, sizeof(gchar), len, fp) != len) {
3024                 FILE_OP_ERROR(file, "fwrite");
3025                 fclose(fp);
3026                 unlink(file);
3027                 g_free(buf);
3028                 return -1;
3029         }
3030         g_free(buf);
3031
3032         if (compose_use_attach(compose))
3033                 compose_write_attach(compose, fp);
3034
3035         if (fclose(fp) == EOF) {
3036                 FILE_OP_ERROR(file, "fclose");
3037                 unlink(file);
3038                 return -1;
3039         }
3040
3041 #if USE_GPGME
3042         if (compose->use_signing) {
3043                 GSList *key_list;
3044                 
3045                 if (compose_create_signers_list(compose, &key_list) == -1 ||
3046                         rfc2015_sign(file, key_list) < 0) {
3047                     
3048                         unlink(file);
3049                         return -1;
3050                 }
3051         }
3052         if (compose->use_encryption) {
3053                 if (rfc2015_encrypt(file, compose->to_list, compose->account->ascii_armored) < 0) {
3054                         unlink(file);
3055                         return -1;
3056                 }
3057         }
3058 #endif /* USE_GPGME */
3059
3060         return 0;
3061 }
3062
3063 static gint compose_write_body_to_file(Compose *compose, const gchar *file)
3064 {
3065         FILE *fp;
3066         size_t len;
3067         gchar *chars;
3068
3069         if ((fp = fopen(file, "wb")) == NULL) {
3070                 FILE_OP_ERROR(file, "fopen");
3071                 return -1;
3072         }
3073
3074         /* chmod for security */
3075         if (change_file_mode_rw(fp, file) < 0) {
3076                 FILE_OP_ERROR(file, "chmod");
3077                 g_warning(_("can't change file mode\n"));
3078         }
3079
3080         chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
3081
3082         /* write body */
3083         len = strlen(chars);
3084         if (fwrite(chars, sizeof(gchar), len, fp) != len) {
3085                 FILE_OP_ERROR(file, "fwrite");
3086                 g_free(chars);
3087                 fclose(fp);
3088                 unlink(file);
3089                 return -1;
3090         }
3091
3092         g_free(chars);
3093
3094         if (fclose(fp) == EOF) {
3095                 FILE_OP_ERROR(file, "fclose");
3096                 unlink(file);
3097                 return -1;
3098         }
3099         return 0;
3100 }
3101
3102 static gint compose_remove_reedit_target(Compose *compose)
3103 {
3104         FolderItem *item;
3105         MsgInfo *msginfo = compose->targetinfo;
3106
3107         g_return_val_if_fail(compose->mode == COMPOSE_REEDIT, -1);
3108         if (!msginfo) return -1;
3109
3110         item = msginfo->folder;
3111         g_return_val_if_fail(item != NULL, -1);
3112
3113         folder_item_scan(item);
3114         if (procmsg_msg_exist(msginfo) &&
3115             (item->stype == F_DRAFT || item->stype == F_QUEUE)) {
3116                 if (folder_item_remove_msg(item, msginfo->msgnum) < 0) {
3117                         g_warning(_("can't remove the old message\n"));
3118                         return -1;
3119                 }
3120         }
3121
3122         return 0;
3123 }
3124
3125
3126 static gint compose_queue(Compose *compose, gint *msgnum, FolderItem **item)
3127 {
3128         FolderItem *queue;
3129         gchar *tmp, *tmp2, *queue_path;
3130         FILE *fp, *src_fp;
3131         GSList *cur;
3132         gchar buf[BUFFSIZE];
3133         gint num;
3134         static gboolean lock = FALSE;
3135         PrefsAccount *mailac = NULL, *newsac = NULL;
3136         
3137         debug_print(_("queueing message...\n"));
3138         g_return_val_if_fail(compose->account != NULL, -1);
3139         g_return_val_if_fail(compose->orig_account != NULL, -1);
3140
3141         lock = TRUE;
3142         
3143         if(!compose_check_for_valid_recipient(compose)) {
3144                 alertpanel_error(_("Recipient is not specified."));
3145                 lock = FALSE;
3146                 return -1;
3147         }
3148                                                                         
3149         if (!compose->to_list && !compose->newsgroup_list) {
3150                 g_warning(_("can't get recipient list."));
3151                 lock = FALSE;
3152                 return -1;
3153         }
3154
3155         if(compose->to_list) {
3156                 if (compose->account->protocol != A_NNTP)
3157                         mailac = compose->account;
3158                 else if (compose->orig_account->protocol != A_NNTP)
3159                         mailac = compose->orig_account;
3160                 else if (cur_account && cur_account->protocol != A_NNTP)
3161                         mailac = cur_account;
3162                 else if (!(mailac = compose_current_mail_account())) {
3163                         lock = FALSE;
3164                         alertpanel_error(_("No account for sending mails available!"));
3165                         return -1;
3166                 }
3167         }
3168
3169         if(compose->newsgroup_list) {
3170                 if (compose->account->protocol == A_NNTP)
3171                         newsac = compose->account;
3172                 else if(!(newsac = compose->orig_account) || (newsac->protocol != A_NNTP)) {
3173                         lock = FALSE;
3174                         alertpanel_error(_("No account for posting news available!"));
3175                         return -1;
3176                 }                       
3177         }
3178
3179         if (prefs_common.linewrap_at_send)
3180                 compose_wrap_line_all(compose);
3181                         
3182         /* write to temporary file */
3183         tmp2 = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
3184                                       G_DIR_SEPARATOR, (gint)compose);
3185
3186         if (compose->bounce_filename != NULL) {
3187                 if (compose_bounce_write_to_file(compose, tmp2) < 0) {
3188                         unlink(tmp2);
3189                         lock = FALSE;
3190                         return -1;
3191                 }
3192         }
3193         else {
3194                 if (compose_write_to_file(compose, tmp2, FALSE) < 0) {
3195                         unlink(tmp2);
3196                         lock = FALSE;
3197                         return -1;
3198                 }
3199         }
3200
3201         /* add queue header */
3202         tmp = g_strdup_printf("%s%cqueue.%d", g_get_tmp_dir(),
3203                               G_DIR_SEPARATOR, (gint)compose);
3204         if ((fp = fopen(tmp, "wb")) == NULL) {
3205                 FILE_OP_ERROR(tmp, "fopen");
3206                 g_free(tmp);
3207                 return -1;
3208         }
3209         if ((src_fp = fopen(tmp2, "rb")) == NULL) {
3210                 FILE_OP_ERROR(tmp2, "fopen");
3211                 fclose(fp);
3212                 unlink(tmp);
3213                 g_free(tmp);
3214                 unlink(tmp2);
3215                 g_free(tmp2);
3216                 return -1;
3217         }
3218         if (change_file_mode_rw(fp, tmp) < 0) {
3219                 FILE_OP_ERROR(tmp, "chmod");
3220                 g_warning(_("can't change file mode\n"));
3221         }
3222
3223         /* queueing variables */
3224         fprintf(fp, "AF:\n");
3225         fprintf(fp, "NF:0\n");
3226         fprintf(fp, "PS:10\n");
3227         fprintf(fp, "SRH:1\n");
3228         fprintf(fp, "SFN:\n");
3229         fprintf(fp, "DSR:\n");
3230         if (compose->msgid)
3231                 fprintf(fp, "MID:<%s>\n", compose->msgid);
3232         else
3233                 fprintf(fp, "MID:\n");
3234         fprintf(fp, "CFG:\n");
3235         fprintf(fp, "PT:0\n");
3236         fprintf(fp, "S:%s\n", compose->account->address);
3237         fprintf(fp, "RQ:\n");
3238         if (mailac)
3239                 fprintf(fp, "SSV:%s\n", mailac->smtp_server);
3240         else
3241                 fprintf(fp, "SSV:\n");
3242         if (newsac)
3243                 fprintf(fp, "NSV:%s\n", newsac->nntp_server);
3244         else
3245                 fprintf(fp, "NSV:\n");
3246         fprintf(fp, "SSH:\n");
3247         /* write recepient list */
3248         fprintf(fp, "R:");
3249         if(compose->to_list) {
3250                 fprintf(fp, "<%s>", (gchar *)compose->to_list->data);
3251                 for (cur = compose->to_list->next; cur != NULL; cur = cur->next)
3252                         fprintf(fp, ",<%s>", (gchar *)cur->data);
3253         }
3254         fprintf(fp, "\n");
3255         /* write newsgroup list */
3256         fprintf(fp, "NG:");
3257         if(compose->newsgroup_list) {
3258                 fprintf(fp, "%s", (gchar *)compose->newsgroup_list->data);
3259                 for (cur = compose->newsgroup_list->next; cur != NULL; cur = cur->next)
3260                         fprintf(fp, ",%s", (gchar *)cur->data);
3261         }
3262         fprintf(fp, "\n");
3263         /* Sylpheed account IDs */
3264         if(mailac) {
3265                 fprintf(fp, "MAID:%d\n", mailac->account_id);
3266         }
3267         if(newsac) {
3268                 fprintf(fp, "NAID:%d\n", newsac->account_id);
3269         }
3270         /* Save copy folder */
3271         if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn))) {
3272                 gchar *str;
3273                 
3274                 str = gtk_editable_get_chars(GTK_EDITABLE(compose->savemsg_entry), 0, -1);
3275                 fprintf(fp, "SCF:%s\n", str);
3276                 g_free(str);
3277         }
3278         fprintf(fp, "\n");
3279
3280         while (fgets(buf, sizeof(buf), src_fp) != NULL) {
3281                 if (fputs(buf, fp) == EOF) {
3282                         FILE_OP_ERROR(tmp, "fputs");
3283                         fclose(fp);
3284                         fclose(src_fp);
3285                         unlink(tmp);
3286                         g_free(tmp);
3287                         unlink(tmp2);
3288                         g_free(tmp2);
3289                         return -1;
3290                 }
3291         }
3292         fclose(src_fp);
3293         if (fclose(fp) == EOF) {
3294                 FILE_OP_ERROR(tmp, "fclose");
3295                 unlink(tmp);
3296                 g_free(tmp);
3297                 unlink(tmp2);
3298                 g_free(tmp2);
3299                 return -1;
3300         }
3301
3302         if (compose->account->folder &&
3303             FOLDER(compose->account->folder)->queue)
3304                 queue = FOLDER(compose->account->folder)->queue;
3305         else
3306                 queue = folder_get_default_queue();
3307
3308         folder_item_scan(queue);
3309         queue_path = folder_item_get_path(queue);
3310         if (!is_dir_exist(queue_path))
3311                 make_dir_hier(queue_path);
3312         if ((num = folder_item_add_msg(queue, tmp, TRUE)) < 0) {
3313                 g_warning(_("can't queue the message\n"));
3314                 unlink(tmp);
3315                 g_free(tmp);
3316                 g_free(queue_path);
3317                 return -1;
3318         }
3319         unlink(tmp);
3320         g_free(tmp);
3321         unlink(tmp2);
3322         g_free(tmp2);
3323
3324         if (compose->mode == COMPOSE_REEDIT) {
3325                 compose_remove_reedit_target(compose);
3326                 if (compose->targetinfo &&
3327                     compose->targetinfo->folder != queue)
3328                         folderview_update_item
3329                                 (compose->targetinfo->folder, TRUE);
3330         }
3331
3332         if ((fp = procmsg_open_mark_file(queue_path, TRUE)) == NULL)
3333                 g_warning(_("can't open mark file\n"));
3334         else {
3335                 MsgInfo newmsginfo;
3336
3337                 newmsginfo.msgnum = num;
3338                 newmsginfo.flags.perm_flags = 0;
3339                 newmsginfo.flags.tmp_flags = 0;
3340                 procmsg_write_flags(&newmsginfo, fp);
3341                 fclose(fp);
3342         }
3343         g_free(queue_path);
3344
3345         folder_item_scan(queue);
3346         folderview_update_item(queue, TRUE);
3347
3348         if((msgnum != NULL) && (item != NULL)) {
3349                 *msgnum = num;
3350                 *item = queue;
3351         }
3352
3353         return 0;
3354 }
3355
3356 static void compose_write_attach(Compose *compose, FILE *fp)
3357 {
3358         AttachInfo *ainfo;
3359         GtkCList *clist = GTK_CLIST(compose->attach_clist);
3360         gint row;
3361         FILE *attach_fp;
3362         gchar filename[BUFFSIZE];
3363         gint len;
3364
3365         for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
3366              row++) {
3367                 gchar buf[BUFFSIZE];
3368                 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
3369
3370                 if ((attach_fp = fopen(ainfo->file, "rb")) == NULL) {
3371                         g_warning(_("Can't open file %s\n"), ainfo->file);
3372                         continue;
3373                 }
3374
3375                 fprintf(fp, "\n--%s\n", compose->boundary);
3376
3377                 if (!strcmp2(ainfo->content_type, "message/rfc822")) {
3378                         fprintf(fp, "Content-Type: %s\n", ainfo->content_type);
3379                         fprintf(fp, "Content-Disposition: inline\n");
3380                 } else {
3381                         conv_encode_header(filename, sizeof(filename),
3382                                            ainfo->name, 12);
3383                         fprintf(fp, "Content-Type: %s;\n"
3384                                     " name=\"%s\"\n",
3385                                 ainfo->content_type, filename);
3386                         fprintf(fp, "Content-Disposition: attachment;\n"
3387                                     " filename=\"%s\"\n", filename);
3388                 }
3389
3390                 fprintf(fp, "Content-Transfer-Encoding: %s\n\n",
3391                         procmime_get_encoding_str(ainfo->encoding));
3392
3393                 switch (ainfo->encoding) {
3394
3395                 case ENC_7BIT:
3396                 case ENC_8BIT:
3397                         /* if (ainfo->encoding == ENC_7BIT) { */
3398
3399                         while (fgets(buf, sizeof(buf), attach_fp) != NULL) {
3400                                 strcrchomp(buf);
3401                                 fputs(buf, fp);
3402                         }
3403                         break;
3404                         /* } else { */
3405                 case ENC_BASE64:
3406
3407                         while ((len = fread(inbuf, sizeof(gchar),
3408                                             B64_LINE_SIZE, attach_fp))
3409                                == B64_LINE_SIZE) {
3410                                 to64frombits(outbuf, inbuf, B64_LINE_SIZE);
3411                                 fputs(outbuf, fp);
3412                                 fputc('\n', fp);
3413                         }
3414                         if (len > 0 && feof(attach_fp)) {
3415                                 to64frombits(outbuf, inbuf, len);
3416                                 fputs(outbuf, fp);
3417                                 fputc('\n', fp);
3418                         }
3419                         break;
3420                 }
3421
3422                 fclose(attach_fp);
3423         }
3424
3425         fprintf(fp, "\n--%s--\n", compose->boundary);
3426 }
3427
3428 #define IS_IN_CUSTOM_HEADER(header) \
3429         (compose->account->add_customhdr && \
3430          custom_header_find(compose->account->customhdr_list, header) != NULL)
3431
3432 static gint compose_write_headers_from_headerlist(Compose *compose, 
3433                                                   FILE *fp, 
3434                                                   gchar *header)
3435 {
3436         gchar buf[BUFFSIZE];
3437         gchar *str, *header_w_colon, *trans_hdr;
3438         gboolean first_address;
3439         GSList *list;
3440         ComposeHeaderEntry *headerentry;
3441         gchar * headerentryname;
3442
3443         if (IS_IN_CUSTOM_HEADER(header)) {
3444                 return 0;
3445         }
3446
3447         debug_print(_("Writing %s-header\n"), header);
3448
3449         header_w_colon = g_strconcat(header, ":", NULL);
3450         trans_hdr = (prefs_common.trans_hdr ? gettext(header_w_colon) : header_w_colon);
3451
3452         first_address = TRUE;
3453         for(list = compose->header_list; list; list = list->next) {
3454                 headerentry = ((ComposeHeaderEntry *)list->data);
3455                 headerentryname = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(headerentry->combo)->entry));
3456
3457                 if(!g_strcasecmp(trans_hdr, headerentryname)) {
3458                         str = gtk_entry_get_text(GTK_ENTRY(headerentry->entry));
3459                         Xstrdup_a(str, str, return -1);
3460                         g_strstrip(str);
3461                         if(str[0] != '\0') {
3462                                 compose_convert_header
3463                                         (buf, sizeof(buf), str,
3464                                         strlen(header) + 2);
3465                                 if(first_address) {
3466                                         fprintf(fp, "%s: ", header);
3467                                         first_address = FALSE;
3468                                 } else {
3469                                         fprintf(fp, ",");
3470                                 }
3471                                 fprintf(fp, "%s", buf);
3472                         }
3473                 }
3474         }
3475         if(!first_address) {
3476                 fprintf(fp, "\n");
3477         }
3478
3479         g_free(header_w_colon);
3480
3481         return(0);
3482 }
3483
3484 static gint compose_write_headers(Compose *compose, FILE *fp,
3485                                   const gchar *charset, EncodingType encoding,
3486                                   gboolean is_draft)
3487 {
3488         gchar buf[BUFFSIZE];
3489         gchar *str;
3490         /* struct utsname utsbuf; */
3491
3492         g_return_val_if_fail(fp != NULL, -1);
3493         g_return_val_if_fail(charset != NULL, -1);
3494         g_return_val_if_fail(compose->account != NULL, -1);
3495         g_return_val_if_fail(compose->account->address != NULL, -1);
3496
3497         /* Date */
3498         if (compose->account->add_date) {
3499                 get_rfc822_date(buf, sizeof(buf));
3500                 fprintf(fp, "Date: %s\n", buf);
3501         }
3502
3503         /* From */
3504         if (!IS_IN_CUSTOM_HEADER("From")) {
3505                 if (compose->account->name && *compose->account->name) {
3506                         compose_convert_header
3507                                 (buf, sizeof(buf), compose->account->name,
3508                                  strlen("From: "));
3509                         fprintf(fp, "From: %s <%s>\n",
3510                                 buf, compose->account->address);
3511                 } else
3512                         fprintf(fp, "From: %s\n", compose->account->address);
3513         }
3514         
3515         /* To */
3516         compose_write_headers_from_headerlist(compose, fp, "To");
3517 #if 0 /* NEW COMPOSE GUI */
3518         if (compose->use_to) {
3519                 str = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
3520                 if (*str != '\0') {
3521                         Xstrdup_a(str, str, return -1);
3522                         g_strstrip(str);
3523                         if (*str != '\0') {
3524                                 compose->to_list = address_list_append
3525                                         (compose->to_list, str);
3526                                 if (!IS_IN_CUSTOM_HEADER("To")) {
3527                                         compose_convert_header
3528                                                 (buf, sizeof(buf), str,
3529                                                  strlen("To: "));
3530                                         fprintf(fp, "To: %s\n", buf);
3531                                 }
3532                         }
3533                 }
3534         }
3535 #endif
3536
3537         /* Newsgroups */
3538         compose_write_headers_from_headerlist(compose, fp, "Newsgroups");
3539 #if 0 /* NEW COMPOSE GUI */
3540         str = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
3541         if (*str != '\0') {
3542                 Xstrdup_a(str, str, return -1);
3543                 g_strstrip(str);
3544                 remove_space(str);
3545                 if (*str != '\0') {
3546                         compose->newsgroup_list =
3547                                 newsgroup_list_append(compose->newsgroup_list,
3548                                                       str);
3549                         if (!IS_IN_CUSTOM_HEADER("Newsgroups")) {
3550                                 compose_convert_header(buf, sizeof(buf), str,
3551                                                        strlen("Newsgroups: "));
3552                                 fprintf(fp, "Newsgroups: %s\n", buf);
3553                         }
3554                 }
3555         }
3556 #endif
3557         /* Cc */
3558         compose_write_headers_from_headerlist(compose, fp, "Cc");
3559 #if 0 /* NEW COMPOSE GUI */
3560         if (compose->use_cc) {
3561                 str = gtk_entry_get_text(GTK_ENTRY(compose->cc_entry));
3562                 if (*str != '\0') {
3563                         Xstrdup_a(str, str, return -1);
3564                         g_strstrip(str);
3565                         if (*str != '\0') {
3566                                 compose->to_list = address_list_append
3567                                         (compose->to_list, str);
3568                                 if (!IS_IN_CUSTOM_HEADER("Cc")) {
3569                                         compose_convert_header
3570                                                 (buf, sizeof(buf), str,
3571                                                  strlen("Cc: "));
3572                                         fprintf(fp, "Cc: %s\n", buf);
3573                                 }
3574                         }
3575                 }
3576         }
3577 #endif
3578         /* Bcc */
3579         compose_write_headers_from_headerlist(compose, fp, "Bcc");
3580 #if 0 /* NEW COMPOSE GUI */
3581         if (compose->use_bcc) {
3582                 str = gtk_entry_get_text(GTK_ENTRY(compose->bcc_entry));
3583                 if (*str != '\0') {
3584                         Xstrdup_a(str, str, return -1);
3585                         g_strstrip(str);
3586                         if (*str != '\0') {
3587                                 compose->to_list = address_list_append
3588                                         (compose->to_list, str);
3589                                 compose_convert_header(buf, sizeof(buf), str,
3590                                                        strlen("Bcc: "));
3591                                 fprintf(fp, "Bcc: %s\n", buf);
3592                         }
3593                 }
3594         }
3595 #endif
3596
3597         /* Subject */
3598         str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
3599         if (*str != '\0' && !IS_IN_CUSTOM_HEADER("Subject")) {
3600                 Xstrdup_a(str, str, return -1);
3601                 g_strstrip(str);
3602                 if (*str != '\0') {
3603                         compose_convert_header(buf, sizeof(buf), str,
3604                                                strlen("Subject: "));
3605                         fprintf(fp, "Subject: %s\n", buf);
3606                 }
3607         }
3608
3609         /* Message-ID */
3610         if (compose->account->gen_msgid) {
3611                 compose_generate_msgid(compose, buf, sizeof(buf));
3612                 fprintf(fp, "Message-Id: <%s>\n", buf);
3613                 compose->msgid = g_strdup(buf);
3614         }
3615
3616         /* In-Reply-To */
3617         if (compose->inreplyto && compose->to_list)
3618                 fprintf(fp, "In-Reply-To: <%s>\n", compose->inreplyto);
3619
3620         /* References */
3621         if (compose->references)
3622                 fprintf(fp, "References: %s\n", compose->references);
3623
3624         /* Followup-To */
3625         compose_write_headers_from_headerlist(compose, fp, "Followup-To");
3626 #if 0 /* NEW COMPOSE GUI */
3627         if (compose->use_followupto && !IS_IN_CUSTOM_HEADER("Followup-To")) {
3628                 str = gtk_entry_get_text(GTK_ENTRY(compose->followup_entry));
3629                 if (*str != '\0') {
3630                         Xstrdup_a(str, str, return -1);
3631                         g_strstrip(str);
3632                         remove_space(str);
3633                         if (*str != '\0') {
3634                                 compose_convert_header(buf, sizeof(buf), str,
3635                                                        strlen("Followup-To: "));
3636                                 fprintf(fp, "Followup-To: %s\n", buf);
3637                         }
3638                 }
3639         }
3640 #endif
3641         /* Reply-To */
3642         compose_write_headers_from_headerlist(compose, fp, "Reply-To");
3643 #if 0 /* NEW COMPOSE GUI */
3644         if (compose->use_replyto && !IS_IN_CUSTOM_HEADER("Reply-To")) {
3645                 str = gtk_entry_get_text(GTK_ENTRY(compose->reply_entry));
3646                 if (*str != '\0') {
3647                         Xstrdup_a(str, str, return -1);
3648                         g_strstrip(str);
3649                         if (*str != '\0') {
3650                                 compose_convert_header(buf, sizeof(buf), str,
3651                                                        strlen("Reply-To: "));
3652                                 fprintf(fp, "Reply-To: %s\n", buf);
3653                         }
3654                 }
3655         }
3656 #endif
3657         /* Organization */
3658         if (compose->account->organization &&
3659             !IS_IN_CUSTOM_HEADER("Organization")) {
3660                 compose_convert_header(buf, sizeof(buf),
3661                                        compose->account->organization,
3662                                        strlen("Organization: "));
3663                 fprintf(fp, "Organization: %s\n", buf);
3664         }
3665
3666         /* Program version and system info */
3667         /* uname(&utsbuf); */
3668         if (g_slist_length(compose->to_list) && !IS_IN_CUSTOM_HEADER("X-Mailer")) {
3669                 fprintf(fp, "X-Mailer: %s (GTK+ %d.%d.%d; %s)\n",
3670                         prog_version,
3671                         gtk_major_version, gtk_minor_version, gtk_micro_version,
3672                         HOST_ALIAS);
3673                         /* utsbuf.sysname, utsbuf.release, utsbuf.machine); */
3674         }
3675         if (g_slist_length(compose->newsgroup_list) && !IS_IN_CUSTOM_HEADER("X-Newsreader")) {
3676                 fprintf(fp, "X-Newsreader: %s (GTK+ %d.%d.%d; %s)\n",
3677                         prog_version,
3678                         gtk_major_version, gtk_minor_version, gtk_micro_version,
3679                         HOST_ALIAS);
3680                         /* utsbuf.sysname, utsbuf.release, utsbuf.machine); */
3681         }
3682
3683         /* custom headers */
3684         if (compose->account->add_customhdr) {
3685                 GSList *cur;
3686
3687                 for (cur = compose->account->customhdr_list; cur != NULL;
3688                      cur = cur->next) {
3689                         CustomHeader *chdr = (CustomHeader *)cur->data;
3690
3691                         if (strcasecmp(chdr->name, "Date")         != 0 &&
3692                             strcasecmp(chdr->name, "From")         != 0 &&
3693                             strcasecmp(chdr->name, "To")           != 0 &&
3694                          /* strcasecmp(chdr->name, "Sender")       != 0 && */
3695                             strcasecmp(chdr->name, "Message-Id")   != 0 &&
3696                             strcasecmp(chdr->name, "In-Reply-To")  != 0 &&
3697                             strcasecmp(chdr->name, "References")   != 0 &&
3698                             strcasecmp(chdr->name, "Mime-Version") != 0 &&
3699                             strcasecmp(chdr->name, "Content-Type") != 0 &&
3700                             strcasecmp(chdr->name, "Content-Transfer-Encoding")
3701                             != 0) {
3702                                 compose_convert_header
3703                                         (buf, sizeof(buf),
3704                                          chdr->value ? chdr->value : "",
3705                                          strlen(chdr->name) + 2);
3706                                 fprintf(fp, "%s: %s\n", chdr->name, buf);
3707                         }
3708                 }
3709         }
3710
3711         /* MIME */
3712         fprintf(fp, "Mime-Version: 1.0\n");
3713         if (compose_use_attach(compose)) {
3714                 get_rfc822_date(buf, sizeof(buf));
3715                 subst_char(buf, ' ', '_');
3716                 subst_char(buf, ',', '_');
3717                 compose->boundary = g_strdup_printf("Multipart_%s_%08x",
3718                                                     buf, (guint)compose);
3719                 fprintf(fp,
3720                         "Content-Type: multipart/mixed;\n"
3721                         " boundary=\"%s\"\n", compose->boundary);
3722         } else {
3723                 fprintf(fp, "Content-Type: text/plain; charset=%s\n", charset);
3724                 fprintf(fp, "Content-Transfer-Encoding: %s\n",
3725                         procmime_get_encoding_str(encoding));
3726         }
3727
3728         /* Request Return Receipt */
3729         if (!IS_IN_CUSTOM_HEADER("Disposition-Notification-To")) {
3730                 if (compose->return_receipt) {
3731                         if (compose->account->name
3732                             && *compose->account->name) {
3733                                 compose_convert_header(buf, sizeof(buf), compose->account->name, strlen("Disposition-Notification-To: "));
3734                                 fprintf(fp, "Disposition-Notification-To: %s <%s>\n", buf, compose->account->address);
3735                         } else
3736                                 fprintf(fp, "Disposition-Notification-To: %s\n", compose->account->address);
3737                 }
3738         }
3739
3740         /* separator between header and body */
3741         fputs("\n", fp);
3742
3743         return 0;
3744 }
3745
3746 #undef IS_IN_CUSTOM_HEADER
3747
3748 static void compose_convert_header(gchar *dest, gint len, gchar *src,
3749                                    gint header_len)
3750 {
3751         g_return_if_fail(src != NULL);
3752         g_return_if_fail(dest != NULL);
3753
3754         if (len < 1) return;
3755
3756         remove_return(src);
3757
3758         if (is_ascii_str(src)) {
3759                 strncpy2(dest, src, len);
3760                 dest[len - 1] = '\0';
3761                 return;
3762         } else
3763                 conv_encode_header(dest, len, src, header_len);
3764 }
3765
3766 static void compose_generate_msgid(Compose *compose, gchar *buf, gint len)
3767 {
3768         struct tm *lt;
3769         time_t t;
3770         gchar *addr;
3771
3772         t = time(NULL);
3773         lt = localtime(&t);
3774
3775         if (compose->account && compose->account->address &&
3776             *compose->account->address) {
3777                 if (strchr(compose->account->address, '@'))
3778                         addr = g_strdup(compose->account->address);
3779                 else
3780                         addr = g_strconcat(compose->account->address, "@",
3781                                            get_domain_name(), NULL);
3782         } else
3783                 addr = g_strconcat(g_get_user_name(), "@", get_domain_name(),
3784                                    NULL);
3785
3786         g_snprintf(buf, len, "%04d%02d%02d%02d%02d%02d.%08x.%s",
3787                    lt->tm_year + 1900, lt->tm_mon + 1,
3788                    lt->tm_mday, lt->tm_hour,
3789                    lt->tm_min, lt->tm_sec,
3790                    (guint)random(), addr);
3791
3792         debug_print(_("generated Message-ID: %s\n"), buf);
3793
3794         g_free(addr);
3795 }
3796
3797 static void compose_add_entry_field(GtkWidget *table, GtkWidget **hbox,
3798                                     GtkWidget **entry, gint *count,
3799                                     const gchar *label_str,
3800                                     gboolean is_addr_entry)
3801 {
3802         GtkWidget *label;
3803
3804         if (GTK_TABLE(table)->nrows < (*count) + 1)
3805                 gtk_table_resize(GTK_TABLE(table), (*count) + 1, 2);
3806
3807         *hbox = gtk_hbox_new(FALSE, 0);
3808         label = gtk_label_new
3809                 (prefs_common.trans_hdr ? gettext(label_str) : label_str);
3810         gtk_box_pack_end(GTK_BOX(*hbox), label, FALSE, FALSE, 0);
3811         gtk_table_attach(GTK_TABLE(table), *hbox, 0, 1, *count, (*count) + 1,
3812                          GTK_FILL, 0, 2, 0);
3813         *entry = gtk_entry_new_with_max_length(MAX_ENTRY_LENGTH);
3814         gtk_table_attach
3815                 (GTK_TABLE(table), *entry, 1, 2, *count, (*count) + 1, GTK_FILL | GTK_EXPAND, GTK_SHRINK, 0, 0);
3816 #if 0 /* NEW COMPOSE GUI */
3817         if (GTK_TABLE(table)->nrows > (*count) + 1)
3818                 gtk_table_set_row_spacing(GTK_TABLE(table), *count, 4);
3819 #endif
3820
3821         if (is_addr_entry)
3822                 address_completion_register_entry(GTK_ENTRY(*entry));
3823
3824         (*count)++;
3825 }
3826
3827 static void compose_create_header_entry(Compose *compose) 
3828 {
3829         gchar *headers[] = {"To:", "Cc:", "Bcc:", "Newsgroups:", "Reply-To:", "Followup-To:", NULL};
3830
3831         GtkWidget *combo;
3832         GtkWidget *entry;
3833         GList *combo_list = NULL;
3834         gchar **string, *header;
3835         ComposeHeaderEntry *headerentry;
3836
3837         headerentry = g_new0(ComposeHeaderEntry, 1);
3838
3839         /* Combo box */
3840         combo = gtk_combo_new();
3841         string = headers; 
3842         while(*string != NULL) {
3843             combo_list = g_list_append(combo_list, (prefs_common.trans_hdr ? gettext(*string) : *string));
3844             string++;
3845         }
3846         gtk_combo_set_popdown_strings(GTK_COMBO(combo), combo_list);
3847         g_list_free(combo_list);
3848         gtk_editable_set_editable(GTK_EDITABLE(GTK_COMBO(combo)->entry), FALSE);
3849         gtk_widget_show(combo);
3850         gtk_table_attach(GTK_TABLE(compose->header_table), combo, 0, 1, compose->header_nextrow, compose->header_nextrow+1, GTK_SHRINK, GTK_FILL, 0, 0);
3851         if(compose->header_last) {      
3852                 header = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(compose->header_last->combo)->entry));
3853         } else {
3854                 switch(compose->account->protocol) {
3855                         case A_NNTP:
3856                                 header = prefs_common.trans_hdr ? _("Newsgroups:") : "Newsgroups:";
3857                                 break;
3858                         default:
3859                                 header = prefs_common.trans_hdr ? _("To:") : "To:";
3860                                 break;
3861                 }                                                                   
3862         }
3863         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo)->entry), header);
3864
3865         /* Entry field */
3866         entry = gtk_entry_new(); 
3867         gtk_widget_show(entry);
3868         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);
3869
3870         gtk_signal_connect(GTK_OBJECT(entry), "key-press-event", GTK_SIGNAL_FUNC(compose_headerentry_key_press_event_cb), headerentry);
3871         gtk_signal_connect(GTK_OBJECT(entry), "changed", GTK_SIGNAL_FUNC(compose_headerentry_changed_cb), headerentry);
3872         gtk_signal_connect(GTK_OBJECT(entry), "activate", GTK_SIGNAL_FUNC(text_activated), compose);
3873
3874         address_completion_register_entry(GTK_ENTRY(entry));
3875
3876         headerentry->compose = compose;
3877         headerentry->combo = combo;
3878         headerentry->entry = entry;
3879         headerentry->headernum = compose->header_nextrow;
3880
3881         compose->header_nextrow++;
3882         compose->header_last = headerentry;
3883 }
3884
3885 static void compose_add_header_entry(Compose *compose, gchar *header, gchar *text) 
3886 {
3887         ComposeHeaderEntry *last_header;
3888         
3889         last_header = compose->header_last;
3890         
3891         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(last_header->combo)->entry), header);
3892         gtk_entry_set_text(GTK_ENTRY(last_header->entry), text);
3893 }
3894
3895 static GtkWidget *compose_create_header(Compose *compose) 
3896 {
3897         GtkWidget *label;
3898         GtkWidget *hbox;
3899         GtkWidget *from_optmenu_hbox;
3900 #if 0 /* NEW COMPOSE GUI */
3901         GtkWidget *to_entry;
3902         GtkWidget *to_hbox;
3903         GtkWidget *newsgroups_entry;
3904         GtkWidget *newsgroups_hbox;
3905 #endif
3906         GtkWidget *header_scrolledwin;
3907         GtkWidget *header_table;
3908 #if 0 /* NEW COMPOSE GUI */
3909         GtkWidget *cc_entry;
3910         GtkWidget *cc_hbox;
3911         GtkWidget *bcc_entry;
3912         GtkWidget *bcc_hbox;
3913         GtkWidget *reply_entry;
3914         GtkWidget *reply_hbox;
3915         GtkWidget *followup_entry;
3916         GtkWidget *followup_hbox;
3917 #endif
3918
3919         gint count = 0;
3920
3921         /* header labels and entries */
3922         header_scrolledwin = gtk_scrolled_window_new(NULL, NULL);
3923         gtk_widget_show(header_scrolledwin);
3924         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(header_scrolledwin), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
3925
3926         header_table = gtk_table_new(2, 2, FALSE);
3927         gtk_widget_show(header_table);
3928         gtk_container_set_border_width(GTK_CONTAINER(header_table), 2);
3929         gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(header_scrolledwin), header_table);
3930         gtk_viewport_set_shadow_type(GTK_VIEWPORT(GTK_BIN(header_scrolledwin)->child), GTK_SHADOW_ETCHED_IN);
3931         count = 0;
3932
3933         /* option menu for selecting accounts */
3934         hbox = gtk_hbox_new(FALSE, 0);
3935         label = gtk_label_new(prefs_common.trans_hdr ? _("From:") : "From:");
3936         gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
3937         gtk_table_attach(GTK_TABLE(header_table), hbox, 0, 1, count, count + 1,
3938                          GTK_FILL, 0, 2, 0);
3939         from_optmenu_hbox = compose_account_option_menu_create(compose);
3940         gtk_table_attach(GTK_TABLE(header_table), from_optmenu_hbox,
3941                                   1, 2, count, count + 1, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
3942 #if 0 /* NEW COMPOSE GUI */
3943         gtk_table_set_row_spacing(GTK_TABLE(table), 0, 4);
3944 #endif
3945         count++;
3946
3947         compose->header_table = header_table;
3948         compose->header_list = NULL;
3949         compose->header_nextrow = count;
3950
3951         compose_create_header_entry(compose);
3952
3953 #if 0 /* NEW COMPOSE GUI */
3954         compose_add_entry_field(table, &to_hbox, &to_entry, &count,
3955                                 "To:", TRUE); 
3956         gtk_table_set_row_spacing(GTK_TABLE(table), 0, 4);
3957         compose_add_entry_field(table, &newsgroups_hbox, &newsgroups_entry,
3958                                 &count, "Newsgroups:", FALSE);
3959         gtk_table_set_row_spacing(GTK_TABLE(table), 1, 4);
3960
3961         gtk_table_set_row_spacing(GTK_TABLE(table), 2, 4);
3962
3963         compose_add_entry_field(table, &cc_hbox, &cc_entry, &count,
3964                                 "Cc:", TRUE);
3965         gtk_table_set_row_spacing(GTK_TABLE(table), 3, 4);
3966         compose_add_entry_field(table, &bcc_hbox, &bcc_entry, &count,
3967                                 "Bcc:", TRUE);
3968         gtk_table_set_row_spacing(GTK_TABLE(table), 4, 4);
3969         compose_add_entry_field(table, &reply_hbox, &reply_entry, &count,
3970                                 "Reply-To:", TRUE);
3971         gtk_table_set_row_spacing(GTK_TABLE(table), 5, 4);
3972         compose_add_entry_field(table, &followup_hbox, &followup_entry, &count,
3973                                 "Followup-To:", FALSE);
3974         gtk_table_set_row_spacing(GTK_TABLE(table), 6, 4);
3975
3976         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
3977
3978         gtk_signal_connect(GTK_OBJECT(to_entry), "activate",
3979                            GTK_SIGNAL_FUNC(to_activated), compose);
3980         gtk_signal_connect(GTK_OBJECT(newsgroups_entry), "activate",
3981                            GTK_SIGNAL_FUNC(newsgroups_activated), compose);
3982         gtk_signal_connect(GTK_OBJECT(subject_entry), "activate",
3983                            GTK_SIGNAL_FUNC(subject_activated), compose);
3984         gtk_signal_connect(GTK_OBJECT(cc_entry), "activate",
3985                            GTK_SIGNAL_FUNC(cc_activated), compose);
3986         gtk_signal_connect(GTK_OBJECT(bcc_entry), "activate",
3987                            GTK_SIGNAL_FUNC(bcc_activated), compose);
3988         gtk_signal_connect(GTK_OBJECT(reply_entry), "activate",
3989                            GTK_SIGNAL_FUNC(replyto_activated), compose);
3990         gtk_signal_connect(GTK_OBJECT(followup_entry), "activate",
3991                            GTK_SIGNAL_FUNC(followupto_activated), compose);
3992
3993         gtk_signal_connect(GTK_OBJECT(subject_entry), "grab_focus",
3994                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
3995         gtk_signal_connect(GTK_OBJECT(to_entry), "grab_focus",
3996                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
3997         gtk_signal_connect(GTK_OBJECT(newsgroups_entry), "grab_focus",
3998                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
3999         gtk_signal_connect(GTK_OBJECT(cc_entry), "grab_focus",
4000                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
4001         gtk_signal_connect(GTK_OBJECT(bcc_entry), "grab_focus",
4002                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
4003         gtk_signal_connect(GTK_OBJECT(reply_entry), "grab_focus",
4004                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
4005         gtk_signal_connect(GTK_OBJECT(followup_entry), "grab_focus",
4006                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
4007 #endif
4008
4009         compose->table            = NULL;
4010 #if 0 /* NEW COMPOSE GUI */
4011         compose->table            = table;
4012         compose->to_hbox          = to_hbox;
4013         compose->to_entry         = to_entry;
4014         compose->newsgroups_hbox  = newsgroups_hbox;
4015         compose->newsgroups_entry = newsgroups_entry;
4016 #endif
4017 #if 0 /* NEW COMPOSE GUI */
4018         compose->cc_hbox          = cc_hbox;
4019         compose->cc_entry         = cc_entry;
4020         compose->bcc_hbox         = bcc_hbox;
4021         compose->bcc_entry        = bcc_entry;
4022         compose->reply_hbox       = reply_hbox;
4023         compose->reply_entry      = reply_entry;
4024         compose->followup_hbox    = followup_hbox;
4025         compose->followup_entry   = followup_entry;
4026 #endif
4027
4028         return header_scrolledwin ;
4029 }
4030
4031 GtkWidget *compose_create_attach(Compose *compose)
4032 {
4033         gchar *titles[N_ATTACH_COLS];
4034         gint i;
4035
4036         GtkWidget *attach_scrwin;
4037         GtkWidget *attach_clist;
4038
4039         titles[COL_MIMETYPE] = _("MIME type");
4040         titles[COL_SIZE]     = _("Size");
4041         titles[COL_NAME]     = _("Name");
4042
4043         /* attachment list */
4044         attach_scrwin = gtk_scrolled_window_new(NULL, NULL);
4045         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(attach_scrwin),
4046                                        GTK_POLICY_AUTOMATIC,
4047                                        GTK_POLICY_ALWAYS);
4048         gtk_widget_set_usize(attach_scrwin, -1, 80);
4049
4050         attach_clist = gtk_clist_new_with_titles(N_ATTACH_COLS, titles);
4051         gtk_clist_set_column_justification(GTK_CLIST(attach_clist), COL_SIZE,
4052                                            GTK_JUSTIFY_RIGHT);
4053         gtk_clist_set_column_width(GTK_CLIST(attach_clist), COL_MIMETYPE, 240);
4054         gtk_clist_set_column_width(GTK_CLIST(attach_clist), COL_SIZE, 64);
4055         gtk_clist_set_selection_mode(GTK_CLIST(attach_clist),
4056                                      GTK_SELECTION_EXTENDED);
4057         for (i = 0; i < N_ATTACH_COLS; i++)
4058                 GTK_WIDGET_UNSET_FLAGS
4059                         (GTK_CLIST(attach_clist)->column[i].button,
4060                          GTK_CAN_FOCUS);
4061         gtk_container_add(GTK_CONTAINER(attach_scrwin), attach_clist);
4062
4063         gtk_signal_connect(GTK_OBJECT(attach_clist), "select_row",
4064                            GTK_SIGNAL_FUNC(attach_selected), compose);
4065         gtk_signal_connect(GTK_OBJECT(attach_clist), "button_press_event",
4066                            GTK_SIGNAL_FUNC(attach_button_pressed), compose);
4067         gtk_signal_connect(GTK_OBJECT(attach_clist), "key_press_event",
4068                            GTK_SIGNAL_FUNC(attach_key_pressed), compose);
4069
4070         /* drag and drop */
4071         gtk_drag_dest_set(attach_clist,
4072                           GTK_DEST_DEFAULT_ALL, compose_mime_types, 1,
4073                           GDK_ACTION_COPY);
4074         gtk_signal_connect(GTK_OBJECT(attach_clist), "drag_data_received",
4075                            GTK_SIGNAL_FUNC(compose_attach_drag_received_cb),
4076                            compose);
4077
4078         compose->attach_scrwin = attach_scrwin;
4079         compose->attach_clist  = attach_clist;
4080
4081         return attach_scrwin;
4082 }
4083
4084 static void compose_savemsg_checkbtn_cb(GtkWidget *widget, Compose *compose);
4085 static void compose_savemsg_select_cb(GtkWidget *widget, Compose *compose);
4086
4087 static GtkWidget *compose_create_others(Compose *compose)
4088 {
4089         GtkWidget *table;
4090         GtkWidget *savemsg_checkbtn;
4091         GtkWidget *savemsg_entry;
4092         GtkWidget *savemsg_select;
4093         
4094         guint rowcount = 0;
4095         gchar *folderidentifier;
4096
4097         /* Table for settings */
4098         table = gtk_table_new(3, 1, FALSE);
4099         gtk_widget_show(table);
4100         gtk_table_set_row_spacings(GTK_TABLE(table), VSPACING_NARROW);
4101         rowcount = 0;
4102
4103         /* Save Message to folder */
4104         savemsg_checkbtn = gtk_check_button_new_with_label(_("Save Message to "));
4105         gtk_widget_show(savemsg_checkbtn);
4106         gtk_table_attach(GTK_TABLE(table), savemsg_checkbtn, 0, 1, rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
4107         if(folder_get_default_outbox()) {
4108                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(savemsg_checkbtn), prefs_common.savemsg);
4109         }
4110         gtk_signal_connect(GTK_OBJECT(savemsg_checkbtn), "toggled",
4111                             GTK_SIGNAL_FUNC(compose_savemsg_checkbtn_cb), compose);
4112
4113         savemsg_entry = gtk_entry_new();
4114         gtk_widget_show(savemsg_entry);
4115         gtk_table_attach_defaults(GTK_TABLE(table), savemsg_entry, 1, 2, rowcount, rowcount + 1);
4116         gtk_editable_set_editable(GTK_EDITABLE(savemsg_entry), prefs_common.savemsg);
4117         if(folder_get_default_outbox()) {
4118                 folderidentifier = folder_item_get_identifier(folder_get_default_outbox());
4119                 gtk_entry_set_text(GTK_ENTRY(savemsg_entry), folderidentifier);
4120                 g_free(folderidentifier);
4121         }
4122
4123         savemsg_select = gtk_button_new_with_label (_("Select ..."));
4124         gtk_widget_show (savemsg_select);
4125         gtk_table_attach(GTK_TABLE(table), savemsg_select, 2, 3, rowcount, rowcount + 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 0, 0);
4126         gtk_signal_connect (GTK_OBJECT (savemsg_select), "clicked",
4127                             GTK_SIGNAL_FUNC (compose_savemsg_select_cb),
4128                             compose);
4129
4130         rowcount++;
4131
4132         compose->savemsg_checkbtn = savemsg_checkbtn;
4133         compose->savemsg_entry = savemsg_entry;
4134
4135         return table;   
4136 }
4137
4138 static void compose_savemsg_checkbtn_cb(GtkWidget *widget, Compose *compose) 
4139 {
4140         gtk_editable_set_editable(GTK_EDITABLE(compose->savemsg_entry),
4141                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn)));
4142 }
4143
4144 static void compose_savemsg_select_cb(GtkWidget *widget, Compose *compose)
4145 {
4146         FolderItem *dest;
4147         gchar * path;
4148
4149         dest = foldersel_folder_sel(NULL, FOLDER_SEL_COPY, NULL);
4150         if (!dest) return;
4151
4152         path = folder_item_get_identifier(dest);
4153
4154         gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), path);
4155         g_free(path);
4156 }
4157
4158 static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
4159 {
4160         Compose   *compose;
4161         GtkWidget *window;
4162         GtkWidget *vbox;
4163         GtkWidget *menubar;
4164         GtkWidget *handlebox;
4165
4166         GtkWidget *notebook;
4167
4168         GtkWidget *vbox2;
4169
4170         GtkWidget *label;
4171         GtkWidget *subject_hbox;
4172         GtkWidget *subject_frame;
4173         GtkWidget *subject_entry;
4174         GtkWidget *subject;
4175         GtkWidget *paned;
4176
4177         GtkWidget *edit_vbox;
4178         GtkWidget *ruler_hbox;
4179         GtkWidget *ruler;
4180         GtkWidget *scrolledwin;
4181         GtkWidget *text;
4182
4183         GtkWidget *table;
4184
4185         UndoMain *undostruct;
4186
4187         gchar *titles[N_ATTACH_COLS];
4188         guint n_menu_entries;
4189         GtkStyle  *style, *new_style;
4190         GdkColormap *cmap;
4191         GdkColor color[1];
4192         gboolean success[1];
4193         GdkFont   *font;
4194         GtkWidget *popupmenu;
4195         GtkWidget *menuitem;
4196         GtkItemFactory *popupfactory;
4197         GtkItemFactory *ifactory;
4198         GtkWidget *tmpl_menu;
4199         gint n_entries;
4200
4201 #if USE_PSPELL
4202         GtkPspell * gtkpspell = NULL;
4203 #endif
4204
4205         g_return_val_if_fail(account != NULL, NULL);
4206
4207         debug_print(_("Creating compose window...\n"));
4208         compose = g_new0(Compose, 1);
4209
4210         titles[COL_MIMETYPE] = _("MIME type");
4211         titles[COL_SIZE]     = _("Size");
4212         titles[COL_NAME]     = _("Name");
4213
4214         compose->account = account;
4215         compose->orig_account = account;
4216
4217         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
4218         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
4219         gtk_widget_set_usize(window, -1, prefs_common.compose_height);
4220         gtk_window_set_wmclass(GTK_WINDOW(window), "compose window", "Sylpheed");
4221         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
4222                            GTK_SIGNAL_FUNC(compose_delete_cb), compose);
4223         gtk_signal_connect(GTK_OBJECT(window), "destroy",
4224                            GTK_SIGNAL_FUNC(compose_destroy_cb), compose);
4225         MANAGE_WINDOW_SIGNALS_CONNECT(window);
4226         gtk_widget_realize(window);
4227
4228         gtkut_widget_set_composer_icon(window);
4229
4230         vbox = gtk_vbox_new(FALSE, 0);
4231         gtk_container_add(GTK_CONTAINER(window), vbox);
4232
4233         n_menu_entries = sizeof(compose_entries) / sizeof(compose_entries[0]);
4234         menubar = menubar_create(window, compose_entries,
4235                                  n_menu_entries, "<Compose>", compose);
4236         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
4237
4238         handlebox = gtk_handle_box_new();
4239         gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
4240
4241         compose_toolbar_create(compose, handlebox);
4242
4243         vbox2 = gtk_vbox_new(FALSE, 2);
4244         gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
4245         gtk_container_set_border_width(GTK_CONTAINER(vbox2), BORDER_WIDTH);
4246         
4247         /* Notebook */
4248         notebook = gtk_notebook_new();
4249         gtk_widget_set_usize(notebook, -1, 130);
4250         gtk_widget_show(notebook);
4251
4252         /* header labels and entries */
4253         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), compose_create_header(compose), gtk_label_new(_("Header")));
4254         /* attachment list */
4255         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), compose_create_attach(compose), gtk_label_new(_("Attachments")));
4256         /* Others Tab */
4257         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), compose_create_others(compose), gtk_label_new(_("Others")));
4258
4259         /* Subject */
4260         subject_hbox = gtk_hbox_new(FALSE, 0);
4261         gtk_widget_show(subject_hbox);
4262
4263         subject_frame = gtk_frame_new(NULL);
4264         gtk_frame_set_shadow_type(GTK_FRAME(subject_frame), GTK_SHADOW_OUT);
4265         gtk_box_pack_start(GTK_BOX(subject_hbox), subject_frame, TRUE, TRUE, BORDER_WIDTH+1);
4266         gtk_widget_show(subject_frame);
4267
4268         subject = gtk_hbox_new(FALSE, 0);
4269         gtk_container_set_border_width(GTK_CONTAINER(subject), BORDER_WIDTH);
4270         gtk_widget_show(subject);
4271
4272         label = gtk_label_new(_("Subject:"));
4273         gtk_box_pack_start(GTK_BOX(subject), label, FALSE, FALSE, 4);
4274         gtk_widget_show(label);
4275
4276         subject_entry = gtk_entry_new();
4277         gtk_box_pack_start(GTK_BOX(subject), subject_entry, TRUE, TRUE, 2);
4278         gtk_signal_connect(GTK_OBJECT(subject_entry), "activate", GTK_SIGNAL_FUNC(text_activated), compose);
4279         gtk_widget_show(subject_entry);
4280         compose->subject_entry = subject_entry;
4281         gtk_container_add(GTK_CONTAINER(subject_frame), subject);
4282         
4283         edit_vbox = gtk_vbox_new(FALSE, 0);
4284 #if 0 /* NEW COMPOSE GUI */
4285         gtk_box_pack_start(GTK_BOX(vbox2), edit_vbox, TRUE, TRUE, 0);
4286 #endif
4287
4288         gtk_box_pack_start(GTK_BOX(edit_vbox), subject_hbox, FALSE, FALSE, 0);
4289
4290         /* ruler */
4291         ruler_hbox = gtk_hbox_new(FALSE, 0);
4292         gtk_box_pack_start(GTK_BOX(edit_vbox), ruler_hbox, FALSE, FALSE, 0);
4293
4294         ruler = gtk_shruler_new();
4295         gtk_ruler_set_range(GTK_RULER(ruler), 0.0, 100.0, 1.0, 100.0);
4296         gtk_box_pack_start(GTK_BOX(ruler_hbox), ruler, TRUE, TRUE,
4297                            BORDER_WIDTH + 1);
4298         gtk_widget_set_usize(ruler_hbox, 1, -1);
4299
4300         /* text widget */
4301         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
4302         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
4303                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
4304         gtk_box_pack_start(GTK_BOX(edit_vbox), scrolledwin, TRUE, TRUE, 0);
4305         gtk_widget_set_usize(scrolledwin, prefs_common.compose_width, -1);
4306
4307         text = gtk_stext_new(gtk_scrolled_window_get_hadjustment
4308                             (GTK_SCROLLED_WINDOW(scrolledwin)),
4309                             gtk_scrolled_window_get_vadjustment
4310                             (GTK_SCROLLED_WINDOW(scrolledwin)));
4311         GTK_STEXT(text)->default_tab_width = 8;
4312         gtk_stext_set_editable(GTK_STEXT(text), TRUE);
4313
4314         if (prefs_common.block_cursor) {
4315                 GTK_STEXT(text)->cursor_type = GTK_STEXT_CURSOR_BLOCK;
4316         }
4317         
4318         if (prefs_common.smart_wrapping) {      
4319                 gtk_stext_set_word_wrap(GTK_STEXT(text), TRUE);
4320                 gtk_stext_set_wrap_rmargin(GTK_STEXT(text), prefs_common.linewrap_len);
4321         }               
4322
4323         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
4324
4325         gtk_signal_connect(GTK_OBJECT(text), "changed",
4326                            GTK_SIGNAL_FUNC(compose_changed_cb), compose);
4327         gtk_signal_connect(GTK_OBJECT(text), "grab_focus",
4328                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
4329         gtk_signal_connect(GTK_OBJECT(text), "activate",
4330                            GTK_SIGNAL_FUNC(text_activated), compose);
4331         gtk_signal_connect_after(GTK_OBJECT(text), "button_press_event",
4332                                  GTK_SIGNAL_FUNC(compose_button_press_cb),
4333                                  edit_vbox);
4334 #if 0
4335         gtk_signal_connect_after(GTK_OBJECT(text), "key_press_event",
4336                                  GTK_SIGNAL_FUNC(compose_key_press_cb),
4337                                  compose);
4338 #endif
4339         gtk_signal_connect_after(GTK_OBJECT(text), "size_allocate",
4340                                  GTK_SIGNAL_FUNC(compose_edit_size_alloc),
4341                                  ruler);
4342
4343         /* drag and drop */
4344         gtk_drag_dest_set(text, GTK_DEST_DEFAULT_ALL, compose_mime_types, 1,
4345                           GDK_ACTION_COPY);
4346         gtk_signal_connect(GTK_OBJECT(text), "drag_data_received",
4347                            GTK_SIGNAL_FUNC(compose_insert_drag_received_cb),
4348                            compose);
4349         gtk_widget_show_all(vbox);
4350
4351         /* pane between attach clist and text */
4352         paned = gtk_vpaned_new();
4353         gtk_paned_set_gutter_size(GTK_PANED(paned), 12);
4354         gtk_paned_set_handle_size(GTK_PANED(paned), 12);
4355         gtk_container_add(GTK_CONTAINER(vbox2), paned);
4356         gtk_paned_add1(GTK_PANED(paned), notebook);
4357         gtk_paned_add2(GTK_PANED(paned), edit_vbox);
4358         gtk_widget_show_all(paned);
4359
4360         style = gtk_widget_get_style(text);
4361
4362         /* workaround for the slow down of GtkSText when using Pixmap theme */
4363         if (style->engine) {
4364                 GtkThemeEngine *engine;
4365
4366                 engine = style->engine;
4367                 style->engine = NULL;
4368                 new_style = gtk_style_copy(style);
4369                 style->engine = engine;
4370         } else
4371                 new_style = gtk_style_copy(style);
4372
4373         if (prefs_common.textfont) {
4374                 CharSet charset;
4375
4376                 charset = conv_get_current_charset();
4377                 if (MB_CUR_MAX == 1) {
4378                         gchar *fontstr, *p;
4379
4380                         Xstrdup_a(fontstr, prefs_common.textfont, );
4381                         if (fontstr && (p = strchr(fontstr, ',')) != NULL)
4382                                 *p = '\0';
4383                         font = gdk_font_load(fontstr);
4384                 } else
4385                         font = gdk_fontset_load(prefs_common.textfont);
4386                 if (font) {
4387                         gdk_font_unref(new_style->font);
4388                         new_style->font = font;
4389                 }
4390         }
4391
4392         gtk_widget_set_style(text, new_style);
4393
4394         color[0] = quote_color;
4395         cmap = gdk_window_get_colormap(window->window);
4396         gdk_colormap_alloc_colors(cmap, color, 1, FALSE, TRUE, success);
4397         if (success[0] == FALSE) {
4398                 g_warning("Compose: color allocation failed.\n");
4399                 style = gtk_widget_get_style(text);
4400                 quote_color = style->black;
4401         }
4402
4403         n_entries = sizeof(compose_popup_entries) /
4404                 sizeof(compose_popup_entries[0]);
4405         popupmenu = menu_create_items(compose_popup_entries, n_entries,
4406                                       "<Compose>", &popupfactory,
4407                                       compose);
4408
4409         ifactory = gtk_item_factory_from_widget(menubar);
4410         menu_set_sensitive(ifactory, "/Edit/Undo", FALSE);
4411         menu_set_sensitive(ifactory, "/Edit/Redo", FALSE);
4412
4413         tmpl_menu = gtk_item_factory_get_item(ifactory, "/Tools/Template");
4414 #if 0 /* NEW COMPOSE GUI */
4415         gtk_widget_hide(bcc_hbox);
4416         gtk_widget_hide(bcc_entry);
4417         gtk_widget_hide(reply_hbox);
4418         gtk_widget_hide(reply_entry);
4419         gtk_widget_hide(followup_hbox);
4420         gtk_widget_hide(followup_entry);
4421         gtk_widget_hide(ruler_hbox);
4422         gtk_table_set_row_spacing(GTK_TABLE(table), 4, 0);
4423         gtk_table_set_row_spacing(GTK_TABLE(table), 5, 0);
4424         gtk_table_set_row_spacing(GTK_TABLE(table), 6, 0);
4425
4426         if (account->protocol == A_NNTP) {
4427                 gtk_widget_hide(to_hbox);
4428                 gtk_widget_hide(to_entry);
4429                 gtk_widget_hide(cc_hbox);
4430                 gtk_widget_hide(cc_entry);
4431                 gtk_table_set_row_spacing(GTK_TABLE(table), 1, 0);
4432                 gtk_table_set_row_spacing(GTK_TABLE(table), 3, 0);
4433         } else {
4434                 gtk_widget_hide(newsgroups_hbox);
4435                 gtk_widget_hide(newsgroups_entry);
4436                 gtk_table_set_row_spacing(GTK_TABLE(table), 2, 0);
4437                 menu_set_sensitive(ifactory, "/View/Followup to", FALSE);
4438         }
4439 #endif
4440
4441         update_compose_actions_menu(ifactory, "/Tools/Actions", compose);
4442
4443
4444         undostruct = undo_init(text);
4445         undo_set_change_state_func(undostruct, &compose_undo_state_changed,
4446                                    menubar);
4447
4448         gtk_widget_show(window);
4449
4450         address_completion_start(window);
4451
4452         compose->window        = window;
4453         compose->vbox          = vbox;
4454         compose->menubar       = menubar;
4455         compose->handlebox     = handlebox;
4456
4457         compose->vbox2         = vbox2;
4458
4459         compose->paned = paned;
4460
4461         compose->edit_vbox     = edit_vbox;
4462         compose->ruler_hbox    = ruler_hbox;
4463         compose->ruler         = ruler;
4464         compose->scrolledwin   = scrolledwin;
4465         compose->text          = text;
4466
4467         compose->focused_editable = NULL;
4468
4469         compose->popupmenu    = popupmenu;
4470         compose->popupfactory = popupfactory;
4471
4472         compose->tmpl_menu = tmpl_menu;
4473
4474         compose->mode = mode;
4475
4476         compose->targetinfo = NULL;
4477         compose->replyinfo  = NULL;
4478
4479         compose->replyto     = NULL;
4480         compose->mailinglist = NULL;
4481         compose->cc          = NULL;
4482         compose->bcc         = NULL;
4483         compose->followup_to = NULL;
4484         compose->inreplyto   = NULL;
4485         compose->references  = NULL;
4486         compose->msgid       = NULL;
4487         compose->boundary    = NULL;
4488
4489 #if USE_GPGME
4490         compose->use_signing    = FALSE;
4491         compose->use_encryption = FALSE;
4492 #endif /* USE_GPGME */
4493
4494         compose->modified = FALSE;
4495
4496         compose->return_receipt = FALSE;
4497
4498         compose->to_list        = NULL;
4499         compose->newsgroup_list = NULL;
4500
4501         compose->exteditor_file    = NULL;
4502         compose->exteditor_pid     = -1;
4503         compose->exteditor_readdes = -1;
4504         compose->exteditor_tag     = -1;
4505
4506         compose->bounce_filename = NULL;
4507         compose->undostruct = undostruct;
4508 #if USE_PSPELL
4509         
4510         menu_set_sensitive(ifactory, "/Spelling", FALSE);
4511         if (prefs_common.enable_pspell) {
4512                 gtkpspell = gtkpspell_new((const gchar*)prefs_common.dictionary,
4513                                           conv_get_current_charset_str(),
4514                                           prefs_common.misspelled_col,
4515                                           prefs_common.check_while_typing,
4516                                           prefs_common.use_alternate,
4517                                           GTK_STEXT(text));
4518                 if (!gtkpspell) {
4519                         alertpanel_error(_("Spell checker could not be started.\n%s"), gtkpspellcheckers->error_message);
4520                         gtkpspell_checkers_reset_error();
4521                 } else {
4522
4523                         GtkWidget *menuitem;
4524
4525                         if (!gtkpspell_set_sug_mode(gtkpspell, prefs_common.pspell_sugmode)) {
4526                                 debug_print(_("Pspell: could not set suggestion mode %s"),
4527                                     gtkpspellcheckers->error_message);
4528                                 gtkpspell_checkers_reset_error();
4529                         }
4530
4531                         menuitem = gtk_item_factory_get_item(ifactory, "/Spelling/Spelling Configuration");
4532                         gtkpspell_populate_submenu(gtkpspell, menuitem);
4533                         menu_set_sensitive(ifactory, "/Spelling", TRUE);
4534                         }
4535         }
4536 #endif
4537
4538         compose_set_title(compose);
4539
4540 #if 0 /* NEW COMPOSE GUI */
4541         compose->use_bcc        = FALSE;
4542         compose->use_replyto    = FALSE;
4543         compose->use_followupto = FALSE;
4544 #endif
4545
4546 #if USE_PSPELL
4547         compose->gtkpspell      = gtkpspell;
4548 #endif
4549
4550 #if 0 /* NEW COMPOSE GUI */
4551         if (account->protocol != A_NNTP) {
4552                 menuitem = gtk_item_factory_get_item(ifactory, "/View/To");
4553                 gtk_check_menu_item_set_active
4554                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
4555                 gtk_widget_set_sensitive(menuitem, FALSE);
4556                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Cc");
4557                 gtk_check_menu_item_set_active
4558                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
4559                 gtk_widget_set_sensitive(menuitem, FALSE);
4560         }
4561 #endif
4562         if (account->set_autocc && account->auto_cc && mode != COMPOSE_REEDIT) {
4563                 compose_entry_append(compose, account->auto_cc, COMPOSE_CC);
4564 #if 0 /* NEW COMPOSE GUI */
4565                 compose->use_cc = TRUE;
4566                 gtk_entry_set_text(GTK_ENTRY(cc_entry), account->auto_cc);
4567                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Cc");
4568                 gtk_check_menu_item_set_active
4569                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
4570 #endif
4571         }
4572         if (account->set_autobcc) {
4573                 compose_entry_append(compose, account->auto_bcc, COMPOSE_BCC);
4574 #if 0 /* NEW COMPOSE GUI */
4575                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Bcc");
4576                 gtk_check_menu_item_set_active
4577                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
4578                 if (account->auto_bcc && mode != COMPOSE_REEDIT)
4579                         gtk_entry_set_text(GTK_ENTRY(bcc_entry),
4580                                            account->auto_bcc);
4581 #endif
4582         }
4583         if (account->set_autoreplyto && account->auto_replyto && mode != COMPOSE_REEDIT) {
4584                 compose_entry_append(compose, account->auto_replyto, COMPOSE_REPLYTO);
4585 #if 0 /* NEW COMPOSE GUI */
4586                 compose->use_replyto = TRUE;
4587                 menuitem = gtk_item_factory_get_item(ifactory,
4588                                                      "/View/Reply to");
4589                 gtk_check_menu_item_set_active
4590                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
4591                 gtk_entry_set_text(GTK_ENTRY(reply_entry),
4592                                    account->auto_replyto);
4593 #endif
4594         }
4595
4596         if (account->protocol != A_NNTP) {
4597                 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(compose->header_last->combo)->entry), prefs_common.trans_hdr ? _("To:") : "To:");
4598         } else {
4599                 gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(compose->header_last->combo)->entry), prefs_common.trans_hdr ? _("Newsgroups:") : "Newsgroups:");
4600         }
4601
4602 #if 0 /* NEW COMPOSE GUI */
4603         menuitem = gtk_item_factory_get_item(ifactory, "/View/Ruler");
4604         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
4605                                        prefs_common.show_ruler);
4606 #endif                                 
4607
4608 #if USE_GPGME
4609         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Sign");
4610         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
4611                                        account->default_sign);
4612         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Encrypt");
4613         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
4614                                        account->default_encrypt);
4615 #endif /* USE_GPGME */
4616
4617         addressbook_set_target_compose(compose);
4618         compose_set_template_menu(compose);
4619
4620         compose_list = g_list_append(compose_list, compose);
4621
4622 #if 0 /* NEW COMPOSE GUI */
4623         compose->use_to         = FALSE;
4624         compose->use_cc         = FALSE;
4625         compose->use_attach     = TRUE;
4626 #endif
4627
4628 #if 0 /* NEW COMPOSE GUI */
4629         if (!compose->use_bcc) {
4630                 gtk_widget_hide(bcc_hbox);
4631                 gtk_widget_hide(bcc_entry);
4632                 gtk_table_set_row_spacing(GTK_TABLE(table), 4, 0);
4633         }
4634         if (!compose->use_replyto) {
4635                 gtk_widget_hide(reply_hbox);
4636                 gtk_widget_hide(reply_entry);
4637                 gtk_table_set_row_spacing(GTK_TABLE(table), 5, 0);
4638         }
4639         if (!compose->use_followupto) {
4640                 gtk_widget_hide(followup_hbox);
4641                 gtk_widget_hide(followup_entry);
4642                 gtk_table_set_row_spacing(GTK_TABLE(table), 6, 0);
4643         }
4644 #endif
4645         if (!prefs_common.show_ruler)
4646                 gtk_widget_hide(ruler_hbox);
4647
4648         select_account(compose, account);
4649         set_toolbar_style(compose);
4650
4651         return compose;
4652 }
4653
4654 static void compose_toolbar_create(Compose *compose, GtkWidget *container)
4655 {
4656         GtkWidget *toolbar;
4657         GtkWidget *icon_wid;
4658         GtkWidget *send_btn;
4659         GtkWidget *sendl_btn;
4660         GtkWidget *draft_btn;
4661         GtkWidget *insert_btn;
4662         GtkWidget *attach_btn;
4663         GtkWidget *sig_btn;
4664         GtkWidget *exteditor_btn;
4665         GtkWidget *linewrap_btn;
4666         GtkWidget *addrbook_btn;
4667
4668         toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
4669                                   GTK_TOOLBAR_BOTH);
4670         gtk_container_add(GTK_CONTAINER(container), toolbar);
4671         gtk_container_set_border_width(GTK_CONTAINER(container), 2);
4672         gtk_toolbar_set_button_relief(GTK_TOOLBAR(toolbar), GTK_RELIEF_NONE);
4673         gtk_toolbar_set_space_style(GTK_TOOLBAR(toolbar),
4674                                     GTK_TOOLBAR_SPACE_LINE);
4675
4676         icon_wid = stock_pixmap_widget(container, STOCK_PIXMAP_MAIL_SEND);
4677         send_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
4678                                            _("Send"),
4679                                            _("Send message"),
4680                                            "Send",
4681                                            icon_wid, toolbar_send_cb, compose);
4682
4683         icon_wid = stock_pixmap_widget(container, STOCK_PIXMAP_MAIL_SEND_QUEUE);
4684         sendl_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
4685                                            _("Send later"),
4686                                            _("Put into queue folder and send later"),
4687                                            "Send later",
4688                                            icon_wid, toolbar_send_later_cb,
4689                                            compose);
4690
4691         icon_wid = stock_pixmap_widget(container, STOCK_PIXMAP_MAIL);
4692         draft_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
4693                                             _("Draft"),
4694                                             _("Save to draft folder"),
4695                                             "Draft",
4696                                             icon_wid, toolbar_draft_cb,
4697                                             compose);
4698
4699         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
4700
4701         icon_wid = stock_pixmap_widget(container, STOCK_PIXMAP_INSERT_FILE);
4702         insert_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
4703                                              _("Insert"),
4704                                              _("Insert file"),
4705                                              "Insert",
4706                                              icon_wid, toolbar_insert_cb,
4707                                              compose);
4708
4709         icon_wid = stock_pixmap_widget(container, STOCK_PIXMAP_MAIL_ATTACH);
4710         attach_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
4711                                              _("Attach"),
4712                                              _("Attach file"),
4713                                              "Attach",
4714                                              icon_wid, toolbar_attach_cb,
4715                                              compose);
4716
4717         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
4718
4719         icon_wid = stock_pixmap_widget(container, STOCK_PIXMAP_MAIL_SIGN);
4720         sig_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
4721                                           _("Signature"),
4722                                           _("Insert signature"),
4723                                           "Signature",
4724                                           icon_wid, toolbar_sig_cb, compose);
4725
4726         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
4727
4728         icon_wid = stock_pixmap_widget(container, STOCK_PIXMAP_EDIT_EXTERN);
4729         exteditor_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
4730                                                 _("Editor"),
4731                                                 _("Edit with external editor"),
4732                                                 "Editor",
4733                                                 icon_wid,
4734                                                 toolbar_ext_editor_cb,
4735                                                 compose);
4736
4737         icon_wid = stock_pixmap_widget(container, STOCK_PIXMAP_LINEWRAP);
4738         linewrap_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
4739                                                _("Linewrap"),
4740                                                _("Wrap all long lines"),
4741                                                "Linewrap",
4742                                                icon_wid,
4743                                                toolbar_linewrap_cb,
4744                                                compose);
4745
4746         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
4747
4748         icon_wid = stock_pixmap_widget(container, STOCK_PIXMAP_ADDRESS_BOOK);
4749         addrbook_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
4750                                                _("Address"),
4751                                                _("Address book"),
4752                                                "Address",
4753                                                icon_wid, toolbar_address_cb,
4754                                                compose);
4755
4756         compose->toolbar       = toolbar;
4757         compose->send_btn      = send_btn;
4758         compose->sendl_btn     = sendl_btn;
4759         compose->draft_btn     = draft_btn;
4760         compose->insert_btn    = insert_btn;
4761         compose->attach_btn    = attach_btn;
4762         compose->sig_btn       = sig_btn;
4763         compose->exteditor_btn = exteditor_btn;
4764         compose->linewrap_btn  = linewrap_btn;
4765         compose->addrbook_btn  = addrbook_btn;
4766
4767         gtk_widget_show_all(toolbar);
4768 }
4769
4770 static GtkWidget *compose_account_option_menu_create(Compose *compose)
4771 {
4772         GList *accounts;
4773         GtkWidget *hbox;
4774         GtkWidget *optmenu;
4775         GtkWidget *menu;
4776         gint num = 0, def_menu = 0;
4777
4778         accounts = account_get_list();
4779         g_return_val_if_fail(accounts != NULL, NULL);
4780
4781         hbox = gtk_hbox_new(FALSE, 0);
4782         optmenu = gtk_option_menu_new();
4783         gtk_box_pack_start(GTK_BOX(hbox), optmenu, FALSE, FALSE, 0);
4784         menu = gtk_menu_new();
4785
4786         for (; accounts != NULL; accounts = accounts->next, num++) {
4787                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
4788                 GtkWidget *menuitem;
4789                 gchar *name;
4790
4791                 if (ac == compose->account) def_menu = num;
4792
4793                 name = g_strdup_printf("%s: %s <%s>",
4794                                        ac->account_name, ac->name, ac->address);
4795                 MENUITEM_ADD(menu, menuitem, name, ac);
4796                 g_free(name);
4797                 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
4798                                    GTK_SIGNAL_FUNC(account_activated),
4799                                    compose);
4800         }
4801
4802         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), menu);
4803         gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), def_menu);
4804
4805         return hbox;
4806 }
4807
4808 static void compose_set_template_menu(Compose *compose)
4809 {
4810         GSList *tmpl_list, *cur;
4811         GtkWidget *menu;
4812         GtkWidget *item;
4813
4814         tmpl_list = template_get_config();
4815
4816         menu = gtk_menu_new();
4817
4818         for (cur = tmpl_list; cur != NULL; cur = cur->next) {
4819                 Template *tmpl = (Template *)cur->data;
4820
4821                 item = gtk_menu_item_new_with_label(tmpl->name);
4822                 gtk_menu_append(GTK_MENU(menu), item);
4823                 gtk_signal_connect(GTK_OBJECT(item), "activate",
4824                                    GTK_SIGNAL_FUNC(compose_template_activate_cb),
4825                                    compose);
4826                 gtk_object_set_data(GTK_OBJECT(item), "template", tmpl);
4827                 gtk_widget_show(item);
4828         }
4829
4830         gtk_widget_show(menu);
4831         gtk_menu_item_set_submenu(GTK_MENU_ITEM(compose->tmpl_menu), menu);
4832 }
4833
4834 void compose_reflect_prefs_all(void)
4835 {
4836         GList *cur;
4837         Compose *compose;
4838
4839         for (cur = compose_list; cur != NULL; cur = cur->next) {
4840                 compose = (Compose *)cur->data;
4841                 compose_set_template_menu(compose);
4842         }
4843 }
4844
4845 void compose_reflect_prefs_pixmap_theme(void)
4846 {
4847         GList *cur;
4848         Compose *compose;
4849
4850         for (cur = compose_list; cur != NULL; cur = cur->next) {
4851                 compose = (Compose *)cur->data;
4852                 gtk_container_remove(GTK_CONTAINER(compose->handlebox), GTK_WIDGET(compose->toolbar));
4853                 compose->toolbar = NULL;
4854                 compose_toolbar_create(compose, compose->handlebox);
4855                 set_toolbar_style(compose);
4856         }
4857 }
4858
4859
4860 static void compose_template_apply(Compose *compose, Template *tmpl)
4861 {
4862         gchar *qmark;
4863         gchar *parsed_str;
4864
4865         if (!tmpl || !tmpl->value) return;
4866
4867         gtk_stext_freeze(GTK_STEXT(compose->text));
4868
4869         if (tmpl->subject && *tmpl->subject != '\0')
4870                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry),
4871                                    tmpl->subject);
4872         if (tmpl->to && *tmpl->to != '\0')
4873                 compose_entry_append(compose, tmpl->to, COMPOSE_TO);
4874
4875         gtk_stext_clear(GTK_STEXT(compose->text));
4876
4877         if (compose->replyinfo == NULL) {
4878                 MsgInfo dummyinfo;
4879
4880                 memset(&dummyinfo, 0, sizeof(MsgInfo));
4881                 parsed_str = compose_quote_fmt(compose, &dummyinfo,
4882                                                tmpl->value, NULL, NULL);
4883         } else {
4884                 if (prefs_common.quotemark && *prefs_common.quotemark)
4885                         qmark = prefs_common.quotemark;
4886                 else
4887                         qmark = "> ";
4888
4889                 parsed_str = compose_quote_fmt(compose, compose->replyinfo,
4890                                                tmpl->value, qmark, NULL);
4891         }
4892
4893         if (parsed_str && prefs_common.auto_sig)
4894                 compose_insert_sig(compose);
4895
4896         gtk_stext_thaw(GTK_STEXT(compose->text));
4897 }
4898
4899 static void compose_destroy(Compose *compose)
4900 {
4901         gint row;
4902         GtkCList *clist = GTK_CLIST(compose->attach_clist);
4903         AttachInfo *ainfo;
4904
4905         /* NOTE: address_completion_end() does nothing with the window
4906          * however this may change. */
4907         address_completion_end(compose->window);
4908
4909         slist_free_strings(compose->to_list);
4910         g_slist_free(compose->to_list);
4911         slist_free_strings(compose->newsgroup_list);
4912         g_slist_free(compose->newsgroup_list);
4913         slist_free_strings(compose->header_list);
4914         g_slist_free(compose->header_list);
4915
4916         procmsg_msginfo_free(compose->targetinfo);
4917         procmsg_msginfo_free(compose->replyinfo);
4918
4919         g_free(compose->replyto);
4920         g_free(compose->cc);
4921         g_free(compose->bcc);
4922         g_free(compose->newsgroups);
4923         g_free(compose->followup_to);
4924
4925         g_free(compose->inreplyto);
4926         g_free(compose->references);
4927         g_free(compose->msgid);
4928         g_free(compose->boundary);
4929
4930         if (compose->bounce_filename)
4931                 g_free(compose->bounce_filename);
4932
4933         g_free(compose->exteditor_file);
4934
4935         for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
4936              row++)
4937                 compose_attach_info_free(ainfo);
4938
4939         if (addressbook_get_target_compose() == compose)
4940                 addressbook_set_target_compose(NULL);
4941
4942 #if USE_PSPELL
4943         if (compose->gtkpspell) {
4944                 gtkpspell_delete(compose->gtkpspell);
4945         }
4946 #endif
4947
4948         prefs_common.compose_width = compose->scrolledwin->allocation.width;
4949         prefs_common.compose_height = compose->window->allocation.height;
4950
4951         gtk_widget_destroy(compose->paned);
4952
4953         g_free(compose);
4954
4955         compose_list = g_list_remove(compose_list, compose);
4956 }
4957
4958 static void compose_attach_info_free(AttachInfo *ainfo)
4959 {
4960         g_free(ainfo->file);
4961         g_free(ainfo->content_type);
4962         g_free(ainfo->name);
4963         g_free(ainfo);
4964 }
4965
4966 static void compose_attach_remove_selected(Compose *compose)
4967 {
4968         GtkCList *clist = GTK_CLIST(compose->attach_clist);
4969         AttachInfo *ainfo;
4970         gint row;
4971
4972         while (clist->selection != NULL) {
4973                 row = GPOINTER_TO_INT(clist->selection->data);
4974                 ainfo = gtk_clist_get_row_data(clist, row);
4975                 compose_attach_info_free(ainfo);
4976                 gtk_clist_remove(clist, row);
4977         }
4978 }
4979
4980 static struct _AttachProperty
4981 {
4982         GtkWidget *window;
4983         GtkWidget *mimetype_entry;
4984         GtkWidget *encoding_optmenu;
4985         GtkWidget *path_entry;
4986         GtkWidget *filename_entry;
4987         GtkWidget *ok_btn;
4988         GtkWidget *cancel_btn;
4989 } attach_prop;
4990
4991 static void compose_attach_property(Compose *compose)
4992 {
4993         GtkCList *clist = GTK_CLIST(compose->attach_clist);
4994         AttachInfo *ainfo;
4995         gint row;
4996         GtkOptionMenu *optmenu;
4997         static gboolean cancelled;
4998
4999         if (!clist->selection) return;
5000         row = GPOINTER_TO_INT(clist->selection->data);
5001
5002         ainfo = gtk_clist_get_row_data(clist, row);
5003         if (!ainfo) return;
5004
5005         if (!attach_prop.window)
5006                 compose_attach_property_create(&cancelled);
5007         gtk_widget_grab_focus(attach_prop.ok_btn);
5008         gtk_widget_show(attach_prop.window);
5009         manage_window_set_transient(GTK_WINDOW(attach_prop.window));
5010
5011         optmenu = GTK_OPTION_MENU(attach_prop.encoding_optmenu);
5012         if (ainfo->encoding == ENC_UNKNOWN)
5013                 gtk_option_menu_set_history(optmenu, ENC_BASE64);
5014         else
5015                 gtk_option_menu_set_history(optmenu, ainfo->encoding);
5016
5017         gtk_entry_set_text(GTK_ENTRY(attach_prop.mimetype_entry),
5018                            ainfo->content_type ? ainfo->content_type : "");
5019         gtk_entry_set_text(GTK_ENTRY(attach_prop.path_entry),
5020                            ainfo->file ? ainfo->file : "");
5021         gtk_entry_set_text(GTK_ENTRY(attach_prop.filename_entry),
5022                            ainfo->name ? ainfo->name : "");
5023
5024         for (;;) {
5025                 gchar *text;
5026                 gchar *cnttype = NULL;
5027                 gchar *file = NULL;
5028                 off_t size = 0;
5029                 GtkWidget *menu;
5030                 GtkWidget *menuitem;
5031
5032                 cancelled = FALSE;
5033                 gtk_main();
5034
5035                 if (cancelled == TRUE) {
5036                         gtk_widget_hide(attach_prop.window);
5037                         break;
5038                 }
5039
5040                 text = gtk_entry_get_text(GTK_ENTRY(attach_prop.mimetype_entry));
5041                 if (*text != '\0') {
5042                         gchar *p;
5043
5044                         text = g_strstrip(g_strdup(text));
5045                         if ((p = strchr(text, '/')) && !strchr(p + 1, '/')) {
5046                                 cnttype = g_strdup(text);
5047                                 g_free(text);
5048                         } else {
5049                                 alertpanel_error(_("Invalid MIME type."));
5050                                 g_free(text);
5051                                 continue;
5052                         }
5053                 }
5054
5055                 menu = gtk_option_menu_get_menu(optmenu);
5056                 menuitem = gtk_menu_get_active(GTK_MENU(menu));
5057                 ainfo->encoding = GPOINTER_TO_INT
5058                         (gtk_object_get_user_data(GTK_OBJECT(menuitem)));
5059
5060                 text = gtk_entry_get_text(GTK_ENTRY(attach_prop.path_entry));
5061                 if (*text != '\0') {
5062                         if (is_file_exist(text) &&
5063                             (size = get_file_size(text)) > 0)
5064                                 file = g_strdup(text);
5065                         else {
5066                                 alertpanel_error
5067                                         (_("File doesn't exist or is empty."));
5068                                 g_free(cnttype);
5069                                 continue;
5070                         }
5071                 }
5072
5073                 text = gtk_entry_get_text(GTK_ENTRY(attach_prop.filename_entry));
5074                 if (*text != '\0') {
5075                         g_free(ainfo->name);
5076                         ainfo->name = g_strdup(text);
5077                 }
5078
5079                 if (cnttype) {
5080                         g_free(ainfo->content_type);
5081                         ainfo->content_type = cnttype;
5082                 }
5083                 if (file) {
5084                         g_free(ainfo->file);
5085                         ainfo->file = file;
5086                 }
5087                 if (size)
5088                         ainfo->size = size;
5089
5090                 gtk_clist_set_text(clist, row, COL_MIMETYPE,
5091                                    ainfo->content_type);
5092                 gtk_clist_set_text(clist, row, COL_SIZE,
5093                                    to_human_readable(ainfo->size));
5094                 gtk_clist_set_text(clist, row, COL_NAME, ainfo->name);
5095
5096                 gtk_widget_hide(attach_prop.window);
5097                 break;
5098         }
5099 }
5100
5101 #define SET_LABEL_AND_ENTRY(str, entry, top) \
5102 { \
5103         label = gtk_label_new(str); \
5104         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), \
5105                          GTK_FILL, 0, 0, 0); \
5106         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); \
5107  \
5108         entry = gtk_entry_new(); \
5109         gtk_table_attach(GTK_TABLE(table), entry, 1, 2, top, (top + 1), \
5110                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); \
5111 }
5112
5113 static void compose_attach_property_create(gboolean *cancelled)
5114 {
5115         GtkWidget *window;
5116         GtkWidget *vbox;
5117         GtkWidget *table;
5118         GtkWidget *label;
5119         GtkWidget *mimetype_entry;
5120         GtkWidget *hbox;
5121         GtkWidget *optmenu;
5122         GtkWidget *optmenu_menu;
5123         GtkWidget *menuitem;
5124         GtkWidget *path_entry;
5125         GtkWidget *filename_entry;
5126         GtkWidget *hbbox;
5127         GtkWidget *ok_btn;
5128         GtkWidget *cancel_btn;
5129         GList     *mime_type_list, *strlist;
5130
5131         debug_print("Creating attach_property window...\n");
5132
5133         window = gtk_window_new(GTK_WINDOW_DIALOG);
5134         gtk_widget_set_usize(window, 480, -1);
5135         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
5136         gtk_window_set_title(GTK_WINDOW(window), _("Property"));
5137         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
5138         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
5139         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
5140                            GTK_SIGNAL_FUNC(attach_property_delete_event),
5141                            cancelled);
5142         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
5143                            GTK_SIGNAL_FUNC(attach_property_key_pressed),
5144                            cancelled);
5145
5146         vbox = gtk_vbox_new(FALSE, 8);
5147         gtk_container_add(GTK_CONTAINER(window), vbox);
5148
5149         table = gtk_table_new(4, 2, FALSE);
5150         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
5151         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
5152         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
5153
5154         label = gtk_label_new(_("MIME type")); 
5155         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, (0 + 1), 
5156                          GTK_FILL, 0, 0, 0); 
5157         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); 
5158         mimetype_entry = gtk_combo_new(); 
5159         gtk_table_attach(GTK_TABLE(table), mimetype_entry, 1, 2, 0, (0 + 1), 
5160                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
5161                          
5162         /* stuff with list */
5163         mime_type_list = procmime_get_mime_type_list();
5164         strlist = NULL;
5165         for (; mime_type_list != NULL; mime_type_list = mime_type_list->next) {
5166                 MimeType *type = (MimeType *) mime_type_list->data;
5167                 strlist = g_list_append(strlist, 
5168                                 g_strdup_printf("%s/%s",
5169                                         type->type, type->sub_type));
5170         }
5171         
5172         gtk_combo_set_popdown_strings(GTK_COMBO(mimetype_entry), strlist);
5173
5174         for (mime_type_list = strlist; mime_type_list != NULL; 
5175                 mime_type_list = mime_type_list->next)
5176                 g_free(mime_type_list->data);
5177         g_list_free(strlist);
5178                          
5179         mimetype_entry = GTK_COMBO(mimetype_entry)->entry;                       
5180
5181         label = gtk_label_new(_("Encoding"));
5182         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
5183                          GTK_FILL, 0, 0, 0);
5184         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
5185
5186         hbox = gtk_hbox_new(FALSE, 0);
5187         gtk_table_attach(GTK_TABLE(table), hbox, 1, 2, 1, 2,
5188                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
5189
5190         optmenu = gtk_option_menu_new();
5191         gtk_box_pack_start(GTK_BOX(hbox), optmenu, TRUE, TRUE, 0);
5192
5193         optmenu_menu = gtk_menu_new();
5194         MENUITEM_ADD(optmenu_menu, menuitem, "7bit", ENC_7BIT);
5195         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), optmenu_menu);
5196 #if 0
5197         gtk_widget_set_sensitive(menuitem, FALSE);
5198 #endif
5199         MENUITEM_ADD(optmenu_menu, menuitem, "8bit", ENC_8BIT);
5200         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), optmenu_menu);
5201 #if 0
5202         gtk_widget_set_sensitive(menuitem, FALSE);
5203 #endif
5204         MENUITEM_ADD(optmenu_menu, menuitem, "quoted-printable", ENC_QUOTED_PRINTABLE);
5205         gtk_widget_set_sensitive(menuitem, FALSE);
5206         MENUITEM_ADD(optmenu_menu, menuitem, "base64", ENC_BASE64);
5207
5208         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), optmenu_menu);
5209
5210         SET_LABEL_AND_ENTRY(_("Path"),      path_entry,     2);
5211         SET_LABEL_AND_ENTRY(_("File name"), filename_entry, 3);
5212
5213         gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
5214                                 &cancel_btn, _("Cancel"), NULL, NULL);
5215         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
5216         gtk_widget_grab_default(ok_btn);
5217
5218         gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
5219                            GTK_SIGNAL_FUNC(attach_property_ok),
5220                            cancelled);
5221         gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
5222                            GTK_SIGNAL_FUNC(attach_property_cancel),
5223                            cancelled);
5224
5225         gtk_widget_show_all(vbox);
5226
5227         attach_prop.window           = window;
5228         attach_prop.mimetype_entry   = mimetype_entry;
5229         attach_prop.encoding_optmenu = optmenu;
5230         attach_prop.path_entry       = path_entry;
5231         attach_prop.filename_entry   = filename_entry;
5232         attach_prop.ok_btn           = ok_btn;
5233         attach_prop.cancel_btn       = cancel_btn;
5234 }
5235
5236 #undef SET_LABEL_AND_ENTRY
5237
5238 static void attach_property_ok(GtkWidget *widget, gboolean *cancelled)
5239 {
5240         *cancelled = FALSE;
5241         gtk_main_quit();
5242 }
5243
5244 static void attach_property_cancel(GtkWidget *widget, gboolean *cancelled)
5245 {
5246         *cancelled = TRUE;
5247         gtk_main_quit();
5248 }
5249
5250 static gint attach_property_delete_event(GtkWidget *widget, GdkEventAny *event,
5251                                          gboolean *cancelled)
5252 {
5253         *cancelled = TRUE;
5254         gtk_main_quit();
5255
5256         return TRUE;
5257 }
5258
5259 static void attach_property_key_pressed(GtkWidget *widget, GdkEventKey *event,
5260                                         gboolean *cancelled)
5261 {
5262         if (event && event->keyval == GDK_Escape) {
5263                 *cancelled = TRUE;
5264                 gtk_main_quit();
5265         }
5266 }
5267
5268 static void compose_exec_ext_editor(Compose *compose)
5269 {
5270         gchar tmp[64];
5271         pid_t pid;
5272         gint pipe_fds[2];
5273
5274         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.%08x",
5275                    g_get_tmp_dir(), G_DIR_SEPARATOR, (gint)compose);
5276
5277         if (pipe(pipe_fds) < 0) {
5278                 perror("pipe");
5279                 return;
5280         }
5281
5282         if ((pid = fork()) < 0) {
5283                 perror("fork");
5284                 return;
5285         }
5286
5287         if (pid != 0) {
5288                 /* close the write side of the pipe */
5289                 close(pipe_fds[1]);
5290
5291                 compose->exteditor_file    = g_strdup(tmp);
5292                 compose->exteditor_pid     = pid;
5293                 compose->exteditor_readdes = pipe_fds[0];
5294
5295                 compose_set_ext_editor_sensitive(compose, FALSE);
5296
5297                 compose->exteditor_tag =
5298                         gdk_input_add(pipe_fds[0], GDK_INPUT_READ,
5299                                       compose_input_cb, compose);
5300         } else {        /* process-monitoring process */
5301                 pid_t pid_ed;
5302
5303                 if (setpgid(0, 0))
5304                         perror("setpgid");
5305
5306                 /* close the read side of the pipe */
5307                 close(pipe_fds[0]);
5308
5309                 if (compose_write_body_to_file(compose, tmp) < 0) {
5310                         fd_write(pipe_fds[1], "2\n", 2);
5311                         _exit(1);
5312                 }
5313
5314                 pid_ed = compose_exec_ext_editor_real(tmp);
5315                 if (pid_ed < 0) {
5316                         fd_write(pipe_fds[1], "1\n", 2);
5317                         _exit(1);
5318                 }
5319
5320                 /* wait until editor is terminated */
5321                 waitpid(pid_ed, NULL, 0);
5322
5323                 fd_write(pipe_fds[1], "0\n", 2);
5324
5325                 close(pipe_fds[1]);
5326                 _exit(0);
5327         }
5328 }
5329
5330 static gint compose_exec_ext_editor_real(const gchar *file)
5331 {
5332         static gchar *def_cmd = "emacs %s";
5333         gchar buf[1024];
5334         gchar *p;
5335         gchar **cmdline;
5336         pid_t pid;
5337
5338         g_return_val_if_fail(file != NULL, -1);
5339
5340         if ((pid = fork()) < 0) {
5341                 perror("fork");
5342                 return -1;
5343         }
5344
5345         if (pid != 0) return pid;
5346
5347         /* grandchild process */
5348
5349         if (setpgid(0, getppid()))
5350                 perror("setpgid");
5351
5352         if (prefs_common.ext_editor_cmd &&
5353             (p = strchr(prefs_common.ext_editor_cmd, '%')) &&
5354             *(p + 1) == 's' && !strchr(p + 2, '%')) {
5355                 g_snprintf(buf, sizeof(buf), prefs_common.ext_editor_cmd, file);
5356         } else {
5357                 if (prefs_common.ext_editor_cmd)
5358                         g_warning(_("External editor command line is invalid: `%s'\n"),
5359                                   prefs_common.ext_editor_cmd);
5360                 g_snprintf(buf, sizeof(buf), def_cmd, file);
5361         }
5362
5363         cmdline = strsplit_with_quote(buf, " ", 1024);
5364         execvp(cmdline[0], cmdline);
5365
5366         perror("execvp");
5367         g_strfreev(cmdline);
5368
5369         _exit(1);
5370 }
5371
5372 static gboolean compose_ext_editor_kill(Compose *compose)
5373 {
5374         pid_t pgid = compose->exteditor_pid * -1;
5375         gint ret;
5376
5377         ret = kill(pgid, 0);
5378
5379         if (ret == 0 || (ret == -1 && EPERM == errno)) {
5380                 AlertValue val;
5381                 gchar *msg;
5382
5383                 msg = g_strdup_printf
5384                         (_("The external editor is still working.\n"
5385                            "Force terminating the process?\n"
5386                            "process group id: %d"), -pgid);
5387                 val = alertpanel(_("Notice"), msg, _("Yes"), _("+No"), NULL);
5388                 g_free(msg);
5389
5390                 if (val == G_ALERTDEFAULT) {
5391                         gdk_input_remove(compose->exteditor_tag);
5392                         close(compose->exteditor_readdes);
5393
5394                         if (kill(pgid, SIGTERM) < 0) perror("kill");
5395                         waitpid(compose->exteditor_pid, NULL, 0);
5396
5397                         g_warning(_("Terminated process group id: %d"), -pgid);
5398                         g_warning(_("Temporary file: %s"),
5399                                   compose->exteditor_file);
5400
5401                         compose_set_ext_editor_sensitive(compose, TRUE);
5402
5403                         g_free(compose->exteditor_file);
5404                         compose->exteditor_file    = NULL;
5405                         compose->exteditor_pid     = -1;
5406                         compose->exteditor_readdes = -1;
5407                         compose->exteditor_tag     = -1;
5408                 } else
5409                         return FALSE;
5410         }
5411
5412         return TRUE;
5413 }
5414
5415 static void compose_input_cb(gpointer data, gint source,
5416                              GdkInputCondition condition)
5417 {
5418         gchar buf[3];
5419         Compose *compose = (Compose *)data;
5420         gint i = 0;
5421
5422         debug_print(_("Compose: input from monitoring process\n"));
5423
5424         gdk_input_remove(compose->exteditor_tag);
5425
5426         for (;;) {
5427                 if (read(source, &buf[i], 1) < 1) {
5428                         buf[0] = '3';
5429                         break;
5430                 }
5431                 if (buf[i] == '\n') {
5432                         buf[i] = '\0';
5433                         break;
5434                 }
5435                 i++;
5436                 if (i == sizeof(buf) - 1)
5437                         break;
5438         }
5439
5440         waitpid(compose->exteditor_pid, NULL, 0);
5441
5442         if (buf[0] == '0') {            /* success */
5443                 GtkSText *text = GTK_STEXT(compose->text);
5444
5445                 gtk_stext_freeze(text);
5446                 gtk_stext_set_point(text, 0);
5447                 gtk_stext_forward_delete(text, gtk_stext_get_length(text));
5448                 compose_insert_file(compose, compose->exteditor_file);
5449                 compose_changed_cb(NULL, compose);
5450                 gtk_stext_thaw(text);
5451
5452                 if (unlink(compose->exteditor_file) < 0)
5453                         FILE_OP_ERROR(compose->exteditor_file, "unlink");
5454         } else if (buf[0] == '1') {     /* failed */
5455                 g_warning(_("Couldn't exec external editor\n"));
5456                 if (unlink(compose->exteditor_file) < 0)
5457                         FILE_OP_ERROR(compose->exteditor_file, "unlink");
5458         } else if (buf[0] == '2') {
5459                 g_warning(_("Couldn't write to file\n"));
5460         } else if (buf[0] == '3') {
5461                 g_warning(_("Pipe read failed\n"));
5462         }
5463
5464         close(source);
5465
5466         compose_set_ext_editor_sensitive(compose, TRUE);
5467
5468         g_free(compose->exteditor_file);
5469         compose->exteditor_file    = NULL;
5470         compose->exteditor_pid     = -1;
5471         compose->exteditor_readdes = -1;
5472         compose->exteditor_tag     = -1;
5473 }
5474
5475 static void compose_set_ext_editor_sensitive(Compose *compose,
5476                                              gboolean sensitive)
5477 {
5478         GtkItemFactory *ifactory;
5479
5480         ifactory = gtk_item_factory_from_widget(compose->menubar);
5481
5482         menu_set_sensitive(ifactory, "/Message/Send", sensitive);
5483         menu_set_sensitive(ifactory, "/Message/Send later", sensitive);
5484         menu_set_sensitive(ifactory, "/Message/Save to draft folder",
5485                            sensitive);
5486         menu_set_sensitive(ifactory, "/File/Insert file", sensitive);
5487         menu_set_sensitive(ifactory, "/File/Insert signature", sensitive);
5488         menu_set_sensitive(ifactory, "/Edit/Wrap current paragraph", sensitive);
5489         menu_set_sensitive(ifactory, "/Edit/Wrap all long lines", sensitive);
5490         menu_set_sensitive(ifactory, "/Edit/Edit with external editor",
5491                            sensitive);
5492
5493         gtk_widget_set_sensitive(compose->text,          sensitive);
5494         gtk_widget_set_sensitive(compose->send_btn,      sensitive);
5495         gtk_widget_set_sensitive(compose->sendl_btn,     sensitive);
5496         gtk_widget_set_sensitive(compose->draft_btn,     sensitive);
5497         gtk_widget_set_sensitive(compose->insert_btn,    sensitive);
5498         gtk_widget_set_sensitive(compose->sig_btn,       sensitive);
5499         gtk_widget_set_sensitive(compose->exteditor_btn, sensitive);
5500         gtk_widget_set_sensitive(compose->linewrap_btn,  sensitive);
5501 }
5502
5503 /**
5504  * compose_undo_state_changed:
5505  *
5506  * Change the sensivity of the menuentries undo and redo
5507  **/
5508 static void compose_undo_state_changed(UndoMain *undostruct, gint undo_state,
5509                                        gint redo_state, gpointer data)
5510 {
5511         GtkWidget *widget = GTK_WIDGET(data);
5512         GtkItemFactory *ifactory;
5513
5514         g_return_if_fail(widget != NULL);
5515
5516         debug_print("Set_undo.  UNDO:%i  REDO:%i\n", undo_state, redo_state);
5517
5518         ifactory = gtk_item_factory_from_widget(widget);
5519
5520         switch (undo_state) {
5521         case UNDO_STATE_TRUE:
5522                 if (!undostruct->undo_state) {
5523                         debug_print ("Set_undo - Testpoint\n");
5524                         undostruct->undo_state = TRUE;
5525                         menu_set_sensitive(ifactory, "/Edit/Undo", TRUE);
5526                 }
5527                 break;
5528         case UNDO_STATE_FALSE:
5529                 if (undostruct->undo_state) {
5530                         undostruct->undo_state = FALSE;
5531                         menu_set_sensitive(ifactory, "/Edit/Undo", FALSE);
5532                 }
5533                 break;
5534         case UNDO_STATE_UNCHANGED:
5535                 break;
5536         case UNDO_STATE_REFRESH:
5537                 menu_set_sensitive(ifactory, "/Edit/Undo",
5538                                    undostruct->undo_state);
5539                 break;
5540         default:
5541                 g_warning("Undo state not recognized");
5542                 break;
5543         }
5544
5545         switch (redo_state) {
5546         case UNDO_STATE_TRUE:
5547                 if (!undostruct->redo_state) {
5548                         undostruct->redo_state = TRUE;
5549                         menu_set_sensitive(ifactory, "/Edit/Redo", TRUE);
5550                 }
5551                 break;
5552         case UNDO_STATE_FALSE:
5553                 if (undostruct->redo_state) {
5554                         undostruct->redo_state = FALSE;
5555                         menu_set_sensitive(ifactory, "/Edit/Redo", FALSE);
5556                 }
5557                 break;
5558         case UNDO_STATE_UNCHANGED:
5559                 break;
5560         case UNDO_STATE_REFRESH:
5561                 menu_set_sensitive(ifactory, "/Edit/Redo",
5562                                    undostruct->redo_state);
5563                 break;
5564         default:
5565                 g_warning("Redo state not recognized");
5566                 break;
5567         }
5568 }
5569
5570 static gint calc_cursor_xpos(GtkSText *text, gint extra, gint char_width)
5571 {
5572         gint cursor_pos;
5573
5574         cursor_pos = (text->cursor_pos_x - extra) / char_width;
5575         cursor_pos = MAX(cursor_pos, 0);
5576
5577         return cursor_pos;
5578 }
5579
5580 /* callback functions */
5581
5582 /* compose_edit_size_alloc() - called when resized. don't know whether Gtk
5583  * includes "non-client" (windows-izm) in calculation, so this calculation
5584  * may not be accurate.
5585  */
5586 static gboolean compose_edit_size_alloc(GtkEditable *widget,
5587                                         GtkAllocation *allocation,
5588                                         GtkSHRuler *shruler)
5589 {
5590         if (prefs_common.show_ruler) {
5591                 gint char_width;
5592                 gint line_width_in_chars;
5593
5594                 char_width = gtkut_get_font_width
5595                         (GTK_WIDGET(widget)->style->font);
5596                 line_width_in_chars =
5597                         (allocation->width - allocation->x) / char_width;
5598
5599                 /* got the maximum */
5600                 gtk_ruler_set_range(GTK_RULER(shruler),
5601                                     0.0, line_width_in_chars,
5602                                     calc_cursor_xpos(GTK_STEXT(widget),
5603                                                      allocation->x,
5604                                                      char_width),
5605                                     /*line_width_in_chars*/ char_width);
5606         }
5607
5608         return TRUE;
5609 }
5610
5611 static void toolbar_send_cb(GtkWidget *widget, gpointer data)
5612 {
5613         compose_send_cb(data, 0, NULL);
5614 }
5615
5616 static void toolbar_send_later_cb(GtkWidget *widget, gpointer data)
5617 {
5618         compose_send_later_cb(data, 0, NULL);
5619 }
5620
5621 static void toolbar_draft_cb(GtkWidget *widget, gpointer data)
5622 {
5623         compose_draft_cb(data, 0, NULL);
5624 }
5625
5626 static void toolbar_insert_cb(GtkWidget *widget, gpointer data)
5627 {
5628         compose_insert_file_cb(data, 0, NULL);
5629 }
5630
5631 static void toolbar_attach_cb(GtkWidget *widget, gpointer data)
5632 {
5633         compose_attach_cb(data, 0, NULL);
5634 }
5635
5636 static void toolbar_sig_cb(GtkWidget *widget, gpointer data)
5637 {
5638         Compose *compose = (Compose *)data;
5639
5640         compose_insert_sig(compose);
5641 }
5642
5643 static void toolbar_ext_editor_cb(GtkWidget *widget, gpointer data)
5644 {
5645         Compose *compose = (Compose *)data;
5646
5647         compose_exec_ext_editor(compose);
5648 }
5649
5650 static void toolbar_linewrap_cb(GtkWidget *widget, gpointer data)
5651 {
5652         Compose *compose = (Compose *)data;
5653
5654         compose_wrap_line(compose);
5655 }
5656
5657 static void toolbar_address_cb(GtkWidget *widget, gpointer data)
5658 {
5659         compose_address_cb(data, 0, NULL);
5660 }
5661
5662 static void select_account(Compose * compose, PrefsAccount * ac)
5663 {
5664 #if USE_GPGME
5665         GtkItemFactory *ifactory;
5666         GtkWidget *menuitem;
5667 #endif /* USE_GPGME */
5668         compose->account = ac;
5669         compose_set_title(compose);
5670
5671 #if 0 /* NEW COMPOSE GUI */
5672                 if (ac->protocol == A_NNTP) {
5673                         GtkItemFactory *ifactory;
5674                         GtkWidget *menuitem;
5675
5676                         ifactory = gtk_item_factory_from_widget(compose->menubar);
5677                         menu_set_sensitive(ifactory,
5678                                            "/Message/Followup to", TRUE);
5679                         gtk_widget_show(compose->newsgroups_hbox);
5680                         gtk_widget_show(compose->newsgroups_entry);
5681                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
5682                                                   1, 4);
5683
5684                         compose->use_to = FALSE;
5685                         compose->use_cc = FALSE;
5686
5687                         menuitem = gtk_item_factory_get_item(ifactory, "/Message/To");
5688                         gtk_check_menu_item_set_active
5689                                 (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
5690
5691                         menu_set_sensitive(ifactory,
5692                                            "/Message/To", TRUE);
5693                         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Cc");
5694                         gtk_check_menu_item_set_active
5695                                 (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
5696
5697                         gtk_widget_hide(compose->to_hbox);
5698                         gtk_widget_hide(compose->to_entry);
5699                         gtk_widget_hide(compose->cc_hbox);
5700                         gtk_widget_hide(compose->cc_entry);
5701                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
5702                                                   0, 0);
5703                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
5704                                                   3, 0);
5705                 }
5706                 else {
5707                         GtkItemFactory *ifactory;
5708                         GtkWidget *menuitem;
5709
5710                         ifactory = gtk_item_factory_from_widget(compose->menubar);
5711                         menu_set_sensitive(ifactory,
5712                                            "/Message/Followup to", FALSE);
5713                         gtk_entry_set_text(GTK_ENTRY(compose->newsgroups_entry), "");
5714                         gtk_widget_hide(compose->newsgroups_hbox);
5715                         gtk_widget_hide(compose->newsgroups_entry);
5716                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
5717                                                   1, 0);
5718
5719                         compose->use_to = TRUE;
5720                         compose->use_cc = TRUE;
5721
5722                         menuitem = gtk_item_factory_get_item(ifactory, "/Message/To");
5723                         gtk_check_menu_item_set_active
5724                                 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
5725                         menu_set_sensitive(ifactory,
5726                                            "/Message/To", FALSE);
5727                         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Cc");
5728                         gtk_check_menu_item_set_active
5729                                 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
5730                         gtk_widget_show(compose->to_hbox);
5731                         gtk_widget_show(compose->to_entry);
5732                         gtk_widget_show(compose->cc_hbox);
5733                         gtk_widget_show(compose->cc_entry);
5734
5735                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
5736                                                   0, 4);
5737                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
5738                                                   3, 4);
5739                 }
5740                 gtk_widget_queue_resize(compose->table_vbox);
5741 #endif
5742 #if USE_GPGME
5743                 ifactory = gtk_item_factory_from_widget(compose->menubar);
5744                         menu_set_sensitive(ifactory,
5745                                            "/Message/Sign", TRUE);
5746                         menu_set_sensitive(ifactory,
5747                                            "/Message/Encrypt", TRUE);
5748
5749                         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Sign");
5750                 if (ac->default_sign)
5751                         gtk_check_menu_item_set_active
5752                                 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
5753                 else
5754                         gtk_check_menu_item_set_active
5755                                 (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
5756
5757                         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Encrypt");
5758                 if (ac->default_encrypt)
5759                         gtk_check_menu_item_set_active
5760                                 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
5761                 else
5762                         gtk_check_menu_item_set_active
5763                                 (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
5764 #endif /* USE_GPGME */
5765
5766 }
5767
5768 static void account_activated(GtkMenuItem *menuitem, gpointer data)
5769 {
5770         Compose *compose = (Compose *)data;
5771
5772         PrefsAccount *ac;
5773
5774         ac = (PrefsAccount *)gtk_object_get_user_data(GTK_OBJECT(menuitem));
5775         g_return_if_fail(ac != NULL);
5776
5777         if (ac != compose->account)
5778                 select_account(compose, ac);
5779 }
5780
5781 static void attach_selected(GtkCList *clist, gint row, gint column,
5782                             GdkEvent *event, gpointer data)
5783 {
5784         Compose *compose = (Compose *)data;
5785
5786         if (event && event->type == GDK_2BUTTON_PRESS)
5787                 compose_attach_property(compose);
5788 }
5789
5790 static void attach_button_pressed(GtkWidget *widget, GdkEventButton *event,
5791                                   gpointer data)
5792 {
5793         Compose *compose = (Compose *)data;
5794         GtkCList *clist = GTK_CLIST(compose->attach_clist);
5795         gint row, column;
5796
5797         if (!event) return;
5798
5799         if (event->button == 3) {
5800                 if ((clist->selection && !clist->selection->next) ||
5801                     !clist->selection) {
5802                         gtk_clist_unselect_all(clist);
5803                         if (gtk_clist_get_selection_info(clist,
5804                                                          event->x, event->y,
5805                                                          &row, &column)) {
5806                                 gtk_clist_select_row(clist, row, column);
5807                                 gtkut_clist_set_focus_row(clist, row);
5808                         }
5809                 }
5810                 gtk_menu_popup(GTK_MENU(compose->popupmenu), NULL, NULL,
5811                                NULL, NULL, event->button, event->time);
5812         }
5813 }
5814
5815 static void attach_key_pressed(GtkWidget *widget, GdkEventKey *event,
5816                                gpointer data)
5817 {
5818         Compose *compose = (Compose *)data;
5819
5820         if (!event) return;
5821
5822         switch (event->keyval) {
5823         case GDK_Delete:
5824                 compose_attach_remove_selected(compose);
5825                 break;
5826         }
5827 }
5828
5829 static void compose_send_cb(gpointer data, guint action, GtkWidget *widget)
5830 {
5831         Compose *compose = (Compose *)data;
5832         gint val;
5833
5834         val = compose_send(compose);
5835
5836         if (val == 0) gtk_widget_destroy(compose->window);
5837 }
5838
5839 static void compose_send_later_cb(gpointer data, guint action,
5840                                   GtkWidget *widget)
5841 {
5842         Compose *compose = (Compose *)data;
5843         gint val;
5844
5845         val = compose_queue(compose, NULL, NULL);
5846         if (!val) gtk_widget_destroy(compose->window);
5847 }
5848
5849 static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
5850 {
5851         Compose *compose = (Compose *)data;
5852         FolderItem *draft;
5853         gchar *tmp;
5854         gint msgnum;
5855         static gboolean lock = FALSE;
5856
5857         if (lock) return;
5858
5859         if (compose->account && compose->account->folder &&
5860             FOLDER(compose->account->folder)->draft)
5861                 draft = FOLDER(compose->account->folder)->draft;
5862         else
5863                 draft = folder_get_default_draft();
5864         g_return_if_fail(draft != NULL);
5865
5866         lock = TRUE;
5867
5868         tmp = g_strdup_printf("%s%cdraft.%d", g_get_tmp_dir(),
5869                               G_DIR_SEPARATOR, (gint)compose);
5870
5871         if (compose_write_to_file(compose, tmp, TRUE) < 0) {
5872                 g_free(tmp);
5873                 lock = FALSE;
5874                 return;
5875         }
5876
5877         folder_item_scan(draft);
5878         if ((msgnum = folder_item_add_msg(draft, tmp, TRUE)) < 0) {
5879                 unlink(tmp);
5880                 g_free(tmp);
5881                 lock = FALSE;
5882                 return;
5883         }
5884         g_free(tmp);
5885         draft->mtime = 0;       /* force updating */
5886
5887         if (compose->mode == COMPOSE_REEDIT) {
5888                 compose_remove_reedit_target(compose);
5889                 if (compose->targetinfo &&
5890                     compose->targetinfo->folder != draft)
5891                         folderview_update_item(compose->targetinfo->folder,
5892                                                TRUE);
5893         }
5894
5895         folder_item_scan(draft);
5896         folderview_update_item(draft, TRUE);
5897
5898         lock = FALSE;
5899
5900         /* 0: quit editing  1: keep editing */
5901         if (action == 0)
5902                 gtk_widget_destroy(compose->window);
5903         else {
5904                 struct stat s;
5905                 gchar *path;
5906
5907                 path = folder_item_fetch_msg(draft, msgnum);
5908                 g_return_if_fail(path != NULL);
5909                 if (stat(path, &s) < 0) {
5910                         FILE_OP_ERROR(path, "stat");
5911                         g_free(path);
5912                         lock = FALSE;
5913                         return;
5914                 }
5915                 g_free(path);
5916
5917                 procmsg_msginfo_free(compose->targetinfo);
5918                 compose->targetinfo = g_new0(MsgInfo, 1);
5919                 compose->targetinfo->msgnum = msgnum;
5920                 compose->targetinfo->size = s.st_size;
5921                 compose->targetinfo->mtime = s.st_mtime;
5922                 compose->targetinfo->folder = draft;
5923                 compose->mode = COMPOSE_REEDIT;
5924         }
5925 }
5926
5927 static void compose_attach_cb(gpointer data, guint action, GtkWidget *widget)
5928 {
5929         Compose *compose = (Compose *)data;
5930         GList *file_list;
5931
5932         if (compose->bounce_filename != NULL)
5933                 return;
5934
5935         file_list = filesel_select_multiple_files(_("Select file"), NULL);
5936
5937         if (file_list) {
5938                 GList *tmp;
5939
5940                 for ( tmp = file_list; tmp; tmp = tmp->next) {
5941                         gchar *file = (gchar *) tmp->data;
5942                         compose_attach_append(compose, file, file, NULL);
5943                         compose_changed_cb(NULL, compose);
5944                         g_free(file);
5945                 }
5946                 g_list_free(file_list);
5947         }               
5948 }
5949
5950 static void compose_insert_file_cb(gpointer data, guint action,
5951                                    GtkWidget *widget)
5952 {
5953         Compose *compose = (Compose *)data;
5954         GList *file_list;
5955
5956         file_list = filesel_select_multiple_files(_("Select file"), NULL);
5957
5958         if (file_list) {
5959                 GList *tmp;
5960
5961                 for ( tmp = file_list; tmp; tmp = tmp->next) {
5962                         gchar *file = (gchar *) tmp->data;
5963                         compose_insert_file(compose, file);
5964                         g_free(file);
5965                 }
5966                 g_list_free(file_list);
5967         }
5968 }
5969
5970 static gint compose_delete_cb(GtkWidget *widget, GdkEventAny *event,
5971                               gpointer data)
5972 {
5973         compose_close_cb(data, 0, NULL);
5974         return TRUE;
5975 }
5976
5977 static void compose_close_cb(gpointer data, guint action, GtkWidget *widget)
5978 {
5979         Compose *compose = (Compose *)data;
5980         AlertValue val;
5981
5982         if (compose->exteditor_tag != -1) {
5983                 if (!compose_ext_editor_kill(compose))
5984                         return;
5985         }
5986
5987         if (compose->modified) {
5988                 val = alertpanel(_("Discard message"),
5989                                  _("This message has been modified. discard it?"),
5990                                  _("Discard"), _("to Draft"), _("Cancel"));
5991
5992                 switch (val) {
5993                 case G_ALERTDEFAULT:
5994                         break;
5995                 case G_ALERTALTERNATE:
5996                         compose_draft_cb(data, 0, NULL);
5997                         return;
5998                 default:
5999                         return;
6000                 }
6001         }
6002         gtk_widget_destroy(compose->window);
6003 }
6004
6005 static void compose_address_cb(gpointer data, guint action, GtkWidget *widget)
6006 {
6007         Compose *compose = (Compose *)data;
6008
6009         addressbook_open(compose);
6010 }
6011
6012 static void compose_template_activate_cb(GtkWidget *widget, gpointer data)
6013 {
6014         Compose *compose = (Compose *)data;
6015         Template *tmpl;
6016
6017         tmpl = gtk_object_get_data(GTK_OBJECT(widget), "template");
6018         g_return_if_fail(tmpl != NULL);
6019
6020         compose_template_apply(compose, tmpl);
6021 }
6022
6023 static void compose_ext_editor_cb(gpointer data, guint action,
6024                                   GtkWidget *widget)
6025 {
6026         Compose *compose = (Compose *)data;
6027
6028         compose_exec_ext_editor(compose);
6029 }
6030
6031 static void compose_destroy_cb(GtkWidget *widget, Compose *compose)
6032 {
6033         compose_destroy(compose);
6034 }
6035
6036 static void compose_undo_cb(Compose *compose)
6037 {
6038         undo_undo(compose->undostruct);
6039 }
6040
6041 static void compose_redo_cb(Compose *compose)
6042 {
6043         undo_redo(compose->undostruct);
6044 }
6045
6046 static void compose_cut_cb(Compose *compose)
6047 {
6048         if (compose->focused_editable &&
6049             GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
6050                 gtk_editable_cut_clipboard
6051                         (GTK_EDITABLE(compose->focused_editable));
6052 }
6053
6054 static void compose_copy_cb(Compose *compose)
6055 {
6056         if (compose->focused_editable &&
6057             GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
6058                 gtk_editable_copy_clipboard
6059                         (GTK_EDITABLE(compose->focused_editable));
6060 }
6061
6062 static void compose_paste_cb(Compose *compose)
6063 {
6064         if (compose->focused_editable &&
6065             GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
6066                 gtk_editable_paste_clipboard
6067                         (GTK_EDITABLE(compose->focused_editable));
6068 }
6069
6070 static void compose_allsel_cb(Compose *compose)
6071 {
6072         if (compose->focused_editable &&
6073             GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
6074                 gtk_editable_select_region
6075                         (GTK_EDITABLE(compose->focused_editable), 0, -1);
6076 }
6077
6078 static void compose_move_beginning_of_line_cb(Compose *compose)
6079 {
6080         if (compose->focused_editable &&
6081                 GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
6082                 gtk_stext_move_beginning_of_line(GTK_STEXT(compose->focused_editable));
6083 }
6084
6085 static void compose_gtk_stext_action_cb(Compose *compose, ComposeCallGtkSTextAction action)
6086 {
6087         if (!(compose->focused_editable && GTK_WIDGET_HAS_FOCUS(compose->focused_editable))) return;
6088                 
6089         switch (action) {
6090                 case COMPOSE_CALL_GTK_STEXT_MOVE_BEGINNING_OF_LINE:
6091                         gtk_stext_move_beginning_of_line(GTK_STEXT(compose->focused_editable));
6092                         break;
6093                 case COMPOSE_CALL_GTK_STEXT_MOVE_FORWARD_CHARACTER:
6094                         gtk_stext_move_forward_character(GTK_STEXT(compose->focused_editable));
6095                         break;
6096                 case COMPOSE_CALL_GTK_STEXT_MOVE_BACKWARD_CHARACTER:
6097                         gtk_stext_move_backward_character(GTK_STEXT(compose->focused_editable));
6098                         break;
6099                 case COMPOSE_CALL_GTK_STEXT_MOVE_FORWARD_WORD:
6100                         gtk_stext_move_forward_word(GTK_STEXT(compose->focused_editable));
6101                         break;
6102                 case COMPOSE_CALL_GTK_STEXT_MOVE_BACKWARD_WORD:
6103                         gtk_stext_move_backward_word(GTK_STEXT(compose->focused_editable));
6104                         break;
6105                 case COMPOSE_CALL_GTK_STEXT_MOVE_END_OF_LINE:
6106                         gtk_stext_move_end_of_line(GTK_STEXT(compose->focused_editable));
6107                         break;
6108                 case COMPOSE_CALL_GTK_STEXT_MOVE_NEXT_LINE:
6109                         gtk_stext_move_next_line(GTK_STEXT(compose->focused_editable));
6110                         break;
6111                 case COMPOSE_CALL_GTK_STEXT_MOVE_PREVIOUS_LINE:
6112                         gtk_stext_move_previous_line(GTK_STEXT(compose->focused_editable));
6113                         break;
6114                 case COMPOSE_CALL_GTK_STEXT_DELETE_FORWARD_CHARACTER:
6115                         gtk_stext_delete_forward_character(GTK_STEXT(compose->focused_editable));
6116                         break;
6117                 case COMPOSE_CALL_GTK_STEXT_DELETE_BACKWARD_CHARACTER:
6118                         gtk_stext_delete_backward_character(GTK_STEXT(compose->focused_editable));
6119                         break;
6120                 case COMPOSE_CALL_GTK_STEXT_DELETE_FORWARD_WORD:
6121                         gtk_stext_delete_forward_word(GTK_STEXT(compose->focused_editable));
6122                         break;
6123                 case COMPOSE_CALL_GTK_STEXT_DELETE_BACKWARD_WORD:
6124                         gtk_stext_delete_backward_word(GTK_STEXT(compose->focused_editable));
6125                         break;
6126                 case COMPOSE_CALL_GTK_STEXT_DELETE_LINE:
6127                         gtk_stext_delete_line(GTK_STEXT(compose->focused_editable));
6128                         break;
6129                 case COMPOSE_CALL_GTK_STEXT_DELETE_LINE_N:
6130                         gtk_stext_delete_line(GTK_STEXT(compose->focused_editable));
6131                         gtk_stext_delete_forward_character(GTK_STEXT(compose->focused_editable));
6132                         break;
6133                 case COMPOSE_CALL_GTK_STEXT_DELETE_TO_LINE_END:
6134                         gtk_stext_delete_to_line_end(GTK_STEXT(compose->focused_editable));
6135                         break;
6136                 default:
6137                         break;
6138         }
6139 }
6140
6141 static void compose_grab_focus_cb(GtkWidget *widget, Compose *compose)
6142 {
6143         if (GTK_IS_EDITABLE(widget))
6144                 compose->focused_editable = widget;
6145 }
6146
6147 static void compose_changed_cb(GtkEditable *editable, Compose *compose)
6148 {
6149         if (compose->modified == FALSE) {
6150                 compose->modified = TRUE;
6151                 compose_set_title(compose);
6152         }
6153 }
6154
6155 static void compose_button_press_cb(GtkWidget *widget, GdkEventButton *event,
6156                                     Compose *compose)
6157 {
6158         gtk_stext_set_point(GTK_STEXT(widget),
6159                            gtk_editable_get_position(GTK_EDITABLE(widget)));
6160 }
6161
6162 #if 0
6163 static void compose_key_press_cb(GtkWidget *widget, GdkEventKey *event,
6164                                  Compose *compose)
6165 {
6166         gtk_stext_set_point(GTK_STEXT(widget),
6167                            gtk_editable_get_position(GTK_EDITABLE(widget)));
6168 }
6169 #endif
6170
6171 #if 0 /* NEW COMPOSE GUI */
6172 static void compose_toggle_to_cb(gpointer data, guint action,
6173                                  GtkWidget *widget)
6174 {
6175         Compose *compose = (Compose *)data;
6176
6177         if (GTK_CHECK_MENU_ITEM(widget)->active) {
6178                 gtk_widget_show(compose->to_hbox);
6179                 gtk_widget_show(compose->to_entry);
6180                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 1, 4);
6181                 compose->use_to = TRUE;
6182         } else {
6183                 gtk_widget_hide(compose->to_hbox);
6184                 gtk_widget_hide(compose->to_entry);
6185                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 1, 0);
6186                 gtk_widget_queue_resize(compose->table_vbox);
6187                 compose->use_to = FALSE;
6188         }
6189
6190         if (addressbook_get_target_compose() == compose)
6191                 addressbook_set_target_compose(compose);
6192 }
6193
6194 static void compose_toggle_cc_cb(gpointer data, guint action,
6195                                  GtkWidget *widget)
6196 {
6197         Compose *compose = (Compose *)data;
6198
6199         if (GTK_CHECK_MENU_ITEM(widget)->active) {
6200                 gtk_widget_show(compose->cc_hbox);
6201                 gtk_widget_show(compose->cc_entry);
6202                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 3, 4);
6203                 compose->use_cc = TRUE;
6204         } else {
6205                 gtk_widget_hide(compose->cc_hbox);
6206                 gtk_widget_hide(compose->cc_entry);
6207                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 3, 0);
6208                 gtk_widget_queue_resize(compose->table_vbox);
6209                 compose->use_cc = FALSE;
6210         }
6211
6212         if (addressbook_get_target_compose() == compose)
6213                 addressbook_set_target_compose(compose);
6214 }
6215
6216 static void compose_toggle_bcc_cb(gpointer data, guint action,
6217                                   GtkWidget *widget)
6218 {
6219         Compose *compose = (Compose *)data;
6220
6221         if (GTK_CHECK_MENU_ITEM(widget)->active) {
6222                 gtk_widget_show(compose->bcc_hbox);
6223                 gtk_widget_show(compose->bcc_entry);
6224                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 4, 4);
6225                 compose->use_bcc = TRUE;
6226         } else {
6227                 gtk_widget_hide(compose->bcc_hbox);
6228                 gtk_widget_hide(compose->bcc_entry);
6229                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 4, 0);
6230                 gtk_widget_queue_resize(compose->table_vbox);
6231                 compose->use_bcc = FALSE;
6232         }
6233
6234         if (addressbook_get_target_compose() == compose)
6235                 addressbook_set_target_compose(compose);
6236 }
6237
6238 static void compose_toggle_replyto_cb(gpointer data, guint action,
6239                                       GtkWidget *widget)
6240 {
6241         Compose *compose = (Compose *)data;
6242
6243         if (GTK_CHECK_MENU_ITEM(widget)->active) {
6244                 gtk_widget_show(compose->reply_hbox);
6245                 gtk_widget_show(compose->reply_entry);
6246                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 5, 4);
6247                 compose->use_replyto = TRUE;
6248         } else {
6249                 gtk_widget_hide(compose->reply_hbox);
6250                 gtk_widget_hide(compose->reply_entry);
6251                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 5, 0);
6252                 gtk_widget_queue_resize(compose->table_vbox);
6253                 compose->use_replyto = FALSE;
6254         }
6255 }
6256
6257 static void compose_toggle_followupto_cb(gpointer data, guint action,
6258                                          GtkWidget *widget)
6259 {
6260         Compose *compose = (Compose *)data;
6261
6262         if (GTK_CHECK_MENU_ITEM(widget)->active) {
6263                 gtk_widget_show(compose->followup_hbox);
6264                 gtk_widget_show(compose->followup_entry);
6265                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 6, 4);
6266                 compose->use_followupto = TRUE;
6267         } else {
6268                 gtk_widget_hide(compose->followup_hbox);
6269                 gtk_widget_hide(compose->followup_entry);
6270                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 6, 0);
6271                 gtk_widget_queue_resize(compose->table_vbox);
6272                 compose->use_followupto = FALSE;
6273         }
6274 }
6275
6276 static void compose_toggle_attach_cb(gpointer data, guint action,
6277                                      GtkWidget *widget)
6278 {
6279         Compose *compose = (Compose *)data;
6280
6281         if (GTK_CHECK_MENU_ITEM(widget)->active) {
6282                 gtk_widget_ref(compose->edit_vbox);
6283
6284                 gtkut_container_remove(GTK_CONTAINER(compose->vbox2),
6285                                        compose->edit_vbox);
6286                 gtk_paned_add2(GTK_PANED(compose->paned), compose->edit_vbox);
6287                 gtk_box_pack_start(GTK_BOX(compose->vbox2), compose->paned,
6288                                    TRUE, TRUE, 0);
6289                 gtk_widget_show(compose->paned);
6290
6291                 gtk_widget_unref(compose->edit_vbox);
6292                 gtk_widget_unref(compose->paned);
6293
6294                 compose->use_attach = TRUE;
6295         } else {
6296                 gtk_widget_ref(compose->paned);
6297                 gtk_widget_ref(compose->edit_vbox);
6298
6299                 gtkut_container_remove(GTK_CONTAINER(compose->vbox2),
6300                                        compose->paned);
6301                 gtkut_container_remove(GTK_CONTAINER(compose->paned),
6302                                        compose->edit_vbox);
6303                 gtk_box_pack_start(GTK_BOX(compose->vbox2),
6304                                    compose->edit_vbox, TRUE, TRUE, 0);
6305
6306                 gtk_widget_unref(compose->edit_vbox);
6307
6308                 compose->use_attach = FALSE;
6309         }
6310 }
6311 #endif
6312
6313 #if USE_GPGME
6314 static void compose_toggle_sign_cb(gpointer data, guint action,
6315                                    GtkWidget *widget)
6316 {
6317         Compose *compose = (Compose *)data;
6318
6319         if (GTK_CHECK_MENU_ITEM(widget)->active)
6320                 compose->use_signing = TRUE;
6321         else
6322                 compose->use_signing = FALSE;
6323 }
6324
6325 static void compose_toggle_encrypt_cb(gpointer data, guint action,
6326                                       GtkWidget *widget)
6327 {
6328         Compose *compose = (Compose *)data;
6329
6330         if (GTK_CHECK_MENU_ITEM(widget)->active)
6331                 compose->use_encryption = TRUE;
6332         else
6333                 compose->use_encryption = FALSE;
6334 }
6335 #endif /* USE_GPGME */
6336
6337 static void compose_toggle_ruler_cb(gpointer data, guint action,
6338                                     GtkWidget *widget)
6339 {
6340         Compose *compose = (Compose *)data;
6341
6342         if (GTK_CHECK_MENU_ITEM(widget)->active) {
6343                 gtk_widget_show(compose->ruler_hbox);
6344                 prefs_common.show_ruler = TRUE;
6345         } else {
6346                 gtk_widget_hide(compose->ruler_hbox);
6347                 gtk_widget_queue_resize(compose->edit_vbox);
6348                 prefs_common.show_ruler = FALSE;
6349         }
6350 }
6351
6352 static void compose_attach_drag_received_cb (GtkWidget          *widget,
6353                                              GdkDragContext     *drag_context,
6354                                              gint                x,
6355                                              gint                y,
6356                                              GtkSelectionData   *data,
6357                                              guint               info,
6358                                              guint               time,
6359                                              gpointer            user_data)
6360 {
6361         Compose *compose = (Compose *)user_data;
6362         GList *list, *tmp;
6363
6364         list = uri_list_extract_filenames((const gchar *)data->data);
6365         for (tmp = list; tmp != NULL; tmp = tmp->next)
6366                 compose_attach_append
6367                         (compose, (const gchar *)tmp->data,
6368                          (const gchar *)tmp->data, NULL);
6369         if (list) compose_changed_cb(NULL, compose);
6370         list_free_strings(list);
6371         g_list_free(list);
6372 }
6373
6374 static void compose_insert_drag_received_cb (GtkWidget          *widget,
6375                                              GdkDragContext     *drag_context,
6376                                              gint                x,
6377                                              gint                y,
6378                                              GtkSelectionData   *data,
6379                                              guint               info,
6380                                              guint               time,
6381                                              gpointer            user_data)
6382 {
6383         Compose *compose = (Compose *)user_data;
6384         GList *list, *tmp;
6385
6386         list = uri_list_extract_filenames((const gchar *)data->data);
6387         for (tmp = list; tmp != NULL; tmp = tmp->next)
6388                 compose_insert_file(compose, (const gchar *)tmp->data);
6389         list_free_strings(list);
6390         g_list_free(list);
6391 }
6392
6393 #if 0 /* NEW COMPOSE GUI */
6394 static void to_activated(GtkWidget *widget, Compose *compose)
6395 {
6396         if (GTK_WIDGET_VISIBLE(compose->newsgroups_entry))
6397                 gtk_widget_grab_focus(compose->newsgroups_entry);
6398         else
6399                 gtk_widget_grab_focus(compose->subject_entry);
6400 }
6401
6402 static void newsgroups_activated(GtkWidget *widget, Compose *compose)
6403 {
6404         gtk_widget_grab_focus(compose->subject_entry);
6405 }
6406
6407 static void subject_activated(GtkWidget *widget, Compose *compose)
6408 {
6409         if (GTK_WIDGET_VISIBLE(compose->cc_entry))
6410                 gtk_widget_grab_focus(compose->cc_entry);
6411         else if (GTK_WIDGET_VISIBLE(compose->bcc_entry))
6412                 gtk_widget_grab_focus(compose->bcc_entry);
6413         else if (GTK_WIDGET_VISIBLE(compose->reply_entry))
6414                 gtk_widget_grab_focus(compose->reply_entry);
6415         else if (GTK_WIDGET_VISIBLE(compose->followup_entry))
6416                 gtk_widget_grab_focus(compose->followup_entry);
6417         else
6418                 gtk_widget_grab_focus(compose->text);
6419 }
6420
6421 static void cc_activated(GtkWidget *widget, Compose *compose)
6422 {
6423         if (GTK_WIDGET_VISIBLE(compose->bcc_entry))
6424                 gtk_widget_grab_focus(compose->bcc_entry);
6425         else if (GTK_WIDGET_VISIBLE(compose->reply_entry))
6426                 gtk_widget_grab_focus(compose->reply_entry);
6427         else if (GTK_WIDGET_VISIBLE(compose->followup_entry))
6428                 gtk_widget_grab_focus(compose->followup_entry);
6429         else
6430                 gtk_widget_grab_focus(compose->text);
6431 }
6432
6433 static void bcc_activated(GtkWidget *widget, Compose *compose)
6434 {
6435         if (GTK_WIDGET_VISIBLE(compose->reply_entry))
6436                 gtk_widget_grab_focus(compose->reply_entry);
6437         else if (GTK_WIDGET_VISIBLE(compose->followup_entry))
6438                 gtk_widget_grab_focus(compose->followup_entry);
6439         else
6440                 gtk_widget_grab_focus(compose->text);
6441 }
6442
6443 static void replyto_activated(GtkWidget *widget, Compose *compose)
6444 {
6445         if (GTK_WIDGET_VISIBLE(compose->followup_entry))
6446                 gtk_widget_grab_focus(compose->followup_entry);
6447         else
6448                 gtk_widget_grab_focus(compose->text);
6449 }
6450
6451 static void followupto_activated(GtkWidget *widget, Compose *compose)
6452 {
6453         gtk_widget_grab_focus(compose->text);
6454 }
6455 #endif
6456
6457 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
6458                                              GtkWidget *widget)
6459 {
6460         Compose *compose = (Compose *)data;
6461
6462         if (GTK_CHECK_MENU_ITEM(widget)->active)
6463                 compose->return_receipt = TRUE;
6464         else
6465                 compose->return_receipt = FALSE;
6466 }
6467
6468 void compose_headerentry_key_press_event_cb(GtkWidget *entry,
6469                                             GdkEventKey *event,
6470                                             ComposeHeaderEntry *headerentry)
6471 {
6472         if ((g_slist_length(headerentry->compose->header_list) > 0) &&
6473             ((headerentry->headernum + 1) != headerentry->compose->header_nextrow) &&
6474             !(event->state & GDK_MODIFIER_MASK) &&
6475             (event->keyval == GDK_BackSpace) &&
6476             (strlen(gtk_entry_get_text(GTK_ENTRY(entry))) == 0)) {
6477                 gtk_container_remove
6478                         (GTK_CONTAINER(headerentry->compose->header_table),
6479                          headerentry->combo);
6480                 gtk_container_remove
6481                         (GTK_CONTAINER(headerentry->compose->header_table),
6482                          headerentry->entry);
6483                 headerentry->compose->header_list =
6484                         g_slist_remove(headerentry->compose->header_list,
6485                                        headerentry);
6486                 g_free(headerentry);
6487         } else  if (event->keyval == GDK_Tab) {
6488                 if (headerentry->compose->header_last == headerentry) {
6489                         /* Override default next focus, and give it to subject_entry
6490                          * instead of notebook tabs
6491                          */
6492                         gtk_signal_emit_stop_by_name(GTK_OBJECT(entry), "key-press-event"); 
6493                         gtk_widget_grab_focus(headerentry->compose->subject_entry);
6494                 }
6495         }
6496
6497 }
6498
6499 void compose_headerentry_changed_cb(GtkWidget *entry,
6500                                     ComposeHeaderEntry *headerentry)
6501 {
6502         if (strlen(gtk_entry_get_text(GTK_ENTRY(entry))) != 0) {
6503                 headerentry->compose->header_list =
6504                         g_slist_append(headerentry->compose->header_list,
6505                                        headerentry);
6506                 compose_create_header_entry(headerentry->compose);
6507                 gtk_signal_disconnect_by_func
6508                         (GTK_OBJECT(entry),
6509                          GTK_SIGNAL_FUNC(compose_headerentry_changed_cb),
6510                          headerentry);
6511                 /* Automatically scroll down */
6512                 compose_show_first_last_header(headerentry->compose, FALSE);
6513                 
6514         }
6515 }
6516
6517 static void compose_show_first_last_header(Compose *compose, gboolean show_first)
6518 {
6519         GtkAdjustment *vadj;
6520
6521         g_return_if_fail(compose);
6522         g_return_if_fail(GTK_IS_WIDGET(compose->header_table));
6523         g_return_if_fail(GTK_IS_VIEWPORT(compose->header_table->parent));
6524
6525         vadj = gtk_viewport_get_vadjustment(GTK_VIEWPORT(compose->header_table->parent));
6526         gtk_adjustment_set_value(vadj, (show_first ? vadj->lower : vadj->upper));
6527 }
6528
6529 static void text_activated(GtkWidget *widget, Compose *compose)
6530 {
6531         compose_send_control_enter(compose);
6532 }
6533
6534 static gboolean compose_send_control_enter(Compose *compose)
6535 {
6536         GdkEvent *ev;
6537         GdkEventKey *kev;
6538         GtkItemFactory *ifactory;
6539         GtkAccelEntry *accel;
6540         GtkWidget *send_menu;
6541         GSList *list;
6542
6543         ev = gtk_get_current_event();
6544         if (ev->type != GDK_KEY_PRESS) return FALSE;
6545
6546         kev = (GdkEventKey *)ev;
6547         if (!(kev->keyval == GDK_Return && (kev->state & GDK_CONTROL_MASK)))
6548                 return FALSE;
6549
6550         ifactory = gtk_item_factory_from_widget(compose->menubar);
6551         send_menu = gtk_item_factory_get_widget(ifactory, "/Message/Send");
6552         list = gtk_accel_group_entries_from_object(GTK_OBJECT(send_menu));
6553         accel = (GtkAccelEntry *)list->data;
6554         if (accel->accelerator_key == kev->keyval &&
6555             accel->accelerator_mods == kev->state) {
6556                 compose_send_cb(compose, 0, NULL);
6557                 return TRUE;
6558         }
6559
6560         return FALSE;
6561 }
6562
6563 #if USE_PSPELL
6564 static void compose_check_all(Compose *compose)
6565 {
6566         if (compose->gtkpspell)
6567                 gtkpspell_check_all(compose->gtkpspell);
6568 }
6569
6570 static void compose_highlight_all(Compose *compose)
6571 {
6572         if (compose->gtkpspell)
6573                 gtkpspell_highlight_all(compose->gtkpspell);
6574 }
6575
6576 static void compose_check_backwards(Compose *compose)
6577 {
6578         if (compose->gtkpspell) 
6579                 gtkpspell_check_backwards(compose->gtkpspell);
6580         else {
6581                 GtkItemFactory *ifactory;
6582                 ifactory = gtk_item_factory_from_widget(compose->popupmenu);
6583                 menu_set_sensitive(ifactory, "/Edit/Check backwards misspelled word", FALSE);
6584                 menu_set_sensitive(ifactory, "/Edit/Forward to next misspelled word", FALSE);
6585         }
6586 }
6587
6588 static void compose_check_forwards_go(Compose *compose)
6589 {
6590         if (compose->gtkpspell) 
6591                 gtkpspell_check_forwards_go(compose->gtkpspell);
6592         else {
6593                 GtkItemFactory *ifactory;
6594                 ifactory = gtk_item_factory_from_widget(compose->popupmenu);
6595                 menu_set_sensitive(ifactory, "/Edit/Check backwards misspelled word", FALSE);
6596                 menu_set_sensitive(ifactory, "/Edit/Forward to next misspelled word", FALSE);
6597         }
6598 }
6599 #endif
6600
6601 static void set_toolbar_style(Compose *compose)
6602 {
6603         switch (prefs_common.toolbar_style) {
6604         case TOOLBAR_NONE:
6605                 gtk_widget_hide(compose->handlebox);
6606                 break;
6607         case TOOLBAR_ICON:
6608                 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
6609                                       GTK_TOOLBAR_ICONS);
6610                 break;
6611         case TOOLBAR_TEXT:
6612                 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
6613                                       GTK_TOOLBAR_TEXT);
6614                 break;
6615         case TOOLBAR_BOTH:
6616                 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
6617                                       GTK_TOOLBAR_BOTH);
6618                 break;
6619         }
6620         
6621         if (prefs_common.toolbar_style != TOOLBAR_NONE) {
6622                 gtk_widget_show(compose->handlebox);
6623                 gtk_widget_queue_resize(compose->handlebox);
6624         }
6625 }