Added msg view window reload feature. Now we can force a message reload
[modest] / src / hildon2 / modest-msg-view-window.c
index 14f6212..acf889c 100644 (file)
@@ -85,6 +85,7 @@ struct _ModestMsgViewWindowPrivate {
        GtkWidget   *prev_toolitem;
        GtkWidget   *next_toolitem;
        gboolean    progress_hint;
+       gint        fetching_images;
 
        /* Optimized view enabled */
        gboolean optimized_view;
@@ -226,6 +227,7 @@ static gboolean _modest_msg_view_window_map_event (GtkWidget *widget,
                                                   GdkEvent *event,
                                                   gpointer userdata);
 
+
 /* list my signals */
 enum {
        MSG_CHANGED_SIGNAL,
@@ -440,6 +442,7 @@ modest_msg_view_window_init (ModestMsgViewWindow *obj)
        priv->row_inserted_handler = 0;
        priv->rows_reordered_handler = 0;
        priv->progress_hint = FALSE;
+       priv->fetching_images = 0;
 
        priv->optimized_view  = FALSE;
        priv->purge_timeout = 0;
@@ -478,6 +481,18 @@ set_toolbar_transfer_mode (ModestMsgViewWindow *self)
        return FALSE;
 }
 
+static void
+update_progress_hint (ModestMsgViewWindow *self)
+{
+       ModestMsgViewWindowPrivate *priv;
+       priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE(self);
+
+       if (GTK_WIDGET_VISIBLE (self)) {
+               hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), 
+                                                         (priv->progress_hint || (priv->fetching_images > 0))?1:0);
+       }
+}
+
 static void 
 set_progress_hint (ModestMsgViewWindow *self, 
                   gboolean enabled)
@@ -493,9 +508,7 @@ set_progress_hint (ModestMsgViewWindow *self,
        /* Sets current progress hint */
        priv->progress_hint = enabled;
 
-       if (GTK_WIDGET_VISIBLE (self)) {
-               hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), enabled?1:0);
-       }
+       update_progress_hint (self);
 
 }
 
@@ -511,22 +524,8 @@ init_window (ModestMsgViewWindow *obj)
        priv->msg_view = GTK_WIDGET (tny_platform_factory_new_msg_view (modest_tny_platform_factory_get_instance ()));
        modest_msg_view_set_shadow_type (MODEST_MSG_VIEW (priv->msg_view), GTK_SHADOW_NONE);
        main_vbox = gtk_vbox_new  (FALSE, 6);
-#ifdef MODEST_TOOLKIT_HILDON2
        priv->main_scroll = hildon_pannable_area_new ();
        gtk_container_add (GTK_CONTAINER (priv->main_scroll), priv->msg_view);
-#else
-#ifdef MODEST_USE_MOZEMBED
-       priv->main_scroll = priv->msg_view;
-       gtk_widget_set_size_request (priv->msg_view, -1, 1600);
-#else
-       priv->main_scroll = gtk_scrolled_window_new (NULL, NULL);
-       gtk_container_add (GTK_CONTAINER (priv->main_scroll), priv->msg_view);
-#endif
-       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->main_scroll), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
-       gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->main_scroll), GTK_SHADOW_NONE);
-       modest_maemo_set_thumbable_scrollbar (GTK_SCROLLED_WINDOW(priv->main_scroll), TRUE);
-
-#endif
        gtk_box_pack_start (GTK_BOX(main_vbox), priv->main_scroll, TRUE, TRUE, 0);
        gtk_container_add   (GTK_CONTAINER(obj), main_vbox);
 
@@ -2422,6 +2421,11 @@ modest_msg_view_window_get_attachments (ModestMsgViewWindow *win)
        return selected_attachments;
 }
 
