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