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-hildon-includes.h"
44 #define PORT_MAX 65535
46 typedef struct _ModestMaemoSecurityOptionsViewPrivate ModestMaemoSecurityOptionsViewPrivate;
47 struct _ModestMaemoSecurityOptionsViewPrivate {
48 gboolean missing_data;
51 #define MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE(o) \
52 (G_TYPE_INSTANCE_GET_PRIVATE((o), \
53 MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, \
54 ModestMaemoSecurityOptionsViewPrivate))
56 static void modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj);
57 static void modest_maemo_security_options_view_finalize (GObject *obj);
58 static void modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass);
60 G_DEFINE_TYPE (ModestMaemoSecurityOptionsView,
61 modest_maemo_security_options_view,
62 MODEST_TYPE_SECURITY_OPTIONS_VIEW);
64 static void on_entry_changed (GtkEditable *editable, gpointer user_data);
66 #ifdef MODEST_NUMBER_ENTRY_SUPPORT_VALID_CHANGED
67 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 modest_togglable_set_active (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_entry_set_value (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 = modest_toolkit_factory_create_check_button (modest_runtime_get_toolkit_factory (),
162 _("mcen_li_emailsetup_secure_authentication"));
164 /* Track changes in UI */
165 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
166 G_CALLBACK (on_security_changed), self);
168 /* Pack into container & show */
169 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
170 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
171 gtk_widget_show (ppriv->security_view);
172 gtk_widget_show (ppriv->auth_view);
175 ppriv->port_view = modest_toolkit_factory_create_number_entry (modest_runtime_get_toolkit_factory (),
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 #ifdef MODEST_NUMBER_ENTRY_SUPPORT_VALID_CHANGED
191 g_signal_connect (G_OBJECT (ppriv->port_view), "valid-changed",
192 G_CALLBACK (on_valid_changed), self);
198 on_entry_max (ModestValidatingEntry *self,
201 modest_platform_information_banner (GTK_WIDGET (self), NULL,
202 _CS("ckdg_ib_maximum_characters_reached"));
206 * TODO: call this whenever the auth picker changes. If we set it
207 * explicitely at the beggining to a value then there is no need to
208 * call this handler directly at the beginning
211 on_entry_changed (GtkEditable *editable,
214 ModestSecurityOptionsView* self;
215 ModestMaemoSecurityOptionsViewPrivate *priv;
216 ModestSecurityOptionsViewPrivate *ppriv;
217 ModestSecureauthPicker *picker;
218 gboolean is_secure = FALSE;
219 ModestProtocolRegistry *protocol_registry;
221 self = MODEST_SECURITY_OPTIONS_VIEW (user_data);
222 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
223 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
224 protocol_registry = modest_runtime_get_protocol_registry ();
226 /* Check if it's a secure protocol */
227 if (MODEST_IS_SECUREAUTH_PICKER (ppriv->auth_view)) {
228 ModestProtocolType auth_proto;
229 picker = MODEST_SECUREAUTH_PICKER (ppriv->auth_view);
230 auth_proto = modest_secureauth_picker_get_active_secureauth (picker);
231 is_secure = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
233 } else if (modest_is_togglable (ppriv->auth_view)) {
234 is_secure = modest_togglable_get_active (ppriv->auth_view);
238 !g_strcmp0 (modest_entry_get_text (ppriv->user_entry), "")) {
239 priv->missing_data = TRUE;
241 priv->missing_data = FALSE;
244 if (!priv->missing_data &&
246 !modest_number_entry_is_valid (ppriv->port_view))
247 priv->missing_data = TRUE;
249 /* Emit a signal to notify if mandatory data is missing */
250 g_signal_emit_by_name (G_OBJECT (self), "missing_mandatory_data",
251 priv->missing_data, NULL);
254 #ifdef MODEST_NUMBER_ENTRY_SUPPORT_VALID_CHANGED
256 on_valid_changed (ModestNumberEditor *editor,
258 ModestSecurityOptionsView *self)
260 on_entry_changed (NULL, (gpointer) self);
265 create_outgoing_security (ModestSecurityOptionsView* self,
266 GtkSizeGroup *title_size_group,
267 GtkSizeGroup *value_size_group)
269 ModestSecurityOptionsViewPrivate *ppriv;
270 GtkWidget *user_caption = NULL;
271 GtkWidget *pwd_caption = NULL, *port_caption = NULL;
273 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
275 /* The secure connection widgets */
276 ppriv->security_view = GTK_WIDGET (modest_serversecurity_picker_new (MODEST_EDITABLE_SIZE,
277 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
278 modest_serversecurity_picker_fill (MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
279 MODEST_PROTOCOLS_TRANSPORT_SMTP);
280 modest_maemo_utils_set_hbutton_layout (title_size_group,
282 _("mcen_li_emailsetup_secure_connection"),
283 ppriv->security_view);
285 /* The secure authentication widgets */
286 ppriv->auth_view = GTK_WIDGET (modest_secureauth_picker_new (MODEST_EDITABLE_SIZE,
287 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
288 modest_maemo_utils_set_hbutton_layout (title_size_group,
290 _("mcen_li_emailsetup_secure_authentication"),
296 /* Username widgets */
297 ppriv->user_entry = GTK_WIDGET (modest_validating_entry_new ());
299 /* Auto-capitalization is the default, so let's turn it off: */
300 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->user_entry),
301 HILDON_GTK_INPUT_MODE_FULL);
303 user_label = g_strdup_printf("%s*", _("mail_fi_username"));
304 user_caption = modest_maemo_utils_create_captioned_with_size_type (title_size_group,
309 MODEST_EDITABLE_SIZE);
312 /* Prevent the use of some characters. Limit the max
314 modest_validating_entry_set_unallowed_characters_whitespace (
315 MODEST_VALIDATING_ENTRY (ppriv->user_entry));
316 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
317 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
320 /* Password widgets */
321 ppriv->pwd_entry = modest_toolkit_factory_create_entry (modest_runtime_get_toolkit_factory ());
323 /* Auto-capitalization is the default, so let's turn it off */
324 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
325 HILDON_GTK_INPUT_MODE_FULL |
326 HILDON_GTK_INPUT_MODE_INVISIBLE);
327 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
330 modest_maemo_utils_create_captioned_with_size_type (title_size_group,
332 _("mail_fi_password"),
335 MODEST_EDITABLE_SIZE);
337 ppriv->port_view = modest_toolkit_factory_create_number_entry (modest_runtime_get_toolkit_factory (),
340 modest_maemo_utils_create_captioned_with_size_type (title_size_group,
342 _("mcen_fi_emailsetup_port"),
345 MODEST_EDITABLE_SIZE);
348 /* Track changes in UI */
349 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
350 G_CALLBACK (on_security_changed), self);
352 g_signal_connect (G_OBJECT (ppriv->auth_view), "value-changed",
353 G_CALLBACK (on_auth_changed), self);
354 g_signal_connect (G_OBJECT (ppriv->user_entry), "changed",
355 G_CALLBACK (on_entry_changed), self);
356 #ifdef MODEST_NUMBER_ENTRY_SUPPORT_VALID_CHANGED
357 g_signal_connect (G_OBJECT (ppriv->port_view), "valid-changed",
358 G_CALLBACK (on_valid_changed), self);
362 /* Initialize widgets */
363 modest_serversecurity_picker_set_active_serversecurity (
364 MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
365 MODEST_PROTOCOLS_CONNECTION_NONE);
366 modest_secureauth_picker_set_active_secureauth (
367 MODEST_SECUREAUTH_PICKER (ppriv->auth_view),
368 MODEST_PROTOCOLS_AUTH_NONE);
370 /* Pack into container */
372 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
373 gtk_box_pack_start (GTK_BOX (self), user_caption, FALSE, FALSE, 0);
374 gtk_box_pack_start (GTK_BOX (self), pwd_caption, FALSE, FALSE, 0);
375 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
376 gtk_box_pack_start (GTK_BOX (self), port_caption, FALSE, FALSE, 0);
378 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
379 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
384 gtk_widget_show (ppriv->pwd_entry);
385 gtk_widget_show (ppriv->user_entry);
386 gtk_widget_show (ppriv->port_view);
387 gtk_widget_show (pwd_caption);
388 gtk_widget_show (user_caption);
389 gtk_widget_show (port_caption);
391 gtk_widget_show (ppriv->security_view);
392 gtk_widget_show (ppriv->auth_view);
396 modest_maemo_security_options_view_new (ModestSecurityOptionsType type,
398 GtkSizeGroup *title_size_group,
399 GtkSizeGroup *value_size_group)
401 ModestSecurityOptionsView* self;
402 ModestSecurityOptionsViewPrivate *ppriv;
404 self = (ModestSecurityOptionsView *)
405 g_object_new (MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, NULL);
406 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
410 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
411 create_incoming_security (self, title_size_group, value_size_group);
413 create_outgoing_security (self, title_size_group, value_size_group);
415 return (GtkWidget *) self;
419 modest_security_options_view_has_missing_mandatory_data (ModestSecurityOptionsView* self)
421 ModestMaemoSecurityOptionsViewPrivate *priv;
423 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
425 return priv->missing_data;
429 modest_maemo_security_options_view_load_settings (ModestSecurityOptionsView* self,
430 ModestAccountSettings *settings)
432 ModestSecurityOptionsViewPrivate *ppriv;
433 ModestServerAccountSettings *server_settings;
436 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
438 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
439 server_settings = modest_account_settings_get_store_settings (settings);
441 server_settings = modest_account_settings_get_transport_settings (settings);
442 port_number = modest_server_account_settings_get_port (server_settings);
444 if (port_number == 0) {
445 /* Show the appropriate port number */
446 on_security_changed (ppriv->security_view,
447 MODEST_MAEMO_SECURITY_OPTIONS_VIEW (self));
448 } else if (ppriv->full) {
449 /* Keep the user-entered port-number, or the
450 * already-appropriate automatic port number */
451 modest_number_entry_set_value (ppriv->port_view,
455 g_object_unref (server_settings);
459 modest_maemo_security_options_view_save_settings (ModestSecurityOptionsView* self,
460 ModestAccountSettings *settings)
462 ModestServerAccountSettings *server_settings;
463 ModestSecurityOptionsViewPrivate *ppriv;
466 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
468 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
469 server_settings = modest_account_settings_get_store_settings (settings);
471 server_settings = modest_account_settings_get_transport_settings (settings);
474 server_port = modest_number_entry_get_value (ppriv->port_view);
476 server_port = modest_serversecurity_picker_get_active_serversecurity_port (MODEST_SERVERSECURITY_PICKER (ppriv->security_view));
479 modest_server_account_settings_set_port (server_settings, server_port);
482 g_object_unref (server_settings);
486 modest_maemo_security_options_view_changed (ModestSecurityOptionsView* self,
487 ModestAccountSettings *settings)
489 ModestServerAccountSettings *server_settings;
490 ModestSecurityOptionsViewPrivate *ppriv;
493 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
495 /* If we're not showing the port number then it never changes */
499 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
500 server_settings = modest_account_settings_get_store_settings (settings);
502 server_settings = modest_account_settings_get_transport_settings (settings);
505 modest_number_entry_get_value (ppriv->port_view);
508 g_object_unref (server_settings);
510 if (server_port != ppriv->initial_state.port)
517 modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj)
522 modest_maemo_security_options_view_finalize (GObject *obj)
524 G_OBJECT_CLASS (modest_maemo_security_options_view_parent_class)->finalize (obj);
529 modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass)
531 GObjectClass *gobject_class = (GObjectClass*) klass;
533 modest_maemo_security_options_view_parent_class = g_type_class_peek_parent (klass);
535 g_type_class_add_private (gobject_class, sizeof (ModestMaemoSecurityOptionsViewPrivate));
536 gobject_class->finalize = modest_maemo_security_options_view_finalize;
538 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->load_settings =
539 modest_maemo_security_options_view_load_settings;
540 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->save_settings =
541 modest_maemo_security_options_view_save_settings;
542 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->changed =
543 modest_maemo_security_options_view_changed;