* Added code to handle fullscreen mode for the whole application
[modest] / src / maemo / modest-main-window.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <hildon-widgets/hildon-window.h>
31 #include <hildon-widgets/hildon-note.h>
32
33 #include <glib/gi18n.h>
34 #include <gtk/gtktreeviewcolumn.h>
35 #include <tny-account-store-view.h>
36 #include <modest-runtime.h>
37 #include <string.h>
38
39 #include <widgets/modest-main-window.h>
40 #include <widgets/modest-msg-edit-window.h>
41 #include <widgets/modest-account-view-window.h>
42
43
44 #include "modest-widget-memory.h"
45 #include "modest-window-priv.h"
46 #include "modest-main-window-ui.h"
47 #include "modest-account-mgr.h"
48 #include "modest-conf.h"
49 #include <modest-maemo-utils.h>
50 #include "modest-tny-platform-factory.h"
51 #include "modest-tny-msg.h"
52 #include "modest-mail-operation.h"
53 #include "modest-icon-names.h"
54
55 /* 'private'/'protected' functions */
56 static void modest_main_window_class_init    (ModestMainWindowClass *klass);
57 static void modest_main_window_init          (ModestMainWindow *obj);
58 static void modest_main_window_finalize      (GObject *obj);
59
60 static void connect_signals (ModestMainWindow *self);
61 static void restore_sizes (ModestMainWindow *self);
62 static void save_sizes (ModestMainWindow *self);
63
64 /* list my signals */
65 enum {
66         /* MY_SIGNAL_1, */
67         /* MY_SIGNAL_2, */
68         LAST_SIGNAL
69 };
70
71 typedef struct _ModestMainWindowPrivate ModestMainWindowPrivate;
72 struct _ModestMainWindowPrivate {
73         GtkWidget *msg_paned;
74         GtkWidget *main_paned;
75         GtkWidget *progress_bar;
76
77         ModestHeaderView *header_view;
78         ModestFolderView *folder_view;
79
80 };
81 #define MODEST_MAIN_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
82                                                 MODEST_TYPE_MAIN_WINDOW, \
83                                                 ModestMainWindowPrivate))
84
85 typedef struct _GetMsgAsyncHelper {
86         ModestMainWindowPrivate *main_window_private;
87         guint action;
88         ModestTnyMsgReplyType reply_type;
89         ModestTnyMsgForwardType forward_type;
90         gchar *from;
91         TnyIterator *iter;
92 } GetMsgAsyncHelper;
93
94
95 /* FIXME use an enum not values, UI actions must know them. Create a
96    modest-window-mgr-ui.h and include it here?  */
97 static const GtkRadioActionEntry main_window_radio_action_entries [] = {
98         { "ViewFolders", NULL, N_("mcen_me_inbox_hidefolders"), NULL, NULL, 0 },
99         { "ViewFullscreen", NULL, N_("mcen_me_inbox_fullscreen"), NULL, NULL, 1 },
100 };
101
102 /* globals */
103 static GtkWindowClass *parent_class = NULL;
104
105 /* uncomment the following if you have defined any signals */
106 /* static guint signals[LAST_SIGNAL] = {0}; */
107
108 GType
109 modest_main_window_get_type (void)
110 {
111         static GType my_type = 0;
112         if (!my_type) {
113                 static const GTypeInfo my_info = {
114                         sizeof(ModestMainWindowClass),
115                         NULL,           /* base init */
116                         NULL,           /* base finalize */
117                         (GClassInitFunc) modest_main_window_class_init,
118                         NULL,           /* class finalize */
119                         NULL,           /* class data */
120                         sizeof(ModestMainWindow),
121                         1,              /* n_preallocs */
122                         (GInstanceInitFunc) modest_main_window_init,
123                         NULL
124                 };
125                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
126                                                   "ModestMainWindow",
127                                                   &my_info, 0);
128         }
129         return my_type;
130 }
131
132 static void
133 modest_main_window_class_init (ModestMainWindowClass *klass)
134 {
135         GObjectClass *gobject_class;
136         gobject_class = (GObjectClass*) klass;
137
138         parent_class            = g_type_class_peek_parent (klass);
139         gobject_class->finalize = modest_main_window_finalize;
140
141         g_type_class_add_private (gobject_class, sizeof(ModestMainWindowPrivate));
142 }
143
144
145 static void
146 on_key_changed (ModestConf* conf, const gchar *key, ModestConfEvent event, ModestMainWindow *win)
147 {
148         TnyAccount *account;
149         
150         if (!key || strcmp (key, MODEST_CONF_DEVICE_NAME) != 0)
151                 return; /* wrong key */
152         
153         /* ok, the device name changed; thus, we have to update the
154          * local folder account name*/
155         account =
156                 modest_tny_account_store_get_tny_account_by_account (modest_runtime_get_account_store(),
157                                                                      MODEST_LOCAL_FOLDERS_ACCOUNT_ID,
158                                                                      TNY_ACCOUNT_TYPE_STORE);
159         if (!account) {
160                 g_printerr ("modest: could not get account\n");
161                 return;
162         }
163
164         if (event == MODEST_CONF_EVENT_KEY_UNSET) 
165                 tny_account_set_name (account, MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME);
166         else {
167                 gchar *device_name = modest_conf_get_string (modest_runtime_get_conf(),
168                                                              MODEST_CONF_DEVICE_NAME, NULL);
169                 tny_account_set_name (account, device_name);
170                 g_free (device_name);
171         }
172         g_object_unref (G_OBJECT(account));     
173 }
174
175 static void
176 modest_main_window_init (ModestMainWindow *obj)
177 {
178         ModestMainWindowPrivate *priv;
179
180         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
181
182         priv->msg_paned    = NULL;
183         priv->main_paned   = NULL;      
184         priv->header_view  = NULL;
185         priv->folder_view  = NULL;
186
187         /* progress bar */
188         priv->progress_bar = gtk_progress_bar_new ();
189         gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR(priv->progress_bar), 1.0);
190         gtk_progress_bar_set_ellipsize (GTK_PROGRESS_BAR(priv->progress_bar),
191                                         PANGO_ELLIPSIZE_END);
192 }
193
194 static void
195 modest_main_window_finalize (GObject *obj)
196 {
197         G_OBJECT_CLASS(parent_class)->finalize (obj);
198 }
199
200 GtkWidget*
201 modest_main_window_get_child_widget (ModestMainWindow *self,
202                                      ModestWidgetType widget_type)
203 {
204         ModestMainWindowPrivate *priv;
205         GtkWidget *widget;
206         
207         g_return_val_if_fail (self, NULL);
208         g_return_val_if_fail (widget_type >= 0 && widget_type < MODEST_WIDGET_TYPE_NUM,
209                               NULL);
210         
211         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
212
213         switch (widget_type) {
214         case MODEST_WIDGET_TYPE_HEADER_VIEW:
215                 widget = (GtkWidget*)priv->header_view; break;
216         case MODEST_WIDGET_TYPE_FOLDER_VIEW:
217                 widget = (GtkWidget*)priv->folder_view; break;
218         default:
219                 return NULL;
220         }
221
222         return widget ? GTK_WIDGET(widget) : NULL;
223 }
224
225
226
227 static void
228 restore_sizes (ModestMainWindow *self)
229 {
230         ModestConf *conf;
231         ModestMainWindowPrivate *priv;
232
233         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
234
235         conf = modest_runtime_get_conf ();
236         
237         modest_widget_memory_restore (conf, G_OBJECT(priv->main_paned),
238                                       "modest-main-paned");
239         modest_widget_memory_restore (conf, G_OBJECT(priv->header_view),
240                                       "header-view");
241 }
242
243
244 static void
245 save_sizes (ModestMainWindow *self)
246 {
247         ModestConf *conf;
248         ModestMainWindowPrivate *priv;
249                 
250         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
251         conf = modest_runtime_get_conf ();
252         
253         modest_widget_memory_save (conf,G_OBJECT(self), "modest-main-window");
254         modest_widget_memory_save (conf, G_OBJECT(priv->main_paned),
255                                    "modest-main-paned");
256         modest_widget_memory_save (conf, G_OBJECT(priv->header_view), "header-view");
257 }
258
259 static GtkWidget*
260 wrapped_in_scrolled_window (GtkWidget *widget, gboolean needs_viewport)
261 {
262         GtkWidget *win;
263
264         win = gtk_scrolled_window_new (NULL, NULL);
265         gtk_scrolled_window_set_policy
266                 (GTK_SCROLLED_WINDOW (win),GTK_POLICY_NEVER,
267                  GTK_POLICY_AUTOMATIC);
268         
269         if (needs_viewport)
270                 gtk_scrolled_window_add_with_viewport
271                         (GTK_SCROLLED_WINDOW(win), widget);
272         else
273                 gtk_container_add (GTK_CONTAINER(win),
274                                    widget);
275
276         return win;
277 }
278
279
280 static gboolean
281 on_delete_event (GtkWidget *widget, GdkEvent  *event, ModestMainWindow *self)
282 {
283         save_sizes (self);
284         return FALSE;
285 }
286
287 static void
288 set_homogeneous (GtkWidget *widget,
289                  gpointer data)
290 {
291         gtk_tool_item_set_expand (GTK_TOOL_ITEM (widget), TRUE);
292         gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (widget), TRUE);
293 }
294
295 static GtkWidget*
296 get_toolbar (ModestMainWindow *self)
297 {
298         GtkWidget   *toolbar, *reply_button, *menu;
299         ModestWindowPrivate *parent_priv;
300         
301         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
302         
303         /* Toolbar */
304         toolbar = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar");
305
306         /* Set homogeneous toolbar */
307         gtk_container_foreach (GTK_CONTAINER (toolbar), set_homogeneous, NULL);
308
309         /* Set reply message tap and hold menu */
310         reply_button = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar/ToolbarMessageReply");
311         menu = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolbarReplyContextMenu");
312         gtk_widget_tap_and_hold_setup (GTK_WIDGET (reply_button), menu, NULL, 0);
313
314         return toolbar;
315 }
316
317 static void
318 connect_signals (ModestMainWindow *self)
319 {       
320         ModestWindowPrivate *parent_priv;
321         ModestMainWindowPrivate *priv;
322         GtkWidget *menu;
323         
324         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
325         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
326         
327         /* folder view */
328         g_signal_connect (G_OBJECT(priv->folder_view), "folder_selection_changed",
329                           G_CALLBACK(modest_ui_actions_on_folder_selection_changed), self);
330
331         menu = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/FolderViewContextMenu");
332         gtk_widget_tap_and_hold_setup (GTK_WIDGET (priv->folder_view), menu, NULL, 0);
333
334         /* header view */
335 /*      g_signal_connect (G_OBJECT(priv->header_view), "status_update", */
336 /*                        G_CALLBACK(modest_ui_actions_on_header_status_update), self); */
337         g_signal_connect (G_OBJECT(priv->header_view), "header_selected",
338                           G_CALLBACK(modest_ui_actions_on_header_selected), self);
339         g_signal_connect (G_OBJECT(priv->header_view), "header_activated",
340                           G_CALLBACK(modest_ui_actions_on_header_activated), self);
341         g_signal_connect (G_OBJECT(priv->header_view), "item_not_found",
342                           G_CALLBACK(modest_ui_actions_on_item_not_found), self);
343
344         /* window */
345         g_signal_connect (G_OBJECT(self), "delete-event", G_CALLBACK(on_delete_event), self);
346
347         
348         /* modest_maemo_utils_get_device_name will probably change
349          * MODEST_CONF_DEVICE_NAME. If that happens, we update the local folders
350          * account name in the callback
351          */
352         g_signal_connect (G_OBJECT(modest_runtime_get_conf()), "key_changed",
353                           G_CALLBACK(on_key_changed), self);
354         
355         g_signal_connect (G_OBJECT(self), "delete-event", G_CALLBACK(on_delete_event), self);
356 }
357
358
359 gboolean
360 sync_accounts_cb (ModestMainWindow *win)
361 {
362         modest_ui_actions_on_send_receive (NULL, MODEST_WINDOW(win));
363         return FALSE;
364 }
365
366
367 ModestWindow*
368 modest_main_window_new (void)
369 {
370         ModestMainWindow *self; 
371         ModestMainWindowPrivate *priv;
372         ModestWindowPrivate *parent_priv;
373         GtkWidget *main_vbox;
374         GtkWidget *header_win, *folder_win;
375         GtkActionGroup *action_group;
376         GError *error = NULL;
377         TnyFolderStoreQuery     *query;
378
379         self  = MODEST_MAIN_WINDOW(g_object_new(MODEST_TYPE_MAIN_WINDOW, NULL));
380         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
381         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
382
383         parent_priv->ui_manager = gtk_ui_manager_new();
384         action_group = gtk_action_group_new ("ModestMainWindowActions");
385         gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
386
387         /* Add common actions */
388         gtk_action_group_add_actions (action_group,
389                                       modest_action_entries,
390                                       G_N_ELEMENTS (modest_action_entries),
391                                       self);
392
393         gtk_action_group_add_radio_actions (action_group,
394                                             main_window_radio_action_entries,
395                                             G_N_ELEMENTS (main_window_radio_action_entries),
396                                             0,
397                                             G_CALLBACK (modest_ui_actions_on_change_fullscreen),
398                                             self);
399         
400         gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 0);
401         g_object_unref (action_group);
402
403         /* Load the UI definition */
404         gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager,
405                                          MODEST_UIDIR "modest-main-window-ui.xml", &error);
406         if (error != NULL) {
407                 g_warning ("Could not merge modest-ui.xml: %s", error->message);
408                 g_error_free (error);
409                 error = NULL;
410         }
411
412         /* Add accelerators */
413         gtk_window_add_accel_group (GTK_WINDOW (self), 
414                                     gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
415
416         /* add the toolbar */
417         parent_priv->toolbar = get_toolbar(self);
418         hildon_window_add_toolbar (HILDON_WINDOW (self), GTK_TOOLBAR (parent_priv->toolbar));
419
420
421         /* Menubar */
422         parent_priv->menubar = modest_maemo_utils_menubar_to_menu (parent_priv->ui_manager);
423         hildon_window_set_menu (HILDON_WINDOW (self), GTK_MENU (parent_priv->menubar));
424
425         /* folder view */
426         query = tny_folder_store_query_new ();
427         tny_folder_store_query_add_item (query, NULL,
428                                          TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
429         priv->folder_view = MODEST_FOLDER_VIEW(modest_folder_view_new (query));
430         if (!priv->folder_view)
431                 g_printerr ("modest: cannot instantiate folder view\n");        
432         g_object_unref (G_OBJECT (query));      
433         modest_maemo_utils_get_device_name ();
434
435         /* header view */
436         priv->header_view  =
437                 MODEST_HEADER_VIEW(modest_header_view_new (NULL, MODEST_HEADER_VIEW_STYLE_DETAILS));
438         if (!priv->header_view)
439                 g_printerr ("modest: cannot instantiate header view\n");
440         modest_header_view_set_style (priv->header_view, MODEST_HEADER_VIEW_STYLE_TWOLINES);
441         
442         folder_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->folder_view), FALSE);
443         header_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->header_view), FALSE);                    
444         /* paned */
445         priv->main_paned = gtk_hpaned_new ();
446         gtk_paned_add1 (GTK_PANED(priv->main_paned), folder_win);
447         gtk_paned_add2 (GTK_PANED(priv->main_paned), header_win);
448         gtk_widget_show (GTK_WIDGET(priv->header_view));
449         gtk_tree_view_columns_autosize (GTK_TREE_VIEW(priv->header_view));
450
451         /* putting it all together... */
452         main_vbox = gtk_vbox_new (FALSE, 6);
453         gtk_box_pack_start (GTK_BOX(main_vbox), priv->main_paned, TRUE, TRUE,0);
454
455         gtk_container_add (GTK_CONTAINER(self), main_vbox);
456         restore_sizes (MODEST_MAIN_WINDOW(self));
457         
458         gtk_window_set_title (GTK_WINDOW(self), _("Modest"));
459         gtk_window_set_icon_from_file (GTK_WINDOW(self), MODEST_APP_ICON, NULL);
460         gtk_widget_show_all (main_vbox);
461
462 /*      /\* should we hide the toolbar? *\/ */
463 /*      if (!modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_SHOW_TOOLBAR, NULL)) */
464 /*              gtk_widget_hide (parent_priv->toolbar); */
465
466         /* Connect signals */
467         connect_signals (self);
468
469         /* Set account store */
470         tny_account_store_view_set_account_store (TNY_ACCOUNT_STORE_VIEW (priv->folder_view),
471                                                   TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
472         g_idle_add ((GSourceFunc)sync_accounts_cb, self);
473         /* do send & receive when we are idle */        
474
475         g_message ("online? %s",
476                    tny_device_is_online (modest_runtime_get_device()) ? "yes" : "no");
477
478         /* Needed to show the contents of the toolbar */
479         gtk_widget_show_all (GTK_WIDGET (self));
480                 
481         return MODEST_WINDOW(self);
482 }
483
484 gboolean 
485 modest_main_window_close_all (ModestMainWindow *self)
486 {
487         GtkWidget *note;
488         GtkResponseType response;
489
490         /* Create the confirmation dialog MSG-NOT308 */
491         note = hildon_note_new_confirmation_add_buttons (GTK_WINDOW (self),
492                                                          _("emev_nc_close_windows"),
493                                                          _("mcen_db_yes"), GTK_RESPONSE_YES,
494                                                          _("mcen_db_no"), GTK_RESPONSE_NO,
495                                                          NULL);
496
497         response = gtk_dialog_run (GTK_DIALOG (note));
498         gtk_widget_destroy (GTK_WIDGET (note));
499
500         if (response == GTK_RESPONSE_YES)
501                 return TRUE;
502         else
503                 return FALSE;
504 }