+typedef struct {
+       ModestMsgViewWindow *self;
+       gchar *file_path;
+} DecodeAsyncHelper;
+
 static void
 on_decode_to_stream_async_handler (TnyMimePart *mime_part, 
                                   gboolean cancelled, 
@@ -2429,25 +2433,32 @@ on_decode_to_stream_async_handler (TnyMimePart *mime_part,
                                   GError *err, 
                                   gpointer user_data)
 {
-       gchar *filepath = (gchar *) user_data;
+       DecodeAsyncHelper *helper = (DecodeAsyncHelper *) user_data;
+
+       /* It could happen that the window was closed */
+       if (GTK_WIDGET_VISIBLE (helper->self))
+               set_progress_hint (helper->self, FALSE);
 
        if (cancelled || err) {
                if (err) {
-                       modest_platform_information_banner (NULL, NULL,
-                                                           _KR("cerm_device_memory_full"));
+                       gchar *msg = g_strdup_printf (_KR("cerm_device_memory_full"), "");
+                       modest_platform_information_banner (NULL, NULL, msg);
+                       g_free (msg);
                }
                goto free;
        }
 
        /* make the file read-only */
-       g_chmod(filepath, 0444);
+       g_chmod(helper->file_path, 0444);
 
        /* Activate the file */
-       modest_platform_activate_file (filepath, tny_mime_part_get_content_type (mime_part));
+       modest_platform_activate_file (helper->file_path, tny_mime_part_get_content_type (mime_part));
 
  free:
        /* Frees */
-       g_free (filepath);
+       g_object_unref (helper->self);
+       g_free (helper->file_path);
+       g_slice_free (DecodeAsyncHelper, helper);
 }
 
 void
@@ -2468,7 +2479,7 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window,
        attachments = modest_msg_view_get_attachments (MODEST_MSG_VIEW (priv->msg_view));
        attachment_index = modest_list_index (attachments, (GObject *) mime_part);
        g_object_unref (attachments);
-       
+
        if (msg_uid && attachment_index >= 0) {
                attachment_uid = g_strdup_printf ("%s/%d", msg_uid, attachment_index);
        }
@@ -2508,10 +2519,19 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window,
                                                               &filepath);
 
                if (temp_stream != NULL) {
-                       tny_mime_part_decode_to_stream_async (mime_part, TNY_STREAM (temp_stream), 
-                                                             on_decode_to_stream_async_handler, 
+                       DecodeAsyncHelper *helper;
+
+                       /* Activate progress hint */
+                       set_progress_hint (window, TRUE);
+
+                       helper = g_slice_new0 (DecodeAsyncHelper);
+                       helper->self = g_object_ref (window);
+                       helper->file_path = g_strdup (filepath);
+
+                       tny_mime_part_decode_to_stream_async (mime_part, TNY_STREAM (temp_stream),
+                                                             on_decode_to_stream_async_handler,
                                                              NULL,
-                                                             g_strdup (filepath));
+                                                             helper);
                        g_object_unref (temp_stream);
                        /* NOTE: files in the temporary area will be automatically
                         * cleaned after some time if they are no longer in use */
@@ -2557,8 +2577,10 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window,
                                                                             mailbox, attachment_uid);
                        modest_window_set_zoom (MODEST_WINDOW (msg_win),
                                                modest_window_get_zoom (MODEST_WINDOW (window)));
-                       modest_window_mgr_register_window (mgr, msg_win, MODEST_WINDOW (window));
-                       gtk_widget_show_all (GTK_WIDGET (msg_win));
+                       if (modest_window_mgr_register_window (mgr, msg_win, MODEST_WINDOW (window)))
+                               gtk_widget_show_all (GTK_WIDGET (msg_win));
+                       else
+                               gtk_widget_destroy (GTK_WIDGET (msg_win));
                }
        }
 
@@ -2586,11 +2608,11 @@ static gboolean idle_save_mime_part_show_result (SaveMimePartInfo *info);
 static gpointer save_mime_part_to_file (SaveMimePartInfo *info);
 static void save_mime_parts_to_file_with_checks (SaveMimePartInfo *info);
 
