Change no accounts label position and size
[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 <hildon/hildon-helper.h>
36 #include <modest-ui-actions.h>
37 #include <modest-window-mgr.h>
38 #include <modest-signal-mgr.h>
39 #include <modest-runtime.h>
40 #include <modest-platform.h>
41 #include <hildon/hildon-program.h>
42 #include <hildon/hildon-button.h>
43 #include <modest-maemo-utils.h>
44 #include <modest-icon-names.h>
45 #include <modest-defs.h>
46 #include <modest-folder-window.h>
47 #include <modest-ui-dimming-rules.h>
48 #include <modest-ui-dimming-manager.h>
49 #include <modest-window-priv.h>
50 #include <modest-ui-constants.h>
51 #include <modest-account-mgr-helpers.h>
52 #include <modest-mailboxes-window.h>
53
54
55 /* 'private'/'protected' functions */
56 static void modest_accounts_window_class_init  (ModestAccountsWindowClass *klass);
57 static void modest_accounts_window_instance_init (ModestAccountsWindow *obj);
58 static void modest_accounts_window_finalize    (GObject *obj);
59
60 static void connect_signals (ModestAccountsWindow *self);
61 static void modest_accounts_window_disconnect_signals (ModestWindow *self);
62
63 static void on_account_activated (GtkTreeView *treeview,
64                                   GtkTreePath *path,
65                                   GtkTreeViewColumn *column,
66                                   ModestAccountsWindow *accounts_window);
67 static void on_progress_list_changed (ModestWindowMgr *mgr,
68                                       ModestAccountsWindow *self);
69 static void setup_menu (ModestAccountsWindow *self);
70 static gboolean _modest_accounts_window_map_event (GtkWidget *widget,
71                                                    GdkEvent *event,
72                                                    gpointer userdata);
73 static void update_progress_hint (ModestAccountsWindow *self);
74 static void on_row_inserted (GtkTreeModel *tree_model,
75                              GtkTreePath  *path,
76                              GtkTreeIter  *iter,
77                              gpointer      user_data);
78 static void on_row_deleted (GtkTreeModel *tree_model,
79                             GtkTreePath  *path,
80                             gpointer      user_data);
81 static void row_count_changed (ModestAccountsWindow *self);
82
83 typedef struct _ModestAccountsWindowPrivate ModestAccountsWindowPrivate;
84 struct _ModestAccountsWindowPrivate {
85
86         GtkWidget *box;
87         GtkWidget *pannable;
88         GtkWidget *account_view;
89         GtkWidget *no_accounts_container;
90         GtkWidget *new_message_button;
91
92         /* signals */
93         GSList *sighandlers;
94
95         gboolean progress_hint;
96 };
97 #define MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
98                                                                             MODEST_TYPE_ACCOUNTS_WINDOW, \
99                                                                             ModestAccountsWindowPrivate))
100
101 /* globals */
102 static GtkWindowClass *parent_class = NULL;
103
104 /************************************************************************/
105
106 GType
107 modest_accounts_window_get_type (void)
108 {
109         static GType my_type = 0;
110         if (!my_type) {
111                 static const GTypeInfo my_info = {
112                         sizeof(ModestAccountsWindowClass),
113                         NULL,           /* base init */
114                         NULL,           /* base finalize */
115                         (GClassInitFunc) modest_accounts_window_class_init,
116                         NULL,           /* class finalize */
117                         NULL,           /* class data */
118                         sizeof(ModestAccountsWindow),
119                         1,              /* n_preallocs */
120                         (GInstanceInitFunc) modest_accounts_window_instance_init,
121                         NULL
122                 };
123                 my_type = g_type_register_static (MODEST_TYPE_HILDON2_WINDOW,
124                                                   "ModestAccountsWindow",
125                                                   &my_info, 0);
126         }
127         return my_type;
128 }
129
130 static void
131 modest_accounts_window_class_init (ModestAccountsWindowClass *klass)
132 {
133         GObjectClass *gobject_class;
134         gobject_class = (GObjectClass*) klass;
135         ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
136
137         parent_class            = g_type_class_peek_parent (klass);
138         gobject_class->finalize = modest_accounts_window_finalize;
139
140         g_type_class_add_private (gobject_class, sizeof(ModestAccountsWindowPrivate));
141         
142         modest_window_class->disconnect_signals_func = modest_accounts_window_disconnect_signals;
143 }
144
145 static void
146 modest_accounts_window_instance_init (ModestAccountsWindow *obj)
147 {
148         ModestAccountsWindowPrivate *priv;
149
150         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(obj);
151
152         priv->sighandlers = NULL;
153         
154         priv->account_view = NULL;
155         priv->progress_hint = FALSE;
156         
157         modest_window_mgr_register_help_id (modest_runtime_get_window_mgr(),
158                                             GTK_WINDOW(obj),
159                                             "applications_email_accountsview");
160 }
161
162 static void
163 modest_accounts_window_finalize (GObject *obj)
164 {
165         ModestAccountsWindowPrivate *priv;
166
167         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(obj);
168
169         /* Sanity check: shouldn't be needed, the window mgr should
170            call this function before */
171         modest_accounts_window_disconnect_signals (MODEST_WINDOW (obj));        
172
173         G_OBJECT_CLASS(parent_class)->finalize (obj);
174 }
175
176 static void
177 modest_accounts_window_disconnect_signals (ModestWindow *self)
178 {       
179         ModestAccountsWindowPrivate *priv;      
180         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
181
182         modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
183         priv->sighandlers = NULL;       
184 }
185
186 static void
187 connect_signals (ModestAccountsWindow *self)
188 {
189         ModestAccountsWindowPrivate *priv;
190         GtkTreeModel *model;
191
192         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
193
194         /* accounts view */
195         priv->sighandlers = 
196                 modest_signal_mgr_connect (priv->sighandlers,
197                                            G_OBJECT (priv->account_view), "row-activated", 
198                                            G_CALLBACK (on_account_activated), self);
199
200         priv->sighandlers = 
201                 modest_signal_mgr_connect (priv->sighandlers,
202                                            G_OBJECT (modest_runtime_get_window_mgr ()),
203                                            "progress-list-changed",
204                                            G_CALLBACK (on_progress_list_changed), self);
205
206         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->account_view));
207
208         priv->sighandlers =
209                 modest_signal_mgr_connect (priv->sighandlers,
210                                            G_OBJECT (model),
211                                            "row-inserted",
212                                            G_CALLBACK (on_row_inserted), self);
213
214         priv->sighandlers =
215                 modest_signal_mgr_connect (priv->sighandlers,
216                                            G_OBJECT (model),
217                                            "row-deleted",
218                                            G_CALLBACK (on_row_deleted), self);
219
220         priv->sighandlers = 
221                 modest_signal_mgr_connect (priv->sighandlers,
222                                            G_OBJECT (priv->new_message_button),
223                                            "clicked",
224                                            G_CALLBACK (modest_ui_actions_on_new_msg), self);
225
226         /* we don't register this in sighandlers, as it should be run
227          * after disconnecting all signals, in destroy stage */
228 }
229
230 ModestWindow *
231 modest_accounts_window_new (void)
232 {
233         ModestAccountsWindow *self = NULL;
234         ModestAccountsWindowPrivate *priv = NULL;
235         HildonProgram *app;
236         GdkPixbuf *window_icon;
237         GdkPixbuf *new_message_pixbuf;
238         GtkWidget *action_area_box;
239         guint accel_key;
240         GdkModifierType accel_mods;
241         GtkAccelGroup *accel_group;
242         GtkWidget *no_accounts_label;
243         GtkWidget *empty_view_new_message_button;
244         GtkWidget *box_alignment;
245
246         self  = MODEST_ACCOUNTS_WINDOW(g_object_new(MODEST_TYPE_ACCOUNTS_WINDOW, NULL));
247         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
248
249         new_message_pixbuf = modest_platform_get_icon ("general_add", MODEST_ICON_SIZE_BIG);
250
251         box_alignment = gtk_alignment_new (0, 0, 1.0, 1.0);
252         gtk_alignment_set_padding (GTK_ALIGNMENT (box_alignment), 
253                                    0, 0,
254                                    HILDON_MARGIN_DOUBLE, HILDON_MARGIN_DOUBLE);
255         priv->box = gtk_vbox_new (FALSE, 0);
256
257         no_accounts_label = gtk_label_new (_("mcen_ia_noaccounts"));
258         
259         gtk_misc_set_alignment (GTK_MISC (no_accounts_label), 0.5, 0.5);
260         hildon_helper_set_logical_font (no_accounts_label, "LargeSystemFont");
261
262         empty_view_new_message_button = hildon_button_new (MODEST_EDITABLE_SIZE, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
263         hildon_button_set_title (HILDON_BUTTON (empty_view_new_message_button), _("mcen_ti_new_message"));
264         hildon_button_set_image (HILDON_BUTTON (empty_view_new_message_button), gtk_image_new_from_pixbuf (new_message_pixbuf));
265
266
267         priv->no_accounts_container = gtk_vbox_new (FALSE, 0);
268         gtk_box_pack_start (GTK_BOX (priv->no_accounts_container), empty_view_new_message_button, FALSE, FALSE, 0);
269         gtk_widget_show_all (empty_view_new_message_button);
270         gtk_box_pack_end (GTK_BOX (priv->no_accounts_container), no_accounts_label, TRUE, TRUE, 0);
271         gtk_widget_show (no_accounts_label);
272         gtk_box_pack_start (GTK_BOX (priv->box), priv->no_accounts_container, TRUE, TRUE, 0);
273
274         g_signal_connect (G_OBJECT (empty_view_new_message_button),
275                           "clicked",
276                           G_CALLBACK (modest_ui_actions_on_new_msg), self);
277         
278         priv->pannable = hildon_pannable_area_new ();
279         priv->account_view  = GTK_WIDGET (modest_account_view_new (modest_runtime_get_account_mgr ()));
280
281         action_area_box = hildon_tree_view_get_action_area_box (GTK_TREE_VIEW (priv->account_view));
282         priv->new_message_button = hildon_button_new (0, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
283
284         hildon_button_set_title (HILDON_BUTTON (priv->new_message_button), _("mcen_ti_new_message"));
285         hildon_button_set_image (HILDON_BUTTON (priv->new_message_button), gtk_image_new_from_pixbuf (new_message_pixbuf));
286
287         gtk_box_pack_start (GTK_BOX (action_area_box), priv->new_message_button, TRUE, TRUE, 0);
288         gtk_widget_show_all (priv->new_message_button);
289         hildon_tree_view_set_action_area_visible (GTK_TREE_VIEW (priv->account_view), TRUE);
290
291         g_object_unref (new_message_pixbuf);
292         setup_menu (self);
293
294         gtk_container_add (GTK_CONTAINER (priv->pannable), priv->account_view);
295         gtk_box_pack_start (GTK_BOX (priv->box), priv->pannable, TRUE, TRUE, 0);
296         gtk_container_add (GTK_CONTAINER (box_alignment), priv->box);
297         gtk_container_add (GTK_CONTAINER (self), box_alignment);
298
299         gtk_widget_show (priv->account_view);
300         gtk_widget_show (priv->pannable);
301         gtk_widget_show (priv->box);
302         gtk_widget_show (box_alignment);
303
304         connect_signals (MODEST_ACCOUNTS_WINDOW (self));
305
306         /* Load previous osso state, for instance if we are being restored from 
307          * hibernation:  */
308         modest_osso_load_state ();
309
310         /* Get device name */
311         modest_maemo_utils_get_device_name ();
312
313         app = hildon_program_get_instance ();
314         hildon_program_add_window (app, HILDON_WINDOW (self));
315         
316         /* Set window icon */
317         window_icon = modest_platform_get_icon (MODEST_APP_ICON, MODEST_ICON_SIZE_BIG);
318         if (window_icon) {
319                 gtk_window_set_icon (GTK_WINDOW (self), window_icon);
320                 g_object_unref (window_icon);
321         }
322
323         /* Dont't restore settings here, 
324          * because it requires a gtk_widget_show(), 
325          * and we don't want to do that until later,
326          * so that the UI is not visible for non-menu D-Bus activation.
327          */
328
329         g_signal_connect (G_OBJECT (self), "map-event",
330                           G_CALLBACK (_modest_accounts_window_map_event),
331                           G_OBJECT (self));
332         update_progress_hint (self);
333
334         row_count_changed (self);
335
336         accel_group = gtk_accel_group_new ();
337         gtk_accelerator_parse ("<Control>n", &accel_key, &accel_mods);
338         gtk_widget_add_accelerator (priv->new_message_button, "clicked", accel_group,
339                                     accel_key, accel_mods, 0);
340         gtk_window_add_accel_group (GTK_WINDOW (self), accel_group);
341
342         return MODEST_WINDOW(self);
343 }
344
345 ModestAccountView *
346 modest_accounts_window_get_account_view (ModestAccountsWindow *self)
347 {
348         ModestAccountsWindowPrivate *priv = NULL;
349
350         g_return_val_if_fail (MODEST_IS_ACCOUNTS_WINDOW(self), FALSE);
351
352         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
353         
354         return MODEST_ACCOUNT_VIEW (priv->account_view);
355 }
356
357 static void 
358 setup_menu (ModestAccountsWindow *self)
359 {
360         g_return_if_fail (MODEST_IS_ACCOUNTS_WINDOW(self));
361
362         /* Settings menu buttons */
363         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_new_account"), NULL, 
364                                            APP_MENU_CALLBACK (modest_ui_actions_on_new_account), 
365                                            NULL);
366         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_edit_accounts"), NULL,
367                                            APP_MENU_CALLBACK (modest_ui_actions_on_accounts), 
368                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_edit_accounts));
369         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_globalsmtpservers"), NULL,
370                                            APP_MENU_CALLBACK (modest_ui_actions_on_smtp_servers),
371                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_tools_smtp_servers));
372         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_sendandreceive"), NULL,
373                                            APP_MENU_CALLBACK (modest_ui_actions_on_send_receive),
374                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_send_receive_all));
375         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_outbox_cancelsend"), NULL,
376                                            APP_MENU_CALLBACK (modest_ui_actions_cancel_send),
377                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_cancel_sending_all));
378         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_options"), NULL,
379                                            APP_MENU_CALLBACK (modest_ui_actions_on_settings), 
380                                            NULL);
381 }
382
383
384 static void
385 on_account_activated (GtkTreeView *account_view,
386                       GtkTreePath *path,
387                       GtkTreeViewColumn *column,
388                       ModestAccountsWindow *self)
389 {
390         ModestAccountsWindowPrivate *priv;
391         gchar* account_name; 
392         GtkWidget *new_window;
393         gboolean registered;
394         ModestProtocolType store_protocol;
395         gboolean mailboxes_protocol;
396
397         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
398
399         account_name = modest_account_view_get_path_account (MODEST_ACCOUNT_VIEW (priv->account_view), path);
400         if (!account_name)
401                 return;
402
403         /* If it's a multimailbox container, we have to show the mailboxes window */
404         store_protocol = modest_account_mgr_get_store_protocol (modest_runtime_get_account_mgr (), 
405                                                                 account_name);
406         mailboxes_protocol = 
407                 modest_protocol_registry_protocol_type_has_tag (modest_runtime_get_protocol_registry (),
408                                                                 store_protocol,
409                                                                 MODEST_PROTOCOL_REGISTRY_MULTI_MAILBOX_PROVIDER_PROTOCOLS);
410         if (mailboxes_protocol) {
411                 new_window = GTK_WIDGET (modest_mailboxes_window_new (account_name));
412         } else {
413
414                 new_window = GTK_WIDGET (modest_folder_window_new (NULL));
415         }
416
417         registered = modest_window_mgr_register_window (modest_runtime_get_window_mgr (), 
418                                                         MODEST_WINDOW (new_window),
419                                                         MODEST_WINDOW (self));
420
421         if (!registered) {
422                 gtk_widget_destroy (new_window);
423                 new_window = NULL;
424         } else {
425                 if (!mailboxes_protocol) {
426                         modest_folder_window_set_account (MODEST_FOLDER_WINDOW (new_window), account_name);
427                 }
428                 gtk_widget_show (new_window);
429         }
430         g_free (account_name);
431 }
432
433 static gboolean
434 _modest_accounts_window_map_event (GtkWidget *widget,
435                                    GdkEvent *event,
436                                    gpointer userdata)
437 {
438         ModestAccountsWindow *self = (ModestAccountsWindow *) userdata;
439         ModestAccountsWindowPrivate *priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
440
441         if (priv->progress_hint) {
442                 hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), TRUE);
443         }
444
445         return FALSE;
446 }
447
448 static void
449 update_progress_hint (ModestAccountsWindow *self)
450 {
451         ModestAccountsWindowPrivate *priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
452
453         priv->progress_hint = modest_window_mgr_has_progress_operation (modest_runtime_get_window_mgr ());
454         
455         if (GTK_WIDGET_VISIBLE (self)) {
456                 hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), priv->progress_hint?1:0);
457         }
458 }
459
460 static void
461 on_progress_list_changed (ModestWindowMgr *mgr,
462                           ModestAccountsWindow *self)
463 {
464         update_progress_hint (self);
465 }
466
467 static void
468 on_row_inserted (GtkTreeModel *tree_model,
469                  GtkTreePath  *path,
470                  GtkTreeIter  *iter,
471                  gpointer      user_data)
472 {
473         ModestAccountsWindow *self;
474
475         self = (ModestAccountsWindow *) user_data;
476
477         row_count_changed (self);
478 }
479
480 static void
481 on_row_deleted (GtkTreeModel *tree_model,
482                 GtkTreePath  *path,
483                 gpointer      user_data)
484 {
485         ModestAccountsWindow *self;
486
487         self = (ModestAccountsWindow *) user_data;
488
489         row_count_changed (self);
490 }
491
492 static void row_count_changed (ModestAccountsWindow *self)
493 {
494         ModestAccountsWindowPrivate *priv;
495         GtkTreeModel *model;
496         gint count;
497         
498         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
499         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->account_view));
500
501         count = gtk_tree_model_iter_n_children (model, NULL);
502
503         if (count == 0) {
504                 gtk_widget_show (priv->no_accounts_container);
505         } else {
506                 gtk_widget_hide (priv->no_accounts_container);
507         }
508         gtk_container_child_set (GTK_CONTAINER(priv->box), priv->pannable, 
509                                  "expand", count > 0,
510                                  "fill", count > 0,
511                                  NULL);
512 }