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