In bodystruct support, if there are embedded internal images and they
[modest] / src / widgets / modest-gtkhtml-msg-view.c
index e41eb30..d7f5238 100644 (file)
@@ -54,6 +54,7 @@
 #include <widgets/modest-isearch-view.h>
 #include <widgets/modest-ui-constants.h>
 #include <modest-icon-names.h>
+#include <tny-camel-bs-mime-part.h>
 
 /* FIXNE: we should have no maemo-deps in widgets/ */
 #ifndef MODEST_TOOLKIT_GTK
@@ -270,11 +271,18 @@ struct _ModestGtkhtmlMsgViewPrivate {
        /* id handler for dragged scroll */
        guint idle_motion_id;
 
+       /* idle changes count */
+       gint idle_changes_count;
+       guint idle_readjust_scroll_id;
+       guint idle_resize_children_id;
+
        /* zoom */
        gdouble current_zoom;
 
        /* link click management */
        gchar *last_url;
+
+       gboolean has_blocked_bs_images;
 };
 
 #define MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
@@ -908,9 +916,10 @@ size_allocate (GtkWidget *widget,
             allocation->height != widget->allocation.height))
                gdk_window_invalidate_rect (widget->window, NULL, FALSE);
 
-       if (widget->allocation.width != allocation->width) {
+       if (priv->idle_readjust_scroll_id == 0 && priv->idle_changes_count < 5 && widget->allocation.width != allocation->width) {
                g_object_ref (self);
-               g_idle_add ((GSourceFunc) idle_readjust_scroll, self);
+               priv->idle_readjust_scroll_id = g_idle_add ((GSourceFunc) idle_readjust_scroll, self);
+               priv->idle_changes_count ++;
        }
 
        widget->allocation = *allocation;
@@ -930,9 +939,14 @@ size_allocate (GtkWidget *widget,
        html_vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->html_scroll));
 
        html_allocation.x = 0;
-       html_allocation.y = headers_allocation.height;
+       html_allocation.y = MAX (0, headers_allocation.height - priv->vadj->value);
        html_allocation.width = view_allocation.width;
-       html_allocation.height = MAX ((gint) html_vadj->upper, (gint)(priv->vadj->upper - headers_allocation.height));
+       if (html_vadj->upper < allocation->height) {
+               html_allocation.height = MAX (html_vadj->upper, allocation->height - headers_allocation.height);
+       } else {
+               html_allocation.height = allocation->height;
+       }
+       /* html_allocation.height = MAX ((gint) html_vadj->upper, (gint)(priv->vadj->upper - headers_allocation.height)); */
 
        if (GTK_WIDGET_REALIZED (widget)) {
                gdk_window_move_resize (widget->window,
@@ -953,7 +967,7 @@ size_allocate (GtkWidget *widget,
                                        headers_allocation.height);
                gdk_window_move_resize (priv->html_window,
                                        (gint) (- priv->hadj->value),
-                                       (gint) (html_allocation.y - priv->vadj->value),
+                                       (gint) html_allocation.y,
                                        (gint) priv->hadj->upper,
                                        html_allocation.height);
        }
@@ -965,8 +979,11 @@ size_allocate (GtkWidget *widget,
                html_allocation.x = 0;
                html_allocation.y = 0;
                html_allocation.width = (gint) priv->hadj->upper;
-               html_allocation.height = (gint) priv->vadj->upper - headers_allocation.height;
                gtk_widget_size_allocate (priv->html_scroll, &html_allocation);
+               if (html_vadj->page_size != html_allocation.height) {
+                       html_vadj->page_size = html_allocation.height;
+                       gtk_adjustment_changed (html_vadj);
+               }
        }
        gtk_adjustment_changed (priv->hadj);
        gtk_adjustment_changed (priv->vadj);
@@ -1015,9 +1032,15 @@ adjustment_value_changed (GtkAdjustment *adj, gpointer data)
                if (priv->html_scroll && GTK_WIDGET_VISIBLE (priv->html_scroll)) {
                        gint old_x, old_y;
                        gint new_x, new_y;
+                       gint new_internal_vvalue;
                        gdk_window_get_position (priv->html_window, &old_x, &old_y);
                        new_x = -hadj->value;
-                       new_y = headers_offset - vadj->value;
+
+                       new_internal_vvalue = MAX (0, vadj->value - headers_offset);
+                       gtk_adjustment_set_value (gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->html_scroll)),
+                                                 new_internal_vvalue);
+
+                       new_y = MAX (0, headers_offset - vadj->value);
 
                        if (new_x != old_x || new_y != old_y) {
                                gdk_window_move (priv->html_window, new_x, new_y);
@@ -1028,6 +1051,18 @@ adjustment_value_changed (GtkAdjustment *adj, gpointer data)
        }
 }
 
