* Added some more UI actions implementations
[modest] / src / gtk / 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
33 #include "modest-main-window.h"
34 #include "modest-window-priv.h"
35 #include "modest-widget-memory.h"
36 #include "modest-icon-factory.h"
37 #include "modest-ui.h"
38 #include "modest-main-window-ui.h"
39 #include "modest-account-view-window.h"
40 #include "modest-account-mgr.h"
41 #include "modest-conf.h"
42 #include "modest-edit-msg-window.h"
43 #include "modest-tny-msg-actions.h"
44 #include "modest-mail-operation.h"
45 #include "modest-icon-names.h"
46
47 /* 'private'/'protected' functions */
48 static void modest_main_window_class_init    (ModestMainWindowClass *klass);
49 static void modest_main_window_init          (ModestMainWindow *obj);
50 static void modest_main_window_finalize      (GObject *obj);
51
52 static void restore_sizes (ModestMainWindow *self);
53 static void save_sizes (ModestMainWindow *self);
54
55 static gboolean     on_header_view_button_press_event   (ModestHeaderView *header_view,
56                                                          GdkEventButton   *event,
57                                                          ModestMainWindow *self);
58
59 static gboolean     on_folder_view_button_press_event   (ModestFolderView *folder_view,
60                                                          GdkEventButton   *event,
61                                                          ModestMainWindow *self);
62
63 static gboolean     show_context_popup_menu             (ModestMainWindow *window,
64                                                          GtkTreeView      *tree_view,
65                                                          GdkEventButton   *event,
66                                                          GtkWidget        *menu);
67
68 /* list my signals */
69 enum {
70         /* MY_SIGNAL_1, */
71         /* MY_SIGNAL_2, */
72         LAST_SIGNAL
73 };
74
75 typedef struct _ModestMainWindowPrivate ModestMainWindowPrivate;
76 struct _ModestMainWindowPrivate {
77
78         GtkWidget *folder_paned;
79         GtkWidget *msg_paned;
80         GtkWidget *main_paned;
81         
82         ModestHeaderView *header_view;
83         ModestFolderView *folder_view;
84         ModestMsgView    *msg_preview;
85 };
86
87
88 #define MODEST_MAIN_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
89                                                 MODEST_TYPE_MAIN_WINDOW, \
90                                                 ModestMainWindowPrivate))
91
92 typedef struct _GetMsgAsyncHelper {
93         ModestMainWindowPrivate *main_window_private;
94         guint action;
95         ModestMailOperationReplyType reply_type;
96         ModestMailOperationForwardType forward_type;
97         gchar *from;
98         TnyIterator *iter;
99 } GetMsgAsyncHelper;
100
101 /* globals */
102 static GtkWindowClass *parent_class = NULL;
103
104 /* uncomment the following if you have defined any signals */
105 /* static guint signals[LAST_SIGNAL] = {0}; */
106
107 GType
108 modest_main_window_get_type (void)
109 {
110         static GType my_type = 0;
111         if (!my_type) {
112                 static const GTypeInfo my_info = {
113                         sizeof(ModestMainWindowClass),
114                         NULL,           /* base init */
115                         NULL,           /* base finalize */
116                         (GClassInitFunc) modest_main_window_class_init,
117                         NULL,           /* class finalize */
118                         NULL,           /* class data */
119                         sizeof(ModestMainWindow),
120                         1,              /* n_preallocs */
121                         (GInstanceInitFunc) modest_main_window_init,
122                         NULL
123                 };
124                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
125                                                   "ModestMainWindow",
126                                                   &my_info, 0);
127         }
128         return my_type;
129 }
130
131 static void
132 modest_main_window_class_init (ModestMainWindowClass *klass)
133 {
134         GObjectClass *gobject_class;
135         gobject_class = (GObjectClass*) klass;
136
137         parent_class            = g_type_class_peek_parent (klass);
138         gobject_class->finalize = modest_main_window_finalize;
139
140         g_type_class_add_private (gobject_class, sizeof(ModestMainWindowPrivate));
141 }
142
143 static void
144 modest_main_window_init (ModestMainWindow *obj)
145 {
146         ModestMainWindowPrivate *priv;
147
148         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
149         
150         priv->folder_paned = NULL;
151         priv->msg_paned    = NULL;
152         priv->main_paned   = NULL;      
153         priv->header_view  = NULL;
154         priv->folder_view  = NULL;
155         priv->msg_preview  = NULL;
156 }
157
158 static void
159 modest_main_window_finalize (GObject *obj)
160 {
161         G_OBJECT_CLASS(parent_class)->finalize (obj);
162 }
163
164
165 static ModestHeaderView*
166 header_view_new (ModestMainWindow *self)
167 {
168         ModestHeaderView *header_view;
169         ModestWindowPrivate *parent_priv;
170
171         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
172         
173         header_view = modest_widget_factory_get_header_view (parent_priv->widget_factory);
174         modest_header_view_set_style (header_view, MODEST_HEADER_VIEW_STYLE_DETAILS);
175         return header_view;
176 }
177
178
179 static void
180 restore_sizes (ModestMainWindow *self)
181 {
182         ModestConf *conf;
183         ModestMainWindowPrivate *priv;
184         ModestWindowPrivate *parent_priv;
185         
186         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
187         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
188
189         conf = modest_tny_platform_factory_get_conf_instance
190                 (MODEST_TNY_PLATFORM_FACTORY(parent_priv->plat_factory));
191
192         modest_widget_memory_restore (conf, G_OBJECT(priv->folder_paned),
193                                       "modest-folder-paned");
194         modest_widget_memory_restore (conf, G_OBJECT(priv->msg_paned),
195                                       "modest-msg-paned");
196         modest_widget_memory_restore (conf, G_OBJECT(priv->main_paned),
197                                       "modest-main-paned");
198         modest_widget_memory_restore (conf, G_OBJECT(priv->header_view),
199                                       "header-view");
200         modest_widget_memory_restore (conf,G_OBJECT(self),
201                                       "modest-main-window");
202 }
203
204
205 static void
206 save_sizes (ModestMainWindow *self)
207 {
208         ModestWindowPrivate *parent_priv;
209         ModestMainWindowPrivate *priv;
210         ModestConf *conf;
211         
212         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
213         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
214
215         conf = modest_tny_platform_factory_get_conf_instance
216                 (MODEST_TNY_PLATFORM_FACTORY(parent_priv->plat_factory));
217         
218         modest_widget_memory_save (conf,G_OBJECT(self), "modest-main-window");
219         modest_widget_memory_save (conf, G_OBJECT(priv->folder_paned),
220                                    "modest-folder-paned");
221         modest_widget_memory_save (conf, G_OBJECT(priv->msg_paned),
222                                    "modest-msg-paned");
223         modest_widget_memory_save (conf, G_OBJECT(priv->main_paned),
224                                    "modest-main-paned");
225         modest_widget_memory_save (conf, G_OBJECT(priv->header_view), "header-view");
226 }
227
228 static GtkWidget*
229 wrapped_in_scrolled_window (GtkWidget *widget, gboolean needs_viewport)
230 {
231         GtkWidget *win;
232
233         win = gtk_scrolled_window_new (NULL, NULL);
234         gtk_scrolled_window_set_policy
235                 (GTK_SCROLLED_WINDOW (win),GTK_POLICY_NEVER,
236                  GTK_POLICY_AUTOMATIC);
237         
238         if (needs_viewport)
239                 gtk_scrolled_window_add_with_viewport
240                         (GTK_SCROLLED_WINDOW(win), widget);
241         else
242                 gtk_container_add (GTK_CONTAINER(win),
243                                    widget);
244
245         return win;
246 }
247
248
249 static gboolean
250 on_delete_event (GtkWidget *widget, GdkEvent  *event, ModestMainWindow *self)
251 {
252         save_sizes (self);
253         return FALSE;
254 }
255
256
257 ModestWindow *
258 modest_main_window_new (ModestWidgetFactory *widget_factory,
259                         TnyAccountStore *account_store)
260 {
261         GObject *obj;
262         ModestMainWindowPrivate *priv;
263         ModestWindowPrivate *parent_priv;
264         GtkWidget *main_vbox;
265         GtkWidget *status_hbox;
266         GtkWidget *header_win, *folder_win;
267         GtkActionGroup *action_group;
268         GError *error = NULL;
269         
270         g_return_val_if_fail (widget_factory, NULL);
271
272         obj  = g_object_new(MODEST_TYPE_MAIN_WINDOW, NULL);
273         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
274         parent_priv = MODEST_WINDOW_GET_PRIVATE(obj);
275
276         parent_priv->widget_factory = g_object_ref (widget_factory);
277         parent_priv->account_store  = g_object_ref (account_store);
278
279         /* ***************** */
280         parent_priv->ui_manager = gtk_ui_manager_new();
281         action_group = gtk_action_group_new ("ModestMainWindowActions");
282
283         /* Add common actions */
284         gtk_action_group_add_actions (action_group,
285                                       modest_action_entries,
286                                       G_N_ELEMENTS (modest_action_entries),
287                                       obj);
288
289         gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 0);
290         g_object_unref (action_group);
291
292         /* Load the UI definition */
293         gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager, MODEST_UIDIR "modest-ui.xml", &error);
294         if (error != NULL) {
295                 g_warning ("Could not merge modest-ui.xml: %s", error->message);
296                 g_error_free (error);
297                 error = NULL;
298         }
299         /* *************** */
300
301         /* Add accelerators */
302         gtk_window_add_accel_group (GTK_WINDOW (obj), 
303                                     gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
304
305
306         /* Toolbar / Menubar */
307         parent_priv->toolbar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar");
308         parent_priv->menubar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/MenuBar");
309
310         gtk_toolbar_set_tooltips (GTK_TOOLBAR (parent_priv->toolbar), TRUE);
311
312         /* widgets from factory */
313         priv->folder_view = modest_widget_factory_get_folder_view (widget_factory);
314         priv->header_view = header_view_new (MODEST_MAIN_WINDOW(obj));
315         priv->msg_preview = modest_widget_factory_get_msg_preview (widget_factory);
316         
317         folder_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->folder_view),
318                                                  FALSE);
319         header_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->header_view),
320                                                  FALSE);                           
321
322         /* Connect platform specific signals */
323         g_signal_connect (priv->header_view,
324                           "button-press-event",
325                           G_CALLBACK (on_header_view_button_press_event),
326                           obj);
327         g_signal_connect (priv->header_view,
328                           "popup-menu",
329                           G_CALLBACK (on_header_view_button_press_event),
330                           obj);
331         g_signal_connect (priv->folder_view,
332                           "button-press-event",
333                           G_CALLBACK (on_folder_view_button_press_event),
334                           obj);
335         g_signal_connect (priv->folder_view,
336                           "popup-menu",
337                           G_CALLBACK (on_folder_view_button_press_event),
338                           obj);
339
340
341         /* paned */
342         priv->folder_paned = gtk_vpaned_new ();
343         priv->msg_paned = gtk_vpaned_new ();
344         priv->main_paned = gtk_hpaned_new ();
345         gtk_paned_add1 (GTK_PANED(priv->main_paned), folder_win);
346         gtk_paned_add2 (GTK_PANED(priv->main_paned), priv->msg_paned);
347         gtk_paned_add1 (GTK_PANED(priv->msg_paned), header_win);
348         gtk_paned_add2 (GTK_PANED(priv->msg_paned), GTK_WIDGET(priv->msg_preview));
349
350         gtk_widget_show (GTK_WIDGET(priv->header_view));
351         
352         /* status bar / progress */
353         status_hbox = gtk_hbox_new (FALSE, 0);
354         gtk_box_pack_start (GTK_BOX(status_hbox),
355                             modest_widget_factory_get_folder_info_label (widget_factory),
356                             FALSE,FALSE, 6);
357         gtk_box_pack_start (GTK_BOX(status_hbox),
358                             modest_widget_factory_get_status_bar(widget_factory),
359                             TRUE, TRUE, 0);
360         gtk_box_pack_start (GTK_BOX(status_hbox),
361                             modest_widget_factory_get_progress_bar(widget_factory),
362                             FALSE, FALSE, 0);
363         gtk_box_pack_start (GTK_BOX(status_hbox),
364                           modest_widget_factory_get_online_toggle(widget_factory),
365                           FALSE, FALSE, 0);
366
367         /* putting it all together... */
368         main_vbox = gtk_vbox_new (FALSE, 6);
369         gtk_box_pack_start (GTK_BOX(main_vbox), parent_priv->menubar, FALSE, FALSE, 0);
370         gtk_box_pack_start (GTK_BOX(main_vbox), parent_priv->toolbar, FALSE, FALSE, 0);
371         gtk_box_pack_start (GTK_BOX(main_vbox), priv->main_paned, TRUE, TRUE,0);
372         gtk_box_pack_start (GTK_BOX(main_vbox), status_hbox, FALSE, FALSE, 0);
373         
374         gtk_container_add (GTK_CONTAINER(obj), main_vbox);
375         restore_sizes (MODEST_MAIN_WINDOW(obj));        
376
377         gtk_window_set_title (GTK_WINDOW(obj), _("Modest"));
378         gtk_window_set_icon  (GTK_WINDOW(obj),
379                               modest_icon_factory_get_icon (MODEST_APP_ICON));  
380         gtk_widget_show_all (main_vbox);
381         
382         g_signal_connect (G_OBJECT(obj), "delete-event",
383                           G_CALLBACK(on_delete_event), obj);
384         
385         return (ModestWindow *) obj;
386 }
387
388 static gboolean 
389 on_header_view_button_press_event (ModestHeaderView *header_view,
390                                    GdkEventButton   *event,
391                                    ModestMainWindow *self)
392 {
393         if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
394                 GtkWidget *menu;
395                 ModestWindowPrivate *parent_priv;
396         
397                 parent_priv = MODEST_WINDOW_GET_PRIVATE (self);
398                 menu = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/HeaderViewContextMenu");
399
400                 return show_context_popup_menu (self,
401                                                 GTK_TREE_VIEW (header_view), 
402                                                 event, 
403                                                 menu);
404         }
405
406         return FALSE;
407 }
408
409 static gboolean 
410 on_folder_view_button_press_event (ModestFolderView *folder_view,
411                                    GdkEventButton   *event,
412                                    ModestMainWindow *self)
413 {
414         if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
415                 GtkWidget *menu;
416                 ModestWindowPrivate *parent_priv;
417         
418                 parent_priv = MODEST_WINDOW_GET_PRIVATE (self);
419                 menu = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/FolderViewContextMenu");
420
421                 return show_context_popup_menu (self,
422                                                 GTK_TREE_VIEW (folder_view), 
423                                                 event, 
424                                                 menu);
425         }
426
427         return FALSE;
428 }
429
430 static gboolean 
431 show_context_popup_menu (ModestMainWindow *window,
432                          GtkTreeView *tree_view,
433                          GdkEventButton   *event,                        
434                          GtkWidget *menu)
435 {
436         g_return_val_if_fail (menu, FALSE);
437
438         if (event != NULL) {
439                 /* Ensure that the header is selected */
440                 GtkTreeSelection *selection;
441
442                 selection = gtk_tree_view_get_selection (tree_view);
443         
444                 if (gtk_tree_selection_count_selected_rows (selection) <= 1) {
445                         GtkTreePath *path;
446                 
447                         /* Get tree path for row that was clicked */
448                         if (gtk_tree_view_get_path_at_pos (tree_view,
449                                                            (gint) event->x, 
450                                                            (gint) event->y,
451                                                            &path, 
452                                                            NULL, NULL, NULL)) {
453                                 gtk_tree_selection_unselect_all (selection);
454                                 gtk_tree_selection_select_path (selection, path);
455                                 gtk_tree_path_free (path);
456                         }
457                 }
458
459                 /* Show popup */
460                 if (gtk_tree_selection_count_selected_rows(selection) == 1)
461                         gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
462                                         NULL, NULL,
463                                         event->button, event->time);
464         }
465
466         return TRUE;
467 }