Changeset 3400

Show
Ignore:
Timestamp:
02/13/08 12:21:24
Author:
agarcia
Message:

2008-02-12 Alberto Garcia Gonzalez <agarcia@igalia.com>

  • libtinymailui-gtk/tny-gtk-text-buffer-stream.c:

Write only full UTF-8 characters into the GtkTextBuffer?. If an
input buffer contains incomplete UTF-8 chars, save those bytes and
write them with the following buffer.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/ChangeLog

    r3399 r3400  
     12008-02-12  Alberto Garcia Gonzalez <agarcia@igalia.com> 
     2 
     3        * libtinymailui-gtk/tny-gtk-text-buffer-stream.c: 
     4        Write only full UTF-8 characters into the GtkTextBuffer. If an 
     5        input buffer contains incomplete UTF-8 chars, save those bytes and 
     6        write them with the following buffer. 
     7 
    182008-02-11  Alberto Garcia Gonzalez <agarcia@igalia.com> 
    29 
  • trunk/libtinymailui-gtk/tny-gtk-text-buffer-stream.c

    r3304 r3400  
    4747        GtkTextBuffer *buffer; 
    4848        GtkTextIter cur; 
    49  
     49        GByteArray *pending_bytes; 
    5050}; 
    5151 
     
    137137        TnyGtkTextBufferStreamPriv *priv = TNY_GTK_TEXT_BUFFER_STREAM_GET_PRIVATE (self); 
    138138        const gchar *end; 
    139  
    140         if (g_utf8_validate (buffer, n, &end)) 
    141                 gtk_text_buffer_insert (priv->buffer, &(priv->cur), buffer, (gint)n); 
    142         else 
    143                 gtk_text_buffer_insert (priv->buffer, &(priv->cur), end, (gint) (end - buffer)); 
     139        gint nb_written; 
     140 
     141        g_byte_array_append (priv->pending_bytes, buffer, n); 
     142 
     143        /* GtkTextBuffer only accepts full UTF-8 chars, but we might 
     144         * receive a single UTF-8 char split into two different 
     145         * buffers -see camel_stream_write_to_stream()- so we write 
     146         * only the part of the buffer that is valid UTF-8 text and 
     147         * leave the rest for later */ 
     148        g_utf8_validate (priv->pending_bytes->data, priv->pending_bytes->len, &end); 
     149        nb_written = (gint) (end - ((char *) priv->pending_bytes->data)); 
     150 
     151        /* However if the rest of the buffer is more than 4 bytes long 
     152         * (4 bytes being the max size of a UTF-8 char) then it's 
     153         * certainly not a UTF-8 char divided in two. In this case it 
     154         * means that the buffer is corrupt. There's not much to do 
     155         * about it but to write it anyway */ 
     156        if (priv->pending_bytes->len - nb_written >= 4) { 
     157                nb_written = priv->pending_bytes->len; 
     158        } 
     159 
     160        gtk_text_buffer_insert (priv->buffer, &(priv->cur), priv->pending_bytes->data, nb_written); 
     161 
     162        /* Leave the unwritten chars in priv->pending_bytes for later */ 
     163        g_byte_array_remove_range (priv->pending_bytes, 0, nb_written); 
    144164 
    145165        return (gssize) n; 
     
    155175tny_gtk_text_buffer_stream_flush_default (TnyStream *self) 
    156176{ 
     177        TnyGtkTextBufferStreamPriv *priv = TNY_GTK_TEXT_BUFFER_STREAM_GET_PRIVATE (self); 
     178        if (priv->pending_bytes->len > 0) { 
     179                gtk_text_buffer_insert (priv->buffer, &(priv->cur), 
     180                                        priv->pending_bytes->data, priv->pending_bytes->len); 
     181                g_byte_array_set_size (priv->pending_bytes, 0); 
     182        } 
    157183        return 0; 
    158184} 
     
    212238        TnyGtkTextBufferStreamPriv *priv = TNY_GTK_TEXT_BUFFER_STREAM_GET_PRIVATE (self); 
    213239 
     240        tny_gtk_text_buffer_stream_flush (self); 
    214241        return tny_gtk_text_buffer_stream_reset_priv (priv); 
    215242} 
     
    236263        g_object_ref (G_OBJECT (buffer)); 
    237264        priv->buffer = buffer; 
     265 
     266        if (!priv->pending_bytes) 
     267                priv->pending_bytes = g_byte_array_new (); 
     268 
     269        g_byte_array_set_size (priv->pending_bytes, 0); 
    238270 
    239271        tny_gtk_text_buffer_stream_reset_priv (priv); 
     
    269301 
    270302        priv->buffer = NULL; 
     303        priv->pending_bytes = NULL; 
    271304 
    272305        return; 
     
    279312        TnyGtkTextBufferStreamPriv *priv = TNY_GTK_TEXT_BUFFER_STREAM_GET_PRIVATE (self); 
    280313 
     314        if (priv->buffer && priv->pending_bytes) 
     315                tny_gtk_text_buffer_stream_flush (self); 
     316 
    281317        if (priv->buffer) 
    282318                g_object_unref (G_OBJECT (priv->buffer)); 
     319        if (priv->pending_bytes) 
     320                g_byte_array_free (priv->pending_bytes, TRUE); 
    283321 
    284322        (*parent_class->finalize) (object);