* Now on opening from main window the account, it just sets the proper account in...
[modest] / src / hildon2 / modest-folder-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-folder-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 <hildon/hildon-program.h>
46 #include <hildon/hildon-banner.h>
47 #include <tny-account-store-view.h>
48
49 /* 'private'/'protected' functions */
50 static void modest_folder_window_class_init  (ModestFolderWindowClass *klass);
51 static void modest_folder_window_init        (ModestFolderWindow *obj);
52 static void modest_folder_window_finalize    (GObject *obj);
53
54 static void connect_signals (ModestFolderWindow *self);
55 static void modest_folder_window_disconnect_signals (ModestWindow *self);
56
57 static gboolean on_zoom_minus_plus_not_implemented (ModestWindow *window);
58 static void add_to_menu (ModestFolderWindow *self,
59                          HildonAppMenu *menu,
60                          gchar *label,
61                          GCallback callback);
62 static void setup_menu (ModestFolderWindow *self);
63
64 typedef struct _ModestFolderWindowPrivate ModestFolderWindowPrivate;
65 struct _ModestFolderWindowPrivate {
66
67         GtkWidget *folder_view;
68
69         /* signals */
70         GSList *sighandlers;
71
72         /* Display state */
73         osso_display_state_t display_state;
74 };
75 #define MODEST_FOLDER_WINDOW_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
76                                                                           MODEST_TYPE_FOLDER_WINDOW, \
77                                                                           ModestFolderWindowPrivate))
78
79 /* globals */
80 static GtkWindowClass *parent_class = NULL;
81
82 /************************************************************************/
83
84 GType
85 modest_folder_window_get_type (void)
86 {
87         static GType my_type = 0;
88         if (!my_type) {
89                 static const GTypeInfo my_info = {
90                         sizeof(ModestFolderWindowClass),
91                         NULL,           /* base init */
92                         NULL,           /* base finalize */
93                         (GClassInitFunc) modest_folder_window_class_init,
94                         NULL,           /* class finalize */
95                         NULL,           /* class data */
96                         sizeof(ModestFolderWindow),
97                         1,              /* n_preallocs */
98                         (GInstanceInitFunc) modest_folder_window_init,
99                         NULL
100                 };
101                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
102                                                   "ModestFolderWindow",
103                                                   &my_info, 0);
104         }
105         return my_type;
106 }
107
108 static void
109 modest_folder_window_class_init (ModestFolderWindowClass *klass)
110 {
111         GObjectClass *gobject_class;
112         gobject_class = (GObjectClass*) klass;
113         ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
114
115         parent_class            = g_type_class_peek_parent (klass);
116         gobject_class->finalize = modest_folder_window_finalize;
117
118         g_type_class_add_private (gobject_class, sizeof(ModestFolderWindowPrivate));
119         
120         modest_window_class->zoom_minus_func = on_zoom_minus_plus_not_implemented;
121         modest_window_class->zoom_plus_func = on_zoom_minus_plus_not_implemented;
122         modest_window_class->disconnect_signals_func = modest_folder_window_disconnect_signals;
123 }
124
125 static void
126 modest_folder_window_init (ModestFolderWindow *obj)
127 {
128         ModestFolderWindowPrivate *priv;
129
130         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(obj);
131
132         priv->sighandlers = NULL;
133         priv->display_state = OSSO_DISPLAY_ON;
134         
135         priv->folder_view = NULL;
136         
137         modest_window_mgr_register_help_id (modest_runtime_get_window_mgr(),
138                                             GTK_WINDOW(obj),
139                                             "applications_email_folderview");
140 }
141
142 static void
143 modest_folder_window_finalize (GObject *obj)
144 {
145         ModestFolderWindowPrivate *priv;
146
147         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(obj);
148
149         /* Sanity check: shouldn't be needed, the window mgr should
150            call this function before */
151         modest_folder_window_disconnect_signals (MODEST_WINDOW (obj));  
152
153         G_OBJECT_CLASS(parent_class)->finalize (obj);
154 }
155
156 static void
157 modest_folder_window_disconnect_signals (ModestWindow *self)
158 {       
159         ModestFolderWindowPrivate *priv;        
160         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(self);
161
162         modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
163         priv->sighandlers = NULL;       
164 }
165
166 static void
167 connect_signals (ModestFolderWindow *self)
168 {       
169         ModestFolderWindowPrivate *priv;
170         
171         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(self);
172
173         /* folder view */
174
175         /* TODO: connect folder view activate */
176         
177         /* window */
178
179         /* we don't register this in sighandlers, as it should be run after disconnecting all signals,
180          * in destroy stage */
181
182         
183 }
184
185 static void 
186 osso_display_event_cb (osso_display_state_t state, 
187                        gpointer data)
188 {
189         ModestFolderWindowPrivate *priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (data);
190
191         priv->display_state = state;
192
193         /* Stop blinking if the screen becomes on */
194         if (priv->display_state == OSSO_DISPLAY_ON)
195                 modest_platform_remove_new_mail_notifications (TRUE);
196 }
197
198 ModestWindow *
199 modest_folder_window_new (TnyFolderStoreQuery *query)
200 {
201         ModestFolderWindow *self = NULL;        
202         ModestFolderWindowPrivate *priv = NULL;
203         HildonProgram *app;
204         GdkPixbuf *window_icon;
205         GtkWidget *pannable;
206         
207         self  = MODEST_FOLDER_WINDOW(g_object_new(MODEST_TYPE_FOLDER_WINDOW, NULL));
208         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(self);
209         pannable = hildon_pannable_area_new ();
210         priv->folder_view  = modest_platform_create_folder_view (query);
211         modest_folder_view_set_cell_style (MODEST_FOLDER_VIEW (priv->folder_view),
212                                            MODEST_FOLDER_VIEW_CELL_STYLE_COMPACT);
213
214         setup_menu (self);
215
216         /* Set account store */
217         tny_account_store_view_set_account_store (TNY_ACCOUNT_STORE_VIEW (priv->folder_view),
218                                                   TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
219
220         gtk_container_add (GTK_CONTAINER (pannable), priv->folder_view);
221         gtk_container_add (GTK_CONTAINER (self), pannable);
222
223         gtk_widget_show (priv->folder_view);
224         gtk_widget_show (pannable);
225
226         connect_signals (MODEST_FOLDER_WINDOW (self));
227
228         /* Load previous osso state, for instance if we are being restored from 
229          * hibernation:  */
230         modest_osso_load_state ();
231
232         /* Get device name */
233         modest_maemo_utils_get_device_name ();
234
235         app = hildon_program_get_instance ();
236         hildon_program_add_window (app, HILDON_WINDOW (self));
237         
238         /* Set window icon */
239         window_icon = modest_platform_get_icon (MODEST_APP_ICON, MODEST_ICON_SIZE_BIG);
240         if (window_icon) {
241                 gtk_window_set_icon (GTK_WINDOW (self), window_icon);
242                 g_object_unref (window_icon);
243         }
244
245         /* Listen for changes in the screen, we don't want to show a
246            led pattern when the display is on for example */
247         osso_hw_set_display_event_cb (modest_maemo_utils_get_osso_context (),
248                                       osso_display_event_cb,
249                                       self); 
250
251         /* Dont't restore settings here, 
252          * because it requires a gtk_widget_show(), 
253          * and we don't want to do that until later,
254          * so that the UI is not visible for non-menu D-Bus activation.
255          */
256
257         return MODEST_WINDOW(self);
258 }
259
260 static gboolean
261 on_zoom_minus_plus_not_implemented (ModestWindow *window)
262 {
263         g_return_val_if_fail (MODEST_IS_FOLDER_WINDOW (window), FALSE);
264
265         hildon_banner_show_information (NULL, NULL, dgettext("hildon-common-strings", "ckct_ib_cannot_zoom_here"));
266         return FALSE;
267
268 }
269
270 gboolean
271 modest_folder_window_screen_is_on (ModestFolderWindow *self)
272 {
273         ModestFolderWindowPrivate *priv = NULL;
274
275         g_return_val_if_fail (MODEST_IS_FOLDER_WINDOW(self), FALSE);
276
277         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
278         
279         return (priv->display_state == OSSO_DISPLAY_ON) ? TRUE : FALSE;
280 }
281
282 ModestFolderView *
283 modest_folder_window_get_folder_view (ModestFolderWindow *self)
284 {
285         ModestFolderWindowPrivate *priv = NULL;
286
287         g_return_val_if_fail (MODEST_IS_FOLDER_WINDOW(self), FALSE);
288
289         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
290         
291         return MODEST_FOLDER_VIEW (priv->folder_view);
292 }
293
294 void
295 modest_folder_window_set_account (ModestFolderWindow *self,
296                                   const gchar *account_name)
297 {
298         ModestFolderWindowPrivate *priv = NULL;
299         ModestAccountMgr *mgr;
300         ModestAccountSettings *settings = NULL;
301         ModestServerAccountSettings *store_settings = NULL;
302
303         g_return_if_fail (MODEST_IS_FOLDER_WINDOW(self));
304
305         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
306         
307         /* Get account data */
308         mgr = modest_runtime_get_account_mgr ();
309
310         settings = modest_account_mgr_load_account_settings (mgr, account_name);
311         if (!settings)
312                 goto free_refs;
313
314         store_settings = modest_account_settings_get_store_settings (settings);
315         if (!store_settings)
316                 goto free_refs;
317
318         modest_folder_view_set_account_id_of_visible_server_account 
319                 (MODEST_FOLDER_VIEW (priv->folder_view),
320                  modest_server_account_settings_get_account_name (store_settings));
321
322         modest_window_set_active_account (MODEST_WINDOW (self), account_name);
323         gtk_window_set_title (GTK_WINDOW (self), 
324                               modest_account_settings_get_display_name (settings));
325
326 free_refs:
327         if (store_settings) 
328                 g_object_unref (store_settings);
329         if (settings)
330                 g_object_unref (settings);
331
332 }
333
334 static void add_to_menu (ModestFolderWindow *self,
335                          HildonAppMenu *menu,
336                          gchar *label,
337                          GCallback callback)
338 {
339         GtkWidget *button;
340
341         button = gtk_button_new_with_label (label);
342         g_signal_connect_after (G_OBJECT (button), "clicked",
343                                 callback, (gpointer) self);
344         hildon_app_menu_append (menu, GTK_BUTTON (button));
345 }
346
347 static void setup_menu (ModestFolderWindow *self)
348 {
349         ModestFolderWindowPrivate *priv = NULL;
350         GtkWidget *app_menu;
351
352         g_return_if_fail (MODEST_IS_FOLDER_WINDOW(self));
353
354         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
355
356         app_menu = hildon_app_menu_new ();
357
358         add_to_menu (self, HILDON_APP_MENU (app_menu), _("mcen_me_viewer_newemail"),
359                      G_CALLBACK (modest_ui_actions_on_new_msg));
360         add_to_menu (self, HILDON_APP_MENU (app_menu), _("mcen_me_inbox_sendandreceive"),
361                      G_CALLBACK (modest_ui_actions_on_send_receive));
362
363         /* Settings menu buttons */
364         add_to_menu (self, HILDON_APP_MENU (app_menu), _("mcen_me_inbox_options"),
365                      G_CALLBACK (modest_ui_actions_on_settings));
366         add_to_menu (self, HILDON_APP_MENU (app_menu), _("mcen_me_inbox_accounts"),
367                      G_CALLBACK (modest_ui_actions_on_accounts));
368         add_to_menu (self, HILDON_APP_MENU (app_menu), _("mcen_me_inbox_globalsmtpservers"),
369                      G_CALLBACK (modest_ui_actions_on_smtp_servers));
370         
371         hildon_stackable_window_set_main_menu (HILDON_STACKABLE_WINDOW (self), 
372                                                HILDON_APP_MENU (app_menu));
373 }