Drop usage of HildonCheckButton in settings dialogs.
[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                         modest_togglable_set_active (priv->auth_view,
109                                                      TRUE);
110         } else {
111 #ifdef MODEST_TOOLKIT_HILDON2
112                 modest_secureauth_picker_set_active_secureauth (
113                    MODEST_SECUREAUTH_PICKER (priv->auth_view), secure_auth);
114 #else
115                 modest_secureauth_combo_box_set_active_secureauth (
116                    MODEST_SECUREAUTH_COMBO_BOX (priv->auth_view), secure_auth);
117 #endif
118         }
119
120         MODEST_SECURITY_OPTIONS_VIEW_GET_CLASS (self)->load_settings (self, settings);
121
122         /* Free */
123         g_object_unref (server_settings);
124 }
125
126 void 
127 modest_security_options_view_save_settings (ModestSecurityOptionsView* self, 
128                                             ModestAccountSettings *settings)
129 {
130         ModestServerAccountSettings *server_settings;
131         ModestProtocolType security_proto, auth_protocol;
132         ModestSecurityOptionsViewPrivate *priv;
133         ModestProtocolRegistry *proto_registry;
134
135         priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
136         proto_registry = modest_runtime_get_protocol_registry ();
137
138         if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
139                 server_settings = modest_account_settings_get_store_settings (settings);
140         else
141                 server_settings = modest_account_settings_get_transport_settings (settings);
142
143         /* initialize */
144         security_proto = MODEST_PROTOCOLS_CONNECTION_NONE;
145         auth_protocol = MODEST_PROTOCOLS_AUTH_NONE;
146
147         /* Get data */
148 #ifdef MODEST_TOOLKIT_HILDON2
149         security_proto = modest_serversecurity_picker_get_active_serversecurity (MODEST_SERVERSECURITY_PICKER (priv->security_view));
150 #else
151         security_proto = modest_serversecurity_combo_box_get_active_serversecurity (MODEST_SERVERSECURITY_COMBO_BOX (priv->security_view));
152 #endif
153
154         if (self->type == MODEST_SECURITY_OPTIONS_INCOMING) {
155                 if (modest_togglable_get_active (priv->auth_view)) {
156                         if (!modest_protocol_registry_protocol_type_is_secure (proto_registry,
157                                                                                security_proto)) {
158                                 /* TODO */
159                                 /*              auth_protocol = check_first_supported_auth_method (self); */
160                                 auth_protocol = MODEST_PROTOCOLS_AUTH_PASSWORD;
161                         } else {
162                                 auth_protocol = MODEST_PROTOCOLS_AUTH_PASSWORD;
163                         }
164                 }
165         } else {
166 #ifdef MODEST_TOOLKIT_HILDON2
167                 auth_protocol = modest_secureauth_picker_get_active_secureauth (
168                         MODEST_SECUREAUTH_PICKER (priv->auth_view));
169 #else
170                 auth_protocol = modest_secureauth_combo_box_get_active_secureauth (
171                         MODEST_SECUREAUTH_COMBO_BOX (priv->auth_view));
172 #endif
173         }
174
175         /* Save settings */
176         modest_server_account_settings_set_security_protocol (server_settings, 
177                                                               security_proto);
178         modest_server_account_settings_set_auth_protocol (server_settings, 
179                                                           auth_protocol);
180
181         if (priv->full && self->type == MODEST_SECURITY_OPTIONS_OUTGOING) {
182                 const gchar *username, *password;
183
184                 username = gtk_entry_get_text (GTK_ENTRY (priv->user_entry));
185                 password = gtk_entry_get_text (GTK_ENTRY (priv->pwd_entry));
186
187                 modest_server_account_settings_set_username (server_settings, username);
188                 modest_server_account_settings_set_password (server_settings, password);
189         }
190
191         MODEST_SECURITY_OPTIONS_VIEW_GET_CLASS (self)->save_settings (self, settings);
192
193
194         /* Free */
195         g_object_unref (server_settings);
196 }
197
198 void 
199 modest_security_options_view_set_server_type (ModestSecurityOptionsView* self, 
200                                               ModestProtocolType server_type)
201 {
202         ModestSecurityOptionsViewPrivate *priv;
203         priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
204
205 #ifdef MODEST_TOOLKIT_HILDON2           
206         modest_serversecurity_picker_fill (MODEST_SERVERSECURITY_PICKER (priv->security_view), server_type);
207         modest_serversecurity_picker_set_active_serversecurity (MODEST_SERVERSECURITY_PICKER (priv->security_view),
208                                                                 MODEST_PROTOCOLS_CONNECTION_NONE);
209 #else
210         modest_serversecurity_combo_box_fill (MODEST_SERVERSECURITY_COMBO_BOX (priv->security_view), server_type);
211         modest_serversecurity_combo_box_set_active_serversecurity (MODEST_SERVERSECURITY_COMBO_BOX (priv->security_view),
212                                                                    MODEST_PROTOCOLS_CONNECTION_NONE);
213 #endif
214 }
215
216 static void
217 get_current_state (ModestSecurityOptionsView* self,
218                    ModestSecurityOptionsState *state)
219 {
220         ModestSecurityOptionsViewPrivate *priv;
221         ModestProtocolRegistry *proto_registry;
222
223         priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
224         proto_registry = modest_runtime_get_protocol_registry ();
225
226         /* Get security */
227 #ifdef MODEST_TOOLKIT_HILDON2
228         state->security =
229                 modest_serversecurity_picker_get_active_serversecurity (MODEST_SERVERSECURITY_PICKER (priv->security_view));
230         state->port =
231                 modest_serversecurity_picker_get_active_serversecurity_port (MODEST_SERVERSECURITY_PICKER (priv->security_view));
232 #else
233         state->security =
234                 modest_serversecurity_combo_box_get_active_serversecurity (MODEST_SERVERSECURITY_COMBO_BOX (priv->security_view));
235         state->port =
236                 modest_serversecurity_combo_box_get_active_serversecurity_port (MODEST_SERVERSECURITY_COMBO_BOX (priv->security_view));
237 #endif
238
239         /* Get auth */
240         if (self->type == MODEST_SECURITY_OPTIONS_OUTGOING) {
241 #ifdef MODEST_TOOLKIT_HILDON2
242                 state->auth = modest_secureauth_picker_get_active_secureauth (MODEST_SECUREAUTH_PICKER (priv->auth_view));
243 #else
244                 state->auth = modest_secureauth_combo_box_get_active_secureauth (MODEST_SECUREAUTH_COMBO_BOX (priv->auth_view));
245 #endif
246                 if (priv->full) {
247                 }
248         } else {
249                 if (modest_togglable_get_active (priv->auth_view))
250                         state->auth = priv->initial_state.auth;
251                 else
252                         state->auth = MODEST_PROTOCOLS_AUTH_NONE;
253         }
254 }
255
256 gboolean 
257 modest_security_options_view_changed (ModestSecurityOptionsView* self,
258                                       ModestAccountSettings *settings)
259 {
260         ModestSecurityOptionsViewPrivate *priv;
261         ModestSecurityOptionsState state = {0};
262
263         priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
264
265         get_current_state (self, &state);
266
267         if (state.security != priv->initial_state.security ||
268             state.auth != priv->initial_state.auth)
269                 return TRUE;
270
271         if (priv->full && self->type == MODEST_SECURITY_OPTIONS_OUTGOING) {
272                 const gchar *username, *password;
273
274                 username = gtk_entry_get_text (GTK_ENTRY (priv->user_entry));
275                 password = gtk_entry_get_text (GTK_ENTRY (priv->pwd_entry));
276
277                 if (!priv->initial_state.user && strcmp (username, ""))
278                         return TRUE;
279                 if (!priv->initial_state.pwd && strcmp (password, ""))
280                         return TRUE;
281
282                 if ((priv->initial_state.user && 
283                      strcmp (priv->initial_state.user, username)) ||
284                     (priv->initial_state.pwd &&
285                      strcmp (priv->initial_state.pwd, password)))
286                         return TRUE;
287         }
288
289         /* Check subclass */
290         return  MODEST_SECURITY_OPTIONS_VIEW_GET_CLASS (self)->changed (self, settings);
291 }
292
293 void 
294 modest_security_options_view_enable_changes (ModestSecurityOptionsView* self,
295                                              gboolean enable)
296 {
297         ModestSecurityOptionsViewPrivate *priv;
298
299         priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
300         gtk_widget_set_sensitive (priv->port_view, enable);
301         gtk_widget_set_sensitive (priv->security_view, enable);
302 }
303
304 gboolean 
305 modest_security_options_view_auth_check (ModestSecurityOptionsView* self)
306 {
307         ModestSecurityOptionsViewPrivate *priv;
308         ModestProtocolType security_incoming_type; 
309         gboolean auth_active, is_secure;
310         ModestProtocolRegistry *protocol_registry;
311
312         priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
313         protocol_registry = modest_runtime_get_protocol_registry ();
314
315         /* Check if the server supports secure authentication */
316 #ifdef MODEST_TOOLKIT_HILDON2
317         security_incoming_type = 
318                 modest_serversecurity_picker_get_active_serversecurity (MODEST_SERVERSECURITY_PICKER (priv->security_view));
319 #else
320         security_incoming_type = 
321                 modest_serversecurity_combo_box_get_active_serversecurity (MODEST_SERVERSECURITY_COMBO_BOX (priv->security_view));
322 #endif
323
324         auth_active = 
325                 modest_togglable_get_active (priv->auth_view);
326         is_secure = 
327                 modest_protocol_registry_protocol_type_has_tag (protocol_registry, 
328                                                                 security_incoming_type, 
329                                                                 MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS);
330
331         if (auth_active && !is_secure)
332                 return TRUE;
333         else
334                 return FALSE;
335 }
336
337 ModestProtocolType 
338 modest_security_options_view_get_connection_protocol (ModestSecurityOptionsView *self)
339 {
340         ModestSecurityOptionsViewPrivate *priv;
341
342         g_return_val_if_fail (MODEST_IS_SECURITY_OPTIONS_VIEW (self), MODEST_PROTOCOL_REGISTRY_TYPE_INVALID);
343         priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
344
345 #ifdef MODEST_TOOLKIT_HILDON2
346         return modest_serversecurity_picker_get_active_serversecurity (MODEST_SERVERSECURITY_PICKER (priv->security_view));
347 #else
348         return modest_serversecurity_combo_box_get_active_serversecurity (MODEST_SERVERSECURITY_COMBO_BOX (priv->security_view));
349 #endif
350 }
351
352 static void 
353 modest_security_options_view_init (ModestSecurityOptionsView *self) 
354 {
355         ModestSecurityOptionsViewPrivate *priv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
356
357         memset (&(priv->initial_state), 0, sizeof (ModestSecurityOptionsState));
358
359         priv->security_view = NULL;
360         priv->port_view = NULL;
361         priv->auth_view = NULL;
362         priv->user_entry = NULL;
363         priv->pwd_entry = NULL;
364         priv->full = FALSE;
365         priv->changed = FALSE;
366 }
367
368 static void 
369 modest_security_options_view_class_init (ModestSecurityOptionsViewClass *klass) 
370 {
371         GObjectClass *gobject_class = (GObjectClass*) klass;
372
373         g_type_class_add_private (gobject_class, sizeof (ModestSecurityOptionsViewPrivate));
374
375         /* Register signals */
376         signals[MISSING_MANDATORY_DATA_SIGNAL] =
377                 g_signal_new ("missing_mandatory_data",
378                               MODEST_TYPE_SECURITY_OPTIONS_VIEW,
379                               G_SIGNAL_RUN_FIRST,
380                               G_STRUCT_OFFSET(ModestSecurityOptionsViewClass, missing_mandatory_data),
381                               NULL, NULL,
382                               g_cclosure_marshal_VOID__BOOLEAN,
383                               G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
384 }
385
386 /* Type definition */
387 G_DEFINE_ABSTRACT_TYPE (ModestSecurityOptionsView, 
388                         modest_security_options_view,
389                         GTK_TYPE_VBOX);