2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2016 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/>.
21 #include "claws-features.h"
27 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.h>
41 #include "summaryview.h"
42 #include "procheader.h"
43 #include "prefs_common.h"
51 #ifndef USE_ALT_ADDRBOOK
52 #include "addressbook.h"
53 #include "addrindex.h"
55 #include "addressbook-dbus.h"
56 #include "addressadd.h"
58 #include "displayheader.h"
61 #include "alertpanel.h"
63 #include "image_viewer.h"
65 #include "inputdialog.h"
68 #include "manage_window.h"
69 #include "folder_item_prefs.h"
73 static GdkColor quote_colors
[3] = {
74 {(gulong
)0, (gushort
)0, (gushort
)0, (gushort
)0},
75 {(gulong
)0, (gushort
)0, (gushort
)0, (gushort
)0},
76 {(gulong
)0, (gushort
)0, (gushort
)0, (gushort
)0}
79 static GdkColor quote_bgcolors
[3] = {
80 {(gulong
)0, (gushort
)0, (gushort
)0, (gushort
)0},
81 {(gulong
)0, (gushort
)0, (gushort
)0, (gushort
)0},
82 {(gulong
)0, (gushort
)0, (gushort
)0, (gushort
)0}
84 static GdkColor signature_color
= {
91 static GdkColor uri_color
= {
98 static GdkColor emphasis_color
= {
105 static GdkColor diff_added_color
= {
112 static GdkColor diff_deleted_color
= {
119 static GdkColor diff_hunk_color
= {
126 static GdkColor tags_bgcolor
= {
133 static GdkColor tags_color
= {
140 static GdkCursor
*hand_cursor
= NULL
;
141 static GdkCursor
*text_cursor
= NULL
;
142 static GdkCursor
*watch_cursor
= NULL
;
144 #define TEXTVIEW_STATUSBAR_PUSH(textview, str) \
145 { if (textview->messageview->statusbar) \
146 gtk_statusbar_push(GTK_STATUSBAR(textview->messageview->statusbar), \
147 textview->messageview->statusbar_cid, str); \
150 #define TEXTVIEW_STATUSBAR_POP(textview) \
151 { if (textview->messageview->statusbar) \
152 gtk_statusbar_pop(GTK_STATUSBAR(textview->messageview->statusbar), \
153 textview->messageview->statusbar_cid); \
156 static void textview_show_ertf (TextView
*textview
,
158 CodeConverter
*conv
);
159 static void textview_add_part (TextView
*textview
,
161 static void textview_add_parts (TextView
*textview
,
163 static void textview_write_body (TextView
*textview
,
165 static void textview_show_html (TextView
*textview
,
167 CodeConverter
*conv
);
169 static void textview_write_line (TextView
*textview
,
172 gboolean do_quote_folding
);
173 static void textview_write_link (TextView
*textview
,
176 CodeConverter
*conv
);
178 static GPtrArray
*textview_scan_header (TextView
*textview
,
180 static void textview_show_header (TextView
*textview
,
183 static gint
textview_key_pressed (GtkWidget
*widget
,
186 static gboolean
textview_motion_notify (GtkWidget
*widget
,
187 GdkEventMotion
*motion
,
189 static gboolean
textview_leave_notify (GtkWidget
*widget
,
190 GdkEventCrossing
*event
,
192 static gboolean
textview_visibility_notify (GtkWidget
*widget
,
193 GdkEventVisibility
*event
,
195 static void textview_uri_update (TextView
*textview
,
198 static gboolean
textview_get_uri_range (TextView
*textview
,
201 GtkTextIter
*start_iter
,
202 GtkTextIter
*end_iter
);
203 static ClickableText
*textview_get_uri_from_range (TextView
*textview
,
206 GtkTextIter
*start_iter
,
207 GtkTextIter
*end_iter
);
208 static ClickableText
*textview_get_uri (TextView
*textview
,
211 static gboolean
textview_uri_button_pressed (GtkTextTag
*tag
,
217 static void textview_uri_list_remove_all (GSList
*uri_list
);
219 static void textview_toggle_quote (TextView
*textview
,
222 gboolean expand_only
);
224 static void open_uri_cb (GtkAction
*action
,
226 static void copy_uri_cb (GtkAction
*action
,
228 static void add_uri_to_addrbook_cb (GtkAction
*action
,
230 static void reply_to_uri_cb (GtkAction
*action
,
232 static void mail_to_uri_cb (GtkAction
*action
,
234 static void copy_mail_to_uri_cb (GtkAction
*action
,
236 static void save_file_cb (GtkAction
*action
,
238 static void open_image_cb (GtkAction
*action
,
240 static void textview_show_tags(TextView
*textview
);
242 static GtkActionEntry textview_link_popup_entries
[] =
244 {"TextviewPopupLink", NULL
, "TextviewPopupLink" },
245 {"TextviewPopupLink/Open", NULL
, N_("_Open in web browser"), NULL
, NULL
, G_CALLBACK(open_uri_cb
) },
246 {"TextviewPopupLink/Copy", NULL
, N_("Copy this _link"), NULL
, NULL
, G_CALLBACK(copy_uri_cb
) },
249 static GtkActionEntry textview_mail_popup_entries
[] =
251 {"TextviewPopupMail", NULL
, "TextviewPopupMail" },
252 {"TextviewPopupMail/Compose", NULL
, N_("Compose _new message"), NULL
, NULL
, G_CALLBACK(mail_to_uri_cb
) },
253 {"TextviewPopupMail/ReplyTo", NULL
, N_("_Reply to this address"), NULL
, NULL
, G_CALLBACK(reply_to_uri_cb
) },
254 {"TextviewPopupMail/AddAB", NULL
, N_("Add to _Address book"), NULL
, NULL
, G_CALLBACK(add_uri_to_addrbook_cb
) },
255 {"TextviewPopupMail/Copy", NULL
, N_("Copy this add_ress"), NULL
, NULL
, G_CALLBACK(copy_mail_to_uri_cb
) },
258 static GtkActionEntry textview_file_popup_entries
[] =
260 {"TextviewPopupFile", NULL
, "TextviewPopupFile" },
261 {"TextviewPopupFile/Open", NULL
, N_("_Open image"), NULL
, NULL
, G_CALLBACK(open_image_cb
) },
262 {"TextviewPopupFile/Save", NULL
, N_("_Save image..."), NULL
, NULL
, G_CALLBACK(save_file_cb
) },
265 static void scrolled_cb (GtkAdjustment
*adj
, TextView
*textview
)
271 if (textview
->image
) {
272 GtkAllocation allocation
;
274 gtk_widget_get_allocation(textview
->text
, &allocation
);
275 x1
= allocation
.width
- WIDTH
- 5;
276 gtk_text_view_buffer_to_window_coords(
277 GTK_TEXT_VIEW(textview
->text
),
278 GTK_TEXT_WINDOW_TEXT
, x1
, 5, &x
, &y
);
279 gtk_text_view_move_child(GTK_TEXT_VIEW(textview
->text
),
280 textview
->image
, x1
, y
);
284 static void textview_size_allocate_cb (GtkWidget
*widget
,
285 GtkAllocation
*allocation
,
288 scrolled_cb(NULL
, (TextView
*)data
);
291 TextView
*textview_create(void)
295 GtkWidget
*scrolledwin
;
297 GtkTextBuffer
*buffer
;
298 GtkClipboard
*clipboard
;
301 debug_print("Creating text view...\n");
302 textview
= g_new0(TextView
, 1);
304 scrolledwin
= gtk_scrolled_window_new(NULL
, NULL
);
305 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin
),
306 GTK_POLICY_AUTOMATIC
,
307 GTK_POLICY_AUTOMATIC
);
308 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin
),
310 gtk_widget_set_size_request
311 (scrolledwin
, prefs_common
.mainview_width
, -1);
313 /* create GtkSText widgets for single-byte and multi-byte character */
314 text
= gtk_text_view_new();
315 gtk_widget_add_events(text
, GDK_LEAVE_NOTIFY_MASK
);
316 gtk_widget_show(text
);
317 gtk_text_view_set_editable(GTK_TEXT_VIEW(text
), FALSE
);
318 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text
), GTK_WRAP_WORD_CHAR
);
319 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(text
), FALSE
);
320 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text
), 6);
321 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text
), 6);
323 buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(text
));
324 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
325 gtk_text_buffer_add_selection_clipboard(buffer
, clipboard
);
327 gtk_widget_ensure_style(text
);
329 g_object_ref(scrolledwin
);
331 gtk_container_add(GTK_CONTAINER(scrolledwin
), text
);
333 g_signal_connect(G_OBJECT(text
), "key-press-event",
334 G_CALLBACK(textview_key_pressed
), textview
);
335 g_signal_connect(G_OBJECT(text
), "motion-notify-event",
336 G_CALLBACK(textview_motion_notify
), textview
);
337 g_signal_connect(G_OBJECT(text
), "leave-notify-event",
338 G_CALLBACK(textview_leave_notify
), textview
);
339 g_signal_connect(G_OBJECT(text
), "visibility-notify-event",
340 G_CALLBACK(textview_visibility_notify
), textview
);
341 adj
= gtk_scrolled_window_get_vadjustment(
342 GTK_SCROLLED_WINDOW(scrolledwin
));
343 g_signal_connect(G_OBJECT(adj
), "value-changed",
344 G_CALLBACK(scrolled_cb
), textview
);
345 g_signal_connect(G_OBJECT(text
), "size_allocate",
346 G_CALLBACK(textview_size_allocate_cb
),
350 gtk_widget_show(scrolledwin
);
352 vbox
= gtk_vbox_new(FALSE
, 0);
353 gtk_box_pack_start(GTK_BOX(vbox
), scrolledwin
, TRUE
, TRUE
, 0);
355 gtk_widget_show(vbox
);
358 textview
->ui_manager
= gtk_ui_manager_new();
359 textview
->link_action_group
= cm_menu_create_action_group_full(textview
->ui_manager
,
361 textview_link_popup_entries
,
362 G_N_ELEMENTS(textview_link_popup_entries
), (gpointer
)textview
);
363 textview
->mail_action_group
= cm_menu_create_action_group_full(textview
->ui_manager
,
365 textview_mail_popup_entries
,
366 G_N_ELEMENTS(textview_mail_popup_entries
), (gpointer
)textview
);
367 textview
->file_action_group
= cm_menu_create_action_group_full(textview
->ui_manager
,
369 textview_file_popup_entries
,
370 G_N_ELEMENTS(textview_file_popup_entries
), (gpointer
)textview
);
372 MENUITEM_ADDUI_MANAGER(textview
->ui_manager
, "/", "Menus", "Menus", GTK_UI_MANAGER_MENUBAR
)
373 MENUITEM_ADDUI_MANAGER(textview
->ui_manager
,
374 "/Menus", "TextviewPopupLink", "TextviewPopupLink", GTK_UI_MANAGER_MENU
)
375 MENUITEM_ADDUI_MANAGER(textview
->ui_manager
,
376 "/Menus", "TextviewPopupMail", "TextviewPopupMail", GTK_UI_MANAGER_MENU
)
377 MENUITEM_ADDUI_MANAGER(textview
->ui_manager
,
378 "/Menus", "TextviewPopupFile", "TextviewPopupFile", GTK_UI_MANAGER_MENU
)
380 MENUITEM_ADDUI_MANAGER(textview
->ui_manager
,
381 "/Menus/TextviewPopupLink", "Open", "TextviewPopupLink/Open", GTK_UI_MANAGER_MENUITEM
)
382 MENUITEM_ADDUI_MANAGER(textview
->ui_manager
,
383 "/Menus/TextviewPopupLink", "Copy", "TextviewPopupLink/Copy", GTK_UI_MANAGER_MENUITEM
)
384 MENUITEM_ADDUI_MANAGER(textview
->ui_manager
,
385 "/Menus/TextviewPopupMail", "Compose", "TextviewPopupMail/Compose", GTK_UI_MANAGER_MENUITEM
)
386 MENUITEM_ADDUI_MANAGER(textview
->ui_manager
,
387 "/Menus/TextviewPopupMail", "ReplyTo", "TextviewPopupMail/ReplyTo", GTK_UI_MANAGER_MENUITEM
)
388 MENUITEM_ADDUI_MANAGER(textview
->ui_manager
,
389 "/Menus/TextviewPopupMail", "AddAB", "TextviewPopupMail/AddAB", GTK_UI_MANAGER_MENUITEM
)
390 MENUITEM_ADDUI_MANAGER(textview
->ui_manager
,
391 "/Menus/TextviewPopupMail", "Copy", "TextviewPopupMail/Copy", GTK_UI_MANAGER_MENUITEM
)
392 MENUITEM_ADDUI_MANAGER(textview
->ui_manager
,
393 "/Menus/TextviewPopupFile", "Open", "TextviewPopupFile/Open", GTK_UI_MANAGER_MENUITEM
)
394 MENUITEM_ADDUI_MANAGER(textview
->ui_manager
,
395 "/Menus/TextviewPopupFile", "Save", "TextviewPopupFile/Save", GTK_UI_MANAGER_MENUITEM
)
397 textview
->link_popup_menu
= gtk_menu_item_get_submenu(GTK_MENU_ITEM(
398 gtk_ui_manager_get_widget(textview
->ui_manager
, "/Menus/TextviewPopupLink")) );
399 textview
->mail_popup_menu
= gtk_menu_item_get_submenu(GTK_MENU_ITEM(
400 gtk_ui_manager_get_widget(textview
->ui_manager
, "/Menus/TextviewPopupMail")) );
401 textview
->file_popup_menu
= gtk_menu_item_get_submenu(GTK_MENU_ITEM(
402 gtk_ui_manager_get_widget(textview
->ui_manager
, "/Menus/TextviewPopupFile")) );
404 textview
->vbox
= vbox
;
405 textview
->scrolledwin
= scrolledwin
;
406 textview
->text
= text
;
407 textview
->uri_list
= NULL
;
408 textview
->body_pos
= 0;
409 textview
->last_buttonpress
= GDK_NOTHING
;
410 textview
->image
= NULL
;
414 static void textview_create_tags(GtkTextView
*text
, TextView
*textview
)
416 GtkTextBuffer
*buffer
;
417 GtkTextTag
*tag
, *qtag
;
418 static PangoFontDescription
*font_desc
, *bold_font_desc
;
421 font_desc
= pango_font_description_from_string
424 if (!bold_font_desc
) {
425 if (prefs_common
.derive_from_normal_font
|| !BOLD_FONT
) {
426 bold_font_desc
= pango_font_description_from_string
428 pango_font_description_set_weight
429 (bold_font_desc
, PANGO_WEIGHT_BOLD
);
431 bold_font_desc
= pango_font_description_from_string
436 buffer
= gtk_text_view_get_buffer(text
);
438 gtk_text_buffer_create_tag(buffer
, "header",
439 "pixels-above-lines", 0,
440 "pixels-above-lines-set", TRUE
,
441 "pixels-below-lines", 0,
442 "pixels-below-lines-set", TRUE
,
443 "font-desc", font_desc
,
445 "left-margin-set", TRUE
,
447 gtk_text_buffer_create_tag(buffer
, "header_title",
448 "font-desc", bold_font_desc
,
450 tag
= gtk_text_buffer_create_tag(buffer
, "hlink",
451 "pixels-above-lines", 0,
452 "pixels-above-lines-set", TRUE
,
453 "pixels-below-lines", 0,
454 "pixels-below-lines-set", TRUE
,
455 "font-desc", font_desc
,
457 "left-margin-set", TRUE
,
458 "foreground-gdk", &uri_color
,
460 g_signal_connect(G_OBJECT(tag
), "event",
461 G_CALLBACK(textview_uri_button_pressed
), textview
);
462 if (prefs_common
.enable_bgcolor
) {
463 gtk_text_buffer_create_tag(buffer
, "quote0",
464 "foreground-gdk", "e_colors
[0],
465 "paragraph-background-gdk", "e_bgcolors
[0],
467 gtk_text_buffer_create_tag(buffer
, "quote1",
468 "foreground-gdk", "e_colors
[1],
469 "paragraph-background-gdk", "e_bgcolors
[1],
471 gtk_text_buffer_create_tag(buffer
, "quote2",
472 "foreground-gdk", "e_colors
[2],
473 "paragraph-background-gdk", "e_bgcolors
[2],
476 gtk_text_buffer_create_tag(buffer
, "quote0",
477 "foreground-gdk", "e_colors
[0],
479 gtk_text_buffer_create_tag(buffer
, "quote1",
480 "foreground-gdk", "e_colors
[1],
482 gtk_text_buffer_create_tag(buffer
, "quote2",
483 "foreground-gdk", "e_colors
[2],
486 gtk_text_buffer_create_tag(buffer
, "tags",
487 "foreground-gdk", &tags_color
,
488 "paragraph-background-gdk", &tags_bgcolor
,
490 gtk_text_buffer_create_tag(buffer
, "emphasis",
491 "foreground-gdk", &emphasis_color
,
493 gtk_text_buffer_create_tag(buffer
, "signature",
494 "foreground-gdk", &signature_color
,
496 tag
= gtk_text_buffer_create_tag(buffer
, "link",
497 "foreground-gdk", &uri_color
,
499 qtag
= gtk_text_buffer_create_tag(buffer
, "qlink",
501 gtk_text_buffer_create_tag(buffer
, "link-hover",
502 "underline", PANGO_UNDERLINE_SINGLE
,
504 gtk_text_buffer_create_tag(buffer
, "diff-add",
505 "foreground-gdk", &diff_added_color
,
507 gtk_text_buffer_create_tag(buffer
, "diff-del",
508 "foreground-gdk", &diff_deleted_color
,
510 gtk_text_buffer_create_tag(buffer
, "diff-add-file",
511 "foreground-gdk", &diff_added_color
,
512 "weight", PANGO_WEIGHT_BOLD
,
514 gtk_text_buffer_create_tag(buffer
, "diff-del-file",
515 "foreground-gdk", &diff_deleted_color
,
516 "weight", PANGO_WEIGHT_BOLD
,
518 gtk_text_buffer_create_tag(buffer
, "diff-hunk",
519 "foreground-gdk", &diff_hunk_color
,
520 "weight", PANGO_WEIGHT_BOLD
,
522 g_signal_connect(G_OBJECT(qtag
), "event",
523 G_CALLBACK(textview_uri_button_pressed
), textview
);
524 g_signal_connect(G_OBJECT(tag
), "event",
525 G_CALLBACK(textview_uri_button_pressed
), textview
);
527 pango_font_description_free(font_desc);
529 pango_font_description_free(bold_font_desc);*/
532 void textview_init(TextView
*textview
)
535 hand_cursor
= gdk_cursor_new(GDK_HAND2
);
537 text_cursor
= gdk_cursor_new(GDK_XTERM
);
539 watch_cursor
= gdk_cursor_new(GDK_WATCH
);
541 textview_reflect_prefs(textview
);
542 textview_set_font(textview
, NULL
);
543 textview_create_tags(GTK_TEXT_VIEW(textview
->text
), textview
);
546 #define CHANGE_TAG_COLOR(tagname, colorfg, colorbg) { \
547 tag = gtk_text_tag_table_lookup(tags, tagname); \
549 g_object_set(G_OBJECT(tag), "foreground-gdk", colorfg, "paragraph-background-gdk", colorbg, NULL); \
552 static void textview_update_message_colors(TextView
*textview
)
554 GdkColor black
= {0, 0, 0, 0};
555 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview
->text
));
557 GtkTextTagTable
*tags
= gtk_text_buffer_get_tag_table(buffer
);
558 GtkTextTag
*tag
= NULL
;
560 quote_bgcolors
[0] = quote_bgcolors
[1] = quote_bgcolors
[2] = black
;
561 quote_colors
[0] = quote_colors
[1] = quote_colors
[2] = black
;
562 uri_color
= emphasis_color
= signature_color
= diff_added_color
=
563 diff_deleted_color
= diff_hunk_color
= black
;
564 tags_bgcolor
= tags_color
= black
;
566 if (prefs_common
.enable_color
) {
567 /* grab the quote colors, converting from an int to a GdkColor */
568 gtkut_convert_int_to_gdk_color(prefs_common
.quote_level1_col
,
570 gtkut_convert_int_to_gdk_color(prefs_common
.quote_level2_col
,
572 gtkut_convert_int_to_gdk_color(prefs_common
.quote_level3_col
,
574 gtkut_convert_int_to_gdk_color(prefs_common
.uri_col
,
576 gtkut_convert_int_to_gdk_color(prefs_common
.signature_col
,
578 gtkut_convert_int_to_gdk_color(prefs_common
.emphasis_col
,
580 gtkut_convert_int_to_gdk_color(prefs_common
.diff_added_color
,
582 gtkut_convert_int_to_gdk_color(prefs_common
.diff_deleted_color
,
583 &diff_deleted_color
);
584 gtkut_convert_int_to_gdk_color(prefs_common
.diff_hunk_color
,
587 if (prefs_common
.enable_color
&& prefs_common
.enable_bgcolor
) {
588 gtkut_convert_int_to_gdk_color(prefs_common
.quote_level1_bgcol
,
590 gtkut_convert_int_to_gdk_color(prefs_common
.quote_level2_bgcol
,
592 gtkut_convert_int_to_gdk_color(prefs_common
.quote_level3_bgcol
,
594 CHANGE_TAG_COLOR("quote0", "e_colors
[0], "e_bgcolors
[0]);
595 CHANGE_TAG_COLOR("quote1", "e_colors
[1], "e_bgcolors
[1]);
596 CHANGE_TAG_COLOR("quote2", "e_colors
[2], "e_bgcolors
[2]);
598 CHANGE_TAG_COLOR("quote0", "e_colors
[0], NULL
);
599 CHANGE_TAG_COLOR("quote1", "e_colors
[1], NULL
);
600 CHANGE_TAG_COLOR("quote2", "e_colors
[2], NULL
);
603 CHANGE_TAG_COLOR("emphasis", &emphasis_color
, NULL
);
604 CHANGE_TAG_COLOR("signature", &signature_color
, NULL
);
605 CHANGE_TAG_COLOR("link", &uri_color
, NULL
);
606 CHANGE_TAG_COLOR("link-hover", &uri_color
, NULL
);
607 CHANGE_TAG_COLOR("diff-add", &diff_added_color
, NULL
);
608 CHANGE_TAG_COLOR("diff-del", &diff_deleted_color
, NULL
);
609 CHANGE_TAG_COLOR("diff-add-file", &diff_added_color
, NULL
);
610 CHANGE_TAG_COLOR("diff-del-file", &diff_deleted_color
, NULL
);
611 CHANGE_TAG_COLOR("diff-hunk", &diff_hunk_color
, NULL
);
613 gtkut_convert_int_to_gdk_color(prefs_common
.tags_bgcolor
,
615 gtkut_convert_int_to_gdk_color(prefs_common
.tags_color
,
618 #undef CHANGE_TAG_COLOR
620 void textview_reflect_prefs(TextView
*textview
)
622 textview_set_font(textview
, NULL
);
623 textview_update_message_colors(textview
);
624 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview
->text
),
625 prefs_common
.textview_cursor_visible
);
628 void textview_show_part(TextView
*textview
, MimeInfo
*mimeinfo
, FILE *fp
)
631 cm_return_if_fail(mimeinfo
!= NULL
);
632 cm_return_if_fail(fp
!= NULL
);
634 textview
->loading
= TRUE
;
635 textview
->stop_loading
= FALSE
;
637 textview_clear(textview
);
639 if (mimeinfo
->type
== MIMETYPE_MULTIPART
||
640 (mimeinfo
->type
== MIMETYPE_MESSAGE
&& !g_ascii_strcasecmp(mimeinfo
->subtype
, "rfc822"))) {
641 textview_add_parts(textview
, mimeinfo
);
643 if (fseek(fp
, mimeinfo
->offset
, SEEK_SET
) < 0)
646 textview_write_body(textview
, mimeinfo
);
649 textview
->loading
= FALSE
;
650 textview
->stop_loading
= FALSE
;
651 textview_set_position(textview
, 0);
656 static void textview_add_part(TextView
*textview
, MimeInfo
*mimeinfo
)
658 GtkAllocation allocation
;
660 GtkTextBuffer
*buffer
;
661 GtkTextIter iter
, start_iter
;
663 GPtrArray
*headers
= NULL
;
670 cm_return_if_fail(mimeinfo
!= NULL
);
671 text
= GTK_TEXT_VIEW(textview
->text
);
672 buffer
= gtk_text_view_get_buffer(text
);
673 charcount
= gtk_text_buffer_get_char_count(buffer
);
674 gtk_text_buffer_get_end_iter(buffer
, &iter
);
676 if (textview
->stop_loading
) {
679 if (mimeinfo
->type
== MIMETYPE_MULTIPART
) {
684 textview
->prev_quote_level
= -1;
686 if ((mimeinfo
->type
== MIMETYPE_MESSAGE
) && !g_ascii_strcasecmp(mimeinfo
->subtype
, "rfc822")) {
688 if (mimeinfo
->content
== MIMECONTENT_MEM
)
689 fp
= str_open_as_stream(mimeinfo
->data
.mem
);
691 fp
= g_fopen(mimeinfo
->data
.filename
, "rb");
693 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fopen");
697 if (fseek(fp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
698 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
703 headers
= textview_scan_header(textview
, fp
);
706 gtk_text_buffer_insert(buffer
, &iter
, "\n", 1);
708 if (procmime_mimeinfo_parent(mimeinfo
) == NULL
)
709 textview_show_tags(textview
);
710 textview_show_header(textview
, headers
);
711 procheader_header_array_destroy(headers
);
718 name
= procmime_mimeinfo_get_parameter(mimeinfo
, "filename");
719 content_type
= procmime_get_content_type_str(mimeinfo
->type
,
722 name
= procmime_mimeinfo_get_parameter(mimeinfo
, "name");
724 g_snprintf(buf
, sizeof(buf
), _("[%s %s (%d bytes)]"),
725 name
, content_type
, mimeinfo
->length
);
727 g_snprintf(buf
, sizeof(buf
), _("[%s (%d bytes)]"),
728 content_type
, mimeinfo
->length
);
730 g_free(content_type
);
732 if (mimeinfo
->disposition
== DISPOSITIONTYPE_ATTACHMENT
733 || (mimeinfo
->disposition
== DISPOSITIONTYPE_INLINE
&&
734 mimeinfo
->type
!= MIMETYPE_TEXT
)) {
735 gtk_text_buffer_insert(buffer
, &iter
, "\n", 1);
736 TEXTVIEW_INSERT_LINK(buf
, "sc://select_attachment", mimeinfo
);
737 gtk_text_buffer_insert(buffer
, &iter
, " \n", -1);
738 if (mimeinfo
->type
== MIMETYPE_IMAGE
&&
739 prefs_common
.inline_img
) {
741 GError
*error
= NULL
;
746 START_TIMING("inserting image");
748 filename
= procmime_get_tmp_file_name(mimeinfo
);
750 if ((err
= procmime_get_part(filename
, mimeinfo
)) < 0) {
751 g_warning("Can't get the image file.(%s)", g_strerror(-err
));
757 pixbuf
= gdk_pixbuf_new_from_file(filename
, &error
);
758 if (textview
->stop_loading
) {
762 g_warning("%s", error
->message
);
766 g_warning("Can't load the image.");
772 gtk_widget_get_allocation(textview
->scrolledwin
, &allocation
);
773 pixbuf
= claws_load_pixbuf_fitting(pixbuf
,
777 if (textview
->stop_loading
) {
781 uri_str
= g_filename_to_uri(filename
, NULL
, NULL
);
783 uri
= g_new0(ClickableText
, 1);
785 uri
->start
= gtk_text_iter_get_offset(&iter
);
787 gtk_text_buffer_insert_pixbuf(buffer
, &iter
, pixbuf
);
788 if (textview
->stop_loading
) {
792 uri
->end
= uri
->start
+ 1;
793 uri
->filename
= procmime_get_part_file_name(mimeinfo
);
795 g_slist_prepend(textview
->uri_list
, uri
);
797 gtk_text_buffer_insert(buffer
, &iter
, " ", 1);
798 gtk_text_buffer_get_iter_at_offset(buffer
, &start_iter
, uri
->start
);
799 gtk_text_buffer_apply_tag_by_name(buffer
, "link",
802 gtk_text_buffer_insert_pixbuf(buffer
, &iter
, pixbuf
);
803 if (textview
->stop_loading
) {
806 gtk_text_buffer_insert(buffer
, &iter
, " ", 1);
809 g_object_unref(pixbuf
);
814 } else if (mimeinfo
->type
== MIMETYPE_TEXT
) {
815 if (prefs_common
.display_header
&& (charcount
> 0))
816 gtk_text_buffer_insert(buffer
, &iter
, "\n", 1);
818 if (!gtk_text_buffer_get_mark(buffer
, "body_start")) {
819 gtk_text_buffer_get_end_iter(buffer
, &iter
);
820 gtk_text_buffer_create_mark(buffer
, "body_start", &iter
, TRUE
);
823 textview_write_body(textview
, mimeinfo
);
825 if (!gtk_text_buffer_get_mark(buffer
, "body_end")) {
826 gtk_text_buffer_get_end_iter(buffer
, &iter
);
827 gtk_text_buffer_create_mark(buffer
, "body_end", &iter
, TRUE
);
833 static void recursive_add_parts(TextView
*textview
, GNode
*node
)
839 mimeinfo
= (MimeInfo
*) node
->data
;
841 textview_add_part(textview
, mimeinfo
);
843 textview_set_position(textview
, 0);
845 if ((mimeinfo
->type
!= MIMETYPE_MULTIPART
) &&
846 (mimeinfo
->type
!= MIMETYPE_MESSAGE
)) {
850 if (g_ascii_strcasecmp(mimeinfo
->subtype
, "alternative") == 0) {
851 GNode
* preferred_body
;
859 preferred_body
= NULL
;
862 for (iter
= g_node_first_child(node
) ; iter
!= NULL
;
863 iter
= g_node_next_sibling(iter
)) {
868 submime
= (MimeInfo
*) iter
->data
;
869 if (submime
->type
== MIMETYPE_TEXT
)
872 if (submime
->subtype
!= NULL
) {
873 if (g_ascii_strcasecmp(submime
->subtype
, "plain") == 0)
877 if (score
> preferred_score
) {
878 preferred_score
= score
;
879 preferred_body
= iter
;
883 if (preferred_body
!= NULL
) {
884 recursive_add_parts(textview
, preferred_body
);
888 for (iter
= g_node_first_child(node
) ; iter
!= NULL
;
889 iter
= g_node_next_sibling(iter
)) {
890 recursive_add_parts(textview
, iter
);
896 static void textview_add_parts(TextView
*textview
, MimeInfo
*mimeinfo
)
898 cm_return_if_fail(mimeinfo
!= NULL
);
899 cm_return_if_fail(mimeinfo
->node
!= NULL
);
901 recursive_add_parts(textview
, mimeinfo
->node
);
904 void textview_show_error(TextView
*textview
)
907 GtkTextBuffer
*buffer
;
910 textview_set_font(textview
, NULL
);
911 textview_clear(textview
);
913 text
= GTK_TEXT_VIEW(textview
->text
);
914 buffer
= gtk_text_view_get_buffer(text
);
915 gtk_text_buffer_get_start_iter(buffer
, &iter
);
917 TEXTVIEW_INSERT(_("\n"
918 " This message can't be displayed.\n"
919 " This is probably due to a network error.\n"
922 TEXTVIEW_INSERT_LINK(_("'Network Log'"), "sc://view_log", NULL
);
923 TEXTVIEW_INSERT(_(" in the Tools menu for more information."));
924 textview_show_icon(textview
, GTK_STOCK_DIALOG_ERROR
);
927 void textview_show_info(TextView
*textview
, const gchar
*info_str
)
930 GtkTextBuffer
*buffer
;
933 textview_set_font(textview
, NULL
);
934 textview_clear(textview
);
936 text
= GTK_TEXT_VIEW(textview
->text
);
937 buffer
= gtk_text_view_get_buffer(text
);
938 gtk_text_buffer_get_start_iter(buffer
, &iter
);
940 TEXTVIEW_INSERT(info_str
);
941 textview_show_icon(textview
, GTK_STOCK_DIALOG_INFO
);
942 textview_cursor_normal(textview
);
945 void textview_show_mime_part(TextView
*textview
, MimeInfo
*partinfo
)
948 GtkTextBuffer
*buffer
;
951 gchar
*content_type
, *shortcut
;
952 GtkUIManager
*ui_manager
;
954 if (!partinfo
) return;
956 if (textview
->messageview
->window
!= NULL
)
957 ui_manager
= textview
->messageview
->ui_manager
;
959 ui_manager
= textview
->messageview
->mainwin
->ui_manager
;
961 textview_set_font(textview
, NULL
);
962 textview_clear(textview
);
964 text
= GTK_TEXT_VIEW(textview
->text
);
965 buffer
= gtk_text_view_get_buffer(text
);
966 gtk_text_buffer_get_start_iter(buffer
, &iter
);
968 TEXTVIEW_INSERT("\n");
970 name
= procmime_mimeinfo_get_parameter(partinfo
, "filename");
972 name
= procmime_mimeinfo_get_parameter(partinfo
, "name");
974 content_type
= procmime_get_content_type_str(partinfo
->type
,
976 TEXTVIEW_INSERT(" ");
977 TEXTVIEW_INSERT_BOLD(name
);
978 TEXTVIEW_INSERT(" (");
979 TEXTVIEW_INSERT(content_type
);
980 TEXTVIEW_INSERT(", ");
981 TEXTVIEW_INSERT(to_human_readable((goffset
)partinfo
->length
));
982 TEXTVIEW_INSERT("):\n\n");
984 g_free(content_type
);
986 TEXTVIEW_INSERT(_(" The following can be performed on this part\n"));
988 TEXTVIEW_INSERT(_(" by right-clicking the icon or list item:"));
990 TEXTVIEW_INSERT("\n");
992 TEXTVIEW_INSERT(_(" - To save, select "));
993 TEXTVIEW_INSERT_LINK(_("'Save as...'"), "sc://save_as", NULL
);
995 TEXTVIEW_INSERT(_(" (Shortcut key: '"));
996 shortcut
= cm_menu_item_get_shortcut(ui_manager
, "Menu/File/SavePartAs");
997 TEXTVIEW_INSERT(shortcut
);
999 TEXTVIEW_INSERT("')");
1001 TEXTVIEW_INSERT("\n");
1003 TEXTVIEW_INSERT(_(" - To display as text, select "));
1004 TEXTVIEW_INSERT_LINK(_("'Display as text'"), "sc://display_as_text", NULL
);
1006 #ifndef GENERIC_UMPC
1007 TEXTVIEW_INSERT(_(" (Shortcut key: '"));
1008 shortcut
= cm_menu_item_get_shortcut(ui_manager
, "Menu/View/Part/AsText");
1009 TEXTVIEW_INSERT(shortcut
);
1011 TEXTVIEW_INSERT("')");
1013 TEXTVIEW_INSERT("\n");
1015 TEXTVIEW_INSERT(_(" - To open with an external program, select "));
1016 TEXTVIEW_INSERT_LINK(_("'Open'"), "sc://open", NULL
);
1018 #ifndef GENERIC_UMPC
1019 TEXTVIEW_INSERT(_(" (Shortcut key: '"));
1020 shortcut
= cm_menu_item_get_shortcut(ui_manager
, "Menu/View/Part/Open");
1021 TEXTVIEW_INSERT(shortcut
);
1023 TEXTVIEW_INSERT("')\n");
1024 TEXTVIEW_INSERT(_(" (alternately double-click, or click the middle "));
1025 TEXTVIEW_INSERT(_("mouse button)\n"));
1027 TEXTVIEW_INSERT(_(" - Or use "));
1028 TEXTVIEW_INSERT_LINK(_("'Open with...'"), "sc://open_with", NULL
);
1029 TEXTVIEW_INSERT(_(" (Shortcut key: '"));
1030 shortcut
= cm_menu_item_get_shortcut(ui_manager
, "Menu/View/Part/OpenWith");
1031 TEXTVIEW_INSERT(shortcut
);
1033 TEXTVIEW_INSERT("')");
1036 TEXTVIEW_INSERT("\n");
1038 textview_show_icon(textview
, GTK_STOCK_DIALOG_INFO
);
1041 static void textview_write_body(TextView
*textview
, MimeInfo
*mimeinfo
)
1044 gchar buf
[BUFFSIZE
];
1045 CodeConverter
*conv
;
1046 const gchar
*charset
;
1048 const gchar
*p
, *cmd
;
1051 gboolean continue_write
= TRUE
;
1052 size_t wrote
= 0, i
= 0;
1054 if (textview
->messageview
->forced_charset
)
1055 charset
= textview
->messageview
->forced_charset
;
1057 /* use supersets transparently when possible */
1058 charset
= procmime_mimeinfo_get_parameter(mimeinfo
, "charset");
1059 if (charset
&& !strcasecmp(charset
, CS_ISO_8859_1
))
1060 charset
= CS_WINDOWS_1252
;
1061 else if (charset
&& !strcasecmp(charset
, CS_X_GBK
))
1062 charset
= CS_GB18030
;
1063 else if (charset
&& !strcasecmp(charset
, CS_GBK
))
1064 charset
= CS_GB18030
;
1065 else if (charset
&& !strcasecmp(charset
, CS_GB2312
))
1066 charset
= CS_GB18030
;
1069 textview_set_font(textview
, charset
);
1071 conv
= conv_code_converter_new(charset
);
1073 procmime_force_encoding(textview
->messageview
->forced_encoding
);
1075 textview
->is_in_signature
= FALSE
;
1076 textview
->is_diff
= FALSE
;
1078 procmime_decode_content(mimeinfo
);
1080 if (!g_ascii_strcasecmp(mimeinfo
->subtype
, "html") &&
1081 prefs_common
.render_html
) {
1084 filename
= procmime_get_tmp_file_name(mimeinfo
);
1085 if (procmime_get_part(filename
, mimeinfo
) == 0) {
1086 tmpfp
= g_fopen(filename
, "rb");
1088 textview_show_html(textview
, tmpfp
, conv
);
1091 claws_unlink(filename
);
1094 } else if (!g_ascii_strcasecmp(mimeinfo
->subtype
, "enriched")) {
1097 filename
= procmime_get_tmp_file_name(mimeinfo
);
1098 if (procmime_get_part(filename
, mimeinfo
) == 0) {
1099 tmpfp
= g_fopen(filename
, "rb");
1101 textview_show_ertf(textview
, tmpfp
, conv
);
1104 claws_unlink(filename
);
1108 } else if ( g_ascii_strcasecmp(mimeinfo
->subtype
, "plain") &&
1109 (cmd
= prefs_common
.mime_textviewer
) && *cmd
&&
1110 (p
= strchr(cmd
, '%')) && *(p
+ 1) == 's') {
1114 fname
= procmime_get_tmp_file_name(mimeinfo
);
1115 if (procmime_get_part(fname
, mimeinfo
)) goto textview_default
;
1117 g_snprintf(buf
, sizeof(buf
), cmd
, fname
);
1118 debug_print("Viewing text content of type: %s (length: %d) "
1119 "using %s\n", mimeinfo
->subtype
, mimeinfo
->length
, buf
);
1121 if (pipe(pfd
) < 0) {
1122 g_snprintf(buf
, sizeof(buf
),
1123 "pipe failed for textview\n\n%s\n", g_strerror(errno
));
1124 textview_write_line(textview
, buf
, conv
, TRUE
);
1125 goto textview_default
;
1129 g_snprintf(buf
, sizeof(buf
),
1130 "fork failed for textview\n\n%s\n", g_strerror(errno
));
1131 textview_write_line(textview
, buf
, conv
, TRUE
);
1134 goto textview_default
;
1136 if (pid
== 0) { /* child */
1139 argv
= strsplit_with_quote(buf
, " ", 0);
1143 rc
= execvp(argv
[0], argv
);
1145 g_print(_("The command to view attachment "
1148 "Exit code %d\n"), buf
, rc
);
1152 tmpfp
= fdopen(pfd
[0], "rb");
1153 while (fgets(buf
, sizeof(buf
), tmpfp
)) {
1154 textview_write_line(textview
, buf
, conv
, TRUE
);
1156 if (textview
->stop_loading
) {
1158 waitpid(pid
, pfd
, 0);
1165 waitpid(pid
, pfd
, 0);
1172 if (!g_ascii_strcasecmp(mimeinfo
->subtype
, "x-patch")
1173 || !g_ascii_strcasecmp(mimeinfo
->subtype
, "x-diff"))
1174 textview
->is_diff
= TRUE
;
1176 if (mimeinfo
->content
== MIMECONTENT_MEM
)
1177 tmpfp
= str_open_as_stream(mimeinfo
->data
.mem
);
1179 tmpfp
= g_fopen(mimeinfo
->data
.filename
, "rb");
1181 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fopen");
1184 if (fseek(tmpfp
, mimeinfo
->offset
, SEEK_SET
) < 0) {
1185 FILE_OP_ERROR(mimeinfo
->data
.filename
, "fseek");
1189 debug_print("Viewing text content of type: %s (length: %d)\n", mimeinfo
->subtype
, mimeinfo
->length
);
1190 while (((i
= ftell(tmpfp
)) < mimeinfo
->offset
+ mimeinfo
->length
) &&
1191 (fgets(buf
, sizeof(buf
), tmpfp
) != NULL
)
1192 && continue_write
) {
1193 textview_write_line(textview
, buf
, conv
, TRUE
);
1194 if (textview
->stop_loading
) {
1198 wrote
+= ftell(tmpfp
)-i
;
1199 if (mimeinfo
->length
> 1024*1024
1200 && wrote
> 1024*1024
1201 && !textview
->messageview
->show_full_text
) {
1202 continue_write
= FALSE
;
1208 conv_code_converter_destroy(conv
);
1209 procmime_force_encoding(0);
1211 textview
->uri_list
= g_slist_reverse(textview
->uri_list
);
1212 for (cur
= textview
->uri_list
; cur
; cur
= cur
->next
) {
1213 ClickableText
*uri
= (ClickableText
*)cur
->data
;
1216 if (!prefs_common
.hide_quotes
||
1217 uri
->quote_level
+1 < prefs_common
.hide_quotes
) {
1218 textview_toggle_quote(textview
, cur
, uri
, TRUE
);
1219 if (textview
->stop_loading
) {
1225 if (continue_write
== FALSE
) {
1226 messageview_show_partial_display(
1227 textview
->messageview
,
1228 textview
->messageview
->msginfo
,
1234 static void textview_show_html(TextView
*textview
, FILE *fp
,
1235 CodeConverter
*conv
)
1237 SC_HTMLParser
*parser
;
1241 parser
= sc_html_parser_new(fp
, conv
);
1242 cm_return_if_fail(parser
!= NULL
);
1244 while ((str
= sc_html_parse(parser
)) != NULL
) {
1245 if (parser
->state
== SC_HTML_HREF
) {
1246 /* first time : get and copy the URL */
1247 if (parser
->href
== NULL
) {
1248 /* ALF - the claws html parser returns an empty string,
1249 * if still inside an <a>, but already parsed past HREF */
1250 str
= strtok(str
, " ");
1252 while (str
&& *str
&& g_ascii_isspace(*str
))
1254 parser
->href
= g_strdup(str
);
1255 /* the URL may (or not) be followed by the
1256 * referenced text */
1257 str
= strtok(NULL
, "");
1261 textview_write_link(textview
, str
, parser
->href
, NULL
);
1263 textview_write_line(textview
, str
, NULL
, FALSE
);
1265 if (lines
% 500 == 0)
1267 if (textview
->stop_loading
) {
1271 textview_write_line(textview
, "\n", NULL
, FALSE
);
1272 sc_html_parser_destroy(parser
);
1275 static void textview_show_ertf(TextView
*textview
, FILE *fp
,
1276 CodeConverter
*conv
)
1282 parser
= ertf_parser_new(fp
, conv
);
1283 cm_return_if_fail(parser
!= NULL
);
1285 while ((str
= ertf_parse(parser
)) != NULL
) {
1286 textview_write_line(textview
, str
, NULL
, FALSE
);
1288 if (lines
% 500 == 0)
1290 if (textview
->stop_loading
) {
1295 ertf_parser_destroy(parser
);
1298 #define ADD_TXT_POS(bp_, ep_, pti_) \
1299 if ((last->next = alloca(sizeof(struct txtpos))) != NULL) { \
1300 last = last->next; \
1301 last->bp = (bp_); last->ep = (ep_); last->pti = (pti_); \
1302 last->next = NULL; \
1304 g_warning("alloc error scanning URIs"); \
1305 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, \
1311 #define ADD_TXT_POS_LATER(bp_, ep_, pti_) \
1312 if ((last->next = alloca(sizeof(struct txtpos))) != NULL) { \
1313 last = last->next; \
1314 last->bp = (bp_); last->ep = (ep_); last->pti = (pti_); \
1315 last->next = NULL; \
1317 g_warning("alloc error scanning URIs"); \
1320 /* textview_make_clickable_parts() - colorizes clickable parts */
1321 static void textview_make_clickable_parts(TextView
*textview
,
1322 const gchar
*fg_tag
,
1323 const gchar
*uri_tag
,
1324 const gchar
*linebuf
,
1327 GtkTextView
*text
= GTK_TEXT_VIEW(textview
->text
);
1328 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer(text
);
1330 gchar
*mybuf
= g_strdup(linebuf
);
1332 /* parse table - in order of priority */
1334 const gchar
*needle
; /* token */
1336 /* token search function */
1337 gchar
*(*search
) (const gchar
*haystack
,
1338 const gchar
*needle
);
1339 /* part parsing function */
1340 gboolean (*parse
) (const gchar
*start
,
1341 const gchar
*scanpos
,
1345 /* part to URI function */
1346 gchar
*(*build_uri
) (const gchar
*bp
,
1350 static struct table parser
[] = {
1351 {"http://", strcasestr
, get_uri_part
, make_uri_string
},
1352 {"https://", strcasestr
, get_uri_part
, make_uri_string
},
1353 {"ftp://", strcasestr
, get_uri_part
, make_uri_string
},
1354 {"sftp://", strcasestr
, get_uri_part
, make_uri_string
},
1355 {"gopher://",strcasestr
, get_uri_part
, make_uri_string
},
1356 {"www.", strcasestr
, get_uri_part
, make_http_string
},
1357 {"mailto:", strcasestr
, get_uri_part
, make_uri_string
},
1358 {"@", strcasestr
, get_email_part
, make_email_string
}
1360 const gint PARSE_ELEMS
= sizeof parser
/ sizeof parser
[0];
1363 const gchar
*walk
, *bp
, *ep
;
1366 const gchar
*bp
, *ep
; /* text position */
1367 gint pti
; /* index in parse table */
1368 struct txtpos
*next
; /* next */
1369 } head
= {NULL
, NULL
, 0, NULL
}, *last
= &head
;
1371 if (!g_utf8_validate(linebuf
, -1, NULL
)) {
1373 mybuf
= g_malloc(strlen(linebuf
)*2 +1);
1374 conv_localetodisp(mybuf
, strlen(linebuf
)*2 +1, linebuf
);
1377 gtk_text_buffer_get_end_iter(buffer
, &iter
);
1379 /* parse for clickable parts, and build a list of begin and end positions */
1380 for (walk
= mybuf
;;) {
1381 gint last_index
= PARSE_ELEMS
;
1382 gchar
*scanpos
= NULL
;
1384 /* FIXME: this looks phony. scanning for anything in the parse table */
1385 for (n
= 0; n
< PARSE_ELEMS
; n
++) {
1388 tmp
= parser
[n
].search(walk
, parser
[n
].needle
);
1390 if (scanpos
== NULL
|| tmp
< scanpos
) {
1398 /* check if URI can be parsed */
1399 if (parser
[last_index
].parse(walk
, scanpos
, &bp
, &ep
, hdr
)
1400 && (size_t) (ep
- bp
- 1) > strlen(parser
[last_index
].needle
)) {
1401 ADD_TXT_POS(bp
, ep
, last_index
);
1405 strlen(parser
[last_index
].needle
);
1410 /* colorize this line */
1412 const gchar
*normal_text
= mybuf
;
1415 for (last
= head
.next
; last
!= NULL
;
1416 normal_text
= last
->ep
, last
= last
->next
) {
1418 uri
= g_new0(ClickableText
, 1);
1419 if (last
->bp
- normal_text
> 0)
1420 gtk_text_buffer_insert_with_tags_by_name
1423 last
->bp
- normal_text
,
1425 uri
->uri
= parser
[last
->pti
].build_uri(last
->bp
,
1427 uri
->start
= gtk_text_iter_get_offset(&iter
);
1428 gtk_text_buffer_insert_with_tags_by_name
1429 (buffer
, &iter
, last
->bp
, last
->ep
- last
->bp
,
1430 uri_tag
, fg_tag
, NULL
);
1431 uri
->end
= gtk_text_iter_get_offset(&iter
);
1432 uri
->filename
= NULL
;
1433 textview
->uri_list
=
1434 g_slist_prepend(textview
->uri_list
, uri
);
1438 gtk_text_buffer_insert_with_tags_by_name
1439 (buffer
, &iter
, normal_text
, -1, fg_tag
, NULL
);
1441 gtk_text_buffer_insert_with_tags_by_name
1442 (buffer
, &iter
, mybuf
, -1, fg_tag
, NULL
);
1447 /* textview_make_clickable_parts() - colorizes clickable parts */
1448 static void textview_make_clickable_parts_later(TextView
*textview
,
1449 gint start
, gint end
)
1451 GtkTextView
*text
= GTK_TEXT_VIEW(textview
->text
);
1452 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer(text
);
1453 GtkTextIter start_iter
, end_iter
;
1456 /* parse table - in order of priority */
1458 const gchar
*needle
; /* token */
1460 /* token search function */
1461 gchar
*(*search
) (const gchar
*haystack
,
1462 const gchar
*needle
);
1463 /* part parsing function */
1464 gboolean (*parse
) (const gchar
*start
,
1465 const gchar
*scanpos
,
1469 /* part to URI function */
1470 gchar
*(*build_uri
) (const gchar
*bp
,
1474 static struct table parser
[] = {
1475 {"http://", strcasestr
, get_uri_part
, make_uri_string
},
1476 {"https://", strcasestr
, get_uri_part
, make_uri_string
},
1477 {"ftp://", strcasestr
, get_uri_part
, make_uri_string
},
1478 {"sftp://", strcasestr
, get_uri_part
, make_uri_string
},
1479 {"www.", strcasestr
, get_uri_part
, make_http_string
},
1480 {"mailto:", strcasestr
, get_uri_part
, make_uri_string
},
1481 {"@", strcasestr
, get_email_part
, make_email_string
}
1483 const gint PARSE_ELEMS
= sizeof parser
/ sizeof parser
[0];
1486 const gchar
*walk
, *bp
, *ep
;
1489 const gchar
*bp
, *ep
; /* text position */
1490 gint pti
; /* index in parse table */
1491 struct txtpos
*next
; /* next */
1492 } head
= {NULL
, NULL
, 0, NULL
}, *last
= &head
;
1494 gtk_text_buffer_get_iter_at_offset(buffer
, &start_iter
, start
);
1495 gtk_text_buffer_get_iter_at_offset(buffer
, &end_iter
, end
);
1496 mybuf
= gtk_text_buffer_get_text(buffer
, &start_iter
, &end_iter
, FALSE
);
1497 offset
= gtk_text_iter_get_offset(&start_iter
);
1499 /* parse for clickable parts, and build a list of begin and end positions */
1500 for (walk
= mybuf
;;) {
1501 gint last_index
= PARSE_ELEMS
;
1502 gchar
*scanpos
= NULL
;
1504 /* FIXME: this looks phony. scanning for anything in the parse table */
1505 for (n
= 0; n
< PARSE_ELEMS
; n
++) {
1508 tmp
= parser
[n
].search(walk
, parser
[n
].needle
);
1510 if (scanpos
== NULL
|| tmp
< scanpos
) {
1518 /* check if URI can be parsed */
1519 if (parser
[last_index
].parse(walk
, scanpos
, &bp
, &ep
, FALSE
)
1520 && (size_t) (ep
- bp
- 1) > strlen(parser
[last_index
].needle
)) {
1521 ADD_TXT_POS_LATER(bp
, ep
, last_index
);
1525 strlen(parser
[last_index
].needle
);
1530 /* colorize this line */
1533 for (last
= head
.next
; last
!= NULL
; last
= last
->next
) {
1535 gint start_offset
, end_offset
;
1538 uri
= g_new0(ClickableText
, 1);
1539 uri
->uri
= parser
[last
->pti
].build_uri(last
->bp
,
1543 old_char
= tmp_str
[last
->ep
- mybuf
];
1544 tmp_str
[last
->ep
- mybuf
] = '\0';
1545 end_offset
= g_utf8_strlen(tmp_str
, -1);
1546 tmp_str
[last
->ep
- mybuf
] = old_char
;
1548 old_char
= tmp_str
[last
->bp
- mybuf
];
1549 tmp_str
[last
->bp
- mybuf
] = '\0';
1550 start_offset
= g_utf8_strlen(tmp_str
, -1);
1551 tmp_str
[last
->bp
- mybuf
] = old_char
;
1553 gtk_text_buffer_get_iter_at_offset(buffer
, &start_iter
, start_offset
+ offset
);
1554 gtk_text_buffer_get_iter_at_offset(buffer
, &end_iter
, end_offset
+ offset
);
1556 uri
->start
= gtk_text_iter_get_offset(&start_iter
);
1558 gtk_text_buffer_apply_tag_by_name(buffer
, "link", &start_iter
, &end_iter
);
1560 uri
->end
= gtk_text_iter_get_offset(&end_iter
);
1561 uri
->filename
= NULL
;
1562 textview
->uri_list
=
1563 g_slist_prepend(textview
->uri_list
, uri
);
1572 static void textview_write_line(TextView
*textview
, const gchar
*str
,
1573 CodeConverter
*conv
, gboolean do_quote_folding
)
1576 GtkTextBuffer
*buffer
;
1578 gchar buf
[BUFFSIZE
];
1580 gint quotelevel
= -1, real_quotelevel
= -1;
1581 gchar quote_tag_str
[10];
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_localetodisp(buf
, sizeof(buf
), str
);
1595 /* change color of quotation
1596 >, foo>, _> ... ok, <foo>, foo bar>, foo-> ... ng
1597 Up to 3 levels of quotations are detected, and each
1598 level is colored using a different color. */
1599 if (prefs_common
.enable_color
1600 && line_has_quote_char(buf
, prefs_common
.quote_chars
)) {
1601 real_quotelevel
= get_quote_level(buf
, prefs_common
.quote_chars
);
1602 quotelevel
= real_quotelevel
;
1603 /* set up the correct foreground color */
1604 if (quotelevel
> 2) {
1605 /* recycle colors */
1606 if (prefs_common
.recycle_quote_colors
)
1613 if (quotelevel
== -1)
1616 g_snprintf(quote_tag_str
, sizeof(quote_tag_str
),
1617 "quote%d", quotelevel
);
1618 fg_color
= quote_tag_str
;
1621 if (prefs_common
.enable_color
) {
1622 if (textview
->is_diff
) {
1623 if (strncmp(buf
, "+++ ", 4) == 0)
1624 fg_color
= "diff-add-file";
1625 else if (buf
[0] == '+')
1626 fg_color
= "diff-add";
1627 else if (strncmp(buf
, "--- ", 4) == 0)
1628 fg_color
= "diff-del-file";
1629 else if (buf
[0] == '-')
1630 fg_color
= "diff-del";
1631 else if (strncmp(buf
, "@@ ", 3) == 0 &&
1632 strcmp(buf
+strlen(buf
)-4, " @@\n") == 0)
1633 fg_color
= "diff-hunk";
1634 } else if (strcmp(buf
,"-- \n") == 0
1635 || strcmp(buf
, "- -- \n") == 0
1636 || textview
->is_in_signature
) {
1637 fg_color
= "signature";
1638 textview
->is_in_signature
= TRUE
;
1642 if (real_quotelevel
> -1 && do_quote_folding
) {
1643 if (!g_utf8_validate(buf
, -1, NULL
)) {
1644 gchar
*utf8buf
= NULL
;
1645 utf8buf
= g_malloc(BUFFSIZE
);
1646 conv_localetodisp(utf8buf
, BUFFSIZE
, buf
);
1647 strncpy2(buf
, utf8buf
, BUFFSIZE
-1);
1651 if ( textview
->prev_quote_level
!= real_quotelevel
) {
1653 uri
= g_new0(ClickableText
, 1);
1654 uri
->uri
= g_strdup("");
1655 uri
->data
= g_strdup(buf
);
1656 uri
->data_len
= strlen(uri
->data
);
1657 uri
->start
= gtk_text_iter_get_offset(&iter
);
1658 uri
->is_quote
= TRUE
;
1659 uri
->quote_level
= real_quotelevel
;
1660 uri
->fg_color
= g_strdup(fg_color
);
1662 gtk_text_buffer_insert_with_tags_by_name
1663 (buffer
, &iter
, " [...]", -1,
1664 "qlink", fg_color
, NULL
);
1665 uri
->end
= gtk_text_iter_get_offset(&iter
);
1666 gtk_text_buffer_insert(buffer
, &iter
, " \n", -1);
1668 uri
->filename
= NULL
;
1669 textview
->uri_list
=
1670 g_slist_prepend(textview
->uri_list
, uri
);
1672 textview
->prev_quote_level
= real_quotelevel
;
1674 GSList
*last
= textview
->uri_list
;
1675 ClickableText
*lasturi
= NULL
;
1676 gint e_len
= 0, n_len
= 0;
1678 if (textview
->uri_list
) {
1679 lasturi
= (ClickableText
*)last
->data
;
1681 g_print("oops (%d %d)\n",
1682 real_quotelevel
, textview
->prev_quote_level
);
1685 if (lasturi
->is_quote
== FALSE
) {
1686 textview
->prev_quote_level
= -1;
1689 e_len
= lasturi
->data
? lasturi
->data_len
:0;
1690 n_len
= strlen(buf
);
1691 lasturi
->data
= g_realloc((gchar
*)lasturi
->data
, e_len
+ n_len
+ 1);
1692 strcpy((gchar
*)lasturi
->data
+ e_len
, buf
);
1693 *((gchar
*)lasturi
->data
+ e_len
+ n_len
) = '\0';
1694 lasturi
->data_len
+= n_len
;
1698 textview_make_clickable_parts(textview
, fg_color
, "link", buf
, FALSE
);
1699 textview
->prev_quote_level
= -1;
1703 void textview_write_link(TextView
*textview
, const gchar
*str
,
1704 const gchar
*uri
, CodeConverter
*conv
)
1707 GtkTextBuffer
*buffer
;
1709 gchar buf
[BUFFSIZE
];
1711 ClickableText
*r_uri
;
1713 if (!str
|| *str
== '\0')
1718 while (uri
&& *uri
&& g_ascii_isspace(*uri
))
1721 text
= GTK_TEXT_VIEW(textview
->text
);
1722 buffer
= gtk_text_view_get_buffer(text
);
1723 gtk_text_buffer_get_end_iter(buffer
, &iter
);
1726 strncpy2(buf
, str
, sizeof(buf
));
1727 else if (conv_convert(conv
, buf
, sizeof(buf
), str
) < 0)
1728 conv_utf8todisp(buf
, sizeof(buf
), str
);
1730 if (g_utf8_validate(buf
, -1, NULL
) == FALSE
)
1735 gtk_text_buffer_get_end_iter(buffer
, &iter
);
1736 for (bufp
= buf
; *bufp
!= '\0'; bufp
= g_utf8_next_char(bufp
)) {
1739 ch
= g_utf8_get_char(bufp
);
1740 if (!g_unichar_isspace(ch
))
1744 gtk_text_buffer_insert(buffer
, &iter
, buf
, bufp
- buf
);
1746 r_uri
= g_new0(ClickableText
, 1);
1747 r_uri
->uri
= g_strdup(uri
);
1748 r_uri
->start
= gtk_text_iter_get_offset(&iter
);
1749 gtk_text_buffer_insert_with_tags_by_name
1750 (buffer
, &iter
, bufp
, -1, "link", NULL
);
1751 r_uri
->end
= gtk_text_iter_get_offset(&iter
);
1752 r_uri
->filename
= NULL
;
1753 textview
->uri_list
= g_slist_prepend(textview
->uri_list
, r_uri
);
1756 static void textview_set_cursor(GdkWindow
*window
, GdkCursor
*cursor
)
1758 if (GDK_IS_WINDOW(window
))
1759 gdk_window_set_cursor(window
, cursor
);
1761 void textview_clear(TextView
*textview
)
1763 GtkTextView
*text
= GTK_TEXT_VIEW(textview
->text
);
1764 GtkTextBuffer
*buffer
;
1765 GdkWindow
*window
= gtk_text_view_get_window(text
,
1766 GTK_TEXT_WINDOW_TEXT
);
1768 buffer
= gtk_text_view_get_buffer(text
);
1769 gtk_text_buffer_set_text(buffer
, "", -1);
1770 if (gtk_text_buffer_get_mark(buffer
, "body_start"))
1771 gtk_text_buffer_delete_mark_by_name(buffer
, "body_start");
1772 if (gtk_text_buffer_get_mark(buffer
, "body_end"))
1773 gtk_text_buffer_delete_mark_by_name(buffer
, "body_end");
1775 TEXTVIEW_STATUSBAR_POP(textview
);
1776 textview_uri_list_remove_all(textview
->uri_list
);
1777 textview
->uri_list
= NULL
;
1778 textview
->uri_hover
= NULL
;
1779 textview
->prev_quote_level
= -1;
1781 textview
->body_pos
= 0;
1782 if (textview
->image
)
1783 gtk_widget_destroy(textview
->image
);
1784 textview
->image
= NULL
;
1785 textview
->avatar_type
= 0;
1787 if (textview
->messageview
->mainwin
->cursor_count
== 0) {
1788 textview_set_cursor(window
, text_cursor
);
1790 textview_set_cursor(window
, watch_cursor
);
1794 void textview_destroy(TextView
*textview
)
1796 GtkTextBuffer
*buffer
;
1797 GtkClipboard
*clipboard
;
1799 buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview
->text
));
1800 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
1801 gtk_text_buffer_remove_selection_clipboard(buffer
, clipboard
);
1803 textview_uri_list_remove_all(textview
->uri_list
);
1804 textview
->uri_list
= NULL
;
1805 textview
->prev_quote_level
= -1;
1810 #define CHANGE_TAG_FONT(tagname, font) { \
1811 tag = gtk_text_tag_table_lookup(tags, tagname); \
1813 g_object_set(G_OBJECT(tag), "font-desc", font, NULL); \
1816 void textview_set_font(TextView
*textview
, const gchar
*codeset
)
1819 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview
->text
));
1820 GtkTextTagTable
*tags
= gtk_text_buffer_get_tag_table(buffer
);
1821 PangoFontDescription
*font_desc
, *bold_font_desc
;
1823 font_desc
= pango_font_description_from_string
1826 gtk_widget_modify_font(textview
->text
, font_desc
);
1827 CHANGE_TAG_FONT("header", font_desc
);
1828 CHANGE_TAG_FONT("hlink", font_desc
);
1829 pango_font_description_free(font_desc
);
1831 if (prefs_common
.derive_from_normal_font
|| !BOLD_FONT
) {
1832 bold_font_desc
= pango_font_description_from_string
1835 pango_font_description_set_weight
1836 (bold_font_desc
, PANGO_WEIGHT_BOLD
);
1838 bold_font_desc
= pango_font_description_from_string
1841 if (bold_font_desc
) {
1842 CHANGE_TAG_FONT("header_title", bold_font_desc
);
1843 pango_font_description_free(bold_font_desc
);
1846 if (prefs_common
.textfont
) {
1847 PangoFontDescription
*font_desc
;
1849 font_desc
= pango_font_description_from_string
1850 (prefs_common
.textfont
);
1852 gtk_widget_modify_font(textview
->text
, font_desc
);
1853 pango_font_description_free(font_desc
);
1856 gtk_text_view_set_pixels_above_lines(GTK_TEXT_VIEW(textview
->text
),
1857 prefs_common
.line_space
/ 2);
1858 gtk_text_view_set_pixels_below_lines(GTK_TEXT_VIEW(textview
->text
),
1859 prefs_common
.line_space
/ 2);
1862 void textview_set_text(TextView
*textview
, const gchar
*text
)
1865 GtkTextBuffer
*buffer
;
1867 cm_return_if_fail(textview
!= NULL
);
1868 cm_return_if_fail(text
!= NULL
);
1870 textview_clear(textview
);
1872 view
= GTK_TEXT_VIEW(textview
->text
);
1873 buffer
= gtk_text_view_get_buffer(view
);
1874 gtk_text_buffer_set_text(buffer
, text
, strlen(text
));
1890 H_ORGANIZATION
= 11,
1893 void textview_set_position(TextView
*textview
, gint pos
)
1895 GtkTextView
*text
= GTK_TEXT_VIEW(textview
->text
);
1897 gtkut_text_view_set_position(text
, pos
);
1900 static GPtrArray
*textview_scan_header(TextView
*textview
, FILE *fp
)
1902 gchar buf
[BUFFSIZE
];
1903 GPtrArray
*headers
, *sorted_headers
;
1904 GSList
*disphdr_list
;
1908 cm_return_val_if_fail(fp
!= NULL
, NULL
);
1910 if (prefs_common
.show_all_headers
) {
1911 headers
= procheader_get_header_array_asis(fp
);
1912 sorted_headers
= g_ptr_array_new();
1913 for (i
= 0; i
< headers
->len
; i
++) {
1914 header
= g_ptr_array_index(headers
, i
);
1915 if (!procheader_header_is_internal(header
->name
))
1916 g_ptr_array_add(sorted_headers
, header
);
1918 procheader_header_free(header
);
1920 g_ptr_array_free(headers
, TRUE
);
1921 return sorted_headers
;
1924 if (!prefs_common
.display_header
) {
1925 while (fgets(buf
, sizeof(buf
), fp
) != NULL
)
1926 if (buf
[0] == '\r' || buf
[0] == '\n') break;
1930 headers
= procheader_get_header_array_asis(fp
);
1932 sorted_headers
= g_ptr_array_new();
1934 for (disphdr_list
= prefs_common
.disphdr_list
; disphdr_list
!= NULL
;
1935 disphdr_list
= disphdr_list
->next
) {
1936 DisplayHeaderProp
*dp
=
1937 (DisplayHeaderProp
*)disphdr_list
->data
;
1939 for (i
= 0; i
< headers
->len
; i
++) {
1940 header
= g_ptr_array_index(headers
, i
);
1942 if (procheader_headername_equal(header
->name
,
1945 procheader_header_free(header
);
1947 g_ptr_array_add(sorted_headers
, header
);
1949 g_ptr_array_remove_index(headers
, i
);
1955 if (prefs_common
.show_other_header
) {
1956 for (i
= 0; i
< headers
->len
; i
++) {
1957 header
= g_ptr_array_index(headers
, i
);
1958 if (!procheader_header_is_internal(header
->name
)) {
1959 g_ptr_array_add(sorted_headers
, header
);
1961 procheader_header_free(header
);
1964 g_ptr_array_free(headers
, TRUE
);
1966 procheader_header_array_destroy(headers
);
1969 return sorted_headers
;
1972 static void textview_show_avatar(TextView
*textview
)
1974 GtkAllocation allocation
;
1975 GtkTextView
*text
= GTK_TEXT_VIEW(textview
->text
);
1976 MsgInfo
*msginfo
= textview
->messageview
->msginfo
;
1978 AvatarRender
*avatarr
;
1980 if (prefs_common
.display_header_pane
|| !prefs_common
.display_xface
)
1983 avatarr
= avatars_avatarrender_new(msginfo
);
1984 hooks_invoke(AVATAR_IMAGE_RENDER_HOOKLIST
, avatarr
);
1986 if (!avatarr
->image
) {
1987 avatars_avatarrender_free(avatarr
);
1991 if (textview
->image
)
1992 gtk_widget_destroy(textview
->image
);
1994 textview
->image
= avatarr
->image
;
1995 textview
->avatar_type
= avatarr
->type
;
1996 avatarr
->image
= NULL
; /* avoid destroying */
1997 avatars_avatarrender_free(avatarr
);
1999 gtk_widget_show(textview
->image
);
2001 gtk_widget_get_allocation(textview
->text
, &allocation
);
2002 x
= allocation
.width
- WIDTH
-5;
2004 gtk_text_view_add_child_in_window(text
, textview
->image
,
2005 GTK_TEXT_WINDOW_TEXT
, x
, 5);
2007 gtk_widget_show_all(textview
->text
);
2011 if (textview
->image
)
2012 gtk_widget_destroy(textview
->image
);
2013 textview
->image
= NULL
;
2014 textview
->avatar_type
= 0;
2017 void textview_show_icon(TextView
*textview
, const gchar
*stock_id
)
2019 GtkAllocation allocation
;
2020 GtkTextView
*text
= GTK_TEXT_VIEW(textview
->text
);
2023 if (textview
->image
)
2024 gtk_widget_destroy(textview
->image
);
2026 textview
->image
= gtk_image_new_from_stock(stock_id
, GTK_ICON_SIZE_DIALOG
);
2027 cm_return_if_fail(textview
->image
!= NULL
);
2029 gtk_widget_show(textview
->image
);
2031 gtk_widget_get_allocation(textview
->text
, &allocation
);
2032 x
= allocation
.width
- WIDTH
-5;
2034 gtk_text_view_add_child_in_window(text
, textview
->image
,
2035 GTK_TEXT_WINDOW_TEXT
, x
, 5);
2037 gtk_widget_show_all(textview
->text
);
2043 static void textview_save_contact_pic(TextView
*textview
)
2045 #ifndef USE_ALT_ADDRBOOK
2046 MsgInfo
*msginfo
= textview
->messageview
->msginfo
;
2047 gchar
*filename
= NULL
;
2048 GError
*error
= NULL
;
2049 GdkPixbuf
*picture
= NULL
;
2051 if (!msginfo
->extradata
|| !msginfo
->extradata
->avatars
)
2054 if (textview
->avatar_type
> AVATAR_FACE
)
2057 if (textview
->image
)
2058 picture
= gtk_image_get_pixbuf(GTK_IMAGE(textview
->image
));
2060 filename
= addrindex_get_picture_file(msginfo
->from
);
2063 if (!is_file_exist(filename
)) {
2064 gdk_pixbuf_save(picture
, filename
, "png", &error
, NULL
);
2066 g_warning("Failed to save image: %s",
2068 g_error_free(error
);
2073 /* new address book */
2077 static void textview_show_contact_pic(TextView
*textview
)
2079 #ifndef USE_ALT_ADDRBOOK
2080 MsgInfo
*msginfo
= textview
->messageview
->msginfo
;
2081 GtkTextView
*text
= GTK_TEXT_VIEW(textview
->text
);
2083 gchar
*filename
= NULL
;
2084 GError
*error
= NULL
;
2085 GdkPixbuf
*picture
= NULL
;
2087 GtkAllocation allocation
;
2089 if (prefs_common
.display_header_pane
2090 || !prefs_common
.display_xface
)
2093 if (msginfo
->extradata
&& msginfo
->extradata
->avatars
)
2096 if (textview
->image
)
2097 gtk_widget_destroy(textview
->image
);
2099 filename
= addrindex_get_picture_file(msginfo
->from
);
2103 if (!is_file_exist(filename
)) {
2108 gdk_pixbuf_get_file_info(filename
, &w
, &h
);
2110 if (w
> 48 || h
> 48)
2111 picture
= gdk_pixbuf_new_from_file_at_scale(filename
,
2112 48, 48, TRUE
, &error
);
2114 picture
= gdk_pixbuf_new_from_file(filename
, &error
);
2117 debug_print("Failed to import image: %s\n",
2119 g_error_free(error
);
2125 textview
->image
= gtk_image_new_from_pixbuf(picture
);
2126 g_object_unref(picture
);
2128 cm_return_if_fail(textview
->image
!= NULL
);
2130 gtk_widget_show(textview
->image
);
2132 gtk_widget_get_allocation(textview
->text
, &allocation
);
2133 x
= allocation
.width
- WIDTH
-5;
2135 gtk_text_view_add_child_in_window(text
, textview
->image
,
2136 GTK_TEXT_WINDOW_TEXT
, x
, 5);
2138 gtk_widget_show_all(textview
->text
);
2142 if (textview
->image
)
2143 gtk_widget_destroy(textview
->image
);
2144 textview
->image
= NULL
;
2145 textview
->avatar_type
= 0;
2147 /* new address book */
2151 static gint
textview_tag_cmp_list(gconstpointer a
, gconstpointer b
)
2153 gint id_a
= GPOINTER_TO_INT(a
);
2154 gint id_b
= GPOINTER_TO_INT(b
);
2155 const gchar
*tag_a
= tags_get_tag(id_a
);
2156 const gchar
*tag_b
= tags_get_tag(id_b
);
2159 return tag_b
== NULL
? 0:1;
2164 return g_utf8_collate(tag_a
, tag_b
);
2168 static void textview_show_tags(TextView
*textview
)
2170 MsgInfo
*msginfo
= textview
->messageview
->msginfo
;
2171 GtkTextView
*text
= GTK_TEXT_VIEW(textview
->text
);
2172 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer(text
);
2176 gboolean found_tag
= FALSE
;
2181 cur
= orig
= g_slist_sort(g_slist_copy(msginfo
->tags
), textview_tag_cmp_list
);
2183 for (; cur
; cur
= cur
->next
) {
2184 if (tags_get_tag(GPOINTER_TO_INT(cur
->data
)) != NULL
) {
2194 gtk_text_buffer_get_end_iter (buffer
, &iter
);
2195 gtk_text_buffer_insert_with_tags_by_name(buffer
,
2196 &iter
, _("Tags: "), -1,
2197 "header_title", "header", "tags", NULL
);
2199 for (cur
= orig
; cur
; cur
= cur
->next
) {
2200 const gchar
*cur_tag
= tags_get_tag(GPOINTER_TO_INT(cur
->data
));
2203 uri
= g_new0(ClickableText
, 1);
2204 uri
->uri
= g_strdup("");
2205 uri
->start
= gtk_text_iter_get_offset(&iter
);
2206 gtk_text_buffer_insert_with_tags_by_name(buffer
, &iter
,
2208 "link", "header", "tags", NULL
);
2209 uri
->end
= gtk_text_iter_get_offset(&iter
);
2210 uri
->filename
= g_strdup_printf("sc://search_tags:%s", cur_tag
);
2212 textview
->uri_list
=
2213 g_slist_prepend(textview
->uri_list
, uri
);
2214 if (cur
->next
&& tags_get_tag(GPOINTER_TO_INT(cur
->next
->data
)))
2215 gtk_text_buffer_insert_with_tags_by_name(buffer
, &iter
, ", ", 2,
2216 "header", "tags", NULL
);
2218 gtk_text_buffer_insert_with_tags_by_name(buffer
, &iter
, " ", 1,
2219 "header", "tags", NULL
);
2223 gtk_text_buffer_insert_with_tags_by_name(buffer
, &iter
, "\n", 1,
2224 "header", "tags", NULL
);
2227 static void textview_show_header(TextView
*textview
, GPtrArray
*headers
)
2229 GtkTextView
*text
= GTK_TEXT_VIEW(textview
->text
);
2230 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer(text
);
2235 cm_return_if_fail(headers
!= NULL
);
2237 for (i
= 0; i
< headers
->len
; i
++) {
2238 header
= g_ptr_array_index(headers
, i
);
2239 cm_return_if_fail(header
->name
!= NULL
);
2241 gtk_text_buffer_get_end_iter (buffer
, &iter
);
2242 if(prefs_common
.trans_hdr
== TRUE
) {
2243 gchar
*hdr
= g_strndup(header
->name
, strlen(header
->name
) - 1);
2244 gchar
*trans_hdr
= gettext(hdr
);
2245 gtk_text_buffer_insert_with_tags_by_name(buffer
,
2246 &iter
, trans_hdr
, -1,
2247 "header_title", "header", NULL
);
2248 gtk_text_buffer_insert_with_tags_by_name(buffer
,
2249 &iter
, ":", 1, "header_title", "header", NULL
);
2252 gtk_text_buffer_insert_with_tags_by_name(buffer
,
2253 &iter
, header
->name
,
2254 -1, "header_title", "header", NULL
);
2256 if (header
->name
[strlen(header
->name
) - 1] != ' ')
2257 gtk_text_buffer_insert_with_tags_by_name
2258 (buffer
, &iter
, " ", 1,
2259 "header_title", "header", NULL
);
2261 if (procheader_headername_equal(header
->name
, "Subject") ||
2262 procheader_headername_equal(header
->name
, "From") ||
2263 procheader_headername_equal(header
->name
, "To") ||
2264 procheader_headername_equal(header
->name
, "Cc"))
2265 unfold_line(header
->body
);
2267 if (procheader_headername_equal(header
->name
, "Date") &&
2268 prefs_common
.msgview_date_format
) {
2271 procheader_date_parse(hbody
, header
->body
, sizeof(hbody
));
2272 gtk_text_buffer_get_end_iter (buffer
, &iter
);
2273 gtk_text_buffer_insert_with_tags_by_name
2274 (buffer
, &iter
, hbody
, -1, "header", NULL
);
2275 } else if ((procheader_headername_equal(header
->name
, "X-Mailer") ||
2276 procheader_headername_equal(header
->name
,
2278 (strstr(header
->body
, "Claws Mail") != NULL
||
2279 strstr(header
->body
, "Sylpheed-Claws") != NULL
)) {
2280 gtk_text_buffer_get_end_iter (buffer
, &iter
);
2281 gtk_text_buffer_insert_with_tags_by_name
2282 (buffer
, &iter
, header
->body
, -1,
2283 "header", "emphasis", NULL
);
2286 procheader_headername_equal(header
->name
, "From") ||
2287 procheader_headername_equal(header
->name
, "To") ||
2288 procheader_headername_equal(header
->name
, "Cc") ||
2289 procheader_headername_equal(header
->name
, "Bcc") ||
2290 procheader_headername_equal(header
->name
, "Reply-To") ||
2291 procheader_headername_equal(header
->name
, "Sender");
2292 textview_make_clickable_parts(textview
, "header",
2293 "hlink", header
->body
,
2296 gtk_text_buffer_get_end_iter (buffer
, &iter
);
2297 gtk_text_buffer_insert_with_tags_by_name(buffer
, &iter
, "\n", 1,
2301 textview_show_avatar(textview
);
2303 textview_save_contact_pic(textview
);
2304 textview_show_contact_pic(textview
);
2307 gboolean
textview_search_string(TextView
*textview
, const gchar
*str
,
2310 GtkTextView
*text
= GTK_TEXT_VIEW(textview
->text
);
2312 return gtkut_text_view_search_string(text
, str
, case_sens
);
2315 gboolean
textview_search_string_backward(TextView
*textview
, const gchar
*str
,
2318 GtkTextView
*text
= GTK_TEXT_VIEW(textview
->text
);
2320 return gtkut_text_view_search_string_backward(text
, str
, case_sens
);
2323 void textview_scroll_one_line(TextView
*textview
, gboolean up
)
2325 GtkTextView
*text
= GTK_TEXT_VIEW(textview
->text
);
2326 GtkAdjustment
*vadj
= gtk_text_view_get_vadjustment(text
);
2328 gtkutils_scroll_one_line(GTK_WIDGET(text
), vadj
, up
);
2331 gboolean
textview_scroll_page(TextView
*textview
, gboolean up
)
2333 GtkTextView
*text
= GTK_TEXT_VIEW(textview
->text
);
2334 GtkAdjustment
*vadj
= gtk_text_view_get_vadjustment(text
);
2336 return gtkutils_scroll_page(GTK_WIDGET(text
), vadj
, up
);
2339 void textview_scroll_max(TextView
*textview
, gboolean up
)
2341 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview
->text
));
2345 gtk_text_buffer_get_start_iter(buffer
, &iter
);
2346 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(textview
->text
),
2347 &iter
, 0.0, TRUE
, 0.0, 1.0);
2350 gtk_text_buffer_get_end_iter(buffer
, &iter
);
2351 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(textview
->text
),
2352 &iter
, 0.0, TRUE
, 0.0, 0.0);
2356 #define KEY_PRESS_EVENT_STOP() \
2357 g_signal_stop_emission_by_name(G_OBJECT(widget), \
2360 static gint
textview_key_pressed(GtkWidget
*widget
, GdkEventKey
*event
,
2363 GdkWindow
*window
= NULL
;
2364 SummaryView
*summaryview
= NULL
;
2365 MessageView
*messageview
= textview
->messageview
;
2367 if (!event
) return FALSE
;
2368 if (messageview
->mainwin
)
2369 summaryview
= messageview
->mainwin
->summaryview
;
2371 switch (event
->keyval
) {
2377 case GDK_KEY_Page_Up
:
2378 case GDK_KEY_Page_Down
:
2379 case GDK_KEY_Control_L
:
2380 case GDK_KEY_Control_R
:
2384 textview_scroll_max(textview
,(event
->keyval
== GDK_KEY_Home
));
2388 summary_pass_key_press_event(summaryview
, event
);
2390 mimeview_scroll_page
2391 (messageview
->mimeview
,
2393 (GDK_SHIFT_MASK
|GDK_MOD1_MASK
)) != 0);
2395 case GDK_KEY_BackSpace
:
2396 mimeview_scroll_page(messageview
->mimeview
, TRUE
);
2398 case GDK_KEY_Return
:
2399 case GDK_KEY_KP_Enter
:
2400 mimeview_scroll_one_line
2401 (messageview
->mimeview
, (event
->state
&
2402 (GDK_SHIFT_MASK
|GDK_MOD1_MASK
)) != 0);
2404 case GDK_KEY_Delete
:
2406 summary_pass_key_press_event(summaryview
, event
);
2409 if (messageview
->mainwin
) {
2410 window
= gtk_widget_get_window(messageview
->mainwin
->window
);
2412 event
->window
!= window
) {
2413 GdkEventKey tmpev
= *event
;
2415 tmpev
.window
= window
;
2416 KEY_PRESS_EVENT_STOP();
2417 gtk_widget_event(messageview
->mainwin
->window
,
2418 (GdkEvent
*)&tmpev
);
2427 static gboolean
textview_motion_notify(GtkWidget
*widget
,
2428 GdkEventMotion
*event
,
2431 if (textview
->loading
)
2433 textview_uri_update(textview
, event
->x
, event
->y
);
2434 gdk_window_get_pointer(gtk_widget_get_window(widget
), NULL
, NULL
, NULL
);
2439 static gboolean
textview_leave_notify(GtkWidget
*widget
,
2440 GdkEventCrossing
*event
,
2443 if (textview
->loading
)
2445 textview_uri_update(textview
, -1, -1);
2450 static gboolean
textview_visibility_notify(GtkWidget
*widget
,
2451 GdkEventVisibility
*event
,
2457 if (textview
->loading
)
2460 window
= gtk_text_view_get_window(GTK_TEXT_VIEW(widget
),
2461 GTK_TEXT_WINDOW_TEXT
);
2463 /* check if occurred for the text window part */
2464 if (window
!= event
->window
)
2467 gdk_window_get_pointer(gtk_widget_get_window(widget
), &wx
, &wy
, NULL
);
2468 textview_uri_update(textview
, wx
, wy
);
2473 void textview_cursor_wait(TextView
*textview
)
2475 GdkWindow
*window
= gtk_text_view_get_window(
2476 GTK_TEXT_VIEW(textview
->text
),
2477 GTK_TEXT_WINDOW_TEXT
);
2478 textview_set_cursor(window
, watch_cursor
);
2481 void textview_cursor_normal(TextView
*textview
)
2483 GdkWindow
*window
= gtk_text_view_get_window(
2484 GTK_TEXT_VIEW(textview
->text
),
2485 GTK_TEXT_WINDOW_TEXT
);
2486 textview_set_cursor(window
, text_cursor
);
2489 static void textview_uri_update(TextView
*textview
, gint x
, gint y
)
2491 GtkTextBuffer
*buffer
;
2492 GtkTextIter start_iter
, end_iter
;
2493 ClickableText
*uri
= NULL
;
2495 buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview
->text
));
2497 if (x
!= -1 && y
!= -1) {
2503 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(textview
->text
),
2504 GTK_TEXT_WINDOW_WIDGET
,
2506 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(textview
->text
),
2509 tags
= gtk_text_iter_get_tags(&iter
);
2510 for (cur
= tags
; cur
!= NULL
; cur
= cur
->next
) {
2511 GtkTextTag
*tag
= cur
->data
;
2514 g_object_get(G_OBJECT(tag
), "name", &name
, NULL
);
2516 if ((!strcmp(name
, "link") || !strcmp(name
, "hlink"))
2517 && textview_get_uri_range(textview
, &iter
, tag
,
2518 &start_iter
, &end_iter
)) {
2520 uri
= textview_get_uri_from_range(textview
,
2532 if (uri
!= textview
->uri_hover
) {
2535 if (textview
->uri_hover
)
2536 gtk_text_buffer_remove_tag_by_name(buffer
,
2538 &textview
->uri_hover_start_iter
,
2539 &textview
->uri_hover_end_iter
);
2541 textview
->uri_hover
= uri
;
2543 textview
->uri_hover_start_iter
= start_iter
;
2544 textview
->uri_hover_end_iter
= end_iter
;
2547 window
= gtk_text_view_get_window(GTK_TEXT_VIEW(textview
->text
),
2548 GTK_TEXT_WINDOW_TEXT
);
2549 if (textview
->messageview
->mainwin
->cursor_count
== 0) {
2550 textview_set_cursor(window
, uri
? hand_cursor
: text_cursor
);
2552 textview_set_cursor(window
, watch_cursor
);
2555 TEXTVIEW_STATUSBAR_POP(textview
);
2559 gtk_text_buffer_apply_tag_by_name(buffer
,
2563 TEXTVIEW_STATUSBAR_PUSH(textview
, uri
->uri
);
2568 static gboolean
textview_get_uri_range(TextView
*textview
,
2571 GtkTextIter
*start_iter
,
2572 GtkTextIter
*end_iter
)
2574 return get_tag_range(iter
, tag
, start_iter
, end_iter
);
2577 static ClickableText
*textview_get_uri_from_range(TextView
*textview
,
2580 GtkTextIter
*start_iter
,
2581 GtkTextIter
*end_iter
)
2583 gint start_pos
, end_pos
, cur_pos
;
2584 ClickableText
*uri
= NULL
;
2587 start_pos
= gtk_text_iter_get_offset(start_iter
);
2588 end_pos
= gtk_text_iter_get_offset(end_iter
);
2589 cur_pos
= gtk_text_iter_get_offset(iter
);
2591 for (cur
= textview
->uri_list
; cur
!= NULL
; cur
= cur
->next
) {
2592 ClickableText
*uri_
= (ClickableText
*)cur
->data
;
2593 if (start_pos
== uri_
->start
&&
2594 end_pos
== uri_
->end
) {
2599 for (cur
= textview
->uri_list
; uri
== NULL
&& cur
!= NULL
; cur
= cur
->next
) {
2600 ClickableText
*uri_
= (ClickableText
*)cur
->data
;
2601 if (start_pos
== uri_
->start
||
2602 end_pos
== uri_
->end
) {
2603 /* in case of contiguous links, textview_get_uri_range
2604 * returns a broader range (start of 1st link to end
2606 * In that case, correct link is the one covering
2609 if (uri_
->start
<= cur_pos
&& cur_pos
<= uri_
->end
) {
2619 static ClickableText
*textview_get_uri(TextView
*textview
,
2623 GtkTextIter start_iter
, end_iter
;
2624 ClickableText
*uri
= NULL
;
2626 if (textview_get_uri_range(textview
, iter
, tag
, &start_iter
,
2628 uri
= textview_get_uri_from_range(textview
, iter
, tag
,
2629 &start_iter
, &end_iter
);
2634 static void textview_shift_uris_after(TextView
*textview
, GSList
*start_list
, gint start
, gint shift
)
2638 start_list
= textview
->uri_list
;
2640 for (cur
= start_list
; cur
; cur
= cur
->next
) {
2641 ClickableText
*uri
= (ClickableText
*)cur
->data
;
2642 if (uri
->start
<= start
)
2644 uri
->start
+= shift
;
2649 static void textview_remove_uris_in(TextView
*textview
, gint start
, gint end
)
2652 for (cur
= textview
->uri_list
; cur
; ) {
2653 ClickableText
*uri
= (ClickableText
*)cur
->data
;
2654 if (uri
->start
> start
&& uri
->end
< end
) {
2656 textview
->uri_list
= g_slist_remove(textview
->uri_list
, uri
);
2658 g_free(uri
->filename
);
2659 if (uri
->is_quote
) {
2660 g_free(uri
->fg_color
);
2662 /* (only free data in quotes uris) */
2672 static void textview_toggle_quote(TextView
*textview
, GSList
*start_list
, ClickableText
*uri
, gboolean expand_only
)
2674 GtkTextIter start
, end
;
2675 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview
->text
));
2680 if (uri
->q_expanded
&& expand_only
)
2683 gtk_text_buffer_get_iter_at_offset(buffer
, &start
, uri
->start
);
2684 gtk_text_buffer_get_iter_at_offset(buffer
, &end
, uri
->end
);
2685 if (textview
->uri_hover
)
2686 gtk_text_buffer_remove_tag_by_name(buffer
,
2688 &textview
->uri_hover_start_iter
,
2689 &textview
->uri_hover_end_iter
);
2690 textview
->uri_hover
= NULL
;
2691 gtk_text_buffer_remove_tag_by_name(buffer
,
2695 /* when shifting URIs start and end, we have to do it per-UTF8-char
2696 * so use g_utf8_strlen(). OTOH, when inserting in the text buffer,
2697 * we have to pass a number of bytes, so use strlen(). disturbing. */
2699 if (!uri
->q_expanded
) {
2700 gtk_text_buffer_get_iter_at_offset(buffer
, &start
, uri
->start
);
2701 gtk_text_buffer_get_iter_at_offset(buffer
, &end
, uri
->end
);
2702 textview_shift_uris_after(textview
, start_list
, uri
->start
,
2703 g_utf8_strlen((gchar
*)uri
->data
, -1)-strlen(" [...]\n"));
2704 gtk_text_buffer_delete(buffer
, &start
, &end
);
2705 gtk_text_buffer_get_iter_at_offset(buffer
, &start
, uri
->start
);
2706 gtk_text_buffer_insert_with_tags_by_name
2707 (buffer
, &start
, (gchar
*)uri
->data
,
2708 strlen((gchar
*)uri
->data
)-1,
2709 "qlink", (gchar
*)uri
->fg_color
, NULL
);
2710 uri
->end
= gtk_text_iter_get_offset(&start
);
2711 textview_make_clickable_parts_later(textview
,
2712 uri
->start
, uri
->end
);
2713 uri
->q_expanded
= TRUE
;
2715 gtk_text_buffer_get_iter_at_offset(buffer
, &start
, uri
->start
);
2716 gtk_text_buffer_get_iter_at_offset(buffer
, &end
, uri
->end
);
2717 textview_remove_uris_in(textview
, uri
->start
, uri
->end
);
2718 textview_shift_uris_after(textview
, start_list
, uri
->start
,
2719 strlen(" [...]\n")-g_utf8_strlen((gchar
*)uri
->data
, -1));
2720 gtk_text_buffer_delete(buffer
, &start
, &end
);
2721 gtk_text_buffer_get_iter_at_offset(buffer
, &start
, uri
->start
);
2722 gtk_text_buffer_insert_with_tags_by_name
2723 (buffer
, &start
, " [...]", -1,
2724 "qlink", (gchar
*)uri
->fg_color
, NULL
);
2725 uri
->end
= gtk_text_iter_get_offset(&start
);
2726 uri
->q_expanded
= FALSE
;
2728 if (textview
->messageview
->mainwin
->cursor_count
== 0) {
2729 textview_cursor_normal(textview
);
2731 textview_cursor_wait(textview
);
2734 static gboolean
textview_uri_button_pressed(GtkTextTag
*tag
, GObject
*obj
,
2735 GdkEvent
*event
, GtkTextIter
*iter
,
2738 GdkEventButton
*bevent
;
2739 ClickableText
*uri
= NULL
;
2741 gboolean qlink
= FALSE
;
2746 if (event
->type
!= GDK_BUTTON_PRESS
&& event
->type
!= GDK_2BUTTON_PRESS
2747 && event
->type
!= GDK_MOTION_NOTIFY
)
2750 uri
= textview_get_uri(textview
, iter
, tag
);
2754 g_object_get(G_OBJECT(tag
), "name", &tagname
, NULL
);
2756 if (!strcmp(tagname
, "qlink"))
2761 bevent
= (GdkEventButton
*) event
;
2763 /* doubleclick: open compose / add address / browser */
2764 if (qlink
&& event
->type
== GDK_BUTTON_PRESS
&& bevent
->button
!= 1) {
2765 /* pass rightclick through */
2767 } else if ((event
->type
== (qlink
? GDK_2BUTTON_PRESS
:GDK_BUTTON_PRESS
) && bevent
->button
== 1) ||
2768 bevent
->button
== 2 || bevent
->button
== 3) {
2769 if (uri
->filename
&& !g_ascii_strncasecmp(uri
->filename
, "sc://", 5)) {
2770 MimeView
*mimeview
=
2771 (textview
->messageview
)?
2772 textview
->messageview
->mimeview
:NULL
;
2773 if (mimeview
&& bevent
->button
== 1) {
2774 mimeview_handle_cmd(mimeview
, uri
->filename
, NULL
, uri
->data
);
2775 } else if (mimeview
&& bevent
->button
== 2 &&
2776 !g_ascii_strcasecmp(uri
->filename
, "sc://select_attachment")) {
2777 mimeview_handle_cmd(mimeview
, "sc://open_attachment", NULL
, uri
->data
);
2778 } else if (mimeview
&& bevent
->button
== 3 &&
2779 !g_ascii_strcasecmp(uri
->filename
, "sc://select_attachment")) {
2780 mimeview_handle_cmd(mimeview
, "sc://menu_attachment", bevent
, uri
->data
);
2783 } else if (qlink
&& bevent
->button
== 1) {
2784 if (prefs_common
.hide_quoted
) {
2785 textview_toggle_quote(textview
, NULL
, uri
, FALSE
);
2789 } else if (!g_ascii_strncasecmp(uri
->uri
, "mailto:", 7)) {
2790 if (bevent
->button
== 3) {
2792 G_OBJECT(textview
->mail_popup_menu
),
2793 "menu_button", uri
);
2794 gtk_menu_popup(GTK_MENU(textview
->mail_popup_menu
),
2795 NULL
, NULL
, NULL
, NULL
,
2796 bevent
->button
, bevent
->time
);
2798 PrefsAccount
*account
= NULL
;
2799 FolderItem
*folder_item
= NULL
;
2802 if (textview
->messageview
&& textview
->messageview
->msginfo
&&
2803 textview
->messageview
->msginfo
->folder
) {
2806 folder_item
= textview
->messageview
->msginfo
->folder
;
2807 if (folder_item
->prefs
&& folder_item
->prefs
->enable_default_account
)
2808 account
= account_find_from_id(folder_item
->prefs
->default_account
);
2810 account
= account_find_from_item(folder_item
);
2812 compose
= compose_new_with_folderitem(account
,
2813 folder_item
, uri
->uri
+ 7);
2814 compose_check_for_email_account(compose
);
2817 } else if (g_ascii_strncasecmp(uri
->uri
, "file:", 5)) {
2818 if (bevent
->button
== 1 &&
2819 textview_uri_security_check(textview
, uri
) == TRUE
)
2821 prefs_common_get_uri_cmd());
2822 else if (bevent
->button
== 3 && !qlink
) {
2824 G_OBJECT(textview
->link_popup_menu
),
2825 "menu_button", uri
);
2826 gtk_menu_popup(GTK_MENU(textview
->link_popup_menu
),
2827 NULL
, NULL
, NULL
, NULL
,
2828 bevent
->button
, bevent
->time
);
2832 if (bevent
->button
== 3 && !qlink
) {
2834 G_OBJECT(textview
->file_popup_menu
),
2835 "menu_button", uri
);
2836 gtk_menu_popup(GTK_MENU(textview
->file_popup_menu
),
2837 NULL
, NULL
, NULL
, NULL
,
2838 bevent
->button
, bevent
->time
);
2847 gchar
*textview_get_visible_uri (TextView
*textview
,
2850 GtkTextBuffer
*buffer
;
2851 GtkTextIter start
, end
;
2853 buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview
->text
));
2855 gtk_text_buffer_get_iter_at_offset(buffer
, &start
, uri
->start
);
2856 gtk_text_buffer_get_iter_at_offset(buffer
, &end
, uri
->end
);
2858 return gtk_text_buffer_get_text(buffer
, &start
, &end
, FALSE
);
2862 *\brief Check to see if a web URL has been disguised as a different
2863 * URL (possible with HTML email).
2865 *\param uri The uri to check
2867 *\param textview The TextView the URL is contained in
2869 *\return gboolean TRUE if the URL is ok, or if the user chose to open
2870 * it anyway, otherwise FALSE
2872 gboolean
textview_uri_security_check(TextView
*textview
, ClickableText
*uri
)
2875 gboolean retval
= TRUE
;
2877 if (is_uri_string(uri
->uri
) == FALSE
)
2880 visible_str
= textview_get_visible_uri(textview
, uri
);
2881 if (visible_str
== NULL
)
2884 g_strstrip(visible_str
);
2886 if (strcmp(visible_str
, uri
->uri
) != 0 && is_uri_string(visible_str
)) {
2888 gchar
*visible_uri_path
;
2890 uri_path
= get_uri_path(uri
->uri
);
2891 visible_uri_path
= get_uri_path(visible_str
);
2892 if (path_cmp(uri_path
, visible_uri_path
) != 0)
2896 if (retval
== FALSE
) {
2900 msg
= g_markup_printf_escaped("%s\n\n"
2904 _("The real URL is different from the displayed URL."),
2905 _("Displayed URL:"), visible_str
,
2906 _("Real URL:"), uri
->uri
,
2907 _("Open it anyway?"));
2908 aval
= alertpanel_full(_("Phishing attempt warning"), msg
,
2909 GTK_STOCK_CANCEL
, _("_Open URL"), NULL
, FALSE
,
2910 NULL
, ALERT_WARNING
, G_ALERTDEFAULT
);
2912 if (aval
== G_ALERTALTERNATE
)
2916 g_free(visible_str
);
2921 static void textview_uri_list_remove_all(GSList
*uri_list
)
2925 for (cur
= uri_list
; cur
!= NULL
; cur
= cur
->next
) {
2927 g_free(((ClickableText
*)cur
->data
)->uri
);
2928 g_free(((ClickableText
*)cur
->data
)->filename
);
2929 if (((ClickableText
*)cur
->data
)->is_quote
) {
2930 g_free(((ClickableText
*)cur
->data
)->fg_color
);
2931 g_free(((ClickableText
*)cur
->data
)->data
);
2932 /* (only free data in quotes uris) */
2938 g_slist_free(uri_list
);
2941 static void open_uri_cb (GtkAction
*action
, TextView
*textview
)
2943 ClickableText
*uri
= g_object_get_data(G_OBJECT(textview
->link_popup_menu
),
2945 const gchar
*raw_url
= g_object_get_data(G_OBJECT(textview
->link_popup_menu
),
2949 if (textview_uri_security_check(textview
, uri
) == TRUE
)
2951 prefs_common_get_uri_cmd());
2952 g_object_set_data(G_OBJECT(textview
->link_popup_menu
), "menu_button",
2956 open_uri(raw_url
, prefs_common_get_uri_cmd());
2957 g_object_set_data(G_OBJECT(textview
->link_popup_menu
), "raw_url",
2962 static void open_image_cb (GtkAction
*action
, TextView
*textview
)
2964 ClickableText
*uri
= g_object_get_data(G_OBJECT(textview
->file_popup_menu
),
2970 gchar
*filename
= NULL
, *filepath
= NULL
;
2971 gchar
*tmp_filename
= NULL
;
2976 if (uri
->filename
== NULL
)
2979 filename
= g_strdup(uri
->filename
);
2981 if (!g_utf8_validate(filename
, -1, NULL
)) {
2982 gchar
*tmp
= conv_filename_to_utf8(filename
);
2987 subst_for_filename(filename
);
2989 filepath
= g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S
,
2992 tmp_filename
= g_filename_from_uri(uri
->uri
, NULL
, NULL
);
2993 copy_file(tmp_filename
, filepath
, FALSE
);
2994 g_free(tmp_filename
);
2996 cmd
= mailcap_get_command_for_type("image/jpeg", filename
);
2998 gboolean remember
= FALSE
;
2999 cmd
= input_dialog_combo_remember
3001 _("Enter the command-line to open file:\n"
3002 "('%s' will be replaced with file name)"),
3003 prefs_common
.mime_open_cmd
,
3004 prefs_common
.mime_open_cmd_history
,
3006 if (cmd
&& remember
) {
3007 mailcap_update_default("image/jpeg", cmd
);
3010 if (cmd
&& (p
= strchr(cmd
, '%')) && *(p
+ 1) == 's' &&
3011 !strchr(p
+ 2, '%'))
3012 g_snprintf(buf
, sizeof(buf
), cmd
, filepath
);
3014 g_warning("Image viewer command-line is invalid: '%s'", cmd
);
3020 execute_command_line(buf
, TRUE
, NULL
);
3026 g_object_set_data(G_OBJECT(textview
->file_popup_menu
), "menu_button",
3030 static void save_file_cb (GtkAction
*action
, TextView
*textview
)
3032 ClickableText
*uri
= g_object_get_data(G_OBJECT(textview
->file_popup_menu
),
3034 gchar
*filename
= NULL
;
3035 gchar
*filepath
= NULL
;
3036 gchar
*filedir
= NULL
;
3037 gchar
*tmp_filename
= NULL
;
3043 if (uri
->filename
== NULL
)
3046 filename
= g_strdup(uri
->filename
);
3048 if (!g_utf8_validate(filename
, -1, NULL
)) {
3049 gchar
*tmp
= conv_filename_to_utf8(filename
);
3054 subst_for_filename(filename
);
3056 if (prefs_common
.attach_save_dir
&& *prefs_common
.attach_save_dir
)
3057 filepath
= g_strconcat(prefs_common
.attach_save_dir
,
3058 G_DIR_SEPARATOR_S
, filename
, NULL
);
3060 filepath
= g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S
,
3065 /* Pick correct window to set the file dialog "transient for" */
3066 if (textview
->messageview
->window
!= NULL
)
3067 window
= textview
->messageview
->window
;
3069 window
= textview
->messageview
->mainwin
->window
;
3071 manage_window_focus_in(window
, NULL
, NULL
);
3073 filename
= filesel_select_file_save(_("Save as"), filepath
);
3079 if (is_file_exist(filename
)) {
3083 res
= g_strdup_printf(_("Overwrite existing file '%s'?"),
3085 aval
= alertpanel(_("Overwrite"), res
, GTK_STOCK_CANCEL
,
3086 GTK_STOCK_OK
, NULL
);
3088 if (G_ALERTALTERNATE
!= aval
)
3092 tmp_filename
= g_filename_from_uri(uri
->uri
, NULL
, NULL
);
3093 copy_file(tmp_filename
, filename
, FALSE
);
3094 g_free(tmp_filename
);
3096 filedir
= g_path_get_dirname(filename
);
3097 if (filedir
&& strcmp(filedir
, ".")) {
3098 g_free(prefs_common
.attach_save_dir
);
3099 prefs_common
.attach_save_dir
= g_filename_to_utf8(filedir
, -1, NULL
, NULL
, NULL
);
3105 g_object_set_data(G_OBJECT(textview
->file_popup_menu
), "menu_button",
3109 static void copy_uri_cb (GtkAction
*action
, TextView
*textview
)
3111 ClickableText
*uri
= g_object_get_data(G_OBJECT(textview
->link_popup_menu
),
3113 const gchar
*raw_url
= g_object_get_data(G_OBJECT(textview
->link_popup_menu
),
3116 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY
), uri
->uri
, -1);
3117 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD
), uri
->uri
, -1);
3118 g_object_set_data(G_OBJECT(textview
->link_popup_menu
), "menu_button",
3122 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY
), raw_url
, -1);
3123 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD
), raw_url
, -1);
3124 g_object_set_data(G_OBJECT(textview
->link_popup_menu
), "raw_url",
3129 static void add_uri_to_addrbook_cb (GtkAction
*action
, TextView
*textview
)
3131 gchar
*fromname
, *fromaddress
;
3132 ClickableText
*uri
= g_object_get_data(G_OBJECT(textview
->mail_popup_menu
),
3134 AvatarRender
*avatarr
= NULL
;
3135 GdkPixbuf
*picture
= NULL
;
3136 gboolean use_picture
= FALSE
;
3142 fromaddress
= g_strdup(uri
->uri
+ 7);
3144 if (textview
->messageview
->msginfo
&&
3145 !strcmp2(fromaddress
, textview
->messageview
->msginfo
->from
))
3148 fromname
= procheader_get_fromname(fromaddress
);
3149 extract_address(fromaddress
);
3152 avatarr
= avatars_avatarrender_new(textview
->messageview
->msginfo
);
3153 hooks_invoke(AVATAR_IMAGE_RENDER_HOOKLIST
, avatarr
);
3156 if (avatarr
&& avatarr
->image
) {
3157 picture
= gtk_image_get_pixbuf(GTK_IMAGE(avatarr
->image
));
3160 avatars_avatarrender_free(avatarr
);
3163 #ifndef USE_ALT_ADDRBOOK
3164 addressbook_add_contact( fromname
, fromaddress
, NULL
, picture
);
3166 if (addressadd_selection(fromname
, fromaddress
, NULL
, picture
)) {
3167 debug_print( "addressbook_add_contact - added\n" );
3171 g_free(fromaddress
);
3175 static void reply_to_uri_cb (GtkAction
*action
, TextView
*textview
)
3177 ClickableText
*uri
= g_object_get_data(G_OBJECT(textview
->mail_popup_menu
),
3179 if (!textview
->messageview
|| !uri
)
3182 compose_reply_to_address (textview
->messageview
,
3183 textview
->messageview
->msginfo
, uri
->uri
+7);
3186 static void mail_to_uri_cb (GtkAction
*action
, TextView
*textview
)
3188 PrefsAccount
*account
= NULL
;
3190 ClickableText
*uri
= g_object_get_data(G_OBJECT(textview
->mail_popup_menu
),
3195 if (textview
->messageview
&& textview
->messageview
->msginfo
&&
3196 textview
->messageview
->msginfo
->folder
) {
3197 FolderItem
*folder_item
;
3199 folder_item
= textview
->messageview
->msginfo
->folder
;
3200 if (folder_item
->prefs
&& folder_item
->prefs
->enable_default_account
)
3201 account
= account_find_from_id(folder_item
->prefs
->default_account
);
3203 compose
= compose_new_with_folderitem(account
, folder_item
, uri
->uri
+7);
3205 compose
= compose_new(account
, uri
->uri
+ 7, NULL
);
3207 compose_check_for_email_account(compose
);
3210 static void copy_mail_to_uri_cb (GtkAction
*action
, TextView
*textview
)
3212 ClickableText
*uri
= g_object_get_data(G_OBJECT(textview
->mail_popup_menu
),
3217 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY
), uri
->uri
+7, -1);
3218 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD
), uri
->uri
+7, -1);
3219 g_object_set_data(G_OBJECT(textview
->mail_popup_menu
), "menu_button",
3223 void textview_get_selection_offsets(TextView
*textview
, gint
*sel_start
, gint
*sel_end
)
3225 GtkTextView
*text
= GTK_TEXT_VIEW(textview
->text
);
3226 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer(text
);
3227 GtkTextIter start
, end
;
3228 if (gtk_text_buffer_get_selection_bounds(buffer
, &start
, &end
)) {
3230 *sel_start
= gtk_text_iter_get_offset(&start
);
3232 *sel_end
= gtk_text_iter_get_offset(&end
);