* widgets/modest-window.c: Add a method to get action from its private
[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 <glib/gi18n.h>
31 #include <gtk/gtktreeviewcolumn.h>
32 #include <tny-account-store-view.h>
33 #include <tny-simple-list.h>
34 #include <tny-maemo-conic-device.h>
35 #include "modest-hildon-includes.h"
36 #include "modest-defs.h"
37 #include <string.h>
38
39 #include "widgets/modest-main-window.h"
40 #include "widgets/modest-msg-edit-window.h"
41 #include "widgets/modest-account-view-window.h"
42 #include "modest-runtime.h"
43 #include "modest-account-mgr-helpers.h"
44 #include "modest-platform.h"
45 #include "modest-widget-memory.h"
46 #include "modest-window-priv.h"
47 #include "modest-main-window-ui.h"
48 #include "modest-main-window-ui-dimming.h"
49 #include "modest-account-mgr.h"
50 #include "modest-tny-account.h"
51 #include "modest-conf.h"
52 #include <modest-maemo-utils.h>
53 #include "modest-tny-platform-factory.h"
54 #include "modest-tny-msg.h"
55 #include "modest-mail-operation.h"
56 #include "modest-icon-names.h"
57 #include "modest-progress-bar-widget.h"
58 #include "modest-text-utils.h"
59 #include "modest-ui-dimming-manager.h"
60 #include "maemo/modest-osso-state-saving.h"
61
62 #ifdef MODEST_HILDON_VERSION_0
63 #include <hildon-widgets/hildon-program.h>
64 #else
65 #include <hildon/hildon-program.h>
66 #endif /*MODEST_HILDON_VERSION_0*/
67
68 #define MODEST_MAIN_WINDOW_ACTION_GROUP_ADDITIONS "ModestMainWindowActionAdditions"
69
70 /* 'private'/'protected' functions */
71 static void modest_main_window_class_init    (ModestMainWindowClass *klass);
72 static void modest_main_window_init          (ModestMainWindow *obj);
73 static void modest_main_window_finalize      (GObject *obj);
74 static gboolean modest_main_window_window_state_event (GtkWidget *widget, 
75                                                            GdkEventWindowState *event, 
76                                                            gpointer userdata);
77
78 static void connect_signals (ModestMainWindow *self);
79
80 static void restore_settings (ModestMainWindow *self);
81 static void save_state (ModestWindow *self);
82
83 static void modest_main_window_show_toolbar   (ModestWindow *window,
84                                                gboolean show_toolbar);
85
86 static void cancel_progressbar (GtkToolButton *toolbutton,
87                                 ModestMainWindow *self);
88
89 static void         on_queue_changed                     (ModestMailOperationQueue *queue,
90                                                           ModestMailOperation *mail_op,
91                                                           ModestMailOperationQueueNotification type,
92                                                           ModestMainWindow *self);
93
94 static void on_account_update                 (TnyAccountStore *account_store, 
95                                                const gchar *account_name,
96                                                gpointer user_data);
97
98 static gboolean on_inner_widgets_key_pressed  (GtkWidget *widget,
99                                                GdkEventKey *event,
100                                                gpointer user_data);
101
102 static void on_configuration_key_changed      (ModestConf* conf, 
103                                                const gchar *key, 
104                                                ModestConfEvent event, 
105                                                ModestMainWindow *self);
106
107 static void set_toolbar_mode                  (ModestMainWindow *self, 
108                                                ModestToolBarModes mode);
109
110 static gboolean set_toolbar_transfer_mode     (ModestMainWindow *self); 
111
112 static void on_show_account_action_activated      (GtkAction *action,
113                                                    gpointer user_data);
114
115 static void on_refresh_account_action_activated   (GtkAction *action,
116                                                    gpointer user_data);
117
118 static void on_send_receive_csm_activated         (GtkMenuItem *item,
119                                                    gpointer user_data);
120 /* list my signals */
121 enum {
122         /* MY_SIGNAL_1, */
123         /* MY_SIGNAL_2, */
124         LAST_SIGNAL
125 };
126
127
128 typedef struct _ModestMainWindowPrivate ModestMainWindowPrivate;
129 struct _ModestMainWindowPrivate {
130         GtkWidget *msg_paned;
131         GtkWidget *main_paned;
132         GtkWidget *main_vbox;
133         GtkWidget *contents_widget;
134
135         /* Progress observers */
136         GtkWidget        *progress_bar;
137         GSList           *progress_widgets;
138
139         /* Tollbar items */
140         GtkWidget   *progress_toolitem;
141         GtkWidget   *cancel_toolitem;
142         GtkWidget   *sort_toolitem;
143         GtkWidget   *refresh_toolitem;
144         ModestToolBarModes current_toolbar_mode;
145
146         /* Merge ids used to add/remove accounts to the ViewMenu*/
147         GByteArray *merge_ids;
148
149         /* On-demand widgets */
150         GtkWidget *accounts_popup;
151         GtkWidget *details_widget;
152
153         /* Optimized view enabled */
154         gboolean optimized_view;
155
156         ModestHeaderView *header_view;
157         ModestFolderView *folder_view;
158
159         ModestMainWindowStyle style;
160         ModestMainWindowContentsStyle contents_style;
161
162         guint progress_bar_timeout;
163
164 };
165 #define MODEST_MAIN_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
166                                                 MODEST_TYPE_MAIN_WINDOW, \
167                                                 ModestMainWindowPrivate))
168
169 typedef struct _GetMsgAsyncHelper {
170         ModestMainWindowPrivate *main_window_private;
171         guint action;
172         ModestTnyMsgReplyType reply_type;
173         ModestTnyMsgForwardType forward_type;
174         gchar *from;
175         TnyIterator *iter;
176 } GetMsgAsyncHelper;
177
178
179 /* globals */
180 static GtkWindowClass *parent_class = NULL;
181
182
183 /* Private actions */
184 /* This is the context sensitive menu: */
185 static const GtkActionEntry modest_folder_view_action_entries [] = {
186
187         /* Folder View CSM actions */
188         { "FolderViewCSMNewFolder", NULL, N_("mcen_ti_new_folder"), NULL, NULL, G_CALLBACK (modest_ui_actions_on_new_folder) },
189         { "FolderViewCSMRenameFolder", NULL, N_("mcen_me_user_renamefolder"), NULL, NULL, G_CALLBACK (modest_ui_actions_on_rename_folder) },
190         { "FolderViewCSMPasteMsgs", NULL, N_("mcen_me_inbox_paste"), NULL, NULL, NULL },
191         { "FolderViewCSMDeleteFolder", NULL, N_("mcen_me_inbox_delete"), NULL, NULL, G_CALLBACK (modest_ui_actions_on_delete_folder) },
192         { "FolderViewCSMSearchMessages", NULL, N_("mcen_me_inbox_search"), NULL, NULL, NULL },
193         { "FolderViewCSMHelp", NULL, N_("mcen_me_inbox_help"), NULL, NULL, NULL },
194 };
195
196
197 static const GtkToggleActionEntry modest_main_window_toggle_action_entries [] = {
198         { "ToolbarToggleView", MODEST_STOCK_SPLIT_VIEW, N_("gqn_toolb_rss_fldonoff"), "<CTRL>t", NULL, G_CALLBACK (modest_ui_actions_toggle_folders_view), FALSE },
199 };
200
201 /************************************************************************/
202
203 GType
204 modest_main_window_get_type (void)
205 {
206         static GType my_type = 0;
207         if (!my_type) {
208                 static const GTypeInfo my_info = {
209                         sizeof(ModestMainWindowClass),
210                         NULL,           /* base init */
211                         NULL,           /* base finalize */
212                         (GClassInitFunc) modest_main_window_class_init,
213                         NULL,           /* class finalize */
214                         NULL,           /* class data */
215                         sizeof(ModestMainWindow),
216                         1,              /* n_preallocs */
217                         (GInstanceInitFunc) modest_main_window_init,
218                         NULL
219                 };
220                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
221                                                   "ModestMainWindow",
222                                                   &my_info, 0);
223         }
224         return my_type;
225 }
226
227 static void
228 modest_main_window_class_init (ModestMainWindowClass *klass)
229 {
230         GObjectClass *gobject_class;
231         gobject_class = (GObjectClass*) klass;
232         ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
233
234         parent_class            = g_type_class_peek_parent (klass);
235         gobject_class->finalize = modest_main_window_finalize;
236
237         g_type_class_add_private (gobject_class, sizeof(ModestMainWindowPrivate));
238         
239         modest_window_class->show_toolbar_func = modest_main_window_show_toolbar;
240         modest_window_class->save_state_func = save_state;
241 }
242
243 static void
244 modest_main_window_init (ModestMainWindow *obj)
245 {
246         ModestMainWindowPrivate *priv;
247
248         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
249
250         priv->msg_paned    = NULL;
251         priv->main_paned   = NULL;      
252         priv->main_vbox    = NULL;
253         priv->header_view  = NULL;
254         priv->folder_view  = NULL;
255         priv->contents_widget  = NULL;
256         priv->accounts_popup  = NULL;
257         priv->details_widget  = NULL;
258
259         priv->progress_widgets  = NULL;
260         priv->progress_bar = NULL;
261         priv->current_toolbar_mode = TOOLBAR_MODE_NORMAL;
262
263         priv->style  = MODEST_MAIN_WINDOW_STYLE_SPLIT;
264         priv->contents_style  = MODEST_MAIN_WINDOW_CONTENTS_STYLE_HEADERS;
265
266         priv->merge_ids = NULL;
267
268         priv->optimized_view  = FALSE;
269         priv->progress_bar_timeout = 0;
270 }
271
272 static void
273 modest_main_window_finalize (GObject *obj)
274 {
275         ModestMainWindowPrivate *priv;
276
277         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
278
279         g_slist_free (priv->progress_widgets);
280
281         g_byte_array_free (priv->merge_ids, TRUE);
282
283         if (priv->progress_bar_timeout > 0) {
284                 g_source_remove (priv->progress_bar_timeout);
285                 priv->progress_bar_timeout = 0;
286         }
287
288         G_OBJECT_CLASS(parent_class)->finalize (obj);
289 }
290
291 GtkWidget*
292 modest_main_window_get_child_widget (ModestMainWindow *self,
293                                      ModestWidgetType widget_type)
294 {
295         ModestMainWindowPrivate *priv;
296         GtkWidget *widget;
297         
298         g_return_val_if_fail (self, NULL);
299         g_return_val_if_fail (widget_type >= 0 && widget_type < MODEST_WIDGET_TYPE_NUM,
300                               NULL);
301         
302         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
303
304         switch (widget_type) {
305         case MODEST_WIDGET_TYPE_HEADER_VIEW:
306                 widget = (GtkWidget*)priv->header_view; break;
307         case MODEST_WIDGET_TYPE_FOLDER_VIEW:
308                 widget = (GtkWidget*)priv->folder_view; break;
309         default:
310                 return NULL;
311         }
312
313         return widget ? GTK_WIDGET(widget) : NULL;
314 }
315
316
317
318 static void
319 restore_settings (ModestMainWindow *self)
320 {
321         ModestConf *conf;
322         ModestMainWindowPrivate *priv;
323
324         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
325
326         conf = modest_runtime_get_conf ();
327         
328         modest_widget_memory_restore (conf, G_OBJECT(self),
329                                       MODEST_CONF_MAIN_WINDOW_KEY);
330         modest_widget_memory_restore (conf, G_OBJECT(priv->header_view),
331                                       MODEST_CONF_HEADER_VIEW_KEY);
332         modest_widget_memory_restore (conf, G_OBJECT(priv->main_paned),
333                                       MODEST_CONF_MAIN_PANED_KEY);
334         modest_widget_memory_restore (conf, G_OBJECT(priv->folder_view),
335                                       MODEST_CONF_FOLDER_VIEW_KEY);
336 }
337
338
339 static void
340 save_state (ModestWindow *window)
341 {
342         ModestConf *conf;
343         ModestMainWindow* self = MODEST_MAIN_WINDOW(window);
344         ModestMainWindowPrivate *priv;
345                 
346         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
347         conf = modest_runtime_get_conf ();
348         
349         modest_widget_memory_save (conf,G_OBJECT(self), 
350                                    MODEST_CONF_MAIN_WINDOW_KEY);
351         modest_widget_memory_save (conf, G_OBJECT(priv->main_paned), 
352                                    MODEST_CONF_MAIN_PANED_KEY);
353         modest_widget_memory_save (conf, G_OBJECT(priv->header_view), 
354                                    MODEST_CONF_HEADER_VIEW_KEY);
355         modest_widget_memory_save (conf, G_OBJECT(priv->folder_view), 
356                                    MODEST_CONF_FOLDER_VIEW_KEY);
357 }
358
359 static void
360 wrap_in_scrolled_window (GtkWidget *win, GtkWidget *widget)
361 {
362         if (!gtk_widget_set_scroll_adjustments (widget, NULL, NULL))
363                 gtk_scrolled_window_add_with_viewport
364                         (GTK_SCROLLED_WINDOW(win), widget);
365         else
366                 gtk_container_add (GTK_CONTAINER(win),
367                                    widget);
368 }
369
370
371 static gboolean
372 on_delete_event (GtkWidget *widget, GdkEvent  *event, ModestMainWindow *self)
373 {
374         modest_window_save_state (MODEST_WINDOW(self));
375         return FALSE;
376 }
377
378
379 static void
380 on_account_store_connecting_finished (TnyAccountStore *store, ModestMainWindow *self)
381 {
382         /* When going online, do the equivalent of pressing the send/receive button, 
383          * as per the specification:
384          * (without the check for >0 accounts, though that is not specified): */
385
386         TnyDevice *device = tny_account_store_get_device (store);
387         const gchar *iap_id = tny_maemo_conic_device_get_current_iap_id (TNY_MAEMO_CONIC_DEVICE (device));
388         printf ("DEBUG: %s: connection id=%s\n", __FUNCTION__, iap_id);
389         
390         /* Stop the existing send queues: */
391         modest_runtime_remove_all_send_queues ();
392         
393         /* Create the send queues again, using the appropriate transport accounts 
394          * for this new connection.
395          * This could be the first time that they are created if this is the first 
396          * connection. */
397         /* TODO: Does this really destroy the TnySendQueues and their threads
398          * We do not want 2 TnySendQueues to exist with the same underlying 
399          * outbox directory. */
400         GSList *account_names = modest_account_mgr_account_names (
401                 modest_runtime_get_account_mgr(), 
402                 TRUE /* enabled accounts only */);
403         GSList *iter = account_names;
404         while (iter) {
405                 const gchar *account_name = (const gchar*)(iter->data);
406                         if (account_name) {
407                         TnyTransportAccount *account = TNY_TRANSPORT_ACCOUNT (
408                                 modest_tny_account_store_get_transport_account_for_open_connection
409                                                  (modest_runtime_get_account_store(), account_name));
410                         if (account) {
411                                 printf ("debug: %s:\n  Transport account for %s: %s\n", __FUNCTION__, account_name, 
412                                         tny_account_get_id(TNY_ACCOUNT(account)));
413                                 modest_runtime_get_send_queue (account);
414                         }
415                 }
416                 
417                 iter = g_slist_next (iter);
418         }
419         
420         g_slist_free (account_names);
421         
422         
423         modest_ui_actions_do_send_receive (NULL, MODEST_WINDOW (self));
424 }
425
426
427
428 static void
429 connect_signals (ModestMainWindow *self)
430 {       
431         ModestWindowPrivate *parent_priv;
432         ModestMainWindowPrivate *priv;
433         GtkWidget *menu;
434         
435         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
436         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
437         
438         /* folder view */
439         g_signal_connect (G_OBJECT(priv->folder_view), "key-press-event",
440                           G_CALLBACK(on_inner_widgets_key_pressed), self);
441         g_signal_connect (G_OBJECT(priv->folder_view), "folder_selection_changed",
442                           G_CALLBACK(modest_ui_actions_on_folder_selection_changed), self);
443         g_signal_connect (G_OBJECT(priv->folder_view), "folder-display-name-changed",
444                           G_CALLBACK(modest_ui_actions_on_folder_display_name_changed), self);
445
446         menu = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/FolderViewCSM");
447         gtk_widget_tap_and_hold_setup (GTK_WIDGET (priv->folder_view), menu, NULL, 0);
448
449         /* header view */
450 /*      g_signal_connect (G_OBJECT(priv->header_view), "status_update", */
451 /*                        G_CALLBACK(modest_ui_actions_on_header_status_update), self); */
452         g_signal_connect (G_OBJECT(priv->header_view), "header_selected",
453                           G_CALLBACK(modest_ui_actions_on_header_selected), self);
454         g_signal_connect (G_OBJECT(priv->header_view), "header_activated",
455                           G_CALLBACK(modest_ui_actions_on_header_activated), self);
456         g_signal_connect (G_OBJECT(priv->header_view), "item_not_found",
457                           G_CALLBACK(modest_ui_actions_on_item_not_found), self);
458         g_signal_connect (G_OBJECT(priv->header_view), "key-press-event",
459                           G_CALLBACK(on_inner_widgets_key_pressed), self);
460
461         /* window */
462         g_signal_connect (G_OBJECT(self), "delete-event", G_CALLBACK(on_delete_event), self);
463         g_signal_connect (G_OBJECT (self), "window-state-event",
464                           G_CALLBACK (modest_main_window_window_state_event),
465                           NULL);
466         g_signal_connect (G_OBJECT(self), "delete-event", G_CALLBACK(on_delete_event), self);
467
468         /* Mail Operation Queue */
469         g_signal_connect (G_OBJECT (modest_runtime_get_mail_operation_queue ()),
470                           "queue-changed",
471                           G_CALLBACK (on_queue_changed),
472                           self);
473
474         /* Track changes in the device name */
475         g_signal_connect (G_OBJECT(modest_runtime_get_conf ()),
476                           "key_changed",
477                           G_CALLBACK (on_configuration_key_changed), 
478                           self);
479
480         /* Track account changes. We need to refresh the toolbar */
481         g_signal_connect (G_OBJECT (modest_runtime_get_account_store ()),
482                           "account_update",
483                           G_CALLBACK (on_account_update),
484                           self);
485
486         /* Account store */
487         g_signal_connect (G_OBJECT (modest_runtime_get_account_store()), "password_requested",
488                           G_CALLBACK (modest_ui_actions_on_password_requested), self);
489                           
490         /* Device */
491         g_signal_connect (G_OBJECT(modest_runtime_get_account_store()), "connecting-finished",
492                           G_CALLBACK(on_account_store_connecting_finished), self);
493 }
494
495 #if 0
496 /** Idle handler, to send/receive at startup .*/
497 gboolean
498 sync_accounts_cb (ModestMainWindow *win)
499 {
500         modest_ui_actions_do_send_receive (NULL, MODEST_WINDOW (win));
501         return FALSE; /* Do not call this idle handler again. */
502 }
503 #endif
504
505 static void on_hildon_program_is_topmost_notify(GObject *self,
506         GParamSpec *propert_param, gpointer user_data)
507 {
508         HildonProgram *app = HILDON_PROGRAM (self);
509         
510         /*
511         ModestWindow* self = MODEST_WINDOW(user_data);
512         */
513         
514         /* Note that use of hildon_program_set_can_hibernate() 
515          * is generally referred to as "setting the killable flag", 
516          * though hibernation does not seem equal to death.
517          * murrayc */
518                  
519         if (hildon_program_get_is_topmost (app)) {
520                 /* Prevent hibernation when the progam comes to the foreground,
521                  * because hibernation should only happen when the application 
522                  * is in the background: */
523                 hildon_program_set_can_hibernate (app, FALSE);
524         } else {
525                 /* Allow hibernation if the program has gone to the background: */
526                 
527                 /* However, prevent hibernation while the settings are being changed: */
528                 const gboolean hibernation_prevented = 
529                         modest_window_mgr_get_hibernation_is_prevented (
530         modest_runtime_get_window_mgr ()); 
531         
532                 if (hibernation_prevented)
533                         hildon_program_set_can_hibernate (app, FALSE);
534                 else {
535                         /* Allow hibernation, after saving the state: */
536                         modest_osso_save_state();
537                         hildon_program_set_can_hibernate (app, TRUE);
538                 }
539         }
540         
541 }
542
543
544
545 ModestWindow*
546 modest_main_window_new (void)
547 {
548         ModestMainWindow *self; 
549         ModestMainWindowPrivate *priv;
550         ModestWindowPrivate *parent_priv;
551         GtkWidget *folder_win;
552         ModestDimmingRulesGroup *rules_group;
553         GtkActionGroup *action_group;
554         GError *error = NULL;
555         TnyFolderStoreQuery *query;
556         GdkPixbuf *window_icon;
557         ModestConf *conf;
558         GtkAction *action;
559
560         self  = MODEST_MAIN_WINDOW(g_object_new(MODEST_TYPE_MAIN_WINDOW, NULL));
561         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
562         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
563
564         parent_priv->ui_manager = gtk_ui_manager_new();
565         parent_priv->ui_dimming_manager = modest_ui_dimming_manager_new();
566
567         action_group = gtk_action_group_new ("ModestMainWindowActions");
568         gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
569
570         rules_group = modest_dimming_rules_group_new ("ModestCommonDimmingRules");
571
572         /* Add common actions */
573         gtk_action_group_add_actions (action_group,
574                                       modest_action_entries,
575                                       G_N_ELEMENTS (modest_action_entries),
576                                       self);
577
578         gtk_action_group_add_actions (action_group,
579                                       modest_folder_view_action_entries,
580                                       G_N_ELEMENTS (modest_folder_view_action_entries),
581                                       self);
582
583         gtk_action_group_add_toggle_actions (action_group,
584                                              modest_toggle_action_entries,
585                                              G_N_ELEMENTS (modest_toggle_action_entries),
586                                              self);
587
588         gtk_action_group_add_toggle_actions (action_group,
589                                              modest_main_window_toggle_action_entries,
590                                              G_N_ELEMENTS (modest_main_window_toggle_action_entries),
591                                              self);
592
593         gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 0);
594         g_object_unref (action_group);
595
596         /* Load the UI definition */
597         gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager,
598                                          MODEST_UIDIR "modest-main-window-ui.xml", &error);
599         if (error != NULL) {
600                 g_warning ("Could not merge modest-ui.xml: %s", error->message);
601                 g_error_free (error);
602                 error = NULL;
603         }
604
605         /* Add common dimming rules */
606         modest_dimming_rules_group_add_rules (rules_group, 
607                                               modest_dimming_entries,
608                                               G_N_ELEMENTS (modest_dimming_entries),
609                                               self);
610
611         /* Insert dimming rules group for this window */
612         modest_ui_dimming_manager_insert_rules_group (parent_priv->ui_dimming_manager, rules_group);                                                     
613         g_object_unref (rules_group);
614         
615         /* Add accelerators */
616         gtk_window_add_accel_group (GTK_WINDOW (self), 
617                                     gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
618
619         /* Menubar. Update the state of some toggles */
620         parent_priv->menubar = modest_maemo_utils_menubar_to_menu (parent_priv->ui_manager);
621         conf = modest_runtime_get_conf ();
622         action = gtk_ui_manager_get_action (parent_priv->ui_manager, 
623                                             "/MenuBar/ViewMenu/ViewShowToolbarMainMenu/ViewShowToolbarNormalScreenMenu");
624         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
625                                       modest_conf_get_bool (conf, MODEST_CONF_SHOW_TOOLBAR, NULL));
626         action = gtk_ui_manager_get_action (parent_priv->ui_manager, 
627                                             "/MenuBar/ViewMenu/ViewShowToolbarMainMenu/ViewShowToolbarFullScreenMenu");
628         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
629                                       modest_conf_get_bool (conf, MODEST_CONF_SHOW_TOOLBAR_FULLSCREEN, NULL));
630         hildon_window_set_menu (HILDON_WINDOW (self), GTK_MENU (parent_priv->menubar));
631
632         /* Get device name */
633         modest_maemo_utils_get_device_name ();
634
635         /* folder view */
636         query = tny_folder_store_query_new ();
637         tny_folder_store_query_add_item (query, NULL,
638                                          TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
639         priv->folder_view = MODEST_FOLDER_VIEW(modest_folder_view_new (query));
640         if (!priv->folder_view)
641                 g_printerr ("modest: cannot instantiate folder view\n");
642         g_object_unref (G_OBJECT (query));
643         modest_folder_view_set_style (priv->folder_view,
644                                       MODEST_FOLDER_VIEW_STYLE_SHOW_ONE);
645
646         /* header view */
647         priv->header_view  =
648                 MODEST_HEADER_VIEW(modest_header_view_new (NULL, MODEST_HEADER_VIEW_STYLE_DETAILS));
649         if (!priv->header_view)
650                 g_printerr ("modest: cannot instantiate header view\n");
651         modest_header_view_set_style (priv->header_view, MODEST_HEADER_VIEW_STYLE_TWOLINES);
652         
653         /* Create scrolled windows */
654         folder_win = gtk_scrolled_window_new (NULL, NULL);
655         priv->contents_widget = gtk_scrolled_window_new (NULL, NULL);
656         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (folder_win),
657                                         GTK_POLICY_NEVER,
658                                         GTK_POLICY_AUTOMATIC);
659         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->contents_widget),
660                                         GTK_POLICY_NEVER,
661                                         GTK_POLICY_AUTOMATIC);
662
663         wrap_in_scrolled_window (folder_win, GTK_WIDGET(priv->folder_view));
664         wrap_in_scrolled_window (priv->contents_widget, GTK_WIDGET(priv->header_view));
665
666         /* paned */
667         priv->main_paned = gtk_hpaned_new ();
668         gtk_paned_add1 (GTK_PANED(priv->main_paned), folder_win);
669         gtk_paned_add2 (GTK_PANED(priv->main_paned), priv->contents_widget);
670         gtk_widget_show (GTK_WIDGET(priv->header_view));
671         gtk_tree_view_columns_autosize (GTK_TREE_VIEW(priv->header_view));
672
673         /* putting it all together... */
674         priv->main_vbox = gtk_vbox_new (FALSE, 6);
675         gtk_box_pack_start (GTK_BOX(priv->main_vbox), priv->main_paned, TRUE, TRUE,0);
676
677         gtk_container_add (GTK_CONTAINER(self), priv->main_vbox);
678         restore_settings (MODEST_MAIN_WINDOW(self));
679
680         /* Set window icon */
681         window_icon = modest_platform_get_icon (MODEST_APP_ICON);
682         gtk_window_set_icon (GTK_WINDOW (self), window_icon);
683         
684         /* Connect signals */
685         connect_signals (self);
686
687         /* Set account store */
688         tny_account_store_view_set_account_store (TNY_ACCOUNT_STORE_VIEW (priv->folder_view),
689                                                   TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
690
691         /* Do send & receive when we are idle */
692         /* TODO: Enable this again. I have commented it out because, 
693          * at least in scratchbox, this can cause us to start a second 
694          * update (in response to a connection change) when we are already 
695          * doing an update (started here, at startup). Tinymail doesn't like that.
696          * murrayc.
697          */
698         /* g_idle_add ((GSourceFunc)sync_accounts_cb, self); */
699         
700         HildonProgram *app = hildon_program_get_instance ();
701         hildon_program_add_window (app, HILDON_WINDOW (self));
702         
703         /* Register HildonProgram  signal handlers: */
704         /* These are apparently deprecated, according to the 
705          * "HildonApp/HildonAppView to HildonProgram/HildonWindow migration guide",
706          * though the API reference does not mention that:
707          *
708         g_signal_connect (G_OBJECT(app), "topmost_status_lose",
709                 G_CALLBACK (on_hildon_program_save_state), self);
710         g_signal_connect (G_OBJECT(app), "topmost_status_acquire",
711                 G_CALLBACK (on_hildon_program_status_acquire), self);
712     */
713         g_signal_connect (G_OBJECT(app), "notify::is-topmost",
714                 G_CALLBACK (on_hildon_program_is_topmost_notify), self);
715                 
716         /* Load previous osso state, for instance if we are being restored from 
717          * hibernation:  */
718         modest_osso_load_state();
719
720         return MODEST_WINDOW(self);
721 }
722
723 gboolean 
724 modest_main_window_close_all (ModestMainWindow *self)
725 {
726         GtkWidget *note;
727         GtkResponseType response;
728
729         /* Create the confirmation dialog MSG-NOT308 */
730         note = hildon_note_new_confirmation_add_buttons (GTK_WINDOW (self),
731                                                          _("emev_nc_close_windows"),
732                                                          _("mcen_bd_yes"), GTK_RESPONSE_YES,
733                                                          _("mcen_bd_no"), GTK_RESPONSE_NO,
734                                                          NULL);
735
736         response = gtk_dialog_run (GTK_DIALOG (note));
737         gtk_widget_destroy (GTK_WIDGET (note));
738
739         if (response == GTK_RESPONSE_YES)
740                 return TRUE;
741         else
742                 return FALSE;
743 }
744
745
746 void 
747 modest_main_window_set_style (ModestMainWindow *self, 
748                               ModestMainWindowStyle style)
749 {
750         ModestMainWindowPrivate *priv;
751         ModestWindowPrivate *parent_priv;
752         GtkAction *action;
753
754         g_return_if_fail (MODEST_IS_MAIN_WINDOW (self));
755
756         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
757         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
758
759         /* no change -> nothing to do */
760         if (priv->style == style)
761                 return;
762
763         /* Get toggle button */
764         action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ToolbarToggleView");
765
766         priv->style = style;
767         switch (style) {
768         case MODEST_MAIN_WINDOW_STYLE_SIMPLE:
769                 /* Remove main paned */
770                 g_object_ref (priv->main_paned);
771                 gtk_container_remove (GTK_CONTAINER (priv->main_vbox), priv->main_paned);
772
773                 /* Reparent the contents widget to the main vbox */
774                 gtk_widget_reparent (priv->contents_widget, priv->main_vbox);
775
776                 g_signal_handlers_block_by_func (action, modest_ui_actions_toggle_folders_view, self);
777                 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
778                 g_signal_handlers_unblock_by_func (action, modest_ui_actions_toggle_folders_view, self);
779
780                 break;
781         case MODEST_MAIN_WINDOW_STYLE_SPLIT:
782                 /* Remove header view */
783                 g_object_ref (priv->contents_widget);
784                 gtk_container_remove (GTK_CONTAINER (priv->main_vbox), priv->contents_widget);
785
786                 /* Reparent the main paned */
787                 gtk_paned_add2 (GTK_PANED (priv->main_paned), priv->contents_widget);
788                 gtk_container_add (GTK_CONTAINER (priv->main_vbox), priv->main_paned);
789
790                 g_signal_handlers_block_by_func (action, modest_ui_actions_toggle_folders_view, self);
791                 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), FALSE);
792                 g_signal_handlers_unblock_by_func (action, modest_ui_actions_toggle_folders_view, self);
793
794                 break;
795         default:
796                 g_return_if_reached ();
797         }
798
799         /* Let header view grab the focus if it's being shown */
800         if (priv->contents_style == MODEST_MAIN_WINDOW_CONTENTS_STYLE_HEADERS)
801                 gtk_widget_grab_focus (GTK_WIDGET (priv->header_view));
802         else 
803                 gtk_widget_grab_focus (GTK_WIDGET (priv->contents_widget));
804
805         /* Show changes */
806         gtk_widget_show_all (GTK_WIDGET (priv->main_vbox));
807 }
808
809 ModestMainWindowStyle
810 modest_main_window_get_style (ModestMainWindow *self)
811 {
812         ModestMainWindowPrivate *priv;
813
814         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW (self), -1);
815
816         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
817         return priv->style;
818 }
819
820
821
822 static gboolean
823 modest_main_window_window_state_event (GtkWidget *widget, GdkEventWindowState *event, gpointer userdata)
824 {
825         if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN) {
826                 ModestWindowPrivate *parent_priv;
827                 ModestWindowMgr *mgr;
828                 gboolean is_fullscreen;
829                 GtkAction *fs_toggle_action;
830                 gboolean active;
831                 
832                 mgr = modest_runtime_get_window_mgr ();
833                 
834                 is_fullscreen = modest_window_mgr_get_fullscreen_mode (mgr);
835
836                 parent_priv = MODEST_WINDOW_GET_PRIVATE (widget);
837                 
838                 fs_toggle_action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/MenuBar/ViewMenu/ViewToggleFullscreenMenu");
839                 active = (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (fs_toggle_action)))?1:0;
840                 if (is_fullscreen != active) {
841                         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (fs_toggle_action), is_fullscreen);
842                 }
843         }
844
845         return FALSE;
846
847 }
848
849 static void
850 set_homogeneous (GtkWidget *widget,
851                  gpointer data)
852 {
853         gtk_tool_item_set_expand (GTK_TOOL_ITEM (widget), TRUE);
854         gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (widget), TRUE);
855 }
856
857 static void 
858 modest_main_window_show_toolbar (ModestWindow *self,
859                                  gboolean show_toolbar)
860 {
861         ModestMainWindowPrivate *priv = NULL;
862         ModestWindowPrivate *parent_priv = NULL;        
863         GtkWidget *reply_button = NULL, *menu = NULL;
864         GtkWidget *placeholder = NULL;
865         gint insert_index;
866
867         g_return_if_fail (MODEST_IS_MAIN_WINDOW (self));
868         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
869         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
870
871         /* Set optimized view status */
872         priv->optimized_view = !show_toolbar;
873
874         if (!parent_priv->toolbar) {
875                 parent_priv->toolbar = gtk_ui_manager_get_widget (parent_priv->ui_manager, 
876                                                                   "/ToolBar");
877
878                 /* Set homogeneous toolbar */
879                 gtk_container_foreach (GTK_CONTAINER (parent_priv->toolbar), 
880                                        set_homogeneous, NULL);
881         
882                 priv->progress_toolitem = GTK_WIDGET (gtk_tool_item_new ());
883                 priv->cancel_toolitem = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/ToolbarCancel");
884                 priv->refresh_toolitem = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/ToolbarSendReceive");
885                 priv->sort_toolitem = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/ToolbarSort");
886                 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (priv->progress_toolitem), TRUE);
887                 gtk_tool_item_set_expand (GTK_TOOL_ITEM (priv->progress_toolitem), TRUE);
888                 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (priv->cancel_toolitem), FALSE);
889                 gtk_tool_item_set_expand (GTK_TOOL_ITEM (priv->cancel_toolitem), FALSE);
890
891                 /* Add ProgressBar (Transfer toolbar) */ 
892                 priv->progress_bar = modest_progress_bar_widget_new ();
893                 gtk_widget_set_no_show_all (priv->progress_bar, TRUE);
894                 placeholder = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/ProgressBarView");
895                 insert_index = gtk_toolbar_get_item_index(GTK_TOOLBAR (parent_priv->toolbar), GTK_TOOL_ITEM(placeholder));
896                 gtk_container_add (GTK_CONTAINER (priv->progress_toolitem), priv->progress_bar);
897                 gtk_toolbar_insert(GTK_TOOLBAR(parent_priv->toolbar), GTK_TOOL_ITEM (priv->progress_toolitem), insert_index);
898                 
899                 /* Connect cancel 'clicked' signal to abort progress mode */
900                 g_signal_connect(priv->cancel_toolitem, "clicked",
901                                  G_CALLBACK(cancel_progressbar),
902                                  self);
903                 
904                 /* Add it to the observers list */
905                 priv->progress_widgets = g_slist_prepend(priv->progress_widgets, priv->progress_bar);
906
907                 /* Add to window */
908                 hildon_window_add_toolbar (HILDON_WINDOW (self), 
909                                            GTK_TOOLBAR (parent_priv->toolbar));
910
911                 /* Set reply button tap and hold menu */
912                 reply_button = gtk_ui_manager_get_widget (parent_priv->ui_manager, 
913                                                           "/ToolBar/ToolbarMessageReply");
914                 menu = gtk_ui_manager_get_widget (parent_priv->ui_manager,
915                                                   "/ToolbarReplyCSM");
916                 gtk_widget_tap_and_hold_setup (GTK_WIDGET (reply_button), menu, NULL, 0);
917
918                 /* Set send & receive button tap and hold menu */
919                 on_account_update (TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()),
920                                    NULL, self);
921         }
922
923         if (show_toolbar) {
924                 /* Quick hack: this prevents toolbar icons "dance" when progress bar show status is changed */ 
925                 /* TODO: resize mode migth be GTK_RESIZE_QUEUE, in order to avoid unneccesary shows */
926                 gtk_container_set_resize_mode (GTK_CONTAINER(parent_priv->toolbar), GTK_RESIZE_IMMEDIATE);
927
928                 gtk_widget_show (GTK_WIDGET (parent_priv->toolbar));
929                 set_toolbar_mode (MODEST_MAIN_WINDOW(self), TOOLBAR_MODE_NORMAL);
930         } else
931                 gtk_widget_hide (GTK_WIDGET (parent_priv->toolbar));
932
933 }
934
935 static gint
936 compare_display_names (ModestAccountData *a,
937                        ModestAccountData *b)
938 {
939         return strcmp (a->display_name, b->display_name);
940 }
941
942 static void 
943 on_account_update (TnyAccountStore *account_store, 
944                    const gchar *account_name,
945                    gpointer user_data)
946 {
947         GSList *account_names, *iter, *accounts;
948         ModestMainWindow *self;
949         ModestMainWindowPrivate *priv;
950         ModestWindowPrivate *parent_priv;
951         ModestAccountMgr *mgr;
952         gint i, num_accounts;                                   
953         GtkActionGroup *action_group;
954         GList *groups;
955         gchar *default_account;
956         GtkWidget *send_receive_button, *item;
957                 
958         self = MODEST_MAIN_WINDOW (user_data);
959         priv = MODEST_MAIN_WINDOW_GET_PRIVATE (self);
960         parent_priv = MODEST_WINDOW_GET_PRIVATE (self);
961
962         /* Get enabled account IDs */
963         mgr = modest_runtime_get_account_mgr ();
964         account_names = modest_account_mgr_account_names (mgr, TRUE);
965         iter = account_names;
966         accounts = NULL;
967
968         while (iter) {
969                 ModestAccountData *account_data = 
970                         modest_account_mgr_get_account_data (mgr, (gchar*) iter->data);
971                 accounts = g_slist_prepend (accounts, account_data);
972
973                 iter = iter->next;
974         }
975         g_slist_free (account_names);
976
977         /* Order the list of accounts by its display name */
978         accounts = g_slist_sort (accounts, (GCompareFunc) compare_display_names);
979         num_accounts = g_slist_length (accounts);
980
981         /* Delete old send&receive popup items. We can not just do a
982            menu_detach because it does not work well with
983            tap_and_hold */
984         if (priv->accounts_popup)
985                 gtk_container_foreach (GTK_CONTAINER (priv->accounts_popup), 
986                                        (GtkCallback) gtk_widget_destroy, NULL);
987
988         /* Delete old entries in the View menu. Do not free groups, it
989            belongs to Gtk+ */
990         groups = gtk_ui_manager_get_action_groups (parent_priv->ui_manager);
991         while (groups) {
992                 if (!strcmp (MODEST_MAIN_WINDOW_ACTION_GROUP_ADDITIONS,
993                              gtk_action_group_get_name (GTK_ACTION_GROUP (groups->data)))) {
994                         gtk_ui_manager_remove_action_group (parent_priv->ui_manager, 
995                                                             GTK_ACTION_GROUP (groups->data));
996                         groups = NULL;
997                         /* Remove uis */
998                         if (priv->merge_ids) {
999                                 for (i = 0; i < priv->merge_ids->len; i++)
1000                                         gtk_ui_manager_remove_ui (parent_priv->ui_manager, priv->merge_ids->data[i]);
1001                                 g_byte_array_free (priv->merge_ids, TRUE);
1002                         }
1003                         /* We need to call this in order to ensure
1004                            that the new actions are added in the right
1005                            order (alphabetical */
1006                         gtk_ui_manager_ensure_update (parent_priv->ui_manager);
1007                 } else 
1008                         groups = g_list_next (groups);
1009         }
1010         priv->merge_ids = g_byte_array_sized_new (num_accounts);
1011
1012         /* Get send receive button */
1013         send_receive_button = gtk_ui_manager_get_widget (parent_priv->ui_manager,
1014                                                           "/ToolBar/ToolbarSendReceive");
1015
1016         /* Create the menu */
1017         if (num_accounts > 1) {
1018                 if (!priv->accounts_popup)
1019                         priv->accounts_popup = gtk_menu_new ();
1020                 item = gtk_menu_item_new_with_label (_("mcen_me_toolbar_sendreceive_all"));
1021                 gtk_menu_shell_append (GTK_MENU_SHELL (priv->accounts_popup), GTK_WIDGET (item));
1022                 g_signal_connect (G_OBJECT (item), 
1023                                   "activate", 
1024                                   G_CALLBACK (on_send_receive_csm_activated),
1025                                   NULL);
1026                 item = gtk_separator_menu_item_new ();
1027                 gtk_menu_shell_append (GTK_MENU_SHELL (priv->accounts_popup), GTK_WIDGET (item));
1028         }
1029
1030         /* Create a new action group */
1031         default_account = modest_account_mgr_get_default_account (mgr);
1032         action_group = gtk_action_group_new (MODEST_MAIN_WINDOW_ACTION_GROUP_ADDITIONS);
1033         for (i = 0; i < num_accounts; i++) {
1034                 gchar *display_name = NULL;
1035                 
1036                 ModestAccountData *account_data = (ModestAccountData *) g_slist_nth_data (accounts, i);
1037
1038                 /* Create display name. The default account is shown differently */
1039                 if (default_account && account_data->account_name && 
1040                         !(strcmp (default_account, account_data->account_name) == 0)) {
1041                         display_name = g_strdup_printf (_("mcen_me_toolbar_sendreceive_default"), 
1042                                                         account_data->display_name);
1043                 }
1044                 else {
1045                         display_name = g_strdup_printf (_("mcen_me_toolbar_sendreceive_mailbox_n"), 
1046                                                         account_data->display_name);
1047                 }
1048
1049                 /* Create action and add it to the action group. The
1050                    action name must be the account name, this way we
1051                    could know in the handlers the account to show */
1052                 if(account_data->account_name) {
1053                         gchar* item_name, *refresh_action_name;
1054                         guint8 merge_id;
1055                         GtkAction *view_account_action, *refresh_account_action;
1056
1057                         view_account_action = gtk_action_new (account_data->account_name,
1058                                                               display_name, NULL, NULL);
1059                         gtk_action_group_add_action (action_group, view_account_action);
1060
1061                         /* Add ui from account data. We allow 2^9-1 account
1062                            changes in a single execution because we're
1063                            downcasting the guint to a guint8 in order to use a
1064                            GByteArray, it should be enough */
1065                         item_name = g_strconcat (account_data->account_name, "Menu", NULL);
1066                         merge_id = (guint8) gtk_ui_manager_new_merge_id (parent_priv->ui_manager);
1067                         priv->merge_ids = g_byte_array_append (priv->merge_ids, &merge_id, 1);
1068                         gtk_ui_manager_add_ui (parent_priv->ui_manager,
1069                                                merge_id,
1070                                                "/MenuBar/ViewMenu/ViewMenuAdditions",
1071                                                item_name,
1072                                                account_data->account_name,
1073                                                GTK_UI_MANAGER_MENUITEM,
1074                                                FALSE);
1075         
1076                         /* Connect the action signal "activate" */
1077                         g_signal_connect (G_OBJECT (view_account_action),
1078                                           "activate",
1079                                           G_CALLBACK (on_show_account_action_activated),
1080                                           self);
1081
1082                         /* Create the items for the Tools->Send&Receive submenu */
1083                         refresh_action_name = g_strconcat ("SendReceive", account_data->account_name, NULL);
1084                         refresh_account_action = gtk_action_new ((const gchar*) refresh_action_name, 
1085                                                                  display_name, NULL, NULL);
1086                         gtk_action_group_add_action (action_group, refresh_account_action);
1087
1088                         merge_id = (guint8) gtk_ui_manager_new_merge_id (parent_priv->ui_manager);
1089                         priv->merge_ids = g_byte_array_append (priv->merge_ids, &merge_id, 1);
1090                         gtk_ui_manager_add_ui (parent_priv->ui_manager, 
1091                                                merge_id,
1092                                                "/MenuBar/ToolsMenu/ToolsSendReceiveMainMenu/ToolsMenuAdditions",
1093                                                item_name,
1094                                                refresh_action_name,
1095                                                GTK_UI_MANAGER_MENUITEM,
1096                                                FALSE);
1097                         g_free (refresh_action_name);
1098
1099                         g_signal_connect_data (G_OBJECT (refresh_account_action), 
1100                                                "activate", 
1101                                                G_CALLBACK (on_refresh_account_action_activated), 
1102                                                g_strdup (account_data->account_name),
1103                                                (GClosureNotify) g_free,
1104                                                0);
1105
1106                         /* Create item and add it to the send&receive
1107                            CSM. If there is only one account then
1108                            it'll be no menu */
1109                         if (priv->accounts_popup) {
1110                                 item = gtk_menu_item_new_with_label (display_name);
1111                                 gtk_menu_shell_append (GTK_MENU_SHELL (priv->accounts_popup), GTK_WIDGET (item));
1112                                 g_signal_connect_data (G_OBJECT (item), 
1113                                                        "activate", 
1114                                                        G_CALLBACK (on_send_receive_csm_activated),
1115                                                        g_strdup (account_data->account_name),
1116                                                        (GClosureNotify) g_free,
1117                                                        0);
1118                         }
1119                         g_free (item_name);
1120                 }
1121
1122                 /* Frees */
1123                 g_free (display_name);
1124                 modest_account_mgr_free_account_data (mgr, account_data);
1125         }
1126         gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 1);
1127
1128         if (priv->accounts_popup) {
1129                 /* Mandatory in order to view the menu contents */
1130                 gtk_widget_show_all (priv->accounts_popup);
1131
1132                 /* Setup tap_and_hold just if was not done before*/
1133                 if (!gtk_menu_get_attach_widget (GTK_MENU (priv->accounts_popup)))
1134                         gtk_widget_tap_and_hold_setup (send_receive_button, priv->accounts_popup, NULL, 0);
1135         }
1136
1137         /* Frees */
1138         g_slist_free (accounts);
1139         g_free (default_account);
1140 }
1141
1142 /* 
1143  * This function manages the key events used to navigate between
1144  * header and folder views (when the window is in split view)
1145  *
1146  * FROM         KEY        ACTION
1147  * -------------------------------------------------
1148  * HeaderView   GDK_Left   Move focus to folder view
1149  * FolderView   GDK_Right  Move focus to header view
1150  *
1151  * There is no need to scroll to selected row, the widgets will be the
1152  * responsibles of doing that (probably managing the focus-in event
1153  */
1154 static gboolean 
1155 on_inner_widgets_key_pressed (GtkWidget *widget,
1156                               GdkEventKey *event,
1157                               gpointer user_data)
1158 {
1159         ModestMainWindowPrivate *priv;
1160
1161         priv = MODEST_MAIN_WINDOW_GET_PRIVATE (user_data);
1162
1163         /* Do nothing if we're in SIMPLE style */
1164         if (priv->style == MODEST_MAIN_WINDOW_STYLE_SIMPLE)
1165                 return FALSE;
1166
1167         if (MODEST_IS_HEADER_VIEW (widget) && event->keyval == GDK_Left)
1168                 gtk_widget_grab_focus (GTK_WIDGET (priv->folder_view));
1169         else if (MODEST_IS_FOLDER_VIEW (widget) && event->keyval == GDK_Right)
1170                 gtk_widget_grab_focus (GTK_WIDGET (priv->header_view));
1171
1172         return FALSE;
1173 }
1174
1175 static void
1176 set_alignment (GtkWidget *widget,
1177                gpointer data)
1178 {
1179         gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.0);
1180         gtk_misc_set_padding (GTK_MISC (widget), 0, 0);
1181 }
1182
1183 static GtkWidget *
1184 create_details_widget (TnyFolderStore *folder_store)
1185 {
1186         GtkWidget *vbox;
1187         gchar *label;
1188
1189         vbox = gtk_vbox_new (FALSE, 0);
1190
1191         /* Account description: */
1192         
1193         if (modest_tny_folder_store_is_virtual_local_folders (folder_store)) {
1194                 /* Local folders: */
1195         
1196                 /* Get device name */
1197                 gchar *device_name = modest_conf_get_string (modest_runtime_get_conf(),
1198                                                       MODEST_CONF_DEVICE_NAME, NULL);
1199    
1200                 label = g_strdup_printf ("%s: %s",
1201                                          _("mcen_fi_localroot_description"),
1202                                          device_name);
1203                 gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new (label), FALSE, FALSE, 0);
1204                 g_free (device_name);
1205                 g_free (label);
1206         } else if (TNY_IS_ACCOUNT (folder_store)) {
1207                 TnyAccount *account = TNY_ACCOUNT(folder_store);
1208                 
1209                 if(!strcmp (tny_account_get_id (account), MODEST_MMC_ACCOUNT_ID)) {
1210                         /* TODO: MMC ? */
1211                         gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new ("FIXME: MMC ?"), FALSE, FALSE, 0);
1212                 } else {
1213                         /* Other accounts, such as IMAP and POP: */
1214                         
1215                         GString *proto;
1216         
1217                         /* Put proto in uppercase */
1218                         proto = g_string_new (tny_account_get_proto (account));
1219                         proto = g_string_ascii_up (proto);
1220         
1221                         label = g_strdup_printf ("%s %s: %s", 
1222                                                  proto->str,
1223                                                  _("mcen_fi_remoteroot_account"),
1224                                                  tny_account_get_name (account));
1225                         gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new (label), FALSE, FALSE, 0);
1226                         g_string_free (proto, TRUE);
1227                         g_free (label);
1228                 }
1229         }
1230
1231         /* Message count */
1232         
1233         label = g_strdup_printf ("%s: %d", _("mcen_fi_rootfolder_messages"), 
1234                                  modest_tny_folder_store_get_message_count (folder_store));
1235         gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new (label), FALSE, FALSE, 0);
1236         g_free (label);
1237
1238         /* Folder count */
1239         label = g_strdup_printf ("%s: %d", _("mcen_fi_rootfolder_folders"), 
1240                                  modest_tny_folder_store_get_folder_count (folder_store));
1241         gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new (label), FALSE, FALSE, 0);
1242         g_free (label);
1243
1244         /* Size / Date */
1245         if (modest_tny_folder_store_is_virtual_local_folders (folder_store)) {
1246                 /* FIXME: format size */
1247                 label = g_strdup_printf ("%s: %d", _("mcen_fi_rootfolder_size"), 
1248                                          modest_tny_folder_store_get_local_size (folder_store));
1249                 gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new (label), FALSE, FALSE, 0);
1250                 g_free (label);
1251         } else if (TNY_IS_ACCOUNT(folder_store)) {
1252                 TnyAccount *account = TNY_ACCOUNT(folder_store);
1253                 
1254                 if (!strcmp (tny_account_get_id (account), MODEST_MMC_ACCOUNT_ID)) {
1255                         gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new ("FIXME: MMC ?"), FALSE, FALSE, 0);
1256                 } else {
1257                         time_t last_updated;
1258                         gchar *last_updated_string;
1259                         /* Get last updated from configuration */
1260                         last_updated = modest_account_mgr_get_int (modest_runtime_get_account_mgr (), 
1261                                                                   tny_account_get_id (account), 
1262                                                                   MODEST_ACCOUNT_LAST_UPDATED, 
1263                                                                   TRUE);
1264                         if (last_updated > 0) 
1265                                 last_updated_string = modest_text_utils_get_display_date(last_updated);
1266                         else
1267                                 last_updated_string = g_strdup (_("FIXME: Never"));
1268         
1269                         label = g_strdup_printf ("%s: %s", _("mcen_ti_lastupdated"), last_updated_string);
1270                         gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new (label), FALSE, FALSE, 0);
1271                         g_free (last_updated_string);
1272                         g_free (label);
1273                 }
1274         }
1275
1276         /* Set alignment */
1277         gtk_container_foreach (GTK_CONTAINER (vbox), (GtkCallback) set_alignment, NULL);
1278
1279         return vbox;
1280 }
1281
1282 void 
1283 modest_main_window_set_contents_style (ModestMainWindow *self, 
1284                                        ModestMainWindowContentsStyle style)
1285 {
1286         ModestMainWindowPrivate *priv;
1287
1288         g_return_if_fail (MODEST_IS_MAIN_WINDOW (self));
1289
1290         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
1291
1292         /* We allow to set the same content style than the previously
1293            set if there are details, because it could happen when we're
1294            selecting different accounts consecutively */
1295         if ((priv->contents_style == style) &&
1296             (priv->contents_style == MODEST_MAIN_WINDOW_CONTENTS_STYLE_HEADERS))
1297                 return;
1298
1299         /* Remove previous child. Delete it if it was an account
1300            details widget */
1301         GtkWidget *content = gtk_bin_get_child (GTK_BIN (priv->contents_widget));
1302         if (content) {
1303                 if (priv->contents_style != MODEST_MAIN_WINDOW_CONTENTS_STYLE_DETAILS)
1304                         g_object_ref (content);
1305                 gtk_container_remove (GTK_CONTAINER (priv->contents_widget), content);
1306         }
1307
1308         priv->contents_style = style;
1309
1310         switch (priv->contents_style) {
1311         case MODEST_MAIN_WINDOW_CONTENTS_STYLE_HEADERS:
1312                 wrap_in_scrolled_window (priv->contents_widget, GTK_WIDGET (priv->header_view));
1313                 break;
1314         case MODEST_MAIN_WINDOW_CONTENTS_STYLE_DETAILS:
1315         {
1316                 /* TODO: show here account details */
1317                 TnyFolderStore *selected_folderstore = 
1318                         modest_folder_view_get_selected (priv->folder_view);
1319                         
1320                 priv->details_widget = create_details_widget (selected_folderstore);
1321
1322                 wrap_in_scrolled_window (priv->contents_widget, 
1323                                  priv->details_widget);
1324                 break;
1325         }
1326         default:
1327                 g_return_if_reached ();
1328         }
1329
1330         /* Show */
1331         gtk_widget_show_all (priv->contents_widget);
1332 }
1333
1334 static void 
1335 on_configuration_key_changed (ModestConf* conf, 
1336                               const gchar *key, 
1337                               ModestConfEvent event, 
1338                               ModestMainWindow *self)
1339 {
1340         ModestMainWindowPrivate *priv;
1341         TnyAccount *account;
1342
1343         if (!key || strcmp (key, MODEST_CONF_DEVICE_NAME))
1344                 return;
1345
1346         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
1347
1348         if (priv->contents_style != MODEST_MAIN_WINDOW_CONTENTS_STYLE_DETAILS)
1349                 return;
1350
1351         account = (TnyAccount *) modest_folder_view_get_selected (priv->folder_view);
1352         if (TNY_IS_ACCOUNT (account) &&
1353             !strcmp (tny_account_get_id (account), MODEST_ACTUAL_LOCAL_FOLDERS_ACCOUNT_ID)) {
1354                 GList *children;
1355                 GtkLabel *label;
1356                 const gchar *device_name;
1357                 gchar *new_text;
1358                 
1359                 /* Get label */
1360                 children = gtk_container_get_children (GTK_CONTAINER (priv->details_widget));
1361                 label = GTK_LABEL (children->data);
1362                 
1363                 device_name = modest_conf_get_string (modest_runtime_get_conf(),
1364                                                       MODEST_CONF_DEVICE_NAME, NULL);
1365                 
1366                 new_text = g_strdup_printf ("%s: %s",
1367                                             _("mcen_fi_localroot_description"),
1368                                             device_name);
1369                 
1370                 gtk_label_set_text (label, new_text);
1371                 gtk_widget_show (GTK_WIDGET (label));
1372                 
1373                 g_free (new_text);
1374                 g_list_free (children);
1375         }
1376 }
1377
1378 static gboolean
1379 set_toolbar_transfer_mode (ModestMainWindow *self)
1380 {
1381         ModestMainWindowPrivate *priv = NULL;
1382         
1383         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW (self), FALSE);
1384
1385         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
1386
1387         set_toolbar_mode (self, TOOLBAR_MODE_TRANSFER);
1388         
1389         if (priv->progress_bar_timeout > 0) {
1390                 g_source_remove (priv->progress_bar_timeout);
1391                 priv->progress_bar_timeout = 0;
1392         }
1393
1394         return FALSE;
1395 }
1396
1397 static void 
1398 set_toolbar_mode (ModestMainWindow *self, 
1399                   ModestToolBarModes mode)
1400 {
1401         ModestWindowPrivate *parent_priv = NULL;
1402         ModestMainWindowPrivate *priv = NULL;
1403         GtkAction *sort_action = NULL, *refresh_action = NULL, *cancel_action = NULL;
1404         
1405         g_return_if_fail (MODEST_IS_MAIN_WINDOW (self));
1406
1407         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
1408         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
1409
1410         g_return_if_fail (GTK_IS_TOOLBAR(parent_priv->toolbar)); 
1411         
1412         sort_action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ToolbarSort");
1413         refresh_action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ToolbarSendReceive");
1414         cancel_action = gtk_ui_manager_get_action (parent_priv->ui_manager, "/ToolBar/ToolbarCancel");
1415
1416         /* Sets current toolbar mode */
1417         priv->current_toolbar_mode = mode;
1418
1419         /* Show and hide toolbar items */
1420         switch (mode) {
1421         case TOOLBAR_MODE_NORMAL:
1422                 if (sort_action) 
1423                         gtk_action_set_visible (sort_action, TRUE);
1424                 if (refresh_action) 
1425                         gtk_action_set_visible (refresh_action, TRUE);
1426                 if (priv->progress_toolitem) {
1427                         gtk_tool_item_set_expand (GTK_TOOL_ITEM (priv->progress_toolitem), FALSE);
1428                         gtk_widget_hide (priv->progress_toolitem);
1429                 }
1430                 if (priv->progress_bar)
1431                         gtk_widget_hide (priv->progress_bar);                   
1432                 
1433                 if (cancel_action)
1434                         gtk_action_set_visible (cancel_action, FALSE);
1435
1436                 /* Hide toolbar if optimized view is enabled */
1437                 if (priv->optimized_view)
1438                         gtk_widget_hide (GTK_WIDGET(parent_priv->toolbar));
1439                 break;
1440         case TOOLBAR_MODE_TRANSFER:
1441                 if (sort_action)
1442                         gtk_action_set_visible (sort_action, FALSE);
1443                 if (refresh_action)
1444                         gtk_action_set_visible (refresh_action, FALSE);
1445                 if (cancel_action)
1446                         gtk_action_set_visible (cancel_action, TRUE);
1447                 if (priv->progress_toolitem) {
1448                         gtk_tool_item_set_expand (GTK_TOOL_ITEM (priv->progress_toolitem), TRUE);
1449                         gtk_widget_show (priv->progress_toolitem);
1450                 }
1451                 if (priv->progress_bar)
1452                         gtk_widget_show (priv->progress_bar);                   
1453
1454                 /* Show toolbar if it's hiden (optimized view ) */
1455                 if (priv->optimized_view)
1456                         gtk_widget_show (GTK_WIDGET (parent_priv->toolbar));
1457                 break;
1458         default:
1459                 g_return_if_reached ();
1460         }
1461 }
1462
1463 static void
1464 cancel_progressbar (GtkToolButton *toolbutton,
1465                     ModestMainWindow *self)
1466 {
1467         GSList *tmp;
1468         ModestMainWindowPrivate *priv;
1469         
1470         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
1471
1472         /* Get operation observers and cancel its current operation */
1473         tmp = priv->progress_widgets;
1474         while (tmp) {
1475                 modest_progress_object_cancel_current_operation (MODEST_PROGRESS_OBJECT(tmp->data));
1476                 tmp=g_slist_next(tmp);
1477         }
1478 }
1479
1480 static gboolean
1481 observers_empty (ModestMainWindow *self)
1482 {
1483         GSList *tmp = NULL;
1484         ModestMainWindowPrivate *priv;
1485         gboolean is_empty = TRUE;
1486         guint pending_ops = 0;
1487  
1488         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
1489         tmp = priv->progress_widgets;
1490
1491         /* Check all observers */
1492         while (tmp && is_empty)  {
1493                 pending_ops = modest_progress_object_num_pending_operations (MODEST_PROGRESS_OBJECT(tmp->data));
1494                 is_empty = pending_ops == 0;
1495                 
1496                 tmp = g_slist_next(tmp);
1497         }
1498         
1499         return is_empty;
1500 }
1501
1502 static void
1503 on_queue_changed (ModestMailOperationQueue *queue,
1504                   ModestMailOperation *mail_op,
1505                   ModestMailOperationQueueNotification type,
1506                   ModestMainWindow *self)
1507 {
1508         ModestMainWindowPrivate *priv;
1509         ModestMailOperationTypeOperation op_type;
1510         ModestToolBarModes mode;
1511         GSList *tmp;
1512         gboolean mode_changed = FALSE;
1513 /*      ModestMailOperationStatus status; */
1514
1515         g_return_if_fail (MODEST_IS_MAIN_WINDOW (self));
1516         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
1517                
1518         /* Get toolbar mode from operation id*/
1519         op_type = modest_mail_operation_get_type_operation (mail_op);
1520         switch (op_type) {
1521         case MODEST_MAIL_OPERATION_TYPE_SEND:
1522         case MODEST_MAIL_OPERATION_TYPE_RECEIVE:
1523                 mode = TOOLBAR_MODE_TRANSFER;
1524                 if (priv->current_toolbar_mode == TOOLBAR_MODE_NORMAL)
1525                         mode_changed = TRUE;
1526                 break;
1527         default:
1528                 mode = TOOLBAR_MODE_NORMAL;
1529                 
1530         }
1531                 
1532                        
1533         /* Add operation observers and change toolbar if neccessary*/
1534         tmp = priv->progress_widgets;
1535         switch (type) {
1536         case MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED:
1537                 if (mode == TOOLBAR_MODE_TRANSFER) {
1538                         if (mode_changed)
1539                                 set_toolbar_transfer_mode(self);                    
1540                         while (tmp) {
1541                                 modest_progress_object_add_operation (MODEST_PROGRESS_OBJECT (tmp->data),
1542                                                                       mail_op);
1543                                 tmp = g_slist_next (tmp);
1544                         }
1545                 }
1546                 break;
1547         case MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED:
1548                 /* If mail_op is mine, check errors */
1549 /*              status = modest_mail_operation_get_status (mail_op); */
1550 /*              if (status != MODEST_MAIL_OPERATION_STATUS_SUCCESS) */
1551 /*                      modest_mail_operation_execute_error_handler (mail_op); */
1552
1553                 /* Change toolbar mode */
1554                 if (mode == TOOLBAR_MODE_TRANSFER) {                    
1555                         while (tmp) {
1556                                 modest_progress_object_remove_operation (MODEST_PROGRESS_OBJECT (tmp->data),
1557                                                                          mail_op);
1558                                 tmp = g_slist_next (tmp);
1559                         }
1560                         
1561                         /* If no more operations are being observed, NORMAL mode is enabled again */
1562                         if (observers_empty (self)) {
1563                                 set_toolbar_mode (self, TOOLBAR_MODE_NORMAL);
1564                                 
1565                         }
1566                 }
1567
1568                 break;
1569         }       
1570
1571 }
1572
1573 static void 
1574 on_show_account_action_activated  (GtkAction *action,
1575                                    gpointer user_data)
1576 {
1577         ModestAccountData *acc_data;
1578         ModestMainWindow *self;
1579         ModestMainWindowPrivate *priv;
1580         ModestAccountMgr *mgr;
1581         const gchar *acc_name;
1582
1583         self = MODEST_MAIN_WINDOW (user_data);
1584         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
1585
1586         /* Get account data */
1587         acc_name = gtk_action_get_name (action);
1588         mgr = modest_runtime_get_account_mgr ();
1589         acc_data = modest_account_mgr_get_account_data (mgr, acc_name);
1590
1591         /* Set the new visible & active account */
1592         if (acc_data->store_account) { 
1593                 modest_folder_view_set_account_id_of_visible_server_account (priv->folder_view,
1594                                                                              acc_data->store_account->account_name);
1595                 modest_window_set_active_account (MODEST_WINDOW (self), acc_data->account_name);
1596         }
1597
1598         /* Free */
1599         modest_account_mgr_free_account_data (mgr, acc_data);
1600 }
1601
1602 static void
1603 refresh_account (const gchar *account_name)
1604 {
1605         ModestWindow *win;
1606
1607         win = MODEST_WINDOW (modest_window_mgr_get_main_window (modest_runtime_get_window_mgr ()));
1608
1609         /* If account_name == NULL, we must update all (option All) */
1610         if (!account_name)
1611                 modest_ui_actions_do_send_receive_all (win);
1612         else
1613                 modest_ui_actions_do_send_receive (account_name, win);
1614 }
1615
1616 static void 
1617 on_refresh_account_action_activated  (GtkAction *action,
1618                                       gpointer user_data)
1619 {
1620         refresh_account ((const gchar*) user_data);
1621 }
1622
1623 static void
1624 on_send_receive_csm_activated (GtkMenuItem *item,
1625                                gpointer user_data)
1626 {
1627         refresh_account ((const gchar*) user_data);
1628 }