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-maemo-utils.h"
39 #include "modest-hildon-includes.h"
42 #define PORT_MAX 65535
44 typedef struct _ModestMaemoSecurityOptionsViewPrivate ModestMaemoSecurityOptionsViewPrivate;
45 struct _ModestMaemoSecurityOptionsViewPrivate {
46 gboolean missing_data;
49 #define MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE(o) \
50 (G_TYPE_INSTANCE_GET_PRIVATE((o), \
51 MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, \
52 ModestMaemoSecurityOptionsViewPrivate))
54 static void modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj);
55 static void modest_maemo_security_options_view_finalize (GObject *obj);
56 static void modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass);
58 G_DEFINE_TYPE (ModestMaemoSecurityOptionsView,
59 modest_maemo_security_options_view,
60 MODEST_TYPE_SECURITY_OPTIONS_VIEW);
62 static void on_entry_changed (GtkEditable *editable, gpointer user_data);
64 #ifdef MODEST_NUMBER_ENTRY_SUPPORT_VALID_CHANGED
65 static void on_valid_changed (ModestNumberEditor *editor, gboolean valid, ModestSecurityOptionsView *self);
68 /* Tracks changes in the incoming security picker */
70 on_security_changed (GtkWidget *widget,
71 ModestMaemoSecurityOptionsView *self)
73 ModestSecurityOptionsViewPrivate* ppriv;
74 ModestProtocolType proto_type;
75 ModestProtocolRegistry *proto_registry;
78 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
80 proto_registry = modest_runtime_get_protocol_registry ();
81 proto_type = modest_serversecurity_selector_get_active_serversecurity (ppriv->security_view);
83 is_secure = modest_protocol_registry_protocol_type_has_tag (proto_registry, proto_type,
84 MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS);
86 if (MODEST_SECURITY_OPTIONS_VIEW (self)->type == MODEST_SECURITY_OPTIONS_INCOMING) {
87 /* Activate and dim checkbutton if it's secure */
88 modest_togglable_set_active (ppriv->auth_view,
90 gtk_widget_set_sensitive (ppriv->auth_view, !is_secure);
97 modest_serversecurity_selector_get_active_serversecurity_port (ppriv->security_view);
100 modest_number_entry_set_value (ppriv->port_view,
107 on_auth_changed (GtkWidget *widget,
108 ModestMaemoSecurityOptionsView *self)
110 ModestSecurityOptionsViewPrivate* ppriv;
111 ModestProtocolRegistry *protocol_registry;
112 ModestProtocolType auth_proto;
113 gboolean secureauth_used;
114 GtkWidget *user_caption, *pwd_caption;
116 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
117 protocol_registry = modest_runtime_get_protocol_registry ();
119 auth_proto = modest_secureauth_selector_get_active_secureauth (ppriv->auth_view);
120 secureauth_used = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
123 /* Get captions, well dimm the whole widget */
124 user_caption = gtk_widget_get_parent (ppriv->user_entry);
125 pwd_caption = gtk_widget_get_parent (ppriv->pwd_entry);
127 /* Enable / disable */
128 gtk_widget_set_sensitive (user_caption, secureauth_used);
129 gtk_widget_set_sensitive (pwd_caption, secureauth_used);
131 /* Check if mandatory data is missing */
132 on_entry_changed (GTK_EDITABLE (ppriv->user_entry), (gpointer) self);
136 create_incoming_security (ModestSecurityOptionsView* self,
137 GtkSizeGroup *title_size_group,
138 GtkSizeGroup *value_size_group)
140 ModestSecurityOptionsViewPrivate *ppriv;
141 GtkWidget *entry_caption = NULL;
143 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
145 ppriv->auth_view = modest_toolkit_factory_create_check_button (modest_runtime_get_toolkit_factory (),
146 _("mcen_li_emailsetup_secure_authentication"));
147 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
149 /* Create widgets for incoming security */
150 ppriv->security_view = modest_toolkit_factory_create_serversecurity_selector (modest_runtime_get_toolkit_factory ());
151 modest_serversecurity_selector_fill (ppriv->security_view,
152 modest_protocol_registry_get_pop_type_id ());
153 if (GTK_IS_COMBO_BOX (ppriv->security_view)) {
154 GtkWidget *captioned;
156 captioned = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
157 _("mcen_li_emailsetup_secure_connection"), FALSE,
158 ppriv->security_view);
159 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
160 G_CALLBACK (on_security_changed), self);
161 gtk_box_pack_start (GTK_BOX (self), captioned, FALSE, FALSE, 0);
162 gtk_widget_show (captioned);
164 modest_maemo_utils_set_hbutton_layout (title_size_group,
166 _("mcen_li_emailsetup_secure_connection"),
167 ppriv->security_view);
168 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
169 G_CALLBACK (on_security_changed), self);
170 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
173 /* Pack into container & show */
174 gtk_widget_show (ppriv->security_view);
175 gtk_widget_show (ppriv->auth_view);
178 ppriv->port_view = modest_toolkit_factory_create_number_entry (modest_runtime_get_toolkit_factory (),
181 modest_maemo_utils_create_captioned_with_size_type (title_size_group,
183 _("mcen_fi_emailsetup_port"),
186 MODEST_EDITABLE_SIZE);
187 /* Pack & show widgets */
188 gtk_box_pack_start (GTK_BOX (self), entry_caption, FALSE, FALSE, 0);
189 gtk_widget_show (ppriv->port_view);
190 gtk_widget_show (entry_caption);
192 /* Track changes in UI */
193 #ifdef MODEST_NUMBER_ENTRY_SUPPORT_VALID_CHANGED
194 g_signal_connect (G_OBJECT (ppriv->port_view), "valid-changed",
195 G_CALLBACK (on_valid_changed), self);
201 on_entry_max (ModestValidatingEntry *self,
204 modest_platform_information_banner (GTK_WIDGET (self), NULL,
205 _CS("ckdg_ib_maximum_characters_reached"));
209 * TODO: call this whenever the auth picker changes. If we set it
210 * explicitely at the beggining to a value then there is no need to
211 * call this handler directly at the beginning
214 on_entry_changed (GtkEditable *editable,
217 ModestSecurityOptionsView* self;
218 ModestMaemoSecurityOptionsViewPrivate *priv;
219 ModestSecurityOptionsViewPrivate *ppriv;
220 gboolean is_secure = FALSE;
221 ModestProtocolRegistry *protocol_registry;
223 self = MODEST_SECURITY_OPTIONS_VIEW (user_data);
224 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
225 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
226 protocol_registry = modest_runtime_get_protocol_registry ();
228 /* Check if it's a secure protocol */
229 if (modest_is_secureauth_selector (ppriv->auth_view)) {
230 ModestProtocolType auth_proto;
231 auth_proto = modest_secureauth_selector_get_active_secureauth (ppriv->auth_view);
232 is_secure = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
234 } else if (modest_is_togglable (ppriv->auth_view)) {
235 is_secure = modest_togglable_get_active (ppriv->auth_view);
239 !g_strcmp0 (modest_entry_get_text (ppriv->user_entry), "")) {
240 priv->missing_data = TRUE;
242 priv->missing_data = FALSE;
245 if (!priv->missing_data &&
247 !modest_number_entry_is_valid (ppriv->port_view))
248 priv->missing_data = TRUE;
250 /* Emit a signal to notify if mandatory data is missing */
251 g_signal_emit_by_name (G_OBJECT (self), "missing_mandatory_data",
252 priv->missing_data, NULL);
255 #ifdef MODEST_NUMBER_ENTRY_SUPPORT_VALID_CHANGED
257 on_valid_changed (ModestNumberEditor *editor,
259 ModestSecurityOptionsView *self)
261 on_entry_changed (NULL, (gpointer) self);
266 create_outgoing_security (ModestSecurityOptionsView* self,
267 GtkSizeGroup *title_size_group,
268 GtkSizeGroup *value_size_group)
270 ModestSecurityOptionsViewPrivate *ppriv;
271 GtkWidget *user_caption = NULL, *security_caption = NULL, *auth_caption = NULL;
272 GtkWidget *pwd_caption = NULL, *port_caption = NULL;
274 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
276 /* The secure connection widgets */
277 ppriv->security_view = modest_toolkit_factory_create_serversecurity (modest_runtime_get_toolkit_factory ());
278 modest_serversecurity_selector_fill (ppriv->security_view,
279 MODEST_PROTOCOLS_TRANSPORT_SMTP);
280 if (GTK_IS_COMBO_BOX (ppriv->security_view)) {
281 security_caption = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
282 _("mcen_li_emailsetup_secure_connection"), FALSE,
283 ppriv->security_view);
284 gtk_widget_show (security_caption);
286 modest_maemo_utils_set_hbutton_layout (title_size_group,
288 _("mcen_li_emailsetup_secure_connection"),
289 ppriv->security_view);
290 security_caption = ppriv->security_view;
293 /* The secure authentication widgets */
294 ppriv->auth_view = modest_toolkit_factory_create_secureauth_selector (modest_runtime_get_toolkit_factory ());
295 if (GTK_IS_COMBO_BOX (ppriv->auth_view)) {
296 auth_caption = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
297 _("mcen_li_emailsetup_secure_authentication"), FALSE,
299 gtk_widget_show (auth_caption);
301 modest_maemo_utils_set_hbutton_layout (title_size_group,
303 _("mcen_li_emailsetup_secure_authentication"),
305 auth_caption = ppriv->auth_view;
311 /* Username widgets */
312 ppriv->user_entry = GTK_WIDGET (modest_validating_entry_new ());
314 /* Auto-capitalization is the default, so let's turn it off: */
315 #ifdef MODEST_TOOLKIT_HILDON2
316 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->user_entry),
317 HILDON_GTK_INPUT_MODE_FULL);
320 user_label = g_strdup_printf("%s*", _("mail_fi_username"));
321 user_caption = modest_maemo_utils_create_captioned_with_size_type (title_size_group,
326 MODEST_EDITABLE_SIZE);
329 /* Prevent the use of some characters. Limit the max
331 modest_validating_entry_set_unallowed_characters_whitespace (
332 MODEST_VALIDATING_ENTRY (ppriv->user_entry));
333 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
334 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
337 /* Password widgets */
338 ppriv->pwd_entry = modest_toolkit_factory_create_entry (modest_runtime_get_toolkit_factory ());
340 /* Auto-capitalization is the default, so let's turn it off */
341 #ifdef MODEST_TOOLKIT_HILDON2
342 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
343 HILDON_GTK_INPUT_MODE_FULL |
344 HILDON_GTK_INPUT_MODE_INVISIBLE);
346 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
349 modest_maemo_utils_create_captioned_with_size_type (title_size_group,
351 _("mail_fi_password"),
354 MODEST_EDITABLE_SIZE);
356 ppriv->port_view = modest_toolkit_factory_create_number_entry (modest_runtime_get_toolkit_factory (),
359 modest_maemo_utils_create_captioned_with_size_type (title_size_group,
361 _("mcen_fi_emailsetup_port"),
364 MODEST_EDITABLE_SIZE);
367 /* Track changes in UI */
368 if (GTK_IS_COMBO_BOX (ppriv->security_view)) {
369 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
370 G_CALLBACK (on_security_changed), self);
372 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
373 G_CALLBACK (on_security_changed), self);
376 if (GTK_IS_COMBO_BOX (ppriv->auth_view)) {
377 g_signal_connect (G_OBJECT (ppriv->auth_view), "changed",
378 G_CALLBACK (on_auth_changed), self);
380 g_signal_connect (G_OBJECT (ppriv->auth_view), "value-changed",
381 G_CALLBACK (on_auth_changed), self);
383 g_signal_connect (G_OBJECT (ppriv->user_entry), "changed",
384 G_CALLBACK (on_entry_changed), self);
385 #ifdef MODEST_NUMBER_ENTRY_SUPPORT_VALID_CHANGED
386 g_signal_connect (G_OBJECT (ppriv->port_view), "valid-changed",
387 G_CALLBACK (on_valid_changed), self);
391 /* Initialize widgets */
392 modest_serversecurity_selector_set_active_serversecurity (
393 ppriv->security_view,
394 MODEST_PROTOCOLS_CONNECTION_NONE);
395 modest_secureauth_selector_set_active_secureauth (
397 MODEST_PROTOCOLS_AUTH_NONE);
399 /* Pack into container */
401 gtk_box_pack_start (GTK_BOX (self), auth_caption, FALSE, FALSE, 0);
402 gtk_box_pack_start (GTK_BOX (self), user_caption, FALSE, FALSE, 0);
403 gtk_box_pack_start (GTK_BOX (self), pwd_caption, FALSE, FALSE, 0);
404 gtk_box_pack_start (GTK_BOX (self), security_caption, FALSE, FALSE, 0);
405 gtk_box_pack_start (GTK_BOX (self), port_caption, FALSE, FALSE, 0);
407 gtk_box_pack_start (GTK_BOX (self), auth_caption, FALSE, FALSE, 0);
408 gtk_box_pack_start (GTK_BOX (self), security_caption, FALSE, FALSE, 0);
413 gtk_widget_show (ppriv->pwd_entry);
414 gtk_widget_show (ppriv->user_entry);
415 gtk_widget_show (ppriv->port_view);
416 gtk_widget_show (pwd_caption);
417 gtk_widget_show (user_caption);
418 gtk_widget_show (port_caption);
420 gtk_widget_show (ppriv->security_view);
421 gtk_widget_show (ppriv->auth_view);
425 modest_maemo_security_options_view_new (ModestSecurityOptionsType type,
427 GtkSizeGroup *title_size_group,
428 GtkSizeGroup *value_size_group)
430 ModestSecurityOptionsView* self;
431 ModestSecurityOptionsViewPrivate *ppriv;
433 self = (ModestSecurityOptionsView *)
434 g_object_new (MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, NULL);
435 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
439 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
440 create_incoming_security (self, title_size_group, value_size_group);
442 create_outgoing_security (self, title_size_group, value_size_group);
444 return (GtkWidget *) self;
448 modest_security_options_view_has_missing_mandatory_data (ModestSecurityOptionsView* self)
450 ModestMaemoSecurityOptionsViewPrivate *priv;
452 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
454 return priv->missing_data;
458 modest_maemo_security_options_view_load_settings (ModestSecurityOptionsView* self,
459 ModestAccountSettings *settings)
461 ModestSecurityOptionsViewPrivate *ppriv;
462 ModestServerAccountSettings *server_settings;
465 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
467 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
468 server_settings = modest_account_settings_get_store_settings (settings);
470 server_settings = modest_account_settings_get_transport_settings (settings);
471 port_number = modest_server_account_settings_get_port (server_settings);
473 if (port_number == 0) {
474 /* Show the appropriate port number */
475 on_security_changed (ppriv->security_view,
476 MODEST_MAEMO_SECURITY_OPTIONS_VIEW (self));
477 } else if (ppriv->full) {
478 /* Keep the user-entered port-number, or the
479 * already-appropriate automatic port number */
480 modest_number_entry_set_value (ppriv->port_view,
484 g_object_unref (server_settings);
488 modest_maemo_security_options_view_save_settings (ModestSecurityOptionsView* self,
489 ModestAccountSettings *settings)
491 ModestServerAccountSettings *server_settings;
492 ModestSecurityOptionsViewPrivate *ppriv;
495 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
497 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
498 server_settings = modest_account_settings_get_store_settings (settings);
500 server_settings = modest_account_settings_get_transport_settings (settings);
503 server_port = modest_number_entry_get_value (ppriv->port_view);
505 server_port = modest_serversecurity_selector_get_active_serversecurity_port (ppriv->security_view);
508 modest_server_account_settings_set_port (server_settings, server_port);
511 g_object_unref (server_settings);
515 modest_maemo_security_options_view_changed (ModestSecurityOptionsView* self,
516 ModestAccountSettings *settings)
518 ModestServerAccountSettings *server_settings;
519 ModestSecurityOptionsViewPrivate *ppriv;
522 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
524 /* If we're not showing the port number then it never changes */
528 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
529 server_settings = modest_account_settings_get_store_settings (settings);
531 server_settings = modest_account_settings_get_transport_settings (settings);
534 modest_number_entry_get_value (ppriv->port_view);
537 g_object_unref (server_settings);
539 if (server_port != ppriv->initial_state.port)
546 modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj)
551 modest_maemo_security_options_view_finalize (GObject *obj)
553 G_OBJECT_CLASS (modest_maemo_security_options_view_parent_class)->finalize (obj);
558 modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass)
560 GObjectClass *gobject_class = (GObjectClass*) klass;
562 modest_maemo_security_options_view_parent_class = g_type_class_peek_parent (klass);
564 g_type_class_add_private (gobject_class, sizeof (ModestMaemoSecurityOptionsViewPrivate));
565 gobject_class->finalize = modest_maemo_security_options_view_finalize;
567 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->load_settings =
568 modest_maemo_security_options_view_load_settings;
569 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->save_settings =
570 modest_maemo_security_options_view_save_settings;
571 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->changed =
572 modest_maemo_security_options_view_changed;