These changes are for reenabling build of gnome port.
[modest] / src / gnome / modest-gnome-info-bar.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
31 #include "modest-gnome-info-bar.h"
32 #include <gtk/gtkprogressbar.h>
33 #include <gtk/gtkstatusbar.h>
34 /* include other impl specific header files */
35
36 /* 'private'/'protected' functions */
37 static void modest_gnome_info_bar_class_init (ModestGnomeInfoBarClass *klass);
38 static void modest_gnome_info_bar_init       (ModestGnomeInfoBar *obj);
39 static void modest_gnome_info_bar_finalize   (GObject *obj);
40
41 static void modest_gnome_info_bar_add_operation    (ModestProgressObject *self,
42                                                     ModestMailOperation  *mail_op);
43
44 static void modest_gnome_info_bar_remove_operation (ModestProgressObject *self,
45                                                     ModestMailOperation  *mail_op);
46
47 static void on_progress_changed                    (ModestMailOperation  *mail_op, 
48                                                     ModestMailOperationState *state,
49                                                     ModestGnomeInfoBar *self);
50
51 static gboolean     progressbar_clean        (GtkProgressBar *bar);
52 static gboolean     statusbar_clean          (GtkStatusbar *bar);
53
54 /* list my signals  */
55 enum {
56         /* MY_SIGNAL_1, */
57         /* MY_SIGNAL_2, */
58         LAST_SIGNAL
59 };
60
61 typedef struct _ObservableData ObservableData;
62 struct _ObservableData {
63         guint signal_handler;
64         ModestMailOperation *mail_op;
65 };
66
67 typedef struct _ModestGnomeInfoBarPrivate ModestGnomeInfoBarPrivate;
68 struct _ModestGnomeInfoBarPrivate {
69         GSList              *observables;
70         ModestMailOperation *current;
71
72         GtkWidget *status_bar;
73         GtkWidget *progress_bar;
74
75         guint status_bar_timeout;
76         guint progress_bar_timeout;
77 };
78
79 #define MODEST_GNOME_INFO_BAR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
80                                               MODEST_TYPE_GNOME_INFO_BAR, \
81                                               ModestGnomeInfoBarPrivate))
82 /* globals */
83 static GtkHBoxClass *parent_class = NULL;
84
85 /* uncomment the following if you have defined any signals */
86 /* static guint signals[LAST_SIGNAL] = {0}; */
87
88 static void
89 modest_progress_object_init (gpointer g, gpointer iface_data)
90 {
91         ModestProgressObjectIface *klass = (ModestProgressObjectIface *)g;
92
93         klass->add_operation_func = modest_gnome_info_bar_add_operation;
94         klass->remove_operation_func = modest_gnome_info_bar_remove_operation;
95 }
96
97 GType
98 modest_gnome_info_bar_get_type (void)
99 {
100         static GType my_type = 0;
101         if (!my_type) {
102                 static const GTypeInfo my_info = {
103                         sizeof(ModestGnomeInfoBarClass),
104                         NULL,           /* base init */
105                         NULL,           /* base finalize */
106                         (GClassInitFunc) modest_gnome_info_bar_class_init,
107                         NULL,           /* class finalize */
108                         NULL,           /* class data */
109                         sizeof(ModestGnomeInfoBar),
110                         1,              /* n_preallocs */
111                         (GInstanceInitFunc) modest_gnome_info_bar_init,
112                         NULL
113                 };
114
115                 static const GInterfaceInfo modest_progress_object_info = 
116                 {
117                   (GInterfaceInitFunc) modest_progress_object_init, /* interface_init */
118                   NULL,         /* interface_finalize */
119                   NULL          /* interface_data */
120                 };
121
122                 my_type = g_type_register_static (GTK_TYPE_HBOX,
123                                                   "ModestGnomeInfoBar",
124                                                   &my_info, 0);
125
126                 g_type_add_interface_static (my_type, MODEST_TYPE_PROGRESS_OBJECT, 
127                                              &modest_progress_object_info);
128         }
129         return my_type;
130 }
131
132 static void
133 modest_gnome_info_bar_class_init (ModestGnomeInfoBarClass *klass)
134 {
135         GObjectClass *gobject_class;
136         gobject_class = (GObjectClass*) klass;
137
138         parent_class            = g_type_class_peek_parent (klass);
139         gobject_class->finalize = modest_gnome_info_bar_finalize;
140
141         g_type_class_add_private (gobject_class, sizeof(ModestGnomeInfoBarPrivate));
142 }
143
144 static void
145 modest_gnome_info_bar_init (ModestGnomeInfoBar *obj)
146 {
147         ModestGnomeInfoBarPrivate *priv = MODEST_GNOME_INFO_BAR_GET_PRIVATE(obj);
148
149         priv->observables = NULL;
150         priv->current = NULL;
151
152         /* Status bar */
153         priv->status_bar = gtk_statusbar_new ();
154         gtk_statusbar_set_has_resize_grip (GTK_STATUSBAR (priv->status_bar), FALSE);
155
156         /* Progress bar */
157         priv->progress_bar = gtk_progress_bar_new ();
158         gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (priv->progress_bar), 1.0);
159         gtk_progress_bar_set_ellipsize (GTK_PROGRESS_BAR (priv->progress_bar),
160                                         PANGO_ELLIPSIZE_END);
161
162         /* Timeouts */
163         priv->status_bar_timeout = 0;
164         priv->progress_bar_timeout = 0;
165
166         /* Pack */
167         gtk_box_pack_start (GTK_BOX (obj), priv->status_bar, TRUE, TRUE, 0);
168         gtk_box_pack_start (GTK_BOX (obj), priv->progress_bar, FALSE, FALSE, 0);
169 }
170
171 static void
172 destroy_observable_data (ObservableData *data)
173 {
174         g_signal_handler_disconnect (data->mail_op, data->signal_handler);
175         g_object_unref (data->mail_op);
176 }
177
178 static void
179 modest_gnome_info_bar_finalize (GObject *obj)
180 {
181         ModestGnomeInfoBarPrivate *priv;
182
183         priv = MODEST_GNOME_INFO_BAR_GET_PRIVATE(obj);
184         if (priv->observables) {
185                 GSList *tmp;
186
187                 for (tmp = priv->observables; tmp; tmp = g_slist_next (tmp)) {
188                         destroy_observable_data ((ObservableData *) tmp->data);
189                         g_free (tmp->data);
190                 }
191                 g_slist_free (priv->observables);
192                 priv->observables = NULL;
193         }
194
195         if (priv->status_bar_timeout > 0) {
196                 g_source_remove (priv->status_bar_timeout);
197                 priv->status_bar_timeout = 0;
198         }
199
200         if (priv->progress_bar_timeout > 0) {
201                 g_source_remove (priv->progress_bar_timeout);
202                 priv->progress_bar_timeout = 0;
203         }
204
205         G_OBJECT_CLASS(parent_class)->finalize (obj);
206 }
207
208 GtkWidget *
209 modest_gnome_info_bar_new (void)
210 {
211         return GTK_WIDGET (g_object_new (MODEST_TYPE_GNOME_INFO_BAR, NULL));
212 }
213
214 static void 
215 modest_gnome_info_bar_add_operation (ModestProgressObject *self,
216                                      ModestMailOperation  *mail_op)
217 {
218         ModestGnomeInfoBar *me;
219         ObservableData *data;
220         ModestGnomeInfoBarPrivate *priv;
221
222         me = MODEST_GNOME_INFO_BAR (self);
223         priv = MODEST_GNOME_INFO_BAR_GET_PRIVATE (me);
224
225         data = g_malloc0 (sizeof (ObservableData));
226         data->mail_op = g_object_ref (mail_op);
227         data->signal_handler = g_signal_connect (data->mail_op, 
228                                                  "progress-changed",
229                                                  G_CALLBACK (on_progress_changed),
230                                                  me);
231
232         if (priv->observables == NULL) {
233                 priv->current = mail_op;
234         }
235         priv->observables = g_slist_append (priv->observables, data);
236 }
237
238 static gint
239 compare_observable_data (ObservableData *data1, ObservableData *data2)
240 {
241         if (data1->mail_op == data2->mail_op)
242                 return 0;
243         else 
244                 return 1;
245 }
246
247 static void 
248 modest_gnome_info_bar_remove_operation (ModestProgressObject *self,
249                                         ModestMailOperation  *mail_op)
250 {
251         ModestGnomeInfoBar *me;
252         ModestGnomeInfoBarPrivate *priv;
253         GSList *link;
254
255         me = MODEST_GNOME_INFO_BAR (self);
256         priv = MODEST_GNOME_INFO_BAR_GET_PRIVATE (me);
257
258         link = g_slist_find_custom (priv->observables,
259                                     mail_op,
260                                     (GCompareFunc) compare_observable_data);
261
262         /* Remove the item */
263         if (link) {
264                 priv->observables = g_slist_remove_link (priv->observables, link);
265                 destroy_observable_data ((ObservableData *) link->data);
266         }
267
268         /* Update the current mail operation */
269         if (priv->current == mail_op) {
270                 if (priv->observables)
271                         priv->current = ((ObservableData *) priv->observables->data)->mail_op;
272                 else
273                         priv->current = NULL;
274
275                 /* Refresh the view */
276                 progressbar_clean (GTK_PROGRESS_BAR (priv->progress_bar));
277         }
278 }
279
280 static void 
281 on_progress_changed (ModestMailOperation  *mail_op, 
282                      ModestMailOperationState *state,
283                      ModestGnomeInfoBar *self)
284 {
285         ModestGnomeInfoBarPrivate *priv;
286
287         priv = MODEST_GNOME_INFO_BAR_GET_PRIVATE (self);
288
289         /* If the mail operation is the currently shown one */
290         if (priv->current == mail_op) {
291                 gchar *msg = NULL;
292
293                 msg = g_strdup_printf ("Mail operation %d of %d",
294                                        modest_mail_operation_get_task_done (mail_op),
295                                        modest_mail_operation_get_task_total (mail_op));
296                 modest_gnome_info_bar_set_message (self, msg);
297                 g_free (msg);
298         }
299 }
300
301 static gboolean
302 progressbar_clean (GtkProgressBar *bar)
303 {
304         gtk_progress_bar_set_fraction (bar, 0);
305         gtk_progress_bar_set_text (bar, 0);
306         return FALSE;
307 }
308
309 static gboolean
310 statusbar_clean (GtkStatusbar *bar)
311 {
312         gtk_statusbar_push (bar, 0, "");
313         return FALSE;
314 }
315
316 void 
317 modest_gnome_info_bar_set_message    (ModestGnomeInfoBar *self,
318                                       const gchar *message)
319 {
320         ModestGnomeInfoBarPrivate *priv;
321
322         priv = MODEST_GNOME_INFO_BAR_GET_PRIVATE (self);
323
324         /* Set a message. Clean it after 2.5 seconds */
325         gtk_statusbar_push (GTK_STATUSBAR (priv->status_bar), 0, message);
326         priv->status_bar_timeout = g_timeout_add (2500, 
327                                                   (GSourceFunc) statusbar_clean, 
328                                                   priv->status_bar);
329 }
330
331 void 
332 modest_gnome_info_bar_set_progress   (ModestGnomeInfoBar *self,
333                                       const gchar *message,
334                                       gint done,
335                                       gint total)
336 {
337         ModestGnomeInfoBarPrivate *priv;
338
339         priv = MODEST_GNOME_INFO_BAR_GET_PRIVATE (self);
340
341         /* Set progress */
342         if (total != 0)
343                 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (priv->progress_bar),
344                                                (gdouble)done/(gdouble)total);
345         else
346                 gtk_progress_bar_pulse (GTK_PROGRESS_BAR (priv->progress_bar));
347
348         /* Set text */
349         gtk_progress_bar_set_text (GTK_PROGRESS_BAR (priv->progress_bar), message);
350 }