* src/widgets/modest-msg-edit-window.h:
authorJose Dapena Paz <jdapena@igalia.com>
Wed, 23 Jan 2008 00:04:36 +0000 (00:04 +0000)
committerJose Dapena Paz <jdapena@igalia.com>
Wed, 23 Jan 2008 00:04:36 +0000 (00:04 +0000)
        * Added methods for getting estimation of parts size in message.
* src/modest-window-mgr.c:
        * If save to drafts fails because of disk being full, then
          we don't close the editor window.
* src/widgets/modest-attachment-view.[ch]:
        * Added methods to set the size externally, and be able to avoid
          counting the size going through the full stream.
* src/widgets/modest-window.c:
        * Close from escape shortcut only on view window, not only on
          edit window. This was really not working properly, as press
          was processed and release was processed after the dialog, if
          more than one second was spent, then the press was considered
          a force close.
* src/widgets/modest-attachments-view.[ch]:
        * Added methods to get the size of attachments inside.
* src/modest-tny-msg.[ch]:
        * Added method to estimate the storage size of a message, using
          simply the size of the attachments and the body parts.
* src/modest-ui-actions.[ch]:
        * On saving to drafts or sending new mails, we estimate the size
          the message will have on device disk. If it's too big, we
          cancel the save operation, and show a banner (fixes NB#77528).
* src/maemo/modest-msg-edit-window.c:
        * Methods to estimate file size of attachments added (using vfs)
          and the full message space (offering size of attachmetns and
          images).
* src/modest-utils.c:
        * New method to get the available space on device disk.
* src/modest-mail-operation.c:
        * If there's an error in create msg, notify operation failed.

pmo-trunk-r4079

15 files changed:
src/maemo/modest-msg-edit-window.c
src/modest-mail-operation.c
src/modest-tny-msg.c
src/modest-tny-msg.h
src/modest-ui-actions.c
src/modest-ui-actions.h
src/modest-utils.c
src/modest-utils.h
src/widgets/modest-attachment-view.c
src/widgets/modest-attachment-view.h
src/widgets/modest-attachments-view.c
src/widgets/modest-attachments-view.h
src/widgets/modest-msg-edit-window.h
src/widgets/modest-window-mgr.c
src/widgets/modest-window.c

index bd6dd5f..d438ddf 100644 (file)
@@ -276,6 +276,8 @@ struct _ModestMsgEditWindowPrivate {
        gint last_cid;
        TnyList *attachments;
        TnyList *images;
+       guint64 images_size;
+       gint images_count;
 
        TnyHeaderFlags priority_flags;
        
@@ -409,6 +411,8 @@ modest_msg_edit_window_init (ModestMsgEditWindow *obj)
        priv->subject_field = NULL;
        priv->attachments   = TNY_LIST (tny_simple_list_new ());
        priv->images        = TNY_LIST (tny_simple_list_new ());
+       priv->images_size   = 0;
+       priv->images_count  = 0;
        priv->last_cid      = 0;
 
        priv->cc_caption    = NULL;
@@ -914,21 +918,28 @@ modest_msg_edit_window_finalize (GObject *obj)
 }
 
 static GdkPixbuf *
-pixbuf_from_stream (TnyStream *stream, const gchar *mime_type)
+pixbuf_from_stream (TnyStream *stream, const gchar *mime_type, guint64 *stream_size)
 {
        GdkPixbufLoader *loader;
        GdkPixbuf *pixbuf;
+       guint64 size;
+       
+       size = 0;
 
        loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, NULL);
 
-       if (loader == NULL)
+       if (loader == NULL) {
+               if (stream_size)
+                       *stream_size = 0;
                return NULL;
+       }
 
        tny_stream_reset (TNY_STREAM (stream));
        while (!tny_stream_is_eos (TNY_STREAM (stream))) {
                unsigned char read_buffer[128];
                gint readed;
                readed = tny_stream_read (TNY_STREAM (stream), (char *) read_buffer, 128);
+               size += readed;
                if (!gdk_pixbuf_loader_write (loader, read_buffer, readed, NULL))
                        break;
        }
@@ -948,6 +959,9 @@ pixbuf_from_stream (TnyStream *stream, const gchar *mime_type)
                pixbuf = new_pixbuf;
        }
 
+       if (stream_size)
+               *stream_size = size;
+
        return pixbuf;
 }
 
