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"
40 #define PORT_MAX 65535
42 typedef struct _ModestGtkSecurityOptionsViewPrivate ModestGtkSecurityOptionsViewPrivate;
43 struct _ModestGtkSecurityOptionsViewPrivate {
44 gboolean missing_data;
47 #define MODEST_GTK_SECURITY_OPTIONS_VIEW_GET_PRIVATE(o) \
48 (G_TYPE_INSTANCE_GET_PRIVATE((o), \
49 MODEST_TYPE_GTK_SECURITY_OPTIONS_VIEW, \
50 ModestGtkSecurityOptionsViewPrivate))
52 static void modest_gtk_security_options_view_init (ModestGtkSecurityOptionsView *obj);
53 static void modest_gtk_security_options_view_finalize (GObject *obj);
54 static void modest_gtk_security_options_view_class_init (ModestGtkSecurityOptionsViewClass *klass);
56 G_DEFINE_TYPE (ModestGtkSecurityOptionsView,
57 modest_gtk_security_options_view,
58 MODEST_TYPE_SECURITY_OPTIONS_VIEW);
60 static void on_entry_changed (GtkEditable *editable, gpointer user_data);
62 /* Tracks changes in the incoming security combo box */
64 on_security_changed (GtkWidget *widget,
65 ModestGtkSecurityOptionsView *self)
67 ModestSecurityOptionsViewPrivate* ppriv;
68 ModestProtocolType proto_type;
69 ModestProtocolRegistry *proto_registry;
72 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
74 proto_registry = modest_runtime_get_protocol_registry ();
75 proto_type = modest_serversecurity_selector_get_active_serversecurity (ppriv->security_view);
77 is_secure = modest_protocol_registry_protocol_type_has_tag (proto_registry, proto_type,
78 MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS);
80 if (MODEST_SECURITY_OPTIONS_VIEW (self)->type == MODEST_SECURITY_OPTIONS_INCOMING) {
81 /* Activate and dim checkbutton if it's secure */
82 modest_togglable_set_active (ppriv->auth_view,
84 gtk_widget_set_sensitive (ppriv->auth_view, !is_secure);
91 modest_serversecurity_selector_get_active_serversecurity_port (ppriv->security_view);
94 modest_number_entry_set_value (ppriv->port_view,
101 on_auth_changed (GtkWidget *widget,
102 ModestGtkSecurityOptionsView *self)
104 ModestSecurityOptionsViewPrivate* ppriv;
105 ModestProtocolRegistry *protocol_registry;
106 ModestProtocolType auth_proto;
107 gboolean secureauth_used;
108 GtkWidget *user_caption, *pwd_caption;
110 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
111 protocol_registry = modest_runtime_get_protocol_registry ();
113 auth_proto = modest_secureauth_selector_get_active_secureauth (ppriv->auth_view);
114 secureauth_used = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
117 /* Get captions, well dimm the whole widget */
118 user_caption = gtk_widget_get_parent (ppriv->user_entry);
119 pwd_caption = gtk_widget_get_parent (ppriv->pwd_entry);
121 /* Enable / disable */
122 gtk_widget_set_sensitive (user_caption, secureauth_used);
123 gtk_widget_set_sensitive (pwd_caption, secureauth_used);
125 /* Check if mandatory data is missing */
126 on_entry_changed (GTK_EDITABLE (ppriv->user_entry), (gpointer) self);
130 create_incoming_security (ModestSecurityOptionsView* self,
131 GtkSizeGroup *title_size_group, GtkSizeGroup *value_size_group)
133 ModestSecurityOptionsViewPrivate *ppriv;
134 GtkWidget *combo_caption, *entry_caption = NULL;
136 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
138 /* Create widgets for incoming security */
139 ppriv->security_view = modest_toolkit_factory_create_serversecurity_selector (modest_runtime_get_toolkit_factory ());
140 combo_caption = modest_toolkit_utils_create_captioned (title_size_group, value_size_group,
141 _("mcen_li_emailsetup_secure_connection"), FALSE,
142 ppriv->security_view);
145 ppriv->port_view = modest_toolkit_factory_create_number_entry (modest_runtime_get_toolkit_factory (), PORT_MIN, PORT_MAX);
146 entry_caption = modest_toolkit_utils_create_captioned (title_size_group, value_size_group,
147 _("mcen_fi_emailsetup_port"), FALSE,
151 ppriv->auth_view = modest_toolkit_factory_create_check_button (modest_runtime_get_toolkit_factory (),
152 _("mcen_li_emailsetup_secure_authentication"));
154 /* Track changes in UI */
155 if (GTK_IS_COMBO_BOX (ppriv->security_view)) {
156 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
157 G_CALLBACK (on_security_changed), self);
159 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
160 G_CALLBACK (on_security_changed), self);
163 /* Pack into container */
164 gtk_box_pack_start (GTK_BOX (self), combo_caption,
165 FALSE, FALSE, MODEST_MARGIN_HALF);
167 gtk_box_pack_start (GTK_BOX (self), entry_caption,
168 FALSE, FALSE, MODEST_MARGIN_HALF);
169 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view,
170 FALSE, FALSE, MODEST_MARGIN_HALF);
174 gtk_widget_show (ppriv->port_view);
175 gtk_widget_show (entry_caption);
177 gtk_widget_show (ppriv->security_view);
178 gtk_widget_show (ppriv->auth_view);
179 gtk_widget_show (combo_caption);
183 on_entry_max (ModestValidatingEntry *self,
186 modest_platform_information_banner (GTK_WIDGET (self), NULL,
187 _CS("ckdg_ib_maximum_characters_reached"));
191 * TODO: call this whenever the auth combo changes. If we set it
192 * explicitely at the beggining to a value then there is no need to
193 * call this handler directly at the beginning
196 on_entry_changed (GtkEditable *editable,
199 ModestSecurityOptionsView* self;
200 ModestGtkSecurityOptionsViewPrivate *priv;
201 ModestSecurityOptionsViewPrivate *ppriv;
202 ModestProtocolType auth_proto;
204 ModestProtocolRegistry *protocol_registry;
206 self = MODEST_SECURITY_OPTIONS_VIEW (user_data);
207 priv = MODEST_GTK_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
208 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
209 protocol_registry = modest_runtime_get_protocol_registry ();
211 /* Outgoing username is mandatory if outgoing auth is secure */
212 auth_proto = modest_secureauth_selector_get_active_secureauth (ppriv->auth_view);
213 is_secure = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
217 !g_ascii_strcasecmp (gtk_entry_get_text (GTK_ENTRY (ppriv->user_entry)), "")) {
218 priv->missing_data = TRUE;
220 priv->missing_data = FALSE;
223 /* Emit a signal to notify if mandatory data is missing */
224 g_signal_emit_by_name (G_OBJECT (self), "missing_mandatory_data",
225 priv->missing_data, NULL);
229 create_outgoing_security (ModestSecurityOptionsView* self,
230 GtkSizeGroup *title_size_group, GtkSizeGroup *value_size_group)
232 ModestSecurityOptionsViewPrivate *ppriv;
233 GtkWidget *sec_caption, *auth_caption, *user_caption = NULL;
234 GtkWidget *pwd_caption = NULL, *port_caption = NULL;
236 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
238 /* The secure connection widgets */
239 ppriv->security_view = modest_toolkit_factory_create_serversecurity_selector (modest_runtime_get_toolkit_factory ());
240 modest_serversecurity_selector_fill (ppriv->security_view,
241 MODEST_PROTOCOLS_TRANSPORT_SMTP);
242 sec_caption = modest_toolkit_utils_create_captioned (title_size_group, value_size_group,
243 _("mcen_li_emailsetup_secure_connection"), FALSE,
244 ppriv->security_view);
246 /* The secure authentication widgets */
247 ppriv->auth_view = modest_toolkit_factory_create_secureauth_selector (modest_runtime_get_toolkit_factory ());
248 auth_caption = modest_toolkit_utils_create_captioned (title_size_group, value_size_group,
249 _("mcen_li_emailsetup_secure_authentication"), FALSE,
255 /* Username widgets */
256 ppriv->user_entry = GTK_WIDGET (modest_validating_entry_new ());
258 /* Auto-capitalization is the default, so let's turn it off: */
259 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->user_entry),
260 HILDON_GTK_INPUT_MODE_FULL);
262 user_label = g_strdup_printf("%s*", _("mail_fi_username"));
263 user_caption = modest_toolkit_utils_create_captioned (title_size_group, value_size_group,
268 /* Prevent the use of some characters. Limit the max
270 modest_validating_entry_set_unallowed_characters_whitespace (
271 MODEST_VALIDATING_ENTRY (ppriv->user_entry));
272 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
273 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
276 /* Password widgets */
277 ppriv->pwd_entry = gtk_entry_new ();
279 /* Auto-capitalization is the default, so let's turn it off */
280 #ifdef MODEST_TOOLKIT_HILDON2
281 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
282 HILDON_GTK_INPUT_MODE_FULL |
283 HILDON_GTK_INPUT_MODE_INVISIBLE);
285 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
287 pwd_caption = modest_toolkit_utils_create_captioned (title_size_group, value_size_group,
288 _("mail_fi_password"), FALSE,
291 ppriv->port_view = modest_toolkit_factory_create_number_entry (modest_runtime_get_toolkit_factory (),
293 port_caption = modest_toolkit_utils_create_captioned (title_size_group, value_size_group,
294 _("mcen_fi_emailsetup_port"), FALSE,
298 /* Track changes in UI */
299 if (GTK_IS_COMBO_BOX (ppriv->security_view)) {
300 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
301 G_CALLBACK (on_security_changed), self);
303 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
304 G_CALLBACK (on_security_changed), self);
307 if (GTK_IS_COMBO_BOX (ppriv->auth_view)) {
308 g_signal_connect (G_OBJECT (ppriv->auth_view), "changed",
309 G_CALLBACK (on_auth_changed), self);
311 g_signal_connect (G_OBJECT (ppriv->auth_view), "value-changed",
312 G_CALLBACK (on_auth_changed), self);
314 g_signal_connect (G_OBJECT (ppriv->user_entry), "changed",
315 G_CALLBACK (on_entry_changed), self);
318 /* Initialize widgets */
319 modest_serversecurity_selector_set_active_serversecurity (
320 ppriv->security_view,
321 MODEST_PROTOCOLS_CONNECTION_NONE);
322 modest_secureauth_selector_set_active_secureauth (
324 MODEST_PROTOCOLS_AUTH_NONE);
326 /* Pack into container */
328 gtk_box_pack_start (GTK_BOX (self), auth_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
329 gtk_box_pack_start (GTK_BOX (self), user_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
330 gtk_box_pack_start (GTK_BOX (self), pwd_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
331 gtk_box_pack_start (GTK_BOX (self), sec_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
332 gtk_box_pack_start (GTK_BOX (self), port_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
334 /* The order is different */
335 gtk_box_pack_start (GTK_BOX (self), sec_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
336 gtk_box_pack_start (GTK_BOX (self), auth_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
341 gtk_widget_show (ppriv->pwd_entry);
342 gtk_widget_show (ppriv->user_entry);
343 gtk_widget_show (ppriv->port_view);
344 gtk_widget_show (pwd_caption);
345 gtk_widget_show (user_caption);
346 gtk_widget_show (port_caption);
348 gtk_widget_show (ppriv->security_view);
349 gtk_widget_show (ppriv->auth_view);
350 gtk_widget_show (sec_caption);
351 gtk_widget_show (auth_caption);
355 modest_gtk_security_options_view_new (ModestSecurityOptionsType type,
357 GtkSizeGroup *title_size_group,
358 GtkSizeGroup *value_size_group)
360 ModestSecurityOptionsView* self;
361 ModestSecurityOptionsViewPrivate *ppriv;
363 self = (ModestSecurityOptionsView *)
364 g_object_new (MODEST_TYPE_GTK_SECURITY_OPTIONS_VIEW, NULL);
365 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
369 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
370 create_incoming_security (self, title_size_group, value_size_group);
372 create_outgoing_security (self, title_size_group, value_size_group);
374 return (GtkWidget *) self;
378 modest_gtk_security_options_view_load_settings (ModestSecurityOptionsView* self,
379 ModestAccountSettings *settings)
381 ModestSecurityOptionsViewPrivate *ppriv;
382 ModestServerAccountSettings *server_settings;
385 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
387 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
388 server_settings = modest_account_settings_get_store_settings (settings);
390 server_settings = modest_account_settings_get_transport_settings (settings);
391 port_number = modest_server_account_settings_get_port (server_settings);
393 if (port_number == 0) {
394 /* Show the appropriate port number */
395 on_security_changed (ppriv->security_view,
396 MODEST_GTK_SECURITY_OPTIONS_VIEW (self));
397 } else if (ppriv->full) {
398 /* Keep the user-entered port-number, or the
399 * already-appropriate automatic port number */
400 modest_number_entry_set_value (ppriv->port_view,
404 g_object_unref (server_settings);
408 modest_gtk_security_options_view_save_settings (ModestSecurityOptionsView* self,
409 ModestAccountSettings *settings)
411 ModestServerAccountSettings *server_settings;
412 ModestSecurityOptionsViewPrivate *ppriv;
415 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
417 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
418 server_settings = modest_account_settings_get_store_settings (settings);
420 server_settings = modest_account_settings_get_transport_settings (settings);
423 server_port = modest_number_entry_get_value (ppriv->port_view);
425 server_port = modest_serversecurity_selector_get_active_serversecurity_port (ppriv->security_view);
428 modest_server_account_settings_set_port (server_settings, server_port);
431 g_object_unref (server_settings);
435 modest_gtk_security_options_view_changed (ModestSecurityOptionsView* self,
436 ModestAccountSettings *settings)
438 ModestServerAccountSettings *server_settings;
439 ModestSecurityOptionsViewPrivate *ppriv;
442 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
444 /* If we're not showing the port number then it never changes */
448 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
449 server_settings = modest_account_settings_get_store_settings (settings);
451 server_settings = modest_account_settings_get_transport_settings (settings);
454 modest_number_entry_get_value (ppriv->port_view);
457 g_object_unref (server_settings);
459 if (server_port != ppriv->initial_state.port)
466 modest_security_options_view_has_missing_mandatory_data (ModestSecurityOptionsView* self)
468 ModestGtkSecurityOptionsViewPrivate *priv;
470 priv = MODEST_GTK_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
472 return priv->missing_data;
476 modest_gtk_security_options_view_init (ModestGtkSecurityOptionsView *obj)
481 modest_gtk_security_options_view_finalize (GObject *obj)
483 G_OBJECT_CLASS (modest_gtk_security_options_view_parent_class)->finalize (obj);
488 modest_gtk_security_options_view_class_init (ModestGtkSecurityOptionsViewClass *klass)
490 GObjectClass *gobject_class = (GObjectClass*) klass;
492 modest_gtk_security_options_view_parent_class = g_type_class_peek_parent (klass);
494 g_type_class_add_private (gobject_class, sizeof (ModestGtkSecurityOptionsViewPrivate));
495 gobject_class->finalize = modest_gtk_security_options_view_finalize;
497 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->load_settings =
498 modest_gtk_security_options_view_load_settings;
499 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->save_settings =
500 modest_gtk_security_options_view_save_settings;
501 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->changed =
502 modest_gtk_security_options_view_changed;