* fix small logic error
[modest] / src / maemo / modest-progress-bar-widget.c
index b699351..8ab228d 100644 (file)
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <string.h>
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
 #include <widgets/modest-combo-box.h>
 #include "modest-progress-bar-widget.h"
-#include <string.h>
+#include "modest-tny-account.h"
 #include "modest-platform.h"
 #include "modest-runtime.h"
 
@@ -58,6 +59,12 @@ static void on_progress_changed                    (ModestMailOperation  *mail_o
 
 static gboolean     progressbar_clean        (GtkProgressBar *bar);
 
+static gboolean modest_progress_bar_widget_is_pulsating (ModestProgressBarWidget *self);
+
+static void modest_progress_bar_widget_set_pulsating_mode (ModestProgressBarWidget *self,
+                                                           const gchar* msg,
+                                                           gboolean is_pulsating);
+
 #define XALIGN 0.5
 #define YALIGN 0.5
 #define XSPACE 1
@@ -66,6 +73,8 @@ static gboolean     progressbar_clean        (GtkProgressBar *bar);
 #define LOWER 0
 #define UPPER 150
 
+#define MODEST_PROGRESS_BAR_PULSE_INTERVAL 125
+
 /* list my signals  */
 /* enum { */
 /*     LAST_SIGNAL */
@@ -79,11 +88,11 @@ struct _ObservableData {
 
 typedef struct _ModestProgressBarWidgetPrivate ModestProgressBarWidgetPrivate;
 struct _ModestProgressBarWidgetPrivate {
-        GSList              *observables;
-        ModestMailOperation *current;
-
+       GSList              *observables;
+       ModestMailOperation *current;
        guint count;
        GtkWidget *progress_bar;
+       guint pulsating_timeout;
 };
 #define MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
                                                  MODEST_TYPE_PROGRESS_BAR_WIDGET, \
@@ -153,16 +162,6 @@ modest_progress_bar_widget_class_init (ModestProgressBarWidgetClass *klass)
        gobject_class->finalize = modest_progress_bar_widget_finalize;
 
        g_type_class_add_private (gobject_class, sizeof(ModestProgressBarWidgetPrivate));
-
-       /* signal definitions go here, e.g.: */
-/*     signals[DATA_CHANGED_SIGNAL] = */
-/*             g_signal_new ("data_changed", */
-/*                           G_TYPE_FROM_CLASS (klass), */
-/*                           G_SIGNAL_RUN_FIRST, */
-/*                           G_STRUCT_OFFSET(ModestProgressBarWidgetClass, data_changed), */
-/*                           NULL, NULL, */
-/*                           g_cclosure_marshal_VOID__VOID, */
-/*                           G_TYPE_NONE, 0); */
 }
 
 static void
@@ -186,7 +185,8 @@ modest_progress_bar_widget_init (ModestProgressBarWidget *self)
        req.height = 64;
        gtk_progress_set_text_alignment (GTK_PROGRESS (priv->progress_bar), 0, 0.5);
        gtk_progress_bar_set_ellipsize (GTK_PROGRESS_BAR (priv->progress_bar), PANGO_ELLIPSIZE_END);
-       gtk_progress_bar_set_pulse_step (GTK_PROGRESS_BAR (priv->progress_bar), 0.05);
+       gtk_progress_bar_set_pulse_step (GTK_PROGRESS_BAR (priv->progress_bar), 0.25);
+       gtk_progress_bar_set_text (GTK_PROGRESS_BAR (priv->progress_bar), " ");
        gtk_widget_size_request (priv->progress_bar, &req);
        gtk_container_add (GTK_CONTAINER (align), priv->progress_bar);
        gtk_widget_size_request (align, &req);
@@ -194,6 +194,8 @@ modest_progress_bar_widget_init (ModestProgressBarWidget *self)
        /* Add progress bar widget */   
        gtk_box_pack_start (GTK_BOX(self), align, TRUE, TRUE, 0);
        gtk_widget_show_all (GTK_WIDGET(self));       
+       
+       priv->pulsating_timeout = 0;
 }
 
 static void
@@ -214,6 +216,10 @@ modest_progress_bar_widget_finalize (GObject *obj)
                g_slist_free (priv->observables);
                priv->observables = NULL;
        }
+       
+       /* remove timeout */
+       if (priv->pulsating_timeout != 0)
+               g_source_remove (priv->pulsating_timeout);
 
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
@@ -237,14 +243,14 @@ modest_progress_bar_add_operation (ModestProgressObject *self,
                                                 G_CALLBACK (on_progress_changed),
                                                 me);
        /* Set curent operation */
-/*     if (priv->current == NULL) { */
-       priv->current = mail_op;
-       
-       priv->count = 0;
-       
-       /* Call progress_change handler to initialize progress message */
-       modest_progress_bar_widget_set_undetermined_progress (MODEST_PROGRESS_BAR_WIDGET(self), mail_op);
-/*     } */
+       if (priv->current == NULL) {
+               priv->current = mail_op;
+               
+               priv->count = 0;
+               
+               /* Call progress_change handler to initialize progress message */
+/*             modest_progress_bar_widget_set_undetermined_progress (MODEST_PROGRESS_BAR_WIDGET(self), mail_op); */
+       }
 
        /* Add operation to obserbable objects list */
        priv->observables = g_slist_prepend (priv->observables, data);
@@ -300,6 +306,7 @@ modest_progress_bar_remove_operation (ModestProgressObject *self,
                        priv->current = NULL;
 
                /* Refresh the view */
+               modest_progress_bar_widget_set_pulsating_mode (me, NULL, FALSE);
                progressbar_clean (GTK_PROGRESS_BAR (priv->progress_bar));
        }
        
