Fixes several G_CRITICALS
[modest] / src / gtk / modest-shell.c
1 /* Copyright (c) 2009, 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 <string.h>
31 #include <modest-shell.h>
32 #include <modest-shell-window.h>
33 #include <modest-icon-names.h>
34 #include <modest-ui-actions.h>
35
36 /* 'private'/'protected' functions */
37 static void modest_shell_class_init (ModestShellClass *klass);
38 static void modest_shell_instance_init (ModestShell *obj);
39 static void modest_shell_finalize   (GObject *obj);
40
41 static void update_title (ModestShell *self);
42
43 static void on_back_button_clicked (GtkToolButton *button, ModestShell *self);
44 static void on_title_button_clicked (GtkToolButton *button, ModestShell *self);
45 static void on_new_msg_button_clicked (GtkToolButton *button, ModestShell *self);
46 static void on_style_set (GtkWidget *widget, GtkStyle *old_style, ModestShell *shell);
47
48
49 typedef struct _ModestShellPrivate ModestShellPrivate;
50 struct _ModestShellPrivate {
51         GtkWidget *main_vbox;
52         GtkWidget *notebook;
53         GtkWidget *top_toolbar;
54         GtkToolItem *new_message_button;
55         GtkToolItem *back_button;
56         GtkToolItem *title_button;
57         GtkWidget *title_label;
58         GtkWidget *subtitle_label;
59
60         GtkWidget *progress_icon;
61         GdkPixbuf **progress_frames;
62         gint next_frame;
63         guint progress_timeout_id;
64 };
65 #define MODEST_SHELL_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
66                                                                       MODEST_TYPE_SHELL, \
67                                                                       ModestShellPrivate))
68 /* globals */
69 static GObjectClass *parent_class = NULL;
70
71 GType
72 modest_shell_get_type (void)
73 {
74         static GType my_type = 0;
75         if (!my_type) {
76                 static const GTypeInfo my_info = {
77                         sizeof(ModestShellClass),
78                         NULL,           /* base init */
79                         NULL,           /* base finalize */
80                         (GClassInitFunc) modest_shell_class_init,
81                         NULL,           /* class finalize */
82                         NULL,           /* class data */
83                         sizeof(ModestShell),
84                         1,              /* n_preallocs */
85                         (GInstanceInitFunc) modest_shell_instance_init,
86                         NULL
87                 };
88                 my_type = g_type_register_static (GTK_TYPE_WINDOW,
89                                                   "ModestShell",
90                                                   &my_info, 0);
91         }
92         return my_type;
93 }
94
95 static void
96 modest_shell_class_init (ModestShellClass *klass)
97 {
98         GObjectClass *gobject_class;
99
100         gobject_class = (GObjectClass*) klass;
101
102         parent_class            = g_type_class_peek_parent (klass);
103         gobject_class->finalize = modest_shell_finalize;
104
105         g_type_class_add_private (gobject_class, sizeof(ModestShellPrivate));
106
107 }
108
109 static void
110 modest_shell_instance_init (ModestShell *obj)
111 {
112         ModestShellPrivate *priv;
113         GtkWidget *title_vbox;
114         GtkWidget *new_message_icon;
115         GtkToolItem *separator_toolitem;
116         GtkWidget *top_hbox;
117
118         priv = MODEST_SHELL_GET_PRIVATE(obj);
119         priv->progress_frames = g_malloc0 (sizeof(GdkPixbuf *)*31);
120         priv->progress_timeout_id = 0;
121         priv->next_frame = 0;
122
123         priv->main_vbox = gtk_vbox_new (FALSE, 0);
124         gtk_widget_show (priv->main_vbox);
125
126         top_hbox = gtk_hbox_new (FALSE, 0);
127         gtk_widget_show (top_hbox);
128         gtk_box_pack_start (GTK_BOX (priv->main_vbox), top_hbox, FALSE, FALSE, 0);
129
130         priv->top_toolbar = gtk_toolbar_new ();
131         gtk_toolbar_set_style (GTK_TOOLBAR (priv->top_toolbar), GTK_TOOLBAR_BOTH_HORIZ);
132         gtk_widget_show (priv->top_toolbar);
133         gtk_box_pack_start (GTK_BOX (top_hbox), priv->top_toolbar, TRUE, TRUE, 0);
134
135         priv->progress_icon = gtk_image_new ();
136         gtk_widget_show (priv->progress_icon);
137         gtk_box_pack_start (GTK_BOX (top_hbox), priv->progress_icon, FALSE, FALSE, 0);
138
139         new_message_icon = gtk_image_new_from_icon_name (MODEST_TOOLBAR_ICON_NEW_MAIL, GTK_ICON_SIZE_LARGE_TOOLBAR);
140         gtk_widget_show (new_message_icon);
141         priv->new_message_button = gtk_tool_button_new (new_message_icon, _("mcen_va_new_email"));
142         g_object_set (priv->new_message_button, "is-important", TRUE, NULL);
143         gtk_toolbar_insert (GTK_TOOLBAR (priv->top_toolbar), priv->new_message_button, -1);
144         gtk_widget_show (GTK_WIDGET (priv->new_message_button));
145         g_signal_connect (G_OBJECT (priv->new_message_button), "clicked", G_CALLBACK (on_new_msg_button_clicked), obj);
146
147         priv->back_button = gtk_tool_button_new_from_stock (GTK_STOCK_GO_BACK);
148         g_object_set (priv->back_button, "is-important", TRUE, NULL);
149         gtk_toolbar_insert (GTK_TOOLBAR (priv->top_toolbar), priv->back_button, -1);
150         gtk_widget_show (GTK_WIDGET (priv->back_button));
151         g_signal_connect (G_OBJECT (priv->back_button), "clicked", G_CALLBACK (on_back_button_clicked), obj);
152
153         separator_toolitem = gtk_separator_tool_item_new ();
154         gtk_toolbar_insert (GTK_TOOLBAR (priv->top_toolbar), separator_toolitem, -1);
155         gtk_widget_show (GTK_WIDGET (separator_toolitem));
156
157         title_vbox = gtk_vbox_new (FALSE, 0);
158         priv->title_label = gtk_label_new (NULL);
159         gtk_misc_set_alignment (GTK_MISC (priv->title_label), 0.0, 1.0);
160         priv->subtitle_label = gtk_label_new (NULL);
161         gtk_misc_set_alignment (GTK_MISC (priv->subtitle_label), 0.0, 0.0);
162         gtk_widget_show (priv->title_label);
163         gtk_widget_show (priv->subtitle_label);
164         gtk_box_pack_start (GTK_BOX (title_vbox), priv->title_label, TRUE, TRUE, 0);
165         gtk_box_pack_start (GTK_BOX (title_vbox), priv->subtitle_label, FALSE, FALSE, 0);
166         gtk_widget_show (title_vbox);
167
168         priv->title_button = gtk_tool_button_new (NULL, NULL);
169         gtk_widget_show (GTK_WIDGET (priv->title_button));
170         gtk_tool_button_set_label_widget (GTK_TOOL_BUTTON (priv->title_button), title_vbox);
171         gtk_toolbar_insert (GTK_TOOLBAR (priv->top_toolbar), priv->title_button, -1);
172         gtk_container_child_set (GTK_CONTAINER (priv->top_toolbar), GTK_WIDGET (priv->title_button), "expand", TRUE, NULL);
173         g_object_set (priv->title_button, "is-important", TRUE, NULL);
174         g_signal_connect (G_OBJECT (priv->title_button), "clicked", G_CALLBACK (on_title_button_clicked), obj);
175
176         priv->notebook = gtk_notebook_new ();
177         gtk_notebook_set_show_tabs ((GtkNotebook *)priv->notebook, FALSE);
178         gtk_notebook_set_show_border ((GtkNotebook *)priv->notebook, FALSE);
179         gtk_widget_show (priv->notebook);
180         gtk_box_pack_start (GTK_BOX (priv->main_vbox), priv->notebook, TRUE, TRUE, 0);
181         gtk_container_add (GTK_CONTAINER (obj), priv->main_vbox);
182
183         g_signal_connect (G_OBJECT (obj), "style-set", G_CALLBACK (on_style_set), obj);
184 }
185
186 static void
187 modest_shell_finalize (GObject *obj)
188 {
189         ModestShellPrivate *priv;
190         int n;
191
192         priv = MODEST_SHELL_GET_PRIVATE (obj);
193
194         if (priv->progress_timeout_id) {
195                 g_source_remove (priv->progress_timeout_id);
196         }
197         for (n = 0; n < 31; n++) {
198                 if (priv->progress_frames[n]) {
199                         g_object_unref (priv->progress_frames[n]);
200                 }
201         }
202         g_free (priv->progress_frames);
203
204         G_OBJECT_CLASS(parent_class)->finalize (obj);
205 }
206
207 GtkWidget*
208 modest_shell_new (void)
209 {
210         return (GtkWidget *) g_object_new(MODEST_TYPE_SHELL, NULL);
211 }
212
213 ModestWindow *
214 modest_shell_peek_window (ModestShell *shell)
215 {
216         ModestShellPrivate *priv;
217         gint count;
218
219         priv = MODEST_SHELL_GET_PRIVATE (shell);
220         count = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
221
222         if (count > 0) {
223                 return (ModestWindow *) gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), count - 1);
224         } else {
225                 return NULL;
226         }
227 }
228
229 gboolean
230 modest_shell_delete_window (ModestShell *shell, ModestWindow *window)
231 {
232         ModestShellPrivate *priv;
233         gboolean ret_value;
234
235         priv = MODEST_SHELL_GET_PRIVATE (shell);
236         g_signal_emit_by_name (G_OBJECT (window), "delete-event", NULL, &ret_value);
237         if (ret_value == FALSE) {
238                 gint page_num;
239                 
240                 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook), GTK_WIDGET (window));
241                 if (page_num != -1) {
242                         gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), page_num);
243                 }
244         }
245
246         update_title (shell);
247
248         return ret_value;
249 }
250
251 void
252 modest_shell_add_window (ModestShell *shell, ModestWindow *window)
253 {
254         ModestShellPrivate *priv;
255
256         priv = MODEST_SHELL_GET_PRIVATE (shell);
257         gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), GTK_WIDGET (window), NULL);
258         gtk_widget_show (GTK_WIDGET (window));
259         gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), -1);
260         modest_shell_window_set_shell (MODEST_SHELL_WINDOW (window), shell);
261         update_title (shell);
262 }
263
264 gint
265 modest_shell_count_windows (ModestShell *shell)
266 {
267         ModestShellPrivate *priv;
268
269         priv = MODEST_SHELL_GET_PRIVATE (shell);
270
271         return gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
272 }
273
274 void
275 modest_shell_set_title (ModestShell *shell, ModestWindow *window, const gchar *title)
276 {
277         ModestShellPrivate *priv;
278
279         priv = MODEST_SHELL_GET_PRIVATE (shell);
280
281         gtk_notebook_set_tab_label_text (GTK_NOTEBOOK (priv->notebook), GTK_WIDGET (window), title);
282
283         update_title (shell);
284 }
285
286 static void
287 show_next_frame (ModestShell *shell)
288 {
289         ModestShellPrivate *priv;
290
291         priv = MODEST_SHELL_GET_PRIVATE (shell);
292
293         gtk_image_set_from_pixbuf (GTK_IMAGE (priv->progress_icon), priv->progress_frames[priv->next_frame]);
294
295         priv->next_frame++;
296         if (priv->next_frame >= 31)
297                 priv->next_frame = 0;
298 }
299
300 static gboolean
301 on_progress_timeout (ModestShell *shell)
302 {
303         show_next_frame (shell);
304         return TRUE;
305 }
306
307 void
308 modest_shell_show_progress (ModestShell *shell, ModestWindow *window, gboolean show)
309 {
310         ModestShellPrivate *priv;
311
312         priv = MODEST_SHELL_GET_PRIVATE (shell);
313
314         if (show) {
315                 if (priv->progress_timeout_id == 0) {
316                         priv->progress_timeout_id = g_timeout_add (100, (GSourceFunc) on_progress_timeout, shell);
317                         show_next_frame (shell);
318                 }
319                 gtk_widget_show (priv->progress_icon);
320         } else {
321                 if (priv->progress_timeout_id) {
322                         g_source_remove (priv->progress_timeout_id);
323                         priv->progress_timeout_id = 0;
324                 }
325                 gtk_widget_hide (priv->progress_icon);
326         }
327 }
328
329 static void
330 update_title (ModestShell *self)
331 {
332         gint n_pages, i;
333         ModestShellPrivate *priv;
334         GtkWidget *child;
335         GString *title_buffer;
336         GString *subtitle_buffer;
337         const gchar *tab_label_text;
338
339         priv = MODEST_SHELL_GET_PRIVATE (self);
340
341         n_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
342         if (n_pages == 0) {
343                 gtk_label_set_text (GTK_LABEL (priv->title_label), "");
344                 gtk_label_set_text (GTK_LABEL (priv->subtitle_label), "");
345                 return;
346         }
347
348         child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), n_pages - 1);
349         title_buffer = g_string_new ("");
350         title_buffer = g_string_append (title_buffer, "<b>");
351         tab_label_text = gtk_notebook_get_tab_label_text (GTK_NOTEBOOK (priv->notebook), child);
352         if (tab_label_text)
353                 title_buffer = g_string_append (title_buffer, tab_label_text);
354         title_buffer = g_string_append (title_buffer, "</b>");
355         gtk_label_set_markup (GTK_LABEL (priv->title_label), 
356                               title_buffer->str);
357         g_string_free (title_buffer, TRUE);
358
359         subtitle_buffer = g_string_new ("");
360         subtitle_buffer = g_string_append (subtitle_buffer, "<small>");
361         for (i = 0; i < n_pages - 1; i++) {
362         child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
363                 if (i != 0) {
364                         subtitle_buffer = g_string_append (subtitle_buffer, " / ");
365                 }
366                 subtitle_buffer = g_string_append (subtitle_buffer,
367                                                    gtk_notebook_get_tab_label_text (GTK_NOTEBOOK (priv->notebook), child));
368         }
369         subtitle_buffer = g_string_append (subtitle_buffer, "</small>");
370         gtk_label_set_markup (GTK_LABEL (priv->subtitle_label), 
371                               subtitle_buffer->str);
372         g_string_free (subtitle_buffer, TRUE);
373 }
374
375 static void
376 on_back_button_clicked (GtkToolButton *button, ModestShell *self)
377 {
378         ModestShellPrivate *priv;
379         gint n_pages;
380         gboolean delete_event_retval;
381         GtkWidget *child;
382
383         priv = MODEST_SHELL_GET_PRIVATE (self);
384
385         n_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
386         if (n_pages < 1)
387                 return;
388
389         child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), -1);
390         g_signal_emit_by_name (G_OBJECT (child), "delete-event", NULL, &delete_event_retval);
391
392         if (!delete_event_retval) {
393                 update_title (self);
394         }
395 }
396
397 static void
398 menu_position_cb (GtkMenu *menu,
399                   gint *x,
400                   gint *y,
401                   gboolean *push_in,
402                   ModestShell *self)
403 {
404         ModestShellPrivate *priv;
405         GtkAllocation *alloc;
406         GdkWindow *parent_window;
407         gint pos_x, pos_y;
408
409         priv = MODEST_SHELL_GET_PRIVATE (self);
410
411         alloc = &(GTK_WIDGET (priv->title_button)->allocation);
412         parent_window = gtk_widget_get_parent_window (GTK_WIDGET (priv->title_button));
413         gdk_window_get_position (parent_window, &pos_x, &pos_y);
414         *x = pos_x + alloc->x;
415         *y = pos_y + alloc->y + alloc->height;
416         *push_in = TRUE;
417         
418 }
419
420 static void
421 on_title_button_clicked (GtkToolButton *button, ModestShell *self)
422 {
423         ModestShellPrivate *priv;
424         gint n_pages;
425         GtkWidget *child;
426         GtkWidget *menu;
427
428         priv = MODEST_SHELL_GET_PRIVATE (self);
429
430         n_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
431         if (n_pages < 1)
432                 return;
433
434         child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), -1);
435         menu = modest_shell_window_get_menu (MODEST_SHELL_WINDOW (child));
436
437         if (menu) {
438                 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, 
439                                 (GtkMenuPositionFunc) menu_position_cb, (gpointer) self,
440                                 1, gtk_get_current_event_time ());
441         }
442 }
443
444 static void
445 on_new_msg_button_clicked (GtkToolButton *button, ModestShell *self)
446 {
447         ModestShellPrivate *priv;
448         gint n_pages;
449         GtkWidget *child;
450
451         priv = MODEST_SHELL_GET_PRIVATE (self);
452
453         n_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
454         if (n_pages < 1)
455                 return;
456
457         child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), -1);
458
459         modest_ui_actions_on_new_msg (NULL, MODEST_WINDOW (child));
460 }
461
462 static void
463 on_style_set (GtkWidget *widget,
464               GtkStyle *old_style,
465               ModestShell *self)
466 {
467         ModestShellPrivate *priv;
468         gint icon_w, icon_h;
469         GdkPixbuf *progress_pixbuf;
470         int n;
471
472         priv = MODEST_SHELL_GET_PRIVATE (self);
473
474         if (!gtk_icon_size_lookup (GTK_ICON_SIZE_LARGE_TOOLBAR, &icon_w, &icon_h))
475                 return;
476         progress_pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), "process-working", icon_w, 0, NULL);
477
478         for (n = 0; n < 31; n++) {
479                 if (priv->progress_frames[n] != NULL) {
480                         g_object_unref (priv->progress_frames[n]);
481                 }
482                 priv->progress_frames[n] = NULL;
483         }
484
485         if (progress_pixbuf) {
486                 gint max_x, max_y;
487                 gint i, j;
488
489                 icon_w = gdk_pixbuf_get_width (progress_pixbuf) / 8;
490
491                 n = 0;
492                 max_x = 8;
493                 max_y = 4;
494                 for (i = 0; i < 4; i++) {
495                         for (j = 0; j < 8; j++) {
496                                         GdkPixbuf *frame;
497
498                                         if ((i == 0) && (j == 0))
499                                                 continue;
500                                         frame = gdk_pixbuf_new_subpixbuf  (progress_pixbuf,
501                                                                            j*icon_w, i*icon_w,
502                                                                            icon_w, icon_w);
503                                         priv->progress_frames[n] = frame;
504                                         n++;
505                                 }
506                         }
507                 g_object_unref (progress_pixbuf);
508         }
509
510 }