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/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>
59 /* #include <sys/utsname.h> */
65 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
72 #include "mainwindow.h"
74 #include "addressbook.h"
75 #include "folderview.h"
80 #include "prefs_common.h"
81 #include "prefs_account.h"
84 #include "procheader.h"
86 #include "statusbar.h"
93 #include "alertpanel.h"
94 #include "manage_window.h"
95 #include "gtkshruler.h"
97 #include "addr_compl.h"
100 # include "rfc2015.h"
110 #define N_ATTACH_COLS 3
112 #define B64_LINE_SIZE 57
113 #define B64_BUFFSIZE 77
115 #define MAX_REFERENCES_LEN 999
117 static GdkColor quote_color = {0, 0, 0, 0xbfff};
119 static GList *compose_list = NULL;
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
126 static void compose_destroy (Compose *compose);
128 static gint compose_parse_header (Compose *compose,
130 static gchar *compose_parse_references (const gchar *ref,
132 static void compose_quote_file (Compose *compose,
135 static gchar *compose_quote_parse_fmt (Compose *compose,
138 static void compose_reply_set_entry (Compose *compose,
141 static void compose_reedit_set_entry (Compose *compose,
143 static void compose_insert_sig (Compose *compose);
144 static void compose_insert_file (Compose *compose,
146 static void compose_attach_append (Compose *compose,
148 ContentType cnttype);
149 static void compose_wrap_line (Compose *compose);
150 static void compose_set_title (Compose *compose);
152 static gint compose_send (Compose *compose);
153 static gint compose_write_to_file (Compose *compose,
156 static gint compose_write_body_to_file (Compose *compose,
158 static gint compose_save_to_outbox (Compose *compose,
160 static gint compose_queue (Compose *compose,
162 static void compose_write_attach (Compose *compose,
164 static gint compose_write_headers (Compose *compose,
166 const gchar *charset,
167 EncodingType encoding,
170 static void compose_convert_header (gchar *dest,
174 static void compose_generate_msgid (Compose *compose,
178 static void compose_attach_info_free (AttachInfo *ainfo);
179 static void compose_attach_remove_selected (Compose *compose);
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,
189 gboolean *cancelled);
190 static void attach_property_key_pressed (GtkWidget *widget,
192 gboolean *cancelled);
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,
199 GdkInputCondition condition);
200 static void compose_set_ext_editor_sensitive (Compose *compose,
203 static gint calc_cursor_xpos (GtkText *text,
207 /* callback functions */
209 static gboolean compose_edit_size_alloc (GtkEditable *widget,
210 GtkAllocation *allocation,
211 GtkSHRuler *shruler);
213 static void toolbar_send_cb (GtkWidget *widget,
215 static void toolbar_send_later_cb (GtkWidget *widget,
217 static void toolbar_draft_cb (GtkWidget *widget,
219 static void toolbar_insert_cb (GtkWidget *widget,
221 static void toolbar_attach_cb (GtkWidget *widget,
223 static void toolbar_sig_cb (GtkWidget *widget,
225 static void toolbar_ext_editor_cb (GtkWidget *widget,
227 static void toolbar_linewrap_cb (GtkWidget *widget,
229 static void toolbar_address_cb (GtkWidget *widget,
232 static void account_activated (GtkMenuItem *menuitem,
235 static void attach_selected (GtkCList *clist,
240 static void attach_button_pressed (GtkWidget *widget,
241 GdkEventButton *event,
243 static void attach_key_pressed (GtkWidget *widget,
247 static void compose_send_cb (gpointer data,
250 static void compose_send_later_cb (gpointer data,
254 static void compose_draft_cb (gpointer data,
258 static void compose_attach_cb (gpointer data,
261 static void compose_insert_file_cb (gpointer data,
265 static void compose_close_cb (gpointer data,
268 static void compose_address_cb (gpointer data,
272 static void compose_ext_editor_cb (gpointer data,
276 static gint compose_delete_cb (GtkWidget *widget,
279 static void compose_destroy_cb (GtkWidget *widget,
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);
287 static void compose_grab_focus_cb (GtkWidget *widget,
290 static void compose_changed_cb (GtkEditable *editable,
292 static void compose_button_press_cb (GtkWidget *widget,
293 GdkEventButton *event,
296 static void compose_key_press_cb (GtkWidget *widget,
301 static void compose_toggle_to_cb (gpointer data,
304 static void compose_toggle_cc_cb (gpointer data,
307 static void compose_toggle_bcc_cb (gpointer data,
310 static void compose_toggle_replyto_cb (gpointer data,
313 static void compose_toggle_followupto_cb(gpointer data,
316 static void compose_toggle_attach_cb (gpointer data,
319 static void compose_toggle_ruler_cb (gpointer data,
323 static void compose_toggle_sign_cb (gpointer data,
326 static void compose_toggle_encrypt_cb (gpointer data,
331 static void compose_attach_drag_received_cb (GtkWidget *widget,
332 GdkDragContext *drag_context,
335 GtkSelectionData *data,
339 static void compose_insert_drag_received_cb (GtkWidget *widget,
340 GdkDragContext *drag_context,
343 GtkSelectionData *data,
348 static void to_activated (GtkWidget *widget,
350 static void newsgroups_activated (GtkWidget *widget,
352 static void subject_activated (GtkWidget *widget,
354 static void cc_activated (GtkWidget *widget,
356 static void bcc_activated (GtkWidget *widget,
358 static void replyto_activated (GtkWidget *widget,
360 static void followupto_activated (GtkWidget *widget,
363 static GtkItemFactoryEntry compose_popup_entries[] =
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}
371 static GtkItemFactoryEntry compose_entries[] =
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},
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},
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>"},
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}
421 static GtkTargetEntry compose_mime_types[] =
423 {"text/uri-list", 0, 0}
426 void compose_new(PrefsAccount *account)
428 compose_new_with_recipient(account, NULL);
431 void compose_new_with_recipient(PrefsAccount *account, const gchar *to)
435 if (!account) account = cur_account;
436 g_return_if_fail(account != NULL);
438 compose = compose_create(account);
439 compose->mode = COMPOSE_NEW;
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);
446 if (account->protocol != A_NNTP) {
448 compose_entry_append(compose, to, COMPOSE_TO);
449 gtk_widget_grab_focus(compose->subject_entry);
451 gtk_widget_grab_focus(compose->to_entry);
453 gtk_widget_grab_focus(compose->newsgroups_entry);
456 void compose_reply(MsgInfo *msginfo, gboolean quote, gboolean to_all)
459 PrefsAccount *account;
462 g_return_if_fail(msginfo != NULL);
463 g_return_if_fail(msginfo->folder != NULL);
465 account = msginfo->folder->folder->account;
466 if (!account) account = cur_account;
467 g_return_if_fail(account != NULL);
469 MSG_UNSET_FLAGS(msginfo->flags, MSG_FORWARDED);
470 MSG_SET_FLAGS(msginfo->flags, MSG_REPLIED);
472 compose = compose_create(account);
473 compose->mode = COMPOSE_REPLY;
475 if (compose_parse_header(compose, msginfo) < 0) return;
476 compose_reply_set_entry(compose, msginfo, to_all);
478 text = GTK_TEXT(compose->text);
479 gtk_text_freeze(text);
485 if ((fp = procmime_get_text_part(msginfo)) == NULL)
486 g_warning(_("Can't get text part\n"));
488 quote_str = compose_quote_parse_fmt
489 (compose, msginfo, prefs_common.quotefmt);
490 gtk_text_insert(text, NULL, NULL, NULL, quote_str, -1);
492 compose_quote_file(compose, msginfo, fp);
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);
503 gtk_widget_grab_focus(compose->text);
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); \
513 void compose_forward(MsgInfo *msginfo, gboolean as_attach)
516 PrefsAccount *account;
521 g_return_if_fail(msginfo != NULL);
522 g_return_if_fail(msginfo->folder != NULL);
524 account = msginfo->folder->folder->account;
525 if (!account) account = cur_account;
526 g_return_if_fail(account != NULL);
528 MSG_UNSET_FLAGS(msginfo->flags, MSG_REPLIED);
529 MSG_SET_FLAGS(msginfo->flags, MSG_FORWARDED);
531 compose = compose_create(account);
532 compose->mode = COMPOSE_FORWARD;
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),
540 text = GTK_TEXT(compose->text);
541 gtk_text_freeze(text);
546 msgfile = procmsg_get_message_file_path(msginfo);
547 if (!is_file_exist(msgfile))
548 g_warning(_("%s: file not exist\n"), msgfile);
550 compose_attach_append(compose, msgfile,
551 MIME_MESSAGE_RFC822);
555 if ((fp = procmime_get_text_part(msginfo)) == NULL)
556 g_warning(_("Can't get text part\n"));
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);
569 while (fgets(buf, sizeof(buf), fp) != NULL)
570 gtk_text_insert(text, NULL, NULL, NULL,
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);
582 if (account->protocol != A_NNTP)
583 gtk_widget_grab_focus(compose->to_entry);
585 gtk_widget_grab_focus(compose->newsgroups_entry);
588 void compose_reedit(MsgInfo *msginfo)
591 PrefsAccount *account;
596 g_return_if_fail(msginfo != NULL);
597 g_return_if_fail(msginfo->folder != NULL);
599 account = msginfo->folder->folder->account;
600 if (!account) account = cur_account;
601 g_return_if_fail(account != NULL);
603 compose = compose_create(account);
604 compose->mode = COMPOSE_REEDIT_DRAFT;
605 compose->targetinfo = procmsg_msginfo_copy(msginfo);
607 if (compose_parse_header(compose, msginfo) < 0) return;
608 compose_reedit_set_entry(compose, msginfo);
610 text = GTK_TEXT(compose->text);
611 gtk_text_freeze(text);
613 if ((fp = procmime_get_text_part(msginfo)) == NULL)
614 g_warning(_("Can't get text part\n"));
616 while (fgets(buf, sizeof(buf), fp) != NULL)
617 gtk_text_insert(text, NULL, NULL, NULL, buf, -1);
622 gtk_widget_grab_focus(compose->text);
625 GList *compose_get_compose_list(void)
630 void compose_entry_append(Compose *compose, const gchar *address,
631 ComposeEntryType type)
636 if (!address || *address == '\0') return;
640 entry = GTK_ENTRY(compose->cc_entry);
643 entry = GTK_ENTRY(compose->bcc_entry);
647 entry = GTK_ENTRY(compose->to_entry);
651 text = gtk_entry_get_text(entry);
653 gtk_entry_append_text(entry, ", ");
654 gtk_entry_append_text(entry, address);
657 static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
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}};
679 g_return_val_if_fail(msginfo != NULL, -1);
681 if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
682 procheader_get_header_fields(fp, hentry);
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;
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;
695 if (hentry[H_REFERENCES].body != NULL) {
696 if (compose->mode == COMPOSE_REEDIT_DRAFT)
697 compose->references = hentry[H_REFERENCES].body;
699 compose->references = compose_parse_references
700 (hentry[H_REFERENCES].body, msginfo->msgid);
701 g_free(hentry[H_REFERENCES].body);
703 hentry[H_REFERENCES].body = NULL;
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;
710 g_free(hentry[H_BCC].body);
711 hentry[H_BCC].body = NULL;
713 if (hentry[H_NEWSGROUPS].body != NULL) {
714 compose->newsgroups = hentry[H_NEWSGROUPS].body;
715 hentry[H_NEWSGROUPS].body = NULL;
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;
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);
729 if (!compose->references) {
730 if (msginfo->inreplyto && *msginfo->inreplyto)
731 compose->references =
732 g_strdup_printf("<%s>\n\t<%s>",
736 compose->references =
737 g_strconcat("<", msginfo->msgid, ">",
745 static gchar *compose_parse_references(const gchar *ref, const gchar *msgid)
747 GSList *ref_id_list, *cur;
751 ref_id_list = references_list_append(NULL, ref);
752 if (!ref_id_list) return NULL;
754 ref_id_list = g_slist_append(ref_id_list, g_strdup(msgid));
759 for (cur = ref_id_list; cur != NULL; cur = cur->next)
760 /* "<" + Message-ID + ">" + CR+LF+TAB */
761 len += strlen((gchar *)cur->data) + 5;
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);
771 slist_free_strings(ref_id_list);
772 g_slist_free(ref_id_list);
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);
786 slist_free_strings(ref_id_list);
787 g_slist_free(ref_id_list);
789 new_ref_str = new_ref->str;
790 g_string_free(new_ref, FALSE);
795 static void compose_quote_file(Compose *compose, MsgInfo *msginfo, FILE *fp)
797 GtkText *text = GTK_TEXT(compose->text);
800 GdkColor *qcolor = NULL;
803 gchar *linep, *cur, *leftp;
804 gint line_len, cur_len;
809 /* if (prefs_common.enable_color) qcolor = "e_color; */
810 if (prefs_common.quotemark && *prefs_common.quotemark)
811 qmark = prefs_common.quotemark;
814 quote_str = compose_quote_parse_fmt(compose, msginfo, qmark);
815 g_return_if_fail(quote_str != NULL);
816 qlen = strlen(quote_str);
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,
823 gtk_text_insert(text, NULL, qcolor, NULL, buf, -1);
829 wrap_len = prefs_common.linewrap_len - qlen;
831 while (fgets(buf, sizeof(buf), fp) != NULL) {
833 str_len = strlen(buf);
835 if (str_len <= wrap_len) {
836 gtk_text_insert(text, NULL, qcolor, NULL,
838 gtk_text_insert(text, NULL, qcolor, NULL, buf, -1);
839 gtk_text_insert(text, NULL, NULL, NULL, "\n", 1);
843 linep = cur = leftp = buf;
844 line_len = cur_len = 0;
846 while (*cur != '\0') {
847 ch_len = mblen(cur, MB_CUR_MAX);
848 if (ch_len < 0) ch_len = 1;
850 if (ch_len == 1 && isspace(*cur)) {
851 linep = cur + ch_len;
852 line_len = cur_len + ch_len;
855 if (cur_len + ch_len > wrap_len && line_len > 0) {
856 gtk_text_insert(text, NULL, qcolor, NULL,
859 if (isspace(*(linep - 1)))
860 gtk_text_insert(text, NULL,
862 leftp, line_len - 1);
864 gtk_text_insert(text, NULL,
867 gtk_text_insert(text, NULL, NULL, NULL,
871 cur_len = cur_len - line_len + ch_len;
878 linep = cur + ch_len;
879 line_len = cur_len + ch_len;
886 gtk_text_insert(text, NULL, qcolor, NULL,
888 gtk_text_insert(text, NULL, qcolor, NULL, leftp, -1);
889 gtk_text_insert(text, NULL, NULL, NULL, "\n", 1);
896 static gchar *compose_quote_parse_fmt(Compose *compose, MsgInfo *msginfo,
900 size_t buf_len = 1024;
908 if (!fmt || *fmt == '\0') return 0;
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);
915 ext_str = g_malloc(sizeof(gchar) * buf_len);
916 g_return_val_if_fail(ext_str != NULL, NULL);
921 len = wctomb(mbs, *sp);
927 wctomb(mbs, *(++sp));
939 case 'I': /* initial */
940 if (!msginfo->fromname) {sp++; break;}
941 p = msginfo->fromname;
942 tmp[0] = tmp[1] = tmp[2] = '\0';
944 if (*p && isalnum(*p))
945 tmp[0] = toupper(*p);
952 while (*p && !isspace(*p)) p++;
953 while (*p && isspace(*p)) p++;
954 if (*p && isalnum(*p))
955 tmp[1] = toupper(*p);
958 if (tmp[1]) str = tmp;
962 str = msginfo->fromname;
966 if (!msginfo->fromname) {sp++; break;}
967 Xstrdup_a(str, msginfo->fromname,
970 while (*p && !isspace(*p)) p++;
975 str = msginfo->subject;
983 if (!msginfo->msgid) {sp++; break;}
984 Xalloca(str, strlen(msginfo->msgid) + 3,
986 g_snprintf(str, strlen(msginfo->msgid) + 3,
987 "<%s>", msginfo->msgid);
999 while (ext_len + strlen(str) + 1 > buf_len)
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);
1007 } else if (*mbs == '\\') {
1008 wctomb(mbs, *(++sp));
1026 while (ext_len + strlen(str) + 1 > buf_len)
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);
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);
1046 ext_str = g_realloc(ext_str, strlen(ext_str) + 1);
1051 static void compose_reply_set_entry(Compose *compose, MsgInfo *msginfo,
1057 GHashTable *to_table;
1059 g_return_if_fail(compose->account != NULL);
1060 g_return_if_fail(msginfo != NULL);
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
1072 if (msginfo->subject && *msginfo->subject) {
1073 gchar *buf, *buf2, *p;
1075 buf = g_strdup(msginfo->subject);
1076 while (!strncasecmp(buf, "Re:", 3)) {
1078 while (isspace(*p)) p++;
1079 memmove(buf, p, strlen(p) + 1);
1082 buf2 = g_strdup_printf("Re: %s", buf);
1083 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), buf2);
1087 gtk_entry_set_text(GTK_ENTRY(compose->subject_entry), "Re: ");
1089 if (!to_all || compose->account->protocol == A_NNTP) return;
1091 from = g_strdup(compose->replyto ? compose->replyto :
1092 msginfo->from ? msginfo->from : "");
1093 extract_address(from);
1095 cc_list = address_list_append(NULL, msginfo->to);
1096 cc_list = address_list_append(cc_list, compose->cc);
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));
1104 /* remove address on To: and that of current account */
1105 for (cur = cc_list; cur != NULL; ) {
1106 GSList *next = cur->next;
1108 if (g_hash_table_lookup(to_table, cur->data) != NULL)
1109 cc_list = g_slist_remove(cc_list, cur->data);
1111 g_hash_table_insert(to_table, cur->data, cur);
1115 g_hash_table_destroy(to_table);
1119 for (cur = cc_list; cur != NULL; cur = cur->next)
1120 compose_entry_append(compose, (gchar *)cur->data,
1122 slist_free_strings(cc_list);
1123 g_slist_free(cc_list);
1127 #define SET_ENTRY(entry, str) \
1130 gtk_entry_set_text(GTK_ENTRY(compose->entry), str); \
1133 static void compose_reedit_set_entry(Compose *compose, MsgInfo *msginfo)
1135 g_return_if_fail(msginfo != NULL);
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);
1144 GtkItemFactory *ifactory;
1145 GtkWidget *menuitem;
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);
1152 if (compose->replyto) {
1153 GtkItemFactory *ifactory;
1154 GtkWidget *menuitem;
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);
1164 static void compose_insert_sig(Compose *compose)
1168 if (compose->account && compose->account->sig_path)
1169 sigfile = g_strdup(compose->account->sig_path);
1171 sigfile = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S,
1172 DEFAULT_SIGNATURE, NULL);
1175 if (!is_file_or_fifo_exist(sigfile)) {
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,
1188 compose_insert_file(compose, sigfile);
1192 static void compose_insert_file(Compose *compose, const gchar *file)
1194 GtkText *text = GTK_TEXT(compose->text);
1195 gchar buf[BUFFSIZE];
1198 g_return_if_fail(file != NULL);
1200 if ((fp = fopen(file, "r")) == NULL) {
1201 FILE_OP_ERROR(file, "fopen");
1205 gtk_text_freeze(text);
1207 while (fgets(buf, sizeof(buf), fp) != NULL)
1208 gtk_text_insert(text, NULL, NULL, NULL, buf, -1);
1210 gtk_text_thaw(text);
1215 static void compose_attach_append(Compose *compose, const gchar *file,
1216 ContentType cnttype)
1219 gchar *text[N_ATTACH_COLS];
1223 if (!is_file_exist(file)) {
1224 g_warning(_("File %s doesn't exist\n"), file);
1227 if ((size = get_file_size(file)) < 0) {
1228 g_warning(_("Can't get file size of %s\n"), file);
1232 alertpanel_notice(_("File %s is empty\n"), file);
1236 if (!compose->use_attach) {
1237 GtkItemFactory *ifactory;
1238 GtkWidget *menuitem;
1240 ifactory = gtk_item_factory_from_widget(compose->menubar);
1241 menuitem = gtk_item_factory_get_item(ifactory,
1243 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem),
1247 ainfo = g_new0(AttachInfo, 1);
1248 ainfo->file = g_strdup(file);
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"),
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));
1265 text[COL_MIMETYPE] = ainfo->content_type;
1266 text[COL_SIZE] = to_human_readable(size);
1267 text[COL_NAME] = ainfo->name;
1269 row = gtk_clist_append(GTK_CLIST(compose->attach_clist), text);
1270 gtk_clist_set_row_data(GTK_CLIST(compose->attach_clist), row, ainfo);
1272 if (cnttype != MIME_MESSAGE_RFC822)
1273 compose_changed_cb(NULL, compose);
1276 static void compose_wrap_line(Compose *compose)
1278 GtkText *text = GTK_TEXT(compose->text);
1280 guint line_pos = 0, cur_pos = 0;
1281 gint line_len = 0, cur_len = 0;
1283 gchar cbuf[MB_CUR_MAX];
1285 gtk_text_freeze(text);
1287 text_len = gtk_text_get_length(text);
1289 for (; cur_pos < text_len; cur_pos++) {
1290 if (text->use_wchar)
1292 (cbuf, (wchar_t)GTK_TEXT_INDEX(text, cur_pos));
1294 cbuf[0] = GTK_TEXT_INDEX(text, cur_pos);
1298 if (ch_len == 1 && *cbuf == '\n') {
1299 line_pos = cur_pos + 1;
1300 line_len = cur_len = 0;
1309 if (ch_len == 1 && isspace(*cbuf)) {
1310 line_pos = cur_pos + 1;
1311 line_len = cur_len + ch_len;
1314 if (cur_len + ch_len > prefs_common.linewrap_len &&
1318 if (text->use_wchar)
1319 tlen = wctomb(cbuf, (wchar_t)GTK_TEXT_INDEX(text, line_pos - 1));
1321 cbuf[0] = GTK_TEXT_INDEX(text, line_pos - 1);
1324 if (tlen == 1 && isspace(*cbuf)) {
1325 gtk_text_set_point(text, line_pos);
1326 gtk_text_backward_delete(text, 1);
1334 gtk_text_set_point(text, line_pos);
1335 gtk_text_insert(text, NULL, NULL, NULL, "\n", 1);
1339 cur_len = cur_len - line_len + ch_len;
1345 line_pos = cur_pos + 1;
1346 line_len = cur_len + ch_len;
1351 gtk_text_thaw(text);
1354 static void compose_set_title(Compose *compose)
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);
1364 str = g_strdup_printf(_("Compose message%s"), edited);
1365 gtk_window_set_title(GTK_WINDOW(compose->window), str);
1369 static gint compose_send(Compose *compose)
1371 gchar tmp[MAXPATHLEN + 1];
1372 gchar *to, *newsgroups;
1374 static gboolean lock = FALSE;
1378 g_return_val_if_fail(compose->account != NULL, -1);
1379 g_return_val_if_fail(compose->orig_account != NULL, -1);
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."));
1391 /* write to temporary file */
1392 g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%d",
1393 get_rc_dir(), G_DIR_SEPARATOR, (gint)compose);
1395 if (prefs_common.linewrap_at_send)
1396 compose_wrap_line(compose);
1398 if (compose_write_to_file(compose, tmp, FALSE) < 0) {
1403 if (!compose->to_list && !compose->newsgroup_list) {
1404 g_warning(_("can't get recipient list."));
1410 if (compose->to_list) {
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)
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."));
1429 ok = send_message(tmp, ac, compose->to_list);
1430 statusbar_pop_all();
1433 if (ok == 0 && compose->newsgroup_list) {
1436 if (compose->account->protocol == A_NNTP)
1437 folder = FOLDER(compose->account->folder);
1439 folder = FOLDER(compose->orig_account->folder);
1441 ok = news_post(folder, tmp);
1443 alertpanel_error(_("Error occurred while posting the message to %s ."),
1444 compose->account->nntp_server);
1451 /* queue message if failed to send */
1453 if (prefs_common.queue_msg) {
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);
1464 alertpanel_error(_("Can't queue the message."));
1467 alertpanel_error(_("Error occurred while sending the message."));
1470 /* save message to outbox */
1471 if (ok == 0 && prefs_common.savemsg) {
1472 if (compose_save_to_outbox(compose, tmp) < 0)
1474 (_("Can't save the message to outbox."));
1477 if (unlink(tmp) < 0) FILE_OP_ERROR(tmp, "unlink");
1482 static gint compose_write_to_file(Compose *compose, const gchar *file,
1489 const gchar *out_codeset;
1490 EncodingType encoding;
1492 if ((fp = fopen(file, "w")) == NULL) {
1493 FILE_OP_ERROR(file, "fopen");
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"));
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;
1511 const gchar *src_codeset;
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));
1520 src_codeset = conv_get_current_charset_str();
1521 buf = conv_codeset_strdup(chars, src_codeset, out_codeset);
1526 alertpanel_error(_("Can't convert the codeset of the message."));
1533 if (compose_write_headers
1534 (compose, fp, out_codeset, encoding, is_draft) < 0) {
1535 g_warning(_("can't write headers\n"));
1542 if (compose->use_attach) {
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)
1549 fputs("This is a multi-part message in MIME format.\n", fp);
1551 fprintf(fp, "\n--%s\n", compose->boundary);
1552 fprintf(fp, "Content-Type: text/plain; charset=%s\n",
1554 fprintf(fp, "Content-Transfer-Encoding: %s\n",
1555 procmime_get_encoding_str(encoding));
1561 if (encoding == ENC_BASE64) {
1562 gchar outbuf[B64_BUFFSIZE];
1565 for (i = 0; i < len; i += B64_LINE_SIZE) {
1566 l = MIN(B64_LINE_SIZE, len - i);
1567 to64frombits(outbuf, buf + i, l);
1571 } else if (fwrite(buf, sizeof(gchar), len, fp) != len) {
1572 FILE_OP_ERROR(file, "fwrite");
1580 if (compose->use_attach)
1581 compose_write_attach(compose, fp);
1583 if (fclose(fp) == EOF) {
1584 FILE_OP_ERROR(file, "fclose");
1590 if (compose->use_signing) {
1591 if (rfc2015_sign(file, compose->account) < 0) {
1596 if (compose->use_encryption) {
1597 if (rfc2015_encrypt(file, compose->to_list) < 0) {
1602 #endif /* USE_GPGME */
1607 static gint compose_write_body_to_file(Compose *compose, const gchar *file)
1613 if ((fp = fopen(file, "w")) == NULL) {
1614 FILE_OP_ERROR(file, "fopen");
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"));
1624 chars = gtk_editable_get_chars(GTK_EDITABLE(compose->text), 0, -1);
1627 len = strlen(chars);
1628 if (fwrite(chars, sizeof(gchar), len, fp) != len) {
1629 FILE_OP_ERROR(file, "fwrite");
1638 if (fclose(fp) == EOF) {
1639 FILE_OP_ERROR(file, "fclose");
1646 static gint compose_save_to_outbox(Compose *compose, const gchar *file)
1653 debug_print(_("saving sent message...\n"));
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"));
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"));
1668 newmsginfo.msgnum = num;
1669 newmsginfo.flags = 0;
1670 procmsg_write_flags(&newmsginfo, fp);
1678 static gint compose_queue(Compose *compose, const gchar *file)
1681 gchar *tmp, *queue_path;
1684 gchar buf[BUFFSIZE];
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);
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");
1698 if ((src_fp = fopen(file, "r")) == NULL) {
1699 FILE_OP_ERROR(file, "fopen");
1705 if (change_file_mode_rw(fp, tmp) < 0) {
1706 FILE_OP_ERROR(tmp, "chmod");
1707 g_warning(_("can't change file mode\n"));
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");
1718 fprintf(fp, "MID:<%s>\n", compose->msgid);
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);
1728 fprintf(fp, "SSV:\n");
1729 if (compose->account->nntp_server)
1730 fprintf(fp, "NSV:%s\n", compose->account->nntp_server);
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);
1740 while (fgets(buf, sizeof(buf), src_fp) != NULL) {
1741 if (fputs(buf, fp) == EOF) {
1742 FILE_OP_ERROR(tmp, "fputs");
1752 if (fclose(fp) == EOF) {
1753 FILE_OP_ERROR(tmp, "fclose");
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"));
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"));
1776 newmsginfo.msgnum = num;
1777 newmsginfo.flags = 0;
1778 procmsg_write_flags(&newmsginfo, fp);
1786 static void compose_write_attach(Compose *compose, FILE *fp)
1789 GtkCList *clist = GTK_CLIST(compose->attach_clist);
1792 gchar filename[BUFFSIZE];
1795 for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
1797 if ((attach_fp = fopen(ainfo->file, "r")) == NULL) {
1798 g_warning(_("Can't open file %s\n"), ainfo->file);
1802 fprintf(fp, "\n--%s\n", compose->boundary);
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");
1808 conv_encode_header(filename, sizeof(filename),
1810 fprintf(fp, "Content-Type: %s;\n"
1812 ainfo->content_type, filename);
1813 fprintf(fp, "Content-Disposition: attachment;\n"
1814 " filename=\"%s\"\n", filename);
1817 fprintf(fp, "Content-Transfer-Encoding: %s\n\n",
1818 procmime_get_encoding_str(ainfo->encoding));
1820 if (ainfo->encoding == ENC_7BIT) {
1821 gchar buf[BUFFSIZE];
1823 while (fgets(buf, sizeof(buf), attach_fp) != NULL) {
1825 if (len > 1 && buf[len - 1] == '\n' &&
1826 buf[len - 2] == '\r') {
1827 buf[len - 2] = '\n';
1828 buf[len - 1] = '\0';
1833 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
1835 while ((len = fread(inbuf, sizeof(gchar),
1836 B64_LINE_SIZE, attach_fp))
1838 to64frombits(outbuf, inbuf, B64_LINE_SIZE);
1842 if (len > 0 && feof(attach_fp)) {
1843 to64frombits(outbuf, inbuf, len);
1852 fprintf(fp, "\n--%s--\n", compose->boundary);
1855 static gint compose_write_headers(Compose *compose, FILE *fp,
1856 const gchar *charset, EncodingType encoding,
1859 gchar buf[BUFFSIZE];
1861 /* struct utsname utsbuf; */
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);
1869 if (compose->account->add_date) {
1870 get_rfc822_date(buf, sizeof(buf));
1871 fprintf(fp, "Date: %s\n", buf);
1875 if (compose->account->name && *compose->account->name) {
1876 compose_convert_header
1877 (buf, sizeof(buf), compose->account->name,
1879 fprintf(fp, "From: %s <%s>\n", buf, compose->account->address);
1881 fprintf(fp, "From: %s\n", compose->account->address);
1883 slist_free_strings(compose->to_list);
1884 g_slist_free(compose->to_list);
1885 compose->to_list = NULL;
1888 if (compose->use_to) {
1889 str = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
1891 Xstrdup_a(str, str, return -1);
1894 compose->to_list = address_list_append
1895 (compose->to_list, str);
1896 compose_convert_header(buf, sizeof(buf), str,
1898 fprintf(fp, "To: %s\n", buf);
1903 slist_free_strings(compose->newsgroup_list);
1904 g_slist_free(compose->newsgroup_list);
1905 compose->newsgroup_list = NULL;
1908 str = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
1910 Xstrdup_a(str, str, return -1);
1914 compose->newsgroup_list =
1915 newsgroup_list_append(compose->newsgroup_list,
1917 compose_convert_header(buf, sizeof(buf), str,
1918 strlen("Newsgroups: "));
1919 fprintf(fp, "Newsgroups: %s\n", buf);
1923 if (!is_draft && !compose->to_list && !compose->newsgroup_list)
1927 if (compose->use_cc) {
1928 str = gtk_entry_get_text(GTK_ENTRY(compose->cc_entry));
1930 Xstrdup_a(str, str, return -1);
1933 compose->to_list = address_list_append
1934 (compose->to_list, str);
1935 compose_convert_header(buf, sizeof(buf), str,
1937 fprintf(fp, "Cc: %s\n", buf);
1943 if (compose->use_bcc) {
1944 str = gtk_entry_get_text(GTK_ENTRY(compose->bcc_entry));
1946 Xstrdup_a(str, str, return -1);
1949 compose->to_list = address_list_append
1950 (compose->to_list, str);
1952 compose_convert_header
1953 (buf, sizeof(buf), str,
1955 fprintf(fp, "Bcc: %s\n", buf);
1962 str = gtk_entry_get_text(GTK_ENTRY(compose->subject_entry));
1964 Xstrdup_a(str, str, return -1);
1967 compose_convert_header(buf, sizeof(buf), str,
1968 strlen("Subject: "));
1969 fprintf(fp, "Subject: %s\n", buf);
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);
1981 if (compose->inreplyto && compose->to_list)
1982 fprintf(fp, "In-Reply-To: <%s>\n", compose->inreplyto);
1985 if (compose->references)
1986 fprintf(fp, "References: %s\n", compose->references);
1989 if (compose->use_followupto) {
1990 str = gtk_entry_get_text(GTK_ENTRY(compose->followup_entry));
1992 Xstrdup_a(str, str, return -1);
1996 compose_convert_header(buf, sizeof(buf), str,
1997 strlen("Followup-To: "));
1998 fprintf(fp, "Followup-To: %s\n", buf);
2004 if (compose->use_replyto) {
2005 str = gtk_entry_get_text(GTK_ENTRY(compose->reply_entry));
2007 Xstrdup_a(str, str, return -1);
2010 compose_convert_header(buf, sizeof(buf), str,
2011 strlen("Reply-To: "));
2012 fprintf(fp, "Reply-To: %s\n", buf);
2017 /* Program version and system info */
2018 /* uname(&utsbuf); */
2019 str = gtk_entry_get_text(GTK_ENTRY(compose->to_entry));
2021 fprintf(fp, "X-Mailer: %s (GTK+ %d.%d.%d; %s)\n",
2023 gtk_major_version, gtk_minor_version, gtk_micro_version,
2025 /* utsbuf.sysname, utsbuf.release, utsbuf.machine); */
2027 str = gtk_entry_get_text(GTK_ENTRY(compose->newsgroups_entry));
2029 fprintf(fp, "X-Newsreader: %s (GTK+ %d.%d.%d; %s)\n",
2031 gtk_major_version, gtk_minor_version, gtk_micro_version,
2033 /* utsbuf.sysname, utsbuf.release, utsbuf.machine); */
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);
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);
2053 "Content-Type: multipart/mixed;\n"
2054 " boundary=\"%s\"\n", compose->boundary);
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));
2061 /* separator between header and body */
2067 static void compose_convert_header(gchar *dest, gint len, gchar *src,
2070 g_return_if_fail(src != NULL);
2071 g_return_if_fail(dest != NULL);
2073 if (len < 1) return;
2077 if (is_ascii_str(src)) {
2078 strncpy2(dest, src, len);
2079 dest[len - 1] = '\0';
2082 conv_encode_header(dest, len, src, header_len);
2085 static void compose_generate_msgid(Compose *compose, gchar *buf, gint len)
2094 if (compose->account && compose->account->address &&
2095 *compose->account->address) {
2096 if (strchr(compose->account->address, '@'))
2097 addr = g_strdup(compose->account->address);
2099 addr = g_strconcat(compose->account->address, "@",
2100 get_domain_name(), NULL);
2102 addr = g_strconcat(g_get_user_name(), "@", get_domain_name(),
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);
2111 debug_print(_("generated Message-ID: %s\n"), buf);
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)
2123 if (GTK_TABLE(table)->nrows < (*count) + 1)
2124 gtk_table_resize(GTK_TABLE(table), (*count) + 1, 2);
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,
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);
2139 address_completion_register_entry(GTK_ENTRY(*entry));
2144 static Compose *compose_create(PrefsAccount *account)
2150 GtkWidget *handlebox;
2154 GtkWidget *table_vbox;
2156 GtkWidget *from_optmenu_hbox;
2157 GtkWidget *to_entry;
2159 GtkWidget *newsgroups_entry;
2160 GtkWidget *newsgroups_hbox;
2161 GtkWidget *subject_entry;
2162 GtkWidget *cc_entry;
2164 GtkWidget *bcc_entry;
2165 GtkWidget *bcc_hbox;
2166 GtkWidget *reply_entry;
2167 GtkWidget *reply_hbox;
2168 GtkWidget *followup_entry;
2169 GtkWidget *followup_hbox;
2173 GtkWidget *attach_scrwin;
2174 GtkWidget *attach_clist;
2176 GtkWidget *edit_vbox;
2177 GtkWidget *ruler_hbox;
2179 GtkWidget *scrolledwin;
2185 gchar *titles[] = {_("MIME type"), _("Size"), _("Name")};
2186 guint n_menu_entries;
2187 GtkStyle *style, *new_style;
2190 gboolean success[1];
2192 GtkWidget *popupmenu;
2193 GtkWidget *menuitem;
2194 GtkItemFactory *popupfactory;
2195 GtkItemFactory *ifactory;
2200 g_return_val_if_fail(account != NULL, NULL);
2202 debug_print(_("Creating compose window...\n"));
2203 compose = g_new0(Compose, 1);
2205 compose->account = account;
2206 compose->orig_account = account;
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);
2221 vbox = gtk_vbox_new(FALSE, 0);
2222 gtk_container_add(GTK_CONTAINER(window), vbox);
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);
2229 handlebox = gtk_handle_box_new();
2230 gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, FALSE, 0);
2232 compose_toolbar_create(compose, handlebox);
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);
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),
2243 table = gtk_table_new(8, 2, FALSE);
2244 gtk_box_pack_start(GTK_BOX(table_vbox), table, FALSE, TRUE, 0);
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,
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);
2258 /* header labels and entries */
2259 compose_add_entry_field(table, &to_hbox, &to_entry, &count,
2261 compose_add_entry_field(table, &newsgroups_hbox, &newsgroups_entry,
2262 &count, "Newsgroups:", FALSE);
2263 compose_add_entry_field(table, &hbox, &subject_entry, &count,
2265 compose_add_entry_field(table, &cc_hbox, &cc_entry, &count,
2267 compose_add_entry_field(table, &bcc_hbox, &bcc_entry, &count,
2269 compose_add_entry_field(table, &reply_hbox, &reply_entry, &count,
2271 compose_add_entry_field(table, &followup_hbox, &followup_entry, &count,
2272 "Followup-To:", FALSE);
2274 gtk_table_set_col_spacings(GTK_TABLE(table), 4);
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);
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);
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,
2311 gtk_widget_set_usize(attach_scrwin, -1, 80);
2313 attach_clist = gtk_clist_new_with_titles(N_ATTACH_COLS, titles);
2314 gtk_clist_set_column_justification(GTK_CLIST(attach_clist), COL_SIZE,
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,
2324 gtk_container_add(GTK_CONTAINER(attach_scrwin), attach_clist);
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);
2334 gtk_drag_dest_set(attach_clist,
2335 GTK_DEST_DEFAULT_ALL, compose_mime_types, 1,
2337 gtk_signal_connect(GTK_OBJECT(attach_clist), "drag_data_received",
2338 GTK_SIGNAL_FUNC(compose_attach_drag_received_cb),
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);
2347 edit_vbox = gtk_vbox_new(FALSE, 0);
2348 gtk_box_pack_start(GTK_BOX(vbox2), edit_vbox, TRUE, TRUE, 0);
2351 ruler_hbox = gtk_hbox_new(FALSE, 0);
2352 gtk_box_pack_start(GTK_BOX(edit_vbox), ruler_hbox, FALSE, FALSE, 0);
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,
2358 gtk_widget_set_usize(ruler_hbox, 1, -1);
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);
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);
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),
2383 gtk_signal_connect_after(GTK_OBJECT(text), "key_press_event",
2384 GTK_SIGNAL_FUNC(compose_key_press_cb),
2387 gtk_signal_connect_after(GTK_OBJECT(text), "size_allocate",
2388 GTK_SIGNAL_FUNC(compose_edit_size_alloc),
2392 gtk_drag_dest_set(text, GTK_DEST_DEFAULT_ALL, compose_mime_types, 1,
2394 gtk_signal_connect(GTK_OBJECT(text), "drag_data_received",
2395 GTK_SIGNAL_FUNC(compose_insert_drag_received_cb),
2398 gtk_widget_show_all(vbox);
2400 style = gtk_widget_get_style(text);
2402 /* workaround for the slow down of GtkText when using Pixmap theme */
2403 if (style->engine) {
2404 GtkThemeEngine *engine;
2406 engine = style->engine;
2407 style->engine = NULL;
2408 new_style = gtk_style_copy(style);
2409 style->engine = engine;
2411 new_style = gtk_style_copy(style);
2413 if (prefs_common.textfont) {
2416 charset = conv_get_current_charset();
2417 if (MB_CUR_MAX == 1) {
2420 Xstrdup_a(fontstr, prefs_common.textfont, );
2421 if (fontstr && (p = strchr(fontstr, ',')) != NULL)
2423 font = gdk_font_load(fontstr);
2425 font = gdk_fontset_load(prefs_common.textfont);
2427 gdk_font_unref(new_style->font);
2428 new_style->font = font;
2432 gtk_widget_set_style(text, new_style);
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;
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,
2449 ifactory = gtk_item_factory_from_widget(menubar);
2450 menu_set_sensitive(ifactory, "/Edit/Undo", FALSE);
2451 menu_set_sensitive(ifactory, "/Edit/Redo", FALSE);
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);
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);
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);
2478 switch (prefs_common.toolbar_style) {
2480 gtk_widget_hide(handlebox);
2483 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
2487 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
2491 gtk_toolbar_set_style(GTK_TOOLBAR(compose->toolbar),
2496 gtk_widget_show(window);
2498 address_completion_start(window);
2500 compose->window = window;
2501 compose->vbox = vbox;
2502 compose->menubar = menubar;
2503 compose->handlebox = handlebox;
2505 compose->vbox2 = vbox2;
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;
2523 compose->paned = paned;
2525 compose->attach_scrwin = attach_scrwin;
2526 compose->attach_clist = attach_clist;
2528 compose->edit_vbox = edit_vbox;
2529 compose->ruler_hbox = ruler_hbox;
2530 compose->ruler = ruler;
2531 compose->scrolledwin = scrolledwin;
2532 compose->text = text;
2534 compose->focused_editable = NULL;
2536 compose->popupmenu = popupmenu;
2537 compose->popupfactory = popupfactory;
2539 compose->mode = COMPOSE_NEW;
2541 compose->replyto = 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;
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;
2558 compose->use_signing = FALSE;
2559 compose->use_encryption = FALSE;
2560 #endif /* USE_GPGME */
2562 compose->modified = FALSE;
2564 compose->to_list = NULL;
2565 compose->newsgroup_list = NULL;
2567 compose->exteditor_file = NULL;
2568 compose->exteditor_pid = -1;
2569 compose->exteditor_readdes = -1;
2570 compose->exteditor_tag = -1;
2572 compose_set_title(compose);
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);
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);
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);
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);
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);
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 */
2618 addressbook_set_target_compose(compose);
2620 compose_list = g_list_append(compose_list, compose);
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"
2634 #define CREATE_TOOLBAR_ICON(xpm_d) \
2636 icon = gdk_pixmap_create_from_xpm_d(container->window, &mask, \
2637 &container->style->white, \
2639 icon_wid = gtk_pixmap_new(icon, mask); \
2642 static void compose_toolbar_create(Compose *compose, GtkWidget *container)
2647 GtkWidget *icon_wid;
2648 GtkWidget *send_btn;
2649 GtkWidget *sendl_btn;
2650 GtkWidget *draft_btn;
2651 GtkWidget *insert_btn;
2652 GtkWidget *attach_btn;
2654 GtkWidget *exteditor_btn;
2655 GtkWidget *linewrap_btn;
2656 GtkWidget *addrbook_btn;
2658 toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
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);
2666 CREATE_TOOLBAR_ICON(stock_mail_send_xpm);
2667 send_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
2671 icon_wid, toolbar_send_cb, compose);
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),
2677 _("Put into queue folder and send later"),
2679 icon_wid, toolbar_send_later_cb,
2682 CREATE_TOOLBAR_ICON(stock_mail_xpm);
2683 draft_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
2685 _("Save to draft folder"),
2687 icon_wid, toolbar_draft_cb,
2690 gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
2692 CREATE_TOOLBAR_ICON(stock_paste_xpm);
2693 insert_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
2697 icon_wid, toolbar_insert_cb,
2700 CREATE_TOOLBAR_ICON(stock_mail_attach_xpm);
2701 attach_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
2705 icon_wid, toolbar_attach_cb,
2708 gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
2710 CREATE_TOOLBAR_ICON(stock_mail_xpm);
2711 sig_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
2713 _("Insert signature"),
2715 icon_wid, toolbar_sig_cb, compose);
2717 gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
2719 CREATE_TOOLBAR_ICON(stock_mail_compose_xpm);
2720 exteditor_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
2722 _("Edit with external editor"),
2725 toolbar_ext_editor_cb,
2728 CREATE_TOOLBAR_ICON(linewrap_xpm);
2729 linewrap_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
2731 _("Wrap long lines"),
2734 toolbar_linewrap_cb,
2737 gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
2739 CREATE_TOOLBAR_ICON(tb_address_book_xpm);
2740 addrbook_btn = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
2744 icon_wid, toolbar_address_cb,
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;
2758 gtk_widget_show_all(toolbar);
2761 static GtkWidget *compose_account_option_menu_create(Compose *compose)
2767 gint num = 0, def_menu = 0;
2769 accounts = account_get_list();
2770 g_return_val_if_fail(accounts != NULL, NULL);
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();
2777 for (; accounts != NULL; accounts = accounts->next, num++) {
2778 PrefsAccount *ac = (PrefsAccount *)accounts->data;
2779 GtkWidget *menuitem;
2782 if (ac == compose->account) def_menu = num;
2784 name = g_strdup_printf("%s <%s> (%s)",
2785 ac->name, ac->address, ac->account_name);
2786 MENUITEM_ADD(menu, menuitem, name, ac);
2788 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
2789 GTK_SIGNAL_FUNC(account_activated),
2793 gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), menu);
2794 gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), def_menu);
2799 static void compose_destroy(Compose *compose)
2802 GtkCList *clist = GTK_CLIST(compose->attach_clist);
2805 /* NOTE: address_completion_end() does nothing with the window
2806 * however this may change. */
2807 address_completion_end(compose->window);
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);
2814 procmsg_msginfo_free(compose->targetinfo);
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);
2822 g_free(compose->inreplyto);
2823 g_free(compose->references);
2824 g_free(compose->msgid);
2825 g_free(compose->boundary);
2827 g_free(compose->exteditor_file);
2829 for (row = 0; (ainfo = gtk_clist_get_row_data(clist, row)) != NULL;
2831 compose_attach_info_free(ainfo);
2833 if (addressbook_get_target_compose() == compose)
2834 addressbook_set_target_compose(NULL);
2836 prefs_common.compose_width = compose->scrolledwin->allocation.width;
2837 prefs_common.compose_height = compose->window->allocation.height;
2839 gtk_widget_destroy(compose->paned);
2843 compose_list = g_list_remove(compose_list, compose);
2846 static void compose_attach_info_free(AttachInfo *ainfo)
2848 g_free(ainfo->file);
2849 g_free(ainfo->content_type);
2850 g_free(ainfo->name);
2854 static void compose_attach_remove_selected(Compose *compose)
2856 GtkCList *clist = GTK_CLIST(compose->attach_clist);
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);
2868 static struct _AttachProperty
2871 GtkWidget *mimetype_entry;
2872 GtkWidget *encoding_optmenu;
2873 GtkWidget *path_entry;
2874 GtkWidget *filename_entry;
2876 GtkWidget *cancel_btn;
2879 static void compose_attach_property(Compose *compose)
2881 GtkCList *clist = GTK_CLIST(compose->attach_clist);
2885 GtkOptionMenu *optmenu;
2887 if (!clist->selection) return;
2888 row = GPOINTER_TO_INT(clist->selection->data);
2890 ainfo = gtk_clist_get_row_data(clist, row);
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));
2899 optmenu = GTK_OPTION_MENU(attach_prop.encoding_optmenu);
2900 if (ainfo->encoding == ENC_UNKNOWN)
2901 gtk_option_menu_set_history(optmenu, ENC_BASE64);
2903 gtk_option_menu_set_history(optmenu, ainfo->encoding);
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 : "");
2914 gchar *cnttype = NULL;
2918 GtkWidget *menuitem;
2922 if (cancelled == TRUE) {
2923 gtk_widget_hide(attach_prop.window);
2927 text = gtk_entry_get_text(GTK_ENTRY(attach_prop.mimetype_entry));
2928 if (*text != '\0') {
2931 text = g_strstrip(g_strdup(text));
2932 if ((p = strchr(text, '/')) && !strchr(p + 1, '/')) {
2933 cnttype = g_strdup(text);
2936 alertpanel_error(_("Invalid MIME type."));
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)));
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);
2954 (_("File doesn't exist or is empty."));
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);
2967 g_free(ainfo->content_type);
2968 ainfo->content_type = cnttype;
2971 g_free(ainfo->file);
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);
2983 gtk_widget_hide(attach_prop.window);
2988 #define SET_LABEL_AND_ENTRY(str, entry, top) \
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); \
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); \
3000 static void compose_attach_property_create(gboolean *cancelled)
3006 GtkWidget *mimetype_entry;
3009 GtkWidget *optmenu_menu;
3010 GtkWidget *menuitem;
3011 GtkWidget *path_entry;
3012 GtkWidget *filename_entry;
3015 GtkWidget *cancel_btn;
3017 debug_print("Creating attach_property window...\n");
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),
3028 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
3029 GTK_SIGNAL_FUNC(attach_property_key_pressed),
3032 vbox = gtk_vbox_new(FALSE, 8);
3033 gtk_container_add(GTK_CONTAINER(window), vbox);
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);
3040 SET_LABEL_AND_ENTRY(_("MIME type"), mimetype_entry, 0);
3042 label = gtk_label_new(_("Encoding"));
3043 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
3045 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
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);
3051 optmenu = gtk_option_menu_new();
3052 gtk_box_pack_start(GTK_BOX(hbox), optmenu, FALSE, FALSE, 0);
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);
3063 gtk_option_menu_set_menu(GTK_OPTION_MENU(optmenu), optmenu_menu);
3065 SET_LABEL_AND_ENTRY(_("Path"), path_entry, 2);
3066 SET_LABEL_AND_ENTRY(_("File name"), filename_entry, 3);
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);
3073 gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
3074 GTK_SIGNAL_FUNC(attach_property_ok),
3076 gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
3077 GTK_SIGNAL_FUNC(attach_property_cancel),
3080 gtk_widget_show_all(vbox);
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;
3091 static void attach_property_ok(GtkWidget *widget, gboolean *cancelled)
3097 static void attach_property_cancel(GtkWidget *widget, gboolean *cancelled)
3103 static gint attach_property_delete_event(GtkWidget *widget, GdkEventAny *event,
3104 gboolean *cancelled)
3112 static void attach_property_key_pressed(GtkWidget *widget, GdkEventKey *event,
3113 gboolean *cancelled)
3115 if (event && event->keyval == GDK_Escape) {
3121 static void compose_exec_ext_editor(Compose *compose)
3127 g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg.%08x",
3128 g_get_tmp_dir(), G_DIR_SEPARATOR, (gint)compose);
3130 if (pipe(pipe_fds) < 0) {
3135 if ((pid = fork()) < 0) {
3141 /* close the write side of the pipe */
3144 compose->exteditor_file = g_strdup(tmp);
3145 compose->exteditor_pid = pid;
3146 compose->exteditor_readdes = pipe_fds[0];
3148 compose_set_ext_editor_sensitive(compose, FALSE);
3150 compose->exteditor_tag =
3151 gdk_input_add(pipe_fds[0], GDK_INPUT_READ,
3152 compose_input_cb, compose);
3153 } else { /* process-monitoring process */
3159 /* close the read side of the pipe */
3162 if (compose_write_body_to_file(compose, tmp) < 0) {
3163 sock_write(pipe_fds[1], "2\n", 2);
3167 pid_ed = compose_exec_ext_editor_real(tmp);
3169 sock_write(pipe_fds[1], "1\n", 2);
3173 /* wait until editor is terminated */
3174 waitpid(pid_ed, NULL, 0);
3176 sock_write(pipe_fds[1], "0\n", 2);
3183 static gint compose_exec_ext_editor_real(const gchar *file)
3185 static gchar *def_cmd = "emacs %s";
3191 g_return_val_if_fail(file != NULL, -1);
3193 if ((pid = fork()) < 0) {
3198 if (pid != 0) return pid;
3200 /* grandchild process */
3202 if (setpgid(0, getppid()))
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);
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);
3216 cmdline = g_strsplit(buf, " ", 1024);
3217 execvp(cmdline[0], cmdline);
3220 g_strfreev(cmdline);
3225 static gboolean compose_ext_editor_kill(Compose *compose)
3227 pid_t pgid = compose->exteditor_pid * -1;
3230 ret = kill(pgid, 0);
3232 if (ret == 0 || (ret == -1 && EPERM == errno)) {
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);
3243 if (val == G_ALERTDEFAULT) {
3244 gdk_input_remove(compose->exteditor_tag);
3245 close(compose->exteditor_readdes);
3247 if (kill(pgid, SIGTERM) < 0) perror("kill");
3248 waitpid(compose->exteditor_pid, NULL, 0);
3250 g_warning(_("Terminated process group id: %d"), -pgid);
3251 g_warning(_("Temporary file: %s"),
3252 compose->exteditor_file);
3254 compose_set_ext_editor_sensitive(compose, TRUE);
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;
3268 static void compose_input_cb(gpointer data, gint source,
3269 GdkInputCondition condition)
3272 Compose *compose = (Compose *)data;
3275 debug_print(_("Compose: input from monitoring process\n"));
3277 gdk_input_remove(compose->exteditor_tag);
3280 if (read(source, &buf[i], 1) < 1) {
3284 if (buf[i] == '\n') {
3289 if (i == sizeof(buf) - 1)
3293 waitpid(compose->exteditor_pid, NULL, 0);
3295 if (buf[0] == '0') { /* success */
3296 GtkText *text = GTK_TEXT(compose->text);
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);
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"));
3319 compose_set_ext_editor_sensitive(compose, TRUE);
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;
3328 static void compose_set_ext_editor_sensitive(Compose *compose,
3331 GtkItemFactory *ifactory;
3333 ifactory = gtk_item_factory_from_widget(compose->menubar);
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",
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",
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);
3355 static gint calc_cursor_xpos(GtkText *text, gint extra, gint char_width)
3359 cursor_pos = (text->cursor_pos_x - extra) / char_width;
3360 cursor_pos = MAX(cursor_pos, 0);
3365 /* callback functions */
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.
3371 static gboolean compose_edit_size_alloc(GtkEditable *widget,
3372 GtkAllocation *allocation,
3373 GtkSHRuler *shruler)
3375 if (prefs_common.show_ruler) {
3377 gint line_width_in_chars;
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;
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),
3390 /*line_width_in_chars*/ char_width);
3396 static void toolbar_send_cb(GtkWidget *widget, gpointer data)
3398 compose_send_cb(data, 0, NULL);
3401 static void toolbar_send_later_cb(GtkWidget *widget, gpointer data)
3403 compose_send_later_cb(data, 0, NULL);
3406 static void toolbar_draft_cb(GtkWidget *widget, gpointer data)
3408 compose_draft_cb(data, 0, NULL);
3411 static void toolbar_insert_cb(GtkWidget *widget, gpointer data)
3413 compose_insert_file_cb(data, 0, NULL);
3416 static void toolbar_attach_cb(GtkWidget *widget, gpointer data)
3418 compose_attach_cb(data, 0, NULL);
3421 static void toolbar_sig_cb(GtkWidget *widget, gpointer data)
3423 Compose *compose = (Compose *)data;
3425 compose_insert_sig(compose);
3428 static void toolbar_ext_editor_cb(GtkWidget *widget, gpointer data)
3430 Compose *compose = (Compose *)data;
3432 compose_exec_ext_editor(compose);
3435 static void toolbar_linewrap_cb(GtkWidget *widget, gpointer data)
3437 Compose *compose = (Compose *)data;
3439 compose_wrap_line(compose);
3442 static void toolbar_address_cb(GtkWidget *widget, gpointer data)
3444 compose_address_cb(data, 0, NULL);
3447 static void account_activated(GtkMenuItem *menuitem, gpointer data)
3449 Compose *compose = (Compose *)data;
3453 ac = (PrefsAccount *)gtk_object_get_user_data(GTK_OBJECT(menuitem));
3454 g_return_if_fail(ac != NULL);
3456 if (ac != compose->account) {
3457 compose->account = ac;
3458 compose_set_title(compose);
3462 static void attach_selected(GtkCList *clist, gint row, gint column,
3463 GdkEvent *event, gpointer data)
3465 Compose *compose = (Compose *)data;
3467 if (event && event->type == GDK_2BUTTON_PRESS)
3468 compose_attach_property(compose);
3471 static void attach_button_pressed(GtkWidget *widget, GdkEventButton *event,
3474 Compose *compose = (Compose *)data;
3475 GtkCList *clist = GTK_CLIST(compose->attach_clist);
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,
3487 gtk_clist_select_row(clist, row, column);
3488 gtkut_clist_set_focus_row(clist, row);
3491 gtk_menu_popup(GTK_MENU(compose->popupmenu), NULL, NULL,
3492 NULL, NULL, event->button, event->time);
3496 static void attach_key_pressed(GtkWidget *widget, GdkEventKey *event,
3499 Compose *compose = (Compose *)data;
3503 switch (event->keyval) {
3505 compose_attach_remove_selected(compose);
3510 static void compose_send_cb(gpointer data, guint action, GtkWidget *widget)
3512 Compose *compose = (Compose *)data;
3515 val = compose_send(compose);
3517 if (val == 0) gtk_widget_destroy(compose->window);
3520 static void compose_send_later_cb(gpointer data, guint action,
3523 Compose *compose = (Compose *)data;
3525 gchar *to, *newsgroups;
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."));
3534 g_snprintf(tmp, 22, "%s%ctmpmsg%d",
3535 g_get_tmp_dir(), G_DIR_SEPARATOR, (gint)compose);
3537 if (prefs_common.linewrap_at_send)
3538 compose_wrap_line(compose);
3540 if (compose_write_to_file(compose, tmp, FALSE) < 0 ||
3541 compose_queue(compose, tmp) < 0) {
3542 alertpanel_error(_("Can't queue the message."));
3546 if (prefs_common.savemsg) {
3547 if (compose_save_to_outbox(compose, tmp) < 0)
3549 (_("Can't save the message to outbox."));
3552 if (unlink(tmp) < 0)
3553 FILE_OP_ERROR(tmp, "unlink");
3555 gtk_widget_destroy(compose->window);
3558 static void compose_draft_cb(gpointer data, guint action, GtkWidget *widget)
3560 Compose *compose = (Compose *)data;
3564 draft = folder_get_default_draft();
3565 folder_item_scan(draft);
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"));
3573 tmp = g_strdup_printf("%s%cdraft.%d", g_get_tmp_dir(),
3574 G_DIR_SEPARATOR, (gint)compose);
3576 if (compose_write_to_file(compose, tmp, TRUE) < 0) {
3581 if (folder_item_add_msg(draft, tmp) < 0) {
3590 //folderview_scan_folder_a(DRAFT_DIR, TRUE);
3592 gtk_widget_destroy(compose->window);
3595 static void compose_attach_cb(gpointer data, guint action, GtkWidget *widget)
3597 Compose *compose = (Compose *)data;
3600 file = filesel_select_file(_("Select file"), NULL);
3603 compose_attach_append(compose, file, MIME_UNKNOWN);
3606 static void compose_insert_file_cb(gpointer data, guint action,
3609 Compose *compose = (Compose *)data;
3612 file = filesel_select_file(_("Select file"), NULL);
3615 compose_insert_file(compose, file);
3618 static gint compose_delete_cb(GtkWidget *widget, GdkEventAny *event,
3621 compose_close_cb(data, 0, NULL);
3625 static void compose_close_cb(gpointer data, guint action, GtkWidget *widget)
3627 Compose *compose = (Compose *)data;
3630 if (compose->exteditor_tag != -1) {
3631 if (!compose_ext_editor_kill(compose))
3635 if (compose->modified) {
3636 val = alertpanel(_("Discard message"),
3637 _("This message has been modified. discard it?"),
3638 _("Discard"), _("to Draft"), _("Cancel"));
3641 case G_ALERTDEFAULT:
3643 case G_ALERTALTERNATE:
3644 compose_draft_cb(data, 0, NULL);
3651 gtk_widget_destroy(compose->window);
3654 static void compose_address_cb(gpointer data, guint action, GtkWidget *widget)
3656 Compose *compose = (Compose *)data;
3658 addressbook_open(compose);
3661 static void compose_ext_editor_cb(gpointer data, guint action,
3664 Compose *compose = (Compose *)data;
3666 compose_exec_ext_editor(compose);
3669 static void compose_destroy_cb(GtkWidget *widget, Compose *compose)
3671 compose_destroy(compose);
3674 static void compose_cut_cb(Compose *compose)
3676 if (compose->focused_editable &&
3677 GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
3678 gtk_editable_cut_clipboard
3679 (GTK_EDITABLE(compose->focused_editable));
3682 static void compose_copy_cb(Compose *compose)
3684 if (compose->focused_editable &&
3685 GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
3686 gtk_editable_copy_clipboard
3687 (GTK_EDITABLE(compose->focused_editable));
3690 static void compose_paste_cb(Compose *compose)
3692 if (compose->focused_editable &&
3693 GTK_WIDGET_HAS_FOCUS(compose->focused_editable))
3694 gtk_editable_paste_clipboard
3695 (GTK_EDITABLE(compose->focused_editable));
3698 static void compose_allsel_cb(Compose *compose)
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);
3706 static void compose_grab_focus_cb(GtkWidget *widget, Compose *compose)
3708 if (GTK_IS_EDITABLE(widget))
3709 compose->focused_editable = widget;
3712 static void compose_changed_cb(GtkEditable *editable, Compose *compose)
3714 if (compose->modified == FALSE) {
3715 compose->modified = TRUE;
3716 compose_set_title(compose);
3720 static void compose_button_press_cb(GtkWidget *widget, GdkEventButton *event,
3723 gtk_text_set_point(GTK_TEXT(widget),
3724 gtk_editable_get_position(GTK_EDITABLE(widget)));
3728 static void compose_key_press_cb(GtkWidget *widget, GdkEventKey *event,
3731 gtk_text_set_point(GTK_TEXT(widget),
3732 gtk_editable_get_position(GTK_EDITABLE(widget)));
3736 static void compose_toggle_to_cb(gpointer data, guint action,
3739 Compose *compose = (Compose *)data;
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;
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;
3754 if (addressbook_get_target_compose() == compose)
3755 addressbook_set_target_compose(compose);
3758 static void compose_toggle_cc_cb(gpointer data, guint action,
3761 Compose *compose = (Compose *)data;
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;
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;
3776 if (addressbook_get_target_compose() == compose)
3777 addressbook_set_target_compose(compose);
3780 static void compose_toggle_bcc_cb(gpointer data, guint action,
3783 Compose *compose = (Compose *)data;
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;
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;
3798 if (addressbook_get_target_compose() == compose)
3799 addressbook_set_target_compose(compose);
3802 static void compose_toggle_replyto_cb(gpointer data, guint action,
3805 Compose *compose = (Compose *)data;
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;
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;
3821 static void compose_toggle_followupto_cb(gpointer data, guint action,
3824 Compose *compose = (Compose *)data;
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;
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;
3840 static void compose_toggle_attach_cb(gpointer data, guint action,
3843 Compose *compose = (Compose *)data;
3845 if (GTK_CHECK_MENU_ITEM(widget)->active) {
3846 gtk_widget_ref(compose->edit_vbox);
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,
3853 gtk_widget_show(compose->paned);
3855 gtk_widget_unref(compose->edit_vbox);
3856 gtk_widget_unref(compose->paned);
3858 compose->use_attach = TRUE;
3860 gtk_widget_ref(compose->paned);
3861 gtk_widget_ref(compose->edit_vbox);
3863 gtk_container_remove(GTK_CONTAINER(compose->vbox2),
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);
3870 gtk_widget_unref(compose->edit_vbox);
3872 compose->use_attach = FALSE;
3877 static void compose_toggle_sign_cb(gpointer data, guint action,
3880 Compose *compose = (Compose *)data;
3882 if (GTK_CHECK_MENU_ITEM(widget)->active)
3883 compose->use_signing = TRUE;
3885 compose->use_signing = FALSE;
3888 static void compose_toggle_encrypt_cb(gpointer data, guint action,
3891 Compose *compose = (Compose *)data;
3893 if (GTK_CHECK_MENU_ITEM(widget)->active)
3894 compose->use_encryption = TRUE;
3896 compose->use_encryption = FALSE;
3898 #endif /* USE_GPGME */
3900 static void compose_toggle_ruler_cb(gpointer data, guint action,
3903 Compose *compose = (Compose *)data;
3905 if (GTK_CHECK_MENU_ITEM(widget)->active) {
3906 gtk_widget_show(compose->ruler_hbox);
3907 prefs_common.show_ruler = TRUE;
3909 gtk_widget_hide(compose->ruler_hbox);
3910 gtk_widget_queue_resize(compose->edit_vbox);
3911 prefs_common.show_ruler = FALSE;
3915 static void compose_attach_drag_received_cb (GtkWidget *widget,
3916 GdkDragContext *drag_context,
3919 GtkSelectionData *data,
3924 Compose *compose = (Compose *)user_data;
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,
3931 list_free_strings(list);
3935 static void compose_insert_drag_received_cb (GtkWidget *widget,
3936 GdkDragContext *drag_context,
3939 GtkSelectionData *data,
3944 Compose *compose = (Compose *)user_data;
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);
3954 static void to_activated(GtkWidget *widget, Compose *compose)
3956 if (GTK_WIDGET_VISIBLE(compose->newsgroups_entry))
3957 gtk_widget_grab_focus(compose->newsgroups_entry);
3959 gtk_widget_grab_focus(compose->subject_entry);
3962 static void newsgroups_activated(GtkWidget *widget, Compose *compose)
3964 gtk_widget_grab_focus(compose->subject_entry);
3967 static void subject_activated(GtkWidget *widget, Compose *compose)
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);
3978 gtk_widget_grab_focus(compose->text);
3981 static void cc_activated(GtkWidget *widget, Compose *compose)
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);
3990 gtk_widget_grab_focus(compose->text);
3993 static void bcc_activated(GtkWidget *widget, Compose *compose)
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);
4000 gtk_widget_grab_focus(compose->text);
4003 static void replyto_activated(GtkWidget *widget, Compose *compose)
4005 if (GTK_WIDGET_VISIBLE(compose->followup_entry))
4006 gtk_widget_grab_focus(compose->followup_entry);
4008 gtk_widget_grab_focus(compose->text);
4011 static void followupto_activated(GtkWidget *widget, Compose *compose)
4013 gtk_widget_grab_focus(compose->text);