@@ -966,11 +980,16 @@ replace_with_images (ModestMsgEditWindow *self, TnyList *attachments)
                const gchar *cid = tny_mime_part_get_content_id (part);
                const gchar *mime_type = tny_mime_part_get_content_type (part);
                if ((cid != NULL)&&(mime_type != NULL)) {
+                       guint64 stream_size;
                        TnyStream *stream = tny_mime_part_get_stream (part);
-                       GdkPixbuf *pixbuf = pixbuf_from_stream (stream, mime_type);
+                       GdkPixbuf *pixbuf = pixbuf_from_stream (stream, mime_type, &stream_size);
+
+
                        g_object_unref (stream);
 
                        if (pixbuf != NULL) {
+                               priv->images_count ++;
+                               priv->images_size += stream_size;
                                wp_text_buffer_replace_image (WP_TEXT_BUFFER (priv->text_buffer), cid, pixbuf);
                                g_object_unref (pixbuf);
                        }
@@ -1500,6 +1519,8 @@ get_formatted_data (ModestMsgEditWindow *edit_window)
 
        wp_text_buffer_save_document (WP_TEXT_BUFFER(priv->text_buffer), get_formatted_data_cb, &string_buffer);
 
+       gtk_text_buffer_set_modified (priv->text_buffer, TRUE);
+
        return g_string_free (string_buffer, FALSE);
                                                                        
 }
@@ -1641,6 +1662,23 @@ modest_msg_edit_window_free_msg_data (ModestMsgEditWindow *edit_window,
        g_slice_free (MsgData, data);
 }
 
+void                    
+modest_msg_edit_window_get_parts_size (ModestMsgEditWindow *window,
+                                      gint *parts_count,
+                                      guint64 *parts_size)
+{
+       ModestMsgEditWindowPrivate *priv;
+
+       priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
+
+       modest_attachments_view_get_sizes (MODEST_ATTACHMENTS_VIEW (priv->attachments_view), parts_count, parts_size);
+
+       /* TODO: add images */
+       *parts_size += priv->images_size;
+       *parts_count += priv->images_count;
+
+}
+
 ModestMsgEditFormat
 modest_msg_edit_window_get_format (ModestMsgEditWindow *self)
 {
@@ -2059,6 +2097,7 @@ modest_msg_edit_window_insert_image (ModestMsgEditWindow *window)
                        gchar *content_id;
                        const gchar *mime_type = NULL;
                        GnomeVFSURI *vfs_uri;
+                       guint64 stream_size;
 
                        vfs_uri = gnome_vfs_uri_new (uri);
 
@@ -2087,9 +2126,11 @@ modest_msg_edit_window_insert_image (ModestMsgEditWindow *window)
                        tny_mime_part_set_filename (mime_part, basename);
                        g_free (basename);
 
-                       pixbuf = pixbuf_from_stream (stream, mime_type);
+                       pixbuf = pixbuf_from_stream (stream, mime_type, &stream_size);
                        
                        if (pixbuf != NULL) {
+                               priv->images_size += stream_size;
+                               priv->images_count ++;
                                insert_mark = gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (priv->text_buffer));
                                gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (priv->text_buffer), &position, insert_mark);
                                wp_text_buffer_insert_image (WP_TEXT_BUFFER (priv->text_buffer), &position, g_strdup (tny_mime_part_get_content_id (mime_part)), pixbuf);
@@ -2192,7 +2233,8 @@ modest_msg_edit_window_attach_file_one (
                
                tny_list_prepend (priv->attachments, (GObject *) mime_part);
                modest_attachments_view_add_attachment (MODEST_ATTACHMENTS_VIEW (priv->attachments_view),
-                                                       mime_part);
+                                                       mime_part,
+                                                       info.size == 0, info.size);
                gtk_widget_set_no_show_all (priv->attachments_caption, FALSE);
                gtk_widget_show_all (priv->attachments_caption);
                gtk_text_buffer_set_modified (priv->text_buffer, TRUE);
@@ -3355,7 +3397,7 @@ modest_msg_edit_window_add_part (ModestMsgEditWindow *window,
 
        g_return_if_fail (TNY_IS_MIME_PART (part));
        tny_list_prepend (priv->attachments, (GObject *) part);
-       modest_attachments_view_add_attachment (MODEST_ATTACHMENTS_VIEW (priv->attachments_view), part);
+       modest_attachments_view_add_attachment (MODEST_ATTACHMENTS_VIEW (priv->attachments_view), part, TRUE, 0);
        gtk_widget_set_no_show_all (priv->attachments_caption, FALSE);
        gtk_widget_show_all (priv->attachments_caption);
        gtk_text_buffer_set_modified (priv->text_buffer, TRUE);
index 15a6307..f7df1b8 100644 (file)
@@ -57,6 +57,9 @@
 #include "modest-marshal.h"
 #include "modest-error.h"
 #include "modest-mail-operation.h"
+#include <modest-count-stream.h>
+#include <libgnomevfs/gnome-vfs.h>
+#include "modest-utils.h"
 
 #define KB 1024
 
@@ -819,6 +822,7 @@ create_msg_thread (gpointer thread_data)
        return NULL;
 }
 
