09a7541e80950473274ae0e780a11bdfe07d7e20
[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(modest_msg_view_new (NULL));
204         if (!priv->msg_preview)
205                 g_printerr ("modest: cannot instantiate msgpreiew\n");
206
207         /* header view */
208         priv->header_view  =
209                 MODEST_HEADER_VIEW(modest_header_view_new (NULL, MODEST_HEADER_VIEW_STYLE_DETAILS));
210         if (!priv->header_view)
211                 g_printerr ("modest: cannot instantiate header view\n");
212
213         /* folder view */
214         query = tny_folder_store_query_new ();
215         tny_folder_store_query_add_item (query, NULL,
216                                          TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
217
218         priv->folder_view = MODEST_FOLDER_VIEW (modest_folder_view_new (query));
219         if (!priv->folder_view)
220                 g_printerr ("modest: cannot instantiate folder view\n");        
221         g_object_unref (G_OBJECT (query));
222 }
223
224 static void
225 modest_main_window_finalize (GObject *obj)
226 {
227         G_OBJECT_CLASS(parent_class)->finalize (obj);
228 }
229
230
231 GtkWidget*
232 modest_main_window_get_child_widget (ModestMainWindow *self,
233                                      ModestMainWindowWidgetType widget_type)
234 {
235         ModestMainWindowPrivate *priv;
236         GtkWidget *widget;
237         
238         g_return_val_if_fail (self, NULL);
239         g_return_val_if_fail (widget_type >= 0 && widget_type < MODEST_MAIN_WINDOW_WIDGET_TYPE_NUM,
240                               NULL);
241                                 
242         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
243
244         switch (widget_type) {
245         case MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW:
246                 widget = (GtkWidget*)priv->header_view; break;
247         case MODEST_MAIN_WINDOW_WIDGET_TYPE_FOLDER_VIEW:
248                 widget = (GtkWidget*)priv->folder_view; break;
249         case MODEST_MAIN_WINDOW_WIDGET_TYPE_MSG_PREVIEW:
250                 widget = (GtkWidget*)priv->msg_preview; break;
251         default:
252                 g_return_val_if_reached (NULL);
253                 return NULL;
254         }
255
256         return widget ? GTK_WIDGET(widget) : NULL;
257 }
258
259
260 static void
261 restore_sizes (ModestMainWindow *self)
262 {
263         ModestConf *conf;
264         ModestMainWindowPrivate *priv;
265         ModestWindowPrivate *parent_priv;
266         
267         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
268         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
269
270         conf = modest_runtime_get_conf ();
271         
272         modest_widget_memory_restore (conf, G_OBJECT(priv->folder_paned),
273                                       MODEST_CONF_FOLDER_PANED_KEY);
274         modest_widget_memory_restore (conf, G_OBJECT(priv->msg_paned),
275                                       MODEST_CONF_MSG_PANED_KEY);
276         modest_widget_memory_restore (conf, G_OBJECT(priv->main_paned),
277                                       MODEST_CONF_MAIN_PANED_KEY);
278         modest_widget_memory_restore (conf, G_OBJECT(priv->header_view),
279                                       MODEST_CONF_HEADER_VIEW_KEY);
280         modest_widget_memory_restore (conf, G_OBJECT(self), 
281                                       MODEST_CONF_MAIN_WINDOW_KEY);
282 }
283
284
285 static void
286 save_sizes (ModestMainWindow *self)
287 {
288         ModestWindowPrivate *parent_priv;
289         ModestMainWindowPrivate *priv;
290         ModestConf *conf;
291         
292         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
293         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
294
295         conf = modest_runtime_get_conf ();
296         
297         modest_widget_memory_save (conf, G_OBJECT(priv->folder_paned),
298                                    MODEST_CONF_FOLDER_PANED_KEY);
299         modest_widget_memory_save (conf, G_OBJECT(priv->msg_paned),
300                                    MODEST_CONF_MSG_PANED_KEY);
301         modest_widget_memory_save (conf, G_OBJECT(priv->main_paned),
302                                    MODEST_CONF_MAIN_PANED_KEY);
303         modest_widget_memory_save (conf, G_OBJECT(priv->header_view),
304                                    MODEST_CONF_HEADER_VIEW_KEY);
305         modest_widget_memory_save (conf, G_OBJECT(self), 
306                                    MODEST_CONF_MAIN_WINDOW_KEY);
307 }
308
309
310 static void
311 on_account_store_connecting_finished (TnyAccountStore *store, ModestMainWindow *self)
312 {
313         const gboolean online = TRUE;
314
315         GtkWidget *icon;
316         const gchar *icon_name;
317         ModestMainWindowPrivate *priv;
318         
319         g_return_if_fail (device);
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         parent_priv->ui_manager = gtk_ui_manager_new();
490         action_group = gtk_action_group_new ("ModestMainWindowActions");
491         
492         /* Add common actions */
493         gtk_action_group_add_actions (action_group,
494                                       modest_action_entries,
495                                       G_N_ELEMENTS (modest_action_entries),
496                                       obj);
497
498         gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 0);
499         g_object_unref (action_group);
500
501         /* Load the UI definition */
502         gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager,
503                                          MODEST_UIDIR "modest-main-window-ui.xml", &error);
504         if (error != NULL) {
505                 g_printerr ("modest: could not merge modest-main-window-ui.xml: %s", error->message);
506                 g_error_free (error);
507                 error = NULL;
508         }
509
510         /* Add accelerators */
511         gtk_window_add_accel_group (GTK_WINDOW (obj), 
512                                     gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
513
514         /* Toolbar / Menubar */
515         parent_priv->toolbar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar");
516         parent_priv->menubar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/MenuBar");
517
518         gtk_toolbar_set_tooltips (GTK_TOOLBAR (parent_priv->toolbar), TRUE);
519         folder_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->folder_view), FALSE);
520         header_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->header_view), FALSE);
521
522         /* Paned */
523         preview_scroll = gtk_scrolled_window_new (NULL, NULL);
524         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (preview_scroll),
525                                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
526         gtk_paned_add1 (GTK_PANED(priv->main_paned), folder_win);
527         gtk_paned_add2 (GTK_PANED(priv->main_paned), priv->msg_paned);
528         gtk_paned_add1 (GTK_PANED(priv->msg_paned), header_win);
529         gtk_container_add (GTK_CONTAINER (preview_scroll),
530                            GTK_WIDGET(priv->msg_preview));
531         gtk_paned_add2 (GTK_PANED(priv->msg_paned), preview_scroll);
532
533         /* Main Bar */
534         status_hbox = gtk_hbox_new (FALSE, 0);
535         gtk_box_pack_start (GTK_BOX(status_hbox), priv->folder_info_label, FALSE,FALSE, 6);
536         gtk_box_pack_start (GTK_BOX(status_hbox), priv->main_bar, TRUE, TRUE, 0);
537         gtk_box_pack_start (GTK_BOX(status_hbox), priv->online_toggle,FALSE, FALSE, 0);
538
539         /* putting it all together... */
540         main_vbox = gtk_vbox_new (FALSE, 0);
541         gtk_box_pack_start (GTK_BOX(main_vbox), parent_priv->menubar, FALSE, FALSE, 0);
542         gtk_box_pack_start (GTK_BOX(main_vbox), parent_priv->toolbar, FALSE, FALSE, 0);
543         gtk_box_pack_start (GTK_BOX(main_vbox), priv->main_paned, TRUE, TRUE,0);
544         gtk_box_pack_start (GTK_BOX(main_vbox), status_hbox, FALSE, FALSE, 0);
545         
546         gtk_container_add (GTK_CONTAINER(obj), main_vbox);
547         restore_sizes (MODEST_MAIN_WINDOW(obj));        
548
549         gtk_window_set_title (GTK_WINDOW(obj), _("Modest"));
550         gtk_window_set_icon_from_file  (GTK_WINDOW(obj), MODEST_APP_ICON, NULL);        
551         gtk_widget_show_all (main_vbox);
552         
553         /* Connect signals */
554         connect_signals (MODEST_MAIN_WINDOW(obj));
555
556         /* Set account store */
557         tny_account_store_view_set_account_store (TNY_ACCOUNT_STORE_VIEW (priv->folder_view),
558                                                   TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
559
560         return (ModestWindow *) obj;
561 }
562
563 static gboolean 
564 on_header_view_button_press_event (ModestHeaderView *header_view,
565                                    GdkEventButton   *event,
566                                    ModestMainWindow *self)
567 {
568         if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
569                 GtkWidget *menu;
570                 ModestWindowPrivate *parent_priv;
571         
572                 parent_priv = MODEST_WINDOW_GET_PRIVATE (self);
573                 menu = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/HeaderViewCSM");
574
575                 return show_context_popup_menu (self,
576                                                 GTK_TREE_VIEW (header_view), 
577                                                 event, 
578                                                 menu);
579         }
580
581         return FALSE;
582 }
583
584 static gboolean 
585 on_folder_view_button_press_event (ModestFolderView *folder_view,
586                                    GdkEventButton   *event,
587                                    ModestMainWindow *self)
588 {
589         if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
590                 GtkWidget *menu;
591                 ModestWindowPrivate *parent_priv;
592         
593                 parent_priv = MODEST_WINDOW_GET_PRIVATE (self);
594                 menu = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/FolderViewCSM");
595
596                 return show_context_popup_menu (self,
597                                                 GTK_TREE_VIEW (folder_view), 
598                                                 event, 
599                                                 menu);
600         }
601
602         return FALSE;
603 }
604
605 static gboolean 
606 show_context_popup_menu (ModestMainWindow *window,
607                          GtkTreeView *tree_view,
608                          GdkEventButton   *event,                        
609                          GtkWidget *menu)
610 {
611         g_return_val_if_fail (menu, FALSE);
612
613         if (event != NULL) {
614                 /* Ensure that the header is selected */
615                 GtkTreeSelection *selection;
616
617                 selection = gtk_tree_view_get_selection (tree_view);
618         
619                 if (gtk_tree_selection_count_selected_rows (selection) <= 1) {
620                         GtkTreePath *path;
621                 
622                         /* Get tree path for row that was clicked */
623                         if (gtk_tree_view_get_path_at_pos (tree_view,
624                                                            (gint) event->x, 
625                                                            (gint) event->y,
626                                                            &path, 
627                                                            NULL, NULL, NULL)) {
628                                 gtk_tree_selection_unselect_all (selection);
629                                 gtk_tree_selection_select_path (selection, path);
630                                 gtk_tree_path_free (path);
631                         }
632                 }
633
634                 /* Show popup */
635                 if (gtk_tree_selection_count_selected_rows(selection) == 1)
636                         gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
637                                         NULL, NULL,
638                                         event->button, event->time);
639         }
640         return TRUE;
641 }
642
643 static void
644 on_queue_changed (ModestMailOperationQueue *queue,
645                   ModestMailOperation *mail_op,
646                   ModestMailOperationQueueNotification type,
647                   ModestMainWindow *self)
648 {
649         GSList *tmp;
650         ModestMainWindowPrivate *priv;
651
652         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
653
654         tmp = priv->progress_widgets;
655
656         switch (type) {
657         case MODEST_MAIL_OPERATION_QUEUE_OPERATION_ADDED:
658                 while (tmp) {
659                         modest_progress_object_add_operation (MODEST_PROGRESS_OBJECT (tmp->data),
660                                                               mail_op);
661                         tmp = g_slist_next (tmp);
662                 }
663                 break;
664         case MODEST_MAIL_OPERATION_QUEUE_OPERATION_REMOVED:
665                 while (tmp) {
666                         modest_progress_object_remove_operation (MODEST_PROGRESS_OBJECT (tmp->data),
667                                                                  mail_op);
668                         tmp = g_slist_next (tmp);
669                 }
670                 break;
671         }
672 }
673
674 static void
675 on_header_status_update (ModestHeaderView *header_view, 
676                          const gchar *msg, gint num, 
677                          gint total,  ModestMainWindow *self)
678 {
679         ModestMainWindowPrivate *priv;
680         gchar *txt;
681
682         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
683
684         /* Set progress */
685         txt = g_strdup_printf (_("Downloading %d of %d"), num, total);
686         modest_gnome_info_bar_set_progress (MODEST_GNOME_INFO_BAR (priv->main_bar), 
687                                             (const gchar*) txt,
688                                             num, total);
689         g_free (txt);
690         
691         /* Set status message */
692         modest_gnome_info_bar_set_message (MODEST_GNOME_INFO_BAR (priv->main_bar), msg);
693 }
694
695 void 
696 modest_main_window_set_style (ModestMainWindow *self, 
697                               ModestMainWindowStyle style)
698 {
699         /* TODO */
700 }
701
702
703 ModestMainWindowStyle
704 modest_main_window_get_style (ModestMainWindow *self)
705 {
706         /* TODO */
707         return MODEST_MAIN_WINDOW_STYLE_SPLIT;
708 }
709
710 void 
711 modest_main_window_set_contents_style (ModestMainWindow *self, 
712                                        ModestMainWindowContentsStyle style)
713 {
714         /* TODO */
715 }
716
717 ModestMainWindowContentsStyle
718 modest_main_window_get_contents_style (ModestMainWindow *self)
719 {
720         /* TODO */
721         return MODEST_MAIN_WINDOW_CONTENTS_STYLE_HEADERS;
722 }
723
724
725 static void
726 get_msg_callback (TnyFolder *folder, 
727                   gboolean cancelled,
728                   TnyMsg *msg, 
729                   GError **err, 
730                   gpointer user_data)
731 {
732         if (!(*err)) {
733                 ModestMsgView *msg_preview;
734
735                 msg_preview = MODEST_MSG_VIEW (user_data);
736                 modest_msg_view_set_message (msg_preview, msg);
737         }
738
739         /* Frees */
740         g_object_unref (folder);
741 }
742
743 static void 
744 on_header_selected (ModestHeaderView *header_view, 
745                     TnyHeader *header,
746                     ModestMainWindow *main_window)
747 {
748         TnyFolder *folder;
749         ModestMainWindowPrivate *priv;
750
751         priv = MODEST_MAIN_WINDOW_GET_PRIVATE (main_window);
752
753         if (!header)
754                 return;
755
756         folder = tny_header_get_folder (header);
757
758         /* FIXME: do not use this directly. Use a mail operation
759            instead in order to get progress info */
760         tny_folder_get_msg_async (folder, 
761                                   header, 
762                                   get_msg_callback, 
763                                   NULL, 
764                                   priv->msg_preview);
765 }