aaabee8d7e106a9c169b970403169484309ea70b
[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 #include "widgets/modest-secureauth-combo-box.h"
39
40 #define PORT_MIN 1
41 #define PORT_MAX 65535
42
43 typedef struct _ModestGtkSecurityOptionsViewPrivate ModestGtkSecurityOptionsViewPrivate;
44 struct _ModestGtkSecurityOptionsViewPrivate {
45         gboolean missing_data;
46 };
47
48 #define MODEST_GTK_SECURITY_OPTIONS_VIEW_GET_PRIVATE(o) \
49         (G_TYPE_INSTANCE_GET_PRIVATE((o), \
50                                      MODEST_TYPE_GTK_SECURITY_OPTIONS_VIEW, \
51                                      ModestGtkSecurityOptionsViewPrivate))
52
53 static void modest_gtk_security_options_view_init (ModestGtkSecurityOptionsView *obj);
54 static void modest_gtk_security_options_view_finalize (GObject *obj);
55 static void modest_gtk_security_options_view_class_init (ModestGtkSecurityOptionsViewClass *klass);
56
57 G_DEFINE_TYPE (ModestGtkSecurityOptionsView, 
58                modest_gtk_security_options_view, 
59                MODEST_TYPE_SECURITY_OPTIONS_VIEW);
60
61 static void on_entry_changed (GtkEditable *editable, gpointer user_data);
62
63 /* Tracks changes in the incoming security combo box */
64 static void
65 on_security_changed (GtkWidget *widget, 
66                      ModestGtkSecurityOptionsView *self)
67 {
68         ModestSecurityOptionsViewPrivate* ppriv;
69         ModestProtocolType proto_type;
70         ModestProtocolRegistry *proto_registry;
71         gboolean is_secure;
72
73         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
74
75         proto_registry = modest_runtime_get_protocol_registry ();
76         proto_type = modest_serversecurity_selector_get_active_serversecurity (ppriv->security_view);
77
78         is_secure = modest_protocol_registry_protocol_type_has_tag (proto_registry, proto_type, 
79                                                                     MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS);
80
81         if (MODEST_SECURITY_OPTIONS_VIEW (self)->type == MODEST_SECURITY_OPTIONS_INCOMING) {
82                 /* Activate and dim checkbutton if it's secure */
83                 modest_togglable_set_active (ppriv->auth_view, 
84                                              is_secure);
85                 gtk_widget_set_sensitive (ppriv->auth_view, !is_secure);
86         } else {
87
88         }
89
90         if (ppriv->full) {
91                 gint port_number = 
92                         modest_serversecurity_selector_get_active_serversecurity_port (ppriv->security_view);
93                 
94                 if(port_number) {
95                         modest_number_entry_set_value (ppriv->port_view,
96                                                        port_number);
97                 }
98         }
99 }
100
101 static void
102 on_auth_changed (GtkWidget *widget, 
103                  ModestGtkSecurityOptionsView *self)
104 {
105         ModestSecurityOptionsViewPrivate* ppriv;
106         ModestSecureauthComboBox *combo;
107         ModestProtocolRegistry *protocol_registry;
108         ModestProtocolType auth_proto;
109         gboolean secureauth_used;
110         GtkWidget *user_caption, *pwd_caption;
111
112         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
113         protocol_registry = modest_runtime_get_protocol_registry ();
114         combo = MODEST_SECUREAUTH_COMBO_BOX (ppriv->auth_view);
115
116         auth_proto = modest_secureauth_combo_box_get_active_secureauth (combo);
117         secureauth_used = modest_protocol_registry_protocol_type_is_secure (protocol_registry, 
118                                                                             auth_proto);
119
120         /* Get captions, well dimm the whole widget */
121         user_caption = gtk_widget_get_parent (ppriv->user_entry);
122         pwd_caption = gtk_widget_get_parent (ppriv->pwd_entry);
123         
124         /* Enable / disable */
125         gtk_widget_set_sensitive (user_caption, secureauth_used);
126         gtk_widget_set_sensitive (pwd_caption, secureauth_used);
127
128         /* Check if mandatory data is missing */
129         on_entry_changed (GTK_EDITABLE (ppriv->user_entry), (gpointer) self);
130 }
131
132 static void
133 create_incoming_security (ModestSecurityOptionsView* self,
134                           GtkSizeGroup *title_size_group, GtkSizeGroup *value_size_group)
135 {
136         ModestSecurityOptionsViewPrivate *ppriv;
137         GtkWidget *combo_caption, *entry_caption = NULL;
138
139         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
140
141         /* Create widgets for incoming security */
142         ppriv->security_view = modest_toolkit_factory_create_serversecurity_selector (modest_runtime_get_toolkit_factory ());
143         combo_caption = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
144                                                              _("mcen_li_emailsetup_secure_connection"), FALSE,
145                                                              ppriv->security_view);
146
147         if (ppriv->full) {              
148                 ppriv->port_view = modest_toolkit_factory_create_number_entry (modest_runtime_get_toolkit_factory (), PORT_MIN, PORT_MAX);
149                 entry_caption = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
150                                                                      _("mcen_fi_emailsetup_port"), FALSE,
151                                                                      ppriv->port_view);
152         }
153
154         ppriv->auth_view = modest_toolkit_factory_create_check_button (modest_runtime_get_toolkit_factory (), 
155                                                                        _("mcen_li_emailsetup_secure_authentication"));
156
157         /* Track changes in UI */
158         if (GTK_IS_COMBO_BOX (ppriv->security_view)) {
159                 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
160                                   G_CALLBACK (on_security_changed), self);
161         } else {
162                 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
163                                   G_CALLBACK (on_security_changed), self);
164         }
165
166         /* Pack into container */
167         gtk_box_pack_start (GTK_BOX (self), combo_caption,
168                             FALSE, FALSE, MODEST_MARGIN_HALF);
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->auth_view,
173                             FALSE, FALSE, MODEST_MARGIN_HALF);
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         gtk_widget_show (combo_caption);
183 }
184
185 static void
186 on_entry_max (ModestValidatingEntry *self, 
187               gpointer user_data)
188 {
189         modest_platform_information_banner (GTK_WIDGET (self), NULL, 
190                                             _CS("ckdg_ib_maximum_characters_reached"));
191 }
192
193 /*
194  * TODO: call this whenever the auth combo changes. If we set it
195  * explicitely at the beggining to a value then there is no need to
196  * call this handler directly at the beginning
197  */
198 static void
199 on_entry_changed (GtkEditable *editable, 
200                   gpointer user_data)
201 {
202         ModestSecurityOptionsView* self;
203         ModestGtkSecurityOptionsViewPrivate *priv;
204         ModestSecurityOptionsViewPrivate *ppriv;
205         ModestProtocolType auth_proto;
206         ModestSecureauthComboBox *combo;
207         gboolean is_secure;
208         ModestProtocolRegistry *protocol_registry;
209
210         self = MODEST_SECURITY_OPTIONS_VIEW (user_data);
211         priv = MODEST_GTK_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
212         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
213         protocol_registry = modest_runtime_get_protocol_registry ();
214
215         /* Outgoing username is mandatory if outgoing auth is secure */
216         combo = MODEST_SECUREAUTH_COMBO_BOX (ppriv->auth_view);
217         auth_proto = modest_secureauth_combo_box_get_active_secureauth (combo);
218         is_secure = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
219                                                                       auth_proto);
220
221         if (is_secure &&
222             !g_ascii_strcasecmp (gtk_entry_get_text (GTK_ENTRY (ppriv->user_entry)), "")) {
223                 priv->missing_data = TRUE;
224         } else {
225                 priv->missing_data = FALSE;
226         }
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                                priv->missing_data, NULL);
231 }
232
233 static void
234 create_outgoing_security (ModestSecurityOptionsView* self,
235                           GtkSizeGroup *title_size_group, GtkSizeGroup *value_size_group)
236 {
237         ModestSecurityOptionsViewPrivate *ppriv;
238         GtkWidget *sec_caption, *auth_caption, *user_caption = NULL;
239         GtkWidget *pwd_caption = NULL, *port_caption = NULL;
240
241         ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
242         
243         /* The secure connection widgets */
244         ppriv->security_view = modest_toolkit_factory_create_serversecurity_selector (modest_runtime_get_toolkit_factory ());
245         modest_serversecurity_selector_fill (ppriv->security_view,
246                                              MODEST_PROTOCOLS_TRANSPORT_SMTP);
247         sec_caption = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
248                                                            _("mcen_li_emailsetup_secure_connection"), FALSE,
249                                                            ppriv->security_view);
250         
251         /* The secure authentication widgets */
252         ppriv->auth_view = GTK_WIDGET (modest_secureauth_combo_box_new ());
253         auth_caption = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
254                                                             _("mcen_li_emailsetup_secure_authentication"), FALSE,
255                                                             ppriv->auth_view);
256
257         if (ppriv->full) {
258                 gchar *user_label;
259
260                 /* Username widgets */
261                 ppriv->user_entry = GTK_WIDGET (modest_validating_entry_new ());
262
263                 /* Auto-capitalization is the default, so let's turn it off: */
264                 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->user_entry), 
265                                                  HILDON_GTK_INPUT_MODE_FULL);
266
267                 user_label = g_strdup_printf("%s*", _("mail_fi_username"));
268                 user_caption = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
269                                                                     user_label, FALSE,
270                                                                     ppriv->user_entry);
271                 g_free (user_label);
272         
273                 /* Prevent the use of some characters. Limit the max
274                    length as well */
275                 modest_validating_entry_set_unallowed_characters_whitespace (
276                      MODEST_VALIDATING_ENTRY (ppriv->user_entry));      
277                 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
278                 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
279                                                       on_entry_max, self);
280                 
281                 /* Password widgets */
282                 ppriv->pwd_entry = gtk_entry_new ();
283
284                 /* Auto-capitalization is the default, so let's turn it off */
285 #ifdef MODEST_TOOLKIT_HILDON2
286                 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
287                                                  HILDON_GTK_INPUT_MODE_FULL | 
288                                                  HILDON_GTK_INPUT_MODE_INVISIBLE);
289 #endif
290                 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
291
292                 pwd_caption = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
293                                                                    _("mail_fi_password"), FALSE,
294                                                                    ppriv->pwd_entry);
295
296                 ppriv->port_view = modest_toolkit_factory_create_number_entry (modest_runtime_get_toolkit_factory (),
297                                                                                PORT_MIN, PORT_MAX);
298                 port_caption = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
299                                                                     _("mcen_fi_emailsetup_port"), FALSE,
300                                                                     ppriv->port_view);
301         }
302
303         /* Track changes in UI */
304         if (GTK_IS_COMBO_BOX (ppriv->security_view)) {
305                 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
306                                   G_CALLBACK (on_security_changed), self);
307         } else {
308                 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
309                                   G_CALLBACK (on_security_changed), self);
310         }
311         if (ppriv->full) {
312                 g_signal_connect (G_OBJECT (ppriv->auth_view), "changed",
313                                   G_CALLBACK (on_auth_changed), self);
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_combo_box_set_active_secureauth (
323            MODEST_SECUREAUTH_COMBO_BOX (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 }