fddce682020ab07ad3615adaa00040bde882e487
[modest] / src / hildon2 / modest-accounts-window.c
1 /* Copyright (c) 2008, 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 <modest-accounts-window.h>
31 #include <modest-osso-state-saving.h>
32 #include <libosso.h>
33 #include <hildon/hildon-pannable-area.h>
34 #include <hildon/hildon-banner.h>
35 #include <modest-ui-actions.h>
36 #include <modest-window-mgr.h>
37 #include <modest-signal-mgr.h>
38 #include <modest-runtime.h>
39 #include <modest-platform.h>
40 #include <hildon/hildon-program.h>
41 #include <modest-maemo-utils.h>
42 #include <modest-icon-names.h>
43 #include <modest-defs.h>
44 #include <modest-folder-window.h>
45 #include <modest-ui-dimming-rules.h>
46 #include <modest-ui-dimming-manager.h>
47 #include <modest-window-priv.h>
48
49
50 /* 'private'/'protected' functions */
51 static void modest_accounts_window_class_init  (ModestAccountsWindowClass *klass);
52 static void modest_accounts_window_instance_init (ModestAccountsWindow *obj);
53 static void modest_accounts_window_finalize    (GObject *obj);
54
55 static void connect_signals (ModestAccountsWindow *self);
56 static void modest_accounts_window_disconnect_signals (ModestWindow *self);
57
58 static gboolean on_zoom_minus_plus_not_implemented (ModestWindow *window);
59 static void on_account_activated (GtkTreeView *treeview,
60                                   GtkTreePath *path,
61                                   GtkTreeViewColumn *column,
62                                   ModestAccountsWindow *accounts_window);
63 static void add_to_menu (ModestAccountsWindow *self,
64                          HildonAppMenu *menu,
65                          gchar *label,
66                          GCallback callback,
67                          ModestDimmingRulesGroup *group,
68                          GCallback dimming_callback);
69 static void setup_menu (ModestAccountsWindow *self,
70                         ModestDimmingRulesGroup *group);
71
72 static void modest_accounts_window_show_toolbar (ModestWindow *self,
73                                                  gboolean show_toolbar);
74 static gboolean modest_accounts_window_toggle_menu (HildonWindow *window,
75                                                     guint button,
76                                                     guint32 time);
77
78 typedef struct _ModestAccountsWindowPrivate ModestAccountsWindowPrivate;
79 struct _ModestAccountsWindowPrivate {
80
81         GtkWidget *account_view;
82         GtkWidget *app_menu;
83
84         /* signals */
85         GSList *sighandlers;
86
87         /* Display state */
88         osso_display_state_t display_state;
89 };
90 #define MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
91                                                                             MODEST_TYPE_ACCOUNTS_WINDOW, \
92                                                                             ModestAccountsWindowPrivate))
93
94 /* globals */
95 static GtkWindowClass *parent_class = NULL;
96
97 /************************************************************************/
98
99 GType
100 modest_accounts_window_get_type (void)
101 {
102         static GType my_type = 0;
103         if (!my_type) {
104                 static const GTypeInfo my_info = {
105                         sizeof(ModestAccountsWindowClass),
106                         NULL,           /* base init */
107                         NULL,           /* base finalize */
108                         (GClassInitFunc) modest_accounts_window_class_init,
109                         NULL,           /* class finalize */
110                         NULL,           /* class data */
111                         sizeof(ModestAccountsWindow),
112                         1,              /* n_preallocs */
113                         (GInstanceInitFunc) modest_accounts_window_instance_init,
114                         NULL
115                 };
116                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
117                                                   "ModestAccountsWindow",
118                                                   &my_info, 0);
119         }
120         return my_type;
121 }
122
123 static void
124 modest_accounts_window_class_init (ModestAccountsWindowClass *klass)
125 {
126         GObjectClass *gobject_class;
127         gobject_class = (GObjectClass*) klass;
128         ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
129         HildonWindowClass *hildon_window_class = (HildonWindowClass *) klass;
130
131         parent_class            = g_type_class_peek_parent (klass);
132         gobject_class->finalize = modest_accounts_window_finalize;
133
134         g_type_class_add_private (gobject_class, sizeof(ModestAccountsWindowPrivate));
135         
136         hildon_window_class->toggle_menu = modest_accounts_window_toggle_menu;
137
138         modest_window_class->zoom_minus_func = on_zoom_minus_plus_not_implemented;
139         modest_window_class->zoom_plus_func = on_zoom_minus_plus_not_implemented;
140         modest_window_class->show_toolbar_func = modest_accounts_window_show_toolbar;
141         modest_window_class->disconnect_signals_func = modest_accounts_window_disconnect_signals;
142 }
143
144 static void
145 modest_accounts_window_instance_init (ModestAccountsWindow *obj)
146 {
147         ModestAccountsWindowPrivate *priv;
148
149         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(obj);
150
151         priv->sighandlers = NULL;
152         priv->display_state = OSSO_DISPLAY_ON;
153         
154         priv->account_view = NULL;
155         
156         modest_window_mgr_register_help_id (modest_runtime_get_window_mgr(),
157                                             GTK_WINDOW(obj),
158                                             "applications_email_accountsview");
159 }
160
161 static void
162 modest_accounts_window_finalize (GObject *obj)
163 {
164         ModestAccountsWindowPrivate *priv;
165
166         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(obj);
167
168         /* Sanity check: shouldn't be needed, the window mgr should
169            call this function before */
170         modest_accounts_window_disconnect_signals (MODEST_WINDOW (obj));        
171
172         G_OBJECT_CLASS(parent_class)->finalize (obj);
173 }
174
175 static void
176 modest_accounts_window_disconnect_signals (ModestWindow *self)
177 {       
178         ModestAccountsWindowPrivate *priv;      
179         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
180
181         modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
182         priv->sighandlers = NULL;       
183 }
184
185 static void
186 connect_signals (ModestAccountsWindow *self)
187 {       
188         ModestAccountsWindowPrivate *priv;
189         
190         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
191
192         /* accounts view */
193         priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers, 
194                                                        G_OBJECT (priv->account_view), "row-activated", 
195                                                        G_CALLBACK (on_account_activated), self);
196
197         /* window */
198
199         /* we don't register this in sighandlers, as it should be run after disconnecting all signals,
200          * in destroy stage */
201
202         
203 }
204
205 static void 
206 osso_display_event_cb (osso_display_state_t state, 
207                        gpointer data)
208 {
209         ModestAccountsWindowPrivate *priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (data);
210
211         priv->display_state = state;
212
213         /* Stop blinking if the screen becomes on */
214         if (priv->display_state == OSSO_DISPLAY_ON)
215                 modest_platform_remove_new_mail_notifications (TRUE);
216 }
217
218 ModestWindow *
219 modest_accounts_window_new (void)
220 {
221         ModestAccountsWindow *self = NULL;      
222         ModestAccountsWindowPrivate *priv = NULL;
223         HildonProgram *app;
224         GdkPixbuf *window_icon;
225         GtkWidget *pannable;
226         ModestWindowPrivate *parent_priv = NULL;
227         ModestDimmingRulesGroup *menu_rules_group = NULL;
228         
229         self  = MODEST_ACCOUNTS_WINDOW(g_object_new(MODEST_TYPE_ACCOUNTS_WINDOW, NULL));
230         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
231         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
232
233         parent_priv->ui_dimming_manager = modest_ui_dimming_manager_new();
234         menu_rules_group = modest_dimming_rules_group_new (MODEST_DIMMING_RULES_MENU, FALSE);
235
236         pannable = hildon_pannable_area_new ();
237         priv->account_view  = GTK_WIDGET (modest_account_view_new (modest_runtime_get_account_mgr ()));
238
239         setup_menu (self, menu_rules_group);
240
241         modest_ui_dimming_manager_insert_rules_group (parent_priv->ui_dimming_manager, menu_rules_group);
242         g_object_unref (menu_rules_group);
243
244         gtk_container_add (GTK_CONTAINER (pannable), priv->account_view);
245         gtk_container_add (GTK_CONTAINER (self), pannable);
246
247         gtk_widget_show (priv->account_view);
248         gtk_widget_show (pannable);
249
250         connect_signals (MODEST_ACCOUNTS_WINDOW (self));
251
252         /* Load previous osso state, for instance if we are being restored from 
253          * hibernation:  */
254         modest_osso_load_state ();
255
256         /* Get device name */
257         modest_maemo_utils_get_device_name ();
258
259         app = hildon_program_get_instance ();
260         hildon_program_add_window (app, HILDON_WINDOW (self));
261         
262         /* Set window icon */
263         window_icon = modest_platform_get_icon (MODEST_APP_ICON, MODEST_ICON_SIZE_BIG);
264         if (window_icon) {
265                 gtk_window_set_icon (GTK_WINDOW (self), window_icon);
266                 g_object_unref (window_icon);
267         }
268
269         /* Listen for changes in the screen, we don't want to show a
270            led pattern when the display is on for example */
271         osso_hw_set_display_event_cb (modest_maemo_utils_get_osso_context (),
272                                       osso_display_event_cb,
273                                       self); 
274
275         /* Dont't restore settings here, 
276          * because it requires a gtk_widget_show(), 
277          * and we don't want to do that until later,
278          * so that the UI is not visible for non-menu D-Bus activation.
279          */
280
281         return MODEST_WINDOW(self);
282 }
283
284 static gboolean
285 on_zoom_minus_plus_not_implemented (ModestWindow *window)
286 {
287         g_return_val_if_fail (MODEST_IS_ACCOUNTS_WINDOW (window), FALSE);
288
289         hildon_banner_show_information (NULL, NULL, dgettext("hildon-common-strings", "ckct_ib_cannot_zoom_here"));
290         return FALSE;
291
292 }
293
294 gboolean
295 modest_accounts_window_screen_is_on (ModestAccountsWindow *self)
296 {
297         ModestAccountsWindowPrivate *priv = NULL;
298
299         g_return_val_if_fail (MODEST_IS_ACCOUNTS_WINDOW(self), FALSE);
300
301         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
302         
303         return (priv->display_state == OSSO_DISPLAY_ON) ? TRUE : FALSE;
304 }
305
306 ModestAccountView *
307 modest_accounts_window_get_account_view (ModestAccountsWindow *self)
308 {
309         ModestAccountsWindowPrivate *priv = NULL;
310
311         g_return_val_if_fail (MODEST_IS_ACCOUNTS_WINDOW(self), FALSE);
312
313         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
314         
315         return MODEST_ACCOUNT_VIEW (priv->account_view);
316 }
317
318 static void add_to_menu (ModestAccountsWindow *self,
319                          HildonAppMenu *menu,
320                          gchar *label,
321                          GCallback callback,
322                          ModestDimmingRulesGroup *group,
323                          GCallback dimming_callback)
324 {
325         GtkWidget *button;
326
327         button = gtk_button_new_with_label (label);
328         g_signal_connect_after (G_OBJECT (button), "clicked",
329                                 callback, (gpointer) self);
330         if (dimming_callback)
331                 modest_dimming_rules_group_add_widget_rule (group,
332                                                             button,
333                                                             dimming_callback,
334                                                             MODEST_WINDOW (self));
335         hildon_app_menu_append (menu, GTK_BUTTON (button));
336 }
337
338 static void setup_menu (ModestAccountsWindow *self, ModestDimmingRulesGroup *group)
339 {
340         ModestAccountsWindowPrivate *priv = NULL;
341
342         g_return_if_fail (MODEST_IS_ACCOUNTS_WINDOW(self));
343
344         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
345
346         priv->app_menu = hildon_app_menu_new ();
347
348         /* Settings menu buttons */
349         add_to_menu (self, HILDON_APP_MENU (priv->app_menu), _("TODO: new account"),
350                      G_CALLBACK (modest_ui_actions_on_new_account),
351                      group, NULL);
352         add_to_menu (self, HILDON_APP_MENU (priv->app_menu), _("TODO: edit accounts"),
353                      G_CALLBACK (modest_ui_actions_on_accounts),
354                      group, NULL);
355         add_to_menu (self, HILDON_APP_MENU (priv->app_menu), _("mcen_me_inbox_options"),
356                      G_CALLBACK (modest_ui_actions_on_settings),
357                      group, NULL);
358         add_to_menu (self, HILDON_APP_MENU (priv->app_menu), _("mcen_me_inbox_globalsmtpservers"),
359                      G_CALLBACK (modest_ui_actions_on_smtp_servers),
360                      group, G_CALLBACK (modest_ui_dimming_rules_on_tools_smtp_servers));
361         add_to_menu (self, HILDON_APP_MENU (priv->app_menu), _("mcen_me_viewer_newemail"),
362                      G_CALLBACK (modest_ui_actions_on_new_msg),
363                      group, G_CALLBACK (modest_ui_dimming_rules_on_new_msg));
364         add_to_menu (self, HILDON_APP_MENU (priv->app_menu), _("mcen_me_inbox_sendandreceive"),
365                      G_CALLBACK (modest_ui_actions_on_send_receive),
366                      group, G_CALLBACK (modest_ui_dimming_rules_on_send_receive_all));
367         add_to_menu (self, HILDON_APP_MENU (priv->app_menu), _("mcen_me_outbox_cancelsend"),
368                      G_CALLBACK (modest_ui_actions_cancel_send),
369                      group, G_CALLBACK (modest_ui_dimming_rules_on_cancel_sending_all));
370         
371         hildon_stackable_window_set_main_menu (HILDON_STACKABLE_WINDOW (self), 
372                                                HILDON_APP_MENU (priv->app_menu));
373 }
374
375 static gboolean 
376 modest_accounts_window_toggle_menu (HildonWindow *window,
377                                     guint button,
378                                     guint32 time)
379 {
380         ModestAccountsWindowPrivate *priv = NULL;
381
382         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (window);
383
384         modest_ui_actions_check_menu_dimming_rules (MODEST_WINDOW (window));
385
386         gtk_widget_queue_resize (priv->app_menu);
387
388         return HILDON_WINDOW_CLASS (parent_class)->toggle_menu (window, button, time);
389 }
390
391
392
393 static void
394 on_account_activated (GtkTreeView *account_view,
395                       GtkTreePath *path,
396                       GtkTreeViewColumn *column,
397                       ModestAccountsWindow *self)
398 {
399         ModestAccountsWindowPrivate *priv;
400         gchar* account_name; 
401         GtkWidget *folder_window;
402
403         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
404         
405         account_name = modest_account_view_get_path_account (MODEST_ACCOUNT_VIEW (priv->account_view), path);
406         if (!account_name)
407                 return;
408
409         folder_window = GTK_WIDGET (modest_folder_window_new (NULL));
410         modest_window_mgr_register_window (modest_runtime_get_window_mgr (), 
411                                            MODEST_WINDOW (folder_window),
412                                            MODEST_WINDOW (self));
413         modest_folder_window_set_account (MODEST_FOLDER_WINDOW (folder_window), account_name);
414         gtk_widget_show (folder_window);
415         g_free (account_name);
416
417 }
418
419 static void
420 modest_accounts_window_show_toolbar (ModestWindow *self,
421                                      gboolean show_toolbar)
422 {
423         /* Empty implementation, this view does not show any
424            toolbar */
425 }