b6993513fe153cf988f1aedadaa3850370531d49
[modest] / src / maemo / modest-progress-bar-widget.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 <glib/gi18n.h>
31 #include <gtk/gtk.h>
32 #include <widgets/modest-combo-box.h>
33 #include "modest-progress-bar-widget.h"
34 #include <string.h>
35 #include "modest-platform.h"
36 #include "modest-runtime.h"
37
38 /* 'private'/'protected' functions */
39 static void modest_progress_bar_widget_class_init (ModestProgressBarWidgetClass *klass);
40 static void modest_progress_bar_widget_init       (ModestProgressBarWidget *obj);
41 static void modest_progress_bar_widget_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                                                     ModestProgressBarWidget *self);
58
59 static gboolean     progressbar_clean        (GtkProgressBar *bar);
60
61 #define XALIGN 0.5
62 #define YALIGN 0.5
63 #define XSPACE 1
64 #define YSPACE 0
65
66 #define LOWER 0
67 #define UPPER 150
68
69 /* list my signals  */
70 /* enum { */
71 /*      LAST_SIGNAL */
72 /* }; */
73
74 typedef struct _ObservableData ObservableData;
75 struct _ObservableData {
76         guint signal_handler;
77         ModestMailOperation *mail_op;
78 };
79
80 typedef struct _ModestProgressBarWidgetPrivate ModestProgressBarWidgetPrivate;
81 struct _ModestProgressBarWidgetPrivate {
82         GSList              *observables;
83         ModestMailOperation *current;
84
85         guint count;
86         GtkWidget *progress_bar;
87 };
88 #define MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
89                                                  MODEST_TYPE_PROGRESS_BAR_WIDGET, \
90                                                  ModestProgressBarWidgetPrivate))
91
92 /* globals */
93 static GtkContainerClass *parent_class = NULL;
94
95 /* uncomment the following if you have defined any signals */
96 /* static guint signals[LAST_SIGNAL] = {0}; */
97
98 static void
99 modest_progress_object_init (gpointer g, gpointer iface_data)
100 {
101         ModestProgressObjectIface *klass = (ModestProgressObjectIface *)g;
102
103         klass->add_operation_func = modest_progress_bar_add_operation;
104         klass->remove_operation_func = modest_progress_bar_remove_operation;
105         klass->cancel_current_operation_func = modest_progress_bar_cancel_current_operation;
106         klass->cancel_all_operations_func = modest_progress_bar_cancel_all_operations;
107         klass->num_pending_operations_func = modest_progress_bar_num_pending_operations;
108 }
109
110
111 GType
112 modest_progress_bar_widget_get_type (void)
113 {
114         static GType my_type = 0;
115         if (!my_type) {
116                 static const GTypeInfo my_info = {
117                         sizeof(ModestProgressBarWidgetClass),
118                         NULL,           /* base init */
119                         NULL,           /* base finalize */
120                         (GClassInitFunc) modest_progress_bar_widget_class_init,
121                         NULL,           /* class finalize */
122                         NULL,           /* class data */
123                         sizeof(ModestProgressBarWidget),
124                         1,              /* n_preallocs */
125                         (GInstanceInitFunc) modest_progress_bar_widget_init,
126                         NULL
127                 };
128
129                 static const GInterfaceInfo modest_progress_object_info = 
130                 {
131                   (GInterfaceInitFunc) modest_progress_object_init, /* interface_init */
132                   NULL,         /* interface_finalize */
133                   NULL          /* interface_data */
134                 };
135
136                 my_type = g_type_register_static (GTK_TYPE_VBOX,
137                                                   "ModestProgressBarWidget",
138                                                   &my_info, 0);
139
140                 g_type_add_interface_static (my_type, MODEST_TYPE_PROGRESS_OBJECT, 
141                                              &modest_progress_object_info);
142         }
143         return my_type;
144 }
145
146 static void
147 modest_progress_bar_widget_class_init (ModestProgressBarWidgetClass *klass)
148 {
149         GObjectClass *gobject_class;
150         gobject_class = (GObjectClass*) klass;
151
152         parent_class            = g_type_class_peek_parent (klass);
153         gobject_class->finalize = modest_progress_bar_widget_finalize;
154
155         g_type_class_add_private (gobject_class, sizeof(ModestProgressBarWidgetPrivate));
156
157         /* signal definitions go here, e.g.: */
158 /*      signals[DATA_CHANGED_SIGNAL] = */
159 /*              g_signal_new ("data_changed", */
160 /*                            G_TYPE_FROM_CLASS (klass), */
161 /*                            G_SIGNAL_RUN_FIRST, */
162 /*                            G_STRUCT_OFFSET(ModestProgressBarWidgetClass, data_changed), */
163 /*                            NULL, NULL, */
164 /*                            g_cclosure_marshal_VOID__VOID, */
165 /*                            G_TYPE_NONE, 0); */
166 }
167
168 static void
169 modest_progress_bar_widget_init (ModestProgressBarWidget *self)
170 {
171         
172         ModestProgressBarWidgetPrivate *priv;
173         GtkWidget *align = NULL;
174         GtkRequisition req;
175         GtkAdjustment *adj;
176
177         priv = MODEST_PROGRESS_BAR_WIDGET_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_with_adjustment (adj);                
185         req.width = 228;
186         req.height = 64;
187         gtk_progress_set_text_alignment (GTK_PROGRESS (priv->progress_bar), 0, 0.5);
188         gtk_progress_bar_set_ellipsize (GTK_PROGRESS_BAR (priv->progress_bar), PANGO_ELLIPSIZE_END);
189         gtk_progress_bar_set_pulse_step (GTK_PROGRESS_BAR (priv->progress_bar), 0.05);
190         gtk_widget_size_request (priv->progress_bar, &req);
191         gtk_container_add (GTK_CONTAINER (align), priv->progress_bar);
192         gtk_widget_size_request (align, &req);
193
194         /* Add progress bar widget */   
195         gtk_box_pack_start (GTK_BOX(self), align, TRUE, TRUE, 0);
196         gtk_widget_show_all (GTK_WIDGET(self));       
197 }
198
199 static void
200 modest_progress_bar_widget_finalize (GObject *obj)
201 {
202         ModestProgressBarWidgetPrivate *priv;
203
204         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE(obj);
205         if (priv->observables) {
206                 GSList *tmp;
207
208                 for (tmp = priv->observables; tmp; tmp = g_slist_next (tmp)) {
209                         ObservableData *ob_data = tmp->data;
210                         g_signal_handler_disconnect (ob_data->mail_op, ob_data->signal_handler);
211                         g_object_unref (ob_data->mail_op);
212                         g_free (ob_data);
213                 }
214                 g_slist_free (priv->observables);
215                 priv->observables = NULL;
216         }
217
218         G_OBJECT_CLASS(parent_class)->finalize (obj);
219 }
220
221
222 static void 
223 modest_progress_bar_add_operation (ModestProgressObject *self,
224                                    ModestMailOperation  *mail_op)
225 {
226         ModestProgressBarWidget *me = NULL;
227         ObservableData *data = NULL;
228         ModestProgressBarWidgetPrivate *priv = NULL;
229         
230         me = MODEST_PROGRESS_BAR_WIDGET (self);
231         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (me);
232
233         data = g_malloc0 (sizeof (ObservableData));
234         data->mail_op = g_object_ref (mail_op);
235         data->signal_handler = g_signal_connect (data->mail_op,
236                                                  "progress-changed",
237                                                  G_CALLBACK (on_progress_changed),
238                                                  me);
239         /* Set curent operation */
240 /*      if (priv->current == NULL) { */
241         priv->current = mail_op;
242         
243         priv->count = 0;
244         
245         /* Call progress_change handler to initialize progress message */
246         modest_progress_bar_widget_set_undetermined_progress (MODEST_PROGRESS_BAR_WIDGET(self), mail_op);
247 /*      } */
248
249         /* Add operation to obserbable objects list */
250         priv->observables = g_slist_prepend (priv->observables, data);
251 }
252
253 static gint
254 compare_observable_data (ObservableData *data1, ObservableData *data2)
255 {
256         if (data1->mail_op == data2->mail_op)
257                 return 0;
258         else 
259                 return 1;
260 }
261
262 static void 
263 modest_progress_bar_remove_operation (ModestProgressObject *self,
264                                       ModestMailOperation  *mail_op)
265 {
266         ModestProgressBarWidget *me;
267         ModestProgressBarWidgetPrivate *priv;
268         GSList *link;
269         ObservableData *tmp_data = NULL;
270         gboolean is_current;
271
272         me = MODEST_PROGRESS_BAR_WIDGET (self);
273         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (me);
274
275         is_current = (priv->current == mail_op);
276
277         /* Find item */
278         tmp_data = g_malloc0 (sizeof (ObservableData));
279         tmp_data->mail_op = mail_op;
280         link = g_slist_find_custom (priv->observables,
281                                     tmp_data,
282                                     (GCompareFunc) compare_observable_data);
283         
284         /* Remove the item */
285         if (link) {
286                 ObservableData *ob_data = link->data;
287                 g_signal_handler_disconnect (ob_data->mail_op, ob_data->signal_handler);
288                 g_object_unref (ob_data->mail_op);
289                 g_free (ob_data);
290                 priv->observables = g_slist_delete_link (priv->observables, link);
291                 tmp_data->mail_op = NULL;
292                 link = NULL;
293         }
294         
295         /* Update the current mail operation */
296         if (is_current) {
297                 if (priv->observables)
298                         priv->current = ((ObservableData *) priv->observables->data)->mail_op;
299                 else
300                         priv->current = NULL;
301
302                 /* Refresh the view */
303                 progressbar_clean (GTK_PROGRESS_BAR (priv->progress_bar));
304         }
305         
306         /* free */
307         g_free(tmp_data);
308 }
309
310 static guint
311 modest_progress_bar_num_pending_operations (ModestProgressObject *self)
312 {
313         ModestProgressBarWidget *me;
314         ModestProgressBarWidgetPrivate *priv;
315         
316         me = MODEST_PROGRESS_BAR_WIDGET (self);
317         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (me);
318         
319         return g_slist_length(priv->observables);
320 }
321
322 static void 
323 modest_progress_bar_cancel_current_operation (ModestProgressObject *self)
324 {
325         ModestProgressBarWidget *me;
326         ModestProgressBarWidgetPrivate *priv;
327
328         me = MODEST_PROGRESS_BAR_WIDGET (self);
329         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (me);
330
331         if (priv->current == NULL) return;
332
333         /* If received canceled we shall show banner */
334         if (modest_mail_operation_get_type_operation (priv->current) ==
335             MODEST_MAIL_OPERATION_TYPE_RECEIVE)
336                 modest_platform_information_banner (NULL, NULL, 
337                                                     _("emev_ib_ui_pop3_msg_recv_cancel"));
338
339         modest_mail_operation_cancel (priv->current);
340 }
341
342 static void 
343 modest_progress_bar_cancel_all_operations (ModestProgressObject *self)
344 {
345         ModestProgressBarWidget *me;
346         ModestProgressBarWidgetPrivate *priv;
347
348         me = MODEST_PROGRESS_BAR_WIDGET (self);
349         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (me);
350
351         /* If received canceled we shall show banner */
352         if (priv->current && modest_mail_operation_get_type_operation (priv->current) ==
353             MODEST_MAIL_OPERATION_TYPE_RECEIVE)
354                 modest_platform_information_banner (NULL, NULL, 
355                                                     _("emev_ib_ui_pop3_msg_recv_cancel"));
356
357         /* Cancel all the mail operations */
358         modest_mail_operation_queue_cancel_all (modest_runtime_get_mail_operation_queue ());
359 }
360
361 static void 
362 on_progress_changed (ModestMailOperation  *mail_op, 
363                      ModestMailOperationState *state,
364                      ModestProgressBarWidget *self)
365 {
366         ModestProgressBarWidgetPrivate *priv;
367         gboolean determined = FALSE;
368
369         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (self);
370
371         /* If the mail operation is the currently shown one */
372         if (priv->current == mail_op) {
373                 gchar *msg = NULL;
374                 
375                 determined = (state->done > 0 && state->total > 1) && 
376                         !(state->done == 1 && state->total == 100);
377
378                 switch (state->op_type) {
379                 case MODEST_MAIL_OPERATION_TYPE_RECEIVE:                
380                         if (determined)
381                                 msg = g_strdup_printf(_("mcen_me_receiving"),
382                                                       state->done, state->total); 
383                         else 
384                                 msg = g_strdup(_("mail_me_receiving"));
385                         break;
386                 case MODEST_MAIL_OPERATION_TYPE_SEND:           
387                         if (determined)
388                                 msg = g_strdup_printf(_("mcen_me_sending"), state->done,
389                                                       state->total);
390                         else
391                                 msg = g_strdup(_("mail_me_sending"));
392                         break;
393                         
394                 case MODEST_MAIL_OPERATION_TYPE_OPEN:           
395                         msg = g_strdup(_("mail_me_opening"));
396                         break;
397                 default:
398                         msg = g_strdup("");
399                 }
400                 
401                 /* If we have byte information use it */
402                 if ((state->bytes_done != 0) && (state->bytes_total != 0))
403                         modest_progress_bar_widget_set_progress (self, msg,
404                                                                  state->bytes_done,
405                                                                  state->bytes_total);
406                 else
407                         modest_progress_bar_widget_set_progress (self, msg,
408                                                                  state->done,
409                                                                  state->total);
410                 g_free (msg);
411         }
412 }
413
414 static gboolean
415 progressbar_clean (GtkProgressBar *bar)
416 {
417         gtk_progress_bar_set_fraction (bar, 0);
418         gtk_progress_bar_set_text (bar, 0);
419         return FALSE;
420 }
421
422
423 GtkWidget*
424 modest_progress_bar_widget_new ()
425 {
426         return GTK_WIDGET (g_object_new (MODEST_TYPE_PROGRESS_BAR_WIDGET, NULL));
427 }
428
429
430 void 
431 modest_progress_bar_widget_set_progress (ModestProgressBarWidget *self,
432                                          const gchar *message,
433                                          gint done,
434                                          gint total)
435 {
436         ModestProgressBarWidgetPrivate *priv;
437         gboolean determined = FALSE;
438
439         g_return_if_fail (MODEST_IS_PROGRESS_BAR_WIDGET(self));
440         g_return_if_fail (done <= total);
441         
442         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (self);
443
444         priv->count++;
445
446         /* Set progress. Tinymail sometimes returns us 1/100 when it
447            does not have any clue, NOTE that 1/100 could be also a
448            valid progress (we will loose it), but it will be recovered
449            once the done is greater than 1 */
450         determined = (done > 0 && total > 1) && 
451                 !(done == 1 && total == 100);
452 /*      if ((done == 0 && total == 0) ||  */
453 /*          (done == 1 && total == 100)) { */
454         if (!determined) {
455                 gtk_progress_bar_set_bar_style (GTK_PROGRESS_BAR (priv->progress_bar), /* Deprecated */
456                                                 GTK_PROGRESS_CONTINUOUS);
457                 gtk_progress_bar_pulse (GTK_PROGRESS_BAR (priv->progress_bar));
458                 printf ("debug: %s:\n  undetermined progress (%i) changed (%i/%i) : %i\n", __FUNCTION__, 
459                         (int) priv->progress_bar,
460                         done, 
461                         total,
462                         priv->count);
463         } else {
464                 gdouble percent = 0;
465                 if (total != 0) /* Avoid division by zero. */
466                         percent = (gdouble)done/(gdouble)total;
467
468                 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (priv->progress_bar),
469                                                percent);
470
471                 printf ("debug: %s:\n  determined progress (%i) changed (%i/%i) : %i\n", __FUNCTION__, 
472                         (int) priv->progress_bar,
473                         done, 
474                         total,
475                         priv->count);
476         }
477         
478         /* Set text */
479         gtk_progress_bar_set_text (GTK_PROGRESS_BAR (priv->progress_bar), message);
480 }
481
482
483 void
484 modest_progress_bar_widget_set_undetermined_progress (ModestProgressBarWidget *self,
485                                                       ModestMailOperation *mail_op)
486 {
487         ModestMailOperationState *state = NULL;
488
489         state = g_malloc0(sizeof(ModestMailOperationState));
490         state->done = 0;
491         state->total = 0;
492         state->op_type = modest_mail_operation_get_type_operation (mail_op);
493         on_progress_changed (mail_op, state, self);
494         g_free(state);
495 }