* Implement Observer pattern using ModestProgressBarWidget.
[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
36 /* 'private'/'protected' functions */
37 static void modest_progress_bar_widget_class_init (ModestProgressBarWidgetClass *klass);
38 static void modest_progress_bar_widget_init       (ModestProgressBarWidget *obj);
39 static void modest_progress_bar_widget_finalize   (GObject *obj);
40
41 static void modest_progress_bar_add_operation    (ModestProgressObject *self,
42                                                     ModestMailOperation  *mail_op);
43
44 static void modest_progress_bar_remove_operation (ModestProgressObject *self,
45                                                     ModestMailOperation  *mail_op);
46
47 static void on_progress_changed                    (ModestMailOperation  *mail_op, 
48                                                     ModestProgressBarWidget *self);
49
50 static gboolean     progressbar_clean        (GtkProgressBar *bar);
51
52 /* list my signals  */
53 /* enum { */
54 /*      LAST_SIGNAL */
55 /* }; */
56
57 typedef struct _ObservableData ObservableData;
58 struct _ObservableData {
59         guint signal_handler;
60         ModestMailOperation *mail_op;
61 };
62
63 typedef struct _ModestProgressBarWidgetPrivate ModestProgressBarWidgetPrivate;
64 struct _ModestProgressBarWidgetPrivate {
65         GSList              *observables;
66         ModestMailOperation *current;
67
68         GtkWidget *progress_bar;
69
70 };
71 #define MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
72                                                  MODEST_TYPE_PROGRESS_BAR_WIDGET, \
73                                                  ModestProgressBarWidgetPrivate))
74 /* globals */
75 static GtkContainerClass *parent_class = NULL;
76
77 /* uncomment the following if you have defined any signals */
78 /* static guint signals[LAST_SIGNAL] = {0}; */
79
80 static void
81 modest_progress_object_init (gpointer g, gpointer iface_data)
82 {
83         ModestProgressObjectIface *klass = (ModestProgressObjectIface *)g;
84
85         klass->add_operation_func = modest_progress_bar_add_operation;
86         klass->remove_operation_func = modest_progress_bar_remove_operation;
87 }
88
89
90 GType
91 modest_progress_bar_widget_get_type (void)
92 {
93         static GType my_type = 0;
94         if (!my_type) {
95                 static const GTypeInfo my_info = {
96                         sizeof(ModestProgressBarWidgetClass),
97                         NULL,           /* base init */
98                         NULL,           /* base finalize */
99                         (GClassInitFunc) modest_progress_bar_widget_class_init,
100                         NULL,           /* class finalize */
101                         NULL,           /* class data */
102                         sizeof(ModestProgressBarWidget),
103                         1,              /* n_preallocs */
104                         (GInstanceInitFunc) modest_progress_bar_widget_init,
105                         NULL
106                 };
107
108                 static const GInterfaceInfo modest_progress_object_info = 
109                 {
110                   (GInterfaceInitFunc) modest_progress_object_init, /* interface_init */
111                   NULL,         /* interface_finalize */
112                   NULL          /* interface_data */
113                 };
114
115                 my_type = g_type_register_static (GTK_TYPE_VBOX,
116                                                   "ModestProgressBarWidget",
117                                                   &my_info, 0);
118
119                 g_type_add_interface_static (my_type, MODEST_TYPE_PROGRESS_OBJECT, 
120                                              &modest_progress_object_info);
121         }
122         return my_type;
123 }
124
125 static void
126 modest_progress_bar_widget_class_init (ModestProgressBarWidgetClass *klass)
127 {
128         GObjectClass *gobject_class;
129         gobject_class = (GObjectClass*) klass;
130
131         parent_class            = g_type_class_peek_parent (klass);
132         gobject_class->finalize = modest_progress_bar_widget_finalize;
133
134         g_type_class_add_private (gobject_class, sizeof(ModestProgressBarWidgetPrivate));
135
136         /* signal definitions go here, e.g.: */
137 /*      signals[DATA_CHANGED_SIGNAL] = */
138 /*              g_signal_new ("data_changed", */
139 /*                            G_TYPE_FROM_CLASS (klass), */
140 /*                            G_SIGNAL_RUN_FIRST, */
141 /*                            G_STRUCT_OFFSET(ModestProgressBarWidgetClass, data_changed), */
142 /*                            NULL, NULL, */
143 /*                            g_cclosure_marshal_VOID__VOID, */
144 /*                            G_TYPE_NONE, 0); */
145 }
146
147 static void
148 modest_progress_bar_widget_init (ModestProgressBarWidget *obj)
149 {
150         ModestProgressBarWidgetPrivate *priv;
151         
152         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE(obj); 
153         priv->progress_bar = NULL;
154 }
155
156 static void
157 destroy_observable_data (ObservableData *data)
158 {
159         g_signal_handler_disconnect (data->mail_op, data->signal_handler);
160         g_object_unref (data->mail_op);
161 }
162
163 static void
164 modest_progress_bar_widget_finalize (GObject *obj)
165 {
166         G_OBJECT_CLASS(parent_class)->finalize (obj);
167 }
168
169
170 static void 
171 modest_progress_bar_add_operation (ModestProgressObject *self,
172                                    ModestMailOperation  *mail_op)
173 {
174         ModestProgressBarWidget *me;
175         ObservableData *data;
176         ModestProgressBarWidgetPrivate *priv;
177         
178         me = MODEST_PROGRESS_BAR_WIDGET (self);
179         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (me);
180
181         data = g_malloc0 (sizeof (ObservableData));
182         data->mail_op = g_object_ref (mail_op);
183         data->signal_handler = g_signal_connect (data->mail_op, 
184                                                  "progress-changed",
185                                                  G_CALLBACK (on_progress_changed),
186                                                  me);
187
188         if (priv->observables == NULL) {
189                 priv->current = mail_op;
190         }
191         priv->observables = g_slist_append (priv->observables, data);
192 }
193
194 static gint
195 compare_observable_data (ObservableData *data1, ObservableData *data2)
196 {
197         if (data1->mail_op == data2->mail_op)
198                 return 0;
199         else 
200                 return 1;
201 }
202
203 static void 
204 modest_progress_bar_remove_operation (ModestProgressObject *self,
205                                         ModestMailOperation  *mail_op)
206 {
207         ModestProgressBarWidget *me;
208         ModestProgressBarWidgetPrivate *priv;
209         GSList *link;
210
211         me = MODEST_PROGRESS_BAR_WIDGET (self);
212         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (me);
213
214         link = g_slist_find_custom (priv->observables,
215                                     mail_op,
216                                     (GCompareFunc) compare_observable_data);
217         
218         /* Remove the item */
219         if (link) {
220                 priv->observables = g_slist_remove_link (priv->observables, link);
221                 destroy_observable_data ((ObservableData *) link->data);
222         }
223         
224         /* Update the current mail operation */
225         if (priv->current == mail_op) {
226                 if (priv->observables)
227                         priv->current = ((ObservableData *) priv->observables->data)->mail_op;
228                 else
229                         priv->current = NULL;
230
231                 /* Refresh the view */
232                 progressbar_clean (GTK_PROGRESS_BAR (priv->progress_bar));
233         }
234 }
235
236 static void 
237 on_progress_changed (ModestMailOperation  *mail_op, 
238                      ModestProgressBarWidget *self)
239 {
240         ModestProgressBarWidgetPrivate *priv;
241         gboolean determined = FALSE;
242         guint id = 0;
243
244         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (self);
245
246         /* If the mail operation is the currently shown one */
247         if (priv->current == mail_op) {
248                 gchar *msg = NULL;
249                 gint done = modest_mail_operation_get_task_done (mail_op);
250                 gint total = modest_mail_operation_get_task_total (mail_op);
251
252                 switch (id) {
253                 case STATUS_RECEIVING:          
254                         if (determined)
255                                 msg = g_strdup_printf(_("mcen_me_receiving"), done, total);
256                         else 
257                                 msg = g_strdup(_("mail_me_receiving"));
258                         break;
259                 case STATUS_SENDING:            
260                         if (determined)
261                                 msg = g_strdup_printf(_("mcen_me_sending"), done, total);
262                         else 
263                                 msg = g_strdup(_("mail_me_sending"));
264                         break;
265                         
266                 case STATUS_OPENING:            
267                         msg = g_strdup(_("mail_me_opening"));
268                         break;
269                 default:
270                         g_return_if_reached();
271                 }
272                 
273                 modest_progress_bar_widget_set_progress (self, msg, done, total);
274                 g_free (msg);
275         }
276 }
277
278 static gboolean
279 progressbar_clean (GtkProgressBar *bar)
280 {
281         gtk_progress_bar_set_fraction (bar, 0);
282         gtk_progress_bar_set_text (bar, 0);
283         return FALSE;
284 }
285
286
287 GtkWidget*
288 modest_progress_bar_widget_new ()
289 {
290         GObject *obj;
291         ModestProgressBarWidget *self;
292         ModestProgressBarWidgetPrivate *priv;
293         GtkRequisition req;
294         
295
296         obj = g_object_new(MODEST_TYPE_PROGRESS_BAR_WIDGET, NULL);
297         self = MODEST_PROGRESS_BAR_WIDGET(obj);
298         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE(self);
299         
300         /* Build GtkProgressBar */
301         priv->progress_bar = gtk_progress_bar_new ();           
302         req.width = 50;
303         req.height = 64;
304         gtk_progress_set_text_alignment (GTK_PROGRESS (priv->progress_bar), 0.0, 0.5);
305         gtk_progress_bar_set_ellipsize (GTK_PROGRESS_BAR (priv->progress_bar), PANGO_ELLIPSIZE_END);
306         gtk_widget_size_request (priv->progress_bar, &req);
307         
308         /* Add progress bar widget */
309         gtk_box_pack_start (GTK_BOX(self), priv->progress_bar, TRUE, TRUE, 2);
310         gtk_widget_show_all (GTK_WIDGET(self));
311
312         return GTK_WIDGET(self);
313 }
314
315
316 void 
317 modest_progress_bar_widget_set_progress   (ModestProgressBarWidget *self,
318                                            const gchar *message,
319                                            gint done,
320                                            gint total)
321 {
322         ModestProgressBarWidgetPrivate *priv;
323         
324         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE (self);
325         
326         /* Set progress */
327         if (total != 0)
328                 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (priv->progress_bar),
329                                                (gdouble)done/(gdouble)total);
330         else
331                 gtk_progress_bar_pulse (GTK_PROGRESS_BAR (priv->progress_bar));
332
333         /* Set text */
334         gtk_progress_bar_set_text (GTK_PROGRESS_BAR (priv->progress_bar), message);
335 }
336