Fix CID 1596595: Resource leak
[claws.git] / src / textview.c
blob0e7b3ae1845252caf89d87f128413276c75f6a1e
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2023 the Claws Mail team and Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 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"
72 #include "file-utils.h"
74 static GdkRGBA quote_colors[3] = {
75 {0, 0, 0, 1},
76 {0, 0, 0, 1},
77 {0, 0, 0, 1}
80 static GdkRGBA quote_bgcolors[3] = {
81 {0, 0, 0, 1},
82 {0, 0, 0, 1},
83 {0, 0, 0, 1}
85 static GdkRGBA signature_color = {
86 0.5, 0.5, 0.5, 1
89 static GdkRGBA uri_color = {
90 0, 0, 0, 1
93 static GdkRGBA emphasis_color = {
94 0, 0, 0, 1
97 static GdkRGBA diff_added_color = {
98 0, 0, 0, 1
101 static GdkRGBA diff_deleted_color = {
102 0, 0, 0, 1
105 static GdkRGBA diff_hunk_color = {
106 0, 0, 0, 1
109 static GdkRGBA tags_bgcolor = {
110 0, 0, 0, 1
113 static GdkRGBA tags_color = {
114 0, 0, 0, 1
117 static GdkCursor *hand_cursor = NULL;
118 static GdkCursor *text_cursor = NULL;
119 static GdkCursor *watch_cursor= NULL;
121 #define TEXTVIEW_FONT_SIZE_STEP 15 /* pango font zoom level change granularity in % */
122 #define TEXTVIEW_FONT_SIZE_MIN -75 /* this gives 5 zoom out steps at 15% */
123 #define TEXTVIEW_FONT_SIZE_MAX 3100 /* this gives 200 zoom in steps at 15% */
124 #define TEXTVIEW_FONT_SIZE_UNSET -666 /* default value when unset (must be lower than TEXTVIEW_FONT_SIZE_MIN */
126 /* font size in session (will apply to next message views we open */
127 /* must be lower than TEXTVIEW_FONT_SIZE_MIN */
128 static gint textview_font_size_percent = TEXTVIEW_FONT_SIZE_UNSET;
129 static gint textview_font_size_default = TEXTVIEW_FONT_SIZE_UNSET;
131 static void textview_set_font_zoom(TextView *textview);
133 #define TEXTVIEW_STATUSBAR_PUSH(textview, str) \
134 { if (textview->messageview->statusbar) \
135 gtk_statusbar_push(GTK_STATUSBAR(textview->messageview->statusbar), \
136 textview->messageview->statusbar_cid, str); \
139 #define TEXTVIEW_STATUSBAR_POP(textview) \
140 { if (textview->messageview->statusbar) \
141 gtk_statusbar_pop(GTK_STATUSBAR(textview->messageview->statusbar), \
142 textview->messageview->statusbar_cid); \
145 static void textview_show_ertf (TextView *textview,
146 FILE *fp,
147 CodeConverter *conv);
148 static void textview_add_part (TextView *textview,
149 MimeInfo *mimeinfo);
150 static void textview_add_parts (TextView *textview,
151 MimeInfo *mimeinfo);
152 static void textview_write_body (TextView *textview,
153 MimeInfo *mimeinfo);
154 static void textview_show_html (TextView *textview,
155 FILE *fp,
156 CodeConverter *conv);
158 static void textview_write_line (TextView *textview,
159 const gchar *str,
160 CodeConverter *conv,
161 gboolean do_quote_folding);
162 static void textview_write_link (TextView *textview,
163 const gchar *str,
164 const gchar *uri,
165 CodeConverter *conv);
167 static GPtrArray *textview_scan_header (TextView *textview,
168 FILE *fp);
169 static void textview_show_header (TextView *textview,
170 GPtrArray *headers);
172 static void textview_zoom(GtkWidget *widget, gboolean zoom_in);
173 static void textview_zoom_in(GtkWidget *widget, gpointer data);
174 static void textview_zoom_out(GtkWidget *widget, gpointer data);
175 static void textview_zoom_reset(GtkWidget *widget, gpointer data);
177 static gint textview_key_pressed (GtkWidget *widget,
178 GdkEventKey *event,
179 TextView *textview);
180 static gboolean textview_scrolled(GtkWidget *widget,
181 GdkEvent *_event,
182 gpointer user_data);
183 static void textview_populate_popup(GtkTextView *self,
184 GtkMenu *menu,
185 gpointer user_data);
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 textview_show_tags(TextView *textview);
238 static GtkActionEntry textview_link_popup_entries[] =
240 {"TextviewPopupLink", NULL, "TextviewPopupLink", NULL, NULL, NULL },
241 {"TextviewPopupLink/Open", NULL, N_("_Open in web browser"), NULL, NULL, G_CALLBACK(open_uri_cb) },
242 {"TextviewPopupLink/Copy", NULL, N_("Copy this _link"), NULL, NULL, G_CALLBACK(copy_uri_cb) },
245 static GtkActionEntry textview_mail_popup_entries[] =
247 {"TextviewPopupMail", NULL, "TextviewPopupMail", NULL, NULL, NULL },
248 {"TextviewPopupMail/Compose", NULL, N_("Compose _new message"), NULL, NULL, G_CALLBACK(mail_to_uri_cb) },
249 {"TextviewPopupMail/ReplyTo", NULL, N_("_Reply to this address"), NULL, NULL, G_CALLBACK(reply_to_uri_cb) },
250 {"TextviewPopupMail/AddAB", NULL, N_("Add to _Address book"), NULL, NULL, G_CALLBACK(add_uri_to_addrbook_cb) },
251 {"TextviewPopupMail/Copy", NULL, N_("Copy this add_ress"), NULL, NULL, G_CALLBACK(copy_mail_to_uri_cb) },
254 static gboolean move_textview_image_cb (gpointer data)
256 #ifndef WIDTH
257 # define WIDTH 48
258 # define HEIGHT 48
259 #endif
260 TextView *textview = (TextView *)data;
261 GtkAllocation allocation;
262 gint x, wx, wy;
263 gtk_widget_get_allocation(textview->text, &allocation);
264 x = allocation.width - WIDTH - 5;
265 gtk_text_view_buffer_to_window_coords(
266 GTK_TEXT_VIEW(textview->text),
267 GTK_TEXT_WINDOW_TEXT, x, 5, &wx, &wy);
268 gtk_text_view_move_child(GTK_TEXT_VIEW(textview->text),
269 textview->image, wx, wy);
271 return G_SOURCE_REMOVE;
274 static void scrolled_cb (GtkAdjustment *adj, gpointer data)
276 TextView *textview = (TextView *)data;
278 if (textview->image) {
279 move_textview_image_cb(textview);
283 static void textview_size_allocate_cb (GtkWidget *widget,
284 GtkAllocation *allocation,
285 gpointer data)
287 TextView *textview = (TextView *)data;
289 if (textview->image) {
290 g_timeout_add(0, &move_textview_image_cb, textview);
294 TextView *textview_create(void)
296 TextView *textview;
297 GtkWidget *vbox;
298 GtkWidget *scrolledwin;
299 GtkWidget *text;
300 GtkTextBuffer *buffer;
301 GtkClipboard *clipboard;
302 GtkAdjustment *adj;
304 debug_print("Creating text view...\n");
305 textview = g_new0(TextView, 1);
307 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
308 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
309 GTK_POLICY_AUTOMATIC,
310 GTK_POLICY_AUTOMATIC);
311 gtk_widget_set_vexpand(GTK_WIDGET(scrolledwin), TRUE);
312 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
313 GTK_SHADOW_IN);
315 /* create GtkSText widgets for single-byte and multi-byte character */
316 text = gtk_text_view_new();
317 gtk_widget_add_events(text, GDK_LEAVE_NOTIFY_MASK);
318 gtk_widget_show(text);
319 gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
320 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD_CHAR);
321 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(text), FALSE);
322 gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
323 gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
325 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
326 clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
327 gtk_text_buffer_add_selection_clipboard(buffer, clipboard);
329 gtk_widget_ensure_style(text);
331 g_object_ref(scrolledwin);
333 gtk_container_add(GTK_CONTAINER(scrolledwin), text);
335 g_signal_connect(G_OBJECT(text), "key-press-event",
336 G_CALLBACK(textview_key_pressed), textview);
337 g_signal_connect(G_OBJECT(text), "motion-notify-event",
338 G_CALLBACK(textview_motion_notify), textview);
339 g_signal_connect(G_OBJECT(text), "leave-notify-event",
340 G_CALLBACK(textview_leave_notify), textview);
341 g_signal_connect(G_OBJECT(text), "visibility-notify-event",
342 G_CALLBACK(textview_visibility_notify), textview);
343 adj = gtk_scrolled_window_get_vadjustment(
344 GTK_SCROLLED_WINDOW(scrolledwin));
345 g_signal_connect(G_OBJECT(adj), "value-changed",
346 G_CALLBACK(scrolled_cb), textview);
347 g_signal_connect(G_OBJECT(text), "size_allocate",
348 G_CALLBACK(textview_size_allocate_cb),
349 textview);
350 g_signal_connect(G_OBJECT(text), "scroll-event",
351 G_CALLBACK(textview_scrolled), textview);
352 g_signal_connect(G_OBJECT(text), "populate-popup",
353 G_CALLBACK(textview_populate_popup), textview);
356 gtk_widget_show(scrolledwin);
358 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
359 gtk_widget_set_name(GTK_WIDGET(vbox), "textview");
360 gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
362 gtk_widget_show(vbox);
365 textview->ui_manager = gtk_ui_manager_new();
366 textview->link_action_group = cm_menu_create_action_group_full(textview->ui_manager,
367 "TextviewPopupLink",
368 textview_link_popup_entries,
369 G_N_ELEMENTS(textview_link_popup_entries), (gpointer)textview);
370 textview->mail_action_group = cm_menu_create_action_group_full(textview->ui_manager,
371 "TextviewPopupMail",
372 textview_mail_popup_entries,
373 G_N_ELEMENTS(textview_mail_popup_entries), (gpointer)textview);
375 MENUITEM_ADDUI_MANAGER(textview->ui_manager, "/", "Menus", "Menus", GTK_UI_MANAGER_MENUBAR)
376 MENUITEM_ADDUI_MANAGER(textview->ui_manager,
377 "/Menus", "TextviewPopupLink", "TextviewPopupLink", GTK_UI_MANAGER_MENU)
378 MENUITEM_ADDUI_MANAGER(textview->ui_manager,
379 "/Menus", "TextviewPopupMail", "TextviewPopupMail", GTK_UI_MANAGER_MENU)
381 MENUITEM_ADDUI_MANAGER(textview->ui_manager,
382 "/Menus/TextviewPopupLink", "Open", "TextviewPopupLink/Open", GTK_UI_MANAGER_MENUITEM)
383 MENUITEM_ADDUI_MANAGER(textview->ui_manager,
384 "/Menus/TextviewPopupLink", "Copy", "TextviewPopupLink/Copy", GTK_UI_MANAGER_MENUITEM)
385 MENUITEM_ADDUI_MANAGER(textview->ui_manager,
386 "/Menus/TextviewPopupMail", "Compose", "TextviewPopupMail/Compose", GTK_UI_MANAGER_MENUITEM)
387 MENUITEM_ADDUI_MANAGER(textview->ui_manager,
388 "/Menus/TextviewPopupMail", "ReplyTo", "TextviewPopupMail/ReplyTo", GTK_UI_MANAGER_MENUITEM)
389 MENUITEM_ADDUI_MANAGER(textview->ui_manager,
390 "/Menus/TextviewPopupMail", "AddAB", "TextviewPopupMail/AddAB", GTK_UI_MANAGER_MENUITEM)
391 MENUITEM_ADDUI_MANAGER(textview->ui_manager,
392 "/Menus/TextviewPopupMail", "Copy", "TextviewPopupMail/Copy", GTK_UI_MANAGER_MENUITEM)
394 textview->link_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
395 gtk_ui_manager_get_widget(textview->ui_manager, "/Menus/TextviewPopupLink")) );
396 textview->mail_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
397 gtk_ui_manager_get_widget(textview->ui_manager, "/Menus/TextviewPopupMail")) );
399 textview->vbox = vbox;
400 textview->scrolledwin = scrolledwin;
401 textview->text = text;
402 textview->uri_list = NULL;
403 textview->body_pos = 0;
404 textview->last_buttonpress = GDK_NOTHING;
405 textview->image = NULL;
406 return textview;
409 static void textview_create_tags(GtkTextView *text, TextView *textview)
411 GtkTextBuffer *buffer;
412 GtkTextTag *tag, *qtag;
413 static PangoFontDescription *font_desc, *bold_font_desc;
415 if (!font_desc)
416 font_desc = pango_font_description_from_string
417 (NORMAL_FONT);
419 if (!bold_font_desc) {
420 if (prefs_common.derive_from_normal_font || !BOLD_FONT) {
421 bold_font_desc = pango_font_description_from_string
422 (NORMAL_FONT);
423 pango_font_description_set_weight
424 (bold_font_desc, PANGO_WEIGHT_BOLD);
425 } else {
426 bold_font_desc = pango_font_description_from_string
427 (BOLD_FONT);
431 buffer = gtk_text_view_get_buffer(text);
433 gtk_text_buffer_create_tag(buffer, "header",
434 "pixels-above-lines", 0,
435 "pixels-above-lines-set", TRUE,
436 "pixels-below-lines", 0,
437 "pixels-below-lines-set", TRUE,
438 "font-desc", font_desc,
439 "left-margin", 3,
440 "left-margin-set", TRUE,
441 NULL);
442 gtk_text_buffer_create_tag(buffer, "header_title",
443 "font-desc", bold_font_desc,
444 NULL);
445 tag = gtk_text_buffer_create_tag(buffer, "hlink",
446 "pixels-above-lines", 0,
447 "pixels-above-lines-set", TRUE,
448 "pixels-below-lines", 0,
449 "pixels-below-lines-set", TRUE,
450 "font-desc", font_desc,
451 "left-margin", 3,
452 "left-margin-set", TRUE,
453 "foreground-rgba", &uri_color,
454 NULL);
455 g_signal_connect(G_OBJECT(tag), "event",
456 G_CALLBACK(textview_uri_button_pressed), textview);
457 if (prefs_common.enable_bgcolor) {
458 gtk_text_buffer_create_tag(buffer, "quote0",
459 "foreground-rgba", &quote_colors[0],
460 "paragraph-background-rgba", &quote_bgcolors[0],
461 NULL);
462 gtk_text_buffer_create_tag(buffer, "quote1",
463 "foreground-rgba", &quote_colors[1],
464 "paragraph-background-rgba", &quote_bgcolors[1],
465 NULL);
466 gtk_text_buffer_create_tag(buffer, "quote2",
467 "foreground-rgba", &quote_colors[2],
468 "paragraph-background-rgba", &quote_bgcolors[2],
469 NULL);
470 } else {
471 gtk_text_buffer_create_tag(buffer, "quote0",
472 "foreground-rgba", &quote_colors[0],
473 NULL);
474 gtk_text_buffer_create_tag(buffer, "quote1",
475 "foreground-rgba", &quote_colors[1],
476 NULL);
477 gtk_text_buffer_create_tag(buffer, "quote2",
478 "foreground-rgba", &quote_colors[2],
479 NULL);
481 gtk_text_buffer_create_tag(buffer, "tags",
482 "foreground-rgba", &tags_color,
483 "paragraph-background-rgba", &tags_bgcolor,
484 NULL);
485 gtk_text_buffer_create_tag(buffer, "emphasis",
486 "foreground-rgba", &emphasis_color,
487 NULL);
488 gtk_text_buffer_create_tag(buffer, "signature",
489 "foreground-rgba", &signature_color,
490 NULL);
491 tag = gtk_text_buffer_create_tag(buffer, "link",
492 "foreground-rgba", &uri_color,
493 NULL);
494 qtag = gtk_text_buffer_create_tag(buffer, "qlink",
495 NULL);
496 gtk_text_buffer_create_tag(buffer, "link-hover",
497 "underline", PANGO_UNDERLINE_SINGLE,
498 NULL);
499 gtk_text_buffer_create_tag(buffer, "diff-add",
500 "foreground-rgba", &diff_added_color,
501 NULL);
502 gtk_text_buffer_create_tag(buffer, "diff-del",
503 "foreground-rgba", &diff_deleted_color,
504 NULL);
505 gtk_text_buffer_create_tag(buffer, "diff-add-file",
506 "foreground-rgba", &diff_added_color,
507 "weight", PANGO_WEIGHT_BOLD,
508 NULL);
509 gtk_text_buffer_create_tag(buffer, "diff-del-file",
510 "foreground-rgba", &diff_deleted_color,
511 "weight", PANGO_WEIGHT_BOLD,
512 NULL);
513 gtk_text_buffer_create_tag(buffer, "diff-hunk",
514 "foreground-rgba", &diff_hunk_color,
515 "weight", PANGO_WEIGHT_BOLD,
516 NULL);
517 g_signal_connect(G_OBJECT(qtag), "event",
518 G_CALLBACK(textview_uri_button_pressed), textview);
519 g_signal_connect(G_OBJECT(tag), "event",
520 G_CALLBACK(textview_uri_button_pressed), textview);
521 /* if (font_desc)
522 pango_font_description_free(font_desc);
523 if (bold_font_desc)
524 pango_font_description_free(bold_font_desc);*/
527 void textview_init(TextView *textview)
529 if (!hand_cursor)
530 hand_cursor = gdk_cursor_new_for_display(
531 gtk_widget_get_display(textview->vbox), GDK_HAND2);
532 if (!text_cursor)
533 text_cursor = gdk_cursor_new_for_display(
534 gtk_widget_get_display(textview->vbox), GDK_XTERM);
535 if (!watch_cursor)
536 watch_cursor = gdk_cursor_new_for_display(
537 gtk_widget_get_display(textview->vbox), GDK_WATCH);
539 textview_reflect_prefs(textview);
540 textview_create_tags(GTK_TEXT_VIEW(textview->text), textview);
543 #define CHANGE_TAG_COLOR(tagname, colorfg, colorbg) { \
544 tag = gtk_text_tag_table_lookup(tags, tagname); \
545 if (tag) \
546 g_object_set(G_OBJECT(tag), "foreground-rgba", colorfg, "paragraph-background-rgba", colorbg, NULL); \
549 static void textview_update_message_colors(TextView *textview)
551 GdkRGBA black = {0, 0, 0, 1};
552 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
554 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
555 GtkTextTag *tag = NULL;
557 quote_bgcolors[0] = quote_bgcolors[1] = quote_bgcolors[2] = black;
558 quote_colors[0] = quote_colors[1] = quote_colors[2] = black;
559 uri_color = emphasis_color = signature_color = diff_added_color =
560 diff_deleted_color = diff_hunk_color = black;
561 tags_bgcolor = tags_color = black;
563 if (prefs_common.enable_color) {
564 /* grab the quote colors, converting from an int to a GdkColor */
565 quote_colors[0] = prefs_common.color[COL_QUOTE_LEVEL1];
566 quote_colors[1] = prefs_common.color[COL_QUOTE_LEVEL2];
567 quote_colors[2] = prefs_common.color[COL_QUOTE_LEVEL3];
568 uri_color = prefs_common.color[COL_URI];
569 signature_color = prefs_common.color[COL_SIGNATURE];
570 emphasis_color = prefs_common.color[COL_EMPHASIS];
571 diff_added_color = prefs_common.color[COL_DIFF_ADDED];
572 diff_deleted_color = prefs_common.color[COL_DIFF_DELETED];
573 diff_hunk_color = prefs_common.color[COL_DIFF_HUNK];
575 if (prefs_common.enable_color && prefs_common.enable_bgcolor) {
576 quote_bgcolors[0] = prefs_common.color[COL_QUOTE_LEVEL1_BG];
577 quote_bgcolors[1] = prefs_common.color[COL_QUOTE_LEVEL2_BG];
578 quote_bgcolors[2] = prefs_common.color[COL_QUOTE_LEVEL3_BG];
579 CHANGE_TAG_COLOR("quote0", &quote_colors[0], &quote_bgcolors[0]);
580 CHANGE_TAG_COLOR("quote1", &quote_colors[1], &quote_bgcolors[1]);
581 CHANGE_TAG_COLOR("quote2", &quote_colors[2], &quote_bgcolors[2]);
582 } else {
583 CHANGE_TAG_COLOR("quote0", &quote_colors[0], NULL);
584 CHANGE_TAG_COLOR("quote1", &quote_colors[1], NULL);
585 CHANGE_TAG_COLOR("quote2", &quote_colors[2], NULL);
588 CHANGE_TAG_COLOR("emphasis", &emphasis_color, NULL);
589 CHANGE_TAG_COLOR("signature", &signature_color, NULL);
590 CHANGE_TAG_COLOR("link", &uri_color, NULL);
591 CHANGE_TAG_COLOR("link-hover", &uri_color, NULL);
592 CHANGE_TAG_COLOR("diff-add", &diff_added_color, NULL);
593 CHANGE_TAG_COLOR("diff-del", &diff_deleted_color, NULL);
594 CHANGE_TAG_COLOR("diff-add-file", &diff_added_color, NULL);
595 CHANGE_TAG_COLOR("diff-del-file", &diff_deleted_color, NULL);
596 CHANGE_TAG_COLOR("diff-hunk", &diff_hunk_color, NULL);
598 tags_bgcolor = prefs_common.color[COL_TAGS_BG];
599 tags_color = prefs_common.color[COL_TAGS];
601 #undef CHANGE_TAG_COLOR
603 void textview_reflect_prefs(TextView *textview)
605 textview_set_font(textview, NULL);
606 textview_update_message_colors(textview);
607 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview->text),
608 prefs_common.textview_cursor_visible);
611 void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
613 START_TIMING("");
614 cm_return_if_fail(mimeinfo != NULL);
615 cm_return_if_fail(fp != NULL);
617 textview->loading = TRUE;
618 textview->stop_loading = FALSE;
620 textview_clear(textview);
622 if (mimeinfo->type == MIMETYPE_MULTIPART ||
623 (mimeinfo->type == MIMETYPE_MESSAGE && !g_ascii_strcasecmp(mimeinfo->subtype, "rfc822"))) {
624 textview_add_parts(textview, mimeinfo);
625 } else {
626 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0)
627 perror("fseek");
629 textview_write_body(textview, mimeinfo);
632 textview->loading = FALSE;
633 textview->stop_loading = FALSE;
634 textview_set_position(textview, 0);
636 END_TIMING();
639 static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
641 GtkAllocation allocation;
642 GtkTextView *text;
643 GtkTextBuffer *buffer;
644 GtkTextIter iter, start_iter;
645 gchar buf[BUFFSIZE];
646 GPtrArray *headers = NULL;
647 const gchar *name;
648 gchar *content_type;
649 gint charcount;
651 START_TIMING("");
653 cm_return_if_fail(mimeinfo != NULL);
654 text = GTK_TEXT_VIEW(textview->text);
655 buffer = gtk_text_view_get_buffer(text);
656 charcount = gtk_text_buffer_get_char_count(buffer);
657 gtk_text_buffer_get_end_iter(buffer, &iter);
659 if (textview->stop_loading) {
660 return;
662 if (mimeinfo->type == MIMETYPE_MULTIPART) {
663 END_TIMING();
664 return;
667 textview->prev_quote_level = -1;
669 if ((mimeinfo->type == MIMETYPE_MESSAGE) && !g_ascii_strcasecmp(mimeinfo->subtype, "rfc822")) {
670 FILE *fp;
671 if (mimeinfo->content == MIMECONTENT_MEM)
672 fp = str_open_as_stream(mimeinfo->data.mem);
673 else
674 fp = claws_fopen(mimeinfo->data.filename, "rb");
675 if (!fp) {
676 FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
677 END_TIMING();
678 return;
680 if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
681 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
682 claws_fclose(fp);
683 END_TIMING();
684 return;
686 headers = textview_scan_header(textview, fp);
687 if (headers) {
688 if (charcount > 0)
689 gtk_text_buffer_insert(buffer, &iter, "\n", 1);
691 if (procmime_mimeinfo_parent(mimeinfo) == NULL &&
692 !prefs_common.display_header_pane)
693 textview_show_tags(textview);
694 textview_show_header(textview, headers);
695 procheader_header_array_destroy(headers);
697 claws_fclose(fp);
698 END_TIMING();
699 return;
702 name = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
703 content_type = procmime_get_content_type_str(mimeinfo->type,
704 mimeinfo->subtype);
705 if (name == NULL)
706 name = procmime_mimeinfo_get_parameter(mimeinfo, "name");
707 if (name != NULL)
708 g_snprintf(buf, sizeof(buf), _("[%s %s (%d bytes)]"),
709 name, content_type, mimeinfo->length);
710 else
711 g_snprintf(buf, sizeof(buf), _("[%s (%d bytes)]"),
712 content_type, mimeinfo->length);
714 g_free(content_type);
716 if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT
717 || (mimeinfo->disposition == DISPOSITIONTYPE_INLINE &&
718 mimeinfo->type != MIMETYPE_TEXT)) {
719 gtk_text_buffer_insert(buffer, &iter, "\n", 1);
720 TEXTVIEW_INSERT_LINK(buf, "sc://select_attachment", mimeinfo);
721 gtk_text_buffer_insert(buffer, &iter, " \n", -1);
722 if (mimeinfo->type == MIMETYPE_IMAGE &&
723 prefs_common.inline_img ) {
724 GdkPixbuf *pixbuf;
725 GError *error = NULL;
726 ClickableText *uri;
728 START_TIMING("inserting image");
730 pixbuf = procmime_get_part_as_pixbuf(mimeinfo, &error);
731 if (error != NULL) {
732 g_warning("can't load the image: %s", error->message);
733 g_error_free(error);
734 END_TIMING();
735 return;
738 if (textview->stop_loading) {
739 END_TIMING();
740 return;
743 gtk_widget_get_allocation(textview->scrolledwin, &allocation);
744 pixbuf = claws_load_pixbuf_fitting(pixbuf, prefs_common.inline_img,
745 prefs_common.fit_img_height, allocation.width,
746 allocation.height);
748 if (textview->stop_loading) {
749 END_TIMING();
750 return;
753 uri = g_new0(ClickableText, 1);
754 uri->uri = g_strdup("");
755 uri->filename = g_strdup("sc://select_attachment");
756 uri->data = mimeinfo;
758 uri->start = gtk_text_iter_get_offset(&iter);
759 gtk_text_buffer_insert_pixbuf(buffer, &iter, pixbuf);
760 g_object_unref(pixbuf);
761 if (textview->stop_loading) {
762 g_free(uri);
763 return;
765 uri->end = gtk_text_iter_get_offset(&iter);
767 textview->uri_list =
768 g_slist_prepend(textview->uri_list, uri);
770 gtk_text_buffer_insert(buffer, &iter, " ", 1);
771 gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, uri->start);
772 gtk_text_buffer_apply_tag_by_name(buffer, "link",
773 &start_iter, &iter);
775 END_TIMING();
776 GTK_EVENTS_FLUSH();
778 } else if (mimeinfo->type == MIMETYPE_TEXT) {
779 if (prefs_common.display_header && (charcount > 0))
780 gtk_text_buffer_insert(buffer, &iter, "\n", 1);
782 if (!gtk_text_buffer_get_mark(buffer, "body_start")) {
783 gtk_text_buffer_get_end_iter(buffer, &iter);
784 gtk_text_buffer_create_mark(buffer, "body_start", &iter, TRUE);
787 textview_write_body(textview, mimeinfo);
789 if (!gtk_text_buffer_get_mark(buffer, "body_end")) {
790 gtk_text_buffer_get_end_iter(buffer, &iter);
791 gtk_text_buffer_create_mark(buffer, "body_end", &iter, TRUE);
794 END_TIMING();
797 static void recursive_add_parts(TextView *textview, GNode *node)
799 GNode * iter;
800 MimeInfo *mimeinfo;
801 START_TIMING("");
803 mimeinfo = (MimeInfo *) node->data;
805 textview_add_part(textview, mimeinfo);
806 #ifdef GENERIC_UMPC
807 textview_set_position(textview, 0);
808 #endif
809 if ((mimeinfo->type != MIMETYPE_MULTIPART) &&
810 (mimeinfo->type != MIMETYPE_MESSAGE)) {
811 END_TIMING();
812 return;
814 if (g_ascii_strcasecmp(mimeinfo->subtype, "alternative") == 0) {
815 GNode * preferred_body;
816 int preferred_score;
819 text/plain : score 3
820 text/ * : score 2
821 other : score 1
823 preferred_body = NULL;
824 preferred_score = 0;
826 for (iter = g_node_first_child(node) ; iter != NULL ;
827 iter = g_node_next_sibling(iter)) {
828 int score;
829 MimeInfo * submime;
831 score = 1;
832 submime = (MimeInfo *) iter->data;
833 if (submime->type == MIMETYPE_TEXT)
834 score = 2;
836 if (submime->subtype != NULL) {
837 if (g_ascii_strcasecmp(submime->subtype, "plain") == 0)
838 score = 3;
841 if (score > preferred_score) {
842 preferred_score = score;
843 preferred_body = iter;
847 if (preferred_body != NULL) {
848 recursive_add_parts(textview, preferred_body);
851 else {
852 for (iter = g_node_first_child(node) ; iter != NULL ;
853 iter = g_node_next_sibling(iter)) {
854 recursive_add_parts(textview, iter);
857 END_TIMING();
860 static void textview_add_parts(TextView *textview, MimeInfo *mimeinfo)
862 cm_return_if_fail(mimeinfo != NULL);
863 cm_return_if_fail(mimeinfo->node != NULL);
865 recursive_add_parts(textview, mimeinfo->node);
868 void textview_show_error(TextView *textview)
870 GtkTextView *text;
871 GtkTextBuffer *buffer;
872 GtkTextIter iter;
874 textview_set_font(textview, NULL);
875 textview_clear(textview);
877 text = GTK_TEXT_VIEW(textview->text);
878 buffer = gtk_text_view_get_buffer(text);
879 gtk_text_buffer_get_start_iter(buffer, &iter);
881 TEXTVIEW_INSERT(_("\n"
882 " This message can't be displayed.\n"
883 " This is probably due to a network error.\n"
884 "\n"
885 " Use "));
886 TEXTVIEW_INSERT_LINK(_("'Network Log'"), "sc://view_log", NULL);
887 TEXTVIEW_INSERT(_(" in the Tools menu for more information."));
888 textview_show_icon(textview, "dialog-error");
891 void textview_show_info(TextView *textview, const gchar *info_str)
893 GtkTextView *text;
894 GtkTextBuffer *buffer;
895 GtkTextIter iter;
897 textview_set_font(textview, NULL);
898 textview_clear(textview);
900 text = GTK_TEXT_VIEW(textview->text);
901 buffer = gtk_text_view_get_buffer(text);
902 gtk_text_buffer_get_start_iter(buffer, &iter);
904 TEXTVIEW_INSERT(info_str);
905 textview_show_icon(textview, "dialog-information");
906 textview_cursor_normal(textview);
909 void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
911 GtkTextView *text;
912 GtkTextBuffer *buffer;
913 GtkTextIter iter;
914 const gchar *name;
915 gchar *content_type;
916 GtkUIManager *ui_manager;
917 #ifndef GENERIC_UMPC
918 gchar *shortcut;
919 #endif
921 if (!partinfo) return;
923 if (textview->messageview->window != NULL)
924 ui_manager = textview->messageview->ui_manager;
925 else
926 ui_manager = textview->messageview->mainwin->ui_manager;
928 textview_set_font(textview, NULL);
929 textview_clear(textview);
931 text = GTK_TEXT_VIEW(textview->text);
932 buffer = gtk_text_view_get_buffer(text);
933 gtk_text_buffer_get_start_iter(buffer, &iter);
935 TEXTVIEW_INSERT("\n");
937 name = procmime_mimeinfo_get_parameter(partinfo, "filename");
938 if (name == NULL)
939 name = procmime_mimeinfo_get_parameter(partinfo, "name");
940 if (name != NULL) {
941 content_type = procmime_get_content_type_str(partinfo->type,
942 partinfo->subtype);
943 TEXTVIEW_INSERT(" ");
944 TEXTVIEW_INSERT_BOLD(name);
945 TEXTVIEW_INSERT(" (");
946 TEXTVIEW_INSERT(content_type);
947 TEXTVIEW_INSERT(", ");
948 TEXTVIEW_INSERT(to_human_readable((goffset)partinfo->length));
949 TEXTVIEW_INSERT("):\n\n");
951 g_free(content_type);
953 TEXTVIEW_INSERT(_(" The following can be performed on this part\n"));
954 #ifndef GENERIC_UMPC
955 TEXTVIEW_INSERT(_(" by right-clicking the icon or list item:"));
956 #endif
957 TEXTVIEW_INSERT("\n");
959 TEXTVIEW_INSERT(_(" - To save, select "));
960 TEXTVIEW_INSERT_LINK(_("'Save as...'"), "sc://save_as", NULL);
961 #ifndef GENERIC_UMPC
962 TEXTVIEW_INSERT(_(" (Shortcut key: '"));
963 shortcut = cm_menu_item_get_shortcut(ui_manager, "Menu/File/SavePartAs");
964 TEXTVIEW_INSERT(shortcut);
965 g_free(shortcut);
966 TEXTVIEW_INSERT("')");
967 #endif
968 TEXTVIEW_INSERT("\n");
970 TEXTVIEW_INSERT(_(" - To display as text, select "));
971 TEXTVIEW_INSERT_LINK(_("'Display as text'"), "sc://display_as_text", NULL);
973 #ifndef GENERIC_UMPC
974 TEXTVIEW_INSERT(_(" (Shortcut key: '"));
975 shortcut = cm_menu_item_get_shortcut(ui_manager, "Menu/View/Part/AsText");
976 TEXTVIEW_INSERT(shortcut);
977 g_free(shortcut);
978 TEXTVIEW_INSERT("')");
979 #endif
980 TEXTVIEW_INSERT("\n");
982 TEXTVIEW_INSERT(_(" - To open with an external program, select "));
983 TEXTVIEW_INSERT_LINK(_("'Open'"), "sc://open", NULL);
985 #ifndef GENERIC_UMPC
986 TEXTVIEW_INSERT(_(" (Shortcut key: '"));
987 shortcut = cm_menu_item_get_shortcut(ui_manager, "Menu/View/Part/Open");
988 TEXTVIEW_INSERT(shortcut);
989 g_free(shortcut);
990 TEXTVIEW_INSERT("')\n");
991 TEXTVIEW_INSERT(_(" (alternately double-click, or click the middle "));
992 TEXTVIEW_INSERT(_("mouse button)\n"));
993 #ifndef G_OS_WIN32
994 TEXTVIEW_INSERT(_(" - Or use "));
995 TEXTVIEW_INSERT_LINK(_("'Open with...'"), "sc://open_with", NULL);
996 TEXTVIEW_INSERT(_(" (Shortcut key: '"));
997 shortcut = cm_menu_item_get_shortcut(ui_manager, "Menu/View/Part/OpenWith");
998 TEXTVIEW_INSERT(shortcut);
999 g_free(shortcut);
1000 TEXTVIEW_INSERT("')");
1001 #endif
1002 #endif
1003 TEXTVIEW_INSERT("\n");
1005 textview_show_icon(textview, "dialog-information");
1008 static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
1010 FILE *tmpfp;
1011 gchar buf[BUFFSIZE];
1012 CodeConverter *conv;
1013 const gchar *charset;
1014 #ifndef G_OS_WIN32
1015 const gchar *p, *cmd;
1016 #endif
1017 GSList *cur;
1018 gboolean continue_write = TRUE;
1019 size_t wrote = 0, i = 0;
1021 if (textview->messageview->forced_charset)
1022 charset = textview->messageview->forced_charset;
1023 else {
1024 /* use supersets transparently when possible */
1025 charset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
1026 if (charset && !strcasecmp(charset, CS_ISO_8859_1))
1027 charset = CS_WINDOWS_1252;
1028 else if (charset && !strcasecmp(charset, CS_X_GBK))
1029 charset = CS_GB18030;
1030 else if (charset && !strcasecmp(charset, CS_GBK))
1031 charset = CS_GB18030;
1032 else if (charset && !strcasecmp(charset, CS_GB2312))
1033 charset = CS_GB18030;
1036 textview_set_font(textview, charset);
1037 textview_set_font_zoom(textview);
1039 conv = conv_code_converter_new(charset);
1041 procmime_force_encoding(textview->messageview->forced_encoding);
1043 textview->is_in_signature = FALSE;
1044 textview->is_diff = FALSE;
1045 textview->is_attachment = FALSE;
1046 textview->is_in_git_patch = FALSE;
1048 procmime_decode_content(mimeinfo);
1050 account_sigsep_matchlist_create();
1052 if (!g_ascii_strcasecmp(mimeinfo->subtype, "html") &&
1053 prefs_common.render_html) {
1054 gchar *filename;
1056 filename = procmime_get_tmp_file_name(mimeinfo);
1057 if (procmime_get_part(filename, mimeinfo) == 0) {
1058 tmpfp = claws_fopen(filename, "rb");
1059 if (tmpfp) {
1060 textview_show_html(textview, tmpfp, conv);
1061 claws_fclose(tmpfp);
1063 claws_unlink(filename);
1065 g_free(filename);
1066 } else if (!g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
1067 gchar *filename;
1069 filename = procmime_get_tmp_file_name(mimeinfo);
1070 if (procmime_get_part(filename, mimeinfo) == 0) {
1071 tmpfp = claws_fopen(filename, "rb");
1072 if (tmpfp) {
1073 textview_show_ertf(textview, tmpfp, conv);
1074 claws_fclose(tmpfp);
1076 claws_unlink(filename);
1078 g_free(filename);
1079 #ifndef G_OS_WIN32
1080 } else if ( g_ascii_strcasecmp(mimeinfo->subtype, "plain") &&
1081 (cmd = prefs_common.mime_textviewer) && *cmd &&
1082 (p = strchr(cmd, '%')) && *(p + 1) == 's') {
1083 int pid, pfd[2];
1084 const gchar *fname;
1086 fname = procmime_get_tmp_file_name(mimeinfo);
1087 if (procmime_get_part(fname, mimeinfo)) goto textview_default;
1089 g_snprintf(buf, sizeof(buf), cmd, fname);
1090 debug_print("Viewing text content of type: %s (length: %d) "
1091 "using %s\n", mimeinfo->subtype, mimeinfo->length, buf);
1093 if (pipe(pfd) < 0) {
1094 g_snprintf(buf, sizeof(buf),
1095 "pipe failed for textview\n\n%s\n", g_strerror(errno));
1096 textview_write_line(textview, buf, conv, TRUE);
1097 goto textview_default;
1099 pid = fork();
1100 if (pid < 0) {
1101 g_snprintf(buf, sizeof(buf),
1102 "fork failed for textview\n\n%s\n", g_strerror(errno));
1103 textview_write_line(textview, buf, conv, TRUE);
1104 close(pfd[0]);
1105 close(pfd[1]);
1106 goto textview_default;
1108 if (pid == 0) { /* child */
1109 int rc;
1110 gchar **argv;
1111 argv = strsplit_with_quote(buf, " ", 0);
1112 close(1);
1113 close(pfd[0]);
1114 rc = dup(pfd[1]);
1115 rc = execvp(argv[0], argv);
1116 perror("execvp");
1117 close(pfd[1]);
1118 g_print(_("The command to view attachment "
1119 "as text failed:\n"
1120 " %s\n"
1121 "Exit code %d\n"), buf, rc);
1122 exit(255);
1124 close(pfd[1]);
1125 tmpfp = claws_fdopen(pfd[0], "rb");
1126 while (claws_fgets(buf, sizeof(buf), tmpfp)) {
1127 textview_write_line(textview, buf, conv, TRUE);
1129 if (textview->stop_loading) {
1130 claws_fclose(tmpfp);
1131 waitpid(pid, pfd, 0);
1132 g_unlink(fname);
1133 account_sigsep_matchlist_delete();
1134 conv_code_converter_destroy(conv);
1135 return;
1139 claws_fclose(tmpfp);
1140 waitpid(pid, pfd, 0);
1141 g_unlink(fname);
1142 #endif
1143 } else {
1144 #ifndef G_OS_WIN32
1145 textview_default:
1146 #endif
1147 if (!g_ascii_strcasecmp(mimeinfo->subtype, "x-patch")
1148 || !g_ascii_strcasecmp(mimeinfo->subtype, "x-diff"))
1149 textview->is_diff = TRUE;
1151 /* Displayed part is an attachment, but not an attached
1152 * e-mail. Set a flag, so that elsewhere in the code we
1153 * know not to try making collapsible quotes in it. */
1154 if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT &&
1155 mimeinfo->type != MIMETYPE_MESSAGE)
1156 textview->is_attachment = TRUE;
1158 if (mimeinfo->content == MIMECONTENT_MEM)
1159 tmpfp = str_open_as_stream(mimeinfo->data.mem);
1160 else
1161 tmpfp = claws_fopen(mimeinfo->data.filename, "rb");
1162 if (!tmpfp) {
1163 FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
1164 account_sigsep_matchlist_delete();
1165 conv_code_converter_destroy(conv);
1166 return;
1168 if (fseek(tmpfp, mimeinfo->offset, SEEK_SET) < 0) {
1169 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
1170 claws_fclose(tmpfp);
1171 account_sigsep_matchlist_delete();
1172 conv_code_converter_destroy(conv);
1173 return;
1175 debug_print("Viewing text content of type: %s (length: %d)\n", mimeinfo->subtype, mimeinfo->length);
1176 while (((i = ftell(tmpfp)) < mimeinfo->offset + mimeinfo->length) &&
1177 (claws_fgets(buf, sizeof(buf), tmpfp) != NULL)
1178 && continue_write) {
1179 textview_write_line(textview, buf, conv, TRUE);
1180 if (textview->stop_loading) {
1181 claws_fclose(tmpfp);
1182 account_sigsep_matchlist_delete();
1183 conv_code_converter_destroy(conv);
1184 return;
1186 wrote += ftell(tmpfp)-i;
1187 if (mimeinfo->length > 1024*1024
1188 && wrote > 1024*1024
1189 && !textview->messageview->show_full_text) {
1190 continue_write = FALSE;
1193 claws_fclose(tmpfp);
1196 account_sigsep_matchlist_delete();
1198 conv_code_converter_destroy(conv);
1199 procmime_force_encoding(0);
1201 textview->uri_list = g_slist_reverse(textview->uri_list);
1202 for (cur = textview->uri_list; cur; cur = cur->next) {
1203 ClickableText *uri = (ClickableText *)cur->data;
1204 if (!uri->is_quote)
1205 continue;
1206 if (!prefs_common.hide_quotes ||
1207 uri->quote_level+1 < prefs_common.hide_quotes) {
1208 textview_toggle_quote(textview, cur, uri, TRUE);
1209 if (textview->stop_loading) {
1210 return;
1215 if (continue_write == FALSE) {
1216 messageview_show_partial_display(
1217 textview->messageview,
1218 textview->messageview->msginfo,
1219 mimeinfo->length);
1221 GTK_EVENTS_FLUSH();
1224 static void textview_show_html(TextView *textview, FILE *fp,
1225 CodeConverter *conv)
1227 SC_HTMLParser *parser;
1228 gchar *str;
1229 gint lines = 0;
1231 parser = sc_html_parser_new(fp, conv);
1232 cm_return_if_fail(parser != NULL);
1234 account_sigsep_matchlist_create();
1236 while ((str = sc_html_parse(parser)) != NULL) {
1237 if (parser->state == SC_HTML_HREF) {
1238 /* first time : get and copy the URL */
1239 if (parser->href == NULL) {
1240 /* ALF - the claws html parser returns an empty string,
1241 * if still inside an <a>, but already parsed past HREF */
1242 str = strtok(str, " ");
1243 if (str) {
1244 while (str && *str && g_ascii_isspace(*str))
1245 str++;
1246 parser->href = g_strdup(str);
1247 /* the URL may (or not) be followed by the
1248 * referenced text */
1249 str = strtok(NULL, "");
1252 if (str != NULL)
1253 textview_write_link(textview, str, parser->href, NULL);
1254 } else
1255 textview_write_line(textview, str, NULL, FALSE);
1256 lines++;
1257 if (lines % 500 == 0)
1258 GTK_EVENTS_FLUSH();
1259 if (textview->stop_loading) {
1260 account_sigsep_matchlist_delete();
1261 sc_html_parser_destroy(parser);
1262 return;
1265 textview_write_line(textview, "\n", NULL, FALSE);
1267 account_sigsep_matchlist_delete();
1269 sc_html_parser_destroy(parser);
1272 static void textview_show_ertf(TextView *textview, FILE *fp,
1273 CodeConverter *conv)
1275 ERTFParser *parser;
1276 gchar *str;
1277 gint lines = 0;
1279 parser = ertf_parser_new(fp, conv);
1280 cm_return_if_fail(parser != NULL);
1282 account_sigsep_matchlist_create();
1284 while ((str = ertf_parse(parser)) != NULL) {
1285 textview_write_line(textview, str, NULL, FALSE);
1286 lines++;
1287 if (lines % 500 == 0)
1288 GTK_EVENTS_FLUSH();
1289 if (textview->stop_loading) {
1290 account_sigsep_matchlist_delete();
1291 ertf_parser_destroy(parser);
1292 return;
1296 account_sigsep_matchlist_delete();
1298 ertf_parser_destroy(parser);
1301 #define ADD_TXT_POS(bp_, ep_, pti_) \
1302 if ((last->next = alloca(sizeof(struct txtpos))) != NULL) { \
1303 last = last->next; \
1304 last->bp = (bp_); last->ep = (ep_); last->pti = (pti_); \
1305 last->next = NULL; \
1306 } else { \
1307 g_warning("alloc error scanning URIs"); \
1308 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, \
1309 linebuf, -1, \
1310 fg_tag, NULL); \
1311 return; \
1314 #define ADD_TXT_POS_LATER(bp_, ep_, pti_) \
1315 if ((last->next = alloca(sizeof(struct txtpos))) != NULL) { \
1316 last = last->next; \
1317 last->bp = (bp_); last->ep = (ep_); last->pti = (pti_); \
1318 last->next = NULL; \
1319 } else { \
1320 g_warning("alloc error scanning URIs"); \
1323 /* textview_make_clickable_parts() - colorizes clickable parts */
1324 static void textview_make_clickable_parts(TextView *textview,
1325 const gchar *fg_tag,
1326 const gchar *uri_tag,
1327 const gchar *linebuf,
1328 gboolean hdr)
1330 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1331 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
1332 GtkTextIter iter;
1333 gchar *mybuf = g_strdup(linebuf);
1335 /* parse table - in order of priority */
1336 struct table {
1337 const gchar *needle; /* token */
1339 /* token search function */
1340 gchar *(*search) (const gchar *haystack,
1341 const gchar *needle);
1342 /* part parsing function */
1343 gboolean (*parse) (const gchar *start,
1344 const gchar *scanpos,
1345 const gchar **bp_,
1346 const gchar **ep_,
1347 gboolean hdr);
1348 /* part to URI function */
1349 gchar *(*build_uri) (const gchar *bp,
1350 const gchar *ep);
1353 static struct table parser[] = {
1354 {"http://", strcasestr, get_uri_part, make_uri_string},
1355 {"https://", strcasestr, get_uri_part, make_uri_string},
1356 {"ftp://", strcasestr, get_uri_part, make_uri_string},
1357 {"ftps://", strcasestr, get_uri_part, make_uri_string},
1358 {"sftp://", strcasestr, get_uri_part, make_uri_string},
1359 {"gopher://",strcasestr, get_uri_part, make_uri_string},
1360 {"www.", strcasestr, get_uri_part, make_http_string},
1361 {"webcal://",strcasestr, get_uri_part, make_uri_string},
1362 {"webcals://",strcasestr, get_uri_part, make_uri_string},
1363 {"mailto:", strcasestr, get_uri_part, make_uri_string},
1364 {"@", strcasestr, get_email_part, make_email_string}
1366 const gint PARSE_ELEMS = sizeof parser / sizeof parser[0];
1368 gint n;
1369 const gchar *walk, *bp, *ep;
1371 struct txtpos {
1372 const gchar *bp, *ep; /* text position */
1373 gint pti; /* index in parse table */
1374 struct txtpos *next; /* next */
1375 } head = {NULL, NULL, 0, NULL}, *last = &head;
1377 if (!g_utf8_validate(linebuf, -1, NULL)) {
1378 g_free(mybuf);
1379 mybuf = g_malloc(strlen(linebuf)*2 +1);
1380 conv_localetodisp(mybuf, strlen(linebuf)*2 +1, linebuf);
1383 gtk_text_buffer_get_end_iter(buffer, &iter);
1385 /* parse for clickable parts, and build a list of begin and end positions */
1386 for (walk = mybuf;;) {
1387 gint last_index = PARSE_ELEMS;
1388 gchar *scanpos = NULL;
1390 /* FIXME: this looks phony. scanning for anything in the parse table */
1391 for (n = 0; n < PARSE_ELEMS; n++) {
1392 gchar *tmp;
1394 tmp = parser[n].search(walk, parser[n].needle);
1395 if (tmp) {
1396 if (scanpos == NULL || tmp < scanpos) {
1397 scanpos = tmp;
1398 last_index = n;
1403 if (scanpos) {
1404 /* check if URI can be parsed */
1405 if (parser[last_index].parse(walk, scanpos, &bp, &ep, hdr)
1406 && (size_t) (ep - bp - 1) > strlen(parser[last_index].needle)) {
1407 ADD_TXT_POS(bp, ep, last_index);
1408 walk = ep;
1409 } else
1410 walk = scanpos +
1411 strlen(parser[last_index].needle);
1412 } else
1413 break;
1416 /* colorize this line */
1417 if (head.next) {
1418 const gchar *normal_text = mybuf;
1420 /* insert URIs */
1421 for (last = head.next; last != NULL;
1422 normal_text = last->ep, last = last->next) {
1423 ClickableText *uri;
1424 uri = g_new0(ClickableText, 1);
1425 if (last->bp - normal_text > 0)
1426 gtk_text_buffer_insert_with_tags_by_name
1427 (buffer, &iter,
1428 normal_text,
1429 last->bp - normal_text,
1430 fg_tag, NULL);
1431 uri->uri = parser[last->pti].build_uri(last->bp,
1432 last->ep);
1433 uri->start = gtk_text_iter_get_offset(&iter);
1434 gtk_text_buffer_insert_with_tags_by_name
1435 (buffer, &iter, last->bp, last->ep - last->bp,
1436 uri_tag, fg_tag, NULL);
1437 uri->end = gtk_text_iter_get_offset(&iter);
1438 uri->filename = NULL;
1439 textview->uri_list =
1440 g_slist_prepend(textview->uri_list, uri);
1443 if (*normal_text)
1444 gtk_text_buffer_insert_with_tags_by_name
1445 (buffer, &iter, normal_text, -1, fg_tag, NULL);
1446 } else {
1447 gtk_text_buffer_insert_with_tags_by_name
1448 (buffer, &iter, mybuf, -1, fg_tag, NULL);
1450 g_free(mybuf);
1453 /* textview_make_clickable_parts() - colorizes clickable parts */
1454 static void textview_make_clickable_parts_later(TextView *textview,
1455 gint start, gint end)
1457 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1458 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
1459 GtkTextIter start_iter, end_iter;
1460 gchar *mybuf;
1461 gint offset = 0;
1462 /* parse table - in order of priority */
1463 struct table {
1464 const gchar *needle; /* token */
1466 /* token search function */
1467 gchar *(*search) (const gchar *haystack,
1468 const gchar *needle);
1469 /* part parsing function */
1470 gboolean (*parse) (const gchar *start,
1471 const gchar *scanpos,
1472 const gchar **bp_,
1473 const gchar **ep_,
1474 gboolean hdr);
1475 /* part to URI function */
1476 gchar *(*build_uri) (const gchar *bp,
1477 const gchar *ep);
1480 static struct table parser[] = {
1481 {"http://", strcasestr, get_uri_part, make_uri_string},
1482 {"https://", strcasestr, get_uri_part, make_uri_string},
1483 {"ftp://", strcasestr, get_uri_part, make_uri_string},
1484 {"ftps://", strcasestr, get_uri_part, make_uri_string},
1485 {"sftp://", strcasestr, get_uri_part, make_uri_string},
1486 {"www.", strcasestr, get_uri_part, make_http_string},
1487 {"mailto:", strcasestr, get_uri_part, make_uri_string},
1488 {"webcal://",strcasestr, get_uri_part, make_uri_string},
1489 {"webcals://",strcasestr, get_uri_part, make_uri_string},
1490 {"@", strcasestr, get_email_part, make_email_string}
1492 const gint PARSE_ELEMS = sizeof parser / sizeof parser[0];
1494 gint n;
1495 const gchar *walk, *bp, *ep;
1497 struct txtpos {
1498 const gchar *bp, *ep; /* text position */
1499 gint pti; /* index in parse table */
1500 struct txtpos *next; /* next */
1501 } head = {NULL, NULL, 0, NULL}, *last = &head;
1503 gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, start);
1504 gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, end);
1505 mybuf = gtk_text_buffer_get_text(buffer, &start_iter, &end_iter, FALSE);
1506 offset = gtk_text_iter_get_offset(&start_iter);
1508 /* parse for clickable parts, and build a list of begin and end positions */
1509 for (walk = mybuf;;) {
1510 gint last_index = PARSE_ELEMS;
1511 gchar *scanpos = NULL;
1513 /* FIXME: this looks phony. scanning for anything in the parse table */
1514 for (n = 0; n < PARSE_ELEMS; n++) {
1515 gchar *tmp;
1517 tmp = parser[n].search(walk, parser[n].needle);
1518 if (tmp) {
1519 if (scanpos == NULL || tmp < scanpos) {
1520 scanpos = tmp;
1521 last_index = n;
1526 if (scanpos) {
1527 /* check if URI can be parsed */
1528 if (parser[last_index].parse(walk, scanpos, &bp, &ep, FALSE)
1529 && (size_t) (ep - bp - 1) > strlen(parser[last_index].needle)) {
1530 ADD_TXT_POS_LATER(bp, ep, last_index);
1531 walk = ep;
1532 } else
1533 walk = scanpos +
1534 strlen(parser[last_index].needle);
1535 } else
1536 break;
1539 /* colorize this line */
1540 if (head.next) {
1541 /* insert URIs */
1542 for (last = head.next; last != NULL; last = last->next) {
1543 ClickableText *uri;
1544 gint start_offset, end_offset;
1545 gchar *tmp_str;
1546 gchar old_char;
1547 uri = g_new0(ClickableText, 1);
1548 uri->uri = parser[last->pti].build_uri(last->bp,
1549 last->ep);
1551 tmp_str = mybuf;
1552 old_char = tmp_str[last->ep - mybuf];
1553 tmp_str[last->ep - mybuf] = '\0';
1554 end_offset = g_utf8_strlen(tmp_str, -1);
1555 tmp_str[last->ep - mybuf] = old_char;
1557 old_char = tmp_str[last->bp - mybuf];
1558 tmp_str[last->bp - mybuf] = '\0';
1559 start_offset = g_utf8_strlen(tmp_str, -1);
1560 tmp_str[last->bp - mybuf] = old_char;
1562 gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, start_offset + offset);
1563 gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, end_offset + offset);
1565 uri->start = gtk_text_iter_get_offset(&start_iter);
1567 gtk_text_buffer_apply_tag_by_name(buffer, "link", &start_iter, &end_iter);
1569 uri->end = gtk_text_iter_get_offset(&end_iter);
1570 uri->filename = NULL;
1571 textview->uri_list =
1572 g_slist_prepend(textview->uri_list, uri);
1576 g_free(mybuf);
1579 #undef ADD_TXT_POS
1581 static void textview_write_line(TextView *textview, const gchar *str,
1582 CodeConverter *conv, gboolean do_quote_folding)
1584 GtkTextView *text;
1585 GtkTextBuffer *buffer;
1586 GtkTextIter iter;
1587 gchar buf[BUFFSIZE];
1588 gchar *fg_color;
1589 gint quotelevel = -1, real_quotelevel = -1;
1590 gchar quote_tag_str[10];
1592 text = GTK_TEXT_VIEW(textview->text);
1593 buffer = gtk_text_view_get_buffer(text);
1594 gtk_text_buffer_get_end_iter(buffer, &iter);
1596 if (!conv)
1597 strncpy2(buf, str, sizeof(buf));
1598 else if (conv_convert(conv, buf, sizeof(buf), str) < 0)
1599 conv_localetodisp(buf, sizeof(buf), str);
1601 strcrchomp(buf);
1602 fg_color = NULL;
1604 /* change color of quotation
1605 >, foo>, _> ... ok, <foo>, foo bar>, foo-> ... ng
1606 Up to 3 levels of quotations are detected, and each
1607 level is colored using a different color. */
1608 if (prefs_common.enable_color
1609 && !textview->is_attachment
1610 && line_has_quote_char(buf, prefs_common.quote_chars)) {
1611 real_quotelevel = get_quote_level(buf, prefs_common.quote_chars);
1612 quotelevel = real_quotelevel;
1613 /* set up the correct foreground color */
1614 if (quotelevel > 2) {
1615 /* recycle colors */
1616 if (prefs_common.recycle_quote_colors)
1617 quotelevel %= 3;
1618 else
1619 quotelevel = 2;
1623 if (quotelevel == -1)
1624 fg_color = NULL;
1625 else {
1626 g_snprintf(quote_tag_str, sizeof(quote_tag_str),
1627 "quote%d", quotelevel);
1628 fg_color = quote_tag_str;
1631 if (prefs_common.enable_color) {
1632 if (textview->is_diff || textview->is_in_git_patch) {
1633 if (strncmp(buf, "+++ ", 4) == 0)
1634 fg_color = "diff-add-file";
1635 else if (buf[0] == '+')
1636 fg_color = "diff-add";
1637 else if (strncmp(buf, "--- ", 4) == 0)
1638 fg_color = "diff-del-file";
1639 else if (buf[0] == '-')
1640 fg_color = "diff-del";
1641 else if (strncmp(buf, "@@ ", 3) == 0 &&
1642 strstr(&buf[3], " @@"))
1643 fg_color = "diff-hunk";
1645 if (account_sigsep_matchlist_nchar_found(buf, "%s\n")) {
1646 textview->is_in_git_patch = FALSE;
1647 textview->is_in_signature = TRUE;
1648 fg_color = "signature";
1650 } else if (account_sigsep_matchlist_str_found(buf, "%s\n")
1651 || account_sigsep_matchlist_str_found(buf, "- %s\n")
1652 || textview->is_in_signature) {
1653 fg_color = "signature";
1654 textview->is_in_signature = TRUE;
1655 } else if (strncmp(buf, "diff --git ", 11) == 0) {
1656 textview->is_in_git_patch = TRUE;
1660 if (!textview->is_attachment && real_quotelevel > -1 && do_quote_folding) {
1661 if (!g_utf8_validate(buf, -1, NULL)) {
1662 gchar *utf8buf = NULL;
1663 utf8buf = g_malloc(BUFFSIZE);
1664 conv_localetodisp(utf8buf, BUFFSIZE, buf);
1665 strncpy2(buf, utf8buf, BUFFSIZE-1);
1666 g_free(utf8buf);
1668 do_quote:
1669 if ( textview->prev_quote_level != real_quotelevel ) {
1670 ClickableText *uri;
1671 uri = g_new0(ClickableText, 1);
1672 uri->uri = g_strdup("");
1673 uri->data = g_strdup(buf);
1674 uri->data_len = strlen(uri->data);
1675 uri->start = gtk_text_iter_get_offset(&iter);
1676 uri->is_quote = TRUE;
1677 uri->quote_level = real_quotelevel;
1678 uri->fg_color = g_strdup(fg_color);
1680 gtk_text_buffer_insert_with_tags_by_name
1681 (buffer, &iter, " [...]", -1,
1682 "qlink", fg_color, NULL);
1683 uri->end = gtk_text_iter_get_offset(&iter);
1684 gtk_text_buffer_insert(buffer, &iter, " \n", -1);
1686 uri->filename = NULL;
1687 textview->uri_list =
1688 g_slist_prepend(textview->uri_list, uri);
1690 textview->prev_quote_level = real_quotelevel;
1691 } else {
1692 GSList *last = textview->uri_list;
1693 ClickableText *lasturi = NULL;
1694 gint e_len = 0, n_len = 0;
1696 if (textview->uri_list) {
1697 lasturi = (ClickableText *)last->data;
1698 } else {
1699 g_print("oops (%d %d)\n",
1700 real_quotelevel, textview->prev_quote_level);
1702 if (lasturi) {
1703 if (lasturi->is_quote == FALSE) {
1704 textview->prev_quote_level = -1;
1705 goto do_quote;
1707 e_len = lasturi->data ? lasturi->data_len:0;
1708 n_len = strlen(buf);
1709 lasturi->data = g_realloc((gchar *)lasturi->data, e_len + n_len + 1);
1710 strcpy((gchar *)lasturi->data + e_len, buf);
1711 *((gchar *)lasturi->data + e_len + n_len) = '\0';
1712 lasturi->data_len += n_len;
1715 } else {
1716 textview_make_clickable_parts(textview, fg_color, "link", buf, FALSE);
1717 textview->prev_quote_level = -1;
1721 void textview_write_link(TextView *textview, const gchar *str,
1722 const gchar *uri, CodeConverter *conv)
1724 GtkTextView *text;
1725 GtkTextBuffer *buffer;
1726 GtkTextIter iter;
1727 gchar buf[BUFFSIZE];
1728 gchar *bufp;
1729 ClickableText *r_uri;
1731 if (!str || *str == '\0')
1732 return;
1733 if (!uri)
1734 return;
1736 while (uri && *uri && g_ascii_isspace(*uri))
1737 uri++;
1739 text = GTK_TEXT_VIEW(textview->text);
1740 buffer = gtk_text_view_get_buffer(text);
1741 gtk_text_buffer_get_end_iter(buffer, &iter);
1743 if (!conv)
1744 strncpy2(buf, str, sizeof(buf));
1745 else if (conv_convert(conv, buf, sizeof(buf), str) < 0)
1746 conv_utf8todisp(buf, sizeof(buf), str);
1748 if (g_utf8_validate(buf, -1, NULL) == FALSE)
1749 return;
1751 strcrchomp(buf);
1753 gtk_text_buffer_get_end_iter(buffer, &iter);
1754 for (bufp = buf; *bufp != '\0'; bufp = g_utf8_next_char(bufp)) {
1755 gunichar ch;
1757 ch = g_utf8_get_char(bufp);
1758 if (!g_unichar_isspace(ch))
1759 break;
1761 if (bufp > buf)
1762 gtk_text_buffer_insert(buffer, &iter, buf, bufp - buf);
1764 r_uri = g_new0(ClickableText, 1);
1765 r_uri->uri = g_strdup(uri);
1766 r_uri->start = gtk_text_iter_get_offset(&iter);
1767 gtk_text_buffer_insert_with_tags_by_name
1768 (buffer, &iter, bufp, -1, "link", NULL);
1769 r_uri->end = gtk_text_iter_get_offset(&iter);
1770 r_uri->filename = NULL;
1771 textview->uri_list = g_slist_prepend(textview->uri_list, r_uri);
1774 static void textview_set_cursor(GdkWindow *window, GdkCursor *cursor)
1776 if (GDK_IS_WINDOW(window))
1777 gdk_window_set_cursor(window, cursor);
1779 void textview_clear(TextView *textview)
1781 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1782 GtkTextBuffer *buffer;
1783 GdkWindow *window = gtk_text_view_get_window(text,
1784 GTK_TEXT_WINDOW_TEXT);
1786 buffer = gtk_text_view_get_buffer(text);
1787 gtk_text_buffer_set_text(buffer, "", -1);
1788 if (gtk_text_buffer_get_mark(buffer, "body_start"))
1789 gtk_text_buffer_delete_mark_by_name(buffer, "body_start");
1790 if (gtk_text_buffer_get_mark(buffer, "body_end"))
1791 gtk_text_buffer_delete_mark_by_name(buffer, "body_end");
1793 TEXTVIEW_STATUSBAR_POP(textview);
1794 textview_uri_list_remove_all(textview->uri_list);
1795 textview->uri_list = NULL;
1796 textview->uri_hover = NULL;
1797 textview->prev_quote_level = -1;
1799 textview->body_pos = 0;
1800 if (textview->image)
1801 gtk_widget_destroy(textview->image);
1802 textview->image = NULL;
1803 textview->avatar_type = 0;
1805 if (textview->messageview->mainwin->cursor_count == 0) {
1806 textview_set_cursor(window, text_cursor);
1807 } else {
1808 textview_set_cursor(window, watch_cursor);
1812 void textview_destroy(TextView *textview)
1814 GtkTextBuffer *buffer;
1815 GtkClipboard *clipboard;
1817 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
1818 clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1819 gtk_text_buffer_remove_selection_clipboard(buffer, clipboard);
1821 textview_uri_list_remove_all(textview->uri_list);
1822 textview->uri_list = NULL;
1823 textview->prev_quote_level = -1;
1825 g_free(textview);
1828 #define CHANGE_TAG_FONT(tagname, font) { \
1829 tag = gtk_text_tag_table_lookup(tags, tagname); \
1830 if (tag) \
1831 g_object_set(G_OBJECT(tag), "font-desc", font, NULL); \
1834 void textview_set_font(TextView *textview, const gchar *codeset)
1836 GtkTextTag *tag;
1837 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
1838 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
1839 PangoFontDescription *font_desc, *bold_font_desc;
1841 font_desc = pango_font_description_from_string
1842 (NORMAL_FONT);
1843 if (font_desc) {
1844 gtk_widget_override_font(textview->text, font_desc);
1845 CHANGE_TAG_FONT("header", font_desc);
1846 CHANGE_TAG_FONT("hlink", font_desc);
1847 pango_font_description_free(font_desc);
1849 if (prefs_common.derive_from_normal_font || !BOLD_FONT) {
1850 bold_font_desc = pango_font_description_from_string
1851 (NORMAL_FONT);
1852 if (bold_font_desc)
1853 pango_font_description_set_weight
1854 (bold_font_desc, PANGO_WEIGHT_BOLD);
1855 } else {
1856 bold_font_desc = pango_font_description_from_string
1857 (BOLD_FONT);
1859 if (bold_font_desc) {
1860 CHANGE_TAG_FONT("header_title", bold_font_desc);
1861 pango_font_description_free(bold_font_desc);
1864 if (prefs_common.textfont) {
1865 PangoFontDescription *font_desc;
1867 font_desc = pango_font_description_from_string
1868 (prefs_common.textfont);
1869 if (font_desc) {
1870 gtk_widget_override_font(textview->text, font_desc);
1871 pango_font_description_free(font_desc);
1874 gtk_text_view_set_pixels_above_lines(GTK_TEXT_VIEW(textview->text),
1875 prefs_common.line_space / 2);
1876 gtk_text_view_set_pixels_below_lines(GTK_TEXT_VIEW(textview->text),
1877 prefs_common.line_space / 2);
1880 void textview_set_text(TextView *textview, const gchar *text)
1882 GtkTextView *view;
1883 GtkTextBuffer *buffer;
1885 cm_return_if_fail(textview != NULL);
1886 cm_return_if_fail(text != NULL);
1888 textview_clear(textview);
1890 view = GTK_TEXT_VIEW(textview->text);
1891 buffer = gtk_text_view_get_buffer(view);
1892 gtk_text_buffer_set_text(buffer, text, strlen(text));
1895 enum
1897 H_DATE = 0,
1898 H_FROM = 1,
1899 H_TO = 2,
1900 H_NEWSGROUPS = 3,
1901 H_SUBJECT = 4,
1902 H_CC = 5,
1903 H_REPLY_TO = 6,
1904 H_FOLLOWUP_TO = 7,
1905 H_X_MAILER = 8,
1906 H_X_NEWSREADER = 9,
1907 H_USER_AGENT = 10,
1908 H_ORGANIZATION = 11,
1911 void textview_set_position(TextView *textview, gint pos)
1913 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1915 gtkut_text_view_set_position(text, pos);
1918 static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
1920 gchar buf[BUFFSIZE];
1921 GPtrArray *headers, *sorted_headers;
1922 GSList *disphdr_list;
1923 Header *header;
1924 gint i;
1926 cm_return_val_if_fail(fp != NULL, NULL);
1928 if (prefs_common.show_all_headers) {
1929 headers = procheader_get_header_array(fp);
1930 sorted_headers = g_ptr_array_new();
1931 for (i = 0; i < headers->len; i++) {
1932 header = g_ptr_array_index(headers, i);
1933 if (!procheader_header_is_internal(header->name))
1934 g_ptr_array_add(sorted_headers, header);
1935 else
1936 procheader_header_free(header);
1938 g_ptr_array_free(headers, TRUE);
1939 return sorted_headers;
1942 if (!prefs_common.display_header) {
1943 while (claws_fgets(buf, sizeof(buf), fp) != NULL)
1944 if (buf[0] == '\r' || buf[0] == '\n') break;
1945 return NULL;
1948 headers = procheader_get_header_array(fp);
1950 sorted_headers = g_ptr_array_new();
1952 for (disphdr_list = prefs_common.disphdr_list; disphdr_list != NULL;
1953 disphdr_list = disphdr_list->next) {
1954 DisplayHeaderProp *dp =
1955 (DisplayHeaderProp *)disphdr_list->data;
1957 for (i = 0; i < headers->len; i++) {
1958 header = g_ptr_array_index(headers, i);
1959 if (procheader_headername_equal(header->name,
1960 dp->name)) {
1961 if (dp->hidden)
1962 procheader_header_free(header);
1963 else
1964 g_ptr_array_add(sorted_headers, header);
1966 g_ptr_array_remove_index(headers, i);
1967 i--;
1972 if (prefs_common.show_other_header) {
1973 for (i = 0; i < headers->len; i++) {
1974 header = g_ptr_array_index(headers, i);
1975 unfold_line(header->body);
1976 if (!procheader_header_is_internal(header->name)) {
1977 g_ptr_array_add(sorted_headers, header);
1978 } else {
1979 procheader_header_free(header);
1982 g_ptr_array_free(headers, TRUE);
1983 } else
1984 procheader_header_array_destroy(headers);
1987 return sorted_headers;
1990 static void textview_show_avatar(TextView *textview)
1992 GtkAllocation allocation;
1993 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
1994 MsgInfo *msginfo = textview->messageview->msginfo;
1995 gint x, wx, wy;
1996 AvatarRender *avatarr;
1998 if (prefs_common.display_header_pane || !prefs_common.display_xface)
1999 goto bail;
2001 avatarr = avatars_avatarrender_new(msginfo);
2002 hooks_invoke(AVATAR_IMAGE_RENDER_HOOKLIST, avatarr);
2004 if (!avatarr->image) {
2005 avatars_avatarrender_free(avatarr);
2006 goto bail;
2009 if (textview->image)
2010 gtk_widget_destroy(textview->image);
2012 textview->image = avatarr->image;
2013 textview->avatar_type = avatarr->type;
2014 avatarr->image = NULL; /* avoid destroying */
2015 avatars_avatarrender_free(avatarr);
2017 gtk_widget_set_name(GTK_WIDGET(textview->image), "textview_avatar");
2018 gtk_widget_show(textview->image);
2020 gtk_widget_get_allocation(textview->text, &allocation);
2021 x = allocation.width - WIDTH - 5;
2023 gtk_text_view_buffer_to_window_coords(
2024 GTK_TEXT_VIEW(textview->text),
2025 GTK_TEXT_WINDOW_TEXT, x, 5, &wx, &wy);
2027 gtk_text_view_add_child_in_window(text, textview->image,
2028 GTK_TEXT_WINDOW_TEXT, wx, wy);
2030 gtk_widget_show_all(textview->text);
2032 return;
2033 bail:
2034 if (textview->image)
2035 gtk_widget_destroy(textview->image);
2036 textview->image = NULL;
2037 textview->avatar_type = 0;
2040 void textview_show_icon(TextView *textview, const gchar *stock_id)
2042 GtkAllocation allocation;
2043 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
2044 gint x, wx, wy;
2046 if (textview->image)
2047 gtk_widget_destroy(textview->image);
2049 textview->image = gtk_image_new_from_icon_name(stock_id, GTK_ICON_SIZE_DIALOG);
2050 cm_return_if_fail(textview->image != NULL);
2052 gtk_widget_set_name(GTK_WIDGET(textview->image), "textview_icon");
2053 gtk_widget_show(textview->image);
2055 gtk_widget_get_allocation(textview->text, &allocation);
2056 x = allocation.width - WIDTH - 5;
2058 gtk_text_view_buffer_to_window_coords(
2059 GTK_TEXT_VIEW(textview->text),
2060 GTK_TEXT_WINDOW_TEXT, x, 5, &wx, &wy);
2062 gtk_text_view_add_child_in_window(text, textview->image,
2063 GTK_TEXT_WINDOW_TEXT, wx, wy);
2065 gtk_widget_show_all(textview->text);
2068 return;
2071 static void textview_save_contact_pic(TextView *textview)
2073 #ifndef USE_ALT_ADDRBOOK
2074 MsgInfo *msginfo = textview->messageview->msginfo;
2075 gchar *filename = NULL;
2076 GError *error = NULL;
2077 GdkPixbuf *picture = NULL;
2079 if (!msginfo->extradata || !msginfo->extradata->avatars)
2080 return;
2082 if (textview->avatar_type > AVATAR_FACE)
2083 return;
2085 if (textview->image)
2086 picture = gtk_image_get_pixbuf(GTK_IMAGE(textview->image));
2088 filename = addrindex_get_picture_file(msginfo->from);
2089 if (!filename)
2090 return;
2091 if (!is_file_exist(filename)) {
2092 gdk_pixbuf_save(picture, filename, "png", &error, NULL);
2093 if (error) {
2094 g_warning("failed to save image: %s",
2095 error->message);
2096 g_error_free(error);
2099 g_free(filename);
2100 #else
2101 /* new address book */
2102 #endif
2105 static void textview_show_contact_pic(TextView *textview)
2107 #ifndef USE_ALT_ADDRBOOK
2108 MsgInfo *msginfo = textview->messageview->msginfo;
2109 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
2110 gint x, wx, wy;
2111 gchar *filename = NULL;
2112 GError *error = NULL;
2113 GdkPixbuf *picture = NULL;
2114 gint w, h;
2115 GtkAllocation allocation;
2117 if (prefs_common.display_header_pane
2118 || !prefs_common.display_xface)
2119 goto bail;
2121 if (msginfo->extradata && msginfo->extradata->avatars)
2122 return;
2124 if (textview->image)
2125 gtk_widget_destroy(textview->image);
2127 filename = addrindex_get_picture_file(msginfo->from);
2129 if (!filename)
2130 goto bail;
2131 if (!is_file_exist(filename)) {
2132 g_free(filename);
2133 goto bail;
2136 gdk_pixbuf_get_file_info(filename, &w, &h);
2138 if (w > 48 || h > 48)
2139 picture = gdk_pixbuf_new_from_file_at_scale(filename,
2140 48, 48, TRUE, &error);
2141 else
2142 picture = gdk_pixbuf_new_from_file(filename, &error);
2144 if (error) {
2145 debug_print("Failed to import image: %s\n",
2146 error->message);
2147 g_error_free(error);
2148 goto bail;
2150 g_free(filename);
2152 if (picture) {
2153 textview->image = gtk_image_new_from_pixbuf(picture);
2154 g_object_unref(picture);
2156 cm_return_if_fail(textview->image != NULL);
2158 gtk_widget_set_name(GTK_WIDGET(textview->image), "textview_contact_pic");
2159 gtk_widget_show(textview->image);
2161 gtk_widget_get_allocation(textview->text, &allocation);
2162 x = allocation.width - WIDTH - 5;
2164 gtk_text_view_buffer_to_window_coords(
2165 GTK_TEXT_VIEW(textview->text),
2166 GTK_TEXT_WINDOW_TEXT, x, 5, &wx, &wy);
2168 gtk_text_view_add_child_in_window(text, textview->image,
2169 GTK_TEXT_WINDOW_TEXT, wx, wy);
2171 gtk_widget_show_all(textview->text);
2173 return;
2174 bail:
2175 if (textview->image)
2176 gtk_widget_destroy(textview->image);
2177 textview->image = NULL;
2178 textview->avatar_type = 0;
2179 #else
2180 /* new address book */
2181 #endif
2184 static gint textview_tag_cmp_list(gconstpointer a, gconstpointer b)
2186 gint id_a = GPOINTER_TO_INT(a);
2187 gint id_b = GPOINTER_TO_INT(b);
2188 const gchar *tag_a = tags_get_tag(id_a);
2189 const gchar *tag_b = tags_get_tag(id_b);
2191 if (tag_a == NULL)
2192 return tag_b == NULL ? 0:1;
2194 if (tag_b == NULL)
2195 return 1;
2197 return g_utf8_collate(tag_a, tag_b);
2201 static void textview_show_tags(TextView *textview)
2203 MsgInfo *msginfo = textview->messageview->msginfo;
2204 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
2205 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
2206 GtkTextIter iter;
2207 ClickableText *uri;
2208 GSList *cur, *orig;
2209 gboolean found_tag = FALSE;
2211 if (!msginfo->tags)
2212 return;
2214 cur = orig = g_slist_sort(g_slist_copy(msginfo->tags), textview_tag_cmp_list);
2216 for (; cur; cur = cur->next) {
2217 if (tags_get_tag(GPOINTER_TO_INT(cur->data)) != NULL) {
2218 found_tag = TRUE;
2219 break;
2222 if (!found_tag) {
2223 g_slist_free(orig);
2224 return;
2227 gtk_text_buffer_get_end_iter (buffer, &iter);
2228 gtk_text_buffer_insert_with_tags_by_name(buffer,
2229 &iter, _("Tags: "), -1,
2230 "header_title", "header", "tags", NULL);
2232 for (cur = orig; cur; cur = cur->next) {
2233 const gchar *cur_tag = tags_get_tag(GPOINTER_TO_INT(cur->data));
2234 if (!cur_tag)
2235 continue;
2236 uri = g_new0(ClickableText, 1);
2237 uri->uri = g_strdup("");
2238 uri->start = gtk_text_iter_get_offset(&iter);
2239 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter,
2240 cur_tag, -1,
2241 "link", "header", "tags", NULL);
2242 uri->end = gtk_text_iter_get_offset(&iter);
2243 uri->filename = g_strdup_printf("sc://search_tags:%s", cur_tag);
2244 uri->data = NULL;
2245 textview->uri_list =
2246 g_slist_prepend(textview->uri_list, uri);
2247 if (cur->next && tags_get_tag(GPOINTER_TO_INT(cur->next->data)))
2248 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, ", ", 2,
2249 "header", "tags", NULL);
2250 else
2251 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, " ", 1,
2252 "header", "tags", NULL);
2254 g_slist_free(orig);
2256 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, "\n", 1,
2257 "header", "tags", NULL);
2260 static void textview_show_header(TextView *textview, GPtrArray *headers)
2262 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
2263 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
2264 GtkTextIter iter;
2265 Header *header;
2266 gint i;
2268 cm_return_if_fail(headers != NULL);
2270 for (i = 0; i < headers->len; i++) {
2271 header = g_ptr_array_index(headers, i);
2272 cm_return_if_fail(header->name != NULL);
2274 gtk_text_buffer_get_end_iter (buffer, &iter);
2275 if(prefs_common.trans_hdr == TRUE) {
2276 gchar *hdr = g_strndup(header->name, strlen(header->name) - 1);
2277 gchar *trans_hdr = gettext(hdr);
2278 gtk_text_buffer_insert_with_tags_by_name(buffer,
2279 &iter, trans_hdr, -1,
2280 "header_title", "header", NULL);
2281 gtk_text_buffer_insert_with_tags_by_name(buffer,
2282 &iter, ":", 1, "header_title", "header", NULL);
2283 g_free(hdr);
2284 } else {
2285 gtk_text_buffer_insert_with_tags_by_name(buffer,
2286 &iter, header->name,
2287 -1, "header_title", "header", NULL);
2289 if (header->name[strlen(header->name) - 1] != ' ')
2290 gtk_text_buffer_insert_with_tags_by_name
2291 (buffer, &iter, " ", 1,
2292 "header_title", "header", NULL);
2294 if (procheader_headername_equal(header->name, "Subject") ||
2295 procheader_headername_equal(header->name, "From") ||
2296 procheader_headername_equal(header->name, "To") ||
2297 procheader_headername_equal(header->name, "Cc") ||
2298 procheader_headername_equal(header->name, "Bcc") ||
2299 procheader_headername_equal(header->name, "Reply-To") ||
2300 procheader_headername_equal(header->name, "Sender") ||
2301 procheader_headername_equal(header->name, "Resent-From") ||
2302 procheader_headername_equal(header->name, "Resent-To"))
2303 unfold_line(header->body);
2305 if (procheader_headername_equal(header->name, "Date") &&
2306 prefs_common.msgview_date_format) {
2307 gchar hbody[80];
2309 procheader_date_parse(hbody, header->body, sizeof(hbody));
2310 gtk_text_buffer_get_end_iter (buffer, &iter);
2311 gtk_text_buffer_insert_with_tags_by_name
2312 (buffer, &iter, hbody, -1, "header", NULL);
2313 } else if ((procheader_headername_equal(header->name, "X-Mailer") ||
2314 procheader_headername_equal(header->name,
2315 "X-Newsreader")) &&
2316 (strstr(header->body, "Claws Mail") != NULL ||
2317 strstr(header->body, "Sylpheed-Claws") != NULL)) {
2318 gtk_text_buffer_get_end_iter (buffer, &iter);
2319 gtk_text_buffer_insert_with_tags_by_name
2320 (buffer, &iter, header->body, -1,
2321 "header", "emphasis", NULL);
2322 } else {
2323 gboolean hdr =
2324 procheader_headername_equal(header->name, "From") ||
2325 procheader_headername_equal(header->name, "To") ||
2326 procheader_headername_equal(header->name, "Cc") ||
2327 procheader_headername_equal(header->name, "Bcc") ||
2328 procheader_headername_equal(header->name, "Reply-To") ||
2329 procheader_headername_equal(header->name, "Sender") ||
2330 procheader_headername_equal(header->name, "Resent-From") ||
2331 procheader_headername_equal(header->name, "Resent-To");
2332 textview_make_clickable_parts(textview, "header",
2333 "hlink", header->body,
2334 hdr);
2336 gtk_text_buffer_get_end_iter (buffer, &iter);
2337 gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, "\n", 1,
2338 "header", NULL);
2341 textview_show_avatar(textview);
2342 if (prefs_common.save_xface)
2343 textview_save_contact_pic(textview);
2344 textview_show_contact_pic(textview);
2347 gboolean textview_search_string(TextView *textview, const gchar *str,
2348 gboolean case_sens)
2350 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
2352 return gtkut_text_view_search_string(text, str, case_sens);
2355 gboolean textview_search_string_backward(TextView *textview, const gchar *str,
2356 gboolean case_sens)
2358 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
2360 return gtkut_text_view_search_string_backward(text, str, case_sens);
2363 void textview_scroll_one_line(TextView *textview, gboolean up)
2365 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
2366 GtkAdjustment *vadj = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(text));
2368 gtkutils_scroll_one_line(GTK_WIDGET(text), vadj, up);
2371 gboolean textview_scroll_page(TextView *textview, gboolean up)
2373 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
2374 GtkAdjustment *vadj = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(text));
2376 return gtkutils_scroll_page(GTK_WIDGET(text), vadj, up);
2379 void textview_scroll_max(TextView *textview, gboolean up)
2381 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
2382 GtkTextIter iter;
2384 if (up) {
2385 gtk_text_buffer_get_start_iter(buffer, &iter);
2386 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(textview->text),
2387 &iter, 0.0, TRUE, 0.0, 1.0);
2389 } else {
2390 gtk_text_buffer_get_end_iter(buffer, &iter);
2391 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(textview->text),
2392 &iter, 0.0, TRUE, 0.0, 0.0);
2396 #define KEY_PRESS_EVENT_STOP() \
2397 g_signal_stop_emission_by_name(G_OBJECT(widget), \
2398 "key_press_event");
2400 static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
2401 TextView *textview)
2403 GdkWindow *window = NULL;
2404 SummaryView *summaryview = NULL;
2405 MessageView *messageview = textview->messageview;
2406 gboolean mod_pressed;
2408 if (!event) return FALSE;
2409 if (messageview->mainwin)
2410 summaryview = messageview->mainwin->summaryview;
2412 switch (event->keyval) {
2413 case GDK_KEY_Tab:
2414 case GDK_KEY_Left:
2415 case GDK_KEY_Up:
2416 case GDK_KEY_Right:
2417 case GDK_KEY_Down:
2418 case GDK_KEY_Control_L:
2419 case GDK_KEY_Control_R:
2420 return FALSE;
2421 case GDK_KEY_Home:
2422 case GDK_KEY_End:
2423 textview_scroll_max(textview,(event->keyval == GDK_KEY_Home));
2424 return TRUE;
2425 case GDK_KEY_space:
2426 mod_pressed = ((event->state & (GDK_SHIFT_MASK|GDK_MOD1_MASK)) != 0);
2427 if (!mimeview_scroll_page(messageview->mimeview, mod_pressed) &&
2428 summaryview != NULL) {
2429 if (mod_pressed)
2430 summary_select_prev_unread(summaryview);
2431 else
2432 summary_select_next_unread(summaryview);
2434 break;
2435 case GDK_KEY_Page_Down:
2436 mimeview_scroll_page(messageview->mimeview, FALSE);
2437 break;
2438 case GDK_KEY_Page_Up:
2439 case GDK_KEY_BackSpace:
2440 mimeview_scroll_page(messageview->mimeview, TRUE);
2441 break;
2442 case GDK_KEY_Return:
2443 case GDK_KEY_KP_Enter:
2444 mimeview_scroll_one_line
2445 (messageview->mimeview, (event->state &
2446 (GDK_SHIFT_MASK|GDK_MOD1_MASK)) != 0);
2447 break;
2448 case GDK_KEY_Delete:
2449 if (summaryview)
2450 summary_pass_key_press_event(summaryview, event);
2451 break;
2452 default:
2453 if (messageview->mainwin) {
2454 window = gtk_widget_get_window(messageview->mainwin->window);
2455 if (summaryview &&
2456 event->window != window) {
2457 GdkEventKey tmpev = *event;
2459 tmpev.window = window;
2460 KEY_PRESS_EVENT_STOP();
2461 gtk_widget_event(messageview->mainwin->window,
2462 (GdkEvent *)&tmpev);
2465 break;
2468 return TRUE;
2471 static gboolean textview_motion_notify(GtkWidget *widget,
2472 GdkEventMotion *event,
2473 TextView *textview)
2475 if (textview->loading)
2476 return FALSE;
2477 textview_uri_update(textview, event->x, event->y);
2479 return FALSE;
2482 static gboolean textview_leave_notify(GtkWidget *widget,
2483 GdkEventCrossing *event,
2484 TextView *textview)
2486 if (textview->loading)
2487 return FALSE;
2488 textview_uri_update(textview, -1, -1);
2490 return FALSE;
2493 static gboolean textview_visibility_notify(GtkWidget *widget,
2494 GdkEventVisibility *event,
2495 TextView *textview)
2497 gint wx, wy;
2498 GdkWindow *window;
2499 GdkDisplay *display;
2500 GdkSeat *seat;
2502 if (textview->loading)
2503 return FALSE;
2505 window = gtk_text_view_get_window(GTK_TEXT_VIEW(widget),
2506 GTK_TEXT_WINDOW_TEXT);
2508 /* check if occurred for the text window part */
2509 if (window != event->window)
2510 return FALSE;
2512 display = gdk_window_get_display(window);
2513 seat = gdk_display_get_default_seat(display);
2514 gdk_device_get_position(gdk_seat_get_pointer(seat),
2515 NULL, &wx, &wy);
2516 textview_uri_update(textview, wx, wy);
2518 return FALSE;
2521 void textview_cursor_wait(TextView *textview)
2523 GdkWindow *window = gtk_text_view_get_window(
2524 GTK_TEXT_VIEW(textview->text),
2525 GTK_TEXT_WINDOW_TEXT);
2526 textview_set_cursor(window, watch_cursor);
2529 void textview_cursor_normal(TextView *textview)
2531 GdkWindow *window = gtk_text_view_get_window(
2532 GTK_TEXT_VIEW(textview->text),
2533 GTK_TEXT_WINDOW_TEXT);
2534 textview_set_cursor(window, text_cursor);
2537 static void textview_uri_update(TextView *textview, gint x, gint y)
2539 GtkTextBuffer *buffer;
2540 GtkTextIter start_iter, end_iter;
2541 ClickableText *uri = NULL;
2543 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
2545 if (x != -1 && y != -1) {
2546 gint bx, by;
2547 GtkTextIter iter;
2548 GSList *tags;
2549 GSList *cur;
2551 gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(textview->text),
2552 GTK_TEXT_WINDOW_WIDGET,
2553 x, y, &bx, &by);
2554 gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(textview->text),
2555 &iter, bx, by);
2557 tags = gtk_text_iter_get_tags(&iter);
2558 for (cur = tags; cur != NULL; cur = cur->next) {
2559 GtkTextTag *tag = cur->data;
2560 char *name;
2562 g_object_get(G_OBJECT(tag), "name", &name, NULL);
2564 if ((!strcmp(name, "link") || !strcmp(name, "hlink"))
2565 && textview_get_uri_range(textview, &iter, tag,
2566 &start_iter, &end_iter)) {
2568 uri = textview_get_uri_from_range(textview,
2569 &iter, tag,
2570 &start_iter,
2571 &end_iter);
2573 g_free(name);
2574 if (uri)
2575 break;
2577 g_slist_free(tags);
2580 if (uri != textview->uri_hover) {
2581 GdkWindow *window;
2583 if (textview->uri_hover)
2584 gtk_text_buffer_remove_tag_by_name(buffer,
2585 "link-hover",
2586 &textview->uri_hover_start_iter,
2587 &textview->uri_hover_end_iter);
2589 textview->uri_hover = uri;
2590 if (uri) {
2591 textview->uri_hover_start_iter = start_iter;
2592 textview->uri_hover_end_iter = end_iter;
2595 window = gtk_text_view_get_window(GTK_TEXT_VIEW(textview->text),
2596 GTK_TEXT_WINDOW_TEXT);
2597 if (textview->messageview->mainwin->cursor_count == 0) {
2598 textview_set_cursor(window, uri ? hand_cursor : text_cursor);
2599 } else {
2600 textview_set_cursor(window, watch_cursor);
2603 TEXTVIEW_STATUSBAR_POP(textview);
2605 if (uri) {
2606 if (!uri->is_quote)
2607 gtk_text_buffer_apply_tag_by_name(buffer,
2608 "link-hover",
2609 &start_iter,
2610 &end_iter);
2611 TEXTVIEW_STATUSBAR_PUSH(textview, uri->uri);
2616 static void textview_set_font_zoom(TextView *textview)
2618 PangoFontDescription *font;
2619 gint size;
2621 /* do nothing if no zoom level has been set */
2622 if (textview_font_size_percent == TEXTVIEW_FONT_SIZE_UNSET)
2623 return;
2625 font = pango_font_description_from_string
2626 (prefs_common.textfont);
2627 cm_return_if_fail(font);
2629 if (textview_font_size_default == TEXTVIEW_FONT_SIZE_UNSET)
2630 textview_font_size_default = pango_font_description_get_size(font);
2632 size = textview_font_size_default + ( textview_font_size_default / 100 * textview_font_size_percent );
2634 pango_font_description_set_size(font, size);
2635 gtk_widget_override_font(textview->text, font);
2636 pango_font_description_free(font);
2639 static void textview_zoom(GtkWidget *widget, gboolean zoom_in)
2641 PangoContext *pctx;
2642 PangoFontDescription *font;
2643 gint size;
2645 pctx = gtk_widget_get_pango_context(widget);
2646 font = pango_context_get_font_description(pctx);
2647 size = pango_font_description_get_size(font);
2649 /* save the default font size first time before zooming */
2650 if (textview_font_size_default == TEXTVIEW_FONT_SIZE_UNSET)
2651 textview_font_size_default = size;
2653 if (textview_font_size_percent == TEXTVIEW_FONT_SIZE_UNSET)
2654 textview_font_size_percent = 0;
2656 if (zoom_in) {
2657 if ((textview_font_size_percent + TEXTVIEW_FONT_SIZE_STEP ) <= TEXTVIEW_FONT_SIZE_MAX)
2658 textview_font_size_percent += TEXTVIEW_FONT_SIZE_STEP;
2659 } else {
2660 if ((textview_font_size_percent - TEXTVIEW_FONT_SIZE_STEP ) >= TEXTVIEW_FONT_SIZE_MIN)
2661 textview_font_size_percent -= TEXTVIEW_FONT_SIZE_STEP;
2663 size = textview_font_size_default + ( textview_font_size_default / 100 * textview_font_size_percent );
2665 pango_font_description_set_size(font, size);
2666 gtk_widget_override_font(widget, font);
2667 gtk_widget_show(widget);
2670 static void textview_zoom_in(GtkWidget *widget, gpointer data)
2672 textview_zoom(GTK_WIDGET(data), TRUE);
2675 static void textview_zoom_out(GtkWidget *widget, gpointer data)
2677 textview_zoom(GTK_WIDGET(data), FALSE);
2680 static void textview_zoom_reset(GtkWidget *widget, gpointer data)
2682 PangoContext *pctx;
2683 PangoFontDescription *font;
2684 gint size;
2686 pctx = gtk_widget_get_pango_context(GTK_WIDGET(data));
2687 font = pango_context_get_font_description(pctx);
2689 /* reset and save the value for current session */
2690 if (textview_font_size_default == TEXTVIEW_FONT_SIZE_UNSET || textview_font_size_percent == TEXTVIEW_FONT_SIZE_UNSET)
2691 return;
2693 textview_font_size_percent = 0;
2694 size = textview_font_size_default + ( textview_font_size_default / 100 * textview_font_size_percent );
2696 pango_font_description_set_size(font, size);
2697 gtk_widget_override_font(GTK_WIDGET(data), font);
2698 gtk_widget_show(GTK_WIDGET(data));
2701 static void textview_populate_popup(GtkTextView* textview,
2702 GtkMenu *menu,
2703 gpointer user_data)
2705 GtkWidget *menuitem;
2707 cm_return_if_fail(menu != NULL);
2708 cm_return_if_fail(GTK_IS_MENU_SHELL(menu));
2710 menuitem = gtk_separator_menu_item_new();
2711 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
2712 gtk_widget_show(menuitem);
2714 menuitem = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
2715 g_signal_connect(G_OBJECT(menuitem), "activate",
2716 G_CALLBACK(textview_zoom_in), textview);
2717 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
2718 gtk_widget_set_sensitive(menuitem,
2719 textview_font_size_percent == TEXTVIEW_FONT_SIZE_UNSET ||
2720 ((textview_font_size_percent + TEXTVIEW_FONT_SIZE_STEP ) <= TEXTVIEW_FONT_SIZE_MAX));
2721 gtk_widget_show(menuitem);
2723 menuitem = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
2724 g_signal_connect(G_OBJECT(menuitem), "activate",
2725 G_CALLBACK(textview_zoom_out), textview);
2726 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
2727 gtk_widget_set_sensitive(menuitem,
2728 textview_font_size_percent == TEXTVIEW_FONT_SIZE_UNSET ||
2729 ((textview_font_size_percent - TEXTVIEW_FONT_SIZE_STEP ) >= TEXTVIEW_FONT_SIZE_MIN));
2730 gtk_widget_show(menuitem);
2732 menuitem = gtk_menu_item_new_with_mnemonic(_("Reset _zoom"));
2733 g_signal_connect(G_OBJECT(menuitem), "activate",
2734 G_CALLBACK(textview_zoom_reset), textview);
2735 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
2736 gtk_widget_set_sensitive(menuitem,
2737 (textview_font_size_percent != TEXTVIEW_FONT_SIZE_UNSET && textview_font_size_percent != 0));
2738 gtk_widget_show(menuitem);
2741 static gboolean textview_scrolled(GtkWidget *widget,
2742 GdkEvent *_event,
2743 gpointer user_data)
2745 GdkEventScroll *event = (GdkEventScroll *)_event;
2747 /* Ctrl+mousewheel changes font size */
2748 if (event->state & GDK_CONTROL_MASK || event->state & GDK_SHIFT_MASK) {
2750 if (event->direction == GDK_SCROLL_UP) {
2751 textview_zoom(widget, TRUE);
2752 } else if (event->direction == GDK_SCROLL_DOWN) {
2753 textview_zoom(widget, FALSE);
2754 } else {
2755 gdouble x, y;
2757 if ((event->direction == GDK_SCROLL_SMOOTH) &&
2758 gdk_event_get_scroll_deltas((GdkEvent *)event, &x, &y)) {
2759 if (y < 0)
2760 textview_zoom(widget, TRUE);
2761 else
2762 if (y >0)
2763 textview_zoom(widget, FALSE);
2764 } else
2765 return FALSE; /* Scrolling left or right */
2767 return TRUE;
2770 return FALSE;
2773 static gboolean textview_get_uri_range(TextView *textview,
2774 GtkTextIter *iter,
2775 GtkTextTag *tag,
2776 GtkTextIter *start_iter,
2777 GtkTextIter *end_iter)
2779 return get_tag_range(iter, tag, start_iter, end_iter);
2782 static ClickableText *textview_get_uri_from_range(TextView *textview,
2783 GtkTextIter *iter,
2784 GtkTextTag *tag,
2785 GtkTextIter *start_iter,
2786 GtkTextIter *end_iter)
2788 gint start_pos, end_pos, cur_pos;
2789 ClickableText *uri = NULL;
2790 GSList *cur;
2792 start_pos = gtk_text_iter_get_offset(start_iter);
2793 end_pos = gtk_text_iter_get_offset(end_iter);
2794 cur_pos = gtk_text_iter_get_offset(iter);
2796 for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
2797 ClickableText *uri_ = (ClickableText *)cur->data;
2798 if (start_pos == uri_->start &&
2799 end_pos == uri_->end) {
2800 uri = uri_;
2801 break;
2804 for (cur = textview->uri_list; uri == NULL && cur != NULL; cur = cur->next) {
2805 ClickableText *uri_ = (ClickableText *)cur->data;
2806 if (start_pos == uri_->start ||
2807 end_pos == uri_->end) {
2808 /* in case of contiguous links, textview_get_uri_range
2809 * returns a broader range (start of 1st link to end
2810 * of last link).
2811 * In that case, correct link is the one covering
2812 * current iter.
2814 if (uri_->start <= cur_pos && cur_pos <= uri_->end) {
2815 uri = uri_;
2816 break;
2821 return uri;
2824 static ClickableText *textview_get_uri(TextView *textview,
2825 GtkTextIter *iter,
2826 GtkTextTag *tag)
2828 GtkTextIter start_iter, end_iter;
2829 ClickableText *uri = NULL;
2831 if (textview_get_uri_range(textview, iter, tag, &start_iter,
2832 &end_iter))
2833 uri = textview_get_uri_from_range(textview, iter, tag,
2834 &start_iter, &end_iter);
2836 return uri;
2839 static void textview_shift_uris_after(TextView *textview, GSList *start_list, gint start, gint shift)
2841 GSList *cur;
2842 if (!start_list)
2843 start_list = textview->uri_list;
2845 for (cur = start_list; cur; cur = cur->next) {
2846 ClickableText *uri = (ClickableText *)cur->data;
2847 if (uri->start <= start)
2848 continue;
2849 uri->start += shift;
2850 uri->end += shift;
2854 static void textview_remove_uris_in(TextView *textview, gint start, gint end)
2856 GSList *cur;
2857 for (cur = textview->uri_list; cur; ) {
2858 ClickableText *uri = (ClickableText *)cur->data;
2859 if (uri->start > start && uri->end < end) {
2860 cur = cur->next;
2861 textview->uri_list = g_slist_remove(textview->uri_list, uri);
2862 g_free(uri->uri);
2863 g_free(uri->filename);
2864 if (uri->is_quote) {
2865 g_free(uri->fg_color);
2866 g_free(uri->data);
2867 /* (only free data in quotes uris) */
2869 g_free(uri);
2870 } else {
2871 cur = cur->next;
2877 static void textview_toggle_quote(TextView *textview, GSList *start_list, ClickableText *uri, gboolean expand_only)
2879 GtkTextIter start, end;
2880 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
2882 if (!uri->is_quote)
2883 return;
2885 if (uri->q_expanded && expand_only)
2886 return;
2888 gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
2889 gtk_text_buffer_get_iter_at_offset(buffer, &end, uri->end);
2890 if (textview->uri_hover)
2891 gtk_text_buffer_remove_tag_by_name(buffer,
2892 "link-hover",
2893 &textview->uri_hover_start_iter,
2894 &textview->uri_hover_end_iter);
2895 textview->uri_hover = NULL;
2896 gtk_text_buffer_remove_tag_by_name(buffer,
2897 "qlink",
2898 &start,
2899 &end);
2900 /* when shifting URIs start and end, we have to do it per-UTF8-char
2901 * so use g_utf8_strlen(). OTOH, when inserting in the text buffer,
2902 * we have to pass a number of bytes, so use strlen(). disturbing. */
2904 if (!uri->q_expanded) {
2905 gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
2906 gtk_text_buffer_get_iter_at_offset(buffer, &end, uri->end);
2907 textview_shift_uris_after(textview, start_list, uri->start,
2908 g_utf8_strlen((gchar *)uri->data, -1)-strlen(" [...]\n"));
2909 gtk_text_buffer_delete(buffer, &start, &end);
2910 gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
2911 gtk_text_buffer_insert_with_tags_by_name
2912 (buffer, &start, (gchar *)uri->data,
2913 strlen((gchar *)uri->data)-1,
2914 "qlink", (gchar *)uri->fg_color, NULL);
2915 uri->end = gtk_text_iter_get_offset(&start);
2916 textview_make_clickable_parts_later(textview,
2917 uri->start, uri->end);
2918 uri->q_expanded = TRUE;
2919 } else {
2920 gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
2921 gtk_text_buffer_get_iter_at_offset(buffer, &end, uri->end);
2922 textview_remove_uris_in(textview, uri->start, uri->end);
2923 textview_shift_uris_after(textview, start_list, uri->start,
2924 strlen(" [...]\n")-g_utf8_strlen((gchar *)uri->data, -1));
2925 gtk_text_buffer_delete(buffer, &start, &end);
2926 gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
2927 gtk_text_buffer_insert_with_tags_by_name
2928 (buffer, &start, " [...]", -1,
2929 "qlink", (gchar *)uri->fg_color, NULL);
2930 uri->end = gtk_text_iter_get_offset(&start);
2931 uri->q_expanded = FALSE;
2933 if (textview->messageview->mainwin->cursor_count == 0) {
2934 textview_cursor_normal(textview);
2935 } else {
2936 textview_cursor_wait(textview);
2939 static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
2940 GdkEvent *event, GtkTextIter *iter,
2941 TextView *textview)
2943 GdkEventButton *bevent;
2944 ClickableText *uri = NULL;
2945 char *tagname;
2946 gboolean qlink = FALSE;
2948 if (!event)
2949 return FALSE;
2951 if (event->type != GDK_BUTTON_PRESS && event->type != GDK_2BUTTON_PRESS
2952 && event->type != GDK_MOTION_NOTIFY)
2953 return FALSE;
2955 uri = textview_get_uri(textview, iter, tag);
2956 if (!uri)
2957 return FALSE;
2959 g_object_get(G_OBJECT(tag), "name", &tagname, NULL);
2961 if (!strcmp(tagname, "qlink"))
2962 qlink = TRUE;
2964 g_free(tagname);
2966 bevent = (GdkEventButton *) event;
2968 /* doubleclick: open compose / add address / browser */
2969 if (qlink && event->type == GDK_BUTTON_PRESS && bevent->button != 1) {
2970 /* pass rightclick through */
2971 return FALSE;
2972 } else if ((event->type == (qlink ? GDK_2BUTTON_PRESS:GDK_BUTTON_PRESS) && bevent->button == 1) ||
2973 bevent->button == 2 || bevent->button == 3) {
2974 if (uri->filename && !g_ascii_strncasecmp(uri->filename, "sc://", 5)) {
2975 MimeView *mimeview =
2976 (textview->messageview)?
2977 textview->messageview->mimeview:NULL;
2978 if (mimeview && bevent->button == 1) {
2979 mimeview_handle_cmd(mimeview, uri->filename, NULL, uri->data);
2980 } else if (mimeview && bevent->button == 2 &&
2981 !g_ascii_strcasecmp(uri->filename, "sc://select_attachment")) {
2982 mimeview_handle_cmd(mimeview, "sc://open_attachment", NULL, uri->data);
2983 } else if (mimeview && bevent->button == 3 &&
2984 !g_ascii_strcasecmp(uri->filename, "sc://select_attachment")) {
2985 mimeview_handle_cmd(mimeview, "sc://menu_attachment", bevent, uri->data);
2987 return TRUE;
2988 } else if (qlink && bevent->button == 1) {
2989 if (prefs_common.hide_quoted) {
2990 textview_toggle_quote(textview, NULL, uri, FALSE);
2991 return TRUE;
2992 } else
2993 return FALSE;
2994 } else if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
2995 if (bevent->button == 3) {
2996 g_object_set_data(
2997 G_OBJECT(textview->mail_popup_menu),
2998 "menu_button", uri);
2999 gtk_menu_popup_at_pointer(GTK_MENU(textview->mail_popup_menu), NULL);
3000 } else {
3001 PrefsAccount *account = NULL;
3002 FolderItem *folder_item = NULL;
3003 Compose *compose;
3005 if (textview->messageview && textview->messageview->msginfo &&
3006 textview->messageview->msginfo->folder) {
3009 folder_item = textview->messageview->msginfo->folder;
3010 if (folder_item->prefs && folder_item->prefs->enable_default_account)
3011 account = account_find_from_id(folder_item->prefs->default_account);
3012 if (!account)
3013 account = account_find_from_item(folder_item);
3015 compose = compose_new_with_folderitem(account,
3016 folder_item, uri->uri + 7);
3017 compose_check_for_email_account(compose);
3019 return TRUE;
3020 } else if (g_ascii_strncasecmp(uri->uri, "file:", 5)) {
3021 if (bevent->button == 1 &&
3022 textview_uri_security_check(textview, uri, FALSE) == TRUE)
3023 open_uri(uri->uri,
3024 prefs_common_get_uri_cmd());
3025 else if (bevent->button == 3 && !qlink) {
3026 g_object_set_data(
3027 G_OBJECT(textview->link_popup_menu),
3028 "menu_button", uri);
3029 gtk_menu_popup_at_pointer(GTK_MENU(textview->link_popup_menu), NULL);
3031 return TRUE;
3032 } else {
3033 if (bevent->button == 3 && !qlink) {
3034 g_object_set_data(
3035 G_OBJECT(textview->file_popup_menu),
3036 "menu_button", uri);
3037 gtk_menu_popup_at_pointer(GTK_MENU(textview->file_popup_menu), NULL);
3038 return TRUE;
3043 return FALSE;
3046 gchar *textview_get_visible_uri (TextView *textview,
3047 ClickableText *uri)
3049 GtkTextBuffer *buffer;
3050 GtkTextIter start, end;
3052 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
3054 gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
3055 gtk_text_buffer_get_iter_at_offset(buffer, &end, uri->end);
3057 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
3061 *\brief Check to see if a web URL has been disguised as a different
3062 * URL (possible with HTML email).
3064 *\param uri The uri to check
3066 *\param textview The TextView the URL is contained in
3068 *\return gboolean TRUE if the URL is ok, or if the user chose to open
3069 * it anyway, otherwise FALSE
3071 gboolean textview_uri_security_check(TextView *textview, ClickableText *uri, gboolean copied)
3073 gchar *visible_str;
3074 gboolean retval = TRUE;
3076 if (is_uri_string(uri->uri) == FALSE)
3077 return FALSE;
3079 visible_str = textview_get_visible_uri(textview, uri);
3080 if (visible_str == NULL)
3081 return TRUE;
3083 g_strstrip(visible_str);
3085 if (strcmp(visible_str, uri->uri) != 0 && is_uri_string(visible_str)) {
3086 gchar *uri_path;
3087 gchar *visible_uri_path;
3089 uri_path = get_uri_path(uri->uri);
3090 visible_uri_path = get_uri_path(visible_str);
3091 if (path_cmp(uri_path, visible_uri_path) != 0)
3092 retval = FALSE;
3095 if (retval == FALSE) {
3096 gchar *open_or_cp;
3097 gchar *open_or_cp_btn;
3098 gchar *msg;
3099 AlertValue aval;
3101 open_or_cp = copied? _("Copy it anyway?") : _("Open it anyway?");
3102 open_or_cp_btn = copied? _("Co_py URL") : _("_Open URL");
3104 msg = g_markup_printf_escaped("%s\n\n"
3105 "<b>%s</b> %s\n\n"
3106 "<b>%s</b> %s\n\n"
3107 "%s",
3108 _("The real URL is different from the displayed URL."),
3109 _("Displayed URL:"), visible_str,
3110 _("Real URL:"), uri->uri,
3111 open_or_cp);
3112 aval = alertpanel_full(_("Phishing attempt warning"), msg,
3113 NULL, _("_Cancel"), NULL, open_or_cp_btn, NULL, NULL,
3114 ALERTFOCUS_FIRST, FALSE, NULL, ALERT_WARNING);
3115 g_free(msg);
3116 if (aval == G_ALERTALTERNATE)
3117 retval = TRUE;
3119 if (strlen(uri->uri) > get_uri_len(uri->uri))
3120 retval = FALSE;
3122 g_free(visible_str);
3124 return retval;
3127 static void textview_uri_list_remove_all(GSList *uri_list)
3129 GSList *cur;
3131 for (cur = uri_list; cur != NULL; cur = cur->next) {
3132 if (cur->data) {
3133 g_free(((ClickableText *)cur->data)->uri);
3134 g_free(((ClickableText *)cur->data)->filename);
3135 if (((ClickableText *)cur->data)->is_quote) {
3136 g_free(((ClickableText *)cur->data)->fg_color);
3137 g_free(((ClickableText *)cur->data)->data);
3138 /* (only free data in quotes uris) */
3140 g_free(cur->data);
3144 g_slist_free(uri_list);
3147 static void open_uri_cb (GtkAction *action, TextView *textview)
3149 ClickableText *uri = g_object_get_data(G_OBJECT(textview->link_popup_menu),
3150 "menu_button");
3151 const gchar *raw_url = g_object_get_data(G_OBJECT(textview->link_popup_menu),
3152 "raw_url");
3154 if (uri) {
3155 if (textview_uri_security_check(textview, uri, FALSE) == TRUE)
3156 open_uri(uri->uri,
3157 prefs_common_get_uri_cmd());
3158 g_object_set_data(G_OBJECT(textview->link_popup_menu), "menu_button",
3159 NULL);
3161 if (raw_url) {
3162 open_uri(raw_url, prefs_common_get_uri_cmd());
3163 g_object_set_data(G_OBJECT(textview->link_popup_menu), "raw_url",
3164 NULL);
3168 static void copy_uri_cb (GtkAction *action, TextView *textview)
3170 ClickableText *uri = g_object_get_data(G_OBJECT(textview->link_popup_menu),
3171 "menu_button");
3172 const gchar *raw_url = g_object_get_data(G_OBJECT(textview->link_popup_menu),
3173 "raw_url");
3174 if (uri) {
3175 if (textview_uri_security_check(textview, uri, TRUE) == TRUE) {
3176 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), uri->uri, -1);
3177 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), uri->uri, -1);
3178 g_object_set_data(G_OBJECT(textview->link_popup_menu), "menu_button", NULL);
3181 if (raw_url) {
3182 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), raw_url, -1);
3183 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), raw_url, -1);
3184 g_object_set_data(G_OBJECT(textview->link_popup_menu), "raw_url", NULL);
3188 static void add_uri_to_addrbook_cb (GtkAction *action, TextView *textview)
3190 gchar *fromname, *fromaddress;
3191 ClickableText *uri = g_object_get_data(G_OBJECT(textview->mail_popup_menu),
3192 "menu_button");
3193 AvatarRender *avatarr = NULL;
3194 GdkPixbuf *picture = NULL;
3195 gboolean use_picture = FALSE;
3197 if (uri == NULL)
3198 return;
3200 /* extract url */
3201 fromaddress = g_strdup(uri->uri + 7);
3203 if (textview->messageview->msginfo &&
3204 !g_strcmp0(fromaddress, textview->messageview->msginfo->from))
3205 use_picture = TRUE;
3207 fromname = procheader_get_fromname(fromaddress);
3208 extract_address(fromaddress);
3210 if (use_picture) {
3211 avatarr = avatars_avatarrender_new(textview->messageview->msginfo);
3212 hooks_invoke(AVATAR_IMAGE_RENDER_HOOKLIST, avatarr);
3215 if (avatarr && avatarr->image) {
3216 picture = gtk_image_get_pixbuf(GTK_IMAGE(avatarr->image));
3218 if (avatarr) {
3219 avatars_avatarrender_free(avatarr);
3222 #ifndef USE_ALT_ADDRBOOK
3223 addressbook_add_contact( fromname, fromaddress, NULL, picture);
3224 #else
3225 if (addressadd_selection(fromname, fromaddress, NULL, picture)) {
3226 debug_print( "addressbook_add_contact - added\n" );
3228 #endif
3230 g_free(fromaddress);
3231 g_free(fromname);
3234 static void reply_to_uri_cb (GtkAction *action, TextView *textview)
3236 ClickableText *uri = g_object_get_data(G_OBJECT(textview->mail_popup_menu),
3237 "menu_button");
3238 if (!textview->messageview || !uri)
3239 return;
3241 compose_reply_to_address (textview->messageview,
3242 textview->messageview->msginfo, uri->uri+7);
3245 static void mail_to_uri_cb (GtkAction *action, TextView *textview)
3247 PrefsAccount *account = NULL;
3248 Compose *compose;
3249 ClickableText *uri = g_object_get_data(G_OBJECT(textview->mail_popup_menu),
3250 "menu_button");
3251 if (uri == NULL)
3252 return;
3254 if (textview->messageview && textview->messageview->msginfo &&
3255 textview->messageview->msginfo->folder) {
3256 FolderItem *folder_item;
3258 folder_item = textview->messageview->msginfo->folder;
3259 if (folder_item->prefs && folder_item->prefs->enable_default_account)
3260 account = account_find_from_id(folder_item->prefs->default_account);
3262 compose = compose_new_with_folderitem(account, folder_item, uri->uri+7);
3263 } else {
3264 compose = compose_new(account, uri->uri + 7, NULL);
3266 compose_check_for_email_account(compose);
3269 static void copy_mail_to_uri_cb (GtkAction *action, TextView *textview)
3271 ClickableText *uri = g_object_get_data(G_OBJECT(textview->mail_popup_menu),
3272 "menu_button");
3273 if (uri == NULL)
3274 return;
3276 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), uri->uri +7, -1);
3277 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), uri->uri +7, -1);
3278 g_object_set_data(G_OBJECT(textview->mail_popup_menu), "menu_button",
3279 NULL);
3282 void textview_get_selection_offsets(TextView *textview, gint *sel_start, gint *sel_end)
3284 GtkTextView *text = GTK_TEXT_VIEW(textview->text);
3285 GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
3286 GtkTextIter start, end;
3287 if (gtk_text_buffer_get_selection_bounds(buffer, &start, &end)) {
3288 if (sel_start)
3289 *sel_start = gtk_text_iter_get_offset(&start);
3290 if (sel_end)
3291 *sel_end = gtk_text_iter_get_offset(&end);
3292 } else {
3293 if (sel_start)
3294 *sel_start = -1;
3295 if (sel_end)
3296 *sel_end = -1;