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