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