1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
30 #include <glib/gi18n.h>
32 #include <widgets/modest-combo-box.h>
33 #include "modest-progress-bar-widget.h"
35 #include "modest-platform.h"
36 #include "modest-runtime.h"
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);
43 static void modest_progress_bar_add_operation (ModestProgressObject *self,
44 ModestMailOperation *mail_op);
46 static void modest_progress_bar_remove_operation (ModestProgressObject *self,
47 ModestMailOperation *mail_op);
49 static void modest_progress_bar_cancel_current_operation (ModestProgressObject *self);
51 static void modest_progress_bar_cancel_all_operations (ModestProgressObject *self);
53 static guint modest_progress_bar_num_pending_operations (ModestProgressObject *self);
55 static void on_progress_changed (ModestMailOperation *mail_op,
56 ModestMailOperationState *state,
57 ModestProgressBarWidget *self);
59 static gboolean progressbar_clean (GtkProgressBar *bar);
74 typedef struct _ObservableData ObservableData;
75 struct _ObservableData {
77 ModestMailOperation *mail_op;
80 typedef struct _ModestProgressBarWidgetPrivate ModestProgressBarWidgetPrivate;
81 struct _ModestProgressBarWidgetPrivate {
83 ModestMailOperation *current;
86 GtkWidget *progress_bar;
88 #define MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
89 MODEST_TYPE_PROGRESS_BAR_WIDGET, \
90 ModestProgressBarWidgetPrivate))
93 static GtkContainerClass *parent_class = NULL;
95 /* uncomment the following if you have defined any signals */
96 /* static guint signals[LAST_SIGNAL] = {0}; */
99 modest_progress_object_init (gpointer g, gpointer iface_data)
101 ModestProgressObjectIface *klass = (ModestProgressObjectIface *)g;
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;
112 modest_progress_bar_widget_get_type (void)
114 static GType my_type = 0;
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),
125 (GInstanceInitFunc) modest_progress_bar_widget_init,
129 static const GInterfaceInfo modest_progress_object_info =
131 (GInterfaceInitFunc) modest_progress_object_init, /* interface_init */
132 NULL, /* interface_finalize */
133 NULL /* interface_data */
136 my_type = g_type_register_static (GTK_TYPE_VBOX,
137 "ModestProgressBarWidget",
140 g_type_add_interface_static (my_type, MODEST_TYPE_PROGRESS_OBJECT,
141 &modest_progress_object_info);
147 modest_progress_bar_widget_class_init (ModestProgressBarWidgetClass *klass)
149 GObjectClass *gobject_class;
150 gobject_class = (GObjectClass*) klass;
152 parent_class = g_type_class_peek_parent (klass);
153 gobject_class->finalize = modest_progress_bar_widget_finalize;
155 g_type_class_add_private (gobject_class, sizeof(ModestProgressBarWidgetPrivate));
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), */
164 /* g_cclosure_marshal_VOID__VOID, */
165 /* G_TYPE_NONE, 0); */
169 modest_progress_bar_widget_init (ModestProgressBarWidget *self)
172 ModestProgressBarWidgetPrivate *priv;
173 GtkWidget *align = NULL;
177 priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE(self);
180 align = gtk_alignment_new(XALIGN, YALIGN, XSPACE, YSPACE);
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);
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);
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));
200 modest_progress_bar_widget_finalize (GObject *obj)
202 ModestProgressBarWidgetPrivate *priv;
204 priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE(obj);
205 if (priv->observables) {
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);
214 g_slist_free (priv->observables);
215 priv->observables = NULL;
218 G_OBJECT_CLASS(parent_class)->finalize (obj);
223 modest_progress_bar_add_operation (ModestProgressObject *self,
224 ModestMailOperation *mail_op)
226 ModestProgressBarWidget *me = NULL;
227 ObservableData *data = NULL;
228 ModestProgressBarWidgetPrivate *priv = NULL;
230 me = MODEST_PROGRESS_BAR_WIDGET (self);
231 priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (me);
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,
237 G_CALLBACK (on_progress_changed),
239 /* Set curent operation */
240 if (priv->current == NULL) {
241 priv->current = mail_op;
245 /* Call progress_change handler to initialize progress message */
246 modest_progress_bar_widget_set_undetermined_progress (MODEST_PROGRESS_BAR_WIDGET(self), mail_op);
249 /* Add operation to obserbable objects list */
250 priv->observables = g_slist_prepend (priv->observables, data);
254 compare_observable_data (ObservableData *data1, ObservableData *data2)
256 if (data1->mail_op == data2->mail_op)
263 modest_progress_bar_remove_operation (ModestProgressObject *self,
264 ModestMailOperation *mail_op)
266 ModestProgressBarWidget *me;
267 ModestProgressBarWidgetPrivate *priv;
269 ObservableData *tmp_data = NULL;
272 me = MODEST_PROGRESS_BAR_WIDGET (self);
273 priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (me);
275 is_current = (priv->current == mail_op);
278 tmp_data = g_malloc0 (sizeof (ObservableData));
279 tmp_data->mail_op = mail_op;
280 link = g_slist_find_custom (priv->observables,
282 (GCompareFunc) compare_observable_data);
284 /* Remove the item */
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);
290 priv->observables = g_slist_delete_link (priv->observables, link);
291 tmp_data->mail_op = NULL;
295 /* Update the current mail operation */
297 if (priv->observables)
298 priv->current = ((ObservableData *) priv->observables->data)->mail_op;
300 priv->current = NULL;
302 /* Refresh the view */
303 progressbar_clean (GTK_PROGRESS_BAR (priv->progress_bar));
311 modest_progress_bar_num_pending_operations (ModestProgressObject *self)
313 ModestProgressBarWidget *me;
314 ModestProgressBarWidgetPrivate *priv;
316 me = MODEST_PROGRESS_BAR_WIDGET (self);
317 priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (me);
319 return g_slist_length(priv->observables);
323 modest_progress_bar_cancel_current_operation (ModestProgressObject *self)
325 ModestProgressBarWidget *me;
326 ModestProgressBarWidgetPrivate *priv;
328 me = MODEST_PROGRESS_BAR_WIDGET (self);
329 priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (me);
331 if (priv->current == NULL) return;
333 modest_mail_operation_cancel (priv->current);
337 modest_progress_bar_cancel_all_operations (ModestProgressObject *self)
339 ModestProgressBarWidget *me;
340 ModestProgressBarWidgetPrivate *priv;
342 me = MODEST_PROGRESS_BAR_WIDGET (self);
343 priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (me);
345 /* Cancel all the mail operations */
346 modest_mail_operation_queue_cancel_all (modest_runtime_get_mail_operation_queue ());
350 on_progress_changed (ModestMailOperation *mail_op,
351 ModestMailOperationState *state,
352 ModestProgressBarWidget *self)
354 ModestProgressBarWidgetPrivate *priv;
355 gboolean determined = FALSE;
357 priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (self);
359 /* If the mail operation is the currently shown one */
360 if (priv->current == mail_op) {
363 determined = (state->done > 0 && state->total > 1) &&
364 !(state->done == 1 && state->total == 100);
366 switch (state->op_type) {
367 case MODEST_MAIL_OPERATION_TYPE_RECEIVE:
369 msg = g_strdup_printf(_("mcen_me_receiving"),
370 state->done, state->total);
372 msg = g_strdup(_("mail_me_receiving"));
374 case MODEST_MAIL_OPERATION_TYPE_SEND:
376 msg = g_strdup_printf(_("mcen_me_sending"), state->done,
379 msg = g_strdup(_("mail_me_sending"));
382 case MODEST_MAIL_OPERATION_TYPE_OPEN:
383 msg = g_strdup(_("mail_me_opening"));
389 /* If we have byte information use it */
390 if ((state->bytes_done != 0) && (state->bytes_total != 0))
391 modest_progress_bar_widget_set_progress (self, msg,
395 modest_progress_bar_widget_set_progress (self, msg,
403 progressbar_clean (GtkProgressBar *bar)
405 gtk_progress_bar_set_fraction (bar, 0);
406 gtk_progress_bar_set_text (bar, 0);
412 modest_progress_bar_widget_new ()
414 return GTK_WIDGET (g_object_new (MODEST_TYPE_PROGRESS_BAR_WIDGET, NULL));
419 modest_progress_bar_widget_set_progress (ModestProgressBarWidget *self,
420 const gchar *message,
424 ModestProgressBarWidgetPrivate *priv;
425 gboolean determined = FALSE;
427 g_return_if_fail (MODEST_IS_PROGRESS_BAR_WIDGET(self));
428 g_return_if_fail (done <= total);
430 priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (self);
434 /* Set progress. Tinymail sometimes returns us 1/100 when it
435 does not have any clue, NOTE that 1/100 could be also a
436 valid progress (we will loose it), but it will be recovered
437 once the done is greater than 1 */
438 determined = (done > 0 && total > 1) &&
439 !(done == 1 && total == 100);
440 /* if ((done == 0 && total == 0) || */
441 /* (done == 1 && total == 100)) { */
443 gtk_progress_bar_set_bar_style (GTK_PROGRESS_BAR (priv->progress_bar), /* Deprecated */
444 GTK_PROGRESS_CONTINUOUS);
445 gtk_progress_bar_pulse (GTK_PROGRESS_BAR (priv->progress_bar));
446 /* printf ("debug: %s:\n undetermined progress (%i) changed (%i/%i) : %i\n", __FUNCTION__, */
447 /* (int) priv->progress_bar, */
453 if (total != 0) /* Avoid division by zero. */
454 percent = (gdouble)done/(gdouble)total;
456 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (priv->progress_bar),
459 /* printf ("debug: %s:\n determined progress (%i) changed (%i/%i) : %i\n", __FUNCTION__, */
460 /* (int) priv->progress_bar, */
467 gtk_progress_bar_set_text (GTK_PROGRESS_BAR (priv->progress_bar), message);
472 modest_progress_bar_widget_set_undetermined_progress (ModestProgressBarWidget *self,
473 ModestMailOperation *mail_op)
475 ModestMailOperationState *state = NULL;
477 state = g_malloc0(sizeof(ModestMailOperationState));
480 state->op_type = modest_mail_operation_get_type_operation (mail_op);
481 on_progress_changed (mail_op, state, self);