@@ -330,12 +337,6 @@ modest_progress_bar_cancel_current_operation (ModestProgressObject *self)
 
        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);
 }
 
@@ -348,12 +349,6 @@ modest_progress_bar_cancel_all_operations (ModestProgressObject *self)
        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 ());
 }
@@ -403,6 +398,8 @@ on_progress_changed (ModestMailOperation  *mail_op,
                        modest_progress_bar_widget_set_progress (self, msg,
                                                                 state->bytes_done,
                                                                 state->bytes_total);
+               else if ((state->done == 0) && (state->total == 0))
+                       modest_progress_bar_widget_set_pulsating_mode (self, msg, TRUE);
                else
                        modest_progress_bar_widget_set_progress (self, msg,
                                                                 state->done,
@@ -415,7 +412,7 @@ static gboolean
 progressbar_clean (GtkProgressBar *bar)
 {
        gtk_progress_bar_set_fraction (bar, 0);
-       gtk_progress_bar_set_text (bar, 0);
+       gtk_progress_bar_set_text (bar, " ");
        return FALSE;
 }
 
@@ -441,7 +438,10 @@ modest_progress_bar_widget_set_progress (ModestProgressBarWidget *self,
        
        priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (self);
 
-       priv->count++;
+       priv->count++;
+
+       if (modest_progress_bar_widget_is_pulsating (self))
+               modest_progress_bar_widget_set_pulsating_mode (self, NULL, FALSE);
 
        /* Set progress. Tinymail sometimes returns us 1/100 when it
           does not have any clue, NOTE that 1/100 could be also a
@@ -449,17 +449,8 @@ modest_progress_bar_widget_set_progress (ModestProgressBarWidget *self,
           once the done is greater than 1 */
        determined = (done > 0 && total > 1) && 
                !(done == 1 && total == 100);
-/*     if ((done == 0 && total == 0) ||  */
-/*         (done == 1 && total == 100)) { */
        if (!determined) {
-               gtk_progress_bar_set_bar_style (GTK_PROGRESS_BAR (priv->progress_bar), /* Deprecated */
-                                               GTK_PROGRESS_CONTINUOUS);
                gtk_progress_bar_pulse (GTK_PROGRESS_BAR (priv->progress_bar));
-               printf ("debug: %s:\n  undetermined progress (%i) changed (%i/%i) : %i\n", __FUNCTION__, 
-                       (int) priv->progress_bar,
-                       done, 
-                       total,
-                       priv->count);
        } else {
                gdouble percent = 0;
                if (total != 0) /* Avoid division by zero. */
@@ -467,16 +458,11 @@ modest_progress_bar_widget_set_progress (ModestProgressBarWidget *self,
 
                gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (priv->progress_bar),
                                               percent);
-
-               printf ("debug: %s:\n  determined progress (%i) changed (%i/%i) : %i\n", __FUNCTION__, 
-                       (int) priv->progress_bar,
-                       done, 
-                       total,
-                       priv->count);
        }
        
        /* Set text */
-       gtk_progress_bar_set_text (GTK_PROGRESS_BAR (priv->progress_bar), message);
+       gtk_progress_bar_set_text (GTK_PROGRESS_BAR (priv->progress_bar), 
+                                  (message && message[0] != '\0')?message:" ");
 }
 
 
@@ -493,3 +479,55 @@ modest_progress_bar_widget_set_undetermined_progress (ModestProgressBarWidget *s
        on_progress_changed (mail_op, state, self);
        g_free(state);
 }
+
+/* this has to be explicitly removed */
+static gboolean
+do_pulse (gpointer data)
+{
+       ModestProgressBarWidgetPrivate *priv;
+       
+       g_return_val_if_fail (MODEST_IS_PROGRESS_BAR_WIDGET(data), FALSE);
+       
+       priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (data);
+       gtk_progress_bar_pulse (GTK_PROGRESS_BAR (priv->progress_bar));
+       return TRUE;
+}
+
+gboolean
+modest_progress_bar_widget_is_pulsating (ModestProgressBarWidget *self)
+{
+       ModestProgressBarWidgetPrivate *priv;
+
+       g_return_val_if_fail (MODEST_IS_PROGRESS_BAR_WIDGET(self), FALSE);
+
+       priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (self);
+       
+       return priv->pulsating_timeout != 0;
+}
+
+void
+modest_progress_bar_widget_set_pulsating_mode (ModestProgressBarWidget *self,
+                                               const gchar* msg,
+                                               gboolean is_pulsating)
+{
+       ModestProgressBarWidgetPrivate *priv;
+
+       g_return_if_fail (MODEST_IS_PROGRESS_BAR_WIDGET(self));
+
+       priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (self);
+       
+       if (msg != NULL)
+               gtk_progress_bar_set_text (GTK_PROGRESS_BAR (priv->progress_bar), msg);
+       
+       if (is_pulsating == (priv->pulsating_timeout != 0))
+               return;
+       else if (is_pulsating && (priv->pulsating_timeout == 0)) {
+               /* enable */
+               priv->pulsating_timeout = g_timeout_add (MODEST_PROGRESS_BAR_PULSE_INTERVAL,
+                               do_pulse, self);
+       } else if (!is_pulsating && (priv->pulsating_timeout != 0)) {
+               /* disable */
+               g_source_remove (priv->pulsating_timeout);
+               priv->pulsating_timeout = 0;
+       }
+}