+
 void
 modest_mail_operation_create_msg (ModestMailOperation *self,
                                  const gchar *from, const gchar *to,
@@ -831,8 +835,11 @@ modest_mail_operation_create_msg (ModestMailOperation *self,
                                  ModestMailOperationCreateMsgCallback callback,
                                  gpointer userdata)
 {
+       ModestMailOperationPrivate *priv;
        CreateMsgInfo *info = NULL;
 
+       priv = MODEST_MAIL_OPERATION_GET_PRIVATE(self);
+
        info = g_slice_new0 (CreateMsgInfo);
        info->mail_op = g_object_ref (self);
 
@@ -866,6 +873,7 @@ modest_mail_operation_send_new_mail_cb (ModestMailOperation *self,
                                        TnyMsg *msg,
                                        gpointer userdata)
 {
+       ModestMailOperationPrivate *priv = NULL;
        SendNewMailInfo *info = (SendNewMailInfo *) userdata;
        TnyFolder *draft_folder = NULL;
        TnyFolder *outbox_folder = NULL;
@@ -876,6 +884,14 @@ modest_mail_operation_send_new_mail_cb (ModestMailOperation *self,
                goto end;
        }
 
+       priv = MODEST_MAIL_OPERATION_GET_PRIVATE (self);
+
+       if (priv->error) {
+               priv->status = MODEST_MAIL_OPERATION_STATUS_FAILED;
+               modest_mail_operation_notify_end (self);
+               goto end;
+       }
+
        /* Call mail operation */
        modest_mail_operation_send_mail (self, info->transport_account, msg);
 
index 0152b4b..9fa15a1 100644 (file)
@@ -852,3 +852,30 @@ get_content_type(const gchar *s)
        }
        return g_string_free(type, FALSE);
 }
+
+guint64
+modest_tny_msg_estimate_size (const gchar *plain_body, const gchar *html_body,
+                             guint64 parts_count,
+                             guint64 parts_size)
+{
+       guint64 result;
+
+       /* estimation of headers size */
+       result = 1024;
+
+       /* We add a 20% of size due to the increase in 7bit encoding */
+       if (plain_body) {
+               result += strlen (plain_body) * 120 / 100;
+       }
+       if (html_body) {
+               result += strlen (html_body) * 120 / 100;
+       }
+
+       /* 256 bytes per additional part because of their headers */
+       result += parts_count * 256;
+
+       /* 150% of increase per encoding */
+       result += parts_size * 3 / 2;
+
+       return result;
+}
index acef7e4..40ba3ea 100644 (file)
@@ -178,6 +178,19 @@ TnyMsg*       modest_tny_msg_create_reply_msg     (TnyMsg *msg,
 const gchar*  modest_tny_msg_get_parent_uid (TnyMsg *msg);
 
 
-
+/**
+ * modest_tny_msg_estimate_size:
+ * @plain_body: a string
+ * @html_body: a string
+ * @parts_number: a gint (number of additional parts)
+ * @parts_size: a guint64 (sum of size of the additional parts)
+ *
+ * Estimates the size of the resulting message obtained from the size of the body
+ * parts, and adding the estimation of size headers.
+ */
+guint64
+modest_tny_msg_estimate_size (const gchar *plain_body, const gchar *html_body,
+                             guint64 parts_count,
+                             guint64 parts_size);
 
 #endif /* __MODEST_TNY_MSG_H__ */
index 110a26f..71fbd0b 100644 (file)
@@ -2285,7 +2285,7 @@ on_save_to_drafts_cb (ModestMailOperation *mail_op,
        g_object_unref(edit_window);
 }
 
-void
+gboolean
 modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edit_window)
 {
        TnyTransportAccount *transport_account;
@@ -2294,11 +2294,30 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
        gchar *account_name, *from;
        ModestAccountMgr *account_mgr;
        char *info_text;
+       gboolean had_error = FALSE;
+       guint64 available_disk, expected_size;
+       gint parts_count;
+       guint64 parts_size;
 
-       g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW(edit_window));
+       g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW(edit_window), FALSE);
        
        data = modest_msg_edit_window_get_msg_data (edit_window);
 
