2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2011 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 3 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, see <http://www.gnu.org/licenses/>.
27 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.h>
40 # include <compface.h>
44 #define XPM_XFACE_HEIGHT (HEIGHT + 3) /* 3 = 1 header + 2 colors */
48 #include "summaryview.h"
49 #include "procheader.h"
50 #include "prefs_common.h"
58 #include "addressbook.h"
59 #include "displayheader.h"
62 #include "alertpanel.h"
64 #include "image_viewer.h"
67 #include "inputdialog.h"
70 #include "addrindex.h"
72 static GdkColor quote_colors[3] = {
73 {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
74 {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
75 {(gulong)0, (gushort)0, (gushort)0, (gushort)0}
78 static GdkColor quote_bgcolors[3] = {
79 {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
80 {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
81 {(gulong)0, (gushort)0, (gushort)0, (gushort)0}
83 static GdkColor signature_color = {
90 static GdkColor uri_color = {
97 static GdkColor emphasis_color = {
104 static GdkCursor *hand_cursor = NULL;
105 static GdkCursor *text_cursor = NULL;
106 static GdkCursor *watch_cursor= NULL;
108 #define TEXTVIEW_STATUSBAR_PUSH(textview, str) \
109 { if (textview->messageview->statusbar) \
110 gtk_statusbar_push(GTK_STATUSBAR(textview->messageview->statusbar), \
111 textview->messageview->statusbar_cid, str); \
114 #define TEXTVIEW_STATUSBAR_POP(textview) \
115 { if (textview->messageview->statusbar) \
116 gtk_statusbar_pop(GTK_STATUSBAR(textview->messageview->statusbar), \
117 textview->messageview->statusbar_cid); \
120 static void textview_show_ertf (TextView *textview,
122 CodeConverter *conv);
123 static void textview_add_part (TextView *textview,
125 static void textview_add_parts (TextView *textview,
127 static void textview_write_body (TextView *textview,
129 static void textview_show_html (TextView *textview,
131 CodeConverter *conv);
133 static void textview_write_line (TextView *textview,
136 gboolean do_quote_folding);
137 static void textview_write_link (TextView *textview,
140 CodeConverter *conv);
142 static GPtrArray *textview_scan_header (TextView *textview,
144 static void textview_show_header (TextView *textview,
147 static gint textview_key_pressed (GtkWidget *widget,
150 static gboolean textview_motion_notify (GtkWidget *widget,
151 GdkEventMotion *motion,
153 static gboolean textview_leave_notify (GtkWidget *widget,
154 GdkEventCrossing *event,
156 static gboolean textview_visibility_notify (GtkWidget *widget,
157 GdkEventVisibility *event,
159 static void textview_uri_update (TextView *textview,
162 static gboolean textview_get_uri_range (TextView *textview,
165 GtkTextIter *start_iter,
166 GtkTextIter *end_iter);
167 static ClickableText *textview_get_uri_from_range (TextView *textview,
170 GtkTextIter *start_iter,
171 GtkTextIter *end_iter);
172 static ClickableText *textview_get_uri (TextView *textview,
175 static gboolean textview_uri_button_pressed (GtkTextTag *tag,
181 static void textview_uri_list_remove_all (GSList *uri_list);
183 static void textview_toggle_quote (TextView *textview,
186 gboolean expand_only);
188 static void open_uri_cb (GtkAction *action,
190 static void copy_uri_cb (GtkAction *action,
192 static void add_uri_to_addrbook_cb (GtkAction *action,
194 static void reply_to_uri_cb (GtkAction *action,
196 static void mail_to_uri_cb (GtkAction *action,
198 static void copy_mail_to_uri_cb (GtkAction *action,
200 static void save_file_cb (GtkAction *action,
202 static void open_image_cb (GtkAction *action,
204 static void textview_show_tags(TextView *textview);
206 static GtkActionEntry textview_link_popup_entries[] =
208 {"TextviewPopupLink", NULL, "TextviewPopupLink" },
209 {"TextviewPopupLink/Open", NULL, N_("_Open in web browser"), NULL, NULL, G_CALLBACK(open_uri_cb) },
210 {"TextviewPopupLink/Copy", NULL, N_("Copy this _link"), NULL, NULL, G_CALLBACK(copy_uri_cb) },
213 static GtkActionEntry textview_mail_popup_entries[] =
215 {"TextviewPopupMail", NULL, "TextviewPopupMail" },
216 {"TextviewPopupMail/Compose", NULL, N_("Compose _new message"), NULL, NULL, G_CALLBACK(mail_to_uri_cb) },
217 {"TextviewPopupMail/ReplyTo", NULL, N_("_Reply to this address"), NULL, NULL, G_CALLBACK(reply_to_uri_cb) },
218 {"TextviewPopupMail/AddAB", NULL, N_("Add to _Address book"), NULL, NULL, G_CALLBACK(add_uri_to_addrbook_cb) },
219 {"TextviewPopupMail/Copy", NULL, N_("Copy this add_ress"), NULL, NULL, G_CALLBACK(copy_mail_to_uri_cb) },
222 static GtkActionEntry textview_file_popup_entries[] =
224 {"TextviewPopupFile", NULL, "TextviewPopupFile" },
225 {"TextviewPopupFile/Open", NULL, N_("_Open image"), NULL, NULL, G_CALLBACK(open_image_cb) },
226 {"TextviewPopupFile/Save", NULL, N_("_Save image..."), NULL, NULL, G_CALLBACK(save_file_cb) },
229 static void scrolled_cb (GtkAdjustment *adj, TextView *textview)
235 if (textview->image) {
237 x1 = textview->text->allocation.width - WIDTH - 5;
238 gtk_text_view_buffer_to_window_coords(
239 GTK_TEXT_VIEW(textview->text),
240 GTK_TEXT_WINDOW_TEXT, x1, 5, &x, &y);
241 gtk_text_view_move_child(GTK_TEXT_VIEW(textview->text),
242 textview->image, x1, y);
246 static void textview_size_allocate_cb (GtkWidget *widget,
247 GtkAllocation *allocation,
250 scrolled_cb(NULL, (TextView *)data);
253 TextView *textview_create(void)
257 GtkWidget *scrolledwin;
259 GtkTextBuffer *buffer;
260 GtkClipboard *clipboard;
263 debug_print("Creating text view...\n");
264 textview = g_new0(TextView, 1);
266 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
267 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
268 GTK_POLICY_AUTOMATIC,
269 GTK_POLICY_AUTOMATIC);
270 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
272 gtk_widget_set_size_request
273 (scrolledwin, prefs_common.mainview_width, -1);
275 /* create GtkSText widgets for single-byte and multi-byte character */
276 text = gtk_text_view_new();
277 gtk_widget_add_events(text, GDK_LEAVE_NOTIFY_MASK);
278 gtk_widget_show(text);
279 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
280 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD_CHAR);
281 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(text), FALSE);
282 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
283 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
285 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
286 clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
287 gtk_text_buffer_add_selection_clipboard(buffer, clipboard);
289 gtk_widget_ensure_style(text);
291 g_object_ref(scrolledwin);
293 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
295 g_signal_connect(G_OBJECT(text), "key-press-event",
296 G_CALLBACK(textview_key_pressed), textview);
297 g_signal_connect(G_OBJECT(text), "motion-notify-event",
298 G_CALLBACK(textview_motion_notify), textview);
299 g_signal_connect(G_OBJECT(text), "leave-notify-event",
300 G_CALLBACK(textview_leave_notify), textview);
301 g_signal_connect(G_OBJECT(text), "visibility-notify-event",
302 G_CALLBACK(textview_visibility_notify), textview);
303 adj = gtk_scrolled_window_get_vadjustment(
304 GTK_SCROLLED_WINDOW(scrolledwin));
305 g_signal_connect(G_OBJECT(adj), "value-changed",
306 G_CALLBACK(scrolled_cb), textview);
307 g_signal_connect(G_OBJECT(text), "size_allocate",
308 G_CALLBACK(textview_size_allocate_cb),
312 gtk_widget_show(scrolledwin);
314 vbox = gtk_vbox_new(FALSE, 0);
315 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
317 gtk_widget_show(vbox);
320 textview->ui_manager = gtk_ui_manager_new();
321 textview->link_action_group = cm_menu_create_action_group_full(textview->ui_manager,
323 textview_link_popup_entries,
324 G_N_ELEMENTS(textview_link_popup_entries), (gpointer)textview);
325 textview->mail_action_group = cm_menu_create_action_group_full(textview->ui_manager,
327 textview_mail_popup_entries,
328 G_N_ELEMENTS(textview_mail_popup_entries), (gpointer)textview);
329 textview->file_action_group = cm_menu_create_action_group_full(textview->ui_manager,
331 textview_file_popup_entries,
332 G_N_ELEMENTS(textview_file_popup_entries), (gpointer)textview);
334 MENUITEM_ADDUI_MANAGER(textview->ui_manager, "/", "Menus", "Menus", GTK_UI_MANAGER_MENUBAR)
335 MENUITEM_ADDUI_MANAGER(textview->ui_manager,
336 "/Menus", "TextviewPopupLink", "TextviewPopupLink", GTK_UI_MANAGER_MENU)
337 MENUITEM_ADDUI_MANAGER(textview->ui_manager,
338 "/Menus", "TextviewPopupMail", "TextviewPopupMail", GTK_UI_MANAGER_MENU)
339 MENUITEM_ADDUI_MANAGER(textview->ui_manager,
340 "/Menus", "TextviewPopupFile", "TextviewPopupFile", GTK_UI_MANAGER_MENU)
342 MENUITEM_ADDUI_MANAGER(textview->ui_manager,
343 "/Menus/TextviewPopupLink", "Open", "TextviewPopupLink/Open", GTK_UI_MANAGER_MENUITEM)
344 MENUITEM_ADDUI_MANAGER(textview->ui_manager,
345 "/Menus/TextviewPopupLink", "Copy", "TextviewPopupLink/Copy", GTK_UI_MANAGER_MENUITEM)
346 MENUITEM_ADDUI_MANAGER(textview->ui_manager,
347 "/Menus/TextviewPopupMail", "Compose", "TextviewPopupMail/Compose", GTK_UI_MANAGER_MENUITEM)
348 MENUITEM_ADDUI_MANAGER(textview->ui_manager,
349 "/Menus/TextviewPopupMail", "ReplyTo", "TextviewPopupMail/ReplyTo", GTK_UI_MANAGER_MENUITEM)
350 MENUITEM_ADDUI_MANAGER(textview->ui_manager,
351 "/Menus/TextviewPopupMail", "AddAB", "TextviewPopupMail/AddAB", GTK_UI_MANAGER_MENUITEM)
352 MENUITEM_ADDUI_MANAGER(textview->ui_manager,
353 "/Menus/TextviewPopupMail", "Copy", "TextviewPopupMail/Copy", GTK_UI_MANAGER_MENUITEM)
354 MENUITEM_ADDUI_MANAGER(textview->ui_manager,
355 "/Menus/TextviewPopupFile", "Open", "TextviewPopupFile/Open", GTK_UI_MANAGER_MENUITEM)
356 MENUITEM_ADDUI_MANAGER(textview->ui_manager,
357 "/Menus/TextviewPopupFile", "Save", "TextviewPopupFile/Save", GTK_UI_MANAGER_MENUITEM)
359 textview->link_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
360 gtk_ui_manager_get_widget(textview->ui_manager, "/Menus/TextviewPopupLink")) );
361 textview->mail_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
362 gtk_ui_manager_get_widget(textview->ui_manager, "/Menus/TextviewPopupMail")) );
363 textview->file_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
364 gtk_ui_manager_get_widget(textview->ui_manager, "/Menus/TextviewPopupFile")) );
366 textview->vbox = vbox;
367 textview->scrolledwin = scrolledwin;
368 textview->text = text;
369 textview->uri_list = NULL;
370 textview->body_pos = 0;
371 textview->show_all_headers = FALSE;
372 textview->last_buttonpress = GDK_NOTHING;
373 textview->image = NULL;
377 static void textview_create_tags(GtkTextView *text, TextView *textview)
379 GtkTextBuffer *buffer;
380 GtkTextTag *tag, *qtag;
381 static GdkColor yellow, black;
382 static gboolean color_init = FALSE;
383 static PangoFontDescription *font_desc, *bold_font_desc;
386 gdk_color_parse("#f5f6be", &yellow);
387 gdk_color_parse("#000000", &black);
388 color_init = gdk_colormap_alloc_color(
389 gdk_colormap_get_system(), &yellow, FALSE, TRUE);
390 color_init &= gdk_colormap_alloc_color(
391 gdk_colormap_get_system(), &black, FALSE, TRUE);
395 font_desc = pango_font_description_from_string
398 if (!bold_font_desc) {
399 if (prefs_common.derive_from_normal_font || !BOLD_FONT) {
400 bold_font_desc = pango_font_description_from_string
402 pango_font_description_set_weight
403 (bold_font_desc, PANGO_WEIGHT_BOLD);
405 bold_font_desc = pango_font_description_from_string
410 buffer = gtk_text_view_get_buffer(text);
412 gtk_text_buffer_create_tag(buffer, "header",
413 "pixels-above-lines", 0,
414 "pixels-above-lines-set", TRUE,
415 "pixels-below-lines", 0,
416 "pixels-below-lines-set", TRUE,
417 "font-desc", font_desc,
419 "left-margin-set", TRUE,
421 gtk_text_buffer_create_tag(buffer, "header_title",
422 "font-desc", bold_font_desc,
424 tag = gtk_text_buffer_create_tag(buffer, "hlink",
425 "pixels-above-lines", 0,
426 "pixels-above-lines-set", TRUE,
427 "pixels-below-lines", 0,
428 "pixels-below-lines-set", TRUE,
429 "font-desc", font_desc,
431 "left-margin-set", TRUE,
432 "foreground-gdk", &uri_color,
434 g_signal_connect(G_OBJECT(tag), "event",
435 G_CALLBACK(textview_uri_button_pressed), textview);
436 if (prefs_common.enable_bgcolor) {
437 gtk_text_buffer_create_tag(buffer, "quote0",
438 "foreground-gdk", "e_colors[0],
439 "paragraph-background-gdk", "e_bgcolors[0],
441 gtk_text_buffer_create_tag(buffer, "quote1",
442 "foreground-gdk", "e_colors[1],
443 "paragraph-background-gdk", "e_bgcolors[1],
445 gtk_text_buffer_create_tag(buffer, "quote2",
446 "foreground-gdk", "e_colors[2],
447 "paragraph-background-gdk", "e_bgcolors[2],
450 gtk_text_buffer_create_tag(buffer, "quote0",
451 "foreground-gdk", "e_colors[0],
453 gtk_text_buffer_create_tag(buffer, "quote1",
454 "foreground-gdk", "e_colors[1],
456 gtk_text_buffer_create_tag(buffer, "quote2",
457 "foreground-gdk", "e_colors[2],
460 gtk_text_buffer_create_tag(buffer, "tags",
461 "foreground-gdk", &black,
462 "paragraph-background-gdk", &yellow,
464 gtk_text_buffer_create_tag(buffer, "emphasis",
465 "foreground-gdk", &emphasis_color,
467 gtk_text_buffer_create_tag(buffer, "signature",
468 "foreground-gdk", &signature_color,
470 tag = gtk_text_buffer_create_tag(buffer, "link",
471 "foreground-gdk", &uri_color,
473 qtag = gtk_text_buffer_create_tag(buffer, "qlink",
475 gtk_text_buffer_create_tag(buffer, "link-hover",
476 "underline", PANGO_UNDERLINE_SINGLE,
478 g_signal_connect(G_OBJECT(qtag), "event",
479 G_CALLBACK(textview_uri_button_pressed), textview);
480 g_signal_connect(G_OBJECT(tag), "event",
481 G_CALLBACK(textview_uri_button_pressed), textview);
483 pango_font_description_free(font_desc);
485 pango_font_description_free(bold_font_desc);*/
488 void textview_init(TextView *textview)
491 hand_cursor = gdk_cursor_new(GDK_HAND2);
493 text_cursor = gdk_cursor_new(GDK_XTERM);
495 watch_cursor = gdk_cursor_new(GDK_WATCH);
497 textview_reflect_prefs(textview);
498 textview_set_all_headers(textview, FALSE);
499 textview_set_font(textview, NULL);
500 textview_create_tags(GTK_TEXT_VIEW(textview->text), textview);
503 #define CHANGE_TAG_COLOR(tagname, colorfg, colorbg) { \
504 tag = gtk_text_tag_table_lookup(tags, tagname); \
506 g_object_set(G_OBJECT(tag), "foreground-gdk", colorfg, "paragraph-background-gdk", colorbg, NULL); \
509 static void textview_update_message_colors(TextView *textview)
511 GdkColor black = {0, 0, 0, 0};
512 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
514 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
515 GtkTextTag *tag = NULL;
517 quote_bgcolors[0] = quote_bgcolors[1] = quote_bgcolors[2] = black;
518 quote_colors[0] = quote_colors[1] = quote_colors[2] =
519 uri_color = emphasis_color = signature_color = black;
521 if (prefs_common.enable_color) {
522 /* grab the quote colors, converting from an int to a GdkColor */
523 gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_col,
525 gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_col,
527 gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_col,
529 gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
531 gtkut_convert_int_to_gdk_color(prefs_common.signature_col,
533 gtkut_convert_int_to_gdk_color(prefs_common.emphasis_col,
536 if (prefs_common.enable_color && prefs_common.enable_bgcolor) {
537 gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_bgcol,
539 gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_bgcol,
541 gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_bgcol,
543 CHANGE_TAG_COLOR("quote0", "e_colors[0], "e_bgcolors[0]);
544 CHANGE_TAG_COLOR("quote1", "e_colors[1], "e_bgcolors[1]);
545 CHANGE_TAG_COLOR("quote2", "e_colors[2], "e_bgcolors[2]);
547 CHANGE_TAG_COLOR("quote0", "e_colors[0], NULL);
548 CHANGE_TAG_COLOR("quote1", "e_colors[1], NULL);
549 CHANGE_TAG_COLOR("quote2", "e_colors[2], NULL);
552 CHANGE_TAG_COLOR("emphasis", &emphasis_color, NULL);
553 CHANGE_TAG_COLOR("signature", &signature_color, NULL);
554 CHANGE_TAG_COLOR("link", &uri_color, NULL);
555 CHANGE_TAG_COLOR("link-hover", &uri_color, NULL);
557 #undef CHANGE_TAG_COLOR
559 void textview_reflect_prefs(TextView *textview)
561 textview_set_font(textview, NULL);
562 textview_update_message_colors(textview);
563 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview->text),
564 prefs_common.textview_cursor_visible);
567 void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
570 cm_return_if_fail(mimeinfo != NULL);
571 cm_return_if_fail(fp != NULL);
573 if ((mimeinfo->type == MIMETYPE_MULTIPART) ||
574 ((mimeinfo->type == MIMETYPE_MESSAGE) && !g_ascii_strcasecmp(mimeinfo->subtype, "rfc822"))) {
575 textview->loading = TRUE;
576 textview->stop_loading = FALSE;
578 textview_clear(textview);
579 textview_add_parts(textview, mimeinfo);
581 textview->loading = FALSE;
582 textview->stop_loading = FALSE;
586 textview->loading = TRUE;
587 textview->stop_loading = FALSE;
589 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0)
592 textview_clear(textview);
594 if (mimeinfo->type == MIMETYPE_MULTIPART)
595 textview_add_parts(textview, mimeinfo);
597 textview_write_body(textview, mimeinfo);
599 textview->loading = FALSE;
600 textview->stop_loading = FALSE;
604 static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
607 GtkTextBuffer *buffer;
608 GtkTextIter iter, start_iter;
610 GPtrArray *headers = NULL;
616 cm_return_if_fail(mimeinfo != NULL);
617 text = GTK_TEXT_VIEW(textview->text);
618 buffer = gtk_text_view_get_buffer(text);
619 charcount = gtk_text_buffer_get_char_count(buffer);
620 gtk_text_buffer_get_end_iter(buffer, &iter);
622 if (textview->stop_loading) {
625 if (mimeinfo->type == MIMETYPE_MULTIPART) {
630 textview->prev_quote_level = -1;
632 if ((mimeinfo->type == MIMETYPE_MESSAGE) && !g_ascii_strcasecmp(mimeinfo->subtype, "rfc822")) {
634 if (mimeinfo->content == MIMECONTENT_MEM)
635 fp = str_open_as_stream(mimeinfo->data.mem);
637 fp = g_fopen(mimeinfo->data.filename, "rb");
639 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
643 fseek(fp, mimeinfo->offset, SEEK_SET);
644 headers = textview_scan_header(textview, fp);
647 gtk_text_buffer_insert(buffer, &iter, "\n", 1);
649 if (procmime_mimeinfo_parent(mimeinfo) == NULL)
650 textview_show_tags(textview);
651 textview_show_header(textview, headers);
652 procheader_header_array_destroy(headers);
659 name = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
660 content_type = procmime_get_content_type_str(mimeinfo->type,
663 name = procmime_mimeinfo_get_parameter(mimeinfo, "name");
665 g_snprintf(buf, sizeof(buf), _("[%s %s (%d bytes)]"),
666 name, content_type, mimeinfo->length);
668 g_snprintf(buf, sizeof(buf), _("[%s (%d bytes)]"),
669 content_type, mimeinfo->length);
671 g_free(content_type);
673 if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT
674 || (mimeinfo->disposition == DISPOSITIONTYPE_INLINE &&
675 mimeinfo->type != MIMETYPE_TEXT)) {
676 gtk_text_buffer_insert(buffer, &iter, "\n", 1);
677 TEXTVIEW_INSERT_LINK(buf, "sc://select_attachment", mimeinfo);
678 gtk_text_buffer_insert(buffer, &iter, " \n", -1);
679 if (mimeinfo->type == MIMETYPE_IMAGE &&
680 prefs_common.inline_img ) {
684 GError *error = NULL;
689 START_TIMING("inserting image");
691 filename = procmime_get_tmp_file_name(mimeinfo);
693 if ((err = procmime_get_part(filename, mimeinfo)) < 0) {
694 g_warning("Can't get the image file.(%s)", strerror(-err));
700 if (!prefs_common.resize_img) {
701 pixbuf = gdk_pixbuf_new_from_file(filename, &error);
704 gdk_pixbuf_get_file_info(filename, &w, &h);
705 avail_width = textview->scrolledwin->allocation.width;
706 avail_height = textview->scrolledwin->allocation.height;
707 if (avail_width - 100 > 0 &&
708 (w > avail_width || h > avail_height))
709 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
710 avail_width, avail_height, TRUE, &error);
712 pixbuf = gdk_pixbuf_new_from_file(filename, &error);
714 if (textview->stop_loading) {
718 g_warning("%s\n", error->message);
722 g_warning("Can't load the image.");
728 uri_str = g_filename_to_uri(filename, NULL, NULL);
730 uri = g_new0(ClickableText, 1);
732 uri->start = gtk_text_iter_get_offset(&iter);
734 gtk_text_buffer_insert_pixbuf(buffer, &iter, pixbuf);
735 if (textview->stop_loading) {
739 uri->end = uri->start + 1;
740 uri->filename = procmime_get_part_file_name(mimeinfo);
742 g_slist_prepend(textview->uri_list, uri);
744 gtk_text_buffer_insert(buffer, &iter, " ", 1);
745 gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, uri->start);
746 gtk_text_buffer_apply_tag_by_name(buffer, "link",
749 gtk_text_buffer_insert_pixbuf(buffer, &iter, pixbuf);
750 if (textview->stop_loading) {
753 gtk_text_buffer_insert(buffer, &iter, " ", 1);
756 g_object_unref(pixbuf);
761 } else if (mimeinfo->type == MIMETYPE_TEXT) {
762 if (prefs_common.display_header && (charcount > 0))
763 gtk_text_buffer_insert(buffer, &iter, "\n", 1);
765 textview_write_body(textview, mimeinfo);
770 static void recursive_add_parts(TextView *textview, GNode *node)
776 mimeinfo = (MimeInfo *) node->data;
778 textview_add_part(textview, mimeinfo);
780 textview_set_position(textview, 0);
782 if ((mimeinfo->type != MIMETYPE_MULTIPART) &&
783 (mimeinfo->type != MIMETYPE_MESSAGE)) {
787 if (g_ascii_strcasecmp(mimeinfo->subtype, "alternative") == 0) {
788 GNode * prefered_body;
796 prefered_body = NULL;
799 for (iter = g_node_first_child(node) ; iter != NULL ;
800 iter = g_node_next_sibling(iter)) {
805 submime = (MimeInfo *) iter->data;
806 if (submime->type == MIMETYPE_TEXT)
809 if (submime->subtype != NULL) {
810 if (g_ascii_strcasecmp(submime->subtype, "plain") == 0)
814 if (score > prefered_score) {
815 prefered_score = score;
816 prefered_body = iter;
820 if (prefered_body != NULL) {
821 recursive_add_parts(textview, prefered_body);
825 for (iter = g_node_first_child(node) ; iter != NULL ;
826 iter = g_node_next_sibling(iter)) {
827 recursive_add_parts(textview, iter);
833 static void textview_add_parts(TextView *textview, MimeInfo *mimeinfo)
835 cm_return_if_fail(mimeinfo != NULL);
837 recursive_add_parts(textview, mimeinfo->node);
840 void textview_show_error(TextView *textview)
843 GtkTextBuffer *buffer;
846 textview_set_font(textview, NULL);
847 textview_clear(textview);
849 text = GTK_TEXT_VIEW(textview->text);
850 buffer = gtk_text_view_get_buffer(text);
851 gtk_text_buffer_get_start_iter(buffer, &iter);
853 TEXTVIEW_INSERT(_("\n"
854 " This message can't be displayed.\n"
855 " This is probably due to a network error.\n"
858 TEXTVIEW_INSERT_LINK(_("'View Log'"), "sc://view_log", NULL);
859 TEXTVIEW_INSERT(_(" in the Tools menu for more information."));
860 textview_show_icon(textview, GTK_STOCK_DIALOG_ERROR);
863 void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
866 GtkTextBuffer *buffer;
871 if (!partinfo) return;
873 textview_set_font(textview, NULL);
874 textview_clear(textview);
876 text = GTK_TEXT_VIEW(textview->text);
877 buffer = gtk_text_view_get_buffer(text);
878 gtk_text_buffer_get_start_iter(buffer, &iter);
880 TEXTVIEW_INSERT("\n");
882 name = procmime_mimeinfo_get_parameter(partinfo, "filename");
884 name = procmime_mimeinfo_get_parameter(partinfo, "name");
886 content_type = procmime_get_content_type_str(partinfo->type,
888 TEXTVIEW_INSERT(" ");
889 TEXTVIEW_INSERT_BOLD(name);
890 TEXTVIEW_INSERT(" (");
891 TEXTVIEW_INSERT(content_type);
892 TEXTVIEW_INSERT(", ");
893 TEXTVIEW_INSERT(to_human_readable((goffset)partinfo->length));
894 TEXTVIEW_INSERT("):\n\n");
896 g_free(content_type);
898 TEXTVIEW_INSERT(_(" The following can be performed on this part\n"));
900 TEXTVIEW_INSERT(_(" by right-clicking the icon or list item:"));
902 TEXTVIEW_INSERT("\n");
904 TEXTVIEW_INSERT(_(" - To save, select "));
905 TEXTVIEW_INSERT_LINK(_("'Save as...'"), "sc://save_as", NULL);
907 TEXTVIEW_INSERT(_(" (Shortcut key: 'y')"));
909 TEXTVIEW_INSERT("\n");
911 TEXTVIEW_INSERT(_(" - To display as text, select "));
912 TEXTVIEW_INSERT_LINK(_("'Display as text'"), "sc://display_as_text", NULL);
915 TEXTVIEW_INSERT(_(" (Shortcut key: 't')"));
917 TEXTVIEW_INSERT("\n");
919 TEXTVIEW_INSERT(_(" - To open with an external program, select "));
920 TEXTVIEW_INSERT_LINK(_("'Open'"), "sc://open", NULL);
923 TEXTVIEW_INSERT(_(" (Shortcut key: 'l')\n"));
924 TEXTVIEW_INSERT(_(" (alternately double-click, or click the middle "));
925 TEXTVIEW_INSERT(_("mouse button)\n"));
927 TEXTVIEW_INSERT(_(" - Or use "));
928 TEXTVIEW_INSERT_LINK(_("'Open with...'"), "sc://open_with", NULL);
929 TEXTVIEW_INSERT(_(" (Shortcut key: 'o')"));
932 TEXTVIEW_INSERT("\n");
934 textview_show_icon(textview, GTK_STOCK_DIALOG_INFO);
937 static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
942 const gchar *charset, *p, *cmd;
944 gboolean continue_write = TRUE;
945 size_t wrote = 0, i = 0;
947 if (textview->messageview->forced_charset)
948 charset = textview->messageview->forced_charset;
950 /* use supersets transparently when possible */
951 charset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
952 if (charset && !strcasecmp(charset, CS_ISO_8859_1))
953 charset = CS_WINDOWS_1252;
954 else if (charset && !strcasecmp(charset, CS_X_GBK))
955 charset = CS_GB18030;
956 else if (charset && !strcasecmp(charset, CS_GBK))
957 charset = CS_GB18030;
958 else if (charset && !strcasecmp(charset, CS_GB2312))
959 charset = CS_GB18030;
962 textview_set_font(textview, charset);
964 conv = conv_code_converter_new(charset);
966 procmime_force_encoding(textview->messageview->forced_encoding);
968 textview->is_in_signature = FALSE;
970 procmime_decode_content(mimeinfo);
972 if (!g_ascii_strcasecmp(mimeinfo->subtype, "html") &&
973 prefs_common.render_html) {
976 filename = procmime_get_tmp_file_name(mimeinfo);
977 if (procmime_get_part(filename, mimeinfo) == 0) {
978 tmpfp = g_fopen(filename, "rb");
979 textview_show_html(textview, tmpfp, conv);
981 claws_unlink(filename);
984 } else if (!g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
987 filename = procmime_get_tmp_file_name(mimeinfo);
988 if (procmime_get_part(filename, mimeinfo) == 0) {
989 tmpfp = g_fopen(filename, "rb");
990 textview_show_ertf(textview, tmpfp, conv);
992 claws_unlink(filename);
996 } else if ( g_ascii_strcasecmp(mimeinfo->subtype, "plain") &&
997 (cmd = prefs_common.mime_textviewer) && *cmd &&
998 (p = strchr(cmd, '%')) && *(p + 1) == 's') {
1002 fname = procmime_get_tmp_file_name(mimeinfo);
1003 if (procmime_get_part(fname, mimeinfo)) goto textview_default;
1005 g_snprintf(buf, sizeof(buf), cmd, fname);
1006 debug_print("Viewing text content of type: %s (length: %d) "
1007 "using %s\n", mimeinfo->subtype, mimeinfo->length, buf);
1009 if (pipe(pfd) < 0) {
1010 g_snprintf(buf, sizeof(buf),
1011 "pipe failed for textview\n\n%s\n", strerror(errno));
1012 textview_write_line(textview, buf, conv, TRUE);
1013 goto textview_default;
1017 g_snprintf(buf, sizeof(buf),
1018 "fork failed for textview\n\n%s\n", strerror(errno));
1019 textview_write_line(textview, buf, conv, TRUE);
1022 goto textview_default;
1024 if (pid == 0) { /* child */
1027 argv = strsplit_with_quote(buf, " ", 0);
1031 rc = execvp(argv[0], argv);
1033 g_print(_("The command to view attachment "
1036 "Exit code %d\n"), buf, rc);
1040 tmpfp = fdopen(pfd[0], "rb");
1041 while (fgets(buf, sizeof(buf), tmpfp)) {
1042 textview_write_line(textview, buf, conv, TRUE);
1044 if (textview->stop_loading) {
1046 waitpid(pid, pfd, 0);
1053 waitpid(pid, pfd, 0);
1058 if (mimeinfo->content == MIMECONTENT_MEM)
1059 tmpfp = str_open_as_stream(mimeinfo->data.mem);
1061 tmpfp = g_fopen(mimeinfo->data.filename, "rb");
1063 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1066 fseek(tmpfp, mimeinfo->offset, SEEK_SET);
1067 debug_print("Viewing text content of type: %s (length: %d)\n", mimeinfo->subtype, mimeinfo->length);
1068 while (((i = ftell(tmpfp)) < mimeinfo->offset + mimeinfo->length) &&
1069 (fgets(buf, sizeof(buf), tmpfp) != NULL)
1070 && continue_write) {
1071 textview_write_line(textview, buf, conv, TRUE);
1072 if (textview->stop_loading) {
1076 wrote += ftell(tmpfp)-i;
1077 if (mimeinfo->length > 1024*1024
1078 && wrote > 1024*1024
1079 && !textview->messageview->show_full_text) {
1080 continue_write = FALSE;
1086 conv_code_converter_destroy(conv);
1087 procmime_force_encoding(0);
1089 textview->uri_list = g_slist_reverse(textview->uri_list);
1090 for (cur = textview->uri_list; cur; cur = cur->next) {
1091 ClickableText *uri = (ClickableText *)cur->data;
1094 if (!prefs_common.hide_quotes ||
1095 uri->quote_level+1 < prefs_common.hide_quotes) {
1096 textview_toggle_quote(textview, cur, uri, TRUE);
1097 if (textview->stop_loading) {
1103 if (continue_write == FALSE) {
1104 messageview_show_partial_display(
1105 textview->messageview,
1106 textview->messageview->msginfo,
1112 static void textview_show_html(TextView *textview, FILE *fp,
1113 CodeConverter *conv)
1115 SC_HTMLParser *parser;
1119 parser = sc_html_parser_new(fp, conv);
1120 cm_return_if_fail(parser != NULL);
1122 while ((str = sc_html_parse(parser)) != NULL) {
1123 if (parser->state == SC_HTML_HREF) {
1124 /* first time : get and copy the URL */
1125 if (parser->href == NULL) {
1126 /* ALF - the claws html parser returns an empty string,
1127 * if still inside an <a>, but already parsed past HREF */
1128 str = strtok(str, " ");
1130 while (str && *str && g_ascii_isspace(*str))
1132 parser->href = g_strdup(str);
1133 /* the URL may (or not) be followed by the
1134 * referenced text */
1135 str = strtok(NULL, "");
1139 textview_write_link(textview, str, parser->href, NULL);
1141 textview_write_line(textview, str, NULL, FALSE);
1143 if (lines % 500 == 0)
1145 if (textview->stop_loading) {
1149 textview_write_line(textview, "\n", NULL, FALSE);
1150 sc_html_parser_destroy(parser);
1153 static void textview_show_ertf(TextView *textview, FILE *fp,
1154 CodeConverter *conv)
1160 parser = ertf_parser_new(fp, conv);
1161 cm_return_if_fail(parser != NULL);
1163 while ((str = ertf_parse(parser)) != NULL) {
1164 textview_write_line(textview, str, NULL, FALSE);
1166 if (lines % 500 == 0)
1168 if (textview->stop_loading) {
1173 ertf_parser_destroy(parser);
1176 #define ADD_TXT_POS(bp_, ep_, pti_) \
1177 if ((last->next = alloca(sizeof(struct txtpos))) != NULL) { \
1178 last = last->next; \
1179 last->bp = (bp_); last->ep = (ep_); last->pti = (pti_); \
1180 last->next = NULL; \
1182 g_warning("alloc error scanning URIs\n"); \
1183 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, \
1189 #define ADD_TXT_POS_LATER(bp_, ep_, pti_) \
1190 if ((last->next = alloca(sizeof(struct txtpos))) != NULL) { \
1191 last = last->next; \
1192 last->bp = (bp_); last->ep = (ep_); last->pti = (pti_); \
1193 last->next = NULL; \
1195 g_warning("alloc error scanning URIs\n"); \
1198 /* textview_make_clickable_parts() - colorizes clickable parts */
1199 static void textview_make_clickable_parts(TextView *textview,
1200 const gchar *fg_tag,
1201 const gchar *uri_tag,
1202 const gchar *linebuf,
1205 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1206 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
1208 gchar *mybuf = g_strdup(linebuf);
1210 /* parse table - in order of priority */
1212 const gchar *needle; /* token */
1214 /* token search function */
1215 gchar *(*search) (const gchar *haystack,
1216 const gchar *needle);
1217 /* part parsing function */
1218 gboolean (*parse) (const gchar *start,
1219 const gchar *scanpos,
1223 /* part to URI function */
1224 gchar *(*build_uri) (const gchar *bp,
1228 static struct table parser[] = {
1229 {"http://", strcasestr, get_uri_part, make_uri_string},
1230 {"https://", strcasestr, get_uri_part, make_uri_string},
1231 {"ftp://", strcasestr, get_uri_part, make_uri_string},
1232 {"sftp://", strcasestr, get_uri_part, make_uri_string},
1233 {"gopher://",strcasestr, get_uri_part, make_uri_string},
1234 {"www.", strcasestr, get_uri_part, make_http_string},
1235 {"mailto:", strcasestr, get_uri_part, make_uri_string},
1236 {"@", strcasestr, get_email_part, make_email_string}
1238 const gint PARSE_ELEMS = sizeof parser / sizeof parser[0];
1241 const gchar *walk, *bp, *ep;
1244 const gchar *bp, *ep; /* text position */
1245 gint pti; /* index in parse table */
1246 struct txtpos *next; /* next */
1247 } head = {NULL, NULL, 0, NULL}, *last = &head;
1249 if (!g_utf8_validate(linebuf, -1, NULL)) {
1251 mybuf = g_malloc(strlen(linebuf)*2 +1);
1252 conv_localetodisp(mybuf, strlen(linebuf)*2 +1, linebuf);
1255 gtk_text_buffer_get_end_iter(buffer, &iter);
1257 /* parse for clickable parts, and build a list of begin and end positions */
1258 for (walk = mybuf, n = 0;;) {
1259 gint last_index = PARSE_ELEMS;
1260 gchar *scanpos = NULL;
1262 /* FIXME: this looks phony. scanning for anything in the parse table */
1263 for (n = 0; n < PARSE_ELEMS; n++) {
1266 tmp = parser[n].search(walk, parser[n].needle);
1268 if (scanpos == NULL || tmp < scanpos) {
1276 /* check if URI can be parsed */
1277 if (parser[last_index].parse(walk, scanpos, &bp, &ep, hdr)
1278 && (size_t) (ep - bp - 1) > strlen(parser[last_index].needle)) {
1279 ADD_TXT_POS(bp, ep, last_index);
1283 strlen(parser[last_index].needle);
1288 /* colorize this line */
1290 const gchar *normal_text = mybuf;
1293 for (last = head.next; last != NULL;
1294 normal_text = last->ep, last = last->next) {
1296 uri = g_new0(ClickableText, 1);
1297 if (last->bp - normal_text > 0)
1298 gtk_text_buffer_insert_with_tags_by_name
1301 last->bp - normal_text,
1303 uri->uri = parser[last->pti].build_uri(last->bp,
1305 uri->start = gtk_text_iter_get_offset(&iter);
1306 gtk_text_buffer_insert_with_tags_by_name
1307 (buffer, &iter, last->bp, last->ep - last->bp,
1308 uri_tag, fg_tag, NULL);
1309 uri->end = gtk_text_iter_get_offset(&iter);
1310 uri->filename = NULL;
1311 textview->uri_list =
1312 g_slist_prepend(textview->uri_list, uri);
1316 gtk_text_buffer_insert_with_tags_by_name
1317 (buffer, &iter, normal_text, -1, fg_tag, NULL);
1319 gtk_text_buffer_insert_with_tags_by_name
1320 (buffer, &iter, mybuf, -1, fg_tag, NULL);
1325 /* textview_make_clickable_parts() - colorizes clickable parts */
1326 static void textview_make_clickable_parts_later(TextView *textview,
1327 gint start, gint end)
1329 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1330 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
1331 GtkTextIter start_iter, end_iter;
1334 /* parse table - in order of priority */
1336 const gchar *needle; /* token */
1338 /* token search function */
1339 gchar *(*search) (const gchar *haystack,
1340 const gchar *needle);
1341 /* part parsing function */
1342 gboolean (*parse) (const gchar *start,
1343 const gchar *scanpos,
1347 /* part to URI function */
1348 gchar *(*build_uri) (const gchar *bp,
1352 static struct table parser[] = {
1353 {"http://", strcasestr, get_uri_part, make_uri_string},
1354 {"https://", strcasestr, get_uri_part, make_uri_string},
1355 {"ftp://", strcasestr, get_uri_part, make_uri_string},
1356 {"sftp://", strcasestr, get_uri_part, make_uri_string},
1357 {"www.", strcasestr, get_uri_part, make_http_string},
1358 {"mailto:", strcasestr, get_uri_part, make_uri_string},
1359 {"@", strcasestr, get_email_part, make_email_string}
1361 const gint PARSE_ELEMS = sizeof parser / sizeof parser[0];
1364 const gchar *walk, *bp, *ep;
1367 const gchar *bp, *ep; /* text position */
1368 gint pti; /* index in parse table */
1369 struct txtpos *next; /* next */
1370 } head = {NULL, NULL, 0, NULL}, *last = &head;
1372 gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, start);
1373 gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, end);
1374 mybuf = gtk_text_buffer_get_text(buffer, &start_iter, &end_iter, FALSE);
1375 offset = gtk_text_iter_get_offset(&start_iter);
1377 /* parse for clickable parts, and build a list of begin and end positions */
1378 for (walk = mybuf, n = 0;;) {
1379 gint last_index = PARSE_ELEMS;
1380 gchar *scanpos = NULL;
1382 /* FIXME: this looks phony. scanning for anything in the parse table */
1383 for (n = 0; n < PARSE_ELEMS; n++) {
1386 tmp = parser[n].search(walk, parser[n].needle);
1388 if (scanpos == NULL || tmp < scanpos) {
1396 /* check if URI can be parsed */
1397 if (parser[last_index].parse(walk, scanpos, &bp, &ep, FALSE)
1398 && (size_t) (ep - bp - 1) > strlen(parser[last_index].needle)) {
1399 ADD_TXT_POS_LATER(bp, ep, last_index);
1403 strlen(parser[last_index].needle);
1408 /* colorize this line */
1411 for (last = head.next; last != NULL; last = last->next) {
1413 gint start_offset, end_offset;
1416 uri = g_new0(ClickableText, 1);
1417 uri->uri = parser[last->pti].build_uri(last->bp,
1421 old_char = tmp_str[last->ep - mybuf];
1422 tmp_str[last->ep - mybuf] = '\0';
1423 end_offset = g_utf8_strlen(tmp_str, -1);
1424 tmp_str[last->ep - mybuf] = old_char;
1426 old_char = tmp_str[last->bp - mybuf];
1427 tmp_str[last->bp - mybuf] = '\0';
1428 start_offset = g_utf8_strlen(tmp_str, -1);
1429 tmp_str[last->bp - mybuf] = old_char;
1431 gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, start_offset + offset);
1432 gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, end_offset + offset);
1434 uri->start = gtk_text_iter_get_offset(&start_iter);
1436 gtk_text_buffer_apply_tag_by_name(buffer, "link", &start_iter, &end_iter);
1438 uri->end = gtk_text_iter_get_offset(&end_iter);
1439 uri->filename = NULL;
1440 textview->uri_list =
1441 g_slist_prepend(textview->uri_list, uri);
1450 static void textview_write_line(TextView *textview, const gchar *str,
1451 CodeConverter *conv, gboolean do_quote_folding)
1454 GtkTextBuffer *buffer;
1456 gchar buf[BUFFSIZE];
1458 gint quotelevel = -1, real_quotelevel = -1;
1459 gchar quote_tag_str[10];
1461 text = GTK_TEXT_VIEW(textview->text);
1462 buffer = gtk_text_view_get_buffer(text);
1463 gtk_text_buffer_get_end_iter(buffer, &iter);
1466 strncpy2(buf, str, sizeof(buf));
1467 else if (conv_convert(conv, buf, sizeof(buf), str) < 0)
1468 conv_localetodisp(buf, sizeof(buf), str);
1473 /* change color of quotation
1474 >, foo>, _> ... ok, <foo>, foo bar>, foo-> ... ng
1475 Up to 3 levels of quotations are detected, and each
1476 level is colored using a different color. */
1477 if (prefs_common.enable_color
1478 && line_has_quote_char(buf, prefs_common.quote_chars)) {
1479 real_quotelevel = get_quote_level(buf, prefs_common.quote_chars);
1480 quotelevel = real_quotelevel;
1481 /* set up the correct foreground color */
1482 if (quotelevel > 2) {
1483 /* recycle colors */
1484 if (prefs_common.recycle_quote_colors)
1491 if (quotelevel == -1)
1494 g_snprintf(quote_tag_str, sizeof(quote_tag_str),
1495 "quote%d", quotelevel);
1496 fg_color = quote_tag_str;
1499 if (prefs_common.enable_color &&
1500 (strcmp(buf,"-- \n") == 0 || strcmp(buf, "- -- \n") == 0 || textview->is_in_signature)) {
1501 fg_color = "signature";
1502 textview->is_in_signature = TRUE;
1505 if (real_quotelevel > -1 && do_quote_folding) {
1506 if (!g_utf8_validate(buf, -1, NULL)) {
1507 gchar *utf8buf = NULL;
1508 utf8buf = g_malloc(BUFFSIZE);
1509 conv_localetodisp(utf8buf, BUFFSIZE, buf);
1510 strncpy2(buf, utf8buf, BUFFSIZE-1);
1514 if ( textview->prev_quote_level != real_quotelevel ) {
1516 uri = g_new0(ClickableText, 1);
1517 uri->uri = g_strdup("");
1518 uri->data = g_strdup(buf);
1519 uri->start = gtk_text_iter_get_offset(&iter);
1520 uri->is_quote = TRUE;
1521 uri->quote_level = real_quotelevel;
1522 uri->fg_color = g_strdup(fg_color);
1524 gtk_text_buffer_insert_with_tags_by_name
1525 (buffer, &iter, " [...]", -1,
1526 "qlink", fg_color, NULL);
1527 uri->end = gtk_text_iter_get_offset(&iter);
1528 gtk_text_buffer_insert(buffer, &iter, " \n", -1);
1530 uri->filename = NULL;
1531 textview->uri_list =
1532 g_slist_prepend(textview->uri_list, uri);
1534 textview->prev_quote_level = real_quotelevel;
1536 GSList *last = textview->uri_list;
1537 ClickableText *lasturi = NULL;
1538 gint e_len = 0, n_len = 0;
1540 if (textview->uri_list) {
1541 lasturi = (ClickableText *)last->data;
1543 g_print("oops (%d %d)\n",
1544 real_quotelevel, textview->prev_quote_level);
1547 if (lasturi->is_quote == FALSE) {
1548 textview->prev_quote_level = -1;
1551 e_len = lasturi->data ? strlen(lasturi->data):0;
1552 n_len = strlen(buf);
1553 lasturi->data = g_realloc((gchar *)lasturi->data, e_len + n_len + 1);
1554 strcpy((gchar *)lasturi->data + e_len, buf);
1555 *((gchar *)lasturi->data + e_len + n_len) = '\0';
1559 textview_make_clickable_parts(textview, fg_color, "link", buf, FALSE);
1560 textview->prev_quote_level = -1;
1564 void textview_write_link(TextView *textview, const gchar *str,
1565 const gchar *uri, CodeConverter *conv)
1567 GdkColor *link_color = NULL;
1569 GtkTextBuffer *buffer;
1571 gchar buf[BUFFSIZE];
1573 ClickableText *r_uri;
1575 if (!str || *str == '\0')
1580 while (uri && *uri && g_ascii_isspace(*uri))
1583 text = GTK_TEXT_VIEW(textview->text);
1584 buffer = gtk_text_view_get_buffer(text);
1585 gtk_text_buffer_get_end_iter(buffer, &iter);
1588 strncpy2(buf, str, sizeof(buf));
1589 else if (conv_convert(conv, buf, sizeof(buf), str) < 0)
1590 conv_utf8todisp(buf, sizeof(buf), str);
1592 if (g_utf8_validate(buf, -1, NULL) == FALSE)
1597 gtk_text_buffer_get_end_iter(buffer, &iter);
1598 for (bufp = buf; *bufp != '\0'; bufp = g_utf8_next_char(bufp)) {
1601 ch = g_utf8_get_char(bufp);
1602 if (!g_unichar_isspace(ch))
1606 gtk_text_buffer_insert(buffer, &iter, buf, bufp - buf);
1608 if (prefs_common.enable_color) {
1609 link_color = &uri_color;
1611 r_uri = g_new0(ClickableText, 1);
1612 r_uri->uri = g_strdup(uri);
1613 r_uri->start = gtk_text_iter_get_offset(&iter);
1614 gtk_text_buffer_insert_with_tags_by_name
1615 (buffer, &iter, bufp, -1, "link", NULL);
1616 r_uri->end = gtk_text_iter_get_offset(&iter);
1617 r_uri->filename = NULL;
1618 textview->uri_list = g_slist_prepend(textview->uri_list, r_uri);
1621 static void textview_set_cursor(GdkWindow *window, GdkCursor *cursor)
1623 if (GDK_IS_WINDOW(window))
1624 gdk_window_set_cursor(window, cursor);
1626 void textview_clear(TextView *textview)
1628 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1629 GtkTextBuffer *buffer;
1630 GdkWindow *window = gtk_text_view_get_window(text,
1631 GTK_TEXT_WINDOW_TEXT);
1633 buffer = gtk_text_view_get_buffer(text);
1634 gtk_text_buffer_set_text(buffer, "", -1);
1636 TEXTVIEW_STATUSBAR_POP(textview);
1637 textview_uri_list_remove_all(textview->uri_list);
1638 textview->uri_list = NULL;
1639 textview->uri_hover = NULL;
1640 textview->prev_quote_level = -1;
1642 textview->body_pos = 0;
1643 if (textview->image)
1644 gtk_widget_destroy(textview->image);
1645 textview->image = NULL;
1647 if (textview->messageview->mainwin->cursor_count == 0) {
1648 textview_set_cursor(window, text_cursor);
1650 textview_set_cursor(window, watch_cursor);
1654 void textview_destroy(TextView *textview)
1656 GtkTextBuffer *buffer;
1657 GtkClipboard *clipboard;
1659 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
1660 clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1661 gtk_text_buffer_remove_selection_clipboard(buffer, clipboard);
1663 textview_uri_list_remove_all(textview->uri_list);
1664 textview->uri_list = NULL;
1665 textview->prev_quote_level = -1;
1670 void textview_set_all_headers(TextView *textview, gboolean all_headers)
1672 textview->show_all_headers = all_headers;
1675 #define CHANGE_TAG_FONT(tagname, font) { \
1676 tag = gtk_text_tag_table_lookup(tags, tagname); \
1678 g_object_set(G_OBJECT(tag), "font-desc", font, NULL); \
1681 void textview_set_font(TextView *textview, const gchar *codeset)
1684 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
1685 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
1686 PangoFontDescription *font_desc, *bold_font_desc;
1688 font_desc = pango_font_description_from_string
1691 gtk_widget_modify_font(textview->text, font_desc);
1692 CHANGE_TAG_FONT("header", font_desc);
1693 CHANGE_TAG_FONT("hlink", font_desc);
1694 pango_font_description_free(font_desc);
1696 if (prefs_common.derive_from_normal_font || !BOLD_FONT) {
1697 bold_font_desc = pango_font_description_from_string
1700 pango_font_description_set_weight
1701 (bold_font_desc, PANGO_WEIGHT_BOLD);
1703 bold_font_desc = pango_font_description_from_string
1706 if (bold_font_desc) {
1707 CHANGE_TAG_FONT("header_title", bold_font_desc);
1708 pango_font_description_free(bold_font_desc);
1711 if (prefs_common.textfont) {
1712 PangoFontDescription *font_desc;
1714 font_desc = pango_font_description_from_string
1715 (prefs_common.textfont);
1717 gtk_widget_modify_font(textview->text, font_desc);
1718 pango_font_description_free(font_desc);
1721 gtk_text_view_set_pixels_above_lines(GTK_TEXT_VIEW(textview->text),
1722 prefs_common.line_space / 2);
1723 gtk_text_view_set_pixels_below_lines(GTK_TEXT_VIEW(textview->text),
1724 prefs_common.line_space / 2);
1727 void textview_set_text(TextView *textview, const gchar *text)
1730 GtkTextBuffer *buffer;
1732 cm_return_if_fail(textview != NULL);
1733 cm_return_if_fail(text != NULL);
1735 textview_clear(textview);
1737 view = GTK_TEXT_VIEW(textview->text);
1738 buffer = gtk_text_view_get_buffer(view);
1739 gtk_text_buffer_set_text(buffer, text, strlen(text));
1755 H_ORGANIZATION = 11,
1758 void textview_set_position(TextView *textview, gint pos)
1760 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1762 gtkut_text_view_set_position(text, pos);
1765 static gboolean header_is_internal(Header *header)
1767 const gchar *internal_hdrs[] =
1768 {"AF:", "NF:", "PS:", "SRH:", "SFN:", "DSR:", "MID:",
1769 "CFG:", "PT:", "S:", "RQ:", "SSV:", "NSV:", "SSH:",
1770 "R:", "MAID:", "SCF:", "RMID:", "FMID:", "NAID:",
1771 "X-Claws-Account-Id:", "X-Claws-Sign:", "X-Claws-Encrypt:",
1772 "X-Claws-Privacy-System:", "X-Claws-End-Special-Headers:",
1773 "X-Sylpheed-Account-Id:", "X-Sylpheed-Sign:", "X-Sylpheed-Encrypt:",
1774 "X-Claws-Auto-Wrapping:", "X-Claws-Auto-Indent:",
1775 "X-Sylpheed-Privacy-System:", "X-Sylpheed-End-Special-Headers:",
1779 for (i = 0; internal_hdrs[i]; i++) {
1780 if (!strcmp(header->name, internal_hdrs[i]))
1786 static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
1788 gchar buf[BUFFSIZE];
1789 GPtrArray *headers, *sorted_headers;
1790 GSList *disphdr_list;
1794 cm_return_val_if_fail(fp != NULL, NULL);
1796 if (textview->show_all_headers) {
1797 headers = procheader_get_header_array_asis(fp);
1798 sorted_headers = g_ptr_array_new();
1799 for (i = 0; i < headers->len; i++) {
1800 header = g_ptr_array_index(headers, i);
1801 if (!header_is_internal(header))
1802 g_ptr_array_add(sorted_headers, header);
1804 procheader_header_free(header);
1806 g_ptr_array_free(headers, TRUE);
1807 return sorted_headers;
1810 if (!prefs_common.display_header) {
1811 while (fgets(buf, sizeof(buf), fp) != NULL)
1812 if (buf[0] == '\r' || buf[0] == '\n') break;
1816 headers = procheader_get_header_array_asis(fp);
1818 sorted_headers = g_ptr_array_new();
1820 for (disphdr_list = prefs_common.disphdr_list; disphdr_list != NULL;
1821 disphdr_list = disphdr_list->next) {
1822 DisplayHeaderProp *dp =
1823 (DisplayHeaderProp *)disphdr_list->data;
1825 for (i = 0; i < headers->len; i++) {
1826 header = g_ptr_array_index(headers, i);
1828 if (procheader_headername_equal(header->name,
1831 procheader_header_free(header);
1833 g_ptr_array_add(sorted_headers, header);
1835 g_ptr_array_remove_index(headers, i);
1841 if (prefs_common.show_other_header) {
1842 for (i = 0; i < headers->len; i++) {
1843 header = g_ptr_array_index(headers, i);
1844 if (!header_is_internal(header)) {
1845 g_ptr_array_add(sorted_headers, header);
1847 procheader_header_free(header);
1850 g_ptr_array_free(headers, TRUE);
1852 procheader_header_array_destroy(headers);
1855 return sorted_headers;
1858 static void textview_show_face(TextView *textview)
1860 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1861 MsgInfo *msginfo = textview->messageview->msginfo;
1864 if (prefs_common.display_header_pane
1865 || !prefs_common.display_xface)
1868 if (!msginfo->extradata || !msginfo->extradata->face) {
1872 if (textview->image)
1873 gtk_widget_destroy(textview->image);
1875 textview->image = face_get_from_header(msginfo->extradata->face);
1876 cm_return_if_fail(textview->image != NULL);
1878 gtk_widget_show(textview->image);
1880 x = textview->text->allocation.width - WIDTH -5;
1882 gtk_text_view_add_child_in_window(text, textview->image,
1883 GTK_TEXT_WINDOW_TEXT, x, 5);
1885 gtk_widget_show_all(textview->text);
1890 if (textview->image)
1891 gtk_widget_destroy(textview->image);
1892 textview->image = NULL;
1895 void textview_show_icon(TextView *textview, const gchar *stock_id)
1897 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1900 if (textview->image)
1901 gtk_widget_destroy(textview->image);
1903 textview->image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_DIALOG);
1904 cm_return_if_fail(textview->image != NULL);
1906 gtk_widget_show(textview->image);
1908 x = textview->text->allocation.width - WIDTH -5;
1910 gtk_text_view_add_child_in_window(text, textview->image,
1911 GTK_TEXT_WINDOW_TEXT, x, 5);
1913 gtk_widget_show_all(textview->text);
1919 #if HAVE_LIBCOMPFACE
1920 static void textview_show_xface(TextView *textview)
1922 MsgInfo *msginfo = textview->messageview->msginfo;
1923 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1925 GdkWindow *window = NULL;
1927 if (prefs_common.display_header_pane
1928 || !prefs_common.display_xface)
1931 if (!msginfo || !msginfo->extradata)
1934 if (msginfo->extradata->face)
1937 if (!msginfo->extradata->xface || strlen(msginfo->extradata->xface) < 5) {
1941 if (textview->image)
1942 gtk_widget_destroy(textview->image);
1944 window = mainwindow_get_mainwindow() ?
1945 mainwindow_get_mainwindow()->window->window :
1946 textview->text->window;
1947 textview->image = xface_get_from_header(msginfo->extradata->xface,
1948 &textview->text->style->white,
1950 cm_return_if_fail(textview->image != NULL);
1952 gtk_widget_show(textview->image);
1954 x = textview->text->allocation.width - WIDTH -5;
1956 gtk_text_view_add_child_in_window(text, textview->image,
1957 GTK_TEXT_WINDOW_TEXT, x, 5);
1959 gtk_widget_show_all(textview->text);
1963 if (textview->image)
1964 gtk_widget_destroy(textview->image);
1965 textview->image = NULL;
1970 static void textview_save_contact_pic(TextView *textview)
1972 MsgInfo *msginfo = textview->messageview->msginfo;
1973 gchar *filename = NULL;
1974 GError *error = NULL;
1975 GdkPixbuf *picture = NULL;
1977 if (!msginfo->extradata || (!msginfo->extradata->face && !msginfo->extradata->xface))
1980 if (textview->image)
1981 picture = gtk_image_get_pixbuf(GTK_IMAGE(textview->image));
1983 filename = addrindex_get_picture_file(msginfo->from);
1986 if (!is_file_exist(filename)) {
1987 gdk_pixbuf_save(picture, filename, "png", &error, NULL);
1989 g_warning(_("Failed to save image: \n%s"),
1991 g_error_free(error);
1997 static void textview_show_contact_pic(TextView *textview)
1999 MsgInfo *msginfo = textview->messageview->msginfo;
2000 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
2002 gchar *filename = NULL;
2003 GError *error = NULL;
2004 GdkPixbuf *picture = NULL;
2007 if (prefs_common.display_header_pane
2008 || !prefs_common.display_xface)
2011 if (msginfo->extradata && (msginfo->extradata->face || msginfo->extradata->xface))
2014 if (textview->image)
2015 gtk_widget_destroy(textview->image);
2017 filename = addrindex_get_picture_file(msginfo->from);
2021 if (!is_file_exist(filename)) {
2026 gdk_pixbuf_get_file_info(filename, &w, &h);
2028 if (w > 48 || h > 48)
2029 picture = gdk_pixbuf_new_from_file_at_scale(filename,
2030 48, 48, TRUE, &error);
2032 picture = gdk_pixbuf_new_from_file(filename, &error);
2035 debug_print("Failed to import image: \n%s",
2037 g_error_free(error);
2043 textview->image = gtk_image_new_from_pixbuf(picture);
2044 g_object_unref(picture);
2046 cm_return_if_fail(textview->image != NULL);
2048 gtk_widget_show(textview->image);
2050 x = textview->text->allocation.width - WIDTH -5;
2052 gtk_text_view_add_child_in_window(text, textview->image,
2053 GTK_TEXT_WINDOW_TEXT, x, 5);
2055 gtk_widget_show_all(textview->text);
2059 if (textview->image)
2060 gtk_widget_destroy(textview->image);
2061 textview->image = NULL;
2065 static gint textview_tag_cmp_list(gconstpointer a, gconstpointer b)
2067 gint id_a = GPOINTER_TO_INT(a);
2068 gint id_b = GPOINTER_TO_INT(b);
2069 const gchar *tag_a = tags_get_tag(id_a);
2070 const gchar *tag_b = tags_get_tag(id_b);
2073 return tag_b == NULL ? 0:1;
2076 return tag_a == NULL ? 0:1;
2078 return g_utf8_collate(tag_a, tag_b);
2082 static void textview_show_tags(TextView *textview)
2084 MsgInfo *msginfo = textview->messageview->msginfo;
2085 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
2086 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
2090 gboolean found_tag = FALSE;
2095 cur = orig = g_slist_sort(g_slist_copy(msginfo->tags), textview_tag_cmp_list);
2097 for (; cur; cur = cur->next) {
2098 if (tags_get_tag(GPOINTER_TO_INT(cur->data)) != NULL) {
2108 gtk_text_buffer_get_end_iter (buffer, &iter);
2109 gtk_text_buffer_insert_with_tags_by_name(buffer,
2110 &iter, _("Tags: "), -1,
2111 "header_title", "header", "tags", NULL);
2113 for (cur = orig; cur; cur = cur->next) {
2114 const gchar *cur_tag = tags_get_tag(GPOINTER_TO_INT(cur->data));
2117 uri = g_new0(ClickableText, 1);
2118 uri->uri = g_strdup("");
2119 uri->start = gtk_text_iter_get_offset(&iter);
2120 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter,
2122 "link", "header", "tags", NULL);
2123 uri->end = gtk_text_iter_get_offset(&iter);
2124 uri->filename = g_strdup_printf("sc://search_tags:%s", cur_tag);
2126 textview->uri_list =
2127 g_slist_prepend(textview->uri_list, uri);
2128 if (cur->next && tags_get_tag(GPOINTER_TO_INT(cur->next->data)))
2129 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, ", ", 2,
2130 "header", "tags", NULL);
2132 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, " ", 1,
2133 "header", "tags", NULL);
2137 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, "\n", 1,
2138 "header", "tags", NULL);
2141 static void textview_show_header(TextView *textview, GPtrArray *headers)
2143 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
2144 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
2149 cm_return_if_fail(headers != NULL);
2151 for (i = 0; i < headers->len; i++) {
2152 header = g_ptr_array_index(headers, i);
2153 cm_return_if_fail(header->name != NULL);
2155 gtk_text_buffer_get_end_iter (buffer, &iter);
2156 if(prefs_common.trans_hdr == TRUE) {
2157 gchar *hdr = g_strndup(header->name, strlen(header->name) - 1);
2158 gchar *trans_hdr = gettext(hdr);
2159 gtk_text_buffer_insert_with_tags_by_name(buffer,
2160 &iter, trans_hdr, -1,
2161 "header_title", "header", NULL);
2162 gtk_text_buffer_insert_with_tags_by_name(buffer,
2163 &iter, ":", 1, "header_title", "header", NULL);
2166 gtk_text_buffer_insert_with_tags_by_name(buffer,
2167 &iter, header->name,
2168 -1, "header_title", "header", NULL);
2170 if (header->name[strlen(header->name) - 1] != ' ')
2171 gtk_text_buffer_insert_with_tags_by_name
2172 (buffer, &iter, " ", 1,
2173 "header_title", "header", NULL);
2175 if (procheader_headername_equal(header->name, "Subject") ||
2176 procheader_headername_equal(header->name, "From") ||
2177 procheader_headername_equal(header->name, "To") ||
2178 procheader_headername_equal(header->name, "Cc"))
2179 unfold_line(header->body);
2181 if (procheader_headername_equal(header->name, "Date") &&
2182 prefs_common.msgview_date_format) {
2185 procheader_date_parse(hbody, header->body, sizeof(hbody));
2186 gtk_text_buffer_get_end_iter (buffer, &iter);
2187 gtk_text_buffer_insert_with_tags_by_name
2188 (buffer, &iter, hbody, -1, "header", NULL);
2189 } else if ((procheader_headername_equal(header->name, "X-Mailer") ||
2190 procheader_headername_equal(header->name,
2192 (strstr(header->body, "Claws Mail") != NULL ||
2193 strstr(header->body, "Sylpheed-Claws") != NULL)) {
2194 gtk_text_buffer_get_end_iter (buffer, &iter);
2195 gtk_text_buffer_insert_with_tags_by_name
2196 (buffer, &iter, header->body, -1,
2197 "header", "emphasis", NULL);
2200 procheader_headername_equal(header->name, "From") ||
2201 procheader_headername_equal(header->name, "To") ||
2202 procheader_headername_equal(header->name, "Cc") ||
2203 procheader_headername_equal(header->name, "Bcc") ||
2204 procheader_headername_equal(header->name, "Reply-To") ||
2205 procheader_headername_equal(header->name, "Sender");
2206 textview_make_clickable_parts(textview, "header",
2207 "hlink", header->body,
2210 gtk_text_buffer_get_end_iter (buffer, &iter);
2211 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, "\n", 1,
2215 textview_show_face(textview);
2216 #if HAVE_LIBCOMPFACE
2217 textview_show_xface(textview);
2219 textview_save_contact_pic(textview);
2220 textview_show_contact_pic(textview);
2223 gboolean textview_search_string(TextView *textview, const gchar *str,
2226 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
2228 return gtkut_text_view_search_string(text, str, case_sens);
2231 gboolean textview_search_string_backward(TextView *textview, const gchar *str,
2234 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
2236 return gtkut_text_view_search_string_backward(text, str, case_sens);
2239 void textview_scroll_one_line(TextView *textview, gboolean up)
2241 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
2242 GtkAdjustment *vadj = text->vadjustment;
2244 gtkutils_scroll_one_line(GTK_WIDGET(text), vadj, up);
2247 gboolean textview_scroll_page(TextView *textview, gboolean up)
2249 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
2250 GtkAdjustment *vadj = text->vadjustment;
2252 return gtkutils_scroll_page(GTK_WIDGET(text), vadj, up);
2255 #define KEY_PRESS_EVENT_STOP() \
2256 g_signal_stop_emission_by_name(G_OBJECT(widget), \
2259 static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
2262 SummaryView *summaryview = NULL;
2263 MessageView *messageview = textview->messageview;
2265 if (!event) return FALSE;
2266 if (messageview->mainwin)
2267 summaryview = messageview->mainwin->summaryview;
2269 switch (event->keyval) {
2284 summary_pass_key_press_event(summaryview, event);
2286 mimeview_scroll_page
2287 (messageview->mimeview,
2289 (GDK_SHIFT_MASK|GDK_MOD1_MASK)) != 0);
2292 mimeview_scroll_page(messageview->mimeview, TRUE);
2296 mimeview_scroll_one_line
2297 (messageview->mimeview, (event->state &
2298 (GDK_SHIFT_MASK|GDK_MOD1_MASK)) != 0);
2302 summary_pass_key_press_event(summaryview, event);
2310 if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) == 0) {
2311 KEY_PRESS_EVENT_STOP();
2312 mimeview_pass_key_press_event(messageview->mimeview,
2316 /* possible fall through */
2319 event->window != messageview->mainwin->window->window) {
2320 GdkEventKey tmpev = *event;
2322 tmpev.window = messageview->mainwin->window->window;
2323 KEY_PRESS_EVENT_STOP();
2324 gtk_widget_event(messageview->mainwin->window,
2325 (GdkEvent *)&tmpev);
2333 static gboolean textview_motion_notify(GtkWidget *widget,
2334 GdkEventMotion *event,
2337 if (textview->loading)
2339 textview_uri_update(textview, event->x, event->y);
2340 gdk_window_get_pointer(widget->window, NULL, NULL, NULL);
2345 static gboolean textview_leave_notify(GtkWidget *widget,
2346 GdkEventCrossing *event,
2349 if (textview->loading)
2351 textview_uri_update(textview, -1, -1);
2356 static gboolean textview_visibility_notify(GtkWidget *widget,
2357 GdkEventVisibility *event,
2363 if (textview->loading)
2366 window = gtk_text_view_get_window(GTK_TEXT_VIEW(widget),
2367 GTK_TEXT_WINDOW_TEXT);
2369 /* check if occurred for the text window part */
2370 if (window != event->window)
2373 gdk_window_get_pointer(widget->window, &wx, &wy, NULL);
2374 textview_uri_update(textview, wx, wy);
2379 void textview_cursor_wait(TextView *textview)
2381 GdkWindow *window = gtk_text_view_get_window(
2382 GTK_TEXT_VIEW(textview->text),
2383 GTK_TEXT_WINDOW_TEXT);
2384 textview_set_cursor(window, watch_cursor);
2387 void textview_cursor_normal(TextView *textview)
2389 GdkWindow *window = gtk_text_view_get_window(
2390 GTK_TEXT_VIEW(textview->text),
2391 GTK_TEXT_WINDOW_TEXT);
2392 textview_set_cursor(window, text_cursor);
2395 static void textview_uri_update(TextView *textview, gint x, gint y)
2397 GtkTextBuffer *buffer;
2398 GtkTextIter start_iter, end_iter;
2399 ClickableText *uri = NULL;
2401 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
2403 if (x != -1 && y != -1) {
2409 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(textview->text),
2410 GTK_TEXT_WINDOW_WIDGET,
2412 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(textview->text),
2415 tags = gtk_text_iter_get_tags(&iter);
2416 for (cur = tags; cur != NULL; cur = cur->next) {
2417 GtkTextTag *tag = cur->data;
2420 g_object_get(G_OBJECT(tag), "name", &name, NULL);
2422 if ((!strcmp(name, "link") || !strcmp(name, "hlink"))
2423 && textview_get_uri_range(textview, &iter, tag,
2424 &start_iter, &end_iter)) {
2426 uri = textview_get_uri_from_range(textview,
2438 if (uri != textview->uri_hover) {
2441 if (textview->uri_hover)
2442 gtk_text_buffer_remove_tag_by_name(buffer,
2444 &textview->uri_hover_start_iter,
2445 &textview->uri_hover_end_iter);
2447 textview->uri_hover = uri;
2449 textview->uri_hover_start_iter = start_iter;
2450 textview->uri_hover_end_iter = end_iter;
2453 window = gtk_text_view_get_window(GTK_TEXT_VIEW(textview->text),
2454 GTK_TEXT_WINDOW_TEXT);
2455 if (textview->messageview->mainwin->cursor_count == 0) {
2456 textview_set_cursor(window, uri ? hand_cursor : text_cursor);
2458 textview_set_cursor(window, watch_cursor);
2461 TEXTVIEW_STATUSBAR_POP(textview);
2465 gtk_text_buffer_apply_tag_by_name(buffer,
2469 TEXTVIEW_STATUSBAR_PUSH(textview, uri->uri);
2474 static gboolean textview_get_uri_range(TextView *textview,
2477 GtkTextIter *start_iter,
2478 GtkTextIter *end_iter)
2480 return get_tag_range(iter, tag, start_iter, end_iter);
2483 static ClickableText *textview_get_uri_from_range(TextView *textview,
2486 GtkTextIter *start_iter,
2487 GtkTextIter *end_iter)
2489 gint start_pos, end_pos, cur_pos;
2490 ClickableText *uri = NULL;
2493 start_pos = gtk_text_iter_get_offset(start_iter);
2494 end_pos = gtk_text_iter_get_offset(end_iter);
2495 cur_pos = gtk_text_iter_get_offset(iter);
2497 for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
2498 ClickableText *uri_ = (ClickableText *)cur->data;
2499 if (start_pos == uri_->start &&
2500 end_pos == uri_->end) {
2505 for (cur = textview->uri_list; uri == NULL && cur != NULL; cur = cur->next) {
2506 ClickableText *uri_ = (ClickableText *)cur->data;
2507 if (start_pos == uri_->start ||
2508 end_pos == uri_->end) {
2509 /* in case of contiguous links, textview_get_uri_range
2510 * returns a broader range (start of 1st link to end
2512 * In that case, correct link is the one covering
2515 if (uri_->start <= cur_pos && cur_pos <= uri_->end) {
2525 static ClickableText *textview_get_uri(TextView *textview,
2529 GtkTextIter start_iter, end_iter;
2530 ClickableText *uri = NULL;
2532 if (textview_get_uri_range(textview, iter, tag, &start_iter,
2534 uri = textview_get_uri_from_range(textview, iter, tag,
2535 &start_iter, &end_iter);
2540 static void textview_shift_uris_after(TextView *textview, GSList *start_list, gint start, gint shift)
2544 start_list = textview->uri_list;
2546 for (cur = start_list; cur; cur = cur->next) {
2547 ClickableText *uri = (ClickableText *)cur->data;
2548 if (uri->start <= start)
2550 uri->start += shift;
2555 static void textview_remove_uris_in(TextView *textview, gint start, gint end)
2558 for (cur = textview->uri_list; cur; ) {
2559 ClickableText *uri = (ClickableText *)cur->data;
2560 if (uri->start > start && uri->end < end) {
2562 textview->uri_list = g_slist_remove(textview->uri_list, uri);
2564 g_free(uri->filename);
2565 if (uri->is_quote) {
2566 g_free(uri->fg_color);
2568 /* (only free data in quotes uris) */
2578 static void textview_toggle_quote(TextView *textview, GSList *start_list, ClickableText *uri, gboolean expand_only)
2580 GtkTextIter start, end;
2581 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
2586 if (uri->q_expanded && expand_only)
2589 gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
2590 gtk_text_buffer_get_iter_at_offset(buffer, &end, uri->end);
2591 if (textview->uri_hover)
2592 gtk_text_buffer_remove_tag_by_name(buffer,
2594 &textview->uri_hover_start_iter,
2595 &textview->uri_hover_end_iter);
2596 textview->uri_hover = NULL;
2597 gtk_text_buffer_remove_tag_by_name(buffer,
2601 /* when shifting URIs start and end, we have to do it per-UTF8-char
2602 * so use g_utf8_strlen(). OTOH, when inserting in the text buffer,
2603 * we have to pass a number of bytes, so use strlen(). disturbing. */
2605 if (!uri->q_expanded) {
2606 gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
2607 gtk_text_buffer_get_iter_at_offset(buffer, &end, uri->end);
2608 textview_shift_uris_after(textview, start_list, uri->start,
2609 g_utf8_strlen((gchar *)uri->data, -1)-strlen(" [...]\n"));
2610 gtk_text_buffer_delete(buffer, &start, &end);
2611 gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
2612 gtk_text_buffer_insert_with_tags_by_name
2613 (buffer, &start, (gchar *)uri->data,
2614 strlen((gchar *)uri->data)-1,
2615 "qlink", (gchar *)uri->fg_color, NULL);
2616 uri->end = gtk_text_iter_get_offset(&start);
2617 textview_make_clickable_parts_later(textview,
2618 uri->start, uri->end);
2619 uri->q_expanded = TRUE;
2621 gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
2622 gtk_text_buffer_get_iter_at_offset(buffer, &end, uri->end);
2623 textview_remove_uris_in(textview, uri->start, uri->end);
2624 textview_shift_uris_after(textview, start_list, uri->start,
2625 strlen(" [...]\n")-g_utf8_strlen((gchar *)uri->data, -1));
2626 gtk_text_buffer_delete(buffer, &start, &end);
2627 gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
2628 gtk_text_buffer_insert_with_tags_by_name
2629 (buffer, &start, " [...]", -1,
2630 "qlink", (gchar *)uri->fg_color, NULL);
2631 uri->end = gtk_text_iter_get_offset(&start);
2632 uri->q_expanded = FALSE;
2634 if (textview->messageview->mainwin->cursor_count == 0) {
2635 textview_cursor_normal(textview);
2637 textview_cursor_wait(textview);
2640 static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
2641 GdkEvent *event, GtkTextIter *iter,
2644 GdkEventButton *bevent;
2645 ClickableText *uri = NULL;
2647 gboolean qlink = FALSE;
2652 if (event->type != GDK_BUTTON_PRESS && event->type != GDK_2BUTTON_PRESS
2653 && event->type != GDK_MOTION_NOTIFY)
2656 uri = textview_get_uri(textview, iter, tag);
2660 g_object_get(G_OBJECT(tag), "name", &tagname, NULL);
2662 if (!strcmp(tagname, "qlink"))
2667 bevent = (GdkEventButton *) event;
2669 /* doubleclick: open compose / add address / browser */
2670 if (qlink && event->type == GDK_BUTTON_PRESS && bevent->button != 1) {
2671 /* pass rightclick through */
2673 } else if ((event->type == (qlink ? GDK_2BUTTON_PRESS:GDK_BUTTON_PRESS) && bevent->button == 1) ||
2674 bevent->button == 2 || bevent->button == 3) {
2675 if (uri->filename && !g_ascii_strncasecmp(uri->filename, "sc://", 5)) {
2676 MimeView *mimeview =
2677 (textview->messageview)?
2678 textview->messageview->mimeview:NULL;
2679 if (mimeview && bevent->button == 1) {
2680 mimeview_handle_cmd(mimeview, uri->filename, NULL, uri->data);
2681 } else if (mimeview && bevent->button == 2 &&
2682 !g_ascii_strcasecmp(uri->filename, "sc://select_attachment")) {
2683 mimeview_handle_cmd(mimeview, "sc://open_attachment", NULL, uri->data);
2684 } else if (mimeview && bevent->button == 3 &&
2685 !g_ascii_strcasecmp(uri->filename, "sc://select_attachment")) {
2686 mimeview_handle_cmd(mimeview, "sc://menu_attachment", bevent, uri->data);
2689 } else if (qlink && bevent->button == 1) {
2690 if (prefs_common.hide_quoted)
2691 textview_toggle_quote(textview, NULL, uri, FALSE);
2693 } else if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
2694 if (bevent->button == 3) {
2696 G_OBJECT(textview->mail_popup_menu),
2697 "menu_button", uri);
2698 gtk_menu_popup(GTK_MENU(textview->mail_popup_menu),
2699 NULL, NULL, NULL, NULL,
2700 bevent->button, bevent->time);
2702 PrefsAccount *account = NULL;
2703 FolderItem *folder_item = NULL;
2706 if (textview->messageview && textview->messageview->msginfo &&
2707 textview->messageview->msginfo->folder) {
2710 folder_item = textview->messageview->msginfo->folder;
2711 if (folder_item->prefs && folder_item->prefs->enable_default_account)
2712 account = account_find_from_id(folder_item->prefs->default_account);
2714 account = account_find_from_item(folder_item);
2716 compose = compose_new_with_folderitem(account,
2717 folder_item, uri->uri + 7);
2718 compose_check_for_email_account(compose);
2721 } else if (g_ascii_strncasecmp(uri->uri, "file:", 5)) {
2722 if (bevent->button == 1 &&
2723 textview_uri_security_check(textview, uri) == TRUE)
2725 prefs_common_get_uri_cmd());
2726 else if (bevent->button == 3 && !qlink) {
2728 G_OBJECT(textview->link_popup_menu),
2729 "menu_button", uri);
2730 gtk_menu_popup(GTK_MENU(textview->link_popup_menu),
2731 NULL, NULL, NULL, NULL,
2732 bevent->button, bevent->time);
2736 if (bevent->button == 3 && !qlink) {
2738 G_OBJECT(textview->file_popup_menu),
2739 "menu_button", uri);
2740 gtk_menu_popup(GTK_MENU(textview->file_popup_menu),
2741 NULL, NULL, NULL, NULL,
2742 bevent->button, bevent->time);
2751 gchar *textview_get_visible_uri (TextView *textview,
2754 GtkTextBuffer *buffer;
2755 GtkTextIter start, end;
2757 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
2759 gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
2760 gtk_text_buffer_get_iter_at_offset(buffer, &end, uri->end);
2762 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
2766 *\brief Check to see if a web URL has been disguised as a different
2767 * URL (possible with HTML email).
2769 *\param uri The uri to check
2771 *\param textview The TextView the URL is contained in
2773 *\return gboolean TRUE if the URL is ok, or if the user chose to open
2774 * it anyway, otherwise FALSE
2776 gboolean textview_uri_security_check(TextView *textview, ClickableText *uri)
2779 gboolean retval = TRUE;
2781 if (is_uri_string(uri->uri) == FALSE)
2784 visible_str = textview_get_visible_uri(textview, uri);
2785 if (visible_str == NULL)
2788 g_strstrip(visible_str);
2790 if (strcmp(visible_str, uri->uri) != 0 && is_uri_string(visible_str)) {
2792 gchar *visible_uri_path;
2794 uri_path = get_uri_path(uri->uri);
2795 visible_uri_path = get_uri_path(visible_str);
2796 if (path_cmp(uri_path, visible_uri_path) != 0)
2800 if (retval == FALSE) {
2804 msg = g_markup_printf_escaped(_("The real URL is different from "
2805 "the displayed URL.\n"
2807 "<b>Displayed URL:</b> %s\n"
2809 "<b>Real URL:</b> %s\n"
2812 visible_str,uri->uri);
2813 aval = alertpanel_full(_("Phishing attempt warning"), msg,
2814 GTK_STOCK_CANCEL, _("_Open URL"), NULL, FALSE,
2815 NULL, ALERT_WARNING, G_ALERTDEFAULT);
2817 if (aval == G_ALERTALTERNATE)
2821 g_free(visible_str);
2826 static void textview_uri_list_remove_all(GSList *uri_list)
2830 for (cur = uri_list; cur != NULL; cur = cur->next) {
2832 g_free(((ClickableText *)cur->data)->uri);
2833 g_free(((ClickableText *)cur->data)->filename);
2834 if (((ClickableText *)cur->data)->is_quote) {
2835 g_free(((ClickableText *)cur->data)->fg_color);
2836 g_free(((ClickableText *)cur->data)->data);
2837 /* (only free data in quotes uris) */
2843 g_slist_free(uri_list);
2846 static void open_uri_cb (GtkAction *action, TextView *textview)
2848 ClickableText *uri = g_object_get_data(G_OBJECT(textview->link_popup_menu),
2850 const gchar *raw_url = g_object_get_data(G_OBJECT(textview->link_popup_menu),
2854 if (textview_uri_security_check(textview, uri) == TRUE)
2856 prefs_common_get_uri_cmd());
2857 g_object_set_data(G_OBJECT(textview->link_popup_menu), "menu_button",
2861 open_uri(raw_url, prefs_common_get_uri_cmd());
2862 g_object_set_data(G_OBJECT(textview->link_popup_menu), "raw_url",
2867 static void open_image_cb (GtkAction *action, TextView *textview)
2869 ClickableText *uri = g_object_get_data(G_OBJECT(textview->file_popup_menu),
2875 gchar *filename = NULL;
2876 gchar *tmp_filename = NULL;
2881 if (uri->filename == NULL)
2884 filename = g_strdup(uri->filename);
2886 if (!g_utf8_validate(filename, -1, NULL)) {
2887 gchar *tmp = conv_filename_to_utf8(filename);
2892 subst_for_filename(filename);
2894 tmp_filename = g_filename_from_uri(uri->uri, NULL, NULL);
2895 copy_file(tmp_filename, filename, FALSE);
2896 g_free(tmp_filename);
2898 cmd = mailcap_get_command_for_type("image/jpeg", filename);
2900 gboolean remember = FALSE;
2901 cmd = input_dialog_combo_remember
2903 _("Enter the command-line to open file:\n"
2904 "('%s' will be replaced with file name)"),
2905 prefs_common.mime_open_cmd,
2906 prefs_common.mime_open_cmd_history,
2908 if (cmd && remember) {
2909 mailcap_update_default("image/jpeg", cmd);
2912 if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
2913 !strchr(p + 2, '%'))
2914 g_snprintf(buf, sizeof(buf), cmd, filename);
2916 g_warning("Image viewer command-line is invalid: '%s'", cmd);
2920 execute_command_line(buf, TRUE);
2925 g_object_set_data(G_OBJECT(textview->file_popup_menu), "menu_button",
2929 static void save_file_cb (GtkAction *action, TextView *textview)
2931 ClickableText *uri = g_object_get_data(G_OBJECT(textview->file_popup_menu),
2933 gchar *filename = NULL;
2934 gchar *filepath = NULL;
2935 gchar *filedir = NULL;
2936 gchar *tmp_filename = NULL;
2940 if (uri->filename == NULL)
2943 filename = g_strdup(uri->filename);
2945 if (!g_utf8_validate(filename, -1, NULL)) {
2946 gchar *tmp = conv_filename_to_utf8(filename);
2951 subst_for_filename(filename);
2953 if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
2954 filepath = g_strconcat(prefs_common.attach_save_dir,
2955 G_DIR_SEPARATOR_S, filename, NULL);
2957 filepath = g_strdup(filename);
2961 filename = filesel_select_file_save(_("Save as"), filepath);
2967 if (is_file_exist(filename)) {
2971 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
2973 aval = alertpanel(_("Overwrite"), res, GTK_STOCK_CANCEL,
2974 GTK_STOCK_OK, NULL);
2976 if (G_ALERTALTERNATE != aval)
2980 tmp_filename = g_filename_from_uri(uri->uri, NULL, NULL);
2981 copy_file(tmp_filename, filename, FALSE);
2982 g_free(tmp_filename);
2984 filedir = g_path_get_dirname(filename);
2985 if (filedir && strcmp(filedir, ".")) {
2986 g_free(prefs_common.attach_save_dir);
2987 prefs_common.attach_save_dir = g_filename_to_utf8(filedir, -1, NULL, NULL, NULL);
2993 g_object_set_data(G_OBJECT(textview->file_popup_menu), "menu_button",
2997 static void copy_uri_cb (GtkAction *action, TextView *textview)
2999 ClickableText *uri = g_object_get_data(G_OBJECT(textview->link_popup_menu),
3001 const gchar *raw_url = g_object_get_data(G_OBJECT(textview->link_popup_menu),
3004 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), uri->uri, -1);
3005 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), uri->uri, -1);
3006 g_object_set_data(G_OBJECT(textview->link_popup_menu), "menu_button",
3010 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), raw_url, -1);
3011 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), raw_url, -1);
3012 g_object_set_data(G_OBJECT(textview->link_popup_menu), "raw_url",
3017 static void add_uri_to_addrbook_cb (GtkAction *action, TextView *textview)
3019 gchar *fromname, *fromaddress;
3020 ClickableText *uri = g_object_get_data(G_OBJECT(textview->mail_popup_menu),
3022 GtkWidget *image = NULL;
3023 GdkPixbuf *picture = NULL;
3024 gboolean use_picture = FALSE;
3029 fromaddress = g_strdup(uri->uri + 7);
3031 if (textview->messageview->msginfo &&
3032 !strcmp2(fromaddress, textview->messageview->msginfo->from))
3035 fromname = procheader_get_fromname(fromaddress);
3036 extract_address(fromaddress);
3039 textview->messageview->msginfo &&
3040 textview->messageview->msginfo->extradata &&
3041 textview->messageview->msginfo->extradata->face) {
3042 image = face_get_from_header(textview->messageview->msginfo->extradata->face);
3044 #if HAVE_LIBCOMPFACE
3045 else if (use_picture &&
3046 textview->messageview->msginfo &&
3047 textview->messageview->msginfo->extradata &&
3048 textview->messageview->msginfo->extradata->xface) {
3049 image = xface_get_from_header(textview->messageview->msginfo->extradata->xface,
3050 &textview->text->style->white,
3051 mainwindow_get_mainwindow()->window->window);
3055 picture = gtk_image_get_pixbuf(GTK_IMAGE(image));
3057 addressbook_add_contact( fromname, fromaddress, NULL, picture);
3059 g_free(fromaddress);
3063 static void reply_to_uri_cb (GtkAction *action, TextView *textview)
3065 ClickableText *uri = g_object_get_data(G_OBJECT(textview->mail_popup_menu),
3067 if (!textview->messageview || !uri)
3070 compose_reply_to_address (textview->messageview,
3071 textview->messageview->msginfo, uri->uri+7);
3074 static void mail_to_uri_cb (GtkAction *action, TextView *textview)
3076 PrefsAccount *account = NULL;
3078 ClickableText *uri = g_object_get_data(G_OBJECT(textview->mail_popup_menu),
3083 if (textview->messageview && textview->messageview->msginfo &&
3084 textview->messageview->msginfo->folder) {
3085 FolderItem *folder_item;
3087 folder_item = textview->messageview->msginfo->folder;
3088 if (folder_item->prefs && folder_item->prefs->enable_default_account)
3089 account = account_find_from_id(folder_item->prefs->default_account);
3091 compose = compose_new_with_folderitem(account, folder_item, uri->uri+7);
3093 compose = compose_new(account, uri->uri + 7, NULL);
3095 compose_check_for_email_account(compose);
3098 static void copy_mail_to_uri_cb (GtkAction *action, TextView *textview)
3100 ClickableText *uri = g_object_get_data(G_OBJECT(textview->mail_popup_menu),
3105 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), uri->uri +7, -1);
3106 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), uri->uri +7, -1);
3107 g_object_set_data(G_OBJECT(textview->mail_popup_menu), "menu_button",
3111 void textview_get_selection_offsets(TextView *textview, gint *sel_start, gint *sel_end)
3113 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
3114 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
3115 GtkTextIter start, end;
3116 if (gtk_text_buffer_get_selection_bounds(buffer, &start, &end)) {
3118 *sel_start = gtk_text_iter_get_offset(&start);
3120 *sel_end = gtk_text_iter_get_offset(&end);