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