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