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