Added msg view window reload feature. Now we can force a message reload
[modest] / src / maemo / modest-msg-view-window.c
index 7c1d972..165dc80 100644 (file)
@@ -775,6 +775,7 @@ select_next_valid_row (GtkTreeModel *model,
                                if (msg_is_visible (header, is_outbox)) {
                                        next = gtk_tree_model_get_path (model, &tmp_iter);
                                        *row_reference = gtk_tree_row_reference_new (model, next);
+                                       gtk_tree_path_free (next);
                                        retval = TRUE;
                                        finished = TRUE;
                                }
@@ -804,6 +805,7 @@ select_next_valid_row (GtkTreeModel *model,
                                   message */
                                finished = TRUE;
                        }
+                       gtk_tree_path_free (next);
                } else {
                        /* If there are no more messages and we don't
                           want to start again in the first one then
@@ -814,8 +816,6 @@ select_next_valid_row (GtkTreeModel *model,
 
        /* Free */
        gtk_tree_path_free (path);
-       if (next)
-               gtk_tree_path_free (next);
 
        return retval;
 }
@@ -934,6 +934,7 @@ modest_msg_view_window_construct (ModestMsgViewWindow *self,
 ModestWindow *
 modest_msg_view_window_new_with_header_model (TnyMsg *msg, 
                                              const gchar *modest_account_name,
+                                             const gchar *mailbox, /*ignored */
                                              const gchar *msg_uid,
                                              GtkTreeModel *model, 
                                              GtkTreeRowReference *row_reference)
@@ -1032,6 +1033,7 @@ modest_msg_view_window_new_with_header_model (TnyMsg *msg,
 ModestWindow *
 modest_msg_view_window_new_for_search_result (TnyMsg *msg, 
                                              const gchar *modest_account_name,
+                                             const gchar *mailbox, /*ignored*/
                                              const gchar *msg_uid)
 {
        ModestMsgViewWindow *window = NULL;
@@ -1065,8 +1067,9 @@ modest_msg_view_window_new_for_search_result (TnyMsg *msg,
 
 ModestWindow *
 modest_msg_view_window_new_for_attachment (TnyMsg *msg, 
-                           const gchar *modest_account_name,
-                           const gchar *msg_uid)
+                                          const gchar *modest_account_name,
+                                          const gchar *mailbox, /* ignored */
+                                          const gchar *msg_uid)
 {
        GObject *obj = NULL;
        ModestMsgViewWindowPrivate *priv;       
@@ -1425,6 +1428,7 @@ modest_msg_view_window_toggle_find_toolbar (GtkToggleAction *toggle,
                hildon_find_toolbar_highlight_entry (HILDON_FIND_TOOLBAR (priv->find_toolbar), TRUE);
        } else {
                gtk_widget_hide (priv->find_toolbar);
+               modest_msg_view_grab_focus (MODEST_MSG_VIEW (priv->msg_view));
        }
 
        /* update the toggle buttons status */
@@ -1467,7 +1471,7 @@ modest_msg_view_window_find_toolbar_search (GtkWidget *widget,
 
        if ((current_search == NULL) || (strcmp (current_search, "") == 0)) {
                g_free (current_search);
-               hildon_banner_show_information (NULL, NULL, dgettext("hildon-common-strings", "ecdg_ib_find_rep_enter_text"));
+               hildon_banner_show_information (NULL, NULL, _CS("ecdg_ib_find_rep_enter_text"));
                return;
        }
 
@@ -1478,7 +1482,7 @@ modest_msg_view_window_find_toolbar_search (GtkWidget *widget,
                result = modest_isearch_view_search (MODEST_ISEARCH_VIEW (priv->msg_view),
                                                     priv->last_search);
                if (!result) {
-                       hildon_banner_show_information (NULL, NULL, dgettext("hildon-libs", "ckct_ib_find_no_matches"));
+                       hildon_banner_show_information (NULL, NULL, _HL("ckct_ib_find_no_matches"));
                        g_free (priv->last_search);
                        priv->last_search = NULL;
                } else {
@@ -1487,7 +1491,7 @@ modest_msg_view_window_find_toolbar_search (GtkWidget *widget,
                }
        } else {
                if (!modest_isearch_view_search_next (MODEST_ISEARCH_VIEW (priv->msg_view))) {
-                       hildon_banner_show_information (NULL, NULL, dgettext("hildon-libs", "ckct_ib_find_search_complete"));
+                       hildon_banner_show_information (NULL, NULL, _HL("ckct_ib_find_search_complete"));
                        g_free (priv->last_search);
                        priv->last_search = NULL;
                } else {
@@ -1546,7 +1550,7 @@ modest_msg_view_window_zoom_plus (ModestWindow *window)
        group = gtk_radio_action_get_group (zoom_radio_action);
 
        if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (group->data))) {
-               hildon_banner_show_information (NULL, NULL, dgettext("hildon-common-strings", "ckct_ib_max_zoom_level_reached"));
+               hildon_banner_show_information (NULL, NULL, _CS("ckct_ib_max_zoom_level_reached"));
                return FALSE;
        }
 
@@ -1578,7 +1582,8 @@ modest_msg_view_window_zoom_minus (ModestWindow *window)
                                gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (node->next->data), TRUE);
                                return TRUE;
                        } else {
-                         hildon_banner_show_information (NULL, NULL, dgettext("hildon-common-strings", "ckct_ib_min_zoom_level_reached"));
+                         hildon_banner_show_information (NULL, NULL, 
+                                                         _CS("ckct_ib_min_zoom_level_reached"));
                                return FALSE;
                        }
                        break;
@@ -2389,7 +2394,7 @@ on_mail_operation_started (ModestMailOperation *mail_op,
        tmp = priv->progress_widgets;
        source = modest_mail_operation_get_source(mail_op);
        if (G_OBJECT (self) == source) {
-               if (op_type == MODEST_MAIL_OPERATION_TYPE_RECEIVE || op_type == MODEST_MAIL_OPERATION_TYPE_OPEN ) {
+               if (op_type == MODEST_MAIL_OPERATION_TYPE_RECEIVE ) {
                        set_toolbar_transfer_mode(self);
                        while (tmp) {
                                modest_progress_object_add_operation (
@@ -2416,7 +2421,7 @@ on_mail_operation_finished (ModestMailOperation *mail_op,
        op_type = modest_mail_operation_get_type_operation (mail_op);
        tmp = priv->progress_widgets;
        
-       if (op_type == MODEST_MAIL_OPERATION_TYPE_RECEIVE || op_type == MODEST_MAIL_OPERATION_TYPE_OPEN ) {
+       if (op_type == MODEST_MAIL_OPERATION_TYPE_RECEIVE ) {
                while (tmp) {
                        modest_progress_object_remove_operation (MODEST_PROGRESS_OBJECT (tmp->data),
                                                                 mail_op);
@@ -2487,14 +2492,69 @@ modest_msg_view_window_get_attachments (ModestMsgViewWindow *win)
        return selected_attachments;
 }
 
+typedef struct {
+       gchar *filepath;
+       GtkWidget *banner;
+       guint banner_idle_id;
+} DecodeAsyncHelper;
+
+static gboolean
+decode_async_banner_idle (gpointer user_data)
+{
+       DecodeAsyncHelper *helper = (DecodeAsyncHelper *) user_data;
+
+       helper->banner_idle_id = 0;
+       helper->banner = hildon_banner_show_animation (NULL, NULL, _("mail_me_opening"));
+       g_object_ref (helper->banner);
+
+       return FALSE;
+}
+
+static void
+on_decode_to_stream_async_handler (TnyMimePart *mime_part, 
+                                  gboolean cancelled, 
+                                  TnyStream *stream, 
+                                  GError *err, 
+                                  gpointer user_data)
+{
+       DecodeAsyncHelper *helper = (DecodeAsyncHelper *) user_data;
+
+       if (helper->banner_idle_id > 0) {
+               g_source_remove (helper->banner_idle_id);
+               helper->banner_idle_id = 0;
+       }
+       if (helper->banner) {
+               gtk_widget_destroy (helper->banner);
+       }
+       if (cancelled || err) {
+               modest_platform_information_banner (NULL, NULL, 
+                                                   _("mail_ib_file_operation_failed"));
+               goto free;
+       }
+
+       /* make the file read-only */
+       g_chmod(helper->filepath, 0444);
+       
+       /* Activate the file */
+       modest_platform_activate_file (helper->filepath, modest_tny_mime_part_get_content_type (mime_part));
+
+ free:
+       /* Frees */
+       g_free (helper->filepath);
+       g_object_unref (helper->banner);
+       g_slice_free (DecodeAsyncHelper, helper);
+}
+
 void
-modest_msg_view_window_view_attachment (ModestMsgViewWindow *window, TnyMimePart *mime_part)
+modest_msg_view_window_view_attachment (ModestMsgViewWindow *window, 
+                                       TnyMimePart *mime_part)
 {
        ModestMsgViewWindowPrivate *priv;
        const gchar *msg_uid;
        gchar *attachment_uid = NULL;
        gint attachment_index = 0;
        TnyList *attachments;
+       TnyMimePart *window_msg;
 
        g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window));
        g_return_if_fail (TNY_IS_MIME_PART (mime_part) || (mime_part == NULL));
@@ -2536,49 +2596,44 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window, TnyMimePart
                return;
        }
 
-       if (!modest_tny_mime_part_is_msg (mime_part)) {
+       /* we also check for mime_part == priv->msg, as this means it's a direct attachment
+        * shown as attachment, so it should behave as a file */
+       window_msg = TNY_MIME_PART (tny_msg_view_get_msg (TNY_MSG_VIEW (priv->msg_view)));
+       if (!modest_tny_mime_part_is_msg (mime_part)||
+           mime_part == window_msg) {
                gchar *filepath = NULL;
                const gchar *att_filename = tny_mime_part_get_filename (mime_part);
-               const gchar *content_type;
                gboolean show_error_banner = FALSE;
-               GError *err;
                TnyFsStream *temp_stream = NULL;
                temp_stream = modest_utils_create_temp_stream (att_filename, attachment_uid,
                                                               &filepath);
                
                if (temp_stream != NULL) {
-                       content_type = tny_mime_part_get_content_type (mime_part);
-                       if (tny_mime_part_decode_to_stream (mime_part, TNY_STREAM (temp_stream), &err) >= 0) {
-                               /* make the file read-only */
-                               if (g_chmod(filepath, 0444) != 0)
-                                       g_warning ("%s: failed to set file '%s' to read-only: %s",
-                                                       __FUNCTION__, filepath, strerror(errno));
-
-                               modest_platform_activate_file (filepath, content_type);
-                       } else {
-                               /* error while saving attachment, maybe cerm_device_memory_full */
-                               show_error_banner = TRUE;
-                               if (err != NULL) {
-                                       g_warning ("%s: tny_mime_part_decode_to_stream failed (%s)", __FUNCTION__, err->message);
-                                       g_error_free (err);
-                               }
-                       }
+                       DecodeAsyncHelper *helper = g_slice_new (DecodeAsyncHelper);
+                       helper->filepath = g_strdup (filepath);
+                       helper->banner = NULL;
+                       helper->banner_idle_id = g_timeout_add (1000, decode_async_banner_idle, helper);
+                       tny_mime_part_decode_to_stream_async (mime_part, TNY_STREAM (temp_stream), 
+                                                             on_decode_to_stream_async_handler, 
+                                                             NULL, 
+                                                             helper);
                        g_object_unref (temp_stream);
-                       g_free (filepath);
                        /* NOTE: files in the temporary area will be automatically
                         * cleaned after some time if they are no longer in use */
                } else {
-                       if (filepath != NULL) {
+                       if (filepath) {
+                               const gchar *content_type;
                                /* the file may already exist but it isn't writable,
                                 * let's try to open it anyway */
-                               content_type = tny_mime_part_get_content_type (mime_part);
+                               content_type = modest_tny_mime_part_get_content_type (mime_part);
                                modest_platform_activate_file (filepath, content_type);
-                               g_free (filepath);
                        } else {
                                g_warning ("%s: modest_utils_create_temp_stream failed", __FUNCTION__);
                                show_error_banner = TRUE;
                        }
                }
+               if (filepath)
+                       g_free (filepath);
                if (show_error_banner)
                        modest_platform_information_banner (NULL, NULL, _("mail_ib_file_operation_failed"));
        } else {
@@ -2605,13 +2660,15 @@ modest_msg_view_window_view_attachment (ModestMsgViewWindow *window, TnyMimePart
                        gchar *account = g_strdup (modest_window_get_active_account (MODEST_WINDOW (window)));
                        if (!account)
                                account = modest_account_mgr_get_default_account (modest_runtime_get_account_mgr ());
-                       msg_win = modest_msg_view_window_new_for_attachment (TNY_MSG (mime_part), account, attachment_uid);
+                       msg_win = modest_msg_view_window_new_for_attachment (TNY_MSG (mime_part), account, 
+                                                                            NULL, 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_mgr_register_window (mgr, msg_win, MODEST_WINDOW (window));
                        gtk_widget_show_all (GTK_WIDGET (msg_win));
                }
        }
+       g_object_unref (window_msg);
        g_object_unref (mime_part);
 }
 
@@ -2666,10 +2723,11 @@ 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, dgettext("ke-recv", 
-                                                                            "cerm_device_memory_full"));
+                       hildon_banner_show_information (NULL, NULL, 
+                                                       _KR("cerm_device_memory_full"));
                } else {
-                       hildon_banner_show_information (NULL, NULL, _("mail_ib_file_operation_failed"));
+                       hildon_banner_show_information (NULL, NULL, 
+                                                       _("mail_ib_file_operation_failed"));
                }
                gdk_threads_leave (); /* CHECKED */
        }
@@ -2686,8 +2744,11 @@ save_mime_part_to_file (SaveMimePartInfo *info)
 
        info->result = gnome_vfs_create (&handle, pair->filename, GNOME_VFS_OPEN_WRITE, FALSE, 0644);
        if (info->result == GNOME_VFS_OK) {
+               GError *error = NULL;
                stream = tny_vfs_stream_new (handle);
-               if (tny_mime_part_decode_to_stream (pair->part, stream, NULL) < 0) {
+               if (tny_mime_part_decode_to_stream (pair->part, stream, &error) < 0) {
+                       g_warning ("modest: could not save attachment %s: %d (%s)\n", pair->filename, error?error->code:-1, error?error->message:"Unknown error");
+                       
                        info->result = GNOME_VFS_ERROR_IO;
                }
                g_object_unref (G_OBJECT (stream));
@@ -2695,6 +2756,7 @@ save_mime_part_to_file (SaveMimePartInfo *info)
                g_slice_free (SaveMimePartPair, pair);
                info->pairs = g_list_delete_link (info->pairs, info->pairs);
        } else {
+               g_warning ("modest: could not create save attachment %s: %s\n", pair->filename, gnome_vfs_result_to_string (info->result));
                save_mime_part_info_free (info, FALSE);
        }
 
@@ -2756,7 +2818,7 @@ save_attachments_response (GtkDialog *dialog,
 
        if (!modest_utils_folder_writable (chooser_uri)) {
                hildon_banner_show_information 
-                       (NULL, NULL, dgettext("hildon-fm", "sfil_ib_readonly_location"));
+                       (NULL, NULL, _FM("sfil_ib_readonly_location"));
        } else {
                TnyIterator *iter;
 
@@ -2768,7 +2830,7 @@ save_attachments_response (GtkDialog *dialog,
                            !tny_mime_part_is_purged (mime_part) &&
                            (tny_mime_part_get_filename (mime_part) != NULL)) {
                                SaveMimePartPair *pair;
-                                       
+
                                pair = g_slice_new0 (SaveMimePartPair);
 
                                if (tny_list_get_length (mime_parts) > 1) {
@@ -2809,6 +2871,7 @@ modest_msg_view_window_save_attachments (ModestMsgViewWindow *window, TnyList *m
        gchar *folder = NULL;
        gchar *filename = NULL;
        gchar *save_multiple_str = NULL;
+       TnyMsg *window_msg;
 
        g_return_if_fail (MODEST_IS_MSG_VIEW_WINDOW (window));
        priv = MODEST_MSG_VIEW_WINDOW_GET_PRIVATE (window);
@@ -2821,6 +2884,7 @@ modest_msg_view_window_save_attachments (ModestMsgViewWindow *window, TnyList *m
                g_object_ref (mime_parts);
        }
 
+       window_msg = tny_msg_view_get_msg (TNY_MSG_VIEW (priv->msg_view));
        /* prepare dialog */
        if (tny_list_get_length (mime_parts) == 1) {
                TnyIterator *iter;
@@ -2843,6 +2907,7 @@ modest_msg_view_window_save_attachments (ModestMsgViewWindow *window, TnyList *m
                save_multiple_str = g_strdup_printf (_FM("sfil_va_number_of_objects_attachments"), 
                                                     tny_list_get_length (mime_parts));
        }
+       g_object_unref (window_msg);
        
        save_dialog = hildon_file_chooser_dialog_new (GTK_WINDOW (window), 
                                                      GTK_FILE_CHOOSER_ACTION_SAVE);
@@ -3149,3 +3214,48 @@ on_fetch_image (ModestMsgView *msgview,
 
        return TRUE;;
 }
+
+void
+modest_msg_view_window_add_to_contacts (ModestMsgViewWindow *self)
+{
+       modest_ui_actions_on_add_to_contacts (NULL, MODEST_WINDOW (self));
+}
+
+
+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);
+}