+       /* Check size */
+       available_disk = modest_folder_available_space (NULL);
+       modest_msg_edit_window_get_parts_size (edit_window, &parts_count, &parts_size);
+       expected_size = modest_tny_msg_estimate_size (data->plain_body,
+                                                data->html_body,
+                                                parts_count,
+                                                parts_size);
+
+       if ((available_disk != -1) && expected_size > available_disk) {
+               modest_msg_edit_window_free_msg_data (edit_window, data);
+
+               modest_platform_information_banner (NULL, NULL, dgettext("ke-recv", "cerm_device_memory_full"));
+               return FALSE;
+       }
+
        account_name = g_strdup (data->account_name);
        account_mgr = modest_runtime_get_account_mgr();
        if (!account_name)
@@ -2308,7 +2327,7 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
        if (!account_name) {
                g_printerr ("modest: no account found\n");
                modest_msg_edit_window_free_msg_data (edit_window, data);
-               return;
+               return FALSE;
        }
 
        if (!strcmp (account_name, MODEST_LOCAL_FOLDERS_ACCOUNT_ID)) {
@@ -2324,7 +2343,7 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
                g_printerr ("modest: no transport account found for '%s'\n", account_name);
                g_free (account_name);
                modest_msg_edit_window_free_msg_data (edit_window, data);
-               return;
+               return FALSE;
        }
        from = modest_account_mgr_get_from_string (account_mgr, account_name);
 
@@ -2347,18 +2366,19 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
                                              data->priority_flags,
                                              on_save_to_drafts_cb,
                                              g_object_ref(edit_window));
+
        info_text = g_strdup_printf (_("mail_va_saved_to_drafts"), _("mcen_me_folder_drafts"));
        modest_platform_information_banner (NULL, NULL, info_text);
+       g_free (info_text);
+       modest_msg_edit_window_reset_modified (edit_window);
 
        /* Frees */
-       g_free (info_text);
        g_free (from);
        g_free (account_name);
        g_object_unref (G_OBJECT (transport_account));
        g_object_unref (G_OBJECT (mail_operation));
 
        modest_msg_edit_window_free_msg_data (edit_window, data);
-       modest_msg_edit_window_reset_modified (edit_window);
 
        /* ** FIXME **
         * If the drafts folder is selected then make the header view
@@ -2377,7 +2397,7 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
         */
        ModestMainWindow *win = MODEST_MAIN_WINDOW(modest_window_mgr_get_main_window(
                modest_runtime_get_window_mgr(), FALSE));
-       if (win != NULL) {
+       if (!had_error && win != NULL) {
                ModestFolderView *view = MODEST_FOLDER_VIEW(modest_main_window_get_child_widget(
                        win, MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW));
                if (view != NULL) {
@@ -2396,24 +2416,45 @@ modest_ui_actions_on_save_to_drafts (GtkWidget *widget, ModestMsgEditWindow *edi
                        if (folder != NULL) g_object_unref(folder);
                }
        }
+
+       return !had_error;
 }
 
 /* For instance, when clicking the Send toolbar button when editing a message: */
-void
+gboolean
 modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window)
 {
        TnyTransportAccount *transport_account = NULL;
+       gboolean had_error = FALSE;
+       guint64 available_disk, expected_size;
+       gint parts_count;
+       guint64 parts_size;
 
-       g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW(edit_window));
+       g_return_val_if_fail (MODEST_IS_MSG_EDIT_WINDOW(edit_window), TRUE);
 
        if (!modest_msg_edit_window_check_names (edit_window, TRUE))
-               return;
+               return TRUE;
        
        /* FIXME: Code added just for testing. The final version will
           use the send queue provided by tinymail and some
           classifier */
        MsgData *data = modest_msg_edit_window_get_msg_data (edit_window);
 
+       /* Check size */
+       available_disk = modest_folder_available_space (NULL);
+       modest_msg_edit_window_get_parts_size (edit_window, &parts_count, &parts_size);
+       expected_size = modest_tny_msg_estimate_size (data->plain_body,
+                                                data->html_body,
+                                                parts_count,
+                                                parts_size);
+
+       if ((available_disk != -1) && expected_size > available_disk) {
+               modest_msg_edit_window_free_msg_data (edit_window, data);
+
+               modest_platform_information_banner (NULL, NULL, dgettext("ke-recv", "cerm_device_memory_full"));
+               return FALSE;
+       }
+
        ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr();
        gchar *account_name = g_strdup (data->account_name);
        if (!account_name)
