These changes are for reenabling build of gnome port.
[modest] / src / gnome / 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-error.h>
35
36 #include <widgets/modest-main-window.h>
37 #include <widgets/modest-window-priv.h>
38 #include <widgets/modest-msg-edit-window.h>
39 #include <widgets/modest-account-view-window.h>
40
41 #include <modest-runtime.h>
42 #include "modest-widget-memory.h"
43 #include "modest-ui-actions.h"
44 #include "modest-main-window-ui.h"
45 #include "modest-account-mgr.h"
46 #include "modest-conf.h"
47 #include <modest-tny-msg.h>
48 #include "modest-mail-operation.h"
49 #include "modest-icon-names.h"
50 #include "modest-gnome-info-bar.h"
51
52 /* 'private'/'protected' functions */
53 static void modest_main_window_class_init    (ModestMainWindowClass *klass);
54 static void modest_main_window_init          (ModestMainWindow *obj);
55 static void modest_main_window_finalize      (GObject *obj);
56
57 static void restore_sizes (ModestMainWindow *self);
58 static void save_sizes (ModestMainWindow *self);
59
60 static gboolean     on_header_view_button_press_event   (ModestHeaderView *header_view,
61                                                          GdkEventButton   *event,
62                                                          ModestMainWindow *self);
63
64 static gboolean     on_folder_view_button_press_event   (ModestFolderView *folder_view,
65                                                          GdkEventButton   *event,
66                                                          ModestMainWindow *self);
67
68 static gboolean     show_context_popup_menu             (ModestMainWindow *window,
69                                                          GtkTreeView      *tree_view,
70                                                          GdkEventButton   *event,
71                                                          GtkWidget        *menu);
72
73 static void         connect_signals                      (ModestMainWindow *self);
74
75 static void         on_queue_changed                     (ModestMailOperationQueue *queue,
76                                                           ModestMailOperation *mail_op,
77                                                           ModestMailOperationQueueNotification type,
78                                                           ModestMainWindow *self);
79
80 static void         on_header_status_update              (ModestHeaderView *header_view, 
81                                                           const gchar *msg, 
82                                                           gint num, 
83                                                           gint total,  
84                                                           ModestMainWindow *main_window);
85
86 static void         on_header_selected                   (ModestHeaderView *header_view, 
87                                                           TnyHeader *header,
88                                                           ModestMainWindow *main_window);
89
90 /* list my signals */
91 enum {
92         /* MY_SIGNAL_1, */
93         /* MY_SIGNAL_2, */
94         LAST_SIGNAL
95 };
96
97 typedef struct _ModestMainWindowPrivate ModestMainWindowPrivate;
98 struct _ModestMainWindowPrivate {
99
100         GtkWidget        *folder_paned;
101         GtkWidget        *msg_paned;
102         GtkWidget        *main_paned;
103         
104         GtkWidget        *online_toggle;
105         GtkWidget        *folder_info_label;
106
107         ModestHeaderView *header_view;
108         ModestFolderView *folder_view;
109         ModestMsgView    *msg_preview;
110
111         GtkWidget        *status_bar;
112         GtkWidget        *progress_bar;
113
114         GSList           *progress_widgets;
115         GtkWidget        *main_bar;
116 };
117
118
119 #define MODEST_MAIN_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
120                                                 MODEST_TYPE_MAIN_WINDOW, \
121                                                 ModestMainWindowPrivate))
122
123 /* globals */
124 static GtkWindowClass *parent_class = NULL;
125
126 /* uncomment the following if you have defined any signals */
127 /* static guint signals[LAST_SIGNAL] = {0}; */
128
129 GType
130 modest_main_window_get_type (void)
131 {
132         static GType my_type = 0;
133         if (!my_type) {
134                 static const GTypeInfo my_info = {
135                         sizeof(ModestMainWindowClass),
136                         NULL,           /* base init */
137                         NULL,           /* base finalize */
138                         (GClassInitFunc) modest_main_window_class_init,
139                         NULL,           /* class finalize */
140                         NULL,           /* class data */
141                         sizeof(ModestMainWindow),
142                         1,              /* n_preallocs */
143                         (GInstanceInitFunc) modest_main_window_init,
144                         NULL
145                 };
146                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
147                                                   "ModestMainWindow",
148                                                   &my_info, 0);
149         }
150         return my_type;
151 }
152
153 static void
154 modest_main_window_class_init (ModestMainWindowClass *klass)
155 {
156         GObjectClass *gobject_class;
157         gobject_class = (GObjectClass*) klass;
158
159         parent_class            = g_type_class_peek_parent (klass);
160         gobject_class->finalize = modest_main_window_finalize;
161
162         g_type_class_add_private (gobject_class, sizeof(ModestMainWindowPrivate));
163 }
164
165 static void
166 modest_main_window_init (ModestMainWindow *obj)
167 {
168         TnyAccountStore         *account_store;
169         ModestMainWindowPrivate *priv;
170         TnyFolderStoreQuery     *query;
171         GtkWidget               *icon;
172         gboolean                online;
173         
174         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
175         
176         priv->folder_paned = NULL;
177         priv->msg_paned    = NULL;
178         priv->main_paned   = NULL;      
179         priv->progress_widgets = NULL;
180
181         account_store = TNY_ACCOUNT_STORE (modest_runtime_get_account_store ());
182
183         /* online/offline toggle */
184         priv->online_toggle = gtk_toggle_button_new ();
185         online  = tny_device_is_online (modest_runtime_get_device());
186         icon    = gtk_image_new_from_icon_name (online ? GTK_STOCK_CONNECT : GTK_STOCK_DISCONNECT,
187                                                 GTK_ICON_SIZE_BUTTON);
188         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(priv->online_toggle), online);
189         gtk_button_set_image (GTK_BUTTON(priv->online_toggle),icon);
190
191         /* Paned */
192         priv->folder_paned = gtk_vpaned_new ();
193         priv->main_paned = gtk_hpaned_new ();
194         priv->msg_paned = gtk_vpaned_new ();
195
196         /* Main bar */
197         priv->folder_info_label = gtk_label_new (NULL);
198         priv->main_bar = modest_gnome_info_bar_new ();
199         priv->progress_widgets = g_slist_prepend (priv->progress_widgets, 
200                                                           priv->main_bar);
201
202         /* msg preview */
203         priv->msg_preview = MODEST_MSG_VIEW(tny_platform_factory_new_msg_view 
204                                             (modest_tny_platform_factory_get_instance ()));
205         if (!priv->msg_preview)
206                 g_printerr ("modest: cannot instantiate msgpreiew\n");
207
208         /* header view */
209         priv->header_view  =
210                 MODEST_HEADER_VIEW(modest_header_view_new (NULL, MODEST_HEADER_VIEW_STYLE_DETAILS));
211         if (!priv->header_view)
212                 g_printerr ("modest: cannot instantiate header view\n");
213
214         /* folder view */
215         query = tny_folder_store_query_new ();
216         tny_folder_store_query_add_item (query, NULL,
217                                          TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
218
219         priv->folder_view = MODEST_FOLDER_VIEW (modest_folder_view_new (query));
220         if (!priv->folder_view)
221                 g_printerr ("modest: cannot instantiate folder view\n");        
222         g_object_unref (G_OBJECT (query));
223 }
224
225 static void
226 modest_main_window_finalize (GObject *obj)
227 {
228         G_OBJECT_CLASS(parent_class)->finalize (obj);
229 }
230
231
232 GtkWidget*
233 modest_main_window_get_child_widget (ModestMainWindow *self,
234                                      ModestMainWindowWidgetType widget_type)
235 {
236         ModestMainWindowPrivate *priv;
237         GtkWidget *widget;
238         
239         g_return_val_if_fail (self, NULL);
240         g_return_val_if_fail (widget_type >= 0 && widget_type < MODEST_MAIN_WINDOW_WIDGET_TYPE_NUM,
241                               NULL);
242                                 
243         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
244
245         switch (widget_type) {
246         case MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW:
247                 widget = (GtkWidget*)priv->header_view; break;
248         case MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW:
249                 widget = (GtkWidget*)priv->folder_view; break;
250         case MODEST_MAIN_WINDOW_WIDGET_TYPE_MSG_PREVIEW:
251                 widget = (GtkWidget*)priv->msg_preview; break;
252         default:
253                 g_return_val_if_reached (NULL);
254                 return NULL;
255         }
256
257         return widget ? GTK_WIDGET(widget) : NULL;
258 }
259
260
261 static void
262 restore_sizes (ModestMainWindow *self)
263 {
264         ModestConf *conf;
265         ModestMainWindowPrivate *priv;
266         ModestWindowPrivate *parent_priv;
267         
268         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
269         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
270
271         conf = modest_runtime_get_conf ();
272         
273         modest_widget_memory_restore (conf, G_OBJECT(priv->folder_paned),
274                                       MODEST_CONF_FOLDER_PANED_KEY);
275         modest_widget_memory_restore (conf, G_OBJECT(priv->msg_paned),
276                                       MODEST_CONF_MSG_PANED_KEY);
277         modest_widget_memory_restore (conf, G_OBJECT(priv->main_paned),
278                                       MODEST_CONF_MAIN_PANED_KEY);
279         modest_widget_memory_restore (conf, G_OBJECT(priv->header_view),
280                                       MODEST_CONF_HEADER_VIEW_KEY);
281         modest_widget_memory_restore (conf, G_OBJECT(self), 
282                                       MODEST_CONF_MAIN_WINDOW_KEY);
283 }
284
285
286 static void
287 save_sizes (ModestMainWindow *self)
288 {
289         ModestWindowPrivate *parent_priv;
290         ModestMainWindowPrivate *priv;
291         ModestConf *conf;
292         
293         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
294         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
295
296         conf = modest_runtime_get_conf ();
297         
298         modest_widget_memory_save (conf, G_OBJECT(priv->folder_paned),
299                                    MODEST_CONF_FOLDER_PANED_KEY);
300         modest_widget_memory_save (conf, G_OBJECT(priv->msg_paned),
301                                    MODEST_CONF_MSG_PANED_KEY);
302         modest_widget_memory_save (conf, G_OBJECT(priv->main_paned),
303                                    MODEST_CONF_MAIN_PANED_KEY);
304         modest_widget_memory_save (conf, G_OBJECT(priv->header_view),
305                                    MODEST_CONF_HEADER_VIEW_KEY);
306         modest_widget_memory_save (conf, G_OBJECT(self), 
307                                    MODEST_CONF_MAIN_WINDOW_KEY);
308 }
309
310
311 static void
312 on_account_store_connecting_finished (TnyAccountStore *store, ModestMainWindow *self)
313 {
314         const gboolean online = TRUE;
315
316         GtkWidget *icon;
317         const gchar *icon_name;
318         ModestMainWindowPrivate *priv;
319         
320         g_return_if_fail (self);
321
322         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
323
324         icon_name = online ? GTK_STOCK_CONNECT : GTK_STOCK_DISCONNECT;
325         icon      = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON);
326
327         /* Block handlers in order to avoid unnecessary calls */
328         //g_signal_handler_block (G_OBJECT (priv->online_toggle), priv->toggle_button_signal);
329         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(priv->online_toggle), online);
330         //g_signal_handler_unblock (G_OBJECT (online_toggle), priv->toggle_button_signal);
331
332         gtk_button_set_image (GTK_BUTTON(priv->online_toggle), icon);
333         //statusbar_push (widget_factory, 0, online ? _("Modest went online") : _("Modest went offline"));
334         
335         /* If Modest has became online and the header view has a
336            header selected then show it */
337         /* TODO: FIXME: there is a race condition if some account needs to
338            ask the user for a password */
339
340 /*      if (online) { */
341 /*              GtkTreeSelection *selected; */
342
343 /*              selected = gtk_tree_view_get_selection (GTK_TREE_VIEW (header_view)); */
344 /*              _modest_header_view_change_selection (selected, header_view); */
345 /*      } */
346 }
347
348 void
349 on_online_toggle_toggled (GtkToggleButton *toggle, ModestMainWindow *self)
350 {
351         gboolean online;
352         TnyDevice *device;
353         ModestMainWindowPrivate *priv;
354
355         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
356
357         device = modest_runtime_get_device ();
358         online  = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->online_toggle));
359
360         if (online) {
361                 /* TODO: Just attempt to go online, instead of forcing the online status: */
362                 tny_device_force_online (device);
363         }
364         else
365                 tny_device_force_offline (device);
366 }
367
368 static gboolean
369 on_delete_event (GtkWidget *widget, GdkEvent  *event, ModestMainWindow *self)
370 {
371         save_sizes (self);
372         return FALSE;
373 }
374
375 static void
376 connect_signals (ModestMainWindow *self)
377 {       
378         ModestWindowPrivate *parent_priv;
379         ModestMainWindowPrivate *priv;
380         ModestTnyAccountStore *account_store;
381         
382         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
383         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
384
385         account_store = modest_runtime_get_account_store ();
386         
387         /* folder view */
388         g_signal_connect (G_OBJECT(priv->folder_view), "button-press-event",
389                           G_CALLBACK (on_folder_view_button_press_event),self);
390         g_signal_connect (G_OBJECT(priv->folder_view), "folder_selection_changed",
391                           G_CALLBACK(modest_ui_actions_on_folder_selection_changed), self);
392         g_signal_connect (G_OBJECT(priv->folder_view), "folder-display-name-changed",
393                           G_CALLBACK(modest_ui_actions_on_folder_display_name_changed), self);
394
395         /* header view */
396         g_signal_connect (G_OBJECT(priv->header_view), "status_update",
397                           G_CALLBACK(on_header_status_update), self);
398         g_signal_connect (G_OBJECT(priv->header_view), "header_selected",
399                           G_CALLBACK(modest_ui_actions_on_header_selected), self);
400         g_signal_connect (G_OBJECT(priv->header_view), "header_selected",
401                           G_CALLBACK(on_header_selected), self);
402         g_signal_connect (G_OBJECT(priv->header_view), "header_activated",
403                           G_CALLBACK(modest_ui_actions_on_header_activated), self);
404         g_signal_connect (G_OBJECT(priv->header_view), "item_not_found",
405                           G_CALLBACK(modest_ui_actions_on_item_not_found), self);
406         g_signal_connect (G_OBJECT(priv->header_view), "button-press-event",
407                           G_CALLBACK (on_header_view_button_press_event), self);
408         g_signal_connect (G_OBJECT(priv->header_view),"popup-menu",
409                           G_CALLBACK (on_header_view_button_press_event), self);
410                 
411         /* msg preview */
412         g_signal_connect (G_OBJECT(priv->msg_preview), "link_clicked",
413                           G_CALLBACK(modest_ui_actions_on_msg_link_clicked), self);
414         g_signal_connect (G_OBJECT(priv->msg_preview), "link_hover",
415                           G_CALLBACK(modest_ui_actions_on_msg_link_hover), self);
416         g_signal_connect (G_OBJECT(priv->msg_preview), "attachment_clicked",
417                           G_CALLBACK(modest_ui_actions_on_msg_attachment_clicked), self);
418         g_signal_connect (G_OBJECT(priv->msg_preview), "recpt-activated",
419                           G_CALLBACK(modest_ui_actions_on_msg_recpt_activated), self);
420
421         /* Account store */
422         /* Emmitted by our TnyAccountStore::get_password_func() implementation,
423          * This is not a normal part of tinymail: */
424         g_signal_connect (G_OBJECT (modest_runtime_get_account_store()), "password_requested",
425                           G_CALLBACK (modest_ui_actions_on_password_requested), self);
426         
427         /* Device */
428         g_signal_connect (G_OBJECT(modest_runtime_get_account_store()), "connecting-finished",
429                           G_CALLBACK(on_account_store_connecting_finished), self);
430         g_signal_connect (G_OBJECT(priv->online_toggle), "toggled",
431                           G_CALLBACK(on_online_toggle_toggled), self);
432
433         /* Mail Operation Queue */
434         g_signal_connect (G_OBJECT (modest_runtime_get_mail_operation_queue ()),
435                           "queue-changed",
436                           G_CALLBACK (on_queue_changed),
437                           self);
438         
439         /* window */
440         g_signal_connect (G_OBJECT(self), "delete-event", G_CALLBACK(on_delete_event), self);
441 }
442
443
444 static GtkWidget*
445 wrapped_in_scrolled_window (GtkWidget *widget, gboolean needs_viewport)
446 {
447         GtkWidget *win;
448
449         win = gtk_scrolled_window_new (NULL, NULL);
450         gtk_scrolled_window_set_policy
451                 (GTK_SCROLLED_WINDOW (win),GTK_POLICY_NEVER,
452                  GTK_POLICY_AUTOMATIC);
453         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (win),
454                                              GTK_SHADOW_IN);
455         
456         if (needs_viewport)
457                 gtk_scrolled_window_add_with_viewport
458                         (GTK_SCROLLED_WINDOW(win), widget);
459         else
460                 gtk_container_add (GTK_CONTAINER(win),
461                                    widget);
462
463         return win;
464 }
465
466
467
468
469 ModestWindow *
470 modest_main_window_new (void)
471 {
472         GObject *obj;
473         ModestMainWindow *self;
474         ModestMainWindowPrivate *priv;
475         ModestWindowPrivate *parent_priv;
476         GtkWidget *main_vbox;
477         GtkWidget *status_hbox;
478         GtkWidget *header_win, *folder_win;
479         GtkWidget *preview_scroll;
480         GtkActionGroup *action_group;
481         GError *error = NULL;
482                 
483         obj  = g_object_new(MODEST_TYPE_MAIN_WINDOW, NULL);
484         self = MODEST_MAIN_WINDOW(obj);
485         
486         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
487         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
488
489         
490         parent_priv->ui_manager = gtk_ui_manager_new();
491         parent_priv->ui_dimming_manager = modest_ui_dimming_manager_new ();
492         action_group = gtk_action_group_new ("ModestMainWindowActions");
493         
494         /* Add common actions */
495         gtk_action_group_add_actions (action_group,
496                                       modest_action_entries,
497                                       G_N_ELEMENTS (modest_action_entries),
498                                       obj);
499
500         gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 0);
501         g_object_unref (action_group);
502
503         /* Load the UI definition */
504         gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager,
505                                          MODEST_UIDIR "modest-main-window-ui.xml", &error);
506         if (error != NULL) {
507                 g_printerr ("modest: could not merge modest-main-window-ui.xml: %s", error->message);
508                 g_error_free (error);
509                 error = NULL;
510         }
511
512         /* Add accelerators */
513         gtk_window_add_accel_group (GTK_WINDOW (obj), 
514                                     gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
515
516         /* Toolbar / Menubar */
517         parent_priv->toolbar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar");
518         parent_priv->menubar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/MenuBar");
519
520         gtk_toolbar_set_tooltips (GTK_TOOLBAR (parent_priv->toolbar), TRUE);
521         folder_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->folder_view), FALSE);
522         header_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->header_view), FALSE);
523
524         /* Paned */
525         preview_scroll = gtk_scrolled_window_new (NULL, NULL);
526         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (preview_scroll),
527                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
528         gtk_paned_add1 (GTK_PANED(priv->main_paned), folder_win);
529         gtk_paned_add2 (GTK_PANED(priv->main_paned), priv->msg_paned);
530         gtk_paned_add1 (GTK_PANED(priv->msg_paned), header_win);
531         gtk_container_add (GTK_CONTAINER (preview_scroll),
532                            GTK_WIDGET(priv->msg_preview));
533         gtk_paned_add2 (GTK_PANED(priv->msg_paned), preview_scroll);
534
535         /* Main Bar */
536         status_hbox = gtk_hbox_new (FALSE, 0);
537         gtk_box_pack_start (GTK_BOX(status_hbox), priv->folder_info_label, FALSE,FALSE, 6);
538         gtk_box_pack_start (GTK_BOX(status_hbox), priv->main_bar, TRUE, TRUE, 0);
539         gtk_box_pack_start (GTK_BOX(status_hbox), priv->online_toggle,FALSE, FALSE, 0);
540
541         /* putting it all together... */
542         main_vbox = gtk_vbox_new (FALSE, 0);
543         gtk_box_pack_start (GTK_BOX(main_vbox), parent_priv->menubar, FALSE, FALSE, 0);
544         gtk_box_pack_start (GTK_BOX(main_vbox), parent_priv->toolbar, FALSE, FALSE, 0);
545         gtk_box_pack_start (GTK_BOX(main_vbox), priv->main_paned, TRUE, TRUE,0);
546         gtk_box_pack_start (GTK_BOX(main_vbox), status_hbox, FALSE, FALSE, 0);
547         
548         gtk_container_add (GTK_CONTAINER(obj), main_vbox);
549         restore_sizes (MODEST_MAIN_WINDOW(obj));        
550
551         gtk_window_set_title (GTK_WINDOW(obj), _("Modest"));
552         gtk_window_set_icon_from_file  (GTK_WINDOW(obj), MODEST_APP_ICON, NULL);        
553         gtk_widget_show_all (main_vbox);
554         
555         /* Connect signals */
556         connect_signals (MODEST_MAIN_WINDOW(obj));
557
558         /* Set account store */
559         tny_account_store_view_set_account_store (TNY_ACCOUNT_STORE_VIEW (priv->folder_view),
560                                                   TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
561
562         return (ModestWindow *) obj;
563 }
564
565 static gboolean 
566 on_header_view_button_press_event (ModestHeaderView *header_view,
567                                    GdkEventButton   *event,
568                                    ModestMainWindow *self)
569 {
570         if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
571                 GtkWidget *menu;
572                 ModestWindowPrivate *parent_priv;
573         
574                 parent_priv = MODEST_WINDOW_GET_PRIVATE (self);
575                 menu = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/HeaderViewCSM");
576
577                 return show_context_popup_menu (self,
578                                                 GTK_TREE_VIEW (header_view), 
579                                                 event, 
580                                                 menu);
581         }
582
583         return FALSE;
584 }
585
586 static gboolean 
587 on_folder_view_button_press_event (ModestFolderView *folder_view,
588                                    GdkEventButton   *event,
589                                    ModestMainWindow *self)
590 {
591         if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
592                 GtkWidget *menu;
593                 ModestWindowPrivate *parent_priv;
594         
595                 parent_priv = MODEST_WINDOW_GET_PRIVATE (self);
596                 menu = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/FolderViewCSM");
597
598                 return show_context_popup_menu (self,
599                                                 GTK_TREE_VIEW (folder_view), 
600                                                 event, 
601                                                 menu);
602         }
603
604         return FALSE;
605 }
606
607 static gboolean 
608 show_context_popup_menu (ModestMainWindow *window,
609                          GtkTreeView *tree_view,
610                          GdkEventButton   *event,                        
611                          GtkWidget *menu)
612 {
613         g_return_val_if_fail (menu, FALSE);
614
615         if (event != NULL) {
616                 /* Ensure that the header is selected */
617                 GtkTreeSelection *selection;
618
619                 selection = gtk_tree_view_get_selection (tree_view);
620         
621                 if (gtk_tree_selection_count_selected_rows (selection) <= 1) {
622                         GtkTreePath *path;
623                 
624                         /* Get tree path for row that was clicked */
625                         if (gtk_tree_view_get_path_at_pos (tree_view,
626                                                            (gint) event->x, 
627                                                            (gint) event->y,
628                                                            &path, 
629                                                            NULL, NULL, NULL)) {
630                                 gtk_tree_selection_unselect_all (selection);
631                                 gtk_tree_selection_select_path (selection, path);
632                                 gtk_tree_path_free (path);
633                         }
634                 }
635
636                 /* Show popup */
637                 if (gtk_tree_selection_count_selected_rows(selection) == 1)
638                         gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
639                                         NULL, NULL,
640                                         event->button, event->time);
641         }
642         return TRUE;
643 }
644
645 static void
646 on_queue_changed (ModestMailOperationQueue *queue,
647                   ModestMailOperation *mail_op,
648                   ModestMailOperationQueueNotification type,
649                   ModestMainWindow *self)
650 {
651         GSList *tmp;
652         ModestMainWindowPrivate *priv;
653
654         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
655
656         tmp = priv->progress_widgets;
657
658         switch (type) {
659         case MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED:
660                 while (tmp) {
661                         modest_progress_object_add_operation (MODEST_PROGRESS_OBJECT (tmp->data),
662                                                               mail_op);
663                         tmp = g_slist_next (tmp);
664                 }
665                 break;
666         case MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED:
667                 while (tmp) {
668                         modest_progress_object_remove_operation (MODEST_PROGRESS_OBJECT (tmp->data),
669                                                                  mail_op);
670                         tmp = g_slist_next (tmp);
671                 }
672                 break;
673         }
674 }
675
676 static void
677 on_header_status_update (ModestHeaderView *header_view, 
678                          const gchar *msg, gint num, 
679                          gint total,  ModestMainWindow *self)
680 {
681         ModestMainWindowPrivate *priv;
682         gchar *txt;
683
684         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
685
686         /* Set progress */
687         txt = g_strdup_printf (_("Downloading %d of %d"), num, total);
688         modest_gnome_info_bar_set_progress (MODEST_GNOME_INFO_BAR (priv->main_bar), 
689                                             (const gchar*) txt,
690                                             num, total);
691         g_free (txt);
692         
693         /* Set status message */
694         modest_gnome_info_bar_set_message (MODEST_GNOME_INFO_BAR (priv->main_bar), msg);
695 }
696
697 void 
698 modest_main_window_set_style (ModestMainWindow *self, 
699                               ModestMainWindowStyle style)
700 {
701         /* TODO */
702 }
703
704
705 ModestMainWindowStyle
706 modest_main_window_get_style (ModestMainWindow *self)
707 {
708         /* TODO */
709         return MODEST_MAIN_WINDOW_STYLE_SPLIT;
710 }
711
712 void 
713 modest_main_window_set_contents_style (ModestMainWindow *self, 
714                                        ModestMainWindowContentsStyle style)
715 {
716         /* TODO */
717 }
718
719 ModestMainWindowContentsStyle
720 modest_main_window_get_contents_style (ModestMainWindow *self)
721 {
722         /* TODO */
723         return MODEST_MAIN_WINDOW_CONTENTS_STYLE_HEADERS;
724 }
725
726
727 static void
728 get_msg_callback (TnyFolder *folder, 
729                   gboolean cancelled,
730                   TnyMsg *msg, 
731                   GError **err, 
732                   gpointer user_data)
733 {
734         if (!err ||!(*err)) {
735                 ModestMsgView *msg_preview;
736
737                 msg_preview = MODEST_MSG_VIEW (user_data);
738                 tny_msg_view_set_msg (TNY_MSG_VIEW (msg_preview), msg);
739         }
740
741         /* Frees */
742         g_object_unref (folder);
743 }
744
745 static void 
746 on_header_selected (ModestHeaderView *header_view, 
747                     TnyHeader *header,
748                     ModestMainWindow *main_window)
749 {
750         TnyFolder *folder;
751         ModestMainWindowPrivate *priv;
752
753         priv = MODEST_MAIN_WINDOW_GET_PRIVATE (main_window);
754
755         if (!header)
756                 return;
757
758         folder = tny_header_get_folder (header);
759
760         /* FIXME: do not use this directly. Use a mail operation
761            instead in order to get progress info */
762         tny_folder_get_msg_async (folder, 
763                                   header, 
764                                   (TnyGetMsgCallback) get_msg_callback, 
765                                   NULL, 
766                                   priv->msg_preview);
767 }
768
769 void      
770 modest_main_window_notify_send_receive_initied    (ModestMainWindow *self)
771 {
772         g_message("NOT IMPLEMENTED %s", __FUNCTION__);
773 }
774 void      
775 modest_main_window_notify_send_receive_completed    (ModestMainWindow *self)
776 {
777         g_message("NOT IMPLEMENTED %s", __FUNCTION__);
778 }
779
780 gboolean
781 modest_main_window_transfer_mode_enabled (ModestMainWindow *self)
782 {
783         g_message("NOT IMPLEMENTED %s", __FUNCTION__);
784         return FALSE;
785 }
786
787 gboolean  
788 modest_main_window_on_msg_view_window_msg_changed (ModestMsgViewWindow *view_window,
789                                                    GtkTreeModel *model,
790                                                    GtkTreeRowReference *row_reference,
791                                                    ModestMainWindow *self)
792 {
793         g_message("NOT IMPLEMENTED %s", __FUNCTION__);
794         return FALSE;
795 }