1 /* Copyright (c) 2008, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
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 "modest-serversecurity-picker.h"
39 #include "modest-secureauth-picker.h"
40 #include "modest-maemo-utils.h"
41 #include <modest-number-editor.h>
42 #include <hildon/hildon-check-button.h>
43 #include "modest-hildon-includes.h"
46 #define PORT_MAX 65535
48 typedef struct _ModestMaemoSecurityOptionsViewPrivate ModestMaemoSecurityOptionsViewPrivate;
49 struct _ModestMaemoSecurityOptionsViewPrivate {
50 gboolean missing_data;
53 #define MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE(o) \
54 (G_TYPE_INSTANCE_GET_PRIVATE((o), \
55 MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, \
56 ModestMaemoSecurityOptionsViewPrivate))
58 static void modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj);
59 static void modest_maemo_security_options_view_finalize (GObject *obj);
60 static void modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass);
62 G_DEFINE_TYPE (ModestMaemoSecurityOptionsView,
63 modest_maemo_security_options_view,
64 MODEST_TYPE_SECURITY_OPTIONS_VIEW);
66 static void on_entry_changed (GtkEditable *editable, gpointer user_data);
68 /* Tracks changes in the incoming security picker */
70 on_security_changed (GtkWidget *widget,
71 ModestMaemoSecurityOptionsView *self)
73 ModestSecurityOptionsViewPrivate* ppriv;
74 ModestServersecurityPicker *picker;
75 ModestProtocolType proto_type;
76 ModestProtocolRegistry *proto_registry;
79 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
81 proto_registry = modest_runtime_get_protocol_registry ();
82 picker = MODEST_SERVERSECURITY_PICKER (ppriv->security_view);
83 proto_type = modest_serversecurity_picker_get_active_serversecurity (picker);
85 is_secure = modest_protocol_registry_protocol_type_has_tag (proto_registry, proto_type,
86 MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS);
88 if (MODEST_SECURITY_OPTIONS_VIEW (self)->type == MODEST_SECURITY_OPTIONS_INCOMING) {
89 /* Activate and dim checkbutton if it's secure */
90 hildon_check_button_set_active (HILDON_CHECK_BUTTON (ppriv->auth_view),
92 gtk_widget_set_sensitive (ppriv->auth_view, !is_secure);
99 modest_serversecurity_picker_get_active_serversecurity_port (MODEST_SERVERSECURITY_PICKER (ppriv->security_view));
102 modest_number_editor_set_value (MODEST_NUMBER_EDITOR (ppriv->port_view),
109 on_auth_changed (GtkWidget *widget,
110 ModestMaemoSecurityOptionsView *self)
112 ModestSecurityOptionsViewPrivate* ppriv;
113 ModestSecureauthPicker *picker;
114 ModestProtocolRegistry *protocol_registry;
115 ModestProtocolType auth_proto;
116 gboolean secureauth_used;
117 GtkWidget *user_caption, *pwd_caption;
119 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
120 protocol_registry = modest_runtime_get_protocol_registry ();
121 picker = MODEST_SECUREAUTH_PICKER (ppriv->auth_view);
123 auth_proto = modest_secureauth_picker_get_active_secureauth (picker);
124 secureauth_used = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
127 /* Get captions, well dimm the whole widget */
128 user_caption = gtk_widget_get_parent (ppriv->user_entry);
129 pwd_caption = gtk_widget_get_parent (ppriv->pwd_entry);
131 /* Enable / disable */
132 gtk_widget_set_sensitive (user_caption, secureauth_used);
133 gtk_widget_set_sensitive (pwd_caption, secureauth_used);
135 /* Check if mandatory data is missing */
136 on_entry_changed (GTK_EDITABLE (ppriv->user_entry), (gpointer) self);
140 create_incoming_security (ModestSecurityOptionsView* self,
141 GtkSizeGroup *title_size_group,
142 GtkSizeGroup *value_size_group)
144 ModestSecurityOptionsViewPrivate *ppriv;
145 GtkWidget *entry_caption = NULL;
147 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
149 /* Create widgets for incoming security */
150 ppriv->security_view = GTK_WIDGET (modest_serversecurity_picker_new (MODEST_EDITABLE_SIZE,
151 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
152 modest_serversecurity_picker_fill (MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
153 modest_protocol_registry_get_pop_type_id ());
154 modest_maemo_utils_set_hbutton_layout (title_size_group,
156 _("mcen_li_emailsetup_secure_connection"),
157 ppriv->security_view);
159 ppriv->auth_view = hildon_check_button_new (MODEST_EDITABLE_SIZE);
160 gtk_button_set_label (GTK_BUTTON (ppriv->auth_view), _("mcen_li_emailsetup_secure_authentication"));
161 gtk_button_set_alignment (GTK_BUTTON (ppriv->auth_view), 0.0, 0.5);
163 /* Track changes in UI */
164 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
165 G_CALLBACK (on_security_changed), self);
167 /* Pack into container & show */
168 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
169 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
170 gtk_widget_show (ppriv->security_view);
171 gtk_widget_show (ppriv->auth_view);
174 ppriv->port_view = GTK_WIDGET (modest_number_editor_new (PORT_MIN, PORT_MAX));
175 entry_caption = modest_maemo_utils_create_captioned (title_size_group,
177 _("mcen_fi_emailsetup_port"),
180 /* Pack & show widgets */
181 gtk_box_pack_start (GTK_BOX (self), entry_caption, FALSE, FALSE, 0);
182 gtk_widget_show (ppriv->port_view);
183 gtk_widget_show (entry_caption);
188 on_entry_max (ModestValidatingEntry *self,
191 modest_platform_information_banner (GTK_WIDGET (self), NULL,
192 _CS("ckdg_ib_maximum_characters_reached"));
196 * TODO: call this whenever the auth picker changes. If we set it
197 * explicitely at the beggining to a value then there is no need to
198 * call this handler directly at the beginning
201 on_entry_changed (GtkEditable *editable,
204 ModestSecurityOptionsView* self;
205 ModestMaemoSecurityOptionsViewPrivate *priv;
206 ModestSecurityOptionsViewPrivate *ppriv;
207 ModestProtocolType auth_proto;
208 ModestSecureauthPicker *picker;
210 ModestProtocolRegistry *protocol_registry;
212 self = MODEST_SECURITY_OPTIONS_VIEW (user_data);
213 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
214 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
215 protocol_registry = modest_runtime_get_protocol_registry ();
217 /* Outgoing username is mandatory if outgoing auth is secure */
218 picker = MODEST_SECUREAUTH_PICKER (ppriv->auth_view);
219 auth_proto = modest_secureauth_picker_get_active_secureauth (picker);
220 is_secure = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
224 !g_ascii_strcasecmp (hildon_entry_get_text (HILDON_ENTRY (ppriv->user_entry)), "")) {
225 priv->missing_data = TRUE;
227 priv->missing_data = FALSE;
230 if (!priv->missing_data &&
232 !modest_number_editor_is_valid (MODEST_NUMBER_EDITOR (ppriv->port_view)))
233 priv->missing_data = TRUE;
235 /* Emit a signal to notify if mandatory data is missing */
236 g_signal_emit_by_name (G_OBJECT (self), "missing_mandatory_data",
237 priv->missing_data, NULL);
241 on_valid_changed (ModestNumberEditor *editor,
243 ModestSecurityOptionsView *self)
245 on_entry_changed (NULL, (gpointer) self);
249 create_outgoing_security (ModestSecurityOptionsView* self,
250 GtkSizeGroup *title_size_group,
251 GtkSizeGroup *value_size_group)
253 ModestSecurityOptionsViewPrivate *ppriv;
254 GtkWidget *user_caption = NULL;
255 GtkWidget *pwd_caption = NULL, *port_caption = NULL;
257 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
259 /* The secure connection widgets */
260 ppriv->security_view = GTK_WIDGET (modest_serversecurity_picker_new (MODEST_EDITABLE_SIZE,
261 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
262 modest_serversecurity_picker_fill (MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
263 MODEST_PROTOCOLS_TRANSPORT_SMTP);
264 modest_maemo_utils_set_hbutton_layout (title_size_group,
266 _("mcen_li_emailsetup_secure_connection"),
267 ppriv->security_view);
269 /* The secure authentication widgets */
270 ppriv->auth_view = GTK_WIDGET (modest_secureauth_picker_new (MODEST_EDITABLE_SIZE,
271 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
272 modest_maemo_utils_set_hbutton_layout (title_size_group,
274 _("mcen_li_emailsetup_secure_authentication"),
280 /* Username widgets */
281 ppriv->user_entry = GTK_WIDGET (modest_validating_entry_new ());
283 /* Auto-capitalization is the default, so let's turn it off: */
284 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->user_entry),
285 HILDON_GTK_INPUT_MODE_FULL);
287 user_label = g_strdup_printf("%s*", _("mail_fi_username"));
288 user_caption = modest_maemo_utils_create_captioned (title_size_group,
295 /* Prevent the use of some characters. Limit the max
297 modest_validating_entry_set_unallowed_characters_whitespace (
298 MODEST_VALIDATING_ENTRY (ppriv->user_entry));
299 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
300 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
303 /* Password widgets */
304 ppriv->pwd_entry = hildon_entry_new (MODEST_EDITABLE_SIZE);
306 /* Auto-capitalization is the default, so let's turn it off */
307 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
308 HILDON_GTK_INPUT_MODE_FULL |
309 HILDON_GTK_INPUT_MODE_INVISIBLE);
310 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
312 pwd_caption = modest_maemo_utils_create_captioned (title_size_group,
314 _("mail_fi_password"),
318 ppriv->port_view = GTK_WIDGET (modest_number_editor_new (PORT_MIN, PORT_MAX));
319 port_caption = modest_maemo_utils_create_captioned (title_size_group,
321 _("mcen_fi_emailsetup_port"),
326 /* Track changes in UI */
327 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
328 G_CALLBACK (on_security_changed), self);
330 g_signal_connect (G_OBJECT (ppriv->auth_view), "value-changed",
331 G_CALLBACK (on_auth_changed), self);
332 g_signal_connect (G_OBJECT (ppriv->user_entry), "changed",
333 G_CALLBACK (on_entry_changed), self);
334 g_signal_connect (G_OBJECT (ppriv->port_view), "valid-changed",
335 G_CALLBACK (on_valid_changed), self);
338 /* Initialize widgets */
339 modest_serversecurity_picker_set_active_serversecurity (
340 MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
341 MODEST_PROTOCOLS_CONNECTION_NONE);
342 modest_secureauth_picker_set_active_secureauth (
343 MODEST_SECUREAUTH_PICKER (ppriv->auth_view),
344 MODEST_PROTOCOLS_AUTH_NONE);
346 /* Pack into container */
348 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, MODEST_MARGIN_HALF);
349 gtk_box_pack_start (GTK_BOX (self), user_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
350 gtk_box_pack_start (GTK_BOX (self), pwd_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
351 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, MODEST_MARGIN_HALF);
352 gtk_box_pack_start (GTK_BOX (self), port_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
354 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, MODEST_MARGIN_HALF);
355 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, MODEST_MARGIN_HALF);
360 gtk_widget_show (ppriv->pwd_entry);
361 gtk_widget_show (ppriv->user_entry);
362 gtk_widget_show (ppriv->port_view);
363 gtk_widget_show (pwd_caption);
364 gtk_widget_show (user_caption);
365 gtk_widget_show (port_caption);
367 gtk_widget_show (ppriv->security_view);
368 gtk_widget_show (ppriv->auth_view);
372 modest_maemo_security_options_view_new (ModestSecurityOptionsType type,
374 GtkSizeGroup *title_size_group,
375 GtkSizeGroup *value_size_group)
377 ModestSecurityOptionsView* self;
378 ModestSecurityOptionsViewPrivate *ppriv;
380 self = (ModestSecurityOptionsView *)
381 g_object_new (MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, NULL);
382 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
386 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
387 create_incoming_security (self, title_size_group, value_size_group);
389 create_outgoing_security (self, title_size_group, value_size_group);
391 return (GtkWidget *) self;
395 modest_security_options_view_has_missing_mandatory_data (ModestSecurityOptionsView* self)
397 ModestMaemoSecurityOptionsViewPrivate *priv;
399 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
401 return priv->missing_data;
405 modest_maemo_security_options_view_load_settings (ModestSecurityOptionsView* self,
406 ModestAccountSettings *settings)
408 ModestSecurityOptionsViewPrivate *ppriv;
409 ModestServerAccountSettings *server_settings;
412 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
414 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
415 server_settings = modest_account_settings_get_store_settings (settings);
417 server_settings = modest_account_settings_get_transport_settings (settings);
418 port_number = modest_server_account_settings_get_port (server_settings);
420 if (port_number == 0) {
421 /* Show the appropriate port number */
422 on_security_changed (ppriv->security_view,
423 MODEST_MAEMO_SECURITY_OPTIONS_VIEW (self));
424 } else if (ppriv->full) {
425 /* Keep the user-entered port-number, or the
426 * already-appropriate automatic port number */
427 modest_number_editor_set_value (MODEST_NUMBER_EDITOR (ppriv->port_view),
431 g_object_unref (server_settings);
435 modest_maemo_security_options_view_save_settings (ModestSecurityOptionsView* self,
436 ModestAccountSettings *settings)
438 ModestServerAccountSettings *server_settings;
439 ModestSecurityOptionsViewPrivate *ppriv;
442 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
444 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
445 server_settings = modest_account_settings_get_store_settings (settings);
447 server_settings = modest_account_settings_get_transport_settings (settings);
450 server_port = modest_number_editor_get_value (MODEST_NUMBER_EDITOR (ppriv->port_view));
452 server_port = modest_serversecurity_picker_get_active_serversecurity_port (MODEST_SERVERSECURITY_PICKER (ppriv->security_view));
455 modest_server_account_settings_set_port (server_settings, server_port);
458 g_object_unref (server_settings);
462 modest_maemo_security_options_view_changed (ModestSecurityOptionsView* self,
463 ModestAccountSettings *settings)
465 ModestServerAccountSettings *server_settings;
466 ModestSecurityOptionsViewPrivate *ppriv;
469 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
471 /* If we're not showing the port number then it never changes */
475 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
476 server_settings = modest_account_settings_get_store_settings (settings);
478 server_settings = modest_account_settings_get_transport_settings (settings);
481 modest_number_editor_get_value (MODEST_NUMBER_EDITOR (ppriv->port_view));
484 g_object_unref (server_settings);
486 if (server_port != ppriv->initial_state.port)
493 modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj)
498 modest_maemo_security_options_view_finalize (GObject *obj)
500 G_OBJECT_CLASS (modest_maemo_security_options_view_parent_class)->finalize (obj);
505 modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass)
507 GObjectClass *gobject_class = (GObjectClass*) klass;
509 modest_maemo_security_options_view_parent_class = g_type_class_peek_parent (klass);
511 g_type_class_add_private (gobject_class, sizeof (ModestMaemoSecurityOptionsViewPrivate));
512 gobject_class->finalize = modest_maemo_security_options_view_finalize;
514 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->load_settings =
515 modest_maemo_security_options_view_load_settings;
516 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->save_settings =
517 modest_maemo_security_options_view_save_settings;
518 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->changed =
519 modest_maemo_security_options_view_changed;