@@ -2426,7 +2467,7 @@ modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window)
                modest_msg_edit_window_free_msg_data (edit_window, data);
                /* Run account setup wizard */
                if (!modest_ui_actions_run_account_setup_wizard (MODEST_WINDOW(edit_window))) {
-                       return;
+                       return TRUE;
                }
        }
        
@@ -2441,7 +2482,7 @@ modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window)
                modest_msg_edit_window_free_msg_data (edit_window, data);
                /* Run account setup wizard */
                if (!modest_ui_actions_run_account_setup_wizard(MODEST_WINDOW(edit_window)))
-                       return;
+                       return TRUE;
        }
        
        gchar *from = modest_account_mgr_get_from_string (account_mgr, account_name);
@@ -2467,6 +2508,15 @@ modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window)
        if (modest_mail_operation_get_status (mail_operation) == MODEST_MAIL_OPERATION_STATUS_IN_PROGRESS)
                modest_platform_information_banner (NULL, NULL, _("mcen_ib_outbox_waiting_to_be_sent"));
 
+
+       if (modest_mail_operation_get_error (mail_operation) != NULL) {
+               const GError *error = modest_mail_operation_get_error (mail_operation);
+               if (error->code == MODEST_MAIL_OPERATION_ERROR_INSTANCE_CREATION_FAILED) {
+                       g_warning ("%s failed: %s\n", __FUNCTION__, (modest_mail_operation_get_error (mail_operation))->message);
+                       modest_platform_information_banner (NULL, NULL, _CS("sfil_ni_not_enough_memory"));
+                       had_error = TRUE;
+               }
+       }
                                             
        /* Free data: */
        g_free (from);
@@ -2475,10 +2525,15 @@ modest_ui_actions_on_send (GtkWidget *widget, ModestMsgEditWindow *edit_window)
        g_object_unref (G_OBJECT (mail_operation));
 
        modest_msg_edit_window_free_msg_data (edit_window, data);
-       modest_msg_edit_window_set_sent (edit_window, TRUE);
 
-       /* Save settings and close the window: */
-       modest_ui_actions_on_close_window (NULL, MODEST_WINDOW (edit_window));
+       if (!had_error) {
+               modest_msg_edit_window_set_sent (edit_window, TRUE);
+
+               /* Save settings and close the window: */
+               modest_ui_actions_on_close_window (NULL, MODEST_WINDOW (edit_window));
+       }
+
+       return !had_error;
 }
 
 void 
