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