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