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