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