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));
176 modest_maemo_utils_create_captioned_with_size_type (title_size_group,
178 _("mcen_fi_emailsetup_port"),
181 MODEST_EDITABLE_SIZE);
182 /* Pack & show widgets */
183 gtk_box_pack_start (GTK_BOX (self), entry_caption, FALSE, FALSE, 0);
184 gtk_widget_show (ppriv->port_view);
185 gtk_widget_show (entry_caption);
190 on_entry_max (ModestValidatingEntry *self,
193 modest_platform_information_banner (GTK_WIDGET (self), NULL,
194 _CS("ckdg_ib_maximum_characters_reached"));
198 * TODO: call this whenever the auth picker 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
203 on_entry_changed (GtkEditable *editable,
206 ModestSecurityOptionsView* self;
207 ModestMaemoSecurityOptionsViewPrivate *priv;
208 ModestSecurityOptionsViewPrivate *ppriv;
209 ModestProtocolType auth_proto;
210 ModestSecureauthPicker *picker;
212 ModestProtocolRegistry *protocol_registry;
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 ();
219 /* Outgoing username is mandatory if outgoing auth is secure */
220 picker = MODEST_SECUREAUTH_PICKER (ppriv->auth_view);
221 auth_proto = modest_secureauth_picker_get_active_secureauth (picker);
222 is_secure = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
226 !g_ascii_strcasecmp (hildon_entry_get_text (HILDON_ENTRY (ppriv->user_entry)), "")) {
227 priv->missing_data = TRUE;
229 priv->missing_data = FALSE;
232 if (!priv->missing_data &&
234 !modest_number_editor_is_valid (MODEST_NUMBER_EDITOR (ppriv->port_view)))
235 priv->missing_data = TRUE;
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);
243 on_valid_changed (ModestNumberEditor *editor,
245 ModestSecurityOptionsView *self)
247 on_entry_changed (NULL, (gpointer) self);
251 create_outgoing_security (ModestSecurityOptionsView* self,
252 GtkSizeGroup *title_size_group,
253 GtkSizeGroup *value_size_group)
255 ModestSecurityOptionsViewPrivate *ppriv;
256 GtkWidget *user_caption = NULL;
257 GtkWidget *pwd_caption = NULL, *port_caption = NULL;
259 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
261 /* The secure connection widgets */
262 ppriv->security_view = GTK_WIDGET (modest_serversecurity_picker_new (MODEST_EDITABLE_SIZE,
263 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
264 modest_serversecurity_picker_fill (MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
265 MODEST_PROTOCOLS_TRANSPORT_SMTP);
266 modest_maemo_utils_set_hbutton_layout (title_size_group,
268 _("mcen_li_emailsetup_secure_connection"),
269 ppriv->security_view);
271 /* The secure authentication widgets */
272 ppriv->auth_view = GTK_WIDGET (modest_secureauth_picker_new (MODEST_EDITABLE_SIZE,
273 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
274 modest_maemo_utils_set_hbutton_layout (title_size_group,
276 _("mcen_li_emailsetup_secure_authentication"),
282 /* Username widgets */
283 ppriv->user_entry = GTK_WIDGET (modest_validating_entry_new ());
285 /* Auto-capitalization is the default, so let's turn it off: */
286 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->user_entry),
287 HILDON_GTK_INPUT_MODE_FULL);
289 user_label = g_strdup_printf("%s*", _("mail_fi_username"));
290 user_caption = modest_maemo_utils_create_captioned_with_size_type (title_size_group,
295 MODEST_EDITABLE_SIZE);
298 /* Prevent the use of some characters. Limit the max
300 modest_validating_entry_set_unallowed_characters_whitespace (
301 MODEST_VALIDATING_ENTRY (ppriv->user_entry));
302 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
303 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
306 /* Password widgets */
307 ppriv->pwd_entry = hildon_entry_new (MODEST_EDITABLE_SIZE);
309 /* Auto-capitalization is the default, so let's turn it off */
310 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
311 HILDON_GTK_INPUT_MODE_FULL |
312 HILDON_GTK_INPUT_MODE_INVISIBLE);
313 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
316 modest_maemo_utils_create_captioned_with_size_type (title_size_group,
318 _("mail_fi_password"),
321 MODEST_EDITABLE_SIZE);
323 ppriv->port_view = GTK_WIDGET (modest_number_editor_new (PORT_MIN, PORT_MAX));
325 modest_maemo_utils_create_captioned_with_size_type (title_size_group,
327 _("mcen_fi_emailsetup_port"),
330 MODEST_EDITABLE_SIZE);
333 /* Track changes in UI */
334 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
335 G_CALLBACK (on_security_changed), self);
337 g_signal_connect (G_OBJECT (ppriv->auth_view), "value-changed",
338 G_CALLBACK (on_auth_changed), self);
339 g_signal_connect (G_OBJECT (ppriv->user_entry), "changed",
340 G_CALLBACK (on_entry_changed), self);
341 g_signal_connect (G_OBJECT (ppriv->port_view), "valid-changed",
342 G_CALLBACK (on_valid_changed), self);
345 /* Initialize widgets */
346 modest_serversecurity_picker_set_active_serversecurity (
347 MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
348 MODEST_PROTOCOLS_CONNECTION_NONE);
349 modest_secureauth_picker_set_active_secureauth (
350 MODEST_SECUREAUTH_PICKER (ppriv->auth_view),
351 MODEST_PROTOCOLS_AUTH_NONE);
353 /* Pack into container */
355 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
356 gtk_box_pack_start (GTK_BOX (self), user_caption, FALSE, FALSE, 0);
357 gtk_box_pack_start (GTK_BOX (self), pwd_caption, FALSE, FALSE, 0);
358 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
359 gtk_box_pack_start (GTK_BOX (self), port_caption, FALSE, FALSE, 0);
361 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
362 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
367 gtk_widget_show (ppriv->pwd_entry);
368 gtk_widget_show (ppriv->user_entry);
369 gtk_widget_show (ppriv->port_view);
370 gtk_widget_show (pwd_caption);
371 gtk_widget_show (user_caption);
372 gtk_widget_show (port_caption);
374 gtk_widget_show (ppriv->security_view);
375 gtk_widget_show (ppriv->auth_view);
379 modest_maemo_security_options_view_new (ModestSecurityOptionsType type,
381 GtkSizeGroup *title_size_group,
382 GtkSizeGroup *value_size_group)
384 ModestSecurityOptionsView* self;
385 ModestSecurityOptionsViewPrivate *ppriv;
387 self = (ModestSecurityOptionsView *)
388 g_object_new (MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, NULL);
389 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
393 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
394 create_incoming_security (self, title_size_group, value_size_group);
396 create_outgoing_security (self, title_size_group, value_size_group);
398 return (GtkWidget *) self;
402 modest_security_options_view_has_missing_mandatory_data (ModestSecurityOptionsView* self)
404 ModestMaemoSecurityOptionsViewPrivate *priv;
406 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
408 return priv->missing_data;
412 modest_maemo_security_options_view_load_settings (ModestSecurityOptionsView* self,
413 ModestAccountSettings *settings)
415 ModestSecurityOptionsViewPrivate *ppriv;
416 ModestServerAccountSettings *server_settings;
419 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
421 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
422 server_settings = modest_account_settings_get_store_settings (settings);
424 server_settings = modest_account_settings_get_transport_settings (settings);
425 port_number = modest_server_account_settings_get_port (server_settings);
427 if (port_number == 0) {
428 /* Show the appropriate port number */
429 on_security_changed (ppriv->security_view,
430 MODEST_MAEMO_SECURITY_OPTIONS_VIEW (self));
431 } else if (ppriv->full) {
432 /* Keep the user-entered port-number, or the
433 * already-appropriate automatic port number */
434 modest_number_editor_set_value (MODEST_NUMBER_EDITOR (ppriv->port_view),
438 g_object_unref (server_settings);
442 modest_maemo_security_options_view_save_settings (ModestSecurityOptionsView* self,
443 ModestAccountSettings *settings)
445 ModestServerAccountSettings *server_settings;
446 ModestSecurityOptionsViewPrivate *ppriv;
449 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
451 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
452 server_settings = modest_account_settings_get_store_settings (settings);
454 server_settings = modest_account_settings_get_transport_settings (settings);
457 server_port = modest_number_editor_get_value (MODEST_NUMBER_EDITOR (ppriv->port_view));
459 server_port = modest_serversecurity_picker_get_active_serversecurity_port (MODEST_SERVERSECURITY_PICKER (ppriv->security_view));
462 modest_server_account_settings_set_port (server_settings, server_port);
465 g_object_unref (server_settings);
469 modest_maemo_security_options_view_changed (ModestSecurityOptionsView* self,
470 ModestAccountSettings *settings)
472 ModestServerAccountSettings *server_settings;
473 ModestSecurityOptionsViewPrivate *ppriv;
476 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
478 /* If we're not showing the port number then it never changes */
482 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
483 server_settings = modest_account_settings_get_store_settings (settings);
485 server_settings = modest_account_settings_get_transport_settings (settings);
488 modest_number_editor_get_value (MODEST_NUMBER_EDITOR (ppriv->port_view));
491 g_object_unref (server_settings);
493 if (server_port != ppriv->initial_state.port)
500 modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj)
505 modest_maemo_security_options_view_finalize (GObject *obj)
507 G_OBJECT_CLASS (modest_maemo_security_options_view_parent_class)->finalize (obj);
512 modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass)
514 GObjectClass *gobject_class = (GObjectClass*) klass;
516 modest_maemo_security_options_view_parent_class = g_type_class_peek_parent (klass);
518 g_type_class_add_private (gobject_class, sizeof (ModestMaemoSecurityOptionsViewPrivate));
519 gobject_class->finalize = modest_maemo_security_options_view_finalize;
521 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->load_settings =
522 modest_maemo_security_options_view_load_settings;
523 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->save_settings =
524 modest_maemo_security_options_view_save_settings;
525 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->changed =
526 modest_maemo_security_options_view_changed;