index 1071ef9..8404866 100644 (file)
@@ -154,9 +154,9 @@ void     modest_ui_actions_on_msg_attachment_clicked   (ModestMsgView *msgview,
 void     modest_ui_actions_on_msg_recpt_activated   (ModestMsgView *msgview, const gchar *address,
                                                     ModestWindow *win);
 
-void     modest_ui_actions_on_send                     (GtkWidget *widget,
+gboolean modest_ui_actions_on_send                     (GtkWidget *widget,
                                                        ModestMsgEditWindow *edit_window);
-void     modest_ui_actions_on_save_to_drafts           (GtkWidget *widget, 
+gboolean modest_ui_actions_on_save_to_drafts           (GtkWidget *widget, 
                                                        ModestMsgEditWindow *edit_window);
 
 
index 0128966..e181782 100644 (file)
@@ -43,6 +43,7 @@
 #include <modest-defs.h>
 #include "modest-utils.h"
 #include "modest-platform.h"
+#include <modest-local-folder-info.h>
 
 GQuark
 modest_utils_get_supported_secure_authentication_error_quark (void)
@@ -462,3 +463,28 @@ modest_list_index (TnyList *list, GObject *object)
        g_object_unref (iter);
        return index;
 }
+
+guint64 
+modest_folder_available_space (const gchar *maildir_path)
+{
+       gchar *folder;
+       gchar *uri_string;
+       GnomeVFSURI *uri;
+       GnomeVFSFileSize size;
+
+       folder = modest_local_folder_info_get_maildir_path (maildir_path);
+       uri_string = gnome_vfs_get_uri_from_local_path (folder);
+       uri = gnome_vfs_uri_new (uri_string);
+       g_free (folder);
+       g_free (uri_string);
+
+       if (uri) {
+               if (gnome_vfs_get_volume_free_space (uri, &size) != GNOME_VFS_OK)
+                       size = -1;
+               gnome_vfs_uri_unref (uri);
+       } else {
+               size = -1;
+       }
+
+       return (guint64) size;
+}
index a47b0d0..64845ba 100644 (file)
@@ -135,4 +135,15 @@ void modest_utils_toggle_action_set_active_block_notify (GtkToggleAction *action
  */
 gint modest_list_index (TnyList *list, GObject *object);
 
+/**
+ * modest_folder_available_space:
+ * @maildir_path: the path of the maildir folder, or %NULL to
+ * get the space available in local folders
+ *
+ * Obtains the space available in the local folder.
+ *
+ * Returns: a #guint64
+ */
+guint64 modest_folder_available_space (const gchar *maildir_path);
+
 #endif /*__MODEST_MAEMO_UTILS_H__*/
index 0a2e7da..b36e7fc 100644 (file)
@@ -58,7 +58,7 @@ struct _ModestAttachmentViewPrivate
        GtkWidget *filename_view;
        GtkWidget *size_view;
 
-       guint get_size_idle_id;
+       gboolean detect_size;
        TnyStream *get_size_stream;
        guint64 size;
 
@@ -93,22 +93,27 @@ static void tny_mime_part_view_init (gpointer g, gpointer iface_data);
 
 static void update_filename_request (ModestAttachmentView *self);
 
+static void update_size_label (ModestAttachmentView *self)
+{
+       ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
+       gchar *size_str;
+       gchar *label_text;
+
+       size_str = modest_text_utils_get_display_size (priv->size);
+       label_text = g_strdup_printf (" (%s)", size_str);
+       g_free (size_str);
+       gtk_label_set_text (GTK_LABEL (priv->size_view), label_text);
+       g_free (label_text);
+}
 
 static gboolean
 idle_get_mime_part_size_cb (gpointer userdata)
 {
        ModestAttachmentView *view = (ModestAttachmentView *) userdata;
-       ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (view);
-       gchar *size_str;
-       gchar *label_text;
        gdk_threads_enter ();
 
        if (GTK_WIDGET_VISIBLE (view)) {
-               size_str = modest_text_utils_get_display_size (priv->size);
-               label_text = g_strdup_printf (" (%s)", size_str);
-               g_free (size_str);
-               gtk_label_set_text (GTK_LABEL (priv->size_view), label_text);
-               g_free (label_text);
+               update_size_label (view);
        }
 
        gdk_threads_leave ();
@@ -150,6 +155,42 @@ get_mime_part_size_thread (gpointer thr_user_data)
        return NULL;
 }
 
+void
+modest_attachment_view_set_detect_size (ModestAttachmentView *self, gboolean detect_size)
+{
+       ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
+
+       priv->detect_size = detect_size;
+       
+}
+
+void
+modest_attachment_view_set_size (ModestAttachmentView *self, guint64 size)
+{
+       ModestAttachmentViewPrivate *priv;
+
+       g_return_if_fail (MODEST_IS_ATTACHMENT_VIEW (self));
+       priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
+
+       if (!priv->detect_size) {
+               priv->size = size;
+               update_size_label (self);
+       } else {
+               g_assert ("Shouldn't set the size of the attachment view if detect size is enabled");
+       }
+}
+
+guint64
+modest_attachment_view_get_size (ModestAttachmentView *self)
+{
+       ModestAttachmentViewPrivate *priv;
+
+       g_return_val_if_fail (MODEST_IS_ATTACHMENT_VIEW (self), 0);
+       priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (self);
+
+       return priv->size;
+}
+
 static TnyMimePart *
 modest_attachment_view_get_part (TnyMimePartView *self)
 {
@@ -254,7 +295,7 @@ modest_attachment_view_set_part_default (TnyMimePartView *self, TnyMimePart *mim
 
        gtk_label_set_text (GTK_LABEL (priv->size_view), "");
 
-       if (show_size) {
+       if (show_size && priv->detect_size) {
                tny_camel_mem_stream_get_type ();
                g_object_ref (self);
                g_thread_create (get_mime_part_size_thread, self, FALSE, NULL);
@@ -280,11 +321,6 @@ modest_attachment_view_clear_default (TnyMimePartView *self)
                priv->mime_part = NULL;
        }
 
-       if (priv->get_size_idle_id != 0) {
-               g_source_remove (priv->get_size_idle_id);
-               priv->get_size_idle_id = 0;
-       }
-
        if (priv->get_size_stream != NULL) {
                g_object_unref (priv->get_size_stream);
                priv->get_size_stream = NULL;
@@ -312,11 +348,13 @@ modest_attachment_view_clear_default (TnyMimePartView *self)
  * Return value: a new #ModestAttachmentView instance implemented for Gtk+
  **/
 GtkWidget*
-modest_attachment_view_new (TnyMimePart *mime_part)
+modest_attachment_view_new (TnyMimePart *mime_part, gboolean detect_size)
 {
        ModestAttachmentView *self = g_object_new (MODEST_TYPE_ATTACHMENT_VIEW, 
                                                   NULL);
 
+       modest_attachment_view_set_detect_size (self, detect_size);
+
        modest_attachment_view_set_part (TNY_MIME_PART_VIEW (self), mime_part);
 
        return GTK_WIDGET (self);
@@ -342,9 +380,9 @@ modest_attachment_view_instance_init (GTypeInstance *instance, gpointer g_class)
        gtk_misc_set_alignment (GTK_MISC (priv->size_view), 0.0, 0.5);
        gtk_misc_set_alignment (GTK_MISC (priv->filename_view), 0.0, 0.5);
 
-       priv->get_size_idle_id = 0;
        priv->get_size_stream = NULL;
        priv->size = 0;
+       priv->detect_size = TRUE;
 
        box = gtk_hbox_new (FALSE, 0);
        gtk_box_pack_start (GTK_BOX (box), priv->icon, FALSE, FALSE, 0);
@@ -374,11 +412,6 @@ modest_attachment_view_finalize (GObject *object)
 {
        ModestAttachmentViewPrivate *priv = MODEST_ATTACHMENT_VIEW_GET_PRIVATE (object);
 
-       if (priv->get_size_idle_id) {
-               g_source_remove (priv->get_size_idle_id);
-               priv->get_size_idle_id = 0;
-       }
-
        if (priv->get_size_stream != NULL) {
                g_object_unref (priv->get_size_stream);
                priv->get_size_stream = NULL;
index cd5a4a8..e910296 100644 (file)
@@ -64,7 +64,12 @@ struct _ModestAttachmentViewClass
 
 GType modest_attachment_view_get_type (void);
 
-GtkWidget* modest_attachment_view_new (TnyMimePart *mime_part);
+GtkWidget* modest_attachment_view_new (TnyMimePart *mime_part, gboolean detect_size);
+void modest_attachment_view_set_detect_size (ModestAttachmentView *self, gboolean detect_size);
+void modest_attachment_view_set_size (ModestAttachmentView *self, guint64 size);
+guint64 modest_attachment_view_get_size (ModestAttachmentView *self);
+
+
 
 G_END_DECLS
 
index a56ceba..557fc88 100644 (file)
@@ -144,7 +144,7 @@ modest_attachments_view_set_message (ModestAttachmentsView *attachments_view, Tn
                part = TNY_MIME_PART (tny_iterator_get_current (iter));
 
                if (part && (modest_tny_mime_part_is_attachment_for_modest (part))) 
-                       modest_attachments_view_add_attachment (attachments_view, part);
+                       modest_attachments_view_add_attachment (attachments_view, part, TRUE, 0);
 
                if (part)
                        g_object_unref (part);
@@ -160,7 +160,8 @@ modest_attachments_view_set_message (ModestAttachmentsView *attachments_view, Tn
 }
 
 void
-modest_attachments_view_add_attachment (ModestAttachmentsView *attachments_view, TnyMimePart *part)
+modest_attachments_view_add_attachment (ModestAttachmentsView *attachments_view, TnyMimePart *part,
+                                       gboolean detect_size, guint64 size)
 {
        GtkWidget *att_view = NULL;
        ModestAttachmentsViewPrivate *priv = NULL;
@@ -170,7 +171,9 @@ modest_attachments_view_add_attachment (ModestAttachmentsView *attachments_view,
 
        priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
 
-       att_view = modest_attachment_view_new (part);
+       att_view = modest_attachment_view_new (part, detect_size);
+       if (!detect_size)
+               modest_attachment_view_set_size (MODEST_ATTACHMENT_VIEW (att_view), size);
        gtk_box_pack_end (GTK_BOX (priv->box), att_view, FALSE, FALSE, 0);
        gtk_widget_show_all (att_view);
 }
@@ -775,6 +778,43 @@ modest_attachments_view_has_attachments (ModestAttachmentsView *atts_view)
        return result;
 }
 
+void
+modest_attachments_view_get_sizes (ModestAttachmentsView *attachments_view,
+                                  gint *attachments_count,
+                                  guint64 *attachments_size)
+{
+       ModestAttachmentsViewPrivate *priv;
+       GList *children, *node;
+
+       g_return_if_fail (MODEST_IS_ATTACHMENTS_VIEW (attachments_view));
+       g_return_if_fail (attachments_count != NULL && attachments_size != NULL);
+
+       *attachments_count = 0;
+       *attachments_size = 0;
+
+       priv = MODEST_ATTACHMENTS_VIEW_GET_PRIVATE (attachments_view);
+
+       children = gtk_container_get_children (GTK_CONTAINER (priv->box));
+       for (node = children; node != NULL; node = g_list_next (node)) {
+               GtkWidget *att_view = (GtkWidget *) node->data;
+               TnyMimePart *part = tny_mime_part_view_get_part (TNY_MIME_PART_VIEW (att_view));
+
+               if (!tny_mime_part_is_purged (part)) {
+                       guint64 size;
+                       *attachments_count ++;
+                       size = modest_attachment_view_get_size (MODEST_ATTACHMENT_VIEW (att_view));
+                       if (size == 0) {
+                               /* we do a random estimation of the size of an attachment */
+                               size = 32768;
+                       }
+                       *attachments_size += size;
+                       
+               }
+               g_object_unref (part);
+       }
+       g_list_free (children);
+}
+
 static void
 own_clipboard (ModestAttachmentsView *atts_view)
 {
index 89837ad..2bad760 100644 (file)
@@ -67,7 +67,8 @@ GType modest_attachments_view_get_type (void);
 GtkWidget* modest_attachments_view_new (TnyMsg *msg);
 
 void modest_attachments_view_set_message (ModestAttachmentsView *attachments_view, TnyMsg *msg);
-void modest_attachments_view_add_attachment (ModestAttachmentsView *attachments_view, TnyMimePart *part);
+void modest_attachments_view_add_attachment (ModestAttachmentsView *attachments_view, TnyMimePart *part,
+                                            gboolean detect_size, guint64 size);
 void modest_attachments_view_remove_attachment (ModestAttachmentsView *attachments_view, TnyMimePart *part);
 void modest_attachments_view_remove_attachment_by_id (ModestAttachmentsView *attachments_view, const gchar *att_id);
 TnyList *modest_attachments_view_get_attachments (ModestAttachmentsView *attachments_view);
@@ -76,6 +77,10 @@ void modest_attachments_view_select_all (ModestAttachmentsView *attachments_view
 
 gboolean modest_attachments_view_has_attachments (ModestAttachmentsView *attachments_view);
 
+void modest_attachments_view_get_sizes (ModestAttachmentsView *attachments_view,
+                                       gint *attachments_count,
+                                       guint64 *attachments_size);
+
 G_END_DECLS
 
 #endif
index c4af36c..dad2dca 100644 (file)
@@ -245,6 +245,15 @@ void                    modest_msg_edit_window_remove_attachments    (ModestMsgE
                                                                      TnyList *att_list);
 
 /**
+ * modest_msg_edit_window_get_parts_size:
+ * @window: a #ModestMsgEditWindow
+ * @parts_count: number of attachments and images attached to the message
+ * @parts_size: sum of sizes of attachments and images
+ */
+void                    modest_msg_edit_window_get_parts_size (ModestMsgEditWindow *window,
+                                                              gint *parts_count,
+                                                              guint64 *parts_size);
+/**
  * modest_msg_edit_window_add_part:
  * @self: a #ModestMsgEditWindow
  * @part: a #TnyMimePart
index 5cbae89..0b0469b 100644 (file)
@@ -712,7 +712,8 @@ on_window_destroy (ModestWindow *window,
                                                                                 _("mcen_nc_no_email_message_modified_save_changes"));
                                /* Save to drafts */
                                if (response != GTK_RESPONSE_CANCEL)
-                                       modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window));                            
+                                       if (!modest_ui_actions_on_save_to_drafts (NULL, MODEST_MSG_EDIT_WINDOW (window)))
+                                               return TRUE;
                        }
                }
        }
index c0e7830..b45e13a 100644 (file)
@@ -424,7 +424,7 @@ on_key_pressed (GtkWidget *self,
        case GDK_Escape: 
                if (modest_window_mgr_get_fullscreen_mode (mgr))
                        modest_ui_actions_on_change_fullscreen (NULL, MODEST_WINDOW(self));
-               else if (MODEST_IS_MSG_VIEW_WINDOW (self)||MODEST_IS_MSG_EDIT_WINDOW (self))
+               else if (MODEST_IS_MSG_VIEW_WINDOW (self))
                        modest_ui_actions_on_close_window (NULL, MODEST_WINDOW (self));
                break;
        }