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