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