* Changes in the autotools stuff affecting a lot of platform dependent
[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-combo-box.h>
34 #include "widgets/modest-progress-bar.h"
35 #include "modest-tny-account.h"
36 #include "modest-platform.h"
37 #include "modest-runtime.h"
38
39 /* 'private'/'protected' functions */
40 static void modest_progress_bar_class_init (ModestProgressBarClass *klass);
41 static void modest_progress_bar_init       (ModestProgressBar *obj);
42 static void modest_progress_bar_finalize   (GObject *obj);
43
44 static void modest_progress_bar_add_operation    (ModestProgressObject *self,
45                                                     ModestMailOperation  *mail_op);
46
47 static void modest_progress_bar_remove_operation (ModestProgressObject *self,
48                                                     ModestMailOperation  *mail_op);
49
50 static void modest_progress_bar_cancel_current_operation (ModestProgressObject *self);
51
52 static void modest_progress_bar_cancel_all_operations    (ModestProgressObject *self);
53
54 static guint modest_progress_bar_num_pending_operations (ModestProgressObject *self);
55
56 static void on_progress_changed                    (ModestMailOperation  *mail_op, 
57                                                     ModestMailOperationState *state,
58                                                     ModestProgressBar *self);
59
60 static gboolean     progressbar_clean        (GtkProgressBar *bar);
61
62 static gboolean modest_progress_bar_is_pulsating (ModestProgressBar *self);
63
64 static void modest_progress_bar_set_pulsating_mode (ModestProgressBar *self,
65                                                            const gchar* msg,
66                                                            gboolean is_pulsating);
67
68 static gchar *progress_string (ModestMailOperationTypeOperation op_type, guint done, guint total);
69
70 #define XALIGN 0.5
71 #define YALIGN 0.5
72 #define XSPACE 1
73 #define YSPACE 0
74
75 #define LOWER 0
76 #define UPPER 150
77
78 #define MODEST_PROGRESS_BAR_PULSE_INTERVAL 125
79
80 /* list my signals  */
81 /* enum { */
82 /*      LAST_SIGNAL */
83 /* }; */
84
85 typedef struct _ObservableData ObservableData;
86 struct _ObservableData {
87         guint signal_handler;
88         ModestMailOperation *mail_op;
89 };
90
91 typedef struct _ModestProgressBarPrivate ModestProgressBarPrivate;
92 struct _ModestProgressBarPrivate {
93         GSList              *observables;
94         ModestMailOperation *current;
95         guint count;
96         GtkWidget *progress_bar;
97         guint pulsating_timeout;
98 };
99 #define MODEST_PROGRESS_BAR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
100                                                  MODEST_TYPE_PROGRESS_BAR_WIDGET, \
101                                                  ModestProgressBarPrivate))
102
103 /* globals */
104 static GtkContainerClass *parent_class = NULL;
105
106 /* uncomment the following if you have defined any signals */
107 /* static guint signals[LAST_SIGNAL] = {0}; */
108
109 static void
110 modest_progress_object_init (gpointer g, gpointer iface_data)
111 {
112         ModestProgressObjectIface *klass = (ModestProgressObjectIface *)g;
113
114         klass->add_operation_func = modest_progress_bar_add_operation;
115         klass->remove_operation_func = modest_progress_bar_remove_operation;
116         klass->cancel_current_operation_func = modest_progress_bar_cancel_current_operation;
117         klass->cancel_all_operations_func = modest_progress_bar_cancel_all_operations;
118         klass->num_pending_operations_func = modest_progress_bar_num_pending_operations;
119 }
120
121
122 GType
123 modest_progress_bar_get_type (void)
124 {
125         static GType my_type = 0;
126         if (!my_type) {
127                 static const GTypeInfo my_info = {
128                         sizeof(ModestProgressBarClass),
129                         NULL,           /* base init */
130                         NULL,           /* base finalize */
131                         (GClassInitFunc) modest_progress_bar_class_init,
132                         NULL,           /* class finalize */
133                         NULL,           /* class data */
134                         sizeof(ModestProgressBar),
135                         1,              /* n_preallocs */
136                         (GInstanceInitFunc) modest_progress_bar_init,
137                         NULL
138                 };
139
140                 static const GInterfaceInfo modest_progress_object_info = 
141                 {
142                   (GInterfaceInitFunc) modest_progress_object_init, /* interface_init */
143                   NULL,         /* interface_finalize */
144                   NULL          /* interface_data */
145                 };
146
147                 my_type = g_type_register_static (GTK_TYPE_VBOX,
148                                                   "ModestProgressBar",
149                                                   &my_info, 0);
150
151                 g_type_add_interface_static (my_type, MODEST_TYPE_PROGRESS_OBJECT, 
152                                              &modest_progress_object_info);
153         }
154         return my_type;
155 }
156
157 static void
158 modest_progress_bar_class_init (ModestProgressBarClass *klass)
159 {
160         GObjectClass *gobject_class;
161         gobject_class = (GObjectClass*) klass;
162
163         parent_class            = g_type_class_peek_parent (klass);
164         gobject_class->finalize = modest_progress_bar_finalize;
165
166         g_type_class_add_private (gobject_class, sizeof(ModestProgressBarPrivate));
167 }
168
169 static void
170 modest_progress_bar_init (ModestProgressBar *self)
171 {
172         
173         ModestProgressBarPrivate *priv;
174         GtkWidget *align = NULL;
175         GtkRequisition req;
176         GtkAdjustment *adj;
177
178         priv = MODEST_PROGRESS_BAR_GET_PRIVATE(self);
179
180         /* Alignment */
181         align = gtk_alignment_new(XALIGN, YALIGN, XSPACE, YSPACE);
182
183         /* Build GtkProgressBar */
184         adj = (GtkAdjustment *) gtk_adjustment_new (0, LOWER, UPPER, 0, 0, 0);
185         priv->progress_bar = gtk_progress_bar_new_with_adjustment (adj);                
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         ModestProgressBar *me;
354         ModestProgressBarPrivate *priv;
355
356         me = MODEST_PROGRESS_BAR (self);
357         priv = MODEST_PROGRESS_BAR_GET_PRIVATE (me);
358
359         /* Cancel all the mail operations */
360         modest_mail_operation_queue_cancel_all (modest_runtime_get_mail_operation_queue ());
361 }
362
363 static gchar *
364 progress_string (ModestMailOperationTypeOperation op_type, guint done, guint total)
365 {
366         gboolean determined = FALSE;
367
368         gchar *msg = NULL;
369
370         determined = (done > 0 && total > 1) && 
371                 !(done == 1 && total == 100);
372
373         switch (op_type) {
374         case MODEST_MAIL_OPERATION_TYPE_SEND_AND_RECEIVE:               
375         case MODEST_MAIL_OPERATION_TYPE_RECEIVE:                
376                 if (determined)
377                         msg = g_strdup_printf(_("mcen_me_receiving"),
378                                               done, total); 
379                 else 
380                         msg = g_strdup(_("mail_me_receiving"));
381                 break;
382         case MODEST_MAIL_OPERATION_TYPE_SEND:           
383                 if (determined)
384                         msg = g_strdup_printf(_("mcen_me_sending"), done,
385                                               total);
386                 else
387                         msg = g_strdup(_("mail_me_sending"));
388                 break;
389                 
390         default:
391                 msg = g_strdup("");
392         }
393
394         return msg;
395 }
396
397 static void 
398 on_progress_changed (ModestMailOperation  *mail_op, 
399                      ModestMailOperationState *state,
400                      ModestProgressBar *self)
401 {
402         ModestProgressBarPrivate *priv;
403
404         priv = MODEST_PROGRESS_BAR_GET_PRIVATE (self);
405
406         /* If the mail operation is the currently shown one */
407         if (priv->current == mail_op) {
408                 gchar *msg = NULL;
409                 
410                 msg = progress_string (state->op_type, state->done, state->total);
411                 
412                 /* If we have byte information use it */
413                 if ((state->bytes_done != 0) && (state->bytes_total != 0))
414                         modest_progress_bar_set_progress (self, msg,
415                                                                  state->bytes_done,
416                                                                  state->bytes_total);
417                 else if ((state->done == 0) && (state->total == 0))
418                         modest_progress_bar_set_pulsating_mode (self, msg, TRUE);
419                 else
420                         modest_progress_bar_set_progress (self, msg,
421                                                                  state->done,
422                                                                  state->total);
423                 g_free (msg);
424         }
425 }
426
427 static gboolean
428 progressbar_clean (GtkProgressBar *bar)
429 {
430
431         gtk_progress_bar_set_fraction (bar, 0);
432         gtk_progress_bar_set_text (bar, " ");
433
434         return FALSE;
435 }
436
437
438 GtkWidget*
439 modest_progress_bar_new ()
440 {
441         return GTK_WIDGET (g_object_new (MODEST_TYPE_PROGRESS_BAR_WIDGET, NULL));
442 }
443
444
445 void 
446 modest_progress_bar_set_progress (ModestProgressBar *self,
447                                          const gchar *message,
448                                          gint done,
449                                          gint total)
450 {
451         ModestProgressBarPrivate *priv;
452         gboolean determined = FALSE;
453
454         g_return_if_fail (MODEST_IS_PROGRESS_BAR_WIDGET(self));
455         g_return_if_fail (done <= total);
456         
457         priv = MODEST_PROGRESS_BAR_GET_PRIVATE (self);
458
459         priv->count++;
460
461         if (modest_progress_bar_is_pulsating (self))
462                 modest_progress_bar_set_pulsating_mode (self, NULL, FALSE);
463
464         /* Set progress. Tinymail sometimes returns us 1/100 when it
465            does not have any clue, NOTE that 1/100 could be also a
466            valid progress (we will loose it), but it will be recovered
467            once the done is greater than 1 */
468         determined = (done > 0 && total > 1) && 
469                 !(done == 1 && total == 100);
470         if (!determined) {
471                 gtk_progress_bar_pulse (GTK_PROGRESS_BAR (priv->progress_bar));
472         } else {
473                 gdouble percent = 0;
474                 if (total != 0) /* Avoid division by zero. */
475                         percent = (gdouble)done/(gdouble)total;
476
477                 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (priv->progress_bar),
478                                                percent);
479         }
480         
481         /* Set text */
482         gtk_progress_bar_set_text (GTK_PROGRESS_BAR (priv->progress_bar), 
483                                    (message && message[0] != '\0')?message:" ");
484 }
485
486
487 void
488 modest_progress_bar_set_undetermined_progress (ModestProgressBar *self,
489                                                       ModestMailOperation *mail_op)
490 {
491         ModestMailOperationState *state = NULL;
492
493         state = g_malloc0(sizeof(ModestMailOperationState));
494         state->done = 0;
495         state->total = 0;
496         state->op_type = modest_mail_operation_get_type_operation (mail_op);
497         on_progress_changed (mail_op, state, self);
498         g_free(state);
499 }
500
501 /* this has to be explicitly removed */
502 static gboolean
503 do_pulse (gpointer data)
504 {
505         ModestProgressBarPrivate *priv;
506         
507         g_return_val_if_fail (MODEST_IS_PROGRESS_BAR_WIDGET(data), FALSE);
508         
509         priv = MODEST_PROGRESS_BAR_GET_PRIVATE (data);
510         gtk_progress_bar_pulse (GTK_PROGRESS_BAR (priv->progress_bar));
511         return TRUE;
512 }
513
514 gboolean
515 modest_progress_bar_is_pulsating (ModestProgressBar *self)
516 {
517         ModestProgressBarPrivate *priv;
518
519         g_return_val_if_fail (MODEST_IS_PROGRESS_BAR_WIDGET(self), FALSE);
520
521         priv = MODEST_PROGRESS_BAR_GET_PRIVATE (self);
522         
523         return priv->pulsating_timeout != 0;
524 }
525
526 void
527 modest_progress_bar_set_pulsating_mode (ModestProgressBar *self,
528                                                const gchar* msg,
529                                                gboolean is_pulsating)
530 {
531         ModestProgressBarPrivate *priv;
532
533         g_return_if_fail (MODEST_IS_PROGRESS_BAR_WIDGET(self));
534
535         priv = MODEST_PROGRESS_BAR_GET_PRIVATE (self);
536         
537         if (msg != NULL)
538                 gtk_progress_bar_set_text (GTK_PROGRESS_BAR (priv->progress_bar), msg);
539         
540         if (is_pulsating == (priv->pulsating_timeout != 0))
541                 return;
542         else if (is_pulsating && (priv->pulsating_timeout == 0)) {
543                 /* enable */
544                 priv->pulsating_timeout = g_timeout_add (MODEST_PROGRESS_BAR_PULSE_INTERVAL,
545                                 do_pulse, self);
546         } else if (!is_pulsating && (priv->pulsating_timeout != 0)) {
547                 /* disable */
548                 g_source_remove (priv->pulsating_timeout);
549                 priv->pulsating_timeout = 0;
550         }
551 }