-static void 
+static void
 save_mime_part_info_free (SaveMimePartInfo *info, gboolean with_struct)
 {
-       
        GList *node;
+
        for (node = info->pairs; node != NULL; node = g_list_next (node)) {
                SaveMimePartPair *pair = (SaveMimePartPair *) node->data;
                g_free (pair->filename);
@@ -2618,8 +2640,9 @@ idle_save_mime_part_show_result (SaveMimePartInfo *info)
                if (info->result == GNOME_VFS_OK) {
                        hildon_banner_show_information (NULL, NULL, _CS("sfil_ib_saved"));
                } else if (info->result == GNOME_VFS_ERROR_NO_SPACE) {
-                       hildon_banner_show_information (NULL, NULL, 
-                                                       _KR("cerm_device_memory_full"));
+                       gchar *msg = g_strdup_printf (_KR("cerm_device_memory_full"), "");
+                       modest_platform_information_banner (NULL, NULL, msg);
+                       g_free (msg);
                } else {
                        hildon_banner_show_information (NULL, NULL, _("mail_ib_file_operation_failed"));
                }
@@ -3071,6 +3094,7 @@ typedef struct {
        gchar *cache_id;
        TnyStream *output_stream;
        GtkWidget *msg_view;
+       GtkWidget *window;
 } FetchImageData;
 
 gboolean
@@ -3078,12 +3102,20 @@ on_fetch_image_idle_refresh_view (gpointer userdata)
 {
 
        FetchImageData *fidata = (FetchImageData *) userdata;
-       g_message ("REFRESH VIEW");
+
+       gdk_threads_enter ();
        if (GTK_WIDGET_DRAWABLE (fidata->msg_view)) {
-               g_message ("QUEUING DRAW");
+               ModestMsgViewWindowPrivate *priv;
+
+               priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (fidata->window);
+               priv->fetching_images--;
                gtk_widget_queue_draw (fidata->msg_view);
+               update_progress_hint (MODEST_MSG_VIEW_WINDOW (fidata->window));
        }
+       gdk_threads_leave ();
+
        g_object_unref (fidata->msg_view);
+       g_object_unref (fidata->window);
        g_slice_free (FetchImageData, fidata);
        return FALSE;
 }
@@ -3096,12 +3128,37 @@ on_fetch_image_thread (gpointer userdata)
        TnyStream *cache_stream;
 
        cache = modest_runtime_get_images_cache ();
-       cache_stream = tny_stream_cache_get_stream (cache, fidata->cache_id, (TnyStreamCacheOpenStreamFetcher) fetch_image_open_stream, (gpointer) fidata->uri);
+       cache_stream = 
+               tny_stream_cache_get_stream (cache, 
+                                            fidata->cache_id, 
+                                            (TnyStreamCacheOpenStreamFetcher) fetch_image_open_stream, 
+                                            (gpointer) fidata->uri);
        g_free (fidata->cache_id);
        g_free (fidata->uri);
 
        if (cache_stream != NULL) {
-               tny_stream_write_to_stream (cache_stream, fidata->output_stream);
+               char buffer[4096];
+
+               while (G_LIKELY (!tny_stream_is_eos (cache_stream))) {
+                       gssize nb_read;
+
+                       nb_read = tny_stream_read (cache_stream, buffer, sizeof (buffer));
+                       if (G_UNLIKELY (nb_read < 0)) {
+                               break;
+                       } else if (G_LIKELY (nb_read > 0)) {
+                               gssize nb_written = 0;
+
+                               while (G_UNLIKELY (nb_written < nb_read)) {
+                                       gssize len;
+
+                                       len = tny_stream_write (fidata->output_stream, buffer + nb_written,
+                                                               nb_read - nb_written);
+                                       if (G_UNLIKELY (len < 0))
+                                               break;
+                                       nb_written += len;
+                               }
+                       }
+               }
                tny_stream_close (cache_stream);
                g_object_unref (cache_stream);
        }
@@ -3109,10 +3166,7 @@ on_fetch_image_thread (gpointer userdata)
        tny_stream_close (fidata->output_stream);
        g_object_unref (fidata->output_stream);
 
-
-       gdk_threads_enter ();
        g_idle_add (on_fetch_image_idle_refresh_view, fidata);
-       gdk_threads_leave ();
 
        return NULL;
 }
@@ -3133,10 +3187,12 @@ on_fetch_image (ModestMsgView *msgview,
 
        fidata = g_slice_new0 (FetchImageData);
        fidata->msg_view = g_object_ref (msgview);
+       fidata->window = g_object_ref (window);
        fidata->uri = g_strdup (uri);
        fidata->cache_id = modest_images_cache_get_id (current_account, uri);
        fidata->output_stream = g_object_ref (stream);
 
+       priv->fetching_images++;
        if (g_thread_create (on_fetch_image_thread, fidata, FALSE, NULL) == NULL) {
                g_object_unref (fidata->output_stream);
                g_free (fidata->cache_id);
@@ -3144,8 +3200,11 @@ on_fetch_image (ModestMsgView *msgview,
                g_object_unref (fidata->msg_view);
                g_slice_free (FetchImageData, fidata);
                tny_stream_close (stream);
+               priv->fetching_images--;
+               update_progress_hint (window);
                return FALSE;
        }
+       update_progress_hint (window);
 
        return TRUE;
 }
@@ -3183,6 +3242,13 @@ setup_menu (ModestMsgViewWindow *self)
        modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_viewer_addtocontacts"), NULL,
                                           APP_MENU_CALLBACK (modest_ui_actions_add_to_contacts),
                                           MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_add_to_contacts));
