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-secureauth-picker.h"
39 #include "modest-maemo-utils.h"
40 #include "modest-hildon-includes.h"
43 #define PORT_MAX 65535
45 typedef struct _ModestMaemoSecurityOptionsViewPrivate ModestMaemoSecurityOptionsViewPrivate;
46 struct _ModestMaemoSecurityOptionsViewPrivate {
47 gboolean missing_data;
50 #define MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE(o) \
51 (G_TYPE_INSTANCE_GET_PRIVATE((o), \
52 MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, \
53 ModestMaemoSecurityOptionsViewPrivate))
55 static void modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj);
56 static void modest_maemo_security_options_view_finalize (GObject *obj);
57 static void modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass);
59 G_DEFINE_TYPE (ModestMaemoSecurityOptionsView,
60 modest_maemo_security_options_view,
61 MODEST_TYPE_SECURITY_OPTIONS_VIEW);
63 static void on_entry_changed (GtkEditable *editable, gpointer user_data);
65 #ifdef MODEST_NUMBER_ENTRY_SUPPORT_VALID_CHANGED
66 static void on_valid_changed (ModestNumberEditor *editor, gboolean valid, ModestSecurityOptionsView *self);
69 /* Tracks changes in the incoming security picker */
71 on_security_changed (GtkWidget *widget,
72 ModestMaemoSecurityOptionsView *self)
74 ModestSecurityOptionsViewPrivate* ppriv;
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 proto_type = modest_serversecurity_selector_get_active_serversecurity (ppriv->security_view);
84 is_secure = modest_protocol_registry_protocol_type_has_tag (proto_registry, proto_type,
85 MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS);
87 if (MODEST_SECURITY_OPTIONS_VIEW (self)->type == MODEST_SECURITY_OPTIONS_INCOMING) {
88 /* Activate and dim checkbutton if it's secure */
89 modest_togglable_set_active (ppriv->auth_view,
91 gtk_widget_set_sensitive (ppriv->auth_view, !is_secure);
98 modest_serversecurity_selector_get_active_serversecurity_port (ppriv->security_view);
101 modest_number_entry_set_value (ppriv->port_view,
108 on_auth_changed (GtkWidget *widget,
109 ModestMaemoSecurityOptionsView *self)
111 ModestSecurityOptionsViewPrivate* ppriv;
112 ModestSecureauthPicker *picker;
113 ModestProtocolRegistry *protocol_registry;
114 ModestProtocolType auth_proto;
115 gboolean secureauth_used;
116 GtkWidget *user_caption, *pwd_caption;
118 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
119 protocol_registry = modest_runtime_get_protocol_registry ();
120 picker = MODEST_SECUREAUTH_PICKER (ppriv->auth_view);
122 auth_proto = modest_secureauth_picker_get_active_secureauth (picker);
123 secureauth_used = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
126 /* Get captions, well dimm the whole widget */
127 user_caption = gtk_widget_get_parent (ppriv->user_entry);
128 pwd_caption = gtk_widget_get_parent (ppriv->pwd_entry);
130 /* Enable / disable */
131 gtk_widget_set_sensitive (user_caption, secureauth_used);
132 gtk_widget_set_sensitive (pwd_caption, secureauth_used);
134 /* Check if mandatory data is missing */
135 on_entry_changed (GTK_EDITABLE (ppriv->user_entry), (gpointer) self);
139 create_incoming_security (ModestSecurityOptionsView* self,
140 GtkSizeGroup *title_size_group,
141 GtkSizeGroup *value_size_group)
143 ModestSecurityOptionsViewPrivate *ppriv;
144 GtkWidget *entry_caption = NULL;
146 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
148 ppriv->auth_view = modest_toolkit_factory_create_check_button (modest_runtime_get_toolkit_factory (),
149 _("mcen_li_emailsetup_secure_authentication"));
150 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
152 /* Create widgets for incoming security */
153 ppriv->security_view = modest_toolkit_factory_create_serversecurity_selector (modest_runtime_get_toolkit_factory ());
154 modest_serversecurity_selector_fill (ppriv->security_view,
155 modest_protocol_registry_get_pop_type_id ());
156 if (GTK_IS_COMBO_BOX (ppriv->security_view)) {
157 GtkWidget *captioned;
159 captioned = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
160 _("mcen_li_emailsetup_secure_connection"), FALSE,
161 ppriv->security_view);
162 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
163 G_CALLBACK (on_security_changed), self);
164 gtk_box_pack_start (GTK_BOX (self), captioned, FALSE, FALSE, 0);
165 gtk_widget_show (captioned);
167 modest_maemo_utils_set_hbutton_layout (title_size_group,
169 _("mcen_li_emailsetup_secure_connection"),
170 ppriv->security_view);
171 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
172 G_CALLBACK (on_security_changed), self);
173 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
176 /* Pack into container & show */
177 gtk_widget_show (ppriv->security_view);
178 gtk_widget_show (ppriv->auth_view);
181 ppriv->port_view = modest_toolkit_factory_create_number_entry (modest_runtime_get_toolkit_factory (),
184 modest_maemo_utils_create_captioned_with_size_type (title_size_group,
186 _("mcen_fi_emailsetup_port"),
189 MODEST_EDITABLE_SIZE);
190 /* Pack & show widgets */
191 gtk_box_pack_start (GTK_BOX (self), entry_caption, FALSE, FALSE, 0);
192 gtk_widget_show (ppriv->port_view);
193 gtk_widget_show (entry_caption);
195 /* Track changes in UI */
196 #ifdef MODEST_NUMBER_ENTRY_SUPPORT_VALID_CHANGED
197 g_signal_connect (G_OBJECT (ppriv->port_view), "valid-changed",
198 G_CALLBACK (on_valid_changed), self);
204 on_entry_max (ModestValidatingEntry *self,
207 modest_platform_information_banner (GTK_WIDGET (self), NULL,
208 _CS("ckdg_ib_maximum_characters_reached"));
212 * TODO: call this whenever the auth picker changes. If we set it
213 * explicitely at the beggining to a value then there is no need to
214 * call this handler directly at the beginning
217 on_entry_changed (GtkEditable *editable,
220 ModestSecurityOptionsView* self;
221 ModestMaemoSecurityOptionsViewPrivate *priv;
222 ModestSecurityOptionsViewPrivate *ppriv;
223 ModestSecureauthPicker *picker;
224 gboolean is_secure = FALSE;
225 ModestProtocolRegistry *protocol_registry;
227 self = MODEST_SECURITY_OPTIONS_VIEW (user_data);
228 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
229 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
230 protocol_registry = modest_runtime_get_protocol_registry ();
232 /* Check if it's a secure protocol */
233 if (MODEST_IS_SECUREAUTH_PICKER (ppriv->auth_view)) {
234 ModestProtocolType auth_proto;
235 picker = MODEST_SECUREAUTH_PICKER (ppriv->auth_view);
236 auth_proto = modest_secureauth_picker_get_active_secureauth (picker);
237 is_secure = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
239 } else if (modest_is_togglable (ppriv->auth_view)) {
240 is_secure = modest_togglable_get_active (ppriv->auth_view);
244 !g_strcmp0 (modest_entry_get_text (ppriv->user_entry), "")) {
245 priv->missing_data = TRUE;
247 priv->missing_data = FALSE;
250 if (!priv->missing_data &&
252 !modest_number_entry_is_valid (ppriv->port_view))
253 priv->missing_data = TRUE;
255 /* Emit a signal to notify if mandatory data is missing */
256 g_signal_emit_by_name (G_OBJECT (self), "missing_mandatory_data",
257 priv->missing_data, NULL);
260 #ifdef MODEST_NUMBER_ENTRY_SUPPORT_VALID_CHANGED
262 on_valid_changed (ModestNumberEditor *editor,
264 ModestSecurityOptionsView *self)
266 on_entry_changed (NULL, (gpointer) self);
271 create_outgoing_security (ModestSecurityOptionsView* self,
272 GtkSizeGroup *title_size_group,
273 GtkSizeGroup *value_size_group)
275 ModestSecurityOptionsViewPrivate *ppriv;
276 GtkWidget *user_caption = NULL, *security_caption = NULL;
277 GtkWidget *pwd_caption = NULL, *port_caption = NULL;
279 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
281 /* The secure connection widgets */
282 ppriv->security_view = modest_toolkit_factory_create_serversecurity (modest_runtime_get_toolkit_factory ());
283 modest_serversecurity_selector_fill (ppriv->security_view,
284 MODEST_PROTOCOLS_TRANSPORT_SMTP);
285 if (GTK_IS_COMBO_BOX (ppriv->security_view)) {
286 security_caption = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
287 _("mcen_li_emailsetup_secure_connection"), FALSE,
288 ppriv->security_view);
290 modest_maemo_utils_set_hbutton_layout (title_size_group,
292 _("mcen_li_emailsetup_secure_connection"),
293 ppriv->security_view);
294 security_caption = ppriv->security_view;
297 /* The secure authentication widgets */
298 ppriv->auth_view = GTK_WIDGET (modest_secureauth_picker_new (MODEST_EDITABLE_SIZE,
299 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
300 modest_maemo_utils_set_hbutton_layout (title_size_group,
302 _("mcen_li_emailsetup_secure_authentication"),
308 /* Username widgets */
309 ppriv->user_entry = GTK_WIDGET (modest_validating_entry_new ());
311 /* Auto-capitalization is the default, so let's turn it off: */
312 #ifdef MODEST_TOOLKIT_HILDON2
313 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->user_entry),
314 HILDON_GTK_INPUT_MODE_FULL);
317 user_label = g_strdup_printf("%s*", _("mail_fi_username"));
318 user_caption = modest_maemo_utils_create_captioned_with_size_type (title_size_group,
323 MODEST_EDITABLE_SIZE);
326 /* Prevent the use of some characters. Limit the max
328 modest_validating_entry_set_unallowed_characters_whitespace (
329 MODEST_VALIDATING_ENTRY (ppriv->user_entry));
330 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
331 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
334 /* Password widgets */
335 ppriv->pwd_entry = modest_toolkit_factory_create_entry (modest_runtime_get_toolkit_factory ());
337 /* Auto-capitalization is the default, so let's turn it off */
338 #ifdef MODEST_TOOLKIT_HILDON2
339 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
340 HILDON_GTK_INPUT_MODE_FULL |
341 HILDON_GTK_INPUT_MODE_INVISIBLE);
343 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
346 modest_maemo_utils_create_captioned_with_size_type (title_size_group,
348 _("mail_fi_password"),
351 MODEST_EDITABLE_SIZE);
353 ppriv->port_view = modest_toolkit_factory_create_number_entry (modest_runtime_get_toolkit_factory (),
356 modest_maemo_utils_create_captioned_with_size_type (title_size_group,
358 _("mcen_fi_emailsetup_port"),
361 MODEST_EDITABLE_SIZE);
364 /* Track changes in UI */
365 if (GTK_IS_COMBO_BOX (ppriv->security_view)) {
366 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
367 G_CALLBACK (on_security_changed), self);
369 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
370 G_CALLBACK (on_security_changed), self);
373 g_signal_connect (G_OBJECT (ppriv->auth_view), "value-changed",
374 G_CALLBACK (on_auth_changed), self);
375 g_signal_connect (G_OBJECT (ppriv->user_entry), "changed",
376 G_CALLBACK (on_entry_changed), self);
377 #ifdef MODEST_NUMBER_ENTRY_SUPPORT_VALID_CHANGED
378 g_signal_connect (G_OBJECT (ppriv->port_view), "valid-changed",
379 G_CALLBACK (on_valid_changed), self);
383 /* Initialize widgets */
384 modest_serversecurity_selector_set_active_serversecurity (
385 ppriv->security_view,
386 MODEST_PROTOCOLS_CONNECTION_NONE);
387 modest_secureauth_picker_set_active_secureauth (
388 MODEST_SECUREAUTH_PICKER (ppriv->auth_view),
389 MODEST_PROTOCOLS_AUTH_NONE);
391 /* Pack into container */
393 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
394 gtk_box_pack_start (GTK_BOX (self), user_caption, FALSE, FALSE, 0);
395 gtk_box_pack_start (GTK_BOX (self), pwd_caption, FALSE, FALSE, 0);
396 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
397 gtk_box_pack_start (GTK_BOX (self), port_caption, FALSE, FALSE, 0);
399 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
400 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
405 gtk_widget_show (ppriv->pwd_entry);
406 gtk_widget_show (ppriv->user_entry);
407 gtk_widget_show (ppriv->port_view);
408 gtk_widget_show (pwd_caption);
409 gtk_widget_show (user_caption);
410 gtk_widget_show (port_caption);
412 gtk_widget_show (ppriv->security_view);
413 gtk_widget_show (ppriv->auth_view);
417 modest_maemo_security_options_view_new (ModestSecurityOptionsType type,
419 GtkSizeGroup *title_size_group,
420 GtkSizeGroup *value_size_group)
422 ModestSecurityOptionsView* self;
423 ModestSecurityOptionsViewPrivate *ppriv;
425 self = (ModestSecurityOptionsView *)
426 g_object_new (MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, NULL);
427 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
431 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
432 create_incoming_security (self, title_size_group, value_size_group);
434 create_outgoing_security (self, title_size_group, value_size_group);
436 return (GtkWidget *) self;
440 modest_security_options_view_has_missing_mandatory_data (ModestSecurityOptionsView* self)
442 ModestMaemoSecurityOptionsViewPrivate *priv;
444 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
446 return priv->missing_data;
450 modest_maemo_security_options_view_load_settings (ModestSecurityOptionsView* self,
451 ModestAccountSettings *settings)
453 ModestSecurityOptionsViewPrivate *ppriv;
454 ModestServerAccountSettings *server_settings;
457 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
459 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
460 server_settings = modest_account_settings_get_store_settings (settings);
462 server_settings = modest_account_settings_get_transport_settings (settings);
463 port_number = modest_server_account_settings_get_port (server_settings);
465 if (port_number == 0) {
466 /* Show the appropriate port number */
467 on_security_changed (ppriv->security_view,
468 MODEST_MAEMO_SECURITY_OPTIONS_VIEW (self));
469 } else if (ppriv->full) {
470 /* Keep the user-entered port-number, or the
471 * already-appropriate automatic port number */
472 modest_number_entry_set_value (ppriv->port_view,
476 g_object_unref (server_settings);
480 modest_maemo_security_options_view_save_settings (ModestSecurityOptionsView* self,
481 ModestAccountSettings *settings)
483 ModestServerAccountSettings *server_settings;
484 ModestSecurityOptionsViewPrivate *ppriv;
487 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
489 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
490 server_settings = modest_account_settings_get_store_settings (settings);
492 server_settings = modest_account_settings_get_transport_settings (settings);
495 server_port = modest_number_entry_get_value (ppriv->port_view);
497 server_port = modest_serversecurity_selector_get_active_serversecurity_port (ppriv->security_view);
500 modest_server_account_settings_set_port (server_settings, server_port);
503 g_object_unref (server_settings);
507 modest_maemo_security_options_view_changed (ModestSecurityOptionsView* self,
508 ModestAccountSettings *settings)
510 ModestServerAccountSettings *server_settings;
511 ModestSecurityOptionsViewPrivate *ppriv;
514 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
516 /* If we're not showing the port number then it never changes */
520 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
521 server_settings = modest_account_settings_get_store_settings (settings);
523 server_settings = modest_account_settings_get_transport_settings (settings);
526 modest_number_entry_get_value (ppriv->port_view);
529 g_object_unref (server_settings);
531 if (server_port != ppriv->initial_state.port)
538 modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj)
543 modest_maemo_security_options_view_finalize (GObject *obj)
545 G_OBJECT_CLASS (modest_maemo_security_options_view_parent_class)->finalize (obj);
550 modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass)
552 GObjectClass *gobject_class = (GObjectClass*) klass;
554 modest_maemo_security_options_view_parent_class = g_type_class_peek_parent (klass);
556 g_type_class_add_private (gobject_class, sizeof (ModestMaemoSecurityOptionsViewPrivate));
557 gobject_class->finalize = modest_maemo_security_options_view_finalize;
559 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->load_settings =
560 modest_maemo_security_options_view_load_settings;
561 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->save_settings =
562 modest_maemo_security_options_view_save_settings;
563 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->changed =
564 modest_maemo_security_options_view_changed;