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