gettextise some forgotten strings
[claws.git] / src / compose.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 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 #include <libgen.h>
66
67 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
68 #  include <wchar.h>
69 #  include <wctype.h>
70 #endif
71
72 #include "intl.h"
73 #include "main.h"
74 #include "mainwindow.h"
75 #include "compose.h"
76 #include "gtkstext.h"
77 #include "addressbook.h"
78 #include "folderview.h"
79 #include "procmsg.h"
80 #include "menu.h"
81 #include "stock_pixmap.h"
82 #include "send_message.h"
83 #include "imap.h"
84 #include "news.h"
85 #include "customheader.h"
86 #include "prefs_common.h"
87 #include "prefs_account.h"
88 #include "action.h"
89 #include "account.h"
90 #include "filesel.h"
91 #include "procheader.h"
92 #include "procmime.h"
93 #include "statusbar.h"
94 #include "about.h"
95 #include "base64.h"
96 #include "quoted-printable.h"
97 #include "codeconv.h"
98 #include "utils.h"
99 #include "gtkutils.h"
100 #include "socket.h"
101 #include "alertpanel.h"
102 #include "manage_window.h"
103 #include "gtkshruler.h"
104 #include "folder.h"
105 #include "addr_compl.h"
106 #include "quote_fmt.h"
107 #include "template.h"
108 #include "undo.h"
109 #include "foldersel.h"
110 #include "toolbar.h"
111
112 #if USE_GPGME
113 #  include "rfc2015.h"
114 #endif
115
116 typedef enum
117 {
118         COL_MIMETYPE = 0,
119         COL_SIZE     = 1,
120         COL_NAME     = 2
121 } AttachColumnPos;
122
123 #define N_ATTACH_COLS           3
124
125 typedef enum
126 {
127         COMPOSE_CALL_GTK_STEXT_MOVE_BEGINNING_OF_LINE,
128         COMPOSE_CALL_GTK_STEXT_MOVE_FORWARD_CHARACTER,
129         COMPOSE_CALL_GTK_STEXT_MOVE_BACKWARD_CHARACTER,
130         COMPOSE_CALL_GTK_STEXT_MOVE_FORWARD_WORD,
131         COMPOSE_CALL_GTK_STEXT_MOVE_BACKWARD_WORD,
132         COMPOSE_CALL_GTK_STEXT_MOVE_END_OF_LINE,
133         COMPOSE_CALL_GTK_STEXT_MOVE_NEXT_LINE,
134         COMPOSE_CALL_GTK_STEXT_MOVE_PREVIOUS_LINE,
135         COMPOSE_CALL_GTK_STEXT_DELETE_FORWARD_CHARACTER,
136         COMPOSE_CALL_GTK_STEXT_DELETE_BACKWARD_CHARACTER,
137         COMPOSE_CALL_GTK_STEXT_DELETE_FORWARD_WORD,
138         COMPOSE_CALL_GTK_STEXT_DELETE_BACKWARD_WORD,
139         COMPOSE_CALL_GTK_STEXT_DELETE_LINE,
140         COMPOSE_CALL_GTK_STEXT_DELETE_LINE_N,
141         COMPOSE_CALL_GTK_STEXT_DELETE_TO_LINE_END
142 } ComposeCallGtkSTextAction;
143
144 typedef enum
145 {
146         PRIORITY_HIGHEST = 1,
147         PRIORITY_HIGH,
148         PRIORITY_NORMAL,
149         PRIORITY_LOW,
150         PRIORITY_LOWEST
151 } PriorityLevel;
152
153 typedef enum
154 {
155         COMPOSE_INSERT_SUCCESS,
156         COMPOSE_INSERT_READ_ERROR,
157         COMPOSE_INSERT_INVALID_CHARACTER,
158         COMPOSE_INSERT_NO_FILE
159 } ComposeInsertResult;
160
161 #define B64_LINE_SIZE           57
162 #define B64_BUFFSIZE            77
163
164 #define MAX_REFERENCES_LEN      999
165
166 static GdkColor quote_color = {0, 0, 0, 0xbfff};
167
168 static GList *compose_list = NULL;
169
170 Compose *compose_generic_new                    (PrefsAccount   *account,
171                                                  const gchar    *to,
172                                                  FolderItem     *item,
173                                                  GPtrArray      *attach_files,
174                                                  GList          *listAddress );
175
176 static Compose *compose_create                  (PrefsAccount   *account,
177                                                  ComposeMode     mode);
178
179 static GtkWidget *compose_account_option_menu_create
180                                                 (Compose        *compose);
181 static void compose_set_template_menu           (Compose        *compose);
182 static void compose_template_apply              (Compose        *compose,
183                                                  Template       *tmpl,
184                                                  gboolean        replace);
185 static void compose_destroy                     (Compose        *compose);
186
187 static void compose_entries_set                 (Compose        *compose,
188                                                  const gchar    *mailto);
189 static gint compose_parse_header                (Compose        *compose,
190                                                  MsgInfo        *msginfo);
191 static gchar *compose_parse_references          (const gchar    *ref,
192                                                  const gchar    *msgid);
193
194 static gchar *compose_quote_fmt                 (Compose        *compose,
195                                                  MsgInfo        *msginfo,
196                                                  const gchar    *fmt,
197                                                  const gchar    *qmark,
198                                                  const gchar    *body);
199
200 static void compose_reply_set_entry             (Compose        *compose,
201                                                  MsgInfo        *msginfo,
202                                                  gboolean        to_all,
203                                                  gboolean        to_ml,
204                                                  gboolean        to_sender,
205                                                  gboolean
206                                                  followup_and_reply_to);
207 static void compose_reedit_set_entry            (Compose        *compose,
208                                                  MsgInfo        *msginfo);
209 static void compose_insert_sig                  (Compose        *compose,
210                                                  gboolean        replace);
211 static gchar *compose_get_signature_str         (Compose        *compose);
212 static ComposeInsertResult compose_insert_file  (Compose        *compose,
213                                                  const gchar    *file);
214 static void compose_attach_append               (Compose        *compose,
215                                                  const gchar    *file,
216                                                  const gchar    *type,
217                                                  const gchar    *content_type);
218 static void compose_attach_parts                (Compose        *compose,
219                                                  MsgInfo        *msginfo);
220 static void compose_wrap_line                   (Compose        *compose);
221 static void compose_wrap_line_all               (Compose        *compose);
222 static void compose_wrap_line_all_full          (Compose        *compose,
223                                                  gboolean        autowrap);
224 static void compose_set_title                   (Compose        *compose);
225 static void compose_select_account              (Compose        *compose,
226                                                  PrefsAccount   *account,
227                                                  gboolean        init);
228
229 static PrefsAccount *compose_current_mail_account(void);
230 /* static gint compose_send                     (Compose        *compose); */
231 static gboolean compose_check_for_valid_recipient
232                                                 (Compose        *compose);
233 static gboolean compose_check_entries           (Compose        *compose,
234                                                  gboolean       check_subject);
235 static gint compose_write_to_file               (Compose        *compose,
236                                                  const gchar    *file,
237                                                  gboolean        is_draft);
238 static gint compose_write_body_to_file          (Compose        *compose,
239                                                  const gchar    *file);
240 static gint compose_remove_reedit_target        (Compose        *compose);
241 void compose_remove_draft                       (Compose        *compose);
242 static gint compose_queue                       (Compose        *compose,
243                                                  gint           *msgnum,
244                                                  FolderItem     **item);
245 static gint compose_queue_sub                   (Compose        *compose,
246                                                  gint           *msgnum,
247                                                  FolderItem     **item,
248                                                  gboolean       check_subject);
249 static void compose_write_attach                (Compose        *compose,
250                                                  FILE           *fp);
251 static gint compose_write_headers               (Compose        *compose,
252                                                  FILE           *fp,
253                                                  const gchar    *charset,
254                                                  EncodingType    encoding,
255                                                  gboolean        is_draft);
256
257 static void compose_convert_header              (gchar          *dest,
258                                                  gint            len,
259                                                  gchar          *src,
260                                                  gint            header_len,
261                                                  gboolean        addr_field);
262 static void compose_generate_msgid              (gchar          *buf,
263                                                  gint            len);
264
265 static void compose_attach_info_free            (AttachInfo     *ainfo);
266 static void compose_attach_remove_selected      (Compose        *compose);
267
268 static void compose_attach_property             (Compose        *compose);
269 static void compose_attach_property_create      (gboolean       *cancelled);
270 static void attach_property_ok                  (GtkWidget      *widget,
271                                                  gboolean       *cancelled);
272 static void attach_property_cancel              (GtkWidget      *widget,
273                                                  gboolean       *cancelled);
274 static gint attach_property_delete_event        (GtkWidget      *widget,
275                                                  GdkEventAny    *event,
276                                                  gboolean       *cancelled);
277 static void attach_property_key_pressed         (GtkWidget      *widget,
278                                                  GdkEventKey    *event,
279                                                  gboolean       *cancelled);
280
281 static void compose_exec_ext_editor             (Compose           *compose);
282 static gint compose_exec_ext_editor_real        (const gchar       *file);
283 static gboolean compose_ext_editor_kill         (Compose           *compose);
284 static void compose_input_cb                    (gpointer           data,
285                                                  gint               source,
286                                                  GdkInputCondition  condition);
287 static void compose_set_ext_editor_sensitive    (Compose           *compose,
288                                                  gboolean           sensitive);
289
290 static void compose_undo_state_changed          (UndoMain       *undostruct,
291                                                  gint            undo_state,
292                                                  gint            redo_state,
293                                                  gpointer        data);
294
295 static gint calc_cursor_xpos    (GtkSText       *text,
296                                  gint            extra,
297                                  gint            char_width);
298
299 static void compose_create_header_entry (Compose *compose);
300 static void compose_add_header_entry    (Compose *compose, gchar *header, gchar *text);
301 static void compose_update_priority_menu_item(Compose * compose);
302
303 static void compose_add_field_list      ( Compose *compose,
304                                           GList *listAddress );
305
306 /* callback functions */
307
308 static gboolean compose_edit_size_alloc (GtkEditable    *widget,
309                                          GtkAllocation  *allocation,
310                                          GtkSHRuler     *shruler);
311 static void account_activated           (GtkMenuItem    *menuitem,
312                                          gpointer        data);
313 static void attach_selected             (GtkCList       *clist,
314                                          gint            row,
315                                          gint            column,
316                                          GdkEvent       *event,
317                                          gpointer        data);
318 static void attach_button_pressed       (GtkWidget      *widget,
319                                          GdkEventButton *event,
320                                          gpointer        data);
321 static void attach_key_pressed          (GtkWidget      *widget,
322                                          GdkEventKey    *event,
323                                          gpointer        data);
324
325 static void compose_send_cb             (gpointer        data,
326                                          guint           action,
327                                          GtkWidget      *widget);
328 static void compose_send_later_cb       (gpointer        data,
329                                          guint           action,
330                                          GtkWidget      *widget);
331
332 static void compose_draft_cb            (gpointer        data,
333                                          guint           action,
334                                          GtkWidget      *widget);
335
336 static void compose_attach_cb           (gpointer        data,
337                                          guint           action,
338                                          GtkWidget      *widget);
339 static void compose_insert_file_cb      (gpointer        data,
340                                          guint           action,
341                                          GtkWidget      *widget);
342 static void compose_insert_sig_cb       (gpointer        data,
343                                          guint           action,
344                                          GtkWidget      *widget);
345
346 static void compose_close_cb            (gpointer        data,
347                                          guint           action,
348                                          GtkWidget      *widget);
349
350 static void compose_address_cb          (gpointer        data,
351                                          guint           action,
352                                          GtkWidget      *widget);
353 static void compose_template_activate_cb(GtkWidget      *widget,
354                                          gpointer        data);
355
356 static void compose_ext_editor_cb       (gpointer        data,
357                                          guint           action,
358                                          GtkWidget      *widget);
359
360 static gint compose_delete_cb           (GtkWidget      *widget,
361                                          GdkEventAny    *event,
362                                          gpointer        data);
363 static void compose_destroy_cb          (GtkWidget      *widget,
364                                          Compose        *compose);
365
366 static void compose_undo_cb             (Compose        *compose);
367 static void compose_redo_cb             (Compose        *compose);
368 static void compose_cut_cb              (Compose        *compose);
369 static void compose_copy_cb             (Compose        *compose);
370 static void compose_paste_cb            (Compose        *compose);
371 static void compose_paste_as_quote_cb   (Compose        *compose);
372 static void compose_allsel_cb           (Compose        *compose);
373
374 static void compose_gtk_stext_action_cb (Compose                   *compose,
375                                          ComposeCallGtkSTextAction  action);
376
377 static void compose_grab_focus_cb       (GtkWidget      *widget,
378                                          Compose        *compose);
379
380 static void compose_changed_cb          (GtkEditable    *editable,
381                                          Compose        *compose);
382 static void compose_button_press_cb     (GtkWidget      *widget,
383                                          GdkEventButton *event,
384                                          Compose        *compose);
385 #if 0
386 static void compose_key_press_cb        (GtkWidget      *widget,
387                                          GdkEventKey    *event,
388                                          Compose        *compose);
389 #endif
390
391 #if 0
392 static void compose_toggle_to_cb        (gpointer        data,
393                                          guint           action,
394                                          GtkWidget      *widget);
395 static void compose_toggle_cc_cb        (gpointer        data,
396                                          guint           action,
397                                          GtkWidget      *widget);
398 static void compose_toggle_bcc_cb       (gpointer        data,
399                                          guint           action,
400                                          GtkWidget      *widget);
401 static void compose_toggle_replyto_cb   (gpointer        data,
402                                          guint           action,
403                                          GtkWidget      *widget);
404 static void compose_toggle_followupto_cb(gpointer        data,
405                                          guint           action,
406                                          GtkWidget      *widget);
407 static void compose_toggle_attach_cb    (gpointer        data,
408                                          guint           action,
409                                          GtkWidget      *widget);
410 #endif
411 static void compose_toggle_ruler_cb     (gpointer        data,
412                                          guint           action,
413                                          GtkWidget      *widget);
414 #if USE_GPGME
415 static void compose_toggle_sign_cb      (gpointer        data,
416                                          guint           action,
417                                          GtkWidget      *widget);
418 static void compose_toggle_encrypt_cb   (gpointer        data,
419                                          guint           action,
420                                          GtkWidget      *widget);
421 static void compose_set_gnupg_mode_cb   (gpointer        data,
422                                          guint           action,
423                                          GtkWidget      *widget);
424 static void compose_update_gnupg_mode_menu_item(Compose * compose);
425 static void activate_gnupg_mode         (Compose *compose, 
426                                          PrefsAccount *account);
427 #endif
428 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
429                                              GtkWidget *widget);
430 static void compose_toggle_remove_refs_cb(gpointer data, guint action,
431                                              GtkWidget *widget);
432 static void compose_set_priority_cb     (gpointer        data,
433                                          guint           action,
434                                          GtkWidget      *widget);
435
436 static void compose_attach_drag_received_cb (GtkWidget          *widget,
437                                              GdkDragContext     *drag_context,
438                                              gint                x,
439                                              gint                y,
440                                              GtkSelectionData   *data,
441                                              guint               info,
442                                              guint               time,
443                                              gpointer            user_data);
444 static void compose_insert_drag_received_cb (GtkWidget          *widget,
445                                              GdkDragContext     *drag_context,
446                                              gint                x,
447                                              gint                y,
448                                              GtkSelectionData   *data,
449                                              guint               info,
450                                              guint               time,
451                                              gpointer            user_data);
452
453 #if 0
454 static void to_activated                (GtkWidget      *widget,
455                                          Compose        *compose);
456 static void newsgroups_activated        (GtkWidget      *widget,
457                                          Compose        *compose);
458 static void cc_activated                (GtkWidget      *widget,
459                                          Compose        *compose);
460 static void bcc_activated               (GtkWidget      *widget,
461                                          Compose        *compose);
462 static void replyto_activated           (GtkWidget      *widget,
463                                          Compose        *compose);
464 static void followupto_activated        (GtkWidget      *widget,
465                                          Compose        *compose);
466 static void subject_activated           (GtkWidget      *widget,
467                                          Compose        *compose);
468 #endif
469
470 static void text_activated              (GtkWidget      *widget,
471                                          Compose        *compose);
472 static void text_inserted               (GtkWidget      *widget,
473                                          const gchar    *text,
474                                          gint            length,
475                                          gint           *position,
476                                          Compose        *compose);
477 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
478                                   gboolean to_all, gboolean to_ml,
479                                   gboolean to_sender,
480                                   gboolean followup_and_reply_to,
481                                   const gchar *body);
482
483 void compose_headerentry_changed_cb        (GtkWidget          *entry,
484                                             ComposeHeaderEntry *headerentry);
485 void compose_headerentry_key_press_event_cb(GtkWidget          *entry,
486                                             GdkEventKey        *event,
487                                             ComposeHeaderEntry *headerentry);
488 static gboolean compose_headerentry_button_pressed (GtkWidget *entry, 
489                                                     GdkEventButton *event,
490                                                     gpointer data);
491
492 static void compose_show_first_last_header (Compose *compose, gboolean show_first);
493
494 #if USE_ASPELL
495 static void compose_check_all              (Compose *compose);
496 static void compose_highlight_all          (Compose *compose);
497 static void compose_check_backwards        (Compose *compose);
498 static void compose_check_forwards_go      (Compose *compose);
499 #endif
500
501 static gboolean compose_send_control_enter      (Compose        *compose);
502 static gint compose_defer_auto_save_draft       (Compose        *compose);
503
504 static GtkItemFactoryEntry compose_popup_entries[] =
505 {
506         {N_("/_Add..."),        NULL, compose_attach_cb, 0, NULL},
507         {N_("/_Remove"),        NULL, compose_attach_remove_selected, 0, NULL},
508         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
509         {N_("/_Properties..."), NULL, compose_attach_property, 0, NULL}
510 };
511
512 static GtkItemFactoryEntry compose_entries[] =
513 {
514         {N_("/_File"),                          NULL, NULL, 0, "<Branch>"},
515         {N_("/_File/_Attach file"),             "<control>M", compose_attach_cb,      0, NULL},
516         {N_("/_File/_Insert file"),             "<control>I", compose_insert_file_cb, 0, NULL},
517         {N_("/_File/Insert si_gnature"),        "<control>G", compose_insert_sig_cb,  0, NULL},
518         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
519         {N_("/_File/_Close"),                   "<control>W", compose_close_cb, 0, NULL},
520
521         {N_("/_Edit"),                  NULL, NULL, 0, "<Branch>"},
522         {N_("/_Edit/_Undo"),            "<control>Z", compose_undo_cb, 0, NULL},
523         {N_("/_Edit/_Redo"),            "<control>Y", compose_redo_cb, 0, NULL},
524         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
525         {N_("/_Edit/Cu_t"),             "<control>X", compose_cut_cb,    0, NULL},
526         {N_("/_Edit/_Copy"),            "<control>C", compose_copy_cb,   0, NULL},
527         {N_("/_Edit/_Paste"),           "<control>V", compose_paste_cb,  0, NULL},
528         {N_("/_Edit/Paste as _quotation"),
529                                         NULL, compose_paste_as_quote_cb, 0, NULL},
530         {N_("/_Edit/Select _all"),      "<control>A", compose_allsel_cb, 0, NULL},
531         {N_("/_Edit/A_dvanced"),        NULL, NULL, 0, "<Branch>"},
532         {N_("/_Edit/A_dvanced/Move a character backward"),
533                                         "<control>B",
534                                         compose_gtk_stext_action_cb,
535                                         COMPOSE_CALL_GTK_STEXT_MOVE_BACKWARD_CHARACTER,
536                                         NULL},
537         {N_("/_Edit/A_dvanced/Move a character forward"),
538                                         "<control>F",
539                                         compose_gtk_stext_action_cb,
540                                         COMPOSE_CALL_GTK_STEXT_MOVE_FORWARD_CHARACTER,
541                                         NULL},
542         {N_("/_Edit/A_dvanced/Move a word backward"),
543                                         NULL, /* "<alt>B" */
544                                         compose_gtk_stext_action_cb,
545                                         COMPOSE_CALL_GTK_STEXT_MOVE_BACKWARD_WORD,
546                                         NULL},
547         {N_("/_Edit/A_dvanced/Move a word forward"),
548                                         NULL, /* "<alt>F" */
549                                         compose_gtk_stext_action_cb,
550                                         COMPOSE_CALL_GTK_STEXT_MOVE_FORWARD_WORD,
551                                         NULL},
552         {N_("/_Edit/A_dvanced/Move to beginning of line"),
553                                         NULL, /* "<control>A" */
554                                         compose_gtk_stext_action_cb,
555                                         COMPOSE_CALL_GTK_STEXT_MOVE_BEGINNING_OF_LINE,
556                                         NULL},
557         {N_("/_Edit/A_dvanced/Move to end of line"),
558                                         "<control>E",
559                                         compose_gtk_stext_action_cb,
560                                         COMPOSE_CALL_GTK_STEXT_MOVE_END_OF_LINE,
561                                         NULL},
562         {N_("/_Edit/A_dvanced/Move to previous line"),
563                                         "<control>P",
564                                         compose_gtk_stext_action_cb,
565                                         COMPOSE_CALL_GTK_STEXT_MOVE_PREVIOUS_LINE,
566                                         NULL},
567         {N_("/_Edit/A_dvanced/Move to next line"),
568                                         "<control>N",
569                                         compose_gtk_stext_action_cb,
570                                         COMPOSE_CALL_GTK_STEXT_MOVE_NEXT_LINE,
571                                         NULL},
572         {N_("/_Edit/A_dvanced/Delete a character backward"),
573                                         "<control>H",
574                                         compose_gtk_stext_action_cb,
575                                         COMPOSE_CALL_GTK_STEXT_DELETE_BACKWARD_CHARACTER,
576                                         NULL},
577         {N_("/_Edit/A_dvanced/Delete a character forward"),
578                                         "<control>D",
579                                         compose_gtk_stext_action_cb,
580                                         COMPOSE_CALL_GTK_STEXT_DELETE_FORWARD_CHARACTER,
581                                         NULL},
582         {N_("/_Edit/A_dvanced/Delete a word backward"),
583                                         NULL, /* "<control>W" */
584                                         compose_gtk_stext_action_cb,
585                                         COMPOSE_CALL_GTK_STEXT_DELETE_BACKWARD_WORD,
586                                         NULL},
587         {N_("/_Edit/A_dvanced/Delete a word forward"),
588                                         NULL, /* "<alt>D", */
589                                         compose_gtk_stext_action_cb,
590                                         COMPOSE_CALL_GTK_STEXT_DELETE_FORWARD_WORD,
591                                         NULL},
592         {N_("/_Edit/A_dvanced/Delete line"),
593                                         "<control>U",
594                                         compose_gtk_stext_action_cb,
595                                         COMPOSE_CALL_GTK_STEXT_DELETE_LINE,
596                                         NULL},
597         {N_("/_Edit/A_dvanced/Delete entire line"),
598                                         NULL,
599                                         compose_gtk_stext_action_cb,
600                                         COMPOSE_CALL_GTK_STEXT_DELETE_LINE_N,
601                                         NULL},
602         {N_("/_Edit/A_dvanced/Delete to end of line"),
603                                         "<control>K",
604                                         compose_gtk_stext_action_cb,
605                                         COMPOSE_CALL_GTK_STEXT_DELETE_TO_LINE_END,
606                                         NULL},
607         {N_("/_Edit/---"),              NULL, NULL, 0, "<Separator>"},
608         {N_("/_Edit/_Wrap current paragraph"),
609                                         "<control>L", compose_wrap_line, 0, NULL},
610         {N_("/_Edit/Wrap all long _lines"),
611                                         "<control><alt>L", compose_wrap_line_all, 0, NULL},
612         {N_("/_Edit/Edit with e_xternal editor"),
613                                         "<shift><control>X", compose_ext_editor_cb, 0, NULL},
614 #if USE_ASPELL
615         {N_("/_Spelling"),              NULL, NULL, 0, "<Branch>"},
616         {N_("/_Spelling/_Check all or check selection"),
617                                         NULL, compose_check_all, 0, NULL},
618         {N_("/_Spelling/_Highlight all misspelled words"),
619                                         NULL, compose_highlight_all, 0, NULL},
620         {N_("/_Spelling/Check _backwards misspelled word"),
621                                         NULL, compose_check_backwards , 0, NULL},
622         {N_("/_Spelling/_Forward to next misspelled word"),
623                                         NULL, compose_check_forwards_go, 0, NULL},
624         {N_("/_Spelling/---"),          NULL, NULL, 0, "<Separator>"},
625         {N_("/_Spelling/_Spelling Configuration"),
626                                         NULL, NULL, 0, "<Branch>"},
627 #endif
628 #if 0 /* NEW COMPOSE GUI */
629         {N_("/_View"),                  NULL, NULL, 0, "<Branch>"},
630         {N_("/_View/_To"),              NULL, compose_toggle_to_cb     , 0, "<ToggleItem>"},
631         {N_("/_View/_Cc"),              NULL, compose_toggle_cc_cb     , 0, "<ToggleItem>"},
632         {N_("/_View/_Bcc"),             NULL, compose_toggle_bcc_cb    , 0, "<ToggleItem>"},
633         {N_("/_View/_Reply to"),        NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
634         {N_("/_View/---"),              NULL, NULL, 0, "<Separator>"},
635         {N_("/_View/_Followup to"),     NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
636         {N_("/_View/---"),              NULL, NULL, 0, "<Separator>"},
637         {N_("/_View/R_uler"),           NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
638         {N_("/_View/---"),              NULL, NULL, 0, "<Separator>"},
639         {N_("/_View/_Attachment"),      NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
640 #endif
641         {N_("/_Message"),               NULL, NULL, 0, "<Branch>"},
642         {N_("/_Message/_Send"),         "<control>Return",
643                                         compose_send_cb, 0, NULL},
644         {N_("/_Message/Send _later"),   "<shift><control>S",
645                                         compose_send_later_cb,  0, NULL},
646         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
647         {N_("/_Message/Save to _draft folder"),
648                                         "<shift><control>D", compose_draft_cb, 0, NULL},
649         {N_("/_Message/Save and _keep editing"),
650                                         "<control>S", compose_draft_cb, 1, NULL},
651 #if 0 /* NEW COMPOSE GUI */
652         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
653         {N_("/_Message/_To"),           NULL, compose_toggle_to_cb     , 0, "<ToggleItem>"},
654         {N_("/_Message/_Cc"),           NULL, compose_toggle_cc_cb     , 0, "<ToggleItem>"},
655         {N_("/_Message/_Bcc"),          NULL, compose_toggle_bcc_cb    , 0, "<ToggleItem>"},
656         {N_("/_Message/_Reply to"),     NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
657         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
658         {N_("/_Message/_Followup to"),  NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
659         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
660         {N_("/_Message/_Attach"),       NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
661 #endif
662 #if USE_GPGME
663         {N_("/_Message/---"),           NULL, NULL, 0, "<Separator>"},
664         {N_("/_Message/Si_gn"),         NULL, compose_toggle_sign_cb   , 0, "<ToggleItem>"},
665         {N_("/_Message/_Encrypt"),      NULL, compose_toggle_encrypt_cb, 0, "<ToggleItem>"},
666         {N_("/_Message/Mode"),          NULL, NULL,   0, "<Branch>"},
667         {N_("/_Message/Mode/MIME"),     NULL, compose_set_gnupg_mode_cb,   GNUPG_MODE_DETACH, "<RadioItem>"},   
668         {N_("/_Message/Mode/Inline"),   NULL, compose_set_gnupg_mode_cb,   GNUPG_MODE_INLINE, "/Message/Mode/MIME"},    
669 #endif /* USE_GPGME */
670         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
671         {N_("/_Message/_Priority"),     NULL,           NULL,   0, "<Branch>"},
672         {N_("/_Message/Priority/_Highest"), NULL, compose_set_priority_cb, PRIORITY_HIGHEST, "<RadioItem>"},
673         {N_("/_Message/Priority/Hi_gh"),    NULL, compose_set_priority_cb, PRIORITY_HIGH, "/Message/Priority/Highest"},
674         {N_("/_Message/Priority/_Normal"),  NULL, compose_set_priority_cb, PRIORITY_NORMAL, "/Message/Priority/Highest"},
675         {N_("/_Message/Priority/Lo_w"),    NULL, compose_set_priority_cb, PRIORITY_LOW, "/Message/Priority/Highest"},
676         {N_("/_Message/Priority/_Lowest"),  NULL, compose_set_priority_cb, PRIORITY_LOWEST, "/Message/Priority/Highest"},
677         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
678         {N_("/_Message/_Request Return Receipt"),       NULL, compose_toggle_return_receipt_cb, 0, "<ToggleItem>"},
679         {N_("/_Message/Remo_ve references"),    NULL, compose_toggle_remove_refs_cb, 0, "<ToggleItem>"},
680         {N_("/_Tools"),                 NULL, NULL, 0, "<Branch>"},
681         {N_("/_Tools/Show _ruler"),     NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
682         {N_("/_Tools/_Address book"),   "<shift><control>A", compose_address_cb , 0, NULL},
683         {N_("/_Tools/_Template"),       NULL, NULL, 0, "<Branch>"},
684         {N_("/_Tools/Actio_ns"),        NULL, NULL, 0, "<Branch>"},
685         {N_("/_Help"),                  NULL, NULL, 0, "<Branch>"},
686         {N_("/_Help/_About"),           NULL, about_show, 0, NULL}
687 };
688
689 static GtkTargetEntry compose_mime_types[] =
690 {
691         {"text/uri-list", 0, 0}
692 };
693
694 Compose *compose_new(PrefsAccount *account, const gchar *mailto,
695                      GPtrArray *attach_files)
696 {
697         return compose_generic_new(account, mailto, NULL, attach_files, NULL);
698 }
699
700 Compose *compose_new_with_folderitem(PrefsAccount *account, FolderItem *item)
701 {
702         return compose_generic_new(account, NULL, item, NULL, NULL);
703 }
704
705 Compose *compose_new_with_list( PrefsAccount *account, GList *listAddress )
706 {
707         return compose_generic_new( account, NULL, NULL, NULL, listAddress );
708 }
709
710 Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderItem *item,
711                              GPtrArray *attach_files, GList *listAddress )
712 {
713         Compose *compose;
714         GtkSText *text;
715         GtkItemFactory *ifactory;
716
717         if (item && item->prefs && item->prefs->enable_default_account)
718                 account = account_find_from_id(item->prefs->default_account);
719
720         if (!account) account = cur_account;
721         g_return_val_if_fail(account != NULL, NULL);
722
723         compose = compose_create(account, COMPOSE_NEW);
724         ifactory = gtk_item_factory_from_widget(compose->menubar);
725
726         compose->replyinfo = NULL;
727         compose->fwdinfo   = NULL;
728
729         text = GTK_STEXT(compose->text);
730         gtk_stext_freeze(text);
731
732 #ifdef USE_ASPELL
733         if (item && item->prefs && item->prefs->enable_default_dictionary &&
734             compose->gtkaspell) 
735                 gtkaspell_change_dict(compose->gtkaspell, 
736                     item->prefs->default_dictionary);
737 #endif
738
739         if (account->auto_sig)
740                 compose_insert_sig(compose, FALSE);
741         gtk_editable_set_position(GTK_EDITABLE(text), 0);
742         gtk_stext_set_point(text, 0);
743
744         gtk_stext_thaw(text);
745
746         if (account->protocol != A_NNTP) {
747                 if (mailto && *mailto != '\0') {
748                         compose_entries_set(compose, mailto);
749
750                 } else if (item && item->prefs->enable_default_to) {
751                         compose_entry_append(compose, item->prefs->default_to, COMPOSE_TO);
752                         compose_entry_mark_default_to(compose, item->prefs->default_to);
753                 }
754                 if (item && item->ret_rcpt) {
755                         menu_set_toggle(ifactory, "/Message/Request Return Receipt", TRUE);
756                 }
757         } else {
758                 if (mailto) {
759                         compose_entry_append(compose, mailto, COMPOSE_NEWSGROUPS);
760                 }
761                 /*
762                  * CLAWS: just don't allow return receipt request, even if the user
763                  * may want to send an email. simple but foolproof.
764                  */
765                 menu_set_sensitive(ifactory, "/Message/Request Return Receipt", FALSE); 
766         }
767         compose_add_field_list( compose, listAddress );
768
769         if (attach_files) {
770                 gint i;
771                 gchar *file;
772
773                 for (i = 0; i < attach_files->len; i++) {
774                         file = g_ptr_array_index(attach_files, i);
775                         compose_attach_append(compose, file, file, NULL);
776                 }
777         }
778
779         compose_show_first_last_header(compose, TRUE);
780
781         /* Set save folder */
782         if (item && item->prefs && item->prefs->save_copy_to_folder) {
783                 gchar *folderidentifier;
784
785                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
786                 folderidentifier = folder_item_get_identifier(item);
787                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
788                 g_free(folderidentifier);
789         }
790         
791         gtk_widget_grab_focus(compose->header_last->entry);
792
793         if (prefs_common.auto_exteditor)
794                 compose_exec_ext_editor(compose);
795
796         return compose;
797 }
798
799 /*
800 Compose *compose_new_followup_and_replyto(PrefsAccount *account,
801                                            const gchar *followupto, gchar * to)
802 {
803         Compose *compose;
804
805         if (!account) account = cur_account;
806         g_return_val_if_fail(account != NULL, NULL);
807         g_return_val_if_fail(account->protocol != A_NNTP, NULL);
808
809         compose = compose_create(account, COMPOSE_NEW);
810
811         if (prefs_common.auto_sig)
812                 compose_insert_sig(compose);
813         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
814         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
815
816         compose_entry_append(compose, to, COMPOSE_TO);
817         compose_entry_append(compose, followupto, COMPOSE_NEWSGROUPS);
818         gtk_widget_grab_focus(compose->subject_entry);
819
820         return compose;
821 }
822 */
823
824 void compose_reply_mode(ComposeMode mode, GSList *msginfo_list, gchar *body)
825 {
826         MsgInfo *msginfo;
827         guint list_len;
828
829         g_return_if_fail(msginfo_list != NULL);
830
831         msginfo = (MsgInfo*)g_slist_nth_data(msginfo_list, 0);
832         g_return_if_fail(msginfo != NULL);
833
834         list_len = g_slist_length(msginfo_list);
835
836         switch (mode) {
837         case COMPOSE_REPLY:
838                 compose_reply(msginfo, prefs_common.reply_with_quote,
839                               FALSE, prefs_common.default_reply_list, FALSE, body);
840                 break;
841         case COMPOSE_REPLY_WITH_QUOTE:
842                 compose_reply(msginfo, TRUE, FALSE, prefs_common.default_reply_list, FALSE, body);
843                 break;
844         case COMPOSE_REPLY_WITHOUT_QUOTE:
845                 compose_reply(msginfo, FALSE, FALSE, prefs_common.default_reply_list, FALSE, NULL);
846                 break;
847         case COMPOSE_REPLY_TO_SENDER:
848                 compose_reply(msginfo, prefs_common.reply_with_quote,
849                               FALSE, FALSE, TRUE, body);
850                 break;
851         case COMPOSE_FOLLOWUP_AND_REPLY_TO:
852                 compose_followup_and_reply_to(msginfo,
853                                               prefs_common.reply_with_quote,
854                                               FALSE, FALSE, body);
855                 break;
856         case COMPOSE_REPLY_TO_SENDER_WITH_QUOTE:
857                 compose_reply(msginfo, TRUE, FALSE, FALSE, TRUE, body);
858                 break;
859         case COMPOSE_REPLY_TO_SENDER_WITHOUT_QUOTE:
860                 compose_reply(msginfo, FALSE, FALSE, FALSE, TRUE, NULL);
861                 break;
862         case COMPOSE_REPLY_TO_ALL:
863                 compose_reply(msginfo, prefs_common.reply_with_quote,
864                               TRUE, FALSE, FALSE, body);
865                 break;
866         case COMPOSE_REPLY_TO_ALL_WITH_QUOTE:
867                 compose_reply(msginfo, TRUE, TRUE, FALSE, FALSE, body);
868                 break;
869         case COMPOSE_REPLY_TO_ALL_WITHOUT_QUOTE:
870                 compose_reply(msginfo, FALSE, TRUE, FALSE, FALSE, NULL);
871                 break;
872         case COMPOSE_REPLY_TO_LIST:
873                 compose_reply(msginfo, prefs_common.reply_with_quote,
874                               FALSE, TRUE, FALSE, body);
875                 break;
876         case COMPOSE_REPLY_TO_LIST_WITH_QUOTE:
877                 compose_reply(msginfo, TRUE, FALSE, TRUE, FALSE, body);
878                 break;
879         case COMPOSE_REPLY_TO_LIST_WITHOUT_QUOTE:
880                 compose_reply(msginfo, FALSE, FALSE, TRUE, FALSE, NULL);
881                 break;
882         case COMPOSE_FORWARD:
883                 if (prefs_common.forward_as_attachment) {
884                         compose_reply_mode(COMPOSE_FORWARD_AS_ATTACH, msginfo_list, body);
885                         return;
886                 } else {
887                         compose_reply_mode(COMPOSE_FORWARD_INLINE, msginfo_list, body);
888                         return;
889                 }
890                 break;
891         case COMPOSE_FORWARD_INLINE:
892                 /* check if we reply to more than one Message */
893                 if (list_len == 1) {
894                         compose_forward(NULL, msginfo, FALSE, body, FALSE);
895                         break;
896                 } 
897                 /* more messages FALL THROUGH */
898         case COMPOSE_FORWARD_AS_ATTACH:
899                 compose_forward_multiple(NULL, msginfo_list);
900                 break;
901         case COMPOSE_REDIRECT:
902                 compose_redirect(NULL, msginfo);
903                 break;
904         default:
905                 g_warning("compose_reply(): invalid Compose Mode: %d\n", mode);
906         }
907 }
908
909 void compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
910                    gboolean to_ml, gboolean to_sender, 
911                    const gchar *body)
912 {
913         compose_generic_reply(msginfo, quote, to_all, to_ml, 
914                               to_sender, FALSE, body);
915 }
916
917 void compose_followup_and_reply_to(MsgInfo *msginfo, gboolean quote,
918                                    gboolean to_all,
919                                    gboolean to_sender,
920                                    const gchar *body)
921 {
922         compose_generic_reply(msginfo, quote, to_all, FALSE, 
923                               to_sender, TRUE, body);
924 }
925
926 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
927                                   gboolean to_all, gboolean to_ml,
928                                   gboolean to_sender,
929                                   gboolean followup_and_reply_to,
930                                   const gchar *body)
931 {
932         GtkItemFactory *ifactory;
933         Compose *compose;
934         PrefsAccount *account = NULL;
935         PrefsAccount *reply_account;
936         GtkSText *text;
937
938         g_return_if_fail(msginfo != NULL);
939         g_return_if_fail(msginfo->folder != NULL);
940
941         account = account_get_reply_account(msginfo, prefs_common.reply_account_autosel);
942         
943         g_return_if_fail(account != NULL);
944
945         if (to_sender && account->protocol == A_NNTP &&
946             !followup_and_reply_to) {
947                 reply_account =
948                         account_find_from_address(account->address);
949                 if (!reply_account)
950                         reply_account = compose_current_mail_account();
951                 if (!reply_account)
952                         return;
953         } else
954                 reply_account = account;
955
956         compose = compose_create(account, COMPOSE_REPLY);
957         ifactory = gtk_item_factory_from_widget(compose->menubar);
958
959         menu_set_toggle(ifactory, "/Message/Remove references", FALSE);
960         menu_set_sensitive(ifactory, "/Message/Remove references", TRUE);
961
962         compose->replyinfo = procmsg_msginfo_get_full_info(msginfo);
963         if (!compose->replyinfo)
964                 compose->replyinfo = procmsg_msginfo_copy(msginfo);
965
966         if (msginfo->folder && msginfo->folder->ret_rcpt)
967                 menu_set_toggle(ifactory, "/Message/Request Return Receipt", TRUE);
968
969         /* Set save folder */
970         if (msginfo->folder && msginfo->folder->prefs && msginfo->folder->prefs->save_copy_to_folder) {
971                 gchar *folderidentifier;
972
973                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
974                 folderidentifier = folder_item_get_identifier(msginfo->folder);
975                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
976                 g_free(folderidentifier);
977         }
978
979         if (compose_parse_header(compose, msginfo) < 0) return;
980         compose_reply_set_entry(compose, msginfo, to_all, to_ml, 
981                                 to_sender, followup_and_reply_to);
982         compose_show_first_last_header(compose, TRUE);
983
984         text = GTK_STEXT(compose->text);
985         gtk_stext_freeze(text);
986
987 #ifdef USE_ASPELL
988         if (msginfo->folder && msginfo->folder->prefs && 
989             msginfo->folder->prefs && 
990             msginfo->folder->prefs->enable_default_dictionary &&
991             compose->gtkaspell)
992                 gtkaspell_change_dict(compose->gtkaspell, 
993                     msginfo->folder->prefs->default_dictionary);
994 #endif
995
996         if (quote) {
997                 gchar *qmark;
998
999                 if (prefs_common.quotemark && *prefs_common.quotemark)
1000                         qmark = prefs_common.quotemark;
1001                 else
1002                         qmark = "> ";
1003
1004                 compose_quote_fmt(compose, compose->replyinfo,
1005                                   prefs_common.quotefmt,
1006                                   qmark, body);
1007         }
1008
1009         if (account->auto_sig)
1010                 compose_insert_sig(compose, FALSE);
1011
1012         /* Must thaw here, otherwise the GtkSTextEdit will become confused 
1013          * when positioning cursor */
1014         gtk_stext_thaw(text);
1015
1016         gtk_editable_set_position
1017                 (GTK_EDITABLE(text), quote_fmt_get_cursor_pos());
1018         gtk_stext_set_point(text, quote_fmt_get_cursor_pos());
1019
1020         if (quote && prefs_common.linewrap_quote) {
1021                 gtk_stext_freeze(text);
1022                 compose_wrap_line_all(compose);
1023                 gtk_stext_thaw(text);
1024         }
1025
1026         gtk_widget_grab_focus(compose->text);
1027
1028         if (prefs_common.auto_exteditor)
1029                 compose_exec_ext_editor(compose);
1030 }
1031
1032 #define INSERT_FW_HEADER(var, hdr) \
1033 if (msginfo->var && *msginfo->var) { \
1034         gtk_stext_insert(text, NULL, NULL, NULL, hdr, -1); \
1035         gtk_stext_insert(text, NULL, NULL, NULL, msginfo->var, -1); \
1036         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1); \
1037 }
1038
1039 Compose *compose_forward(PrefsAccount *account, MsgInfo *msginfo,
1040                          gboolean as_attach, const gchar *body,
1041                          gboolean no_extedit)
1042 {
1043         Compose *compose;
1044         GtkSText *text;
1045
1046         g_return_val_if_fail(msginfo != NULL, NULL);
1047         g_return_val_if_fail(msginfo->folder != NULL, NULL);
1048
1049         if (msginfo->folder->prefs->enable_default_account)
1050                 account = account_find_from_id(msginfo->folder->prefs->default_account);
1051         if (!account) 
1052                 account = msginfo->folder->folder->account;
1053         if (!account && msginfo->to && prefs_common.forward_account_autosel) {
1054                 gchar *to;
1055                 Xstrdup_a(to, msginfo->to, return NULL);
1056                 extract_address(to);
1057                 account = account_find_from_address(to);
1058         }
1059
1060         if (!account && prefs_common.forward_account_autosel) {
1061                 gchar cc[BUFFSIZE];
1062                 if (!procheader_get_header_from_msginfo(msginfo,cc,sizeof(cc),"CC:")){ /* Found a CC header */
1063                         extract_address(cc);
1064                         account = account_find_from_address(cc);
1065                 }
1066         }
1067
1068         if (account == NULL) {
1069                 account = cur_account;
1070                 /*
1071                 account = msginfo->folder->folder->account;
1072                 if (!account) account = cur_account;
1073                 */
1074         }
1075         g_return_val_if_fail(account != NULL, NULL);
1076
1077         compose = compose_create(account, COMPOSE_FORWARD);
1078
1079         compose->fwdinfo = procmsg_msginfo_get_full_info(msginfo);
1080         if (!compose->fwdinfo)
1081                 compose->fwdinfo = procmsg_msginfo_copy(msginfo);
1082
1083         if (msginfo->subject && *msginfo->subject) {
1084                 gchar *buf, *buf2, *p;
1085
1086                 buf = p = g_strdup(msginfo->subject);
1087                 p += subject_get_prefix_length(p);
1088                 memmove(buf, p, strlen(p) + 1);
1089
1090                 buf2 = g_strdup_printf("Fw: %s", buf);
1091                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1092                 
1093                 g_free(buf);
1094                 g_free(buf2);
1095         }
1096
1097         text = GTK_STEXT(compose->text);
1098         gtk_stext_freeze(text);
1099
1100         if (as_attach) {
1101                 gchar *msgfile;
1102
1103                 msgfile = procmsg_get_message_file_path(msginfo);
1104                 if (!is_file_exist(msgfile))
1105                         g_warning("%s: file not exist\n", msgfile);
1106                 else
1107                         compose_attach_append(compose, msgfile, msgfile,
1108                                               "message/rfc822");
1109
1110                 g_free(msgfile);
1111         } else {
1112                 gchar *qmark;
1113                 MsgInfo *full_msginfo;
1114
1115                 full_msginfo = procmsg_msginfo_get_full_info(msginfo);
1116                 if (!full_msginfo)
1117                         full_msginfo = procmsg_msginfo_copy(msginfo);
1118
1119                 if (prefs_common.fw_quotemark &&
1120                     *prefs_common.fw_quotemark)
1121                         qmark = prefs_common.fw_quotemark;
1122                 else
1123                         qmark = "> ";
1124
1125                 compose_quote_fmt(compose, full_msginfo,
1126                                   prefs_common.fw_quotefmt,
1127                                   qmark, body);
1128                 compose_attach_parts(compose, msginfo);
1129
1130                 procmsg_msginfo_free(full_msginfo);
1131         }
1132
1133         if (account->auto_sig)
1134                 compose_insert_sig(compose, FALSE);
1135
1136         if (prefs_common.linewrap_quote)
1137                 compose_wrap_line_all(compose);
1138
1139         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
1140         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
1141
1142         gtk_stext_thaw(text);
1143 #if 0 /* NEW COMPOSE GUI */
1144         if (account->protocol != A_NNTP)
1145                 gtk_widget_grab_focus(compose->to_entry);
1146         else
1147                 gtk_widget_grab_focus(compose->newsgroups_entry);
1148 #endif
1149         gtk_widget_grab_focus(compose->header_last->entry);
1150
1151         if (!no_extedit && prefs_common.auto_exteditor)
1152                 compose_exec_ext_editor(compose);
1153         
1154         /*save folder*/
1155         if (msginfo->folder && msginfo->folder->prefs && msginfo->folder->prefs->save_copy_to_folder) {
1156                 gchar *folderidentifier;
1157
1158                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1159                 folderidentifier = folder_item_get_identifier(msginfo->folder);
1160                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
1161                 g_free(folderidentifier);
1162         }
1163
1164         return compose;
1165 }
1166
1167 #undef INSERT_FW_HEADER
1168
1169 Compose *compose_forward_multiple(PrefsAccount *account, GSList *msginfo_list)
1170 {
1171         Compose *compose;
1172         GtkSText *text;
1173         GSList *msginfo;
1174         gchar *msgfile;
1175
1176         g_return_val_if_fail(msginfo_list != NULL, NULL);
1177         
1178         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1179                 if ( ((MsgInfo *)msginfo->data)->folder == NULL )
1180                         return NULL;
1181         }
1182
1183         if (account == NULL) {
1184                 account = cur_account;
1185                 /*
1186                 account = msginfo->folder->folder->account;
1187                 if (!account) account = cur_account;
1188                 */
1189         }
1190         g_return_val_if_fail(account != NULL, NULL);
1191
1192         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1193                 MSG_UNSET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_REPLIED);
1194                 MSG_SET_PERM_FLAGS(((MsgInfo *)msginfo->data)->flags, MSG_FORWARDED);
1195         }
1196
1197         compose = compose_create(account, COMPOSE_FORWARD);
1198
1199         text = GTK_STEXT(compose->text);
1200         gtk_stext_freeze(text);
1201
1202         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
1203                 msgfile = procmsg_get_message_file_path((MsgInfo *)msginfo->data);
1204                 if (!is_file_exist(msgfile))
1205                         g_warning("%s: file not exist\n", msgfile);
1206                 else
1207                         compose_attach_append(compose, msgfile, msgfile,
1208                                 "message/rfc822");
1209                 g_free(msgfile);
1210         }
1211
1212         if (account->auto_sig)
1213                 compose_insert_sig(compose, FALSE);
1214
1215         if (prefs_common.linewrap_quote)
1216                 compose_wrap_line_all(compose);
1217
1218         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
1219         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
1220
1221         gtk_stext_thaw(text);
1222         gtk_widget_grab_focus(compose->header_last->entry);
1223         
1224 #if 0 /* NEW COMPOSE GUI */
1225         if (account->protocol != A_NNTP)
1226                 gtk_widget_grab_focus(compose->to_entry);
1227         else
1228                 gtk_widget_grab_focus(compose->newsgroups_entry);
1229 #endif
1230
1231         return compose;
1232 }
1233
1234 void compose_reedit(MsgInfo *msginfo)
1235 {
1236         Compose *compose;
1237         PrefsAccount *account = NULL;
1238         GtkSText *text;
1239         FILE *fp;
1240         gchar buf[BUFFSIZE];
1241
1242         g_return_if_fail(msginfo != NULL);
1243         g_return_if_fail(msginfo->folder != NULL);
1244
1245         if (msginfo->folder->stype == F_QUEUE || msginfo->folder->stype == F_DRAFT) {
1246                 gchar queueheader_buf[BUFFSIZE];
1247                 gint id;
1248
1249                 /* Select Account from queue headers */
1250                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1251                                              sizeof(queueheader_buf), "X-Sylpheed-Account-Id:")) {
1252                         id = atoi(&queueheader_buf[22]);
1253                         account = account_find_from_id(id);
1254                 }
1255                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1256                                              sizeof(queueheader_buf), "NAID:")) {
1257                         id = atoi(&queueheader_buf[5]);
1258                         account = account_find_from_id(id);
1259                 }
1260                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1261                                                     sizeof(queueheader_buf), "MAID:")) {
1262                         id = atoi(&queueheader_buf[5]);
1263                         account = account_find_from_id(id);
1264                 }
1265                 if (!account && !procheader_get_header_from_msginfo(msginfo, queueheader_buf, 
1266                                                                 sizeof(queueheader_buf), "S:")) {
1267                         account = account_find_from_address(queueheader_buf);
1268                 }
1269         } else 
1270                 account = msginfo->folder->folder->account;
1271
1272         if (!account && prefs_common.reedit_account_autosel) {
1273                 gchar from[BUFFSIZE];
1274                 if (!procheader_get_header_from_msginfo(msginfo, from, sizeof(from), "FROM:")){
1275                         extract_address(from);
1276                         account = account_find_from_address(from);
1277                 }
1278         }
1279         if (!account) account = cur_account;
1280         g_return_if_fail(account != NULL);
1281
1282         compose = compose_create(account, COMPOSE_REEDIT);
1283         compose->targetinfo = procmsg_msginfo_copy(msginfo);
1284
1285         if (msginfo->folder->stype == F_QUEUE
1286         ||  msginfo->folder->stype == F_DRAFT) {
1287                 gchar queueheader_buf[BUFFSIZE];
1288
1289                 /* Set message save folder */
1290                 if (!procheader_get_header_from_msginfo(msginfo, queueheader_buf, sizeof(queueheader_buf), "SCF:")) {
1291                         gint startpos = 0;
1292
1293                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), TRUE);
1294                         gtk_editable_delete_text(GTK_EDITABLE(compose->savemsg_entry), 0, -1);
1295                         gtk_editable_insert_text(GTK_EDITABLE(compose->savemsg_entry), &queueheader_buf[4], strlen(&queueheader_buf[4]), &startpos);
1296                 }
1297         }
1298         
1299         if (compose_parse_header(compose, msginfo) < 0) return;
1300         compose_reedit_set_entry(compose, msginfo);
1301
1302         text = GTK_STEXT(compose->text);
1303         gtk_stext_freeze(text);
1304
1305         if ((fp = procmime_get_first_text_content(msginfo)) == NULL)
1306                 g_warning("Can't get text part\n");
1307         else {
1308                 while (fgets(buf, sizeof(buf), fp) != NULL) {
1309                         strcrchomp(buf);
1310                         gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1311                 }
1312                 fclose(fp);
1313         }
1314         compose_attach_parts(compose, msginfo);
1315
1316         gtk_stext_thaw(text);
1317         gtk_widget_grab_focus(compose->text);
1318
1319         if (prefs_common.auto_exteditor)
1320                 compose_exec_ext_editor(compose);
1321 }
1322
1323 Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo)
1324 {
1325         Compose *compose;
1326         gchar *filename;
1327         GtkItemFactory *ifactory;
1328         FolderItem *item;
1329
1330         g_return_val_if_fail(msginfo != NULL, NULL);
1331
1332         if (!account)
1333                 account = account_get_reply_account(msginfo,
1334                                         prefs_common.reply_account_autosel);
1335         g_return_val_if_fail(account != NULL, NULL);
1336
1337         compose = compose_create(account, COMPOSE_REDIRECT);
1338         ifactory = gtk_item_factory_from_widget(compose->menubar);
1339
1340         compose->replyinfo = NULL;
1341         compose->fwdinfo = NULL;
1342
1343         compose_show_first_last_header(compose, TRUE);
1344
1345         gtk_widget_grab_focus(compose->header_last->entry);
1346
1347         filename = procmsg_get_message_file(msginfo);
1348         if (filename == NULL)
1349                 return NULL;
1350
1351         compose->redirect_filename = filename;
1352         
1353         /* Set save folder */
1354         item = msginfo->folder;
1355         if (item && item->prefs && item->prefs->save_copy_to_folder) {
1356                 gchar *folderidentifier;
1357
1358                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compose->savemsg_checkbtn), prefs_common.savemsg);
1359                 folderidentifier = folder_item_get_identifier(item);
1360                 gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
1361                 g_free(folderidentifier);
1362         }
1363
1364         compose_attach_parts(compose, msginfo);
1365
1366         if (msginfo->subject)
1367                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry),
1368                                    msginfo->subject);
1369         gtk_editable_set_editable(GTK_EDITABLE(compose->subject_entry), FALSE);
1370
1371         gtk_stext_freeze(GTK_STEXT(compose->text));
1372         compose_quote_fmt(compose, msginfo, "%M", NULL, NULL);
1373         gtk_editable_set_editable(GTK_EDITABLE(compose->text), FALSE);
1374         gtk_stext_thaw(GTK_STEXT(compose->text));
1375
1376         ifactory = gtk_item_factory_from_widget(compose->popupmenu);
1377         menu_set_sensitive(ifactory, "/Add...", FALSE);
1378         menu_set_sensitive(ifactory, "/Remove", FALSE);
1379         menu_set_sensitive(ifactory, "/Property...", FALSE);
1380
1381         ifactory = gtk_item_factory_from_widget(compose->menubar);
1382         menu_set_sensitive(ifactory, "/File/Insert file", FALSE);
1383         menu_set_sensitive(ifactory, "/File/Attach file", FALSE);
1384         menu_set_sensitive(ifactory, "/File/Insert signature", FALSE);
1385         menu_set_sensitive(ifactory, "/Edit", FALSE);
1386         menu_set_sensitive(ifactory, "/Message/Save to draft folder", FALSE);
1387         menu_set_sensitive(ifactory, "/Message/Save and keep editing", FALSE);
1388 #if USE_GPGME
1389         menu_set_sensitive(ifactory, "/Message/Sign", FALSE);
1390         menu_set_sensitive(ifactory, "/Message/Encrypt", FALSE);
1391         menu_set_sensitive(ifactory, "/Message/Mode/MIME", FALSE);
1392         menu_set_sensitive(ifactory, "/Message/Mode/Inline", FALSE);
1393 #endif
1394         menu_set_sensitive(ifactory, "/Message/Priority", FALSE);
1395         menu_set_sensitive(ifactory, "/Message/Request Return Receipt", FALSE);
1396         menu_set_sensitive(ifactory, "/Tools/Show ruler", FALSE);
1397         menu_set_sensitive(ifactory, "/Tools/Actions", FALSE);
1398         
1399         gtk_widget_set_sensitive(compose->toolbar->draft_btn, FALSE);
1400         gtk_widget_set_sensitive(compose->toolbar->insert_btn, FALSE);
1401         gtk_widget_set_sensitive(compose->toolbar->attach_btn, FALSE);
1402         gtk_widget_set_sensitive(compose->toolbar->sig_btn, FALSE);
1403         gtk_widget_set_sensitive(compose->toolbar->exteditor_btn, FALSE);
1404         gtk_widget_set_sensitive(compose->toolbar->linewrap_current_btn, FALSE);
1405         gtk_widget_set_sensitive(compose->toolbar->linewrap_all_btn, FALSE);
1406
1407         return compose;
1408 }
1409
1410 GList *compose_get_compose_list(void)
1411 {
1412         return compose_list;
1413 }
1414
1415 void compose_entry_append(Compose *compose, const gchar *address,
1416                           ComposeEntryType type)
1417 {
1418         gchar *header;
1419
1420         if (!address || *address == '\0') return;
1421
1422 #if 0 /* NEW COMPOSE GUI */
1423         switch (type) {
1424         case COMPOSE_CC:
1425                 entry = GTK_ENTRY(compose->cc_entry);
1426                 break;
1427         case COMPOSE_BCC:
1428                 entry = GTK_ENTRY(compose->bcc_entry);
1429                 break;
1430         case COMPOSE_NEWSGROUPS:
1431                 entry = GTK_ENTRY(compose->newsgroups_entry);
1432                 break;
1433         case COMPOSE_TO:
1434         default:
1435                 entry = GTK_ENTRY(compose->to_entry);
1436                 break;
1437         }
1438
1439         text = gtk_entry_get_text(entry);
1440         if (*text != '\0')
1441                 gtk_entry_append_text(entry, ", ");
1442         gtk_entry_append_text(entry, address);
1443 #endif
1444
1445         switch (type) {
1446         case COMPOSE_CC:
1447                 header = N_("Cc:");
1448                 break;
1449         case COMPOSE_BCC:
1450                 header = N_("Bcc:");
1451                 break;
1452         case COMPOSE_REPLYTO:
1453                 header = N_("Reply-To:");
1454                 break;
1455         case COMPOSE_NEWSGROUPS:
1456                 header = N_("Newsgroups:");
1457                 break;
1458         case COMPOSE_FOLLOWUPTO:
1459                 header = N_( "Followup-To:");
1460                 break;
1461         case COMPOSE_TO:
1462         default:
1463                 header = N_("To:");
1464                 break;
1465         }
1466         header = prefs_common.trans_hdr ? gettext(header) : header;
1467
1468         compose_add_header_entry(compose, header, (gchar *)address);
1469 }
1470
1471 void compose_entry_mark_default_to(Compose *compose, const gchar *mailto)
1472 {
1473         static GtkStyle *bold_style = NULL;
1474         static GdkColor bold_color;
1475         static GdkFont *bold_font = NULL;
1476         GSList *h_list;
1477         GtkEntry *entry;
1478                 
1479         for (h_list = compose->header_list; h_list != NULL; h_list = h_list->next) {
1480                 entry = GTK_ENTRY(((ComposeHeaderEntry *)h_list->data)->entry);
1481                 if (gtk_entry_get_text(entry) && 
1482                     !g_strcasecmp(gtk_entry_get_text(entry), mailto)) {
1483                         gtk_widget_ensure_style(GTK_WIDGET(entry));
1484                         if (!bold_style) {
1485                                 gtkut_convert_int_to_gdk_color
1486                                         (prefs_common.color_new, &bold_color);
1487                                 bold_style = gtk_style_copy(gtk_widget_get_style
1488                                         (GTK_WIDGET(entry)));
1489                                 if (!bold_font)
1490                                         bold_font = gtkut_font_load
1491                                                 (prefs_common.boldfont);
1492                                 if (bold_font)
1493                                         bold_style->font = bold_font;
1494                                 bold_style->fg[GTK_STATE_NORMAL] = bold_color;
1495                         }
1496                         gtk_widget_set_style(GTK_WIDGET(entry), bold_style);
1497                 }
1498         }
1499 }
1500
1501 void compose_toolbar_cb(gint action, gpointer data)
1502 {
1503         ToolbarItem *toolbar_item = (ToolbarItem*)data;
1504         Compose *compose = (Compose*)toolbar_item->parent;
1505         
1506         g_return_if_fail(compose != NULL);
1507
1508         switch(action) {
1509         case A_SEND:
1510                 compose_send_cb(compose, 0, NULL);
1511                 break;
1512         case A_SENDL:
1513                 compose_send_later_cb(compose, 0, NULL);
1514                 break;
1515         case A_DRAFT:
1516                 compose_draft_cb(compose, 0, NULL);
1517                 break;
1518         case A_INSERT:
1519                 compose_insert_file_cb(compose, 0, NULL);
1520                 break;
1521         case A_ATTACH:
1522                 compose_attach_cb(compose, 0, NULL);
1523                 break;
1524         case A_SIG:
1525                 compose_insert_sig(compose, FALSE);
1526                 break;
1527         case A_EXTEDITOR:
1528                 compose_ext_editor_cb(compose, 0, NULL);
1529                 break;
1530         case A_LINEWRAP_CURRENT:
1531                 compose_wrap_line(compose);
1532                 break;
1533         case A_LINEWRAP_ALL:
1534                 compose_wrap_line_all(compose);
1535                 break;
1536         case A_ADDRBOOK:
1537                 compose_address_cb(compose, 0, NULL);
1538                 break;
1539 #ifdef USE_ASPELL
1540         case A_CHECK_SPELLING:
1541                 compose_check_all(compose);
1542                 break;
1543 #endif
1544         default:
1545                 break;
1546         }
1547 }
1548
1549 static void compose_entries_set(Compose *compose, const gchar *mailto)
1550 {
1551         gchar *to = NULL;
1552         gchar *cc = NULL;
1553         gchar *bcc = NULL;
1554         gchar *subject = NULL;
1555         gchar *body = NULL;
1556
1557         scan_mailto_url(mailto, &to, &cc, &bcc, &subject, &body);
1558
1559         if (to)
1560                 compose_entry_append(compose, to, COMPOSE_TO);
1561         if (cc)
1562                 compose_entry_append(compose, cc, COMPOSE_CC);
1563         if (bcc)
1564                 compose_entry_append(compose, bcc, COMPOSE_BCC);
1565         if (subject)
1566                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), subject);
1567         if (body) {
1568                 gtk_stext_insert(GTK_STEXT(compose->text),
1569                                 NULL, NULL, NULL, body, -1);
1570                 gtk_stext_insert(GTK_STEXT(compose->text),
1571                                 NULL, NULL, NULL, "\n", 1);
1572         }
1573
1574         g_free(to);
1575         g_free(cc);
1576         g_free(bcc);
1577         g_free(subject);
1578         g_free(body);
1579 }
1580
1581 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
1582 {
1583         static HeaderEntry hentry[] = {{"Reply-To:",    NULL, TRUE},
1584                                        {"Cc:",          NULL, TRUE},
1585                                        {"References:",  NULL, FALSE},
1586                                        {"Bcc:",         NULL, TRUE},
1587                                        {"Newsgroups:",  NULL, TRUE},
1588                                        {"Followup-To:", NULL, TRUE},
1589                                        {"List-Post:",   NULL, FALSE},
1590                                        {"X-Priority:",  NULL, FALSE},
1591                                        {NULL,           NULL, FALSE}};
1592
1593         enum
1594         {
1595                 H_REPLY_TO      = 0,
1596                 H_CC            = 1,
1597                 H_REFERENCES    = 2,
1598                 H_BCC           = 3,
1599                 H_NEWSGROUPS    = 4,
1600                 H_FOLLOWUP_TO   = 5,
1601                 H_LIST_POST     = 6,
1602                 H_X_PRIORITY    = 7
1603         };
1604
1605         FILE *fp;
1606
1607         g_return_val_if_fail(msginfo != NULL, -1);
1608
1609         if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
1610         procheader_get_header_fields(fp, hentry);
1611         fclose(fp);
1612
1613         if (hentry[H_REPLY_TO].body != NULL) {
1614                 conv_unmime_header_overwrite(hentry[H_REPLY_TO].body);
1615                 compose->replyto = hentry[H_REPLY_TO].body;
1616                 hentry[H_REPLY_TO].body = NULL;
1617         }
1618         if (hentry[H_CC].body != NULL) {
1619                 conv_unmime_header_overwrite(hentry[H_CC].body);
1620                 compose->cc = hentry[H_CC].body;
1621                 hentry[H_CC].body = NULL;
1622         }
1623         if (hentry[H_REFERENCES].body != NULL) {
1624                 if (compose->mode == COMPOSE_REEDIT)
1625                         compose->references = hentry[H_REFERENCES].body;
1626                 else {
1627                         compose->references = compose_parse_references
1628                                 (hentry[H_REFERENCES].body, msginfo->msgid);
1629                         g_free(hentry[H_REFERENCES].body);
1630                 }
1631                 hentry[H_REFERENCES].body = NULL;
1632         }
1633         if (hentry[H_BCC].body != NULL) {
1634                 if (compose->mode == COMPOSE_REEDIT) {
1635                         conv_unmime_header_overwrite(hentry[H_BCC].body);
1636                         compose->bcc = hentry[H_BCC].body;
1637                 } else
1638                         g_free(hentry[H_BCC].body);
1639                 hentry[H_BCC].body = NULL;
1640         }
1641         if (hentry[H_NEWSGROUPS].body != NULL) {
1642                 conv_unmime_header_overwrite(hentry[H_NEWSGROUPS].body);
1643                 compose->newsgroups = hentry[H_NEWSGROUPS].body;
1644                 hentry[H_NEWSGROUPS].body = NULL;
1645         }
1646         if (hentry[H_FOLLOWUP_TO].body != NULL) {
1647                 conv_unmime_header_overwrite(hentry[H_FOLLOWUP_TO].body);
1648                 compose->followup_to = hentry[H_FOLLOWUP_TO].body;
1649                 hentry[H_FOLLOWUP_TO].body = NULL;
1650         }
1651         if (hentry[H_LIST_POST].body != NULL) {
1652                 gchar *to = NULL;
1653
1654                 extract_address(hentry[H_LIST_POST].body);
1655                 if (hentry[H_LIST_POST].body[0] != '\0') {
1656                         scan_mailto_url(hentry[H_LIST_POST].body,
1657                                         &to, NULL, NULL, NULL, NULL);
1658                         if (to) {
1659                                 g_free(compose->ml_post);
1660                                 compose->ml_post = to;
1661                         }
1662                 }
1663                 g_free(hentry[H_LIST_POST].body);
1664                 hentry[H_LIST_POST].body = NULL;
1665         }
1666
1667         /* CLAWS - X-Priority */
1668         if (compose->mode == COMPOSE_REEDIT)
1669                 if (hentry[H_X_PRIORITY].body != NULL) {
1670                         gint priority;
1671                         
1672                         priority = atoi(hentry[H_X_PRIORITY].body);
1673                         g_free(hentry[H_X_PRIORITY].body);
1674                         
1675                         hentry[H_X_PRIORITY].body = NULL;
1676                         
1677                         if (priority < PRIORITY_HIGHEST || 
1678                             priority > PRIORITY_LOWEST)
1679                                 priority = PRIORITY_NORMAL;
1680                         
1681                         compose->priority =  priority;
1682                 }
1683  
1684         if (compose->mode == COMPOSE_REEDIT && msginfo->inreplyto)
1685                 compose->inreplyto = g_strdup(msginfo->inreplyto);
1686         else if (compose->mode != COMPOSE_REEDIT &&
1687                  msginfo->msgid && *msginfo->msgid) {
1688                 compose->inreplyto = g_strdup(msginfo->msgid);
1689
1690                 if (!compose->references) {
1691                         if (msginfo->inreplyto && *msginfo->inreplyto)
1692                                 compose->references =
1693                                         g_strdup_printf("<%s>\n\t<%s>",
1694                                                         msginfo->inreplyto,
1695                                                         msginfo->msgid);
1696                         else
1697                                 compose->references =
1698                                         g_strconcat("<", msginfo->msgid, ">",
1699                                                     NULL);
1700                 }
1701         }
1702
1703         return 0;
1704 }
1705
1706 static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
1707 {
1708         GSList *ref_id_list, *cur;
1709         GString *new_ref;
1710         gchar *new_ref_str;
1711
1712         ref_id_list = references_list_append(NULL, ref);
1713         if (!ref_id_list) return NULL;
1714         if (msgid && *msgid)
1715                 ref_id_list = g_slist_append(ref_id_list, g_strdup(msgid));
1716
1717         for (;;) {
1718                 gint len = 0;
1719
1720                 for (cur = ref_id_list; cur != NULL; cur = cur->next)
1721                         /* "<" + Message-ID + ">" + CR+LF+TAB */
1722                         len += strlen((gchar *)cur->data) + 5;
1723
1724                 if (len > MAX_REFERENCES_LEN) {
1725                         /* remove second message-ID */
1726                         if (ref_id_list && ref_id_list->next &&
1727                             ref_id_list->next->next) {
1728                                 g_free(ref_id_list->next->data);
1729                                 ref_id_list = g_slist_remove
1730                                         (ref_id_list, ref_id_list->next->data);
1731                         } else {
1732                                 slist_free_strings(ref_id_list);
1733                                 g_slist_free(ref_id_list);
1734                                 return NULL;
1735                         }
1736                 } else
1737                         break;
1738         }
1739
1740         new_ref = g_string_new("");
1741         for (cur = ref_id_list; cur != NULL; cur = cur->next) {
1742                 if (new_ref->len > 0)
1743                         g_string_append(new_ref, "\n\t");
1744                 g_string_sprintfa(new_ref, "<%s>", (gchar *)cur->data);
1745         }
1746
1747         slist_free_strings(ref_id_list);
1748         g_slist_free(ref_id_list);
1749
1750         new_ref_str = new_ref->str;
1751         g_string_free(new_ref, FALSE);
1752
1753         return new_ref_str;
1754 }
1755
1756 static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
1757                                 const gchar *fmt, const gchar *qmark,
1758                                 const gchar *body)
1759 {
1760         GtkSText *text = GTK_STEXT(compose->text);
1761         static MsgInfo dummyinfo;
1762         gchar *quote_str = NULL;
1763         gchar *buf;
1764         gchar *p, *lastp;
1765         gint len;
1766         const gchar *trimmed_body = body;
1767         
1768         if (!msginfo)
1769                 msginfo = &dummyinfo;
1770
1771         if (qmark != NULL) {
1772                 quote_fmt_init(msginfo, NULL, NULL);
1773                 quote_fmt_scan_string(qmark);
1774                 quote_fmt_parse();
1775
1776                 buf = quote_fmt_get_buffer();
1777                 if (buf == NULL)
1778                         alertpanel_error(_("Quote mark format error."));
1779                 else
1780                         Xstrdup_a(quote_str, buf, return NULL)
1781         }
1782
1783         if (fmt && *fmt != '\0') {
1784                 while (trimmed_body && strlen(trimmed_body) > 1
1785                         && trimmed_body[0]=='\n')
1786                         *trimmed_body++;
1787
1788                 quote_fmt_init(msginfo, quote_str, trimmed_body);
1789                 quote_fmt_scan_string(fmt);
1790                 quote_fmt_parse();
1791
1792                 buf = quote_fmt_get_buffer();
1793                 if (buf == NULL) {
1794                         alertpanel_error(_("Message reply/forward format error."));
1795                         return NULL;
1796                 }
1797         } else
1798                 buf = "";
1799
1800         gtk_stext_freeze(text);
1801
1802         for (p = buf; *p != '\0'; ) {
1803                 lastp = strchr(p, '\n');
1804                 len = lastp ? lastp - p + 1 : -1;
1805                 gtk_stext_insert(text, NULL, NULL, NULL, p, len);
1806                 if (lastp)
1807                         p = lastp + 1;
1808                 else
1809                         break;
1810         }
1811
1812         gtk_stext_thaw(text);
1813
1814         return buf;
1815 }
1816
1817 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
1818                                     gboolean to_all, gboolean to_ml,
1819                                     gboolean to_sender,
1820                                     gboolean followup_and_reply_to)
1821 {
1822         GSList *cc_list = NULL;
1823         GSList *cur;
1824         gchar *from = NULL;
1825         gchar *replyto = NULL;
1826         GHashTable *to_table;
1827
1828         g_return_if_fail(compose->account != NULL);
1829         g_return_if_fail(msginfo != NULL);
1830
1831         if (compose->account->protocol != A_NNTP) {
1832                 if (!compose->replyto && to_ml && compose->ml_post
1833                     && !(msginfo->folder && msginfo->folder->prefs->enable_default_reply_to))
1834                         compose_entry_append(compose,
1835                                            compose->ml_post,
1836                                            COMPOSE_TO);
1837                 else if (!(to_all || to_sender)
1838                          && msginfo->folder
1839                          && msginfo->folder->prefs->enable_default_reply_to) {
1840                         compose_entry_append(compose,
1841                             msginfo->folder->prefs->default_reply_to,
1842                             COMPOSE_TO);
1843                 } else
1844                         compose_entry_append(compose,
1845                                  (compose->replyto && !to_sender)
1846                                   ? compose->replyto :
1847                                   msginfo->from ? msginfo->from : "",
1848                                   COMPOSE_TO);
1849         } else {
1850                 if (to_sender || (compose->followup_to && 
1851                         !strncmp(compose->followup_to, "poster", 6)))
1852                         compose_entry_append
1853                                 (compose, 
1854                                  (compose->replyto ? compose->replyto :
1855                                         msginfo->from ? msginfo->from : ""),
1856                                  COMPOSE_TO);
1857                                  
1858                 else if (followup_and_reply_to || to_all) {
1859                         compose_entry_append
1860                                 (compose,
1861                                  (compose->replyto ? compose->replyto :
1862                                  msginfo->from ? msginfo->from : ""),
1863                                  COMPOSE_TO);                           
1864                 
1865                         compose_entry_append
1866                                 (compose,
1867                                  compose->followup_to ? compose->followup_to :
1868                                  compose->newsgroups ? compose->newsgroups : "",
1869                                  COMPOSE_NEWSGROUPS);
1870                 } 
1871                 else 
1872                         compose_entry_append
1873                                 (compose,
1874                                  compose->followup_to ? compose->followup_to :
1875                                  compose->newsgroups ? compose->newsgroups : "",
1876                                  COMPOSE_NEWSGROUPS);
1877         }
1878
1879         if (msginfo->subject && *msginfo->subject) {
1880                 gchar *buf, *buf2;
1881                 guchar *p;
1882
1883                 buf = p = g_strdup(msginfo->subject);
1884                 p += subject_get_prefix_length(p);
1885                 memmove(buf, p, strlen(p) + 1);
1886
1887                 buf2 = g_strdup_printf("Re: %s", buf);
1888                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1889
1890                 g_free(buf2);
1891                 g_free(buf);
1892         } else
1893                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
1894
1895         if (to_ml && compose->ml_post) return;
1896         if (!to_all || compose->account->protocol == A_NNTP) return;
1897
1898         if (compose->replyto) {
1899                 Xstrdup_a(replyto, compose->replyto, return);
1900                 extract_address(replyto);
1901         }
1902         if (msginfo->from) {
1903                 Xstrdup_a(from, msginfo->from, return);
1904                 extract_address(from);
1905         }
1906
1907         if (replyto && from)
1908                 cc_list = address_list_append_with_comments(cc_list, from);
1909         if (to_all && msginfo->folder && 
1910             msginfo->folder->prefs->enable_default_reply_to)
1911                 cc_list = address_list_append_with_comments(cc_list,
1912                                 msginfo->folder->prefs->default_reply_to);
1913         cc_list = address_list_append_with_comments(cc_list, msginfo->to);
1914         cc_list = address_list_append_with_comments(cc_list, compose->cc);
1915
1916         to_table = g_hash_table_new(g_str_hash, g_str_equal);
1917         if (replyto)
1918                 g_hash_table_insert(to_table, g_strdup(replyto), GINT_TO_POINTER(1));
1919         if (compose->account)
1920                 g_hash_table_insert(to_table, g_strdup(compose->account->address),
1921                                     GINT_TO_POINTER(1));
1922
1923         /* remove address on To: and that of current account */
1924         for (cur = cc_list; cur != NULL; ) {
1925                 GSList *next = cur->next;
1926                 gchar *addr;
1927
1928                 addr = g_strdup(cur->data);
1929                 extract_address(addr);
1930
1931                 if (GPOINTER_TO_INT(g_hash_table_lookup(to_table, addr)) == 1)
1932                         cc_list = g_slist_remove(cc_list, cur->data);
1933                 else
1934                         g_hash_table_insert(to_table, addr, GINT_TO_POINTER(1));
1935
1936                 cur = next;
1937         }
1938         hash_free_strings(to_table);
1939         g_hash_table_destroy(to_table);
1940
1941         if (cc_list) {
1942                 for (cur = cc_list; cur != NULL; cur = cur->next)
1943                         compose_entry_append(compose, (gchar *)cur->data,
1944                                              COMPOSE_CC);
1945                 slist_free_strings(cc_list);
1946                 g_slist_free(cc_list);
1947         }
1948
1949 }
1950
1951 #define SET_ENTRY(entry, str) \
1952 { \
1953         if (str && *str) \
1954                 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
1955 }
1956
1957 #define SET_ADDRESS(type, str) \
1958 { \
1959         if (str && *str) \
1960                 compose_entry_append(compose, str, type); \
1961 }
1962
1963 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
1964 {
1965         g_return_if_fail(msginfo != NULL);
1966
1967         SET_ENTRY(subject_entry, msginfo->subject);
1968         SET_ADDRESS(COMPOSE_TO, msginfo->to);
1969         SET_ADDRESS(COMPOSE_CC, compose->cc);
1970         SET_ADDRESS(COMPOSE_BCC, compose->bcc);
1971         SET_ADDRESS(COMPOSE_REPLYTO, compose->replyto);
1972         SET_ADDRESS(COMPOSE_NEWSGROUPS, compose->newsgroups);
1973         SET_ADDRESS(COMPOSE_FOLLOWUPTO, compose->followup_to);
1974
1975         compose_update_priority_menu_item(compose);
1976 #if USE_GPGME   
1977         compose_update_gnupg_mode_menu_item(compose);
1978 #endif
1979         compose_show_first_last_header(compose, TRUE);
1980
1981 }
1982
1983 #undef SET_ENTRY
1984 #undef SET_ADDRESS
1985
1986 static void compose_insert_sig(Compose *compose, gboolean replace)
1987 {
1988         GtkSText *text = GTK_STEXT(compose->text);
1989         gint cur_pos;
1990         gint len;
1991
1992         g_return_if_fail(compose->account != NULL);
1993
1994         cur_pos = gtk_editable_get_position(GTK_EDITABLE(text));
1995
1996         gtk_stext_freeze(text);
1997
1998         if (replace) {
1999                 len = gtk_stext_get_length(text);
2000                 gtk_stext_set_point(text, len);
2001         }
2002
2003         if (replace && compose->sig_str) {
2004                 gint pos;
2005
2006                 if (compose->sig_str[0] == '\0')
2007                         pos = -1;
2008                 else
2009                         pos = gtkut_stext_find(text, 0, compose->sig_str, TRUE);
2010
2011                 if (pos != -1) {
2012                         len = get_mbs_len(compose->sig_str);
2013                         if (len >= 0) {
2014                                 gtk_stext_set_point(text, pos);
2015                                 gtk_stext_forward_delete(text, len);
2016                         }
2017                 }
2018         }
2019
2020         g_free(compose->sig_str);
2021         compose->sig_str = compose_get_signature_str(compose);
2022         if (!compose->sig_str || (replace && !compose->account->auto_sig))
2023                 compose->sig_str = g_strdup("");
2024
2025         gtk_stext_insert(text, NULL, NULL, NULL, compose->sig_str, -1);
2026
2027         gtk_stext_thaw(text);
2028
2029         if (cur_pos > gtk_stext_get_length(text))
2030                 cur_pos = gtk_stext_get_length(text);
2031
2032         gtk_editable_set_position(GTK_EDITABLE(text), cur_pos);
2033         gtk_stext_set_point(text, cur_pos);
2034 }
2035
2036 static gchar *compose_get_signature_str(Compose *compose)
2037 {
2038         gchar *sig_body = NULL;
2039         gchar *sig_str = NULL;
2040
2041         g_return_val_if_fail(compose->account != NULL, NULL);
2042
2043         if (!compose->account->sig_path)
2044                 return NULL;
2045
2046         if (compose->account->sig_type == SIG_FILE) {
2047                 if (!is_file_or_fifo_exist(compose->account->sig_path)) {
2048                         g_warning("can't open signature file: %s\n",
2049                                   compose->account->sig_path);
2050                         return NULL;
2051                 }
2052         }
2053
2054         if (compose->account->sig_type == SIG_COMMAND)
2055                 sig_body = get_command_output(compose->account->sig_path);
2056         else {
2057                 gchar *tmp;
2058
2059                 tmp = file_read_to_str(compose->account->sig_path);
2060                 if (!tmp)
2061                         return NULL;
2062                 sig_body = normalize_newlines(tmp);
2063                 g_free(tmp);
2064         }
2065
2066         if (compose->account->sig_sep)
2067                 sig_str = g_strconcat("\n\n", compose->account->sig_sep, "\n", sig_body,
2068                                       NULL);
2069         else
2070                 sig_str = g_strconcat("\n\n", sig_body, NULL);
2071
2072         g_free(sig_body);
2073         
2074         return sig_str;
2075 }
2076
2077 static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *file)
2078 {
2079         GtkSText *text = GTK_STEXT(compose->text);
2080         gchar buf[BUFFSIZE];
2081         gint len;
2082         FILE *fp;
2083         gboolean badtxt = FALSE;
2084
2085         g_return_val_if_fail(file != NULL, COMPOSE_INSERT_NO_FILE);
2086
2087         if ((fp = fopen(file, "rb")) == NULL) {
2088                 FILE_OP_ERROR(file, "fopen");
2089                 return COMPOSE_INSERT_READ_ERROR;
2090         }
2091
2092         gtk_stext_freeze(text);
2093
2094         while (fgets(buf, sizeof(buf), fp) != NULL) {
2095                 /* strip <CR> if DOS/Windows file,
2096                    replace <CR> with <LF> if Macintosh file. */
2097                 strcrchomp(buf);
2098                 len = strlen(buf);
2099                 if (len > 0 && buf[len - 1] != '\n') {
2100                         while (--len >= 0)
2101                                 if (buf[len] == '\r') buf[len] = '\n';
2102                 }
2103                 if (mbstowcs(NULL, buf, 0) == -1)
2104                         badtxt = TRUE;
2105                 gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
2106         }
2107
2108         gtk_stext_thaw(text);
2109
2110         fclose(fp);
2111
2112         if (badtxt)
2113                 return COMPOSE_INSERT_INVALID_CHARACTER;
2114         else 
2115                 return COMPOSE_INSERT_SUCCESS;
2116 }
2117
2118 static void compose_attach_append(Compose *compose, const gchar *file,
2119                                   const gchar *filename,
2120                                   const gchar *content_type)
2121 {
2122         AttachInfo *ainfo;
2123         gchar *text[N_ATTACH_COLS];
2124         FILE *fp;
2125         off_t size;
2126         gint row;
2127
2128         if (!is_file_exist(file)) {
2129                 g_warning("File %s doesn't exist\n", file);
2130                 return;
2131         }
2132         if ((size = get_file_size(file)) < 0) {
2133                 g_warning("Can't get file size of %s\n", file);
2134                 return;
2135         }
2136         if (size == 0) {
2137                 alertpanel_notice(_("File %s is empty."), file);
2138                 return;
2139         }
2140         if ((fp = fopen(file, "rb")) == NULL) {
2141                 alertpanel_error(_("Can't read %s."), file);
2142                 return;
2143         }
2144         fclose(fp);
2145
2146 #if 0 /* NEW COMPOSE GUI */
2147         if (!compose->use_attach) {
2148                 GtkItemFactory *ifactory;
2149                 GtkWidget *menuitem;
2150
2151                 ifactory = gtk_item_factory_from_widget(compose->menubar);
2152                 menuitem = gtk_item_factory_get_item(ifactory,
2153                                                      "/View/Attachment");
2154                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2155                                                TRUE);
2156         }
2157 #endif
2158         ainfo = g_new0(AttachInfo, 1);
2159         ainfo->file = g_strdup(file);
2160
2161         if (content_type) {
2162                 ainfo->content_type = g_strdup(content_type);
2163                 if (!strcasecmp(content_type, "message/rfc822")) {
2164                         MsgInfo *msginfo;
2165                         MsgFlags flags = {0, 0};
2166                         gchar *name;
2167
2168                         if (procmime_get_encoding_for_file(file) == ENC_7BIT)
2169                                 ainfo->encoding = ENC_7BIT;
2170                         else
2171                                 ainfo->encoding = ENC_8BIT;
2172
2173                         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
2174                         if (msginfo && msginfo->subject)
2175                                 name = msginfo->subject;
2176                         else
2177                                 name = g_basename(filename ? filename : file);
2178
2179                         ainfo->name = g_strdup_printf(_("Message: %s"), name);
2180
2181                         procmsg_msginfo_free(msginfo);
2182                 } else {
2183                         if (!g_strncasecmp(content_type, "text", 4))
2184                                 ainfo->encoding =
2185                                         procmime_get_encoding_for_file(file);
2186                         else
2187                                 ainfo->encoding = ENC_BASE64;
2188                         ainfo->name = g_strdup
2189                                 (g_basename(filename ? filename : file));
2190                 }
2191         } else {
2192                 ainfo->content_type = procmime_get_mime_type(file);
2193                 if (!ainfo->content_type) {
2194                         ainfo->content_type =
2195                                 g_strdup("application/octet-stream");
2196                         ainfo->encoding = ENC_BASE64;
2197                 } else if (!g_strncasecmp(ainfo->content_type, "text", 4))
2198                         ainfo->encoding = procmime_get_encoding_for_file(file);
2199                 else
2200                         ainfo->encoding = ENC_BASE64;
2201                 ainfo->name = g_strdup(g_basename(filename ? filename : file)); 
2202         }
2203         ainfo->size = size;
2204
2205         text[COL_MIMETYPE] = ainfo->content_type;
2206         text[COL_SIZE] = to_human_readable(size);
2207         text[COL_NAME] = ainfo->name;
2208
2209         row = gtk_clist_append(GTK_CLIST(compose->attach_clist), text);
2210         gtk_clist_set_row_data(GTK_CLIST(compose->attach_clist), row, ainfo);
2211 }
2212
2213 #ifdef USE_GPGME
2214 static void compose_use_signing(Compose *compose, gboolean use_signing)
2215 {
2216         GtkItemFactory *ifactory;
2217         GtkWidget *menuitem = NULL;
2218
2219         compose->use_signing = use_signing;
2220         ifactory = gtk_item_factory_from_widget(compose->menubar);
2221         menuitem = gtk_item_factory_get_item
2222                 (ifactory, "/Message/Sign");
2223         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), 
2224                                        use_signing);
2225         compose_update_gnupg_mode_menu_item(compose);
2226 }
2227 #endif /* USE_GPGME */
2228
2229 #define NEXT_PART_NOT_CHILD(info)  \
2230 {  \
2231         node = info->node;  \
2232         while (node->children)  \
2233                 node = g_node_last_child(node);  \
2234         info = procmime_mimeinfo_next((MimeInfo *)node->data);  \
2235 }
2236
2237 static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
2238 {
2239         MimeInfo *mimeinfo;
2240         MimeInfo *child;
2241         MimeInfo *firsttext = NULL;
2242         MimeInfo *encrypted = NULL;
2243         GNode    *node;
2244         gchar *outfile;
2245         const gchar *partname = NULL;
2246
2247         mimeinfo = procmime_scan_message(msginfo);
2248         if (!mimeinfo) return;
2249
2250         if (mimeinfo->node->children == NULL) {
2251                 procmime_mimeinfo_free_all(mimeinfo);
2252                 return;
2253         }
2254
2255         /* find first content part */
2256         child = (MimeInfo *) mimeinfo->node->children->data;
2257         while (child && child->node->children && (child->type == MIMETYPE_MULTIPART))
2258                 child = (MimeInfo *)child->node->children->data;
2259
2260         if (child->type == MIMETYPE_TEXT) {
2261                 firsttext = child;
2262                 debug_print("First text part found\n");
2263         } else if (compose->mode == COMPOSE_REEDIT &&
2264                  child->type == MIMETYPE_APPLICATION &&
2265                  !strcasecmp(child->subtype, "pgp-encrypted")) {
2266                 AlertValue val;
2267                 val = alertpanel(_("Encrypted message"),
2268                                  _("Cannot re-edit an encrypted message. \n"
2269                                    "Discard encrypted part?"),
2270                                  _("Yes"), _("No"), NULL);
2271                 if (val == G_ALERTDEFAULT) 
2272                         encrypted = (MimeInfo *)child->node->parent->data;
2273         }
2274      
2275         child = (MimeInfo *) mimeinfo->node->children->data;
2276         while (child != NULL) {
2277                 if (child == encrypted) {
2278                         /* skip this part of tree */
2279                         NEXT_PART_NOT_CHILD(child);
2280                         continue;
2281                 }
2282
2283                 if (child->type == MIMETYPE_MULTIPART) {
2284                         /* get the actual content */
2285                         child = procmime_mimeinfo_next(child);
2286                         continue;
2287                 }
2288                     
2289                 if (child == firsttext) {
2290                         child = procmime_mimeinfo_next(child);
2291                         continue;
2292                 }
2293
2294                 if ((compose->mode == COMPOSE_REEDIT || 
2295                      compose->mode == COMPOSE_FORWARD_INLINE ||
2296                      compose->mode == COMPOSE_FORWARD )  &&
2297                     (child->type == MIMETYPE_APPLICATION) && 
2298                     !strcmp(child->subtype, "pgp-signature")) {
2299 #ifdef USE_GPGME
2300                         if (compose->mode == COMPOSE_REEDIT) {
2301                                 compose->gnupg_mode  = GNUPG_MODE_DETACH;
2302                                 compose_use_signing(compose, TRUE);
2303                         }
2304 #endif
2305                         child = procmime_mimeinfo_next(child);
2306                         continue;
2307                 }
2308                 outfile = procmime_get_tmp_file_name(child);
2309                 if (procmime_get_part(outfile, child) < 0)
2310                         g_warning("Can't get the part of multipart message.");
2311                 else {
2312                         gchar *content_type;
2313
2314                         content_type = procmime_get_content_type_str(child->type, child->subtype);
2315                         partname = procmime_mimeinfo_get_parameter(child, "name");
2316                         if (partname == NULL)
2317                                 partname = "";
2318                         compose_attach_append(compose, outfile, 
2319                                               partname, content_type);
2320                         g_free(content_type);
2321                 }
2322                 g_free(outfile);
2323                 NEXT_PART_NOT_CHILD(child);
2324         }
2325         procmime_mimeinfo_free_all(mimeinfo);
2326 }
2327
2328 #undef NEXT_PART_NOT_CHILD
2329
2330 #define GET_CHAR(pos, buf, len)                                              \
2331 {                                                                            \
2332         if (text->use_wchar)                                                 \
2333                 len = wctomb(buf, (wchar_t)GTK_STEXT_INDEX(text, (pos)));    \
2334         else {                                                               \
2335                 buf[0] = GTK_STEXT_INDEX(text, (pos));                       \
2336                 len = 1;                                                     \
2337         }                                                                    \
2338 }
2339
2340 #define DISP_WIDTH(len) \
2341         ((len > 2 && conv_get_current_charset() == C_UTF_8) ? 2 : \
2342          (len == 2 && conv_get_current_charset() == C_UTF_8) ? 1 : len)
2343
2344 #define SPACE_CHARS     " \t"
2345
2346 static void compose_wrap_line(Compose *compose)
2347 {
2348         GtkSText *text = GTK_STEXT(compose->text);
2349         gint ch_len, last_ch_len;
2350         gchar cbuf[MB_LEN_MAX], last_ch;
2351         guint text_len;
2352         guint line_end;
2353         guint quoted;
2354         gint p_start, p_end;
2355         gint line_pos, cur_pos;
2356         gint line_len, cur_len;
2357
2358         gtk_stext_freeze(text);
2359
2360         text_len = gtk_stext_get_length(text);
2361
2362         /* check to see if the point is on the paragraph mark (empty line). */
2363         cur_pos = gtk_stext_get_point(text);
2364         GET_CHAR(cur_pos, cbuf, ch_len);
2365         if ((ch_len == 1 && *cbuf == '\n') || cur_pos == text_len) {
2366                 if (cur_pos == 0)
2367                         goto compose_end; /* on the paragraph mark */
2368                 GET_CHAR(cur_pos - 1, cbuf, ch_len);
2369                 if (ch_len == 1 && *cbuf == '\n')
2370                         goto compose_end; /* on the paragraph mark */
2371         }
2372
2373         /* find paragraph start. */
2374         line_end = quoted = 0;
2375         for (p_start = cur_pos; p_start >= 0; --p_start) {
2376                 GET_CHAR(p_start, cbuf, ch_len);
2377                 if (ch_len == 1 && *cbuf == '\n') {
2378                         if (quoted)
2379                                 goto compose_end; /* quoted part */
2380                         if (line_end) {
2381                                 p_start += 2;
2382                                 break;
2383                         }
2384                         line_end = 1;
2385                 } else {
2386                         if (ch_len == 1 
2387                             && strchr(prefs_common.quote_chars, *cbuf))
2388                                 quoted = 1;
2389                         else if (ch_len != 1 || !isspace(*(guchar *)cbuf))
2390                                 quoted = 0;
2391
2392                         line_end = 0;
2393                 }
2394         }
2395         if (p_start < 0)
2396                 p_start = 0;
2397
2398         /* find paragraph end. */
2399         line_end = 0;
2400         for (p_end = cur_pos; p_end < text_len; p_end++) {
2401                 GET_CHAR(p_end, cbuf, ch_len);
2402                 if (ch_len == 1 && *cbuf == '\n') {
2403                         if (line_end) {
2404                                 p_end -= 1;
2405                                 break;
2406                         }
2407                         line_end = 1;
2408                 } else {
2409                         if (line_end && ch_len == 1 &&
2410                             strchr(prefs_common.quote_chars, *cbuf))
2411                                 goto compose_end; /* quoted part */
2412
2413                         line_end = 0;
2414                 }
2415         }
2416         if (p_end >= text_len)
2417                 p_end = text_len;
2418
2419         if (p_start >= p_end)
2420                 goto compose_end;
2421
2422         line_len = cur_len = 0;
2423         last_ch_len = 0;
2424         last_ch = '\0';
2425         line_pos = p_start;
2426         for (cur_pos = p_start; cur_pos < p_end; cur_pos++) {
2427                 guint space = 0;
2428
2429                 GET_CHAR(cur_pos, cbuf, ch_len);
2430
2431                 if (ch_len < 0) {
2432                         cbuf[0] = '\0';
2433                         ch_len = 1;
2434                 }
2435
2436                 if (ch_len == 1 && isspace(*(guchar *)cbuf))
2437                         space = 1;
2438
2439                 if (ch_len == 1 && *cbuf == '\n') {
2440                         guint replace = 0;
2441                         if (last_ch_len == 1 && !isspace((guchar)last_ch)) {
2442                                 if (cur_pos + 1 < p_end) {
2443                                         GET_CHAR(cur_pos + 1, cbuf, ch_len);
2444                                         if (ch_len == 1 &&
2445                                             !isspace(*(guchar *)cbuf))
2446                                                 replace = 1;
2447                                 }
2448                         }
2449                         gtk_stext_set_point(text, cur_pos + 1);
2450                         gtk_stext_backward_delete(text, 1);
2451                         if (replace) {
2452                                 gtk_stext_set_point(text, cur_pos);
2453                                 gtk_stext_insert(text, NULL, NULL, NULL, " ", 1);
2454                                 space = 1;
2455                         }
2456                         else {
2457                                 p_end--;
2458                                 cur_pos--;
2459                                 continue;
2460                         }
2461                 }
2462
2463                 last_ch_len = ch_len;
2464                 last_ch = *cbuf;
2465
2466                 if (space) {
2467                         line_pos = cur_pos + 1;
2468                         line_len = cur_len + ch_len;
2469                 }
2470
2471                 if (cur_len + DISP_WIDTH(ch_len) > prefs_common.linewrap_len &&
2472                     line_len > 0) {
2473                         gint tlen = ch_len;
2474
2475                         GET_CHAR(line_pos - 1, cbuf, ch_len);
2476                         if (ch_len == 1 && isspace(*(guchar *)cbuf)) {
2477                                 gtk_stext_set_point(text, line_pos);
2478                                 gtk_stext_backward_delete(text, 1);
2479                                 p_end--;
2480                                 cur_pos--;
2481                                 line_pos--;
2482                                 cur_len--;
2483                                 line_len--;
2484                         }
2485                         ch_len = tlen;
2486
2487                         gtk_stext_set_point(text, line_pos);
2488                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2489                         p_end++;
2490                         cur_pos++;
2491                         line_pos++;
2492                         cur_len = cur_len - line_len + DISP_WIDTH(ch_len);
2493                         line_len = 0;
2494                         continue;
2495                 }
2496
2497                 if (ch_len > 1) {
2498                         line_pos = cur_pos + 1;
2499                         line_len = cur_len + DISP_WIDTH(ch_len);
2500                 }
2501                 cur_len += DISP_WIDTH(ch_len);
2502         }
2503
2504 compose_end:
2505         gtk_stext_thaw(text);
2506 }
2507
2508 #undef WRAP_DEBUG
2509 #ifdef WRAP_DEBUG
2510 /* Darko: used when I debug wrapping */
2511 void dump_text(GtkSText *text, int pos, int tlen, int breakoncr)
2512 {
2513         gint i, clen;
2514         gchar cbuf[MB_LEN_MAX];
2515
2516         printf("%d [", pos);
2517         for (i = pos; i < tlen; i++) {
2518                 GET_CHAR(i, cbuf, clen);
2519                 if (clen < 0) break;
2520                 if (breakoncr && clen == 1 && cbuf[0] == '\n')
2521                         break;
2522                 fwrite(cbuf, clen, 1, stdout);
2523         }
2524         printf("]\n");
2525 }
2526 #endif
2527
2528 typedef enum {
2529         WAIT_FOR_SPACE,
2530         WAIT_FOR_INDENT_CHAR,
2531         WAIT_FOR_INDENT_CHAR_OR_SPACE
2532 } IndentState;
2533
2534 /* return indent length, we allow:
2535    > followed by spaces/tabs
2536    | followed by spaces/tabs
2537    uppercase characters immediately followed by >,
2538    and the repeating sequences of the above */
2539 /* return indent length */
2540 static guint get_indent_length(GtkSText *text, guint start_pos, guint text_len)
2541 {
2542         guint i_len = 0;
2543         guint i, ch_len, alnum_cnt = 0;
2544         IndentState state = WAIT_FOR_INDENT_CHAR;
2545         gchar cbuf[MB_LEN_MAX];
2546         gboolean is_space;
2547         gboolean is_indent;
2548
2549         if (prefs_common.quote_chars == NULL) {
2550                 return 0 ;
2551         }
2552
2553         for (i = start_pos; i < text_len; i++) {
2554                 GET_CHAR(i, cbuf, ch_len);
2555                 if (ch_len > 1)
2556                         break;
2557
2558                 if (cbuf[0] == '\n')
2559                         break;
2560
2561                 is_indent = strchr(prefs_common.quote_chars, cbuf[0]) ? TRUE : FALSE;
2562                 is_space = strchr(SPACE_CHARS, cbuf[0]) ? TRUE : FALSE;
2563
2564                 switch (state) {
2565                 case WAIT_FOR_SPACE:
2566                         if (is_space == FALSE)
2567                                 goto out;
2568                         state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
2569                         break;
2570                 case WAIT_FOR_INDENT_CHAR_OR_SPACE:
2571                         if (is_indent == FALSE && is_space == FALSE &&
2572                             !isupper((guchar)cbuf[0]))
2573                                 goto out;
2574                         if (is_space == TRUE) {
2575                                 alnum_cnt = 0;
2576                                 state = WAIT_FOR_INDENT_CHAR_OR_SPACE;
2577                         } else if (is_indent == TRUE) {
2578                                 alnum_cnt = 0;
2579                                 state = WAIT_FOR_SPACE;
2580                         } else {
2581                                 alnum_cnt++;
2582                                 state = WAIT_FOR_INDENT_CHAR;
2583                         }
2584                         break;
2585                 case WAIT_FOR_INDENT_CHAR:
2586                         if (is_indent == FALSE && !isupper((guchar)cbuf[0]))
2587                                 goto out;
2588                         if (is_indent == TRUE) {
2589                                 if (alnum_cnt > 0 
2590                                     && !strchr(prefs_common.quote_chars, cbuf[0]))
2591                                         goto out;
2592                                 alnum_cnt = 0;
2593                                 state = WAIT_FOR_SPACE;
2594                         } else {
2595                                 alnum_cnt++;
2596                         }
2597                         break;
2598                 }
2599
2600                 i_len++;
2601         }
2602
2603 out:
2604         if ((i_len > 0) && (state == WAIT_FOR_INDENT_CHAR))
2605                 i_len -= alnum_cnt;
2606
2607         return i_len;
2608 }
2609
2610 /* insert quotation string when line was wrapped */
2611 static guint ins_quote(GtkSText *text, guint indent_len,
2612                        guint prev_line_pos, guint text_len,
2613                        gchar *quote_fmt)
2614 {
2615         guint i, ins_len = 0;
2616         gchar ch;
2617
2618         if (indent_len) {
2619                 for (i = 0; i < indent_len; i++) {
2620                         ch = GTK_STEXT_INDEX(text, prev_line_pos + i);
2621                         gtk_stext_insert(text, NULL, NULL, NULL, &ch, 1);
2622                 }
2623                 ins_len = indent_len;
2624         }
2625
2626         return ins_len;
2627 }
2628
2629 /* check if we should join the next line */
2630 static gboolean join_next_line(GtkSText *text, guint start_pos, guint tlen,
2631                                guint prev_ilen, gboolean autowrap)
2632 {
2633         guint indent_len, ch_len;
2634         gboolean do_join = FALSE;
2635         gchar cbuf[MB_LEN_MAX];
2636
2637         indent_len = get_indent_length(text, start_pos, tlen);
2638
2639         if ((autowrap || indent_len > 0) && indent_len == prev_ilen) {
2640                 GET_CHAR(start_pos + indent_len, cbuf, ch_len);
2641                 if (ch_len > 0 && (cbuf[0] != '\n'))
2642                         do_join = TRUE;
2643         }
2644
2645         return do_join;
2646 }
2647
2648 static void compose_wrap_line_all(Compose *compose)
2649 {
2650         compose_wrap_line_all_full(compose, FALSE);
2651 }
2652
2653 #define STEXT_FREEZE() \
2654         if (!frozen) { gtk_stext_freeze(text); frozen = TRUE; }
2655
2656 static void compose_wrap_line_all_full(Compose *compose, gboolean autowrap)
2657 {
2658         GtkSText *text = GTK_STEXT(compose->text);
2659         guint tlen;
2660         guint line_pos = 0, cur_pos = 0, p_pos = 0;
2661         gint line_len = 0, cur_len = 0;
2662         gint ch_len;
2663         gboolean is_new_line = TRUE, do_delete = FALSE;
2664         guint i_len = 0;
2665         gboolean linewrap_quote = TRUE;
2666         gboolean set_editable_pos = FALSE;
2667         gint editable_pos = 0;
2668         gboolean frozen = FALSE;
2669         guint linewrap_len = prefs_common.linewrap_len;
2670         gchar *qfmt = prefs_common.quotemark;
2671         gchar cbuf[MB_LEN_MAX];
2672
2673         tlen = gtk_stext_get_length(text);
2674
2675         for (; cur_pos < tlen; cur_pos++) {
2676                 /* mark position of new line - needed for quotation wrap */
2677                 if (is_new_line) {
2678                         if (linewrap_quote)
2679                                 i_len = get_indent_length(text, cur_pos, tlen);
2680
2681                         is_new_line = FALSE;
2682                         p_pos = cur_pos;
2683 #ifdef WRAP_DEBUG
2684                         g_print("new line i_len=%d p_pos=", i_len);
2685                         dump_text(text, p_pos, tlen, 1);
2686 #endif
2687                 }
2688
2689                 GET_CHAR(cur_pos, cbuf, ch_len);
2690
2691                 /* fix line length for tabs */
2692                 if (ch_len == 1 && *cbuf == '\t') {
2693                         guint tab_width = text->default_tab_width;
2694                         guint tab_offset = line_len % tab_width;
2695
2696 #ifdef WRAP_DEBUG
2697                         g_print("found tab at pos=%d line_len=%d ", cur_pos,
2698                                 line_len);
2699 #endif
2700                         if (tab_offset) {
2701                                 line_len += tab_width - tab_offset - 1;
2702                                 cur_len = line_len;
2703                         }
2704 #ifdef WRAP_DEBUG
2705                         printf("new_len=%d\n", line_len);
2706 #endif
2707                 }
2708
2709                 /* we have encountered line break */
2710                 if (ch_len == 1 && *cbuf == '\n') {
2711                         gint clen;
2712                         gchar cb[MB_LEN_MAX];
2713
2714                         /* should we join the next line */
2715                         if ((autowrap || i_len != cur_len) && do_delete &&
2716                             join_next_line
2717                                 (text, cur_pos + 1, tlen, i_len, autowrap))
2718                                 do_delete = TRUE;
2719                         else
2720                                 do_delete = FALSE;
2721
2722 #ifdef WRAP_DEBUG
2723                         g_print("found CR at %d do_del is %d next line is ",
2724                                cur_pos, do_delete);
2725                         dump_text(text, cur_pos + 1, tlen, 1);
2726 #endif
2727
2728                         /* skip delete if it is continuous URL */
2729                         if (do_delete && (line_pos - p_pos <= i_len) &&
2730                             gtk_stext_is_uri_string(text, line_pos, tlen))
2731                                 do_delete = FALSE;
2732
2733 #ifdef WRAP_DEBUG
2734                         g_print("l_len=%d wrap_len=%d do_del=%d\n",
2735                                 line_len, linewrap_len, do_delete);
2736 #endif
2737                         /* should we delete to perform smart wrapping */
2738                         if (line_len < linewrap_len && do_delete) {
2739                                 STEXT_FREEZE();
2740                                 /* get rid of newline */
2741                                 gtk_stext_set_point(text, cur_pos);
2742                                 gtk_stext_forward_delete(text, 1);
2743                                 tlen--;
2744
2745                                 /* if text starts with quote fmt or with
2746                                    indent string, delete them */
2747                                 if (i_len) {
2748                                         guint ilen;
2749                                         ilen =  gtk_stext_str_compare_n
2750                                                 (text, cur_pos, p_pos, i_len,
2751                                                  tlen);
2752                                         if (ilen) {
2753                                                 gtk_stext_forward_delete
2754                                                         (text, ilen);
2755                                                 tlen -= ilen;
2756                                         }
2757                                 }
2758
2759                                 GET_CHAR(cur_pos, cb, clen);
2760
2761                                 /* insert space if it's alphanumeric */
2762                                 if ((cur_pos != line_pos) &&
2763                                     ((clen > 1) || isalnum((guchar)cb[0]))) {
2764                                         gtk_stext_insert(text, NULL, NULL,
2765                                                         NULL, " ", 1);
2766                                         tlen++;
2767                                 }
2768
2769                                 /* and start over with current line */
2770                                 cur_pos = p_pos - 1;
2771                                 line_pos = cur_pos;
2772                                 line_len = cur_len = 0;
2773                                 do_delete = FALSE;
2774                                 is_new_line = TRUE;
2775 #ifdef WRAP_DEBUG
2776                                 g_print("after delete l_pos=");
2777                                 dump_text(text, line_pos, tlen, 1);
2778 #endif
2779                                 /* move beginning of line if we are on LF */
2780                                 GET_CHAR(line_pos, cb, clen);
2781                                 if (clen == 1 && *cb == '\n')
2782                                         line_pos++;
2783 #ifdef WRAP_DEBUG
2784                                 g_print("new line_pos=%d\n", line_pos);
2785 #endif
2786
2787                                 continue;
2788                         }
2789
2790                         /* mark new line beginning */
2791                         line_pos = cur_pos + 1;
2792                         line_len = cur_len = 0;
2793                         do_delete = FALSE;
2794                         is_new_line = TRUE;
2795                         continue;
2796                 }
2797
2798                 if (ch_len < 0) {
2799                         cbuf[0] = '\0';
2800                         ch_len = 1;
2801                 }
2802
2803                 /* possible line break */
2804                 if (ch_len == 1 && isspace(*(guchar *)cbuf)) {
2805                         line_pos = cur_pos + 1;
2806                         line_len = cur_len + ch_len;
2807                 }
2808
2809                 /* are we over wrapping length set in preferences ? */
2810                 if (cur_len + DISP_WIDTH(ch_len) > linewrap_len) {
2811                         gint clen;
2812
2813 #ifdef WRAP_DEBUG
2814                         g_print("should wrap cur_pos=%d ", cur_pos);
2815                         dump_text(text, p_pos, tlen, 1);
2816                         dump_text(text, line_pos, tlen, 1);
2817 #endif
2818                         /* force wrapping if it is one long word but not URL */
2819                         if (line_pos - p_pos <= i_len)
2820                                 if (!gtk_stext_is_uri_string
2821                                     (text, line_pos, tlen))
2822                                         line_pos = cur_pos - 1;
2823 #ifdef WRAP_DEBUG
2824                         g_print("new line_pos=%d\n", line_pos);
2825 #endif
2826
2827                         GET_CHAR(line_pos - 1, cbuf, clen);
2828
2829                         /* if next character is space delete it */
2830                         if (clen == 1 && isspace(*(guchar *)cbuf)) {
2831                                 if (p_pos + i_len != line_pos ||
2832                                     !gtk_stext_is_uri_string
2833                                         (text, line_pos, tlen)) {
2834                                         STEXT_FREEZE();
2835                                         /* workaround for correct cursor
2836                                            position */
2837                                         if (set_editable_pos == FALSE) {
2838                                                 editable_pos = gtk_editable_get_position(GTK_EDITABLE(text));
2839                                                 if (editable_pos == line_pos)
2840                                                         set_editable_pos = TRUE;
2841                                         }
2842                                         gtk_stext_set_point(text, line_pos);
2843                                         gtk_stext_backward_delete(text, 1);
2844                                         tlen--;
2845                                         cur_pos--;
2846                                         line_pos--;
2847                                         cur_len--;
2848                                         line_len--;
2849                                 }
2850                         }
2851
2852                         /* if it is URL at beginning of line don't wrap */
2853                         if (p_pos + i_len == line_pos &&
2854                             gtk_stext_is_uri_string(text, line_pos, tlen)) {
2855 #ifdef WRAP_DEBUG
2856                                 g_print("found URL at ");
2857                                 dump_text(text, line_pos, tlen, 1);
2858 #endif
2859                                 continue;
2860                         }
2861
2862                         /* insert CR */
2863                         STEXT_FREEZE();
2864                         gtk_stext_set_point(text, line_pos);
2865                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2866                         gtk_stext_compact_buffer(text);
2867                         tlen++;
2868                         line_pos++;
2869                         /* for loop will increase it */
2870                         cur_pos = line_pos - 1;
2871                         /* start over with current line */
2872                         is_new_line = TRUE;
2873                         line_len = cur_len = 0;
2874                         if (autowrap || i_len > 0)
2875                                 do_delete = TRUE;
2876                         else
2877                                 do_delete = FALSE;
2878 #ifdef WRAP_DEBUG
2879                         g_print("after CR insert ");
2880                         dump_text(text, line_pos, tlen, 1);
2881                         dump_text(text, cur_pos, tlen, 1);
2882 #endif
2883
2884                         /* should we insert quotation ? */
2885                         if (linewrap_quote && i_len) {
2886                                 /* only if line is not already quoted  */
2887                                 if (!gtk_stext_str_compare
2888                                         (text, line_pos, tlen, qfmt)) {
2889                                         guint ins_len;
2890
2891                                         if (line_pos - p_pos > i_len) {
2892                                                 ins_len = ins_quote
2893                                                         (text, i_len, p_pos,
2894                                                          tlen, qfmt);
2895                                                 tlen += ins_len;
2896                                         }
2897 #ifdef WRAP_DEBUG
2898                                         g_print("after quote insert ");
2899                                         dump_text(text, line_pos, tlen, 1);
2900 #endif
2901                                 }
2902                         }
2903                         continue;
2904                 }
2905
2906                 if (ch_len > 1) {
2907                         line_pos = cur_pos + 1;
2908                         line_len = cur_len + DISP_WIDTH(ch_len);
2909                 }
2910                 /* advance to next character in buffer */
2911                 cur_len += DISP_WIDTH(ch_len);
2912         }
2913
2914         if (frozen)
2915                 gtk_stext_thaw(text);
2916
2917         if (set_editable_pos && editable_pos <= tlen)
2918                 gtk_editable_set_position(GTK_EDITABLE(text), editable_pos);
2919 }
2920
2921 #undef STEXT_FREEZE
2922 #undef GET_CHAR
2923
2924 static void compose_set_title(Compose *compose)
2925 {
2926         gchar *str;
2927         gchar *edited;
2928
2929         edited = compose->modified ? _(" [Edited]") : "";
2930         if (compose->account && compose->account->address)
2931                 str = g_strdup_printf(_("%s - Compose message%s"),
2932                                       compose->account->address, edited);
2933         else
2934                 str = g_strdup_printf(_("Compose message%s"), edited);
2935         gtk_window_set_title(GTK_WINDOW(compose->window), str);
2936         g_free(str);
2937 }
2938
2939 /**
2940  * compose_current_mail_account:
2941  * 
2942  * Find a current mail account (the currently selected account, or the
2943  * default account, if a news account is currently selected).  If a
2944  * mail account cannot be found, display an error message.
2945  * 
2946  * Return value: Mail account, or NULL if not found.
2947  **/
2948 static PrefsAccount *
2949 compose_current_mail_account(void)
2950 {
2951         PrefsAccount *ac;
2952
2953         if (cur_account && cur_account->protocol != A_NNTP)
2954                 ac = cur_account;
2955         else {
2956                 ac = account_get_default();
2957                 if (!ac || ac->protocol == A_NNTP) {
2958                         alertpanel_error(_("Account for sending mail is not specified.\n"
2959                                            "Please select a mail account before sending."));
2960                         return NULL;
2961                 }
2962         }
2963         return ac;
2964 }
2965
2966 static void compose_select_account(Compose *compose, PrefsAccount *account,
2967                                    gboolean init)
2968 {
2969         GtkWidget *menuitem;
2970         GtkItemFactory *ifactory;
2971
2972         g_return_if_fail(account != NULL);
2973
2974         compose->account = account;
2975
2976         compose_set_title(compose);
2977
2978         ifactory = gtk_item_factory_from_widget(compose->menubar);
2979 #if 0
2980         if (account->protocol == A_NNTP) {
2981                 gtk_widget_show(compose->newsgroups_hbox);
2982                 gtk_widget_show(compose->newsgroups_entry);
2983                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 2, 4);
2984                 compose->use_newsgroups = TRUE;
2985
2986                 menuitem = gtk_item_factory_get_item(ifactory, "/View/To");
2987                 gtk_check_menu_item_set_active
2988                         (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2989                 gtk_widget_set_sensitive(menuitem, TRUE);
2990                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Cc");
2991                 gtk_check_menu_item_set_active
2992                         (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
2993                 gtk_widget_set_sensitive(menuitem, TRUE);
2994
2995                 menu_set_sensitive(ifactory, "/View/Followup to", TRUE);
2996         } else {
2997                 gtk_widget_hide(compose->newsgroups_hbox);
2998                 gtk_widget_hide(compose->newsgroups_entry);
2999                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 2, 0);
3000                 gtk_widget_queue_resize(compose->table_vbox);
3001                 compose->use_newsgroups = FALSE;
3002
3003                 menuitem = gtk_item_factory_get_item(ifactory, "/View/To");
3004                 gtk_check_menu_item_set_active
3005                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
3006                 gtk_widget_set_sensitive(menuitem, FALSE);
3007                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Cc");
3008                 gtk_check_menu_item_set_active
3009                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
3010                 gtk_widget_set_sensitive(menuitem, FALSE);
3011
3012                 menuitem = gtk_item_factory_get_item(ifactory, "/View/Followup to");
3013                 gtk_check_menu_item_set_active
3014                         (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
3015                 gtk_widget_set_sensitive(menuitem, FALSE);
3016         }
3017
3018         if (account->set_autocc) {
3019                 compose_entry_show(compose, COMPOSE_ENTRY_CC);
3020                 if (account->auto_cc && compose->mode != COMPOSE_REEDIT)
3021                         compose_entry_set(compose, account->auto_cc,
3022                                           COMPOSE_ENTRY_CC);
3023         }
3024         if (account->set_autobcc) {
3025                 compose_entry_show(compose, COMPOSE_ENTRY_BCC);
3026                 if (account->auto_bcc && compose->mode != COMPOSE_REEDIT)
3027                         compose_entry_set(compose, account->auto_bcc,
3028                                           COMPOSE_ENTRY_BCC);
3029         }
3030         if (account->set_autoreplyto) {
3031                 compose_entry_show(compose, COMPOSE_ENTRY_REPLY_TO);
3032                 if (account->auto_replyto && compose->mode != COMPOSE_REEDIT)
3033                         compose_entry_set(compose, account->auto_replyto,
3034                                           COMPOSE_ENTRY_REPLY_TO);
3035         }
3036
3037         menuitem = gtk_item_factory_get_item(ifactory, "/View/Ruler");
3038         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
3039                                        prefs_common.show_ruler);
3040 #endif
3041
3042 #if USE_GPGME
3043         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Sign");
3044         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
3045                                        account->default_sign);
3046         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Encrypt");
3047         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
3048                                        account->default_encrypt);
3049                                        
3050         activate_gnupg_mode(compose, account);          
3051 #endif /* USE_GPGME */
3052
3053         if (!init && compose->mode != COMPOSE_REDIRECT)
3054                 compose_insert_sig(compose, TRUE);
3055 }
3056
3057 gboolean compose_check_for_valid_recipient(Compose *compose) {
3058         gchar *recipient_headers_mail[] = {"To:", "Cc:", "Bcc:", NULL};
3059         gchar *recipient_headers_news[] = {"Newsgroups:", NULL};
3060         gboolean recipient_found = FALSE;
3061         GSList *list;
3062         gchar **strptr;
3063
3064         /* free to and newsgroup list */
3065         slist_free_strings(compose->to_list);
3066         g_slist_free(compose->to_list);
3067         compose->to_list = NULL;
3068                         
3069         slist_free_strings(compose->newsgroup_list);
3070         g_slist_free(compose->newsgroup_list);
3071         compose->newsgroup_list = NULL;
3072
3073         /* search header entries for to and newsgroup entries */
3074         for (list = compose->header_list; list; list = list->next) {
3075                 gchar *header;
3076                 gchar *entry;
3077                 header = gtk_editable_get_chars(GTK_EDITABLE(GTK_COMBO(((ComposeHeaderEntry *)list->data)->combo)->entry), 0, -1);
3078                 entry = gtk_editable_get_chars(GTK_EDITABLE(((ComposeHeaderEntry *)list->data)->entry), 0, -1);
3079                 g_strstrip(entry);
3080                 if (entry[0] != '\0') {
3081                         for (strptr = recipient_headers_mail; *strptr != NULL; strptr++) {
3082                                 if (!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
3083                                         compose->to_list = address_list_append(compose->to_list, entry);
3084                                         recipient_found = TRUE;
3085                                 }
3086                         }
3087                         for (strptr = recipient_headers_news; *strptr != NULL; strptr++) {
3088                                 if (!strcmp(header, (prefs_common.trans_hdr ? gettext(*strptr) : *strptr))) {
3089                                         compose->newsgroup_list = newsgroup_list_append(compose->newsgroup_list, entry);
3090                                         recipient_found = TRUE;
3091                                 }
3092                         }
3093                 }
3094                 g_free(header);
3095                 g_free(entry);
3096         }
3097         return recipient_found;
3098 }
3099
3100 static gboolean compose_check_entries(Compose *compose, gboolean check_subject)
3101 {
3102         gchar *str;
3103
3104         if (compose_check_for_valid_recipient(compose) == FALSE) {
3105                 alertpanel_error(_("Recipient is not specified."));
3106                 return FALSE;
3107         }
3108
3109         str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
3110         if (*str == '\0' && check_subject == TRUE) {
3111                 AlertValue aval;
3112
3113                 aval = alertpanel(_("Send"),
3114                                   _("Subject is empty. Send it anyway?"),
3115                                   _("Yes"), _("No"), NULL);
3116                 if (aval != G_ALERTDEFAULT)
3117                         return FALSE;
3118         }
3119
3120         return TRUE;
3121 }
3122
3123 gint compose_send(Compose *compose)
3124 {
3125         gint msgnum;
3126         FolderItem *folder;
3127         gint val;
3128         gchar *msgpath;
3129
3130         if (compose_check_entries(compose, TRUE) == FALSE)
3131                 return -1;
3132
3133         val = compose_queue(compose, &msgnum, &folder);
3134         if (val) {
3135                 alertpanel_error(_("Could not queue message for sending"));
3136                 return -1;
3137         }
3138
3139         if (msgnum == 0) {
3140                 alertpanel_error(_("The message was queued but could not be sent.\nUse \"Send queued messages\" from the main window to retry."));
3141                 return 0;
3142         }
3143         
3144         msgpath = folder_item_fetch_msg(folder, msgnum);
3145         val = procmsg_send_message_queue(msgpath);
3146         g_free(msgpath);
3147
3148         folder_item_remove_msg(folder, msgnum);
3149         
3150         folder_item_scan(folder);
3151
3152         return val;
3153 }
3154
3155 #if 0 /* compose restructure */
3156 gint compose_send(Compose *compose)
3157 {
3158         gchar tmp[MAXPATHLEN + 1];
3159         gint ok = 0;
3160         static gboolean lock = FALSE;
3161
3162         if (lock) return 1;
3163
3164         g_return_val_if_fail(compose->account != NULL, -1);
3165
3166         lock = TRUE;
3167
3168         if (compose_check_entries(compose, TRUE) == FALSE) {
3169                 lock = FALSE;
3170                 return 1;
3171         }
3172
3173         /* write to temporary file */
3174         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.%08x",
3175                    get_tmp_dir(), G_DIR_SEPARATOR, (gint)compose);
3176
3177         if (prefs_common.linewrap_at_send)
3178                 compose_wrap_line_all(compose);
3179
3180         if (compose_write_to_file(compose, tmp, FALSE) < 0) {
3181                 lock = FALSE;
3182                 return -1;
3183         }
3184
3185         if (!compose->to_list && !compose->newsgroup_list) {
3186                 g_warning("can't get recipient list.");
3187                 unlink(tmp);
3188                 lock = FALSE;
3189                 return -1;
3190         }
3191
3192         if (compose->to_list) {
3193                 PrefsAccount *ac;
3194
3195 #if 0 /* NEW COMPOSE GUI */
3196                 if (compose->account->protocol != A_NNTP)
3197                         ac = compose->account;
3198                 else {
3199                         ac = account_find_from_address(compose->account->address);
3200                         if (!ac) {
3201                                 if (cur_account && cur_account->protocol != A_NNTP)
3202                                         ac = cur_account;
3203                                 else
3204                                         ac = account_get_default();
3205                         }
3206                         if (!ac || ac->protocol == A_NNTP) {
3207                                 alertpanel_error(_("Account for sending mail is not specified.\n"
3208         &nbs