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"
40 #include <modest-toolkit-factory.h>
41 #include <modest-toolkit-utils.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 ModestProtocolType proto_type;
77 ModestProtocolRegistry *proto_registry;
80 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
82 proto_registry = modest_runtime_get_protocol_registry ();
83 proto_type = modest_serversecurity_selector_get_active_serversecurity (ppriv->security_view);
85 is_secure = modest_protocol_registry_protocol_type_has_tag (proto_registry, proto_type,
86 MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS);
90 modest_serversecurity_selector_get_active_serversecurity_port (ppriv->security_view);
93 modest_number_entry_set_value (ppriv->port_view,
100 on_auth_changed (GtkWidget *widget,
101 ModestMaemoSecurityOptionsView *self)
103 ModestSecurityOptionsViewPrivate* ppriv;
104 ModestProtocolRegistry *protocol_registry;
105 ModestProtocolType auth_proto;
106 gboolean secureauth_used;
107 GtkWidget *user_caption, *pwd_caption;
109 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
110 protocol_registry = modest_runtime_get_protocol_registry ();
112 auth_proto = modest_secureauth_selector_get_active_secureauth (ppriv->auth_view);
113 secureauth_used = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
116 /* Get captions, well dimm the whole widget */
117 user_caption = gtk_widget_get_parent (ppriv->user_entry);
118 pwd_caption = gtk_widget_get_parent (ppriv->pwd_entry);
120 /* Enable / disable */
121 gtk_widget_set_sensitive (user_caption, secureauth_used);
122 gtk_widget_set_sensitive (pwd_caption, secureauth_used);
124 /* Check if mandatory data is missing */
125 on_entry_changed (GTK_EDITABLE (ppriv->user_entry), (gpointer) self);
129 create_incoming_security (ModestSecurityOptionsView* self,
130 GtkSizeGroup *title_size_group,
131 GtkSizeGroup *value_size_group)
133 ModestSecurityOptionsViewPrivate *ppriv;
134 GtkWidget *entry_caption = NULL;
136 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
138 ppriv->auth_view = modest_toolkit_factory_create_check_button (modest_runtime_get_toolkit_factory (),
139 _("mcen_li_emailsetup_secure_authentication"));
140 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
142 /* Create widgets for incoming security */
143 ppriv->security_view = modest_toolkit_factory_create_serversecurity_selector (modest_runtime_get_toolkit_factory ());
144 modest_serversecurity_selector_fill (ppriv->security_view,
145 modest_protocol_registry_get_pop_type_id ());
146 if (GTK_IS_COMBO_BOX (ppriv->security_view)) {
147 GtkWidget *captioned;
149 captioned = modest_toolkit_utils_create_captioned (title_size_group, value_size_group,
150 _("mcen_li_emailsetup_secure_connection"), FALSE,
151 ppriv->security_view);
152 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
153 G_CALLBACK (on_security_changed), self);
154 gtk_box_pack_start (GTK_BOX (self), captioned, FALSE, FALSE, 0);
155 gtk_widget_show (captioned);
157 modest_toolkit_utils_set_hbutton_layout (title_size_group,
159 _("mcen_li_emailsetup_secure_connection"),
160 ppriv->security_view);
161 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
162 G_CALLBACK (on_security_changed), self);
163 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
166 /* Pack into container & show */
167 gtk_widget_show (ppriv->security_view);
168 gtk_widget_show (ppriv->auth_view);
171 ppriv->port_view = modest_toolkit_factory_create_number_entry (modest_runtime_get_toolkit_factory (),
174 modest_toolkit_utils_create_captioned_with_size_type (title_size_group,
176 _("mcen_fi_emailsetup_port"),
179 MODEST_EDITABLE_SIZE);
180 /* Pack & show widgets */
181 gtk_box_pack_start (GTK_BOX (self), entry_caption, FALSE, FALSE, 0);
182 gtk_widget_show (ppriv->port_view);
183 gtk_widget_show (entry_caption);
185 /* Track changes in UI */
186 #ifdef MODEST_NUMBER_ENTRY_SUPPORT_VALID_CHANGED
187 g_signal_connect (G_OBJECT (ppriv->port_view), "valid-changed",
188 G_CALLBACK (on_valid_changed), self);
194 on_entry_max (ModestValidatingEntry *self,
197 modest_platform_information_banner (GTK_WIDGET (self), NULL,
198 _CS_MAXIMUM_CHARACTERS_REACHED);
202 * TODO: call this whenever the auth picker changes. If we set it
203 * explicitely at the beggining to a value then there is no need to
204 * call this handler directly at the beginning
207 on_entry_changed (GtkEditable *editable,
210 ModestSecurityOptionsView* self;
211 ModestMaemoSecurityOptionsViewPrivate *priv;
212 ModestSecurityOptionsViewPrivate *ppriv;
213 gboolean is_secure = FALSE;
214 ModestProtocolRegistry *protocol_registry;
216 self = MODEST_SECURITY_OPTIONS_VIEW (user_data);
217 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
218 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
219 protocol_registry = modest_runtime_get_protocol_registry ();
221 /* Check if it's a secure protocol */
222 if (modest_is_secureauth_selector (ppriv->auth_view)) {
223 ModestProtocolType auth_proto;
224 auth_proto = modest_secureauth_selector_get_active_secureauth (ppriv->auth_view);
225 is_secure = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
227 } else if (modest_is_togglable (ppriv->auth_view)) {
228 is_secure = modest_togglable_get_active (ppriv->auth_view);
232 !g_strcmp0 (modest_entry_get_text (ppriv->user_entry), "")) {
233 priv->missing_data = TRUE;
235 priv->missing_data = FALSE;
238 if (!priv->missing_data &&
240 !modest_number_entry_is_valid (ppriv->port_view))
241 priv->missing_data = TRUE;
243 /* Emit a signal to notify if mandatory data is missing */
244 g_signal_emit_by_name (G_OBJECT (self), "missing_mandatory_data",
245 priv->missing_data, NULL);
248 #ifdef MODEST_NUMBER_ENTRY_SUPPORT_VALID_CHANGED
250 on_valid_changed (ModestNumberEditor *editor,
252 ModestSecurityOptionsView *self)
254 on_entry_changed (NULL, (gpointer) self);
259 create_outgoing_security (ModestSecurityOptionsView* self,
260 GtkSizeGroup *title_size_group,
261 GtkSizeGroup *value_size_group)
263 ModestSecurityOptionsViewPrivate *ppriv;
264 GtkWidget *user_caption = NULL, *security_caption = NULL, *auth_caption = NULL;
265 GtkWidget *pwd_caption = NULL, *port_caption = NULL;
267 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
269 /* The secure connection widgets */
270 ppriv->security_view = modest_toolkit_factory_create_serversecurity_selector (modest_runtime_get_toolkit_factory ());
271 modest_serversecurity_selector_fill (ppriv->security_view,
272 MODEST_PROTOCOLS_TRANSPORT_SMTP);
273 if (GTK_IS_COMBO_BOX (ppriv->security_view)) {
274 security_caption = modest_toolkit_utils_create_captioned (title_size_group, value_size_group,
275 _("mcen_li_emailsetup_secure_connection"), FALSE,
276 ppriv->security_view);
277 gtk_widget_show (security_caption);
279 modest_toolkit_utils_set_hbutton_layout (title_size_group,
281 _("mcen_li_emailsetup_secure_connection"),
282 ppriv->security_view);
283 security_caption = ppriv->security_view;
286 /* The secure authentication widgets */
287 ppriv->auth_view = modest_toolkit_factory_create_secureauth_selector (modest_runtime_get_toolkit_factory ());
288 if (GTK_IS_COMBO_BOX (ppriv->auth_view)) {
289 auth_caption = modest_toolkit_utils_create_captioned (title_size_group, value_size_group,
290 _("mcen_li_emailsetup_secure_authentication"), FALSE,
292 gtk_widget_show (auth_caption);
294 modest_toolkit_utils_set_hbutton_layout (title_size_group,
296 _("mcen_li_emailsetup_secure_authentication"),
298 auth_caption = ppriv->auth_view;
304 /* Username widgets */
305 ppriv->user_entry = GTK_WIDGET (modest_validating_entry_new ());
307 /* Auto-capitalization is the default, so let's turn it off: */
308 #ifdef MODEST_TOOLKIT_HILDON2
309 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->user_entry),
310 HILDON_GTK_INPUT_MODE_FULL);
313 user_label = g_strdup_printf("%s*", _("mail_fi_username"));
314 user_caption = modest_toolkit_utils_create_captioned_with_size_type (title_size_group,
319 MODEST_EDITABLE_SIZE);
322 /* Prevent the use of some characters. Limit the max
324 modest_validating_entry_set_unallowed_characters_whitespace (
325 MODEST_VALIDATING_ENTRY (ppriv->user_entry));
326 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
327 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
330 /* Password widgets */
331 ppriv->pwd_entry = modest_toolkit_factory_create_entry (modest_runtime_get_toolkit_factory ());
333 /* Auto-capitalization is the default, so let's turn it off */
334 #ifdef MODEST_TOOLKIT_HILDON2
335 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
336 HILDON_GTK_INPUT_MODE_FULL |
337 HILDON_GTK_INPUT_MODE_INVISIBLE);
339 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
342 modest_toolkit_utils_create_captioned_with_size_type (title_size_group,
344 _("mail_fi_password"),
347 MODEST_EDITABLE_SIZE);
349 ppriv->port_view = modest_toolkit_factory_create_number_entry (modest_runtime_get_toolkit_factory (),
352 modest_toolkit_utils_create_captioned_with_size_type (title_size_group,
354 _("mcen_fi_emailsetup_port"),
357 MODEST_EDITABLE_SIZE);
360 /* Track changes in UI */
361 if (GTK_IS_COMBO_BOX (ppriv->security_view)) {
362 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
363 G_CALLBACK (on_security_changed), self);
365 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
366 G_CALLBACK (on_security_changed), self);
369 if (GTK_IS_COMBO_BOX (ppriv->auth_view)) {
370 g_signal_connect (G_OBJECT (ppriv->auth_view), "changed",
371 G_CALLBACK (on_auth_changed), self);
373 g_signal_connect (G_OBJECT (ppriv->auth_view), "value-changed",
374 G_CALLBACK (on_auth_changed), self);
376 g_signal_connect (G_OBJECT (ppriv->user_entry), "changed",
377 G_CALLBACK (on_entry_changed), self);
378 #ifdef MODEST_NUMBER_ENTRY_SUPPORT_VALID_CHANGED
379 g_signal_connect (G_OBJECT (ppriv->port_view), "valid-changed",
380 G_CALLBACK (on_valid_changed), self);
384 /* Initialize widgets */
385 modest_serversecurity_selector_set_active_serversecurity (
386 ppriv->security_view,
387 MODEST_PROTOCOLS_CONNECTION_NONE);
388 modest_secureauth_selector_set_active_secureauth (
390 MODEST_PROTOCOLS_AUTH_NONE);
392 /* Pack into container */
394 gtk_box_pack_start (GTK_BOX (self), auth_caption, FALSE, FALSE, 0);
395 gtk_box_pack_start (GTK_BOX (self), user_caption, FALSE, FALSE, 0);
396 gtk_box_pack_start (GTK_BOX (self), pwd_caption, FALSE, FALSE, 0);
397 gtk_box_pack_start (GTK_BOX (self), security_caption, FALSE, FALSE, 0);
398 gtk_box_pack_start (GTK_BOX (self), port_caption, FALSE, FALSE, 0);
400 gtk_box_pack_start (GTK_BOX (self), auth_caption, FALSE, FALSE, 0);
401 gtk_box_pack_start (GTK_BOX (self), security_caption, FALSE, FALSE, 0);
406 gtk_widget_show (ppriv->pwd_entry);
407 gtk_widget_show (ppriv->user_entry);
408 gtk_widget_show (ppriv->port_view);
409 gtk_widget_show (pwd_caption);
410 gtk_widget_show (user_caption);
411 gtk_widget_show (port_caption);
413 gtk_widget_show (ppriv->security_view);
414 gtk_widget_show (ppriv->auth_view);
418 modest_maemo_security_options_view_new (ModestSecurityOptionsType type,
420 GtkSizeGroup *title_size_group,
421 GtkSizeGroup *value_size_group)
423 ModestSecurityOptionsView* self;
424 ModestSecurityOptionsViewPrivate *ppriv;
426 self = (ModestSecurityOptionsView *)
427 g_object_new (MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, NULL);
428 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
432 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
433 create_incoming_security (self, title_size_group, value_size_group);
435 create_outgoing_security (self, title_size_group, value_size_group);
437 return (GtkWidget *) self;
441 modest_security_options_view_has_missing_mandatory_data (ModestSecurityOptionsView* self)
443 ModestMaemoSecurityOptionsViewPrivate *priv;
445 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
447 return priv->missing_data;
451 modest_maemo_security_options_view_load_settings (ModestSecurityOptionsView* self,
452 ModestAccountSettings *settings)
454 ModestSecurityOptionsViewPrivate *ppriv;
455 ModestServerAccountSettings *server_settings;
458 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
460 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
461 server_settings = modest_account_settings_get_store_settings (settings);
463 server_settings = modest_account_settings_get_transport_settings (settings);
464 port_number = modest_server_account_settings_get_port (server_settings);
466 if (port_number == 0) {
467 /* Show the appropriate port number */
468 on_security_changed (ppriv->security_view,
469 MODEST_MAEMO_SECURITY_OPTIONS_VIEW (self));
470 } else if (ppriv->full) {
471 /* Keep the user-entered port-number, or the
472 * already-appropriate automatic port number */
473 modest_number_entry_set_value (ppriv->port_view,
477 g_object_unref (server_settings);
481 modest_maemo_security_options_view_save_settings (ModestSecurityOptionsView* self,
482 ModestAccountSettings *settings)
484 ModestServerAccountSettings *server_settings;
485 ModestSecurityOptionsViewPrivate *ppriv;
488 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
490 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
491 server_settings = modest_account_settings_get_store_settings (settings);
493 server_settings = modest_account_settings_get_transport_settings (settings);
496 server_port = modest_number_entry_get_value (ppriv->port_view);
498 server_port = modest_serversecurity_selector_get_active_serversecurity_port (ppriv->security_view);
501 modest_server_account_settings_set_port (server_settings, server_port);
504 g_object_unref (server_settings);
508 modest_maemo_security_options_view_changed (ModestSecurityOptionsView* self,
509 ModestAccountSettings *settings)
511 ModestServerAccountSettings *server_settings;
512 ModestSecurityOptionsViewPrivate *ppriv;
515 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
517 /* If we're not showing the port number then it never changes */
521 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
522 server_settings = modest_account_settings_get_store_settings (settings);
524 server_settings = modest_account_settings_get_transport_settings (settings);
527 modest_number_entry_get_value (ppriv->port_view);
530 g_object_unref (server_settings);
532 if (server_port != ppriv->initial_state.port)
539 modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj)
544 modest_maemo_security_options_view_finalize (GObject *obj)
546 G_OBJECT_CLASS (modest_maemo_security_options_view_parent_class)->finalize (obj);
551 modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass)
553 GObjectClass *gobject_class = (GObjectClass*) klass;
555 modest_maemo_security_options_view_parent_class = g_type_class_peek_parent (klass);
557 g_type_class_add_private (gobject_class, sizeof (ModestMaemoSecurityOptionsViewPrivate));
558 gobject_class->finalize = modest_maemo_security_options_view_finalize;
560 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->load_settings =
561 modest_maemo_security_options_view_load_settings;
562 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->save_settings =
563 modest_maemo_security_options_view_save_settings;
564 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->changed =
565 modest_maemo_security_options_view_changed;