a9f9e59f1f8176d0f6475b0fc299544017fecda0
[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 on_progress_list_changed (ModestWindowMgr *mgr,
63                                       ModestAccountsWindow *self);
64 static void setup_menu (ModestAccountsWindow *self);
65 static gboolean _modest_accounts_window_map_event (GtkWidget *widget,
66                                                    GdkEvent *event,
67                                                    gpointer userdata);
68 static void update_progress_hint (ModestAccountsWindow *self);
69
70
71 typedef struct _ModestAccountsWindowPrivate ModestAccountsWindowPrivate;
72 struct _ModestAccountsWindowPrivate {
73
74         GtkWidget *account_view;
75
76         /* signals */
77         GSList *sighandlers;
78
79         gboolean progress_hint;
80 };
81 #define MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
82                                                                             MODEST_TYPE_ACCOUNTS_WINDOW, \
83                                                                             ModestAccountsWindowPrivate))
84
85 /* globals */
86 static GtkWindowClass *parent_class = NULL;
87
88 /************************************************************************/
89
90 GType
91 modest_accounts_window_get_type (void)
92 {
93         static GType my_type = 0;
94         if (!my_type) {
95                 static const GTypeInfo my_info = {
96                         sizeof(ModestAccountsWindowClass),
97                         NULL,           /* base init */
98                         NULL,           /* base finalize */
99                         (GClassInitFunc) modest_accounts_window_class_init,
100                         NULL,           /* class finalize */
101                         NULL,           /* class data */
102                         sizeof(ModestAccountsWindow),
103                         1,              /* n_preallocs */
104                         (GInstanceInitFunc) modest_accounts_window_instance_init,
105                         NULL
106                 };
107                 my_type = g_type_register_static (MODEST_TYPE_HILDON2_WINDOW,
108                                                   "ModestAccountsWindow",
109                                                   &my_info, 0);
110         }
111         return my_type;
112 }
113
114 static void
115 modest_accounts_window_class_init (ModestAccountsWindowClass *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_accounts_window_finalize;
123
124         g_type_class_add_private (gobject_class, sizeof(ModestAccountsWindowPrivate));
125         
126         modest_window_class->disconnect_signals_func = modest_accounts_window_disconnect_signals;
127 }
128
129 static void
130 modest_accounts_window_instance_init (ModestAccountsWindow *obj)
131 {
132         ModestAccountsWindowPrivate *priv;
133
134         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(obj);
135
136         priv->sighandlers = NULL;
137         
138         priv->account_view = NULL;
139         priv->progress_hint = FALSE;
140         
141         modest_window_mgr_register_help_id (modest_runtime_get_window_mgr(),
142                                             GTK_WINDOW(obj),
143                                             "applications_email_accountsview");
144 }
145
146 static void
147 modest_accounts_window_finalize (GObject *obj)
148 {
149         ModestAccountsWindowPrivate *priv;
150
151         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(obj);
152
153         /* Sanity check: shouldn't be needed, the window mgr should
154            call this function before */
155         modest_accounts_window_disconnect_signals (MODEST_WINDOW (obj));        
156
157         G_OBJECT_CLASS(parent_class)->finalize (obj);
158 }
159
160 static void
161 modest_accounts_window_disconnect_signals (ModestWindow *self)
162 {       
163         ModestAccountsWindowPrivate *priv;      
164         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
165
166         modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
167         priv->sighandlers = NULL;       
168 }
169
170 static void
171 connect_signals (ModestAccountsWindow *self)
172 {
173         ModestAccountsWindowPrivate *priv;
174
175         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
176
177         /* accounts view */
178         priv->sighandlers = 
179                 modest_signal_mgr_connect (priv->sighandlers,
180                                            G_OBJECT (priv->account_view), "row-activated", 
181                                            G_CALLBACK (on_account_activated), self);
182
183         priv->sighandlers = 
184                 modest_signal_mgr_connect (priv->sighandlers,
185                                            G_OBJECT (modest_runtime_get_window_mgr ()),
186                                            "progress-list-changed",
187                                            G_CALLBACK (on_progress_list_changed), self);
188
189         /* we don't register this in sighandlers, as it should be run
190          * after disconnecting all signals, in destroy stage */
191 }
192
193 ModestWindow *
194 modest_accounts_window_new (void)
195 {
196         ModestAccountsWindow *self = NULL;
197         ModestAccountsWindowPrivate *priv = NULL;
198         HildonProgram *app;
199         GdkPixbuf *window_icon;
200         GtkWidget *pannable;
201
202         self  = MODEST_ACCOUNTS_WINDOW(g_object_new(MODEST_TYPE_ACCOUNTS_WINDOW, NULL));
203         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE(self);
204
205         pannable = hildon_pannable_area_new ();
206         priv->account_view  = GTK_WIDGET (modest_account_view_new (modest_runtime_get_account_mgr ()));
207
208         setup_menu (self);
209
210         gtk_container_add (GTK_CONTAINER (pannable), priv->account_view);
211         gtk_container_add (GTK_CONTAINER (self), pannable);
212
213         gtk_widget_show (priv->account_view);
214         gtk_widget_show (pannable);
215
216         connect_signals (MODEST_ACCOUNTS_WINDOW (self));
217
218         /* Load previous osso state, for instance if we are being restored from 
219          * hibernation:  */
220         modest_osso_load_state ();
221
222         /* Get device name */
223         modest_maemo_utils_get_device_name ();
224
225         app = hildon_program_get_instance ();
226         hildon_program_add_window (app, HILDON_WINDOW (self));
227         
228         /* Set window icon */
229         window_icon = modest_platform_get_icon (MODEST_APP_ICON, MODEST_ICON_SIZE_BIG);
230         if (window_icon) {
231                 gtk_window_set_icon (GTK_WINDOW (self), window_icon);
232                 g_object_unref (window_icon);
233         }
234
235         /* Dont't restore settings here, 
236          * because it requires a gtk_widget_show(), 
237          * and we don't want to do that until later,
238          * so that the UI is not visible for non-menu D-Bus activation.
239          */
240
241         g_signal_connect (G_OBJECT (self), "map-event",
242                           G_CALLBACK (_modest_accounts_window_map_event),
243                           G_OBJECT (self));
244         update_progress_hint (self);
245
246
247         return MODEST_WINDOW(self);
248 }
249
250 ModestAccountView *
251 modest_accounts_window_get_account_view (ModestAccountsWindow *self)
252 {
253         ModestAccountsWindowPrivate *priv = NULL;
254
255         g_return_val_if_fail (MODEST_IS_ACCOUNTS_WINDOW(self), FALSE);
256
257         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
258         
259         return MODEST_ACCOUNT_VIEW (priv->account_view);
260 }
261
262 static void 
263 setup_menu (ModestAccountsWindow *self)
264 {
265         g_return_if_fail (MODEST_IS_ACCOUNTS_WINDOW(self));
266
267         /* Settings menu buttons */
268         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_new_account"), NULL, 
269                                            APP_MENU_CALLBACK (modest_ui_actions_on_new_account), 
270                                            NULL);
271         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_edit_accounts"), NULL,
272                                            APP_MENU_CALLBACK (modest_ui_actions_on_accounts), 
273                                            NULL);
274         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_options"), NULL,
275                                            APP_MENU_CALLBACK (modest_ui_actions_on_settings), 
276                                            NULL);
277         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_globalsmtpservers"), NULL,
278                                            APP_MENU_CALLBACK (modest_ui_actions_on_smtp_servers),
279                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_tools_smtp_servers));
280         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_new_message"), "<Control>n",
281                                            APP_MENU_CALLBACK (modest_ui_actions_on_new_msg),
282                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_new_msg));
283         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_inbox_sendandreceive"), NULL,
284                                            APP_MENU_CALLBACK (modest_ui_actions_on_send_receive),
285                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_send_receive_all));
286         modest_hildon2_window_add_to_menu (MODEST_HILDON2_WINDOW (self), _("mcen_me_outbox_cancelsend"), NULL,
287                                            APP_MENU_CALLBACK (modest_ui_actions_cancel_send),
288                                            MODEST_DIMMING_CALLBACK (modest_ui_dimming_rules_on_cancel_sending_all));
289 }
290
291
292 static void
293 on_account_activated (GtkTreeView *account_view,
294                       GtkTreePath *path,
295                       GtkTreeViewColumn *column,
296                       ModestAccountsWindow *self)
297 {
298         ModestAccountsWindowPrivate *priv;
299         gchar* account_name; 
300         GtkWidget *folder_window;
301
302         priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
303
304         account_name = modest_account_view_get_path_account (MODEST_ACCOUNT_VIEW (priv->account_view), path);
305         if (!account_name)
306                 return;
307
308         folder_window = GTK_WIDGET (modest_folder_window_new (NULL));
309         modest_window_mgr_register_window (modest_runtime_get_window_mgr (), 
310                                            MODEST_WINDOW (folder_window),
311                                            MODEST_WINDOW (self));
312         modest_folder_window_set_account (MODEST_FOLDER_WINDOW (folder_window), account_name);
313         gtk_widget_show (folder_window);
314         g_free (account_name);
315
316 }
317
318 static gboolean 
319 _modest_accounts_window_map_event (GtkWidget *widget,
320                                    GdkEvent *event,
321                                    gpointer userdata)
322 {
323         ModestAccountsWindow *self = (ModestAccountsWindow *) userdata;
324         ModestAccountsWindowPrivate *priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
325
326         if (priv->progress_hint) {
327                 hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), TRUE);
328         }
329
330         return FALSE;
331 }
332
333 static void
334 update_progress_hint (ModestAccountsWindow *self)
335 {
336         ModestAccountsWindowPrivate *priv = MODEST_ACCOUNTS_WINDOW_GET_PRIVATE (self);
337
338         priv->progress_hint = modest_window_mgr_has_progress_operation (modest_runtime_get_window_mgr ());
339         
340         if (GTK_WIDGET_VISIBLE (self)) {
341                 hildon_gtk_window_set_progress_indicator (GTK_WINDOW (self), priv->progress_hint?1:0);
342         }
343 }
344
345 static void
346 on_progress_list_changed (ModestWindowMgr *mgr,
347                           ModestAccountsWindow *self)
348 {
349         update_progress_hint (self);
350 }