[evolution-patches] fix for 43069
- From: Larry Ewing <lewing ximian com>
- To: Radek Doulik <rodo ximian com>, patches <evolution-patches ximian com>
- Subject: [evolution-patches] fix for 43069
- Date: 17 May 2003 01:59:06 -0500
We weren't making sure the text was valid utf-8 before inserting it.
This patch reworks the file reading to use g_io_channels and tests for
charset issues. It tries to load as utf-8 if that fails it tries as the
locale charset. The error reporting dialog might break the string
freeze, I can disable that code if it is important.
http://bugzilla.ximian.com/show_bug.cgi?id=43069
--Larry
Index: menubar.c
===================================================================
RCS file: /cvs/gnome/gtkhtml/components/html-editor/menubar.c,v
retrieving revision 1.87
diff -u -p -r1.87 menubar.c
--- menubar.c 12 May 2003 20:46:48 -0000 1.87
+++ menubar.c 17 May 2003 06:54:37 -0000
@@ -176,44 +176,94 @@ file_dialog_destroy (GtkWidget *w, GtkHT
cd->file_dialog = NULL;
}
-#define BUFFER_SIZE 4096
-
static void
file_dialog_ok (GtkWidget *w, GtkHTMLControlData *cd)
{
const gchar *filename;
- gint fd;
-
+ GIOChannel *io = NULL;
+ GError *error = NULL;
+ gchar *data = NULL;
+ gsize len = 0;
+ const char *charset;
+
filename = gtk_file_selection_get_filename (GTK_FILE_SELECTION (cd->file_dialog));
- fd = open (filename, O_RDONLY);
- if (fd != -1) {
+ io = g_io_channel_new_file (filename, "r", &error);
+
+ if (error || !io)
+ goto end;
+
+ /* try reading as utf-8 */
+ g_io_channel_read_to_end (io, &data, &len, &error);
+
+ /* If we get a charset error try reading as the locale charset */
+ if (error && g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
+ && !g_get_charset (&charset)) {
+
+ g_error_free (error);
+ error = NULL;
+
+ /*
+ * reopen the io channel since we can't set the
+ * charset once we've begun reading.
+ */
+ g_io_channel_unref (io);
+ io = g_io_channel_new_file (filename, "r", &error);
+
+ if (error || !io)
+ goto end;
+
+ g_io_channel_set_encoding (io, charset, NULL);
+ g_io_channel_read_to_end (io, &data, &len, &error);
+ }
+
+ if (error)
+ goto end;
+
+ if (cd->file_html) {
GtkHTML *tmp;
GtkHTMLStream *stream;
- gchar buffer [BUFFER_SIZE];
- ssize_t rb;
-
+
tmp = GTK_HTML (gtk_html_new ());
stream = gtk_html_begin_content (tmp, "text/html; charset=utf-8");
- if (!cd->file_html) {
- gtk_html_write (tmp, stream, "<PRE>", 5);
- }
- while ((rb = read (fd, buffer, BUFFER_SIZE - 1)) > 0) {
- buffer [rb] = 0;
-
- if (cd->file_html) {
- gtk_html_write (tmp, stream, buffer, -1);
- } else {
- html_engine_paste_text (cd->html->engine, buffer, g_utf8_strlen (buffer, -1));
- }
- }
- if (!cd->file_html) {
- gtk_html_write (tmp, stream, "</PRE>", 6);
- }
- gtk_html_end (tmp, stream, rb >=0 ? GTK_HTML_STREAM_OK : GTK_HTML_STREAM_ERROR);
+ gtk_html_write (tmp, stream, data, len);
+ gtk_html_end (tmp, stream, error ? GTK_HTML_STREAM_OK : GTK_HTML_STREAM_ERROR);
gtk_html_insert_gtk_html (cd->html, tmp);
+ } else {
+ html_engine_paste_text (cd->html->engine, data, g_utf8_strlen (data, -1));
+ }
+ g_free (data);
+
+ end:
+ if (io)
+ g_io_channel_unref (io);
- close (fd);
+ if (error) {
+ GtkWidget *toplevel;
+
+ toplevel = gtk_widget_get_toplevel (cd->html);
+
+ if (GTK_WIDGET_TOPLEVEL (toplevel)) {
+ GtkDialog *dialog;
+
+ dialog = gtk_message_dialog_new (toplevel,
+ GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_MESSAGE_ERROR,
+ GTK_BUTTONS_CLOSE,
+ _("Error loading file '%s': %s"),
+ filename, error->message);
+
+ /* Destroy the dialog when the user responds to it (e.g. clicks a button) */
+ g_signal_connect_swapped (GTK_OBJECT (dialog), "response",
+ G_CALLBACK (gtk_widget_destroy),
+ GTK_OBJECT (dialog));
+
+ gtk_widget_show (dialog);
+ } else {
+ g_warning ("Error loading file '%s': %s", filename, error->message);
+ }
+ g_error_free (error);
}
+
gtk_widget_destroy (cd->file_dialog);
}
Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/gtkhtml/components/html-editor/ChangeLog,v
retrieving revision 1.401
diff -u -p -r1.401 ChangeLog
--- ChangeLog 12 May 2003 20:46:48 -0000 1.401
+++ ChangeLog 17 May 2003 06:54:38 -0000
@@ -1,3 +1,8 @@
+2003-05-17 Larry Ewing <lewing ximian com>
+
+ * menubar.c (file_dialog_ok): rewrite file insertion to try utf-8
+ then locale charset and improve error reporting. Fixes #43069.
+
2003-05-08 Radek Doulik <rodo ximian com>
* include config.h and gnome-i18n.h everywhere so that we use
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]