0b4b1eec6caf0a88d2145a56c93a58ee698ac2eb
[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 <modest-maemo-utils.h>
42 #include <modest-icon-names.h>
43 #include <modest-defs.h>
44 #include <modest-folder-window.h>
45 #include <modest-ui-dimming-rules.h>
46 #include <modest-ui-dimming-manager.h>
47 #include <modest-window-priv.h>
48
49
50 /* 'private'/'protected' functions */
51 static void modest_accounts_window_class_init  (ModestAccountsWindowClass *klass);
52 static void modest_accounts_window_instance_init (ModestAccountsWindow *obj);
53 static void modest_accounts_window_finalize    (GObject *obj);
54
55 static void connect_signals (ModestAccountsWindow *self);
56 static void modest_accounts_window_disconnect_signals (ModestWindow *self);
57
58 static void on_account_activated (GtkTreeView *treeview,
59                                   GtkTreePath *path,
60                                   GtkTreeViewColumn *column,
61                                   ModestAccountsWindow *accounts_window);
62 static void setup_menu (ModestAccountsWindow *self);
63
64
65 typedef struct _ModestAccountsWindowPrivate ModestAccountsWindowPrivate;
66 struct _ModestAccountsWindowPrivate {
67
68         GtkWidget *account_view;
69
70         /* signals */
71         GSList *sighandlers;
72
73         /* Display state */
74         osso_display_state_t display_state;
75 };
76 #define MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
77                                                                             MODEST_TYPE_ACCOUNTS_WINDOW, \
78                                                                             ModestAccountsWindowPrivate))
79
80 /* globals */
81 static GtkWindowClass *parent_class = NULL;
82
83 /************************************************************************/
84
85 GType
86 modest_accounts_window_get_type (void)
87 {
88         static GType my_type = 0;
89         if (!my_type) {
90                 static const GTypeInfo my_info = {
91                         sizeof(ModestAccountsWindowClass),
92                         NULL,           /* base init */
93                         NULL,           /* base finalize */
94                         (GClassInitFunc) modest_accounts_window_class_init,
95                         NULL,           /* class finalize */
96                         NULL,           /* class data */
97                         sizeof(ModestAccountsWindow),
98                         1,              /* n_preallocs */
99                         (GInstanceInitFunc) modest_accounts_window_instance_init,
100                         NULL
101                 };
102                 my_type = g_type_register_static (MODEST_TYPE_HILDON2_WINDOW,
103                                                   "ModestAccountsWindow",
104                                                   &my_info, 0);
105         }
106         return my_type;
107 }
108
109 static void
110 modest_accounts_window_class_init (ModestAccountsWindowClass *klass)
111 {
112         GObjectClass *gobject_class;
113         gobject_class = (GObjectClass*) klass;
114         ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
115
116         parent_class            = g_type_class_peek_parent (klass);
117         gobject_class->finalize = modest_accounts_window_finalize;
118
119         g_type_class_add_private (gobject_class, sizeof(ModestAccountsWindowPrivate));
120         
121         modest_window_class->disconnect_signals_func = modest_accounts_window_disconnect_signals;
122 }
123
124 static void
125 modest_accounts_window_instance_init (ModestAccountsWindow *obj)
126 {
127         ModestAccountsWindowPrivate *priv;
128
129         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(obj);
130
131         priv->sighandlers = NULL;
132         priv->display_state = OSSO_DISPLAY_ON;
133         
134         priv->account_view = NULL;
135         
136         modest_window_mgr_register_help_id (modest_runtime_get_window_mgr(),
137                                             GTK_WINDOW(obj),
138                                             "applications_email_accountsview");
139 }
140
141 static void
142 modest_accounts_window_finalize (GObject *obj)
143 {
144         ModestAccountsWindowPrivate *priv;
145
146         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(obj);
147
148         /* Sanity check: shouldn't be needed, the window mgr should
149            call this function before */
150         modest_accounts_window_disconnect_signals (MODEST_WINDOW (obj));        
151
152         G_OBJECT_CLASS(parent_class)->finalize (obj);
153 }
154
155 static void
156 modest_accounts_window_disconnect_signals (ModestWindow *self)
157 {       
158         ModestAccountsWindowPrivate *priv;      
159         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
160
161         modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
162         priv->sighandlers = NULL;       
163 }
164
165 static void
166 connect_signals (ModestAccountsWindow *self)
167 {       
168         ModestAccountsWindowPrivate *priv;
169         
170         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
171
172         /* accounts view */
173         priv->sighandlers = modest_signal_mgr_connect (priv->sighandlers, 
174                                                        G_OBJECT (priv->account_view), "row-activated", 
175                                                        G_CALLBACK (on_account_activated), self);
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         ModestAccountsWindowPrivate *priv = MODEST_ACCOUNTS_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_accounts_window_new (void)
200 {
201         ModestAccountsWindow *self = NULL;      
202         ModestAccountsWindowPrivate *priv = NULL;
203         HildonProgram *app;
204         GdkPixbuf *window_icon;
205         GtkWidget *pannable;
206         
207         self  = MODEST_ACCOUNTS_WINDOW(g_object_new(MODEST_TYPE_ACCOUNTS_WINDOW, NULL));
208         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
209
210         pannable = hildon_pannable_area_new ();
211         priv->account_view  = GTK_WIDGET (modest_account_view_new (modest_runtime_get_account_mgr ()));
212
213         setup_menu (self);
214
215         gtk_container_add (GTK_CONTAINER (pannable), priv->account_view);
216         gtk_container_add (GTK_CONTAINER (self), pannable);
217
218         gtk_widget_show (priv->account_view);
219         gtk_widget_show (pannable);
220
221         connect_signals (MODEST_ACCOUNTS_WINDOW (self));
222
223         /* Load previous osso state, for instance if we are being restored from 
224          * hibernation:  */
225         modest_osso_load_state ();
226
227         /* Get device name */
228         modest_maemo_utils_get_device_name ();
229
230         app = hildon_program_get_instance ();
231         hildon_program_add_window (app, HILDON_WINDOW (self));
232         
233         /* Set window icon */
234         window_icon = modest_platform_get_icon (MODEST_APP_ICON, MODEST_ICON_SIZE_BIG);
235         if (window_icon) {
236                 gtk_window_set_icon (GTK_WINDOW (self), window_icon);
237                 g_object_unref (window_icon);
238         }
239
240         /* Listen for changes in the screen, we don't want to show a
241            led pattern when the display is on for example */
242         osso_hw_set_display_event_cb (modest_maemo_utils_get_osso_context (),
243                                       osso_display_event_cb,
244                                       self); 
245
246         /* Dont't restore settings here, 
247          * because it requires a gtk_widget_show(), 
248          * and we don't want to do that until later,
249          * so that the UI is not visible for non-menu D-Bus activation.
250          */
251
252         return MODEST_WINDOW(self);
253 }
254
255 gboolean
256 modest_accounts_window_screen_is_on (ModestAccountsWindow *self)
257 {
258         ModestAccountsWindowPrivate *priv = NULL;
259
260         g_return_val_if_fail (MODEST_IS_ACCOUNTS_WINDOW(self), FALSE);
261
262         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
263         
264         return (priv->display_state == OSSO_DISPLAY_ON) ? TRUE : FALSE;
265 }
266
267 ModestAccountView *
268 modest_accounts_window_get_account_view (ModestAccountsWindow *self)
269 {
270         ModestAccountsWindowPrivate *priv = NULL;
271
272         g_return_val_if_fail (MODEST_IS_ACCOUNTS_WINDOW(self), FALSE);
273
274         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
275         
276         return MODEST_ACCOUNT_VIEW (priv->account_view);
277 }
278
279 static void 
280 setup_menu (ModestAccountsWindow *self)
281 {
282         g_return_if_fail (MODEST_IS_ACCOUNTS_WINDOW(self));
283
284         /* Settings menu buttons */
285         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("TODO: new account"),
286                                            APP_MENU_CALLBACK (modest_ui_actions_on_new_account), 
287                                            NULL);
288         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("TODO: edit accounts"),
289                                            APP_MENU_CALLBACK (modest_ui_actions_on_accounts), 
290                                            NULL);
291         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_options"),
292                                            APP_MENU_CALLBACK (modest_ui_actions_on_settings), 
293                                            NULL);
294         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_globalsmtpservers"),
295                                            APP_MENU_CALLBACK (modest_ui_actions_on_smtp_servers),
296                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_tools_smtp_servers));
297         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_viewer_newemail"),
298                                            APP_MENU_CALLBACK (modest_ui_actions_on_new_msg),
299                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_new_msg));
300         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_sendandreceive"),
301                                            APP_MENU_CALLBACK (modest_ui_actions_on_send_receive),
302                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_send_receive_all));
303         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_outbox_cancelsend"),
304                                            APP_MENU_CALLBACK (modest_ui_actions_cancel_send),
305                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_cancel_sending_all));
306 }
307
308
309 static void
310 on_account_activated (GtkTreeView *account_view,
311                       GtkTreePath *path,
312                       GtkTreeViewColumn *column,
313                       ModestAccountsWindow *self)
314 {
315         ModestAccountsWindowPrivate *priv;
316         gchar* account_name; 
317         GtkWidget *folder_window;
318
319         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
320         
321         account_name = modest_account_view_get_path_account (MODEST_ACCOUNT_VIEW (priv->account_view), path);
322         if (!account_name)
323                 return;
324
325         folder_window = GTK_WIDGET (modest_folder_window_new (NULL));
326         modest_window_mgr_register_window (modest_runtime_get_window_mgr (), 
327                                            MODEST_WINDOW (folder_window),
328                                            MODEST_WINDOW (self));
329         modest_folder_window_set_account (MODEST_FOLDER_WINDOW (folder_window), account_name);
330         gtk_widget_show (folder_window);
331         g_free (account_name);
332
333 }
334