Modified webpage: now tinymail repository is in gitorious.
[modest] / src / gtk / modest-shell-banner.c
1 /* Copyright (c) 2008, 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 <modest-shell-banner.h>
31 #include <modest-gtk-window-mgr.h>
32 #include <modest-runtime.h>
33 #include <glib-object.h>
34
35 #define _DEFAULT_TIMEOUT 5000
36
37 /* 'private'/'protected' functions */
38 static void modest_shell_banner_class_init  (gpointer klass, gpointer class_data);
39 static void modest_shell_banner_instance_init (GTypeInstance *instance, gpointer g_class);
40 static void modest_shell_banner_dispose     (GObject *obj);
41 static void modest_shell_banner_finalize     (GObject *obj);
42 static gboolean _on_timeout (ModestShellBanner *self);
43
44 typedef struct _ModestShellBannerPrivate ModestShellBannerPrivate;
45 struct _ModestShellBannerPrivate {
46         GtkWidget *label;
47         guint timeout_id;
48 };
49 #define MODEST_SHELL_BANNER_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
50                                                                          MODEST_TYPE_SHELL_BANNER, \
51                                                                          ModestShellBannerPrivate))
52
53 /* globals */
54 static GtkFrameClass *parent_class = NULL;
55
56 /* uncomment the following if you have defined any signals */
57 /* static guint signals[LAST_SIGNAL] = {0}; */
58
59 /************************************************************************/
60
61 GType
62 modest_shell_banner_get_type (void)
63 {
64         static GType my_type = 0;
65         if (!my_type) {
66                 static const GTypeInfo my_info = {
67                         sizeof(ModestShellBannerClass),
68                         NULL,           /* base init */
69                         NULL,           /* base finalize */
70                         (GClassInitFunc) modest_shell_banner_class_init,
71                         NULL,           /* class finalize */
72                         NULL,           /* class data */
73                         sizeof(ModestShellBanner),
74                         1,              /* n_preallocs */
75                         (GInstanceInitFunc) modest_shell_banner_instance_init,
76                         NULL
77                 };
78                 my_type = g_type_register_static (GTK_TYPE_FRAME,
79                                                   "ModestShellBanner",
80                                                   &my_info, 0);
81         }
82         return my_type;
83 }
84
85 static void
86 modest_shell_banner_class_init (gpointer klass, gpointer class_data)
87 {
88         GObjectClass *gobject_class;
89         gobject_class = (GObjectClass*) klass;
90
91         parent_class            = g_type_class_peek_parent (klass);
92         gobject_class->dispose  = modest_shell_banner_dispose;
93         gobject_class->finalize  = modest_shell_banner_finalize;
94
95         g_type_class_add_private (gobject_class, sizeof(ModestShellBannerPrivate));
96         
97
98 }
99
100 static void
101 modest_shell_banner_dispose (GObject *obj)
102 {
103         ModestShellBannerPrivate *priv;
104         priv = MODEST_SHELL_BANNER_GET_PRIVATE (obj);
105         if (priv->timeout_id) {
106                 g_source_remove (priv->timeout_id);
107                 priv->timeout_id = 0;
108         }
109         G_OBJECT_CLASS(parent_class)->dispose (obj);
110 }
111
112 static void
113 modest_shell_banner_finalize (GObject *obj)
114 {
115         G_OBJECT_CLASS(parent_class)->finalize (obj);
116 }
117
118 static void
119 modest_shell_banner_instance_init (GTypeInstance *instance, gpointer g_class)
120 {
121         ModestShellBannerPrivate *priv;
122         GtkWidget *hbox;
123
124         ModestShellBanner *self = (ModestShellBanner *) instance;
125         priv = MODEST_SHELL_BANNER_GET_PRIVATE (self);
126
127         gtk_frame_set_shadow_type (GTK_FRAME (self), GTK_SHADOW_OUT);
128         priv->label = gtk_label_new (NULL);
129         gtk_misc_set_alignment (GTK_MISC (priv->label), 0.5, 0.5);
130         gtk_widget_show (priv->label);
131
132         hbox = gtk_hbox_new (FALSE, 0);
133         gtk_box_pack_start (GTK_BOX (hbox), priv->label, TRUE, TRUE, 0);
134         gtk_widget_show (hbox);
135
136         gtk_container_add (GTK_CONTAINER (self), hbox);
137 }
138
139 GtkWidget *
140 modest_shell_banner_new (GtkWidget *parent)
141 {
142         return modest_shell_banner_new_with_timeout (parent, _DEFAULT_TIMEOUT);
143 }
144
145 GtkWidget *
146 modest_shell_banner_new_with_timeout (GtkWidget *parent, gint timeout)
147 {
148         GtkWidget *self;
149         GtkWidget *toplevel;
150         ModestShellBannerPrivate *priv;
151
152         self = g_object_new (MODEST_TYPE_SHELL_BANNER, NULL);
153         priv = MODEST_SHELL_BANNER_GET_PRIVATE (self);
154
155         if (parent)
156                 toplevel = gtk_widget_get_toplevel (parent);
157         else
158                 toplevel = NULL;
159
160         if (toplevel == NULL) {
161                 GtkWidget *shell;
162                 GList *toplevels;
163
164                 shell = modest_gtk_window_mgr_get_shell (MODEST_GTK_WINDOW_MGR (modest_runtime_get_window_mgr ()));
165
166                 toplevels = gtk_window_list_toplevels ();
167                 while (toplevels) {
168                         if (gtk_window_has_toplevel_focus (GTK_WINDOW (toplevels->data))) {
169                                 toplevel = toplevels->data;
170                                 break;
171                         }
172                         toplevels = g_list_next (toplevels);
173                 }
174
175                 if (toplevel == NULL)
176                         toplevel = shell;
177         }
178
179         if (MODEST_IS_SHELL (toplevel)) {
180                 modest_shell_add_banner (MODEST_SHELL (toplevel), MODEST_SHELL_BANNER (self));
181         } else if (GTK_IS_DIALOG (toplevel)) {
182                 gtk_container_add (GTK_CONTAINER (GTK_DIALOG (toplevel)->vbox), self);
183                 gtk_container_child_set (GTK_CONTAINER (GTK_DIALOG (toplevel)->vbox), self,
184                                          "position", 0, NULL);
185         }
186         
187         gtk_widget_show (self);
188
189         if (timeout != 0)
190                 priv->timeout_id = g_timeout_add (timeout, (GSourceFunc) _on_timeout, self);
191         else
192                 priv->timeout_id = 0;
193
194         return self;
195 }
196
197 void
198 modest_shell_banner_set_icon (ModestShellBanner *self, const gchar *icon_name)
199 {
200         return;
201 }
202
203 void
204 modest_shell_banner_set_text (ModestShellBanner *self, const gchar *label)
205 {
206         ModestShellBannerPrivate *priv;
207
208         priv = MODEST_SHELL_BANNER_GET_PRIVATE (self);
209
210         gtk_label_set_text (GTK_LABEL (priv->label), label);
211 }
212
213 void
214 modest_shell_banner_set_animation (ModestShellBanner *self, const gchar *animation_name)
215 {
216         return;
217 }
218
219 static gboolean
220 _on_timeout (ModestShellBanner *self)
221 {
222         ModestShellBannerPrivate *priv;
223
224         priv = MODEST_SHELL_BANNER_GET_PRIVATE (self);
225         if (priv->timeout_id) {
226                 priv->timeout_id = 0;
227         }
228         gtk_widget_destroy (GTK_WIDGET (self));
229         return FALSE;
230 }