Use GTK+ single includes
[modest] / src / widgets / modest-security-options-view.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 <string.h>
31 #include <gtk/gtk.h>
32 #include "modest-utils.h"
33 #include "modest-runtime.h"
34 #include "modest-platform.h"
35 #include "modest-security-options-view.h"
36 #include "modest-security-options-view-priv.h"
37 #ifdef MODEST_TOOLKIT_HILDON2
38 #include "modest-serversecurity-picker.h"
39 #include "modest-secureauth-picker.h"
40 #include <modest-hildon-includes.h>
41 #else
42 #include "widgets/modest-serversecurity-combo-box.h"
43 #include "widgets/modest-secureauth-combo-box.h"
44 #endif
45
46 /* list my signals */
47 enum {
48         MISSING_MANDATORY_DATA_SIGNAL,
49         LAST_SIGNAL
50 };
51
52 static guint signals[LAST_SIGNAL] = {0};
53
54 void 
55 modest_security_options_view_load_settings (ModestSecurityOptionsView* self, 
56                                             ModestAccountSettings *settings)
57 {
58         ModestSecurityOptionsViewPrivate *priv;
59         ModestServerAccountSettings *server_settings;
60         ModestProtocolType server_proto, secure_protocol, secure_auth;
61
62         priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
63
64         /* Save initial settings */
65         if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
66                 server_settings = modest_account_settings_get_store_settings (settings);
67         else
68                 server_settings = modest_account_settings_get_transport_settings (settings);
69
70         server_proto = modest_server_account_settings_get_protocol (server_settings);
71         secure_protocol = modest_server_account_settings_get_security_protocol (server_settings);
72         secure_auth = modest_server_account_settings_get_auth_protocol (server_settings);
73
74         priv->initial_state.security = secure_protocol;
75         priv->initial_state.auth = secure_auth;
76         priv->initial_state.port = modest_server_account_settings_get_port (server_settings);
77
78         /* Update UI */
79         modest_security_options_view_set_server_type (self, server_proto);
80 #ifdef MODEST_TOOLKIT_HILDON2
81         modest_serversecurity_picker_set_active_serversecurity (MODEST_SERVERSECURITY_PICKER (priv->security_view), secure_protocol);
82 #else
83         modest_serversecurity_combo_box_set_active_serversecurity (MODEST_SERVERSECURITY_COMBO_BOX (priv->security_view), secure_protocol);
84 #endif
85
86 /*              update_incoming_server_title (dialog, dialog->incoming_protocol); */
87
88         /* Username and password */
89         if (priv->full && self->type == MODEST_SECURITY_OPTIONS_OUTGOING) {
90                 priv->initial_state.user = 
91                         modest_server_account_settings_get_username (server_settings);
92                 priv->initial_state.pwd = 
93                         modest_server_account_settings_get_password (server_settings);
94
95                 if (priv->initial_state.user)
96                         gtk_entry_set_text(GTK_ENTRY (priv->user_entry), 
97                                            priv->initial_state.user);
98                 if (priv->initial_state.pwd)
99                         gtk_entry_set_text(GTK_ENTRY (priv->pwd_entry), 
100                                            priv->initial_state.pwd);
101         }
102
103         /* Set auth */
104         if (self->type == MODEST_SECURITY_OPTIONS_INCOMING) {
105                 /* Active the authentication checkbox */
106                 if (modest_protocol_registry_protocol_type_is_secure (modest_runtime_get_protocol_registry (), 
107                                                                       secure_auth))
108 #ifdef MODEST_TOOLKIT_HILDON2
109                         hildon_check_button_set_active (HILDON_CHECK_BUTTON (priv->auth_view),
110                                                         TRUE);
111 #else
112                         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->auth_view),
113                                                       TRUE);
114 #endif
115         } else {
116 #ifdef MODEST_TOOLKIT_HILDON2
117                 modest_secureauth_picker_set_active_secureauth (
118                    MODEST_SECUREAUTH_PICKER (priv->auth_view), secure_auth);
119 #else
120                 modest_secureauth_combo_box_set_active_secureauth (
121                    MODEST_SECUREAUTH_COMBO_BOX (priv->auth_view), secure_auth);
122 #endif
123         }
124
125         MODEST_SECURITY_OPTIONS_VIEW_GET_CLASS (self)->load_settings (self, settings);
126
127         /* Free */
128         g_object_unref (server_settings);
129 }
130
131 void 
132 modest_security_options_view_save_settings (ModestSecurityOptionsView* self, 
133                                             ModestAccountSettings *settings)
134 {
135         ModestServerAccountSettings *server_settings;
136         ModestProtocolType security_proto, auth_protocol;
137         ModestSecurityOptionsViewPrivate *priv;
138         ModestProtocolRegistry *proto_registry;
139
140         priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
141         proto_registry = modest_runtime_get_protocol_registry ();
142
143         if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
144                 server_settings = modest_account_settings_get_store_settings (settings);
145         else
146                 server_settings = modest_account_settings_get_transport_settings (settings);
147
148         /* initialize */
149         security_proto = MODEST_PROTOCOLS_CONNECTION_NONE;
150         auth_protocol = MODEST_PROTOCOLS_AUTH_NONE;
151
152         /* Get data */
153 #ifdef MODEST_TOOLKIT_HILDON2
154         security_proto = modest_serversecurity_picker_get_active_serversecurity (MODEST_SERVERSECURITY_PICKER (priv->security_view));
155 #else
156         security_proto = modest_serversecurity_combo_box_get_active_serversecurity (MODEST_SERVERSECURITY_COMBO_BOX (priv->security_view));
157 #endif
158
159         if (self->type == MODEST_SECURITY_OPTIONS_INCOMING) {
160 #ifdef MODEST_TOOLKIT_HILDON2
161                 if (hildon_check_button_get_active (HILDON_CHECK_BUTTON (priv->auth_view))) {
162 #else
163                 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->auth_view))) {
164 #endif
165                         if (!modest_protocol_registry_protocol_type_is_secure (proto_registry,
166                                                                                security_proto)) {
167                                 /* TODO */
168                                 /*              auth_protocol = check_first_supported_auth_method (self); */
169                                 auth_protocol = MODEST_PROTOCOLS_AUTH_PASSWORD;
170                         } else {
171                                 auth_protocol = MODEST_PROTOCOLS_AUTH_PASSWORD;
172                         }
173                 }
174         } else {
175 #ifdef MODEST_TOOLKIT_HILDON2
176                 auth_protocol = modest_secureauth_picker_get_active_secureauth (
177                         MODEST_SECUREAUTH_PICKER (priv->auth_view));
178 #else
179                 auth_protocol = modest_secureauth_combo_box_get_active_secureauth (
180                         MODEST_SECUREAUTH_COMBO_BOX (priv->auth_view));
181 #endif
182         }
183
184         /* Save settings */
185         modest_server_account_settings_set_security_protocol (server_settings, 
186                                                               security_proto);
187         modest_server_account_settings_set_auth_protocol (server_settings, 
188                                                           auth_protocol);
189
190         if (priv->full && self->type == MODEST_SECURITY_OPTIONS_OUTGOING) {
191                 const gchar *username, *password;
192
193                 username = gtk_entry_get_text (GTK_ENTRY (priv->user_entry));
194                 password = gtk_entry_get_text (GTK_ENTRY (priv->pwd_entry));
195
196                 modest_server_account_settings_set_username (server_settings, username);
197                 modest_server_account_settings_set_password (server_settings, password);
198         }
199
200         MODEST_SECURITY_OPTIONS_VIEW_GET_CLASS (self)->save_settings (self, settings);
201
202
203         /* Free */
204         g_object_unref (server_settings);
205 }
206
207 void 
208 modest_security_options_view_set_server_type (ModestSecurityOptionsView* self, 
209                                               ModestProtocolType server_type)
210 {
211         ModestSecurityOptionsViewPrivate *priv;
212         priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
213
214 #ifdef MODEST_TOOLKIT_HILDON2           
215         modest_serversecurity_picker_fill (MODEST_SERVERSECURITY_PICKER (priv->security_view), server_type);
216         modest_serversecurity_picker_set_active_serversecurity (MODEST_SERVERSECURITY_PICKER (priv->security_view),
217                                                                 MODEST_PROTOCOLS_CONNECTION_NONE);
218 #else
219         modest_serversecurity_combo_box_fill (MODEST_SERVERSECURITY_COMBO_BOX (priv->security_view), server_type);
220         modest_serversecurity_combo_box_set_active_serversecurity (MODEST_SERVERSECURITY_COMBO_BOX (priv->security_view),
221                                                                    MODEST_PROTOCOLS_CONNECTION_NONE);
222 #endif
223 }
224
225 static void
226 get_current_state (ModestSecurityOptionsView* self,
227                    ModestSecurityOptionsState *state)
228 {
229         ModestSecurityOptionsViewPrivate *priv;
230         ModestProtocolRegistry *proto_registry;
231
232         priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
233         proto_registry = modest_runtime_get_protocol_registry ();
234
235         /* Get security */
236 #ifdef MODEST_TOOLKIT_HILDON2
237         state->security =
238                 modest_serversecurity_picker_get_active_serversecurity (MODEST_SERVERSECURITY_PICKER (priv->security_view));
239         state->port =
240                 modest_serversecurity_picker_get_active_serversecurity_port (MODEST_SERVERSECURITY_PICKER (priv->security_view));
241 #else
242         state->security =
243                 modest_serversecurity_combo_box_get_active_serversecurity (MODEST_SERVERSECURITY_COMBO_BOX (priv->security_view));
244         state->port =
245                 modest_serversecurity_combo_box_get_active_serversecurity_port (MODEST_SERVERSECURITY_COMBO_BOX (priv->security_view));
246 #endif
247
248         /* Get auth */
249         if (self->type == MODEST_SECURITY_OPTIONS_OUTGOING) {
250 #ifdef MODEST_TOOLKIT_HILDON2
251                 state->auth = modest_secureauth_picker_get_active_secureauth (MODEST_SECUREAUTH_PICKER (priv->auth_view));
252 #else
253                 state->auth = modest_secureauth_combo_box_get_active_secureauth (MODEST_SECUREAUTH_COMBO_BOX (priv->auth_view));
254 #endif
255                 if (priv->full) {
256                 }
257         } else {
258 #ifdef MODEST_TOOLKIT_HILDON2
259                 if (hildon_check_button_get_active (HILDON_CHECK_BUTTON (priv->auth_view)))
260 #else
261                 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->auth_view)))
262 #endif
263                         state->auth = priv->initial_state.auth;
264                 else
265                         state->auth = MODEST_PROTOCOLS_AUTH_NONE;
266         }
267 }
268
269 gboolean 
270 modest_security_options_view_changed (ModestSecurityOptionsView* self,
271                                       ModestAccountSettings *settings)
272 {
273         ModestSecurityOptionsViewPrivate *priv;
274         ModestSecurityOptionsState state = {0};
275
276         priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
277
278         get_current_state (self, &state);
279
280         if (state.security != priv->initial_state.security ||
281             state.auth != priv->initial_state.auth)
282                 return TRUE;
283
284         if (priv->full && self->type == MODEST_SECURITY_OPTIONS_OUTGOING) {
285                 const gchar *username, *password;
286
287                 username = gtk_entry_get_text (GTK_ENTRY (priv->user_entry));
288                 password = gtk_entry_get_text (GTK_ENTRY (priv->pwd_entry));
289
290                 if (!priv->initial_state.user && strcmp (username, ""))
291                         return TRUE;
292                 if (!priv->initial_state.pwd && strcmp (password, ""))
293                         return TRUE;
294
295                 if ((priv->initial_state.user && 
296                      strcmp (priv->initial_state.user, username)) ||
297                     (priv->initial_state.pwd &&
298                      strcmp (priv->initial_state.pwd, password)))
299                         return TRUE;
300         }
301
302         /* Check subclass */
303         return  MODEST_SECURITY_OPTIONS_VIEW_GET_CLASS (self)->changed (self, settings);
304 }
305
306 void 
307 modest_security_options_view_enable_changes (ModestSecurityOptionsView* self,
308                                              gboolean enable)
309 {
310         ModestSecurityOptionsViewPrivate *priv;
311
312         priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
313         gtk_widget_set_sensitive (priv->port_view, enable);
314         gtk_widget_set_sensitive (priv->security_view, enable);
315 }
316
317 gboolean 
318 modest_security_options_view_auth_check (ModestSecurityOptionsView* self)
319 {
320         ModestSecurityOptionsViewPrivate *priv;
321         ModestProtocolType security_incoming_type; 
322         gboolean auth_active, is_secure;
323         ModestProtocolRegistry *protocol_registry;
324
325         priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
326         protocol_registry = modest_runtime_get_protocol_registry ();
327
328         /* Check if the server supports secure authentication */
329 #ifdef MODEST_TOOLKIT_HILDON2
330         security_incoming_type = 
331                 modest_serversecurity_picker_get_active_serversecurity (MODEST_SERVERSECURITY_PICKER (priv->security_view));
332 #else
333         security_incoming_type = 
334                 modest_serversecurity_combo_box_get_active_serversecurity (MODEST_SERVERSECURITY_COMBO_BOX (priv->security_view));
335 #endif
336
337 #ifdef MODEST_TOOLKIT_HILDON2
338         auth_active = 
339                 hildon_check_button_get_active (HILDON_CHECK_BUTTON (priv->auth_view));
340 #else
341         auth_active = 
342                 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->auth_view));
343 #endif
344         is_secure = 
345                 modest_protocol_registry_protocol_type_has_tag (protocol_registry, 
346                                                                 security_incoming_type, 
347                                                                 MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS);
348
349         if (auth_active && !is_secure)
350                 return TRUE;
351         else
352                 return FALSE;
353 }
354
355 ModestProtocolType 
356 modest_security_options_view_get_connection_protocol (ModestSecurityOptionsView *self)
357 {
358         ModestSecurityOptionsViewPrivate *priv;
359
360         g_return_val_if_fail (MODEST_IS_SECURITY_OPTIONS_VIEW (self), MODEST_PROTOCOL_REGISTRY_TYPE_INVALID);
361         priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
362
363 #ifdef MODEST_TOOLKIT_HILDON2
364         return modest_serversecurity_picker_get_active_serversecurity (MODEST_SERVERSECURITY_PICKER (priv->security_view));
365 #else
366         return modest_serversecurity_combo_box_get_active_serversecurity (MODEST_SERVERSECURITY_COMBO_BOX (priv->security_view));
367 #endif
368 }
369
370 static void 
371 modest_security_options_view_init (ModestSecurityOptionsView *self) 
372 {
373         ModestSecurityOptionsViewPrivate *priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
374
375         memset (&(priv->initial_state), 0, sizeof (ModestSecurityOptionsState));
376
377         priv->security_view = NULL;
378         priv->port_view = NULL;
379         priv->auth_view = NULL;
380         priv->user_entry = NULL;
381         priv->pwd_entry = NULL;
382         priv->full = FALSE;
383         priv->changed = FALSE;
384 }
385
386 static void 
387 modest_security_options_view_class_init (ModestSecurityOptionsViewClass *klass) 
388 {
389         GObjectClass *gobject_class = (GObjectClass*) klass;
390
391         g_type_class_add_private (gobject_class, sizeof (ModestSecurityOptionsViewPrivate));
392
393         /* Register signals */
394         signals[MISSING_MANDATORY_DATA_SIGNAL] =
395                 g_signal_new ("missing_mandatory_data",
396                               MODEST_TYPE_SECURITY_OPTIONS_VIEW,
397                               G_SIGNAL_RUN_FIRST,
398                               G_STRUCT_OFFSET(ModestSecurityOptionsViewClass, missing_mandatory_data),
399                               NULL, NULL,
400                               g_cclosure_marshal_VOID__BOOLEAN,
401                               G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
402 }
403
404 /* Type definition */
405 G_DEFINE_ABSTRACT_TYPE (ModestSecurityOptionsView, 
406                         modest_security_options_view,
407                         GTK_TYPE_VBOX);