+
+       modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mail_bd_external_images"), NULL,
+                                          APP_MENU_CALLBACK (modest_ui_actions_on_fetch_images),
+                                          MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_fetch_images));
+       modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_ti_message_properties"), NULL,
+                                          APP_MENU_CALLBACK (modest_ui_actions_on_details),
+                                          MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_details));
 }
 
 void
@@ -3261,11 +3327,46 @@ _modest_msg_view_window_map_event (GtkWidget *widget,
                                   gpointer userdata)
 {
        ModestMsgViewWindow *self = (ModestMsgViewWindow *) userdata;
-       ModestMsgViewWindowPrivate *priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
 
-       if (priv->progress_hint) {
-               hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), TRUE);
-       }
+       update_progress_hint (self);
 
        return FALSE;
 }
+
+void
+modest_msg_view_window_fetch_images (ModestMsgViewWindow *self)
+{
+       ModestMsgViewWindowPrivate *priv;
+       priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
+
+       modest_msg_view_request_fetch_images (MODEST_MSG_VIEW (priv->msg_view));
+}
+
+gboolean 
+modest_msg_view_window_has_blocked_external_images (ModestMsgViewWindow *self)
+{
+       ModestMsgViewWindowPrivate *priv;
+       priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
+
+       g_return_val_if_fail (MODEST_IS_MSG_VIEW_WINDOW (self), FALSE);
+
+       return modest_msg_view_has_blocked_external_images (MODEST_MSG_VIEW (priv->msg_view));
+}
+
+void 
+modest_msg_view_window_reload (ModestMsgViewWindow *self)
+{
+       ModestMsgViewWindowPrivate *priv;
+       TnyHeader *header;
+
+       g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (self));
+
+       priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (self);
+       header = modest_msg_view_window_get_header (MODEST_MSG_VIEW_WINDOW (self));     
+
+       if (!message_reader (self, priv, header, priv->row_reference)) {
+               g_warning ("Shouldn't happen, trying to reload a message failed");
+       }
+
+       g_object_unref (header);
+}