/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2001 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2004 Hiroyuki Yamamoto
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
#include <gdk/gdkkeysyms.h>
#include <gtk/gtkvbox.h>
#include <gtk/gtkscrolledwindow.h>
-#include <gtk/gtktext.h>
#include <gtk/gtksignal.h>
#include <stdio.h>
#include <ctype.h>
#include "compose.h"
#include "addressbook.h"
#include "displayheader.h"
-
-#define FONT_LOAD(font, s) \
-{ \
- gchar *fontstr, *p; \
- \
- Xstrdup_a(fontstr, s, ); \
- if ((p = strchr(fontstr, ',')) != NULL) *p = '\0'; \
- font = gdk_font_load(fontstr); \
-}
-
-typedef struct _RemoteURI RemoteURI;
+#include "account.h"
+#include "mimeview.h"
+#include "alertpanel.h"
struct _RemoteURI
{
{(gulong)0, (gushort)0, (gushort)0, (gushort)0}
};
+static GdkColor signature_color = {
+ (gulong)0,
+ (gushort)0x7fff,
+ (gushort)0x7fff,
+ (gushort)0x7fff
+};
+
static GdkColor uri_color = {
(gulong)0,
(gushort)0,
(gushort)0xcfff
};
+#if 0
static GdkColor error_color = {
(gulong)0,
(gushort)0xefff,
(gushort)0,
(gushort)0
};
+#endif
+
+
+static GdkCursor *hand_cursor = NULL;
+
+#define TEXTVIEW_STATUSBAR_PUSH(textview, str) \
+{ \
+ gtk_statusbar_push(GTK_STATUSBAR(textview->messageview->statusbar), \
+ textview->messageview->statusbar_cid, str); \
+}
-static GdkFont *spacingfont;
+#define TEXTVIEW_STATUSBAR_POP(textview) \
+{ \
+ gtk_statusbar_pop(GTK_STATUSBAR(textview->messageview->statusbar), \
+ textview->messageview->statusbar_cid); \
+}
static void textview_show_ertf (TextView *textview,
FILE *fp,
CodeConverter *conv);
+static void textview_add_part (TextView *textview,
+ MimeInfo *mimeinfo);
+static void textview_add_parts (TextView *textview,
+ MimeInfo *mimeinfo);
+static void textview_write_body (TextView *textview,
+ MimeInfo *mimeinfo);
static void textview_show_html (TextView *textview,
FILE *fp,
CodeConverter *conv);
+
static void textview_write_line (TextView *textview,
const gchar *str,
CodeConverter *conv);
-static void textview_write_link (TextView *textview,
- const gchar *url,
+static void textview_write_link (TextView *textview,
const gchar *str,
+ const gchar *uri,
CodeConverter *conv);
+
static GPtrArray *textview_scan_header (TextView *textview,
FILE *fp);
static void textview_show_header (TextView *textview,
GPtrArray *headers);
-static void textview_key_pressed (GtkWidget *widget,
- GdkEventKey *event,
- TextView *textview);
-static void textview_button_pressed (GtkWidget *widget,
- GdkEventButton *event,
- TextView *textview);
-
-static void textview_uri_list_remove_all(GSList *uri_list);
+static gint textview_key_pressed (GtkWidget *widget,
+ GdkEventKey *event,
+ TextView *textview);
+static gboolean textview_motion_notify (GtkWidget *widget,
+ GdkEventMotion *motion,
+ TextView *textview);
+static gboolean textview_leave_notify (GtkWidget *widget,
+ GdkEventCrossing *event,
+ TextView *textview);
+static gboolean textview_visibility_notify (GtkWidget *widget,
+ GdkEventVisibility *event,
+ TextView *textview);
+static void textview_uri_update (TextView *textview,
+ gint x,
+ gint y);
+static gboolean textview_get_uri_range (TextView *textview,
+ GtkTextIter *iter,
+ GtkTextTag *tag,
+ GtkTextIter *start_iter,
+ GtkTextIter *end_iter);
+static RemoteURI *textview_get_uri_from_range (TextView *textview,
+ GtkTextIter *iter,
+ GtkTextTag *tag,
+ GtkTextIter *start_iter,
+ GtkTextIter *end_iter);
+static RemoteURI *textview_get_uri (TextView *textview,
+ GtkTextIter *iter,
+ GtkTextTag *tag);
+static gboolean textview_uri_button_pressed (GtkTextTag *tag,
+ GObject *obj,
+ GdkEvent *event,
+ GtkTextIter *iter,
+ TextView *textview);
static void textview_smooth_scroll_do (TextView *textview,
gfloat old_value,
static gboolean textview_smooth_scroll_page (TextView *textview,
gboolean up);
+static gboolean textview_uri_security_check (TextView *textview,
+ RemoteURI *uri);
+static void textview_uri_list_remove_all (GSList *uri_list);
+
+
+static void populate_popup(GtkTextView *textview, GtkMenu *menu,
+ gpointer *dummy)
+{
+ gtk_menu_detach(menu);
+}
TextView *textview_create(void)
{
TextView *textview;
GtkWidget *vbox;
- GtkWidget *scrolledwin_sb;
- GtkWidget *scrolledwin_mb;
- GtkWidget *text_sb;
- GtkWidget *text_mb;
+ GtkWidget *scrolledwin;
+ GtkWidget *text;
+ GtkTextBuffer *buffer;
+ GtkClipboard *clipboard;
+ PangoFontDescription *font_desc = NULL;
- debug_print(_("Creating text view...\n"));
+ debug_print("Creating text view...\n");
textview = g_new0(TextView, 1);
- scrolledwin_sb = gtk_scrolled_window_new(NULL, NULL);
- scrolledwin_mb = gtk_scrolled_window_new(NULL, NULL);
- gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin_sb),
- GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
- gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin_mb),
+ scrolledwin = gtk_scrolled_window_new(NULL, NULL);
+ gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
- gtk_widget_set_usize(scrolledwin_sb, prefs_common.mainview_width, -1);
- gtk_widget_set_usize(scrolledwin_mb, prefs_common.mainview_width, -1);
-
- /* create GtkText widgets for single-byte and multi-byte character */
- text_sb = gtk_text_new(NULL, NULL);
- text_mb = gtk_text_new(NULL, NULL);
- GTK_TEXT(text_sb)->default_tab_width = 8;
- GTK_TEXT(text_mb)->default_tab_width = 8;
- gtk_widget_show(text_sb);
- gtk_widget_show(text_mb);
- gtk_text_set_word_wrap(GTK_TEXT(text_sb), TRUE);
- gtk_text_set_word_wrap(GTK_TEXT(text_mb), TRUE);
- gtk_widget_ensure_style(text_sb);
- gtk_widget_ensure_style(text_mb);
- if (text_sb->style && text_sb->style->font->type == GDK_FONT_FONTSET) {
- GtkStyle *style;
-
- style = gtk_style_copy(text_sb->style);
- gdk_font_unref(style->font);
- FONT_LOAD(style->font, NORMAL_FONT);
- gtk_widget_set_style(text_sb, style);
+ gtk_widget_set_size_request(scrolledwin, prefs_common.mainview_width, -1);
+ gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
+ GTK_SHADOW_IN);
+
+ /* create GtkSText widgets for single-byte and multi-byte character */
+ text = gtk_text_view_new();
+ gtk_widget_add_events(text, GDK_LEAVE_NOTIFY_MASK);
+ gtk_widget_show(text);
+ gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
+ gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD_CHAR);
+ gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(text), FALSE);
+ g_signal_connect(G_OBJECT(text), "populate-popup",
+ G_CALLBACK(populate_popup), NULL);
+
+
+ buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
+ clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
+ gtk_text_buffer_add_selection_clipboard(buffer, clipboard);
+
+ gtk_widget_ensure_style(text);
+
+ if (prefs_common.normalfont)
+ font_desc = pango_font_description_from_string
+ (prefs_common.normalfont);
+ if (font_desc) {
+ gtk_widget_modify_font(text, font_desc);
}
- if (text_mb->style && text_mb->style->font->type == GDK_FONT_FONT) {
- GtkStyle *style;
+ pango_font_description_free(font_desc);
- style = gtk_style_copy(text_mb->style);
- gdk_font_unref(style->font);
- style->font = gdk_fontset_load(NORMAL_FONT);
- gtk_widget_set_style(text_mb, style);
- }
- gtk_widget_ref(scrolledwin_sb);
- gtk_widget_ref(scrolledwin_mb);
-
- gtk_container_add(GTK_CONTAINER(scrolledwin_sb), text_sb);
- gtk_container_add(GTK_CONTAINER(scrolledwin_mb), text_mb);
- gtk_signal_connect(GTK_OBJECT(text_sb), "key_press_event",
- GTK_SIGNAL_FUNC(textview_key_pressed),
- textview);
- gtk_signal_connect_after(GTK_OBJECT(text_sb), "button_press_event",
- GTK_SIGNAL_FUNC(textview_button_pressed),
- textview);
- gtk_signal_connect(GTK_OBJECT(text_mb), "key_press_event",
- GTK_SIGNAL_FUNC(textview_key_pressed),
- textview);
- gtk_signal_connect_after(GTK_OBJECT(text_mb), "button_press_event",
- GTK_SIGNAL_FUNC(textview_button_pressed),
- textview);
-
- gtk_widget_show(scrolledwin_sb);
- gtk_widget_show(scrolledwin_mb);
+ gtk_widget_ref(scrolledwin);
+
+ gtk_container_add(GTK_CONTAINER(scrolledwin), text);
+
+ if (!hand_cursor)
+ hand_cursor = gdk_cursor_new(GDK_HAND2);
+
+ g_signal_connect(G_OBJECT(text), "key_press_event",
+ G_CALLBACK(textview_key_pressed),
+ textview);
+ g_signal_connect(G_OBJECT(text), "motion_notify_event",
+ G_CALLBACK(textview_motion_notify),
+ textview);
+ g_signal_connect(G_OBJECT(text), "leave_notify_event",
+ G_CALLBACK(textview_leave_notify),
+ textview);
+ g_signal_connect(G_OBJECT(text), "visibility_notify_event",
+ G_CALLBACK(textview_visibility_notify),
+ textview);
+
+ gtk_widget_show(scrolledwin);
vbox = gtk_vbox_new(FALSE, 0);
- gtk_box_pack_start(GTK_BOX(vbox), scrolledwin_sb, TRUE, TRUE, 0);
-
- textview->vbox = vbox;
- textview->scrolledwin = scrolledwin_sb;
- textview->scrolledwin_sb = scrolledwin_sb;
- textview->scrolledwin_mb = scrolledwin_mb;
- textview->text = text_sb;
- textview->text_sb = text_sb;
- textview->text_mb = text_mb;
- textview->text_is_mb = FALSE;
- textview->uri_list = NULL;
- textview->body_pos = 0;
- textview->cur_pos = 0;
+ gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
+
+ gtk_widget_show(vbox);
+
+ textview->vbox = vbox;
+ textview->scrolledwin = scrolledwin;
+ textview->text = text;
+ textview->uri_list = NULL;
+ textview->body_pos = 0;
+ textview->show_all_headers = FALSE;
+ textview->last_buttonpress = GDK_NOTHING;
return textview;
}
+static void textview_create_tags(GtkTextView *text, TextView *textview)
+{
+ GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
+ GtkTextTag *tag;
+
+ gtk_text_buffer_create_tag(buffer, "header",
+ "pixels-above-lines", 0,
+ "pixels-above-lines-set", TRUE,
+ "pixels-below-lines", 0,
+ "pixels-below-lines-set", TRUE,
+ "left-margin", 0,
+ "left-margin-set", TRUE,
+ NULL);
+ gtk_text_buffer_create_tag(buffer, "header_title",
+ "font", prefs_common.boldfont,
+ NULL);
+ gtk_text_buffer_create_tag(buffer, "quote0",
+ "foreground-gdk", "e_colors[0],
+ NULL);
+ gtk_text_buffer_create_tag(buffer, "quote1",
+ "foreground-gdk", "e_colors[1],
+ NULL);
+ gtk_text_buffer_create_tag(buffer, "quote2",
+ "foreground-gdk", "e_colors[2],
+ NULL);
+ gtk_text_buffer_create_tag(buffer, "emphasis",
+ "foreground-gdk", &emphasis_color,
+ NULL);
+ gtk_text_buffer_create_tag(buffer, "signature",
+ "foreground-gdk", &signature_color,
+ NULL);
+ tag = gtk_text_buffer_create_tag(buffer, "link",
+ "foreground-gdk", &uri_color,
+ NULL);
+ gtk_text_buffer_create_tag(buffer, "link-hover",
+ "underline", PANGO_UNDERLINE_SINGLE,
+ NULL);
+
+ g_signal_connect(G_OBJECT(tag), "event",
+ G_CALLBACK(textview_uri_button_pressed), textview);
+ }
+
void textview_init(TextView *textview)
{
- gtkut_widget_disable_theme_engine(textview->text_sb);
- gtkut_widget_disable_theme_engine(textview->text_mb);
+ gtkut_widget_disable_theme_engine(textview->text);
textview_update_message_colors();
+ textview_set_all_headers(textview, FALSE);
textview_set_font(textview, NULL);
+
+ textview_create_tags(GTK_TEXT_VIEW(textview->text), textview);
}
void textview_update_message_colors(void)
"e_colors[2]);
gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
&uri_color);
+ gtkut_convert_int_to_gdk_color(prefs_common.signature_col,
+ &signature_color);
} else {
quote_colors[0] = quote_colors[1] = quote_colors[2] =
- uri_color = emphasis_color = black;
+ uri_color = emphasis_color = signature_color = black;
}
}
{
FILE *fp;
- if ((fp = fopen(file, "r")) == NULL) {
+ if ((fp = fopen(file, "rb")) == NULL) {
FILE_OP_ERROR(file, "fopen");
return;
}
- textview_show_part(textview, mimeinfo, fp);
+ textview_clear(textview);
+
+ textview_add_parts(textview, mimeinfo);
fclose(fp);
+
+ textview_set_position(textview, 0);
}
void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
{
- GtkText *text;
- gchar buf[BUFFSIZE];
- const gchar *boundary = NULL;
- gint boundary_len = 0;
- const gchar *charset = NULL;
- FILE *tmpfp;
- GPtrArray *headers = NULL;
- CodeConverter *conv;
+ GtkTextView *text;
g_return_if_fail(mimeinfo != NULL);
g_return_if_fail(fp != NULL);
- if (mimeinfo->mime_type == MIME_MULTIPART) {
- if (mimeinfo->sub) {
- mimeinfo = mimeinfo->sub;
- if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0) {
- perror("fseek");
- return;
- }
- } else
- return;
- }
- if (mimeinfo->parent && mimeinfo->parent->boundary) {
- boundary = mimeinfo->parent->boundary;
- boundary_len = strlen(boundary);
+ if ((mimeinfo->type == MIMETYPE_MULTIPART) ||
+ ((mimeinfo->type == MIMETYPE_MESSAGE) && !g_ascii_strcasecmp(mimeinfo->subtype, "rfc822"))) {
+ textview_clear(textview);
+ textview_add_parts(textview, mimeinfo);
+ return;
}
- if (!boundary && (mimeinfo->mime_type == MIME_TEXT || mimeinfo->mime_type == MIME_TEXT_HTML || mimeinfo->mime_type == MIME_TEXT_ENRICHED)) {
-
- if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
- perror("fseek");
- headers = textview_scan_header(textview, fp);
- } else {
- if (mimeinfo->mime_type == MIME_TEXT && mimeinfo->parent) {
- glong fpos;
- MimeInfo *parent = mimeinfo->parent;
-
- while (parent->parent) {
- if (parent->main &&
- parent->main->mime_type ==
- MIME_MESSAGE_RFC822)
- break;
- parent = parent->parent;
- }
+ if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0)
+ perror("fseek");
- if ((fpos = ftell(fp)) < 0)
- perror("ftell");
- else if (fseek(fp, parent->fpos, SEEK_SET) < 0)
- perror("fseek");
- else {
- headers = textview_scan_header(textview, fp);
- if (fseek(fp, fpos, SEEK_SET) < 0)
- perror("fseek");
- }
- }
- /* skip MIME part headers */
- while (fgets(buf, sizeof(buf), fp) != NULL)
- if (buf[0] == '\r' || buf[0] == '\n') break;
- }
+ textview_clear(textview);
- /* display attached RFC822 single text message */
- if (mimeinfo->parent && mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
- if (headers) procheader_header_array_destroy(headers);
- if (!mimeinfo->sub || mimeinfo->sub->children) return;
- headers = textview_scan_header(textview, fp);
- mimeinfo = mimeinfo->sub;
- } else if (!mimeinfo->parent &&
- mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
- if (headers) procheader_header_array_destroy(headers);
- if (!mimeinfo->sub) return;
- headers = textview_scan_header(textview, fp);
- mimeinfo = mimeinfo->sub;
- }
+ if (mimeinfo->type == MIMETYPE_MULTIPART)
+ textview_add_parts(textview, mimeinfo);
+ else
+ textview_write_body(textview, mimeinfo);
- if (prefs_common.force_charset)
- charset = prefs_common.force_charset;
- else if (mimeinfo->charset)
- charset = mimeinfo->charset;
- textview_set_font(textview, charset);
+}
- conv = conv_code_converter_new(charset);
+static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
+{
+ GtkTextView *text;
+ GtkTextBuffer *buffer;
+ GtkTextIter iter;
+ gchar buf[BUFFSIZE];
+ GPtrArray *headers = NULL;
+ const gchar *name;
+ gchar *content_type;
+ gint charcount;
- textview_clear(textview);
- text = GTK_TEXT(textview->text);
- gtk_text_freeze(text);
+ g_return_if_fail(mimeinfo != NULL);
+ text = GTK_TEXT_VIEW(textview->text);
+ buffer = gtk_text_view_get_buffer(text);
+ charcount = gtk_text_buffer_get_char_count(buffer);
+ gtk_text_buffer_get_end_iter(buffer, &iter);
- textview->body_pos = 0;
- textview->cur_pos = 0;
+ if (mimeinfo->type == MIMETYPE_MULTIPART) return;
- if (headers) {
- textview_show_header(textview, headers);
- procheader_header_array_destroy(headers);
+ if ((mimeinfo->type == MIMETYPE_MESSAGE) && !g_ascii_strcasecmp(mimeinfo->subtype, "rfc822")) {
+ FILE *fp;
+
+ fp = fopen(mimeinfo->data.filename, "rb");
+ fseek(fp, mimeinfo->offset, SEEK_SET);
+ headers = textview_scan_header(textview, fp);
+ if (headers) {
+ if (charcount > 0)
+ gtk_text_buffer_insert(buffer, &iter, "\n", 1);
+ textview_show_header(textview, headers);
+ procheader_header_array_destroy(headers);
+ }
+ fclose(fp);
+ return;
}
- tmpfp = procmime_decode_content(NULL, fp, mimeinfo);
- if (tmpfp) {
- if (mimeinfo->mime_type == MIME_TEXT_HTML)
- textview_show_html(textview, tmpfp, conv);
- else if (mimeinfo->mime_type == MIME_TEXT_ENRICHED)
- textview_show_ertf(textview, tmpfp, conv);
- else
- while (fgets(buf, sizeof(buf), tmpfp) != NULL)
- textview_write_line(textview, buf, conv);
- fclose(tmpfp);
+ name = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
+ content_type = procmime_get_content_type_str(mimeinfo->type,
+ mimeinfo->subtype);
+ if (name == NULL)
+ name = procmime_mimeinfo_get_parameter(mimeinfo, "name");
+ if (name != NULL)
+ g_snprintf(buf, sizeof(buf), "\n[%s %s (%d bytes)]\n",
+ name, content_type, mimeinfo->length);
+ else
+ g_snprintf(buf, sizeof(buf), "\n[%s (%d bytes)]\n",
+ content_type, mimeinfo->length);
+
+ g_free(content_type);
+
+ if (mimeinfo->type != MIMETYPE_TEXT) {
+ gtk_text_buffer_insert(buffer, &iter, buf, -1);
+ } else if (mimeinfo->disposition != DISPOSITIONTYPE_ATTACHMENT) {
+ if (prefs_common.display_header && (charcount > 0))
+ gtk_text_buffer_insert(buffer, &iter, "\n", 1);
+
+ textview_write_body(textview, mimeinfo);
}
+}
- conv_code_converter_destroy(conv);
+static void recursive_add_parts(TextView *textview, GNode *node)
+{
+ GNode * iter;
+ MimeInfo *mimeinfo;
+
+ mimeinfo = (MimeInfo *) node->data;
+
+ textview_add_part(textview, mimeinfo);
+
+ if ((mimeinfo->type != MIMETYPE_MULTIPART) &&
+ (mimeinfo->type != MIMETYPE_MESSAGE))
+ return;
+
+ if (g_ascii_strcasecmp(mimeinfo->subtype, "alternative") == 0) {
+ GNode * prefered_body;
+ int prefered_score;
+
+ /*
+ text/plain : score 3
+ text/ * : score 2
+ other : score 1
+ */
+ prefered_body = NULL;
+ prefered_score = 0;
+
+ for(iter = g_node_first_child(node) ; iter != NULL ;
+ iter = g_node_next_sibling(iter)) {
+ int score;
+ MimeInfo * submime;
+
+ score = 1;
+ submime = (MimeInfo *) iter->data;
+ if (submime->type == MIMETYPE_TEXT)
+ score = 2;
+
+ if (submime->subtype != NULL) {
+ if (g_ascii_strcasecmp(submime->subtype, "plain") == 0)
+ score = 3;
+ }
+
+ if (score > prefered_score) {
+ prefered_score = score;
+ prefered_body = iter;
+ }
+ }
+
+ if (prefered_body != NULL) {
+ recursive_add_parts(textview, prefered_body);
+ }
+ }
+ else {
+ for(iter = g_node_first_child(node) ; iter != NULL ;
+ iter = g_node_next_sibling(iter)) {
+ recursive_add_parts(textview, iter);
+ }
+ }
+}
- gtk_text_thaw(text);
+static void textview_add_parts(TextView *textview, MimeInfo *mimeinfo)
+{
+ g_return_if_fail(mimeinfo != NULL);
+
+ recursive_add_parts(textview, mimeinfo->node);
}
#define TEXT_INSERT(str) \
- gtk_text_insert(text, textview->msgfont, NULL, NULL, str, -1)
+ gtk_text_buffer_insert(buffer, &iter, str, -1)
+
+void textview_show_error(TextView *textview)
+{
+ GtkTextView *text;
+ GtkTextBuffer *buffer;
+ GtkTextIter iter;
+
+ textview_set_font(textview, NULL);
+ textview_clear(textview);
+
+ text = GTK_TEXT_VIEW(textview->text);
+ buffer = gtk_text_view_get_buffer(text);
+ gtk_text_buffer_get_start_iter(buffer, &iter);
+
+ TEXT_INSERT(_("This message can't be displayed.\n"));
+
+}
void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
{
- GtkText *text;
+ GtkTextView *text;
+ GtkTextBuffer *buffer;
+ GtkTextIter iter;
if (!partinfo) return;
textview_set_font(textview, NULL);
- text = GTK_TEXT(textview->text);
textview_clear(textview);
- gtk_text_freeze(text);
-
- TEXT_INSERT(_("To save this part, pop up the context menu with "));
- TEXT_INSERT(_("right click and select `Save as...', "));
- TEXT_INSERT(_("or press `y' key.\n\n"));
+ text = GTK_TEXT_VIEW(textview->text);
+ buffer = gtk_text_view_get_buffer(text);
+ gtk_text_buffer_get_start_iter(buffer, &iter);
- TEXT_INSERT(_("To display this part as a text message, select "));
- TEXT_INSERT(_("`Display as text', or press `t' key.\n\n"));
+ TEXT_INSERT(_("The following can be performed on this part by "));
+ TEXT_INSERT(_("right-clicking the icon or list item:\n"));
- TEXT_INSERT(_("To open this part with external program, select "));
- TEXT_INSERT(_("`Open' or `Open with...', "));
- TEXT_INSERT(_("or double-click, or click the center button, "));
- TEXT_INSERT(_("or press `l' key."));
+ TEXT_INSERT(_(" To save select 'Save as...' (Shortcut key: 'y')\n"));
+ TEXT_INSERT(_(" To display as text select 'Display as text' "));
+ TEXT_INSERT(_("(Shortcut key: 't')\n"));
+ TEXT_INSERT(_(" To open with an external program select 'Open' "));
+ TEXT_INSERT(_("(Shortcut key: 'l'),\n"));
+ TEXT_INSERT(_(" (alternately double-click, or click the middle "));
+ TEXT_INSERT(_("mouse button),\n"));
+ TEXT_INSERT(_(" or 'Open with...' (Shortcut key: 'o')\n"));
- gtk_text_thaw(text);
}
-#if USE_GPGME
-void textview_show_signature_part(TextView *textview, MimeInfo *partinfo)
+#undef TEXT_INSERT
+
+static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
{
- GtkText *text;
+ FILE *tmpfp;
+ gchar buf[BUFFSIZE];
+ CodeConverter *conv;
+ const gchar *charset;
+
+ if (textview->messageview->forced_charset)
+ charset = textview->messageview->forced_charset;
+ else
+ charset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
- if (!partinfo) return;
+ textview_set_font(textview, charset);
- textview_set_font(textview, NULL);
- text = GTK_TEXT(textview->text);
- textview_clear(textview);
+ conv = conv_code_converter_new(charset);
- gtk_text_freeze(text);
+ procmime_force_encoding(textview->messageview->forced_encoding);
+
+ textview->is_in_signature = FALSE;
- if (partinfo->sigstatus_full == NULL) {
- TEXT_INSERT(_("This signature has not been checked yet.\n"));
- TEXT_INSERT(_("To check it, pop up the context menu with\n"));
- TEXT_INSERT(_("right click and select `Check signature'.\n"));
+ procmime_decode_content(mimeinfo);
+
+ if (!g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
+ gchar *filename;
+
+ filename = procmime_get_tmp_file_name(mimeinfo);
+ if (procmime_get_part(filename, mimeinfo) == 0) {
+ tmpfp = fopen(filename, "rb");
+ textview_show_html(textview, tmpfp, conv);
+ fclose(tmpfp);
+ unlink(filename);
+ }
+ g_free(filename);
+ } else if (!g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
+ gchar *filename;
+
+ filename = procmime_get_tmp_file_name(mimeinfo);
+ if (procmime_get_part(filename, mimeinfo) == 0) {
+ tmpfp = fopen(filename, "rb");
+ textview_show_ertf(textview, tmpfp, conv);
+ fclose(tmpfp);
+ unlink(filename);
+ }
+ g_free(filename);
} else {
- TEXT_INSERT(partinfo->sigstatus_full);
+ tmpfp = fopen(mimeinfo->data.filename, "rb");
+ fseek(tmpfp, mimeinfo->offset, SEEK_SET);
+ debug_print("Viewing text content of type: %s (length: %d)\n", mimeinfo->subtype, mimeinfo->length);
+ while ((fgets(buf, sizeof(buf), tmpfp) != NULL) &&
+ (ftell(tmpfp) <= mimeinfo->offset + mimeinfo->length))
+ textview_write_line(textview, buf, conv);
+ fclose(tmpfp);
}
- gtk_text_thaw(text);
+ conv_code_converter_destroy(conv);
+ procmime_force_encoding(0);
}
-#endif /* USE_GPGME */
-
-#undef TEXT_INSERT
static void textview_show_html(TextView *textview, FILE *fp,
CodeConverter *conv)
{
HTMLParser *parser;
gchar *str;
- gchar* url = NULL;
parser = html_parser_new(fp, conv);
g_return_if_fail(parser != NULL);
while ((str = html_parse(parser)) != NULL) {
if (parser->state == HTML_HREF) {
/* first time : get and copy the URL */
- if (url == NULL) {
+ if (parser->href == NULL) {
/* ALF - the sylpheed html parser returns an empty string,
* if still inside an <a>, but already parsed past HREF */
str = strtok(str, " ");
if (str) {
- url = strdup(str);
+ parser->href = g_strdup(str);
/* the URL may (or not) be followed by the
* referenced text */
str = strtok(NULL, "");
}
}
- if (str != NULL) {
- textview_write_link(textview, url, str, NULL);
- }
- } else {
- if (url != NULL) {
- free(url);
- url = NULL;
- }
+ if (str != NULL)
+ textview_write_link(textview, str, parser->href, NULL);
+ } else
textview_write_line(textview, str, NULL);
- }
}
html_parser_destroy(parser);
}
{
ERTFParser *parser;
gchar *str;
- gchar* url = NULL;
parser = ertf_parser_new(fp, conv);
g_return_if_fail(parser != NULL);
while ((str = ertf_parse(parser)) != NULL) {
textview_write_line(textview, str, NULL);
}
+
ertf_parser_destroy(parser);
}
/* find end point of URI */
for (ep_ = scanpos; *ep_ != '\0'; ep_++) {
- if (!isgraph(*ep_) || !isascii(*ep_) || strchr("()<>\"", *ep_))
+ if (!isgraph(*(const guchar *)ep_) ||
+ !IS_ASCII(*(const guchar *)ep_) ||
+ strchr("()<>\"", *ep_))
break;
}
#define IS_REAL_PUNCT(ch) (ispunct(ch) && ((ch) != '/'))
- for (; ep_ - 1 > scanpos + 1 && IS_REAL_PUNCT(*(ep_ - 1)); ep_--)
+ for (; ep_ - 1 > scanpos + 1 &&
+ IS_REAL_PUNCT(*(const guchar *)(ep_ - 1));
+ ep_--)
;
#undef IS_REAL_PUNCT
/* valid mail address characters */
#define IS_RFC822_CHAR(ch) \
- (isascii(ch) && \
+ (IS_ASCII(ch) && \
(ch) > 32 && \
(ch) != 127 && \
!isspace(ch) && \
- !strchr("()<>\"", (ch)))
+ !strchr("(),;<>\"", (ch)))
/* alphabet and number within 7bit ASCII */
-#define IS_ASCII_ALNUM(ch) (isascii(ch) && isalnum(ch))
+#define IS_ASCII_ALNUM(ch) (IS_ASCII(ch) && isalnum(ch))
#define IS_QUOTE(ch) ((ch) == '\'' || (ch) == '"')
+static GHashTable *create_domain_tab(void)
+{
+ static const gchar *toplvl_domains [] = {
+ "museum", "aero",
+ "arpa", "coop", "info", "name", "biz", "com", "edu", "gov",
+ "int", "mil", "net", "org", "ac", "ad", "ae", "af", "ag",
+ "ai", "al", "am", "an", "ao", "aq", "ar", "as", "at", "au",
+ "aw", "az", "ba", "bb", "bd", "be", "bf", "bg", "bh", "bi",
+ "bj", "bm", "bn", "bo", "br", "bs", "bt", "bv", "bw", "by",
+ "bz", "ca", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl",
+ "cm", "cn", "co", "cr", "cu", "cv", "cx", "cy", "cz", "de",
+ "dj", "dk", "dm", "do", "dz", "ec", "ee", "eg", "eh", "er",
+ "es", "et", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gd",
+ "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gp", "gq",
+ "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr",
+ "ht", "hu", "id", "ie", "il", "im", "in", "io", "iq", "ir",
+ "is", "it", "je", "jm", "jo", "jp", "ke", "kg", "kh", "ki",
+ "km", "kn", "kp", "kr", "kw", "ky", "kz", "la", "lb", "lc",
+ "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc",
+ "md", "mg", "mh", "mk", "ml", "mm", "mn", "mo", "mp", "mq",
+ "mr", "ms", "mt", "mu", "mv", "mw", "mx", "my", "mz", "na",
+ "nc", "ne", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu",
+ "nz", "om", "pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm",
+ "pn", "pr", "ps", "pt", "pw", "py", "qa", "re", "ro", "ru",
+ "rw", "sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sj",
+ "sk", "sl", "sm", "sn", "so", "sr", "st", "sv", "sy", "sz",
+ "tc", "td", "tf", "tg", "th", "tj", "tk", "tm", "tn", "to",
+ "tp", "tr", "tt", "tv", "tw", "tz", "ua", "ug", "uk", "um",
+ "us", "uy", "uz", "va", "vc", "ve", "vg", "vi", "vn", "vu",
+ "wf", "ws", "ye", "yt", "yu", "za", "zm", "zw"
+ };
+ gint n;
+ GHashTable *htab = g_hash_table_new(g_stricase_hash, g_stricase_equal);
+
+ g_return_val_if_fail(htab, NULL);
+ for (n = 0; n < sizeof toplvl_domains / sizeof toplvl_domains[0]; n++)
+ g_hash_table_insert(htab, (gpointer) toplvl_domains[n], (gpointer) toplvl_domains[n]);
+ return htab;
+}
+
+static gboolean is_toplvl_domain(GHashTable *tab, const gchar *first, const gchar *last)
+{
+ const gint MAX_LVL_DOM_NAME_LEN = 6;
+ gchar buf[MAX_LVL_DOM_NAME_LEN + 1];
+ const gchar *m = buf + MAX_LVL_DOM_NAME_LEN + 1;
+ register gchar *p;
+
+ if (last - first > MAX_LVL_DOM_NAME_LEN || first > last)
+ return FALSE;
+
+ for (p = buf; p < m && first < last; *p++ = *first++)
+ ;
+ *p = 0;
+
+ return g_hash_table_lookup(tab, buf) != NULL;
+}
+
/* get_email_part() - retrieves an email address. Returns TRUE if succesful */
static gboolean get_email_part(const gchar *start, const gchar *scanpos,
const gchar **bp, const gchar **ep)
gboolean result = FALSE;
const gchar *bp_ = NULL;
const gchar *ep_ = NULL;
+ static GHashTable *dom_tab;
+ const gchar *last_dot = NULL;
+ const gchar *prelast_dot = NULL;
+ const gchar *last_tld_char = NULL;
/* the informative part of the email address (describing the name
* of the email address owner) may contain quoted parts. the
g_return_val_if_fail(bp != NULL, FALSE);
g_return_val_if_fail(ep != NULL, FALSE);
+ if (!dom_tab)
+ dom_tab = create_domain_tab();
+ g_return_val_if_fail(dom_tab, FALSE);
+
/* scan start of address */
- for (bp_ = scanpos - 1; bp_ >= start && IS_RFC822_CHAR(*bp_); bp_--)
+ for (bp_ = scanpos - 1;
+ bp_ >= start && IS_RFC822_CHAR(*(const guchar *)bp_); bp_--)
;
/* TODO: should start with an alnum? */
bp_++;
- for (; bp_ < scanpos && !IS_ASCII_ALNUM(*bp_); bp_++)
+ for (; bp_ < scanpos && !IS_ASCII_ALNUM(*(const guchar *)bp_); bp_++)
;
if (bp_ != scanpos) {
/* scan end of address */
- for (ep_ = scanpos + 1; *ep_ && IS_RFC822_CHAR(*ep_); ep_++)
- ;
+ for (ep_ = scanpos + 1;
+ *ep_ && IS_RFC822_CHAR(*(const guchar *)ep_); ep_++)
+ if (*ep_ == '.') {
+ prelast_dot = last_dot;
+ last_dot = ep_;
+ if (*(last_dot + 1) == '.') {
+ if (prelast_dot == NULL)
+ return FALSE;
+ last_dot = prelast_dot;
+ break;
+ }
+ }
/* TODO: really should terminate with an alnum? */
- for (; ep_ > scanpos && !IS_ASCII_ALNUM(*ep_); --ep_)
+ for (; ep_ > scanpos && !IS_ASCII_ALNUM(*(const guchar *)ep_);
+ --ep_)
;
ep_++;
- if (ep_ > scanpos + 1) {
- *ep = ep_;
- *bp = bp_;
+ if (last_dot == NULL)
+ return FALSE;
+ if (last_dot >= ep_)
+ last_dot = prelast_dot;
+ if (last_dot == NULL || (scanpos + 1 >= last_dot))
+ return FALSE;
+ last_dot++;
+
+ for (last_tld_char = last_dot; last_tld_char < ep_; last_tld_char++)
+ if (*last_tld_char == '?')
+ break;
+
+ if (is_toplvl_domain(dom_tab, last_dot, last_tld_char))
result = TRUE;
- }
+
+ *ep = ep_;
+ *bp = bp_;
}
if (!result) return FALSE;
}
#undef IS_QUOTE
+#undef IS_ASCII_ALNUM
#undef IS_RFC822_CHAR
static gchar *make_email_string(const gchar *bp, const gchar *ep)
return result;
}
+static gchar *make_http_string(const gchar *bp, const gchar *ep)
+{
+ /* returns an http: URI; */
+ gchar *tmp;
+ gchar *result;
+
+ tmp = g_strndup(bp, ep - bp);
+ result = g_strconcat("http://", tmp, NULL);
+ g_free(tmp);
+
+ return result;
+}
+
#define ADD_TXT_POS(bp_, ep_, pti_) \
if ((last->next = alloca(sizeof(struct txtpos))) != NULL) { \
last = last->next; \
last->next = NULL; \
} else { \
g_warning("alloc error scanning URIs\n"); \
- gtk_text_insert(text, textview->msgfont, fg_color, NULL, \
- linebuf, -1); \
+ gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, \
+ linebuf, -1, \
+ fg_tag, NULL); \
return; \
}
/* textview_make_clickable_parts() - colorizes clickable parts */
static void textview_make_clickable_parts(TextView *textview,
- GdkFont *font,
- GdkColor *fg_color,
- GdkColor *uri_color,
+ const gchar *fg_tag,
+ const gchar *uri_tag,
const gchar *linebuf)
{
+ GtkTextView *text = GTK_TEXT_VIEW(textview->text);
+ GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
+ GtkTextIter iter;
+
/* parse table - in order of priority */
struct table {
const gchar *needle; /* token */
{"http://", strcasestr, get_uri_part, make_uri_string},
{"https://", strcasestr, get_uri_part, make_uri_string},
{"ftp://", strcasestr, get_uri_part, make_uri_string},
+ {"www.", strcasestr, get_uri_part, make_http_string},
{"mailto:", strcasestr, get_uri_part, make_uri_string},
{"@", strcasestr, get_email_part, make_email_string}
};
struct txtpos *next; /* next */
} head = {NULL, NULL, 0, NULL}, *last = &head;
- GtkText *text = GTK_TEXT(textview->text);
+ gtk_text_buffer_get_end_iter(buffer, &iter);
/* parse for clickable parts, and build a list of begin and end positions */
for (walk = linebuf, n = 0;;) {
if (scanpos) {
/* check if URI can be parsed */
- if (parser[last_index].parse(linebuf, scanpos, &bp, &ep)
+ if (parser[last_index].parse(walk, scanpos, &bp, &ep)
&& (size_t) (ep - bp - 1) > strlen(parser[last_index].needle)) {
ADD_TXT_POS(bp, ep, last_index);
walk = ep;
uri = g_new(RemoteURI, 1);
if (last->bp - normal_text > 0)
- gtk_text_insert(text, font,
- fg_color, NULL,
- normal_text,
- last->bp - normal_text);
- uri->uri = parser[last->pti].build_uri(last->bp,
+ gtk_text_buffer_insert_with_tags_by_name
+ (buffer, &iter,
+ normal_text,
+ last->bp - normal_text,
+ fg_tag, NULL);
+ uri->uri = parser[last->pti].build_uri(last->bp,
last->ep);
- uri->start = gtk_text_get_point(text);
- gtk_text_insert(text, font, uri_color,
- NULL, last->bp, last->ep - last->bp);
- uri->end = gtk_text_get_point(text);
+ uri->start = gtk_text_iter_get_offset(&iter);
+ gtk_text_buffer_insert_with_tags_by_name
+ (buffer, &iter,
+ last->bp, last->ep - last->bp,
+ uri_tag, NULL);
+ uri->end = gtk_text_iter_get_offset(&iter);
textview->uri_list =
g_slist_append(textview->uri_list, uri);
}
if (*normal_text)
- gtk_text_insert(text, font, fg_color,
- NULL, normal_text, -1);
- } else
- gtk_text_insert(text, font, fg_color, NULL, linebuf, -1);
+ gtk_text_buffer_insert_with_tags_by_name(buffer, &iter,
+ normal_text, -1,
+ fg_tag, NULL);
+ } else {
+ gtk_text_buffer_insert_with_tags_by_name(buffer, &iter,
+ linebuf, -1,
+ fg_tag, NULL);
+ }
}
#undef ADD_TXT_POS
-/* This function writes str as a double-clickable link with the given url. */
-static void textview_write_link(TextView *textview, const gchar *url,
- const gchar *str, CodeConverter *conv)
-{
- GdkColor *link_color = NULL;
- RemoteURI* uri;
- GtkText *text = GTK_TEXT(textview->text);
- gchar buf[BUFFSIZE];
-
- /* this part is taken from textview_write_line. Right now the only place
- * that calls this function passes NULL for conv, but you never know. */
- if (!conv)
- strncpy2(buf, str, sizeof(buf));
- else if (conv_convert(conv, buf, sizeof(buf), str) < 0) {
- gtk_text_insert(text, textview->msgfont,
- prefs_common.enable_color
- ? &error_color : NULL, NULL,
- "*** Warning: code conversion failed ***\n",
- -1);
- return;
- }
-
- /* this part is based on the code in make_clickable_parts */
- if (prefs_common.enable_color) {
- link_color = &uri_color;
- }
- uri = g_new(RemoteURI, 1);
- uri->uri = g_strdup(url);
- uri->start = gtk_text_get_point(text);
- gtk_text_insert(text, textview->msgfont, link_color, NULL, buf,
- strlen(buf));
- uri->end = gtk_text_get_point(text);
- textview->uri_list = g_slist_append(textview->uri_list, uri);
-}
-
static void textview_write_line(TextView *textview, const gchar *str,
CodeConverter *conv)
{
- GtkText *text = GTK_TEXT(textview->text);
+ GtkTextView *text;
+ GtkTextBuffer *buffer;
+ GtkTextIter iter;
gchar buf[BUFFSIZE];
- GdkColor *fg_color;
+ gchar *fg_color;
gint quotelevel = -1;
+ gchar quote_tag_str[10];
- if (!conv)
+ text = GTK_TEXT_VIEW(textview->text);
+ buffer = gtk_text_view_get_buffer(text);
+ gtk_text_buffer_get_end_iter(buffer, &iter);
+
+ if (!conv) {
strncpy2(buf, str, sizeof(buf));
- else if (conv_convert(conv, buf, sizeof(buf), str) < 0) {
- gtk_text_insert(text, textview->msgfont,
- prefs_common.enable_color
- ? &error_color : NULL, NULL,
- "*** Warning: code conversion failed ***\n",
- -1);
- return;
+ } else if (conv_convert(conv, buf, sizeof(buf), str) < 0) {
+ conv_localetodisp(buf, sizeof(buf), str);
}
strcrchomp(buf);
>, foo>, _> ... ok, <foo>, foo bar>, foo-> ... ng
Up to 3 levels of quotations are detected, and each
level is colored using a different color. */
- if (prefs_common.enable_color && strchr(buf, '>')) {
- quotelevel = get_quote_level(buf);
+ if (prefs_common.enable_color
+ && line_has_quote_char(buf, prefs_common.quote_chars)) {
+ quotelevel = get_quote_level(buf, prefs_common.quote_chars);
/* set up the correct foreground color */
if (quotelevel > 2) {
if (quotelevel == -1)
fg_color = NULL;
- else
- fg_color = "e_colors[quotelevel];
+ else {
+ g_snprintf(quote_tag_str, sizeof(quote_tag_str),
+ "quote%d", quotelevel);
+ fg_color = quote_tag_str;
+ }
- if (prefs_common.head_space && spacingfont && buf[0] != '\n')
- gtk_text_insert(text, spacingfont, NULL, NULL, " ", 1);
+ if (prefs_common.enable_color && (strcmp(buf,"-- \n") == 0 || textview->is_in_signature)) {
+ fg_color = "signature";
+ textview->is_in_signature = TRUE;
+ }
if (prefs_common.enable_color)
- textview_make_clickable_parts(textview, textview->msgfont,
- fg_color, &uri_color, buf);
- else
- textview_make_clickable_parts(textview, textview->msgfont,
- fg_color, NULL, buf);
+ textview_make_clickable_parts(textview, fg_color, "link", buf);
+ else
+ textview_make_clickable_parts(textview, fg_color, NULL, buf);
+}
+
+void textview_write_link(TextView *textview, const gchar *str,
+ const gchar *uri, CodeConverter *conv)
+{
+ GdkColor *link_color = NULL;
+ GtkTextView *text;
+ GtkTextBuffer *buffer;
+ GtkTextIter iter;
+ gchar buf[BUFFSIZE];
+ gchar *bufp;
+ RemoteURI *r_uri;
+
+ if (*str == '\0')
+ return;
+
+ text = GTK_TEXT_VIEW(textview->text);
+ buffer = gtk_text_view_get_buffer(text);
+ gtk_text_buffer_get_end_iter(buffer, &iter);
+
+#warning FIXME_GTK2
+#if 0
+ if (!conv) {
+ if (textview->text_is_mb)
+ conv_localetodisp(buf, sizeof(buf), str);
+ else
+ strncpy2(buf, str, sizeof(buf));
+ } else if (conv_convert(conv, buf, sizeof(buf), str) < 0)
+ conv_localetodisp(buf, sizeof(buf), str);
+ else if (textview->text_is_mb)
+ conv_unreadable_locale(buf);
+#else
+ if (!conv)
+ strncpy2(buf, str, sizeof(buf));
+ else if (conv_convert(conv, buf, sizeof(buf), str) < 0)
+ conv_localetodisp(buf, sizeof(buf), str);
+#endif
+
+ strcrchomp(buf);
+
+ gtk_text_buffer_get_end_iter(buffer, &iter);
+
+ for (bufp = buf; isspace(*(guchar *)bufp); bufp++)
+ gtk_text_buffer_insert(buffer, &iter, bufp, 1);
+
+ if (prefs_common.enable_color) {
+ link_color = &uri_color;
+ }
+ r_uri = g_new(RemoteURI, 1);
+ r_uri->uri = g_strdup(uri);
+ r_uri->start = gtk_text_iter_get_offset(&iter);
+ gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, bufp, -1,
+ "link", NULL);
+ r_uri->end = gtk_text_iter_get_offset(&iter);
+ textview->uri_list = g_slist_append(textview->uri_list, r_uri);
}
void textview_clear(TextView *textview)
{
- GtkText *text = GTK_TEXT(textview->text);
+ GtkTextView *text = GTK_TEXT_VIEW(textview->text);
+ GtkTextBuffer *buffer;
- gtk_text_freeze(text);
- gtk_text_set_point(text, 0);
- gtk_text_forward_delete(text, gtk_text_get_length(text));
- gtk_text_thaw(text);
+ buffer = gtk_text_view_get_buffer(text);
+ gtk_text_buffer_set_text(buffer, "\0", -1);
+ TEXTVIEW_STATUSBAR_POP(textview);
textview_uri_list_remove_all(textview->uri_list);
textview->uri_list = NULL;
+
+ textview->body_pos = 0;
}
void textview_destroy(TextView *textview)
textview_uri_list_remove_all(textview->uri_list);
textview->uri_list = NULL;
- if (!textview->scrolledwin_sb->parent)
- gtk_widget_destroy(textview->scrolledwin_sb);
- if (!textview->scrolledwin_mb->parent)
- gtk_widget_destroy(textview->scrolledwin_mb);
-
- if (textview->msgfont) {
- textview->msgfont->ascent = textview->prev_ascent;
- textview->msgfont->descent = textview->prev_descent;
- gdk_font_unref(textview->msgfont);
- }
- if (textview->boldfont)
- gdk_font_unref(textview->boldfont);
-
g_free(textview);
}
+void textview_set_all_headers(TextView *textview, gboolean all_headers)
+{
+ textview->show_all_headers = all_headers;
+}
+
void textview_set_font(TextView *textview, const gchar *codeset)
{
- gboolean use_fontset = TRUE;
-
- /* In multi-byte mode, GtkText can't display 8bit characters
- correctly, so it must be single-byte mode. */
- if (MB_CUR_MAX > 1) {
- if (codeset) {
- if (!g_strncasecmp(codeset, "ISO-8859-", 9) ||
- !g_strcasecmp(codeset, "BALTIC"))
- use_fontset = FALSE;
- else if (conv_get_current_charset() != C_EUC_JP &&
- (!g_strncasecmp(codeset, "KOI8-", 5) ||
- !g_strncasecmp(codeset, "CP", 2) ||
- !g_strncasecmp(codeset, "WINDOWS-", 8)))
- use_fontset = FALSE;
+ if (prefs_common.textfont) {
+ PangoFontDescription *font_desc = NULL;
+
+ if (prefs_common.textfont)
+ font_desc = pango_font_description_from_string
+ (prefs_common.textfont);
+ if (font_desc) {
+ gtk_widget_modify_font(textview->text, font_desc);
+ pango_font_description_free(font_desc);
}
- } else
- use_fontset = FALSE;
-
- if (textview->text_is_mb && !use_fontset) {
- GtkWidget *parent;
-
- parent = textview->scrolledwin_mb->parent;
- gtk_editable_claim_selection(GTK_EDITABLE(textview->text_mb),
- FALSE, GDK_CURRENT_TIME);
- gtk_container_remove(GTK_CONTAINER(parent),
- textview->scrolledwin_mb);
- gtk_container_add(GTK_CONTAINER(parent),
- textview->scrolledwin_sb);
-
- textview->text = textview->text_sb;
- textview->text_is_mb = FALSE;
- } else if (!textview->text_is_mb && use_fontset) {
- GtkWidget *parent;
-
- parent = textview->scrolledwin_sb->parent;
- gtk_editable_claim_selection(GTK_EDITABLE(textview->text_sb),
- FALSE, GDK_CURRENT_TIME);
- gtk_container_remove(GTK_CONTAINER(parent),
- textview->scrolledwin_sb);
- gtk_container_add(GTK_CONTAINER(parent),
- textview->scrolledwin_mb);
-
- textview->text = textview->text_mb;
- textview->text_is_mb = TRUE;
}
+ gtk_text_view_set_pixels_above_lines(GTK_TEXT_VIEW(textview->text),
+ prefs_common.line_space / 2);
+ gtk_text_view_set_pixels_below_lines(GTK_TEXT_VIEW(textview->text),
+ prefs_common.line_space / 2);
+ if (prefs_common.head_space) {
+ gtk_text_view_set_left_margin(GTK_TEXT_VIEW(textview->text), 6);
+ } else {
+ gtk_text_view_set_left_margin(GTK_TEXT_VIEW(textview->text), 0);
+ }
+}
- if (prefs_common.textfont) {
- if (textview->msgfont) {
- textview->msgfont->ascent = textview->prev_ascent;
- textview->msgfont->descent = textview->prev_descent;
- gdk_font_unref(textview->msgfont);
- textview->msgfont = NULL;
- }
- if (use_fontset)
- textview->msgfont =
- gdk_fontset_load(prefs_common.textfont);
- else {
- if (MB_CUR_MAX > 1) {
- FONT_LOAD(textview->msgfont,
- "-*-courier-medium-r-normal--14-*-*-*-*-*-iso8859-1");
- } else {
- FONT_LOAD(textview->msgfont,
- prefs_common.textfont);
- }
- }
+void textview_set_text(TextView *textview, const gchar *text)
+{
+ GtkTextView *view;
+ GtkTextBuffer *buffer;
- if (textview->msgfont) {
- gint ascent, descent;
+ g_return_if_fail(textview != NULL);
+ g_return_if_fail(text != NULL);
- textview->prev_ascent = textview->msgfont->ascent;
- textview->prev_descent = textview->msgfont->descent;
- descent = prefs_common.line_space / 2;
- ascent = prefs_common.line_space - descent;
- textview->msgfont->ascent += ascent;
- textview->msgfont->descent += descent;
- }
- }
+ textview_clear(textview);
- if (!textview->boldfont)
- FONT_LOAD(textview->boldfont, BOLD_FONT);
- if (!spacingfont)
- spacingfont = gdk_font_load("-*-*-medium-r-normal--6-*");
+ view = GTK_TEXT_VIEW(textview->text);
+ buffer = gtk_text_view_get_buffer(view);
+ gtk_text_buffer_set_text(buffer, text, strlen(text));
}
enum
void textview_set_position(TextView *textview, gint pos)
{
- if (pos < 0) {
- textview->cur_pos =
- gtk_text_get_length(GTK_TEXT(textview->text));
- } else {
- textview->cur_pos = pos;
- }
+ GtkTextView *text = GTK_TEXT_VIEW(textview->text);
+ GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
+ GtkTextIter iter;
+
+ gtk_text_buffer_get_iter_at_offset(buffer, &iter, pos);
+ gtk_text_buffer_place_cursor(buffer, &iter);
+ gtk_text_view_scroll_to_iter(text, &iter, 0.0, FALSE, 0.0, 0.0);
}
static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
g_return_val_if_fail(fp != NULL, NULL);
+ if (textview->show_all_headers)
+ return procheader_get_header_array_asis(fp);
+
if (!prefs_common.display_header) {
while (fgets(buf, sizeof(buf), fp) != NULL)
if (buf[0] == '\r' || buf[0] == '\n') break;
header = g_ptr_array_index(headers, i);
g_ptr_array_add(sorted_headers, header);
}
- }
+ g_ptr_array_free(headers, TRUE);
+ } else
+ procheader_header_array_destroy(headers);
- g_ptr_array_free(headers, FALSE);
return sorted_headers;
}
static void textview_show_header(TextView *textview, GPtrArray *headers)
{
- GtkText *text = GTK_TEXT(textview->text);
+ GtkTextView *text = GTK_TEXT_VIEW(textview->text);
+ GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
+ GtkTextIter iter;
Header *header;
gint i;
g_return_if_fail(headers != NULL);
- gtk_text_freeze(text);
-
for (i = 0; i < headers->len; i++) {
header = g_ptr_array_index(headers, i);
g_return_if_fail(header->name != NULL);
- gtk_text_insert(text, textview->boldfont, NULL, NULL,
- header->name, -1);
+ gtk_text_buffer_get_end_iter (buffer, &iter);
+ gtk_text_buffer_insert_with_tags_by_name
+ (buffer, &iter, header->name, -1,
+ "header_title", "header", NULL);
if (header->name[strlen(header->name) - 1] != ' ')
- gtk_text_insert(text, textview->boldfont,
- NULL, NULL, " ", 1);
+ gtk_text_buffer_insert_with_tags_by_name
+ (buffer, &iter, " ", 1,
+ "header_title", "header", NULL);
if (procheader_headername_equal(header->name, "Subject") ||
procheader_headername_equal(header->name, "From") ||
procheader_headername_equal(header->name, "Cc"))
unfold_line(header->body);
+#warning FIXME_GTK2
+#if 0
+ if (textview->text_is_mb == TRUE)
+ conv_unreadable_locale(header->body);
+#endif
+
if (prefs_common.enable_color &&
(procheader_headername_equal(header->name, "X-Mailer") ||
procheader_headername_equal(header->name,
"X-Newsreader")) &&
- strstr(header->body, "Sylpheed") != NULL)
- gtk_text_insert(text, NULL, &emphasis_color, NULL,
- header->body, -1);
- else if (prefs_common.enable_color) {
- textview_make_clickable_parts(textview,
- NULL, NULL, &uri_color,
+ strstr(header->body, "Sylpheed") != NULL) {
+ gtk_text_buffer_get_end_iter (buffer, &iter);
+ gtk_text_buffer_insert_with_tags_by_name
+ (buffer, &iter, header->body, -1,
+ "header", "emphasis", NULL);
+ } else if (prefs_common.enable_color) {
+ textview_make_clickable_parts(textview, "header", "link",
header->body);
} else {
- textview_make_clickable_parts(textview,
- NULL, NULL, NULL,
+ textview_make_clickable_parts(textview, "header", NULL,
header->body);
}
- gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
+ gtk_text_buffer_get_end_iter (buffer, &iter);
+ gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, "\n", 1,
+ "header", NULL);
}
-
- gtk_text_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
- gtk_text_thaw(text);
- textview->body_pos = gtk_text_get_length(text);
}
gboolean textview_search_string(TextView *textview, const gchar *str,
gboolean case_sens)
{
- GtkText *text = GTK_TEXT(textview->text);
- gint pos;
- wchar_t *wcs;
- gint len;
- gint text_len;
+ GtkTextView *text = GTK_TEXT_VIEW(textview->text);
+ GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
+ GtkTextMark *mark;
+ GtkTextIter iter, start, end, real_end, *pos;
gboolean found = FALSE;
-
- g_return_val_if_fail(str != NULL, FALSE);
-
- wcs = strdup_mbstowcs(str);
- g_return_val_if_fail(wcs != NULL, FALSE);
- len = wcslen(wcs);
- pos = textview->cur_pos;
- if (pos < textview->body_pos)
- pos = textview->body_pos;
- text_len = gtk_text_get_length(text);
- if (text_len - pos < len) {
- g_free(wcs);
- return FALSE;
- }
-
- for (; pos < text_len; pos++) {
- if (text_len - pos < len) break;
- if (gtkut_text_match_string(text, pos, wcs, len, case_sens)
- == TRUE) {
- gtk_editable_set_position(GTK_EDITABLE(text),
- pos + len);
- gtk_editable_select_region(GTK_EDITABLE(text),
- pos, pos + len);
- textview_set_position(textview, pos + len);
- found = TRUE;
- break;
+ gint insert_offset, selbound_offset;
+
+ /* reset selection */
+ mark = gtk_text_buffer_get_mark(buffer, "insert");
+ gtk_text_buffer_get_iter_at_mark(buffer, &start, mark);
+ insert_offset = gtk_text_iter_get_offset(&start);
+ mark = gtk_text_buffer_get_mark(buffer, "selection_bound");
+ gtk_text_buffer_get_iter_at_mark(buffer, &end, mark);
+ selbound_offset = gtk_text_iter_get_offset(&end);
+
+ pos = insert_offset > selbound_offset ? &start : &end;
+ gtk_text_buffer_place_cursor(buffer, pos);
+
+ /* search */
+ mark = gtk_text_buffer_get_insert(buffer);
+ gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
+ if (case_sens) {
+ found = gtk_text_iter_forward_search(&iter, str,
+ GTK_TEXT_SEARCH_VISIBLE_ONLY,
+ &start, &end, NULL);
+ } else {
+ gchar *text = NULL;
+ int i = 0;
+ gtk_text_buffer_get_end_iter(buffer, &real_end);
+ text = gtk_text_buffer_get_text(buffer, &iter,
+ &real_end, FALSE);
+
+ while (!found && i++ < strlen(text) - 1) {
+ found = (strncasecmp(text+i, str, strlen(str)) == 0);
+ }
+
+ i += gtk_text_iter_get_offset(&end);
+
+ if (found) {
+ gtk_text_buffer_get_iter_at_offset(buffer, &start, i);
+ gtk_text_buffer_get_iter_at_offset(buffer, &end,
+ i + strlen(str));
}
- if (text_len - pos == len) break;
+
+ g_free(text);
+ }
+
+ if (found) {
+ gtk_text_buffer_place_cursor(buffer, &start);
+ gtk_text_buffer_move_mark_by_name(buffer, "selection_bound",
+ &end);
+ mark = gtk_text_buffer_get_mark(buffer, "insert");
+ gtk_text_view_scroll_mark_onscreen(text, mark);
}
- g_free(wcs);
return found;
}
gboolean textview_search_string_backward(TextView *textview, const gchar *str,
gboolean case_sens)
{
- GtkText *text = GTK_TEXT(textview->text);
- gint pos;
- wchar_t *wcs;
- gint len;
- gint text_len;
+ GtkTextView *text = GTK_TEXT_VIEW(textview->text);
+ GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
+ GtkTextMark *mark;
+ GtkTextIter iter, start, real_start, end, *pos;
gboolean found = FALSE;
-
- g_return_val_if_fail(str != NULL, FALSE);
-
- wcs = strdup_mbstowcs(str);
- g_return_val_if_fail(wcs != NULL, FALSE);
- len = wcslen(wcs);
- pos = textview->cur_pos;
- text_len = gtk_text_get_length(text);
- if (text_len - textview->body_pos < len) {
- g_free(wcs);
- return FALSE;
- }
- if (pos <= textview->body_pos || text_len - pos < len)
- pos = text_len - len;
-
- for (; pos >= textview->body_pos; pos--) {
- if (gtkut_text_match_string(text, pos, wcs, len, case_sens)
- == TRUE) {
- gtk_editable_set_position(GTK_EDITABLE(text), pos);
- gtk_editable_select_region(GTK_EDITABLE(text),
- pos, pos + len);
- textview_set_position(textview, pos - 1);
- found = TRUE;
- break;
+ gint insert_offset, selbound_offset;
+
+ /* reset selection */
+ mark = gtk_text_buffer_get_mark(buffer, "insert");
+ gtk_text_buffer_get_iter_at_mark(buffer, &start, mark);
+ insert_offset = gtk_text_iter_get_offset(&start);
+ mark = gtk_text_buffer_get_mark(buffer, "selection_bound");
+ gtk_text_buffer_get_iter_at_mark(buffer, &end, mark);
+ selbound_offset = gtk_text_iter_get_offset(&end);
+
+ pos = insert_offset < selbound_offset ? &start : &end;
+ gtk_text_buffer_place_cursor(buffer, pos);
+
+ /* search */
+ mark = gtk_text_buffer_get_insert(buffer);
+ gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
+ if (case_sens) {
+ found = gtk_text_iter_backward_search(&iter, str,
+ GTK_TEXT_SEARCH_VISIBLE_ONLY,
+ &start, &end, NULL);
+ } else {
+ gchar *text = NULL;
+ int i = 0;
+ if (gtk_text_iter_get_offset(&iter) == 0)
+ gtk_text_buffer_get_end_iter(buffer, &iter);
+
+ i = gtk_text_iter_get_offset(&iter) - strlen(str) - 1;
+ gtk_text_buffer_get_start_iter(buffer, &real_start);
+
+ text = gtk_text_buffer_get_text(buffer, &real_start,
+ &iter, FALSE);
+
+ while (!found && i-- > 0) {
+ found = (strncasecmp(text+i, str, strlen(str)) == 0);
+ }
+
+ if (found) {
+ gtk_text_buffer_get_iter_at_offset(buffer, &start, i);
+ gtk_text_buffer_get_iter_at_offset(buffer, &end,
+ i + strlen(str));
}
- if (pos == textview->body_pos) break;
+
+ g_free(text);
+ }
+
+ if (found) {
+ gtk_text_buffer_place_cursor(buffer, &end);
+ gtk_text_buffer_move_mark_by_name(buffer, "selection_bound",
+ &start);
+ mark = gtk_text_buffer_get_mark(buffer, "insert");
+ gtk_text_view_scroll_mark_onscreen(text, mark);
}
- g_free(wcs);
return found;
}
void textview_scroll_one_line(TextView *textview, gboolean up)
{
- GtkText *text = GTK_TEXT(textview->text);
+ GtkTextView *text = GTK_TEXT_VIEW(textview->text);
+ GtkAdjustment *vadj = text->vadjustment;
gfloat upper;
if (prefs_common.enable_smooth_scroll) {
}
if (!up) {
- upper = text->vadj->upper - text->vadj->page_size;
- if (text->vadj->value < upper) {
- text->vadj->value +=
- text->vadj->step_increment * 4;
- text->vadj->value =
- MIN(text->vadj->value, upper);
- gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
- "value_changed");
+ upper = vadj->upper - vadj->page_size;
+ if (vadj->value < upper) {
+ vadj->value +=
+ vadj->step_increment * 4;
+ vadj->value =
+ MIN(vadj->value, upper);
+ g_signal_emit_by_name(G_OBJECT(vadj),
+ "value_changed", 0);
}
} else {
- if (text->vadj->value > 0.0) {
- text->vadj->value -=
- text->vadj->step_increment * 4;
- text->vadj->value =
- MAX(text->vadj->value, 0.0);
- gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
- "value_changed");
+ if (vadj->value > 0.0) {
+ vadj->value -=
+ vadj->step_increment * 4;
+ vadj->value =
+ MAX(vadj->value, 0.0);
+ g_signal_emit_by_name(G_OBJECT(vadj),
+ "value_changed", 0);
}
}
}
gboolean textview_scroll_page(TextView *textview, gboolean up)
{
- GtkText *text = GTK_TEXT(textview->text);
+ GtkTextView *text = GTK_TEXT_VIEW(textview->text);
+ GtkAdjustment *vadj = text->vadjustment;
gfloat upper;
gfloat page_incr;
return textview_smooth_scroll_page(textview, up);
if (prefs_common.scroll_halfpage)
- page_incr = text->vadj->page_increment / 2;
+ page_incr = vadj->page_increment / 2;
else
- page_incr = text->vadj->page_increment;
+ page_incr = vadj->page_increment;
if (!up) {
- upper = text->vadj->upper - text->vadj->page_size;
- if (text->vadj->value < upper) {
- text->vadj->value += page_incr;
- text->vadj->value = MIN(text->vadj->value, upper);
- gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
- "value_changed");
+ upper = vadj->upper - vadj->page_size;
+ if (vadj->value < upper) {
+ vadj->value += page_incr;
+ vadj->value = MIN(vadj->value, upper);
+ g_signal_emit_by_name(G_OBJECT(vadj),
+ "value_changed", 0);
} else
return FALSE;
} else {
- if (text->vadj->value > 0.0) {
- text->vadj->value -= page_incr;
- text->vadj->value = MAX(text->vadj->value, 0.0);
- gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
- "value_changed");
+ if (vadj->value > 0.0) {
+ vadj->value -= page_incr;
+ vadj->value = MAX(vadj->value, 0.0);
+ g_signal_emit_by_name(G_OBJECT(vadj),
+ "value_changed", 0);
} else
return FALSE;
}
gfloat old_value, gfloat last_value,
gint step)
{
- GtkText *text = GTK_TEXT(textview->text);
+ GtkTextView *text = GTK_TEXT_VIEW(textview->text);
+ GtkAdjustment *vadj = text->vadjustment;
gint change_value;
gboolean up;
gint i;
up = TRUE;
}
- gdk_key_repeat_disable();
-
for (i = step; i <= change_value; i += step) {
- text->vadj->value = old_value + (up ? -i : i);
- gtk_signal_emit_by_name(GTK_OBJECT(text->vadj),
- "value_changed");
+ vadj->value = old_value + (up ? -i : i);
+ g_signal_emit_by_name(G_OBJECT(vadj),
+ "value_changed", 0);
}
- text->vadj->value = last_value;
- gtk_signal_emit_by_name(GTK_OBJECT(text->vadj), "value_changed");
-
- gdk_key_repeat_restore();
+ vadj->value = last_value;
+ g_signal_emit_by_name(G_OBJECT(vadj), "value_changed", 0);
}
static void textview_smooth_scroll_one_line(TextView *textview, gboolean up)
{
- GtkText *text = GTK_TEXT(textview->text);
+ GtkTextView *text = GTK_TEXT_VIEW(textview->text);
+ GtkAdjustment *vadj = text->vadjustment;
gfloat upper;
gfloat old_value;
gfloat last_value;
if (!up) {
- upper = text->vadj->upper - text->vadj->page_size;
- if (text->vadj->value < upper) {
- old_value = text->vadj->value;
- last_value = text->vadj->value +
- text->vadj->step_increment * 4;
+ upper = vadj->upper - vadj->page_size;
+ if (vadj->value < upper) {
+ old_value = vadj->value;
+ last_value = vadj->value +
+ vadj->step_increment * 4;
last_value = MIN(last_value, upper);
textview_smooth_scroll_do(textview, old_value,
prefs_common.scroll_step);
}
} else {
- if (text->vadj->value > 0.0) {
- old_value = text->vadj->value;
- last_value = text->vadj->value -
- text->vadj->step_increment * 4;
+ if (vadj->value > 0.0) {
+ old_value = vadj->value;
+ last_value = vadj->value -
+ vadj->step_increment * 4;
last_value = MAX(last_value, 0.0);
textview_smooth_scroll_do(textview, old_value,
static gboolean textview_smooth_scroll_page(TextView *textview, gboolean up)
{
- GtkText *text = GTK_TEXT(textview->text);
+ GtkTextView *text = GTK_TEXT_VIEW(textview->text);
+ GtkAdjustment *vadj = text->vadjustment;
gfloat upper;
gfloat page_incr;
gfloat old_value;
gfloat last_value;
if (prefs_common.scroll_halfpage)
- page_incr = text->vadj->page_increment / 2;
+ page_incr = vadj->page_increment / 2;
else
- page_incr = text->vadj->page_increment;
+ page_incr = vadj->page_increment;
if (!up) {
- upper = text->vadj->upper - text->vadj->page_size;
- if (text->vadj->value < upper) {
- old_value = text->vadj->value;
- last_value = text->vadj->value + page_incr;
+ upper = vadj->upper - vadj->page_size;
+ if (vadj->value < upper) {
+ old_value = vadj->value;
+ last_value = vadj->value + page_incr;
last_value = MIN(last_value, upper);
textview_smooth_scroll_do(textview, old_value,
} else
return FALSE;
} else {
- if (text->vadj->value > 0.0) {
- old_value = text->vadj->value;
- last_value = text->vadj->value - page_incr;
+ if (vadj->value > 0.0) {
+ old_value = vadj->value;
+ last_value = vadj->value - page_incr;
last_value = MAX(last_value, 0.0);
textview_smooth_scroll_do(textview, old_value,
return TRUE;
}
-static void textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
+#define KEY_PRESS_EVENT_STOP() \
+ g_signal_stop_emission_by_name(G_OBJECT(widget), \
+ "key_press_event");
+
+static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
TextView *textview)
{
SummaryView *summaryview = NULL;
+ MessageView *messageview = textview->messageview;
- if (!event) return;
- if (textview->messageview->mainwin)
- summaryview = textview->messageview->mainwin->summaryview;
+ if (!event) return FALSE;
+ if (messageview->mainwin)
+ summaryview = messageview->mainwin->summaryview;
switch (event->keyval) {
case GDK_Tab:
textview_scroll_one_line(textview,
(event->state & GDK_MOD1_MASK) != 0);
break;
- default:
+ case GDK_Delete:
if (summaryview)
summary_pass_key_press_event(summaryview, event);
break;
+ case GDK_y:
+ case GDK_t:
+ case GDK_l:
+ if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) == 0) {
+ KEY_PRESS_EVENT_STOP();
+ mimeview_pass_key_press_event(messageview->mimeview,
+ event);
+ break;
+ }
+ /* possible fall through */
+ default:
+ if (summaryview &&
+ event->window != messageview->mainwin->window->window) {
+ GdkEventKey tmpev = *event;
+
+ tmpev.window = messageview->mainwin->window->window;
+ KEY_PRESS_EVENT_STOP();
+ gtk_widget_event(messageview->mainwin->window,
+ (GdkEvent *)&tmpev);
+ }
+ break;
}
+
+ return TRUE;
}
-static void textview_button_pressed(GtkWidget *widget, GdkEventButton *event,
- TextView *textview)
+/*!
+ *\brief Check to see if a web URL has been disguised as a different
+ * URL (possible with HTML email).
+ *
+ *\param uri The uri to check
+ *
+ *\param textview The TextView the URL is contained in
+ *
+ *\return gboolean TRUE if the URL is ok, or if the user chose to open
+ * it anyway, otherwise FALSE
+ */
+static gboolean uri_security_check(RemoteURI *uri, TextView *textview)
{
- textview->cur_pos =
- gtk_editable_get_position(GTK_EDITABLE(textview->text));
+ gchar *clicked_str;
+ gboolean retval = TRUE;
+
+ if (g_ascii_strncasecmp(uri->uri, "http:", 5) &&
+ g_ascii_strncasecmp(uri->uri, "https:", 6) &&
+ g_ascii_strncasecmp(uri->uri, "www.", 4))
+ return retval;
+
+ clicked_str = gtk_editable_get_chars(GTK_EDITABLE(textview->text),
+ uri->start,
+ uri->end);
+ if (clicked_str == NULL)
+ return TRUE;
+
+ if (strcmp(clicked_str, uri->uri) &&
+ (!g_ascii_strncasecmp(clicked_str, "http:", 5) ||
+ !g_ascii_strncasecmp(clicked_str, "https:", 6) ||
+ !g_ascii_strncasecmp(clicked_str, "www.", 4))) {
+ gchar *str;
+ retval = FALSE;
+
+ /* allow uri->uri == http://somewhere.com
+ and clicked_str == somewhere.com */
+ str = g_strconcat("http://", clicked_str, NULL);
- if (event &&
- ((event->button == 1 && event->type == GDK_2BUTTON_PRESS)
- || event->button == 2 || event->button == 3)) {
+ if (!g_ascii_strcasecmp(str, uri->uri))
+ retval = TRUE;
+ g_free(str);
+ }
+
+ if (retval == FALSE) {
+ gchar *msg = NULL;
+ AlertValue resp;
+
+ msg = g_strdup_printf(_("The real URL (%s) is different from\n"
+ "the apparent URL (%s). \n"
+ "Open it anyway?"),
+ uri->uri, clicked_str);
+ resp = alertpanel_with_type(_("Warning"),
+ msg,
+ _("Yes"),
+ _("No"),
+ NULL, NULL, ALERT_WARNING);
+ g_free(msg);
+ if (resp == G_ALERTDEFAULT)
+ retval = TRUE;
+ }
+ g_free(clicked_str);
+ return retval;
+}
+
+static gboolean textview_motion_notify(GtkWidget *widget,
+ GdkEventMotion *event,
+ TextView *textview)
+{
+ textview_uri_update(textview, event->x, event->y);
+ gdk_window_get_pointer(widget->window, NULL, NULL, NULL);
+
+ return FALSE;
+}
+
+static gboolean textview_leave_notify(GtkWidget *widget,
+ GdkEventCrossing *event,
+ TextView *textview)
+{
+ textview_uri_update(textview, -1, -1);
+
+ return FALSE;
+}
+
+static gboolean textview_visibility_notify(GtkWidget *widget,
+ GdkEventVisibility *event,
+ TextView *textview)
+{
+ gint wx, wy;
+
+ gdk_window_get_pointer(widget->window, &wx, &wy, NULL);
+ textview_uri_update(textview, wx, wy);
+
+ return FALSE;
+}
+
+static void textview_uri_update(TextView *textview, gint x, gint y)
+{
+ GtkTextBuffer *buffer;
+ GtkTextIter start_iter, end_iter;
+ RemoteURI *uri = NULL;
+
+ buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
+
+ if (x != -1 && y != -1) {
+ gint bx, by;
+ GtkTextIter iter;
+ GSList *tags;
GSList *cur;
+
+ gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(textview->text),
+ GTK_TEXT_WINDOW_WIDGET,
+ x, y, &bx, &by);
+ gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(textview->text),
+ &iter, bx, by);
+
+ tags = gtk_text_iter_get_tags(&iter);
+ for (cur = tags; cur != NULL; cur = cur->next) {
+ GtkTextTag *tag = cur->data;
+ char *name;
+
+ g_object_get(G_OBJECT(tag), "name", &name, NULL);
+ if (!strcmp(name, "link")
+ && textview_get_uri_range(textview, &iter, tag,
+ &start_iter, &end_iter))
+ uri = textview_get_uri_from_range(textview,
+ &iter, tag,
+ &start_iter,
+ &end_iter);
+ g_free(name);
+
+ if (uri)
+ break;
+ }
+ g_slist_free(tags);
+ }
+
+ if (uri != textview->uri_hover) {
+ GdkWindow *window;
+
+ if (textview->uri_hover)
+ gtk_text_buffer_remove_tag_by_name(buffer,
+ "link-hover",
+ &textview->uri_hover_start_iter,
+ &textview->uri_hover_end_iter);
+
+ textview->uri_hover = uri;
+ if (uri) {
+ textview->uri_hover_start_iter = start_iter;
+ textview->uri_hover_end_iter = end_iter;
+ }
+
+ window = gtk_text_view_get_window(GTK_TEXT_VIEW(textview->text),
+ GTK_TEXT_WINDOW_TEXT);
+ gdk_window_set_cursor(window, uri ? hand_cursor : NULL);
+
+ TEXTVIEW_STATUSBAR_POP(textview);
+
+ if (uri) {
+ char *trimmed_uri;
+
+ gtk_text_buffer_apply_tag_by_name(buffer,
+ "link-hover",
+ &start_iter,
+ &end_iter);
+
+ trimmed_uri = trim_string(uri->uri, 60);
+ TEXTVIEW_STATUSBAR_PUSH(textview, trimmed_uri);
+ g_free(trimmed_uri);
+ }
+ }
+}
+
+static gboolean textview_get_uri_range(TextView *textview,
+ GtkTextIter *iter,
+ GtkTextTag *tag,
+ GtkTextIter *start_iter,
+ GtkTextIter *end_iter)
+{
+ GtkTextIter _start_iter, _end_iter;
+
+ _start_iter = *iter;
+ if(!gtk_text_iter_backward_to_tag_toggle(&_start_iter, tag)) {
+ debug_print("Can't find start.");
+ return FALSE;
+ }
+
+ _end_iter = *iter;
+ if(!gtk_text_iter_forward_to_tag_toggle(&_end_iter, tag)) {
+ debug_print("Can't find end");
+ return FALSE;
+ }
+
+ *start_iter = _start_iter;
+ *end_iter = _end_iter;
+
+ return TRUE;
+}
+
+static RemoteURI *textview_get_uri_from_range(TextView *textview,
+ GtkTextIter *iter,
+ GtkTextTag *tag,
+ GtkTextIter *start_iter,
+ GtkTextIter *end_iter)
+{
+ gint start_pos, end_pos;
+ RemoteURI *uri = NULL;
+ GSList *cur;
+
+ start_pos = gtk_text_iter_get_offset(start_iter);
+ end_pos = gtk_text_iter_get_offset(end_iter);
+
+ for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
+ RemoteURI *uri_ = (RemoteURI *)cur->data;
- /* double click seems to set the cursor after the current
- * word. The cursor position needs fixing, otherwise the
- * last word of a clickable zone will not work */
- if (event->button == 1 && event->type == GDK_2BUTTON_PRESS) {
- textview->cur_pos--;
+ if (start_pos == uri_->start &&
+ end_pos == uri_->end) {
+ uri = uri_;
+ break;
}
+ }
+
+ return uri;
+}
+
+static RemoteURI *textview_get_uri(TextView *textview,
+ GtkTextIter *iter,
+ GtkTextTag *tag)
+{
+ GtkTextIter start_iter, end_iter;
+ RemoteURI *uri = NULL;
+
+ if (textview_get_uri_range(textview, iter, tag, &start_iter,
+ &end_iter))
+ uri = textview_get_uri_from_range(textview, iter, tag,
+ &start_iter, &end_iter);
+
+ return uri;
+}
+
+static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
+ GdkEvent *event, GtkTextIter *iter,
+ TextView *textview)
+{
+ GdkEventButton *bevent;
+ RemoteURI *uri = NULL;
+ GSList *cur;
+ gchar *trimmed_uri;
+
+ if (!event)
+ return FALSE;
+
+ if (event->type != GDK_BUTTON_PRESS && event->type != GDK_2BUTTON_PRESS
+ && event->type != GDK_MOTION_NOTIFY)
+ return FALSE;
+
+ uri = textview_get_uri(textview, iter, tag);
+ if (!uri)
+ return FALSE;
+
+ bevent = (GdkEventButton *) event;
+
+ /* doubleclick: open compose / add address / browser */
+ if ((event->type == GDK_BUTTON_PRESS && bevent->button == 1) ||
+ bevent->button == 2 || bevent->button == 3) {
+ if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
+ if (bevent->button == 3) {
+ gchar *fromname, *fromaddress;
+
+ /* extract url */
+ fromaddress = g_strdup(uri->uri + 7);
+ /* Hiroyuki: please put this function in utils.c! */
+ fromname = procheader_get_fromname(fromaddress);
+ extract_address(fromaddress);
+ g_message("adding from textview %s <%s>", fromname, fromaddress);
+ /* Add to address book - Match */
+ addressbook_add_contact( fromname, fromaddress, NULL );
+
+ g_free(fromaddress);
+ g_free(fromname);
+ } else {
+ PrefsAccount *account = NULL;
- for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
- RemoteURI *uri = (RemoteURI *)cur->data;
-
- if (textview->cur_pos >= uri->start &&
- textview->cur_pos < uri->end) {
- if (!g_strncasecmp(uri->uri, "mailto:", 7)) {
- if (event->button == 3) {
- gchar *fromname, *fromaddress;
- /* extract url */
- fromaddress = g_strdup(uri->uri + 7);
- /* Hiroyuki: please put this function in utils.c! */
- fromname = procheader_get_fromname(fromaddress);
- extract_address(fromaddress);
- g_message("adding from textview %s <%s>", fromname, fromaddress);
- // Add to address book - Match
- addressbook_add_contact( fromname, fromaddress, NULL );
- g_free(fromaddress);
- g_free(fromname);
- } else {
- compose_new_with_recipient
- (NULL, uri->uri + 7);
- }
- } else {
- open_uri(uri->uri,
- prefs_common.uri_cmd);
+ if (textview->messageview && textview->messageview->msginfo &&
+ textview->messageview->msginfo->folder) {
+ FolderItem *folder_item;
+
+ folder_item = textview->messageview->msginfo->folder;
+ if (folder_item->prefs && folder_item->prefs->enable_default_account)
+ account = account_find_from_id(folder_item->prefs->default_account);
}
+ compose_new(account, uri->uri + 7, NULL);
}
+ return TRUE;
+ } else {
+ if (textview_uri_security_check(textview, uri) == TRUE)
+ open_uri(uri->uri,
+ prefs_common.uri_cmd);
+ return TRUE;
}
}
+
+ return FALSE;
+}
+
+/*!
+ *\brief Check to see if a web URL has been disguised as a different
+ * URL (possible with HTML email).
+ *
+ *\param uri The uri to check
+ *
+ *\param textview The TextView the URL is contained in
+ *
+ *\return gboolean TRUE if the URL is ok, or if the user chose to open
+ * it anyway, otherwise FALSE
+ */
+static gboolean textview_uri_security_check(TextView *textview, RemoteURI *uri)
+{
+ gchar *visible_str;
+ gboolean retval = TRUE;
+
+ if (is_uri_string(uri->uri) == FALSE)
+ return TRUE;
+
+ visible_str = gtk_editable_get_chars(GTK_EDITABLE(textview->text),
+ uri->start, uri->end);
+ if (visible_str == NULL)
+ return TRUE;
+
+ if (strcmp(visible_str, uri->uri) != 0 && is_uri_string(visible_str)) {
+ gchar *uri_path;
+ gchar *visible_uri_path;
+
+ uri_path = get_uri_path(uri->uri);
+ visible_uri_path = get_uri_path(visible_str);
+ if (strcmp(uri_path, visible_uri_path) != 0)
+ retval = FALSE;
+ }
+
+ if (retval == FALSE) {
+ gchar *msg;
+ AlertValue aval;
+
+ msg = g_strdup_printf(_("The real URL (%s) is different from\n"
+ "the apparent URL (%s).\n"
+ "Open it anyway?"),
+ uri->uri, visible_str);
+ aval = alertpanel_with_type(_("Warning"), msg, _("Yes"), _("No"), NULL,
+ NULL, ALERT_WARNING);
+ g_free(msg);
+ if (aval == G_ALERTDEFAULT)
+ retval = TRUE;
+ }
+
+ g_free(visible_str);
+
+ return retval;
}
static void textview_uri_list_remove_all(GSList *uri_list)