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