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