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