+static gboolean
+resize_children_idle (GtkContainer *cont)
+{
+       ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (cont);
+       if (GTK_WIDGET_DRAWABLE (cont)) {
+               gtk_container_resize_children (cont);
+       }
+       priv->idle_resize_children_id = 0;
+
+       return FALSE;
+}
+
 static void
 html_adjustment_changed (GtkAdjustment *adj,
                         gpointer userdata)
@@ -1041,19 +1076,25 @@ html_adjustment_changed (GtkAdjustment *adj,
        g_signal_stop_emission_by_name (G_OBJECT (adj), "changed");
 
        priv->html_scroll->requisition.height = html_vadj->upper;
-       if (html_vadj->upper == priv->html_scroll->allocation.height)
+       if ((priv->vadj != NULL && (html_vadj->upper == (priv->vadj->upper - GTK_WIDGET (priv->headers_box)->allocation.height)))
+           && html_vadj->page_size == priv->html_scroll->allocation.height)
                return;
-       priv->html_scroll->allocation.height = html_vadj->upper;
 
        set_vadjustment_values (self, &vadj_changed);
 
-       new_height = MAX ((gint) html_vadj->upper, (gint) (priv->vadj->upper - priv->headers_box->allocation.height));
+       if (html_vadj->upper < GTK_WIDGET (self)->allocation.height) {
+               new_height = MAX (html_vadj->upper, 
+                                       GTK_WIDGET (self)->allocation.height - GTK_WIDGET(priv->headers_box)->allocation.height);
+       } else {
+               new_height = GTK_WIDGET (self)->allocation.height;
+       }
        
        gtk_adjustment_changed (priv->vadj);
        if (GTK_WIDGET_DRAWABLE (priv->html_scroll)) {
                gdk_window_resize (priv->html_window, (gint) priv->hadj->upper, (gint) new_height);
                gdk_window_process_updates (priv->view_window, TRUE);
-               gtk_container_resize_children (GTK_CONTAINER (self));
+               if (priv->idle_resize_children_id == 0)
+                       priv->idle_resize_children_id = gdk_threads_add_idle ((GSourceFunc) resize_children_idle, self);
        }
        
 }
