2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999,2000 Hiroyuki Yamamoto
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.
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.
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.
27 #include <gdk/gdkkeysyms.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkmenu.h>
30 #include <gtk/gtkmenuitem.h>
31 #include <gtk/gtkitemfactory.h>
32 #include <gtk/gtkcheckmenuitem.h>
33 #include <gtk/gtkoptionmenu.h>
34 #include <gtk/gtkwidget.h>
35 #include <gtk/gtkclist.h>
36 #include <gtk/gtkctree.h>
37 #include <gtk/gtkvpaned.h>
38 #include <gtk/gtkentry.h>
39 #include <gtk/gtkeditable.h>
40 #include <gtk/gtkwindow.h>
41 #include <gtk/gtksignal.h>
42 #include <gtk/gtkvbox.h>
43 #include <gtk/gtkcontainer.h>
44 #include <gtk/gtkhandlebox.h>
45 #include <gtk/gtktoolbar.h>
46 #include <gtk/gtktable.h>
47 #include <gtk/gtkhbox.h>
48 #include <gtk/gtklabel.h>
49 #include <gtk/gtkscrolledwindow.h>
50 #include <gtk/gtkthemes.h>
51 #include <gtk/gtkdnd.h>
58 /* #include <sys/utsname.h> */
64 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
74 #include "mainwindow.h"
76 #include "addressbook.h"
77 #include "folderview.h"
82 #include "prefs_common.h"
83 #include "prefs_account.h"
86 #include "procheader.h"
88 #include "statusbar.h"
95 #include "alertpanel.h"
96 #include "manage_window.h"
97 #include "gtkshruler.h"
99 #include "addr_compl.h"
102 # include "rfc2015.h"
112 #define N_ATTACH_COLS 3
114 #define B64_LINE_SIZE 57
115 #define B64_BUFFSIZE 77
117 #define MAX_REFERENCES_LEN 999
119 static GdkColor quote_color = {0, 0, 0, 0xbfff};
121 static GList *compose_list = NULL;
123 static Compose *compose_create (PrefsAccount *account);
124 static void compose_toolbar_create (Compose *compose,
125 GtkWidget *container);
126 static GtkWidget *compose_account_option_menu_create
128 static void compose_destroy (Compose *compose);
130 static gint compose_parse_header (Compose *compose,
132 static gchar *compose_parse_references (const gchar *ref,
134 static void compose_quote_file (Compose *compose,
137 static gchar *compose_quote_parse_fmt (Compose *compose,
140 static void compose_reply_set_entry (Compose *compose,
143 static void compose_reedit_set_entry (Compose *compose,
145 static void compose_insert_sig (Compose *compose);
146 static void compose_insert_file (Compose *compose,
148 static void compose_attach_append (Compose *compose,
150 ContentType cnttype);
151 static void compose_wrap_line (Compose *compose);
152 static void compose_set_title (Compose *compose);
154 static gint compose_send (Compose *compose);
155 static gint compose_write_to_file (Compose *compose,
158 static gint compose_write_body_to_file (Compose *compose,
160 static gint compose_save_to_outbox (Compose *compose,
162 static gint compose_queue (Compose *compose,
164 static void compose_write_attach (Compose *compose,
166 static gint compose_write_headers (Compose *compose,
168 const gchar *charset,
169 EncodingType encoding,
172 static void compose_convert_header (gchar *dest,
176 static void compose_generate_msgid (Compose *compose,
180 static void compose_attach_info_free (AttachInfo *ainfo);
181 static void compose_attach_remove_selected (Compose *compose);
183 static void compose_attach_property (Compose *compose);
184 static void compose_attach_property_create (gboolean *cancelled);
185 static void attach_property_ok (GtkWidget *widget,
186 gboolean *cancelled);
187 static void attach_property_cancel (GtkWidget *widget,
188 gboolean *cancelled);
189 static gint attach_property_delete_event (GtkWidget *widget,
191 gboolean *cancelled);
192 static void attach_property_key_pressed (GtkWidget *widget,
194 gboolean *cancelled);
196 static void compose_exec_ext_editor (Compose *compose);
197 static gint compose_exec_ext_editor_real (const gchar *file);
198 static gboolean compose_ext_editor_kill (Compose *compose);
199 static void compose_input_cb (gpointer data,
201 GdkInputCondition condition);
202 static void compose_set_ext_editor_sensitive (Compose *compose,
205 static gint calc_cursor_xpos (GtkSText *text,
209 /* callback functions */
211 static gboolean compose_edit_size_alloc (GtkEditable *widget,
212 GtkAllocation *allocation,
213 GtkSHRuler *shruler);
215 static void toolbar_send_cb (GtkWidget *widget,
217 static void toolbar_send_later_cb (GtkWidget *widget,
219 static void toolbar_draft_cb (GtkWidget *widget,
221 static void toolbar_insert_cb (GtkWidget *widget,
223 static void toolbar_attach_cb (GtkWidget *widget,
225 static void toolbar_sig_cb (GtkWidget *widget,
227 static void toolbar_ext_editor_cb (GtkWidget *widget,
229 static void toolbar_linewrap_cb (GtkWidget *widget,
231 static void toolbar_address_cb (GtkWidget *widget,
234 static void account_activated (GtkMenuItem *menuitem,
237 static void attach_selected (GtkCList *clist,
242 static void attach_button_pressed (GtkWidget *widget,
243 GdkEventButton *event,
245 static void attach_key_pressed (GtkWidget *widget,
249 static void compose_send_cb (gpointer data,
252 static void compose_send_later_cb (gpointer data,
256 static void compose_draft_cb (gpointer data,
260 static void compose_attach_cb (gpointer data,
263 static void compose_insert_file_cb (gpointer data,
267 static void compose_close_cb (gpointer data,
270 static void compose_address_cb (gpointer data,
274 static void compose_ext_editor_cb (gpointer data,
278 static gint compose_delete_cb (GtkWidget *widget,
281 static void compose_destroy_cb (GtkWidget *widget,
284 static void compose_cut_cb (Compose *compose);
285 static void compose_copy_cb (Compose *compose);
286 static void compose_paste_cb (Compose *compose);
287 static void compose_allsel_cb (Compose *compose);
289 static void compose_grab_focus_cb (GtkWidget *widget,
292 static void compose_changed_cb (GtkEditable *editable,
294 static void compose_button_press_cb (GtkWidget *widget,
295 GdkEventButton *event,
298 static void compose_key_press_cb (GtkWidget *widget,
303 static void compose_toggle_to_cb (gpointer data,
306 static void compose_toggle_cc_cb (gpointer data,
309 static void compose_toggle_bcc_cb (gpointer data,
312 static void compose_toggle_replyto_cb (gpointer data,
315 static void compose_toggle_followupto_cb(gpointer data,
318 static void compose_toggle_attach_cb (gpointer data,
321 static void compose_toggle_ruler_cb (gpointer data,
325 static void compose_toggle_sign_cb (gpointer data,
328 static void compose_toggle_encrypt_cb (gpointer data,
333 static void compose_attach_drag_received_cb (GtkWidget *widget,
334 GdkDragContext *drag_context,
337 GtkSelectionData *data,
341 static void compose_insert_drag_received_cb (GtkWidget *widget,
342 GdkDragContext *drag_context,
345 GtkSelectionData *data,
350 static void to_activated (GtkWidget *widget,
352 static void newsgroups_activated (GtkWidget *widget,
354 static void subject_activated (GtkWidget *widget,
356 static void cc_activated (GtkWidget *widget,
358 static void bcc_activated (GtkWidget *widget,
360 static void replyto_activated (GtkWidget *widget,
362 static void followupto_activated (GtkWidget *widget,
365 static GtkItemFactoryEntry compose_popup_entries[] =
367 {N_("/_Add..."), NULL, compose_attach_cb, 0, NULL},
368 {N_("/_Remove"), NULL, compose_attach_remove_selected, 0, NULL},
369 {N_("/---"), NULL, NULL, 0, "<Separator>"},
370 {N_("/_Property..."), NULL, compose_attach_property, 0, NULL}
373 static GtkItemFactoryEntry compose_entries[] =
375 {N_("/_File"), NULL, NULL, 0, "<Branch>"},
376 {N_("/_File/_Attach file"), "<control>M", compose_attach_cb, 0, NULL},
377 {N_("/_File/_Insert file"), "<control>I", compose_insert_file_cb, 0, NULL},
378 {N_("/_File/Insert si_gnature"), "<control>G", compose_insert_sig, 0, NULL},
379 {N_("/_File/---"), NULL, NULL, 0, "<Separator>"},
380 {N_("/_File/_Close"), "<alt>W", compose_close_cb, 0, NULL},
382 {N_("/_Edit"), NULL, NULL, 0, "<Branch>"},
383 {N_("/_Edit/_Undo"), "<control>Z", NULL, 0, NULL},
384 {N_("/_Edit/_Redo"), "<control>Y", NULL, 0, NULL},
385 {N_("/_Edit/---"), NULL, NULL, 0, "<Separator>"},
386 {N_("/_Edit/Cu_t"), "<control>X", compose_cut_cb, 0, NULL},
387 {N_("/_Edit/_Copy"), "<control>C", compose_copy_cb, 0, NULL},
388 {N_("/_Edit/_Paste"), "<control>V", compose_paste_cb, 0, NULL},
389 {N_("/_Edit/Select _all"), "<control>A", compose_allsel_cb, 0, NULL},
390 {N_("/_Edit/---"), NULL, NULL, 0, "<Separator>"},
391 {N_("/_Edit/Wrap long _lines"), "<alt>L", compose_wrap_line, 0, NULL},
392 {N_("/_Edit/Edit with e_xternal editor"), "<alt>X",
393 compose_ext_editor_cb, 0, NULL},
395 {N_("/_Message"), NULL, NULL, 0, "<Branch>"},
396 {N_("/_Message/_Send"), "<shift><control>S",
397 compose_send_cb, 0, NULL},
398 {N_("/_Message/Send _later"), "<shift><alt>S",
399 compose_send_later_cb, 0, NULL},
400 {N_("/_Message/Save to _draft folder"),
401 "<alt>D", compose_draft_cb, 0, NULL},
402 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
403 {N_("/_Message/_To"), NULL, compose_toggle_to_cb, 0, "<ToggleItem>"},
404 {N_("/_Message/_Cc"), NULL, compose_toggle_cc_cb, 0, "<ToggleItem>"},
405 {N_("/_Message/_Bcc"), NULL, compose_toggle_bcc_cb, 0, "<ToggleItem>"},
406 {N_("/_Message/_Reply to"), NULL, compose_toggle_replyto_cb, 0, "<ToggleItem>"},
407 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
408 {N_("/_Message/_Followup to"), NULL, compose_toggle_followupto_cb, 0, "<ToggleItem>"},
409 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
410 {N_("/_Message/_Attach"), NULL, compose_toggle_attach_cb, 0, "<ToggleItem>"},
412 {N_("/_Message/---"), NULL, NULL, 0, "<Separator>"},
413 {N_("/_Message/Si_gn"), NULL, compose_toggle_sign_cb, 0, "<ToggleItem>"},
414 {N_("/_Message/_Encrypt"), NULL, compose_toggle_encrypt_cb, 0, "<ToggleItem>"},
415 #endif /* USE_GPGME */
416 {N_("/_Tool"), NULL, NULL, 0, "<Branch>"},
417 {N_("/_Tool/Show _ruler"), NULL, compose_toggle_ruler_cb, 0, "<ToggleItem>"},
418 {N_("/_Tool/_Address book"), "<alt>A", compose_address_cb, 0, NULL},
419 {N_("/_Help"), NULL, NULL, 0, "<LastBranch>"},
420 {N_("/_Help/_About"), NULL, about_show, 0, NULL}
423 static GtkTargetEntry compose_mime_types[] =
425 {"text/uri-list", 0, 0}
428 void compose_new(PrefsAccount *account)
430 compose_new_with_recipient(account, NULL);
433 void compose_new_with_recipient(PrefsAccount *account, const gchar *to)
437 if (!account) account = cur_account;
438 g_return_if_fail(account != NULL);
440 compose = compose_create(account);
441 compose->mode = COMPOSE_NEW;
443 if (prefs_common.auto_sig)
444 compose_insert_sig(compose);
445 gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
446 gtk_stext_set_point(GTK_STEXT(compose->text), 0);
448 if (account->protocol != A_NNTP) {
450 compose_entry_append(compose, to, COMPOSE_TO);
451 gtk_widget_grab_focus(compose->subject_entry);
453 gtk_widget_grab_focus(compose->to_entry);
455 gtk_widget_grab_focus(compose->newsgroups_entry);
458 void compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all)
461 PrefsAccount *account;
464 g_return_if_fail(msginfo != NULL);
465 g_return_if_fail(msginfo->folder != NULL);
467 account = msginfo->folder->folder->account;
468 if (!account) account = cur_account;
469 g_return_if_fail(account != NULL);
471 MSG_UNSET_FLAGS(msginfo->flags, MSG_FORWARDED);
472 MSG_SET_FLAGS(msginfo->flags, MSG_REPLIED);
474 compose = compose_create(account);
475 compose->mode = COMPOSE_REPLY;
477 if (compose_parse_header(compose, msginfo) < 0) return;
478 compose_reply_set_entry(compose, msginfo, to_all);
480 text = GTK_STEXT(compose->text);
481 gtk_stext_freeze(text);
487 if ((fp = procmime_get_text_part(msginfo)) == NULL)
488 g_warning(_("Can't get text part\n"));
490 quote_str = compose_quote_parse_fmt
491 (compose, msginfo, prefs_common.quotefmt);
492 gtk_stext_insert(text, NULL, NULL, NULL, quote_str, -1);
494 compose_quote_file(compose, msginfo, fp);
499 if (prefs_common.auto_sig)
500 compose_insert_sig(compose);
501 gtk_editable_set_position(GTK_EDITABLE(text), 0);
502 gtk_stext_set_point(text, 0);
504 gtk_stext_thaw(text);
505 gtk_widget_grab_focus(compose->text);
508 #define INSERT_FW_HEADER(var, hdr) \
509 if (msginfo->var && *msginfo->var) { \
510 gtk_stext_insert(text, NULL, NULL, NULL, hdr, -1); \
511 gtk_stext_insert(text, NULL, NULL, NULL, msginfo->var, -1); \
512 gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1); \
515 void compose_forward(MsgInfo *msginfo, gboolean as_attach)
518 PrefsAccount *account;
523 g_return_if_fail(msginfo != NULL);
524 g_return_if_fail(msginfo->folder != NULL);
526 account = msginfo->folder->folder->account;
527 if (!account) account = cur_account;
528 g_return_if_fail(account != NULL);
530 MSG_UNSET_FLAGS(msginfo->flags, MSG_REPLIED);
531 MSG_SET_FLAGS(msginfo->flags, MSG_FORWARDED);
533 compose = compose_create(account);
534 compose->mode = COMPOSE_FORWARD;
536 if (msginfo->subject && *msginfo->subject) {
537 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Fw: ");
538 gtk_entry_append_text(GTK_ENTRY(compose->subject_entry),
542 text = GTK_STEXT(compose->text);
543 gtk_stext_freeze(text);
548 msgfile = procmsg_get_message_file_path(msginfo);
549 if (!is_file_exist(msgfile))
550 g_warning(_("%s: file not exist\n"), msgfile);
552 compose_attach_append(compose, msgfile,
553 MIME_MESSAGE_RFC822);
557 if ((fp = procmime_get_text_part(msginfo)) == NULL)
558 g_warning(_("Can't get text part\n"));
561 gtk_stext_insert(text, NULL, NULL, NULL,
562 _("\n\nBegin forwarded message:\n\n"), -1);
563 INSERT_FW_HEADER(date, "Date: ");
564 INSERT_FW_HEADER(from, "From: ");
565 INSERT_FW_HEADER(to, "To: ");
566 INSERT_FW_HEADER(newsgroups, "Newsgroups: ");
567 INSERT_FW_HEADER(subject, "Subject: ");
568 gtk_stext_insert(text, NULL, NULL, NULL, "\n\n", 2);
571 while (fgets(buf, sizeof(buf), fp) != NULL)
572 gtk_stext_insert(text, NULL, NULL, NULL,
578 if (prefs_common.auto_sig)
579 compose_insert_sig(compose);
580 gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
581 gtk_stext_set_point(GTK_STEXT(compose->text), 0);
583 gtk_stext_thaw(text);
584 if (account->protocol != A_NNTP)
585 gtk_widget_grab_focus(compose->to_entry);
587 gtk_widget_grab_focus(compose->newsgroups_entry);
590 void compose_reedit(MsgInfo *msginfo)
593 PrefsAccount *account;
598 g_return_if_fail(msginfo != NULL);
599 g_return_if_fail(msginfo->folder != NULL);
601 account = msginfo->folder->folder->account;
602 if (!account) account = cur_account;
603 g_return_if_fail(account != NULL);
605 compose = compose_create(account);
606 compose->mode = COMPOSE_REEDIT_DRAFT;
607 compose->targetinfo = procmsg_msginfo_copy(msginfo);
609 if (compose_parse_header(compose, msginfo) < 0) return;
610 compose_reedit_set_entry(compose, msginfo);
612 text = GTK_STEXT(compose->text);
613 gtk_stext_freeze(text);
615 if ((fp = procmime_get_text_part(msginfo)) == NULL)
616 g_warning(_("Can't get text part\n"));
618 while (fgets(buf, sizeof(buf), fp) != NULL)
619 gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
623 gtk_stext_thaw(text);
624 gtk_widget_grab_focus(compose->text);
627 GList *compose_get_compose_list(void)
632 void compose_entry_append(Compose *compose, const gchar *address,
633 ComposeEntryType type)
638 if (!address || *address == '\0') return;
642 entry = GTK_ENTRY(compose->cc_entry);
645 entry = GTK_ENTRY(compose->bcc_entry);
649 entry = GTK_ENTRY(compose->to_entry);
653 text = gtk_entry_get_text(entry);
655 gtk_entry_append_text(entry, ", ");
656 gtk_entry_append_text(entry, address);
659 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
661 static HeaderEntry hentry[] = {{"Reply-To:", NULL, TRUE},
662 {"Cc:", NULL, FALSE},
663 {"References:", NULL, FALSE},
664 {"Bcc:", NULL, FALSE},
665 {"Newsgroups:", NULL, FALSE},
666 {"Followup-To:", NULL, FALSE},
667 {NULL, NULL, FALSE}};
681 g_return_val_if_fail(msginfo != NULL, -1);
683 if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
684 procheader_get_header_fields(fp, hentry);
687 if (hentry[H_REPLY_TO].body != NULL) {
688 conv_unmime_header_overwrite(hentry[H_REPLY_TO].body);
689 compose->replyto = hentry[H_REPLY_TO].body;
690 hentry[H_REPLY_TO].body = NULL;
692 if (hentry[H_CC].body != NULL) {
693 conv_unmime_header_overwrite(hentry[H_CC].body);
694 compose->cc = hentry[H_CC].body;
695 hentry[H_CC].body = NULL;
697 if (hentry[H_REFERENCES].body != NULL) {
698 if (compose->mode == COMPOSE_REEDIT_DRAFT)
699 compose->references = hentry[H_REFERENCES].body;
701 compose->references = compose_parse_references
702 (hentry[H_REFERENCES].body, msginfo->msgid);
703 g_free(hentry[H_REFERENCES].body);
705 hentry[H_REFERENCES].body = NULL;
707 if (hentry[H_BCC].body != NULL) {
708 if (compose->mode == COMPOSE_REEDIT_DRAFT) {
709 conv_unmime_header_overwrite(hentry[H_BCC].body);
710 compose->bcc = hentry[H_BCC].body;
712 g_free(hentry[H_BCC].body);
713 hentry[H_BCC].body = NULL;
715 if (hentry[H_NEWSGROUPS].body != NULL) {
716 compose->newsgroups = hentry[H_NEWSGROUPS].body;
717 hentry[H_NEWSGROUPS].body = NULL;
719 if (hentry[H_FOLLOWUP_TO].body != NULL) {
720 conv_unmime_header_overwrite(hentry[H_FOLLOWUP_TO].body);
721 compose->followup_to = hentry[H_FOLLOWUP_TO].body;
722 hentry[H_FOLLOWUP_TO].body = NULL;
725 if (compose->mode == COMPOSE_REEDIT_DRAFT && msginfo->inreplyto)
726 compose->inreplyto = g_strdup(msginfo->inreplyto);
727 else if (compose->mode != COMPOSE_REEDIT_DRAFT &&
728 msginfo->msgid && *msginfo->msgid) {
729 compose->inreplyto = g_strdup(msginfo->msgid);
731 if (!compose->references) {
732 if (msginfo->inreplyto && *msginfo->inreplyto)
733 compose->references =
734 g_strdup_printf("<%s>\n\t<%s>",
738 compose->references =
739 g_strconcat("<", msginfo->msgid, ">",
747 static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
749 GSList *ref_id_list, *cur;
753 ref_id_list = references_list_append(NULL, ref);
754 if (!ref_id_list) return NULL;
756 ref_id_list = g_slist_append(ref_id_list, g_strdup(msgid));
761 for (cur = ref_id_list; cur != NULL; cur = cur->next)
762 /* "<" + Message-ID + ">" + CR+LF+TAB */
763 len += strlen((gchar *)cur->data) + 5;
765 if (len > MAX_REFERENCES_LEN) {
766 /* remove second message-ID */
767 if (ref_id_list && ref_id_list->next &&
768 ref_id_list->next->next) {
769 g_free(ref_id_list->next->data);
770 ref_id_list = g_slist_remove
771 (ref_id_list, ref_id_list->next->data);
773 slist_free_strings(ref_id_list);
774 g_slist_free(ref_id_list);
781 new_ref = g_string_new("");
782 for (cur = ref_id_list; cur != NULL; cur = cur->next) {
783 if (new_ref->len > 0)
784 g_string_append(new_ref, "\n\t");
785 g_string_sprintfa(new_ref, "<%s>", (gchar *)cur->data);
788 slist_free_strings(ref_id_list);
789 g_slist_free(ref_id_list);
791 new_ref_str = new_ref->str;
792 g_string_free(new_ref, FALSE);
797 static void compose_quote_file(Compose *compose, MsgInfo *msginfo, FILE *fp)
799 GtkSText *text = GTK_STEXT(compose->text);
802 GdkColor *qcolor = NULL;
805 gchar *linep, *cur, *leftp;
806 gint line_len, cur_len;
811 /* if (prefs_common.enable_color) qcolor = "e_color; */
812 if (prefs_common.quotemark && *prefs_common.quotemark)
813 qmark = prefs_common.quotemark;
816 quote_str = compose_quote_parse_fmt(compose, msginfo, qmark);
817 g_return_if_fail(quote_str != NULL);
818 qlen = strlen(quote_str);
820 if (!prefs_common.linewrap_quote ||
821 prefs_common.linewrap_len <= qlen) {
822 while (fgets(buf, sizeof(buf), fp) != NULL) {
823 gtk_stext_insert(text, NULL, qcolor, NULL,
825 gtk_stext_insert(text, NULL, qcolor, NULL, buf, -1);
831 wrap_len = prefs_common.linewrap_len - qlen;
833 while (fgets(buf, sizeof(buf), fp) != NULL) {
835 str_len = strlen(buf);
837 if (str_len <= wrap_len) {
838 gtk_stext_insert(text, NULL, qcolor, NULL,
840 gtk_stext_insert(text, NULL, qcolor, NULL, buf, -1);
841 gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
845 linep = cur = leftp = buf;
846 line_len = cur_len = 0;
848 while (*cur != '\0') {
849 ch_len = mblen(cur, MB_CUR_MAX);
850 if (ch_len < 0) ch_len = 1;
852 if (ch_len == 1 && isspace(*cur)) {
853 linep = cur + ch_len;
854 line_len = cur_len + ch_len;
857 if (cur_len + ch_len > wrap_len && line_len > 0) {
858 gtk_stext_insert(text, NULL, qcolor, NULL,
861 if (isspace(*(linep - 1)))
862 gtk_stext_insert(text, NULL,
864 leftp, line_len - 1);
866 gtk_stext_insert(text, NULL,
869 gtk_stext_insert(text, NULL, NULL, NULL,
873 cur_len = cur_len - line_len + ch_len;
880 linep = cur + ch_len;
881 line_len = cur_len + ch_len;
888 gtk_stext_insert(text, NULL, qcolor, NULL,
890 gtk_stext_insert(text, NULL, qcolor, NULL, leftp, -1);
891 gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
898 static gchar *compose_quote_parse_fmt(Compose *compose, MsgInfo *msginfo,
902 size_t buf_len = 1024;
910 if (!fmt || *fmt == '\0') return 0;
912 Xalloca(mbs, sizeof(wchar_t) + 1, return 0);
913 Xalloca(wcsfmt, (strlen(fmt) + 1) * sizeof(wchar_t), return 0);
914 mbstowcs(wcsfmt, fmt, strlen(fmt) + 1);
917 ext_str = g_malloc(sizeof(gchar) * buf_len);
918 g_return_val_if_fail(ext_str != NULL, NULL);
923 len = wctomb(mbs, *sp);
929 wctomb(mbs, *(++sp));
941 case 'I': /* initial */
942 if (!msginfo->fromname) {sp++; break;}
943 p = msginfo->fromname;
944 tmp[0] = tmp[1] = tmp[2] = '\0';
946 if (*p && isalnum(*p))
947 tmp[0] = toupper(*p);
954 while (*p && !isspace(*p)) p++;
955 while (*p && isspace(*p)) p++;
956 if (*p && isalnum(*p))
957 tmp[1] = toupper(*p);
960 if (tmp[1]) str = tmp;
964 str = msginfo->fromname;
968 if (!msginfo->fromname) {sp++; break;}
969 Xstrdup_a(str, msginfo->fromname,
972 while (*p && !isspace(*p)) p++;
977 str = msginfo->subject;
985 if (!msginfo->msgid) {sp++; break;}
986 Xalloca(str, strlen(msginfo->msgid) + 3,
988 g_snprintf(str, strlen(msginfo->msgid) + 3,
989 "<%s>", msginfo->msgid);
1001 while (ext_len + strlen(str) + 1 > buf_len)
1003 ext_str = g_realloc(ext_str,
1004 sizeof(gchar) * buf_len);
1005 g_return_val_if_fail(ext_str != NULL, NULL);
1006 strcpy(ext_str + ext_len, str);
1007 ext_len += strlen(str);
1009 } else if (*mbs == '\\') {
1010 wctomb(mbs, *(++sp));
1028 while (ext_len + strlen(str) + 1 > buf_len)
1030 ext_str = g_realloc(ext_str,
1031 sizeof(gchar) * buf_len);
1032 g_return_val_if_fail(ext_str != NULL, NULL);
1033 strcpy(ext_str + ext_len, str);
1034 ext_len += strlen(str);
1038 while (ext_len + len + 1 > buf_len) buf_len += 1024;
1039 ext_str = g_realloc(ext_str, sizeof(gchar) * buf_len);
1040 g_return_val_if_fail(ext_str != NULL, NULL);
1041 strcpy(ext_str + ext_len, mbs);
1048 ext_str = g_realloc(ext_str, strlen(ext_str) + 1);
1053 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
1059 GHashTable *to_table;
1061 g_return_if_fail(compose->account != NULL);
1062 g_return_if_fail(msginfo != NULL);
1064 if (compose->account->protocol != A_NNTP)
1065 gtk_entry_set_text(GTK_ENTRY(compose->to_entry),
1066 compose->replyto ? compose->replyto
1067 : msginfo->from ? msginfo->from : "");
1068 if (compose->account->protocol == A_NNTP)
1069 gtk_entry_set_text(GTK_ENTRY(compose->newsgroups_entry),
1070 compose->followup_to ? compose->followup_to
1071 : compose->newsgroups ? compose->newsgroups
1074 if (msginfo->subject && *msginfo->subject) {
1075 gchar *buf, *buf2, *p;
1077 buf = g_strdup(msginfo->subject);
1078 while (!strncasecmp(buf, "Re:", 3)) {
1080 while (isspace(*p)) p++;
1081 memmove(buf, p, strlen(p) + 1);
1084 buf2 = g_strdup_printf("Re: %s", buf);
1085 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1089 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
1091 if (!to_all || compose->account->protocol == A_NNTP) return;
1093 from = g_strdup(compose->replyto ? compose->replyto :
1094 msginfo->from ? msginfo->from : "");
1095 extract_address(from);
1097 cc_list = address_list_append(NULL, msginfo->to);
1098 cc_list = address_list_append(cc_list, compose->cc);
1100 to_table = g_hash_table_new(g_str_hash, g_str_equal);
1101 g_hash_table_insert(to_table, from, GINT_TO_POINTER(1));
1102 if (compose->account)
1103 g_hash_table_insert(to_table, compose->account->address,
1104 GINT_TO_POINTER(1));
1106 /* remove address on To: and that of current account */
1107 for (cur = cc_list; cur != NULL; ) {
1108 GSList *next = cur->next;
1110 if (g_hash_table_lookup(to_table, cur->data) != NULL)
1111 cc_list = g_slist_remove(cc_list, cur->data);
1113 g_hash_table_insert(to_table, cur->data, cur);
1117 g_hash_table_destroy(to_table);
1121 for (cur = cc_list; cur != NULL; cur = cur->next)
1122 compose_entry_append(compose, (gchar *)cur->data,
1124 slist_free_strings(cc_list);
1125 g_slist_free(cc_list);
1129 #define SET_ENTRY(entry, str) \
1132 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
1135 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
1137 g_return_if_fail(msginfo != NULL);
1139 SET_ENTRY(to_entry, msginfo->to);
1140 SET_ENTRY(subject_entry, msginfo->subject);
1141 SET_ENTRY(cc_entry, compose->cc);
1142 SET_ENTRY(bcc_entry, compose->bcc);
1143 SET_ENTRY(reply_entry, compose->replyto);
1146 GtkItemFactory *ifactory;
1147 GtkWidget *menuitem;
1149 ifactory = gtk_item_factory_from_widget(compose->menubar);
1150 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Bcc");
1151 gtk_check_menu_item_set_active
1152 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1154 if (compose->replyto) {
1155 GtkItemFactory *ifactory;
1156 GtkWidget *menuitem;
1158 ifactory = gtk_item_factory_from_widget(compose->menubar);
1159 menuitem = gtk_item_factory_get_item
1160 (ifactory, "/Message/Reply to");
1161 gtk_check_menu_item_set_active
1162 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
1166 static void compose_insert_sig(Compose *compose)
1170 if (compose->account && compose->account->sig_path)
1171 sigfile = g_strdup(compose->account->sig_path);
1173 sigfile = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S,
1174 DEFAULT_SIGNATURE, NULL);
1177 if (!is_file_or_fifo_exist(sigfile)) {
1182 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL, "\n\n", 2);
1183 if (prefs_common.sig_sep) {
1184 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1185 prefs_common.sig_sep, -1);
1186 gtk_stext_insert(GTK_STEXT(compose->text), NULL, NULL, NULL,
1190 compose_insert_file(compose, sigfile);
1194 static void compose_insert_file(Compose *compose, const gchar *file)
1196 GtkSText *text = GTK_STEXT(compose->text);
1197 gchar buf[BUFFSIZE];
1200 g_return_if_fail(file != NULL);
1202 if ((fp = fopen(file, "r")) == NULL) {
1203 FILE_OP_ERROR(file, "fopen");
1207 gtk_stext_freeze(text);
1209 while (fgets(buf, sizeof(buf), fp) != NULL)
1210 gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
1212 gtk_stext_thaw(text);
1217 static void compose_attach_append(Compose *compose, const gchar *file,
1218 ContentType cnttype)
1221 gchar *text[N_ATTACH_COLS];
1225 if (!is_file_exist(file)) {
1226 g_warning(_("File %s doesn't exist\n"), file);
1229 if ((size = get_file_size(file)) < 0) {
1230 g_warning(_("Can't get file size of %s\n"), file);
1234 alertpanel_notice(_("File %s is empty\n"), file);
1238 if (!compose->use_attach) {
1239 GtkItemFactory *ifactory;
1240 GtkWidget *menuitem;
1242 ifactory = gtk_item_factory_from_widget(compose->menubar);
1243 menuitem = gtk_item_factory_get_item(ifactory,
1245 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1249 ainfo = g_new0(AttachInfo, 1);
1250 ainfo->file = g_strdup(file);
1252 if (cnttype == MIME_MESSAGE_RFC822) {
1253 ainfo->content_type = g_strdup("message/rfc822");
1254 ainfo->encoding = ENC_7BIT;
1255 ainfo->name = g_strdup_printf(_("Message: %s"),
1258 ainfo->content_type = procmime_get_mime_type(file);
1259 if (!ainfo->content_type)
1260 ainfo->content_type =
1261 g_strdup("application/octet-stream");
1262 ainfo->encoding = ENC_BASE64;
1263 ainfo->name = g_strdup(g_basename(file));
1267 text[COL_MIMETYPE] = ainfo->content_type;
1268 text[COL_SIZE] = to_human_readable(size);
1269 text[COL_NAME] = ainfo->name;
1271 row = gtk_clist_append(GTK_CLIST(compose->attach_clist), text);
1272 gtk_clist_set_row_data(GTK_CLIST(compose->attach_clist), row, ainfo);
1274 if (cnttype != MIME_MESSAGE_RFC822)
1275 compose_changed_cb(NULL, compose);
1278 static void compose_wrap_line(Compose *compose)
1280 GtkSText *text = GTK_STEXT(compose->text);
1282 guint line_pos = 0, cur_pos = 0;
1283 gint line_len = 0, cur_len = 0;
1285 gchar cbuf[MB_CUR_MAX];
1287 gtk_stext_freeze(text);
1289 text_len = gtk_stext_get_length(text);
1291 for (; cur_pos < text_len; cur_pos++) {
1292 if (text->use_wchar)
1294 (cbuf, (wchar_t)GTK_STEXT_INDEX(text, cur_pos));
1296 cbuf[0] = GTK_STEXT_INDEX(text, cur_pos);
1300 if (ch_len == 1 && *cbuf == '\n') {
1301 line_pos = cur_pos + 1;
1302 line_len = cur_len = 0;
1311 if (ch_len == 1 && isspace(*cbuf)) {
1312 line_pos = cur_pos + 1;
1313 line_len = cur_len + ch_len;
1316 if (cur_len + ch_len > prefs_common.linewrap_len &&
1320 if (text->use_wchar)
1321 tlen = wctomb(cbuf, (wchar_t)GTK_STEXT_INDEX(text, line_pos - 1));
1323 cbuf[0] = GTK_STEXT_INDEX(text, line_pos - 1);
1326 if (tlen == 1 && isspace(*cbuf)) {
1327 gtk_stext_set_point(text, line_pos);
1328 gtk_stext_backward_delete(text, 1);
1336 gtk_stext_set_point(text, line_pos);
1337 gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
1341 cur_len = cur_len - line_len + ch_len;
1347 line_pos = cur_pos + 1;
1348 line_len = cur_len + ch_len;
1353 gtk_stext_thaw(text);
1356 static void compose_set_title(Compose *compose)
1361 edited = compose->modified ? _(" [Edited]") : "";
1362 if (compose->account && compose->account->address)
1363 str = g_strdup_printf(_("%s - Compose message%s"),
1364 compose->account->address, edited);
1366 str = g_strdup_printf(_("Compose message%s"), edited);
1367 gtk_window_set_title(GTK_WINDOW(compose->window), str);
1371 static gint compose_send(Compose *compose)
1373 gchar tmp[MAXPATHLEN + 1];
1374 gchar *to, *newsgroups;
1376 static gboolean lock = FALSE;
1380 g_return_val_if_fail(compose->account != NULL, -1);
1381 g_return_val_if_fail(compose->orig_account != NULL, -1);
1385 to = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
1386 newsgroups = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
1387 if (*to == '\0' && *newsgroups == '\0') {
1388 alertpanel_error(_("Recipient is not specified."));
1393 /* write to temporary file */
1394 g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
1395 get_rc_dir(), G_DIR_SEPARATOR, (gint)compose);
1397 if (prefs_common.linewrap_at_send)
1398 compose_wrap_line(compose);
1400 if (compose_write_to_file(compose, tmp, FALSE) < 0) {
1405 if (!compose->to_list && !compose->newsgroup_list) {
1406 g_warning(_("can't get recipient list."));
1412 if (compose->to_list) {
1415 if (compose->account->protocol != A_NNTP)
1416 ac = compose->account;
1417 else if (compose->orig_account->protocol != A_NNTP)
1418 ac = compose->orig_account;
1419 else if (cur_account && cur_account->protocol != A_NNTP)
1422 ac = account_get_default();
1423 if (!ac || ac->protocol == A_NNTP) {
1424 alertpanel_error(_("Account for sending mail is not specified.\n"
1425 "Please select a mail account before sending."));
1431 ok = send_message(tmp, ac, compose->to_list);
1432 statusbar_pop_all();
1435 if (ok == 0 && compose->newsgroup_list) {
1438 if (compose->account->protocol == A_NNTP)
1439 folder = FOLDER(compose->account->folder);
1441 folder = FOLDER(compose->orig_account->folder);
1443 ok = news_post(folder, tmp);
1445 alertpanel_error(_("Error occurred while posting the message to %s ."),
1446 compose->account->nntp_server);
1453 /* queue message if failed to send */
1455 if (prefs_common.queue_msg) {
1460 _("Error occurred while sending the message.\n"
1461 "Put this message into queue folder?"),
1462 _("OK"), _("Cancel"), NULL);
1463 if (G_ALERTDEFAULT == val) {
1464 ok = compose_queue(compose, tmp);
1466 alertpanel_error(_("Can't queue the message."));
1469 alertpanel_error(_("Error occurred while sending the message."));
1472 /* save message to outbox */
1473 if (ok == 0 && prefs_common.savemsg) {
1474 if (compose_save_to_outbox(compose, tmp) < 0)
1476 (_("Can't save the message to outbox."));
1479 if (unlink(tmp) < 0) FILE_OP_ERROR(tmp, "unlink");
1484 static gint compose_write_to_file(Compose *compose, const gchar *file,
1491 const gchar *out_codeset;
1492 EncodingType encoding;
1494 if ((fp = fopen(file, "w")) == NULL) {
1495 FILE_OP_ERROR(file, "fopen");
1499 /* chmod for security */
1500 if (change_file_mode_rw(fp, file) < 0) {
1501 FILE_OP_ERROR(file, "chmod");
1502 g_warning(_("can't change file mode\n"));
1505 /* get all composed text */
1506 chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
1507 len = strlen(chars);
1508 if (is_ascii_str(chars)) {
1509 buf = g_strdup(chars);
1510 out_codeset = "US-ASCII";
1511 encoding = ENC_7BIT;
1513 const gchar *src_codeset;
1515 out_codeset = conv_get_outgoing_charset_str();
1516 if (!strcasecmp(out_codeset, "US-ASCII"))
1517 out_codeset = "ISO-8859-1";
1518 encoding = procmime_get_encoding_for_charset(out_codeset);
1519 debug_print("charset = %s, encoding = %s\n",
1520 out_codeset, procmime_get_encoding_str(encoding));
1522 src_codeset = conv_get_current_charset_str();
1523 buf = conv_codeset_strdup(chars, src_codeset, out_codeset);
1528 alertpanel_error(_("Can't convert the codeset of the message."));
1535 if (compose_write_headers
1536 (compose, fp, out_codeset, encoding, is_draft) < 0) {
1537 g_warning(_("can't write headers\n"));
1544 if (compose->use_attach) {
1546 /* This prolog message is ignored by mime software and
1547 * because it would make our signing/encryption task
1548 * tougher, we don't emit it in that case */
1549 if (!compose->use_signing && !compose->use_encryption)
1551 fputs("This is a multi-part message in MIME format.\n", fp);
1553 fprintf(fp, "\n--%s\n", compose->boundary);
1554 fprintf(fp, "Content-Type: text/plain; charset=%s\n",
1556 fprintf(fp, "Content-Transfer-Encoding: %s\n",
1557 procmime_get_encoding_str(encoding));
1563 if (encoding == ENC_BASE64) {
1564 gchar outbuf[B64_BUFFSIZE];
1567 for (i = 0; i < len; i += B64_LINE_SIZE) {
1568 l = MIN(B64_LINE_SIZE, len - i);
1569 to64frombits(outbuf, buf + i, l);
1573 } else if (fwrite(buf, sizeof(gchar), len, fp) != len) {
1574 FILE_OP_ERROR(file, "fwrite");
1582 if (compose->use_attach)
1583 compose_write_attach(compose, fp);
1585 if (fclose(fp) == EOF) {
1586 FILE_OP_ERROR(file, "fclose");
1592 if (compose->use_signing) {
1593 if (rfc2015_sign(file, compose->account) < 0) {
1598 if (compose->use_encryption) {
1599 if (rfc2015_encrypt(file, compose->to_list) < 0) {
1604 #endif /* USE_GPGME */
1609 static gint compose_write_body_to_file(Compose *compose, const gchar *file)
1615 if ((fp = fopen(file, "w")) == NULL) {
1616 FILE_OP_ERROR(file, "fopen");
1620 /* chmod for security */
1621 if (change_file_mode_rw(fp, file) < 0) {
1622 FILE_OP_ERROR(file, "chmod");
1623 g_warning(_("can't change file mode\n"));
1626 chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
1629 len = strlen(chars);
1630 if (fwrite(chars, sizeof(gchar), len, fp) != len) {
1631 FILE_OP_ERROR(file, "fwrite");
1640 if (fclose(fp) == EOF) {
1641 FILE_OP_ERROR(file, "fclose");
1648 static gint compose_save_to_outbox(Compose *compose, const gchar *file)
1655 debug_print(_("saving sent message...\n"));
1657 outbox = folder_get_default_outbox();
1658 folder_item_scan(outbox);
1659 if ((num = folder_item_add_msg(outbox, file)) < 0) {
1660 g_warning(_("can't save message\n"));
1664 path = folder_item_get_path(outbox);
1665 if ((fp = procmsg_open_mark_file(path, TRUE)) == NULL)
1666 g_warning(_("can't open mark file\n"));
1670 newmsginfo.msgnum = num;
1671 newmsginfo.flags = 0;
1672 procmsg_write_flags(&newmsginfo, fp);
1680 static gint compose_queue(Compose *compose, const gchar *file)
1683 gchar *tmp, *queue_path;
1686 gchar buf[BUFFSIZE];
1689 debug_print(_("queueing message...\n"));
1690 g_return_val_if_fail(compose->to_list != NULL, -1);
1691 g_return_val_if_fail(compose->account != NULL, -1);
1693 tmp = g_strdup_printf("%s%cqueue.%d", g_get_tmp_dir(),
1694 G_DIR_SEPARATOR, (gint)compose);
1695 if ((fp = fopen(tmp, "w")) == NULL) {
1696 FILE_OP_ERROR(tmp, "fopen");
1700 if ((src_fp = fopen(file, "r")) == NULL) {
1701 FILE_OP_ERROR(file, "fopen");
1707 if (change_file_mode_rw(fp, tmp) < 0) {
1708 FILE_OP_ERROR(tmp, "chmod");
1709 g_warning(_("can't change file mode\n"));
1712 /* queueing variables */
1713 fprintf(fp, "AF:\n");
1714 fprintf(fp, "NF:0\n");
1715 fprintf(fp, "PS:10\n");
1716 fprintf(fp, "SRH:1\n");
1717 fprintf(fp, "SFN:\n");
1718 fprintf(fp, "DSR:\n");
1720 fprintf(fp, "MID:<%s>\n", compose->msgid);
1722 fprintf(fp, "MID:\n");
1723 fprintf(fp, "CFG:\n");
1724 fprintf(fp, "PT:0\n");
1725 fprintf(fp, "S:%s\n", compose->account->address);
1726 fprintf(fp, "RQ:\n");
1727 if (compose->account->smtp_server)
1728 fprintf(fp, "SSV:%s\n", compose->account->smtp_server);
1730 fprintf(fp, "SSV:\n");
1731 if (compose->account->nntp_server)
1732 fprintf(fp, "NSV:%s\n", compose->account->nntp_server);
1734 fprintf(fp, "NSV:\n");
1735 fprintf(fp, "SSH:\n");
1736 fprintf(fp, "R:<%s>", (gchar *)compose->to_list->data);
1737 for (cur = compose->to_list->next; cur != NULL; cur = cur->next)
1738 fprintf(fp, ",<%s>", (gchar *)cur->data);
1742 while (fgets(buf, sizeof(buf), src_fp) != NULL) {
1743 if (fputs(buf, fp) == EOF) {
1744 FILE_OP_ERROR(tmp, "fputs");
1754 if (fclose(fp) == EOF) {
1755 FILE_OP_ERROR(tmp, "fclose");
1761 queue = folder_get_default_queue();
1762 folder_item_scan(queue);
1763 if ((num = folder_item_add_msg(queue, tmp)) < 0) {
1764 g_warning(_("can't queue the message\n"));
1772 queue_path = folder_item_get_path(queue);
1773 if ((fp = procmsg_open_mark_file(queue_path, TRUE)) == NULL)
1774 g_warning(_("can't open mark file\n"));
1778 newmsginfo.msgnum = num;
1779 newmsginfo.flags = 0;
1780 procmsg_write_flags(&newmsginfo, fp);
1785 folder_item_scan(queue);
1786 folderview_update_item(queue, TRUE);
1791 static void compose_write_attach(Compose *compose, FILE *fp)
1794 GtkCList *clist = GTK_CLIST(compose->attach_clist);
1797 gchar filename[BUFFSIZE];
1800 for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
1802 if ((attach_fp = fopen(ainfo->file, "r")) == NULL) {
1803 g_warning(_("Can't open file %s\n"), ainfo->file);
1807 fprintf(fp, "\n--%s\n", compose->boundary);
1809 if (!strcmp2(ainfo->content_type, "message/rfc822")) {
1810 fprintf(fp, "Content-Type: %s\n", ainfo->content_type);
1811 fprintf(fp, "Content-Disposition: inline\n");
1813 conv_encode_header(filename, sizeof(filename),
1815 fprintf(fp, "Content-Type: %s;\n"
1817 ainfo->content_type, filename);
1818 fprintf(fp, "Content-Disposition: attachment;\n"
1819 " filename=\"%s\"\n", filename);
1822 fprintf(fp, "Content-Transfer-Encoding: %s\n\n",
1823 procmime_get_encoding_str(ainfo->encoding));
1825 if (ainfo->encoding == ENC_7BIT) {
1826 gchar buf[BUFFSIZE];
1828 while (fgets(buf, sizeof(buf), attach_fp) != NULL) {
1830 if (len > 1 && buf[len - 1] == '\n' &&
1831 buf[len - 2] == '\r') {
1832 buf[len - 2] = '\n';
1833 buf[len - 1] = '\0';
1838 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
1840 while ((len = fread(inbuf, sizeof(gchar),
1841 B64_LINE_SIZE, attach_fp))
1843 to64frombits(outbuf, inbuf, B64_LINE_SIZE);
1847 if (len > 0 && feof(attach_fp)) {
1848 to64frombits(outbuf, inbuf, len);
1857 fprintf(fp, "\n--%s--\n", compose->boundary);
1860 static gint compose_write_headers(Compose *compose, FILE *fp,
1861 const gchar *charset, EncodingType encoding,
1864 gchar buf[BUFFSIZE];
1866 /* struct utsname utsbuf; */
1868 g_return_val_if_fail(fp != NULL, -1);
1869 g_return_val_if_fail(charset != NULL, -1);
1870 g_return_val_if_fail(compose->account != NULL, -1);
1871 g_return_val_if_fail(compose->account->address != NULL, -1);
1874 if (compose->account->add_date) {
1875 get_rfc822_date(buf, sizeof(buf));
1876 fprintf(fp, "Date: %s\n", buf);
1880 if (compose->account->name && *compose->account->name) {
1881 compose_convert_header
1882 (buf, sizeof(buf), compose->account->name,
1884 fprintf(fp, "From: %s <%s>\n", buf, compose->account->address);
1886 fprintf(fp, "From: %s\n", compose->account->address);
1888 slist_free_strings(compose->to_list);
1889 g_slist_free(compose->to_list);
1890 compose->to_list = NULL;
1893 if (compose->use_to) {
1894 str = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
1896 Xstrdup_a(str, str, return -1);
1899 compose->to_list = address_list_append
1900 (compose->to_list, str);
1901 compose_convert_header(buf, sizeof(buf), str,
1903 fprintf(fp, "To: %s\n", buf);
1908 slist_free_strings(compose->newsgroup_list);
1909 g_slist_free(compose->newsgroup_list);
1910 compose->newsgroup_list = NULL;
1913 str = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
1915 Xstrdup_a(str, str, return -1);
1919 compose->newsgroup_list =
1920 newsgroup_list_append(compose->newsgroup_list,
1922 compose_convert_header(buf, sizeof(buf), str,
1923 strlen("Newsgroups: "));
1924 fprintf(fp, "Newsgroups: %s\n", buf);
1928 if (!is_draft && !compose->to_list && !compose->newsgroup_list)
1932 if (compose->use_cc) {
1933 str = gtk_entry_get_text(GTK_ENTRY(compose->cc_entry));
1935 Xstrdup_a(str, str, return -1);
1938 compose->to_list = address_list_append
1939 (compose->to_list, str);
1940 compose_convert_header(buf, sizeof(buf), str,
1942 fprintf(fp, "Cc: %s\n", buf);
1948 if (compose->use_bcc) {
1949 str = gtk_entry_get_text(GTK_ENTRY(compose->bcc_entry));
1951 Xstrdup_a(str, str, return -1);
1954 compose->to_list = address_list_append
1955 (compose->to_list, str);
1957 compose_convert_header
1958 (buf, sizeof(buf), str,
1960 fprintf(fp, "Bcc: %s\n", buf);
1967 str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
1969 Xstrdup_a(str, str, return -1);
1972 compose_convert_header(buf, sizeof(buf), str,
1973 strlen("Subject: "));
1974 fprintf(fp, "Subject: %s\n", buf);
1979 if (compose->account->gen_msgid) {
1980 compose_generate_msgid(compose, buf, sizeof(buf));
1981 fprintf(fp, "Message-Id: <%s>\n", buf);
1982 compose->msgid = g_strdup(buf);
1986 if (compose->inreplyto && compose->to_list)
1987 fprintf(fp, "In-Reply-To: <%s>\n", compose->inreplyto);
1990 if (compose->references)
1991 fprintf(fp, "References: %s\n", compose->references);
1994 if (compose->use_followupto) {
1995 str = gtk_entry_get_text(GTK_ENTRY(compose->followup_entry));
1997 Xstrdup_a(str, str, return -1);
2001 compose_convert_header(buf, sizeof(buf), str,
2002 strlen("Followup-To: "));
2003 fprintf(fp, "Followup-To: %s\n", buf);
2009 if (compose->use_replyto) {
2010 str = gtk_entry_get_text(GTK_ENTRY(compose->reply_entry));
2012 Xstrdup_a(str, str, return -1);
2015 compose_convert_header(buf, sizeof(buf), str,
2016 strlen("Reply-To: "));
2017 fprintf(fp, "Reply-To: %s\n", buf);
2022 /* Program version and system info */
2023 /* uname(&utsbuf); */
2024 str = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
2026 fprintf(fp, "X-Mailer: %s (GTK+ %d.%d.%d; %s)\n",
2028 gtk_major_version, gtk_minor_version, gtk_micro_version,
2030 /* utsbuf.sysname, utsbuf.release, utsbuf.machine); */
2032 str = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
2034 fprintf(fp, "X-Newsreader: %s (GTK+ %d.%d.%d; %s)\n",
2036 gtk_major_version, gtk_minor_version, gtk_micro_version,
2038 /* utsbuf.sysname, utsbuf.release, utsbuf.machine); */
2042 if (compose->account->organization) {
2043 compose_convert_header(buf, sizeof(buf),
2044 compose->account->organization,
2045 strlen("Organization: "));
2046 fprintf(fp, "Organization: %s\n", buf);
2050 fprintf(fp, "Mime-Version: 1.0\n");
2051 if (compose->use_attach) {
2052 get_rfc822_date(buf, sizeof(buf));
2053 subst_char(buf, ' ', '_');
2054 subst_char(buf, ',', '_');
2055 compose->boundary = g_strdup_printf("Multipart_%s_%08x",
2056 buf, (guint)compose);
2058 "Content-Type: multipart/mixed;\n"
2059 " boundary=\"%s\"\n", compose->boundary);
2061 fprintf(fp, "Content-Type: text/plain; charset=%s\n", charset);
2062 fprintf(fp, "Content-Transfer-Encoding: %s\n",
2063 procmime_get_encoding_str(encoding));
2066 /* separator between header and body */
2072 static void compose_convert_header(gchar *dest, gint len, gchar *src,
2075 g_return_if_fail(src != NULL);
2076 g_return_if_fail(dest != NULL);
2078 if (len < 1) return;
2082 if (is_ascii_str(src)) {
2083 strncpy2(dest, src, len);
2084 dest[len - 1] = '\0';
2087 conv_encode_header(dest, len, src, header_len);
2090 static void compose_generate_msgid(Compose *compose, gchar *buf, gint len)
2099 if (compose->account && compose->account->address &&
2100 *compose->account->address) {
2101 if (strchr(compose->account->address, '@'))
2102 addr = g_strdup(compose->account->address);
2104 addr = g_strconcat(compose->account->address, "@",
2105 get_domain_name(), NULL);
2107 addr = g_strconcat(g_get_user_name(), "@", get_domain_name(),
2110 g_snprintf(buf, len, "%04d%02d%02d%02d%02d%02d.%08x.%s",
2111 lt->tm_year + 1900, lt->tm_mon + 1,
2112 lt->tm_mday, lt->tm_hour,
2113 lt->tm_min, lt->tm_sec,
2114 (guint)random(), addr);
2116 debug_print(_("generated Message-ID: %s\n"), buf);
2121 static void compose_add_entry_field(GtkWidget *table, GtkWidget **hbox,
2122 GtkWidget **entry, gint *count,
2123 const gchar *label_str,
2124 gboolean is_addr_entry)
2128 if (GTK_TABLE(table)->nrows < (*count) + 1)
2129 gtk_table_resize(GTK_TABLE(table), (*count) + 1, 2);
2131 *hbox = gtk_hbox_new(FALSE, 0);
2132 label = gtk_label_new
2133 (prefs_common.trans_hdr ? gettext(label_str) : label_str);
2134 gtk_box_pack_end(GTK_BOX(*hbox), label, FALSE, FALSE, 0);
2135 gtk_table_attach(GTK_TABLE(table), *hbox, 0, 1, *count, (*count) + 1,
2137 *entry = gtk_entry_new();
2138 gtk_table_attach_defaults
2139 (GTK_TABLE(table), *entry, 1, 2, *count, (*count) + 1);
2140 if (GTK_TABLE(table)->nrows > (*count) + 1)
2141 gtk_table_set_row_spacing(GTK_TABLE(table), *count, 4);
2144 address_completion_register_entry(GTK_ENTRY(*entry));
2149 static Compose *compose_create(PrefsAccount *account)
2155 GtkWidget *handlebox;
2159 GtkWidget *table_vbox;
2161 GtkWidget *from_optmenu_hbox;
2162 GtkWidget *to_entry;
2164 GtkWidget *newsgroups_entry;
2165 GtkWidget *newsgroups_hbox;
2166 GtkWidget *subject_entry;
2167 GtkWidget *cc_entry;
2169 GtkWidget *bcc_entry;
2170 GtkWidget *bcc_hbox;
2171 GtkWidget *reply_entry;
2172 GtkWidget *reply_hbox;
2173 GtkWidget *followup_entry;
2174 GtkWidget *followup_hbox;
2178 GtkWidget *attach_scrwin;
2179 GtkWidget *attach_clist;
2181 GtkWidget *edit_vbox;
2182 GtkWidget *ruler_hbox;
2184 GtkWidget *scrolledwin;
2190 gchar *titles[] = {_("MIME type"), _("Size"), _("Name")};
2191 guint n_menu_entries;
2192 GtkStyle *style, *new_style;
2195 gboolean success[1];
2197 GtkWidget *popupmenu;
2198 GtkWidget *menuitem;
2199 GtkItemFactory *popupfactory;
2200 GtkItemFactory *ifactory;
2205 g_return_val_if_fail(account != NULL, NULL);
2207 debug_print(_("Creating compose window...\n"));
2208 compose = g_new0(Compose, 1);
2210 compose->account = account;
2211 compose->orig_account = account;
2213 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2214 gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
2215 gtk_widget_set_usize(window, -1, prefs_common.compose_height);
2216 gtk_signal_connect(GTK_OBJECT(window), "delete_event",
2217 GTK_SIGNAL_FUNC(compose_delete_cb), compose);
2218 gtk_signal_connect(GTK_OBJECT(window), "destroy",
2219 GTK_SIGNAL_FUNC(compose_destroy_cb), compose);
2220 gtk_signal_connect(GTK_OBJECT(window), "focus_in_event",
2221 GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
2222 gtk_signal_connect(GTK_OBJECT(window), "focus_out_event",
2223 GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
2224 gtk_widget_realize(window);
2226 vbox = gtk_vbox_new(FALSE, 0);
2227 gtk_container_add(GTK_CONTAINER(window), vbox);
2229 n_menu_entries = sizeof(compose_entries) / sizeof(compose_entries[0]);
2230 menubar = menubar_create(window, compose_entries,
2231 n_menu_entries, "<Compose>", compose);
2232 gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
2234 handlebox = gtk_handle_box_new();
2235 gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
2237 compose_toolbar_create(compose, handlebox);
2239 vbox2 = gtk_vbox_new(FALSE, 2);
2240 gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
2241 gtk_container_set_border_width(GTK_CONTAINER(vbox2), BORDER_WIDTH);
2243 table_vbox = gtk_vbox_new(FALSE, 0);
2244 gtk_box_pack_start(GTK_BOX(vbox2), table_vbox, FALSE, TRUE, 0);
2245 gtk_container_set_border_width(GTK_CONTAINER(table_vbox),
2248 table = gtk_table_new(8, 2, FALSE);
2249 gtk_box_pack_start(GTK_BOX(table_vbox), table, FALSE, TRUE, 0);
2251 /* option menu for selecting accounts */
2252 hbox = gtk_hbox_new(FALSE, 0);
2253 label = gtk_label_new(prefs_common.trans_hdr ? _("From:") : "From:");
2254 gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
2255 gtk_table_attach(GTK_TABLE(table), hbox, 0, 1, count, count + 1,
2257 from_optmenu_hbox = compose_account_option_menu_create(compose);
2258 gtk_table_attach_defaults(GTK_TABLE(table), from_optmenu_hbox,
2259 1, 2, count, count + 1);
2260 gtk_table_set_row_spacing(GTK_TABLE(table), 0, 4);
2263 /* header labels and entries */
2264 compose_add_entry_field(table, &to_hbox, &to_entry, &count,
2266 compose_add_entry_field(table, &newsgroups_hbox, &newsgroups_entry,
2267 &count, "Newsgroups:", FALSE);
2268 compose_add_entry_field(table, &hbox, &subject_entry, &count,
2270 compose_add_entry_field(table, &cc_hbox, &cc_entry, &count,
2272 compose_add_entry_field(table, &bcc_hbox, &bcc_entry, &count,
2274 compose_add_entry_field(table, &reply_hbox, &reply_entry, &count,
2276 compose_add_entry_field(table, &followup_hbox, &followup_entry, &count,
2277 "Followup-To:", FALSE);
2279 gtk_table_set_col_spacings(GTK_TABLE(table), 4);
2281 gtk_signal_connect(GTK_OBJECT(to_entry), "activate",
2282 GTK_SIGNAL_FUNC(to_activated), compose);
2283 gtk_signal_connect(GTK_OBJECT(newsgroups_entry), "activate",
2284 GTK_SIGNAL_FUNC(newsgroups_activated), compose);
2285 gtk_signal_connect(GTK_OBJECT(subject_entry), "activate",
2286 GTK_SIGNAL_FUNC(subject_activated), compose);
2287 gtk_signal_connect(GTK_OBJECT(cc_entry), "activate",
2288 GTK_SIGNAL_FUNC(cc_activated), compose);
2289 gtk_signal_connect(GTK_OBJECT(bcc_entry), "activate",
2290 GTK_SIGNAL_FUNC(bcc_activated), compose);
2291 gtk_signal_connect(GTK_OBJECT(reply_entry), "activate",
2292 GTK_SIGNAL_FUNC(replyto_activated), compose);
2293 gtk_signal_connect(GTK_OBJECT(followup_entry), "activate",
2294 GTK_SIGNAL_FUNC(followupto_activated), compose);
2296 gtk_signal_connect(GTK_OBJECT(subject_entry), "grab_focus",
2297 GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2298 gtk_signal_connect(GTK_OBJECT(to_entry), "grab_focus",
2299 GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2300 gtk_signal_connect(GTK_OBJECT(newsgroups_entry), "grab_focus",
2301 GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2302 gtk_signal_connect(GTK_OBJECT(cc_entry), "grab_focus",
2303 GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2304 gtk_signal_connect(GTK_OBJECT(bcc_entry), "grab_focus",
2305 GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2306 gtk_signal_connect(GTK_OBJECT(reply_entry), "grab_focus",
2307 GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2308 gtk_signal_connect(GTK_OBJECT(followup_entry), "grab_focus",
2309 GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2311 /* attachment list */
2312 attach_scrwin = gtk_scrolled_window_new(NULL, NULL);
2313 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(attach_scrwin),
2314 GTK_POLICY_AUTOMATIC,
2316 gtk_widget_set_usize(attach_scrwin, -1, 80);
2318 attach_clist = gtk_clist_new_with_titles(N_ATTACH_COLS, titles);
2319 gtk_clist_set_column_justification(GTK_CLIST(attach_clist), COL_SIZE,
2321 gtk_clist_set_column_width(GTK_CLIST(attach_clist), COL_MIMETYPE, 240);
2322 gtk_clist_set_column_width(GTK_CLIST(attach_clist), COL_SIZE, 64);
2323 gtk_clist_set_selection_mode(GTK_CLIST(attach_clist),
2324 GTK_SELECTION_EXTENDED);
2325 for (i = 0; i < N_ATTACH_COLS; i++)
2326 GTK_WIDGET_UNSET_FLAGS
2327 (GTK_CLIST(attach_clist)->column[i].button,
2329 gtk_container_add(GTK_CONTAINER(attach_scrwin), attach_clist);
2331 gtk_signal_connect(GTK_OBJECT(attach_clist), "select_row",
2332 GTK_SIGNAL_FUNC(attach_selected), compose);
2333 gtk_signal_connect(GTK_OBJECT(attach_clist), "button_press_event",
2334 GTK_SIGNAL_FUNC(attach_button_pressed), compose);
2335 gtk_signal_connect(GTK_OBJECT(attach_clist), "key_press_event",
2336 GTK_SIGNAL_FUNC(attach_key_pressed), compose);
2339 gtk_drag_dest_set(attach_clist,
2340 GTK_DEST_DEFAULT_ALL, compose_mime_types, 1,
2342 gtk_signal_connect(GTK_OBJECT(attach_clist), "drag_data_received",
2343 GTK_SIGNAL_FUNC(compose_attach_drag_received_cb),
2346 /* pane between attach clist and text */
2347 paned = gtk_vpaned_new();
2348 gtk_paned_add1(GTK_PANED(paned), attach_scrwin);
2349 gtk_widget_ref(paned);
2350 gtk_widget_show_all(paned);
2352 edit_vbox = gtk_vbox_new(FALSE, 0);
2353 gtk_box_pack_start(GTK_BOX(vbox2), edit_vbox, TRUE, TRUE, 0);
2356 ruler_hbox = gtk_hbox_new(FALSE, 0);
2357 gtk_box_pack_start(GTK_BOX(edit_vbox), ruler_hbox, FALSE, FALSE, 0);
2359 ruler = gtk_shruler_new();
2360 gtk_ruler_set_range(GTK_RULER(ruler), 0.0, 100.0, 1.0, 100.0);
2361 gtk_box_pack_start(GTK_BOX(ruler_hbox), ruler, TRUE, TRUE,
2363 gtk_widget_set_usize(ruler_hbox, 1, -1);
2366 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
2367 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
2368 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
2369 gtk_box_pack_start(GTK_BOX(edit_vbox), scrolledwin, TRUE, TRUE, 0);
2370 gtk_widget_set_usize(scrolledwin, prefs_common.compose_width, -1);
2372 text = gtk_stext_new(gtk_scrolled_window_get_hadjustment
2373 (GTK_SCROLLED_WINDOW(scrolledwin)),
2374 gtk_scrolled_window_get_vadjustment
2375 (GTK_SCROLLED_WINDOW(scrolledwin)));
2376 gtk_stext_set_editable(GTK_STEXT(text), TRUE);
2377 gtk_stext_set_word_wrap(GTK_STEXT(text), TRUE);
2378 gtk_stext_set_wrap_rmargin(GTK_STEXT(text), prefs_common.linewrap_len);
2380 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
2382 gtk_signal_connect(GTK_OBJECT(text), "changed",
2383 GTK_SIGNAL_FUNC(compose_changed_cb), compose);
2384 gtk_signal_connect(GTK_OBJECT(text), "grab_focus",
2385 GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
2386 gtk_signal_connect_after(GTK_OBJECT(text), "button_press_event",
2387 GTK_SIGNAL_FUNC(compose_button_press_cb),
2390 gtk_signal_connect_after(GTK_OBJECT(text), "key_press_event",
2391 GTK_SIGNAL_FUNC(compose_key_press_cb),
2394 gtk_signal_connect_after(GTK_OBJECT(text), "size_allocate",
2395 GTK_SIGNAL_FUNC(compose_edit_size_alloc),
2399 gtk_drag_dest_set(text, GTK_DEST_DEFAULT_ALL, compose_mime_types, 1,
2401 gtk_signal_connect(GTK_OBJECT(text), "drag_data_received",
2402 GTK_SIGNAL_FUNC(compose_insert_drag_received_cb),
2405 gtk_widget_show_all(vbox);
2407 style = gtk_widget_get_style(text);
2409 /* workaround for the slow down of GtkSText when using Pixmap theme */
2410 if (style->engine) {
2411 GtkThemeEngine *engine;
2413 engine = style->engine;
2414 style->engine = NULL;
2415 new_style = gtk_style_copy(style);
2416 style->engine = engine;
2418 new_style = gtk_style_copy(style);
2420 if (prefs_common.textfont) {
2423 charset = conv_get_current_charset();
2424 if (MB_CUR_MAX == 1) {
2427 Xstrdup_a(fontstr, prefs_common.textfont, );
2428 if (fontstr && (p = strchr(fontstr, ',')) != NULL)
2430 font = gdk_font_load(fontstr);
2432 font = gdk_fontset_load(prefs_common.textfont);
2434 gdk_font_unref(new_style->font);
2435 new_style->font = font;
2439 gtk_widget_set_style(text, new_style);
2441 color[0] = quote_color;
2442 cmap = gdk_window_get_colormap(window->window);
2443 gdk_colormap_alloc_colors(cmap, color, 1, FALSE, TRUE, success);
2444 if (success[0] == FALSE) {
2445 g_warning("Compose: color allocation failed.\n");
2446 style = gtk_widget_get_style(text);
2447 quote_color = style->black;
2450 n_entries = sizeof(compose_popup_entries) /
2451 sizeof(compose_popup_entries[0]);
2452 popupmenu = menu_create_items(compose_popup_entries, n_entries,
2453 "<Compose>", &popupfactory,
2456 ifactory = gtk_item_factory_from_widget(menubar);
2457 menu_set_sensitive(ifactory, "/Edit/Undo", FALSE);
2458 menu_set_sensitive(ifactory, "/Edit/Redo", FALSE);
2460 gtk_widget_hide(bcc_hbox);
2461 gtk_widget_hide(bcc_entry);
2462 gtk_widget_hide(reply_hbox);
2463 gtk_widget_hide(reply_entry);
2464 gtk_widget_hide(followup_hbox);
2465 gtk_widget_hide(followup_entry);
2466 gtk_widget_hide(ruler_hbox);
2467 gtk_table_set_row_spacing(GTK_TABLE(table), 4, 0);
2468 gtk_table_set_row_spacing(GTK_TABLE(table), 5, 0);
2469 gtk_table_set_row_spacing(GTK_TABLE(table), 6, 0);
2471 if (account->protocol == A_NNTP) {
2472 gtk_widget_hide(to_hbox);
2473 gtk_widget_hide(to_entry);
2474 gtk_widget_hide(cc_hbox);
2475 gtk_widget_hide(cc_entry);
2476 gtk_table_set_row_spacing(GTK_TABLE(table), 1, 0);
2477 gtk_table_set_row_spacing(GTK_TABLE(table), 3, 0);
2479 gtk_widget_hide(newsgroups_hbox);
2480 gtk_widget_hide(newsgroups_entry);
2481 gtk_table_set_row_spacing(GTK_TABLE(table), 2, 0);
2482 menu_set_sensitive(ifactory, "/Message/Followup to", FALSE);
2485 switch (prefs_common.toolbar_style) {
2487 gtk_widget_hide(handlebox);
2490 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
2494 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
2498 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
2503 gtk_widget_show(window);
2505 address_completion_start(window);
2507 compose->window = window;
2508 compose->vbox = vbox;
2509 compose->menubar = menubar;
2510 compose->handlebox = handlebox;
2512 compose->vbox2 = vbox2;
2514 compose->table_vbox = table_vbox;
2515 compose->table = table;
2516 compose->to_hbox = to_hbox;
2517 compose->to_entry = to_entry;
2518 compose->newsgroups_hbox = newsgroups_hbox;
2519 compose->newsgroups_entry = newsgroups_entry;
2520 compose->subject_entry = subject_entry;
2521 compose->cc_hbox = cc_hbox;
2522 compose->cc_entry = cc_entry;
2523 compose->bcc_hbox = bcc_hbox;
2524 compose->bcc_entry = bcc_entry;
2525 compose->reply_hbox = reply_hbox;
2526 compose->reply_entry = reply_entry;
2527 compose->followup_hbox = followup_hbox;
2528 compose->followup_entry = followup_entry;
2530 compose->paned = paned;
2532 compose->attach_scrwin = attach_scrwin;
2533 compose->attach_clist = attach_clist;
2535 compose->edit_vbox = edit_vbox;
2536 compose->ruler_hbox = ruler_hbox;
2537 compose->ruler = ruler;
2538 compose->scrolledwin = scrolledwin;
2539 compose->text = text;
2541 compose->focused_editable = NULL;
2543 compose->popupmenu = popupmenu;
2544 compose->popupfactory = popupfactory;
2546 compose->mode = COMPOSE_NEW;
2548 compose->replyto = NULL;
2550 compose->bcc = NULL;
2551 compose->followup_to = NULL;
2552 compose->inreplyto = NULL;
2553 compose->references = NULL;
2554 compose->msgid = NULL;
2555 compose->boundary = NULL;
2557 compose->use_to = FALSE;
2558 compose->use_cc = FALSE;
2559 compose->use_bcc = FALSE;
2560 compose->use_replyto = FALSE;
2561 compose->use_followupto = FALSE;
2562 compose->use_attach = FALSE;
2565 compose->use_signing = FALSE;
2566 compose->use_encryption = FALSE;
2567 #endif /* USE_GPGME */
2569 compose->modified = FALSE;
2571 compose->to_list = NULL;
2572 compose->newsgroup_list = NULL;
2574 compose->exteditor_file = NULL;
2575 compose->exteditor_pid = -1;
2576 compose->exteditor_readdes = -1;
2577 compose->exteditor_tag = -1;
2579 compose_set_title(compose);
2581 if (account->protocol != A_NNTP) {
2582 menuitem = gtk_item_factory_get_item(ifactory, "/Message/To");
2583 gtk_check_menu_item_set_active
2584 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2585 gtk_widget_set_sensitive(menuitem, FALSE);
2586 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Cc");
2587 gtk_check_menu_item_set_active
2588 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2589 gtk_widget_set_sensitive(menuitem, FALSE);
2591 if (account->set_autocc && account->auto_cc) {
2592 gtk_entry_set_text(GTK_ENTRY(cc_entry), account->auto_cc);
2593 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Cc");
2594 gtk_check_menu_item_set_active
2595 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2597 if (account->set_autobcc && account->auto_bcc) {
2598 gtk_entry_set_text(GTK_ENTRY(bcc_entry), account->auto_bcc);
2599 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Bcc");
2600 gtk_check_menu_item_set_active
2601 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2603 if (account->set_autoreplyto && account->auto_replyto) {
2604 gtk_entry_set_text(GTK_ENTRY(reply_entry),
2605 account->auto_replyto);
2606 menuitem = gtk_item_factory_get_item(ifactory,
2607 "/Message/Reply to");
2608 gtk_check_menu_item_set_active
2609 (GTK_CHECK_MENU_ITEM(menuitem), TRUE);
2612 menuitem = gtk_item_factory_get_item(ifactory, "/Tool/Show ruler");
2613 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2614 prefs_common.show_ruler);
2617 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Sign");
2618 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2619 prefs_common.default_sign);
2620 menuitem = gtk_item_factory_get_item(ifactory, "/Message/Encrypt");
2621 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
2622 prefs_common.default_encrypt);
2623 #endif /* USE_GPGME */
2625 addressbook_set_target_compose(compose);
2627 compose_list = g_list_append(compose_list, compose);
2632 #include "pixmaps/stock_mail_send.xpm"
2633 #include "pixmaps/stock_mail.xpm"
2634 #include "pixmaps/stock_paste.xpm"
2635 #include "pixmaps/stock_mail_attach.xpm"
2636 #include "pixmaps/stock_mail_compose.xpm"
2637 #include "pixmaps/linewrap.xpm"
2638 //#include "pixmaps/tb_mail_queue_send.xpm"
2639 #include "pixmaps/tb_address_book.xpm"
2641 #define CREATE_TOOLBAR_ICON(xpm_d) \
2643 icon = gdk_pixmap_create_from_xpm_d(container->window, &mask, \
2644 &container->style->white, \
2646 icon_wid = gtk_pixmap_new(icon, mask); \
2649 static void compose_toolbar_create(Compose *compose, GtkWidget *container)
2654 GtkWidget *icon_wid;
2655 GtkWidget *send_btn;
2656 GtkWidget *sendl_btn;
2657 GtkWidget *draft_btn;
2658 GtkWidget *insert_btn;
2659 GtkWidget *attach_btn;
2661 GtkWidget *exteditor_btn;
2662 GtkWidget *linewrap_btn;
2663 GtkWidget *addrbook_btn;
2665 toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
2667 gtk_container_add(GTK_CONTAINER(container), toolbar);
2668 gtk_container_set_border_width(GTK_CONTAINER(container), 2);
2669 gtk_toolbar_set_button_relief(GTK_TOOLBAR(toolbar), GTK_RELIEF_NONE);
2670 gtk_toolbar_set_space_style(GTK_TOOLBAR(toolbar),
2671 GTK_TOOLBAR_SPACE_LINE);
2673 CREATE_TOOLBAR_ICON(stock_mail_send_xpm);
2674 send_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
2678 icon_wid, toolbar_send_cb, compose);
2680 CREATE_TOOLBAR_ICON(stock_mail_send_xpm);
2681 //CREATE_TOOLBAR_ICON(tb_mail_queue_send_xpm);
2682 sendl_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
2684 _("Put into queue folder and send later"),
2686 icon_wid, toolbar_send_later_cb,
2689 CREATE_TOOLBAR_ICON(stock_mail_xpm);
2690 draft_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
2692 _("Save to draft folder"),
2694 icon_wid, toolbar_draft_cb,
2697 gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
2699 CREATE_TOOLBAR_ICON(stock_paste_xpm);
2700 insert_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
2704 icon_wid, toolbar_insert_cb,
2707 CREATE_TOOLBAR_ICON(stock_mail_attach_xpm);
2708 attach_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
2712 icon_wid, toolbar_attach_cb,
2715 gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
2717 CREATE_TOOLBAR_ICON(stock_mail_xpm);
2718 sig_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
2720 _("Insert signature"),
2722 icon_wid, toolbar_sig_cb, compose);
2724 gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
2726 CREATE_TOOLBAR_ICON(stock_mail_compose_xpm);
2727 exteditor_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
2729 _("Edit with external editor"),
2732 toolbar_ext_editor_cb,
2735 CREATE_TOOLBAR_ICON(linewrap_xpm);
2736 linewrap_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
2738 _("Wrap long lines"),
2741 toolbar_linewrap_cb,
2744 gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
2746 CREATE_TOOLBAR_ICON(tb_address_book_xpm);
2747 addrbook_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
2751 icon_wid, toolbar_address_cb,
2754 compose->toolbar = toolbar;
2755 compose->send_btn = send_btn;
2756 compose->sendl_btn = sendl_btn;
2757 compose->draft_btn = draft_btn;
2758 compose->insert_btn = insert_btn;
2759 compose->attach_btn = attach_btn;
2760 compose->sig_btn = sig_btn;
2761 compose->exteditor_btn = exteditor_btn;
2762 compose->linewrap_btn = linewrap_btn;
2763 compose->addrbook_btn = addrbook_btn;
2765 gtk_widget_show_all(toolbar);
2768 static GtkWidget *compose_account_option_menu_create(Compose *compose)
2774 gint num = 0, def_menu = 0;
2776 accounts = account_get_list();
2777 g_return_val_if_fail(accounts != NULL, NULL);
2779 hbox = gtk_hbox_new(FALSE, 0);
2780 optmenu = gtk_option_menu_new();
2781 gtk_box_pack_start(GTK_BOX(hbox), optmenu, FALSE, FALSE, 0);
2782 menu = gtk_menu_new();
2784 for (; accounts != NULL; accounts = accounts->next, num++) {
2785 PrefsAccount *ac = (PrefsAccount *)accounts->data;
2786 GtkWidget *menuitem;
2789 if (ac == compose->account) def_menu = num;
2791 name = g_strdup_printf("%s <%s> (%s)",
2792 ac->name, ac->address, ac->account_name);
2793 MENUITEM_ADD(menu, menuitem, name, ac);
2795 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
2796 GTK_SIGNAL_FUNC(account_activated),
2800 gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), menu);
2801 gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), def_menu);
2806 static void compose_destroy(Compose *compose)
2809 GtkCList *clist = GTK_CLIST(compose->attach_clist);
2812 /* NOTE: address_completion_end() does nothing with the window
2813 * however this may change. */
2814 address_completion_end(compose->window);
2816 slist_free_strings(compose->to_list);
2817 g_slist_free(compose->to_list);
2818 slist_free_strings(compose->newsgroup_list);
2819 g_slist_free(compose->newsgroup_list);
2821 procmsg_msginfo_free(compose->targetinfo);
2823 g_free(compose->replyto);
2824 g_free(compose->cc);
2825 g_free(compose->bcc);
2826 g_free(compose->newsgroups);
2827 g_free(compose->followup_to);
2829 g_free(compose->inreplyto);
2830 g_free(compose->references);
2831 g_free(compose->msgid);
2832 g_free(compose->boundary);
2834 g_free(compose->exteditor_file);
2836 for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
2838 compose_attach_info_free(ainfo);
2840 if (addressbook_get_target_compose() == compose)
2841 addressbook_set_target_compose(NULL);
2843 prefs_common.compose_width = compose->scrolledwin->allocation.width;
2844 prefs_common.compose_height = compose->window->allocation.height;
2846 gtk_widget_destroy(compose->paned);
2850 compose_list = g_list_remove(compose_list, compose);
2853 static void compose_attach_info_free(AttachInfo *ainfo)
2855 g_free(ainfo->file);
2856 g_free(ainfo->content_type);
2857 g_free(ainfo->name);
2861 static void compose_attach_remove_selected(Compose *compose)
2863 GtkCList *clist = GTK_CLIST(compose->attach_clist);
2867 while (clist->selection != NULL) {
2868 row = GPOINTER_TO_INT(clist->selection->data);
2869 ainfo = gtk_clist_get_row_data(clist, row);
2870 compose_attach_info_free(ainfo);
2871 gtk_clist_remove(clist, row);
2875 static struct _AttachProperty
2878 GtkWidget *mimetype_entry;
2879 GtkWidget *encoding_optmenu;
2880 GtkWidget *path_entry;
2881 GtkWidget *filename_entry;
2883 GtkWidget *cancel_btn;
2886 static void compose_attach_property(Compose *compose)
2888 GtkCList *clist = GTK_CLIST(compose->attach_clist);
2892 GtkOptionMenu *optmenu;
2894 if (!clist->selection) return;
2895 row = GPOINTER_TO_INT(clist->selection->data);
2897 ainfo = gtk_clist_get_row_data(clist, row);
2900 if (!attach_prop.window)
2901 compose_attach_property_create(&cancelled);
2902 gtk_widget_grab_focus(attach_prop.ok_btn);
2903 gtk_widget_show(attach_prop.window);
2904 manage_window_set_transient(GTK_WINDOW(attach_prop.window));
2906 optmenu = GTK_OPTION_MENU(attach_prop.encoding_optmenu);
2907 if (ainfo->encoding == ENC_UNKNOWN)
2908 gtk_option_menu_set_history(optmenu, ENC_BASE64);
2910 gtk_option_menu_set_history(optmenu, ainfo->encoding);
2912 gtk_entry_set_text(GTK_ENTRY(attach_prop.mimetype_entry),
2913 ainfo->content_type ? ainfo->content_type : "");
2914 gtk_entry_set_text(GTK_ENTRY(attach_prop.path_entry),
2915 ainfo->file ? ainfo->file : "");
2916 gtk_entry_set_text(GTK_ENTRY(attach_prop.filename_entry),
2917 ainfo->name ? ainfo->name : "");
2921 gchar *cnttype = NULL;
2925 GtkWidget *menuitem;
2929 if (cancelled == TRUE) {
2930 gtk_widget_hide(attach_prop.window);
2934 text = gtk_entry_get_text(GTK_ENTRY(attach_prop.mimetype_entry));
2935 if (*text != '\0') {
2938 text = g_strstrip(g_strdup(text));
2939 if ((p = strchr(text, '/')) && !strchr(p + 1, '/')) {
2940 cnttype = g_strdup(text);
2943 alertpanel_error(_("Invalid MIME type."));
2949 menu = gtk_option_menu_get_menu(optmenu);
2950 menuitem = gtk_menu_get_active(GTK_MENU(menu));
2951 ainfo->encoding = GPOINTER_TO_INT
2952 (gtk_object_get_user_data(GTK_OBJECT(menuitem)));
2954 text = gtk_entry_get_text(GTK_ENTRY(attach_prop.path_entry));
2955 if (*text != '\0') {
2956 if (is_file_exist(text) &&
2957 (size = get_file_size(text)) > 0)
2958 file = g_strdup(text);
2961 (_("File doesn't exist or is empty."));
2967 text = gtk_entry_get_text(GTK_ENTRY(attach_prop.filename_entry));
2968 if (*text != '\0') {
2969 g_free(ainfo->name);
2970 ainfo->name = g_strdup(text);
2974 g_free(ainfo->content_type);
2975 ainfo->content_type = cnttype;
2978 g_free(ainfo->file);
2984 gtk_clist_set_text(clist, row, COL_MIMETYPE,
2985 ainfo->content_type);
2986 gtk_clist_set_text(clist, row, COL_SIZE,
2987 to_human_readable(ainfo->size));
2988 gtk_clist_set_text(clist, row, COL_NAME, ainfo->name);
2990 gtk_widget_hide(attach_prop.window);
2995 #define SET_LABEL_AND_ENTRY(str, entry, top) \
2997 label = gtk_label_new(str); \
2998 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), \
2999 GTK_FILL, 0, 0, 0); \
3000 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); \
3002 entry = gtk_entry_new(); \
3003 gtk_table_attach(GTK_TABLE(table), entry, 1, 2, top, (top + 1), \
3004 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); \
3007 static void compose_attach_property_create(gboolean *cancelled)
3013 GtkWidget *mimetype_entry;
3016 GtkWidget *optmenu_menu;
3017 GtkWidget *menuitem;
3018 GtkWidget *path_entry;
3019 GtkWidget *filename_entry;
3022 GtkWidget *cancel_btn;
3024 debug_print("Creating attach_property window...\n");
3026 window = gtk_window_new(GTK_WINDOW_DIALOG);
3027 gtk_widget_set_usize(window, 480, -1);
3028 gtk_container_set_border_width(GTK_CONTAINER(window), 8);
3029 gtk_window_set_title(GTK_WINDOW(window), _("Property"));
3030 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
3031 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
3032 gtk_signal_connect(GTK_OBJECT(window), "delete_event",
3033 GTK_SIGNAL_FUNC(attach_property_delete_event),
3035 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
3036 GTK_SIGNAL_FUNC(attach_property_key_pressed),
3039 vbox = gtk_vbox_new(FALSE, 8);
3040 gtk_container_add(GTK_CONTAINER(window), vbox);
3042 table = gtk_table_new(4, 2, FALSE);
3043 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
3044 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
3045 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
3047 SET_LABEL_AND_ENTRY(_("MIME type"), mimetype_entry, 0);
3049 label = gtk_label_new(_("Encoding"));
3050 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
3052 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
3054 hbox = gtk_hbox_new(FALSE, 0);
3055 gtk_table_attach(GTK_TABLE(table), hbox, 1, 2, 1, 2,
3056 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
3058 optmenu = gtk_option_menu_new();
3059 gtk_box_pack_start(GTK_BOX(hbox), optmenu, FALSE, FALSE, 0);
3061 optmenu_menu = gtk_menu_new();
3062 MENUITEM_ADD(optmenu_menu, menuitem, "7bit", ENC_7BIT);
3063 gtk_widget_set_sensitive(menuitem, FALSE);
3064 MENUITEM_ADD(optmenu_menu, menuitem, "8bit", ENC_8BIT);
3065 gtk_widget_set_sensitive(menuitem, FALSE);
3066 MENUITEM_ADD(optmenu_menu, menuitem, "quoted-printable", ENC_QUOTED_PRINTABLE);
3067 gtk_widget_set_sensitive(menuitem, FALSE);
3068 MENUITEM_ADD(optmenu_menu, menuitem, "base64", ENC_BASE64);
3070 gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), optmenu_menu);
3072 SET_LABEL_AND_ENTRY(_("Path"), path_entry, 2);
3073 SET_LABEL_AND_ENTRY(_("File name"), filename_entry, 3);
3075 gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
3076 &cancel_btn, _("Cancel"), NULL, NULL);
3077 gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
3078 gtk_widget_grab_default(ok_btn);
3080 gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
3081 GTK_SIGNAL_FUNC(attach_property_ok),
3083 gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
3084 GTK_SIGNAL_FUNC(attach_property_cancel),
3087 gtk_widget_show_all(vbox);
3089 attach_prop.window = window;
3090 attach_prop.mimetype_entry = mimetype_entry;
3091 attach_prop.encoding_optmenu = optmenu;
3092 attach_prop.path_entry = path_entry;
3093 attach_prop.filename_entry = filename_entry;
3094 attach_prop.ok_btn = ok_btn;
3095 attach_prop.cancel_btn = cancel_btn;
3098 static void attach_property_ok(GtkWidget *widget, gboolean *cancelled)
3104 static void attach_property_cancel(GtkWidget *widget, gboolean *cancelled)
3110 static gint attach_property_delete_event(GtkWidget *widget, GdkEventAny *event,
3111 gboolean *cancelled)
3119 static void attach_property_key_pressed(GtkWidget *widget, GdkEventKey *event,
3120 gboolean *cancelled)
3122 if (event && event->keyval == GDK_Escape) {
3128 static void compose_exec_ext_editor(Compose *compose)
3134 g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.%08x",
3135 g_get_tmp_dir(), G_DIR_SEPARATOR, (gint)compose);
3137 if (pipe(pipe_fds) < 0) {
3142 if ((pid = fork()) < 0) {
3148 /* close the write side of the pipe */
3151 compose->exteditor_file = g_strdup(tmp);
3152 compose->exteditor_pid = pid;
3153 compose->exteditor_readdes = pipe_fds[0];
3155 compose_set_ext_editor_sensitive(compose, FALSE);
3157 compose->exteditor_tag =
3158 gdk_input_add(pipe_fds[0], GDK_INPUT_READ,
3159 compose_input_cb, compose);
3160 } else { /* process-monitoring process */
3166 /* close the read side of the pipe */
3169 if (compose_write_body_to_file(compose, tmp) < 0) {
3170 sock_write(pipe_fds[1], "2\n", 2);
3174 pid_ed = compose_exec_ext_editor_real(tmp);
3176 sock_write(pipe_fds[1], "1\n", 2);
3180 /* wait until editor is terminated */
3181 waitpid(pid_ed, NULL, 0);
3183 sock_write(pipe_fds[1], "0\n", 2);
3190 static gint compose_exec_ext_editor_real(const gchar *file)
3192 static gchar *def_cmd = "emacs %s";
3198 g_return_val_if_fail(file != NULL, -1);
3200 if ((pid = fork()) < 0) {
3205 if (pid != 0) return pid;
3207 /* grandchild process */
3209 if (setpgid(0, getppid()))
3212 if (prefs_common.ext_editor_cmd &&
3213 (p = strchr(prefs_common.ext_editor_cmd, '%')) &&
3214 *(p + 1) == 's' && !strchr(p + 2, '%')) {
3215 g_snprintf(buf, sizeof(buf), prefs_common.ext_editor_cmd, file);
3217 if (prefs_common.ext_editor_cmd)
3218 g_warning(_("External editor command line is invalid: `%s'\n"),
3219 prefs_common.ext_editor_cmd);
3220 g_snprintf(buf, sizeof(buf), def_cmd, file);
3223 cmdline = g_strsplit(buf, " ", 1024);
3224 execvp(cmdline[0], cmdline);
3227 g_strfreev(cmdline);
3232 static gboolean compose_ext_editor_kill(Compose *compose)
3234 pid_t pgid = compose->exteditor_pid * -1;
3237 ret = kill(pgid, 0);
3239 if (ret == 0 || (ret == -1 && EPERM == errno)) {
3243 msg = g_strdup_printf
3244 (_("The external editor is still working.\n"
3245 "Force terminating the process?\n"
3246 "process group id: %d"), -pgid);
3247 val = alertpanel(_("Notice"), msg, _("Yes"), _("+No"), NULL);
3250 if (val == G_ALERTDEFAULT) {
3251 gdk_input_remove(compose->exteditor_tag);
3252 close(compose->exteditor_readdes);
3254 if (kill(pgid, SIGTERM) < 0) perror("kill");
3255 waitpid(compose->exteditor_pid, NULL, 0);
3257 g_warning(_("Terminated process group id: %d"), -pgid);
3258 g_warning(_("Temporary file: %s"),
3259 compose->exteditor_file);
3261 compose_set_ext_editor_sensitive(compose, TRUE);
3263 g_free(compose->exteditor_file);
3264 compose->exteditor_file = NULL;
3265 compose->exteditor_pid = -1;
3266 compose->exteditor_readdes = -1;
3267 compose->exteditor_tag = -1;
3275 static void compose_input_cb(gpointer data, gint source,
3276 GdkInputCondition condition)
3279 Compose *compose = (Compose *)data;
3282 debug_print(_("Compose: input from monitoring process\n"));
3284 gdk_input_remove(compose->exteditor_tag);
3287 if (read(source, &buf[i], 1) < 1) {
3291 if (buf[i] == '\n') {
3296 if (i == sizeof(buf) - 1)
3300 waitpid(compose->exteditor_pid, NULL, 0);
3302 if (buf[0] == '0') { /* success */
3303 GtkSText *text = GTK_STEXT(compose->text);
3305 gtk_stext_freeze(text);
3306 gtk_stext_set_point(text, 0);
3307 gtk_stext_forward_delete(text, gtk_stext_get_length(text));
3308 compose_insert_file(compose, compose->exteditor_file);
3309 compose_changed_cb(NULL, compose);
3310 gtk_stext_thaw(text);
3312 if (unlink(compose->exteditor_file) < 0)
3313 FILE_OP_ERROR(compose->exteditor_file, "unlink");
3314 } else if (buf[0] == '1') { /* failed */
3315 g_warning(_("Couldn't exec external editor\n"));
3316 if (unlink(compose->exteditor_file) < 0)
3317 FILE_OP_ERROR(compose->exteditor_file, "unlink");
3318 } else if (buf[0] == '2') {
3319 g_warning(_("Couldn't write to file\n"));
3320 } else if (buf[0] == '3') {
3321 g_warning(_("Pipe read failed\n"));
3326 compose_set_ext_editor_sensitive(compose, TRUE);
3328 g_free(compose->exteditor_file);
3329 compose->exteditor_file = NULL;
3330 compose->exteditor_pid = -1;
3331 compose->exteditor_readdes = -1;
3332 compose->exteditor_tag = -1;
3335 static void compose_set_ext_editor_sensitive(Compose *compose,
3338 GtkItemFactory *ifactory;
3340 ifactory = gtk_item_factory_from_widget(compose->menubar);
3342 menu_set_sensitive(ifactory, "/Message/Send", sensitive);
3343 menu_set_sensitive(ifactory, "/Message/Send later", sensitive);
3344 menu_set_sensitive(ifactory, "/Message/Save to draft folder",
3346 menu_set_sensitive(ifactory, "/File/Insert file", sensitive);
3347 menu_set_sensitive(ifactory, "/File/Insert signature", sensitive);
3348 menu_set_sensitive(ifactory, "/Edit/Wrap long lines", sensitive);
3349 menu_set_sensitive(ifactory, "/Edit/Edit with external editor",
3352 gtk_widget_set_sensitive(compose->text, sensitive);
3353 gtk_widget_set_sensitive(compose->send_btn, sensitive);
3354 gtk_widget_set_sensitive(compose->sendl_btn, sensitive);
3355 gtk_widget_set_sensitive(compose->draft_btn, sensitive);
3356 gtk_widget_set_sensitive(compose->insert_btn, sensitive);
3357 gtk_widget_set_sensitive(compose->sig_btn, sensitive);
3358 gtk_widget_set_sensitive(compose->exteditor_btn, sensitive);
3359 gtk_widget_set_sensitive(compose->linewrap_btn, sensitive);
3362 static gint calc_cursor_xpos(GtkSText *text, gint extra, gint char_width)
3366 cursor_pos = (text->cursor_pos_x - extra) / char_width;
3367 cursor_pos = MAX(cursor_pos, 0);
3372 /* callback functions */
3374 /* compose_edit_size_alloc() - called when resized. don't know whether Gtk
3375 * includes "non-client" (windows-izm) in calculation, so this calculation
3376 * may not be accurate.
3378 static gboolean compose_edit_size_alloc(GtkEditable *widget,
3379 GtkAllocation *allocation,
3380 GtkSHRuler *shruler)
3382 if (prefs_common.show_ruler) {
3384 gint line_width_in_chars;
3386 char_width = gtkut_get_font_width
3387 (GTK_WIDGET(widget)->style->font);
3388 line_width_in_chars =
3389 (allocation->width - allocation->x) / char_width;
3391 /* got the maximum */
3392 gtk_ruler_set_range(GTK_RULER(shruler),
3393 0.0, line_width_in_chars,
3394 calc_cursor_xpos(GTK_STEXT(widget),
3397 /*line_width_in_chars*/ char_width);
3403 static void toolbar_send_cb(GtkWidget *widget, gpointer data)
3405 compose_send_cb(data, 0, NULL);
3408 static void toolbar_send_later_cb(GtkWidget *widget, gpointer data)
3410 compose_send_later_cb(data, 0, NULL);
3413 static void toolbar_draft_cb(GtkWidget *widget, gpointer data)
3415 compose_draft_cb(data, 0, NULL);
3418 static void toolbar_insert_cb(GtkWidget *widget, gpointer data)
3420 compose_insert_file_cb(data, 0, NULL);
3423 static void toolbar_attach_cb(GtkWidget *widget, gpointer data)
3425 compose_attach_cb(data, 0, NULL);
3428 static void toolbar_sig_cb(GtkWidget *widget, gpointer data)
3430 Compose *compose = (Compose *)data;
3432 compose_insert_sig(compose);
3435 static void toolbar_ext_editor_cb(GtkWidget *widget, gpointer data)
3437 Compose *compose = (Compose *)data;
3439 compose_exec_ext_editor(compose);
3442 static void toolbar_linewrap_cb(GtkWidget *widget, gpointer data)
3444 Compose *compose = (Compose *)data;
3446 compose_wrap_line(compose);
3449 static void toolbar_address_cb(GtkWidget *widget, gpointer data)
3451 compose_address_cb(data, 0, NULL);
3454 static void account_activated(GtkMenuItem *menuitem, gpointer data)
3456 Compose *compose = (Compose *)data;
3460 ac = (PrefsAccount *)gtk_object_get_user_data(GTK_OBJECT(menuitem));
3461 g_return_if_fail(ac != NULL);
3463 if (ac != compose->account) {
3464 compose->account = ac;
3465 compose_set_title(compose);
3469 static void attach_selected(GtkCList *clist, gint row, gint column,
3470 GdkEvent *event, gpointer data)
3472 Compose *compose = (Compose *)data;
3474 if (event && event->type == GDK_2BUTTON_PRESS)
3475 compose_attach_property(compose);
3478 static void attach_button_pressed(GtkWidget *widget, GdkEventButton *event,
3481 Compose *compose = (Compose *)data;
3482 GtkCList *clist = GTK_CLIST(compose->attach_clist);
3487 if (event->button == 3) {
3488 if ((clist->selection && !clist->selection->next) ||
3489 !clist->selection) {
3490 gtk_clist_unselect_all(clist);
3491 if (gtk_clist_get_selection_info(clist,
3494 gtk_clist_select_row(clist, row, column);
3495 gtkut_clist_set_focus_row(clist, row);
3498 gtk_menu_popup(GTK_MENU(compose->popupmenu), NULL, NULL,
3499 NULL, NULL, event->button, event->time);
3503 static void attach_key_pressed(GtkWidget *widget, GdkEventKey *event,
3506 Compose *compose = (Compose *)data;
3510 switch (event->keyval) {
3512 compose_attach_remove_selected(compose);
3517 static void compose_send_cb(gpointer data, guint action, GtkWidget *widget)
3519 Compose *compose = (Compose *)data;
3522 val = compose_send(compose);
3524 if (val == 0) gtk_widget_destroy(compose->window);
3527 static void compose_send_later_cb(gpointer data, guint action,
3530 Compose *compose = (Compose *)data;
3532 gchar *to, *newsgroups;
3534 to = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
3535 newsgroups = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
3536 if (*to == '\0' && *newsgroups == '\0') {
3537 alertpanel_error(_("Recipient is not specified."));
3541 g_snprintf(tmp, 22, "%s%ctmpmsg%d",
3542 g_get_tmp_dir(), G_DIR_SEPARATOR, (gint)compose);
3544 if (prefs_common.linewrap_at_send)
3545 compose_wrap_line(compose);
3547 if (compose_write_to_file(compose, tmp, FALSE) < 0 ||
3548 compose_queue(compose, tmp) < 0) {
3549 alertpanel_error(_("Can't queue the message."));
3553 if (prefs_common.savemsg) {
3554 if (compose_save_to_outbox(compose, tmp) < 0)
3556 (_("Can't save the message to outbox."));
3559 if (unlink(tmp) < 0)
3560 FILE_OP_ERROR(tmp, "unlink");
3562 gtk_widget_destroy(compose->window);
3565 static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
3567 Compose *compose = (Compose *)data;
3571 draft = folder_get_default_draft();
3572 folder_item_scan(draft);
3574 if (procmsg_msg_exist(compose->targetinfo) &&
3575 compose->targetinfo->folder == draft) {
3576 if (folder_item_remove_msg(draft,
3577 compose->targetinfo->msgnum) < 0)
3578 g_warning(_("can't remove the old draft message\n"));
3580 tmp = g_strdup_printf("%s%cdraft.%d", g_get_tmp_dir(),
3581 G_DIR_SEPARATOR, (gint)compose);
3583 if (compose_write_to_file(compose, tmp, TRUE) < 0) {
3588 if (folder_item_add_msg(draft, tmp) < 0) {
3597 //folderview_scan_folder_a(DRAFT_DIR, TRUE);
3599 gtk_widget_destroy(compose->window);
3602 static void compose_attach_cb(gpointer data, guint action, GtkWidget *widget)
3604 Compose *compose = (Compose *)data;
3607 file = filesel_select_file(_("Select file"), NULL);
3610 compose_attach_append(compose, file, MIME_UNKNOWN);
3613 static void compose_insert_file_cb(gpointer data, guint action,
3616 Compose *compose = (Compose *)data;
3619 file = filesel_select_file(_("Select file"), NULL);
3622 compose_insert_file(compose, file);
3625 static gint compose_delete_cb(GtkWidget *widget, GdkEventAny *event,
3628 compose_close_cb(data, 0, NULL);
3632 static void compose_close_cb(gpointer data, guint action, GtkWidget *widget)
3634 Compose *compose = (Compose *)data;
3637 if (compose->exteditor_tag != -1) {
3638 if (!compose_ext_editor_kill(compose))
3642 if (compose->modified) {
3643 val = alertpanel(_("Discard message"),
3644 _("This message has been modified. discard it?"),
3645 _("Discard"), _("to Draft"), _("Cancel"));
3648 case G_ALERTDEFAULT:
3650 case G_ALERTALTERNATE:
3651 compose_draft_cb(data, 0, NULL);
3658 gtk_widget_destroy(compose->window);
3661 static void compose_address_cb(gpointer data, guint action, GtkWidget *widget)
3663 Compose *compose = (Compose *)data;
3665 addressbook_open(compose);
3668 static void compose_ext_editor_cb(gpointer data, guint action,
3671 Compose *compose = (Compose *)data;
3673 compose_exec_ext_editor(compose);
3676 static void compose_destroy_cb(GtkWidget *widget, Compose *compose)
3678 compose_destroy(compose);
3681 static void compose_cut_cb(Compose *compose)
3683 if (compose->focused_editable &&
3684 GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
3685 gtk_editable_cut_clipboard
3686 (GTK_EDITABLE(compose->focused_editable));
3689 static void compose_copy_cb(Compose *compose)
3691 if (compose->focused_editable &&
3692 GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
3693 gtk_editable_copy_clipboard
3694 (GTK_EDITABLE(compose->focused_editable));
3697 static void compose_paste_cb(Compose *compose)
3699 if (compose->focused_editable &&
3700 GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
3701 gtk_editable_paste_clipboard
3702 (GTK_EDITABLE(compose->focused_editable));
3705 static void compose_allsel_cb(Compose *compose)
3707 if (compose->focused_editable &&
3708 GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
3709 gtk_editable_select_region
3710 (GTK_EDITABLE(compose->focused_editable), 0, -1);
3713 static void compose_grab_focus_cb(GtkWidget *widget, Compose *compose)
3715 if (GTK_IS_EDITABLE(widget))
3716 compose->focused_editable = widget;
3719 static void compose_changed_cb(GtkEditable *editable, Compose *compose)
3721 if (compose->modified == FALSE) {
3722 compose->modified = TRUE;
3723 compose_set_title(compose);
3727 static void compose_button_press_cb(GtkWidget *widget, GdkEventButton *event,
3730 gtk_stext_set_point(GTK_STEXT(widget),
3731 gtk_editable_get_position(GTK_EDITABLE(widget)));
3735 static void compose_key_press_cb(GtkWidget *widget, GdkEventKey *event,
3738 gtk_stext_set_point(GTK_STEXT(widget),
3739 gtk_editable_get_position(GTK_EDITABLE(widget)));
3743 static void compose_toggle_to_cb(gpointer data, guint action,
3746 Compose *compose = (Compose *)data;
3748 if (GTK_CHECK_MENU_ITEM(widget)->active) {
3749 gtk_widget_show(compose->to_hbox);
3750 gtk_widget_show(compose->to_entry);
3751 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 1, 4);
3752 compose->use_to = TRUE;
3754 gtk_widget_hide(compose->to_hbox);
3755 gtk_widget_hide(compose->to_entry);
3756 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 1, 0);
3757 gtk_widget_queue_resize(compose->table_vbox);
3758 compose->use_to = FALSE;
3761 if (addressbook_get_target_compose() == compose)
3762 addressbook_set_target_compose(compose);
3765 static void compose_toggle_cc_cb(gpointer data, guint action,
3768 Compose *compose = (Compose *)data;
3770 if (GTK_CHECK_MENU_ITEM(widget)->active) {
3771 gtk_widget_show(compose->cc_hbox);
3772 gtk_widget_show(compose->cc_entry);
3773 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 3, 4);
3774 compose->use_cc = TRUE;
3776 gtk_widget_hide(compose->cc_hbox);
3777 gtk_widget_hide(compose->cc_entry);
3778 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 3, 0);
3779 gtk_widget_queue_resize(compose->table_vbox);
3780 compose->use_cc = FALSE;
3783 if (addressbook_get_target_compose() == compose)
3784 addressbook_set_target_compose(compose);
3787 static void compose_toggle_bcc_cb(gpointer data, guint action,
3790 Compose *compose = (Compose *)data;
3792 if (GTK_CHECK_MENU_ITEM(widget)->active) {
3793 gtk_widget_show(compose->bcc_hbox);
3794 gtk_widget_show(compose->bcc_entry);
3795 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 4, 4);
3796 compose->use_bcc = TRUE;
3798 gtk_widget_hide(compose->bcc_hbox);
3799 gtk_widget_hide(compose->bcc_entry);
3800 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 4, 0);
3801 gtk_widget_queue_resize(compose->table_vbox);
3802 compose->use_bcc = FALSE;
3805 if (addressbook_get_target_compose() == compose)
3806 addressbook_set_target_compose(compose);
3809 static void compose_toggle_replyto_cb(gpointer data, guint action,
3812 Compose *compose = (Compose *)data;
3814 if (GTK_CHECK_MENU_ITEM(widget)->active) {
3815 gtk_widget_show(compose->reply_hbox);
3816 gtk_widget_show(compose->reply_entry);
3817 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 5, 4);
3818 compose->use_replyto = TRUE;
3820 gtk_widget_hide(compose->reply_hbox);
3821 gtk_widget_hide(compose->reply_entry);
3822 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 5, 0);
3823 gtk_widget_queue_resize(compose->table_vbox);
3824 compose->use_replyto = FALSE;
3828 static void compose_toggle_followupto_cb(gpointer data, guint action,
3831 Compose *compose = (Compose *)data;
3833 if (GTK_CHECK_MENU_ITEM(widget)->active) {
3834 gtk_widget_show(compose->followup_hbox);
3835 gtk_widget_show(compose->followup_entry);
3836 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 6, 4);
3837 compose->use_followupto = TRUE;
3839 gtk_widget_hide(compose->followup_hbox);
3840 gtk_widget_hide(compose->followup_entry);
3841 gtk_table_set_row_spacing(GTK_TABLE(compose->table), 6, 0);
3842 gtk_widget_queue_resize(compose->table_vbox);
3843 compose->use_followupto = FALSE;
3847 static void compose_toggle_attach_cb(gpointer data, guint action,
3850 Compose *compose = (Compose *)data;
3852 if (GTK_CHECK_MENU_ITEM(widget)->active) {
3853 gtk_widget_ref(compose->edit_vbox);
3855 gtk_container_remove(GTK_CONTAINER(compose->vbox2),
3856 compose->edit_vbox);
3857 gtk_paned_add2(GTK_PANED(compose->paned), compose->edit_vbox);
3858 gtk_box_pack_start(GTK_BOX(compose->vbox2), compose->paned,
3860 gtk_widget_show(compose->paned);
3862 gtk_widget_unref(compose->edit_vbox);
3863 gtk_widget_unref(compose->paned);
3865 compose->use_attach = TRUE;
3867 gtk_widget_ref(compose->paned);
3868 gtk_widget_ref(compose->edit_vbox);
3870 gtk_container_remove(GTK_CONTAINER(compose->vbox2),
3872 gtk_container_remove(GTK_CONTAINER(compose->paned),