2038b5f69f525ddd1b526a84306cc712cf28788b
[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 <hildon/hildon-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                         hildon_number_editor_set_value (HILDON_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 *size_group)
132 {
133         ModestSecurityOptionsViewPrivate *ppriv;
134         GtkWidget *entry_caption = NULL;
135
136         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
137
138         /* Create widgets for incoming security */
139         ppriv->security_view = GTK_WIDGET (modest_serversecurity_picker_new (MODEST_EDITABLE_SIZE,
140                                                                              MODEST_EDITABLE_ARRANGEMENT));
141         modest_serversecurity_picker_fill (MODEST_SERVERSECURITY_PICKER (ppriv->security_view), 
142                                            modest_protocol_registry_get_pop_type_id ());
143         modest_maemo_utils_create_picker_layout (size_group, 
144                                                  _("mcen_li_emailsetup_secure_connection"), 
145                                                  ppriv->security_view);
146
147         if (ppriv->full) {              
148                 ppriv->port_view = GTK_WIDGET (hildon_number_editor_new (PORT_MIN, PORT_MAX));
149                 entry_caption = modest_maemo_utils_create_captioned (size_group, _("mcen_fi_emailsetup_port"), 
150                                                                      ppriv->port_view);
151         }
152
153         ppriv->auth_view = hildon_check_button_new (MODEST_EDITABLE_SIZE);
154         gtk_button_set_label (GTK_BUTTON (ppriv->auth_view), _("mcen_li_emailsetup_secure_authentication"));
155         gtk_size_group_add_widget (size_group, ppriv->auth_view);
156
157         /* Track changes in UI */       
158         g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
159                           G_CALLBACK (on_security_changed), self);
160
161         /* Pack into container */
162         gtk_box_pack_start (GTK_BOX (self), ppriv->security_view,
163                             FALSE, FALSE, MODEST_MARGIN_HALF);
164         if (ppriv->full)
165                 gtk_box_pack_start (GTK_BOX (self), entry_caption, 
166                                     FALSE, FALSE, MODEST_MARGIN_HALF);
167         gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view,
168                             FALSE, FALSE, MODEST_MARGIN_HALF);
169
170         /* Show widgets */
171         if (ppriv->full) {
172                 gtk_widget_show (ppriv->port_view);
173                 gtk_widget_show (entry_caption);
174         }
175         gtk_widget_show (ppriv->security_view);
176         gtk_widget_show (ppriv->auth_view);
177 }
178
179 static void
180 on_entry_max (ModestValidatingEntry *self, 
181               gpointer user_data)
182 {
183         modest_platform_information_banner (GTK_WIDGET (self), NULL, 
184                                             _CS("ckdg_ib_maximum_characters_reached"));
185 }
186
187 /*
188  * TODO: call this whenever the auth picker changes. If we set it
189  * explicitely at the beggining to a value then there is no need to
190  * call this handler directly at the beginning
191  */
192 static void
193 on_entry_changed (GtkEditable *editable, 
194                   gpointer user_data)
195 {
196         ModestSecurityOptionsView* self;
197         ModestSecurityOptionsViewPrivate *ppriv;
198         ModestProtocolType auth_proto;
199         ModestSecureauthPicker *picker;
200         gboolean is_secure, missing;
201         ModestProtocolRegistry *protocol_registry;
202
203         self = MODEST_SECURITY_OPTIONS_VIEW (user_data);
204         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
205         protocol_registry = modest_runtime_get_protocol_registry ();
206
207         /* Outgoing username is mandatory if outgoing auth is secure */
208         picker = MODEST_SECUREAUTH_PICKER (ppriv->auth_view);
209         auth_proto = modest_secureauth_picker_get_active_secureauth (picker);
210         is_secure = modest_protocol_registry_protocol_type_is_secure (protocol_registry, 
211                                                                       auth_proto);
212
213         if (is_secure && 
214             !g_ascii_strcasecmp (gtk_entry_get_text (GTK_ENTRY (ppriv->user_entry)), "")) {
215                 missing = TRUE;
216         } else {
217                 missing = FALSE;
218         }
219         
220         /* Emit a signal to notify if mandatory data is missing */
221         g_signal_emit_by_name (G_OBJECT (self), "missing_mandatory_data", 
222                                missing, NULL);
223 }
224
225 static void
226 create_outgoing_security (ModestSecurityOptionsView* self,
227                           GtkSizeGroup *size_group)
228 {
229         ModestSecurityOptionsViewPrivate *ppriv;
230         GtkWidget *user_caption = NULL;
231         GtkWidget *pwd_caption = NULL, *port_caption = NULL;
232
233         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
234         
235         /* The secure connection widgets */
236         ppriv->security_view = GTK_WIDGET (modest_serversecurity_picker_new (MODEST_EDITABLE_SIZE,
237                                                                              MODEST_EDITABLE_ARRANGEMENT));
238         modest_serversecurity_picker_fill (MODEST_SERVERSECURITY_PICKER (ppriv->security_view), 
239                                               MODEST_PROTOCOLS_TRANSPORT_SMTP);
240         modest_maemo_utils_create_picker_layout (size_group, _("mcen_li_emailsetup_secure_connection"), 
241                                                  ppriv->security_view);
242         
243         /* The secure authentication widgets */
244         ppriv->auth_view = GTK_WIDGET (modest_secureauth_picker_new (MODEST_EDITABLE_SIZE,
245                                                                      MODEST_EDITABLE_ARRANGEMENT));
246         modest_maemo_utils_create_picker_layout (size_group, _("mcen_li_emailsetup_secure_authentication"), 
247                                                  ppriv->auth_view);
248
249         if (ppriv->full) {
250                 gchar *user_label;
251
252                 /* Username widgets */
253                 ppriv->user_entry = GTK_WIDGET (modest_validating_entry_new ());
254
255                 /* Auto-capitalization is the default, so let's turn it off: */
256                 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->user_entry), 
257                                                  HILDON_GTK_INPUT_MODE_FULL);
258
259                 user_label = g_strdup_printf("%s*", _("mail_fi_username"));
260                 user_caption = modest_maemo_utils_create_captioned (size_group, user_label, 
261                                                                     ppriv->user_entry);
262                 g_free (user_label);
263         
264                 /* Prevent the use of some characters. Limit the max
265                    length as well */
266                 modest_validating_entry_set_unallowed_characters_whitespace (
267                      MODEST_VALIDATING_ENTRY (ppriv->user_entry));      
268                 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
269                 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
270                                                       on_entry_max, self);
271                 
272                 /* Password widgets */
273                 ppriv->pwd_entry = gtk_entry_new ();
274
275                 /* Auto-capitalization is the default, so let's turn it off */
276                 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
277                                                  HILDON_GTK_INPUT_MODE_FULL | 
278                                                  HILDON_GTK_INPUT_MODE_INVISIBLE);
279                 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
280
281                 pwd_caption = modest_maemo_utils_create_captioned (size_group, _("mail_fi_password"), 
282                                                                    ppriv->pwd_entry);
283
284                 ppriv->port_view = GTK_WIDGET (hildon_number_editor_new (PORT_MIN, PORT_MAX));
285                 port_caption = modest_maemo_utils_create_captioned (size_group, _("mcen_fi_emailsetup_port"), 
286                                                                     ppriv->port_view);
287         }
288
289         /* Track changes in UI */       
290         g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
291                           G_CALLBACK (on_security_changed), self);
292         if (ppriv->full) {
293                 g_signal_connect (G_OBJECT (ppriv->auth_view), "value-changed",
294                                   G_CALLBACK (on_auth_changed), self);
295                 g_signal_connect (G_OBJECT (ppriv->user_entry), "changed",
296                                   G_CALLBACK (on_entry_changed), self);
297         }
298
299         /* Initialize widgets */
300         modest_serversecurity_picker_set_active_serversecurity (
301                 MODEST_SERVERSECURITY_PICKER (ppriv->security_view), 
302                 MODEST_PROTOCOLS_CONNECTION_NONE);
303         modest_secureauth_picker_set_active_secureauth (
304            MODEST_SECUREAUTH_PICKER (ppriv->auth_view),
305            MODEST_PROTOCOLS_AUTH_NONE);
306
307         /* Pack into container */
308         if (ppriv->full) {
309                 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, MODEST_MARGIN_HALF);
310                 gtk_box_pack_start (GTK_BOX (self), user_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
311                 gtk_box_pack_start (GTK_BOX (self), pwd_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
312                 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, MODEST_MARGIN_HALF);
313                 gtk_box_pack_start (GTK_BOX (self), port_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
314         } else {
315                 /* The order is different */
316                 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, MODEST_MARGIN_HALF);
317                 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, MODEST_MARGIN_HALF);
318         }
319
320         /* Show widgets */
321         if (ppriv->full) {
322                 gtk_widget_show (ppriv->pwd_entry);
323                 gtk_widget_show (ppriv->user_entry);
324                 gtk_widget_show (ppriv->port_view);
325                 gtk_widget_show (pwd_caption);
326                 gtk_widget_show (user_caption);
327                 gtk_widget_show (port_caption);
328         }
329         gtk_widget_show (ppriv->security_view);
330         gtk_widget_show (ppriv->auth_view);
331 }
332
333 GtkWidget *    
334 modest_maemo_security_options_view_new  (ModestSecurityOptionsType type,
335                                          gboolean full,
336                                          GtkSizeGroup *size_group)
337 {
338         ModestSecurityOptionsView* self;
339         ModestSecurityOptionsViewPrivate *ppriv;
340
341         self = (ModestSecurityOptionsView *)
342                 g_object_new (MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, NULL);
343         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
344
345         ppriv->full = full;
346         self->type = type;
347         if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
348                 create_incoming_security (self, size_group);
349         else
350                 create_outgoing_security (self, size_group);
351
352         return (GtkWidget *) self;
353 }
354
355 static void 
356 modest_maemo_security_options_view_load_settings (ModestSecurityOptionsView* self, 
357                                                   ModestAccountSettings *settings)
358 {
359         ModestSecurityOptionsViewPrivate *ppriv;
360         ModestServerAccountSettings *server_settings;
361         gint port_number;
362
363         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
364
365         if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
366                 server_settings = modest_account_settings_get_store_settings (settings);
367         else
368                 server_settings = modest_account_settings_get_transport_settings (settings);
369         port_number = modest_server_account_settings_get_port (server_settings);
370
371         if (port_number == 0) {
372                 /* Show the appropriate port number */
373                 on_security_changed (ppriv->security_view, 
374                                      MODEST_MAEMO_SECURITY_OPTIONS_VIEW (self));
375         } else if (ppriv->full) {
376                 /* Keep the user-entered port-number, or the
377                  * already-appropriate automatic port number */
378                 hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (ppriv->port_view), 
379                                                 port_number);
380         }
381         /* Frees */
382         g_object_unref (server_settings);
383 }
384
385 static void
386 modest_maemo_security_options_view_save_settings (ModestSecurityOptionsView* self, 
387                                                   ModestAccountSettings *settings)
388 {
389         ModestServerAccountSettings *server_settings;
390         ModestSecurityOptionsViewPrivate *ppriv;
391         gint server_port;
392
393         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
394
395         if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
396                 server_settings = modest_account_settings_get_store_settings (settings);
397         else
398                 server_settings = modest_account_settings_get_transport_settings (settings);
399
400         if (ppriv->full) {
401                 server_port = hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (ppriv->port_view));
402         } else {
403                 server_port = modest_serversecurity_picker_get_active_serversecurity_port (MODEST_SERVERSECURITY_PICKER (ppriv->security_view));
404         }
405
406         modest_server_account_settings_set_port (server_settings, server_port);
407
408         /* Frees */
409         g_object_unref (server_settings);
410 }
411
412 static gboolean 
413 modest_maemo_security_options_view_changed (ModestSecurityOptionsView* self,
414                                             ModestAccountSettings *settings)
415 {
416         ModestServerAccountSettings *server_settings;
417         ModestSecurityOptionsViewPrivate *ppriv;
418         gint server_port;
419
420         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
421         
422         /* If we're not showing the port number then it never changes */
423         if (!ppriv->full)
424                 return FALSE;
425
426         if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
427                 server_settings = modest_account_settings_get_store_settings (settings);
428         else
429                 server_settings = modest_account_settings_get_transport_settings (settings);
430         
431         server_port = 
432                 hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (ppriv->port_view));
433
434         /* Frees */
435         g_object_unref (server_settings);
436
437         if (server_port != ppriv->initial_state.port)
438                 return TRUE;
439         else
440                 return FALSE;
441 }
442
443 static void
444 modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj)
445 {
446 }
447
448 static void
449 modest_maemo_security_options_view_finalize (GObject *obj)
450 {
451         G_OBJECT_CLASS (modest_maemo_security_options_view_parent_class)->finalize (obj);
452 }
453
454
455 static void     
456 modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass)
457 {
458         GObjectClass *gobject_class = (GObjectClass*) klass;
459
460         modest_maemo_security_options_view_parent_class = g_type_class_peek_parent (klass);
461
462         gobject_class->finalize = modest_maemo_security_options_view_finalize;
463
464         MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->load_settings = 
465                 modest_maemo_security_options_view_load_settings;
466         MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->save_settings = 
467                 modest_maemo_security_options_view_save_settings;
468         MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->changed = 
469                 modest_maemo_security_options_view_changed;
470 }