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