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-number-editor.h>
42 #include <hildon/hildon-check-button.h>
43 #include "modest-hildon-includes.h"
46 #define PORT_MAX 65535
48 typedef struct _ModestMaemoSecurityOptionsViewPrivate ModestMaemoSecurityOptionsViewPrivate;
49 struct _ModestMaemoSecurityOptionsViewPrivate {
50 gboolean missing_data;
53 #define MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE(o) \
54 (G_TYPE_INSTANCE_GET_PRIVATE((o), \
55 MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, \
56 ModestMaemoSecurityOptionsViewPrivate))
58 static void modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj);
59 static void modest_maemo_security_options_view_finalize (GObject *obj);
60 static void modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass);
62 G_DEFINE_TYPE (ModestMaemoSecurityOptionsView,
63 modest_maemo_security_options_view,
64 MODEST_TYPE_SECURITY_OPTIONS_VIEW);
66 static void on_entry_changed (GtkEditable *editable, gpointer user_data);
68 /* Tracks changes in the incoming security picker */
70 on_security_changed (GtkWidget *widget,
71 ModestMaemoSecurityOptionsView *self)
73 ModestSecurityOptionsViewPrivate* ppriv;
74 ModestServersecurityPicker *picker;
75 ModestProtocolType proto_type;
76 ModestProtocolRegistry *proto_registry;
79 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
81 proto_registry = modest_runtime_get_protocol_registry ();
82 picker = MODEST_SERVERSECURITY_PICKER (ppriv->security_view);
83 proto_type = modest_serversecurity_picker_get_active_serversecurity (picker);
85 is_secure = modest_protocol_registry_protocol_type_has_tag (proto_registry, proto_type,
86 MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS);
88 if (MODEST_SECURITY_OPTIONS_VIEW (self)->type == MODEST_SECURITY_OPTIONS_INCOMING) {
89 /* Activate and dim checkbutton if it's secure */
90 hildon_check_button_set_active (HILDON_CHECK_BUTTON (ppriv->auth_view),
92 gtk_widget_set_sensitive (ppriv->auth_view, !is_secure);
99 modest_serversecurity_picker_get_active_serversecurity_port (MODEST_SERVERSECURITY_PICKER (ppriv->security_view));
102 modest_number_editor_set_value (MODEST_NUMBER_EDITOR (ppriv->port_view),
109 on_auth_changed (GtkWidget *widget,
110 ModestMaemoSecurityOptionsView *self)
112 ModestSecurityOptionsViewPrivate* ppriv;
113 ModestSecureauthPicker *picker;
114 ModestProtocolRegistry *protocol_registry;
115 ModestProtocolType auth_proto;
116 gboolean secureauth_used;
117 GtkWidget *user_caption, *pwd_caption;
119 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
120 protocol_registry = modest_runtime_get_protocol_registry ();
121 picker = MODEST_SECUREAUTH_PICKER (ppriv->auth_view);
123 auth_proto = modest_secureauth_picker_get_active_secureauth (picker);
124 secureauth_used = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
127 /* Get captions, well dimm the whole widget */
128 user_caption = gtk_widget_get_parent (ppriv->user_entry);
129 pwd_caption = gtk_widget_get_parent (ppriv->pwd_entry);
131 /* Enable / disable */
132 gtk_widget_set_sensitive (user_caption, secureauth_used);
133 gtk_widget_set_sensitive (pwd_caption, secureauth_used);
135 /* Check if mandatory data is missing */
136 on_entry_changed (GTK_EDITABLE (ppriv->user_entry), (gpointer) self);
140 create_incoming_security (ModestSecurityOptionsView* self,
141 GtkSizeGroup *title_size_group,
142 GtkSizeGroup *value_size_group)
144 ModestSecurityOptionsViewPrivate *ppriv;
145 GtkWidget *entry_caption = NULL;
147 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
149 /* Create widgets for incoming security */
150 ppriv->security_view = GTK_WIDGET (modest_serversecurity_picker_new (MODEST_EDITABLE_SIZE,
151 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
152 modest_serversecurity_picker_fill (MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
153 modest_protocol_registry_get_pop_type_id ());
154 modest_maemo_utils_set_hbutton_layout (title_size_group,
156 _("mcen_li_emailsetup_secure_connection"),
157 ppriv->security_view);
160 ppriv->port_view = GTK_WIDGET (modest_number_editor_new (PORT_MIN, PORT_MAX));
161 entry_caption = modest_maemo_utils_create_captioned (title_size_group,
163 _("mcen_fi_emailsetup_port"),
168 ppriv->auth_view = hildon_check_button_new (MODEST_EDITABLE_SIZE);
169 gtk_button_set_label (GTK_BUTTON (ppriv->auth_view), _("mcen_li_emailsetup_secure_authentication"));
170 gtk_button_set_alignment (GTK_BUTTON (ppriv->auth_view), 0.0, 0.5);
172 /* Track changes in UI */
173 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
174 G_CALLBACK (on_security_changed), self);
176 /* Pack into container */
177 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view,
180 gtk_box_pack_start (GTK_BOX (self), entry_caption,
181 FALSE, FALSE, MODEST_MARGIN_HALF);
182 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view,
187 gtk_widget_show (ppriv->port_view);
188 gtk_widget_show (entry_caption);
190 gtk_widget_show (ppriv->security_view);
191 gtk_widget_show (ppriv->auth_view);
195 on_entry_max (ModestValidatingEntry *self,
198 modest_platform_information_banner (GTK_WIDGET (self), NULL,
199 _CS("ckdg_ib_maximum_characters_reached"));
203 * TODO: call this whenever the auth picker changes. If we set it
204 * explicitely at the beggining to a value then there is no need to
205 * call this handler directly at the beginning
208 on_entry_changed (GtkEditable *editable,
211 ModestSecurityOptionsView* self;
212 ModestMaemoSecurityOptionsViewPrivate *priv;
213 ModestSecurityOptionsViewPrivate *ppriv;
214 ModestProtocolType auth_proto;
215 ModestSecureauthPicker *picker;
217 ModestProtocolRegistry *protocol_registry;
219 self = MODEST_SECURITY_OPTIONS_VIEW (user_data);
220 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
221 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
222 protocol_registry = modest_runtime_get_protocol_registry ();
224 /* Outgoing username is mandatory if outgoing auth is secure */
225 picker = MODEST_SECUREAUTH_PICKER (ppriv->auth_view);
226 auth_proto = modest_secureauth_picker_get_active_secureauth (picker);
227 is_secure = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
231 !g_ascii_strcasecmp (hildon_entry_get_text (HILDON_ENTRY (ppriv->user_entry)), "")) {
232 priv->missing_data = TRUE;
234 priv->missing_data = FALSE;
237 if (!priv->missing_data &&
239 !modest_number_editor_is_valid (MODEST_NUMBER_EDITOR (ppriv->port_view)))
240 priv->missing_data = TRUE;
242 /* Emit a signal to notify if mandatory data is missing */
243 g_signal_emit_by_name (G_OBJECT (self), "missing_mandatory_data",
244 priv->missing_data, NULL);
248 on_valid_changed (ModestNumberEditor *editor,
250 ModestSecurityOptionsView *self)
252 on_entry_changed (NULL, (gpointer) self);
256 create_outgoing_security (ModestSecurityOptionsView* self,
257 GtkSizeGroup *title_size_group,
258 GtkSizeGroup *value_size_group)
260 ModestSecurityOptionsViewPrivate *ppriv;
261 GtkWidget *user_caption = NULL;
262 GtkWidget *pwd_caption = NULL, *port_caption = NULL;
264 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
266 /* The secure connection widgets */
267 ppriv->security_view = GTK_WIDGET (modest_serversecurity_picker_new (MODEST_EDITABLE_SIZE,
268 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
269 modest_serversecurity_picker_fill (MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
270 MODEST_PROTOCOLS_TRANSPORT_SMTP);
271 modest_maemo_utils_set_hbutton_layout (title_size_group,
273 _("mcen_li_emailsetup_secure_connection"),
274 ppriv->security_view);
276 /* The secure authentication widgets */
277 ppriv->auth_view = GTK_WIDGET (modest_secureauth_picker_new (MODEST_EDITABLE_SIZE,
278 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
279 modest_maemo_utils_set_hbutton_layout (title_size_group,
281 _("mcen_li_emailsetup_secure_authentication"),
287 /* Username widgets */
288 ppriv->user_entry = GTK_WIDGET (modest_validating_entry_new ());
290 /* Auto-capitalization is the default, so let's turn it off: */
291 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->user_entry),
292 HILDON_GTK_INPUT_MODE_FULL);
294 user_label = g_strdup_printf("%s*", _("mail_fi_username"));
295 user_caption = modest_maemo_utils_create_captioned (title_size_group,
302 /* Prevent the use of some characters. Limit the max
304 modest_validating_entry_set_unallowed_characters_whitespace (
305 MODEST_VALIDATING_ENTRY (ppriv->user_entry));
306 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
307 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
310 /* Password widgets */
311 ppriv->pwd_entry = hildon_entry_new (MODEST_EDITABLE_SIZE);
313 /* Auto-capitalization is the default, so let's turn it off */
314 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
315 HILDON_GTK_INPUT_MODE_FULL |
316 HILDON_GTK_INPUT_MODE_INVISIBLE);
317 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
319 pwd_caption = modest_maemo_utils_create_captioned (title_size_group,
321 _("mail_fi_password"),
325 ppriv->port_view = GTK_WIDGET (modest_number_editor_new (PORT_MIN, PORT_MAX));
326 port_caption = modest_maemo_utils_create_captioned (title_size_group,
328 _("mcen_fi_emailsetup_port"),
333 /* Track changes in UI */
334 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
335 G_CALLBACK (on_security_changed), self);
337 g_signal_connect (G_OBJECT (ppriv->auth_view), "value-changed",
338 G_CALLBACK (on_auth_changed), self);
339 g_signal_connect (G_OBJECT (ppriv->user_entry), "changed",
340 G_CALLBACK (on_entry_changed), self);
341 g_signal_connect (G_OBJECT (ppriv->port_view), "valid-changed",
342 G_CALLBACK (on_valid_changed), self);
345 /* Initialize widgets */
346 modest_serversecurity_picker_set_active_serversecurity (
347 MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
348 MODEST_PROTOCOLS_CONNECTION_NONE);
349 modest_secureauth_picker_set_active_secureauth (
350 MODEST_SECUREAUTH_PICKER (ppriv->auth_view),
351 MODEST_PROTOCOLS_AUTH_NONE);
353 /* Pack into container */
355 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, MODEST_MARGIN_HALF);
356 gtk_box_pack_start (GTK_BOX (self), user_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
357 gtk_box_pack_start (GTK_BOX (self), pwd_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
358 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, MODEST_MARGIN_HALF);
359 gtk_box_pack_start (GTK_BOX (self), port_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
361 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, MODEST_MARGIN_HALF);
362 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, MODEST_MARGIN_HALF);
367 gtk_widget_show (ppriv->pwd_entry);
368 gtk_widget_show (ppriv->user_entry);
369 gtk_widget_show (ppriv->port_view);
370 gtk_widget_show (pwd_caption);
371 gtk_widget_show (user_caption);
372 gtk_widget_show (port_caption);
374 gtk_widget_show (ppriv->security_view);
375 gtk_widget_show (ppriv->auth_view);
379 modest_maemo_security_options_view_new (ModestSecurityOptionsType type,
381 GtkSizeGroup *title_size_group,
382 GtkSizeGroup *value_size_group)
384 ModestSecurityOptionsView* self;
385 ModestSecurityOptionsViewPrivate *ppriv;
387 self = (ModestSecurityOptionsView *)
388 g_object_new (MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, NULL);
389 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
393 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
394 create_incoming_security (self, title_size_group, value_size_group);
396 create_outgoing_security (self, title_size_group, value_size_group);
398 return (GtkWidget *) self;
402 modest_security_options_view_has_missing_mandatory_data (ModestSecurityOptionsView* self)
404 ModestMaemoSecurityOptionsViewPrivate *priv;
406 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
408 return priv->missing_data;
412 modest_maemo_security_options_view_load_settings (ModestSecurityOptionsView* self,
413 ModestAccountSettings *settings)
415 ModestSecurityOptionsViewPrivate *ppriv;
416 ModestServerAccountSettings *server_settings;
419 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
421 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
422 server_settings = modest_account_settings_get_store_settings (settings);
424 server_settings = modest_account_settings_get_transport_settings (settings);
425 port_number = modest_server_account_settings_get_port (server_settings);
427 if (port_number == 0) {
428 /* Show the appropriate port number */
429 on_security_changed (ppriv->security_view,
430 MODEST_MAEMO_SECURITY_OPTIONS_VIEW (self));
431 } else if (ppriv->full) {
432 /* Keep the user-entered port-number, or the
433 * already-appropriate automatic port number */
434 modest_number_editor_set_value (MODEST_NUMBER_EDITOR (ppriv->port_view),
438 g_object_unref (server_settings);
442 modest_maemo_security_options_view_save_settings (ModestSecurityOptionsView* self,
443 ModestAccountSettings *settings)
445 ModestServerAccountSettings *server_settings;
446 ModestSecurityOptionsViewPrivate *ppriv;
449 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
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 server_port = modest_number_editor_get_value (MODEST_NUMBER_EDITOR (ppriv->port_view));
459 server_port = modest_serversecurity_picker_get_active_serversecurity_port (MODEST_SERVERSECURITY_PICKER (ppriv->security_view));
462 modest_server_account_settings_set_port (server_settings, server_port);
465 g_object_unref (server_settings);
469 modest_maemo_security_options_view_changed (ModestSecurityOptionsView* self,
470 ModestAccountSettings *settings)
472 ModestServerAccountSettings *server_settings;
473 ModestSecurityOptionsViewPrivate *ppriv;
476 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
478 /* If we're not showing the port number then it never changes */
482 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
483 server_settings = modest_account_settings_get_store_settings (settings);
485 server_settings = modest_account_settings_get_transport_settings (settings);
488 modest_number_editor_get_value (MODEST_NUMBER_EDITOR (ppriv->port_view));
491 g_object_unref (server_settings);
493 if (server_port != ppriv->initial_state.port)
500 modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj)
505 modest_maemo_security_options_view_finalize (GObject *obj)
507 G_OBJECT_CLASS (modest_maemo_security_options_view_parent_class)->finalize (obj);
512 modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass)
514 GObjectClass *gobject_class = (GObjectClass*) klass;
516 modest_maemo_security_options_view_parent_class = g_type_class_peek_parent (klass);
518 g_type_class_add_private (gobject_class, sizeof (ModestMaemoSecurityOptionsViewPrivate));
519 gobject_class->finalize = modest_maemo_security_options_view_finalize;
521 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->load_settings =
522 modest_maemo_security_options_view_load_settings;
523 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->save_settings =
524 modest_maemo_security_options_view_save_settings;
525 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->changed =
526 modest_maemo_security_options_view_changed;