2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <gtk/gtkvbox.h>
31 #include <gtk/gtkscrolledwindow.h>
32 #include <gtk/gtksignal.h>
42 # include <compface.h>
46 #define XPM_XFACE_HEIGHT (HEIGHT + 3) /* 3 = 1 header + 2 colors */
50 #include "summaryview.h"
51 #include "procheader.h"
52 #include "prefs_common.h"
60 #include "addressbook.h"
61 #include "displayheader.h"
64 #include "alertpanel.h"
66 #include "image_viewer.h"
69 #include "inputdialog.h"
72 gint previousquotelevel = -1;
74 static GdkColor quote_colors[3] = {
75 {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
76 {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
77 {(gulong)0, (gushort)0, (gushort)0, (gushort)0}
80 static GdkColor quote_bgcolors[3] = {
81 {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
82 {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
83 {(gulong)0, (gushort)0, (gushort)0, (gushort)0}
85 static GdkColor signature_color = {
92 static GdkColor uri_color = {
99 static GdkColor emphasis_color = {
106 static GdkCursor *hand_cursor = NULL;
107 static GdkCursor *text_cursor = NULL;
108 static GdkCursor *watch_cursor= NULL;
110 #define TEXTVIEW_STATUSBAR_PUSH(textview, str) \
111 { if (textview->messageview->statusbar) \
112 gtk_statusbar_push(GTK_STATUSBAR(textview->messageview->statusbar), \
113 textview->messageview->statusbar_cid, str); \
116 #define TEXTVIEW_STATUSBAR_POP(textview) \
117 { if (textview->messageview->statusbar) \
118 gtk_statusbar_pop(GTK_STATUSBAR(textview->messageview->statusbar), \
119 textview->messageview->statusbar_cid); \
122 static void textview_show_ertf (TextView *textview,
124 CodeConverter *conv);
125 static void textview_add_part (TextView *textview,
127 static void textview_add_parts (TextView *textview,
129 static void textview_write_body (TextView *textview,
131 static void textview_show_html (TextView *textview,
133 CodeConverter *conv);
135 static void textview_write_line (TextView *textview,
138 gboolean do_quote_folding);
139 static void textview_write_link (TextView *textview,
142 CodeConverter *conv);
144 static GPtrArray *textview_scan_header (TextView *textview,
146 static void textview_show_header (TextView *textview,
149 static gint textview_key_pressed (GtkWidget *widget,
152 static gboolean textview_motion_notify (GtkWidget *widget,
153 GdkEventMotion *motion,
155 static gboolean textview_leave_notify (GtkWidget *widget,
156 GdkEventCrossing *event,
158 static gboolean textview_visibility_notify (GtkWidget *widget,
159 GdkEventVisibility *event,
161 static void textview_uri_update (TextView *textview,
164 static gboolean textview_get_uri_range (TextView *textview,
167 GtkTextIter *start_iter,
168 GtkTextIter *end_iter);
169 static ClickableText *textview_get_uri_from_range (TextView *textview,
172 GtkTextIter *start_iter,
173 GtkTextIter *end_iter);
174 static ClickableText *textview_get_uri (TextView *textview,
177 static gboolean textview_uri_button_pressed (GtkTextTag *tag,
183 static gboolean textview_uri_security_check (TextView *textview,
185 static void textview_uri_list_remove_all (GSList *uri_list);
187 static void textview_toggle_quote (TextView *textview,
190 gboolean expand_only);
192 static void open_uri_cb (TextView *textview,
195 static void copy_uri_cb (TextView *textview,
198 static void add_uri_to_addrbook_cb (TextView *textview,
201 static void mail_to_uri_cb (TextView *textview,
204 static void copy_mail_to_uri_cb (TextView *textview,
207 static void save_file_cb (TextView *textview,
210 static void open_image_cb (TextView *textview,
214 static GtkItemFactoryEntry textview_link_popup_entries[] =
216 {N_("/_Open with Web browser"), NULL, open_uri_cb, 0, NULL},
217 {N_("/Copy this _link"), NULL, copy_uri_cb, 0, NULL},
220 static GtkItemFactoryEntry textview_mail_popup_entries[] =
222 {N_("/Compose _new message"), NULL, mail_to_uri_cb, 0, NULL},
223 {N_("/Add to _address book"), NULL, add_uri_to_addrbook_cb, 0, NULL},
224 {N_("/Copy this add_ress"), NULL, copy_mail_to_uri_cb, 0, NULL},
227 static GtkItemFactoryEntry textview_file_popup_entries[] =
229 {N_("/_Open image"), NULL, open_image_cb, 0, NULL},
230 {N_("/_Save image..."), NULL, save_file_cb, 0, NULL},
233 static void scrolled_cb (GtkAdjustment *adj, TextView *textview)
239 if (textview->image) {
241 x1 = textview->text->allocation.width - WIDTH - 5;
242 gtk_text_view_buffer_to_window_coords(
243 GTK_TEXT_VIEW(textview->text),
244 GTK_TEXT_WINDOW_TEXT, x1, 5, &x, &y);
245 gtk_text_view_move_child(GTK_TEXT_VIEW(textview->text),
246 textview->image, x1, y);
250 static void textview_size_allocate_cb (GtkWidget *widget,
251 GtkAllocation *allocation,
254 scrolled_cb(NULL, (TextView *)data);
257 TextView *textview_create(void)
261 GtkWidget *scrolledwin;
263 GtkTextBuffer *buffer;
264 GtkClipboard *clipboard;
265 GtkItemFactory *link_popupfactory, *mail_popupfactory, *file_popupfactory;
266 GtkWidget *link_popupmenu, *mail_popupmenu, *file_popupmenu;
270 debug_print("Creating text view...\n");
271 textview = g_new0(TextView, 1);
273 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
274 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
275 GTK_POLICY_AUTOMATIC,
276 GTK_POLICY_AUTOMATIC);
277 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
279 gtk_widget_set_size_request
280 (scrolledwin, prefs_common.mainview_width, -1);
282 /* create GtkSText widgets for single-byte and multi-byte character */
283 text = gtk_text_view_new();
284 gtk_widget_add_events(text, GDK_LEAVE_NOTIFY_MASK);
285 gtk_widget_show(text);
286 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
287 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD_CHAR);
288 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(text), FALSE);
289 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
290 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
292 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
293 clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
294 gtk_text_buffer_add_selection_clipboard(buffer, clipboard);
296 gtk_widget_ensure_style(text);
298 gtk_widget_ref(scrolledwin);
300 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
302 g_signal_connect(G_OBJECT(text), "key-press-event",
303 G_CALLBACK(textview_key_pressed), textview);
304 g_signal_connect(G_OBJECT(text), "motion-notify-event",
305 G_CALLBACK(textview_motion_notify), textview);
306 g_signal_connect(G_OBJECT(text), "leave-notify-event",
307 G_CALLBACK(textview_leave_notify), textview);
308 g_signal_connect(G_OBJECT(text), "visibility-notify-event",
309 G_CALLBACK(textview_visibility_notify), textview);
310 adj = gtk_scrolled_window_get_vadjustment(
311 GTK_SCROLLED_WINDOW(scrolledwin));
312 g_signal_connect(G_OBJECT(adj), "value-changed",
313 G_CALLBACK(scrolled_cb), textview);
314 g_signal_connect(G_OBJECT(text), "size_allocate",
315 G_CALLBACK(textview_size_allocate_cb),
319 gtk_widget_show(scrolledwin);
321 vbox = gtk_vbox_new(FALSE, 0);
322 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
324 gtk_widget_show(vbox);
326 n_entries = sizeof(textview_link_popup_entries) /
327 sizeof(textview_link_popup_entries[0]);
328 link_popupmenu = menu_create_items(textview_link_popup_entries, n_entries,
329 "<UriPopupMenu>", &link_popupfactory,
332 n_entries = sizeof(textview_mail_popup_entries) /
333 sizeof(textview_mail_popup_entries[0]);
334 mail_popupmenu = menu_create_items(textview_mail_popup_entries, n_entries,
335 "<UriPopupMenu>", &mail_popupfactory,
338 n_entries = sizeof(textview_file_popup_entries) /
339 sizeof(textview_file_popup_entries[0]);
340 file_popupmenu = menu_create_items(textview_file_popup_entries, n_entries,
341 "<FilePopupMenu>", &file_popupfactory,
344 textview->vbox = vbox;
345 textview->scrolledwin = scrolledwin;
346 textview->text = text;
347 textview->uri_list = NULL;
348 textview->body_pos = 0;
349 textview->show_all_headers = FALSE;
350 textview->last_buttonpress = GDK_NOTHING;
351 textview->link_popup_menu = link_popupmenu;
352 textview->link_popup_factory = link_popupfactory;
353 textview->mail_popup_menu = mail_popupmenu;
354 textview->mail_popup_factory = mail_popupfactory;
355 textview->file_popup_menu = file_popupmenu;
356 textview->file_popup_factory = file_popupfactory;
357 textview->image = NULL;
361 static void textview_create_tags(GtkTextView *text, TextView *textview)
363 GtkTextBuffer *buffer;
364 GtkTextTag *tag, *qtag;
365 static PangoFontDescription *font_desc, *bold_font_desc;
368 font_desc = pango_font_description_from_string
371 if (!bold_font_desc) {
372 bold_font_desc = pango_font_description_from_string
374 pango_font_description_set_weight
375 (bold_font_desc, PANGO_WEIGHT_BOLD);
378 buffer = gtk_text_view_get_buffer(text);
380 gtk_text_buffer_create_tag(buffer, "header",
381 "pixels-above-lines", 0,
382 "pixels-above-lines-set", TRUE,
383 "pixels-below-lines", 0,
384 "pixels-below-lines-set", TRUE,
385 "font-desc", font_desc,
387 "left-margin-set", TRUE,
389 gtk_text_buffer_create_tag(buffer, "header_title",
390 "font-desc", bold_font_desc,
392 tag = gtk_text_buffer_create_tag(buffer, "hlink",
393 "pixels-above-lines", 0,
394 "pixels-above-lines-set", TRUE,
395 "pixels-below-lines", 0,
396 "pixels-below-lines-set", TRUE,
397 "font-desc", font_desc,
399 "left-margin-set", TRUE,
400 "foreground-gdk", &uri_color,
402 g_signal_connect(G_OBJECT(tag), "event",
403 G_CALLBACK(textview_uri_button_pressed), textview);
404 if (prefs_common.enable_bgcolor) {
405 gtk_text_buffer_create_tag(buffer, "quote0",
406 "foreground-gdk", "e_colors[0],
407 "paragraph-background-gdk", "e_bgcolors[0],
409 gtk_text_buffer_create_tag(buffer, "quote1",
410 "foreground-gdk", "e_colors[1],
411 "paragraph-background-gdk", "e_bgcolors[1],
413 gtk_text_buffer_create_tag(buffer, "quote2",
414 "foreground-gdk", "e_colors[2],
415 "paragraph-background-gdk", "e_bgcolors[2],
418 gtk_text_buffer_create_tag(buffer, "quote0",
419 "foreground-gdk", "e_colors[0],
421 gtk_text_buffer_create_tag(buffer, "quote1",
422 "foreground-gdk", "e_colors[1],
424 gtk_text_buffer_create_tag(buffer, "quote2",
425 "foreground-gdk", "e_colors[2],
428 gtk_text_buffer_create_tag(buffer, "emphasis",
429 "foreground-gdk", &emphasis_color,
431 gtk_text_buffer_create_tag(buffer, "signature",
432 "foreground-gdk", &signature_color,
434 tag = gtk_text_buffer_create_tag(buffer, "link",
435 "foreground-gdk", &uri_color,
437 qtag = gtk_text_buffer_create_tag(buffer, "qlink",
439 gtk_text_buffer_create_tag(buffer, "link-hover",
440 "underline", PANGO_UNDERLINE_SINGLE,
442 g_signal_connect(G_OBJECT(qtag), "event",
443 G_CALLBACK(textview_uri_button_pressed), textview);
444 g_signal_connect(G_OBJECT(tag), "event",
445 G_CALLBACK(textview_uri_button_pressed), textview);
448 void textview_init(TextView *textview)
451 hand_cursor = gdk_cursor_new(GDK_HAND2);
453 text_cursor = gdk_cursor_new(GDK_XTERM);
455 watch_cursor = gdk_cursor_new(GDK_WATCH);
457 textview_reflect_prefs(textview);
458 textview_set_all_headers(textview, FALSE);
459 textview_set_font(textview, NULL);
460 textview_create_tags(GTK_TEXT_VIEW(textview->text), textview);
463 #if GTK_CHECK_VERSION(2, 8, 0)
464 #define CHANGE_TAG_COLOR(tagname, colorfg, colorbg) { \
465 tag = gtk_text_tag_table_lookup(tags, tagname); \
467 g_object_set(G_OBJECT(tag), "foreground-gdk", colorfg, "paragraph-background-gdk", colorbg, NULL); \
470 #define CHANGE_TAG_COLOR(tagname, colorfg, colorbg) { \
471 tag = gtk_text_tag_table_lookup(tags, tagname); \
473 g_object_set(G_OBJECT(tag), "foreground-gdk", colorfg, NULL); \
477 static void textview_update_message_colors(TextView *textview)
479 GdkColor black = {0, 0, 0, 0};
480 GdkColor colored_emphasis = {0, 0, 0, 0xcfff};
481 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
483 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
484 GtkTextTag *tag = NULL;
486 quote_bgcolors[0] = quote_bgcolors[1] = quote_bgcolors[2] = black;
487 quote_colors[0] = quote_colors[1] = quote_colors[2] =
488 uri_color = emphasis_color = signature_color = black;
490 if (prefs_common.enable_color) {
491 /* grab the quote colors, converting from an int to a GdkColor */
492 gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_col,
494 gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_col,
496 gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_col,
498 gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
500 gtkut_convert_int_to_gdk_color(prefs_common.signature_col,
502 emphasis_color = colored_emphasis;
504 if (prefs_common.enable_color && prefs_common.enable_bgcolor) {
505 gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_bgcol,
507 gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_bgcol,
509 gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_bgcol,
511 CHANGE_TAG_COLOR("quote0", "e_colors[0], "e_bgcolors[0]);
512 CHANGE_TAG_COLOR("quote1", "e_colors[1], "e_bgcolors[1]);
513 CHANGE_TAG_COLOR("quote2", "e_colors[2], "e_bgcolors[2]);
515 CHANGE_TAG_COLOR("quote0", "e_colors[0], NULL);
516 CHANGE_TAG_COLOR("quote1", "e_colors[1], NULL);
517 CHANGE_TAG_COLOR("quote2", "e_colors[2], NULL);
520 CHANGE_TAG_COLOR("emphasis", &emphasis_color, NULL);
521 CHANGE_TAG_COLOR("signature", &signature_color, NULL);
522 CHANGE_TAG_COLOR("link", &uri_color, NULL);
523 CHANGE_TAG_COLOR("link-hover", &uri_color, NULL);
525 #undef CHANGE_TAG_COLOR
527 void textview_reflect_prefs(TextView *textview)
529 textview_set_font(textview, NULL);
530 textview_update_message_colors(textview);
531 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview->text),
532 prefs_common.textview_cursor_visible);
535 void textview_show_message(TextView *textview, MimeInfo *mimeinfo,
538 textview->loading = TRUE;
539 textview->stop_loading = FALSE;
541 textview_clear(textview);
543 textview_add_parts(textview, mimeinfo);
545 textview_set_position(textview, 0);
547 textview->loading = FALSE;
548 textview->stop_loading = FALSE;
551 void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
554 g_return_if_fail(mimeinfo != NULL);
555 g_return_if_fail(fp != NULL);
557 if ((mimeinfo->type == MIMETYPE_MULTIPART) ||
558 ((mimeinfo->type == MIMETYPE_MESSAGE) && !g_ascii_strcasecmp(mimeinfo->subtype, "rfc822"))) {
559 textview->loading = TRUE;
560 textview->stop_loading = FALSE;
562 textview_clear(textview);
563 textview_add_parts(textview, mimeinfo);
565 textview->loading = FALSE;
566 textview->stop_loading = FALSE;
570 textview->loading = TRUE;
571 textview->stop_loading = FALSE;
573 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0)
576 textview_clear(textview);
578 if (mimeinfo->type == MIMETYPE_MULTIPART)
579 textview_add_parts(textview, mimeinfo);
581 textview_write_body(textview, mimeinfo);
583 textview->loading = FALSE;
584 textview->stop_loading = FALSE;
588 static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
591 GtkTextBuffer *buffer;
592 GtkTextIter iter, start_iter;
594 GPtrArray *headers = NULL;
600 g_return_if_fail(mimeinfo != NULL);
601 text = GTK_TEXT_VIEW(textview->text);
602 buffer = gtk_text_view_get_buffer(text);
603 charcount = gtk_text_buffer_get_char_count(buffer);
604 gtk_text_buffer_get_end_iter(buffer, &iter);
606 if (textview->stop_loading) {
609 if (mimeinfo->type == MIMETYPE_MULTIPART) {
614 previousquotelevel = -1;
616 if ((mimeinfo->type == MIMETYPE_MESSAGE) && !g_ascii_strcasecmp(mimeinfo->subtype, "rfc822")) {
619 fp = g_fopen(mimeinfo->data.filename, "rb");
620 fseek(fp, mimeinfo->offset, SEEK_SET);
621 headers = textview_scan_header(textview, fp);
624 gtk_text_buffer_insert(buffer, &iter, "\n", 1);
625 textview_show_header(textview, headers);
626 procheader_header_array_destroy(headers);
633 name = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
634 content_type = procmime_get_content_type_str(mimeinfo->type,
637 name = procmime_mimeinfo_get_parameter(mimeinfo, "name");
639 g_snprintf(buf, sizeof(buf), _("[%s %s (%d bytes)]"),
640 name, content_type, mimeinfo->length);
642 g_snprintf(buf, sizeof(buf), _("[%s (%d bytes)]"),
643 content_type, mimeinfo->length);
645 g_free(content_type);
647 if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT
648 || (mimeinfo->disposition == DISPOSITIONTYPE_INLINE &&
649 mimeinfo->type != MIMETYPE_TEXT)) {
650 gtk_text_buffer_insert(buffer, &iter, "\n", 1);
651 TEXTVIEW_INSERT_LINK(buf, "sc://select_attachment", mimeinfo);
652 gtk_text_buffer_insert(buffer, &iter, " \n", -1);
653 if (mimeinfo->type == MIMETYPE_IMAGE &&
654 prefs_common.inline_img ) {
656 GError *error = NULL;
661 START_TIMING("inserting image");
663 filename = procmime_get_tmp_file_name(mimeinfo);
665 if ((err = procmime_get_part(filename, mimeinfo)) < 0) {
666 g_warning("Can't get the image file.(%s)", strerror(-err));
672 if (!prefs_common.resize_img) {
673 pixbuf = gdk_pixbuf_new_from_file(filename, &error);
676 gdk_pixbuf_get_file_info(filename, &w, &h);
677 if (textview->scrolledwin->allocation.width - 100 > 0 &&
678 w > textview->scrolledwin->allocation.width - 100)
679 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
680 textview->scrolledwin->allocation.width - 100,
683 pixbuf = gdk_pixbuf_new_from_file(filename, &error);
686 g_warning("%s\n", error->message);
690 g_warning("Can't load the image.");
696 uri_str = g_filename_to_uri(filename, NULL, NULL);
698 uri = g_new0(ClickableText, 1);
700 uri->start = gtk_text_iter_get_offset(&iter);
702 gtk_text_buffer_insert_pixbuf(buffer, &iter, pixbuf);
704 uri->end = uri->start + 1;
705 uri->filename = procmime_get_part_file_name(mimeinfo);
707 g_slist_prepend(textview->uri_list, uri);
709 gtk_text_buffer_insert(buffer, &iter, " ", 1);
710 gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, uri->start);
711 gtk_text_buffer_apply_tag_by_name(buffer, "link",
714 gtk_text_buffer_insert_pixbuf(buffer, &iter, pixbuf);
715 gtk_text_buffer_insert(buffer, &iter, " ", 1);
718 g_object_unref(pixbuf);
723 } else if (mimeinfo->type == MIMETYPE_TEXT) {
724 if (prefs_common.display_header && (charcount > 0))
725 gtk_text_buffer_insert(buffer, &iter, "\n", 1);
727 textview_write_body(textview, mimeinfo);
732 static void recursive_add_parts(TextView *textview, GNode *node)
738 mimeinfo = (MimeInfo *) node->data;
740 textview_add_part(textview, mimeinfo);
742 if ((mimeinfo->type != MIMETYPE_MULTIPART) &&
743 (mimeinfo->type != MIMETYPE_MESSAGE)) {
747 if (g_ascii_strcasecmp(mimeinfo->subtype, "alternative") == 0) {
748 GNode * prefered_body;
756 prefered_body = NULL;
759 for (iter = g_node_first_child(node) ; iter != NULL ;
760 iter = g_node_next_sibling(iter)) {
765 submime = (MimeInfo *) iter->data;
766 if (submime->type == MIMETYPE_TEXT)
769 if (submime->subtype != NULL) {
770 if (g_ascii_strcasecmp(submime->subtype, "plain") == 0)
774 if (score > prefered_score) {
775 prefered_score = score;
776 prefered_body = iter;
780 if (prefered_body != NULL) {
781 recursive_add_parts(textview, prefered_body);
785 for (iter = g_node_first_child(node) ; iter != NULL ;
786 iter = g_node_next_sibling(iter)) {
787 recursive_add_parts(textview, iter);
793 static void textview_add_parts(TextView *textview, MimeInfo *mimeinfo)
795 g_return_if_fail(mimeinfo != NULL);
797 recursive_add_parts(textview, mimeinfo->node);
800 void textview_show_error(TextView *textview)
803 GtkTextBuffer *buffer;
806 textview_set_font(textview, NULL);
807 textview_clear(textview);
809 text = GTK_TEXT_VIEW(textview->text);
810 buffer = gtk_text_view_get_buffer(text);
811 gtk_text_buffer_get_start_iter(buffer, &iter);
813 TEXTVIEW_INSERT(_("\n"
814 " This message can't be displayed.\n"
815 " This is probably due to a network error.\n"
818 TEXTVIEW_INSERT_LINK(_("'View Log'"), "sc://view_log", NULL);
819 TEXTVIEW_INSERT(_(" in the Tools menu for more information."));
820 textview_show_icon(textview, GTK_STOCK_DIALOG_ERROR);
824 void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
827 GtkTextBuffer *buffer;
830 if (!partinfo) return;
832 textview_set_font(textview, NULL);
833 textview_clear(textview);
835 text = GTK_TEXT_VIEW(textview->text);
836 buffer = gtk_text_view_get_buffer(text);
837 gtk_text_buffer_get_start_iter(buffer, &iter);
839 TEXTVIEW_INSERT("\n");
840 TEXTVIEW_INSERT(_(" The following can be performed on this part by\n"));
841 TEXTVIEW_INSERT(_(" right-clicking the icon or list item:\n"));
843 TEXTVIEW_INSERT(_(" - To save, select "));
844 TEXTVIEW_INSERT_LINK(_("'Save as...'"), "sc://save_as", NULL);
845 TEXTVIEW_INSERT(_(" (Shortcut key: 'y')\n"));
846 TEXTVIEW_INSERT(_(" - To display as text, select "));
847 TEXTVIEW_INSERT_LINK(_("'Display as text'"), "sc://display_as_text", NULL);
848 TEXTVIEW_INSERT(_(" (Shortcut key: 't')\n"));
849 TEXTVIEW_INSERT(_(" - To open with an external program, select "));
850 TEXTVIEW_INSERT_LINK(_("'Open'"), "sc://open", NULL);
851 TEXTVIEW_INSERT(_(" (Shortcut key: 'l')\n"));
852 TEXTVIEW_INSERT(_(" (alternately double-click, or click the middle "));
853 TEXTVIEW_INSERT(_("mouse button)\n"));
854 TEXTVIEW_INSERT(_(" - Or use "));
855 TEXTVIEW_INSERT_LINK(_("'Open with...'"), "sc://open_with", NULL);
856 TEXTVIEW_INSERT(_(" (Shortcut key: 'o')\n"));
857 textview_show_icon(textview, GTK_STOCK_DIALOG_INFO);
860 static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
865 const gchar *charset, *p, *cmd;
869 if (textview->messageview->forced_charset)
870 charset = textview->messageview->forced_charset;
872 charset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
874 textview_set_font(textview, charset);
876 conv = conv_code_converter_new(charset);
878 procmime_force_encoding(textview->messageview->forced_encoding);
880 textview->is_in_signature = FALSE;
882 procmime_decode_content(mimeinfo);
884 if (!g_ascii_strcasecmp(mimeinfo->subtype, "html") &&
885 prefs_common.render_html) {
888 filename = procmime_get_tmp_file_name(mimeinfo);
889 if (procmime_get_part(filename, mimeinfo) == 0) {
890 tmpfp = g_fopen(filename, "rb");
891 textview_show_html(textview, tmpfp, conv);
896 } else if (!g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
899 filename = procmime_get_tmp_file_name(mimeinfo);
900 if (procmime_get_part(filename, mimeinfo) == 0) {
901 tmpfp = g_fopen(filename, "rb");
902 textview_show_ertf(textview, tmpfp, conv);
908 } else if ( g_ascii_strcasecmp(mimeinfo->subtype, "plain") &&
909 (cmd = prefs_common.mime_textviewer) && *cmd &&
910 (p = strchr(cmd, '%')) && *(p + 1) == 's') {
914 fname = procmime_get_tmp_file_name(mimeinfo);
915 if (procmime_get_part(fname, mimeinfo)) goto textview_default;
917 g_snprintf(buf, sizeof(buf), cmd, fname);
918 debug_print("Viewing text content of type: %s (length: %d) "
919 "using %s\n", mimeinfo->subtype, mimeinfo->length, buf);
922 g_snprintf(buf, sizeof(buf),
923 "pipe failed for textview\n\n%s\n", strerror(errno));
924 textview_write_line(textview, buf, conv, TRUE);
925 goto textview_default;
929 g_snprintf(buf, sizeof(buf),
930 "fork failed for textview\n\n%s\n", strerror(errno));
931 textview_write_line(textview, buf, conv, TRUE);
934 goto textview_default;
936 if (pid == 0) { /* child */
939 argv = strsplit_with_quote(buf, " ", 0);
943 rc = execvp(argv[0], argv);
945 printf (_("The command to view attachment "
948 "Exit code %d\n"), buf, rc);
952 tmpfp = fdopen(pfd[0], "rb");
953 while (fgets(buf, sizeof(buf), tmpfp)) {
954 textview_write_line(textview, buf, conv, TRUE);
957 if (lines % 500 == 0)
959 if (textview->stop_loading) {
961 waitpid(pid, pfd, 0);
968 waitpid(pid, pfd, 0);
974 tmpfp = g_fopen(mimeinfo->data.filename, "rb");
975 fseek(tmpfp, mimeinfo->offset, SEEK_SET);
976 debug_print("Viewing text content of type: %s (length: %d)\n", mimeinfo->subtype, mimeinfo->length);
977 while ((ftell(tmpfp) < mimeinfo->offset + mimeinfo->length) &&
978 (fgets(buf, sizeof(buf), tmpfp) != NULL)) {
979 textview_write_line(textview, buf, conv, TRUE);
981 if (lines % 500 == 0)
983 if (textview->stop_loading) {
991 conv_code_converter_destroy(conv);
992 procmime_force_encoding(0);
995 textview->uri_list = g_slist_reverse(textview->uri_list);
996 for (cur = textview->uri_list; cur; cur = cur->next) {
997 ClickableText *uri = (ClickableText *)cur->data;
1000 if (!prefs_common.hide_quotes ||
1001 uri->quote_level+1 < prefs_common.hide_quotes) {
1002 textview_toggle_quote(textview, cur, uri, TRUE);
1004 if (lines % 500 == 0)
1006 if (textview->stop_loading) {
1013 static void textview_show_html(TextView *textview, FILE *fp,
1014 CodeConverter *conv)
1016 SC_HTMLParser *parser;
1020 parser = sc_html_parser_new(fp, conv);
1021 g_return_if_fail(parser != NULL);
1023 while ((str = sc_html_parse(parser)) != NULL) {
1024 if (parser->state == SC_HTML_HREF) {
1025 /* first time : get and copy the URL */
1026 if (parser->href == NULL) {
1027 /* ALF - the claws html parser returns an empty string,
1028 * if still inside an <a>, but already parsed past HREF */
1029 str = strtok(str, " ");
1031 while (str && *str && g_ascii_isspace(*str))
1033 parser->href = g_strdup(str);
1034 /* the URL may (or not) be followed by the
1035 * referenced text */
1036 str = strtok(NULL, "");
1040 textview_write_link(textview, str, parser->href, NULL);
1042 textview_write_line(textview, str, NULL, FALSE);
1044 if (lines % 500 == 0)
1046 if (textview->stop_loading) {
1050 textview_write_line(textview, "\n", NULL, FALSE);
1051 sc_html_parser_destroy(parser);
1054 static void textview_show_ertf(TextView *textview, FILE *fp,
1055 CodeConverter *conv)
1061 parser = ertf_parser_new(fp, conv);
1062 g_return_if_fail(parser != NULL);
1064 while ((str = ertf_parse(parser)) != NULL) {
1065 textview_write_line(textview, str, NULL, FALSE);
1067 if (lines % 500 == 0)
1069 if (textview->stop_loading) {
1074 ertf_parser_destroy(parser);
1077 #define ADD_TXT_POS(bp_, ep_, pti_) \
1078 if ((last->next = alloca(sizeof(struct txtpos))) != NULL) { \
1079 last = last->next; \
1080 last->bp = (bp_); last->ep = (ep_); last->pti = (pti_); \
1081 last->next = NULL; \
1083 g_warning("alloc error scanning URIs\n"); \
1084 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, \
1090 #define ADD_TXT_POS_LATER(bp_, ep_, pti_) \
1091 if ((last->next = alloca(sizeof(struct txtpos))) != NULL) { \
1092 last = last->next; \
1093 last->bp = (bp_); last->ep = (ep_); last->pti = (pti_); \
1094 last->next = NULL; \
1096 g_warning("alloc error scanning URIs\n"); \
1099 /* textview_make_clickable_parts() - colorizes clickable parts */
1100 static void textview_make_clickable_parts(TextView *textview,
1101 const gchar *fg_tag,
1102 const gchar *uri_tag,
1103 const gchar *linebuf,
1106 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1107 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
1109 gchar *mybuf = g_strdup(linebuf);
1111 /* parse table - in order of priority */
1113 const gchar *needle; /* token */
1115 /* token search function */
1116 gchar *(*search) (const gchar *haystack,
1117 const gchar *needle);
1118 /* part parsing function */
1119 gboolean (*parse) (const gchar *start,
1120 const gchar *scanpos,
1124 /* part to URI function */
1125 gchar *(*build_uri) (const gchar *bp,
1129 static struct table parser[] = {
1130 {"http://", strcasestr, get_uri_part, make_uri_string},
1131 {"https://", strcasestr, get_uri_part, make_uri_string},
1132 {"ftp://", strcasestr, get_uri_part, make_uri_string},
1133 {"sftp://", strcasestr, get_uri_part, make_uri_string},
1134 {"www.", strcasestr, get_uri_part, make_http_string},
1135 {"mailto:", strcasestr, get_uri_part, make_uri_string},
1136 {"@", strcasestr, get_email_part, make_email_string}
1138 const gint PARSE_ELEMS = sizeof parser / sizeof parser[0];
1141 const gchar *walk, *bp, *ep;
1144 const gchar *bp, *ep; /* text position */
1145 gint pti; /* index in parse table */
1146 struct txtpos *next; /* next */
1147 } head = {NULL, NULL, 0, NULL}, *last = &head;
1149 if (!g_utf8_validate(linebuf, -1, NULL)) {
1151 mybuf = g_malloc(strlen(linebuf)*2 +1);
1152 conv_localetodisp(mybuf, strlen(linebuf)*2 +1, linebuf);
1155 gtk_text_buffer_get_end_iter(buffer, &iter);
1157 /* parse for clickable parts, and build a list of begin and end positions */
1158 for (walk = mybuf, n = 0;;) {
1159 gint last_index = PARSE_ELEMS;
1160 gchar *scanpos = NULL;
1162 /* FIXME: this looks phony. scanning for anything in the parse table */
1163 for (n = 0; n < PARSE_ELEMS; n++) {
1166 tmp = parser[n].search(walk, parser[n].needle);
1168 if (scanpos == NULL || tmp < scanpos) {
1176 /* check if URI can be parsed */
1177 if (parser[last_index].parse(walk, scanpos, &bp, &ep, hdr)
1178 && (size_t) (ep - bp - 1) > strlen(parser[last_index].needle)) {
1179 ADD_TXT_POS(bp, ep, last_index);
1183 strlen(parser[last_index].needle);
1188 /* colorize this line */
1190 const gchar *normal_text = mybuf;
1193 for (last = head.next; last != NULL;
1194 normal_text = last->ep, last = last->next) {
1196 uri = g_new0(ClickableText, 1);
1197 if (last->bp - normal_text > 0)
1198 gtk_text_buffer_insert_with_tags_by_name
1201 last->bp - normal_text,
1203 uri->uri = parser[last->pti].build_uri(last->bp,
1205 uri->start = gtk_text_iter_get_offset(&iter);
1206 gtk_text_buffer_insert_with_tags_by_name
1207 (buffer, &iter, last->bp, last->ep - last->bp,
1208 uri_tag, fg_tag, NULL);
1209 uri->end = gtk_text_iter_get_offset(&iter);
1210 uri->filename = NULL;
1211 textview->uri_list =
1212 g_slist_prepend(textview->uri_list, uri);
1216 gtk_text_buffer_insert_with_tags_by_name
1217 (buffer, &iter, normal_text, -1, fg_tag, NULL);
1219 gtk_text_buffer_insert_with_tags_by_name
1220 (buffer, &iter, mybuf, -1, fg_tag, NULL);
1225 /* textview_make_clickable_parts() - colorizes clickable parts */
1226 static void textview_make_clickable_parts_later(TextView *textview,
1227 gint start, gint end)
1229 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1230 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
1231 GtkTextIter start_iter, end_iter;
1234 /* parse table - in order of priority */
1236 const gchar *needle; /* token */
1238 /* token search function */
1239 gchar *(*search) (const gchar *haystack,
1240 const gchar *needle);
1241 /* part parsing function */
1242 gboolean (*parse) (const gchar *start,
1243 const gchar *scanpos,
1247 /* part to URI function */
1248 gchar *(*build_uri) (const gchar *bp,
1252 static struct table parser[] = {
1253 {"http://", strcasestr, get_uri_part, make_uri_string},
1254 {"https://", strcasestr, get_uri_part, make_uri_string},
1255 {"ftp://", strcasestr, get_uri_part, make_uri_string},
1256 {"sftp://", strcasestr, get_uri_part, make_uri_string},
1257 {"www.", strcasestr, get_uri_part, make_http_string},
1258 {"mailto:", strcasestr, get_uri_part, make_uri_string},
1259 {"@", strcasestr, get_email_part, make_email_string}
1261 const gint PARSE_ELEMS = sizeof parser / sizeof parser[0];
1264 const gchar *walk, *bp, *ep;
1267 const gchar *bp, *ep; /* text position */
1268 gint pti; /* index in parse table */
1269 struct txtpos *next; /* next */
1270 } head = {NULL, NULL, 0, NULL}, *last = &head;
1272 gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, start);
1273 gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, end);
1274 mybuf = gtk_text_buffer_get_text(buffer, &start_iter, &end_iter, FALSE);
1275 offset = gtk_text_iter_get_offset(&start_iter);
1277 /* parse for clickable parts, and build a list of begin and end positions */
1278 for (walk = mybuf, n = 0;;) {
1279 gint last_index = PARSE_ELEMS;
1280 gchar *scanpos = NULL;
1282 /* FIXME: this looks phony. scanning for anything in the parse table */
1283 for (n = 0; n < PARSE_ELEMS; n++) {
1286 tmp = parser[n].search(walk, parser[n].needle);
1288 if (scanpos == NULL || tmp < scanpos) {
1296 /* check if URI can be parsed */
1297 if (parser[last_index].parse(walk, scanpos, &bp, &ep, FALSE)
1298 && (size_t) (ep - bp - 1) > strlen(parser[last_index].needle)) {
1299 ADD_TXT_POS_LATER(bp, ep, last_index);
1303 strlen(parser[last_index].needle);
1308 /* colorize this line */
1311 for (last = head.next; last != NULL; last = last->next) {
1313 gint start_offset, end_offset;
1316 uri = g_new0(ClickableText, 1);
1317 uri->uri = parser[last->pti].build_uri(last->bp,
1321 old_char = tmp_str[last->ep - mybuf];
1322 tmp_str[last->ep - mybuf] = '\0';
1323 end_offset = g_utf8_strlen(tmp_str, -1);
1324 tmp_str[last->ep - mybuf] = old_char;
1326 old_char = tmp_str[last->bp - mybuf];
1327 tmp_str[last->bp - mybuf] = '\0';
1328 start_offset = g_utf8_strlen(tmp_str, -1);
1329 tmp_str[last->bp - mybuf] = old_char;
1331 gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, start_offset + offset);
1332 gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, end_offset + offset);
1334 uri->start = gtk_text_iter_get_offset(&start_iter);
1336 gtk_text_buffer_apply_tag_by_name(buffer, "link", &start_iter, &end_iter);
1338 uri->end = gtk_text_iter_get_offset(&end_iter);
1339 uri->filename = NULL;
1340 textview->uri_list =
1341 g_slist_prepend(textview->uri_list, uri);
1350 static void textview_write_line(TextView *textview, const gchar *str,
1351 CodeConverter *conv, gboolean do_quote_folding)
1354 GtkTextBuffer *buffer;
1356 gchar buf[BUFFSIZE];
1358 gint quotelevel = -1, real_quotelevel = -1;
1359 gchar quote_tag_str[10];
1361 text = GTK_TEXT_VIEW(textview->text);
1362 buffer = gtk_text_view_get_buffer(text);
1363 gtk_text_buffer_get_end_iter(buffer, &iter);
1366 strncpy2(buf, str, sizeof(buf));
1367 else if (conv_convert(conv, buf, sizeof(buf), str) < 0)
1368 conv_localetodisp(buf, sizeof(buf), str);
1373 /* change color of quotation
1374 >, foo>, _> ... ok, <foo>, foo bar>, foo-> ... ng
1375 Up to 3 levels of quotations are detected, and each
1376 level is colored using a different color. */
1377 if (prefs_common.enable_color
1378 && line_has_quote_char(buf, prefs_common.quote_chars)) {
1379 real_quotelevel = get_quote_level(buf, prefs_common.quote_chars);
1380 quotelevel = real_quotelevel;
1381 /* set up the correct foreground color */
1382 if (quotelevel > 2) {
1383 /* recycle colors */
1384 if (prefs_common.recycle_quote_colors)
1391 if (quotelevel == -1)
1394 g_snprintf(quote_tag_str, sizeof(quote_tag_str),
1395 "quote%d", quotelevel);
1396 fg_color = quote_tag_str;
1399 if (prefs_common.enable_color && (strcmp(buf,"-- \n") == 0 || textview->is_in_signature)) {
1400 fg_color = "signature";
1401 textview->is_in_signature = TRUE;
1404 if (real_quotelevel > -1 && do_quote_folding) {
1405 if (!g_utf8_validate(buf, -1, NULL)) {
1406 gchar *utf8buf = NULL;
1407 utf8buf = g_malloc(BUFFSIZE);
1408 conv_localetodisp(utf8buf, BUFFSIZE, buf);
1409 strncpy2(buf, utf8buf, BUFFSIZE-1);
1413 if ( previousquotelevel != real_quotelevel ) {
1415 uri = g_new0(ClickableText, 1);
1416 uri->uri = g_strdup("");
1417 uri->data = g_strdup(buf);
1418 uri->start = gtk_text_iter_get_offset(&iter);
1419 uri->is_quote = TRUE;
1420 uri->quote_level = real_quotelevel;
1421 uri->fg_color = g_strdup(fg_color);
1423 gtk_text_buffer_insert_with_tags_by_name
1424 (buffer, &iter, " [...]", -1,
1425 "qlink", fg_color, NULL);
1426 uri->end = gtk_text_iter_get_offset(&iter);
1427 gtk_text_buffer_insert(buffer, &iter, " \n", -1);
1429 uri->filename = NULL;
1430 textview->uri_list =
1431 g_slist_prepend(textview->uri_list, uri);
1433 previousquotelevel = real_quotelevel;
1435 GSList *last = textview->uri_list;
1436 ClickableText *lasturi = (ClickableText *)last->data;
1437 gint e_len = 0, n_len = 0;
1439 if (lasturi->is_quote == FALSE) {
1440 previousquotelevel = -1;
1443 e_len = lasturi->data ? strlen(lasturi->data):0;
1444 n_len = strlen(buf);
1445 lasturi->data = g_realloc((gchar *)lasturi->data, e_len + n_len + 1);
1446 strcpy((gchar *)lasturi->data + e_len, buf);
1447 *((gchar *)lasturi->data + e_len + n_len) = '\0';
1450 textview_make_clickable_parts(textview, fg_color, "link", buf, FALSE);
1451 previousquotelevel = -1;
1455 void textview_write_link(TextView *textview, const gchar *str,
1456 const gchar *uri, CodeConverter *conv)
1458 GdkColor *link_color = NULL;
1460 GtkTextBuffer *buffer;
1462 gchar buf[BUFFSIZE];
1464 ClickableText *r_uri;
1466 if (!str || *str == '\0')
1471 while (uri && *uri && g_ascii_isspace(*uri))
1474 text = GTK_TEXT_VIEW(textview->text);
1475 buffer = gtk_text_view_get_buffer(text);
1476 gtk_text_buffer_get_end_iter(buffer, &iter);
1479 strncpy2(buf, str, sizeof(buf));
1480 else if (conv_convert(conv, buf, sizeof(buf), str) < 0)
1481 conv_utf8todisp(buf, sizeof(buf), str);
1483 if (g_utf8_validate(buf, -1, NULL) == FALSE)
1488 gtk_text_buffer_get_end_iter(buffer, &iter);
1489 for (bufp = buf; *bufp != '\0'; bufp = g_utf8_next_char(bufp)) {
1492 ch = g_utf8_get_char(bufp);
1493 if (!g_unichar_isspace(ch))
1497 gtk_text_buffer_insert(buffer, &iter, buf, bufp - buf);
1499 if (prefs_common.enable_color) {
1500 link_color = &uri_color;
1502 r_uri = g_new0(ClickableText, 1);
1503 r_uri->uri = g_strdup(uri);
1504 r_uri->start = gtk_text_iter_get_offset(&iter);
1505 gtk_text_buffer_insert_with_tags_by_name
1506 (buffer, &iter, bufp, -1, "link", NULL);
1507 r_uri->end = gtk_text_iter_get_offset(&iter);
1508 r_uri->filename = NULL;
1509 textview->uri_list = g_slist_prepend(textview->uri_list, r_uri);
1512 static void textview_set_cursor(GdkWindow *window, GdkCursor *cursor)
1514 if (GDK_IS_WINDOW(window))
1515 gdk_window_set_cursor(window, cursor);
1517 void textview_clear(TextView *textview)
1519 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1520 GtkTextBuffer *buffer;
1521 GdkWindow *window = gtk_text_view_get_window(text,
1522 GTK_TEXT_WINDOW_TEXT);
1524 buffer = gtk_text_view_get_buffer(text);
1525 gtk_text_buffer_set_text(buffer, "", -1);
1527 TEXTVIEW_STATUSBAR_POP(textview);
1528 textview_uri_list_remove_all(textview->uri_list);
1529 textview->uri_list = NULL;
1530 textview->uri_hover = NULL;
1532 textview->body_pos = 0;
1533 if (textview->image)
1534 gtk_widget_destroy(textview->image);
1535 textview->image = NULL;
1537 if (textview->messageview->mainwin->cursor_count == 0) {
1538 textview_set_cursor(window, text_cursor);
1540 textview_set_cursor(window, watch_cursor);
1544 void textview_destroy(TextView *textview)
1546 GtkTextBuffer *buffer;
1547 GtkClipboard *clipboard;
1549 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
1550 clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1551 gtk_text_buffer_remove_selection_clipboard(buffer, clipboard);
1553 textview_uri_list_remove_all(textview->uri_list);
1554 textview->uri_list = NULL;
1559 void textview_set_all_headers(TextView *textview, gboolean all_headers)
1561 textview->show_all_headers = all_headers;
1564 #define CHANGE_TAG_FONT(tagname, font) { \
1565 tag = gtk_text_tag_table_lookup(tags, tagname); \
1567 g_object_set(G_OBJECT(tag), "font-desc", font, NULL); \
1570 void textview_set_font(TextView *textview, const gchar *codeset)
1573 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
1574 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
1577 PangoFontDescription *font_desc, *bold_font_desc;
1578 font_desc = pango_font_description_from_string
1580 bold_font_desc = pango_font_description_from_string
1583 gtk_widget_modify_font(textview->text, font_desc);
1584 CHANGE_TAG_FONT("header", font_desc);
1585 pango_font_description_free(font_desc);
1587 if (bold_font_desc) {
1588 pango_font_description_set_weight
1589 (bold_font_desc, PANGO_WEIGHT_BOLD);
1590 CHANGE_TAG_FONT("header_title", bold_font_desc);
1591 pango_font_description_free(bold_font_desc);
1595 if (prefs_common.textfont) {
1596 PangoFontDescription *font_desc;
1598 font_desc = pango_font_description_from_string
1599 (prefs_common.textfont);
1601 gtk_widget_modify_font(textview->text, font_desc);
1602 pango_font_description_free(font_desc);
1605 gtk_text_view_set_pixels_above_lines(GTK_TEXT_VIEW(textview->text),
1606 prefs_common.line_space / 2);
1607 gtk_text_view_set_pixels_below_lines(GTK_TEXT_VIEW(textview->text),
1608 prefs_common.line_space / 2);
1611 void textview_set_text(TextView *textview, const gchar *text)
1614 GtkTextBuffer *buffer;
1616 g_return_if_fail(textview != NULL);
1617 g_return_if_fail(text != NULL);
1619 textview_clear(textview);
1621 view = GTK_TEXT_VIEW(textview->text);
1622 buffer = gtk_text_view_get_buffer(view);
1623 gtk_text_buffer_set_text(buffer, text, strlen(text));
1639 H_ORGANIZATION = 11,
1642 void textview_set_position(TextView *textview, gint pos)
1644 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1646 gtkut_text_view_set_position(text, pos);
1649 static gboolean header_is_internal(Header *header)
1651 const gchar *internal_hdrs[] =
1652 {"AF:", "NF:", "PS:", "SRH:", "SFN:", "DSR:", "MID:",
1653 "CFG:", "PT:", "S:", "RQ:", "SSV:", "NSV:", "SSH:",
1654 "R:", "MAID:", "SCF:", "RMID:", "FMID:", "NAID:",
1655 "X-Claws-Account-Id:", "X-Claws-Sign:", "X-Claws-Encrypt:",
1656 "X-Claws-Privacy-System:", "X-Claws-End-Special-Headers:",
1657 "X-Sylpheed-Account-Id:", "X-Sylpheed-Sign:", "X-Sylpheed-Encrypt:",
1658 "X-Sylpheed-Privacy-System:", "X-Sylpheed-End-Special-Headers:",
1662 for (i = 0; internal_hdrs[i]; i++) {
1663 if (!strcmp(header->name, internal_hdrs[i]))
1669 static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
1671 gchar buf[BUFFSIZE];
1672 GPtrArray *headers, *sorted_headers;
1673 GSList *disphdr_list;
1677 g_return_val_if_fail(fp != NULL, NULL);
1679 if (textview->show_all_headers) {
1680 headers = procheader_get_header_array_asis(fp);
1681 sorted_headers = g_ptr_array_new();
1682 for (i = 0; i < headers->len; i++) {
1683 header = g_ptr_array_index(headers, i);
1684 if (!header_is_internal(header))
1685 g_ptr_array_add(sorted_headers, header);
1687 procheader_header_free(header);
1689 g_ptr_array_free(headers, TRUE);
1690 return sorted_headers;
1693 if (!prefs_common.display_header) {
1694 while (fgets(buf, sizeof(buf), fp) != NULL)
1695 if (buf[0] == '\r' || buf[0] == '\n') break;
1699 headers = procheader_get_header_array_asis(fp);
1701 sorted_headers = g_ptr_array_new();
1703 for (disphdr_list = prefs_common.disphdr_list; disphdr_list != NULL;
1704 disphdr_list = disphdr_list->next) {
1705 DisplayHeaderProp *dp =
1706 (DisplayHeaderProp *)disphdr_list->data;
1708 for (i = 0; i < headers->len; i++) {
1709 header = g_ptr_array_index(headers, i);
1711 if (procheader_headername_equal(header->name,
1714 procheader_header_free(header);
1716 g_ptr_array_add(sorted_headers, header);
1718 g_ptr_array_remove_index(headers, i);
1724 if (prefs_common.show_other_header) {
1725 for (i = 0; i < headers->len; i++) {
1726 header = g_ptr_array_index(headers, i);
1727 if (!header_is_internal(header)) {
1728 g_ptr_array_add(sorted_headers, header);
1730 procheader_header_free(header);
1733 g_ptr_array_free(headers, TRUE);
1735 procheader_header_array_destroy(headers);
1738 return sorted_headers;
1741 static void textview_show_face(TextView *textview)
1743 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1744 MsgInfo *msginfo = textview->messageview->msginfo;
1747 if (prefs_common.display_header_pane
1748 || !prefs_common.display_xface)
1751 if (!msginfo->extradata || !msginfo->extradata->face) {
1755 if (textview->image)
1756 gtk_widget_destroy(textview->image);
1758 textview->image = face_get_from_header(msginfo->extradata->face);
1759 g_return_if_fail(textview->image != NULL);
1761 gtk_widget_show(textview->image);
1763 x = textview->text->allocation.width - WIDTH -5;
1765 gtk_text_view_add_child_in_window(text, textview->image,
1766 GTK_TEXT_WINDOW_TEXT, x, 5);
1768 gtk_widget_show_all(textview->text);
1773 if (textview->image)
1774 gtk_widget_destroy(textview->image);
1775 textview->image = NULL;
1778 void textview_show_icon(TextView *textview, const gchar *stock_id)
1780 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1783 if (textview->image)
1784 gtk_widget_destroy(textview->image);
1786 textview->image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_DIALOG);
1787 g_return_if_fail(textview->image != NULL);
1789 gtk_widget_show(textview->image);
1791 x = textview->text->allocation.width - WIDTH -5;
1793 gtk_text_view_add_child_in_window(text, textview->image,
1794 GTK_TEXT_WINDOW_TEXT, x, 5);
1796 gtk_widget_show_all(textview->text);
1802 #if HAVE_LIBCOMPFACE
1803 static void textview_show_xface(TextView *textview)
1805 MsgInfo *msginfo = textview->messageview->msginfo;
1806 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1808 GdkWindow *window = NULL;
1810 if (prefs_common.display_header_pane
1811 || !prefs_common.display_xface)
1814 if (!msginfo || !msginfo->extradata)
1817 if (msginfo->extradata->face)
1820 if (!msginfo->extradata->xface || strlen(msginfo->extradata->xface) < 5) {
1824 if (textview->image)
1825 gtk_widget_destroy(textview->image);
1827 window = mainwindow_get_mainwindow() ?
1828 mainwindow_get_mainwindow()->window->window :
1829 textview->text->window;
1830 textview->image = xface_get_from_header(msginfo->extradata->xface,
1831 &textview->text->style->white,
1833 g_return_if_fail(textview->image != NULL);
1835 gtk_widget_show(textview->image);
1837 x = textview->text->allocation.width - WIDTH -5;
1839 gtk_text_view_add_child_in_window(text, textview->image,
1840 GTK_TEXT_WINDOW_TEXT, x, 5);
1842 gtk_widget_show_all(textview->text);
1846 if (textview->image)
1847 gtk_widget_destroy(textview->image);
1848 textview->image = NULL;
1853 static void textview_show_header(TextView *textview, GPtrArray *headers)
1855 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1856 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
1861 g_return_if_fail(headers != NULL);
1863 for (i = 0; i < headers->len; i++) {
1864 header = g_ptr_array_index(headers, i);
1865 g_return_if_fail(header->name != NULL);
1867 gtk_text_buffer_get_end_iter (buffer, &iter);
1868 if(prefs_common.trans_hdr == TRUE) {
1869 gchar *hdr = g_strndup(header->name, strlen(header->name) - 1);
1870 gchar *trans_hdr = gettext(hdr);
1871 gtk_text_buffer_insert_with_tags_by_name(buffer,
1872 &iter, trans_hdr, -1,
1873 "header_title", "header", NULL);
1874 gtk_text_buffer_insert_with_tags_by_name(buffer,
1875 &iter, ":", 1, "header_title", "header", NULL);
1878 gtk_text_buffer_insert_with_tags_by_name(buffer,
1879 &iter, header->name,
1880 -1, "header_title", "header", NULL);
1882 if (header->name[strlen(header->name) - 1] != ' ')
1883 gtk_text_buffer_insert_with_tags_by_name
1884 (buffer, &iter, " ", 1,
1885 "header_title", "header", NULL);
1887 if (procheader_headername_equal(header->name, "Subject") ||
1888 procheader_headername_equal(header->name, "From") ||
1889 procheader_headername_equal(header->name, "To") ||
1890 procheader_headername_equal(header->name, "Cc"))
1891 unfold_line(header->body);
1893 if ((procheader_headername_equal(header->name, "X-Mailer") ||
1894 procheader_headername_equal(header->name,
1896 (strstr(header->body, "Claws Mail") != NULL ||
1897 strstr(header->body, "Sylpheed-Claws") != NULL)) {
1898 gtk_text_buffer_get_end_iter (buffer, &iter);
1899 gtk_text_buffer_insert_with_tags_by_name
1900 (buffer, &iter, header->body, -1,
1901 "header", "emphasis", NULL);
1904 procheader_headername_equal(header->name, "From") ||
1905 procheader_headername_equal(header->name, "To") ||
1906 procheader_headername_equal(header->name, "Cc") ||
1907 procheader_headername_equal(header->name, "Bcc") ||
1908 procheader_headername_equal(header->name, "Reply-To") ||
1909 procheader_headername_equal(header->name, "Sender");
1910 textview_make_clickable_parts(textview, "header",
1911 "hlink", header->body,
1914 gtk_text_buffer_get_end_iter (buffer, &iter);
1915 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, "\n", 1,
1919 textview_show_face(textview);
1920 #if HAVE_LIBCOMPFACE
1921 textview_show_xface(textview);
1925 gboolean textview_search_string(TextView *textview, const gchar *str,
1928 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1930 return gtkut_text_view_search_string(text, str, case_sens);
1933 gboolean textview_search_string_backward(TextView *textview, const gchar *str,
1936 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1938 return gtkut_text_view_search_string_backward(text, str, case_sens);
1941 void textview_scroll_one_line(TextView *textview, gboolean up)
1943 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1944 GtkAdjustment *vadj = text->vadjustment;
1946 gtkutils_scroll_one_line(GTK_WIDGET(text), vadj, up);
1949 gboolean textview_scroll_page(TextView *textview, gboolean up)
1951 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1952 GtkAdjustment *vadj = text->vadjustment;
1954 return gtkutils_scroll_page(GTK_WIDGET(text), vadj, up);
1957 #define KEY_PRESS_EVENT_STOP() \
1958 g_signal_stop_emission_by_name(G_OBJECT(widget), \
1961 static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
1964 SummaryView *summaryview = NULL;
1965 MessageView *messageview = textview->messageview;
1967 if (!event) return FALSE;
1968 if (messageview->mainwin)
1969 summaryview = messageview->mainwin->summaryview;
1971 switch (event->keyval) {
1986 summary_pass_key_press_event(summaryview, event);
1988 mimeview_scroll_page
1989 (messageview->mimeview,
1991 (GDK_SHIFT_MASK|GDK_MOD1_MASK)) != 0);
1994 mimeview_scroll_page(messageview->mimeview, TRUE);
1997 mimeview_scroll_one_line
1998 (messageview->mimeview, (event->state &
1999 (GDK_SHIFT_MASK|GDK_MOD1_MASK)) != 0);
2003 summary_pass_key_press_event(summaryview, event);
2009 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) == 0) {
2010 KEY_PRESS_EVENT_STOP();
2011 mimeview_pass_key_press_event(messageview->mimeview,
2015 /* possible fall through */
2018 event->window != messageview->mainwin->window->window) {
2019 GdkEventKey tmpev = *event;
2021 tmpev.window = messageview->mainwin->window->window;
2022 KEY_PRESS_EVENT_STOP();
2023 gtk_widget_event(messageview->mainwin->window,
2024 (GdkEvent *)&tmpev);
2032 static gboolean textview_motion_notify(GtkWidget *widget,
2033 GdkEventMotion *event,
2036 if (textview->loading)
2038 textview_uri_update(textview, event->x, event->y);
2039 gdk_window_get_pointer(widget->window, NULL, NULL, NULL);
2044 static gboolean textview_leave_notify(GtkWidget *widget,
2045 GdkEventCrossing *event,
2048 if (textview->loading)
2050 textview_uri_update(textview, -1, -1);
2055 static gboolean textview_visibility_notify(GtkWidget *widget,
2056 GdkEventVisibility *event,
2062 if (textview->loading)
2065 window = gtk_text_view_get_window(GTK_TEXT_VIEW(widget),
2066 GTK_TEXT_WINDOW_TEXT);
2068 /* check if occurred for the text window part */
2069 if (window != event->window)
2072 gdk_window_get_pointer(widget->window, &wx, &wy, NULL);
2073 textview_uri_update(textview, wx, wy);
2078 void textview_cursor_wait(TextView *textview)
2080 GdkWindow *window = gtk_text_view_get_window(
2081 GTK_TEXT_VIEW(textview->text),
2082 GTK_TEXT_WINDOW_TEXT);
2083 textview_set_cursor(window, watch_cursor);
2086 void textview_cursor_normal(TextView *textview)
2088 GdkWindow *window = gtk_text_view_get_window(
2089 GTK_TEXT_VIEW(textview->text),
2090 GTK_TEXT_WINDOW_TEXT);
2091 textview_set_cursor(window, text_cursor);
2094 static void textview_uri_update(TextView *textview, gint x, gint y)
2096 GtkTextBuffer *buffer;
2097 GtkTextIter start_iter, end_iter;
2098 ClickableText *uri = NULL;
2100 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
2102 if (x != -1 && y != -1) {
2108 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(textview->text),
2109 GTK_TEXT_WINDOW_WIDGET,
2111 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(textview->text),
2114 tags = gtk_text_iter_get_tags(&iter);
2115 for (cur = tags; cur != NULL; cur = cur->next) {
2116 GtkTextTag *tag = cur->data;
2119 g_object_get(G_OBJECT(tag), "name", &name, NULL);
2121 if ((!strcmp(name, "link") || !strcmp(name, "hlink"))
2122 && textview_get_uri_range(textview, &iter, tag,
2123 &start_iter, &end_iter)) {
2125 uri = textview_get_uri_from_range(textview,
2137 if (uri != textview->uri_hover) {
2140 if (textview->uri_hover)
2141 gtk_text_buffer_remove_tag_by_name(buffer,
2143 &textview->uri_hover_start_iter,
2144 &textview->uri_hover_end_iter);
2146 textview->uri_hover = uri;
2148 textview->uri_hover_start_iter = start_iter;
2149 textview->uri_hover_end_iter = end_iter;
2152 window = gtk_text_view_get_window(GTK_TEXT_VIEW(textview->text),
2153 GTK_TEXT_WINDOW_TEXT);
2154 if (textview->messageview->mainwin->cursor_count == 0) {
2155 textview_set_cursor(window, uri ? hand_cursor : text_cursor);
2157 textview_set_cursor(window, watch_cursor);
2160 TEXTVIEW_STATUSBAR_POP(textview);
2166 gtk_text_buffer_apply_tag_by_name(buffer,
2171 trimmed_uri = trim_string(uri->uri, 60);
2172 TEXTVIEW_STATUSBAR_PUSH(textview, trimmed_uri);
2173 g_free(trimmed_uri);
2178 static gboolean textview_get_uri_range(TextView *textview,
2181 GtkTextIter *start_iter,
2182 GtkTextIter *end_iter)
2184 return get_tag_range(iter, tag, start_iter, end_iter);
2187 static ClickableText *textview_get_uri_from_range(TextView *textview,
2190 GtkTextIter *start_iter,
2191 GtkTextIter *end_iter)
2193 gint start_pos, end_pos, cur_pos;
2194 ClickableText *uri = NULL;
2197 start_pos = gtk_text_iter_get_offset(start_iter);
2198 end_pos = gtk_text_iter_get_offset(end_iter);
2199 cur_pos = gtk_text_iter_get_offset(iter);
2201 for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
2202 ClickableText *uri_ = (ClickableText *)cur->data;
2203 if (start_pos == uri_->start &&
2204 end_pos == uri_->end) {
2209 for (cur = textview->uri_list; uri == NULL && cur != NULL; cur = cur->next) {
2210 ClickableText *uri_ = (ClickableText *)cur->data;
2211 if (start_pos == uri_->start ||
2212 end_pos == uri_->end) {
2213 /* in case of contiguous links, textview_get_uri_range
2214 * returns a broader range (start of 1st link to end
2216 * In that case, correct link is the one covering
2219 if (uri_->start <= cur_pos && cur_pos <= uri_->end) {
2229 static ClickableText *textview_get_uri(TextView *textview,
2233 GtkTextIter start_iter, end_iter;
2234 ClickableText *uri = NULL;
2236 if (textview_get_uri_range(textview, iter, tag, &start_iter,
2238 uri = textview_get_uri_from_range(textview, iter, tag,
2239 &start_iter, &end_iter);
2244 static void textview_shift_uris_after(TextView *textview, GSList *start_list, gint start, gint shift)
2248 start_list = textview->uri_list;
2250 for (cur = start_list; cur; cur = cur->next) {
2251 ClickableText *uri = (ClickableText *)cur->data;
2252 if (uri->start <= start)
2254 uri->start += shift;
2259 static void textview_remove_uris_in(TextView *textview, gint start, gint end)
2262 for (cur = textview->uri_list; cur; ) {
2263 ClickableText *uri = (ClickableText *)cur->data;
2264 if (uri->start > start && uri->end < end) {
2266 textview->uri_list = g_slist_remove(textview->uri_list, uri);
2268 g_free(uri->filename);
2269 if (uri->is_quote) {
2270 g_free(uri->fg_color);
2272 /* (only free data in quotes uris) */
2282 static void textview_toggle_quote(TextView *textview, GSList *start_list, ClickableText *uri, gboolean expand_only)
2284 GtkTextIter start, end;
2285 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
2290 if (uri->q_expanded && expand_only)
2293 gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
2294 gtk_text_buffer_get_iter_at_offset(buffer, &end, uri->end);
2295 if (textview->uri_hover)
2296 gtk_text_buffer_remove_tag_by_name(buffer,
2298 &textview->uri_hover_start_iter,
2299 &textview->uri_hover_end_iter);
2300 textview->uri_hover = NULL;
2301 gtk_text_buffer_remove_tag_by_name(buffer,
2305 /* when shifting URIs start and end, we have to do it per-UTF8-char
2306 * so use g_utf8_strlen(). OTOH, when inserting in the text buffer,
2307 * we have to pass a number of bytes, so use strlen(). disturbing. */
2309 if (!uri->q_expanded) {
2310 gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
2311 gtk_text_buffer_get_iter_at_offset(buffer, &end, uri->end);
2312 textview_shift_uris_after(textview, start_list, uri->start,
2313 g_utf8_strlen((gchar *)uri->data, -1)-strlen(" [...]\n"));
2314 gtk_text_buffer_delete(buffer, &start, &end);
2315 gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
2316 gtk_text_buffer_insert_with_tags_by_name
2317 (buffer, &start, (gchar *)uri->data,
2318 strlen((gchar *)uri->data)-1,
2319 "qlink", (gchar *)uri->fg_color, NULL);
2320 uri->end = gtk_text_iter_get_offset(&start);
2321 textview_make_clickable_parts_later(textview,
2322 uri->start, uri->end);
2323 uri->q_expanded = TRUE;
2325 gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
2326 gtk_text_buffer_get_iter_at_offset(buffer, &end, uri->end);
2327 textview_remove_uris_in(textview, uri->start, uri->end);
2328 textview_shift_uris_after(textview, start_list, uri->start,
2329 strlen(" [...]\n")-g_utf8_strlen((gchar *)uri->data, -1));
2330 gtk_text_buffer_delete(buffer, &start, &end);
2331 gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
2332 gtk_text_buffer_insert_with_tags_by_name
2333 (buffer, &start, " [...]", -1,
2334 "qlink", (gchar *)uri->fg_color, NULL);
2335 uri->end = gtk_text_iter_get_offset(&start);
2336 uri->q_expanded = FALSE;
2338 if (textview->messageview->mainwin->cursor_count == 0) {
2339 textview_cursor_normal(textview);
2341 textview_cursor_wait(textview);
2344 static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
2345 GdkEvent *event, GtkTextIter *iter,
2348 GdkEventButton *bevent;
2349 ClickableText *uri = NULL;
2351 gboolean qlink = FALSE;
2356 if (event->type != GDK_BUTTON_PRESS && event->type != GDK_2BUTTON_PRESS
2357 && event->type != GDK_MOTION_NOTIFY)
2360 uri = textview_get_uri(textview, iter, tag);
2364 g_object_get(G_OBJECT(tag), "name", &tagname, NULL);
2366 if (!strcmp(tagname, "qlink"))
2371 bevent = (GdkEventButton *) event;
2373 /* doubleclick: open compose / add address / browser */
2374 if (qlink && event->type == GDK_BUTTON_PRESS && bevent->button != 1) {
2375 /* pass rightclick through */
2377 } else if ((event->type == (qlink ? GDK_2BUTTON_PRESS:GDK_BUTTON_PRESS) && bevent->button == 1) ||
2378 bevent->button == 2 || bevent->button == 3) {
2379 if (uri->filename && !g_ascii_strncasecmp(uri->filename, "sc://", 5)) {
2380 MimeView *mimeview =
2381 (textview->messageview)?
2382 textview->messageview->mimeview:NULL;
2383 if (mimeview && bevent->button == 1) {
2384 mimeview_handle_cmd(mimeview, uri->filename, NULL, uri->data);
2385 } else if (mimeview && bevent->button == 2 &&
2386 !g_ascii_strcasecmp(uri->filename, "sc://select_attachment")) {
2387 mimeview_handle_cmd(mimeview, "sc://open_attachment", NULL, uri->data);
2388 } else if (mimeview && bevent->button == 3 &&
2389 !g_ascii_strcasecmp(uri->filename, "sc://select_attachment")) {
2390 mimeview_handle_cmd(mimeview, "sc://menu_attachment", bevent, uri->data);
2393 } else if (qlink && bevent->button == 1) {
2394 textview_toggle_quote(textview, NULL, uri, FALSE);
2396 } else if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
2397 if (bevent->button == 3) {
2399 G_OBJECT(textview->mail_popup_menu),
2400 "menu_button", uri);
2401 gtk_menu_popup(GTK_MENU(textview->mail_popup_menu),
2402 NULL, NULL, NULL, NULL,
2403 bevent->button, bevent->time);
2405 PrefsAccount *account = NULL;
2406 FolderItem *folder_item = NULL;
2407 if (textview->messageview && textview->messageview->msginfo &&
2408 textview->messageview->msginfo->folder) {
2411 folder_item = textview->messageview->msginfo->folder;
2412 if (folder_item->prefs && folder_item->prefs->enable_default_account)
2413 account = account_find_from_id(folder_item->prefs->default_account);
2415 compose_new_with_folderitem(account, folder_item, uri->uri + 7);
2418 } else if (g_ascii_strncasecmp(uri->uri, "file:", 5)) {
2419 if (bevent->button == 1 &&
2420 textview_uri_security_check(textview, uri) == TRUE)
2422 prefs_common.uri_cmd);
2423 else if (bevent->button == 3 && !qlink) {
2425 G_OBJECT(textview->link_popup_menu),
2426 "menu_button", uri);
2427 gtk_menu_popup(GTK_MENU(textview->link_popup_menu),
2428 NULL, NULL, NULL, NULL,
2429 bevent->button, bevent->time);
2433 if (bevent->button == 3 && !qlink) {
2435 G_OBJECT(textview->file_popup_menu),
2436 "menu_button", uri);
2437 gtk_menu_popup(GTK_MENU(textview->file_popup_menu),
2438 NULL, NULL, NULL, NULL,
2439 bevent->button, bevent->time);
2449 *\brief Check to see if a web URL has been disguised as a different
2450 * URL (possible with HTML email).
2452 *\param uri The uri to check
2454 *\param textview The TextView the URL is contained in
2456 *\return gboolean TRUE if the URL is ok, or if the user chose to open
2457 * it anyway, otherwise FALSE
2459 static gboolean textview_uri_security_check(TextView *textview, ClickableText *uri)
2462 gboolean retval = TRUE;
2463 GtkTextBuffer *buffer;
2464 GtkTextIter start, end;
2466 if (is_uri_string(uri->uri) == FALSE)
2469 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
2471 gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
2472 gtk_text_buffer_get_iter_at_offset(buffer, &end, uri->end);
2474 visible_str = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
2476 if (visible_str == NULL)
2479 if (strcmp(visible_str, uri->uri) != 0 && is_uri_string(visible_str)) {
2481 gchar *visible_uri_path;
2483 uri_path = get_uri_path(uri->uri);
2484 visible_uri_path = get_uri_path(visible_str);
2485 if (path_cmp(uri_path, visible_uri_path) != 0)
2489 if (retval == FALSE) {
2493 msg = g_markup_printf_escaped(_("The real URL is different from "
2494 "the displayed URL.\n"
2496 "<b>Displayed URL:</b> %s\n"
2498 "<b>Real URL:</b> %s\n"
2501 visible_str,uri->uri);
2502 aval = alertpanel_full(_("Phishing attempt warning"), msg,
2503 GTK_STOCK_CANCEL, _("_Open URL"), NULL, FALSE,
2504 NULL, ALERT_WARNING, G_ALERTDEFAULT);
2506 if (aval == G_ALERTALTERNATE)
2510 g_free(visible_str);
2515 static void textview_uri_list_remove_all(GSList *uri_list)
2519 for (cur = uri_list; cur != NULL; cur = cur->next) {
2521 g_free(((ClickableText *)cur->data)->uri);
2522 g_free(((ClickableText *)cur->data)->filename);
2523 if (((ClickableText *)cur->data)->is_quote) {
2524 g_free(((ClickableText *)cur->data)->fg_color);
2525 g_free(((ClickableText *)cur->data)->data);
2526 /* (only free data in quotes uris) */
2532 g_slist_free(uri_list);
2535 static void open_uri_cb (TextView *textview, guint action, void *data)
2537 ClickableText *uri = g_object_get_data(G_OBJECT(textview->link_popup_menu),
2542 if (textview_uri_security_check(textview, uri) == TRUE)
2544 prefs_common.uri_cmd);
2545 g_object_set_data(G_OBJECT(textview->link_popup_menu), "menu_button",
2549 static void open_image_cb (TextView *textview, guint action, void *data)
2551 ClickableText *uri = g_object_get_data(G_OBJECT(textview->file_popup_menu),
2557 gchar *filename = NULL;
2558 gchar *tmp_filename = NULL;
2563 if (uri->filename == NULL)
2566 filename = g_strdup(uri->filename);
2568 if (!g_utf8_validate(filename, -1, NULL)) {
2569 gchar *tmp = conv_filename_to_utf8(filename);
2574 subst_for_filename(filename);
2576 tmp_filename = g_filename_from_uri(uri->uri, NULL, NULL);
2577 copy_file(tmp_filename, filename, FALSE);
2578 g_free(tmp_filename);
2580 cmd = mailcap_get_command_for_type("image/jpeg", filename);
2582 gboolean remember = FALSE;
2583 cmd = input_dialog_combo_remember
2585 _("Enter the command line to open file:\n"
2586 "('%s' will be replaced with file name)"),
2587 prefs_common.mime_open_cmd,
2588 prefs_common.mime_open_cmd_history,
2590 if (cmd && remember) {
2591 mailcap_update_default("image/jpeg", cmd);
2594 if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
2595 !strchr(p + 2, '%'))
2596 g_snprintf(buf, sizeof(buf), cmd, filename);
2598 g_warning("Image viewer command line is invalid: '%s'", cmd);
2602 execute_command_line(buf, TRUE);
2607 g_object_set_data(G_OBJECT(textview->file_popup_menu), "menu_button",
2611 static void save_file_cb (TextView *textview, guint action, void *data)
2613 ClickableText *uri = g_object_get_data(G_OBJECT(textview->file_popup_menu),
2615 gchar *filename = NULL;
2616 gchar *filepath = NULL;
2617 gchar *filedir = NULL;
2618 gchar *tmp_filename = NULL;
2622 if (uri->filename == NULL)
2625 filename = g_strdup(uri->filename);
2627 if (!g_utf8_validate(filename, -1, NULL)) {
2628 gchar *tmp = conv_filename_to_utf8(filename);
2633 subst_for_filename(filename);
2635 if (prefs_common.attach_save_dir)
2636 filepath = g_strconcat(prefs_common.attach_save_dir,
2637 G_DIR_SEPARATOR_S, filename, NULL);
2639 filepath = g_strdup(filename);
2643 filename = filesel_select_file_save(_("Save as"), filepath);
2649 if (is_file_exist(filename)) {
2653 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
2655 aval = alertpanel(_("Overwrite"), res, GTK_STOCK_CANCEL,
2656 GTK_STOCK_OK, NULL);
2658 if (G_ALERTALTERNATE != aval)
2662 tmp_filename = g_filename_from_uri(uri->uri, NULL, NULL);
2663 copy_file(tmp_filename, filename, FALSE);
2664 g_free(tmp_filename);
2666 filedir = g_path_get_dirname(filename);
2667 if (filedir && strcmp(filedir, ".")) {
2668 g_free(prefs_common.attach_save_dir);
2669 prefs_common.attach_save_dir = g_filename_to_utf8(filedir, -1, NULL, NULL, NULL);
2675 g_object_set_data(G_OBJECT(textview->file_popup_menu), "menu_button",
2679 static void copy_uri_cb (TextView *textview, guint action, void *data)
2681 ClickableText *uri = g_object_get_data(G_OBJECT(textview->link_popup_menu),
2686 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), uri->uri, -1);
2687 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), uri->uri, -1);
2688 g_object_set_data(G_OBJECT(textview->link_popup_menu), "menu_button",
2692 static void add_uri_to_addrbook_cb (TextView *textview, guint action, void *data)
2694 gchar *fromname, *fromaddress;
2695 ClickableText *uri = g_object_get_data(G_OBJECT(textview->mail_popup_menu),
2701 fromaddress = g_strdup(uri->uri + 7);
2702 /* Hiroyuki: please put this function in utils.c! */
2703 fromname = procheader_get_fromname(fromaddress);
2704 extract_address(fromaddress);
2705 g_message("adding from textview %s <%s>", fromname, fromaddress);
2706 /* Add to address book - Match */
2707 addressbook_add_contact( fromname, fromaddress, NULL );
2709 g_free(fromaddress);
2713 static void mail_to_uri_cb (TextView *textview, guint action, void *data)
2715 PrefsAccount *account = NULL;
2716 ClickableText *uri = g_object_get_data(G_OBJECT(textview->mail_popup_menu),
2721 if (textview->messageview && textview->messageview->msginfo &&
2722 textview->messageview->msginfo->folder) {
2723 FolderItem *folder_item;
2725 folder_item = textview->messageview->msginfo->folder;
2726 if (folder_item->prefs && folder_item->prefs->enable_default_account)
2727 account = account_find_from_id(folder_item->prefs->default_account);
2729 compose_new(account, uri->uri + 7, NULL);
2732 static void copy_mail_to_uri_cb (TextView *textview, guint action, void *data)
2734 ClickableText *uri = g_object_get_data(G_OBJECT(textview->mail_popup_menu),
2739 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), uri->uri +7, -1);
2740 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), uri->uri +7, -1);
2741 g_object_set_data(G_OBJECT(textview->mail_popup_menu), "menu_button",
2745 void textview_get_selection_offsets(TextView *textview, gint *sel_start, gint *sel_end)
2747 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
2748 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
2749 GtkTextIter start, end;
2750 if (gtk_text_buffer_get_selection_bounds(buffer, &start, &end)) {
2752 *sel_start = gtk_text_iter_get_offset(&start);
2754 *sel_end = gtk_text_iter_get_offset(&end);