claws-mail/src/textview.c

1815 lines
48 KiB
C
Raw Normal View History

2001-04-19 14:21:46 +02:00
/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
2002-01-21 12:34:09 +01:00
* Copyright (C) 1999-2002 Hiroyuki Yamamoto
2001-04-19 14:21:46 +02:00
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "defs.h"
#include <glib.h>
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtkvbox.h>
#include <gtk/gtkscrolledwindow.h>
#include <gtk/gtksignal.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "intl.h"
#include "main.h"
#include "summaryview.h"
#include "procheader.h"
#include "prefs_common.h"
#include "codeconv.h"
2002-01-06 13:21:36 +01:00
#include "gtkstext.h"
2001-04-19 14:21:46 +02:00
#include "utils.h"
#include "gtkutils.h"
#include "procmime.h"
#include "html.h"
#include "enriched.h"
2001-04-19 14:21:46 +02:00
#include "compose.h"
#include "addressbook.h"
2001-05-06 22:06:56 +02:00
#include "displayheader.h"
#include "account.h"
2001-04-19 14:21:46 +02:00
#define FONT_LOAD(font, s) \
{ \
gchar *fontstr, *p; \
\
Xstrdup_a(fontstr, s, ); \
if ((p = strchr(fontstr, ',')) != NULL) *p = '\0'; \
font = gdk_font_load(fontstr); \
2002-01-21 12:34:09 +01:00
if (!font) \
g_warning("Couldn't load the font '%s'\n", fontstr); \
2001-04-19 14:21:46 +02:00
}
typedef struct _RemoteURI RemoteURI;
struct _RemoteURI
{
gchar *uri;
guint start;
guint end;
};
static GdkColor quote_colors[3] = {
{(gulong)0, (gushort)0, (gushort)0, (gushort)0},
{(gulong)0, (gushort)0, (gushort)0, (gushort)0},
{(gulong)0, (gushort)0, (gushort)0, (gushort)0}
};
static GdkColor uri_color = {
(gulong)0,
(gushort)0,
(gushort)0,
(gushort)0
};
static GdkColor emphasis_color = {
(gulong)0,
(gushort)0,
(gushort)0,
(gushort)0xcfff
};
2002-03-02 11:02:42 +01:00
#if 0
2001-04-19 14:21:46 +02:00
static GdkColor error_color = {
(gulong)0,
(gushort)0xefff,
(gushort)0,
(gushort)0
};
2002-03-02 11:02:42 +01:00
#endif
2001-04-19 14:21:46 +02:00
2002-02-25 10:25:17 +01:00
static GdkFont *text_sb_font;
static GdkFont *text_mb_font;
static gint text_sb_font_orig_ascent;
static gint text_sb_font_orig_descent;
static gint text_mb_font_orig_ascent;
static gint text_mb_font_orig_descent;
2001-04-19 14:21:46 +02:00
static GdkFont *spacingfont;
static void textview_show_ertf (TextView *textview,
FILE *fp,
CodeConverter *conv);
2002-03-02 11:02:42 +01:00
static void textview_add_part (TextView *textview,
MimeInfo *mimeinfo,
FILE *fp);
2002-05-18 17:22:17 +02:00
static void textview_add_parts (TextView *textview,
MimeInfo *mimeinfo,
FILE *fp);
2002-03-02 11:02:42 +01:00
static void textview_write_body (TextView *textview,
MimeInfo *mimeinfo,
FILE *fp,
2002-03-07 14:22:39 +01:00
const gchar *charset);
2001-04-19 14:21:46 +02:00
static void textview_show_html (TextView *textview,
FILE *fp,
CodeConverter *conv);
static void textview_write_line (TextView *textview,
const gchar *str,
CodeConverter *conv);
2001-10-29 16:42:21 +01:00
static void textview_write_link (TextView *textview,
const gchar *url,
const gchar *str,
CodeConverter *conv);
2001-04-19 14:21:46 +02:00
static GPtrArray *textview_scan_header (TextView *textview,
FILE *fp);
static void textview_show_header (TextView *textview,
GPtrArray *headers);
2002-01-29 11:03:33 +01:00
static gint textview_key_pressed (GtkWidget *widget,
2001-04-19 14:21:46 +02:00
GdkEventKey *event,
TextView *textview);
static gint textview_button_pressed (GtkWidget *widget,
GdkEventButton *event,
TextView *textview);
static gint textview_button_released (GtkWidget *widget,
2001-04-19 14:21:46 +02:00
GdkEventButton *event,
TextView *textview);
static void textview_uri_list_remove_all(GSList *uri_list);
static void textview_smooth_scroll_do (TextView *textview,
gfloat old_value,
gfloat last_value,
gint step);
static void textview_smooth_scroll_one_line (TextView *textview,
gboolean up);
static gboolean textview_smooth_scroll_page (TextView *textview,
gboolean up);
TextView *textview_create(void)
{
TextView *textview;
GtkWidget *vbox;
GtkWidget *scrolledwin_sb;
GtkWidget *scrolledwin_mb;
GtkWidget *text_sb;
GtkWidget *text_mb;
2002-08-15 09:38:17 +02:00
debug_print("Creating text view...\n");
2001-04-19 14:21:46 +02:00
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),
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);
2002-01-06 13:21:36 +01:00
/* create GtkSText widgets for single-byte and multi-byte character */
text_sb = gtk_stext_new(NULL, NULL);
text_mb = gtk_stext_new(NULL, NULL);
GTK_STEXT(text_sb)->default_tab_width = 8;
GTK_STEXT(text_mb)->default_tab_width = 8;
2001-04-19 14:21:46 +02:00
gtk_widget_show(text_sb);
gtk_widget_show(text_mb);
2002-01-06 13:21:36 +01:00
gtk_stext_set_word_wrap(GTK_STEXT(text_sb), TRUE);
gtk_stext_set_word_wrap(GTK_STEXT(text_mb), TRUE);
2001-04-19 14:21:46 +02:00
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;
2002-01-21 12:34:09 +01:00
GdkFont *font;
FONT_LOAD(font, prefs_common.normalfont);
if (font) {
style = gtk_style_copy(text_sb->style);
gdk_font_unref(style->font);
style->font = font;
gtk_widget_set_style(text_sb, style);
}
2001-04-19 14:21:46 +02:00
}
if (text_mb->style && text_mb->style->font->type == GDK_FONT_FONT) {
GtkStyle *style;
2002-01-21 12:34:09 +01:00
GdkFont *font;
font = gdk_fontset_load(prefs_common.normalfont);
if (font) {
style = gtk_style_copy(text_mb->style);
gdk_font_unref(style->font);
style->font = font;
gtk_widget_set_style(text_mb, style);
}
2001-04-19 14:21:46 +02:00
}
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);
2001-09-30 13:07:34 +02:00
gtk_signal_connect_after(GTK_OBJECT(text_sb), "button_press_event",
GTK_SIGNAL_FUNC(textview_button_pressed),
textview);
gtk_signal_connect_after(GTK_OBJECT(text_sb), "button_release_event",
GTK_SIGNAL_FUNC(textview_button_released),
textview);
2001-04-19 14:21:46 +02:00
gtk_signal_connect(GTK_OBJECT(text_mb), "key_press_event",
GTK_SIGNAL_FUNC(textview_key_pressed),
textview);
2001-09-30 13:07:34 +02:00
gtk_signal_connect_after(GTK_OBJECT(text_mb), "button_press_event",
GTK_SIGNAL_FUNC(textview_button_pressed),
textview);
gtk_signal_connect_after(GTK_OBJECT(text_mb), "button_release_event",
GTK_SIGNAL_FUNC(textview_button_released),
textview);
2001-04-19 14:21:46 +02:00
gtk_widget_show(scrolledwin_sb);
gtk_widget_show(scrolledwin_mb);
vbox = gtk_vbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), scrolledwin_sb, TRUE, TRUE, 0);
2002-02-25 10:25:17 +01:00
gtk_widget_show(vbox);
2002-02-04 10:56:44 +01:00
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;
textview->show_all_headers = FALSE;
textview->last_buttonpress = GDK_NOTHING;
2002-05-08 08:27:40 +02:00
textview->show_url_msgid = 0;
2001-04-19 14:21:46 +02:00
return textview;
}
void textview_init(TextView *textview)
{
gtkut_widget_disable_theme_engine(textview->text_sb);
gtkut_widget_disable_theme_engine(textview->text_mb);
textview_update_message_colors();
2002-02-04 10:56:44 +01:00
textview_set_all_headers(textview, FALSE);
2001-04-19 14:21:46 +02:00
textview_set_font(textview, NULL);
}
void textview_update_message_colors(void)
{
GdkColor black = {0, 0, 0, 0};
if (prefs_common.enable_color) {
/* grab the quote colors, converting from an int to a GdkColor */
gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_col,
&quote_colors[0]);
gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_col,
&quote_colors[1]);
gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_col,
&quote_colors[2]);
gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
&uri_color);
} else {
quote_colors[0] = quote_colors[1] = quote_colors[2] =
uri_color = emphasis_color = black;
}
}
void textview_show_message(TextView *textview, MimeInfo *mimeinfo,
const gchar *file)
{
2002-05-25 07:46:08 +02:00
GtkSText *text;
2001-04-19 14:21:46 +02:00
FILE *fp;
2002-03-02 11:02:42 +01:00
const gchar *charset = NULL;
GPtrArray *headers = NULL;
2001-04-19 14:21:46 +02:00
2002-03-14 11:17:32 +01:00
if ((fp = fopen(file, "rb")) == NULL) {
2001-04-19 14:21:46 +02:00
FILE_OP_ERROR(file, "fopen");
return;
}
2002-03-02 11:02:42 +01:00
if (prefs_common.force_charset)
charset = prefs_common.force_charset;
else if (mimeinfo->charset)
charset = mimeinfo->charset;
textview_set_font(textview, charset);
textview_clear(textview);
2002-05-25 07:46:08 +02:00
text = GTK_STEXT(textview->text);
2002-03-03 10:32:58 +01:00
gtk_stext_freeze(text);
2002-03-02 11:02:42 +01:00
if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0) perror("fseek");
headers = textview_scan_header(textview, fp);
if (headers) {
textview_show_header(textview, headers);
procheader_header_array_destroy(headers);
2002-03-03 10:32:58 +01:00
textview->body_pos = gtk_stext_get_length(text);
2002-03-02 11:02:42 +01:00
}
2002-05-18 17:22:17 +02:00
textview_add_parts(textview, mimeinfo, fp);
2001-04-19 14:21:46 +02:00
2002-03-03 10:32:58 +01:00
gtk_stext_thaw(text);
2001-04-19 14:21:46 +02:00
fclose(fp);
}
void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
{
2002-05-25 07:46:08 +02:00
GtkSText *text;
2001-04-19 14:21:46 +02:00
gchar buf[BUFFSIZE];
const gchar *boundary = NULL;
gint boundary_len = 0;
const gchar *charset = NULL;
GPtrArray *headers = NULL;
2002-05-25 07:46:08 +02:00
gboolean is_rfc822_part = FALSE;
2001-04-19 14:21:46 +02:00
g_return_if_fail(mimeinfo != NULL);
g_return_if_fail(fp != NULL);
2002-05-18 17:22:17 +02:00
if (mimeinfo->mime_type == MIME_MULTIPART) {
textview_clear(textview);
textview_add_parts(textview, mimeinfo, fp);
return;
}
2002-03-03 10:32:58 +01:00
2001-04-19 14:21:46 +02:00
if (mimeinfo->parent && mimeinfo->parent->boundary) {
boundary = mimeinfo->parent->boundary;
boundary_len = strlen(boundary);
}
2002-03-02 11:02:42 +01:00
if (!boundary && (mimeinfo->mime_type == MIME_TEXT ||
mimeinfo->mime_type == MIME_TEXT_HTML ||
mimeinfo->mime_type == MIME_TEXT_ENRICHED)) {
2001-04-19 14:21:46 +02:00
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;
2001-11-18 13:23:39 +01:00
MimeInfo *parent = mimeinfo->parent;
2001-11-27 08:46:42 +01:00
while (parent->parent) {
if (parent->main &&
parent->main->mime_type ==
MIME_MESSAGE_RFC822)
break;
2001-11-18 13:23:39 +01:00
parent = parent->parent;
2001-11-27 08:46:42 +01:00
}
2001-04-19 14:21:46 +02:00
if ((fpos = ftell(fp)) < 0)
perror("ftell");
2001-11-18 13:23:39 +01:00
else if (fseek(fp, parent->fpos, SEEK_SET) < 0)
2001-04-19 14:21:46 +02:00
perror("fseek");
else {
headers = textview_scan_header(textview, fp);
if (fseek(fp, fpos, SEEK_SET) < 0)
perror("fseek");
}
}
2001-11-27 08:46:42 +01:00
/* skip MIME part headers */
2001-04-19 14:21:46 +02:00
while (fgets(buf, sizeof(buf), fp) != NULL)
if (buf[0] == '\r' || buf[0] == '\n') break;
}
/* display attached RFC822 single text message */
2002-05-18 17:22:17 +02:00
if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
2001-07-15 15:54:20 +02:00
if (headers) procheader_header_array_destroy(headers);
2002-05-18 17:22:17 +02:00
if (!mimeinfo->sub) {
textview_clear(textview);
return;
}
2001-04-19 14:21:46 +02:00
headers = textview_scan_header(textview, fp);
mimeinfo = mimeinfo->sub;
2002-05-25 07:46:08 +02:00
is_rfc822_part = TRUE;
2001-04-19 14:21:46 +02:00
}
if (prefs_common.force_charset)
charset = prefs_common.force_charset;
else if (mimeinfo->charset)
charset = mimeinfo->charset;
textview_set_font(textview, charset);
2002-05-25 07:46:08 +02:00
text = GTK_STEXT(textview->text);
2002-01-06 13:21:36 +01:00
gtk_stext_freeze(text);
2002-05-25 07:46:08 +02:00
textview_clear(textview);
2001-04-19 14:21:46 +02:00
if (headers) {
textview_show_header(textview, headers);
2001-05-23 05:04:46 +02:00
procheader_header_array_destroy(headers);
2002-03-03 10:32:58 +01:00
textview->body_pos = gtk_stext_get_length(text);
2002-05-18 17:22:17 +02:00
if (!mimeinfo->main)
gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
2001-04-19 14:21:46 +02:00
}
2002-05-25 07:46:08 +02:00
if (mimeinfo->mime_type == MIME_MULTIPART || is_rfc822_part)
2002-05-18 17:22:17 +02:00
textview_add_parts(textview, mimeinfo, fp);
else
textview_write_body(textview, mimeinfo, fp, charset);
2002-03-02 11:02:42 +01:00
gtk_stext_thaw(text);
}
static void textview_add_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
{
2002-03-03 10:32:58 +01:00
GtkSText *text = GTK_STEXT(textview->text);
2002-03-02 11:02:42 +01:00
gchar buf[BUFFSIZE];
const gchar *boundary = NULL;
gint boundary_len = 0;
const gchar *charset = NULL;
GPtrArray *headers = NULL;
g_return_if_fail(mimeinfo != NULL);
g_return_if_fail(fp != NULL);
2002-03-03 10:32:58 +01:00
if (mimeinfo->mime_type == MIME_MULTIPART) return;
2002-03-07 14:22:39 +01:00
if (!mimeinfo->parent &&
mimeinfo->mime_type != MIME_TEXT &&
mimeinfo->mime_type != MIME_TEXT_HTML &&
mimeinfo->mime_type != MIME_TEXT_ENRICHED)
return;
2002-03-02 11:02:42 +01:00
if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0) {
perror("fseek");
return;
}
if (mimeinfo->parent && mimeinfo->parent->boundary) {
boundary = mimeinfo->parent->boundary;
boundary_len = strlen(boundary);
2001-04-19 14:21:46 +02:00
}
2002-03-02 11:02:42 +01:00
while (fgets(buf, sizeof(buf), fp) != NULL)
if (buf[0] == '\r' || buf[0] == '\n') break;
2002-03-03 10:32:58 +01:00
if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
2002-03-02 11:02:42 +01:00
headers = textview_scan_header(textview, fp);
2002-03-03 10:32:58 +01:00
if (headers) {
2002-05-25 07:46:08 +02:00
gtk_stext_freeze(text);
2002-03-03 10:32:58 +01:00
gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
textview_show_header(textview, headers);
procheader_header_array_destroy(headers);
2002-05-25 07:46:08 +02:00
gtk_stext_thaw(text);
2002-03-03 10:32:58 +01:00
}
return;
2002-03-02 11:02:42 +01:00
}
2001-04-19 14:21:46 +02:00
2002-03-02 11:02:42 +01:00
gtk_stext_freeze(text);
2002-08-06 11:34:13 +02:00
#if USE_GPGME
if (mimeinfo->sigstatus)
g_snprintf(buf, sizeof(buf), "\n[%s (%s)]\n",
mimeinfo->content_type, mimeinfo->sigstatus);
else
#endif
2002-03-03 10:32:58 +01:00
if (mimeinfo->filename || mimeinfo->name)
g_snprintf(buf, sizeof(buf), "\n[%s %s (%d bytes)]\n",
mimeinfo->filename ? mimeinfo->filename :
mimeinfo->name,
mimeinfo->content_type, mimeinfo->size);
else
g_snprintf(buf, sizeof(buf), "\n[%s (%d bytes)]\n",
mimeinfo->content_type, mimeinfo->size);
2002-03-02 11:02:42 +01:00
if (mimeinfo->mime_type != MIME_TEXT &&
mimeinfo->mime_type != MIME_TEXT_HTML &&
mimeinfo->mime_type != MIME_TEXT_ENRICHED) {
gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
} else {
2002-03-03 10:32:58 +01:00
if (!mimeinfo->main &&
mimeinfo->parent &&
mimeinfo->parent->children != mimeinfo)
gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
else
gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
if (prefs_common.force_charset)
charset = prefs_common.force_charset;
else if (mimeinfo->charset)
charset = mimeinfo->charset;
2002-03-07 14:22:39 +01:00
textview_write_body(textview, mimeinfo, fp, charset);
2002-03-02 11:02:42 +01:00
}
2002-01-06 13:21:36 +01:00
gtk_stext_thaw(text);
2001-04-19 14:21:46 +02:00
}
2002-05-18 17:22:17 +02:00
static void textview_add_parts(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
{
gint level;
g_return_if_fail(mimeinfo != NULL);
g_return_if_fail(fp != NULL);
level = mimeinfo->level;
for (;;) {
textview_add_part(textview, mimeinfo, fp);
if (mimeinfo->parent && mimeinfo->parent->content_type &&
!strcasecmp(mimeinfo->parent->content_type,
"multipart/alternative"))
mimeinfo = mimeinfo->parent->next;
else
mimeinfo = procmime_mimeinfo_next(mimeinfo);
if (!mimeinfo || mimeinfo->level <= level)
break;
}
}
2001-05-10 13:19:38 +02:00
#define TEXT_INSERT(str) \
2002-01-06 13:21:36 +01:00
gtk_stext_insert(text, textview->msgfont, NULL, NULL, str, -1)
2001-05-10 13:19:38 +02:00
2001-04-19 14:21:46 +02:00
void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
{
2002-01-06 13:21:36 +01:00
GtkSText *text;
2001-04-19 14:21:46 +02:00
if (!partinfo) return;
textview_set_font(textview, NULL);
2002-01-06 13:21:36 +01:00
text = GTK_STEXT(textview->text);
2001-04-19 14:21:46 +02:00
textview_clear(textview);
2002-01-06 13:21:36 +01:00
gtk_stext_freeze(text);
2001-04-19 14:21:46 +02:00
2001-05-10 13:19:38 +02:00
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_INSERT(_("To display this part as a text message, select "));
TEXT_INSERT(_("`Display as text', or press `t' key.\n\n"));
TEXT_INSERT(_("To display this part as an image, select "));
TEXT_INSERT(_("`Display image', or press `i' key.\n\n"));
2001-05-10 13:19:38 +02:00
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."));
2001-04-19 14:21:46 +02:00
2002-01-06 13:21:36 +01:00
gtk_stext_thaw(text);
2001-04-19 14:21:46 +02:00
}
#if USE_GPGME
void textview_show_signature_part(TextView *textview, MimeInfo *partinfo)
{
2002-01-06 13:21:36 +01:00
GtkSText *text;
2001-04-19 14:21:46 +02:00
if (!partinfo) return;
textview_set_font(textview, NULL);
2002-01-06 13:21:36 +01:00
text = GTK_STEXT(textview->text);
2001-04-19 14:21:46 +02:00
textview_clear(textview);
2002-01-06 13:21:36 +01:00
gtk_stext_freeze(text);
2001-04-19 14:21:46 +02:00
if (partinfo->sigstatus_full == NULL) {
2001-05-10 13:19:38 +02:00
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"));
2001-04-19 14:21:46 +02:00
} else {
2001-05-10 13:19:38 +02:00
TEXT_INSERT(partinfo->sigstatus_full);
2001-04-19 14:21:46 +02:00
}
2002-01-06 13:21:36 +01:00
gtk_stext_thaw(text);
2001-04-19 14:21:46 +02:00
}
#endif /* USE_GPGME */
2001-05-10 13:19:38 +02:00
#undef TEXT_INSERT
2002-03-02 11:02:42 +01:00
static void textview_write_body(TextView *textview, MimeInfo *mimeinfo,
2002-03-07 14:22:39 +01:00
FILE *fp, const gchar *charset)
2002-03-02 11:02:42 +01:00
{
FILE *tmpfp;
gchar buf[BUFFSIZE];
2002-03-07 14:22:39 +01:00
CodeConverter *conv;
conv = conv_code_converter_new(charset);
2002-03-02 11:02:42 +01:00
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);
}
2002-03-07 14:22:39 +01:00
conv_code_converter_destroy(conv);
2002-03-02 11:02:42 +01:00
}
2001-04-19 14:21:46 +02:00
static void textview_show_html(TextView *textview, FILE *fp,
CodeConverter *conv)
{
HTMLParser *parser;
gchar *str;
2001-10-29 16:42:21 +01:00
gchar* url = NULL;
2001-04-19 14:21:46 +02:00
parser = html_parser_new(fp, conv);
g_return_if_fail(parser != NULL);
while ((str = html_parse(parser)) != NULL) {
2001-10-29 16:42:21 +01:00
if (parser->state == HTML_HREF) {
/* first time : get and copy the URL */
if (url == 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);
/* the URL may (or not) be followed by the
* referenced text */
str = strtok(NULL, "");
}
2001-10-29 16:42:21 +01:00
}
if (str != NULL) {
textview_write_link(textview, url, str, NULL);
}
} else {
if (url != NULL) {
free(url);
url = NULL;
}
textview_write_line(textview, str, NULL);
}
2001-04-19 14:21:46 +02:00
}
2001-04-19 14:21:46 +02:00
html_parser_destroy(parser);
}
static void textview_show_ertf(TextView *textview, FILE *fp,
CodeConverter *conv)
{
ERTFParser *parser;
gchar *str;
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);
2001-04-19 14:21:46 +02:00
}
/* get_uri_part() - retrieves a URI starting from scanpos.
Returns TRUE if succesful */
static gboolean get_uri_part(const gchar *start, const gchar *scanpos,
const gchar **bp, const gchar **ep)
{
const gchar *ep_;
g_return_val_if_fail(start != NULL, FALSE);
g_return_val_if_fail(scanpos != NULL, FALSE);
g_return_val_if_fail(bp != NULL, FALSE);
g_return_val_if_fail(ep != NULL, FALSE);
*bp = scanpos;
/* find end point of URI */
for (ep_ = scanpos; *ep_ != '\0'; ep_++) {
if (!isgraph(*ep_) || !isascii(*ep_) || strchr("()<>\"", *ep_))
break;
}
/* no punctuation at end of string */
/* FIXME: this stripping of trailing punctuations may bite with other URIs.
* should pass some URI type to this function and decide on that whether
* to perform punctuation stripping */
2001-11-19 10:46:04 +01:00
#define IS_REAL_PUNCT(ch) (ispunct(ch) && ((ch) != '/'))
2001-04-19 14:21:46 +02:00
for (; ep_ - 1 > scanpos + 1 && IS_REAL_PUNCT(*(ep_ - 1)); ep_--)
;
#undef IS_REAL_PUNCT
*ep = ep_;
return TRUE;
}
static gchar *make_uri_string(const gchar *bp, const gchar *ep)
{
return g_strndup(bp, ep - bp);
}
/* valid mail address characters */
#define IS_RFC822_CHAR(ch) \
(isascii(ch) && \
(ch) > 32 && \
(ch) != 127 && \
!isspace(ch) && \
!strchr("(),;<>\"", (ch)))
2001-04-19 14:21:46 +02:00
2001-11-19 10:46:04 +01:00
/* alphabet and number within 7bit ASCII */
#define IS_ASCII_ALNUM(ch) (isascii(ch) && isalnum(ch))
#define IS_QUOTE(ch) ((ch) == '\'' || (ch) == '"')
2001-04-19 14:21:46 +02:00
/* 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)
{
/* more complex than the uri part because we need to scan back and forward starting from
* the scan position. */
gboolean result = FALSE;
const gchar *bp_ = NULL;
const gchar *ep_ = NULL;
2001-04-19 14:21:46 +02:00
2001-05-14 11:32:34 +02:00
/* the informative part of the email address (describing the name
* of the email address owner) may contain quoted parts. the
* closure stack stores the last encountered quotes. */
gchar closure_stack[128];
gchar *ptr = closure_stack;
2001-04-19 14:21:46 +02:00
g_return_val_if_fail(start != NULL, FALSE);
g_return_val_if_fail(scanpos != NULL, FALSE);
g_return_val_if_fail(bp != NULL, FALSE);
g_return_val_if_fail(ep != NULL, FALSE);
/* scan start of address */
for (bp_ = scanpos - 1; bp_ >= start && IS_RFC822_CHAR(*bp_); bp_--)
;
/* TODO: should start with an alnum? */
bp_++;
2001-11-19 10:46:04 +01:00
for (; bp_ < scanpos && !IS_ASCII_ALNUM(*bp_); bp_++)
2001-04-19 14:21:46 +02:00
;
if (bp_ != scanpos) {
/* scan end of address */
for (ep_ = scanpos + 1; *ep_ && IS_RFC822_CHAR(*ep_); ep_++)
;
/* TODO: really should terminate with an alnum? */
2001-11-19 10:46:04 +01:00
for (; ep_ > scanpos && !IS_ASCII_ALNUM(*ep_); --ep_)
2001-04-19 14:21:46 +02:00
;
ep_++;
if (ep_ > scanpos + 1) {
*ep = ep_;
*bp = bp_;
result = TRUE;
}
}
2001-05-14 11:32:34 +02:00
if (!result) return FALSE;
/* skip if it's between quotes "'alfons@proteus.demon.nl'" <alfons@proteus.demon.nl> */
if (bp_ - 1 > start && IS_QUOTE(*(bp_ - 1)) && IS_QUOTE(*ep_))
return FALSE;
/* see if this is <bracketed>; in this case we also scan for the informative part. */
2001-05-14 11:32:34 +02:00
if (bp_ - 1 <= start || *(bp_ - 1) != '<' || *ep_ != '>')
return TRUE;
#define FULL_STACK() ((size_t) (ptr - closure_stack) >= sizeof closure_stack)
#define IN_STACK() (ptr > closure_stack)
/* has underrun check */
2001-05-14 11:32:34 +02:00
#define POP_STACK() if(IN_STACK()) --ptr
/* has overrun check */
2001-05-14 11:32:34 +02:00
#define PUSH_STACK(c) if(!FULL_STACK()) *ptr++ = (c); else return TRUE
/* has underrun check */
#define PEEK_STACK() (IN_STACK() ? *(ptr - 1) : 0)
2001-05-14 11:32:34 +02:00
ep_++;
/* scan for the informative part. */
for (bp_ -= 2; bp_ >= start; bp_--) {
/* if closure on the stack keep scanning */
if (PEEK_STACK() == *bp_) {
POP_STACK();
continue;
}
if (*bp_ == '\'' || *bp_ == '"') {
PUSH_STACK(*bp_);
continue;
}
/* if nothing in the closure stack, do the special conditions
* the following if..else expression simply checks whether
* a token is acceptable. if not acceptable, the clause
* should terminate the loop with a 'break' */
if (!PEEK_STACK()) {
if (*bp_ == '-'
&& (((bp_ - 1) >= start) && isalnum(*(bp_ - 1)))
&& (((bp_ + 1) < ep_) && isalnum(*(bp_ + 1)))) {
/* hyphens are allowed, but only in
between alnums */
} else if (!ispunct(*bp_)) {
/* but anything not being a punctiation
is ok */
} else {
break; /* anything else is rejected */
}
2001-05-14 11:32:34 +02:00
}
}
bp_++;
#undef PEEK_STACK
#undef PUSH_STACK
#undef POP_STACK
#undef IN_STACK
#undef FULL_STACK
2001-04-19 14:21:46 +02:00
2001-05-14 11:32:34 +02:00
/* scan forward (should start with an alnum) */
for (; *bp_ != '<' && isspace(*bp_) && *bp_ != '"'; bp_++)
;
*ep = ep_;
*bp = bp_;
2001-04-19 14:21:46 +02:00
return result;
}
#undef IS_QUOTE
2001-04-19 14:21:46 +02:00
#undef IS_RFC822_CHAR
static gchar *make_email_string(const gchar *bp, const gchar *ep)
{
/* returns a mailto: URI; mailto: is also used to detect the
* uri type later on in the button_pressed signal handler */
gchar *tmp;
gchar *result;
tmp = g_strndup(bp, ep - bp);
result = g_strconcat("mailto:", 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->bp = (bp_); last->ep = (ep_); last->pti = (pti_); \
last->next = NULL; \
} else { \
g_warning("alloc error scanning URIs\n"); \
2002-01-06 13:21:36 +01:00
gtk_stext_insert(text, textview->msgfont, fg_color, NULL, \
2001-04-19 14:21:46 +02:00
linebuf, -1); \
return; \
}
/* textview_make_clickable_parts() - colorizes clickable parts */
2001-05-06 22:06:56 +02:00
static void textview_make_clickable_parts(TextView *textview,
2001-05-23 05:04:46 +02:00
GdkFont *font,
2001-04-19 14:21:46 +02:00
GdkColor *fg_color,
GdkColor *uri_color,
const gchar *linebuf)
{
/* parse table - in order of priority */
struct table {
const gchar *needle; /* token */
/* token search function */
gchar *(*search) (const gchar *haystack,
const gchar *needle);
/* part parsing function */
gboolean (*parse) (const gchar *start,
const gchar *scanpos,
const gchar **bp_,
const gchar **ep_);
/* part to URI function */
gchar *(*build_uri) (const gchar *bp,
const gchar *ep);
};
static struct table parser[] = {
{"http://", strcasestr, get_uri_part, make_uri_string},
{"https://", strcasestr, get_uri_part, make_uri_string},
{"www.", strcasestr, get_uri_part, make_uri_string},
2001-04-19 14:21:46 +02:00
{"ftp://", strcasestr, get_uri_part, make_uri_string},
{"mailto:", strcasestr, get_uri_part, make_uri_string},
{"@", strcasestr, get_email_part, make_email_string}
};
const gint PARSE_ELEMS = sizeof parser / sizeof parser[0];
gint n;
const gchar *walk, *bp, *ep;
struct txtpos {
const gchar *bp, *ep; /* text position */
gint pti; /* index in parse table */
struct txtpos *next; /* next */
} head = {NULL, NULL, 0, NULL}, *last = &head;
2002-01-06 13:21:36 +01:00
GtkSText *text = GTK_STEXT(textview->text);
2001-05-06 22:06:56 +02:00
2001-04-19 14:21:46 +02:00
/* parse for clickable parts, and build a list of begin and end positions */
for (walk = linebuf, n = 0;;) {
gint last_index = PARSE_ELEMS;
gchar *scanpos = NULL;
/* FIXME: this looks phony. scanning for anything in the parse table */
for (n = 0; n < PARSE_ELEMS; n++) {
gchar *tmp;
tmp = parser[n].search(walk, parser[n].needle);
if (tmp) {
if (scanpos == NULL || tmp < scanpos) {
scanpos = tmp;
last_index = n;
}
}
}
if (scanpos) {
/* check if URI can be parsed */
if (parser[last_index].parse(linebuf, scanpos, &bp, &ep)
&& (size_t) (ep - bp - 1) > strlen(parser[last_index].needle)) {
2001-04-19 14:21:46 +02:00
ADD_TXT_POS(bp, ep, last_index);
walk = ep;
} else
walk = scanpos +
strlen(parser[last_index].needle);
} else
break;
}
/* colorize this line */
if (head.next) {
const gchar *normal_text = linebuf;
/* insert URIs */
for (last = head.next; last != NULL;
normal_text = last->ep, last = last->next) {
RemoteURI *uri;
uri = g_new(RemoteURI, 1);
if (last->bp - normal_text > 0)
2002-01-06 13:21:36 +01:00
gtk_stext_insert(text, font,
2001-04-19 14:21:46 +02:00
fg_color, NULL,
normal_text,
last->bp - normal_text);
2002-01-06 13:21:36 +01:00
uri->uri = parser[last->pti].build_uri(last->bp,
2001-05-23 05:04:46 +02:00
last->ep);
2002-01-06 13:21:36 +01:00
uri->start = gtk_stext_get_point(text);
gtk_stext_insert(text, font, uri_color,
2001-04-19 14:21:46 +02:00
NULL, last->bp, last->ep - last->bp);
2002-01-06 13:21:36 +01:00
uri->end = gtk_stext_get_point(text);
2001-04-19 14:21:46 +02:00
textview->uri_list =
g_slist_append(textview->uri_list, uri);
}
if (*normal_text)
2002-01-06 13:21:36 +01:00
gtk_stext_insert(text, font, fg_color,
2001-04-19 14:21:46 +02:00
NULL, normal_text, -1);
} else
2002-01-06 13:21:36 +01:00
gtk_stext_insert(text, font, fg_color, NULL, linebuf, -1);
2001-04-19 14:21:46 +02:00
}
#undef ADD_TXT_POS
2001-10-29 16:42:21 +01:00
/* 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;
2002-01-06 13:21:36 +01:00
GtkSText *text = GTK_STEXT(textview->text);
2001-10-29 16:42:21 +01:00
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. */
2002-03-02 11:02:42 +01:00
#if 0
2001-10-29 16:42:21 +01:00
if (!conv)
strncpy2(buf, str, sizeof(buf));
else if (conv_convert(conv, buf, sizeof(buf), str) < 0) {
2002-01-06 13:21:36 +01:00
gtk_stext_insert(text, textview->msgfont,
2001-10-29 16:42:21 +01:00
prefs_common.enable_color
? &error_color : NULL, NULL,
"*** Warning: code conversion failed ***\n",
-1);
return;
}
2002-03-02 11:02:42 +01:00
#endif
2001-10-29 16:42:21 +01:00
2002-05-20 09:41:45 +02:00
if (!conv || conv_convert(conv, buf, sizeof(buf), str) < 0)
strncpy2(buf, str, sizeof(buf));
strcrchomp(buf);
gtk_stext_insert(text, textview->msgfont, NULL, NULL, " ", 1);
2001-10-29 16:42:21 +01:00
/* 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);
2002-01-06 13:21:36 +01:00
uri->start = gtk_stext_get_point(text);
2002-05-06 12:20:19 +02:00
gtk_stext_insert(text, textview->msgfont, link_color, NULL, str,
strlen(str));
2002-01-06 13:21:36 +01:00
uri->end = gtk_stext_get_point(text);
2001-10-29 16:42:21 +01:00
textview->uri_list = g_slist_append(textview->uri_list, uri);
}
2001-04-19 14:21:46 +02:00
static void textview_write_line(TextView *textview, const gchar *str,
CodeConverter *conv)
{
2002-01-06 13:21:36 +01:00
GtkSText *text = GTK_STEXT(textview->text);
2001-04-19 14:21:46 +02:00
gchar buf[BUFFSIZE];
GdkColor *fg_color;
gint quotelevel = -1;
2002-02-28 11:06:43 +01:00
#if 0
2001-04-19 14:21:46 +02:00
if (!conv)
strncpy2(buf, str, sizeof(buf));
else if (conv_convert(conv, buf, sizeof(buf), str) < 0) {
2002-01-06 13:21:36 +01:00
gtk_stext_insert(text, textview->msgfont,
2001-04-19 14:21:46 +02:00
prefs_common.enable_color
? &error_color : NULL, NULL,
"*** Warning: code conversion failed ***\n",
-1);
return;
}
2002-02-28 11:06:43 +01:00
#endif
if (!conv || conv_convert(conv, buf, sizeof(buf), str) < 0)
strncpy2(buf, str, sizeof(buf));
2001-04-19 14:21:46 +02:00
2001-11-07 11:29:45 +01:00
strcrchomp(buf);
2001-04-19 14:21:46 +02:00
if (prefs_common.conv_mb_alnum) conv_mb_alnum(buf);
fg_color = NULL;
/* change color of quotation
>, 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
&& line_has_quote_char(buf, prefs_common.quote_chars)) {
quotelevel = get_quote_level(buf, prefs_common.quote_chars);
2001-04-19 14:21:46 +02:00
/* set up the correct foreground color */
if (quotelevel > 2) {
/* recycle colors */
if (prefs_common.recycle_quote_colors)
quotelevel %= 3;
else
quotelevel = 2;
}
}
if (quotelevel == -1)
fg_color = NULL;
else
fg_color = &quote_colors[quotelevel];
if (prefs_common.head_space && spacingfont && buf[0] != '\n')
2002-01-06 13:21:36 +01:00
gtk_stext_insert(text, spacingfont, NULL, NULL, " ", 1);
2001-04-19 14:21:46 +02:00
if (prefs_common.enable_color)
2001-05-06 22:06:56 +02:00
textview_make_clickable_parts(textview, textview->msgfont,
2001-04-19 14:21:46 +02:00
fg_color, &uri_color, buf);
else
2001-07-15 15:54:20 +02:00
textview_make_clickable_parts(textview, textview->msgfont,
fg_color, NULL, buf);
2001-04-19 14:21:46 +02:00
}
void textview_clear(TextView *textview)
{
2002-01-06 13:21:36 +01:00
GtkSText *text = GTK_STEXT(textview->text);
2001-04-19 14:21:46 +02:00
2002-01-06 13:21:36 +01:00
gtk_stext_freeze(text);
gtk_stext_set_point(text, 0);
gtk_stext_forward_delete(text, gtk_stext_get_length(text));
gtk_stext_thaw(text);
2001-04-19 14:21:46 +02:00
textview_uri_list_remove_all(textview->uri_list);
textview->uri_list = NULL;
2002-05-18 17:22:17 +02:00
textview->body_pos = 0;
textview->cur_pos = 0;
2001-04-19 14:21:46 +02:00
}
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);
2002-02-25 10:25:17 +01:00
if (textview->msgfont)
2001-04-19 14:21:46 +02:00
gdk_font_unref(textview->msgfont);
if (textview->boldfont)
gdk_font_unref(textview->boldfont);
g_free(textview);
}
2002-02-04 10:56:44 +01:00
void textview_set_all_headers(TextView *textview, gboolean all_headers)
{
textview->show_all_headers = all_headers;
}
2001-04-19 14:21:46 +02:00
void textview_set_font(TextView *textview, const gchar *codeset)
{
gboolean use_fontset = TRUE;
2002-01-06 13:21:36 +01:00
/* In multi-byte mode, GtkSText can't display 8bit characters
2001-04-19 14:21:46 +02:00
correctly, so it must be single-byte mode. */
if (MB_CUR_MAX > 1) {
if (codeset) {
2001-05-08 15:15:53 +02:00
if (!g_strncasecmp(codeset, "ISO-8859-", 9) ||
!g_strcasecmp(codeset, "BALTIC"))
2001-04-19 14:21:46 +02:00
use_fontset = FALSE;
2001-10-26 07:41:51 +02:00
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;
2001-04-19 14:21:46 +02:00
}
} else
use_fontset = FALSE;
if (textview->text_is_mb && !use_fontset) {
GtkWidget *parent;
parent = textview->scrolledwin_mb->parent;
2001-12-11 10:18:29 +01:00
gtkut_container_remove(GTK_CONTAINER(parent),
textview->scrolledwin_mb);
2001-04-19 14:21:46 +02:00
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;
2001-12-11 10:18:29 +01:00
gtkut_container_remove(GTK_CONTAINER(parent),
textview->scrolledwin_sb);
2001-04-19 14:21:46 +02:00
gtk_container_add(GTK_CONTAINER(parent),
textview->scrolledwin_mb);
textview->text = textview->text_mb;
textview->text_is_mb = TRUE;
}
if (prefs_common.textfont) {
2002-02-25 10:25:17 +01:00
GdkFont *font;
if (use_fontset) {
if (text_mb_font) {
text_mb_font->ascent = text_mb_font_orig_ascent;
text_mb_font->descent = text_mb_font_orig_descent;
}
font = gdk_fontset_load(prefs_common.textfont);
if (font && text_mb_font != font) {
if (text_mb_font)
gdk_font_unref(text_mb_font);
text_mb_font = font;
text_mb_font_orig_ascent = font->ascent;
text_mb_font_orig_descent = font->descent;
}
} else {
if (text_sb_font) {
text_sb_font->ascent = text_sb_font_orig_ascent;
text_sb_font->descent = text_sb_font_orig_descent;
}
2001-04-19 14:21:46 +02:00
if (MB_CUR_MAX > 1) {
2002-02-25 10:25:17 +01:00
FONT_LOAD(font, "-*-courier-medium-r-normal--14-*-*-*-*-*-iso8859-1");
2001-04-19 14:21:46 +02:00
} else {
2002-02-25 10:25:17 +01:00
FONT_LOAD(font, prefs_common.textfont);
}
if (font && text_sb_font != font) {
if (text_sb_font)
gdk_font_unref(text_sb_font);
text_sb_font = font;
text_sb_font_orig_ascent = font->ascent;
text_sb_font_orig_descent = font->descent;
2001-04-19 14:21:46 +02:00
}
}
2002-02-25 10:25:17 +01:00
if (font) {
2001-04-19 14:21:46 +02:00
gint ascent, descent;
descent = prefs_common.line_space / 2;
ascent = prefs_common.line_space - descent;
2002-02-25 10:25:17 +01:00
font->ascent += ascent;
font->descent += descent;
if (textview->msgfont)
gdk_font_unref(textview->msgfont);
textview->msgfont = font;
gdk_font_ref(font);
2001-04-19 14:21:46 +02:00
}
}
2002-02-25 10:25:17 +01:00
if (!textview->boldfont && prefs_common.boldfont)
FONT_LOAD(textview->boldfont, prefs_common.boldfont);
2001-04-19 14:21:46 +02:00
if (!spacingfont)
spacingfont = gdk_font_load("-*-*-medium-r-normal--6-*");
}
enum
{
H_DATE = 0,
H_FROM = 1,
H_TO = 2,
H_NEWSGROUPS = 3,
H_SUBJECT = 4,
H_CC = 5,
H_REPLY_TO = 6,
H_FOLLOWUP_TO = 7,
H_X_MAILER = 8,
H_X_NEWSREADER = 9,
H_USER_AGENT = 10,
H_ORGANIZATION = 11,
};
2001-09-30 13:07:34 +02:00
void textview_set_position(TextView *textview, gint pos)
{
if (pos < 0) {
textview->cur_pos =
2002-01-06 13:21:36 +01:00
gtk_stext_get_length(GTK_STEXT(textview->text));
2001-09-30 13:07:34 +02:00
} else {
textview->cur_pos = pos;
}
}
2001-04-19 14:21:46 +02:00
static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
{
2001-05-06 22:06:56 +02:00
gchar buf[BUFFSIZE];
GPtrArray *headers, *sorted_headers;
GSList *disphdr_list;
Header *header;
2001-07-15 15:54:20 +02:00
gint i;
2001-04-19 14:21:46 +02:00
g_return_val_if_fail(fp != NULL, NULL);
2002-02-04 10:56:44 +01:00
if (textview->show_all_headers)
return procheader_get_header_array_asis(fp);
2001-04-19 14:21:46 +02:00
if (!prefs_common.display_header) {
while (fgets(buf, sizeof(buf), fp) != NULL)
if (buf[0] == '\r' || buf[0] == '\n') break;
return NULL;
}
2001-05-06 22:06:56 +02:00
headers = procheader_get_header_array_asis(fp);
2001-04-19 14:21:46 +02:00
2001-05-06 22:06:56 +02:00
sorted_headers = g_ptr_array_new();
2001-04-19 14:21:46 +02:00
2001-05-06 22:06:56 +02:00
for (disphdr_list = prefs_common.disphdr_list; disphdr_list != NULL;
disphdr_list = disphdr_list->next) {
DisplayHeaderProp *dp =
(DisplayHeaderProp *)disphdr_list->data;
2001-04-19 14:21:46 +02:00
2001-05-06 22:06:56 +02:00
for (i = 0; i < headers->len; i++) {
header = g_ptr_array_index(headers, i);
2001-04-19 14:21:46 +02:00
if (procheader_headername_equal(header->name,
dp->name)) {
2001-05-06 22:06:56 +02:00
if (dp->hidden)
2001-05-06 06:27:28 +02:00
procheader_header_free(header);
2001-05-06 22:06:56 +02:00
else
g_ptr_array_add(sorted_headers, header);
g_ptr_array_remove_index(headers, i);
i--;
}
}
}
2001-05-06 22:06:56 +02:00
if (prefs_common.show_other_header) {
for (i = 0; i < headers->len; i++) {
header = g_ptr_array_index(headers, i);
g_ptr_array_add(sorted_headers, header);
}
2002-08-05 10:01:46 +02:00
g_ptr_array_free(headers, TRUE);
} else
procheader_header_array_destroy(headers);
2001-05-23 05:04:46 +02:00
return sorted_headers;
2001-04-19 14:21:46 +02:00
}
static void textview_show_header(TextView *textview, GPtrArray *headers)
{
2002-01-06 13:21:36 +01:00
GtkSText *text = GTK_STEXT(textview->text);
2001-04-19 14:21:46 +02:00
Header *header;
2001-07-15 15:54:20 +02:00
gint i;
2001-04-19 14:21:46 +02:00
g_return_if_fail(headers != NULL);
2002-01-06 13:21:36 +01:00
gtk_stext_freeze(text);
2001-04-19 14:21:46 +02:00
for (i = 0; i < headers->len; i++) {
header = g_ptr_array_index(headers, i);
g_return_if_fail(header->name != NULL);
2002-01-06 13:21:36 +01:00
gtk_stext_insert(text, textview->boldfont, NULL, NULL,
2001-04-19 14:21:46 +02:00
header->name, -1);
2001-05-28 00:40:29 +02:00
if (header->name[strlen(header->name) - 1] != ' ')
2002-01-06 13:21:36 +01:00
gtk_stext_insert(text, textview->boldfont,
2001-05-28 00:40:29 +02:00
NULL, NULL, " ", 1);
2001-05-06 22:06:56 +02:00
if (procheader_headername_equal(header->name, "Subject") ||
procheader_headername_equal(header->name, "From") ||
procheader_headername_equal(header->name, "To") ||
procheader_headername_equal(header->name, "Cc"))
2001-05-06 22:06:56 +02:00
unfold_line(header->body);
2001-04-19 14:21:46 +02:00
if (prefs_common.enable_color &&
(procheader_headername_equal(header->name, "X-Mailer") ||
procheader_headername_equal(header->name,
"X-Newsreader")) &&
2001-04-19 14:21:46 +02:00
strstr(header->body, "Sylpheed") != NULL)
2002-01-06 13:21:36 +01:00
gtk_stext_insert(text, NULL, &emphasis_color, NULL,
2001-04-19 14:21:46 +02:00
header->body, -1);
2001-05-06 22:06:56 +02:00
else if (prefs_common.enable_color) {
textview_make_clickable_parts(textview,
NULL, NULL, &uri_color,
header->body);
} else {
2001-07-15 15:54:20 +02:00
textview_make_clickable_parts(textview,
NULL, NULL, NULL,
header->body);
2001-05-06 22:06:56 +02:00
}
2002-01-06 13:21:36 +01:00
gtk_stext_insert(text, textview->msgfont, NULL, NULL, "\n", 1);
2001-04-19 14:21:46 +02:00
}
2002-01-06 13:21:36 +01:00
gtk_stext_thaw(text);
2001-09-24 10:15:52 +02:00
}
gboolean textview_search_string(TextView *textview, const gchar *str,
gboolean case_sens)
{
2002-01-06 13:21:36 +01:00
GtkSText *text = GTK_STEXT(textview->text);
2001-09-24 10:15:52 +02:00
gint pos;
wchar_t *wcs;
gint len;
gint text_len;
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);
2001-09-30 13:07:34 +02:00
pos = textview->cur_pos;
2001-09-24 10:15:52 +02:00
if (pos < textview->body_pos)
pos = textview->body_pos;
2002-01-06 13:21:36 +01:00
text_len = gtk_stext_get_length(text);
2001-09-30 13:07:34 +02:00
if (text_len - pos < len) {
g_free(wcs);
return FALSE;
}
2001-09-24 10:15:52 +02:00
for (; pos < text_len; pos++) {
if (text_len - pos < len) break;
2002-01-06 13:21:36 +01:00
if (gtkut_stext_match_string(text, pos, wcs, len, case_sens)
2001-09-24 10:15:52 +02:00
== TRUE) {
gtk_widget_hide(GTK_WIDGET(textview->scrolledwin));
2001-09-30 13:07:34 +02:00
gtk_editable_set_position(GTK_EDITABLE(text),
pos + len);
2001-09-24 10:15:52 +02:00
gtk_editable_select_region(GTK_EDITABLE(text),
pos, pos + len);
gtk_widget_show(GTK_WIDGET(textview->scrolledwin));
2001-09-30 13:07:34 +02:00
textview_set_position(textview, pos + len);
2001-09-24 10:15:52 +02:00
found = TRUE;
break;
}
2001-09-30 13:07:34 +02:00
if (text_len - pos == len) break;
}
g_free(wcs);
return found;
}
gboolean textview_search_string_backward(TextView *textview, const gchar *str,
gboolean case_sens)
{
2002-01-06 13:21:36 +01:00
GtkSText *text = GTK_STEXT(textview->text);
2001-09-30 13:07:34 +02:00
gint pos;
wchar_t *wcs;
gint len;
gint text_len;
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;
2002-01-06 13:21:36 +01:00
text_len = gtk_stext_get_length(text);
2001-09-30 13:07:34 +02:00
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--) {
2002-01-06 13:21:36 +01:00
if (gtkut_stext_match_string(text, pos, wcs, len, case_sens)
2001-09-30 13:07:34 +02:00
== 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;
}
if (pos == textview->body_pos) break;
2001-09-24 10:15:52 +02:00
}
g_free(wcs);
return found;
2001-04-19 14:21:46 +02:00
}
void textview_scroll_one_line(TextView *textview, gboolean up)
{
2002-01-06 13:21:36 +01:00
GtkSText *text = GTK_STEXT(textview->text);
2001-04-19 14:21:46 +02:00
gfloat upper;
if (prefs_common.enable_smooth_scroll) {
textview_smooth_scroll_one_line(textview, up);
return;
}
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");
}
} 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");
}
}
}
gboolean textview_scroll_page(TextView *textview, gboolean up)
{
2002-01-06 13:21:36 +01:00
GtkSText *text = GTK_STEXT(textview->text);
2001-04-19 14:21:46 +02:00
gfloat upper;
gfloat page_incr;
if (prefs_common.enable_smooth_scroll)
return textview_smooth_scroll_page(textview, up);
if (prefs_common.scroll_halfpage)
page_incr = text->vadj->page_increment / 2;
else
page_incr = text->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");
} 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");
} else
return FALSE;
}
return TRUE;
}
static void textview_smooth_scroll_do(TextView *textview,
gfloat old_value, gfloat last_value,
gint step)
{
2002-01-06 13:21:36 +01:00
GtkSText *text = GTK_STEXT(textview->text);
2001-04-19 14:21:46 +02:00
gint change_value;
gboolean up;
gint i;
if (old_value < last_value) {
change_value = last_value - old_value;
up = FALSE;
} else {
change_value = old_value - last_value;
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");
}
text->vadj->value = last_value;
gtk_signal_emit_by_name(GTK_OBJECT(text->vadj), "value_changed");
gdk_key_repeat_restore();
}
static void textview_smooth_scroll_one_line(TextView *textview, gboolean up)
{
2002-01-06 13:21:36 +01:00
GtkSText *text = GTK_STEXT(textview->text);
2001-04-19 14:21:46 +02:00
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;
last_value = MIN(last_value, upper);
textview_smooth_scroll_do(textview, old_value,
last_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;
last_value = MAX(last_value, 0.0);
textview_smooth_scroll_do(textview, old_value,
last_value,
prefs_common.scroll_step);
}
}
}
static gboolean textview_smooth_scroll_page(TextView *textview, gboolean up)
{
2002-01-06 13:21:36 +01:00
GtkSText *text = GTK_STEXT(textview->text);
2001-04-19 14:21:46 +02:00
gfloat upper;
gfloat page_incr;
gfloat old_value;
gfloat last_value;
if (prefs_common.scroll_halfpage)
page_incr = text->vadj->page_increment / 2;
else
page_incr = text->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;
last_value = MIN(last_value, upper);
textview_smooth_scroll_do(textview, old_value,
last_value,
prefs_common.scroll_step);
} else
return FALSE;
} else {
if (text->vadj->value > 0.0) {
old_value = text->vadj->value;
last_value = text->vadj->value - page_incr;
last_value = MAX(last_value, 0.0);
textview_smooth_scroll_do(textview, old_value,
last_value,
prefs_common.scroll_step);
} else
return FALSE;
}
return TRUE;
}
2002-01-29 11:03:33 +01:00
#define KEY_PRESS_EVENT_STOP() \
if (gtk_signal_n_emissions_by_name \
(GTK_OBJECT(widget), "key_press_event") > 0) { \
gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), \
"key_press_event"); \
}
static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
2001-04-19 14:21:46 +02:00
TextView *textview)
{
SummaryView *summaryview = NULL;
2002-01-29 11:03:33 +01:00
MessageView *messageview = textview->messageview;
2001-04-19 14:21:46 +02:00
2002-01-29 11:03:33 +01:00
if (!event) return FALSE;
if (messageview->mainwin)
summaryview = messageview->mainwin->summaryview;
2001-04-19 14:21:46 +02:00
switch (event->keyval) {
case GDK_Tab:
case GDK_Home:
case GDK_Left:
case GDK_Up:
case GDK_Right:
case GDK_Down:
case GDK_Page_Up:
case GDK_Page_Down:
case GDK_End:
case GDK_Control_L:
case GDK_Control_R:
break;
case GDK_space:
if (summaryview)
summary_pass_key_press_event(summaryview, event);
else
textview_scroll_page(textview, FALSE);
break;
case GDK_BackSpace:
textview_scroll_page(textview, TRUE);
break;
case GDK_Return:
textview_scroll_one_line(textview,
(event->state & GDK_MOD1_MASK) != 0);
break;
2002-09-03 09:47:27 +02:00
case GDK_Delete:
if (summaryview)
summary_pass_key_press_event(summaryview, event);
break;
2002-01-29 11:03:33 +01:00
case GDK_n:
case GDK_N:
case GDK_p:
case GDK_P:
case GDK_y:
case GDK_t:
case GDK_l:
2002-06-19 10:04:32 +02:00
if (messageview->type == MVIEW_MIME &&
textview == messageview->mimeview->textview) {
2002-01-29 11:03:33 +01:00
KEY_PRESS_EVENT_STOP();
mimeview_pass_key_press_event(messageview->mimeview,
event);
break;
}
/* fall through */
2001-04-19 14:21:46 +02:00
default:
2002-06-19 10:04:32 +02:00
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);
}
2001-04-19 14:21:46 +02:00
break;
}
2002-01-29 11:03:33 +01:00
return TRUE;
2001-04-19 14:21:46 +02:00
}
static gint show_url_timeout_cb(gpointer data)
{
TextView *textview = (TextView *)data;
2002-05-08 08:27:40 +02:00
if (textview->messageview->mainwin)
if (textview->show_url_msgid)
gtk_statusbar_remove(GTK_STATUSBAR(
textview->messageview->mainwin->statusbar),
textview->messageview->mainwin->folderview_cid,
textview->show_url_msgid);
return FALSE;
}
static gint textview_button_pressed(GtkWidget *widget, GdkEventButton *event,
TextView *textview)
{
if (event)
textview->last_buttonpress = event->type;
return FALSE;
}
static gint textview_button_released(GtkWidget *widget, GdkEventButton *event,
2001-04-19 14:21:46 +02:00
TextView *textview)
{
2001-09-30 13:07:34 +02:00
textview->cur_pos =
gtk_editable_get_position(GTK_EDITABLE(textview->text));
if (event &&
2002-05-06 12:20:19 +02:00
((event->button == 1)
2001-05-23 05:04:46 +02:00
|| event->button == 2 || event->button == 3)) {
2001-04-19 14:21:46 +02:00
GSList *cur;
2001-10-29 16:42:21 +01:00
/* 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 && textview->last_buttonpress == GDK_2BUTTON_PRESS) {
2001-10-29 16:42:21 +01:00
textview->cur_pos--;
}
2001-04-19 14:21:46 +02:00
for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
RemoteURI *uri = (RemoteURI *)cur->data;
2001-09-30 13:07:34 +02:00
if (textview->cur_pos >= uri->start &&
textview->cur_pos < uri->end) {
2002-05-06 12:20:19 +02:00
/* single click: display url in statusbar */
if (event->button == 1 && textview->last_buttonpress != GDK_2BUTTON_PRESS) {
if (textview->messageview->mainwin) {
2002-05-08 08:27:40 +02:00
if (textview->show_url_msgid) {
gtk_timeout_remove(textview->show_url_timeout_tag);
gtk_statusbar_remove(GTK_STATUSBAR(
textview->messageview->mainwin->statusbar),
textview->messageview->mainwin->folderview_cid,
textview->show_url_msgid);
textview->show_url_msgid = 0;
}
textview->show_url_msgid = gtk_statusbar_push(
GTK_STATUSBAR(textview->messageview->mainwin->statusbar),
textview->messageview->mainwin->folderview_cid,
uri->uri);
2002-07-13 08:49:35 +02:00
textview->show_url_timeout_tag = gtk_timeout_add( 4000, show_url_timeout_cb, textview );
2002-05-08 08:27:40 +02:00
gtkut_widget_wait_for_draw(textview->messageview->mainwin->hbox_stat);
}
2002-05-06 12:20:19 +02:00
} else
2001-05-08 15:15:53 +02:00
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 */
2001-09-02 01:48:06 +02:00
addressbook_add_contact( fromname, fromaddress, NULL );
g_free(fromaddress);
g_free(fromname);
2001-05-23 05:04:46 +02:00
} else {
PrefsAccount *account = NULL;
FolderItem *folder_item;
if (textview->messageview && textview->messageview->mainwin
&& textview->messageview->mainwin->summaryview
&& textview->messageview->mainwin->summaryview->folder_item) {
folder_item = textview->messageview->mainwin->summaryview->folder_item;
if (folder_item->prefs && folder_item->prefs->enable_default_account)
account = account_find_from_id(folder_item->prefs->default_account);
}
2002-07-30 11:49:51 +02:00
compose_new(account, uri->uri + 7, NULL);
2001-05-23 05:04:46 +02:00
}
} else {
2001-04-19 14:21:46 +02:00
open_uri(uri->uri,
prefs_common.uri_cmd);
2001-05-23 05:04:46 +02:00
}
}
2001-04-19 14:21:46 +02:00
}
}
if (event)
textview->last_buttonpress = event->type;
return FALSE;
2001-04-19 14:21:46 +02:00
}
static void textview_uri_list_remove_all(GSList *uri_list)
{
GSList *cur;
for (cur = uri_list; cur != NULL; cur = cur->next) {
if (cur->data) {
g_free(((RemoteURI *)cur->data)->uri);
g_free(cur->data);
}
}
g_slist_free(uri_list);
}