ab685c6bed8a2c116841d6c1a6322390bad3d027
[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
37 /* 'private'/'protected' functions */
38 static void modest_progress_bar_widget_class_init (ModestProgressBarWidgetClass *klass);
39 static void modest_progress_bar_widget_init       (ModestProgressBarWidget *obj);
40 static void modest_progress_bar_widget_finalize   (GObject *obj);
41
42 static void modest_progress_bar_add_operation    (ModestProgressObject *self,
43                                                     ModestMailOperation  *mail_op);
44
45 static void modest_progress_bar_remove_operation (ModestProgressObject *self,
46                                                     ModestMailOperation  *mail_op);
47
48 static void 
49 modest_progress_bar_cancel_current_operation (ModestProgressObject *self);
50
51 static guint
52 modest_progress_bar_num_pending_operations (ModestProgressObject *self);
53
54 static void on_progress_changed                    (ModestMailOperation  *mail_op, 
55                                                     ModestMailOperationState *state,
56                                                     ModestProgressBarWidget *self);
57
58 static gboolean     progressbar_clean        (GtkProgressBar *bar);
59
60 #define XALIGN 0.5
61 #define YALIGN 0.5
62 #define XSPACE 1
63 #define YSPACE 0
64
65 #define LOWER 0
66 #define UPPER 150
67
68 /* list my signals  */
69 /* enum { */
70 /*      LAST_SIGNAL */
71 /* }; */
72
73 typedef struct _ObservableData ObservableData;
74 struct _ObservableData {
75         guint signal_handler;
76         ModestMailOperation *mail_op;
77 };
78
79 typedef struct _ModestProgressBarWidgetPrivate ModestProgressBarWidgetPrivate;
80 struct _ModestProgressBarWidgetPrivate {
81         GSList              *observables;
82         ModestMailOperation *current;
83
84         GtkWidget *progress_bar;
85 };
86 #define MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
87                                                  MODEST_TYPE_PROGRESS_BAR_WIDGET, \
88                                                  ModestProgressBarWidgetPrivate))
89
90 /* globals */
91 static GtkContainerClass *parent_class = NULL;
92
93 /* Flag to show or not show Recv cancellation banner */
94 static gboolean f_receivingOngoing = FALSE;
95
96 /* uncomment the following if you have defined any signals */
97 /* static guint signals[LAST_SIGNAL] = {0}; */
98
99 static void
100 modest_progress_object_init (gpointer g, gpointer iface_data)
101 {
102         ModestProgressObjectIface *klass = (ModestProgressObjectIface *)g;
103
104         klass->add_operation_func = modest_progress_bar_add_operation;
105         klass->remove_operation_func = modest_progress_bar_remove_operation;
106         klass->cancel_current_operation_func = modest_progress_bar_cancel_current_operation;
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_widget_size_request (priv->progress_bar, &req);
190         gtk_container_add (GTK_CONTAINER (align), priv->progress_bar);
191         gtk_widget_size_request (align, &req);
192
193         /* Add progress bar widget */   
194         gtk_box_pack_start (GTK_BOX(self), align, TRUE, TRUE, 0);
195         gtk_widget_show_all (GTK_WIDGET(self));       
196 }
197
198 static void
199 modest_progress_bar_widget_finalize (GObject *obj)
200 {
201         ModestProgressBarWidgetPrivate *priv;
202
203         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE(obj);
204         if (priv->observables) {
205                 GSList *tmp;
206
207                 for (tmp = priv->observables; tmp; tmp = g_slist_next (tmp)) {
208                         ObservableData *ob_data = tmp->data;
209                         g_signal_handler_disconnect (ob_data->mail_op, ob_data->signal_handler);
210                         g_object_unref (ob_data->mail_op);
211                         g_free (ob_data);
212                 }
213                 g_slist_free (priv->observables);
214                 priv->observables = NULL;
215         }
216
217         G_OBJECT_CLASS(parent_class)->finalize (obj);
218 }
219
220
221 static void 
222 modest_progress_bar_add_operation (ModestProgressObject *self,
223                                    ModestMailOperation  *mail_op)
224 {
225         ModestProgressBarWidget *me = NULL;
226         ObservableData *data = NULL;
227         ModestProgressBarWidgetPrivate *priv = NULL;
228         ModestMailOperationState *state = 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         if (priv->observables == NULL) {
240                 priv->current = mail_op;
241
242                 /* Call progress_change handler to initialize progress message */
243                 state = g_malloc0(sizeof(ModestMailOperationState));
244                 state->done = 0;
245                 state->total = 0;
246                 state->op_type = modest_mail_operation_get_type_operation (mail_op);;
247                 on_progress_changed (mail_op, state, me);
248                 g_free(state);
249         }
250         priv->observables = g_slist_append (priv->observables, data);
251
252 }
253
254 static gint
255 compare_observable_data (ObservableData *data1, ObservableData *data2)
256 {
257         if (data1->mail_op == data2->mail_op)
258                 return 0;
259         else 
260                 return 1;
261 }
262
263 static void 
264 modest_progress_bar_remove_operation (ModestProgressObject *self,
265                                         ModestMailOperation  *mail_op)
266 {
267         ModestProgressBarWidget *me;
268         ModestProgressBarWidgetPrivate *priv;
269         GSList *link;
270         ObservableData *tmp_data = NULL;
271
272         me = MODEST_PROGRESS_BAR_WIDGET (self);
273         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (me);
274
275         /* Find item */
276         tmp_data = g_malloc0 (sizeof (ObservableData));
277         tmp_data->mail_op = mail_op;  
278         link = g_slist_find_custom (priv->observables,
279                                     tmp_data,
280                                     (GCompareFunc) compare_observable_data);
281         
282         /* Remove the item */
283         if (link) {
284                 ObservableData *ob_data = link->data;
285                 g_signal_handler_disconnect (ob_data->mail_op, ob_data->signal_handler);
286                 g_object_unref (ob_data->mail_op);
287                 g_free (ob_data);
288                 priv->observables = g_slist_delete_link (priv->observables, link);
289                 tmp_data->mail_op = NULL;
290                 link = NULL;
291         }
292         
293         /* Update the current mail operation */
294         if (priv->current == mail_op) {
295                 if (priv->observables)
296                         priv->current = ((ObservableData *) priv->observables->data)->mail_op;
297                 else
298                         priv->current = NULL;
299
300                 /* Refresh the view */
301                 progressbar_clean (GTK_PROGRESS_BAR (priv->progress_bar));
302         }
303         
304         /* free */
305         g_free(tmp_data);
306 }
307
308 static guint
309 modest_progress_bar_num_pending_operations (ModestProgressObject *self)
310 {
311         ModestProgressBarWidget *me;
312         ModestProgressBarWidgetPrivate *priv;
313         
314         me = MODEST_PROGRESS_BAR_WIDGET (self);
315         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (me);
316         
317         return g_slist_length(priv->observables);
318 }
319
320 static void 
321 modest_progress_bar_cancel_current_operation (ModestProgressObject *self)
322 {
323         ModestProgressBarWidget *me;
324         ModestProgressBarWidgetPrivate *priv;
325
326         me = MODEST_PROGRESS_BAR_WIDGET (self);
327         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (me);
328
329         if (priv->current == NULL) return;
330
331         /* bug 59107: if received canceled we shall show banner */
332         if ( f_receivingOngoing )
333         {
334                 f_receivingOngoing = FALSE;
335                 modest_platform_information_banner (NULL, NULL, _("emev_ib_ui_pop3_msg_recv_cancel"));
336         }
337
338         modest_mail_operation_cancel (priv->current);
339 }
340
341 static void 
342 on_progress_changed (ModestMailOperation  *mail_op, 
343                      ModestMailOperationState *state,
344                      ModestProgressBarWidget *self)
345 {
346         ModestProgressBarWidgetPrivate *priv;
347         gboolean determined = FALSE;
348
349         f_receivingOngoing = FALSE;
350         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (self);
351
352         /* If the mail operation is the currently shown one */
353         if (priv->current == mail_op) {
354                 gchar *msg = NULL;
355                 
356                 determined = (state->done > 0 && state->total > 0) && 
357                         !(state->done == 1 && state->total == 100);
358
359                 switch (state->op_type) {
360                 case MODEST_MAIL_OPERATION_TYPE_RECEIVE:                
361                         f_receivingOngoing = TRUE;
362                         if (determined)
363                                 msg = g_strdup_printf(_("mcen_me_receiving"),
364                                                       state->done, state->total); 
365                         else 
366                                 msg = g_strdup(_("mail_me_receiving"));
367                         break;
368                 case MODEST_MAIL_OPERATION_TYPE_SEND:           
369                         if (determined)
370                                 msg = g_strdup_printf(_("mcen_me_sending"), state->done,
371                                                       state->total);
372                         else 
373                                 msg = g_strdup(_("mail_me_sending"));
374                         break;
375                         
376                 case MODEST_MAIL_OPERATION_TYPE_OPEN:           
377                         msg = g_strdup(_("mail_me_opening"));
378                         break;
379                 default:
380                         msg = g_strdup("");
381                 }
382                 
383                 /* If we have byte information use it */
384                 if ((state->bytes_done != 0) && (state->bytes_total != 0))
385                         modest_progress_bar_widget_set_progress (self, msg, 
386                                                                  state->bytes_done, 
387                                                                  state->bytes_total);
388                 else
389                         modest_progress_bar_widget_set_progress (self, msg,
390                                                                  state->done,
391                                                                  state->total);
392                 g_free (msg);
393         }
394 }
395
396 static gboolean
397 progressbar_clean (GtkProgressBar *bar)
398 {
399         gtk_progress_bar_set_fraction (bar, 0);
400         gtk_progress_bar_set_text (bar, 0);
401         return FALSE;
402 }
403
404
405 GtkWidget*
406 modest_progress_bar_widget_new ()
407 {
408         return GTK_WIDGET (g_object_new (MODEST_TYPE_PROGRESS_BAR_WIDGET, NULL));
409 }
410
411
412 void 
413 modest_progress_bar_widget_set_progress (ModestProgressBarWidget *self,
414                                          const gchar *message,
415                                          gint done,
416                                          gint total)
417 {
418         ModestProgressBarWidgetPrivate *priv;
419         
420         g_return_if_fail (MODEST_IS_PROGRESS_BAR_WIDGET(self));
421         g_return_if_fail (done <= total);
422         
423         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (self);
424
425         /* Set progress. Tinymail sometimes returns us 1/100 when it
426            does not have any clue, NOTE that 1/100 could be also a
427            valid progress (we will loose it), but it will be recovered
428            once the done is greater than 1 */
429         if ((done == 0 && total == 0) || 
430             (done == 1 && total == 100)) {
431                 gtk_progress_bar_pulse (GTK_PROGRESS_BAR (priv->progress_bar));
432         } else {
433                 gdouble percent = 0;
434                 if (total != 0) /* Avoid division by zero. */
435                         percent = (gdouble)done/(gdouble)total;
436
437                 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (priv->progress_bar),
438                                                        percent);
439         }
440
441         /* Set text */
442         gtk_progress_bar_set_text (GTK_PROGRESS_BAR (priv->progress_bar), message);
443 }
444