Modified webpage: now tinymail repository is in gitorious.
[modest] / src / widgets / modest-progress-bar.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <string.h>
31 #include <glib/gi18n.h>
32 #include <gtk/gtk.h>
33 #include "widgets/modest-progress-bar.h"
34 #include "modest-tny-account.h"
35 #include "modest-platform.h"
36 #include "modest-runtime.h"
37
38 /* 'private'/'protected' functions */
39 static void modest_progress_bar_class_init (ModestProgressBarClass *klass);
40 static void modest_progress_bar_init       (ModestProgressBar *obj);
41 static void modest_progress_bar_finalize   (GObject *obj);
42
43 static void modest_progress_bar_add_operation    (ModestProgressObject *self,
44                                                     ModestMailOperation  *mail_op);
45
46 static void modest_progress_bar_remove_operation (ModestProgressObject *self,
47                                                     ModestMailOperation  *mail_op);
48
49 static void modest_progress_bar_cancel_current_operation (ModestProgressObject *self);
50
51 static void modest_progress_bar_cancel_all_operations    (ModestProgressObject *self);
52
53 static guint modest_progress_bar_num_pending_operations (ModestProgressObject *self);
54
55 static void on_progress_changed                    (ModestMailOperation  *mail_op, 
56                                                     ModestMailOperationState *state,
57                                                     ModestProgressBar *self);
58
59 static gboolean     progressbar_clean        (GtkProgressBar *bar);
60
61 static gboolean modest_progress_bar_is_pulsating (ModestProgressBar *self);
62
63 static void modest_progress_bar_set_pulsating_mode (ModestProgressBar *self,
64                                                            const gchar* msg,
65                                                            gboolean is_pulsating);
66
67 static gchar *progress_string (ModestMailOperationTypeOperation op_type, guint done, guint total);
68
69 #define XALIGN 0.5
70 #define YALIGN 0.5
71 #define XSPACE 1
72 #define YSPACE 0
73
74 #define LOWER 0
75 #define UPPER 150
76
77 #define MODEST_PROGRESS_BAR_PULSE_INTERVAL 125
78
79 /* list my signals  */
80 /* enum { */
81 /*      LAST_SIGNAL */
82 /* }; */
83
84 typedef struct _ObservableData ObservableData;
85 struct _ObservableData {
86         guint signal_handler;
87         ModestMailOperation *mail_op;
88 };
89
90 typedef struct _ModestProgressBarPrivate ModestProgressBarPrivate;
91 struct _ModestProgressBarPrivate {
92         GSList              *observables;
93         ModestMailOperation *current;
94         guint count;
95         GtkWidget *progress_bar;
96         guint pulsating_timeout;
97 };
98 #define MODEST_PROGRESS_BAR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
99                                                  MODEST_TYPE_PROGRESS_BAR_WIDGET, \
100                                                  ModestProgressBarPrivate))
101
102 /* globals */
103 static GtkContainerClass *parent_class = NULL;
104
105 /* uncomment the following if you have defined any signals */
106 /* static guint signals[LAST_SIGNAL] = {0}; */
107
108 static void
109 modest_progress_object_init (gpointer g, gpointer iface_data)
110 {
111         ModestProgressObjectIface *klass = (ModestProgressObjectIface *)g;
112
113         klass->add_operation_func = modest_progress_bar_add_operation;
114         klass->remove_operation_func = modest_progress_bar_remove_operation;
115         klass->cancel_current_operation_func = modest_progress_bar_cancel_current_operation;
116         klass->cancel_all_operations_func = modest_progress_bar_cancel_all_operations;
117         klass->num_pending_operations_func = modest_progress_bar_num_pending_operations;
118 }
119
120
121 GType
122 modest_progress_bar_get_type (void)
123 {
124         static GType my_type = 0;
125         if (!my_type) {
126                 static const GTypeInfo my_info = {
127                         sizeof(ModestProgressBarClass),
128                         NULL,           /* base init */
129                         NULL,           /* base finalize */
130                         (GClassInitFunc) modest_progress_bar_class_init,
131                         NULL,           /* class finalize */
132                         NULL,           /* class data */
133                         sizeof(ModestProgressBar),
134                         1,              /* n_preallocs */
135                         (GInstanceInitFunc) modest_progress_bar_init,
136                         NULL
137                 };
138
139                 static const GInterfaceInfo modest_progress_object_info = 
140                 {
141                   (GInterfaceInitFunc) modest_progress_object_init, /* interface_init */
142                   NULL,         /* interface_finalize */
143                   NULL          /* interface_data */
144                 };
145
146                 my_type = g_type_register_static (GTK_TYPE_VBOX,
147                                                   "ModestProgressBar",
148                                                   &my_info, 0);
149
150                 g_type_add_interface_static (my_type, MODEST_TYPE_PROGRESS_OBJECT, 
151                                              &modest_progress_object_info);
152         }
153         return my_type;
154 }
155
156 static void
157 modest_progress_bar_class_init (ModestProgressBarClass *klass)
158 {
159         GObjectClass *gobject_class;
160         gobject_class = (GObjectClass*) klass;
161
162         parent_class            = g_type_class_peek_parent (klass);
163         gobject_class->finalize = modest_progress_bar_finalize;
164
165         g_type_class_add_private (gobject_class, sizeof(ModestProgressBarPrivate));
166 }
167
168 static void
169 modest_progress_bar_init (ModestProgressBar *self)
170 {
171         
172         ModestProgressBarPrivate *priv;
173         GtkWidget *align = NULL;
174         GtkRequisition req;
175         GtkAdjustment *adj;
176
177         priv = MODEST_PROGRESS_BAR_GET_PRIVATE(self);
178
179         /* Alignment */
180         align = gtk_alignment_new(XALIGN, YALIGN, XSPACE, YSPACE);
181
182         /* Build GtkProgressBar */
183         adj = (GtkAdjustment *) gtk_adjustment_new (0, LOWER, UPPER, 0, 0, 0);
184         priv->progress_bar = gtk_progress_bar_new ();
185         g_object_set (priv->progress_bar, "adjustment", adj, NULL);
186         req.width = 228;
187         req.height = 64;
188         gtk_progress_set_text_alignment (GTK_PROGRESS (priv->progress_bar), 0, 0.5);
189         gtk_progress_bar_set_ellipsize (GTK_PROGRESS_BAR (priv->progress_bar), PANGO_ELLIPSIZE_END);
190         gtk_progress_bar_set_pulse_step (GTK_PROGRESS_BAR (priv->progress_bar), 0.25);
191         gtk_progress_bar_set_text (GTK_PROGRESS_BAR (priv->progress_bar), " ");
192         gtk_widget_size_request (priv->progress_bar, &req);
193         gtk_container_add (GTK_CONTAINER (align), priv->progress_bar);
194         gtk_widget_size_request (align, &req);
195
196         /* Add progress bar widget */   
197         gtk_box_pack_start (GTK_BOX(self), align, TRUE, TRUE, 0);
198         gtk_widget_show_all (GTK_WIDGET(self));       
199         
200         priv->pulsating_timeout = 0;
201 }
202
203 static void
204 modest_progress_bar_finalize (GObject *obj)
205 {
206         ModestProgressBarPrivate *priv;
207
208         priv = MODEST_PROGRESS_BAR_GET_PRIVATE(obj);
209         if (priv->observables) {
210                 GSList *tmp;
211
212                 for (tmp = priv->observables; tmp; tmp = g_slist_next (tmp)) {
213                         ObservableData *ob_data = tmp->data;
214                         g_signal_handler_disconnect (ob_data->mail_op, ob_data->signal_handler);
215                         g_object_unref (ob_data->mail_op);
216                         g_free (ob_data);
217                 }
218                 g_slist_free (priv->observables);
219                 priv->observables = NULL;
220         }
221         
222         /* remove timeout */
223         if (priv->pulsating_timeout != 0)
224                 g_source_remove (priv->pulsating_timeout);
225
226         G_OBJECT_CLASS(parent_class)->finalize (obj);
227 }
228
229
230 static void 
231 modest_progress_bar_add_operation (ModestProgressObject *self,
232                                    ModestMailOperation  *mail_op)
233 {
234         ModestProgressBar *me = NULL;
235         ObservableData *data = NULL;
236         ModestProgressBarPrivate *priv = NULL;
237         
238         me = MODEST_PROGRESS_BAR (self);
239         priv = MODEST_PROGRESS_BAR_GET_PRIVATE (me);
240
241         data = g_malloc0 (sizeof (ObservableData));
242         data->mail_op = g_object_ref (mail_op);
243         data->signal_handler = g_signal_connect (data->mail_op,
244                                                  "progress-changed",
245                                                  G_CALLBACK (on_progress_changed),
246                                                  me);
247         /* Set curent operation */
248         if (priv->current == NULL) {
249                 priv->current = mail_op;
250                 
251                 priv->count = 0;
252                 
253                 /* Call progress_change handler to initialize progress message */
254 /*              modest_progress_bar_set_undetermined_progress (MODEST_PROGRESS_BAR(self), mail_op); */
255         }
256
257         /* Add operation to obserbable objects list */
258         priv->observables = g_slist_prepend (priv->observables, data);
259 }
260
261 static gint
262 compare_observable_data (ObservableData *data1, ObservableData *data2)
263 {
264         if (data1->mail_op == data2->mail_op)
265                 return 0;
266         else 
267                 return 1;
268 }
269
270 static void 
271 modest_progress_bar_remove_operation (ModestProgressObject *self,
272                                       ModestMailOperation  *mail_op)
273 {
274         ModestProgressBar *me;
275         ModestProgressBarPrivate *priv;
276         GSList *link;
277         ObservableData *tmp_data = NULL;
278         gboolean is_current;
279
280         me = MODEST_PROGRESS_BAR (self);
281         priv = MODEST_PROGRESS_BAR_GET_PRIVATE (me);
282
283         is_current = (priv->current == mail_op);
284
285         /* Find item */
286         tmp_data = g_malloc0 (sizeof (ObservableData));
287         tmp_data->mail_op = mail_op;
288         link = g_slist_find_custom (priv->observables,
289                                     tmp_data,
290                                     (GCompareFunc) compare_observable_data);
291         
292         /* Remove the item */
293         if (link) {
294                 ObservableData *ob_data = link->data;
295                 g_signal_handler_disconnect (ob_data->mail_op, ob_data->signal_handler);
296                 g_object_unref (ob_data->mail_op);
297                 g_free (ob_data);
298                 priv->observables = g_slist_delete_link (priv->observables, link);
299                 tmp_data->mail_op = NULL;
300                 link = NULL;
301         }
302         
303         /* Update the current mail operation */
304         if (is_current) {
305                 if (priv->observables) {
306                         gchar *msg;
307                         priv->current = ((ObservableData *) priv->observables->data)->mail_op;
308                         msg = progress_string (modest_mail_operation_get_type_operation (MODEST_MAIL_OPERATION (priv->current)), 0, 0);
309                         modest_progress_bar_set_pulsating_mode (me, msg, TRUE);
310                         g_free (msg);
311                 } else {
312                         priv->current = NULL;
313                         modest_progress_bar_set_pulsating_mode (me, NULL, FALSE);
314                         progressbar_clean (GTK_PROGRESS_BAR (priv->progress_bar));
315                 }
316
317                 /* Refresh the view */
318         }
319         
320         /* free */
321         g_free(tmp_data);
322 }
323
324 static guint
325 modest_progress_bar_num_pending_operations (ModestProgressObject *self)
326 {
327         ModestProgressBar *me;
328         ModestProgressBarPrivate *priv;
329         
330         me = MODEST_PROGRESS_BAR (self);
331         priv = MODEST_PROGRESS_BAR_GET_PRIVATE (me);
332         
333         return g_slist_length(priv->observables);
334 }
335
336 static void 
337 modest_progress_bar_cancel_current_operation (ModestProgressObject *self)
338 {
339         ModestProgressBar *me;
340         ModestProgressBarPrivate *priv;
341
342         me = MODEST_PROGRESS_BAR (self);
343         priv = MODEST_PROGRESS_BAR_GET_PRIVATE (me);
344
345         if (priv->current == NULL) return;
346
347         modest_mail_operation_cancel (priv->current);
348 }
349
350 static void 
351 modest_progress_bar_cancel_all_operations (ModestProgressObject *self)
352 {
353
354         /* Cancel all the mail operations */
355         modest_mail_operation_queue_cancel_all (modest_runtime_get_mail_operation_queue ());
356 }
357
358 static gchar *
359 progress_string (ModestMailOperationTypeOperation op_type, guint done, guint total)
360 {
361         gboolean determined = FALSE;
362
363         gchar *msg = NULL;
364
365         determined = (done > 0 && total > 1) && 
366                 !(done == 1 && total == 100);
367
368         switch (op_type) {
369         case MODEST_MAIL_OPERATION_TYPE_SEND_AND_RECEIVE:               
370         case MODEST_MAIL_OPERATION_TYPE_RECEIVE:                
371                 if (determined)
372                         msg = g_strdup_printf(_("mcen_me_receiving"),
373                                               done, total); 
374                 else 
375                         msg = g_strdup(_("mail_me_receiving"));
376                 break;
377         case MODEST_MAIL_OPERATION_TYPE_SEND:           
378                 if (determined)
379                         msg = g_strdup_printf(_("mcen_me_sending"), done,
380                                               total);
381                 else
382                         msg = g_strdup(_("mail_me_sending"));
383                 break;
384                 
385         default:
386                 msg = g_strdup("");
387         }
388
389         return msg;
390 }
391
392 static void 
393 on_progress_changed (ModestMailOperation  *mail_op, 
394                      ModestMailOperationState *state,
395                      ModestProgressBar *self)
396 {
397         ModestProgressBarPrivate *priv;
398
399         priv = MODEST_PROGRESS_BAR_GET_PRIVATE (self);
400
401         /* If the mail operation is the currently shown one */
402         if (priv->current == mail_op) {
403                 gchar *msg = NULL;
404                 
405                 msg = progress_string (state->op_type, state->done, state->total);
406                 
407                 /* If we have byte information use it */
408                 if ((state->bytes_done != 0) && (state->bytes_total != 0))
409                         modest_progress_bar_set_progress (self, msg,
410                                                                  state->bytes_done,
411                                                                  state->bytes_total);
412                 else if ((state->done == 0) && (state->total == 0))
413                         modest_progress_bar_set_pulsating_mode (self, msg, TRUE);
414                 else
415                         modest_progress_bar_set_progress (self, msg,
416                                                                  state->done,
417                                                                  state->total);
418                 g_free (msg);
419         }
420 }
421
422 static gboolean
423 progressbar_clean (GtkProgressBar *bar)
424 {
425
426         gtk_progress_bar_set_fraction (bar, 0);
427         gtk_progress_bar_set_text (bar, " ");
428
429         return FALSE;
430 }
431
432
433 GtkWidget*
434 modest_progress_bar_new ()
435 {
436         return GTK_WIDGET (g_object_new (MODEST_TYPE_PROGRESS_BAR_WIDGET, NULL));
437 }
438
439
440 void 
441 modest_progress_bar_set_progress (ModestProgressBar *self,
442                                          const gchar *message,
443                                          gint done,
444                                          gint total)
445 {
446         ModestProgressBarPrivate *priv;
447         gboolean determined = FALSE;
448
449         g_return_if_fail (MODEST_IS_PROGRESS_BAR_WIDGET(self));
450         g_return_if_fail (done <= total);
451         
452         priv = MODEST_PROGRESS_BAR_GET_PRIVATE (self);
453
454         priv->count++;
455
456         if (modest_progress_bar_is_pulsating (self))
457                 modest_progress_bar_set_pulsating_mode (self, NULL, FALSE);
458
459         /* Set progress. Tinymail sometimes returns us 1/100 when it
460            does not have any clue, NOTE that 1/100 could be also a
461            valid progress (we will loose it), but it will be recovered
462            once the done is greater than 1 */
463         determined = (done > 0 && total > 1) && 
464                 !(done == 1 && total == 100);
465         if (!determined) {
466                 gtk_progress_bar_pulse (GTK_PROGRESS_BAR (priv->progress_bar));
467         } else {
468                 gdouble percent = 0;
469                 if (total != 0) /* Avoid division by zero. */
470                         percent = (gdouble)done/(gdouble)total;
471
472                 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (priv->progress_bar),
473                                                percent);
474         }
475         
476         /* Set text */
477         gtk_progress_bar_set_text (GTK_PROGRESS_BAR (priv->progress_bar), 
478                                    (message && message[0] != '\0')?message:" ");
479 }
480
481
482 void
483 modest_progress_bar_set_undetermined_progress (ModestProgressBar *self,
484                                                       ModestMailOperation *mail_op)
485 {
486         ModestMailOperationState *state = NULL;
487
488         state = g_malloc0(sizeof(ModestMailOperationState));
489         state->done = 0;
490         state->total = 0;
491         state->op_type = modest_mail_operation_get_type_operation (mail_op);
492         on_progress_changed (mail_op, state, self);
493         g_free(state);
494 }
495
496 /* this has to be explicitly removed */
497 static gboolean
498 do_pulse (gpointer data)
499 {
500         ModestProgressBarPrivate *priv;
501         
502         g_return_val_if_fail (MODEST_IS_PROGRESS_BAR_WIDGET(data), FALSE);
503         
504         priv = MODEST_PROGRESS_BAR_GET_PRIVATE (data);
505         gtk_progress_bar_pulse (GTK_PROGRESS_BAR (priv->progress_bar));
506         return TRUE;
507 }
508
509 gboolean
510 modest_progress_bar_is_pulsating (ModestProgressBar *self)
511 {
512         ModestProgressBarPrivate *priv;
513
514         g_return_val_if_fail (MODEST_IS_PROGRESS_BAR_WIDGET(self), FALSE);
515
516         priv = MODEST_PROGRESS_BAR_GET_PRIVATE (self);
517         
518         return priv->pulsating_timeout != 0;
519 }
520
521 void
522 modest_progress_bar_set_pulsating_mode (ModestProgressBar *self,
523                                                const gchar* msg,
524                                                gboolean is_pulsating)
525 {
526         ModestProgressBarPrivate *priv;
527
528         g_return_if_fail (MODEST_IS_PROGRESS_BAR_WIDGET(self));
529
530         priv = MODEST_PROGRESS_BAR_GET_PRIVATE (self);
531         
532         if (msg != NULL)
533                 gtk_progress_bar_set_text (GTK_PROGRESS_BAR (priv->progress_bar), msg);
534         
535         if (is_pulsating == (priv->pulsating_timeout != 0))
536                 return;
537         else if (is_pulsating && (priv->pulsating_timeout == 0)) {
538                 /* enable */
539                 priv->pulsating_timeout = g_timeout_add (MODEST_PROGRESS_BAR_PULSE_INTERVAL,
540                                 do_pulse, self);
541         } else if (!is_pulsating && (priv->pulsating_timeout != 0)) {
542                 /* disable */
543                 g_source_remove (priv->pulsating_timeout);
544                 priv->pulsating_timeout = 0;
545         }
546 }