* Added preliminar code to support send and receive from the toolbar menu
[modest] / src / maemo / modest-main-window.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 <hildon-widgets/hildon-window.h>
31 #include <hildon-widgets/hildon-note.h>
32
33 #include <glib/gi18n.h>
34 #include <gtk/gtktreeviewcolumn.h>
35 #include <tny-account-store-view.h>
36 #include <tny-simple-list.h>
37
38 #include <string.h>
39
40 #include "widgets/modest-main-window.h"
41 #include "widgets/modest-msg-edit-window.h"
42 #include "widgets/modest-account-view-window.h"
43 #include "modest-runtime.h"
44 #include "modest-account-mgr-helpers.h"
45 #include "modest-platform.h"
46 #include "modest-widget-memory.h"
47 #include "modest-window-priv.h"
48 #include "modest-main-window-ui.h"
49 #include "modest-account-mgr.h"
50 #include "modest-conf.h"
51 #include <modest-maemo-utils.h>
52 #include "modest-tny-platform-factory.h"
53 #include "modest-tny-msg.h"
54 #include "modest-mail-operation.h"
55 #include "modest-icon-names.h"
56
57 /* 'private'/'protected' functions */
58 static void modest_main_window_class_init    (ModestMainWindowClass *klass);
59 static void modest_main_window_init          (ModestMainWindow *obj);
60 static void modest_main_window_finalize      (GObject *obj);
61 static gboolean modest_main_window_window_state_event (GtkWidget *widget, 
62                                                            GdkEventWindowState *event, 
63                                                            gpointer userdata);
64
65 static void connect_signals (ModestMainWindow *self);
66 static void restore_sizes (ModestMainWindow *self);
67 static void save_sizes (ModestMainWindow *self);
68
69 static void modest_main_window_show_toolbar   (ModestWindow *window,
70                                                gboolean show_toolbar);
71
72 static void on_account_update                 (TnyAccountStore *account_store, 
73                                                gchar *account_name,
74                                                gpointer user_data);
75
76 /* list my signals */
77 enum {
78         /* MY_SIGNAL_1, */
79         /* MY_SIGNAL_2, */
80         LAST_SIGNAL
81 };
82
83 typedef struct _ModestMainWindowPrivate ModestMainWindowPrivate;
84 struct _ModestMainWindowPrivate {
85         GtkWidget *msg_paned;
86         GtkWidget *main_paned;
87         GtkWidget *main_vbox;
88         GtkWidget *progress_bar;
89
90         ModestHeaderView *header_view;
91         ModestFolderView *folder_view;
92
93         ModestMainWindowStyle style;
94 };
95 #define MODEST_MAIN_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
96                                                 MODEST_TYPE_MAIN_WINDOW, \
97                                                 ModestMainWindowPrivate))
98
99 typedef struct _GetMsgAsyncHelper {
100         ModestMainWindowPrivate *main_window_private;
101         guint action;
102         ModestTnyMsgReplyType reply_type;
103         ModestTnyMsgForwardType forward_type;
104         gchar *from;
105         TnyIterator *iter;
106 } GetMsgAsyncHelper;
107
108
109 /* globals */
110 static GtkWindowClass *parent_class = NULL;
111
112 /* uncomment the following if you have defined any signals */
113 /* static guint signals[LAST_SIGNAL] = {0}; */
114
115 GType
116 modest_main_window_get_type (void)
117 {
118         static GType my_type = 0;
119         if (!my_type) {
120                 static const GTypeInfo my_info = {
121                         sizeof(ModestMainWindowClass),
122                         NULL,           /* base init */
123                         NULL,           /* base finalize */
124                         (GClassInitFunc) modest_main_window_class_init,
125                         NULL,           /* class finalize */
126                         NULL,           /* class data */
127                         sizeof(ModestMainWindow),
128                         1,              /* n_preallocs */
129                         (GInstanceInitFunc) modest_main_window_init,
130                         NULL
131                 };
132                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
133                                                   "ModestMainWindow",
134                                                   &my_info, 0);
135         }
136         return my_type;
137 }
138
139 static void
140 modest_main_window_class_init (ModestMainWindowClass *klass)
141 {
142         GObjectClass *gobject_class;
143         gobject_class = (GObjectClass*) klass;
144         ModestWindowClass *modest_window_class;
145
146         modest_window_class = (ModestWindowClass *) klass;
147
148         parent_class            = g_type_class_peek_parent (klass);
149         gobject_class->finalize = modest_main_window_finalize;
150
151         modest_window_class->show_toolbar_func = modest_main_window_show_toolbar;
152
153         g_type_class_add_private (gobject_class, sizeof(ModestMainWindowPrivate));
154 }
155
156
157 static void
158 on_key_changed (ModestConf* conf, const gchar *key, ModestConfEvent event, ModestMainWindow *win)
159 {
160         TnyAccount *account;
161         
162         if (!key || strcmp (key, MODEST_CONF_DEVICE_NAME) != 0)
163                 return; /* wrong key */
164         
165         /* ok, the device name changed; thus, we have to update the
166          * local folder account name*/
167         account =
168                 modest_tny_account_store_get_tny_account_by_account (modest_runtime_get_account_store(),
169                                                                      MODEST_LOCAL_FOLDERS_ACCOUNT_ID,
170                                                                      TNY_ACCOUNT_TYPE_STORE);
171         if (!account) {
172                 g_printerr ("modest: could not get account\n");
173                 return;
174         }
175
176         if (event == MODEST_CONF_EVENT_KEY_UNSET) 
177                 tny_account_set_name (account, MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME);
178         else {
179                 gchar *device_name = modest_conf_get_string (modest_runtime_get_conf(),
180                                                              MODEST_CONF_DEVICE_NAME, NULL);
181                 tny_account_set_name (account, device_name);
182                 g_free (device_name);
183         }
184         g_object_unref (G_OBJECT(account));     
185 }
186
187 static void
188 modest_main_window_init (ModestMainWindow *obj)
189 {
190         ModestMainWindowPrivate *priv;
191
192         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
193
194         priv->msg_paned    = NULL;
195         priv->main_paned   = NULL;      
196         priv->main_vbox    = NULL;
197         priv->header_view  = NULL;
198         priv->folder_view  = NULL;
199         priv->style  = MODEST_MAIN_WINDOW_STYLE_SPLIT;
200
201         /* progress bar */
202         priv->progress_bar = gtk_progress_bar_new ();
203         gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR(priv->progress_bar), 1.0);
204         gtk_progress_bar_set_ellipsize (GTK_PROGRESS_BAR(priv->progress_bar),
205                                         PANGO_ELLIPSIZE_END);
206 }
207
208 static void
209 modest_main_window_finalize (GObject *obj)
210 {
211         G_OBJECT_CLASS(parent_class)->finalize (obj);
212 }
213
214 GtkWidget*
215 modest_main_window_get_child_widget (ModestMainWindow *self,
216                                      ModestWidgetType widget_type)
217 {
218         ModestMainWindowPrivate *priv;
219         GtkWidget *widget;
220         
221         g_return_val_if_fail (self, NULL);
222         g_return_val_if_fail (widget_type >= 0 && widget_type < MODEST_WIDGET_TYPE_NUM,
223                               NULL);
224         
225         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
226
227         switch (widget_type) {
228         case MODEST_WIDGET_TYPE_HEADER_VIEW:
229                 widget = (GtkWidget*)priv->header_view; break;
230         case MODEST_WIDGET_TYPE_FOLDER_VIEW:
231                 widget = (GtkWidget*)priv->folder_view; break;
232         default:
233                 return NULL;
234         }
235
236         return widget ? GTK_WIDGET(widget) : NULL;
237 }
238
239
240
241 static void
242 restore_sizes (ModestMainWindow *self)
243 {
244         ModestConf *conf;
245         ModestMainWindowPrivate *priv;
246
247         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
248
249         conf = modest_runtime_get_conf ();
250         
251         modest_widget_memory_restore (conf, G_OBJECT(priv->main_paned),
252                                       "modest-main-paned");
253         modest_widget_memory_restore (conf, G_OBJECT(priv->header_view),
254                                       "header-view");
255         modest_widget_memory_restore (conf, G_OBJECT(self), 
256                                       "modest-main-window");
257 }
258
259
260 static void
261 save_sizes (ModestMainWindow *self)
262 {
263         ModestConf *conf;
264         ModestMainWindowPrivate *priv;
265                 
266         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
267         conf = modest_runtime_get_conf ();
268         
269         modest_widget_memory_save (conf,G_OBJECT(self), "modest-main-window");
270         modest_widget_memory_save (conf, G_OBJECT(priv->main_paned),
271                                    "modest-main-paned");
272         modest_widget_memory_save (conf, G_OBJECT(priv->header_view), "header-view");
273 }
274
275 static GtkWidget*
276 wrapped_in_scrolled_window (GtkWidget *widget, gboolean needs_viewport)
277 {
278         GtkWidget *win;
279
280         win = gtk_scrolled_window_new (NULL, NULL);
281         gtk_scrolled_window_set_policy
282                 (GTK_SCROLLED_WINDOW (win),GTK_POLICY_NEVER,
283                  GTK_POLICY_AUTOMATIC);
284         
285         if (needs_viewport)
286                 gtk_scrolled_window_add_with_viewport
287                         (GTK_SCROLLED_WINDOW(win), widget);
288         else
289                 gtk_container_add (GTK_CONTAINER(win),
290                                    widget);
291
292         return win;
293 }
294
295
296 static gboolean
297 on_delete_event (GtkWidget *widget, GdkEvent  *event, ModestMainWindow *self)
298 {
299         save_sizes (self);
300         return FALSE;
301 }
302
303 static void
304 connect_signals (ModestMainWindow *self)
305 {       
306         ModestWindowPrivate *parent_priv;
307         ModestMainWindowPrivate *priv;
308         GtkWidget *menu;
309         
310         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
311         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
312         
313         /* folder view */
314         g_signal_connect (G_OBJECT(priv->folder_view), "folder_selection_changed",
315                           G_CALLBACK(modest_ui_actions_on_folder_selection_changed), self);
316
317         menu = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/FolderViewContextMenu");
318         gtk_widget_tap_and_hold_setup (GTK_WIDGET (priv->folder_view), menu, NULL, 0);
319
320         /* header view */
321 /*      g_signal_connect (G_OBJECT(priv->header_view), "status_update", */
322 /*                        G_CALLBACK(modest_ui_actions_on_header_status_update), self); */
323         g_signal_connect (G_OBJECT(priv->header_view), "header_selected",
324                           G_CALLBACK(modest_ui_actions_on_header_selected), self);
325         g_signal_connect (G_OBJECT(priv->header_view), "header_activated",
326                           G_CALLBACK(modest_ui_actions_on_header_activated), self);
327         g_signal_connect (G_OBJECT(priv->header_view), "item_not_found",
328                           G_CALLBACK(modest_ui_actions_on_item_not_found), self);
329
330         /* window */
331         g_signal_connect (G_OBJECT(self), "delete-event", G_CALLBACK(on_delete_event), self);
332         g_signal_connect (G_OBJECT (self), "window-state-event",
333                           G_CALLBACK (modest_main_window_window_state_event),
334                           NULL);
335
336
337         
338         /* modest_maemo_utils_get_device_name will probably change
339          * MODEST_CONF_DEVICE_NAME. If that happens, we update the local folders
340          * account name in the callback
341          */
342         g_signal_connect (G_OBJECT(modest_runtime_get_conf()), "key_changed",
343                           G_CALLBACK(on_key_changed), self);
344         
345         g_signal_connect (G_OBJECT(self), "delete-event", G_CALLBACK(on_delete_event), self);
346
347         /* Track account changes. We need to refresh the toolbar */
348         g_signal_connect (G_OBJECT (modest_runtime_get_account_store ()),
349                           "account_update",
350                           G_CALLBACK (on_account_update),
351                           self);
352 }
353
354
355 gboolean
356 sync_accounts_cb (ModestMainWindow *win)
357 {
358         modest_ui_actions_on_send_receive (NULL, MODEST_WINDOW(win));
359         return FALSE;
360 }
361
362
363 ModestWindow*
364 modest_main_window_new (void)
365 {
366         ModestMainWindow *self; 
367         ModestMainWindowPrivate *priv;
368         ModestWindowPrivate *parent_priv;
369         GtkWidget *header_win, *folder_win;
370         GtkActionGroup *action_group;
371         GError *error = NULL;
372         TnyFolderStoreQuery *query;
373         GdkPixbuf *window_icon;
374         ModestConf *conf;
375         GtkAction *action;
376
377         self  = MODEST_MAIN_WINDOW(g_object_new(MODEST_TYPE_MAIN_WINDOW, NULL));
378         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
379         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
380
381         parent_priv->ui_manager = gtk_ui_manager_new();
382         action_group = gtk_action_group_new ("ModestMainWindowActions");
383         gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
384
385         /* Add common actions */
386         gtk_action_group_add_actions (action_group,
387                                       modest_action_entries,
388                                       G_N_ELEMENTS (modest_action_entries),
389                                       self);
390
391         gtk_action_group_add_toggle_actions (action_group,
392                                              modest_toggle_action_entries,
393                                              G_N_ELEMENTS (modest_toggle_action_entries),
394                                              self);
395
396         gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 0);
397         g_object_unref (action_group);
398
399         /* Load the UI definition */
400         gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager,
401                                          MODEST_UIDIR "modest-main-window-ui.xml", &error);
402         if (error != NULL) {
403                 g_warning ("Could not merge modest-ui.xml: %s", error->message);
404                 g_error_free (error);
405                 error = NULL;
406         }
407
408         /* Add accelerators */
409         gtk_window_add_accel_group (GTK_WINDOW (self), 
410                                     gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
411
412         /* Menubar. Update the state of some toggles */
413         parent_priv->menubar = modest_maemo_utils_menubar_to_menu (parent_priv->ui_manager);
414         conf = modest_runtime_get_conf ();
415         action = gtk_ui_manager_get_action (parent_priv->ui_manager, 
416                                             "/MenuBar/ViewMenu/ViewShowToolbarMainMenu/ViewShowToolbarNormalScreenMenu");
417         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
418                                       modest_conf_get_bool (conf, MODEST_CONF_SHOW_TOOLBAR, NULL));
419         action = gtk_ui_manager_get_action (parent_priv->ui_manager, 
420                                             "/MenuBar/ViewMenu/ViewShowToolbarMainMenu/ViewShowToolbarFullScreenMenu");
421         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
422                                       modest_conf_get_bool (conf, MODEST_CONF_SHOW_TOOLBAR_FULLSCREEN, NULL));
423         hildon_window_set_menu (HILDON_WINDOW (self), GTK_MENU (parent_priv->menubar));
424
425         /* folder view */
426         query = tny_folder_store_query_new ();
427         tny_folder_store_query_add_item (query, NULL,
428                                          TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
429         priv->folder_view = MODEST_FOLDER_VIEW(modest_folder_view_new (query));
430         if (!priv->folder_view)
431                 g_printerr ("modest: cannot instantiate folder view\n");        
432         g_object_unref (G_OBJECT (query));      
433         modest_maemo_utils_get_device_name ();
434
435         /* header view */
436         priv->header_view  =
437                 MODEST_HEADER_VIEW(modest_header_view_new (NULL, MODEST_HEADER_VIEW_STYLE_DETAILS));
438         if (!priv->header_view)
439                 g_printerr ("modest: cannot instantiate header view\n");
440         modest_header_view_set_style (priv->header_view, MODEST_HEADER_VIEW_STYLE_TWOLINES);
441         
442         folder_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->folder_view), FALSE);
443         header_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->header_view), FALSE);
444
445         /* paned */
446         priv->main_paned = gtk_hpaned_new ();
447         gtk_paned_add1 (GTK_PANED(priv->main_paned), folder_win);
448         gtk_paned_add2 (GTK_PANED(priv->main_paned), header_win);
449         gtk_widget_show (GTK_WIDGET(priv->header_view));
450         gtk_tree_view_columns_autosize (GTK_TREE_VIEW(priv->header_view));
451
452         /* putting it all together... */
453         priv->main_vbox = gtk_vbox_new (FALSE, 6);
454         gtk_box_pack_start (GTK_BOX(priv->main_vbox), priv->main_paned, TRUE, TRUE,0);
455
456         gtk_container_add (GTK_CONTAINER(self), priv->main_vbox);
457         restore_sizes (MODEST_MAIN_WINDOW(self));
458
459         /* Set window icon */
460         window_icon = modest_platform_get_icon (MODEST_APP_ICON);
461         gtk_window_set_icon (GTK_WINDOW (self), window_icon);
462
463         /* Connect signals */
464         connect_signals (self);
465
466         /* Set account store */
467         tny_account_store_view_set_account_store (TNY_ACCOUNT_STORE_VIEW (priv->folder_view),
468                                                   TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
469         g_idle_add ((GSourceFunc)sync_accounts_cb, self);
470         /* do send & receive when we are idle */        
471
472         g_message ("online? %s",
473                    tny_device_is_online (modest_runtime_get_device()) ? "yes" : "no");
474                 
475         return MODEST_WINDOW(self);
476 }
477
478 gboolean 
479 modest_main_window_close_all (ModestMainWindow *self)
480 {
481         GtkWidget *note;
482         GtkResponseType response;
483
484         /* Create the confirmation dialog MSG-NOT308 */
485         note = hildon_note_new_confirmation_add_buttons (GTK_WINDOW (self),
486                                                          _("emev_nc_close_windows"),
487                                                          _("mcen_db_yes"), GTK_RESPONSE_YES,
488                                                          _("mcen_db_no"), GTK_RESPONSE_NO,
489                                                          NULL);
490
491         response = gtk_dialog_run (GTK_DIALOG (note));
492         gtk_widget_destroy (GTK_WIDGET (note));
493
494         if (response == GTK_RESPONSE_YES)
495                 return TRUE;
496         else
497                 return FALSE;
498 }
499
500 void 
501 modest_main_window_set_style (ModestMainWindow *self, 
502                               ModestMainWindowStyle style)
503 {
504         ModestMainWindowPrivate *priv;
505         GtkWidget *scrolled_win;
506
507         g_return_if_fail (MODEST_IS_MAIN_WINDOW (self));
508
509         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
510
511         /* no change -> nothing to do */
512         if (priv->style == style)
513                 return;
514
515         priv->style = style;
516         scrolled_win = gtk_widget_get_parent (GTK_WIDGET (priv->header_view));
517
518         switch (style) {
519         case MODEST_MAIN_WINDOW_STYLE_SIMPLE:
520                 /* Remove main paned */
521                 g_object_ref (priv->main_paned);
522                 gtk_container_remove (GTK_CONTAINER (priv->main_vbox), priv->main_paned);
523
524                 /* Reparent header view with scrolled window */
525                 gtk_widget_reparent (scrolled_win, priv->main_vbox);
526                 break;
527         case MODEST_MAIN_WINDOW_STYLE_SPLIT:
528                 /* Remove header view */
529                 g_object_ref (scrolled_win);
530                 gtk_container_remove (GTK_CONTAINER (priv->main_vbox), scrolled_win);
531
532                 /* Reparent the main paned */
533                 gtk_paned_add2 (GTK_PANED (priv->main_paned), scrolled_win);
534                 gtk_container_add (GTK_CONTAINER (priv->main_vbox), priv->main_paned);
535                 break;
536         default:
537                 g_return_if_reached ();
538         }
539
540         /* Show changes */
541         gtk_widget_show_all (GTK_WIDGET (priv->main_vbox));
542 }
543
544 ModestMainWindowStyle
545 modest_main_window_get_style (ModestMainWindow *self)
546 {
547         ModestMainWindowPrivate *priv;
548
549         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW (self), -1);
550
551         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
552         return priv->style;
553 }
554
555 static gboolean
556 modest_main_window_window_state_event (GtkWidget *widget, GdkEventWindowState *event, gpointer userdata)
557 {
558         if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN) {
559                 ModestWindowPrivate *parent_priv;
560                 ModestWindowMgr *mgr;
561                 gboolean is_fullscreen;
562                 GtkAction *fs_toggle_action;
563                 gboolean active;
564                 
565                 mgr = modest_runtime_get_window_mgr ();
566                 
567                 is_fullscreen = modest_window_mgr_get_fullscreen_mode (mgr);
568
569                 parent_priv = MODEST_WINDOW_GET_PRIVATE (widget);
570                 
571                 fs_toggle_action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/ViewMenu/ViewToggleFullscreenMenu");
572                 active = (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (fs_toggle_action)))?1:0;
573                 if (is_fullscreen != active) {
574                         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (fs_toggle_action), is_fullscreen);
575                 }
576         }
577
578         return FALSE;
579
580 }
581
582 static void
583 set_homogeneous (GtkWidget *widget,
584                  gpointer data)
585 {
586         gtk_tool_item_set_expand (GTK_TOOL_ITEM (widget), TRUE);
587         gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (widget), TRUE);
588 }
589
590 static void 
591 modest_main_window_show_toolbar (ModestWindow *self,
592                                  gboolean show_toolbar)
593 {
594         ModestWindowPrivate *parent_priv;
595         GtkWidget *reply_button = NULL, *menu = NULL;
596         
597         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
598
599         if (!parent_priv->toolbar && show_toolbar) {
600                 parent_priv->toolbar = gtk_ui_manager_get_widget (parent_priv->ui_manager, 
601                                                                   "/ToolBar");
602
603                 /* Set homogeneous toolbar */
604                 gtk_container_foreach (GTK_CONTAINER (parent_priv->toolbar), 
605                                        set_homogeneous, NULL);
606
607                 /* Add to window */
608                 hildon_window_add_toolbar (HILDON_WINDOW (self), 
609                                            GTK_TOOLBAR (parent_priv->toolbar));
610
611                 /* Set reply button tap and hold menu */
612                 reply_button = gtk_ui_manager_get_widget (parent_priv->ui_manager, 
613                                                           "/ToolBar/ToolbarMessageReply");
614                 menu = gtk_ui_manager_get_widget (parent_priv->ui_manager,
615                                                   "/ToolbarReplyContextMenu");
616                 gtk_widget_tap_and_hold_setup (GTK_WIDGET (reply_button), menu, NULL, 0);
617
618                 /* Set send & receive button tap and hold menu */
619                 on_account_update (TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()),
620                                    NULL, self);
621         }
622
623
624         if (show_toolbar)
625                 gtk_widget_show (GTK_WIDGET (parent_priv->toolbar));
626         else
627                 gtk_widget_hide (GTK_WIDGET (parent_priv->toolbar));
628 }
629
630 /*
631  * TODO: modify the menu dinamically. Add handlers to each item of the
632  * menu when created
633  */
634 static void 
635 on_account_update (TnyAccountStore *account_store, 
636                    gchar *accout_name,
637                    gpointer user_data)
638 {
639         ModestMainWindow *self;
640         ModestWindowPrivate *parent_priv;
641         TnyList *account_list;
642         GtkWidget *popup = NULL, *item, *send_receive_button;
643         TnyIterator *iter;
644         ModestAccountMgr *mgr;
645         gchar *default_account;
646
647         self = MODEST_MAIN_WINDOW (user_data);
648         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
649
650         /* If there is no toolbar then exit */
651         if (!parent_priv->toolbar)
652                 return;
653
654         /* Get accounts */
655         account_list = tny_simple_list_new ();
656         tny_account_store_get_accounts (account_store, 
657                                         account_list, 
658                                         TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
659
660         /* If there is only one account do not show any menu */
661         if (tny_list_get_length (account_list) <= 1)
662                 goto free;
663         
664         /* Get send receive button */
665         send_receive_button = gtk_ui_manager_get_widget (parent_priv->ui_manager, 
666                                                           "/ToolBar/ToolbarSendReceive");
667
668         /* Create the menu */
669         popup = gtk_menu_new ();
670         item = gtk_menu_item_new_with_label (_("FIXME All"));
671         gtk_menu_shell_append (GTK_MENU_SHELL (popup), GTK_WIDGET (item));
672         item = gtk_separator_menu_item_new ();
673         gtk_menu_shell_append (GTK_MENU_SHELL (popup), GTK_WIDGET (item));
674
675         iter = tny_list_create_iterator (account_list);
676         mgr = modest_runtime_get_account_mgr ();
677         default_account = modest_account_mgr_get_default_account (mgr);
678
679         do {
680                 TnyAccount *acc;
681                 const gchar *acc_name;
682
683                 /* Create tool item */
684                 acc = TNY_ACCOUNT (tny_iterator_get_current (iter));
685                 acc_name = tny_account_get_name (acc);
686
687                 if (!strcmp (default_account, acc_name)) {
688                         gchar *bold_name;
689                         bold_name = g_strdup_printf ("<b>%s</b>", acc_name);
690                         item = gtk_menu_item_new_with_label (bold_name);
691                         g_free (bold_name);
692                 } else {
693                         item = gtk_menu_item_new_with_label (acc_name);
694                 }
695                 g_object_unref (acc);
696
697                 /* Append item */
698                 gtk_menu_shell_append (GTK_MENU_SHELL (popup), GTK_WIDGET (item));
699
700                 /* Go to next */
701                 tny_iterator_next (iter);
702
703         } while (!tny_iterator_is_done (iter));
704
705         g_object_unref (iter);
706
707         /* Mandatory in order to view the menu contents */
708         gtk_widget_show_all (popup);
709
710         /* Setup tap_and_hold */
711         gtk_widget_tap_and_hold_setup (send_receive_button, popup, NULL, 0);
712
713  free:
714
715         /* Free */
716         g_object_unref (account_list);
717 }