@@ -1061,12 +1102,12 @@ html_adjustment_changed (GtkAdjustment *adj,
 gboolean
 idle_readjust_scroll (ModestGtkhtmlMsgView *self)
 {
+       ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
 
        /* We're out the main lock */
        gdk_threads_enter ();
 
        if (GTK_WIDGET_DRAWABLE (self)) {
-               ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
                GtkAdjustment *html_vadj;
                GtkAdjustment *html_hadj;
 
@@ -1093,10 +1134,10 @@ idle_readjust_scroll (ModestGtkhtmlMsgView *self)
                gtk_adjustment_set_value (priv->vadj, 0.0);
 
        }
+       priv->idle_readjust_scroll_id = 0;
        g_object_unref (G_OBJECT (self));
 
 
-
        gdk_threads_leave ();
 
        return FALSE;
@@ -1115,6 +1156,11 @@ modest_gtkhtml_msg_view_init (ModestGtkhtmlMsgView *obj)
        
        priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE(obj);
 
+       priv->has_blocked_bs_images = FALSE;
+
+       priv->idle_changes_count = 0;
+       priv->idle_readjust_scroll_id = 0;
+       priv->idle_resize_children_id = 0;
        priv->current_zoom = 1.0;
 
        priv->hadj = NULL;
@@ -1248,6 +1294,16 @@ modest_gtkhtml_msg_view_finalize (GObject *obj)
                priv->msg = NULL;
        }
 
+       if (priv->idle_resize_children_id > 0) {
+               g_source_remove (priv->idle_resize_children_id);
+               priv->idle_resize_children_id = 0;
+       }
+
+       if (priv->idle_readjust_scroll_id > 0) {
+               g_source_remove (priv->idle_readjust_scroll_id);
+               priv->idle_readjust_scroll_id = 0;
+       }
+
        if (priv->idle_motion_id > 0) {
                g_source_remove (priv->idle_motion_id);
                priv->idle_motion_id = 0;
@@ -1559,7 +1615,8 @@ has_blocked_external_images (ModestGtkhtmlMsgView *self)
 {
        ModestGtkhtmlMsgViewPrivate *priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE (self);
 
-       return (modest_mime_part_view_has_external_images (MODEST_MIME_PART_VIEW (priv->body_view)) &&
+       return ((modest_mime_part_view_has_external_images (MODEST_MIME_PART_VIEW (priv->body_view)) ||
+                (priv->has_blocked_bs_images)) &&
                !modest_mime_part_view_get_view_images (MODEST_MIME_PART_VIEW (priv->body_view)));
 }
 
@@ -1707,6 +1764,13 @@ on_fetch_url (GtkWidget *widget, const gchar *uri,
                } else {
                        return FALSE;
                }
+       } else if (TNY_IS_CAMEL_BS_MIME_PART (part) && 
+                  !tny_camel_bs_mime_part_is_fetched (TNY_CAMEL_BS_MIME_PART (part))){
+               if (!modest_mime_part_view_get_view_images (MODEST_MIME_PART_VIEW (priv->body_view))) {
+                       priv->has_blocked_bs_images = TRUE;
+                       tny_stream_close (stream);
+                       return TRUE;
+               }
        }
 
        tny_mime_part_decode_to_stream ((TnyMimePart*)part, stream, NULL);
@@ -1726,6 +1790,7 @@ set_message (ModestGtkhtmlMsgView *self, TnyMsg *msg, TnyMimePart *other_body)
        g_return_if_fail (self);
 
        priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE(self);
+       priv->has_blocked_bs_images = FALSE;
        modest_mail_header_view_set_loading (MODEST_MAIL_HEADER_VIEW (priv->mail_header_view), FALSE);
        gtk_widget_set_no_show_all (priv->mail_header_view, FALSE);
        modest_mime_part_view_set_view_images (MODEST_MIME_PART_VIEW (priv->body_view), FALSE);
@@ -1762,6 +1827,7 @@ set_message (ModestGtkhtmlMsgView *self, TnyMsg *msg, TnyMimePart *other_body)
                gtk_widget_set_size_request (GTK_WIDGET (priv->body_view), 1, 1);
                gtk_widget_set_size_request (GTK_WIDGET (priv->body_view), -1, -1);
 
+               priv->idle_changes_count = 0;
                gtk_widget_queue_resize (GTK_WIDGET (priv->body_view));
 
                gtk_widget_queue_resize (GTK_WIDGET(self));
@@ -1845,6 +1911,7 @@ set_message (ModestGtkhtmlMsgView *self, TnyMsg *msg, TnyMimePart *other_body)
        gtk_widget_set_size_request (GTK_WIDGET (priv->body_view), 1, 1);
        gtk_widget_set_size_request (GTK_WIDGET (priv->body_view), -1, -1);
 
+       priv->idle_changes_count = 0;
        gtk_widget_queue_resize (GTK_WIDGET (priv->body_view));
 
        gtk_widget_queue_resize (GTK_WIDGET(self));
@@ -1860,8 +1927,10 @@ set_message (ModestGtkhtmlMsgView *self, TnyMsg *msg, TnyMimePart *other_body)
        /* This is a hack to force reallocation of scroll after drawing all the stuff. This
         * makes the html view get the proper and expected size and prevent being able to scroll
         * the buffer when it shouldn't be scrollable */
-       g_object_ref (self);
-       g_idle_add ((GSourceFunc) idle_readjust_scroll, self);
+       if (priv->idle_readjust_scroll_id == 0) {
+               g_object_ref (self);
+               priv->idle_readjust_scroll_id = g_idle_add ((GSourceFunc) idle_readjust_scroll, self);
+       }
 }
 
 static void
@@ -1878,6 +1947,7 @@ set_header (ModestGtkhtmlMsgView *self, TnyHeader *header)
        }
        
        priv = MODEST_GTKHTML_MSG_VIEW_GET_PRIVATE(self);
+       priv->has_blocked_bs_images = FALSE;
        modest_mail_header_view_set_loading (MODEST_MAIL_HEADER_VIEW (priv->mail_header_view), TRUE);
        gtk_widget_set_no_show_all (priv->mail_header_view, FALSE);
        modest_mime_part_view_set_view_images (MODEST_MIME_PART_VIEW (priv->body_view), FALSE);
@@ -1892,6 +1962,7 @@ set_header (ModestGtkhtmlMsgView *self, TnyHeader *header)
        html_hadj->page_size = 0;
        g_signal_emit_by_name (G_OBJECT (html_hadj), "changed");
 
+       priv->idle_changes_count = 0;
        gtk_widget_set_size_request (GTK_WIDGET (priv->body_view), 1, 1);
        gtk_widget_set_size_request (GTK_WIDGET (priv->body_view), -1, -1);
        gtk_widget_queue_resize (GTK_WIDGET (priv->body_view));
@@ -1910,6 +1981,7 @@ set_header (ModestGtkhtmlMsgView *self, TnyHeader *header)
 #endif
        gtk_widget_set_no_show_all (priv->mail_header_view, TRUE);
        tny_mime_part_view_clear (TNY_MIME_PART_VIEW (priv->body_view));
+       priv->idle_changes_count = 0;
        gtk_widget_queue_resize (GTK_WIDGET(self));
        gtk_widget_queue_draw (GTK_WIDGET(self));
 }
@@ -1962,6 +2034,7 @@ set_zoom (ModestGtkhtmlMsgView *self, gdouble zoom)
        gtk_widget_set_size_request (GTK_WIDGET (priv->body_view), 1, 1);
        gtk_widget_set_size_request (GTK_WIDGET (priv->body_view), -1, -1);
 
+       priv->idle_changes_count = 0;
        gtk_widget_queue_resize (priv->body_view);
 }