made smart wrapping an option
[claws.git] / src / compose.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gdk/gdkkeysyms.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkmenu.h>
30 #include <gtk/gtkmenuitem.h>
31 #include <gtk/gtkitemfactory.h>
32 #include <gtk/gtkcheckmenuitem.h>
33 #include <gtk/gtkoptionmenu.h>
34 #include <gtk/gtkwidget.h>
35 #include <gtk/gtkclist.h>
36 #include <gtk/gtkctree.h>
37 #include <gtk/gtkvpaned.h>
38 #include <gtk/gtkentry.h>
39 #include <gtk/gtkeditable.h>
40 #include <gtk/gtkwindow.h>
41 #include <gtk/gtksignal.h>
42 #include <gtk/gtkvbox.h>
43 #include <gtk/gtkcontainer.h>
44 #include <gtk/gtkhandlebox.h>
45 #include <gtk/gtktoolbar.h>
46 #include <gtk/gtktable.h>
47 #include <gtk/gtkhbox.h>
48 #include <gtk/gtklabel.h>
49 #include <gtk/gtkscrolledwindow.h>
50 #include <gtk/gtkthemes.h>
51 #include <gtk/gtkdnd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <ctype.h>
56 #include <unistd.h>
57 #include <time.h>
58 /* #include <sys/utsname.h> */
59 #include <stdlib.h>
60 #include <sys/wait.h>
61 #include <signal.h>
62 #include <errno.h>
63
64 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
65 #  include <wchar.h>
66 #  include <wctype.h>
67 #endif
68
69
70 #include "gtkstext.h"
71
72 #include "intl.h"
73 #include "main.h"
74 #include "mainwindow.h"
75 #include "compose.h"
76 #include "addressbook.h"
77 #include "folderview.h"
78 #include "procmsg.h"
79 #include "menu.h"
80 #include "send.h"
81 #include "news.h"
82 #include "customheader.h"
83 #include "prefs_common.h"
84 #include "prefs_account.h"
85 #include "account.h"
86 #include "filesel.h"
87 #include "procheader.h"
88 #include "procmime.h"
89 #include "statusbar.h"
90 #include "about.h"
91 #include "base64.h"
92 #include "codeconv.h"
93 #include "utils.h"
94 #include "gtkutils.h"
95 #include "socket.h"
96 #include "alertpanel.h"
97 #include "manage_window.h"
98 #include "gtkshruler.h"
99 #include "folder.h"
100 #include "addr_compl.h"
101
102 #if USE_GPGME
103 #  include "rfc2015.h"
104 #endif
105
106 #include "quote_fmt.h"
107
108 typedef enum
109 {
110         COL_MIMETYPE = 0,
111         COL_SIZE     = 1,
112         COL_NAME     = 2
113 } AttachColumnPos;
114
115 #define N_ATTACH_COLS           3
116
117 #define B64_LINE_SIZE           57
118 #define B64_BUFFSIZE            77
119
120 #define MAX_REFERENCES_LEN      999
121
122 static GdkColor quote_color = {0, 0, 0, 0xbfff};
123
124 static GList *compose_list = NULL;
125
126 static Compose *compose_create                  (PrefsAccount   *account);
127 static void compose_toolbar_create              (Compose        *compose,
128                                                  GtkWidget      *container);
129 static GtkWidget *compose_account_option_menu_create
130                                                 (Compose        *compose);
131 static void compose_destroy                     (Compose        *compose);
132
133 static gint compose_parse_header                (Compose        *compose,
134                                                  MsgInfo        *msginfo);
135 static gchar *compose_parse_references          (const gchar    *ref,
136                                                  const gchar    *msgid);
137 static void compose_quote_file                  (Compose        *compose,
138                                                  MsgInfo        *msginfo,
139                                                  FILE           *fp);
140 static gchar *compose_quote_parse_fmt           (Compose        *compose,
141                                                  MsgInfo        *msginfo,
142                                                  const gchar    *fmt);
143 static void compose_reply_set_entry             (Compose        *compose,
144                                                  MsgInfo        *msginfo,
145                                                  gboolean        to_all,
146                                                  gboolean        to_sender,
147                                                  gboolean
148                                                  followup_and_reply_to);
149 static void compose_reedit_set_entry            (Compose        *compose,
150                                                  MsgInfo        *msginfo);
151 static void compose_insert_sig                  (Compose        *compose);
152 static void compose_insert_file                 (Compose        *compose,
153                                                  const gchar    *file);
154 static void compose_attach_append               (Compose        *compose,
155                                                  const gchar    *file,
156                                                  ContentType     cnttype);
157 static void compose_attach_append_with_type(Compose *compose,
158                                             const gchar *file,
159                                             const gchar *type,
160                                             ContentType cnttype);
161 static void compose_wrap_line                   (Compose        *compose);
162 static void compose_set_title                   (Compose        *compose);
163
164 static PrefsAccount *compose_current_mail_account(void);
165 /* static gint compose_send                     (Compose        *compose); */
166 static gint compose_write_to_file               (Compose        *compose,
167                                                  const gchar    *file,
168                                                  gboolean        is_draft);
169 static gint compose_write_body_to_file          (Compose        *compose,
170                                                  const gchar    *file);
171 static gint compose_save_to_outbox              (Compose        *compose,
172                                                  const gchar    *file);
173 static gint compose_queue                       (Compose        *compose,
174                                                  const gchar    *file);
175 static void compose_write_attach                (Compose        *compose,
176                                                  FILE           *fp);
177 static gint compose_write_headers               (Compose        *compose,
178                                                  FILE           *fp,
179                                                  const gchar    *charset,
180                                                  EncodingType    encoding,
181                                                  gboolean        is_draft);
182
183 static void compose_convert_header              (gchar          *dest,
184                                                  gint            len,
185                                                  gchar          *src,
186                                                  gint            header_len);
187 static void compose_generate_msgid              (Compose        *compose,
188                                                  gchar          *buf,
189                                                  gint            len);
190
191 static void compose_attach_info_free            (AttachInfo     *ainfo);
192 static void compose_attach_remove_selected      (Compose        *compose);
193
194 static void compose_attach_property             (Compose        *compose);
195 static void compose_attach_property_create      (gboolean       *cancelled);
196 static void attach_property_ok                  (GtkWidget      *widget,
197                                                  gboolean       *cancelled);
198 static void attach_property_cancel              (GtkWidget      *widget,
199                                                  gboolean       *cancelled);
200 static gint attach_property_delete_event        (GtkWidget      *widget,
201                                                  GdkEventAny    *event,
202                                                  gboolean       *cancelled);
203 static void attach_property_key_pressed         (GtkWidget      *widget,
204                                                  GdkEventKey    *event,
205                                                  gboolean       *cancelled);
206
207 static void compose_exec_ext_editor             (Compose           *compose);
208 static gint compose_exec_ext_editor_real        (const gchar       *file);
209 static gboolean compose_ext_editor_kill         (Compose           *compose);
210 static void compose_input_cb                    (gpointer           data,
211                                                  gint               source,
212                                                  GdkInputCondition  condition);
213 static void compose_set_ext_editor_sensitive    (Compose           *compose,
214                                                  gboolean           sensitive);
215
216 static gint calc_cursor_xpos    (GtkSText       *text,
217                                  gint            extra,
218                                  gint            char_width);
219
220 /* callback functions */
221
222 static gboolean compose_edit_size_alloc (GtkEditable    *widget,
223                                          GtkAllocation  *allocation,
224                                          GtkSHRuler     *shruler);
225
226 static void toolbar_send_cb             (GtkWidget      *widget,
227                                          gpointer        data);
228 static void toolbar_send_later_cb       (GtkWidget      *widget,
229                                          gpointer        data);
230 static void toolbar_draft_cb            (GtkWidget      *widget,
231                                          gpointer        data);
232 static void toolbar_insert_cb           (GtkWidget      *widget,
233                                          gpointer        data);
234 static void toolbar_attach_cb           (GtkWidget      *widget,
235                                          gpointer        data);
236 static void toolbar_sig_cb              (GtkWidget      *widget,
237                                          gpointer        data);
238 static void toolbar_ext_editor_cb       (GtkWidget      *widget,
239                                          gpointer        data);
240 static void toolbar_linewrap_cb         (GtkWidget      *widget,
241                                          gpointer        data);
242 static void toolbar_address_cb          (GtkWidget      *widget,
243                                          gpointer        data);
244
245 static void select_account(Compose * compose, PrefsAccount * ac);
246 static void account_activated           (GtkMenuItem    *menuitem,
247                                          gpointer        data);
248
249 static void attach_selected             (GtkCList       *clist,
250                                          gint            row,
251                                          gint            column,
252                                          GdkEvent       *event,
253                                          gpointer        data);
254 static void attach_button_pressed       (GtkWidget      *widget,
255                                          GdkEventButton *event,
256                                          gpointer        data);
257 static void attach_key_pressed          (GtkWidget      *widget,
258                                          GdkEventKey    *event,
259                                          gpointer        data);
260
261 static void compose_send_cb             (gpointer        data,
262                                          guint           action,
263                                          GtkWidget      *widget);
264 static void compose_send_later_cb       (gpointer        data,
265                                          guint           action,
266                                          GtkWidget      *widget);
267
268 static void compose_draft_cb            (gpointer        data,
269                                          guint           action,
270                                          GtkWidget      *widget);
271
272 static void compose_attach_cb           (gpointer        data,
273                                          guint           action,
274                                          GtkWidget      *widget);
275 static void compose_insert_file_cb      (gpointer        data,
276                                          guint           action,
277                                          GtkWidget      *widget);
278
279 static void compose_close_cb            (gpointer        data,
280                                          guint           action,
281                                          GtkWidget      *widget);
282 static void compose_address_cb          (gpointer        data,
283                                          guint           action,
284                                          GtkWidget      *widget);
285
286 static void compose_ext_editor_cb       (gpointer        data,
287                                          guint           action,
288                                          GtkWidget      *widget);
289
290 static gint compose_delete_cb           (GtkWidget      *widget,
291                                          GdkEventAny    *event,
292                                          gpointer        data);
293 static void compose_destroy_cb          (GtkWidget      *widget,
294                                          Compose        *compose);
295
296 static void compose_cut_cb              (Compose        *compose);
297 static void compose_copy_cb             (Compose        *compose);
298 static void compose_paste_cb            (Compose        *compose);
299 static void compose_allsel_cb           (Compose        *compose);
300
301 static void compose_grab_focus_cb       (GtkWidget      *widget,
302                                          Compose        *compose);
303
304 static void compose_changed_cb          (GtkEditable    *editable,
305                                          Compose        *compose);
306 static void compose_button_press_cb     (GtkWidget      *widget,
307                                          GdkEventButton *event,
308                                          Compose        *compose);
309 #if 0
310 static void compose_key_press_cb        (GtkWidget      *widget,
311                                          GdkEventKey    *event,
312                                          Compose        *compose);
313 #endif
314
315 static void compose_toggle_to_cb        (gpointer        data,
316                                          guint           action,
317                                          GtkWidget      *widget);
318 static void compose_toggle_cc_cb        (gpointer        data,
319                                          guint           action,
320                                          GtkWidget      *widget);
321 static void compose_toggle_bcc_cb       (gpointer        data,
322                                          guint           action,
323                                          GtkWidget      *widget);
324 static void compose_toggle_replyto_cb   (gpointer        data,
325                                          guint           action,
326                                          GtkWidget      *widget);
327 static void compose_toggle_followupto_cb(gpointer        data,
328                                          guint           action,
329                                          GtkWidget      *widget);
330 static void compose_toggle_attach_cb    (gpointer        data,
331                                          guint           action,
332                                          GtkWidget      *widget);
333 static void compose_toggle_ruler_cb     (gpointer        data,
334                                          guint           action,
335                                          GtkWidget      *widget);
336 #if USE_GPGME
337 static void compose_toggle_sign_cb      (gpointer        data,
338                                          guint           action,
339                                          GtkWidget      *widget);
340 static void compose_toggle_encrypt_cb   (gpointer        data,
341                                          guint           action,
342                                          GtkWidget      *widget);
343 #endif
344 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
345                                              GtkWidget *widget);
346
347 static void compose_attach_drag_received_cb (GtkWidget          *widget,
348                                              GdkDragContext     *drag_context,
349                                              gint                x,
350                                              gint                y,
351                                              GtkSelectionData   *data,
352                                              guint               info,
353                                              guint               time,
354                                              gpointer            user_data);
355 static void compose_insert_drag_received_cb (GtkWidget          *widget,
356                                              GdkDragContext     *drag_context,
357                                              gint                x,
358                                              gint                y,
359                                              GtkSelectionData   *data,
360                                              guint               info,
361                                              guint               time,
362                                              gpointer            user_data);
363
364 static void to_activated                (GtkWidget      *widget,
365                                          Compose        *compose);
366 static void newsgroups_activated        (GtkWidget      *widget,
367                                          Compose        *compose);
368 static void subject_activated           (GtkWidget      *widget,
369                                          Compose        *compose);
370 static void cc_activated                (GtkWidget      *widget,
371                                          Compose        *compose);
372 static void bcc_activated               (GtkWidget      *widget,
373                                          Compose        *compose);
374 static void replyto_activated           (GtkWidget      *widget,
375                                          Compose        *compose);
376 static void followupto_activated        (GtkWidget      *widget,
377                                          Compose        *compose);
378 static void compose_attach_parts(Compose * compose,
379                                  MsgInfo * msginfo);
380
381 static gchar *compose_quote_fmt         (Compose        *compose,
382                                          MsgInfo        *msginfo,
383                                          const gchar    *fmt,
384                                          const gchar    * qmark);
385
386 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
387                                   gboolean to_all,
388                                   gboolean ignore_replyto,
389                                   gboolean followup_and_reply_to);
390
391 static GtkItemFactoryEntry compose_popup_entries[] =
392 {
393         {N_("/_Add..."),        NULL, compose_attach_cb, 0, NULL},
394         {N_("/_Remove"),        NULL, compose_attach_remove_selected, 0, NULL},
395         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
396         {N_("/_Property..."),   NULL, compose_attach_property, 0, NULL}
397 };
398
399 static GtkItemFactoryEntry compose_entries[] =
400 {
401         {N_("/_File"),                          NULL, NULL, 0, "<Branch>"},
402         {N_("/_File/_Attach file"),             "<control>M", compose_attach_cb, 0, NULL},
403         {N_("/_File/_Insert file"),             "<control>I", compose_insert_file_cb, 0, NULL},
404         {N_("/_File/Insert si_gnature"),        "<control>G", compose_insert_sig, 0, NULL},
405         {N_("/_File/---"),                      NULL, NULL, 0, "<Separator>"},
406         {N_("/_File/_Close"),                   "<alt>W", compose_close_cb, 0, NULL},
407
408         {N_("/_Edit"),             NULL,         NULL,  0, "<Branch>"},
409         {N_("/_Edit/_Undo"),       "<control>Z", NULL,  0, NULL},
410         {N_("/_Edit/_Redo"),       "<control>Y", NULL,  0, NULL},
411         {N_("/_Edit/---"),         NULL,         NULL,  0, "<Separator>"},
412         {N_("/_Edit/Cu_t"),        "<control>X", compose_cut_cb,    0, NULL},
413         {N_("/_Edit/_Copy"),       "<control>C", compose_copy_cb,   0, NULL},
414         {N_("/_Edit/_Paste"),      "<control>V", compose_paste_cb,  0, NULL},
415         {N_("/_Edit/Select _all"), "<control>A", compose_allsel_cb, 0, NULL},
416         {N_("/_Edit/---"),         NULL,         NULL,  0, "<Separator>"},
417         {N_("/_Edit/Wrap long _lines"), "<alt>L", compose_wrap_line, 0, NULL},
418         {N_("/_Edit/Edit with e_xternal editor"), "<alt>X",
419                                         compose_ext_editor_cb,  0, NULL},
420
421         {N_("/_Message"),               NULL,           NULL,   0, "<Branch>"},
422         {N_("/_Message/_Send"),         "<shift><control>S",
423                                         compose_send_cb, 0, NULL},
424         {N_("/_Message/Send _later"),   "<shift><alt>S",
425                                         compose_send_later_cb,  0, NULL},
426         {N_("/_Message/Save to _draft folder"),
427                                         "<alt>D",       compose_draft_cb, 0, NULL},
428         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
429         {N_("/_Message/_To"),           NULL, compose_toggle_to_cb, 0, "<ToggleItem>"},
430         {N_("/_Message/_Cc"),           NULL, compose_toggle_cc_cb, 0, "<ToggleItem>"},
431         {N_("/_Message/_Bcc"),          NULL, compose_toggle_bcc_cb, 0, "<ToggleItem>"},
432         {N_("/_Message/_Reply to"),     NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
433         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
434         {N_("/_Message/_Followup to"),  NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
435         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
436         {N_("/_Message/_Attach"),       NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
437 #if USE_GPGME
438         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
439         {N_("/_Message/Si_gn"),         NULL, compose_toggle_sign_cb, 0, "<ToggleItem>"},
440         {N_("/_Message/_Encrypt"),      NULL, compose_toggle_encrypt_cb, 0, "<ToggleItem>"},
441 #endif /* USE_GPGME */
442         {N_("/_Message/---"),           NULL,           NULL,   0, "<Separator>"},
443         {N_("/_Message/_Request Return Receipt"),       NULL, compose_toggle_return_receipt_cb, 0, "<ToggleItem>"},
444         {N_("/_Tool"),                  NULL, NULL,     0, "<Branch>"},
445         {N_("/_Tool/Show _ruler"),      NULL, compose_toggle_ruler_cb,  0, "<ToggleItem>"},
446         {N_("/_Tool/_Address book"),    "<alt>A",       compose_address_cb, 0, NULL},
447         {N_("/_Help"),                  NULL, NULL,     0, "<LastBranch>"},
448         {N_("/_Help/_About"),           NULL,           about_show,     0, NULL}
449 };
450
451 static GtkTargetEntry compose_mime_types[] =
452 {
453         {"text/uri-list", 0, 0}
454 };
455
456 Compose * compose_new(PrefsAccount *account)
457 {
458         return compose_new_with_recipient(account, NULL);
459 }
460
461 Compose * compose_new_with_recipient(PrefsAccount *account, const gchar *to)
462 {
463         Compose *compose;
464
465         if (!account) account = cur_account;
466         g_return_val_if_fail(account != NULL, NULL);
467
468         compose = compose_create(account);
469         compose->mode = COMPOSE_NEW;
470
471         if (prefs_common.auto_sig)
472                 compose_insert_sig(compose);
473         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
474         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
475
476         if (account->protocol != A_NNTP) {
477                 if (to) {
478                         compose_entry_append(compose, to, COMPOSE_TO);
479                         gtk_widget_grab_focus(compose->subject_entry);
480                 } else
481                         gtk_widget_grab_focus(compose->to_entry);
482         } else {
483                 if (to) {
484                         compose_entry_append(compose, to, COMPOSE_NEWSGROUPS);
485                         gtk_widget_grab_focus(compose->subject_entry);
486                 } else
487                         gtk_widget_grab_focus(compose->newsgroups_entry);
488         }
489
490         return compose;
491 }
492
493 #define CHANGE_FLAGS(msginfo) \
494 { \
495 if (msginfo->folder->folder->change_flags != NULL) \
496 msginfo->folder->folder->change_flags(msginfo->folder->folder, \
497                                       msginfo->folder, \
498                                       msginfo); \
499 }
500
501 /*
502 Compose * compose_new_followup_and_replyto(PrefsAccount *account,
503                                            const gchar *followupto, gchar * to)
504 {
505         Compose *compose;
506
507         if (!account) account = cur_account;
508         g_return_val_if_fail(account != NULL, NULL);
509         g_return_val_if_fail(account->protocol != A_NNTP, NULL);
510
511         compose = compose_create(account);
512         compose->mode = COMPOSE_NEW;
513
514         if (prefs_common.auto_sig)
515                 compose_insert_sig(compose);
516         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
517         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
518
519         compose_entry_append(compose, to, COMPOSE_TO);
520         compose_entry_append(compose, followupto, COMPOSE_NEWSGROUPS);
521         gtk_widget_grab_focus(compose->subject_entry);
522
523         return compose;
524 }
525 */
526
527 void compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all,
528                    gboolean ignore_replyto)
529 {
530         compose_generic_reply(msginfo, quote, to_all, ignore_replyto, FALSE);
531 }
532
533 void compose_followup_and_reply_to(MsgInfo *msginfo, gboolean quote,
534                                    gboolean to_all,
535                                    gboolean ignore_replyto)
536 {
537         compose_generic_reply(msginfo, quote, to_all, ignore_replyto, TRUE);
538 }
539
540 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
541                                   gboolean to_all,
542                                   gboolean ignore_replyto,
543                                   gboolean followup_and_reply_to)
544 {
545         Compose *compose;
546         PrefsAccount *account;
547         PrefsAccount *reply_account;
548         GtkSText *text;
549
550         g_return_if_fail(msginfo != NULL);
551         g_return_if_fail(msginfo->folder != NULL);
552
553         account = msginfo->folder->folder->account;
554         if (!account && msginfo->to && prefs_common.reply_account_autoselect) {
555                 gchar *to;
556
557                 to = g_strdup(msginfo->to);
558                 extract_address(to);
559                 account = account_find_mail_from_address(to);
560                 g_free(to);
561         }
562         if (!account) account = cur_account;
563         g_return_if_fail(account != NULL);
564
565         if (ignore_replyto && account->protocol == A_NNTP &&
566             !followup_and_reply_to) {
567                 reply_account =
568                         account_find_mail_from_address(account->address);
569                 if (!reply_account)
570                         reply_account = compose_current_mail_account();
571                 if (!reply_account)
572                         return;
573         } else
574                 reply_account = account;
575
576         MSG_UNSET_FLAGS(msginfo->flags, MSG_FORWARDED);
577         MSG_SET_FLAGS(msginfo->flags, MSG_REPLIED);
578
579         CHANGE_FLAGS(msginfo);
580
581         compose = compose_create(reply_account);
582         compose->mode = COMPOSE_REPLY;
583
584
585         if (followup_and_reply_to) {
586                 gtk_widget_show(compose->to_hbox);
587                 gtk_widget_show(compose->to_entry);
588                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 1, 4);
589                 compose->use_to = TRUE;
590         }
591
592         if (compose_parse_header(compose, msginfo) < 0) return;
593         compose_reply_set_entry(compose, msginfo, to_all, ignore_replyto,
594                                 followup_and_reply_to);
595
596         text = GTK_STEXT(compose->text);
597         gtk_stext_freeze(text);
598
599         if (quote) {
600                 FILE *fp;
601                 gchar *quote_str;
602
603                 if ((fp = procmime_get_text_part(msginfo)) == NULL)
604                         g_warning(_("Can't get text part\n"));
605                 else {
606                         gchar * qmark;
607
608                         if (prefs_common.quotemark && *prefs_common.quotemark)
609                                 qmark = prefs_common.quotemark;
610                         else
611                                 qmark = "> ";
612
613                         quote_str = compose_quote_fmt(compose, msginfo,
614                                                       prefs_common.quotefmt,
615                                                       qmark);
616
617                         /*
618                         quote_str = compose_quote_parse_fmt
619                                 (compose, msginfo, prefs_common.quotefmt);
620                         */
621
622                         if (quote_str != NULL)
623                                 gtk_stext_insert(text, NULL, NULL, NULL,
624                                                  quote_str, -1);
625                         /*                      g_free(quote_str); */
626                         /* compose_quote_file(compose, msginfo, fp); */
627                         fclose(fp);
628                 }
629         }
630
631         if (prefs_common.auto_sig)
632                 compose_insert_sig(compose);
633         gtk_editable_set_position(GTK_EDITABLE(text), 0);
634         gtk_stext_set_point(text, 0);
635
636         gtk_stext_thaw(text);
637         gtk_widget_grab_focus(compose->text);
638 }
639
640
641 static gchar *procmime_get_file_name(MimeInfo *mimeinfo)
642 {
643         gchar *base;
644         gchar *filename;
645
646         g_return_val_if_fail(mimeinfo != NULL, NULL);
647
648         base = mimeinfo->filename ? mimeinfo->filename
649                 : mimeinfo->name ? mimeinfo->name : NULL;
650
651         if (MIME_TEXT_HTML == mimeinfo->mime_type && base == NULL){
652                 filename = g_strdup_printf("%s%smimetmp%i.html",
653                                            get_mime_tmp_dir(),
654                                            G_DIR_SEPARATOR_S,
655                                            mimeinfo);
656                 return filename;
657         }
658         else {
659                 base = base ? base : "";
660                 base = g_basename(base);
661                 if (*base == '\0') {
662                         filename = g_strdup_printf("%s%smimetmp%i",
663                                                    get_mime_tmp_dir(),
664                                                    G_DIR_SEPARATOR_S,
665                                                    mimeinfo);
666                         return filename;
667                 }
668         }
669
670         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
671                                base, NULL);
672
673         return filename;
674 }
675
676 static gchar * mime_extract_file(gchar * source, MimeInfo *partinfo)
677 {
678         gchar *filename;
679
680         if (!partinfo) return;
681
682         filename = procmime_get_file_name(partinfo);
683
684         if (procmime_get_part(filename, source, partinfo) < 0)
685                 alertpanel_error
686                         (_("Can't get the part of multipart message."));
687
688         return filename;
689 }
690
691 static void compose_attach_parts(Compose * compose,
692                                  MsgInfo * msginfo)
693 {
694
695         FILE *fp;
696         gchar *file;
697         MimeInfo *mimeinfo;
698         MsgInfo *tmpmsginfo;
699         gchar *p;
700         gchar *boundary;
701         gint boundary_len = 0;
702         gchar buf[BUFFSIZE];
703         glong fpos, prev_fpos;
704         gint npart;
705         gchar * source;
706         gchar * filename;
707
708         g_return_if_fail(msginfo != NULL);
709         
710 #if USE_GPGME
711         for (;;) {
712                 if ((fp = procmsg_open_message(msginfo)) == NULL) return;
713                 mimeinfo = procmime_scan_mime_header(fp);
714                 if (!mimeinfo) break;
715
716                 if (!MSG_IS_ENCRYPTED(msginfo->flags) &&
717                     rfc2015_is_encrypted(mimeinfo)) {
718                         MSG_SET_FLAGS(msginfo->flags, MSG_ENCRYPTED);
719                 }
720                 if (MSG_IS_ENCRYPTED(msginfo->flags) &&
721                     !msginfo->plaintext_file  &&
722                     !msginfo->decryption_failed) {
723                         rfc2015_decrypt_message(msginfo, mimeinfo, fp);
724                         if (msginfo->plaintext_file &&
725                             !msginfo->decryption_failed) {
726                                 fclose(fp);
727                                 continue;
728                         }
729                 }
730                 
731                 break;
732         }
733 #else /* !USE_GPGME */
734         if ((fp = procmsg_open_message(msginfo)) == NULL) return;
735         mimeinfo = procmime_scan_mime_header(fp);
736 #endif /* USE_GPGME */
737
738         fclose(fp);
739         if (!mimeinfo) return;
740         if (mimeinfo->mime_type == MIME_TEXT)
741                 return;
742
743         if ((fp = procmsg_open_message(msginfo)) == NULL) return;
744
745         g_return_if_fail(mimeinfo != NULL);
746         g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
747
748         if (mimeinfo->mime_type == MIME_MULTIPART) {
749                 g_return_if_fail(mimeinfo->boundary != NULL);
750                 g_return_if_fail(mimeinfo->sub == NULL);
751         }
752         g_return_if_fail(fp != NULL);
753
754         boundary = mimeinfo->boundary;
755
756         if (boundary) {
757                 boundary_len = strlen(boundary);
758
759                 /* look for first boundary */
760                 while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
761                         if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
762                 if (!p) {
763                         fclose(fp);
764                         return;
765                 }
766         }
767
768         if ((fpos = ftell(fp)) < 0) {
769                 perror("ftell");
770                 fclose(fp);
771                 return;
772         }
773
774         for (npart = 0;; npart++) {
775                 MimeInfo *partinfo;
776                 gboolean eom = FALSE;
777
778                 prev_fpos = fpos;
779
780                 partinfo = procmime_scan_mime_header(fp);
781                 if (!partinfo) break;
782
783                 if (npart != 0)
784                         procmime_mimeinfo_insert(mimeinfo, partinfo);
785                 else
786                         procmime_mimeinfo_free(partinfo);
787
788                 /* look for next boundary */
789                 buf[0] = '\0';
790                 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
791                         if (IS_BOUNDARY(buf, boundary, boundary_len)) {
792                                 if (buf[2 + boundary_len]     == '-' &&
793                                     buf[2 + boundary_len + 1] == '-')
794                                         eom = TRUE;
795                                 break;
796                         }
797                 }
798                 if (p == NULL)
799                         eom = TRUE;     /* broken MIME message */
800                 fpos = ftell(fp);
801
802                 partinfo->size = fpos - prev_fpos - strlen(buf);
803
804                 if (eom) break;
805         }
806
807         source = procmsg_get_message_file_path(msginfo);
808
809         g_return_if_fail(mimeinfo != NULL);
810
811         if (!mimeinfo->main && mimeinfo->parent)
812                 {
813                         filename = mime_extract_file(source, mimeinfo);
814
815                         compose_attach_append_with_type(compose, filename,
816                                                         mimeinfo->content_type,
817                                                         mimeinfo->mime_type);
818
819                         g_free(filename);
820                 }
821
822         if (mimeinfo->sub && mimeinfo->sub->children)
823                 {
824                         filename = mime_extract_file(source, mimeinfo->sub);
825
826                         compose_attach_append_with_type(compose, filename,
827                                                         mimeinfo->content_type,
828                                                         mimeinfo->sub->mime_type);
829
830                         g_free(filename);
831                 }
832
833         if (mimeinfo->children) {
834                 MimeInfo *child;
835
836                 child = mimeinfo->children;
837                 while (child) {
838                         filename = mime_extract_file(source, child);
839
840                         compose_attach_append_with_type(compose, filename,
841                                                         child->content_type,
842                                                         child->mime_type);
843
844                         g_free(filename);
845
846                         child = child->next;
847                 }
848         }
849
850         fclose(fp);
851
852         procmime_mimeinfo_free_all(mimeinfo);
853 }
854
855
856 #define INSERT_FW_HEADER(var, hdr) \
857 if (msginfo->var && *msginfo->var) { \
858         gtk_stext_insert(text, NULL, NULL, NULL, hdr, -1); \
859         gtk_stext_insert(text, NULL, NULL, NULL, msginfo->var, -1); \
860         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1); \
861 }
862
863 Compose * compose_forward(PrefsAccount * account, MsgInfo *msginfo,
864                           gboolean as_attach)
865 {
866         Compose *compose;
867         /*      PrefsAccount *account; */
868         GtkSText *text;
869         FILE *fp;
870         gchar buf[BUFFSIZE];
871
872         g_return_val_if_fail(msginfo != NULL, NULL);
873         g_return_val_if_fail(msginfo->folder != NULL, NULL);
874
875         if (account == NULL) {
876                 account = cur_account;
877                 /*
878                 account = msginfo->folder->folder->account;
879                 if (!account) account = cur_account;
880                 */
881         }
882         g_return_val_if_fail(account != NULL, NULL);
883
884         MSG_UNSET_FLAGS(msginfo->flags, MSG_REPLIED);
885         MSG_SET_FLAGS(msginfo->flags, MSG_FORWARDED);
886
887         CHANGE_FLAGS(msginfo);
888
889         compose = compose_create(account);
890         compose->mode = COMPOSE_FORWARD;
891
892         if (msginfo->subject && *msginfo->subject) {
893                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Fw: ");
894                 gtk_entry_append_text(GTK_ENTRY(compose->subject_entry),
895                                       msginfo->subject);
896         }
897
898         text = GTK_STEXT(compose->text);
899         gtk_stext_freeze(text);
900
901         if (as_attach) {
902                 gchar *msgfile;
903
904                 msgfile = procmsg_get_message_file_path(msginfo);
905                 if (!is_file_exist(msgfile))
906                         g_warning(_("%s: file not exist\n"), msgfile);
907                 else
908                         compose_attach_append(compose, msgfile,
909                                               MIME_MESSAGE_RFC822);
910
911                 g_free(msgfile);
912         } else {
913                 FILE *fp;
914                 gchar *quote_str;
915
916                 if ((fp = procmime_get_text_part(msginfo)) == NULL)
917                         g_warning(_("Can't get text part\n"));
918                 else {
919                         gchar * qmark;
920
921                         if (prefs_common.fw_quotemark &&
922                             *prefs_common.fw_quotemark)
923                                 qmark = prefs_common.fw_quotemark;
924                         else
925                                 qmark = "> ";
926
927                         quote_str = compose_quote_fmt(compose, msginfo,
928                                                       prefs_common.fw_quotefmt,
929                                                       qmark);
930
931                         if (quote_str != NULL)
932                                 gtk_stext_insert(text, NULL, NULL, NULL,
933                                                  quote_str, -1);
934
935                         fclose(fp);
936                 }
937
938                 compose_attach_parts(compose, msginfo);
939         }
940
941         if (prefs_common.auto_sig)
942                 compose_insert_sig(compose);
943         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
944         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
945
946         gtk_stext_thaw(text);
947         if (account->protocol != A_NNTP)
948                 gtk_widget_grab_focus(compose->to_entry);
949         else
950                 gtk_widget_grab_focus(compose->newsgroups_entry);
951
952         return compose;
953 }
954
955 #undef INSERT_FW_HEADER
956
957 Compose * compose_forward_multiple(PrefsAccount * account, 
958                           GSList *msginfo_list) 
959 {
960         Compose *compose;
961         GtkSText *text;
962         GSList *msginfo;
963         gchar *msgfile;
964
965         g_return_val_if_fail(msginfo_list != NULL, NULL);
966         
967         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
968                 if ( ((MsgInfo *)msginfo->data)->folder == NULL )
969                         return NULL;
970         }
971         
972         if (account == NULL) {
973                 account = cur_account;
974                 /*
975                 account = msginfo->folder->folder->account;
976                 if (!account) account = cur_account;
977                 */
978         }
979         g_return_val_if_fail(account != NULL, NULL);
980
981         compose = compose_create(account);
982         compose->mode = COMPOSE_FORWARD;
983
984         text = GTK_STEXT(compose->text);
985         gtk_stext_freeze(text);
986
987         for (msginfo = msginfo_list; msginfo != NULL; msginfo = msginfo->next) {
988                 msgfile = procmsg_get_message_file_path((MsgInfo *)msginfo->data);
989                 if (!is_file_exist(msgfile))
990                         g_warning(_("%s: file not exist\n"), msgfile);
991                 else
992                         compose_attach_append(compose, msgfile,
993                                 MIME_MESSAGE_RFC822);
994                 g_free(msgfile);
995         }
996
997         if (prefs_common.auto_sig)
998                 compose_insert_sig(compose);
999         gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
1000         gtk_stext_set_point(GTK_STEXT(compose->text), 0);
1001
1002         gtk_stext_thaw(text);
1003         if (account->protocol != A_NNTP)
1004                 gtk_widget_grab_focus(compose->to_entry);
1005         else
1006                 gtk_widget_grab_focus(compose->newsgroups_entry);
1007
1008         return compose;
1009 }
1010
1011 void compose_reedit(MsgInfo *msginfo)
1012 {
1013         Compose *compose;
1014         PrefsAccount *account;
1015         GtkSText *text;
1016         FILE *fp;
1017         gchar buf[BUFFSIZE];
1018
1019         g_return_if_fail(msginfo != NULL);
1020         g_return_if_fail(msginfo->folder != NULL);
1021
1022         account = msginfo->folder->folder->account;
1023         if (!account) account = cur_account;
1024         g_return_if_fail(account != NULL);
1025
1026         compose = compose_create(account);
1027         compose->mode = COMPOSE_REEDIT_DRAFT;
1028         compose->targetinfo = procmsg_msginfo_copy(msginfo);
1029
1030         if (compose_parse_header(compose, msginfo) < 0) return;
1031         compose_reedit_set_entry(compose, msginfo);
1032
1033         text = GTK_STEXT(compose->text);
1034         gtk_stext_freeze(text);
1035
1036         if ((fp = procmime_get_text_part(msginfo)) == NULL)
1037                 g_warning(_("Can't get text part\n"));
1038         else {
1039                 while (fgets(buf, sizeof(buf), fp) != NULL)
1040                         gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1041                 fclose(fp);
1042         }
1043         compose_attach_parts(compose, msginfo);
1044
1045         gtk_stext_thaw(text);
1046         gtk_widget_grab_focus(compose->text);
1047 }
1048
1049 GList *compose_get_compose_list(void)
1050 {
1051         return compose_list;
1052 }
1053
1054 void compose_entry_append(Compose *compose, const gchar *address,
1055                           ComposeEntryType type)
1056 {
1057         GtkEntry *entry;
1058         const gchar *text;
1059
1060         if (!address || *address == '\0') return;
1061
1062         switch (type) {
1063         case COMPOSE_CC:
1064                 entry = GTK_ENTRY(compose->cc_entry);
1065                 break;
1066         case COMPOSE_BCC:
1067                 entry = GTK_ENTRY(compose->bcc_entry);
1068                 break;
1069         case COMPOSE_NEWSGROUPS:
1070                 entry = GTK_ENTRY(compose->newsgroups_entry);
1071                 break;
1072         case COMPOSE_TO:
1073         default:
1074                 entry = GTK_ENTRY(compose->to_entry);
1075                 break;
1076         }
1077
1078         text = gtk_entry_get_text(entry);
1079         if (*text != '\0')
1080                 gtk_entry_append_text(entry, ", ");
1081         gtk_entry_append_text(entry, address);
1082 }
1083
1084 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
1085 {
1086         static HeaderEntry hentry[] = {{"Reply-To:",    NULL, TRUE},
1087                                        {"Cc:",          NULL, FALSE},
1088                                        {"References:",  NULL, FALSE},
1089                                        {"Bcc:",         NULL, FALSE},
1090                                        {"Newsgroups:",  NULL, FALSE},
1091                                        {"Followup-To:", NULL, FALSE},
1092                                        {NULL,           NULL, FALSE}};
1093
1094         enum
1095         {
1096                 H_REPLY_TO      = 0,
1097                 H_CC            = 1,
1098                 H_REFERENCES    = 2,
1099                 H_BCC           = 3,
1100                 H_NEWSGROUPS    = 4,
1101                 H_FOLLOWUP_TO   = 5
1102         };
1103
1104         FILE *fp;
1105
1106         g_return_val_if_fail(msginfo != NULL, -1);
1107
1108         if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
1109         procheader_get_header_fields(fp, hentry);
1110         fclose(fp);
1111
1112         if (hentry[H_REPLY_TO].body != NULL) {
1113                 conv_unmime_header_overwrite(hentry[H_REPLY_TO].body);
1114                 compose->replyto = hentry[H_REPLY_TO].body;
1115                 hentry[H_REPLY_TO].body = NULL;
1116         }
1117         if (hentry[H_CC].body != NULL) {
1118                 conv_unmime_header_overwrite(hentry[H_CC].body);
1119                 compose->cc = hentry[H_CC].body;
1120                 hentry[H_CC].body = NULL;
1121         }
1122         if (hentry[H_REFERENCES].body != NULL) {
1123                 if (compose->mode == COMPOSE_REEDIT_DRAFT)
1124                         compose->references = hentry[H_REFERENCES].body;
1125                 else {
1126                         compose->references = compose_parse_references
1127                                 (hentry[H_REFERENCES].body, msginfo->msgid);
1128                         g_free(hentry[H_REFERENCES].body);
1129                 }
1130                 hentry[H_REFERENCES].body = NULL;
1131         }
1132         if (hentry[H_BCC].body != NULL) {
1133                 if (compose->mode == COMPOSE_REEDIT_DRAFT) {
1134                         conv_unmime_header_overwrite(hentry[H_BCC].body);
1135                         compose->bcc = hentry[H_BCC].body;
1136                 } else
1137                         g_free(hentry[H_BCC].body);
1138                 hentry[H_BCC].body = NULL;
1139         }
1140         if (hentry[H_NEWSGROUPS].body != NULL) {
1141                 compose->newsgroups = hentry[H_NEWSGROUPS].body;
1142                 hentry[H_NEWSGROUPS].body = NULL;
1143         }
1144         if (hentry[H_FOLLOWUP_TO].body != NULL) {
1145                 conv_unmime_header_overwrite(hentry[H_FOLLOWUP_TO].body);
1146                 compose->followup_to = hentry[H_FOLLOWUP_TO].body;
1147                 hentry[H_FOLLOWUP_TO].body = NULL;
1148         }
1149
1150         if (compose->mode == COMPOSE_REEDIT_DRAFT && msginfo->inreplyto)
1151                 compose->inreplyto = g_strdup(msginfo->inreplyto);
1152         else if (compose->mode != COMPOSE_REEDIT_DRAFT &&
1153                  msginfo->msgid && *msginfo->msgid) {
1154                 compose->inreplyto = g_strdup(msginfo->msgid);
1155
1156                 if (!compose->references) {
1157                         if (msginfo->inreplyto && *msginfo->inreplyto)
1158                                 compose->references =
1159                                         g_strdup_printf("<%s>\n\t<%s>",
1160                                                         msginfo->inreplyto,
1161                                                         msginfo->msgid);
1162                         else
1163                                 compose->references =
1164                                         g_strconcat("<", msginfo->msgid, ">",
1165                                                     NULL);
1166                 }
1167         }
1168
1169         return 0;
1170 }
1171
1172 static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
1173 {
1174         GSList *ref_id_list, *cur;
1175         GString *new_ref;
1176         gchar *new_ref_str;
1177
1178         ref_id_list = references_list_append(NULL, ref);
1179         if (!ref_id_list) return NULL;
1180         if (msgid && *msgid)
1181                 ref_id_list = g_slist_append(ref_id_list, g_strdup(msgid));
1182
1183         for (;;) {
1184                 gint len = 0;
1185
1186                 for (cur = ref_id_list; cur != NULL; cur = cur->next)
1187                         /* "<" + Message-ID + ">" + CR+LF+TAB */
1188                         len += strlen((gchar *)cur->data) + 5;
1189
1190                 if (len > MAX_REFERENCES_LEN) {
1191                         /* remove second message-ID */
1192                         if (ref_id_list && ref_id_list->next &&
1193                             ref_id_list->next->next) {
1194                                 g_free(ref_id_list->next->data);
1195                                 ref_id_list = g_slist_remove
1196                                         (ref_id_list, ref_id_list->next->data);
1197                         } else {
1198                                 slist_free_strings(ref_id_list);
1199                                 g_slist_free(ref_id_list);
1200                                 return NULL;
1201                         }
1202                 } else
1203                         break;
1204         }
1205
1206         new_ref = g_string_new("");
1207         for (cur = ref_id_list; cur != NULL; cur = cur->next) {
1208                 if (new_ref->len > 0)
1209                         g_string_append(new_ref, "\n\t");
1210                 g_string_sprintfa(new_ref, "<%s>", (gchar *)cur->data);
1211         }
1212
1213         slist_free_strings(ref_id_list);
1214         g_slist_free(ref_id_list);
1215
1216         new_ref_str = new_ref->str;
1217         g_string_free(new_ref, FALSE);
1218
1219         return new_ref_str;
1220 }
1221
1222 /*
1223 static void compose_quote_file(Compose *compose, MsgInfo *msginfo, FILE *fp)
1224 {
1225         GtkSText *text = GTK_STEXT(compose->text);
1226         gchar *qmark;
1227         gchar *quote_str;
1228         GdkColor *qcolor = NULL;
1229         gchar buf[BUFFSIZE];
1230         gint qlen;
1231         gchar *linep, *cur, *leftp;
1232         gint line_len, cur_len;
1233         gint wrap_len;
1234         gint str_len;
1235         gint ch_len;
1236
1237         // if (prefs_common.enable_color) qcolor = &quote_color;
1238         if (prefs_common.quotemark && *prefs_common.quotemark)
1239                 qmark = prefs_common.quotemark;
1240         else
1241                 qmark = "> ";
1242         quote_str = compose_quote_parse_fmt(compose, msginfo, qmark);
1243         g_return_if_fail(quote_str != NULL);
1244         qlen = strlen(quote_str);
1245
1246         if (!prefs_common.linewrap_quote ||
1247             prefs_common.linewrap_len <= qlen) {
1248                 while (fgets(buf, sizeof(buf), fp) != NULL) {
1249                         gtk_stext_insert(text, NULL, qcolor, NULL,
1250                                         quote_str, -1);
1251                         gtk_stext_insert(text, NULL, qcolor, NULL, buf, -1);
1252                 }
1253                 g_free(quote_str);
1254                 return;
1255         }
1256
1257         wrap_len = prefs_common.linewrap_len - qlen;
1258
1259         while (fgets(buf, sizeof(buf), fp) != NULL) {
1260                 strretchomp(buf);
1261                 str_len = strlen(buf);
1262
1263                 if (str_len <= wrap_len) {
1264                         gtk_stext_insert(text, NULL, qcolor, NULL,
1265                                         quote_str, -1);
1266                         gtk_stext_insert(text, NULL, qcolor, NULL, buf, -1);
1267                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
1268                         continue;
1269                 }
1270
1271                 linep = cur = leftp = buf;
1272                 line_len = cur_len = 0;
1273
1274                 while (*cur != '\0') {
1275                         ch_len = mblen(cur, MB_CUR_MAX);
1276                         if (ch_len < 0) ch_len = 1;
1277
1278                         if (ch_len == 1 && isspace(*cur)) {
1279                                 linep = cur + ch_len;
1280                                 line_len = cur_len + ch_len;
1281                         }
1282
1283                         if (cur_len + ch_len > wrap_len && line_len > 0) {
1284                                 gtk_stext_insert(text, NULL, qcolor, NULL,
1285                                                 quote_str, -1);
1286
1287                                 if (isspace(*(linep - 1)))
1288                                         gtk_stext_insert(text, NULL,
1289                                                         qcolor, NULL,
1290                                                         leftp, line_len - 1);
1291                                 else
1292                                         gtk_stext_insert(text, NULL,
1293                                                         qcolor, NULL,
1294                                                         leftp, line_len);
1295                                 gtk_stext_insert(text, NULL, NULL, NULL,
1296                                                 "\n", 1);
1297
1298                                 leftp = linep;
1299                                 cur_len = cur_len - line_len + ch_len;
1300                                 line_len = 0;
1301                                 cur += ch_len;
1302                                 continue;
1303                         }
1304
1305                         if (ch_len > 1) {
1306                                 linep = cur + ch_len;
1307                                 line_len = cur_len + ch_len;
1308                         }
1309                         cur_len += ch_len;
1310                         cur += ch_len;
1311                 }
1312
1313                 if (*leftp) {
1314                         gtk_stext_insert(text, NULL, qcolor, NULL,
1315                                         quote_str, -1);
1316                         gtk_stext_insert(text, NULL, qcolor, NULL, leftp, -1);
1317                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
1318                 }
1319         }
1320
1321         g_free(quote_str);
1322 }
1323 */
1324
1325 /*
1326 static gchar *compose_quote_parse_fmt(Compose *compose, MsgInfo *msginfo,
1327                                       const gchar *fmt)
1328 {
1329         gchar *ext_str;
1330         size_t buf_len = 1024;
1331         size_t ext_len = 0;
1332         gchar *str;
1333         gchar *mbs;
1334         wchar_t *wcsfmt;
1335         wchar_t *sp;
1336         gchar tmp[3];
1337
1338         if (!fmt || *fmt == '\0') return 0;
1339
1340         Xalloca(mbs, sizeof(wchar_t) + 1, return 0);
1341         Xalloca(wcsfmt, (strlen(fmt) + 1) * sizeof(wchar_t), return 0);
1342         mbstowcs(wcsfmt, fmt, strlen(fmt) + 1);
1343         sp = wcsfmt;
1344
1345         ext_str = g_malloc(sizeof(gchar) * buf_len);
1346         g_return_val_if_fail(ext_str != NULL, NULL);
1347
1348         while (*sp) {
1349                 gint len;
1350
1351                 len = wctomb(mbs, *sp);
1352                 mbs[len] = '\0';
1353
1354                 if (*mbs == '%') {
1355                         gchar *p;
1356
1357                         wctomb(mbs, *(++sp));
1358                         str = NULL;
1359
1360                         switch (*mbs) {
1361                         case 'd':
1362                                 str = msginfo->date;
1363                                 sp++;
1364                                 break;
1365                         case 'f':
1366                                 str = msginfo->from;
1367                                 sp++;
1368                                 break;
1369                         case 'I':
1370                                 if (!msginfo->fromname) {sp++; break;}
1371                                 p = msginfo->fromname;
1372                                 tmp[0] = tmp[1] = tmp[2] = '\0';
1373
1374                                 if (*p && isalnum(*p))
1375                                         tmp[0] = toupper(*p);
1376                                 else {
1377                                         sp++;
1378                                         break;
1379                                 }
1380
1381                                 while (*p) {
1382                                         while (*p && !isspace(*p)) p++;
1383                                         while (*p && isspace(*p)) p++;
1384                                         if (*p && isalnum(*p))
1385                                                 tmp[1] = toupper(*p);
1386                                 }
1387
1388                                 if (tmp[1]) str = tmp;
1389                                 sp++;
1390                                 break;
1391                         case 'n':
1392                                 str = msginfo->fromname;
1393                                 sp++;
1394                                 break;
1395                         case 'N':
1396                                 if (!msginfo->fromname) {sp++; break;}
1397                                 Xstrdup_a(str, msginfo->fromname,
1398                                           {sp++; break;});
1399                                 p = str;
1400                                 while (*p && !isspace(*p)) p++;
1401                                 *p = '\0';
1402                                 sp++;
1403                                 break;
1404                         case 's':
1405                                 str = msginfo->subject;
1406                                 sp++;
1407                                 break;
1408                         case 't':
1409                                 str = msginfo->to;
1410                                 sp++;
1411                                 break;
1412                         case 'i':
1413                                 if (!msginfo->msgid) {sp++; break;}
1414                                 Xalloca(str, strlen(msginfo->msgid) + 3,
1415                                         {sp++; break;});
1416                                 g_snprintf(str, strlen(msginfo->msgid) + 3,
1417                                            "<%s>", msginfo->msgid);
1418                                 sp++;
1419                                 break;
1420                         case '%':
1421                                 str = "%";
1422                                 sp++;
1423                                 break;
1424                         default:
1425                                 break;
1426                         }
1427
1428                         if (str) {
1429                                 while (ext_len + strlen(str) + 1 > buf_len)
1430                                         buf_len += 1024;
1431                                 ext_str = g_realloc(ext_str,
1432                                                     sizeof(gchar) * buf_len);
1433                                 g_return_val_if_fail(ext_str != NULL, NULL);
1434                                 strcpy(ext_str + ext_len, str);
1435                                 ext_len += strlen(str);
1436                         }
1437                 } else if (*mbs == '\\') {
1438                         wctomb(mbs, *(++sp));
1439                         str = NULL;
1440
1441                         switch (*mbs) {
1442                         case 'n':
1443                                 str = "\n";
1444                                 break;
1445                         case 't':
1446                                 str = "\t";
1447                                 break;
1448                         case '\\':
1449                                 str = "\\";
1450                                 break;
1451                         default:
1452                                 break;
1453                         }
1454
1455                         if (str) {
1456                                 while (ext_len + strlen(str) + 1 > buf_len)
1457                                         buf_len += 1024;
1458                                 ext_str = g_realloc(ext_str,
1459                                                     sizeof(gchar) * buf_len);
1460                                 g_return_val_if_fail(ext_str != NULL, NULL);
1461                                 strcpy(ext_str + ext_len, str);
1462                                 ext_len += strlen(str);
1463                                 sp++;
1464                         }
1465                 } else {
1466                         while (ext_len + len + 1 > buf_len) buf_len += 1024;
1467                         ext_str = g_realloc(ext_str, sizeof(gchar) * buf_len);
1468                         g_return_val_if_fail(ext_str != NULL, NULL);
1469                         strcpy(ext_str + ext_len, mbs);
1470                         ext_len += len;
1471                         sp++;
1472                 }
1473         }
1474
1475         if (ext_str)
1476                 ext_str = g_realloc(ext_str, strlen(ext_str) + 1);
1477
1478         return ext_str;
1479 }
1480 */
1481
1482 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
1483                                     gboolean to_all, gboolean ignore_replyto,
1484                                     gboolean followup_and_reply_to)
1485 {
1486         GSList *cc_list;
1487         GSList *cur;
1488         gchar *from;
1489         GHashTable *to_table;
1490
1491         g_return_if_fail(compose->account != NULL);
1492         g_return_if_fail(msginfo != NULL);
1493
1494         if ((compose->account->protocol != A_NNTP) || followup_and_reply_to)
1495                 gtk_entry_set_text(GTK_ENTRY(compose->to_entry),
1496                                    ( (compose->replyto && !ignore_replyto) 
1497                                      ? compose->replyto
1498                                      : msginfo->from ? msginfo->from : ""));
1499         if (compose->account->protocol == A_NNTP)
1500                 gtk_entry_set_text(GTK_ENTRY(compose->newsgroups_entry),
1501                                    compose->followup_to ? compose->followup_to
1502                                    : compose->newsgroups ? compose->newsgroups
1503                                    : "");
1504
1505         if (msginfo->subject && *msginfo->subject) {
1506                 gchar *buf, *buf2, *p;
1507
1508                 buf = g_strdup(msginfo->subject);
1509                 while (!strncasecmp(buf, "Re:", 3)) {
1510                         p = buf + 3;
1511                         while (isspace(*p)) p++;
1512                         memmove(buf, p, strlen(p) + 1);
1513                 }
1514
1515                 buf2 = g_strdup_printf("Re: %s", buf);
1516                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1517                 g_free(buf2);
1518                 g_free(buf);
1519         } else
1520                 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
1521
1522         if (!to_all || compose->account->protocol == A_NNTP) return;
1523
1524         from = g_strdup(compose->replyto ? compose->replyto :
1525                         msginfo->from ? msginfo->from : "");
1526         extract_address(from);
1527
1528         cc_list = address_list_append(NULL, msginfo->to);
1529         cc_list = address_list_append(cc_list, compose->cc);
1530
1531         to_table = g_hash_table_new(g_str_hash, g_str_equal);
1532         g_hash_table_insert(to_table, from, GINT_TO_POINTER(1));
1533         if (compose->account)
1534                 g_hash_table_insert(to_table, compose->account->address,
1535                                     GINT_TO_POINTER(1));
1536
1537         /* remove address on To: and that of current account */
1538         for (cur = cc_list; cur != NULL; ) {
1539                 GSList *next = cur->next;
1540
1541                 if (g_hash_table_lookup(to_table, cur->data) != NULL)
1542                         cc_list = g_slist_remove(cc_list, cur->data);
1543                 else
1544                         g_hash_table_insert(to_table, cur->data, cur);
1545
1546                 cur = next;
1547         }
1548         g_hash_table_destroy(to_table);
1549         g_free(from);
1550
1551         if (cc_list) {
1552                 for (cur = cc_list; cur != NULL; cur = cur->next)
1553                         compose_entry_append(compose, (gchar *)cur->data,
1554                                              COMPOSE_CC);
1555                 slist_free_strings(cc_list);
1556                 g_slist_free(cc_list);
1557         }
1558 }
1559
1560 #define SET_ENTRY(entry, str) \
1561 { \
1562         if (str && *str) \
1563                 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
1564 }
1565
1566 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
1567 {
1568         g_return_if_fail(msginfo != NULL);
1569
1570         SET_ENTRY(to_entry, msginfo->to);
1571         SET_ENTRY(subject_entry, msginfo->subject);
1572         SET_ENTRY(cc_entry, compose->cc);
1573         SET_ENTRY(bcc_entry, compose->bcc);
1574         SET_ENTRY(reply_entry, compose->replyto);
1575
1576         if (compose->bcc) {
1577                 GtkItemFactory *ifactory;
1578                 GtkWidget *menuitem;
1579
1580                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1581                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Bcc");
1582                 gtk_check_menu_item_set_active
1583                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1584         }
1585         if (compose->replyto) {
1586                 GtkItemFactory *ifactory;
1587                 GtkWidget *menuitem;
1588
1589                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1590                 menuitem = gtk_item_factory_get_item
1591                         (ifactory, "/Message/Reply to");
1592                 gtk_check_menu_item_set_active
1593                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1594         }
1595 }
1596
1597 #undef SET_ENTRY
1598
1599 static void compose_insert_sig(Compose *compose)
1600 {
1601         gchar *sigfile;
1602
1603         if (compose->account && compose->account->sig_path)
1604                 sigfile = g_strdup(compose->account->sig_path);
1605         else {
1606                 sigfile = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1607                                       DEFAULT_SIGNATURE, NULL);
1608         }
1609
1610         if (!is_file_or_fifo_exist(sigfile)) {
1611                 g_free(sigfile);
1612                 return;
1613         }
1614
1615         gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, "\n\n", 2);
1616         if (prefs_common.sig_sep) {
1617                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1618                                 prefs_common.sig_sep, -1);
1619                 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1620                                 "\n", 1);
1621         }
1622
1623         compose_insert_file(compose, sigfile);
1624         g_free(sigfile);
1625 }
1626
1627 static void compose_insert_file(Compose *compose, const gchar *file)
1628 {
1629         GtkSText *text = GTK_STEXT(compose->text);
1630         gchar buf[BUFFSIZE];
1631         FILE *fp;
1632
1633         g_return_if_fail(file != NULL);
1634
1635         if ((fp = fopen(file, "r")) == NULL) {
1636                 FILE_OP_ERROR(file, "fopen");
1637                 return;
1638         }
1639
1640         gtk_stext_freeze(text);
1641
1642         while (fgets(buf, sizeof(buf), fp) != NULL)
1643                 gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1644
1645         gtk_stext_thaw(text);
1646
1647         fclose(fp);
1648 }
1649
1650 static void compose_attach_info(Compose * compose, AttachInfo * ainfo,
1651                                 ContentType cnttype)
1652 {
1653         gchar *text[N_ATTACH_COLS];
1654         gint row;
1655
1656         text[COL_MIMETYPE] = ainfo->content_type;
1657         text[COL_SIZE] = to_human_readable(ainfo->size);
1658         text[COL_NAME] = ainfo->name;
1659
1660         row = gtk_clist_append(GTK_CLIST(compose->attach_clist), text);
1661         gtk_clist_set_row_data(GTK_CLIST(compose->attach_clist), row, ainfo);
1662
1663         if (cnttype != MIME_MESSAGE_RFC822)
1664                 compose_changed_cb(NULL, compose);
1665 }
1666
1667 static void compose_attach_append_with_type(Compose *compose,
1668                                             const gchar *file,
1669                                             const gchar *type,
1670                                             ContentType cnttype)
1671 {
1672         AttachInfo *ainfo;
1673         off_t size;
1674
1675         if (!is_file_exist(file)) {
1676                 g_warning(_("File %s doesn't exist\n"), file);
1677                 return;
1678         }
1679         if ((size = get_file_size(file)) < 0) {
1680                 g_warning(_("Can't get file size of %s\n"), file);
1681                 return;
1682         }
1683         if (size == 0) {
1684                 alertpanel_notice(_("File %s is empty\n"), file);
1685                 return;
1686         }
1687
1688         if (!compose->use_attach) {
1689                 GtkItemFactory *ifactory;
1690                 GtkWidget *menuitem;
1691
1692                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1693                 menuitem = gtk_item_factory_get_item(ifactory,
1694                                                      "/Message/Attach");
1695                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1696                                                TRUE);
1697         }
1698
1699         ainfo = g_new0(AttachInfo, 1);
1700         ainfo->file = g_strdup(file);
1701
1702         if (cnttype == MIME_MESSAGE_RFC822) {
1703                 ainfo->encoding = ENC_7BIT;
1704                 ainfo->name = g_strdup_printf(_("Message: %s"),
1705                                               g_basename(file));
1706         } else {
1707                 ainfo->encoding = ENC_BASE64;
1708                 ainfo->name = g_strdup(g_basename(file));
1709         }
1710
1711         ainfo->content_type = g_strdup(type);
1712         ainfo->size = size;
1713
1714         compose_attach_info(compose, ainfo, cnttype);
1715 }
1716
1717 static void compose_attach_append(Compose *compose, const gchar *file,
1718                                   ContentType cnttype)
1719 {
1720         AttachInfo *ainfo;
1721         gchar *text[N_ATTACH_COLS];
1722         off_t size;
1723         gint row;
1724
1725         if (!is_file_exist(file)) {
1726                 g_warning(_("File %s doesn't exist\n"), file);
1727                 return;
1728         }
1729         if ((size = get_file_size(file)) < 0) {
1730                 g_warning(_("Can't get file size of %s\n"), file);
1731                 return;
1732         }
1733         if (size == 0) {
1734                 alertpanel_notice(_("File %s is empty\n"), file);
1735                 return;
1736         }
1737
1738         if (!compose->use_attach) {
1739                 GtkItemFactory *ifactory;
1740                 GtkWidget *menuitem;
1741
1742                 ifactory = gtk_item_factory_from_widget(compose->menubar);
1743                 menuitem = gtk_item_factory_get_item(ifactory,
1744                                                      "/Message/Attach");
1745                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1746                                                TRUE);
1747         }
1748
1749         ainfo = g_new0(AttachInfo, 1);
1750         ainfo->file = g_strdup(file);
1751
1752         if (cnttype == MIME_MESSAGE_RFC822) {
1753                 ainfo->content_type = g_strdup("message/rfc822");
1754                 ainfo->encoding = ENC_7BIT;
1755                 ainfo->name = g_strdup_printf(_("Message: %s"),
1756                                               g_basename(file));
1757         } else {
1758                 ainfo->content_type = procmime_get_mime_type(file);
1759                 if (!ainfo->content_type)
1760                         ainfo->content_type =
1761                                 g_strdup("application/octet-stream");
1762                 ainfo->encoding = ENC_BASE64;
1763                 ainfo->name = g_strdup(g_basename(file));
1764         }
1765         ainfo->size = size;
1766
1767         compose_attach_info(compose, ainfo, cnttype);
1768 }
1769
1770 static void compose_wrap_line(Compose *compose)
1771 {
1772         GtkSText *text = GTK_STEXT(compose->text);
1773         guint text_len;
1774         guint line_pos = 0, cur_pos = 0;
1775         gint line_len = 0, cur_len = 0;
1776         gint ch_len;
1777         gchar cbuf[MB_CUR_MAX];
1778
1779         gtk_stext_freeze(text);
1780
1781         text_len = gtk_stext_get_length(text);
1782
1783         for (; cur_pos < text_len; cur_pos++) {
1784                 if (text->use_wchar)
1785                         ch_len = wctomb
1786                                 (cbuf, (wchar_t)GTK_STEXT_INDEX(text, cur_pos));
1787                 else {
1788                         cbuf[0] = GTK_STEXT_INDEX(text, cur_pos);
1789                         ch_len = 1;
1790                 }
1791
1792                 if (ch_len == 1 && *cbuf == '\n') {
1793                         line_pos = cur_pos + 1;
1794                         line_len = cur_len = 0;
1795                         continue;
1796                 }
1797
1798                 if (ch_len < 0) {
1799                         cbuf[0] = '\0';
1800                         ch_len = 1;
1801                 }
1802
1803                 if (ch_len == 1 && isspace(*cbuf)) {
1804                         line_pos = cur_pos + 1;
1805                         line_len = cur_len + ch_len;
1806                 }
1807
1808                 if (cur_len + ch_len > prefs_common.linewrap_len &&
1809                     line_len > 0) {
1810                         gint tlen;
1811
1812                         if (text->use_wchar)
1813                                 tlen = wctomb(cbuf, (wchar_t)GTK_STEXT_INDEX(text, line_pos - 1));
1814                         else {
1815                                 cbuf[0] = GTK_STEXT_INDEX(text, line_pos - 1);
1816                                 tlen = 1;
1817                         }
1818                         if (tlen == 1 && isspace(*cbuf)) {
1819                                 gtk_stext_set_point(text, line_pos);
1820                                 gtk_stext_backward_delete(text, 1);
1821                                 text_len--;
1822                                 cur_pos--;
1823                                 line_pos--;
1824                                 cur_len--;
1825                                 line_len--;
1826                         }
1827
1828                         gtk_stext_set_point(text, line_pos);
1829                         gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
1830                         text_len++;
1831                         cur_pos++;
1832                         line_pos++;
1833                         cur_len = cur_len - line_len + ch_len;
1834                         line_len = 0;
1835                         continue;
1836                 }
1837
1838                 if (ch_len > 1) {
1839                         line_pos = cur_pos + 1;
1840                         line_len = cur_len + ch_len;
1841                 }
1842                 cur_len += ch_len;
1843         }
1844
1845         gtk_stext_thaw(text);
1846 }
1847
1848 static void compose_set_title(Compose *compose)
1849 {
1850         gchar *str;
1851         gchar *edited;
1852
1853         edited = compose->modified ? _(" [Edited]") : "";
1854         if (compose->account && compose->account->address)
1855                 str = g_strdup_printf(_("%s - Compose message%s"),
1856                                       compose->account->address, edited);
1857         else
1858                 str = g_strdup_printf(_("Compose message%s"), edited);
1859         gtk_window_set_title(GTK_WINDOW(compose->window), str);
1860         g_free(str);
1861 }
1862
1863 /**
1864  * compose_current_mail_account:
1865  * 
1866  * Find a current mail account (the currently selected account, or the
1867  * default account, if a news account is currently selected).  If a
1868  * mail account cannot be found, display an error message.
1869  * 
1870  * Return value: Mail account, or NULL if not found.
1871  **/
1872 static PrefsAccount *
1873 compose_current_mail_account(void)
1874 {
1875         PrefsAccount *ac;
1876
1877         if (cur_account && cur_account->protocol != A_NNTP)
1878                 ac = cur_account;
1879         else {
1880                 ac = account_get_default();
1881                 if (!ac || ac->protocol == A_NNTP) {
1882                         alertpanel_error(_("Account for sending mail is not specified.\n"
1883                                            "Please select a mail account before sending."));
1884                         return NULL;
1885                 }
1886         }
1887         return ac;
1888 }
1889
1890 gint compose_send(Compose *compose)
1891 {
1892         gchar tmp[MAXPATHLEN + 1];
1893         gchar *to, *newsgroups;
1894         gint ok = 0;
1895         static gboolean lock = FALSE;
1896
1897         if (lock) return 1;
1898
1899         g_return_val_if_fail(compose->account != NULL, -1);
1900         g_return_val_if_fail(compose->orig_account != NULL, -1);
1901
1902         lock = TRUE;
1903
1904         to = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
1905         newsgroups = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
1906         if (*to == '\0' && *newsgroups == '\0') {
1907                 alertpanel_error(_("Recipient is not specified."));
1908                 lock = FALSE;
1909                 return 1;
1910         }
1911
1912         /* write to temporary file */
1913         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
1914                    get_rc_dir(), G_DIR_SEPARATOR, (gint)compose);
1915
1916         if (prefs_common.linewrap_at_send)
1917                 compose_wrap_line(compose);
1918
1919         if (compose_write_to_file(compose, tmp, FALSE) < 0) {
1920                 lock = FALSE;
1921                 return -1;
1922         }
1923
1924         if (!compose->to_list && !compose->newsgroup_list) {
1925                 g_warning(_("can't get recipient list."));
1926                 unlink(tmp);
1927                 lock = FALSE;
1928                 return -1;
1929         }
1930
1931         if (compose->to_list) {
1932                 PrefsAccount *ac;
1933
1934                 /*
1935                 if (compose->account->protocol != A_NNTP)
1936                         ac = compose->account;
1937                 else if (compose->orig_account->protocol != A_NNTP)
1938                         ac = compose->orig_account;
1939                 else {
1940                         ac = compose_current_mail_account();
1941                         if (!ac) {
1942                                 unlink(tmp);
1943                                 lock = FALSE;
1944                                 return -1;
1945                         }
1946                 }
1947                 */
1948                 ac = compose->account;
1949
1950                 ok = send_message(tmp, ac, compose->to_list);
1951                 statusbar_pop_all();
1952         }
1953
1954         if (ok == 0 && compose->newsgroup_list) {
1955                 Folder *folder;
1956
1957                 if (compose->account->protocol == A_NNTP)
1958                         folder = FOLDER(compose->account->folder);
1959                 else
1960                         folder = FOLDER(compose->orig_account->folder);
1961
1962                 ok = news_post(folder, tmp);
1963                 if (ok < 0) {
1964                         alertpanel_error(_("Error occurred while posting the message to %s ."),
1965                                          compose->account->nntp_server);
1966                         unlink(tmp);
1967                         lock = FALSE;
1968                         return -1;
1969                 }
1970         }
1971
1972         /* queue message if failed to send */
1973         if (ok < 0) {
1974                 if (prefs_common.queue_msg) {
1975                         AlertValue val;
1976
1977                         val = alertpanel
1978                                 (_("Queueing"),
1979                                  _("Error occurred while sending the message.\n"
1980                                    "Put this message into queue folder?"),
1981                                  _("OK"), _("Cancel"), NULL);
1982                         if (G_ALERTDEFAULT == val) {
1983                                 ok = compose_queue(compose, tmp);
1984                                 if (ok < 0)
1985                                         alertpanel_error(_("Can't queue the message."));
1986                         }
1987                 } else
1988                         alertpanel_error(_("Error occurred while sending the message."));
1989         }
1990
1991         /* save message to outbox */
1992         if (ok == 0 && prefs_common.savemsg) {
1993                 if (compose_save_to_outbox(compose, tmp) < 0)
1994                         alertpanel_error
1995                                 (_("Can't save the message to outbox."));
1996         }
1997
1998         unlink(tmp);
1999         lock = FALSE;
2000         return ok;
2001 }
2002
2003 static gint compose_write_to_file(Compose *compose, const gchar *file,
2004                                   gboolean is_draft)
2005 {
2006         FILE *fp;
2007         size_t len;
2008         gchar *chars;
2009         gchar *buf;
2010         const gchar *out_codeset;
2011         EncodingType encoding;
2012
2013         if ((fp = fopen(file, "w")) == NULL) {
2014                 FILE_OP_ERROR(file, "fopen");
2015                 return -1;
2016         }
2017
2018         /* chmod for security */
2019         if (change_file_mode_rw(fp, file) < 0) {
2020                 FILE_OP_ERROR(file, "chmod");
2021                 g_warning(_("can't change file mode\n"));
2022         }
2023
2024         /* get all composed text */
2025         chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
2026         len = strlen(chars);
2027         if (is_ascii_str(chars)) {
2028                 buf = g_strdup(chars);
2029                 out_codeset = "US-ASCII";
2030                 encoding = ENC_7BIT;
2031         } else {
2032                 const gchar *src_codeset;
2033
2034                 out_codeset = conv_get_outgoing_charset_str();
2035                 if (!strcasecmp(out_codeset, "US-ASCII"))
2036                         out_codeset = "ISO-8859-1";
2037                 encoding = procmime_get_encoding_for_charset(out_codeset);
2038                 debug_print("charset = %s, encoding = %s\n",
2039                             out_codeset, procmime_get_encoding_str(encoding));
2040
2041                 src_codeset = conv_get_current_charset_str();
2042                 buf = conv_codeset_strdup(chars, src_codeset, out_codeset);
2043                 if (!buf) {
2044                         g_free(chars);
2045                         fclose(fp);
2046                         unlink(file);
2047                         alertpanel_error(_("Can't convert the codeset of the message."));
2048                         return -1;
2049                 }
2050         }
2051         g_free(chars);
2052
2053         /* write headers */
2054         if (compose_write_headers
2055                 (compose, fp, out_codeset, encoding, is_draft) < 0) {
2056                 g_warning(_("can't write headers\n"));
2057                 fclose(fp);
2058                 /* unlink(file); */
2059                 g_free(buf);
2060                 return -1;
2061         }
2062
2063         if (compose->use_attach) {
2064 #if USE_GPGME
2065             /* This prolog message is ignored by mime software and
2066              * because it would make our signing/encryption task
2067              * tougher, we don't emit it in that case */
2068             if (!compose->use_signing && !compose->use_encryption)
2069 #endif
2070                 fputs("This is a multi-part message in MIME format.\n", fp);
2071
2072                 fprintf(fp, "\n--%s\n", compose->boundary);
2073                 fprintf(fp, "Content-Type: text/plain; charset=%s\n",
2074                         out_codeset);
2075                 fprintf(fp, "Content-Transfer-Encoding: %s\n",
2076                         procmime_get_encoding_str(encoding));
2077                 fputc('\n', fp);
2078         }
2079
2080         /* write body */
2081         len = strlen(buf);
2082         if (encoding == ENC_BASE64) {
2083                 gchar outbuf[B64_BUFFSIZE];
2084                 gint i, l;
2085
2086                 for (i = 0; i < len; i += B64_LINE_SIZE) {
2087                         l = MIN(B64_LINE_SIZE, len - i);
2088                         to64frombits(outbuf, buf + i, l);
2089                         fputs(outbuf, fp);
2090                         fputc('\n', fp);
2091                 }
2092         } else if (fwrite(buf, sizeof(gchar), len, fp) != len) {
2093                 FILE_OP_ERROR(file, "fwrite");
2094                 fclose(fp);
2095                 unlink(file);
2096                 g_free(buf);
2097                 return -1;
2098         }
2099         g_free(buf);
2100
2101         if (compose->use_attach)
2102                 compose_write_attach(compose, fp);
2103
2104         if (fclose(fp) == EOF) {
2105                 FILE_OP_ERROR(file, "fclose");
2106                 unlink(file);
2107                 return -1;
2108         }
2109
2110 #if USE_GPGME
2111         if (compose->use_signing) {
2112                 if (rfc2015_sign(file, compose->account) < 0) {
2113                         unlink(file);
2114                         return -1;
2115                 }
2116         }
2117         if (compose->use_encryption) {
2118                 if (rfc2015_encrypt(file, compose->to_list) < 0) {
2119                         unlink(file);
2120                         return -1;
2121                 }
2122         }
2123 #endif /* USE_GPGME */
2124
2125         return 0;
2126 }
2127
2128 static gint compose_write_body_to_file(Compose *compose, const gchar *file)
2129 {
2130         FILE *fp;
2131         size_t len;
2132         gchar *chars;
2133
2134         if ((fp = fopen(file, "w")) == NULL) {
2135                 FILE_OP_ERROR(file, "fopen");
2136                 return -1;
2137         }
2138
2139         /* chmod for security */
2140         if (change_file_mode_rw(fp, file) < 0) {
2141                 FILE_OP_ERROR(file, "chmod");
2142                 g_warning(_("can't change file mode\n"));
2143         }
2144
2145         chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
2146
2147         /* write body */
2148         len = strlen(chars);
2149         if (fwrite(chars, sizeof(gchar), len, fp) != len) {
2150                 FILE_OP_ERROR(file, "fwrite");
2151                 g_free(chars);
2152                 fclose(fp);
2153                 unlink(file);
2154                 return -1;
2155         }
2156
2157         g_free(chars);
2158
2159         if (fclose(fp) == EOF) {
2160                 FILE_OP_ERROR(file, "fclose");
2161                 unlink(file);
2162                 return -1;
2163         }
2164         return 0;
2165 }
2166
2167 static gint compose_save_to_outbox(Compose *compose, const gchar *file)
2168 {
2169         FolderItem *outbox;
2170         gchar *path;
2171         gint num;
2172         FILE *fp;
2173
2174         debug_print(_("saving sent message...\n"));
2175
2176         outbox = folder_get_default_outbox();
2177         path = folder_item_get_path(outbox);
2178         if (!is_dir_exist(path))
2179                 make_dir_hier(path);
2180
2181         folder_item_scan(outbox);
2182         if ((num = folder_item_add_msg(outbox, file, FALSE)) < 0) {
2183                 g_free(path);
2184                 g_warning(_("can't save message\n"));
2185                 return -1;
2186         }
2187
2188         if ((fp = procmsg_open_mark_file(path, TRUE)) == NULL)
2189                 g_warning(_("can't open mark file\n"));
2190         else {
2191                 MsgInfo newmsginfo;
2192
2193                 newmsginfo.msgnum = num;
2194                 newmsginfo.flags = 0;
2195                 procmsg_write_flags(&newmsginfo, fp);
2196                 fclose(fp);
2197         }
2198         g_free(path);
2199
2200         return 0;
2201 }
2202
2203 static gint compose_queue(Compose *compose, const gchar *file)
2204 {
2205         FolderItem *queue;
2206         gchar *tmp, *queue_path;
2207         FILE *fp, *src_fp;
2208         GSList *cur;
2209         gchar buf[BUFFSIZE];
2210         gint num;
2211
2212         debug_print(_("queueing message...\n"));
2213         g_return_val_if_fail(compose->to_list != NULL, -1);
2214         g_return_val_if_fail(compose->account != NULL, -1);
2215
2216         tmp = g_strdup_printf("%s%cqueue.%d", g_get_tmp_dir(),
2217                               G_DIR_SEPARATOR, (gint)compose);
2218         if ((fp = fopen(tmp, "w")) == NULL) {
2219                 FILE_OP_ERROR(tmp, "fopen");
2220                 g_free(tmp);
2221                 return -1;
2222         }
2223         if ((src_fp = fopen(file, "r")) == NULL) {
2224                 FILE_OP_ERROR(file, "fopen");
2225                 fclose(fp);
2226                 unlink(tmp);
2227                 g_free(tmp);
2228                 return -1;
2229         }
2230         if (change_file_mode_rw(fp, tmp) < 0) {
2231                 FILE_OP_ERROR(tmp, "chmod");
2232                 g_warning(_("can't change file mode\n"));
2233         }
2234
2235         /* queueing variables */
2236         fprintf(fp, "AF:\n");
2237         fprintf(fp, "NF:0\n");
2238         fprintf(fp, "PS:10\n");
2239         fprintf(fp, "SRH:1\n");
2240         fprintf(fp, "SFN:\n");
2241         fprintf(fp, "DSR:\n");
2242         if (compose->msgid)
2243                 fprintf(fp, "MID:<%s>\n", compose->msgid);
2244         else
2245                 fprintf(fp, "MID:\n");
2246         fprintf(fp, "CFG:\n");
2247         fprintf(fp, "PT:0\n");
2248         fprintf(fp, "S:%s\n", compose->account->address);
2249         fprintf(fp, "RQ:\n");
2250         if (compose->account->smtp_server)
2251                 fprintf(fp, "SSV:%s\n", compose->account->smtp_server);
2252         else
2253                 fprintf(fp, "SSV:\n");
2254         if (compose->account->nntp_server)
2255                 fprintf(fp, "NSV:%s\n", compose->account->nntp_server);
2256         else
2257                 fprintf(fp, "NSV:\n");
2258         fprintf(fp, "SSH:\n");
2259         fprintf(fp, "R:<%s>", (gchar *)compose->to_list->data);
2260         for (cur = compose->to_list->next; cur != NULL; cur = cur->next)
2261                 fprintf(fp, ",<%s>", (gchar *)cur->data);
2262         fprintf(fp, "\n");
2263         fprintf(fp, "\n");
2264
2265         while (fgets(buf, sizeof(buf), src_fp) != NULL) {
2266                 if (fputs(buf, fp) == EOF) {
2267                         FILE_OP_ERROR(tmp, "fputs");
2268                         fclose(fp);
2269                         fclose(src_fp);
2270                         unlink(tmp);
2271                         g_free(tmp);
2272                         return -1;
2273                 }
2274         }
2275
2276         fclose(src_fp);
2277         if (fclose(fp) == EOF) {
2278                 FILE_OP_ERROR(tmp, "fclose");
2279                 unlink(tmp);
2280                 g_free(tmp);
2281                 return -1;
2282         }
2283
2284         queue = folder_get_default_queue();
2285         folder_item_scan(queue);
2286         queue_path = folder_item_get_path(queue);
2287         if (!is_dir_exist(queue_path))
2288                 make_dir_hier(queue_path);
2289         if ((num = folder_item_add_msg(queue, tmp, TRUE)) < 0) {
2290                 g_warning(_("can't queue the message\n"));
2291                 unlink(tmp);
2292                 g_free(tmp);
2293                 g_free(queue_path);
2294                 return -1;
2295         }
2296         g_free(tmp);
2297
2298         if ((fp = procmsg_open_mark_file(queue_path, TRUE)) == NULL)
2299                 g_warning(_("can't open mark file\n"));
2300         else {
2301                 MsgInfo newmsginfo;
2302
2303                 newmsginfo.msgnum = num;
2304                 newmsginfo.flags = 0;
2305                 procmsg_write_flags(&newmsginfo, fp);
2306                 fclose(fp);
2307         }
2308         g_free(queue_path);
2309
2310         folder_item_scan(queue);
2311         folderview_update_item(queue, TRUE);
2312
2313         return 0;
2314 }
2315
2316 static void compose_write_attach(Compose *compose, FILE *fp)
2317 {
2318         AttachInfo *ainfo;
2319         GtkCList *clist = GTK_CLIST(compose->attach_clist);
2320         gint row;
2321         FILE *attach_fp;
2322         gchar filename[BUFFSIZE];
2323         gint len;
2324
2325         for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
2326              row++) {
2327                 if ((attach_fp = fopen(ainfo->file, "r")) == NULL) {
2328                         g_warning(_("Can't open file %s\n"), ainfo->file);
2329                         continue;
2330                 }
2331
2332                 fprintf(fp, "\n--%s\n", compose->boundary);
2333
2334                 if (!strcmp2(ainfo->content_type, "message/rfc822")) {
2335                         fprintf(fp, "Content-Type: %s\n", ainfo->content_type);
2336                         fprintf(fp, "Content-Disposition: inline\n");
2337                 } else {
2338                         conv_encode_header(filename, sizeof(filename),
2339                                            ainfo->name, 12);
2340                         fprintf(fp, "Content-Type: %s;\n"
2341                                     " name=\"%s\"\n",
2342                                 ainfo->content_type, filename);
2343                         fprintf(fp, "Content-Disposition: attachment;\n"
2344                                     " filename=\"%s\"\n", filename);
2345                 }
2346
2347                 fprintf(fp, "Content-Transfer-Encoding: %s\n\n",
2348                         procmime_get_encoding_str(ainfo->encoding));
2349
2350                 if (ainfo->encoding == ENC_7BIT) {
2351                         gchar buf[BUFFSIZE];
2352
2353                         while (fgets(buf, sizeof(buf), attach_fp) != NULL) {
2354                                 len = strlen(buf);
2355                                 if (len > 1 && buf[len - 1] == '\n' &&
2356                                     buf[len - 2] == '\r') {
2357                                         buf[len - 2] = '\n';
2358                                         buf[len - 1] = '\0';
2359                                 }
2360                                 fputs(buf, fp);
2361                         }
2362                 } else {
2363                         gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
2364
2365                         while ((len = fread(inbuf, sizeof(gchar),
2366                                             B64_LINE_SIZE, attach_fp))
2367                                == B64_LINE_SIZE) {
2368                                 to64frombits(outbuf, inbuf, B64_LINE_SIZE);
2369                                 fputs(outbuf, fp);
2370                                 fputc('\n', fp);
2371                         }
2372                         if (len > 0 && feof(attach_fp)) {
2373                                 to64frombits(outbuf, inbuf, len);
2374                                 fputs(outbuf, fp);
2375                                 fputc('\n', fp);
2376                         }
2377                 }
2378
2379                 fclose(attach_fp);
2380         }
2381
2382         fprintf(fp, "\n--%s--\n", compose->boundary);
2383 }
2384
2385 #define IS_IN_CUSTOM_HEADER(header) \
2386         (compose->account->add_customhdr && \
2387          custom_header_find(compose->account->customhdr_list, header) != NULL)
2388
2389 static gint compose_write_headers(Compose *compose, FILE *fp,
2390                                   const gchar *charset, EncodingType encoding,
2391                                   gboolean is_draft)
2392 {
2393         gchar buf[BUFFSIZE];
2394         gchar *str;
2395         /* struct utsname utsbuf; */
2396
2397         g_return_val_if_fail(fp != NULL, -1);
2398         g_return_val_if_fail(charset != NULL, -1);
2399         g_return_val_if_fail(compose->account != NULL, -1);
2400         g_return_val_if_fail(compose->account->address != NULL, -1);
2401
2402         /* Date */
2403         if (compose->account->add_date) {
2404                 get_rfc822_date(buf, sizeof(buf));
2405                 fprintf(fp, "Date: %s\n", buf);
2406         }
2407         
2408         /* From */
2409         if (!IS_IN_CUSTOM_HEADER("From")) {
2410                 if (compose->account->name && *compose->account->name) {
2411                         compose_convert_header
2412                                 (buf, sizeof(buf), compose->account->name,
2413                                  strlen("From: "));
2414                         fprintf(fp, "From: %s <%s>\n",
2415                                 buf, compose->account->address);
2416                 } else
2417                         fprintf(fp, "From: %s\n", compose->account->address);
2418         }
2419         
2420         slist_free_strings(compose->to_list);
2421         g_slist_free(compose->to_list);
2422         compose->to_list = NULL;
2423
2424         /* To */
2425         if (compose->use_to) {
2426                 str = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
2427                 if (*str != '\0') {
2428                         Xstrdup_a(str, str, return -1);
2429                         g_strstrip(str);
2430                         if (*str != '\0') {
2431                                 compose->to_list = address_list_append
2432                                         (compose->to_list, str);
2433                                 if (!IS_IN_CUSTOM_HEADER("To")) {
2434                                         compose_convert_header
2435                                                 (buf, sizeof(buf), str,
2436                                                  strlen("To: "));
2437                                         fprintf(fp, "To: %s\n", buf);
2438                                 }
2439                         }
2440                 }
2441         }
2442         
2443         slist_free_strings(compose->newsgroup_list);
2444         g_slist_free(compose->newsgroup_list);
2445         compose->newsgroup_list = NULL;
2446
2447         /* Newsgroups */
2448         str = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
2449         if (*str != '\0') {
2450                 Xstrdup_a(str, str, return -1);
2451                 g_strstrip(str);
2452                 remove_space(str);
2453                 if (*str != '\0') {
2454                         compose->newsgroup_list =
2455                                 newsgroup_list_append(compose->newsgroup_list,
2456                                                       str);
2457                         if (!IS_IN_CUSTOM_HEADER("Newsgroups")) {
2458                                 compose_convert_header(buf, sizeof(buf), str,
2459                                                        strlen("Newsgroups: "));
2460                                 fprintf(fp, "Newsgroups: %s\n", buf);
2461                         }
2462                 }
2463         }
2464
2465         if (!is_draft && !compose->to_list && !compose->newsgroup_list)
2466                 return -1;
2467
2468         /* Cc */
2469         if (compose->use_cc) {
2470                 str = gtk_entry_get_text(GTK_ENTRY(compose->cc_entry));
2471                 if (*str != '\0') {
2472                         Xstrdup_a(str, str, return -1);
2473                         g_strstrip(str);
2474                         if (*str != '\0') {
2475                                 compose->to_list = address_list_append
2476                                         (compose->to_list, str);
2477                                 if (!IS_IN_CUSTOM_HEADER("Cc")) {
2478                                         compose_convert_header
2479                                                 (buf, sizeof(buf), str,
2480                                                  strlen("Cc: "));
2481                                         fprintf(fp, "Cc: %s\n", buf);
2482                                 }
2483                         }
2484                 }
2485         }
2486         
2487         /* Bcc */
2488         if (compose->use_bcc) {
2489                 str = gtk_entry_get_text(GTK_ENTRY(compose->bcc_entry));
2490                 if (*str != '\0') {
2491                         Xstrdup_a(str, str, return -1);
2492                         g_strstrip(str);
2493                         if (*str != '\0') {
2494                                 compose->to_list = address_list_append
2495                                         (compose->to_list, str);
2496                                 if (is_draft) {
2497                                         compose_convert_header
2498                                                 (buf, sizeof(buf), str,
2499                                                  strlen("Bcc: "));
2500                                         fprintf(fp, "Bcc: %s\n", buf);
2501                                 }
2502                         }
2503                 }
2504         }
2505
2506         /* Subject */
2507         str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
2508         if (*str != '\0' && !IS_IN_CUSTOM_HEADER("Subject")) {
2509                 Xstrdup_a(str, str, return -1);
2510                 g_strstrip(str);
2511                 if (*str != '\0') {
2512                         compose_convert_header(buf, sizeof(buf), str,
2513                                                strlen("Subject: "));
2514                         fprintf(fp, "Subject: %s\n", buf);
2515                 }
2516         }
2517
2518         /* Message-ID */
2519         if (compose->account->gen_msgid) {
2520                 compose_generate_msgid(compose, buf, sizeof(buf));
2521                 fprintf(fp, "Message-Id: <%s>\n", buf);
2522                 compose->msgid = g_strdup(buf);
2523         }
2524
2525         /* In-Reply-To */
2526         if (compose->inreplyto && compose->to_list)
2527                 fprintf(fp, "In-Reply-To: <%s>\n", compose->inreplyto);
2528
2529         /* References */
2530         if (compose->references)
2531                 fprintf(fp, "References: %s\n", compose->references);
2532
2533         /* Followup-To */
2534         if (compose->use_followupto && !IS_IN_CUSTOM_HEADER("Followup-To")) {
2535                 str = gtk_entry_get_text(GTK_ENTRY(compose->followup_entry));
2536                 if (*str != '\0') {
2537                         Xstrdup_a(str, str, return -1);
2538                         g_strstrip(str);
2539                         remove_space(str);
2540                         if (*str != '\0') {
2541                                 compose_convert_header(buf, sizeof(buf), str,
2542                                                        strlen("Followup-To: "));
2543                                 fprintf(fp, "Followup-To: %s\n", buf);
2544                         }
2545                 }
2546         }
2547
2548         /* Reply-To */
2549         if (compose->use_replyto && !IS_IN_CUSTOM_HEADER("Reply-To")) {
2550                 str = gtk_entry_get_text(GTK_ENTRY(compose->reply_entry));
2551                 if (*str != '\0') {
2552                         Xstrdup_a(str, str, return -1);
2553                         g_strstrip(str);
2554                         if (*str != '\0') {
2555                                 compose_convert_header(buf, sizeof(buf), str,
2556                                                        strlen("Reply-To: "));
2557                                 fprintf(fp, "Reply-To: %s\n", buf);
2558                         }
2559                 }
2560         }
2561
2562         /* Organization */
2563         if (compose->account->organization &&
2564             !IS_IN_CUSTOM_HEADER("Organization")) {
2565                 compose_convert_header(buf, sizeof(buf),
2566                                        compose->account->organization,
2567                                        strlen("Organization: "));
2568                 fprintf(fp, "Organization: %s\n", buf);
2569         }
2570
2571         /* Program version and system info */
2572         /* uname(&utsbuf); */
2573         str = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
2574         if (*str != '\0' && !IS_IN_CUSTOM_HEADER("X-Mailer")) {
2575                 fprintf(fp, "X-Mailer: %s (GTK+ %d.%d.%d; %s)\n",
2576                         prog_version,
2577                         gtk_major_version, gtk_minor_version, gtk_micro_version,
2578                         HOST_ALIAS);
2579                         /* utsbuf.sysname, utsbuf.release, utsbuf.machine); */
2580         }
2581         str = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
2582         if (*str != '\0' && !IS_IN_CUSTOM_HEADER("X-Newsreader")) {
2583                 fprintf(fp, "X-Newsreader: %s (GTK+ %d.%d.%d; %s)\n",
2584                         prog_version,
2585                         gtk_major_version, gtk_minor_version, gtk_micro_version,
2586                         HOST_ALIAS);
2587                         /* utsbuf.sysname, utsbuf.release, utsbuf.machine); */
2588         }
2589
2590         /* custom headers */
2591         if (compose->account->add_customhdr) {
2592                 GSList *cur;
2593
2594                 for (cur = compose->account->customhdr_list; cur != NULL;
2595                      cur = cur->next) {
2596                         CustomHeader *chdr = (CustomHeader *)cur->data;
2597
2598                         if (strcasecmp(chdr->name, "Date")         != 0 &&
2599                             strcasecmp(chdr->name, "From")         != 0 &&
2600                             strcasecmp(chdr->name, "To")           != 0 &&
2601                             strcasecmp(chdr->name, "Sender")       != 0 &&
2602                             strcasecmp(chdr->name, "Message-Id")   != 0 &&
2603                             strcasecmp(chdr->name, "In-Reply-To")  != 0 &&
2604                             strcasecmp(chdr->name, "References")   != 0 &&
2605                             strcasecmp(chdr->name, "Mime-Version") != 0 &&
2606                             strcasecmp(chdr->name, "Content-Type") != 0 &&
2607                             strcasecmp(chdr->name, "Content-Transfer-Encoding")
2608                             != 0)
2609                                 compose_convert_header
2610                                         (buf, sizeof(buf),
2611                                          chdr->value ? chdr->value : "",
2612                                          strlen(chdr->name) + 2);
2613                                 fprintf(fp, "%s: %s\n", chdr->name, buf);
2614                 }
2615         }
2616         
2617         /* MIME */
2618         fprintf(fp, "Mime-Version: 1.0\n");
2619         if (compose->use_attach) {
2620                 get_rfc822_date(buf, sizeof(buf));
2621                 subst_char(buf, ' ', '_');
2622                 subst_char(buf, ',', '_');
2623                 compose->boundary = g_strdup_printf("Multipart_%s_%08x",
2624                                                     buf, (guint)compose);
2625                 fprintf(fp,
2626                         "Content-Type: multipart/mixed;\n"
2627                         " boundary=\"%s\"\n", compose->boundary);
2628         } else {
2629                 fprintf(fp, "Content-Type: text/plain; charset=%s\n", charset);
2630                 fprintf(fp, "Content-Transfer-Encoding: %s\n",
2631                         procmime_get_encoding_str(encoding));
2632         }
2633
2634         /* Request Return Receipt */
2635         if (!IS_IN_CUSTOM_HEADER("Disposition-Notification-To")) {
2636                 if (compose->return_receipt) {
2637                         if (compose->account->name
2638                             && *compose->account->name) {
2639                                 compose_convert_header(buf, sizeof(buf), compose->account->name, strlen("Disposition-Notification-To: "));
2640                                 fprintf(fp, "Disposition-Notification-To: %s <%s>\n", buf, compose->account->address);
2641                         } else
2642                                 fprintf(fp, "Disposition-Notification-To: %s\n", compose->account->address);
2643                 }
2644         }
2645
2646         /* separator between header and body */
2647         fputs("\n", fp);
2648
2649         return 0;
2650 }
2651
2652 #undef IS_IN_CUSTOM_HEADER
2653
2654 static void compose_convert_header(gchar *dest, gint len, gchar *src,
2655                                    gint header_len)
2656 {
2657         g_return_if_fail(src != NULL);
2658         g_return_if_fail(dest != NULL);
2659
2660         if (len < 1) return;
2661
2662         remove_return(src);
2663
2664         if (is_ascii_str(src)) {
2665                 strncpy2(dest, src, len);
2666                 dest[len - 1] = '\0';
2667                 return;
2668         } else
2669                 conv_encode_header(dest, len, src, header_len);
2670 }
2671
2672 static void compose_generate_msgid(Compose *compose, gchar *buf, gint len)
2673 {
2674         struct tm *lt;
2675         time_t t;
2676         gchar *addr;
2677
2678         t = time(NULL);
2679         lt = localtime(&t);
2680
2681         if (compose->account && compose->account->address &&
2682             *compose->account->address) {
2683                 if (strchr(compose->account->address, '@'))
2684                         addr = g_strdup(compose->account->address);
2685                 else
2686                         addr = g_strconcat(compose->account->address, "@",
2687                                            get_domain_name(), NULL);
2688         } else
2689                 addr = g_strconcat(g_get_user_name(), "@", get_domain_name(),
2690                                    NULL);
2691
2692         g_snprintf(buf, len, "%04d%02d%02d%02d%02d%02d.%08x.%s",
2693                    lt->tm_year + 1900, lt->tm_mon + 1,
2694                    lt->tm_mday, lt->tm_hour,
2695                    lt->tm_min, lt->tm_sec,
2696                    (guint)random(), addr);
2697
2698         debug_print(_("generated Message-ID: %s\n"), buf);
2699
2700         g_free(addr);
2701 }
2702
2703 static void compose_add_entry_field(GtkWidget *table, GtkWidget **hbox,
2704                                     GtkWidget **entry, gint *count,
2705                                     const gchar *label_str,
2706                                     gboolean is_addr_entry)
2707 {
2708         GtkWidget *label;
2709
2710         if (GTK_TABLE(table)->nrows < (*count) + 1)
2711                 gtk_table_resize(GTK_TABLE(table), (*count) + 1, 2);
2712
2713         *hbox = gtk_hbox_new(FALSE, 0);
2714         label = gtk_label_new
2715                 (prefs_common.trans_hdr ? gettext(label_str) : label_str);
2716         gtk_box_pack_end(GTK_BOX(*hbox), label, FALSE, FALSE, 0);
2717         gtk_table_attach(GTK_TABLE(table), *hbox, 0, 1, *count, (*count) + 1,
2718                          GTK_FILL, 0, 2, 0);
2719         *entry = gtk_entry_new();
2720         gtk_table_attach_defaults
2721                 (GTK_TABLE(table), *entry, 1, 2, *count, (*count) + 1);
2722         if (GTK_TABLE(table)->nrows > (*count) + 1)
2723                 gtk_table_set_row_spacing(GTK_TABLE(table), *count, 4);
2724
2725         if (is_addr_entry)
2726                 address_completion_register_entry(GTK_ENTRY(*entry));
2727
2728         (*count)++;
2729 }
2730
2731 static Compose *compose_create(PrefsAccount *account)
2732 {
2733         Compose   *compose;
2734         GtkWidget *window;
2735         GtkWidget *vbox;
2736         GtkWidget *menubar;
2737         GtkWidget *handlebox;
2738
2739         GtkWidget *vbox2;
2740
2741         GtkWidget *table_vbox;
2742         GtkWidget *label;
2743         GtkWidget *from_optmenu_hbox;
2744         GtkWidget *to_entry;
2745         GtkWidget *to_hbox;
2746         GtkWidget *newsgroups_entry;
2747         GtkWidget *newsgroups_hbox;
2748         GtkWidget *subject_entry;
2749         GtkWidget *cc_entry;
2750         GtkWidget *cc_hbox;
2751         GtkWidget *bcc_entry;
2752         GtkWidget *bcc_hbox;
2753         GtkWidget *reply_entry;
2754         GtkWidget *reply_hbox;
2755         GtkWidget *followup_entry;
2756         GtkWidget *followup_hbox;
2757
2758         GtkWidget *paned;
2759
2760         GtkWidget *attach_scrwin;
2761         GtkWidget *attach_clist;
2762
2763         GtkWidget *edit_vbox;
2764         GtkWidget *ruler_hbox;
2765         GtkWidget *ruler;
2766         GtkWidget *scrolledwin;
2767         GtkWidget *text;
2768
2769         GtkWidget *table;
2770         GtkWidget *hbox;
2771
2772         gchar *titles[] = {_("MIME type"), _("Size"), _("Name")};
2773         guint n_menu_entries;
2774         GtkStyle  *style, *new_style;
2775         GdkColormap *cmap;
2776         GdkColor color[1];
2777         gboolean success[1];
2778         GdkFont   *font;
2779         GtkWidget *popupmenu;
2780         GtkWidget *menuitem;
2781         GtkItemFactory *popupfactory;
2782         GtkItemFactory *ifactory;
2783         gint n_entries;
2784         gint count = 0;
2785         gint i;
2786
2787         g_return_val_if_fail(account != NULL, NULL);
2788
2789         debug_print(_("Creating compose window...\n"));
2790         compose = g_new0(Compose, 1);
2791
2792         compose->account = account;
2793         compose->orig_account = account;
2794
2795         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2796         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
2797         gtk_widget_set_usize(window, -1, prefs_common.compose_height);
2798         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
2799                            GTK_SIGNAL_FUNC(compose_delete_cb), compose);
2800         gtk_signal_connect(GTK_OBJECT(window), "destroy",
2801                            GTK_SIGNAL_FUNC(compose_destroy_cb), compose);
2802         gtk_signal_connect(GTK_OBJECT(window), "focus_in_event",
2803                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
2804         gtk_signal_connect(GTK_OBJECT(window), "focus_out_event",
2805                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
2806         gtk_widget_realize(window);
2807
2808         vbox = gtk_vbox_new(FALSE, 0);
2809         gtk_container_add(GTK_CONTAINER(window), vbox);
2810
2811         n_menu_entries = sizeof(compose_entries) / sizeof(compose_entries[0]);
2812         menubar = menubar_create(window, compose_entries,
2813                                  n_menu_entries, "<Compose>", compose);
2814         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
2815
2816         handlebox = gtk_handle_box_new();
2817         gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
2818
2819         compose_toolbar_create(compose, handlebox);
2820
2821         vbox2 = gtk_vbox_new(FALSE, 2);
2822         gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
2823         gtk_container_set_border_width(GTK_CONTAINER(vbox2), BORDER_WIDTH);
2824
2825         table_vbox = gtk_vbox_new(FALSE, 0);
2826         gtk_box_pack_start(GTK_BOX(vbox2), table_vbox, FALSE, TRUE, 0);
2827         gtk_container_set_border_width(GTK_CONTAINER(table_vbox),
2828                                        BORDER_WIDTH * 2);
2829
2830         table = gtk_table_new(8, 2, FALSE);
2831         gtk_box_pack_start(GTK_BOX(table_vbox), table, FALSE, TRUE, 0);
2832
2833         /* option menu for selecting accounts */
2834         hbox = gtk_hbox_new(FALSE, 0);
2835         label = gtk_label_new(prefs_common.trans_hdr ? _("From:") : "From:");
2836         gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
2837         gtk_table_attach(GTK_TABLE(table), hbox, 0, 1, count, count + 1,
2838                          GTK_FILL, 0, 2, 0);
2839         from_optmenu_hbox = compose_account_option_menu_create(compose);
2840         gtk_table_attach_defaults(GTK_TABLE(table), from_optmenu_hbox,
2841                                   1, 2, count, count + 1);
2842         gtk_table_set_row_spacing(GTK_TABLE(table), 0, 4);
2843         count++;
2844
2845         /* header labels and entries */
2846         compose_add_entry_field(table, &to_hbox, &to_entry, &count,
2847                                 "To:", TRUE); 
2848         gtk_table_set_row_spacing(GTK_TABLE(table), 0, 4);
2849         compose_add_entry_field(table, &newsgroups_hbox, &newsgroups_entry,
2850                                 &count, "Newsgroups:", FALSE);
2851         gtk_table_set_row_spacing(GTK_TABLE(table), 1, 4);
2852         compose_add_entry_field(table, &hbox, &subject_entry, &count,
2853                                 "Subject:", FALSE);
2854         gtk_table_set_row_spacing(GTK_TABLE(table), 2, 4);
2855         compose_add_entry_field(table, &cc_hbox, &cc_entry, &count,
2856                                 "Cc:", TRUE);
2857         gtk_table_set_row_spacing(GTK_TABLE(table), 3, 4);
2858         compose_add_entry_field(table, &bcc_hbox, &bcc_entry, &count,
2859                                 "Bcc:", TRUE);
2860         gtk_table_set_row_spacing(GTK_TABLE(table), 4, 4);
2861         compose_add_entry_field(table, &reply_hbox, &reply_entry, &count,
2862                                 "Reply-To:", TRUE);
2863         gtk_table_set_row_spacing(GTK_TABLE(table), 5, 4);
2864         compose_add_entry_field(table, &followup_hbox, &followup_entry, &count,
2865                                 "Followup-To:", FALSE);
2866         gtk_table_set_row_spacing(GTK_TABLE(table), 6, 4);
2867
2868         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
2869
2870         gtk_signal_connect(GTK_OBJECT(to_entry), "activate",
2871                            GTK_SIGNAL_FUNC(to_activated), compose);
2872         gtk_signal_connect(GTK_OBJECT(newsgroups_entry), "activate",
2873                            GTK_SIGNAL_FUNC(newsgroups_activated), compose);
2874         gtk_signal_connect(GTK_OBJECT(subject_entry), "activate",
2875                            GTK_SIGNAL_FUNC(subject_activated), compose);
2876         gtk_signal_connect(GTK_OBJECT(cc_entry), "activate",
2877                            GTK_SIGNAL_FUNC(cc_activated), compose);
2878         gtk_signal_connect(GTK_OBJECT(bcc_entry), "activate",
2879                            GTK_SIGNAL_FUNC(bcc_activated), compose);
2880         gtk_signal_connect(GTK_OBJECT(reply_entry), "activate",
2881                            GTK_SIGNAL_FUNC(replyto_activated), compose);
2882         gtk_signal_connect(GTK_OBJECT(followup_entry), "activate",
2883                            GTK_SIGNAL_FUNC(followupto_activated), compose);
2884
2885         gtk_signal_connect(GTK_OBJECT(subject_entry), "grab_focus",
2886                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2887         gtk_signal_connect(GTK_OBJECT(to_entry), "grab_focus",
2888                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2889         gtk_signal_connect(GTK_OBJECT(newsgroups_entry), "grab_focus",
2890                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2891         gtk_signal_connect(GTK_OBJECT(cc_entry), "grab_focus",
2892                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2893         gtk_signal_connect(GTK_OBJECT(bcc_entry), "grab_focus",
2894                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2895         gtk_signal_connect(GTK_OBJECT(reply_entry), "grab_focus",
2896                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2897         gtk_signal_connect(GTK_OBJECT(followup_entry), "grab_focus",
2898                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2899
2900         /* attachment list */
2901         attach_scrwin = gtk_scrolled_window_new(NULL, NULL);
2902         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(attach_scrwin),
2903                                        GTK_POLICY_AUTOMATIC,
2904                                        GTK_POLICY_ALWAYS);
2905         gtk_widget_set_usize(attach_scrwin, -1, 80);
2906
2907         attach_clist = gtk_clist_new_with_titles(N_ATTACH_COLS, titles);
2908         gtk_clist_set_column_justification(GTK_CLIST(attach_clist), COL_SIZE,
2909                                            GTK_JUSTIFY_RIGHT);
2910         gtk_clist_set_column_width(GTK_CLIST(attach_clist), COL_MIMETYPE, 240);
2911         gtk_clist_set_column_width(GTK_CLIST(attach_clist), COL_SIZE, 64);
2912         gtk_clist_set_selection_mode(GTK_CLIST(attach_clist),
2913                                      GTK_SELECTION_EXTENDED);
2914         for (i = 0; i < N_ATTACH_COLS; i++)
2915                 GTK_WIDGET_UNSET_FLAGS
2916                         (GTK_CLIST(attach_clist)->column[i].button,
2917                          GTK_CAN_FOCUS);
2918         gtk_container_add(GTK_CONTAINER(attach_scrwin), attach_clist);
2919
2920         gtk_signal_connect(GTK_OBJECT(attach_clist), "select_row",
2921                            GTK_SIGNAL_FUNC(attach_selected), compose);
2922         gtk_signal_connect(GTK_OBJECT(attach_clist), "button_press_event",
2923                            GTK_SIGNAL_FUNC(attach_button_pressed), compose);
2924         gtk_signal_connect(GTK_OBJECT(attach_clist), "key_press_event",
2925                            GTK_SIGNAL_FUNC(attach_key_pressed), compose);
2926
2927         /* drag and drop */
2928         gtk_drag_dest_set(attach_clist,
2929                           GTK_DEST_DEFAULT_ALL, compose_mime_types, 1,
2930                           GDK_ACTION_COPY);
2931         gtk_signal_connect(GTK_OBJECT(attach_clist), "drag_data_received",
2932                            GTK_SIGNAL_FUNC(compose_attach_drag_received_cb),
2933                            compose);
2934
2935         /* pane between attach clist and text */
2936         paned = gtk_vpaned_new();
2937         gtk_paned_add1(GTK_PANED(paned), attach_scrwin);
2938         gtk_widget_ref(paned);
2939         gtk_widget_show_all(paned);
2940
2941         edit_vbox = gtk_vbox_new(FALSE, 0);
2942         gtk_box_pack_start(GTK_BOX(vbox2), edit_vbox, TRUE, TRUE, 0);
2943
2944         /* ruler */
2945         ruler_hbox = gtk_hbox_new(FALSE, 0);
2946         gtk_box_pack_start(GTK_BOX(edit_vbox), ruler_hbox, FALSE, FALSE, 0);
2947
2948         ruler = gtk_shruler_new();
2949         gtk_ruler_set_range(GTK_RULER(ruler), 0.0, 100.0, 1.0, 100.0);
2950         gtk_box_pack_start(GTK_BOX(ruler_hbox), ruler, TRUE, TRUE,
2951                            BORDER_WIDTH + 1);
2952         gtk_widget_set_usize(ruler_hbox, 1, -1);
2953
2954         /* text widget */
2955         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
2956         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
2957                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
2958         gtk_box_pack_start(GTK_BOX(edit_vbox), scrolledwin, TRUE, TRUE, 0);
2959         gtk_widget_set_usize(scrolledwin, prefs_common.compose_width, -1);
2960
2961         text = gtk_stext_new(gtk_scrolled_window_get_hadjustment
2962                             (GTK_SCROLLED_WINDOW(scrolledwin)),
2963                             gtk_scrolled_window_get_vadjustment
2964                             (GTK_SCROLLED_WINDOW(scrolledwin)));
2965         gtk_stext_set_editable(GTK_STEXT(text), TRUE);
2966         if (prefs_common.smart_wrapping) {      
2967                 gtk_stext_set_word_wrap(GTK_STEXT(text), TRUE);
2968                 gtk_stext_set_wrap_rmargin(GTK_STEXT(text), prefs_common.linewrap_len);
2969         }               
2970
2971         gtk_container_add(GTK_CONTAINER(scrolledwin), text);
2972
2973         gtk_signal_connect(GTK_OBJECT(text), "changed",
2974                            GTK_SIGNAL_FUNC(compose_changed_cb), compose);
2975         gtk_signal_connect(GTK_OBJECT(text), "grab_focus",
2976                            GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2977         gtk_signal_connect_after(GTK_OBJECT(text), "button_press_event",
2978                                  GTK_SIGNAL_FUNC(compose_button_press_cb),
2979                                  compose);
2980 #if 0
2981         gtk_signal_connect_after(GTK_OBJECT(text), "key_press_event",
2982                                  GTK_SIGNAL_FUNC(compose_key_press_cb),
2983                                  compose);
2984 #endif
2985         gtk_signal_connect_after(GTK_OBJECT(text), "size_allocate",
2986                                  GTK_SIGNAL_FUNC(compose_edit_size_alloc),
2987                                  ruler);
2988
2989         /* drag and drop */
2990         gtk_drag_dest_set(text, GTK_DEST_DEFAULT_ALL, compose_mime_types, 1,
2991                           GDK_ACTION_COPY);
2992         gtk_signal_connect(GTK_OBJECT(text), "drag_data_received",
2993                            GTK_SIGNAL_FUNC(compose_insert_drag_received_cb),
2994                            compose);
2995
2996         gtk_widget_show_all(vbox);
2997
2998         style = gtk_widget_get_style(text);
2999
3000         /* workaround for the slow down of GtkSText when using Pixmap theme */
3001         if (style->engine) {
3002                 GtkThemeEngine *engine;
3003
3004                 engine = style->engine;
3005                 style->engine = NULL;
3006                 new_style = gtk_style_copy(style);
3007                 style->engine = engine;
3008         } else
3009                 new_style = gtk_style_copy(style);
3010
3011         if (prefs_common.textfont) {
3012                 CharSet charset;
3013
3014                 charset = conv_get_current_charset();
3015                 if (MB_CUR_MAX == 1) {
3016                         gchar *fontstr, *p;
3017
3018                         Xstrdup_a(fontstr, prefs_common.textfont, );
3019                         if (fontstr && (p = strchr(fontstr, ',')) != NULL)
3020                                 *p = '\0';
3021                         font = gdk_font_load(fontstr);
3022                 } else
3023                         font = gdk_fontset_load(prefs_common.textfont);
3024                 if (font) {
3025                         gdk_font_unref(new_style->font);
3026                         new_style->font = font;
3027                 }
3028         }
3029
3030         gtk_widget_set_style(text, new_style);
3031
3032         color[0] = quote_color;
3033         cmap = gdk_window_get_colormap(window->window);
3034         gdk_colormap_alloc_colors(cmap, color, 1, FALSE, TRUE, success);
3035         if (success[0] == FALSE) {
3036                 g_warning("Compose: color allocation failed.\n");
3037                 style = gtk_widget_get_style(text);
3038                 quote_color = style->black;
3039         }
3040
3041         n_entries = sizeof(compose_popup_entries) /
3042                 sizeof(compose_popup_entries[0]);
3043         popupmenu = menu_create_items(compose_popup_entries, n_entries,
3044                                       "<Compose>", &popupfactory,
3045                                       compose);
3046
3047         ifactory = gtk_item_factory_from_widget(menubar);
3048         menu_set_sensitive(ifactory, "/Edit/Undo", FALSE);
3049         menu_set_sensitive(ifactory, "/Edit/Redo", FALSE);
3050
3051         /*
3052         if (account->protocol == A_NNTP) {
3053                 gtk_widget_hide(to_hbox);
3054                 gtk_widget_hide(to_entry);
3055                 gtk_widget_hide(cc_hbox);
3056                 gtk_widget_hide(cc_entry);
3057                 gtk_table_set_row_spacing(GTK_TABLE(table), 1, 0);
3058                 gtk_table_set_row_spacing(GTK_TABLE(table), 3, 0);
3059         } else {
3060                 gtk_widget_hide(newsgroups_hbox);
3061                 gtk_widget_hide(newsgroups_entry);
3062                 gtk_table_set_row_spacing(GTK_TABLE(table), 2, 0);
3063
3064                 menu_set_sensitive(ifactory, "/Message/Followup to", FALSE);
3065         }
3066         */
3067
3068         switch (prefs_common.toolbar_style) {
3069         case TOOLBAR_NONE:
3070                 gtk_widget_hide(handlebox);
3071                 break;
3072         case TOOLBAR_ICON:
3073                 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
3074                                       GTK_TOOLBAR_ICONS);
3075                 break;
3076         case TOOLBAR_TEXT:
3077                 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
3078                                       GTK_TOOLBAR_TEXT);
3079                 break;
3080         case TOOLBAR_BOTH:
3081                 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
3082                                       GTK_TOOLBAR_BOTH);
3083                 break;
3084         }
3085
3086         gtk_widget_show(window);
3087
3088         address_completion_start(window);
3089
3090         compose->window        = window;
3091         compose->vbox          = vbox;
3092         compose->menubar       = menubar;
3093         compose->handlebox     = handlebox;
3094
3095         compose->vbox2         = vbox2;
3096
3097         compose->table_vbox       = table_vbox;
3098         compose->table            = table;
3099         compose->to_hbox          = to_hbox;
3100         compose->to_entry         = to_entry;
3101         compose->newsgroups_hbox  = newsgroups_hbox;
3102         compose->newsgroups_entry = newsgroups_entry;
3103         compose->subject_entry    = subject_entry;
3104         compose->cc_hbox          = cc_hbox;
3105         compose->cc_entry         = cc_entry;
3106         compose->bcc_hbox         = bcc_hbox;
3107         compose->bcc_entry        = bcc_entry;
3108         compose->reply_hbox       = reply_hbox;
3109         compose->reply_entry      = reply_entry;
3110         compose->followup_hbox    = followup_hbox;
3111         compose->followup_entry   = followup_entry;
3112
3113         compose->paned = paned;
3114
3115         compose->attach_scrwin = attach_scrwin;
3116         compose->attach_clist  = attach_clist;
3117
3118         compose->edit_vbox     = edit_vbox;
3119         compose->ruler_hbox    = ruler_hbox;
3120         compose->ruler         = ruler;
3121         compose->scrolledwin   = scrolledwin;
3122         compose->text          = text;
3123
3124         compose->focused_editable = NULL;
3125
3126         compose->popupmenu    = popupmenu;
3127         compose->popupfactory = popupfactory;
3128
3129         compose->mode = COMPOSE_NEW;
3130
3131         compose->replyto     = NULL;
3132         compose->cc          = NULL;
3133         compose->bcc         = NULL;
3134         compose->followup_to = NULL;
3135         compose->inreplyto   = NULL;
3136         compose->references  = NULL;
3137         compose->msgid       = NULL;
3138         compose->boundary    = NULL;
3139
3140 #if USE_GPGME
3141         compose->use_signing    = FALSE;
3142         compose->use_encryption = FALSE;
3143 #endif /* USE_GPGME */
3144
3145         compose->modified = FALSE;
3146
3147         compose->return_receipt = FALSE;
3148
3149         compose->to_list        = NULL;
3150         compose->newsgroup_list = NULL;
3151
3152         compose->exteditor_file    = NULL;
3153         compose->exteditor_pid     = -1;
3154         compose->exteditor_readdes = -1;
3155         compose->exteditor_tag     = -1;
3156
3157         compose_set_title(compose);
3158
3159         compose->use_bcc        = FALSE;
3160         compose->use_replyto    = FALSE;
3161         compose->use_followupto = FALSE;
3162
3163         /*
3164         if (account->protocol != A_NNTP) {
3165                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/To");
3166                 gtk_check_menu_item_set_active
3167                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
3168                 gtk_widget_set_sensitive(menuitem, FALSE);
3169                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Cc");
3170                 gtk_check_menu_item_set_active
3171                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
3172                 gtk_widget_set_sensitive(menuitem, FALSE);
3173         }
3174         */
3175         if (account->set_autocc && account->auto_cc) {
3176                 compose->use_cc = TRUE;
3177                 gtk_entry_set_text(GTK_ENTRY(cc_entry), account->auto_cc);
3178                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Cc");
3179                 gtk_check_menu_item_set_active
3180                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
3181         }
3182
3183         if (account->set_autobcc) {
3184                 compose->use_bcc = TRUE;
3185                 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Bcc");
3186                 gtk_check_menu_item_set_active
3187                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
3188                 if (account->auto_bcc)
3189                         gtk_entry_set_text(GTK_ENTRY(bcc_entry),
3190                                            account->auto_bcc);
3191         }
3192         if (account->set_autoreplyto) {
3193                 compose->use_replyto = TRUE;
3194                 menuitem = gtk_item_factory_get_item(ifactory,
3195                                                      "/Message/Reply to");
3196                 gtk_check_menu_item_set_active
3197                         (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
3198                 if (account->auto_replyto)
3199                         gtk_entry_set_text(GTK_ENTRY(reply_entry),
3200                                            account->auto_replyto);
3201         }
3202
3203         menuitem = gtk_item_factory_get_item(ifactory, "/Tool/Show ruler");
3204         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
3205                                        prefs_common.show_ruler);
3206
3207 #if USE_GPGME
3208         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Sign");
3209         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
3210                                        prefs_common.default_sign);
3211         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Encrypt");
3212         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
3213                                        prefs_common.default_encrypt);
3214 #endif /* USE_GPGME */
3215
3216         addressbook_set_target_compose(compose);
3217
3218         compose_list = g_list_append(compose_list, compose);
3219
3220         /*
3221         compose->use_to         = FALSE;
3222         compose->use_cc         = FALSE;
3223         */
3224         compose->use_attach     = FALSE;
3225
3226         if (!compose->use_bcc) {
3227                 gtk_widget_hide(bcc_hbox);
3228                 gtk_widget_hide(bcc_entry);
3229                 gtk_table_set_row_spacing(GTK_TABLE(table), 4, 0);
3230         }
3231         if (!compose->use_replyto) {
3232                 gtk_widget_hide(reply_hbox);
3233                 gtk_widget_hide(reply_entry);
3234                 gtk_table_set_row_spacing(GTK_TABLE(table), 5, 0);
3235         }
3236         if (!compose->use_followupto) {
3237                 gtk_widget_hide(followup_hbox);
3238                 gtk_widget_hide(followup_entry);
3239                 gtk_table_set_row_spacing(GTK_TABLE(table), 6, 0);
3240         }
3241
3242         if (!prefs_common.show_ruler)
3243                 gtk_widget_hide(ruler_hbox);
3244
3245         select_account(compose, account);
3246
3247         return compose;
3248 }
3249
3250 #include "pixmaps/stock_mail_send.xpm"
3251 #include "pixmaps/stock_mail.xpm"
3252 #include "pixmaps/stock_paste.xpm"
3253 #include "pixmaps/stock_mail_attach.xpm"
3254 #include "pixmaps/stock_mail_compose.xpm"
3255 #include "pixmaps/linewrap.xpm"
3256 /*#include "pixmaps/tb_mail_queue_send.xpm"*/
3257 #include "pixmaps/tb_address_book.xpm"
3258
3259 #define CREATE_TOOLBAR_ICON(xpm_d) \
3260 { \
3261         icon = gdk_pixmap_create_from_xpm_d(container->window, &mask, \
3262                                             &container->style->white, \
3263                                             xpm_d); \
3264         icon_wid = gtk_pixmap_new(icon, mask); \
3265 }
3266
3267 static void compose_toolbar_create(Compose *compose, GtkWidget *container)
3268 {
3269         GtkWidget *toolbar;
3270         GdkPixmap *icon;
3271         GdkBitmap *mask;
3272         GtkWidget *icon_wid;
3273         GtkWidget *send_btn;
3274         GtkWidget *sendl_btn;
3275         GtkWidget *draft_btn;
3276         GtkWidget *insert_btn;
3277         GtkWidget *attach_btn;
3278         GtkWidget *sig_btn;
3279         GtkWidget *exteditor_btn;
3280         GtkWidget *linewrap_btn;
3281         GtkWidget *addrbook_btn;
3282
3283         toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
3284                                   GTK_TOOLBAR_BOTH);
3285         gtk_container_add(GTK_CONTAINER(container), toolbar);
3286         gtk_container_set_border_width(GTK_CONTAINER(container), 2);
3287         gtk_toolbar_set_button_relief(GTK_TOOLBAR(toolbar), GTK_RELIEF_NONE);
3288         gtk_toolbar_set_space_style(GTK_TOOLBAR(toolbar),
3289                                     GTK_TOOLBAR_SPACE_LINE);
3290
3291         CREATE_TOOLBAR_ICON(stock_mail_send_xpm);
3292         send_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3293                                            _("Send"),
3294                                            _("Send message"),
3295                                            "Send",
3296                                            icon_wid, toolbar_send_cb, compose);
3297
3298         CREATE_TOOLBAR_ICON(stock_mail_send_xpm);
3299         /* CREATE_TOOLBAR_ICON(tb_mail_queue_send_xpm); */
3300         sendl_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3301                                            _("Send later"),
3302                                            _("Put into queue folder and send later"),
3303                                            "Send later",
3304                                            icon_wid, toolbar_send_later_cb,
3305                                            compose);
3306
3307         CREATE_TOOLBAR_ICON(stock_mail_xpm);
3308         draft_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3309                                             _("Draft"),
3310                                             _("Save to draft folder"),
3311                                             "Draft",
3312                                             icon_wid, toolbar_draft_cb,
3313                                             compose);
3314
3315         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
3316
3317         CREATE_TOOLBAR_ICON(stock_paste_xpm);
3318         insert_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3319                                              _("Insert"),
3320                                              _("Insert file"),
3321                                              "Insert",
3322                                              icon_wid, toolbar_insert_cb,
3323                                              compose);
3324
3325         CREATE_TOOLBAR_ICON(stock_mail_attach_xpm);
3326         attach_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3327                                              _("Attach"),
3328                                              _("Attach file"),
3329                                              "Attach",
3330                                              icon_wid, toolbar_attach_cb,
3331                                              compose);
3332
3333         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
3334
3335         CREATE_TOOLBAR_ICON(stock_mail_xpm);
3336         sig_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3337                                           _("Signature"),
3338                                           _("Insert signature"),
3339                                           "Signature",
3340                                           icon_wid, toolbar_sig_cb, compose);
3341
3342         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
3343
3344         CREATE_TOOLBAR_ICON(stock_mail_compose_xpm);
3345         exteditor_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3346                                                 _("Editor"),
3347                                                 _("Edit with external editor"),
3348                                                 "Editor",
3349                                                 icon_wid,
3350                                                 toolbar_ext_editor_cb,
3351                                                 compose);
3352
3353         CREATE_TOOLBAR_ICON(linewrap_xpm);
3354         linewrap_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3355                                                _("Linewrap"),
3356                                                _("Wrap long lines"),
3357                                                "Linewrap",
3358                                                icon_wid,
3359                                                toolbar_linewrap_cb,
3360                                                compose);
3361
3362         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
3363
3364         CREATE_TOOLBAR_ICON(tb_address_book_xpm);
3365         addrbook_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
3366                                                _("Address"),
3367                                                _("Address book"),
3368                                                "Address",
3369                                                icon_wid, toolbar_address_cb,
3370                                                compose);
3371
3372         compose->toolbar       = toolbar;
3373         compose->send_btn      = send_btn;
3374         compose->sendl_btn     = sendl_btn;
3375         compose->draft_btn     = draft_btn;
3376         compose->insert_btn    = insert_btn;
3377         compose->attach_btn    = attach_btn;
3378         compose->sig_btn       = sig_btn;
3379         compose->exteditor_btn = exteditor_btn;
3380         compose->linewrap_btn  = linewrap_btn;
3381         compose->addrbook_btn  = addrbook_btn;
3382
3383         gtk_widget_show_all(toolbar);
3384 }
3385
3386 #undef CREATE_TOOLBAR_ICON
3387
3388 static GtkWidget *compose_account_option_menu_create(Compose *compose)
3389 {
3390         GList *accounts;
3391         GtkWidget *hbox;
3392         GtkWidget *optmenu;
3393         GtkWidget *menu;
3394         gint num = 0, def_menu = 0;
3395
3396         accounts = account_get_list();
3397         g_return_val_if_fail(accounts != NULL, NULL);
3398
3399         hbox = gtk_hbox_new(FALSE, 0);
3400         optmenu = gtk_option_menu_new();
3401         gtk_box_pack_start(GTK_BOX(hbox), optmenu, FALSE, FALSE, 0);
3402         menu = gtk_menu_new();
3403
3404         for (; accounts != NULL; accounts = accounts->next, num++) {
3405                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
3406                 GtkWidget *menuitem;
3407                 gchar *name;
3408
3409                 if (ac == compose->account) def_menu = num;
3410
3411                 name = g_strdup_printf("%s <%s> (%s)",
3412                                        ac->name, ac->address, ac->account_name);
3413                 MENUITEM_ADD(menu, menuitem, name, ac);
3414                 g_free(name);
3415                 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3416                                    GTK_SIGNAL_FUNC(account_activated),
3417                                    compose);
3418         }
3419
3420         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), menu);
3421         gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), def_menu);
3422
3423         return hbox;
3424 }
3425
3426 static void compose_destroy(Compose *compose)
3427 {
3428         gint row;
3429         GtkCList *clist = GTK_CLIST(compose->attach_clist);
3430         AttachInfo *ainfo;
3431
3432         /* NOTE: address_completion_end() does nothing with the window
3433          * however this may change. */
3434         address_completion_end(compose->window);
3435
3436         slist_free_strings(compose->to_list);
3437         g_slist_free(compose->to_list);
3438         slist_free_strings(compose->newsgroup_list);
3439         g_slist_free(compose->newsgroup_list);
3440
3441         procmsg_msginfo_free(compose->targetinfo);
3442
3443         g_free(compose->replyto);
3444         g_free(compose->cc);
3445         g_free(compose->bcc);
3446         g_free(compose->newsgroups);
3447         g_free(compose->followup_to);
3448
3449         g_free(compose->inreplyto);
3450         g_free(compose->references);
3451         g_free(compose->msgid);
3452         g_free(compose->boundary);
3453
3454         g_free(compose->exteditor_file);
3455
3456         for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
3457              row++)
3458                 compose_attach_info_free(ainfo);
3459
3460         if (addressbook_get_target_compose() == compose)
3461                 addressbook_set_target_compose(NULL);
3462
3463         prefs_common.compose_width = compose->scrolledwin->allocation.width;
3464         prefs_common.compose_height = compose->window->allocation.height;
3465
3466         gtk_widget_destroy(compose->paned);
3467
3468         g_free(compose);
3469
3470         compose_list = g_list_remove(compose_list, compose);
3471 }
3472
3473 static void compose_attach_info_free(AttachInfo *ainfo)
3474 {
3475         g_free(ainfo->file);
3476         g_free(ainfo->content_type);
3477         g_free(ainfo->name);
3478         g_free(ainfo);
3479 }
3480
3481 static void compose_attach_remove_selected(Compose *compose)
3482 {
3483         GtkCList *clist = GTK_CLIST(compose->attach_clist);
3484         AttachInfo *ainfo;
3485         gint row;
3486
3487         while (clist->selection != NULL) {
3488                 row = GPOINTER_TO_INT(clist->selection->data);
3489                 ainfo = gtk_clist_get_row_data(clist, row);
3490                 compose_attach_info_free(ainfo);
3491                 gtk_clist_remove(clist, row);
3492         }
3493 }
3494
3495 static struct _AttachProperty
3496 {
3497         GtkWidget *window;
3498         GtkWidget *mimetype_entry;
3499         GtkWidget *encoding_optmenu;
3500         GtkWidget *path_entry;
3501         GtkWidget *filename_entry;
3502         GtkWidget *ok_btn;
3503         GtkWidget *cancel_btn;
3504 } attach_prop;
3505
3506 static void compose_attach_property(Compose *compose)
3507 {
3508         GtkCList *clist = GTK_CLIST(compose->attach_clist);
3509         AttachInfo *ainfo;
3510         gint row;
3511         gboolean cancelled;
3512         GtkOptionMenu *optmenu;
3513
3514         if (!clist->selection) return;
3515         row = GPOINTER_TO_INT(clist->selection->data);
3516
3517         ainfo = gtk_clist_get_row_data(clist, row);
3518         if (!ainfo) return;
3519
3520         if (!attach_prop.window)
3521                 compose_attach_property_create(&cancelled);
3522         gtk_widget_grab_focus(attach_prop.ok_btn);
3523         gtk_widget_show(attach_prop.window);
3524         manage_window_set_transient(GTK_WINDOW(attach_prop.window));
3525
3526         optmenu = GTK_OPTION_MENU(attach_prop.encoding_optmenu);
3527         if (ainfo->encoding == ENC_UNKNOWN)
3528                 gtk_option_menu_set_history(optmenu, ENC_BASE64);
3529         else
3530                 gtk_option_menu_set_history(optmenu, ainfo->encoding);
3531
3532         gtk_entry_set_text(GTK_ENTRY(attach_prop.mimetype_entry),
3533                            ainfo->content_type ? ainfo->content_type : "");
3534         gtk_entry_set_text(GTK_ENTRY(attach_prop.path_entry),
3535                            ainfo->file ? ainfo->file : "");
3536         gtk_entry_set_text(GTK_ENTRY(attach_prop.filename_entry),
3537                            ainfo->name ? ainfo->name : "");
3538
3539         for (;;) {
3540                 gchar *text;
3541                 gchar *cnttype = NULL;
3542                 gchar *file = NULL;
3543                 off_t size = 0;
3544                 GtkWidget *menu;
3545                 GtkWidget *menuitem;
3546
3547                 gtk_main();
3548
3549                 if (cancelled == TRUE) {
3550                         gtk_widget_hide(attach_prop.window);
3551                         break;
3552                 }
3553
3554                 text = gtk_entry_get_text(GTK_ENTRY(attach_prop.mimetype_entry));
3555                 if (*text != '\0') {
3556                         gchar *p;
3557
3558                         text = g_strstrip(g_strdup(text));
3559                         if ((p = strchr(text, '/')) && !strchr(p + 1, '/')) {
3560                                 cnttype = g_strdup(text);
3561                                 g_free(text);
3562                         } else {
3563                                 alertpanel_error(_("Invalid MIME type."));
3564                                 g_free(text);
3565                                 continue;
3566                         }
3567                 }
3568
3569                 menu = gtk_option_menu_get_menu(optmenu);
3570                 menuitem = gtk_menu_get_active(GTK_MENU(menu));
3571                 ainfo->encoding = GPOINTER_TO_INT
3572                         (gtk_object_get_user_data(GTK_OBJECT(menuitem)));
3573
3574                 text = gtk_entry_get_text(GTK_ENTRY(attach_prop.path_entry));
3575                 if (*text != '\0') {
3576                         if (is_file_exist(text) &&
3577                             (size = get_file_size(text)) > 0)
3578                                 file = g_strdup(text);
3579                         else {
3580                                 alertpanel_error
3581                                         (_("File doesn't exist or is empty."));
3582                                 g_free(cnttype);
3583                                 continue;
3584                         }
3585                 }
3586
3587                 text = gtk_entry_get_text(GTK_ENTRY(attach_prop.filename_entry));
3588                 if (*text != '\0') {
3589                         g_free(ainfo->name);
3590                         ainfo->name = g_strdup(text);
3591                 }
3592
3593                 if (cnttype) {
3594                         g_free(ainfo->content_type);
3595                         ainfo->content_type = cnttype;
3596                 }
3597                 if (file) {
3598                         g_free(ainfo->file);
3599                         ainfo->file = file;
3600                 }
3601                 if (size)
3602                         ainfo->size = size;
3603
3604                 gtk_clist_set_text(clist, row, COL_MIMETYPE,
3605                                    ainfo->content_type);
3606                 gtk_clist_set_text(clist, row, COL_SIZE,
3607                                    to_human_readable(ainfo->size));
3608                 gtk_clist_set_text(clist, row, COL_NAME, ainfo->name);
3609
3610                 gtk_widget_hide(attach_prop.window);
3611                 break;
3612         }
3613 }
3614
3615 #define SET_LABEL_AND_ENTRY(str, entry, top) \
3616 { \
3617         label = gtk_label_new(str); \
3618         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), \
3619                          GTK_FILL, 0, 0, 0); \
3620         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); \
3621  \
3622         entry = gtk_entry_new(); \
3623         gtk_table_attach(GTK_TABLE(table), entry, 1, 2, top, (top + 1), \
3624                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); \
3625 }
3626
3627 static void compose_attach_property_create(gboolean *cancelled)
3628 {
3629         GtkWidget *window;
3630         GtkWidget *vbox;
3631         GtkWidget *table;
3632         GtkWidget *label;
3633         GtkWidget *mimetype_entry;
3634         GtkWidget *hbox;
3635         GtkWidget *optmenu;
3636         GtkWidget *optmenu_menu;
3637         GtkWidget *menuitem;
3638         GtkWidget *path_entry;
3639         GtkWidget *filename_entry;
3640         GtkWidget *hbbox;
3641         GtkWidget *ok_btn;
3642         GtkWidget *cancel_btn;
3643         GList     *mime_type_list, *strlist;
3644
3645         debug_print("Creating attach_property window...\n");
3646
3647         window = gtk_window_new(GTK_WINDOW_DIALOG);
3648         gtk_widget_set_usize(window, 480, -1);
3649         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
3650         gtk_window_set_title(GTK_WINDOW(window), _("Property"));
3651         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3652         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
3653         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
3654                            GTK_SIGNAL_FUNC(attach_property_delete_event),
3655                            cancelled);
3656         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
3657                            GTK_SIGNAL_FUNC(attach_property_key_pressed),
3658                            cancelled);
3659
3660         vbox = gtk_vbox_new(FALSE, 8);
3661         gtk_container_add(GTK_CONTAINER(window), vbox);
3662
3663         table = gtk_table_new(4, 2, FALSE);
3664         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
3665         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
3666         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
3667
3668         label = gtk_label_new(_("MIME type")); 
3669         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, (0 + 1), 
3670                          GTK_FILL, 0, 0, 0); 
3671         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); 
3672         mimetype_entry = gtk_combo_new(); 
3673         gtk_table_attach(GTK_TABLE(table), mimetype_entry, 1, 2, 0, (0 + 1), 
3674                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
3675                          
3676         /* stuff with list */
3677         mime_type_list = procmime_get_mime_type_list();
3678         strlist = NULL;
3679         for (; mime_type_list != NULL; mime_type_list = mime_type_list->next) {
3680                 MimeType *type = (MimeType *) mime_type_list->data;
3681                 strlist = g_list_append(strlist, 
3682                                 g_strdup_printf("%s/%s",
3683                                         type->type, type->sub_type));
3684         }
3685         
3686         gtk_combo_set_popdown_strings(GTK_COMBO(mimetype_entry), strlist);
3687
3688         for (mime_type_list = strlist; mime_type_list != NULL; 
3689                 mime_type_list = mime_type_list->next)
3690                 g_free(mime_type_list->data);
3691         g_list_free(strlist);
3692                          
3693         mimetype_entry = GTK_COMBO(mimetype_entry)->entry;                       
3694
3695         label = gtk_label_new(_("Encoding"));
3696         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
3697                          GTK_FILL, 0, 0, 0);
3698         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
3699
3700         hbox = gtk_hbox_new(FALSE, 0);
3701         gtk_table_attach(GTK_TABLE(table), hbox, 1, 2, 1, 2,
3702                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
3703
3704         optmenu = gtk_option_menu_new();
3705         gtk_box_pack_start(GTK_BOX(hbox), optmenu, FALSE, FALSE, 0);
3706
3707         optmenu_menu = gtk_menu_new();
3708         MENUITEM_ADD(optmenu_menu, menuitem, "7bit", ENC_7BIT);
3709         gtk_widget_set_sensitive(menuitem, FALSE);
3710         MENUITEM_ADD(optmenu_menu, menuitem, "8bit", ENC_8BIT);
3711         gtk_widget_set_sensitive(menuitem, FALSE);
3712         MENUITEM_ADD(optmenu_menu, menuitem, "quoted-printable", ENC_QUOTED_PRINTABLE);
3713         gtk_widget_set_sensitive(menuitem, FALSE);
3714         MENUITEM_ADD(optmenu_menu, menuitem, "base64", ENC_BASE64);
3715
3716         gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), optmenu_menu);
3717
3718         SET_LABEL_AND_ENTRY(_("Path"),      path_entry,     2);
3719         SET_LABEL_AND_ENTRY(_("File name"), filename_entry, 3);
3720
3721         gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
3722                                 &cancel_btn, _("Cancel"), NULL, NULL);
3723         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
3724         gtk_widget_grab_default(ok_btn);
3725
3726         gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
3727                            GTK_SIGNAL_FUNC(attach_property_ok),
3728                            cancelled);
3729         gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
3730                            GTK_SIGNAL_FUNC(attach_property_cancel),
3731                            cancelled);
3732
3733         gtk_widget_show_all(vbox);
3734
3735         attach_prop.window           = window;
3736         attach_prop.mimetype_entry   = mimetype_entry;
3737         attach_prop.encoding_optmenu = optmenu;
3738         attach_prop.path_entry       = path_entry;
3739         attach_prop.filename_entry   = filename_entry;
3740         attach_prop.ok_btn           = ok_btn;
3741         attach_prop.cancel_btn       = cancel_btn;
3742 }
3743
3744 #undef SET_LABEL_AND_ENTRY
3745
3746 static void attach_property_ok(GtkWidget *widget, gboolean *cancelled)
3747 {
3748         *cancelled = FALSE;
3749         gtk_main_quit();
3750 }
3751
3752 static void attach_property_cancel(GtkWidget *widget, gboolean *cancelled)
3753 {
3754         *cancelled = TRUE;
3755         gtk_main_quit();
3756 }
3757
3758 static gint attach_property_delete_event(GtkWidget *widget, GdkEventAny *event,
3759                                          gboolean *cancelled)
3760 {
3761         *cancelled = TRUE;
3762         gtk_main_quit();
3763
3764         return TRUE;
3765 }
3766
3767 static void attach_property_key_pressed(GtkWidget *widget, GdkEventKey *event,
3768                                         gboolean *cancelled)
3769 {
3770         if (event && event->keyval == GDK_Escape) {
3771                 *cancelled = TRUE;
3772                 gtk_main_quit();
3773         }
3774 }
3775
3776 static void compose_exec_ext_editor(Compose *compose)
3777 {
3778         gchar tmp[64];
3779         pid_t pid;
3780         gint pipe_fds[2];
3781
3782         g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.%08x",
3783                    g_get_tmp_dir(), G_DIR_SEPARATOR, (gint)compose);
3784
3785         if (pipe(pipe_fds) < 0) {
3786                 perror("pipe");
3787                 return;
3788         }
3789
3790         if ((pid = fork()) < 0) {
3791                 perror("fork");
3792                 return;
3793         }
3794
3795         if (pid != 0) {
3796                 /* close the write side of the pipe */
3797                 close(pipe_fds[1]);
3798
3799                 compose->exteditor_file    = g_strdup(tmp);
3800                 compose->exteditor_pid     = pid;
3801                 compose->exteditor_readdes = pipe_fds[0];
3802
3803                 compose_set_ext_editor_sensitive(compose, FALSE);
3804
3805                 compose->exteditor_tag =
3806                         gdk_input_add(pipe_fds[0], GDK_INPUT_READ,
3807                                       compose_input_cb, compose);
3808         } else {        /* process-monitoring process */
3809                 pid_t pid_ed;
3810
3811                 if (setpgid(0, 0))
3812                         perror("setpgid");
3813
3814                 /* close the read side of the pipe */
3815                 close(pipe_fds[0]);
3816
3817                 if (compose_write_body_to_file(compose, tmp) < 0) {
3818                         fd_write(pipe_fds[1], "2\n", 2);
3819                         _exit(1);
3820                 }
3821
3822                 pid_ed = compose_exec_ext_editor_real(tmp);
3823                 if (pid_ed < 0) {
3824                         fd_write(pipe_fds[1], "1\n", 2);
3825                         _exit(1);
3826                 }
3827
3828                 /* wait until editor is terminated */
3829                 waitpid(pid_ed, NULL, 0);
3830
3831                 fd_write(pipe_fds[1], "0\n", 2);
3832
3833                 close(pipe_fds[1]);
3834                 _exit(0);
3835         }
3836 }
3837
3838 static gint compose_exec_ext_editor_real(const gchar *file)
3839 {
3840         static gchar *def_cmd = "emacs %s";
3841         gchar buf[1024];
3842         gchar *p;
3843         gchar **cmdline;
3844         pid_t pid;
3845
3846         g_return_val_if_fail(file != NULL, -1);
3847
3848         if ((pid = fork()) < 0) {
3849                 perror("fork");
3850                 return -1;
3851         }
3852
3853         if (pid != 0) return pid;
3854
3855         /* grandchild process */
3856
3857         if (setpgid(0, getppid()))
3858                 perror("setpgid");
3859
3860         if (prefs_common.ext_editor_cmd &&
3861             (p = strchr(prefs_common.ext_editor_cmd, '%')) &&
3862             *(p + 1) == 's' && !strchr(p + 2, '%')) {
3863                 g_snprintf(buf, sizeof(buf), prefs_common.ext_editor_cmd, file);
3864         } else {
3865                 if (prefs_common.ext_editor_cmd)
3866                         g_warning(_("External editor command line is invalid: `%s'\n"),
3867                                   prefs_common.ext_editor_cmd);
3868                 g_snprintf(buf, sizeof(buf), def_cmd, file);
3869         }
3870
3871         cmdline = g_strsplit(buf, " ", 1024);
3872         execvp(cmdline[0], cmdline);
3873
3874         perror("execvp");
3875         g_strfreev(cmdline);
3876
3877         _exit(1);
3878 }
3879
3880 static gboolean compose_ext_editor_kill(Compose *compose)
3881 {
3882         pid_t pgid = compose->exteditor_pid * -1;
3883         gint ret;
3884
3885         ret = kill(pgid, 0);
3886
3887         if (ret == 0 || (ret == -1 && EPERM == errno)) {
3888                 AlertValue val;
3889                 gchar *msg;
3890
3891                 msg = g_strdup_printf
3892                         (_("The external editor is still working.\n"
3893                            "Force terminating the process?\n"
3894                            "process group id: %d"), -pgid);
3895                 val = alertpanel(_("Notice"), msg, _("Yes"), _("+No"), NULL);
3896                 g_free(msg);
3897
3898                 if (val == G_ALERTDEFAULT) {
3899                         gdk_input_remove(compose->exteditor_tag);
3900                         close(compose->exteditor_readdes);
3901
3902                         if (kill(pgid, SIGTERM) < 0) perror("kill");
3903                         waitpid(compose->exteditor_pid, NULL, 0);
3904
3905                         g_warning(_("Terminated process group id: %d"), -pgid);
3906                         g_warning(_("Temporary file: %s"),
3907                                   compose->exteditor_file);
3908
3909                         compose_set_ext_editor_sensitive(compose, TRUE);
3910
3911                         g_free(compose->exteditor_file);
3912                         compose->exteditor_file    = NULL;
3913                         compose->exteditor_pid     = -1;
3914                         compose->exteditor_readdes = -1;
3915                         compose->exteditor_tag     = -1;
3916                 } else
3917                         return FALSE;
3918         }
3919
3920         return TRUE;
3921 }
3922
3923 static void compose_input_cb(gpointer data, gint source,
3924                              GdkInputCondition condition)
3925 {
3926         gchar buf[3];
3927         Compose *compose = (Compose *)data;
3928         gint i = 0;
3929
3930         debug_print(_("Compose: input from monitoring process\n"));
3931
3932         gdk_input_remove(compose->exteditor_tag);
3933
3934         for (;;) {
3935                 if (read(source, &buf[i], 1) < 1) {
3936                         buf[0] = '3';
3937                         break;
3938                 }
3939                 if (buf[i] == '\n') {
3940                         buf[i] = '\0';
3941                         break;
3942                 }
3943                 i++;
3944                 if (i == sizeof(buf) - 1)
3945                         break;
3946         }
3947
3948         waitpid(compose->exteditor_pid, NULL, 0);
3949
3950         if (buf[0] == '0') {            /* success */
3951                 GtkSText *text = GTK_STEXT(compose->text);
3952
3953                 gtk_stext_freeze(text);
3954                 gtk_stext_set_point(text, 0);
3955                 gtk_stext_forward_delete(text, gtk_stext_get_length(text));
3956                 compose_insert_file(compose, compose->exteditor_file);
3957                 compose_changed_cb(NULL, compose);
3958                 gtk_stext_thaw(text);
3959
3960                 if (unlink(compose->exteditor_file) < 0)
3961                         FILE_OP_ERROR(compose->exteditor_file, "unlink");
3962         } else if (buf[0] == '1') {     /* failed */
3963                 g_warning(_("Couldn't exec external editor\n"));
3964                 if (unlink(compose->exteditor_file) < 0)
3965                         FILE_OP_ERROR(compose->exteditor_file, "unlink");
3966         } else if (buf[0] == '2') {
3967                 g_warning(_("Couldn't write to file\n"));
3968         } else if (buf[0] == '3') {
3969                 g_warning(_("Pipe read failed\n"));
3970         }
3971
3972         close(source);
3973
3974         compose_set_ext_editor_sensitive(compose, TRUE);
3975
3976         g_free(compose->exteditor_file);
3977         compose->exteditor_file    = NULL;
3978         compose->exteditor_pid     = -1;
3979         compose->exteditor_readdes = -1;
3980         compose->exteditor_tag     = -1;
3981 }
3982
3983 static void compose_set_ext_editor_sensitive(Compose *compose,
3984                                              gboolean sensitive)
3985 {
3986         GtkItemFactory *ifactory;
3987
3988         ifactory = gtk_item_factory_from_widget(compose->menubar);
3989
3990         menu_set_sensitive(ifactory, "/Message/Send", sensitive);
3991         menu_set_sensitive(ifactory, "/Message/Send later", sensitive);
3992         menu_set_sensitive(ifactory, "/Message/Save to draft folder",
3993                            sensitive);
3994         menu_set_sensitive(ifactory, "/File/Insert file", sensitive);
3995         menu_set_sensitive(ifactory, "/File/Insert signature", sensitive);
3996         menu_set_sensitive(ifactory, "/Edit/Wrap long lines", sensitive);
3997         menu_set_sensitive(ifactory, "/Edit/Edit with external editor",
3998                            sensitive);
3999
4000         gtk_widget_set_sensitive(compose->text,          sensitive);
4001         gtk_widget_set_sensitive(compose->send_btn,      sensitive);
4002         gtk_widget_set_sensitive(compose->sendl_btn,     sensitive);
4003         gtk_widget_set_sensitive(compose->draft_btn,     sensitive);
4004         gtk_widget_set_sensitive(compose->insert_btn,    sensitive);
4005         gtk_widget_set_sensitive(compose->sig_btn,       sensitive);
4006         gtk_widget_set_sensitive(compose->exteditor_btn, sensitive);
4007         gtk_widget_set_sensitive(compose->linewrap_btn,  sensitive);
4008 }
4009
4010 static gint calc_cursor_xpos(GtkSText *text, gint extra, gint char_width)
4011 {
4012         gint cursor_pos;
4013
4014         cursor_pos = (text->cursor_pos_x - extra) / char_width;
4015         cursor_pos = MAX(cursor_pos, 0);
4016
4017         return cursor_pos;
4018 }
4019
4020 /* callback functions */
4021
4022 /* compose_edit_size_alloc() - called when resized. don't know whether Gtk
4023  * includes "non-client" (windows-izm) in calculation, so this calculation
4024  * may not be accurate.
4025  */
4026 static gboolean compose_edit_size_alloc(GtkEditable *widget,
4027                                         GtkAllocation *allocation,
4028                                         GtkSHRuler *shruler)
4029 {
4030         if (prefs_common.show_ruler) {
4031                 gint char_width;
4032                 gint line_width_in_chars;
4033
4034                 char_width = gtkut_get_font_width
4035                         (GTK_WIDGET(widget)->style->font);
4036                 line_width_in_chars =
4037                         (allocation->width - allocation->x) / char_width;
4038
4039                 /* got the maximum */
4040                 gtk_ruler_set_range(GTK_RULER(shruler),
4041                                     0.0, line_width_in_chars,
4042                                     calc_cursor_xpos(GTK_STEXT(widget),
4043                                                      allocation->x,
4044                                                      char_width),
4045                                     /*line_width_in_chars*/ char_width);
4046         }
4047
4048         return TRUE;
4049 }
4050
4051 static void toolbar_send_cb(GtkWidget *widget, gpointer data)
4052 {
4053         compose_send_cb(data, 0, NULL);
4054 }
4055
4056 static void toolbar_send_later_cb(GtkWidget *widget, gpointer data)
4057 {
4058         compose_send_later_cb(data, 0, NULL);
4059 }
4060
4061 static void toolbar_draft_cb(GtkWidget *widget, gpointer data)
4062 {
4063         compose_draft_cb(data, 0, NULL);
4064 }
4065
4066 static void toolbar_insert_cb(GtkWidget *widget, gpointer data)
4067 {
4068         compose_insert_file_cb(data, 0, NULL);
4069 }
4070
4071 static void toolbar_attach_cb(GtkWidget *widget, gpointer data)
4072 {
4073         compose_attach_cb(data, 0, NULL);
4074 }
4075
4076 static void toolbar_sig_cb(GtkWidget *widget, gpointer data)
4077 {
4078         Compose *compose = (Compose *)data;
4079
4080         compose_insert_sig(compose);
4081 }
4082
4083 static void toolbar_ext_editor_cb(GtkWidget *widget, gpointer data)
4084 {
4085         Compose *compose = (Compose *)data;
4086
4087         compose_exec_ext_editor(compose);
4088 }
4089
4090 static void toolbar_linewrap_cb(GtkWidget *widget, gpointer data)
4091 {
4092         Compose *compose = (Compose *)data;
4093
4094         compose_wrap_line(compose);
4095 }
4096
4097 static void toolbar_address_cb(GtkWidget *widget, gpointer data)
4098 {
4099         compose_address_cb(data, 0, NULL);
4100 }
4101
4102 static void select_account(Compose * compose, PrefsAccount * ac)
4103 {
4104                 compose->account = ac;
4105                 compose_set_title(compose);
4106
4107                 if (ac->protocol == A_NNTP) {
4108                         GtkItemFactory *ifactory;
4109                         GtkWidget *menuitem;
4110
4111                         ifactory = gtk_item_factory_from_widget(compose->menubar);
4112                         menu_set_sensitive(ifactory,
4113                                            "/Message/Followup to", TRUE);
4114
4115                         gtk_widget_show(compose->newsgroups_hbox);
4116                         gtk_widget_show(compose->newsgroups_entry);
4117                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
4118                                                   1, 4);
4119
4120                         compose->use_to = FALSE;
4121                         compose->use_cc = FALSE;
4122
4123                         menuitem = gtk_item_factory_get_item(ifactory, "/Message/To");
4124                         gtk_check_menu_item_set_active
4125                                 (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
4126                         menu_set_sensitive(ifactory,
4127                                            "/Message/To", TRUE);
4128                         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Cc");
4129                         gtk_check_menu_item_set_active
4130                                 (GTK_CHECK_MENU_ITEM(menuitem), FALSE);
4131
4132                         gtk_widget_hide(compose->to_hbox);
4133                         gtk_widget_hide(compose->to_entry);
4134                         gtk_widget_hide(compose->cc_hbox);
4135                         gtk_widget_hide(compose->cc_entry);
4136
4137                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
4138                                                   0, 0);
4139                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
4140                                                   3, 0);
4141                 }
4142                 else {
4143                         GtkItemFactory *ifactory;
4144                         GtkWidget *menuitem;
4145
4146                         ifactory = gtk_item_factory_from_widget(compose->menubar);
4147                         menu_set_sensitive(ifactory,
4148                                            "/Message/Followup to", FALSE);
4149
4150                         gtk_entry_set_text(GTK_ENTRY(compose->newsgroups_entry), "");
4151                         gtk_widget_hide(compose->newsgroups_hbox);
4152                         gtk_widget_hide(compose->newsgroups_entry);
4153                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
4154                                                   1, 0);
4155
4156                         compose->use_to = TRUE;
4157                         compose->use_cc = TRUE;
4158
4159                         menuitem = gtk_item_factory_get_item(ifactory, "/Message/To");
4160                         gtk_check_menu_item_set_active
4161                                 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
4162                         menu_set_sensitive(ifactory,
4163                                            "/Message/To", FALSE);
4164                         menuitem = gtk_item_factory_get_item(ifactory, "/Message/Cc");
4165                         gtk_check_menu_item_set_active
4166                                 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
4167
4168                         gtk_widget_show(compose->to_hbox);
4169                         gtk_widget_show(compose->to_entry);
4170                         gtk_widget_show(compose->cc_hbox);
4171                         gtk_widget_show(compose->cc_entry);
4172
4173                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
4174                                                   0, 4);
4175                         gtk_table_set_row_spacing(GTK_TABLE(compose->table),
4176                                                   3, 4);
4177                 }
4178                 gtk_widget_queue_resize(compose->table_vbox);
4179 }
4180
4181 static void account_activated(GtkMenuItem *menuitem, gpointer data)
4182 {
4183         Compose *compose = (Compose *)data;
4184
4185         PrefsAccount *ac;
4186
4187         ac = (PrefsAccount *)gtk_object_get_user_data(GTK_OBJECT(menuitem));
4188         g_return_if_fail(ac != NULL);
4189
4190         if (ac != compose->account)
4191                 select_account(compose, ac);
4192 }
4193
4194 static void attach_selected(GtkCList *clist, gint row, gint column,
4195                             GdkEvent *event, gpointer data)
4196 {
4197         Compose *compose = (Compose *)data;
4198
4199         if (event && event->type == GDK_2BUTTON_PRESS)
4200                 compose_attach_property(compose);
4201 }
4202
4203 static void attach_button_pressed(GtkWidget *widget, GdkEventButton *event,
4204                                   gpointer data)
4205 {
4206         Compose *compose = (Compose *)data;
4207         GtkCList *clist = GTK_CLIST(compose->attach_clist);
4208         gint row, column;
4209
4210         if (!event) return;
4211
4212         if (event->button == 3) {
4213                 if ((clist->selection && !clist->selection->next) ||
4214                     !clist->selection) {
4215                         gtk_clist_unselect_all(clist);
4216                         if (gtk_clist_get_selection_info(clist,
4217                                                          event->x, event->y,
4218                                                          &row, &column)) {
4219                                 gtk_clist_select_row(clist, row, column);
4220                                 gtkut_clist_set_focus_row(clist, row);
4221                         }
4222                 }
4223                 gtk_menu_popup(GTK_MENU(compose->popupmenu), NULL, NULL,
4224                                NULL, NULL, event->button, event->time);
4225         }
4226 }
4227
4228 static void attach_key_pressed(GtkWidget *widget, GdkEventKey *event,
4229                                gpointer data)
4230 {
4231         Compose *compose = (Compose *)data;
4232
4233         if (!event) return;
4234
4235         switch (event->keyval) {
4236         case GDK_Delete:
4237                 compose_attach_remove_selected(compose);
4238                 break;
4239         }
4240 }
4241
4242 static void compose_send_cb(gpointer data, guint action, GtkWidget *widget)
4243 {
4244         Compose *compose = (Compose *)data;
4245         gint val;
4246
4247         val = compose_send(compose);
4248
4249         if (val == 0) gtk_widget_destroy(compose->window);
4250 }
4251
4252 static void compose_send_later_cb(gpointer data, guint action,
4253                                   GtkWidget *widget)
4254 {
4255         Compose *compose = (Compose *)data;
4256         gchar tmp[22];
4257         gchar *to, *newsgroups;
4258
4259         to = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
4260         newsgroups = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
4261         if (*to == '\0' && *newsgroups == '\0') {
4262                 alertpanel_error(_("Recipient is not specified."));
4263                 return;
4264         }
4265
4266         g_snprintf(tmp, 22, "%s%ctmpmsg%d",
4267                    g_get_tmp_dir(), G_DIR_SEPARATOR, (gint)compose);
4268
4269         if (prefs_common.linewrap_at_send)
4270                 compose_wrap_line(compose);
4271
4272         if (compose_write_to_file(compose, tmp, FALSE) < 0 ||
4273             compose_queue(compose, tmp) < 0) {
4274                 alertpanel_error(_("Can't queue the message."));
4275                 return;
4276         }
4277
4278         if (prefs_common.savemsg) {
4279                 if (compose_save_to_outbox(compose, tmp) < 0)
4280                         alertpanel_error
4281                                 (_("Can't save the message to outbox."));
4282         }
4283
4284         if (unlink(tmp) < 0)
4285                 FILE_OP_ERROR(tmp, "unlink");
4286
4287         gtk_widget_destroy(compose->window);
4288 }
4289
4290 static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
4291 {
4292         Compose *compose = (Compose *)data;
4293         FolderItem *draft;
4294         gchar *tmp;
4295
4296         draft = folder_get_default_draft();
4297         folder_item_scan(draft);
4298
4299         if (procmsg_msg_exist(compose->targetinfo) &&
4300             compose->targetinfo->folder == draft) {
4301                 if (folder_item_remove_msg(draft,
4302                                            compose->targetinfo->msgnum) < 0)
4303                         g_warning(_("can't remove the old draft message\n"));
4304         }
4305         tmp = g_strdup_printf("%s%cdraft.%d", g_get_tmp_dir(),
4306                               G_DIR_SEPARATOR, (gint)compose);
4307
4308         if (compose_write_to_file(compose, tmp, TRUE) < 0) {
4309                 g_free(tmp);
4310                 return;
4311         }
4312
4313         if (folder_item_add_msg(draft, tmp, TRUE) < 0) {
4314                 unlink(tmp);
4315                 g_free(tmp);
4316                 return;
4317         }
4318
4319         g_free(tmp);
4320
4321         /* folderview_scan_folder_a(DRAFT_DIR, TRUE); */
4322
4323         gtk_widget_destroy(compose->window);
4324 }
4325
4326 static void compose_attach_cb(gpointer data, guint action, GtkWidget *widget)
4327 {
4328         Compose *compose = (Compose *)data;
4329         GList *file_list;
4330
4331         file_list = filesel_select_multiple_files(_("Select file"), NULL);
4332
4333         if (file_list) {
4334                 GList *tmp;
4335
4336                 for ( tmp = file_list; tmp; tmp = tmp->next) {
4337                         gchar *file = (gchar *) tmp->data;
4338                         compose_attach_append(compose, file, MIME_UNKNOWN);
4339                         g_free(file);
4340                 }
4341                 g_list_free(file_list);
4342         }               
4343 }
4344
4345 static void compose_insert_file_cb(gpointer data, guint action,
4346                                    GtkWidget *widget)
4347 {
4348         Compose *compose = (Compose *)data;
4349         GList *file_list;
4350
4351         file_list = filesel_select_multiple_files(_("Select file"), NULL);
4352
4353         if (file_list) {
4354                 GList *tmp;
4355
4356                 for ( tmp = file_list; tmp; tmp = tmp->next) {
4357                         gchar *file = (gchar *) tmp->data;
4358                         compose_insert_file(compose, file);
4359                         g_free(file);
4360                 }
4361                 g_list_free(file_list);
4362         }
4363 }
4364
4365 static gint compose_delete_cb(GtkWidget *widget, GdkEventAny *event,
4366                               gpointer data)
4367 {
4368         compose_close_cb(data, 0, NULL);
4369         return TRUE;
4370 }
4371
4372 static void compose_close_cb(gpointer data, guint action, GtkWidget *widget)
4373 {
4374         Compose *compose = (Compose *)data;
4375         AlertValue val;
4376
4377         if (compose->exteditor_tag != -1) {
4378                 if (!compose_ext_editor_kill(compose))
4379                         return;
4380         }
4381
4382         if (compose->modified) {
4383                 val = alertpanel(_("Discard message"),
4384                                  _("This message has been modified. discard it?"),
4385                                  _("Discard"), _("to Draft"), _("Cancel"));
4386
4387                 switch (val) {
4388                 case G_ALERTDEFAULT:
4389                         break;
4390                 case G_ALERTALTERNATE:
4391                         compose_draft_cb(data, 0, NULL);
4392                         return;
4393                 default:
4394                         return;
4395                 }
4396         }
4397
4398         gtk_widget_destroy(compose->window);
4399 }
4400
4401 static void compose_address_cb(gpointer data, guint action, GtkWidget *widget)
4402 {
4403         Compose *compose = (Compose *)data;
4404
4405         addressbook_open(compose);
4406 }
4407
4408 static void compose_ext_editor_cb(gpointer data, guint action,
4409                                   GtkWidget *widget)
4410 {
4411         Compose *compose = (Compose *)data;
4412
4413         compose_exec_ext_editor(compose);
4414 }
4415
4416 static void compose_destroy_cb(GtkWidget *widget, Compose *compose)
4417 {
4418         compose_destroy(compose);
4419 }
4420
4421 static void compose_cut_cb(Compose *compose)
4422 {
4423         if (compose->focused_editable &&
4424             GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
4425                 gtk_editable_cut_clipboard
4426                         (GTK_EDITABLE(compose->focused_editable));
4427 }
4428
4429 static void compose_copy_cb(Compose *compose)
4430 {
4431         if (compose->focused_editable &&
4432             GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
4433                 gtk_editable_copy_clipboard
4434                         (GTK_EDITABLE(compose->focused_editable));
4435 }
4436
4437 static void compose_paste_cb(Compose *compose)
4438 {
4439         if (compose->focused_editable &&
4440             GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
4441                 gtk_editable_paste_clipboard
4442                         (GTK_EDITABLE(compose->focused_editable));
4443 }
4444
4445 static void compose_allsel_cb(Compose *compose)
4446 {
4447         if (compose->focused_editable &&
4448             GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
4449                 gtk_editable_select_region
4450                         (GTK_EDITABLE(compose->focused_editable), 0, -1);
4451 }
4452
4453 static void compose_grab_focus_cb(GtkWidget *widget, Compose *compose)
4454 {
4455         if (GTK_IS_EDITABLE(widget))
4456                 compose->focused_editable = widget;
4457 }
4458
4459 static void compose_changed_cb(GtkEditable *editable, Compose *compose)
4460 {
4461         if (compose->modified == FALSE) {
4462                 compose->modified = TRUE;
4463                 compose_set_title(compose);
4464         }
4465 }
4466
4467 static void compose_button_press_cb(GtkWidget *widget, GdkEventButton *event,
4468                                     Compose *compose)
4469 {
4470         gtk_stext_set_point(GTK_STEXT(widget),
4471                            gtk_editable_get_position(GTK_EDITABLE(widget)));
4472 }
4473
4474 #if 0
4475 static void compose_key_press_cb(GtkWidget *widget, GdkEventKey *event,
4476                                  Compose *compose)
4477 {
4478         gtk_stext_set_point(GTK_STEXT(widget),
4479                            gtk_editable_get_position(GTK_EDITABLE(widget)));
4480 }
4481 #endif
4482
4483 static void compose_toggle_to_cb(gpointer data, guint action,
4484                                  GtkWidget *widget)
4485 {
4486         Compose *compose = (Compose *)data;
4487
4488         if (GTK_CHECK_MENU_ITEM(widget)->active) {
4489                 gtk_widget_show(compose->to_hbox);
4490                 gtk_widget_show(compose->to_entry);
4491                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 1, 4);
4492                 compose->use_to = TRUE;
4493         } else {
4494                 gtk_widget_hide(compose->to_hbox);
4495                 gtk_widget_hide(compose->to_entry);
4496                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 1, 0);
4497                 gtk_widget_queue_resize(compose->table_vbox);
4498                 compose->use_to = FALSE;
4499         }
4500
4501         if (addressbook_get_target_compose() == compose)
4502                 addressbook_set_target_compose(compose);
4503 }
4504
4505 static void compose_toggle_cc_cb(gpointer data, guint action,
4506                                  GtkWidget *widget)
4507 {
4508         Compose *compose = (Compose *)data;
4509
4510         if (GTK_CHECK_MENU_ITEM(widget)->active) {
4511                 gtk_widget_show(compose->cc_hbox);
4512                 gtk_widget_show(compose->cc_entry);
4513                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 3, 4);
4514                 compose->use_cc = TRUE;
4515         } else {
4516                 gtk_widget_hide(compose->cc_hbox);
4517                 gtk_widget_hide(compose->cc_entry);
4518                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 3, 0);
4519                 gtk_widget_queue_resize(compose->table_vbox);
4520                 compose->use_cc = FALSE;
4521         }
4522
4523         if (addressbook_get_target_compose() == compose)
4524                 addressbook_set_target_compose(compose);
4525 }
4526
4527 static void compose_toggle_bcc_cb(gpointer data, guint action,
4528                                   GtkWidget *widget)
4529 {
4530         Compose *compose = (Compose *)data;
4531
4532         if (GTK_CHECK_MENU_ITEM(widget)->active) {
4533                 gtk_widget_show(compose->bcc_hbox);
4534                 gtk_widget_show(compose->bcc_entry);
4535                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 4, 4);
4536                 compose->use_bcc = TRUE;
4537         } else {
4538                 gtk_widget_hide(compose->bcc_hbox);
4539                 gtk_widget_hide(compose->bcc_entry);
4540                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 4, 0);
4541                 gtk_widget_queue_resize(compose->table_vbox);
4542                 compose->use_bcc = FALSE;
4543         }
4544
4545         if (addressbook_get_target_compose() == compose)
4546                 addressbook_set_target_compose(compose);
4547 }
4548
4549 static void compose_toggle_replyto_cb(gpointer data, guint action,
4550                                       GtkWidget *widget)
4551 {
4552         Compose *compose = (Compose *)data;
4553
4554         if (GTK_CHECK_MENU_ITEM(widget)->active) {
4555                 gtk_widget_show(compose->reply_hbox);
4556                 gtk_widget_show(compose->reply_entry);
4557                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 5, 4);
4558                 compose->use_replyto = TRUE;
4559         } else {
4560                 gtk_widget_hide(compose->reply_hbox);
4561                 gtk_widget_hide(compose->reply_entry);
4562                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 5, 0);
4563                 gtk_widget_queue_resize(compose->table_vbox);
4564                 compose->use_replyto = FALSE;
4565         }
4566 }
4567
4568 static void compose_toggle_followupto_cb(gpointer data, guint action,
4569                                          GtkWidget *widget)
4570 {
4571         Compose *compose = (Compose *)data;
4572
4573         if (GTK_CHECK_MENU_ITEM(widget)->active) {
4574                 gtk_widget_show(compose->followup_hbox);
4575                 gtk_widget_show(compose->followup_entry);
4576                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 6, 4);
4577                 compose->use_followupto = TRUE;
4578         } else {
4579                 gtk_widget_hide(compose->followup_hbox);
4580                 gtk_widget_hide(compose->followup_entry);
4581                 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 6, 0);
4582                 gtk_widget_queue_resize(compose->table_vbox);
4583                 compose->use_followupto = FALSE;
4584         }
4585 }
4586
4587 static void compose_toggle_attach_cb(gpointer data, guint action,
4588                                      GtkWidget *widget)
4589 {
4590         Compose *compose = (Compose *)data;
4591
4592         if (GTK_CHECK_MENU_ITEM(widget)->active) {
4593                 gtk_widget_ref(compose->edit_vbox);
4594
4595                 gtk_container_remove(GTK_CONTAINER(compose->vbox2),
4596                                      compose->edit_vbox);
4597                 gtk_paned_add2(GTK_PANED(compose->paned), compose->edit_vbox);
4598                 gtk_box_pack_start(GTK_BOX(compose->vbox2), compose->paned,
4599                                    TRUE, TRUE, 0);
4600                 gtk_widget_show(compose->paned);
4601
4602                 gtk_widget_unref(compose->edit_vbox);
4603                 gtk_widget_unref(compose->paned);
4604
4605                 compose->use_attach = TRUE;
4606         } else {
4607                 gtk_widget_ref(compose->paned);
4608                 gtk_widget_ref(compose->edit_vbox);
4609
4610                 gtk_container_remove(GTK_CONTAINER(compose->vbox2),
4611                                      compose->paned);
4612                 gtk_container_remove(GTK_CONTAINER(compose->paned),
4613                                      compose->edit_vbox);
4614                 gtk_box_pack_start(GTK_BOX(compose->vbox2),
4615                                    compose->edit_vbox, TRUE, TRUE, 0);
4616
4617                 gtk_widget_unref(compose->edit_vbox);
4618
4619                 compose->use_attach = FALSE;
4620         }
4621 }
4622
4623 #if USE_GPGME
4624 static void compose_toggle_sign_cb(gpointer data, guint action,
4625                                    GtkWidget *widget)
4626 {
4627         Compose *compose = (Compose *)data;
4628
4629         if (GTK_CHECK_MENU_ITEM(widget)->active)
4630                 compose->use_signing = TRUE;
4631         else
4632                 compose->use_signing = FALSE;
4633 }
4634
4635 static void compose_toggle_encrypt_cb(gpointer data, guint action,
4636                                       GtkWidget *widget)
4637 {
4638         Compose *compose = (Compose *)data;
4639
4640         if (GTK_CHECK_MENU_ITEM(widget)->active)
4641                 compose->use_encryption = TRUE;
4642         else
4643                 compose->use_encryption = FALSE;
4644 }
4645 #endif /* USE_GPGME */
4646
4647 static void compose_toggle_ruler_cb(gpointer data, guint action,
4648                                     GtkWidget *widget)
4649 {
4650         Compose *compose = (Compose *)data;
4651
4652         if (GTK_CHECK_MENU_ITEM(widget)->active) {
4653                 gtk_widget_show(compose->ruler_hbox);
4654                 prefs_common.show_ruler = TRUE;
4655         } else {
4656                 gtk_widget_hide(compose->ruler_hbox);
4657                 gtk_widget_queue_resize(compose->edit_vbox);
4658                 prefs_common.show_ruler = FALSE;
4659         }
4660 }
4661
4662 static void compose_attach_drag_received_cb (GtkWidget          *widget,
4663                                              GdkDragContext     *drag_context,
4664                                              gint                x,
4665                                              gint                y,
4666                                              GtkSelectionData   *data,
4667                                              guint               info,
4668                                              guint               time,
4669                                              gpointer            user_data)
4670 {
4671         Compose *compose = (Compose *)user_data;
4672         GList *list, *tmp;
4673
4674         list = uri_list_extract_filenames((const gchar *)data->data);
4675         for (tmp = list; tmp != NULL; tmp = tmp->next)
4676                 compose_attach_append(compose, (const gchar *)tmp->data,
4677                                       MIME_UNKNOWN);
4678         list_free_strings(list);
4679         g_list_free(list);
4680 }
4681
4682 static void compose_insert_drag_received_cb (GtkWidget          *widget,
4683                                              GdkDragContext     *drag_context,
4684                                              gint                x,
4685                                              gint                y,
4686                                              GtkSelectionData   *data,
4687                                              guint               info,
4688                                              guint               time,
4689                                              gpointer            user_data)
4690 {
4691         Compose *compose = (Compose *)user_data;
4692         GList *list, *tmp;
4693
4694         list = uri_list_extract_filenames((const gchar *)data->data);
4695         for (tmp = list; tmp != NULL; tmp = tmp->next)
4696                 compose_insert_file(compose, (const gchar *)tmp->data);
4697         list_free_strings(list);
4698         g_list_free(list);
4699 }
4700
4701 static void to_activated(GtkWidget *widget, Compose *compose)
4702 {
4703         if (GTK_WIDGET_VISIBLE(compose->newsgroups_entry))
4704                 gtk_widget_grab_focus(compose->newsgroups_entry);
4705         else
4706                 gtk_widget_grab_focus(compose->subject_entry);
4707 }
4708
4709 static void newsgroups_activated(GtkWidget *widget, Compose *compose)
4710 {
4711         gtk_widget_grab_focus(compose->subject_entry);
4712 }
4713
4714 static void subject_activated(GtkWidget *widget, Compose *compose)
4715 {
4716         if (GTK_WIDGET_VISIBLE(compose->cc_entry))
4717                 gtk_widget_grab_focus(compose->cc_entry);
4718         else if (GTK_WIDGET_VISIBLE(compose->bcc_entry))
4719                 gtk_widget_grab_focus(compose->bcc_entry);
4720         else if (GTK_WIDGET_VISIBLE(compose->reply_entry))
4721                 gtk_widget_grab_focus(compose->reply_entry);
4722         else if (GTK_WIDGET_VISIBLE(compose->followup_entry))
4723                 gtk_widget_grab_focus(compose->followup_entry);
4724         else
4725                 gtk_widget_grab_focus(compose->text);
4726 }
4727
4728 static void cc_activated(GtkWidget *widget, Compose *compose)
4729 {
4730         if (GTK_WIDGET_VISIBLE(compose->bcc_entry))
4731                 gtk_widget_grab_focus(compose->bcc_entry);
4732         else if (GTK_WIDGET_VISIBLE(compose->reply_entry))
4733                 gtk_widget_grab_focus(compose->reply_entry);
4734         else if (GTK_WIDGET_VISIBLE(compose->followup_entry))
4735                 gtk_widget_grab_focus(compose->followup_entry);
4736         else
4737                 gtk_widget_grab_focus(compose->text);
4738 }
4739
4740 static void bcc_activated(GtkWidget *widget, Compose *compose)
4741 {
4742         if (GTK_WIDGET_VISIBLE(compose->reply_entry))
4743                 gtk_widget_grab_focus(compose->reply_entry);
4744         else if (GTK_WIDGET_VISIBLE(compose->followup_entry))
4745                 gtk_widget_grab_focus(compose->followup_entry);
4746         else
4747                 gtk_widget_grab_focus(compose->text);
4748 }
4749
4750 static void replyto_activated(GtkWidget *widget, Compose *compose)
4751 {
4752         if (GTK_WIDGET_VISIBLE(compose->followup_entry))
4753                 gtk_widget_grab_focus(compose->followup_entry);
4754         else
4755                 gtk_widget_grab_focus(compose->text);
4756 }
4757
4758 static void followupto_activated(GtkWidget *widget, Compose *compose)
4759 {
4760         gtk_widget_grab_focus(compose->text);
4761 }
4762
4763 static void compose_toggle_return_receipt_cb(gpointer data, guint action,
4764                                              GtkWidget *widget)
4765 {
4766         Compose *compose = (Compose *)data;
4767
4768         if (GTK_CHECK_MENU_ITEM(widget)->active)
4769                 compose->return_receipt = TRUE;
4770         else
4771                 compose->return_receipt = FALSE;
4772 }
4773
4774 static gchar *compose_quote_fmt         (Compose        *compose,
4775                                          MsgInfo        *msginfo,
4776                                          const gchar    *fmt,
4777                                          const gchar    *qmark)
4778 {
4779         gchar * quote_str = NULL;
4780
4781         if (qmark != NULL) {
4782                 gchar * p;
4783
4784                 quote_fmt_init(msginfo, NULL);
4785                 quote_fmt_scan_string(qmark);
4786                 quote_fmtparse();
4787
4788                 p = quote_fmt_get_buffer();
4789                 if (p == NULL) {
4790                         alertpanel_error
4791                                 (_("Quote mark format error."));
4792                 }
4793                 else {
4794                         quote_str = alloca(strlen(p) + 1);
4795                         strcpy(quote_str, p);
4796                 }
4797         }
4798
4799         quote_fmt_init(msginfo, quote_str);
4800         quote_fmt_scan_string(fmt);
4801         quote_fmtparse();
4802
4803         if (quote_fmt_get_buffer() == NULL)
4804                 alertpanel_error
4805                         (_("Message reply/forward format error."));
4806
4807         return quote_fmt_get_buffer();
4808 }