Add support in security options view to check validity status of number
[modest] / src / hildon2 / modest-maemo-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 "modest-runtime.h"
31 #include "modest-security-options-view-priv.h"
32 #include "modest-maemo-security-options-view.h"
33 #include "modest-text-utils.h"
34 #include "modest-platform.h"
35 #include "modest-account-protocol.h"
36 #include "widgets/modest-ui-constants.h"
37 #include "widgets/modest-validating-entry.h"
38 #include "modest-serversecurity-picker.h"
39 #include "modest-secureauth-picker.h"
40 #include "modest-maemo-utils.h"
41 #include <modest-number-editor.h>
42 #include <hildon/hildon-check-button.h>
43 #include "modest-hildon-includes.h"
44
45 #define PORT_MIN 1
46 #define PORT_MAX 65535
47
48 static void modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj);
49 static void modest_maemo_security_options_view_finalize (GObject *obj);
50 static void modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass);
51
52 G_DEFINE_TYPE (ModestMaemoSecurityOptionsView, 
53                modest_maemo_security_options_view, 
54                MODEST_TYPE_SECURITY_OPTIONS_VIEW);
55
56 static void on_entry_changed (GtkEditable *editable, gpointer user_data);
57
58 /* Tracks changes in the incoming security picker */
59 static void
60 on_security_changed (GtkWidget *widget, 
61                      ModestMaemoSecurityOptionsView *self)
62 {
63         ModestSecurityOptionsViewPrivate* ppriv;
64         ModestServersecurityPicker *picker;
65         ModestProtocolType proto_type;
66         ModestProtocolRegistry *proto_registry;
67         gboolean is_secure;
68
69         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
70
71         proto_registry = modest_runtime_get_protocol_registry ();
72         picker = MODEST_SERVERSECURITY_PICKER (ppriv->security_view);
73         proto_type = modest_serversecurity_picker_get_active_serversecurity (picker);
74
75         is_secure = modest_protocol_registry_protocol_type_has_tag (proto_registry, proto_type, 
76                                                                     MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS);
77
78         if (MODEST_SECURITY_OPTIONS_VIEW (self)->type == MODEST_SECURITY_OPTIONS_INCOMING) {
79                 /* Activate and dim checkbutton if it's secure */
80                 hildon_check_button_set_active (HILDON_CHECK_BUTTON (ppriv->auth_view), 
81                                                 is_secure);
82                 gtk_widget_set_sensitive (ppriv->auth_view, !is_secure);
83         } else {
84
85         }
86
87         if (ppriv->full) {
88                 gint port_number = 
89                         modest_serversecurity_picker_get_active_serversecurity_port (MODEST_SERVERSECURITY_PICKER (ppriv->security_view));
90                 
91                 if(port_number) {
92                         modest_number_editor_set_value (MODEST_NUMBER_EDITOR (ppriv->port_view), 
93                                                         port_number);
94                 }
95         }
96 }
97
98 static void
99 on_auth_changed (GtkWidget *widget, 
100                  ModestMaemoSecurityOptionsView *self)
101 {
102         ModestSecurityOptionsViewPrivate* ppriv;
103         ModestSecureauthPicker *picker;
104         ModestProtocolRegistry *protocol_registry;
105         ModestProtocolType auth_proto;
106         gboolean secureauth_used;
107         GtkWidget *user_caption, *pwd_caption;
108
109         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
110         protocol_registry = modest_runtime_get_protocol_registry ();
111         picker = MODEST_SECUREAUTH_PICKER (ppriv->auth_view);
112
113         auth_proto = modest_secureauth_picker_get_active_secureauth (picker);
114         secureauth_used = modest_protocol_registry_protocol_type_is_secure (protocol_registry, 
115                                                                             auth_proto);
116
117         /* Get captions, well dimm the whole widget */
118         user_caption = gtk_widget_get_parent (ppriv->user_entry);
119         pwd_caption = gtk_widget_get_parent (ppriv->pwd_entry);
120         
121         /* Enable / disable */
122         gtk_widget_set_sensitive (user_caption, secureauth_used);
123         gtk_widget_set_sensitive (pwd_caption, secureauth_used);
124
125         /* Check if mandatory data is missing */
126         on_entry_changed (GTK_EDITABLE (ppriv->user_entry), (gpointer) self);
127 }
128
129 static void
130 create_incoming_security (ModestSecurityOptionsView* self,
131                           GtkSizeGroup *title_size_group,
132                           GtkSizeGroup *value_size_group)
133 {
134         ModestSecurityOptionsViewPrivate *ppriv;
135         GtkWidget *entry_caption = NULL;
136
137         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
138
139         /* Create widgets for incoming security */
140         ppriv->security_view = GTK_WIDGET (modest_serversecurity_picker_new (MODEST_EDITABLE_SIZE,
141                                                                              HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
142         modest_serversecurity_picker_fill (MODEST_SERVERSECURITY_PICKER (ppriv->security_view), 
143                                            modest_protocol_registry_get_pop_type_id ());
144         modest_maemo_utils_set_hbutton_layout (title_size_group, 
145                                                value_size_group,
146                                                _("mcen_li_emailsetup_secure_connection"), 
147                                                ppriv->security_view);
148
149         if (ppriv->full) {              
150                 ppriv->port_view = GTK_WIDGET (modest_number_editor_new (PORT_MIN, PORT_MAX));
151                 entry_caption = modest_maemo_utils_create_captioned (title_size_group,
152                                                                      value_size_group,
153                                                                      _("mcen_fi_emailsetup_port"), 
154                                                                      FALSE,
155                                                                      ppriv->port_view);
156         }
157
158         ppriv->auth_view = hildon_check_button_new (MODEST_EDITABLE_SIZE);
159         gtk_button_set_label (GTK_BUTTON (ppriv->auth_view), _("mcen_li_emailsetup_secure_authentication"));
160         gtk_button_set_alignment (GTK_BUTTON (ppriv->auth_view), 0.0, 0.5);
161
162         /* Track changes in UI */       
163         g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
164                           G_CALLBACK (on_security_changed), self);
165
166         /* Pack into container */
167         gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view,
168                             FALSE, FALSE, 0);
169         if (ppriv->full)
170                 gtk_box_pack_start (GTK_BOX (self), entry_caption, 
171                                     FALSE, FALSE, MODEST_MARGIN_HALF);
172         gtk_box_pack_start (GTK_BOX (self), ppriv->security_view,
173                             FALSE, FALSE, 0);
174
175         /* Show widgets */
176         if (ppriv->full) {
177                 gtk_widget_show (ppriv->port_view);
178                 gtk_widget_show (entry_caption);
179         }
180         gtk_widget_show (ppriv->security_view);
181         gtk_widget_show (ppriv->auth_view);
182 }
183
184 static void
185 on_entry_max (ModestValidatingEntry *self, 
186               gpointer user_data)
187 {
188         modest_platform_information_banner (GTK_WIDGET (self), NULL, 
189                                             _CS("ckdg_ib_maximum_characters_reached"));
190 }
191
192 /*
193  * TODO: call this whenever the auth picker changes. If we set it
194  * explicitely at the beggining to a value then there is no need to
195  * call this handler directly at the beginning
196  */
197 static void
198 on_entry_changed (GtkEditable *editable, 
199                   gpointer user_data)
200 {
201         ModestSecurityOptionsView* self;
202         ModestSecurityOptionsViewPrivate *ppriv;
203         ModestProtocolType auth_proto;
204         ModestSecureauthPicker *picker;
205         gboolean is_secure, missing;
206         ModestProtocolRegistry *protocol_registry;
207
208         self = MODEST_SECURITY_OPTIONS_VIEW (user_data);
209         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
210         protocol_registry = modest_runtime_get_protocol_registry ();
211
212         /* Outgoing username is mandatory if outgoing auth is secure */
213         picker = MODEST_SECUREAUTH_PICKER (ppriv->auth_view);
214         auth_proto = modest_secureauth_picker_get_active_secureauth (picker);
215         is_secure = modest_protocol_registry_protocol_type_is_secure (protocol_registry, 
216                                                                       auth_proto);
217
218         if (is_secure && 
219             !g_ascii_strcasecmp (hildon_entry_get_text (HILDON_ENTRY (ppriv->user_entry)), "")) {
220                 missing = TRUE;
221         } else {
222                 missing = FALSE;
223         }
224
225         if (!missing && ppriv->full && !modest_number_editor_is_valid (MODEST_NUMBER_EDITOR (ppriv->port_view)))
226                 missing = TRUE;
227         
228         /* Emit a signal to notify if mandatory data is missing */
229         g_signal_emit_by_name (G_OBJECT (self), "missing_mandatory_data", 
230                                missing, NULL);
231 }
232
233 void
234 on_valid_changed (ModestNumberEditor *editor,
235                   gboolean valid,
236                   ModestSecurityOptionsView *self)
237 {
238         on_entry_changed (NULL, (gpointer) self);
239 }
240
241 static void
242 create_outgoing_security (ModestSecurityOptionsView* self,
243                           GtkSizeGroup *title_size_group,
244                           GtkSizeGroup *value_size_group)
245 {
246         ModestSecurityOptionsViewPrivate *ppriv;
247         GtkWidget *user_caption = NULL;
248         GtkWidget *pwd_caption = NULL, *port_caption = NULL;
249
250         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
251         
252         /* The secure connection widgets */
253         ppriv->security_view = GTK_WIDGET (modest_serversecurity_picker_new (MODEST_EDITABLE_SIZE,
254                                                                              HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
255         modest_serversecurity_picker_fill (MODEST_SERVERSECURITY_PICKER (ppriv->security_view), 
256                                               MODEST_PROTOCOLS_TRANSPORT_SMTP);
257         modest_maemo_utils_set_hbutton_layout (title_size_group,
258                                                value_size_group,
259                                                _("mcen_li_emailsetup_secure_connection"), 
260                                                ppriv->security_view);
261         
262         /* The secure authentication widgets */
263         ppriv->auth_view = GTK_WIDGET (modest_secureauth_picker_new (MODEST_EDITABLE_SIZE,
264                                                                      HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
265         modest_maemo_utils_set_hbutton_layout (title_size_group,
266                                                value_size_group,
267                                                _("mcen_li_emailsetup_secure_authentication"), 
268                                                ppriv->auth_view);
269
270         if (ppriv->full) {
271                 gchar *user_label;
272
273                 /* Username widgets */
274                 ppriv->user_entry = GTK_WIDGET (modest_validating_entry_new ());
275
276                 /* Auto-capitalization is the default, so let's turn it off: */
277                 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->user_entry), 
278                                                  HILDON_GTK_INPUT_MODE_FULL);
279
280                 user_label = g_strdup_printf("%s*", _("mail_fi_username"));
281                 user_caption = modest_maemo_utils_create_captioned (title_size_group,
282                                                                     value_size_group,
283                                                                     user_label,
284                                                                     FALSE,
285                                                                     ppriv->user_entry);
286                 g_free (user_label);
287         
288                 /* Prevent the use of some characters. Limit the max
289                    length as well */
290                 modest_validating_entry_set_unallowed_characters_whitespace (
291                      MODEST_VALIDATING_ENTRY (ppriv->user_entry));      
292                 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
293                 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
294                                                       on_entry_max, self);
295                 
296                 /* Password widgets */
297                 ppriv->pwd_entry = hildon_entry_new (MODEST_EDITABLE_SIZE);
298
299                 /* Auto-capitalization is the default, so let's turn it off */
300                 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
301                                                  HILDON_GTK_INPUT_MODE_FULL | 
302                                                  HILDON_GTK_INPUT_MODE_INVISIBLE);
303                 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
304
305                 pwd_caption = modest_maemo_utils_create_captioned (title_size_group,
306                                                                    value_size_group,
307                                                                    _("mail_fi_password"), 
308                                                                    FALSE,
309                                                                    ppriv->pwd_entry);
310
311                 ppriv->port_view = GTK_WIDGET (modest_number_editor_new (PORT_MIN, PORT_MAX));
312                 port_caption = modest_maemo_utils_create_captioned (title_size_group,
313                                                                     value_size_group,
314                                                                     _("mcen_fi_emailsetup_port"), 
315                                                                     FALSE,
316                                                                     ppriv->port_view);
317         }
318
319         /* Track changes in UI */       
320         g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
321                           G_CALLBACK (on_security_changed), self);
322         if (ppriv->full) {
323                 g_signal_connect (G_OBJECT (ppriv->auth_view), "value-changed",
324                                   G_CALLBACK (on_auth_changed), self);
325                 g_signal_connect (G_OBJECT (ppriv->user_entry), "changed",
326                                   G_CALLBACK (on_entry_changed), self);
327                 g_signal_connect (G_OBJECT (ppriv->port_view), "valid-changed",
328                                             G_CALLBACK (on_valid_changed), self);
329         }
330
331         /* Initialize widgets */
332         modest_serversecurity_picker_set_active_serversecurity (
333                 MODEST_SERVERSECURITY_PICKER (ppriv->security_view), 
334                 MODEST_PROTOCOLS_CONNECTION_NONE);
335         modest_secureauth_picker_set_active_secureauth (
336            MODEST_SECUREAUTH_PICKER (ppriv->auth_view),
337            MODEST_PROTOCOLS_AUTH_NONE);
338
339         /* Pack into container */
340         if (ppriv->full) {
341                 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, MODEST_MARGIN_HALF);
342                 gtk_box_pack_start (GTK_BOX (self), user_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
343                 gtk_box_pack_start (GTK_BOX (self), pwd_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
344                 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, MODEST_MARGIN_HALF);
345                 gtk_box_pack_start (GTK_BOX (self), port_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
346         } else {
347                 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, MODEST_MARGIN_HALF);
348                 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, MODEST_MARGIN_HALF);
349         }
350
351         /* Show widgets */
352         if (ppriv->full) {
353                 gtk_widget_show (ppriv->pwd_entry);
354                 gtk_widget_show (ppriv->user_entry);
355                 gtk_widget_show (ppriv->port_view);
356                 gtk_widget_show (pwd_caption);
357                 gtk_widget_show (user_caption);
358                 gtk_widget_show (port_caption);
359         }
360         gtk_widget_show (ppriv->security_view);
361         gtk_widget_show (ppriv->auth_view);
362 }
363
364 GtkWidget *    
365 modest_maemo_security_options_view_new  (ModestSecurityOptionsType type,
366                                          gboolean full,
367                                          GtkSizeGroup *title_size_group,
368                                          GtkSizeGroup *value_size_group)
369 {
370         ModestSecurityOptionsView* self;
371         ModestSecurityOptionsViewPrivate *ppriv;
372
373         self = (ModestSecurityOptionsView *)
374                 g_object_new (MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, NULL);
375         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
376
377         ppriv->full = full;
378         self->type = type;
379         if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
380                 create_incoming_security (self, title_size_group, value_size_group);
381         else
382                 create_outgoing_security (self, title_size_group, value_size_group);
383
384         return (GtkWidget *) self;
385 }
386
387 static void 
388 modest_maemo_security_options_view_load_settings (ModestSecurityOptionsView* self, 
389                                                   ModestAccountSettings *settings)
390 {
391         ModestSecurityOptionsViewPrivate *ppriv;
392         ModestServerAccountSettings *server_settings;
393         gint port_number;
394
395         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
396
397         if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
398                 server_settings = modest_account_settings_get_store_settings (settings);
399         else
400                 server_settings = modest_account_settings_get_transport_settings (settings);
401         port_number = modest_server_account_settings_get_port (server_settings);
402
403         if (port_number == 0) {
404                 /* Show the appropriate port number */
405                 on_security_changed (ppriv->security_view, 
406                                      MODEST_MAEMO_SECURITY_OPTIONS_VIEW (self));
407         } else if (ppriv->full) {
408                 /* Keep the user-entered port-number, or the
409                  * already-appropriate automatic port number */
410                 modest_number_editor_set_value (MODEST_NUMBER_EDITOR (ppriv->port_view), 
411                                                 port_number);
412         }
413         /* Frees */
414         g_object_unref (server_settings);
415 }
416
417 static void
418 modest_maemo_security_options_view_save_settings (ModestSecurityOptionsView* self, 
419                                                   ModestAccountSettings *settings)
420 {
421         ModestServerAccountSettings *server_settings;
422         ModestSecurityOptionsViewPrivate *ppriv;
423         gint server_port;
424
425         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
426
427         if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
428                 server_settings = modest_account_settings_get_store_settings (settings);
429         else
430                 server_settings = modest_account_settings_get_transport_settings (settings);
431
432         if (ppriv->full) {
433                 server_port = modest_number_editor_get_value (MODEST_NUMBER_EDITOR (ppriv->port_view));
434         } else {
435                 server_port = modest_serversecurity_picker_get_active_serversecurity_port (MODEST_SERVERSECURITY_PICKER (ppriv->security_view));
436         }
437
438         modest_server_account_settings_set_port (server_settings, server_port);
439
440         /* Frees */
441         g_object_unref (server_settings);
442 }
443
444 static gboolean 
445 modest_maemo_security_options_view_changed (ModestSecurityOptionsView* self,
446                                             ModestAccountSettings *settings)
447 {
448         ModestServerAccountSettings *server_settings;
449         ModestSecurityOptionsViewPrivate *ppriv;
450         gint server_port;
451
452         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
453         
454         /* If we're not showing the port number then it never changes */
455         if (!ppriv->full)
456                 return FALSE;
457
458         if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
459                 server_settings = modest_account_settings_get_store_settings (settings);
460         else
461                 server_settings = modest_account_settings_get_transport_settings (settings);
462         
463         server_port = 
464                 modest_number_editor_get_value (MODEST_NUMBER_EDITOR (ppriv->port_view));
465
466         /* Frees */
467         g_object_unref (server_settings);
468
469         if (server_port != ppriv->initial_state.port)
470                 return TRUE;
471         else
472                 return FALSE;
473 }
474
475 static void
476 modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj)
477 {
478 }
479
480 static void
481 modest_maemo_security_options_view_finalize (GObject *obj)
482 {
483         G_OBJECT_CLASS (modest_maemo_security_options_view_parent_class)->finalize (obj);
484 }
485
486
487 static void     
488 modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass)
489 {
490         GObjectClass *gobject_class = (GObjectClass*) klass;
491
492         modest_maemo_security_options_view_parent_class = g_type_class_peek_parent (klass);
493
494         gobject_class->finalize = modest_maemo_security_options_view_finalize;
495
496         MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->load_settings = 
497                 modest_maemo_security_options_view_load_settings;
498         MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->save_settings = 
499                 modest_maemo_security_options_view_save_settings;
500         MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->changed = 
501                 modest_maemo_security_options_view_changed;
502 }