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-gtk-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-toolkit-utils.h"
41 #define PORT_MAX 65535
43 typedef struct _ModestGtkSecurityOptionsViewPrivate ModestGtkSecurityOptionsViewPrivate;
44 struct _ModestGtkSecurityOptionsViewPrivate {
45 gboolean missing_data;
48 #define MODEST_GTK_SECURITY_OPTIONS_VIEW_GET_PRIVATE(o) \
49 (G_TYPE_INSTANCE_GET_PRIVATE((o), \
50 MODEST_TYPE_GTK_SECURITY_OPTIONS_VIEW, \
51 ModestGtkSecurityOptionsViewPrivate))
53 static void modest_gtk_security_options_view_init (ModestGtkSecurityOptionsView *obj);
54 static void modest_gtk_security_options_view_finalize (GObject *obj);
55 static void modest_gtk_security_options_view_class_init (ModestGtkSecurityOptionsViewClass *klass);
57 G_DEFINE_TYPE (ModestGtkSecurityOptionsView,
58 modest_gtk_security_options_view,
59 MODEST_TYPE_SECURITY_OPTIONS_VIEW);
61 static void on_entry_changed (GtkEditable *editable, gpointer user_data);
63 /* Tracks changes in the incoming security combo box */
65 on_security_changed (GtkWidget *widget,
66 ModestGtkSecurityOptionsView *self)
68 ModestSecurityOptionsViewPrivate* ppriv;
69 ModestProtocolType proto_type;
70 ModestProtocolRegistry *proto_registry;
73 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
75 proto_registry = modest_runtime_get_protocol_registry ();
76 proto_type = modest_serversecurity_selector_get_active_serversecurity (ppriv->security_view);
78 is_secure = modest_protocol_registry_protocol_type_has_tag (proto_registry, proto_type,
79 MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS);
81 if (MODEST_SECURITY_OPTIONS_VIEW (self)->type == MODEST_SECURITY_OPTIONS_INCOMING) {
82 /* Activate and dim checkbutton if it's secure */
83 modest_togglable_set_active (ppriv->auth_view,
85 gtk_widget_set_sensitive (ppriv->auth_view, !is_secure);
92 modest_serversecurity_selector_get_active_serversecurity_port (ppriv->security_view);
95 modest_number_entry_set_value (ppriv->port_view,
102 on_auth_changed (GtkWidget *widget,
103 ModestGtkSecurityOptionsView *self)
105 ModestSecurityOptionsViewPrivate* ppriv;
106 ModestProtocolRegistry *protocol_registry;
107 ModestProtocolType auth_proto;
108 gboolean secureauth_used;
109 GtkWidget *user_caption, *pwd_caption;
111 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
112 protocol_registry = modest_runtime_get_protocol_registry ();
114 auth_proto = modest_secureauth_selector_get_active_secureauth (ppriv->auth_view);
115 secureauth_used = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
118 /* Get captions, well dimm the whole widget */
119 user_caption = gtk_widget_get_parent (ppriv->user_entry);
120 pwd_caption = gtk_widget_get_parent (ppriv->pwd_entry);
122 /* Enable / disable */
123 gtk_widget_set_sensitive (user_caption, secureauth_used);
124 gtk_widget_set_sensitive (pwd_caption, secureauth_used);
126 /* Check if mandatory data is missing */
127 on_entry_changed (GTK_EDITABLE (ppriv->user_entry), (gpointer) self);
131 create_incoming_security (ModestSecurityOptionsView* self,
132 GtkSizeGroup *title_size_group, GtkSizeGroup *value_size_group)
134 ModestSecurityOptionsViewPrivate *ppriv;
135 GtkWidget *combo_caption, *entry_caption = NULL;
137 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
139 /* Create widgets for incoming security */
140 ppriv->security_view = modest_toolkit_factory_create_serversecurity_selector (modest_runtime_get_toolkit_factory ());
141 combo_caption = modest_toolkit_utils_create_captioned (title_size_group, value_size_group,
142 _("mcen_li_emailsetup_secure_connection"), FALSE,
143 ppriv->security_view);
146 ppriv->port_view = modest_toolkit_factory_create_number_entry (modest_runtime_get_toolkit_factory (), PORT_MIN, PORT_MAX);
147 entry_caption = modest_toolkit_utils_create_captioned (title_size_group, value_size_group,
148 _("mcen_fi_emailsetup_port"), FALSE,
152 ppriv->auth_view = modest_toolkit_factory_create_check_button (modest_runtime_get_toolkit_factory (),
153 _("mcen_li_emailsetup_secure_authentication"));
155 /* Track changes in UI */
156 if (GTK_IS_COMBO_BOX (ppriv->security_view)) {
157 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
158 G_CALLBACK (on_security_changed), self);
160 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
161 G_CALLBACK (on_security_changed), self);
164 /* Pack into container */
165 gtk_box_pack_start (GTK_BOX (self), combo_caption,
166 FALSE, FALSE, MODEST_MARGIN_HALF);
168 gtk_box_pack_start (GTK_BOX (self), entry_caption,
169 FALSE, FALSE, MODEST_MARGIN_HALF);
170 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view,
171 FALSE, FALSE, MODEST_MARGIN_HALF);
175 gtk_widget_show (ppriv->port_view);
176 gtk_widget_show (entry_caption);
178 gtk_widget_show (ppriv->security_view);
179 gtk_widget_show (ppriv->auth_view);
180 gtk_widget_show (combo_caption);
184 on_entry_max (ModestValidatingEntry *self,
187 modest_platform_information_banner (GTK_WIDGET (self), NULL,
188 _CS_MAXIMUM_CHARACTERS_REACHED);
192 * TODO: call this whenever the auth combo changes. If we set it
193 * explicitely at the beggining to a value then there is no need to
194 * call this handler directly at the beginning
197 on_entry_changed (GtkEditable *editable,
200 ModestSecurityOptionsView* self;
201 ModestGtkSecurityOptionsViewPrivate *priv;
202 ModestSecurityOptionsViewPrivate *ppriv;
203 ModestProtocolType auth_proto;
205 ModestProtocolRegistry *protocol_registry;
207 self = MODEST_SECURITY_OPTIONS_VIEW (user_data);
208 priv = MODEST_GTK_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
209 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
210 protocol_registry = modest_runtime_get_protocol_registry ();
212 /* Outgoing username is mandatory if outgoing auth is secure */
213 auth_proto = modest_secureauth_selector_get_active_secureauth (ppriv->auth_view);
214 is_secure = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
218 !g_ascii_strcasecmp (gtk_entry_get_text (GTK_ENTRY (ppriv->user_entry)), "")) {
219 priv->missing_data = TRUE;
221 priv->missing_data = FALSE;
224 /* Emit a signal to notify if mandatory data is missing */
225 g_signal_emit_by_name (G_OBJECT (self), "missing_mandatory_data",
226 priv->missing_data, NULL);
230 create_outgoing_security (ModestSecurityOptionsView* self,
231 GtkSizeGroup *title_size_group, GtkSizeGroup *value_size_group)
233 ModestSecurityOptionsViewPrivate *ppriv;
234 GtkWidget *sec_caption, *auth_caption, *user_caption = NULL;
235 GtkWidget *pwd_caption = NULL, *port_caption = NULL;
237 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
239 /* The secure connection widgets */
240 ppriv->security_view = modest_toolkit_factory_create_serversecurity_selector (modest_runtime_get_toolkit_factory ());
241 modest_serversecurity_selector_fill (ppriv->security_view,
242 MODEST_PROTOCOLS_TRANSPORT_SMTP);
243 sec_caption = modest_toolkit_utils_create_captioned (title_size_group, value_size_group,
244 _("mcen_li_emailsetup_secure_connection"), FALSE,
245 ppriv->security_view);
247 /* The secure authentication widgets */
248 ppriv->auth_view = modest_toolkit_factory_create_secureauth_selector (modest_runtime_get_toolkit_factory ());
249 auth_caption = modest_toolkit_utils_create_captioned (title_size_group, value_size_group,
250 _("mcen_li_emailsetup_secure_authentication"), FALSE,
256 /* Username widgets */
257 ppriv->user_entry = GTK_WIDGET (modest_validating_entry_new ());
259 /* Auto-capitalization is the default, so let's turn it off: */
261 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->user_entry),
262 HILDON_GTK_INPUT_MODE_FULL);
265 user_label = g_strdup_printf("%s*", _("mail_fi_username"));
266 user_caption = modest_toolkit_utils_create_captioned (title_size_group, value_size_group,
271 /* Prevent the use of some characters. Limit the max
273 modest_validating_entry_set_unallowed_characters_whitespace (
274 MODEST_VALIDATING_ENTRY (ppriv->user_entry));
275 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
276 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
279 /* Password widgets */
280 ppriv->pwd_entry = gtk_entry_new ();
282 /* Auto-capitalization is the default, so let's turn it off */
284 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
285 HILDON_GTK_INPUT_MODE_FULL |
286 HILDON_GTK_INPUT_MODE_INVISIBLE);
288 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
290 pwd_caption = modest_toolkit_utils_create_captioned (title_size_group, value_size_group,
291 _("mail_fi_password"), FALSE,
294 ppriv->port_view = modest_toolkit_factory_create_number_entry (modest_runtime_get_toolkit_factory (),
296 port_caption = modest_toolkit_utils_create_captioned (title_size_group, value_size_group,
297 _("mcen_fi_emailsetup_port"), FALSE,
301 /* Track changes in UI */
302 if (GTK_IS_COMBO_BOX (ppriv->security_view)) {
303 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
304 G_CALLBACK (on_security_changed), self);
306 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
307 G_CALLBACK (on_security_changed), self);
310 if (GTK_IS_COMBO_BOX (ppriv->auth_view)) {
311 g_signal_connect (G_OBJECT (ppriv->auth_view), "changed",
312 G_CALLBACK (on_auth_changed), self);
314 g_signal_connect (G_OBJECT (ppriv->auth_view), "value-changed",
315 G_CALLBACK (on_auth_changed), self);
317 g_signal_connect (G_OBJECT (ppriv->user_entry), "changed",
318 G_CALLBACK (on_entry_changed), self);
321 /* Initialize widgets */
322 modest_serversecurity_selector_set_active_serversecurity (
323 ppriv->security_view,
324 MODEST_PROTOCOLS_CONNECTION_NONE);
325 modest_secureauth_selector_set_active_secureauth (
327 MODEST_PROTOCOLS_AUTH_NONE);
329 /* Pack into container */
331 gtk_box_pack_start (GTK_BOX (self), auth_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
332 gtk_box_pack_start (GTK_BOX (self), user_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
333 gtk_box_pack_start (GTK_BOX (self), pwd_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
334 gtk_box_pack_start (GTK_BOX (self), sec_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
335 gtk_box_pack_start (GTK_BOX (self), port_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
337 /* The order is different */
338 gtk_box_pack_start (GTK_BOX (self), sec_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
339 gtk_box_pack_start (GTK_BOX (self), auth_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
344 gtk_widget_show (ppriv->pwd_entry);
345 gtk_widget_show (ppriv->user_entry);
346 gtk_widget_show (ppriv->port_view);
347 gtk_widget_show (pwd_caption);
348 gtk_widget_show (user_caption);
349 gtk_widget_show (port_caption);
351 gtk_widget_show (ppriv->security_view);
352 gtk_widget_show (ppriv->auth_view);
353 gtk_widget_show (sec_caption);
354 gtk_widget_show (auth_caption);
358 modest_gtk_security_options_view_new (ModestSecurityOptionsType type,
360 GtkSizeGroup *title_size_group,
361 GtkSizeGroup *value_size_group)
363 ModestSecurityOptionsView* self;
364 ModestSecurityOptionsViewPrivate *ppriv;
366 self = (ModestSecurityOptionsView *)
367 g_object_new (MODEST_TYPE_GTK_SECURITY_OPTIONS_VIEW, NULL);
368 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
372 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
373 create_incoming_security (self, title_size_group, value_size_group);
375 create_outgoing_security (self, title_size_group, value_size_group);
377 return (GtkWidget *) self;
381 modest_gtk_security_options_view_load_settings (ModestSecurityOptionsView* self,
382 ModestAccountSettings *settings)
384 ModestSecurityOptionsViewPrivate *ppriv;
385 ModestServerAccountSettings *server_settings;
388 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
390 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
391 server_settings = modest_account_settings_get_store_settings (settings);
393 server_settings = modest_account_settings_get_transport_settings (settings);
394 port_number = modest_server_account_settings_get_port (server_settings);
396 if (port_number == 0) {
397 /* Show the appropriate port number */
398 on_security_changed (ppriv->security_view,
399 MODEST_GTK_SECURITY_OPTIONS_VIEW (self));
400 } else if (ppriv->full) {
401 /* Keep the user-entered port-number, or the
402 * already-appropriate automatic port number */
403 modest_number_entry_set_value (ppriv->port_view,
407 g_object_unref (server_settings);
411 modest_gtk_security_options_view_save_settings (ModestSecurityOptionsView* self,
412 ModestAccountSettings *settings)
414 ModestServerAccountSettings *server_settings;
415 ModestSecurityOptionsViewPrivate *ppriv;
418 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
420 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
421 server_settings = modest_account_settings_get_store_settings (settings);
423 server_settings = modest_account_settings_get_transport_settings (settings);
426 server_port = modest_number_entry_get_value (ppriv->port_view);
428 server_port = modest_serversecurity_selector_get_active_serversecurity_port (ppriv->security_view);
431 modest_server_account_settings_set_port (server_settings, server_port);
434 g_object_unref (server_settings);
438 modest_gtk_security_options_view_changed (ModestSecurityOptionsView* self,
439 ModestAccountSettings *settings)
441 ModestServerAccountSettings *server_settings;
442 ModestSecurityOptionsViewPrivate *ppriv;
445 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
447 /* If we're not showing the port number then it never changes */
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 modest_number_entry_get_value (ppriv->port_view);
460 g_object_unref (server_settings);
462 if (server_port != ppriv->initial_state.port)
469 modest_security_options_view_has_missing_mandatory_data (ModestSecurityOptionsView* self)
471 ModestGtkSecurityOptionsViewPrivate *priv;
473 priv = MODEST_GTK_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
475 return priv->missing_data;
479 modest_gtk_security_options_view_init (ModestGtkSecurityOptionsView *obj)
484 modest_gtk_security_options_view_finalize (GObject *obj)
486 G_OBJECT_CLASS (modest_gtk_security_options_view_parent_class)->finalize (obj);
491 modest_gtk_security_options_view_class_init (ModestGtkSecurityOptionsViewClass *klass)
493 GObjectClass *gobject_class = (GObjectClass*) klass;
495 modest_gtk_security_options_view_parent_class = g_type_class_peek_parent (klass);
497 g_type_class_add_private (gobject_class, sizeof (ModestGtkSecurityOptionsViewPrivate));
498 gobject_class->finalize = modest_gtk_security_options_view_finalize;
500 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->load_settings =
501 modest_gtk_security_options_view_load_settings;
502 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->save_settings =
503 modest_gtk_security_options_view_save_settings;
504 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->changed =
505 modest_gtk_security_options_view_changed;