* Add a new widget ModestProgressBarWidget to work as observer
[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 /* list my signals  */
41 /* enum { */
42 /*      LAST_SIGNAL */
43 /* }; */
44
45 typedef struct _ModestProgressBarWidgetPrivate ModestProgressBarWidgetPrivate;
46 struct _ModestProgressBarWidgetPrivate {
47         GtkWidget *bar;
48
49 };
50 #define MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
51                                                  MODEST_TYPE_PROGRESS_BAR_WIDGET, \
52                                                  ModestProgressBarWidgetPrivate))
53 /* globals */
54 static GtkContainerClass *parent_class = NULL;
55
56 /* uncomment the following if you have defined any signals */
57 /* static guint signals[LAST_SIGNAL] = {0}; */
58
59 GType
60 modest_progress_bar_widget_get_type (void)
61 {
62         static GType my_type = 0;
63         if (!my_type) {
64                 static const GTypeInfo my_info = {
65                         sizeof(ModestProgressBarWidgetClass),
66                         NULL,           /* base init */
67                         NULL,           /* base finalize */
68                         (GClassInitFunc) modest_progress_bar_widget_class_init,
69                         NULL,           /* class finalize */
70                         NULL,           /* class data */
71                         sizeof(ModestProgressBarWidget),
72                         1,              /* n_preallocs */
73                         (GInstanceInitFunc) modest_progress_bar_widget_init,
74                         NULL
75                 };
76                 my_type = g_type_register_static (GTK_TYPE_VBOX,
77                                                   "ModestProgressBarWidget",
78                                                   &my_info, 0);
79         }
80         return my_type;
81 }
82
83 static void
84 modest_progress_bar_widget_class_init (ModestProgressBarWidgetClass *klass)
85 {
86         GObjectClass *gobject_class;
87         gobject_class = (GObjectClass*) klass;
88
89         parent_class            = g_type_class_peek_parent (klass);
90         gobject_class->finalize = modest_progress_bar_widget_finalize;
91
92         g_type_class_add_private (gobject_class, sizeof(ModestProgressBarWidgetPrivate));
93
94         /* signal definitions go here, e.g.: */
95 /*      signals[DATA_CHANGED_SIGNAL] = */
96 /*              g_signal_new ("data_changed", */
97 /*                            G_TYPE_FROM_CLASS (klass), */
98 /*                            G_SIGNAL_RUN_FIRST, */
99 /*                            G_STRUCT_OFFSET(ModestProgressBarWidgetClass, data_changed), */
100 /*                            NULL, NULL, */
101 /*                            g_cclosure_marshal_VOID__VOID, */
102 /*                            G_TYPE_NONE, 0); */
103 }
104
105 static void
106 modest_progress_bar_widget_init (ModestProgressBarWidget *obj)
107 {
108         ModestProgressBarWidgetPrivate *priv;
109         
110         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE(obj); 
111         priv->bar = NULL;
112 }
113
114
115 static void
116 modest_progress_bar_widget_finalize (GObject *obj)
117 {
118         G_OBJECT_CLASS(parent_class)->finalize (obj);
119 }
120
121 GtkWidget*
122 modest_progress_bar_widget_new ()
123 {
124         GObject *obj;
125         ModestProgressBarWidget *self;
126         ModestProgressBarWidgetPrivate *priv;
127         GtkRequisition req;
128         
129
130         obj = g_object_new(MODEST_TYPE_PROGRESS_BAR_WIDGET, NULL);
131         self = MODEST_PROGRESS_BAR_WIDGET(obj);
132         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE(self);
133         
134         /* Build GtkProgressBar */
135         priv->bar = gtk_progress_bar_new ();            
136         req.width = 50;
137         req.height = 64;
138         gtk_progress_set_text_alignment (GTK_PROGRESS (priv->bar), 0.0, 0.5);
139         gtk_progress_bar_set_ellipsize (GTK_PROGRESS_BAR (priv->bar), PANGO_ELLIPSIZE_END);
140         gtk_widget_size_request (priv->bar, &req);
141         
142         /* Add progress bar widget */
143         gtk_box_pack_start (GTK_BOX(self), priv->bar, TRUE, TRUE, 2);
144         gtk_widget_show_all (GTK_WIDGET(self));
145
146         return GTK_WIDGET(self);
147 }
148
149
150 void 
151 modest_progress_bar_widget_set_status (ModestProgressBarWidget *self, 
152                                        guint id)
153 {
154         ModestProgressBarWidgetPrivate *priv;
155         gchar *status = NULL;
156         gboolean determined = FALSE;
157         guint d1 = 0, d2 = 0;
158
159         priv = MODEST_PROGRESS_BAR_WIDGET_GET_PRIVATE(self);
160
161         switch (id) {
162         case STATUS_RECEIVING:          
163                 if (determined)
164                         status = g_strdup_printf(_("mcen_me_receiving"), d1, d2);
165                 else 
166                         status = g_strdup(_("mail_me_receiving"));
167                 break;
168         case STATUS_SENDING:            
169                 if (determined)
170                         status = g_strdup_printf(_("mcen_me_sending"), d1, d2);
171                 else 
172                         status = g_strdup(_("mail_me_sending"));
173                 break;
174
175         case STATUS_OPENING:            
176                                 status = g_strdup(_("mail_me_opening"));
177                 break;
178         default:
179                 g_return_if_reached();
180         }
181         
182         gtk_progress_bar_set_text (GTK_PROGRESS_BAR(priv->bar), status);
183         gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR(priv->bar), 0.5);
184         
185         /* free*/
186         g_free(status);
187 }