This is a manual merge of branch drop split view intro trunk.
[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/gtkvbox.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 #else
240         state->security = 
241                 modest_serversecurity_combo_box_get_active_serversecurity (MODEST_SERVERSECURITY_COMBO_BOX (priv->security_view));
242 #endif
243
244         /* Get auth */
245         if (self->type == MODEST_SECURITY_OPTIONS_OUTGOING) {
246 #ifdef MODEST_TOOLKIT_HILDON2
247                 state->auth = modest_secureauth_picker_get_active_secureauth (MODEST_SECUREAUTH_PICKER (priv->auth_view));
248 #else
249                 state->auth = modest_secureauth_combo_box_get_active_secureauth (MODEST_SECUREAUTH_COMBO_BOX (priv->auth_view));
250 #endif
251                 if (priv->full) {
252                 }
253         } else {
254 #ifdef MODEST_TOOLKIT_HILDON2
255                 if (hildon_check_button_get_active (HILDON_CHECK_BUTTON (priv->auth_view)))
256 #else
257                 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->auth_view)))
258 #endif
259                         state->auth = priv->initial_state.auth;
260                 else
261                         state->auth = MODEST_PROTOCOLS_AUTH_NONE;
262         }
263 }
264
265 gboolean 
266 modest_security_options_view_changed (ModestSecurityOptionsView* self,
267                                       ModestAccountSettings *settings)
268 {
269         ModestSecurityOptionsViewPrivate *priv;
270         ModestSecurityOptionsState state = {0};
271
272         priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
273
274         get_current_state (self, &state);
275
276         if (state.security != priv->initial_state.security ||
277             state.auth != priv->initial_state.auth)
278                 return TRUE;
279
280         if (priv->full && self->type == MODEST_SECURITY_OPTIONS_OUTGOING) {
281                 const gchar *username, *password;
282
283                 username = gtk_entry_get_text (GTK_ENTRY (priv->user_entry));
284                 password = gtk_entry_get_text (GTK_ENTRY (priv->pwd_entry));
285
286                 if (!priv->initial_state.user && strcmp (username, ""))
287                         return TRUE;
288                 if (!priv->initial_state.pwd && strcmp (password, ""))
289                         return TRUE;
290
291                 if ((priv->initial_state.user && 
292                      strcmp (priv->initial_state.user, username)) ||
293                     (priv->initial_state.pwd &&
294                      strcmp (priv->initial_state.pwd, password)))
295                         return TRUE;
296         }
297
298         /* Check subclass */
299         return  MODEST_SECURITY_OPTIONS_VIEW_GET_CLASS (self)->changed (self, settings);
300 }
301
302 void 
303 modest_security_options_view_enable_changes (ModestSecurityOptionsView* self,
304                                              gboolean enable)
305 {
306         ModestSecurityOptionsViewPrivate *priv;
307
308         priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
309         gtk_widget_set_sensitive (priv->port_view, enable);
310         gtk_widget_set_sensitive (priv->security_view, enable);
311 }
312
313 gboolean 
314 modest_security_options_view_auth_check (ModestSecurityOptionsView* self)
315 {
316         ModestSecurityOptionsViewPrivate *priv;
317         ModestProtocolType security_incoming_type; 
318         gboolean auth_active, is_secure;
319         ModestProtocolRegistry *protocol_registry;
320
321         priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
322         protocol_registry = modest_runtime_get_protocol_registry ();
323
324         /* Check if the server supports secure authentication */
325 #ifdef MODEST_TOOLKIT_HILDON2
326         security_incoming_type = 
327                 modest_serversecurity_picker_get_active_serversecurity (MODEST_SERVERSECURITY_PICKER (priv->security_view));
328 #else
329         security_incoming_type = 
330                 modest_serversecurity_combo_box_get_active_serversecurity (MODEST_SERVERSECURITY_COMBO_BOX (priv->security_view));
331 #endif
332
333 #ifdef MODEST_TOOLKIT_HILDON2
334         auth_active = 
335                 hildon_check_button_get_active (HILDON_CHECK_BUTTON (priv->auth_view));
336 #else
337         auth_active = 
338                 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->auth_view));
339 #endif
340         is_secure = 
341                 modest_protocol_registry_protocol_type_has_tag (protocol_registry, 
342                                                                 security_incoming_type, 
343                                                                 MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS);
344
345         if (auth_active && !is_secure)
346                 return TRUE;
347         else
348                 return FALSE;
349 }
350
351 /* GList*  */
352 /* modest_security_options_view_get_supported_auth_methods (ModestSecurityOptionsView *self, */
353 /*                                                       const gchar *hostname, */
354 /*                                                       const gchar *username, */
355 /*                                                       ModestProtocolType server_type) */
356 /* { */
357 /*      GtkWindow *window; */
358 /*      GError *error = NULL; */
359 /*      GList *list_auth_methods, *retval = NULL; */
360 /*      ModestSecurityOptionsViewPrivate *priv; */
361 /*      ModestAccountSettings current_settings; */
362 /*      ModestServerAccountSettings *server_settings; */
363         
364 /*      priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self); */
365
366 /*      window = GTK_WINDOW (gtk_widget_get_ancestor (GTK_WIDGET (self), GTK_TYPE_WINDOW)); */
367
368 /*      /\* Get current settings *\/ */
369 /*      modest_security_options_view_save_settings (self, &current_settings); */
370
371 /*      if (self->type == MODEST_SECURITY_OPTIONS_INCOMING) */
372 /*              server_settings = modest_account_settings_get_store_settings (&current_settings); */
373 /*      else */
374 /*              server_settings = modest_account_settings_get_transport_settings (&current_settings); */
375
376 /*      list_auth_methods = */
377 /*              modest_utils_get_supported_secure_authentication_methods (server_type, */
378 /*                                                                        hostname, */
379 /*                                                                        modest_server_account_settings_get_port (server_settings), */
380 /*                                                                        username, */
381 /*                                                                        window, */
382 /*                                                                        &error); */
383
384 /*      if (list_auth_methods) { */
385 /*              GList *list = NULL, *method = NULL; */
386 /*              ModestProtocolRegistry *registry = modest_runtime_get_protocol_registry (); */
387
388 /*              for (method = list_auth_methods; method != NULL; method = g_list_next(method)) { */
389 /*                      ModestProtocolType auth_protocol_type =  */
390 /*                              (ModestProtocolType) (GPOINTER_TO_INT(method->data)); */
391 /*                      if (modest_protocol_registry_protocol_type_is_secure (registry,  */
392 /*                                                                            auth_protocol_type)) { */
393 /*                              list = g_list_append(list, GINT_TO_POINTER(auth_protocol_type)); */
394 /*                      } */
395 /*              } */
396 /*              g_list_free(list_auth_methods); */
397 /*              if (list) { */
398 /*                      retval = list; */
399 /*                      goto end; */
400 /*              } */
401 /*      } */
402
403 /*      if(error != NULL &&  */
404 /*         error->domain == modest_utils_get_supported_secure_authentication_error_quark() && */
405 /*         error->code != MODEST_UTILS_GET_SUPPORTED_SECURE_AUTHENTICATION_ERROR_CANCELED) { */
406 /*              modest_platform_information_banner (GTK_WIDGET(self), */
407 /*                                                  NULL, */
408 /*                                                  _("mcen_ib_unableto_discover_auth_methods")); */
409 /*      } */
410
411 /*      if(error != NULL) */
412 /*              g_error_free(error); */
413
414 /*  end: */
415 /*      /\* Frees *\/ */
416 /*      g_object_unref (server_settings); */
417
418 /*      return retval; */
419 /* } */
420
421 static void 
422 modest_security_options_view_init (ModestSecurityOptionsView *self) 
423 {
424         ModestSecurityOptionsViewPrivate *priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
425
426         memset (&(priv->initial_state), 0, sizeof (ModestSecurityOptionsState));
427
428         priv->security_view = NULL;
429         priv->port_view = NULL;
430         priv->auth_view = NULL;
431         priv->user_entry = NULL;
432         priv->pwd_entry = NULL;
433         priv->full = FALSE;
434         priv->changed = FALSE;
435 }
436
437 static void 
438 modest_security_options_view_class_init (ModestSecurityOptionsViewClass *klass) 
439 {
440         GObjectClass *gobject_class = (GObjectClass*) klass;
441
442         g_type_class_add_private (gobject_class, sizeof (ModestSecurityOptionsViewPrivate));
443
444         /* Register signals */
445         signals[MISSING_MANDATORY_DATA_SIGNAL] =
446                 g_signal_new ("missing_mandatory_data",
447                               MODEST_TYPE_SECURITY_OPTIONS_VIEW,
448                               G_SIGNAL_RUN_FIRST,
449                               G_STRUCT_OFFSET(ModestSecurityOptionsViewClass, missing_mandatory_data),
450                               NULL, NULL,
451                               g_cclosure_marshal_VOID__BOOLEAN,
452                               G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
453 }
454
455 /* Type definition */
456 G_DEFINE_ABSTRACT_TYPE (ModestSecurityOptionsView, 
457                         modest_security_options_view,
458                         GTK_TYPE_VBOX);