248829acf68d83f39b12c9caa9d51394dee57750
[modest] / src / hildon2 / modest-mailboxes-window.c
1 /* Copyright (c) 2009, 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-mailboxes-window.h>
31 #include <modest-osso-state-saving.h>
32 #include <libosso.h>
33 #include <hildon/hildon-pannable-area.h>
34 #include <modest-window-mgr.h>
35 #include <modest-signal-mgr.h>
36 #include <modest-runtime.h>
37 #include <modest-platform.h>
38 #include <modest-maemo-utils.h>
39 #include <modest-icon-names.h>
40 #include <modest-ui-constants.h>
41 #include <modest-account-mgr.h>
42 #include <modest-account-mgr-helpers.h>
43 #include <modest-defs.h>
44 #include <modest-ui-actions.h>
45 #include <modest-window.h>
46 #include <hildon/hildon-program.h>
47 #include <hildon/hildon-banner.h>
48 #include <hildon/hildon-button.h>
49 #include <tny-account-store-view.h>
50 #include <modest-header-window.h>
51 #include <modest-ui-dimming-rules.h>
52 #include <modest-ui-dimming-manager.h>
53 #include <modest-window-priv.h>
54 #include "modest-text-utils.h"
55 #include "modest-tny-account.h"
56 #include <modest-folder-window.h>
57
58 /* 'private'/'protected' functions */
59 static void modest_mailboxes_window_class_init  (ModestMailboxesWindowClass *klass);
60 static void modest_mailboxes_window_init        (ModestMailboxesWindow *obj);
61 static void modest_mailboxes_window_finalize    (GObject *obj);
62
63 static void connect_signals (ModestMailboxesWindow *self);
64 static void modest_mailboxes_window_disconnect_signals (ModestWindow *self);
65
66 static void on_mailbox_activated (ModestFolderView *mailboxes_view,
67                                   TnyFolder *folder,
68                                   gpointer userdata);
69 typedef struct _ModestMailboxesWindowPrivate ModestMailboxesWindowPrivate;
70 struct _ModestMailboxesWindowPrivate {
71
72         GtkWidget *folder_view;
73         GtkWidget *top_vbox;
74         GtkWidget *new_message_button;
75
76         /* signals */
77         GSList *sighandlers;
78
79         gchar *current_store_account;
80 };
81 #define MODEST_MAILBOXES_WINDOW_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
82                                                                           MODEST_TYPE_MAILBOXES_WINDOW, \
83                                                                           ModestMailboxesWindowPrivate))
84
85 /* globals */
86 static GtkWindowClass *parent_class = NULL;
87
88 /************************************************************************/
89
90 GType
91 modest_mailboxes_window_get_type (void)
92 {
93         static GType my_type = 0;
94         if (!my_type) {
95                 static const GTypeInfo my_info = {
96                         sizeof(ModestMailboxesWindowClass),
97                         NULL,           /* base init */
98                         NULL,           /* base finalize */
99                         (GClassInitFunc) modest_mailboxes_window_class_init,
100                         NULL,           /* class finalize */
101                         NULL,           /* class data */
102                         sizeof(ModestMailboxesWindow),
103                         1,              /* n_preallocs */
104                         (GInstanceInitFunc) modest_mailboxes_window_init,
105                         NULL
106                 };
107                 my_type = g_type_register_static (MODEST_TYPE_HILDON2_WINDOW,
108                                                   "ModestMailboxesWindow",
109                                                   &my_info, 0);
110         }
111         return my_type;
112 }
113
114 static void
115 modest_mailboxes_window_class_init (ModestMailboxesWindowClass *klass)
116 {
117         GObjectClass *gobject_class;
118         gobject_class = (GObjectClass*) klass;
119         ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
120
121         parent_class            = g_type_class_peek_parent (klass);
122         gobject_class->finalize = modest_mailboxes_window_finalize;
123
124         g_type_class_add_private (gobject_class, sizeof(ModestMailboxesWindowPrivate));
125         
126         modest_window_class->disconnect_signals_func = modest_mailboxes_window_disconnect_signals;
127 }
128
129 static void
130 modest_mailboxes_window_init (ModestMailboxesWindow *obj)
131 {
132         ModestMailboxesWindowPrivate *priv;
133
134         priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE(obj);
135
136         priv->sighandlers = NULL;
137         
138         priv->folder_view = NULL;
139
140         priv->top_vbox = NULL;
141
142         priv->current_store_account = NULL;
143 }
144
145 static void
146 modest_mailboxes_window_finalize (GObject *obj)
147 {
148         ModestMailboxesWindowPrivate *priv;
149
150         priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE(obj);
151
152         if (priv->current_store_account) {
153                 g_free (priv->current_store_account);
154                 priv->current_store_account = NULL;
155         }
156
157         /* Sanity check: shouldn't be needed, the window mgr should
158            call this function before */
159         modest_mailboxes_window_disconnect_signals (MODEST_WINDOW (obj));       
160
161         G_OBJECT_CLASS(parent_class)->finalize (obj);
162 }
163
164 static void
165 modest_mailboxes_window_disconnect_signals (ModestWindow *self)
166 {
167         ModestMailboxesWindowPrivate *priv;
168         priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE(self);
169
170         modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
171         priv->sighandlers = NULL;
172 }
173
174 static void
175 on_visible_account_changed (ModestFolderView *mailboxes_view,
176                             const gchar *account_id,
177                             gpointer user_data)
178 {
179         if (account_id) {
180                 TnyAccount *acc = 
181                         modest_tny_account_store_get_tny_account_by (modest_runtime_get_account_store(),
182                                                                      MODEST_TNY_ACCOUNT_STORE_QUERY_ID,
183                                                                      account_id);
184                 if (acc) {
185                         const gchar *name;
186                         gchar *title = NULL;
187
188                         name = modest_tny_account_get_parent_modest_account_name_for_server_account (acc);
189                         title = modest_account_mgr_get_display_name (modest_runtime_get_account_mgr(),
190                                                                      name);
191                         if (title) {
192                                 gtk_window_set_title (GTK_WINDOW (user_data), title);
193                                 g_free (title);
194                         } else {
195                                 gtk_window_set_title (GTK_WINDOW (user_data), _("mcen_ap_name"));
196                         }
197                         g_object_unref (acc);
198                 }
199         } else {
200                 gtk_window_set_title (GTK_WINDOW (user_data), _("mcen_ap_name"));
201         }
202 }
203
204 static void
205 connect_signals (ModestMailboxesWindow *self)
206 {
207         ModestMailboxesWindowPrivate *priv;
208
209         priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE(self);
210
211         /* mailboxes view */
212         priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers, 
213                                                        G_OBJECT (priv->folder_view), "folder-activated", 
214                                                        G_CALLBACK (on_mailbox_activated), self);
215
216         priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers,
217                                                        G_OBJECT (priv->folder_view),
218                                                        "visible-account-changed",
219                                                        G_CALLBACK (on_visible_account_changed), self);
220
221         priv->sighandlers = 
222                 modest_signal_mgr_connect (priv->sighandlers,
223                                            G_OBJECT (priv->new_message_button),
224                                            "clicked",
225                                            G_CALLBACK (modest_ui_actions_on_new_msg), self);
226
227 }
228
229 ModestWindow *
230 modest_mailboxes_window_new (const gchar *account)
231 {
232         ModestMailboxesWindow *self = NULL;     
233         ModestMailboxesWindowPrivate *priv = NULL;
234         HildonProgram *app;
235         GdkPixbuf *window_icon;
236         GtkWidget *pannable;
237         GtkWidget *action_area_box;
238         GdkPixbuf *new_message_pixbuf;
239         guint accel_key;
240         GdkModifierType accel_mods;
241         GtkAccelGroup *accel_group;
242         GtkWidget *top_alignment;
243         
244         self  = MODEST_MAILBOXES_WINDOW(g_object_new(MODEST_TYPE_MAILBOXES_WINDOW, NULL));
245         priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE(self);
246
247         pannable = hildon_pannable_area_new ();
248         priv->folder_view  = modest_platform_create_folder_view (NULL);
249         modest_folder_view_set_cell_style (MODEST_FOLDER_VIEW (priv->folder_view),
250                                            MODEST_FOLDER_VIEW_CELL_STYLE_COMPACT);
251         modest_folder_view_set_filter (MODEST_FOLDER_VIEW (priv->folder_view), 
252                                        MODEST_FOLDER_VIEW_FILTER_HIDE_ACCOUNTS);
253
254         action_area_box = hildon_tree_view_get_action_area_box (GTK_TREE_VIEW (priv->folder_view));
255         priv->new_message_button = hildon_button_new (0, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
256
257         hildon_button_set_title (HILDON_BUTTON (priv->new_message_button), _("mcen_ti_new_message"));
258         new_message_pixbuf = modest_platform_get_icon ("general_add", MODEST_ICON_SIZE_BIG);
259         hildon_button_set_image (HILDON_BUTTON (priv->new_message_button), gtk_image_new_from_pixbuf (new_message_pixbuf));
260         g_object_unref (new_message_pixbuf);
261
262         gtk_box_pack_start (GTK_BOX (action_area_box), priv->new_message_button, TRUE, TRUE, 0);
263         gtk_widget_show_all (priv->new_message_button);
264         hildon_tree_view_set_action_area_visible (GTK_TREE_VIEW (priv->folder_view), TRUE);
265         
266         /* Set account store */
267         tny_account_store_view_set_account_store (TNY_ACCOUNT_STORE_VIEW (priv->folder_view),
268                                                   TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
269
270         priv->top_vbox = gtk_vbox_new (0, FALSE);
271         top_alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
272         gtk_alignment_set_padding (GTK_ALIGNMENT (top_alignment), 
273                                    0, 0, 
274                                    MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE);
275
276         gtk_container_add (GTK_CONTAINER (pannable), priv->folder_view);
277         gtk_box_pack_end (GTK_BOX (priv->top_vbox), pannable, TRUE, TRUE, 0);
278         gtk_container_add (GTK_CONTAINER (top_alignment), priv->top_vbox);
279         gtk_container_add (GTK_CONTAINER (self), top_alignment);
280
281         gtk_widget_show (priv->folder_view);
282         gtk_widget_show (pannable);
283         gtk_widget_show (top_alignment);
284         gtk_widget_show (priv->top_vbox);
285
286         connect_signals (MODEST_MAILBOXES_WINDOW (self));
287
288         /* Load previous osso state, for instance if we are being restored from 
289          * hibernation:  */
290         modest_osso_load_state ();
291
292         /* Get device name */
293         modest_maemo_utils_get_device_name ();
294
295         app = hildon_program_get_instance ();
296         hildon_program_add_window (app, HILDON_WINDOW (self));
297         
298         /* Set window icon */
299         window_icon = modest_platform_get_icon (MODEST_APP_ICON, MODEST_ICON_SIZE_BIG);
300         if (window_icon) {
301                 gtk_window_set_icon (GTK_WINDOW (self), window_icon);
302                 g_object_unref (window_icon);
303         }
304
305         /* Dont't restore settings here, 
306          * because it requires a gtk_widget_show(), 
307          * and we don't want to do that until later,
308          * so that the UI is not visible for non-menu D-Bus activation.
309          */
310
311         /* Register edit modes */
312         accel_group = gtk_accel_group_new ();
313         gtk_accelerator_parse ("<Control>n", &accel_key, &accel_mods);
314         gtk_widget_add_accelerator (priv->new_message_button, "clicked", accel_group,
315                                     accel_key, accel_mods, 0);
316         gtk_window_add_accel_group (GTK_WINDOW (self), accel_group);
317
318         modest_folder_view_set_filter (MODEST_FOLDER_VIEW (priv->folder_view),
319                                        MODEST_FOLDER_VIEW_FILTER_SHOW_ONLY_MAILBOXES);
320
321         modest_mailboxes_window_set_account (MODEST_MAILBOXES_WINDOW (self), account);
322
323         return MODEST_WINDOW(self);
324 }
325
326 ModestFolderView *
327 modest_mailboxes_window_get_folder_view (ModestMailboxesWindow *self)
328 {
329         ModestMailboxesWindowPrivate *priv = NULL;
330
331         g_return_val_if_fail (MODEST_IS_MAILBOXES_WINDOW(self), FALSE);
332
333         priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE (self);
334         
335         return MODEST_FOLDER_VIEW (priv->folder_view);
336 }
337
338 void
339 modest_mailboxes_window_set_account (ModestMailboxesWindow *self,
340                                      const gchar *account_name)
341 {
342         ModestMailboxesWindowPrivate *priv = NULL;
343         ModestAccountMgr *mgr;
344         ModestAccountSettings *settings = NULL;
345         ModestServerAccountSettings *store_settings = NULL;
346
347         g_return_if_fail (MODEST_IS_MAILBOXES_WINDOW(self));
348
349         priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE (self);
350
351         /* Get account data */
352         mgr = modest_runtime_get_account_mgr ();
353
354         settings = modest_account_mgr_load_account_settings (mgr, account_name);
355         if (!settings)
356                 goto free_refs;
357
358         store_settings = modest_account_settings_get_store_settings (settings);
359         if (!store_settings)
360                 goto free_refs;
361
362         if (priv->current_store_account != NULL)
363                 g_free (priv->current_store_account);
364         priv->current_store_account = g_strdup (modest_server_account_settings_get_account_name (store_settings));
365
366         modest_folder_view_set_account_id_of_visible_server_account
367                 (MODEST_FOLDER_VIEW (priv->folder_view),
368                  priv->current_store_account);
369
370         modest_window_set_active_account (MODEST_WINDOW (self), account_name);
371
372 free_refs:
373         if (store_settings)
374                 g_object_unref (store_settings);
375         if (settings)
376                 g_object_unref (settings);
377
378 }
379
380 static void
381 on_mailbox_activated (ModestFolderView *mailboxes_view,
382                       TnyFolder *folder,
383                       gpointer userdata)
384 {
385         ModestMailboxesWindowPrivate *priv = NULL;
386         ModestMailboxesWindow *self = (ModestMailboxesWindow *) userdata;
387         GtkWidget *new_window;
388         gboolean registered;
389         const gchar *active_account;
390
391         g_return_if_fail (MODEST_IS_MAILBOXES_WINDOW(self));
392
393         priv = MODEST_MAILBOXES_WINDOW_GET_PRIVATE (self);
394
395         if (!folder)
396                 return;
397
398         if (!TNY_IS_FOLDER (folder))
399                 return;
400
401         new_window = GTK_WIDGET (modest_folder_window_new (NULL));
402         registered = modest_window_mgr_register_window (modest_runtime_get_window_mgr (), 
403                                                         MODEST_WINDOW (new_window),
404                                                         MODEST_WINDOW (self));
405
406         if (!registered) {
407                 gtk_widget_destroy (new_window);
408                 new_window = NULL;
409         } else {
410                 const gchar *name;
411                 active_account = modest_window_get_active_account (MODEST_WINDOW (self));
412                 modest_folder_window_set_account (MODEST_FOLDER_WINDOW (new_window), active_account);
413                 name = tny_folder_get_name (folder);
414                 if (name) {
415                         modest_folder_window_set_mailbox (MODEST_FOLDER_WINDOW (new_window), name);
416                 }
417                 gtk_widget_show (new_window);
418         }
419         
420 }
421