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