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 "widgets/modest-secureauth-combo-box.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 ModestSecureauthComboBox *combo;
107 ModestProtocolRegistry *protocol_registry;
108 ModestProtocolType auth_proto;
109 gboolean secureauth_used;
110 GtkWidget *user_caption, *pwd_caption;
112 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
113 protocol_registry = modest_runtime_get_protocol_registry ();
114 combo = MODEST_SECUREAUTH_COMBO_BOX (ppriv->auth_view);
116 auth_proto = modest_secureauth_combo_box_get_active_secureauth (combo);
117 secureauth_used = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
120 /* Get captions, well dimm the whole widget */
121 user_caption = gtk_widget_get_parent (ppriv->user_entry);
122 pwd_caption = gtk_widget_get_parent (ppriv->pwd_entry);
124 /* Enable / disable */
125 gtk_widget_set_sensitive (user_caption, secureauth_used);
126 gtk_widget_set_sensitive (pwd_caption, secureauth_used);
128 /* Check if mandatory data is missing */
129 on_entry_changed (GTK_EDITABLE (ppriv->user_entry), (gpointer) self);
133 create_incoming_security (ModestSecurityOptionsView* self,
134 GtkSizeGroup *title_size_group, GtkSizeGroup *value_size_group)
136 ModestSecurityOptionsViewPrivate *ppriv;
137 GtkWidget *combo_caption, *entry_caption = NULL;
139 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
141 /* Create widgets for incoming security */
142 ppriv->security_view = modest_toolkit_factory_create_serversecurity_selector (modest_runtime_get_toolkit_factory ());
143 combo_caption = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
144 _("mcen_li_emailsetup_secure_connection"), FALSE,
145 ppriv->security_view);
148 ppriv->port_view = modest_toolkit_factory_create_number_entry (modest_runtime_get_toolkit_factory (), PORT_MIN, PORT_MAX);
149 entry_caption = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
150 _("mcen_fi_emailsetup_port"), FALSE,
154 ppriv->auth_view = modest_toolkit_factory_create_check_button (modest_runtime_get_toolkit_factory (),
155 _("mcen_li_emailsetup_secure_authentication"));
157 /* Track changes in UI */
158 if (GTK_IS_COMBO_BOX (ppriv->security_view)) {
159 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
160 G_CALLBACK (on_security_changed), self);
162 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
163 G_CALLBACK (on_security_changed), self);
166 /* Pack into container */
167 gtk_box_pack_start (GTK_BOX (self), combo_caption,
168 FALSE, FALSE, MODEST_MARGIN_HALF);
170 gtk_box_pack_start (GTK_BOX (self), entry_caption,
171 FALSE, FALSE, MODEST_MARGIN_HALF);
172 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view,
173 FALSE, FALSE, MODEST_MARGIN_HALF);
177 gtk_widget_show (ppriv->port_view);
178 gtk_widget_show (entry_caption);
180 gtk_widget_show (ppriv->security_view);
181 gtk_widget_show (ppriv->auth_view);
182 gtk_widget_show (combo_caption);
186 on_entry_max (ModestValidatingEntry *self,
189 modest_platform_information_banner (GTK_WIDGET (self), NULL,
190 _CS("ckdg_ib_maximum_characters_reached"));
194 * TODO: call this whenever the auth combo changes. If we set it
195 * explicitely at the beggining to a value then there is no need to
196 * call this handler directly at the beginning
199 on_entry_changed (GtkEditable *editable,
202 ModestSecurityOptionsView* self;
203 ModestGtkSecurityOptionsViewPrivate *priv;
204 ModestSecurityOptionsViewPrivate *ppriv;
205 ModestProtocolType auth_proto;
206 ModestSecureauthComboBox *combo;
208 ModestProtocolRegistry *protocol_registry;
210 self = MODEST_SECURITY_OPTIONS_VIEW (user_data);
211 priv = MODEST_GTK_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
212 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
213 protocol_registry = modest_runtime_get_protocol_registry ();
215 /* Outgoing username is mandatory if outgoing auth is secure */
216 combo = MODEST_SECUREAUTH_COMBO_BOX (ppriv->auth_view);
217 auth_proto = modest_secureauth_combo_box_get_active_secureauth (combo);
218 is_secure = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
222 !g_ascii_strcasecmp (gtk_entry_get_text (GTK_ENTRY (ppriv->user_entry)), "")) {
223 priv->missing_data = TRUE;
225 priv->missing_data = FALSE;
228 /* Emit a signal to notify if mandatory data is missing */
229 g_signal_emit_by_name (G_OBJECT (self), "missing_mandatory_data",
230 priv->missing_data, NULL);
234 create_outgoing_security (ModestSecurityOptionsView* self,
235 GtkSizeGroup *title_size_group, GtkSizeGroup *value_size_group)
237 ModestSecurityOptionsViewPrivate *ppriv;
238 GtkWidget *sec_caption, *auth_caption, *user_caption = NULL;
239 GtkWidget *pwd_caption = NULL, *port_caption = NULL;
241 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
243 /* The secure connection widgets */
244 ppriv->security_view = modest_toolkit_factory_create_serversecurity_selector (modest_runtime_get_toolkit_factory ());
245 modest_serversecurity_selector_fill (ppriv->security_view,
246 MODEST_PROTOCOLS_TRANSPORT_SMTP);
247 sec_caption = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
248 _("mcen_li_emailsetup_secure_connection"), FALSE,
249 ppriv->security_view);
251 /* The secure authentication widgets */
252 ppriv->auth_view = GTK_WIDGET (modest_secureauth_combo_box_new ());
253 auth_caption = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
254 _("mcen_li_emailsetup_secure_authentication"), FALSE,
260 /* Username widgets */
261 ppriv->user_entry = GTK_WIDGET (modest_validating_entry_new ());
263 /* Auto-capitalization is the default, so let's turn it off: */
264 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->user_entry),
265 HILDON_GTK_INPUT_MODE_FULL);
267 user_label = g_strdup_printf("%s*", _("mail_fi_username"));
268 user_caption = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
273 /* Prevent the use of some characters. Limit the max
275 modest_validating_entry_set_unallowed_characters_whitespace (
276 MODEST_VALIDATING_ENTRY (ppriv->user_entry));
277 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
278 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
281 /* Password widgets */
282 ppriv->pwd_entry = gtk_entry_new ();
284 /* Auto-capitalization is the default, so let's turn it off */
285 #ifdef MODEST_TOOLKIT_HILDON2
286 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
287 HILDON_GTK_INPUT_MODE_FULL |
288 HILDON_GTK_INPUT_MODE_INVISIBLE);
290 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
292 pwd_caption = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
293 _("mail_fi_password"), FALSE,
296 ppriv->port_view = modest_toolkit_factory_create_number_entry (modest_runtime_get_toolkit_factory (),
298 port_caption = modest_maemo_utils_create_captioned (title_size_group, value_size_group,
299 _("mcen_fi_emailsetup_port"), FALSE,
303 /* Track changes in UI */
304 if (GTK_IS_COMBO_BOX (ppriv->security_view)) {
305 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
306 G_CALLBACK (on_security_changed), self);
308 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
309 G_CALLBACK (on_security_changed), self);
312 g_signal_connect (G_OBJECT (ppriv->auth_view), "changed",
313 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_combo_box_set_active_secureauth (
323 MODEST_SECUREAUTH_COMBO_BOX (ppriv->auth_view),
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;