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 static void on_valid_changed (ModestNumberEditor *editor, gboolean valid, ModestSecurityOptionsView *self);
70 /* Tracks changes in the incoming security picker */
72 on_security_changed (GtkWidget *widget,
73 ModestMaemoSecurityOptionsView *self)
75 ModestSecurityOptionsViewPrivate* ppriv;
76 ModestServersecurityPicker *picker;
77 ModestProtocolType proto_type;
78 ModestProtocolRegistry *proto_registry;
81 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
83 proto_registry = modest_runtime_get_protocol_registry ();
84 picker = MODEST_SERVERSECURITY_PICKER (ppriv->security_view);
85 proto_type = modest_serversecurity_picker_get_active_serversecurity (picker);
87 is_secure = modest_protocol_registry_protocol_type_has_tag (proto_registry, proto_type,
88 MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS);
90 if (MODEST_SECURITY_OPTIONS_VIEW (self)->type == MODEST_SECURITY_OPTIONS_INCOMING) {
91 /* Activate and dim checkbutton if it's secure */
92 hildon_check_button_set_active (HILDON_CHECK_BUTTON (ppriv->auth_view),
94 gtk_widget_set_sensitive (ppriv->auth_view, !is_secure);
101 modest_serversecurity_picker_get_active_serversecurity_port (MODEST_SERVERSECURITY_PICKER (ppriv->security_view));
104 modest_number_editor_set_value (MODEST_NUMBER_EDITOR (ppriv->port_view),
111 on_auth_changed (GtkWidget *widget,
112 ModestMaemoSecurityOptionsView *self)
114 ModestSecurityOptionsViewPrivate* ppriv;
115 ModestSecureauthPicker *picker;
116 ModestProtocolRegistry *protocol_registry;
117 ModestProtocolType auth_proto;
118 gboolean secureauth_used;
119 GtkWidget *user_caption, *pwd_caption;
121 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
122 protocol_registry = modest_runtime_get_protocol_registry ();
123 picker = MODEST_SECUREAUTH_PICKER (ppriv->auth_view);
125 auth_proto = modest_secureauth_picker_get_active_secureauth (picker);
126 secureauth_used = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
129 /* Get captions, well dimm the whole widget */
130 user_caption = gtk_widget_get_parent (ppriv->user_entry);
131 pwd_caption = gtk_widget_get_parent (ppriv->pwd_entry);
133 /* Enable / disable */
134 gtk_widget_set_sensitive (user_caption, secureauth_used);
135 gtk_widget_set_sensitive (pwd_caption, secureauth_used);
137 /* Check if mandatory data is missing */
138 on_entry_changed (GTK_EDITABLE (ppriv->user_entry), (gpointer) self);
142 create_incoming_security (ModestSecurityOptionsView* self,
143 GtkSizeGroup *title_size_group,
144 GtkSizeGroup *value_size_group)
146 ModestSecurityOptionsViewPrivate *ppriv;
147 GtkWidget *entry_caption = NULL;
149 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
151 /* Create widgets for incoming security */
152 ppriv->security_view = GTK_WIDGET (modest_serversecurity_picker_new (MODEST_EDITABLE_SIZE,
153 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
154 modest_serversecurity_picker_fill (MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
155 modest_protocol_registry_get_pop_type_id ());
156 modest_maemo_utils_set_hbutton_layout (title_size_group,
158 _("mcen_li_emailsetup_secure_connection"),
159 ppriv->security_view);
161 ppriv->auth_view = hildon_check_button_new (MODEST_EDITABLE_SIZE);
162 gtk_button_set_label (GTK_BUTTON (ppriv->auth_view), _("mcen_li_emailsetup_secure_authentication"));
163 gtk_button_set_alignment (GTK_BUTTON (ppriv->auth_view), 0.0, 0.5);
165 /* Track changes in UI */
166 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
167 G_CALLBACK (on_security_changed), self);
169 /* Pack into container & show */
170 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
171 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
172 gtk_widget_show (ppriv->security_view);
173 gtk_widget_show (ppriv->auth_view);
176 ppriv->port_view = GTK_WIDGET (modest_number_editor_new (PORT_MIN, PORT_MAX));
178 modest_maemo_utils_create_captioned_with_size_type (title_size_group,
180 _("mcen_fi_emailsetup_port"),
183 MODEST_EDITABLE_SIZE);
184 /* Pack & show widgets */
185 gtk_box_pack_start (GTK_BOX (self), entry_caption, FALSE, FALSE, 0);
186 gtk_widget_show (ppriv->port_view);
187 gtk_widget_show (entry_caption);
189 /* Track changes in UI */
190 g_signal_connect (G_OBJECT (ppriv->port_view), "valid-changed",
191 G_CALLBACK (on_valid_changed), self);
196 on_entry_max (ModestValidatingEntry *self,
199 modest_platform_information_banner (GTK_WIDGET (self), NULL,
200 _CS("ckdg_ib_maximum_characters_reached"));
204 * TODO: call this whenever the auth picker changes. If we set it
205 * explicitely at the beggining to a value then there is no need to
206 * call this handler directly at the beginning
209 on_entry_changed (GtkEditable *editable,
212 ModestSecurityOptionsView* self;
213 ModestMaemoSecurityOptionsViewPrivate *priv;
214 ModestSecurityOptionsViewPrivate *ppriv;
215 ModestSecureauthPicker *picker;
216 gboolean is_secure = FALSE;
217 ModestProtocolRegistry *protocol_registry;
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 ();
224 /* Check if it's a secure protocol */
225 if (MODEST_IS_SECUREAUTH_PICKER (ppriv->auth_view)) {
226 ModestProtocolType auth_proto;
227 picker = MODEST_SECUREAUTH_PICKER (ppriv->auth_view);
228 auth_proto = modest_secureauth_picker_get_active_secureauth (picker);
229 is_secure = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
231 } else if (HILDON_IS_CHECK_BUTTON (ppriv->auth_view)) {
232 is_secure = hildon_check_button_get_active (HILDON_CHECK_BUTTON (ppriv->auth_view));
236 !g_strcmp0 (hildon_entry_get_text (HILDON_ENTRY (ppriv->user_entry)), "")) {
237 priv->missing_data = TRUE;
239 priv->missing_data = FALSE;
242 if (!priv->missing_data &&
244 !modest_number_editor_is_valid (MODEST_NUMBER_EDITOR (ppriv->port_view)))
245 priv->missing_data = TRUE;
247 /* Emit a signal to notify if mandatory data is missing */
248 g_signal_emit_by_name (G_OBJECT (self), "missing_mandatory_data",
249 priv->missing_data, NULL);
253 on_valid_changed (ModestNumberEditor *editor,
255 ModestSecurityOptionsView *self)
257 on_entry_changed (NULL, (gpointer) self);
261 create_outgoing_security (ModestSecurityOptionsView* self,
262 GtkSizeGroup *title_size_group,
263 GtkSizeGroup *value_size_group)
265 ModestSecurityOptionsViewPrivate *ppriv;
266 GtkWidget *user_caption = NULL;
267 GtkWidget *pwd_caption = NULL, *port_caption = NULL;
269 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
271 /* The secure connection widgets */
272 ppriv->security_view = GTK_WIDGET (modest_serversecurity_picker_new (MODEST_EDITABLE_SIZE,
273 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
274 modest_serversecurity_picker_fill (MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
275 MODEST_PROTOCOLS_TRANSPORT_SMTP);
276 modest_maemo_utils_set_hbutton_layout (title_size_group,
278 _("mcen_li_emailsetup_secure_connection"),
279 ppriv->security_view);
281 /* The secure authentication widgets */
282 ppriv->auth_view = GTK_WIDGET (modest_secureauth_picker_new (MODEST_EDITABLE_SIZE,
283 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
284 modest_maemo_utils_set_hbutton_layout (title_size_group,
286 _("mcen_li_emailsetup_secure_authentication"),
292 /* Username widgets */
293 ppriv->user_entry = GTK_WIDGET (modest_validating_entry_new ());
295 /* Auto-capitalization is the default, so let's turn it off: */
296 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->user_entry),
297 HILDON_GTK_INPUT_MODE_FULL);
299 user_label = g_strdup_printf("%s*", _("mail_fi_username"));
300 user_caption = modest_maemo_utils_create_captioned_with_size_type (title_size_group,
305 MODEST_EDITABLE_SIZE);
308 /* Prevent the use of some characters. Limit the max
310 modest_validating_entry_set_unallowed_characters_whitespace (
311 MODEST_VALIDATING_ENTRY (ppriv->user_entry));
312 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
313 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
316 /* Password widgets */
317 ppriv->pwd_entry = hildon_entry_new (MODEST_EDITABLE_SIZE);
319 /* Auto-capitalization is the default, so let's turn it off */
320 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
321 HILDON_GTK_INPUT_MODE_FULL |
322 HILDON_GTK_INPUT_MODE_INVISIBLE);
323 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
326 modest_maemo_utils_create_captioned_with_size_type (title_size_group,
328 _("mail_fi_password"),
331 MODEST_EDITABLE_SIZE);
333 ppriv->port_view = GTK_WIDGET (modest_number_editor_new (PORT_MIN, PORT_MAX));
335 modest_maemo_utils_create_captioned_with_size_type (title_size_group,
337 _("mcen_fi_emailsetup_port"),
340 MODEST_EDITABLE_SIZE);
343 /* Track changes in UI */
344 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
345 G_CALLBACK (on_security_changed), self);
347 g_signal_connect (G_OBJECT (ppriv->auth_view), "value-changed",
348 G_CALLBACK (on_auth_changed), self);
349 g_signal_connect (G_OBJECT (ppriv->user_entry), "changed",
350 G_CALLBACK (on_entry_changed), self);
351 g_signal_connect (G_OBJECT (ppriv->port_view), "valid-changed",
352 G_CALLBACK (on_valid_changed), self);
355 /* Initialize widgets */
356 modest_serversecurity_picker_set_active_serversecurity (
357 MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
358 MODEST_PROTOCOLS_CONNECTION_NONE);
359 modest_secureauth_picker_set_active_secureauth (
360 MODEST_SECUREAUTH_PICKER (ppriv->auth_view),
361 MODEST_PROTOCOLS_AUTH_NONE);
363 /* Pack into container */
365 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
366 gtk_box_pack_start (GTK_BOX (self), user_caption, FALSE, FALSE, 0);
367 gtk_box_pack_start (GTK_BOX (self), pwd_caption, FALSE, FALSE, 0);
368 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
369 gtk_box_pack_start (GTK_BOX (self), port_caption, FALSE, FALSE, 0);
371 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
372 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
377 gtk_widget_show (ppriv->pwd_entry);
378 gtk_widget_show (ppriv->user_entry);
379 gtk_widget_show (ppriv->port_view);
380 gtk_widget_show (pwd_caption);
381 gtk_widget_show (user_caption);
382 gtk_widget_show (port_caption);
384 gtk_widget_show (ppriv->security_view);
385 gtk_widget_show (ppriv->auth_view);
389 modest_maemo_security_options_view_new (ModestSecurityOptionsType type,
391 GtkSizeGroup *title_size_group,
392 GtkSizeGroup *value_size_group)
394 ModestSecurityOptionsView* self;
395 ModestSecurityOptionsViewPrivate *ppriv;
397 self = (ModestSecurityOptionsView *)
398 g_object_new (MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, NULL);
399 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
403 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
404 create_incoming_security (self, title_size_group, value_size_group);
406 create_outgoing_security (self, title_size_group, value_size_group);
408 return (GtkWidget *) self;
412 modest_security_options_view_has_missing_mandatory_data (ModestSecurityOptionsView* self)
414 ModestMaemoSecurityOptionsViewPrivate *priv;
416 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
418 return priv->missing_data;
422 modest_maemo_security_options_view_load_settings (ModestSecurityOptionsView* self,
423 ModestAccountSettings *settings)
425 ModestSecurityOptionsViewPrivate *ppriv;
426 ModestServerAccountSettings *server_settings;
429 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
431 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
432 server_settings = modest_account_settings_get_store_settings (settings);
434 server_settings = modest_account_settings_get_transport_settings (settings);
435 port_number = modest_server_account_settings_get_port (server_settings);
437 if (port_number == 0) {
438 /* Show the appropriate port number */
439 on_security_changed (ppriv->security_view,
440 MODEST_MAEMO_SECURITY_OPTIONS_VIEW (self));
441 } else if (ppriv->full) {
442 /* Keep the user-entered port-number, or the
443 * already-appropriate automatic port number */
444 modest_number_editor_set_value (MODEST_NUMBER_EDITOR (ppriv->port_view),
448 g_object_unref (server_settings);
452 modest_maemo_security_options_view_save_settings (ModestSecurityOptionsView* self,
453 ModestAccountSettings *settings)
455 ModestServerAccountSettings *server_settings;
456 ModestSecurityOptionsViewPrivate *ppriv;
459 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
461 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
462 server_settings = modest_account_settings_get_store_settings (settings);
464 server_settings = modest_account_settings_get_transport_settings (settings);
467 server_port = modest_number_editor_get_value (MODEST_NUMBER_EDITOR (ppriv->port_view));
469 server_port = modest_serversecurity_picker_get_active_serversecurity_port (MODEST_SERVERSECURITY_PICKER (ppriv->security_view));
472 modest_server_account_settings_set_port (server_settings, server_port);
475 g_object_unref (server_settings);
479 modest_maemo_security_options_view_changed (ModestSecurityOptionsView* self,
480 ModestAccountSettings *settings)
482 ModestServerAccountSettings *server_settings;
483 ModestSecurityOptionsViewPrivate *ppriv;
486 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
488 /* If we're not showing the port number then it never changes */
492 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
493 server_settings = modest_account_settings_get_store_settings (settings);
495 server_settings = modest_account_settings_get_transport_settings (settings);
498 modest_number_editor_get_value (MODEST_NUMBER_EDITOR (ppriv->port_view));
501 g_object_unref (server_settings);
503 if (server_port != ppriv->initial_state.port)
510 modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj)
515 modest_maemo_security_options_view_finalize (GObject *obj)
517 G_OBJECT_CLASS (modest_maemo_security_options_view_parent_class)->finalize (obj);
522 modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass)
524 GObjectClass *gobject_class = (GObjectClass*) klass;
526 modest_maemo_security_options_view_parent_class = g_type_class_peek_parent (klass);
528 g_type_class_add_private (gobject_class, sizeof (ModestMaemoSecurityOptionsViewPrivate));
529 gobject_class->finalize = modest_maemo_security_options_view_finalize;
531 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->load_settings =
532 modest_maemo_security_options_view_load_settings;
533 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->save_settings =
534 modest_maemo_security_options_view_save_settings;
535 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->changed =
536 modest_maemo_security_options_view_changed;