* src/maemo/modest-progress-bar-widget.c:
[modest] / src / maemo / modest-progress-bar-widget.c
index b0e8078..638ab31 100644 (file)
@@ -32,6 +32,8 @@
 #include <widgets/modest-combo-box.h>
 #include "modest-progress-bar-widget.h"
 #include <string.h>
+#include "modest-platform.h"
+#include "modest-runtime.h"
 
 /* 'private'/'protected' functions */
 static void modest_progress_bar_widget_class_init (ModestProgressBarWidgetClass *klass);
@@ -44,11 +46,11 @@ static void modest_progress_bar_add_operation    (ModestProgressObject *self,
 static void modest_progress_bar_remove_operation (ModestProgressObject *self,
                                                    ModestMailOperation  *mail_op);
 
-static void 
-modest_progress_bar_cancel_current_operation (ModestProgressObject *self);
+static void modest_progress_bar_cancel_current_operation (ModestProgressObject *self);
 
-static guint
-modest_progress_bar_num_pending_operations (ModestProgressObject *self);
+static void modest_progress_bar_cancel_all_operations    (ModestProgressObject *self);
+
+static guint modest_progress_bar_num_pending_operations (ModestProgressObject *self);
 
 static void on_progress_changed                    (ModestMailOperation  *mail_op, 
                                                    ModestMailOperationState *state,
@@ -100,6 +102,7 @@ modest_progress_object_init (gpointer g, gpointer iface_data)
        klass->add_operation_func = modest_progress_bar_add_operation;
        klass->remove_operation_func = modest_progress_bar_remove_operation;
        klass->cancel_current_operation_func = modest_progress_bar_cancel_current_operation;
+       klass->cancel_all_operations_func = modest_progress_bar_cancel_all_operations;
        klass->num_pending_operations_func = modest_progress_bar_num_pending_operations;
 }
 
@@ -171,7 +174,7 @@ modest_progress_bar_widget_init (ModestProgressBarWidget *self)
        GtkAdjustment *adj;
 
        priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE(self);
-       
+
        /* Alignment */
        align = gtk_alignment_new(XALIGN, YALIGN, XSPACE, YSPACE);
 
@@ -184,7 +187,8 @@ modest_progress_bar_widget_init (ModestProgressBarWidget *self)
        gtk_progress_bar_set_ellipsize (GTK_PROGRESS_BAR (priv->progress_bar), PANGO_ELLIPSIZE_END);
        gtk_widget_size_request (priv->progress_bar, &req);
        gtk_container_add (GTK_CONTAINER (align), priv->progress_bar);
-       
+       gtk_widget_size_request (align, &req);
+
        /* Add progress bar widget */   
        gtk_box_pack_start (GTK_BOX(self), align, TRUE, TRUE, 0);
        gtk_widget_show_all (GTK_WIDGET(self));       
@@ -217,9 +221,10 @@ static void
 modest_progress_bar_add_operation (ModestProgressObject *self,
                                   ModestMailOperation  *mail_op)
 {
-       ModestProgressBarWidget *me;
-       ObservableData *data;
-       ModestProgressBarWidgetPrivate *priv;
+       ModestProgressBarWidget *me = NULL;
+       ObservableData *data = NULL;
+       ModestProgressBarWidgetPrivate *priv = NULL;
+       ModestMailOperationState *state = NULL;
        
        me = MODEST_PROGRESS_BAR_WIDGET (self);
        priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (me);
@@ -230,14 +235,21 @@ modest_progress_bar_add_operation (ModestProgressObject *self,
                                                 "progress-changed",
                                                 G_CALLBACK (on_progress_changed),
                                                 me);
-
-       if (priv->observables == NULL) {
+       /* Set curent operation */
+       if (priv->current == NULL) {
                priv->current = mail_op;
+
+               /* Call progress_change handler to initialize progress message */
+               state = g_malloc0(sizeof(ModestMailOperationState));
+               state->done = 0;
+               state->total = 0;
+               state->op_type = modest_mail_operation_get_type_operation (mail_op);
+               on_progress_changed (mail_op, state, me);
+               g_free(state);
        }
-       priv->observables = g_slist_append (priv->observables, data);
 
-       /* Call progress_change handler to initialize progress message */
-/*     on_progress_changed (mail_op, me); */
+       /* Add operation to obserbable objects list */
+       priv->observables = g_slist_prepend (priv->observables, data);
 }
 
 static gint
@@ -251,19 +263,22 @@ compare_observable_data (ObservableData *data1, ObservableData *data2)
 
 static void 
 modest_progress_bar_remove_operation (ModestProgressObject *self,
-                                       ModestMailOperation  *mail_op)
+                                     ModestMailOperation  *mail_op)
 {
        ModestProgressBarWidget *me;
        ModestProgressBarWidgetPrivate *priv;
        GSList *link;
        ObservableData *tmp_data = NULL;
+       gboolean is_current;
 
        me = MODEST_PROGRESS_BAR_WIDGET (self);
        priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (me);
 
+       is_current = (priv->current == mail_op);
+
        /* Find item */
        tmp_data = g_malloc0 (sizeof (ObservableData));
-        tmp_data->mail_op = mail_op;  
+        tmp_data->mail_op = mail_op;
        link = g_slist_find_custom (priv->observables,
                                    tmp_data,
                                    (GCompareFunc) compare_observable_data);
@@ -280,7 +295,7 @@ modest_progress_bar_remove_operation (ModestProgressObject *self,
        }
        
        /* Update the current mail operation */
-       if (priv->current == mail_op) {
+       if (is_current) {
                if (priv->observables)
                        priv->current = ((ObservableData *) priv->observables->data)->mail_op;
                else
@@ -316,9 +331,35 @@ modest_progress_bar_cancel_current_operation (ModestProgressObject *self)
        priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (me);
 
        if (priv->current == NULL) return;
+
+       /* If received canceled we shall show banner */
+       if (modest_mail_operation_get_type_operation (priv->current) ==
+           MODEST_MAIL_OPERATION_TYPE_RECEIVE)
+               modest_platform_information_banner (NULL, NULL, 
+                                                   _("emev_ib_ui_pop3_msg_recv_cancel"));
+
        modest_mail_operation_cancel (priv->current);
+}
+
+static void 
+modest_progress_bar_cancel_all_operations (ModestProgressObject *self)
+{
+       ModestProgressBarWidget *me;
+       ModestProgressBarWidgetPrivate *priv;
 
+       me = MODEST_PROGRESS_BAR_WIDGET (self);
+       priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (me);
+
+       /* If received canceled we shall show banner */
+       if (priv->current && modest_mail_operation_get_type_operation (priv->current) ==
+           MODEST_MAIL_OPERATION_TYPE_RECEIVE)
+               modest_platform_information_banner (NULL, NULL, 
+                                                   _("emev_ib_ui_pop3_msg_recv_cancel"));
+
+       /* Cancel all the mail operations */
+       modest_mail_operation_queue_cancel_all (modest_runtime_get_mail_operation_queue ());
 }
+
 static void 
 on_progress_changed (ModestMailOperation  *mail_op, 
                     ModestMailOperationState *state,
@@ -333,22 +374,22 @@ on_progress_changed (ModestMailOperation  *mail_op,
        if (priv->current == mail_op) {
                gchar *msg = NULL;
                
-               determined = (state->done > 0 && state->total > 0) & 
+               determined = (state->done > 0 && state->total > 1) && 
                        !(state->done == 1 && state->total == 100);
 
                switch (state->op_type) {
                case MODEST_MAIL_OPERATION_TYPE_RECEIVE:                
                        if (determined)
-/*                             msg = g_strdup_printf(_("mcen_me_receiving"), done, total); */
-                               msg = g_strdup_printf("Receiving %d/%d", state->done, state->total);
+                               msg = g_strdup_printf(_("mcen_me_receiving"),
+                                                     state->done, state->total); 
                        else 
-/*                             msg = g_strdup(_("mail_me_receiving")); */
-                               msg = g_strdup("Receiving ...");
+                               msg = g_strdup(_("mail_me_receiving"));
                        break;
                case MODEST_MAIL_OPERATION_TYPE_SEND:           
                        if (determined)
-                               msg = g_strdup_printf(_("mcen_me_sending"), state->done, state->total);
-                       else 
+                               msg = g_strdup_printf(_("mcen_me_sending"), state->done,
+                                                     state->total);
+                       else
                                msg = g_strdup(_("mail_me_sending"));
                        break;
                        
@@ -359,7 +400,15 @@ on_progress_changed (ModestMailOperation  *mail_op,
                        msg = g_strdup("");
                }
                
-               modest_progress_bar_widget_set_progress (self, msg, state->done, state->total);
+               /* If we have byte information use it */
+               if ((state->bytes_done != 0) && (state->bytes_total != 0))
+                       modest_progress_bar_widget_set_progress (self, msg, 
+                                                                state->bytes_done, 
+                                                                state->bytes_total);
+               else
+                       modest_progress_bar_widget_set_progress (self, msg,
+                                                                state->done,
+                                                                state->total);
                g_free (msg);
        }
 }
@@ -381,10 +430,10 @@ modest_progress_bar_widget_new ()
 
 
 void 
-modest_progress_bar_widget_set_progress   (ModestProgressBarWidget *self,
-                                          const gchar *message,
-                                          guint done,
-                                          guint total)
+modest_progress_bar_widget_set_progress (ModestProgressBarWidget *self,
+                                        const gchar *message,
+                                        gint done,
+                                        gint total)
 {
        ModestProgressBarWidgetPrivate *priv;
        
@@ -392,13 +441,22 @@ modest_progress_bar_widget_set_progress   (ModestProgressBarWidget *self,
        g_return_if_fail (done <= total);
        
        priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (self);
-       
-       /* Set progress */
-       if (total != 0)
-               gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (priv->progress_bar),
-                                              (gdouble)done/(gdouble)total);
-       else
+
+       /* Set progress. Tinymail sometimes returns us 1/100 when it
+          does not have any clue, NOTE that 1/100 could be also a
+          valid progress (we will loose it), but it will be recovered
+          once the done is greater than 1 */
+       if ((done == 0 && total == 0) || 
+           (done == 1 && total == 100)) {
                gtk_progress_bar_pulse (GTK_PROGRESS_BAR (priv->progress_bar));
+       } else {
+               gdouble percent = 0;
+               if (total != 0) /* Avoid division by zero. */
+                       percent = (gdouble)done/(gdouble)total;
+
+               gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (priv->progress_bar),
+                                                      percent);
+       }
 
        /* Set text */
        gtk_progress_bar_set_text (GTK_PROGRESS_BAR (priv->progress_bar), message);