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