Fix bug #3574: Template addressing
[claws.git] / src / textview.c
blobb73071f071f83d5bafbba0d16ec2077627339740
1 /*
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/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
24 #include "defs.h"
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gdk/gdk.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <gtk/gtk.h>
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #if HAVE_SYS_WAIT_H
37 #include <sys/wait.h>
38 #endif
40 #include "main.h"
41 #include "summaryview.h"
42 #include "procheader.h"
43 #include "prefs_common.h"
44 #include "codeconv.h"
45 #include "utils.h"
46 #include "gtkutils.h"
47 #include "procmime.h"
48 #include "html.h"
49 #include "enriched.h"
50 #include "compose.h"
51 #ifndef USE_ALT_ADDRBOOK
52 #include "addressbook.h"
53 #include "addrindex.h"
54 #else
55 #include "addressbook-dbus.h"
56 #include "addressadd.h"
57 #endif
58 #include "displayheader.h"
59 #include "account.h"
60 #include "mimeview.h"
61 #include "alertpanel.h"
62 #include "menu.h"
63 #include "image_viewer.h"
64 #include "filesel.h"
65 #include "inputdialog.h"
66 #include "timing.h"
67 #include "tags.h"
68 #include "manage_window.h"
69 #include "folder_item_prefs.h"
70 #include "hooks.h"
71 #include "avatars.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 = {
85 (gulong)0,
86 (gushort)0x7fff,
87 (gushort)0x7fff,
88 (gushort)0x7fff
91 static GdkColor uri_color = {
92 (gulong)0,
93 (gushort)0,
94 (gushort)0,
95 (gushort)0
98 static GdkColor emphasis_color = {
99 (gulong)0,
100 (gushort)0,
101 (gushort)0,
102 (gushort)0
105 static GdkColor diff_added_color = {
106 (gulong)0,
107 (gushort)0,
108 (gushort)0,
109 (gushort)0
112 static GdkColor diff_deleted_color = {
113 (gulong)0,
114 (gushort)0,
115 (gushort)0,
116 (gushort)0
119 static GdkColor diff_hunk_color = {
120 (gulong)0,
121 (gushort)0,
122 (gushort)0,
123 (gushort)0
126 static GdkColor tags_bgcolor = {
127 (gulong)0,
128 (gushort)0,
129 (gushort)0,
130 (gushort)0
133 static GdkColor tags_color = {
134 (gulong)0,
135 (gushort)0,
136 (gushort)0,
137 (gushort)0
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,
157 FILE *fp,
158 CodeConverter *conv);
159 static void textview_add_part (TextView *textview,
160 MimeInfo *mimeinfo);
161 static void textview_add_parts (TextView *textview,
162 MimeInfo *mimeinfo);
163 static void textview_write_body (TextView *textview,
164 MimeInfo *mimeinfo);
165 static void textview_show_html (TextView *textview,
166 FILE *fp,
167 CodeConverter *conv);
169 static void textview_write_line (TextView *textview,
170 const gchar *str,
171 CodeConverter *conv,
172 gboolean do_quote_folding);
173 static void textview_write_link (TextView *textview,
174 const gchar *str,
175 const gchar *uri,
176 CodeConverter *conv);
178 static GPtrArray *textview_scan_header (TextView *textview,
179 FILE *fp);
180 static void textview_show_header (TextView *textview,
181 GPtrArray *headers);
183 static gint textview_key_pressed (GtkWidget *widget,
184 GdkEventKey *event,
185 TextView *textview);
186 static gboolean textview_motion_notify (GtkWidget *widget,
187 GdkEventMotion *motion,
188 TextView *textview);
189 static gboolean textview_leave_notify (GtkWidget *widget,
190 GdkEventCrossing *event,
191 TextView *textview);
192 static gboolean textview_visibility_notify (GtkWidget *widget,
193 GdkEventVisibility *event,
194 TextView *textview);
195 static void textview_uri_update (TextView *textview,
196 gint x,
197 gint y);
198 static gboolean textview_get_uri_range (TextView *textview,
199 GtkTextIter *iter,
200 GtkTextTag *tag,
201 GtkTextIter *start_iter,
202 GtkTextIter *end_iter);
203 static ClickableText *textview_get_uri_from_range (TextView *textview,
204 GtkTextIter *iter,
205 GtkTextTag *tag,
206 GtkTextIter *start_iter,
207 GtkTextIter *end_iter);
208 static ClickableText *textview_get_uri (TextView *textview,
209 GtkTextIter *iter,
210 GtkTextTag *tag);
211 static gboolean textview_uri_button_pressed (GtkTextTag *tag,
212 GObject *obj,
213 GdkEvent *event,
214 GtkTextIter *iter,
215 TextView *textview);
217 static void textview_uri_list_remove_all (GSList *uri_list);
219 static void textview_toggle_quote (TextView *textview,
220 GSList *start_list,
221 ClickableText *uri,
222 gboolean expand_only);
224 static void open_uri_cb (GtkAction *action,
225 TextView *textview);
226 static void copy_uri_cb (GtkAction *action,
227 TextView *textview);
228 static void add_uri_to_addrbook_cb (GtkAction *action,
229 TextView *textview);
230 static void reply_to_uri_cb (GtkAction *action,
231 TextView *textview);
232 static void mail_to_uri_cb (GtkAction *action,
233 TextView *textview);
234 static void copy_mail_to_uri_cb (GtkAction *action,
235 TextView *textview);
236 static void save_file_cb (GtkAction *action,
237 TextView *textview);
238 static void open_image_cb (GtkAction *action,
239 TextView *textview);
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)
267 #ifndef WIDTH
268 # define WIDTH 48
269 # define HEIGHT 48
270 #endif
271 if (textview->image) {
272 GtkAllocation allocation;
273 gint x, y, x1;
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,
286 gpointer data)
288 scrolled_cb(NULL, (TextView *)data);
291 TextView *textview_create(void)
293 TextView *textview;
294 GtkWidget *vbox;
295 GtkWidget *scrolledwin;
296 GtkWidget *text;
297 GtkTextBuffer *buffer;
298 GtkClipboard *clipboard;
299 GtkAdjustment *adj;
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),
309 GTK_SHADOW_IN);
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),
347 textview);
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,
360 "TextviewPopupLink",
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,
364 "TextviewPopupMail",
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,
368 "TextviewPopupFile",
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;
411 return textview;
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;
420 if (!font_desc)
421 font_desc = pango_font_description_from_string
422 (NORMAL_FONT);
424 if (!bold_font_desc) {
425 if (prefs_common.derive_from_normal_font || !BOLD_FONT) {
426 bold_font_desc = pango_font_description_from_string
427 (NORMAL_FONT);
428 pango_font_description_set_weight
429 (bold_font_desc, PANGO_WEIGHT_BOLD);
430 } else {
431 bold_font_desc = pango_font_description_from_string
432 (BOLD_FONT);
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,
444 "left-margin", 3,
445 "left-margin-set", TRUE,
446 NULL);
447 gtk_text_buffer_create_tag(buffer, "header_title",
448 "font-desc", bold_font_desc,
449 NULL);
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,
456 "left-margin", 3,
457 "left-margin-set", TRUE,
458 "foreground-gdk", &uri_color,
459 NULL);
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", &quote_colors[0],
465 "paragraph-background-gdk", &quote_bgcolors[0],
466 NULL);
467 gtk_text_buffer_create_tag(buffer, "quote1",
468 "foreground-gdk", &quote_colors[1],
469 "paragraph-background-gdk", &quote_bgcolors[1],
470 NULL);
471 gtk_text_buffer_create_tag(buffer, "quote2",
472 "foreground-gdk", &quote_colors[2],
473 "paragraph-background-gdk", &quote_bgcolors[2],
474 NULL);
475 } else {
476 gtk_text_buffer_create_tag(buffer, "quote0",
477 "foreground-gdk", &quote_colors[0],
478 NULL);
479 gtk_text_buffer_create_tag(buffer, "quote1",
480 "foreground-gdk", &quote_colors[1],
481 NULL);
482 gtk_text_buffer_create_tag(buffer, "quote2",
483 "foreground-gdk", &quote_colors[2],
484 NULL);
486 gtk_text_buffer_create_tag(buffer, "tags",
487 "foreground-gdk", &tags_color,
488 "paragraph-background-gdk", &tags_bgcolor,
489 NULL);
490 gtk_text_buffer_create_tag(buffer, "emphasis",
491 "foreground-gdk", &emphasis_color,
492 NULL);
493 gtk_text_buffer_create_tag(buffer, "signature",
494 "foreground-gdk", &signature_color,
495 NULL);
496 tag = gtk_text_buffer_create_tag(buffer, "link",
497 "foreground-gdk", &uri_color,
498 NULL);
499 qtag = gtk_text_buffer_create_tag(buffer, "qlink",
500 NULL);
501 gtk_text_buffer_create_tag(buffer, "link-hover",
502 "underline", PANGO_UNDERLINE_SINGLE,
503 NULL);
504 gtk_text_buffer_create_tag(buffer, "diff-add",
505 "foreground-gdk", &diff_added_color,
506 NULL);
507 gtk_text_buffer_create_tag(buffer, "diff-del",
508 "foreground-gdk", &diff_deleted_color,
509 NULL);
510 gtk_text_buffer_create_tag(buffer, "diff-add-file",
511 "foreground-gdk", &diff_added_color,
512 "weight", PANGO_WEIGHT_BOLD,
513 NULL);
514 gtk_text_buffer_create_tag(buffer, "diff-del-file",
515 "foreground-gdk", &diff_deleted_color,
516 "weight", PANGO_WEIGHT_BOLD,
517 NULL);
518 gtk_text_buffer_create_tag(buffer, "diff-hunk",
519 "foreground-gdk", &diff_hunk_color,
520 "weight", PANGO_WEIGHT_BOLD,
521 NULL);
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);
526 /* if (font_desc)
527 pango_font_description_free(font_desc);
528 if (bold_font_desc)
529 pango_font_description_free(bold_font_desc);*/
532 void textview_init(TextView *textview)
534 if (!hand_cursor)
535 hand_cursor = gdk_cursor_new(GDK_HAND2);
536 if (!text_cursor)
537 text_cursor = gdk_cursor_new(GDK_XTERM);
538 if (!watch_cursor)
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); \
548 if (tag) \
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,
569 &quote_colors[0]);
570 gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_col,
571 &quote_colors[1]);
572 gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_col,
573 &quote_colors[2]);
574 gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
575 &uri_color);
576 gtkut_convert_int_to_gdk_color(prefs_common.signature_col,
577 &signature_color);
578 gtkut_convert_int_to_gdk_color(prefs_common.emphasis_col,
579 &emphasis_color);
580 gtkut_convert_int_to_gdk_color(prefs_common.diff_added_color,
581 &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,
585 &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,
589 &quote_bgcolors[0]);
590 gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_bgcol,
591 &quote_bgcolors[1]);
592 gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_bgcol,
593 &quote_bgcolors[2]);
594 CHANGE_TAG_COLOR("quote0", &quote_colors[0], &quote_bgcolors[0]);
595 CHANGE_TAG_COLOR("quote1", &quote_colors[1], &quote_bgcolors[1]);
596 CHANGE_TAG_COLOR("quote2", &quote_colors[2], &quote_bgcolors[2]);
597 } else {
598 CHANGE_TAG_COLOR("quote0", &quote_colors[0], NULL);
599 CHANGE_TAG_COLOR("quote1", &quote_colors[1], NULL);
600 CHANGE_TAG_COLOR("quote2", &quote_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,
614 &tags_bgcolor);
615 gtkut_convert_int_to_gdk_color(prefs_common.tags_color,
616 &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)
630 START_TIMING("");
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);
642 } else {
643 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0)
644 perror("fseek");
646 textview_write_body(textview, mimeinfo);
649 textview->loading = FALSE;
650 textview->stop_loading = FALSE;
651 textview_set_position(textview, 0);
653 END_TIMING();
656 static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
658 GtkAllocation allocation;
659 GtkTextView *text;
660 GtkTextBuffer *buffer;
661 GtkTextIter iter, start_iter;
662 gchar buf[BUFFSIZE];
663 GPtrArray *headers = NULL;
664 const gchar *name;
665 gchar *content_type;
666 gint charcount;
668 START_TIMING("");
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) {
677 return;
679 if (mimeinfo->type == MIMETYPE_MULTIPART) {
680 END_TIMING();
681 return;
684 textview->prev_quote_level = -1;
686 if ((mimeinfo->type == MIMETYPE_MESSAGE) && !g_ascii_strcasecmp(mimeinfo->subtype, "rfc822")) {
687 FILE *fp;
688 if (mimeinfo->content == MIMECONTENT_MEM)
689 fp = str_open_as_stream(mimeinfo->data.mem);
690 else
691 fp = g_fopen(mimeinfo->data.filename, "rb");
692 if (!fp) {
693 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
694 END_TIMING();
695 return;
697 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
698 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
699 fclose(fp);
700 END_TIMING();
701 return;
703 headers = textview_scan_header(textview, fp);
704 if (headers) {
705 if (charcount > 0)
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);
713 fclose(fp);
714 END_TIMING();
715 return;
718 name = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
719 content_type = procmime_get_content_type_str(mimeinfo->type,
720 mimeinfo->subtype);
721 if (name == NULL)
722 name = procmime_mimeinfo_get_parameter(mimeinfo, "name");
723 if (name != NULL)
724 g_snprintf(buf, sizeof(buf), _("[%s %s (%d bytes)]"),
725 name, content_type, mimeinfo->length);
726 else
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 ) {
740 GdkPixbuf *pixbuf;
741 GError *error = NULL;
742 gchar *filename;
743 ClickableText *uri;
744 gchar *uri_str;
745 gint err;
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));
752 g_free(filename);
753 END_TIMING();
754 return;
757 pixbuf = gdk_pixbuf_new_from_file(filename, &error);
758 if (textview->stop_loading) {
759 return;
761 if (error != NULL) {
762 g_warning("%s", error->message);
763 g_error_free(error);
765 if (!pixbuf) {
766 g_warning("Can't load the image.");
767 g_free(filename);
768 END_TIMING();
769 return;
772 gtk_widget_get_allocation(textview->scrolledwin, &allocation);
773 pixbuf = claws_load_pixbuf_fitting(pixbuf,
774 allocation.width,
775 allocation.height);
777 if (textview->stop_loading) {
778 return;
781 uri_str = g_filename_to_uri(filename, NULL, NULL);
782 if (uri_str) {
783 uri = g_new0(ClickableText, 1);
784 uri->uri = uri_str;
785 uri->start = gtk_text_iter_get_offset(&iter);
787 gtk_text_buffer_insert_pixbuf(buffer, &iter, pixbuf);
788 if (textview->stop_loading) {
789 g_free(uri);
790 return;
792 uri->end = uri->start + 1;
793 uri->filename = procmime_get_part_file_name(mimeinfo);
794 textview->uri_list =
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",
800 &start_iter, &iter);
801 } else {
802 gtk_text_buffer_insert_pixbuf(buffer, &iter, pixbuf);
803 if (textview->stop_loading) {
804 return;
806 gtk_text_buffer_insert(buffer, &iter, " ", 1);
809 g_object_unref(pixbuf);
810 g_free(filename);
811 END_TIMING();
812 GTK_EVENTS_FLUSH();
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);
830 END_TIMING();
833 static void recursive_add_parts(TextView *textview, GNode *node)
835 GNode * iter;
836 MimeInfo *mimeinfo;
837 START_TIMING("");
839 mimeinfo = (MimeInfo *) node->data;
841 textview_add_part(textview, mimeinfo);
842 #ifdef GENERIC_UMPC
843 textview_set_position(textview, 0);
844 #endif
845 if ((mimeinfo->type != MIMETYPE_MULTIPART) &&
846 (mimeinfo->type != MIMETYPE_MESSAGE)) {
847 END_TIMING();
848 return;
850 if (g_ascii_strcasecmp(mimeinfo->subtype, "alternative") == 0) {
851 GNode * preferred_body;
852 int preferred_score;
855 text/plain : score 3
856 text/ * : score 2
857 other : score 1
859 preferred_body = NULL;
860 preferred_score = 0;
862 for (iter = g_node_first_child(node) ; iter != NULL ;
863 iter = g_node_next_sibling(iter)) {
864 int score;
865 MimeInfo * submime;
867 score = 1;
868 submime = (MimeInfo *) iter->data;
869 if (submime->type == MIMETYPE_TEXT)
870 score = 2;
872 if (submime->subtype != NULL) {
873 if (g_ascii_strcasecmp(submime->subtype, "plain") == 0)
874 score = 3;
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);
887 else {
888 for (iter = g_node_first_child(node) ; iter != NULL ;
889 iter = g_node_next_sibling(iter)) {
890 recursive_add_parts(textview, iter);
893 END_TIMING();
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)
906 GtkTextView *text;
907 GtkTextBuffer *buffer;
908 GtkTextIter iter;
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"
920 "\n"
921 " Use "));
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)
929 GtkTextView *text;
930 GtkTextBuffer *buffer;
931 GtkTextIter iter;
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)
947 GtkTextView *text;
948 GtkTextBuffer *buffer;
949 GtkTextIter iter;
950 const gchar *name;
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;
958 else
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");
971 if (name == NULL)
972 name = procmime_mimeinfo_get_parameter(partinfo, "name");
973 if (name != NULL) {
974 content_type = procmime_get_content_type_str(partinfo->type,
975 partinfo->subtype);
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"));
987 #ifndef GENERIC_UMPC
988 TEXTVIEW_INSERT(_(" by right-clicking the icon or list item:"));
989 #endif
990 TEXTVIEW_INSERT("\n");
992 TEXTVIEW_INSERT(_(" - To save, select "));
993 TEXTVIEW_INSERT_LINK(_("'Save as...'"), "sc://save_as", NULL);
994 #ifndef GENERIC_UMPC
995 TEXTVIEW_INSERT(_(" (Shortcut key: '"));
996 shortcut = cm_menu_item_get_shortcut(ui_manager, "Menu/File/SavePartAs");
997 TEXTVIEW_INSERT(shortcut);
998 g_free(shortcut);
999 TEXTVIEW_INSERT("')");
1000 #endif
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);
1010 g_free(shortcut);
1011 TEXTVIEW_INSERT("')");
1012 #endif
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);
1022 g_free(shortcut);
1023 TEXTVIEW_INSERT("')\n");
1024 TEXTVIEW_INSERT(_(" (alternately double-click, or click the middle "));
1025 TEXTVIEW_INSERT(_("mouse button)\n"));
1026 #ifndef G_OS_WIN32
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);
1032 g_free(shortcut);
1033 TEXTVIEW_INSERT("')");
1034 #endif
1035 #endif
1036 TEXTVIEW_INSERT("\n");
1038 textview_show_icon(textview, GTK_STOCK_DIALOG_INFO);
1041 static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
1043 FILE *tmpfp;
1044 gchar buf[BUFFSIZE];
1045 CodeConverter *conv;
1046 const gchar *charset;
1047 #ifndef G_OS_WIN32
1048 const gchar *p, *cmd;
1049 #endif
1050 GSList *cur;
1051 gboolean continue_write = TRUE;
1052 size_t wrote = 0, i = 0;
1054 if (textview->messageview->forced_charset)
1055 charset = textview->messageview->forced_charset;
1056 else {
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) {
1082 gchar *filename;
1084 filename = procmime_get_tmp_file_name(mimeinfo);
1085 if (procmime_get_part(filename, mimeinfo) == 0) {
1086 tmpfp = g_fopen(filename, "rb");
1087 if (tmpfp) {
1088 textview_show_html(textview, tmpfp, conv);
1089 fclose(tmpfp);
1091 claws_unlink(filename);
1093 g_free(filename);
1094 } else if (!g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
1095 gchar *filename;
1097 filename = procmime_get_tmp_file_name(mimeinfo);
1098 if (procmime_get_part(filename, mimeinfo) == 0) {
1099 tmpfp = g_fopen(filename, "rb");
1100 if (tmpfp) {
1101 textview_show_ertf(textview, tmpfp, conv);
1102 fclose(tmpfp);
1104 claws_unlink(filename);
1106 g_free(filename);
1107 #ifndef G_OS_WIN32
1108 } else if ( g_ascii_strcasecmp(mimeinfo->subtype, "plain") &&
1109 (cmd = prefs_common.mime_textviewer) && *cmd &&
1110 (p = strchr(cmd, '%')) && *(p + 1) == 's') {
1111 int pid, pfd[2];
1112 const gchar *fname;
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;
1127 pid = fork();
1128 if (pid < 0) {
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);
1132 close(pfd[0]);
1133 close(pfd[1]);
1134 goto textview_default;
1136 if (pid == 0) { /* child */
1137 int rc;
1138 gchar **argv;
1139 argv = strsplit_with_quote(buf, " ", 0);
1140 close(1);
1141 close(pfd[0]);
1142 rc = dup(pfd[1]);
1143 rc = execvp(argv[0], argv);
1144 close(pfd[1]);
1145 g_print(_("The command to view attachment "
1146 "as text failed:\n"
1147 " %s\n"
1148 "Exit code %d\n"), buf, rc);
1149 exit(255);
1151 close(pfd[1]);
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) {
1157 fclose(tmpfp);
1158 waitpid(pid, pfd, 0);
1159 g_unlink(fname);
1160 return;
1164 fclose(tmpfp);
1165 waitpid(pid, pfd, 0);
1166 g_unlink(fname);
1167 #endif
1168 } else {
1169 #ifndef G_OS_WIN32
1170 textview_default:
1171 #endif
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);
1178 else
1179 tmpfp = g_fopen(mimeinfo->data.filename, "rb");
1180 if (!tmpfp) {
1181 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1182 return;
1184 if (fseek(tmpfp, mimeinfo->offset, SEEK_SET) < 0) {
1185 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
1186 fclose(tmpfp);
1187 return;
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) {
1195 fclose(tmpfp);
1196 return;
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;
1205 fclose(tmpfp);
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;
1214 if (!uri->is_quote)
1215 continue;
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) {
1220 return;
1225 if (continue_write == FALSE) {
1226 messageview_show_partial_display(
1227 textview->messageview,
1228 textview->messageview->msginfo,
1229 mimeinfo->length);
1231 GTK_EVENTS_FLUSH();
1234 static void textview_show_html(TextView *textview, FILE *fp,
1235 CodeConverter *conv)
1237 SC_HTMLParser *parser;
1238 gchar *str;
1239 gint lines = 0;
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, " ");
1251 if (str) {
1252 while (str && *str && g_ascii_isspace(*str))
1253 str++;
1254 parser->href = g_strdup(str);
1255 /* the URL may (or not) be followed by the
1256 * referenced text */
1257 str = strtok(NULL, "");
1260 if (str != NULL)
1261 textview_write_link(textview, str, parser->href, NULL);
1262 } else
1263 textview_write_line(textview, str, NULL, FALSE);
1264 lines++;
1265 if (lines % 500 == 0)
1266 GTK_EVENTS_FLUSH();
1267 if (textview->stop_loading) {
1268 return;
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)
1278 ERTFParser *parser;
1279 gchar *str;
1280 gint lines = 0;
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);
1287 lines++;
1288 if (lines % 500 == 0)
1289 GTK_EVENTS_FLUSH();
1290 if (textview->stop_loading) {
1291 return;
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; \
1303 } else { \
1304 g_warning("alloc error scanning URIs"); \
1305 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, \
1306 linebuf, -1, \
1307 fg_tag, NULL); \
1308 return; \
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; \
1316 } else { \
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,
1325 gboolean hdr)
1327 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1328 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
1329 GtkTextIter iter;
1330 gchar *mybuf = g_strdup(linebuf);
1332 /* parse table - in order of priority */
1333 struct table {
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,
1342 const gchar **bp_,
1343 const gchar **ep_,
1344 gboolean hdr);
1345 /* part to URI function */
1346 gchar *(*build_uri) (const gchar *bp,
1347 const gchar *ep);
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];
1362 gint n;
1363 const gchar *walk, *bp, *ep;
1365 struct txtpos {
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)) {
1372 g_free(mybuf);
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++) {
1386 gchar *tmp;
1388 tmp = parser[n].search(walk, parser[n].needle);
1389 if (tmp) {
1390 if (scanpos == NULL || tmp < scanpos) {
1391 scanpos = tmp;
1392 last_index = n;
1397 if (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);
1402 walk = ep;
1403 } else
1404 walk = scanpos +
1405 strlen(parser[last_index].needle);
1406 } else
1407 break;
1410 /* colorize this line */
1411 if (head.next) {
1412 const gchar *normal_text = mybuf;
1414 /* insert URIs */
1415 for (last = head.next; last != NULL;
1416 normal_text = last->ep, last = last->next) {
1417 ClickableText *uri;
1418 uri = g_new0(ClickableText, 1);
1419 if (last->bp - normal_text > 0)
1420 gtk_text_buffer_insert_with_tags_by_name
1421 (buffer, &iter,
1422 normal_text,
1423 last->bp - normal_text,
1424 fg_tag, NULL);
1425 uri->uri = parser[last->pti].build_uri(last->bp,
1426 last->ep);
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);
1437 if (*normal_text)
1438 gtk_text_buffer_insert_with_tags_by_name
1439 (buffer, &iter, normal_text, -1, fg_tag, NULL);
1440 } else {
1441 gtk_text_buffer_insert_with_tags_by_name
1442 (buffer, &iter, mybuf, -1, fg_tag, NULL);
1444 g_free(mybuf);
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;
1454 gchar *mybuf;
1455 gint offset = 0;
1456 /* parse table - in order of priority */
1457 struct table {
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,
1466 const gchar **bp_,
1467 const gchar **ep_,
1468 gboolean hdr);
1469 /* part to URI function */
1470 gchar *(*build_uri) (const gchar *bp,
1471 const gchar *ep);
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];
1485 gint n;
1486 const gchar *walk, *bp, *ep;
1488 struct txtpos {
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++) {
1506 gchar *tmp;
1508 tmp = parser[n].search(walk, parser[n].needle);
1509 if (tmp) {
1510 if (scanpos == NULL || tmp < scanpos) {
1511 scanpos = tmp;
1512 last_index = n;
1517 if (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);
1522 walk = ep;
1523 } else
1524 walk = scanpos +
1525 strlen(parser[last_index].needle);
1526 } else
1527 break;
1530 /* colorize this line */
1531 if (head.next) {
1532 /* insert URIs */
1533 for (last = head.next; last != NULL; last = last->next) {
1534 ClickableText *uri;
1535 gint start_offset, end_offset;
1536 gchar *tmp_str;
1537 gchar old_char;
1538 uri = g_new0(ClickableText, 1);
1539 uri->uri = parser[last->pti].build_uri(last->bp,
1540 last->ep);
1542 tmp_str = mybuf;
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);
1567 g_free(mybuf);
1570 #undef ADD_TXT_POS
1572 static void textview_write_line(TextView *textview, const gchar *str,
1573 CodeConverter *conv, gboolean do_quote_folding)
1575 GtkTextView *text;
1576 GtkTextBuffer *buffer;
1577 GtkTextIter iter;
1578 gchar buf[BUFFSIZE];
1579 gchar *fg_color;
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);
1587 if (!conv)
1588 strncpy2(buf, str, sizeof(buf));
1589 else if (conv_convert(conv, buf, sizeof(buf), str) < 0)
1590 conv_localetodisp(buf, sizeof(buf), str);
1592 strcrchomp(buf);
1593 fg_color = NULL;
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)
1607 quotelevel %= 3;
1608 else
1609 quotelevel = 2;
1613 if (quotelevel == -1)
1614 fg_color = NULL;
1615 else {
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);
1648 g_free(utf8buf);
1650 do_quote:
1651 if ( textview->prev_quote_level != real_quotelevel ) {
1652 ClickableText *uri;
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;
1673 } else {
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;
1680 } else {
1681 g_print("oops (%d %d)\n",
1682 real_quotelevel, textview->prev_quote_level);
1684 if (lasturi) {
1685 if (lasturi->is_quote == FALSE) {
1686 textview->prev_quote_level = -1;
1687 goto do_quote;
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;
1697 } else {
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)
1706 GtkTextView *text;
1707 GtkTextBuffer *buffer;
1708 GtkTextIter iter;
1709 gchar buf[BUFFSIZE];
1710 gchar *bufp;
1711 ClickableText *r_uri;
1713 if (!str || *str == '\0')
1714 return;
1715 if (!uri)
1716 return;
1718 while (uri && *uri && g_ascii_isspace(*uri))
1719 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);
1725 if (!conv)
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)
1731 return;
1733 strcrchomp(buf);
1735 gtk_text_buffer_get_end_iter(buffer, &iter);
1736 for (bufp = buf; *bufp != '\0'; bufp = g_utf8_next_char(bufp)) {
1737 gunichar ch;
1739 ch = g_utf8_get_char(bufp);
1740 if (!g_unichar_isspace(ch))
1741 break;
1743 if (bufp > buf)
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);
1789 } else {
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;
1807 g_free(textview);
1810 #define CHANGE_TAG_FONT(tagname, font) { \
1811 tag = gtk_text_tag_table_lookup(tags, tagname); \
1812 if (tag) \
1813 g_object_set(G_OBJECT(tag), "font-desc", font, NULL); \
1816 void textview_set_font(TextView *textview, const gchar *codeset)
1818 GtkTextTag *tag;
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
1824 (NORMAL_FONT);
1825 if (font_desc) {
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
1833 (NORMAL_FONT);
1834 if (bold_font_desc)
1835 pango_font_description_set_weight
1836 (bold_font_desc, PANGO_WEIGHT_BOLD);
1837 } else {
1838 bold_font_desc = pango_font_description_from_string
1839 (BOLD_FONT);
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);
1851 if (font_desc) {
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)
1864 GtkTextView *view;
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));
1877 enum
1879 H_DATE = 0,
1880 H_FROM = 1,
1881 H_TO = 2,
1882 H_NEWSGROUPS = 3,
1883 H_SUBJECT = 4,
1884 H_CC = 5,
1885 H_REPLY_TO = 6,
1886 H_FOLLOWUP_TO = 7,
1887 H_X_MAILER = 8,
1888 H_X_NEWSREADER = 9,
1889 H_USER_AGENT = 10,
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;
1905 Header *header;
1906 gint i;
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);
1917 else
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;
1927 return NULL;
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,
1943 dp->name)) {
1944 if (dp->hidden)
1945 procheader_header_free(header);
1946 else
1947 g_ptr_array_add(sorted_headers, header);
1949 g_ptr_array_remove_index(headers, i);
1950 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);
1960 } else {
1961 procheader_header_free(header);
1964 g_ptr_array_free(headers, TRUE);
1965 } else
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;
1977 int x = 0;
1978 AvatarRender *avatarr;
1980 if (prefs_common.display_header_pane || !prefs_common.display_xface)
1981 goto bail;
1983 avatarr = avatars_avatarrender_new(msginfo);
1984 hooks_invoke(AVATAR_IMAGE_RENDER_HOOKLIST, avatarr);
1986 if (!avatarr->image) {
1987 avatars_avatarrender_free(avatarr);
1988 goto bail;
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);
2009 return;
2010 bail:
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);
2021 int x = 0;
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);
2040 return;
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)
2052 return;
2054 if (textview->avatar_type > AVATAR_FACE)
2055 return;
2057 if (textview->image)
2058 picture = gtk_image_get_pixbuf(GTK_IMAGE(textview->image));
2060 filename = addrindex_get_picture_file(msginfo->from);
2061 if (!filename)
2062 return;
2063 if (!is_file_exist(filename)) {
2064 gdk_pixbuf_save(picture, filename, "png", &error, NULL);
2065 if (error) {
2066 g_warning("Failed to save image: %s",
2067 error->message);
2068 g_error_free(error);
2071 g_free(filename);
2072 #else
2073 /* new address book */
2074 #endif
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);
2082 int x = 0;
2083 gchar *filename = NULL;
2084 GError *error = NULL;
2085 GdkPixbuf *picture = NULL;
2086 gint w, h;
2087 GtkAllocation allocation;
2089 if (prefs_common.display_header_pane
2090 || !prefs_common.display_xface)
2091 goto bail;
2093 if (msginfo->extradata && msginfo->extradata->avatars)
2094 return;
2096 if (textview->image)
2097 gtk_widget_destroy(textview->image);
2099 filename = addrindex_get_picture_file(msginfo->from);
2101 if (!filename)
2102 goto bail;
2103 if (!is_file_exist(filename)) {
2104 g_free(filename);
2105 goto bail;
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);
2113 else
2114 picture = gdk_pixbuf_new_from_file(filename, &error);
2116 if (error) {
2117 debug_print("Failed to import image: %s\n",
2118 error->message);
2119 g_error_free(error);
2120 goto bail;
2122 g_free(filename);
2124 if (picture) {
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);
2140 return;
2141 bail:
2142 if (textview->image)
2143 gtk_widget_destroy(textview->image);
2144 textview->image = NULL;
2145 textview->avatar_type = 0;
2146 #else
2147 /* new address book */
2148 #endif
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);
2158 if (tag_a == NULL)
2159 return tag_b == NULL ? 0:1;
2161 if (tag_b == NULL)
2162 return 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);
2173 GtkTextIter iter;
2174 ClickableText *uri;
2175 GSList *cur, *orig;
2176 gboolean found_tag = FALSE;
2178 if (!msginfo->tags)
2179 return;
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) {
2185 found_tag = TRUE;
2186 break;
2189 if (!found_tag) {
2190 g_slist_free(orig);
2191 return;
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));
2201 if (!cur_tag)
2202 continue;
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,
2207 cur_tag, -1,
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);
2211 uri->data = NULL;
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);
2217 else
2218 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, " ", 1,
2219 "header", "tags", NULL);
2221 g_slist_free(orig);
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);
2231 GtkTextIter iter;
2232 Header *header;
2233 gint i;
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);
2250 g_free(hdr);
2251 } else {
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) {
2269 gchar hbody[80];
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,
2277 "X-Newsreader")) &&
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);
2284 } else {
2285 gboolean hdr =
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,
2294 hdr);
2296 gtk_text_buffer_get_end_iter (buffer, &iter);
2297 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, "\n", 1,
2298 "header", NULL);
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,
2308 gboolean case_sens)
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,
2316 gboolean case_sens)
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));
2342 GtkTextIter iter;
2344 if (up) {
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);
2349 } else {
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), \
2358 "key_press_event");
2360 static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
2361 TextView *textview)
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) {
2372 case GDK_KEY_Tab:
2373 case GDK_KEY_Left:
2374 case GDK_KEY_Up:
2375 case GDK_KEY_Right:
2376 case GDK_KEY_Down:
2377 case GDK_KEY_Page_Up:
2378 case GDK_KEY_Page_Down:
2379 case GDK_KEY_Control_L:
2380 case GDK_KEY_Control_R:
2381 return FALSE;
2382 case GDK_KEY_Home:
2383 case GDK_KEY_End:
2384 textview_scroll_max(textview,(event->keyval == GDK_KEY_Home));
2385 return TRUE;
2386 case GDK_KEY_space:
2387 if (summaryview)
2388 summary_pass_key_press_event(summaryview, event);
2389 else
2390 mimeview_scroll_page
2391 (messageview->mimeview,
2392 (event->state &
2393 (GDK_SHIFT_MASK|GDK_MOD1_MASK)) != 0);
2394 break;
2395 case GDK_KEY_BackSpace:
2396 mimeview_scroll_page(messageview->mimeview, TRUE);
2397 break;
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);
2403 break;
2404 case GDK_KEY_Delete:
2405 if (summaryview)
2406 summary_pass_key_press_event(summaryview, event);
2407 break;
2408 default:
2409 if (messageview->mainwin) {
2410 window = gtk_widget_get_window(messageview->mainwin->window);
2411 if (summaryview &&
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);
2421 break;
2424 return TRUE;
2427 static gboolean textview_motion_notify(GtkWidget *widget,
2428 GdkEventMotion *event,
2429 TextView *textview)
2431 if (textview->loading)
2432 return FALSE;
2433 textview_uri_update(textview, event->x, event->y);
2434 gdk_window_get_pointer(gtk_widget_get_window(widget), NULL, NULL, NULL);
2436 return FALSE;
2439 static gboolean textview_leave_notify(GtkWidget *widget,
2440 GdkEventCrossing *event,
2441 TextView *textview)
2443 if (textview->loading)
2444 return FALSE;
2445 textview_uri_update(textview, -1, -1);
2447 return FALSE;
2450 static gboolean textview_visibility_notify(GtkWidget *widget,
2451 GdkEventVisibility *event,
2452 TextView *textview)
2454 gint wx, wy;
2455 GdkWindow *window;
2457 if (textview->loading)
2458 return FALSE;
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)
2465 return FALSE;
2467 gdk_window_get_pointer(gtk_widget_get_window(widget), &wx, &wy, NULL);
2468 textview_uri_update(textview, wx, wy);
2470 return FALSE;
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) {
2498 gint bx, by;
2499 GtkTextIter iter;
2500 GSList *tags;
2501 GSList *cur;
2503 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(textview->text),
2504 GTK_TEXT_WINDOW_WIDGET,
2505 x, y, &bx, &by);
2506 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(textview->text),
2507 &iter, bx, by);
2509 tags = gtk_text_iter_get_tags(&iter);
2510 for (cur = tags; cur != NULL; cur = cur->next) {
2511 GtkTextTag *tag = cur->data;
2512 char *name;
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,
2521 &iter, tag,
2522 &start_iter,
2523 &end_iter);
2525 g_free(name);
2526 if (uri)
2527 break;
2529 g_slist_free(tags);
2532 if (uri != textview->uri_hover) {
2533 GdkWindow *window;
2535 if (textview->uri_hover)
2536 gtk_text_buffer_remove_tag_by_name(buffer,
2537 "link-hover",
2538 &textview->uri_hover_start_iter,
2539 &textview->uri_hover_end_iter);
2541 textview->uri_hover = uri;
2542 if (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);
2551 } else {
2552 textview_set_cursor(window, watch_cursor);
2555 TEXTVIEW_STATUSBAR_POP(textview);
2557 if (uri) {
2558 if (!uri->is_quote)
2559 gtk_text_buffer_apply_tag_by_name(buffer,
2560 "link-hover",
2561 &start_iter,
2562 &end_iter);
2563 TEXTVIEW_STATUSBAR_PUSH(textview, uri->uri);
2568 static gboolean textview_get_uri_range(TextView *textview,
2569 GtkTextIter *iter,
2570 GtkTextTag *tag,
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,
2578 GtkTextIter *iter,
2579 GtkTextTag *tag,
2580 GtkTextIter *start_iter,
2581 GtkTextIter *end_iter)
2583 gint start_pos, end_pos, cur_pos;
2584 ClickableText *uri = NULL;
2585 GSList *cur;
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) {
2595 uri = uri_;
2596 break;
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
2605 * of last link).
2606 * In that case, correct link is the one covering
2607 * current iter.
2609 if (uri_->start <= cur_pos && cur_pos <= uri_->end) {
2610 uri = uri_;
2611 break;
2616 return uri;
2619 static ClickableText *textview_get_uri(TextView *textview,
2620 GtkTextIter *iter,
2621 GtkTextTag *tag)
2623 GtkTextIter start_iter, end_iter;
2624 ClickableText *uri = NULL;
2626 if (textview_get_uri_range(textview, iter, tag, &start_iter,
2627 &end_iter))
2628 uri = textview_get_uri_from_range(textview, iter, tag,
2629 &start_iter, &end_iter);
2631 return uri;
2634 static void textview_shift_uris_after(TextView *textview, GSList *start_list, gint start, gint shift)
2636 GSList *cur;
2637 if (!start_list)
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)
2643 continue;
2644 uri->start += shift;
2645 uri->end += shift;
2649 static void textview_remove_uris_in(TextView *textview, gint start, gint end)
2651 GSList *cur;
2652 for (cur = textview->uri_list; cur; ) {
2653 ClickableText *uri = (ClickableText *)cur->data;
2654 if (uri->start > start && uri->end < end) {
2655 cur = cur->next;
2656 textview->uri_list = g_slist_remove(textview->uri_list, uri);
2657 g_free(uri->uri);
2658 g_free(uri->filename);
2659 if (uri->is_quote) {
2660 g_free(uri->fg_color);
2661 g_free(uri->data);
2662 /* (only free data in quotes uris) */
2664 g_free(uri);
2665 } else {
2666 cur = cur->next;
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));
2677 if (!uri->is_quote)
2678 return;
2680 if (uri->q_expanded && expand_only)
2681 return;
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,
2687 "link-hover",
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,
2692 "qlink",
2693 &start,
2694 &end);
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;
2714 } else {
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);
2730 } else {
2731 textview_cursor_wait(textview);
2734 static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
2735 GdkEvent *event, GtkTextIter *iter,
2736 TextView *textview)
2738 GdkEventButton *bevent;
2739 ClickableText *uri = NULL;
2740 char *tagname;
2741 gboolean qlink = FALSE;
2743 if (!event)
2744 return FALSE;
2746 if (event->type != GDK_BUTTON_PRESS && event->type != GDK_2BUTTON_PRESS
2747 && event->type != GDK_MOTION_NOTIFY)
2748 return FALSE;
2750 uri = textview_get_uri(textview, iter, tag);
2751 if (!uri)
2752 return FALSE;
2754 g_object_get(G_OBJECT(tag), "name", &tagname, NULL);
2756 if (!strcmp(tagname, "qlink"))
2757 qlink = TRUE;
2759 g_free(tagname);
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 */
2766 return FALSE;
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);
2782 return TRUE;
2783 } else if (qlink && bevent->button == 1) {
2784 if (prefs_common.hide_quoted) {
2785 textview_toggle_quote(textview, NULL, uri, FALSE);
2786 return TRUE;
2787 } else
2788 return FALSE;
2789 } else if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
2790 if (bevent->button == 3) {
2791 g_object_set_data(
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);
2797 } else {
2798 PrefsAccount *account = NULL;
2799 FolderItem *folder_item = NULL;
2800 Compose *compose;
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);
2809 if (!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);
2816 return TRUE;
2817 } else if (g_ascii_strncasecmp(uri->uri, "file:", 5)) {
2818 if (bevent->button == 1 &&
2819 textview_uri_security_check(textview, uri) == TRUE)
2820 open_uri(uri->uri,
2821 prefs_common_get_uri_cmd());
2822 else if (bevent->button == 3 && !qlink) {
2823 g_object_set_data(
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);
2830 return TRUE;
2831 } else {
2832 if (bevent->button == 3 && !qlink) {
2833 g_object_set_data(
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);
2839 return TRUE;
2844 return FALSE;
2847 gchar *textview_get_visible_uri (TextView *textview,
2848 ClickableText *uri)
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)
2874 gchar *visible_str;
2875 gboolean retval = TRUE;
2877 if (is_uri_string(uri->uri) == FALSE)
2878 return TRUE;
2880 visible_str = textview_get_visible_uri(textview, uri);
2881 if (visible_str == NULL)
2882 return TRUE;
2884 g_strstrip(visible_str);
2886 if (strcmp(visible_str, uri->uri) != 0 && is_uri_string(visible_str)) {
2887 gchar *uri_path;
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)
2893 retval = FALSE;
2896 if (retval == FALSE) {
2897 gchar *msg;
2898 AlertValue aval;
2900 msg = g_markup_printf_escaped("%s\n\n"
2901 "<b>%s</b> %s\n\n"
2902 "<b>%s</b> %s\n\n"
2903 "%s",
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);
2911 g_free(msg);
2912 if (aval == G_ALERTALTERNATE)
2913 retval = TRUE;
2916 g_free(visible_str);
2918 return retval;
2921 static void textview_uri_list_remove_all(GSList *uri_list)
2923 GSList *cur;
2925 for (cur = uri_list; cur != NULL; cur = cur->next) {
2926 if (cur->data) {
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) */
2934 g_free(cur->data);
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),
2944 "menu_button");
2945 const gchar *raw_url = g_object_get_data(G_OBJECT(textview->link_popup_menu),
2946 "raw_url");
2948 if (uri) {
2949 if (textview_uri_security_check(textview, uri) == TRUE)
2950 open_uri(uri->uri,
2951 prefs_common_get_uri_cmd());
2952 g_object_set_data(G_OBJECT(textview->link_popup_menu), "menu_button",
2953 NULL);
2955 if (raw_url) {
2956 open_uri(raw_url, prefs_common_get_uri_cmd());
2957 g_object_set_data(G_OBJECT(textview->link_popup_menu), "raw_url",
2958 NULL);
2962 static void open_image_cb (GtkAction *action, TextView *textview)
2964 ClickableText *uri = g_object_get_data(G_OBJECT(textview->file_popup_menu),
2965 "menu_button");
2967 gchar *cmd = NULL;
2968 gchar buf[1024];
2969 const gchar *p;
2970 gchar *filename = NULL, *filepath = NULL;
2971 gchar *tmp_filename = NULL;
2973 if (uri == NULL)
2974 return;
2976 if (uri->filename == NULL)
2977 return;
2979 filename = g_strdup(uri->filename);
2981 if (!g_utf8_validate(filename, -1, NULL)) {
2982 gchar *tmp = conv_filename_to_utf8(filename);
2983 g_free(filename);
2984 filename = tmp;
2987 subst_for_filename(filename);
2989 filepath = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
2990 filename, NULL);
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);
2997 if (cmd == NULL) {
2998 gboolean remember = FALSE;
2999 cmd = input_dialog_combo_remember
3000 (_("Open with"),
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,
3005 &remember);
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);
3013 else {
3014 g_warning("Image viewer command-line is invalid: '%s'", cmd);
3015 g_free(filepath);
3016 g_free(filename);
3017 return;
3020 execute_command_line(buf, TRUE, NULL);
3022 g_free(filepath);
3023 g_free(filename);
3024 g_free(cmd);
3026 g_object_set_data(G_OBJECT(textview->file_popup_menu), "menu_button",
3027 NULL);
3030 static void save_file_cb (GtkAction *action, TextView *textview)
3032 ClickableText *uri = g_object_get_data(G_OBJECT(textview->file_popup_menu),
3033 "menu_button");
3034 gchar *filename = NULL;
3035 gchar *filepath = NULL;
3036 gchar *filedir = NULL;
3037 gchar *tmp_filename = NULL;
3038 GtkWidget *window;
3040 if (uri == NULL)
3041 return;
3043 if (uri->filename == NULL)
3044 return;
3046 filename = g_strdup(uri->filename);
3048 if (!g_utf8_validate(filename, -1, NULL)) {
3049 gchar *tmp = conv_filename_to_utf8(filename);
3050 g_free(filename);
3051 filename = tmp;
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);
3059 else
3060 filepath = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
3061 filename, NULL);
3063 g_free(filename);
3065 /* Pick correct window to set the file dialog "transient for" */
3066 if (textview->messageview->window != NULL)
3067 window = textview->messageview->window;
3068 else
3069 window = textview->messageview->mainwin->window;
3071 manage_window_focus_in(window, NULL, NULL);
3073 filename = filesel_select_file_save(_("Save as"), filepath);
3074 if (!filename) {
3075 g_free(filepath);
3076 return;
3079 if (is_file_exist(filename)) {
3080 AlertValue aval;
3081 gchar *res;
3083 res = g_strdup_printf(_("Overwrite existing file '%s'?"),
3084 filename);
3085 aval = alertpanel(_("Overwrite"), res, GTK_STOCK_CANCEL,
3086 GTK_STOCK_OK, NULL);
3087 g_free(res);
3088 if (G_ALERTALTERNATE != aval)
3089 return;
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);
3102 g_free(filedir);
3103 g_free(filepath);
3105 g_object_set_data(G_OBJECT(textview->file_popup_menu), "menu_button",
3106 NULL);
3109 static void copy_uri_cb (GtkAction *action, TextView *textview)
3111 ClickableText *uri = g_object_get_data(G_OBJECT(textview->link_popup_menu),
3112 "menu_button");
3113 const gchar *raw_url = g_object_get_data(G_OBJECT(textview->link_popup_menu),
3114 "raw_url");
3115 if (uri) {
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",
3119 NULL);
3121 if (raw_url) {
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",
3125 NULL);
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),
3133 "menu_button");
3134 AvatarRender *avatarr = NULL;
3135 GdkPixbuf *picture = NULL;
3136 gboolean use_picture = FALSE;
3138 if (uri == NULL)
3139 return;
3141 /* extract url */
3142 fromaddress = g_strdup(uri->uri + 7);
3144 if (textview->messageview->msginfo &&
3145 !strcmp2(fromaddress, textview->messageview->msginfo->from))
3146 use_picture = TRUE;
3148 fromname = procheader_get_fromname(fromaddress);
3149 extract_address(fromaddress);
3151 if (use_picture) {
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));
3159 if (avatarr) {
3160 avatars_avatarrender_free(avatarr);
3163 #ifndef USE_ALT_ADDRBOOK
3164 addressbook_add_contact( fromname, fromaddress, NULL, picture);
3165 #else
3166 if (addressadd_selection(fromname, fromaddress, NULL, picture)) {
3167 debug_print( "addressbook_add_contact - added\n" );
3169 #endif
3171 g_free(fromaddress);
3172 g_free(fromname);
3175 static void reply_to_uri_cb (GtkAction *action, TextView *textview)
3177 ClickableText *uri = g_object_get_data(G_OBJECT(textview->mail_popup_menu),
3178 "menu_button");
3179 if (!textview->messageview || !uri)
3180 return;
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;
3189 Compose *compose;
3190 ClickableText *uri = g_object_get_data(G_OBJECT(textview->mail_popup_menu),
3191 "menu_button");
3192 if (uri == NULL)
3193 return;
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);
3204 } else {
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),
3213 "menu_button");
3214 if (uri == NULL)
3215 return;
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",
3220 NULL);
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)) {
3229 if (sel_start)
3230 *sel_start = gtk_text_iter_get_offset(&start);
3231 if (sel_end)
3232 *sel_end = gtk_text_iter_get_offset(&end);
3233 } else {
3234 if (sel_start)
3235 *sel_start = -1;
3236 if (sel_end)
3237 *sel_end = -1;