From 587841e2cb7a496338354f15385eea4deee3a3e1 Mon Sep 17 00:00:00 2001 From: Sergio Villar Senin Date: Wed, 14 May 2008 11:11:29 +0000 Subject: [PATCH] * Added a new parameter to modest_platform_check_memory_low in order to show or not a visual notification * Fixes NB#85299, do not crash in memory low conditions, added memory low checks to the mail operations pmo-trunk-r4526 --- src/dbus_api/modest-dbus-callbacks.c | 2 +- src/gnome/modest-platform.c | 4 +- src/maemo/modest-msg-edit-window.c | 5 +-- src/maemo/modest-platform.c | 11 +++--- src/modest-error.h | 1 + src/modest-mail-operation.c | 67 ++++++++++++++++++++++++++++++++-- src/modest-platform.h | 4 +- src/modest-ui-actions.c | 61 ++++++++++++++++++++++++------- 8 files changed, 127 insertions(+), 28 deletions(-) diff --git a/src/dbus_api/modest-dbus-callbacks.c b/src/dbus_api/modest-dbus-callbacks.c index 6cb03ba..8e4c047 100644 --- a/src/dbus_api/modest-dbus-callbacks.c +++ b/src/dbus_api/modest-dbus-callbacks.c @@ -1842,7 +1842,7 @@ modest_dbus_req_filter (DBusConnection *con, MODEST_DBUS_METHOD_SEARCH)) { /* don't try to search when there not enough mem */ - if (modest_platform_check_memory_low (NULL)) { + if (modest_platform_check_memory_low (NULL, TRUE)) { g_warning ("%s: not enough memory for searching", __FUNCTION__); reply_empty_results (con, message); diff --git a/src/gnome/modest-platform.c b/src/gnome/modest-platform.c index af39057..c11e23b 100644 --- a/src/gnome/modest-platform.c +++ b/src/gnome/modest-platform.c @@ -499,7 +499,9 @@ modest_platform_get_account_settings_wizard (void) -gboolean modest_platform_check_memory_low (ModestWindow *win) +gboolean modest_platform_check_memory_low (ModestWindow *win, + gboolean visuals) { + g_debug ("%s not implemented", __FUNCTION__); return FALSE; } diff --git a/src/maemo/modest-msg-edit-window.c b/src/maemo/modest-msg-edit-window.c index 5a4180c..2cba7b2 100644 --- a/src/maemo/modest-msg-edit-window.c +++ b/src/maemo/modest-msg-edit-window.c @@ -2266,7 +2266,7 @@ modest_msg_edit_window_offer_attach_file (ModestMsgEditWindow *window) priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window); - if (modest_platform_check_memory_low (MODEST_WINDOW(window))) + if (modest_platform_check_memory_low (MODEST_WINDOW(window), TRUE)) return; dialog = hildon_file_chooser_dialog_new (GTK_WINDOW (window), GTK_FILE_CHOOSER_ACTION_OPEN); @@ -2667,10 +2667,9 @@ modest_msg_edit_window_open_addressbook (ModestMsgEditWindow *window, /* we check for low-mem; in that case, show a warning, and don't allow * for the addressbook */ - if (modest_platform_check_memory_low (MODEST_WINDOW(window))) + if (modest_platform_check_memory_low (MODEST_WINDOW(window), TRUE)) return; - priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window); if (editor == NULL) { diff --git a/src/maemo/modest-platform.c b/src/maemo/modest-platform.c index 666fdef..7f3f6b2 100644 --- a/src/maemo/modest-platform.c +++ b/src/maemo/modest-platform.c @@ -2276,24 +2276,23 @@ modest_platform_get_current_connection (void) gboolean -modest_platform_check_memory_low (ModestWindow *win) +modest_platform_check_memory_low (ModestWindow *win, + gboolean visuals) { gboolean lowmem; - - g_return_val_if_fail (win == NULL || MODEST_IS_WINDOW(win), FALSE); /* are we in low memory state? */ lowmem = osso_mem_in_lowmem_state () ? TRUE : FALSE; - if (win && lowmem) + if (win && lowmem && visuals) modest_platform_run_information_dialog ( GTK_WINDOW(win), dgettext("ke-recv","memr_ib_operation_disabled"), TRUE); if (lowmem) - g_warning ("%s: low memory reached. disallowing some operations", - __FUNCTION__); + g_debug ("%s: low memory reached. disallowing some operations", + __FUNCTION__); return lowmem; } diff --git a/src/modest-error.h b/src/modest-error.h index 2e2d2ae..e929517 100644 --- a/src/modest-error.h +++ b/src/modest-error.h @@ -49,6 +49,7 @@ typedef enum _ModestErrorCode { MODEST_MAIL_OPERATION_ERROR_INSTANCE_CREATION_FAILED, MODEST_MAIL_OPERATION_ERROR_FILE_IO, /* couldn't retrieve a file to construct a mail */ MODEST_MAIL_OPERATION_ERROR_SEND_QUEUE_ADD_ERROR, + MODEST_MAIL_OPERATION_ERROR_LOW_MEMORY, } ModestErrorCode; G_END_DECLS diff --git a/src/modest-mail-operation.c b/src/modest-mail-operation.c index 02ae707..3098926 100644 --- a/src/modest-mail-operation.c +++ b/src/modest-mail-operation.c @@ -107,6 +107,8 @@ static void sync_folder_finish_callback (TnyFolder *self, GError *err, gpointer user_data); +static gboolean _check_memory_low (ModestMailOperation *mail_op); + enum _ModestMailOperationSignals { PROGRESS_CHANGED_SIGNAL, @@ -2235,13 +2237,20 @@ modest_mail_operation_get_msg (ModestMailOperation *self, g_return_if_fail (TNY_IS_HEADER (header)); priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self); - folder = tny_header_get_folder (header); - priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS; priv->total = 1; priv->done = 0; + /* Check memory low */ + if (_check_memory_low (self)) { + if (user_callback) + user_callback (self, header, FALSE, NULL, priv->error, user_data); + modest_mail_operation_notify_end (self); + return; + } + /* Get account and set it into mail_operation */ + folder = tny_header_get_folder (header); priv->account = modest_tny_folder_get_account (TNY_FOLDER(folder)); /* Check for cached messages */ @@ -2405,6 +2414,28 @@ modest_mail_operation_get_msgs_full (ModestMailOperation *self, priv->done = 0; priv->total = tny_list_get_length(header_list); + /* Check memory low */ + if (_check_memory_low (self)) { + if (user_callback) { + TnyHeader *header = NULL; + TnyIterator *iter; + + if (tny_list_get_length (header_list) > 0) { + iter = tny_list_create_iterator (header_list); + header = (TnyHeader *) tny_iterator_get_current (iter); + g_object_unref (iter); + } + user_callback (self, header, FALSE, NULL, priv->error, user_data); + if (header) + g_object_unref (header); + } + if (notify) + notify (user_data); + /* Notify about operation end */ + modest_mail_operation_notify_end (self); + return; + } + /* Check uncached messages */ for (iter = tny_list_create_iterator (header_list), has_uncached_messages = FALSE; !has_uncached_messages && !tny_iterator_is_done (iter); @@ -3039,9 +3070,17 @@ modest_mail_operation_refresh_folder (ModestMailOperation *self, priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self); - priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS; + /* Check memory low */ + if (_check_memory_low (self)) { + if (user_callback) + user_callback (self, folder, user_data); + /* Notify about operation end */ + modest_mail_operation_notify_end (self); + return; + } /* Get account and set it into mail_operation */ + priv->status = MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS; priv->account = modest_tny_folder_get_account (folder); priv->op_type = MODEST_MAIL_OPERATION_TYPE_RECEIVE; @@ -3277,3 +3316,25 @@ modest_mail_operation_to_string (ModestMailOperation *self) priv->done, priv->total, priv->error && priv->error->message ? priv->error->message : ""); } + +/* + * Once the mail operations were objects this will be no longer + * needed. I don't like it, but we need it for the moment + */ +static gboolean +_check_memory_low (ModestMailOperation *mail_op) +{ + if (modest_platform_check_memory_low (NULL, FALSE)) { + ModestMailOperationPrivate *priv; + + priv = MODEST_MAIL_OPERATION_GET_PRIVATE (mail_op); + priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED; + g_set_error (&(priv->error), + MODEST_MAIL_OPERATION_ERROR, + MODEST_MAIL_OPERATION_ERROR_LOW_MEMORY, + "Not enough memory to complete the operation"); + return TRUE; + } else { + return FALSE; + } +} diff --git a/src/modest-platform.h b/src/modest-platform.h index c196d28..9098084 100644 --- a/src/modest-platform.h +++ b/src/modest-platform.h @@ -513,13 +513,15 @@ ModestConnectedVia modest_platform_get_current_connection (void); * modest_platform_check_memory_low: * * @win: a ModestWindow, or NULL + * @visuals: whether or not show visual information * * see if memory is too low for big memory consuming operations * optionally show a warning dialog if @win was provided * * Returns: TRUE if we're in lowmem state, FALSE otherwise */ -gboolean modest_platform_check_memory_low (ModestWindow *win); +gboolean modest_platform_check_memory_low (ModestWindow *win, + gboolean visuals); G_END_DECLS diff --git a/src/modest-ui-actions.c b/src/modest-ui-actions.c index 0a1e0ea..2fa174c 100644 --- a/src/modest-ui-actions.c +++ b/src/modest-ui-actions.c @@ -702,7 +702,7 @@ modest_ui_actions_compose_msg(ModestWindow *win, /* we check for low-mem; in that case, show a warning, and don't allow * composing a message with attachments */ - if (attachments && modest_platform_check_memory_low (win)) + if (attachments && modest_platform_check_memory_low (win, TRUE)) goto cleanup; account_name = modest_account_mgr_get_default_account(mgr); @@ -801,6 +801,17 @@ modest_ui_actions_msg_retrieval_check (ModestMailOperation *mail_op, /* If there is no message or the operation was not successful */ status = modest_mail_operation_get_status (mail_op); if (!msg || status != MODEST_MAIL_OPERATION_STATUS_SUCCESS) { + const GError *error; + + /* If it's a memory low issue, then show a banner */ + error = modest_mail_operation_get_error (mail_op); + if (error && error->code == MODEST_MAIL_OPERATION_ERROR_LOW_MEMORY) { + GObject *source = modest_mail_operation_get_source (mail_op); + modest_platform_run_information_dialog (GTK_IS_WINDOW (source) ? GTK_WINDOW (source) : NULL, + dgettext("ke-recv","memr_ib_operation_disabled"), + TRUE); + g_object_unref (source); + } /* Remove the header from the preregistered uids */ modest_window_mgr_unregister_header (modest_runtime_get_window_mgr (), @@ -1402,7 +1413,7 @@ modest_ui_actions_on_open (GtkAction *action, ModestWindow *win) /* we check for low-mem; in that case, show a warning, and don't allow * opening */ - if (modest_platform_check_memory_low (MODEST_WINDOW(win))) + if (modest_platform_check_memory_low (MODEST_WINDOW(win), TRUE)) return; /* Get headers */ @@ -1643,7 +1654,7 @@ reply_forward (ReplyForwardAction action, ModestWindow *win) /* we check for low-mem; in that case, show a warning, and don't allow * reply/forward (because it could potentially require a lot of memory */ - if (modest_platform_check_memory_low (MODEST_WINDOW(win))) + if (modest_platform_check_memory_low (MODEST_WINDOW(win), TRUE)) return; @@ -2245,7 +2256,7 @@ modest_ui_actions_on_header_activated (ModestHeaderView *header_view, /* we check for low-mem; in that case, show a warning, and don't allow * activating headers */ - if (modest_platform_check_memory_low (MODEST_WINDOW(main_window))) + if (modest_platform_check_memory_low (MODEST_WINDOW(main_window), TRUE)) return; modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (main_window)); @@ -2300,10 +2311,21 @@ folder_refreshed_cb (ModestMailOperation *mail_op, { ModestMainWindow *win = NULL; GtkWidget *header_view; + const GError *error; g_return_if_fail (TNY_IS_FOLDER (folder)); win = MODEST_MAIN_WINDOW (user_data); + + /* Check if the operation failed due to memory low conditions */ + error = modest_mail_operation_get_error (mail_op); + if (error && error->code == MODEST_MAIL_OPERATION_ERROR_LOW_MEMORY) { + modest_platform_run_information_dialog (GTK_WINDOW (win), + dgettext("ke-recv","memr_ib_operation_disabled"), + TRUE); + return; + } + header_view = modest_main_window_get_child_widget(win, MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW); @@ -2482,7 +2504,7 @@ modest_ui_actions_on_msg_attachment_clicked (ModestMsgView *msgview, TnyMimePart /* we check for low-mem; in that case, show a warning, and don't allow * viewing attachments */ - if (modest_platform_check_memory_low (MODEST_WINDOW(win))) + if (modest_platform_check_memory_low (MODEST_WINDOW(win), TRUE)) return; modest_msg_view_window_view_attachment (MODEST_MSG_VIEW_WINDOW (win), mime_part); @@ -2566,7 +2588,7 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi */ if (expected_size > MODEST_MAX_LOW_MEMORY_MESSAGE_SIZE) { - if (modest_platform_check_memory_low (MODEST_WINDOW(edit_window))) { + if (modest_platform_check_memory_low (MODEST_WINDOW(edit_window), TRUE)) { modest_msg_edit_window_free_msg_data (edit_window, data); return FALSE; } @@ -2736,7 +2758,7 @@ modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window) * this should still allow for sending anything critical... */ if (expected_size > MODEST_MAX_LOW_MEMORY_MESSAGE_SIZE) { - if (modest_platform_check_memory_low (MODEST_WINDOW(edit_window))) { + if (modest_platform_check_memory_low (MODEST_WINDOW(edit_window), TRUE)) { modest_msg_edit_window_free_msg_data (edit_window, data); return FALSE; } @@ -2958,7 +2980,7 @@ modest_ui_actions_on_insert_image (GtkAction *action, g_return_if_fail (GTK_IS_ACTION (action)); - if (modest_platform_check_memory_low (MODEST_WINDOW(window))) + if (modest_platform_check_memory_low (MODEST_WINDOW(window), TRUE)) return; if (modest_msg_edit_window_get_format (MODEST_MSG_EDIT_WINDOW(window)) == MODEST_MSG_EDIT_FORMAT_TEXT) @@ -2974,7 +2996,7 @@ modest_ui_actions_on_attach_file (GtkAction *action, g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (window)); g_return_if_fail (GTK_IS_ACTION (action)); - if (modest_platform_check_memory_low (MODEST_WINDOW(window))) + if (modest_platform_check_memory_low (MODEST_WINDOW(window), TRUE)) return; modest_msg_edit_window_offer_attach_file (window); @@ -5447,7 +5469,7 @@ modest_ui_actions_save_attachments (GtkAction *action, { if (MODEST_IS_MSG_VIEW_WINDOW (window)) { - if (modest_platform_check_memory_low (MODEST_WINDOW(window))) + if (modest_platform_check_memory_low (MODEST_WINDOW(window), TRUE)) return; modest_msg_view_window_save_attachments (MODEST_MSG_VIEW_WINDOW (window), NULL); @@ -5504,6 +5526,19 @@ modest_ui_actions_on_help (GtkAction *action, g_warning ("%s: no help for window %p", __FUNCTION__, win); } +static void +retrieve_contents_cb (ModestMailOperation *mail_op, + TnyHeader *header, + gboolean canceled, + TnyMsg *msg, + GError *err, + gpointer user_data) +{ + /* We only need this callback to show an error in case of + memory low condition */ + modest_ui_actions_msg_retrieval_check (mail_op, header, msg); +} + static void retrieve_msg_contents_performer (gboolean canceled, GError *err, @@ -5528,7 +5563,7 @@ retrieve_msg_contents_performer (gboolean canceled, modest_ui_actions_disk_operations_error_handler, NULL, NULL); modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), mail_op); - modest_mail_operation_get_msgs_full (mail_op, headers, NULL, NULL, NULL); + modest_mail_operation_get_msgs_full (mail_op, headers, retrieve_contents_cb, NULL, NULL); /* Frees */ g_object_unref (mail_op); @@ -5688,7 +5723,7 @@ modest_ui_actions_on_search_messages (GtkAction *action, ModestWindow *window) /* we check for low-mem; in that case, show a warning, and don't allow * searching */ - if (modest_platform_check_memory_low (window)) + if (modest_platform_check_memory_low (window, TRUE)) return; modest_platform_show_search_messages (GTK_WINDOW (window)); @@ -5703,7 +5738,7 @@ modest_ui_actions_on_open_addressbook (GtkAction *action, ModestWindow *win) /* we check for low-mem; in that case, show a warning, and don't allow * for the addressbook */ - if (modest_platform_check_memory_low (win)) + if (modest_platform_check_memory_low (win, TRUE)) return; -- 1.7.9.5