Modified webpage: now tinymail repository is in gitorious.
[modest] / src / widgets / modest-tny-stream-gtkhtml.c
index cdca2d0..3bdd6b3 100644 (file)
@@ -57,6 +57,9 @@ struct _ModestTnyStreamGtkhtmlPrivate {
        GtkHTMLStream *stream;
        GtkHTML *html;
        guint stop_streams_id;
+
+       gssize max_size;
+       gssize current_size;
 };
 #define MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
                                                        MODEST_TYPE_TNY_STREAM_GTKHTML, \
@@ -123,6 +126,9 @@ modest_tny_stream_gtkhtml_init (ModestTnyStreamGtkhtml *obj)
        priv->stream  = NULL;
        priv->html = NULL;
        priv->stop_streams_id = 0;
+
+       priv->max_size = 0;
+       priv->current_size = 0;
 }
 
 static void
@@ -160,6 +166,7 @@ modest_tny_stream_gtkhtml_new (GtkHTMLStream *stream, GtkHTML *html)
 
        priv->stop_streams_id = g_signal_connect (G_OBJECT (html), "stop-streams",
                                                  G_CALLBACK (stop_streams), obj);
+       priv->current_size = 0;
 
        return obj;
 }
@@ -174,34 +181,11 @@ gtkhtml_read (TnyStream *self, char *buffer, size_t n)
        return -1; /* we cannot read */
 }
 
-typedef struct {
-       ModestTnyStreamGtkhtmlPrivate *priv;
-       const char *buffer;
-       size_t n;
-       GMutex *mutex;
-       GCond *cond;
-} WriteInfo;
-
-static gboolean
-write_in_mainloop (gpointer userdata)
-{
-       WriteInfo * info = (WriteInfo *) userdata;
-
-       g_mutex_lock (info->mutex);
-       if (info->priv->html && info->priv->stream)
-               gtk_html_stream_write (info->priv->stream, info->buffer, info->n);
-       g_cond_signal (info->cond);
-       g_mutex_unlock (info->mutex);
-
-       return FALSE;
-}
-
 
 static ssize_t
 gtkhtml_write (TnyStream *self, const char *buffer, size_t n)
 {
        ModestTnyStreamGtkhtmlPrivate *priv;
-       WriteInfo *info;
        
        priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(self);
 
@@ -216,26 +200,25 @@ gtkhtml_write (TnyStream *self, const char *buffer, size_t n)
        if (!priv->html || !GTK_WIDGET_VISIBLE (priv->html))
                return -1;
 
-       info = g_slice_new (WriteInfo);
-       info->mutex = g_mutex_new ();
-       info->priv = priv;
-       info->buffer = buffer;
-       info->n = n;
-       info->cond = g_cond_new ();
-
-       if (g_main_context_acquire (NULL)) {
-               write_in_mainloop (info);
-               g_main_context_release (NULL);
-       } else {
-               g_mutex_lock (info->mutex);
-               g_idle_add (write_in_mainloop, info);
-               g_cond_wait (info->cond, info->mutex);
-               g_mutex_unlock (info->mutex);
+       if (priv->max_size > 0) {
+
+               /* We only use the maximum size for write method, and even we
+                * ignore and fake as we would do a successfull read */
+               if (priv->current_size >= priv->max_size)
+                       return n;
+
+               if (priv->current_size + n > priv->max_size)
+                       n = priv->max_size - priv->current_size;
        }
 
-       g_cond_free (info->cond);
-       g_mutex_free (info->mutex);
-       g_slice_free (WriteInfo, info);
+       if (!g_main_context_is_owner (NULL))
+               gdk_threads_enter ();
+
+       gtk_html_stream_write (priv->stream, buffer, n);
+
+       if (!g_main_context_is_owner (NULL))
+               gdk_threads_leave ();
+       priv->current_size += n;
 
        return n; /* hmmm */
 }
@@ -248,26 +231,6 @@ gtkhtml_flush (TnyStream *self)
 }
        
 
-typedef struct {
-       ModestTnyStreamGtkhtmlPrivate *priv;
-       GMutex *mutex;
-       GCond *cond;
-} CloseInfo;
-
-static gboolean
-close_in_mainloop (gpointer userdata)
-{
-       CloseInfo * info = (CloseInfo *) userdata;
-
-       g_mutex_lock (info->mutex);
-       if (info->priv->html && GTK_WIDGET_VISIBLE (info->priv->html))
-               gtk_html_stream_close (info->priv->stream, GTK_HTML_STREAM_OK);
-       g_cond_signal (info->cond);
-       g_mutex_unlock (info->mutex);
-
-       return FALSE;
-}
-
 static gint
 gtkhtml_close (TnyStream *self)
 {
@@ -276,29 +239,15 @@ gtkhtml_close (TnyStream *self)
        priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(self);
        
        if (priv->html && GTK_WIDGET_VISIBLE (priv->html)) {
-               CloseInfo *info;
-
-               info = g_slice_new (CloseInfo);
-               info->mutex = g_mutex_new ();
-               info->cond = g_cond_new ();
-               info->priv = priv;
-
-               if (g_main_context_acquire (NULL)) {
-                       close_in_mainloop (info);
-                       g_main_context_release (NULL);
-               } else {
-                       g_mutex_lock (info->mutex);
-                       g_idle_add (close_in_mainloop, info);
-                       g_cond_wait (info->cond, info->mutex);
-                       g_mutex_unlock (info->mutex);
-               }
-
-               g_cond_free (info->cond);
-               g_mutex_free (info->mutex);
-               g_slice_free (CloseInfo, info);
+               if (!g_main_context_is_owner (NULL))
+                       gdk_threads_enter ();
 
-       }
+               gtk_html_stream_close   (priv->stream, GTK_HTML_STREAM_OK);
+
+               if (!g_main_context_is_owner (NULL))
+                       gdk_threads_leave ();
 
+       }
        priv->stream = NULL;
        if (priv->html && priv->stop_streams_id > 0) {
                g_signal_handler_disconnect (G_OBJECT (priv->html), priv->stop_streams_id);
@@ -354,6 +303,40 @@ stop_streams (ModestGtkhtmlMimePartView *view, gpointer userdata)
        }
 }
 
+void 
+modest_tny_stream_gtkhtml_set_max_size (ModestTnyStreamGtkhtml *stream, 
+                                       gssize max_size)
+{
+       ModestTnyStreamGtkhtmlPrivate *priv;
+
+       g_return_if_fail (MODEST_IS_TNY_STREAM_GTKHTML (stream));
+       priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE (stream);
+
+       priv->max_size = max_size;
+}
+
+gssize 
+modest_tny_stream_gtkhtml_get_max_size (ModestTnyStreamGtkhtml *stream)
+{
+       ModestTnyStreamGtkhtmlPrivate *priv;
+
+       g_return_val_if_fail (MODEST_IS_TNY_STREAM_GTKHTML (stream), 0);
+       priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE (stream);
+
+       return priv->max_size;
+}
+
+gboolean
+modest_tny_stream_gtkhtml_limit_reached (ModestTnyStreamGtkhtml *self)
+{
+       ModestTnyStreamGtkhtmlPrivate *priv;
+
+       g_return_val_if_fail (MODEST_IS_TNY_STREAM_GTKHTML (self), 0);
+       priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE (self);
+
+       return (priv->max_size > 0) && (priv->current_size >= priv->max_size);
+}
+
 static void
 modest_tny_stream_gtkhml_iface_init (gpointer g_iface, gpointer iface_data)
 {