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 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 ModestServersecurityPicker *picker;
77 ModestProtocolType proto_type;
78 ModestProtocolRegistry *proto_registry;
81 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
83 proto_registry = modest_runtime_get_protocol_registry ();
84 picker = MODEST_SERVERSECURITY_PICKER (ppriv->security_view);
85 proto_type = modest_serversecurity_picker_get_active_serversecurity (picker);
87 is_secure = modest_protocol_registry_protocol_type_has_tag (proto_registry, proto_type,
88 MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS);
92 modest_serversecurity_picker_get_active_serversecurity_port (MODEST_SERVERSECURITY_PICKER (ppriv->security_view));
95 modest_number_editor_set_value (MODEST_NUMBER_EDITOR (ppriv->port_view),
102 on_auth_changed (GtkWidget *widget,
103 ModestMaemoSecurityOptionsView *self)
105 ModestSecurityOptionsViewPrivate* ppriv;
106 ModestSecureauthPicker *picker;
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 picker = MODEST_SECUREAUTH_PICKER (ppriv->auth_view);
116 auth_proto = modest_secureauth_picker_get_active_secureauth (picker);
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,
135 GtkSizeGroup *value_size_group)
137 ModestSecurityOptionsViewPrivate *ppriv;
138 GtkWidget *entry_caption = NULL;
140 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
142 /* Create widgets for incoming security */
143 ppriv->security_view = GTK_WIDGET (modest_serversecurity_picker_new (MODEST_EDITABLE_SIZE,
144 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
145 modest_serversecurity_picker_fill (MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
146 modest_protocol_registry_get_pop_type_id ());
147 modest_maemo_utils_set_hbutton_layout (title_size_group,
149 _("mcen_li_emailsetup_secure_connection"),
150 ppriv->security_view);
152 ppriv->auth_view = hildon_check_button_new (MODEST_EDITABLE_SIZE);
153 gtk_button_set_label (GTK_BUTTON (ppriv->auth_view), _("mcen_li_emailsetup_secure_authentication"));
154 gtk_button_set_alignment (GTK_BUTTON (ppriv->auth_view), 0.0, 0.5);
156 /* Track changes in UI */
157 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
158 G_CALLBACK (on_security_changed), self);
160 /* Pack into container & show */
161 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
162 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
163 gtk_widget_show (ppriv->security_view);
164 gtk_widget_show (ppriv->auth_view);
167 ppriv->port_view = GTK_WIDGET (modest_number_editor_new (PORT_MIN, PORT_MAX));
169 modest_maemo_utils_create_captioned_with_size_type (title_size_group,
171 _("mcen_fi_emailsetup_port"),
174 MODEST_EDITABLE_SIZE);
175 /* Pack & show widgets */
176 gtk_box_pack_start (GTK_BOX (self), entry_caption, FALSE, FALSE, 0);
177 gtk_widget_show (ppriv->port_view);
178 gtk_widget_show (entry_caption);
180 /* Track changes in UI */
181 g_signal_connect (G_OBJECT (ppriv->port_view), "valid-changed",
182 G_CALLBACK (on_valid_changed), self);
187 on_entry_max (ModestValidatingEntry *self,
190 modest_platform_information_banner (GTK_WIDGET (self), NULL,
191 _CS("ckdg_ib_maximum_characters_reached"));
195 * TODO: call this whenever the auth picker changes. If we set it
196 * explicitely at the beggining to a value then there is no need to
197 * call this handler directly at the beginning
200 on_entry_changed (GtkEditable *editable,
203 ModestSecurityOptionsView* self;
204 ModestMaemoSecurityOptionsViewPrivate *priv;
205 ModestSecurityOptionsViewPrivate *ppriv;
206 ModestSecureauthPicker *picker;
207 gboolean is_secure = FALSE;
208 ModestProtocolRegistry *protocol_registry;
210 self = MODEST_SECURITY_OPTIONS_VIEW (user_data);
211 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
212 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
213 protocol_registry = modest_runtime_get_protocol_registry ();
215 /* Check if it's a secure protocol */
216 if (MODEST_IS_SECUREAUTH_PICKER (ppriv->auth_view)) {
217 ModestProtocolType auth_proto;
218 picker = MODEST_SECUREAUTH_PICKER (ppriv->auth_view);
219 auth_proto = modest_secureauth_picker_get_active_secureauth (picker);
220 is_secure = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
222 } else if (HILDON_IS_CHECK_BUTTON (ppriv->auth_view)) {
223 is_secure = hildon_check_button_get_active (HILDON_CHECK_BUTTON (ppriv->auth_view));
227 !g_strcmp0 (hildon_entry_get_text (HILDON_ENTRY (ppriv->user_entry)), "")) {
228 priv->missing_data = TRUE;
230 priv->missing_data = FALSE;
233 if (!priv->missing_data &&
235 !modest_number_editor_is_valid (MODEST_NUMBER_EDITOR (ppriv->port_view)))
236 priv->missing_data = TRUE;
238 /* Emit a signal to notify if mandatory data is missing */
239 g_signal_emit_by_name (G_OBJECT (self), "missing_mandatory_data",
240 priv->missing_data, NULL);
244 on_valid_changed (ModestNumberEditor *editor,
246 ModestSecurityOptionsView *self)
248 on_entry_changed (NULL, (gpointer) self);
252 create_outgoing_security (ModestSecurityOptionsView* self,
253 GtkSizeGroup *title_size_group,
254 GtkSizeGroup *value_size_group)
256 ModestSecurityOptionsViewPrivate *ppriv;
257 GtkWidget *user_caption = NULL;
258 GtkWidget *pwd_caption = NULL, *port_caption = NULL;
260 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
262 /* The secure connection widgets */
263 ppriv->security_view = GTK_WIDGET (modest_serversecurity_picker_new (MODEST_EDITABLE_SIZE,
264 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
265 modest_serversecurity_picker_fill (MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
266 MODEST_PROTOCOLS_TRANSPORT_SMTP);
267 modest_maemo_utils_set_hbutton_layout (title_size_group,
269 _("mcen_li_emailsetup_secure_connection"),
270 ppriv->security_view);
272 /* The secure authentication widgets */
273 ppriv->auth_view = GTK_WIDGET (modest_secureauth_picker_new (MODEST_EDITABLE_SIZE,
274 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
275 modest_maemo_utils_set_hbutton_layout (title_size_group,
277 _("mcen_li_emailsetup_secure_authentication"),
283 /* Username widgets */
284 ppriv->user_entry = GTK_WIDGET (modest_validating_entry_new ());
286 /* Auto-capitalization is the default, so let's turn it off: */
287 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->user_entry),
288 HILDON_GTK_INPUT_MODE_FULL);
290 user_label = g_strdup_printf("%s*", _("mail_fi_username"));
291 user_caption = modest_maemo_utils_create_captioned_with_size_type (title_size_group,
296 MODEST_EDITABLE_SIZE);
299 /* Prevent the use of some characters. Limit the max
301 modest_validating_entry_set_unallowed_characters_whitespace (
302 MODEST_VALIDATING_ENTRY (ppriv->user_entry));
303 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
304 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
307 /* Password widgets */
308 ppriv->pwd_entry = hildon_entry_new (MODEST_EDITABLE_SIZE);
310 /* Auto-capitalization is the default, so let's turn it off */
311 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
312 HILDON_GTK_INPUT_MODE_FULL |
313 HILDON_GTK_INPUT_MODE_INVISIBLE);
314 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
317 modest_maemo_utils_create_captioned_with_size_type (title_size_group,
319 _("mail_fi_password"),
322 MODEST_EDITABLE_SIZE);
324 ppriv->port_view = GTK_WIDGET (modest_number_editor_new (PORT_MIN, PORT_MAX));
326 modest_maemo_utils_create_captioned_with_size_type (title_size_group,
328 _("mcen_fi_emailsetup_port"),
331 MODEST_EDITABLE_SIZE);
334 /* Track changes in UI */
335 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
336 G_CALLBACK (on_security_changed), self);
338 g_signal_connect (G_OBJECT (ppriv->auth_view), "value-changed",
339 G_CALLBACK (on_auth_changed), self);
340 g_signal_connect (G_OBJECT (ppriv->user_entry), "changed",
341 G_CALLBACK (on_entry_changed), self);
342 g_signal_connect (G_OBJECT (ppriv->port_view), "valid-changed",
343 G_CALLBACK (on_valid_changed), self);
346 /* Initialize widgets */
347 modest_serversecurity_picker_set_active_serversecurity (
348 MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
349 MODEST_PROTOCOLS_CONNECTION_NONE);
350 modest_secureauth_picker_set_active_secureauth (
351 MODEST_SECUREAUTH_PICKER (ppriv->auth_view),
352 MODEST_PROTOCOLS_AUTH_NONE);
354 /* Pack into container */
356 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
357 gtk_box_pack_start (GTK_BOX (self), user_caption, FALSE, FALSE, 0);
358 gtk_box_pack_start (GTK_BOX (self), pwd_caption, FALSE, FALSE, 0);
359 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
360 gtk_box_pack_start (GTK_BOX (self), port_caption, FALSE, FALSE, 0);
362 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
363 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
368 gtk_widget_show (ppriv->pwd_entry);
369 gtk_widget_show (ppriv->user_entry);
370 gtk_widget_show (ppriv->port_view);
371 gtk_widget_show (pwd_caption);
372 gtk_widget_show (user_caption);
373 gtk_widget_show (port_caption);
375 gtk_widget_show (ppriv->security_view);
376 gtk_widget_show (ppriv->auth_view);
380 modest_maemo_security_options_view_new (ModestSecurityOptionsType type,
382 GtkSizeGroup *title_size_group,
383 GtkSizeGroup *value_size_group)
385 ModestSecurityOptionsView* self;
386 ModestSecurityOptionsViewPrivate *ppriv;
388 self = (ModestSecurityOptionsView *)
389 g_object_new (MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, NULL);
390 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
394 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
395 create_incoming_security (self, title_size_group, value_size_group);
397 create_outgoing_security (self, title_size_group, value_size_group);
399 return (GtkWidget *) self;
403 modest_security_options_view_has_missing_mandatory_data (ModestSecurityOptionsView* self)
405 ModestMaemoSecurityOptionsViewPrivate *priv;
407 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
409 return priv->missing_data;
413 modest_maemo_security_options_view_load_settings (ModestSecurityOptionsView* self,
414 ModestAccountSettings *settings)
416 ModestSecurityOptionsViewPrivate *ppriv;
417 ModestServerAccountSettings *server_settings;
420 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
422 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
423 server_settings = modest_account_settings_get_store_settings (settings);
425 server_settings = modest_account_settings_get_transport_settings (settings);
426 port_number = modest_server_account_settings_get_port (server_settings);
428 if (port_number == 0) {
429 /* Show the appropriate port number */
430 on_security_changed (ppriv->security_view,
431 MODEST_MAEMO_SECURITY_OPTIONS_VIEW (self));
432 } else if (ppriv->full) {
433 /* Keep the user-entered port-number, or the
434 * already-appropriate automatic port number */
435 modest_number_editor_set_value (MODEST_NUMBER_EDITOR (ppriv->port_view),
439 g_object_unref (server_settings);
443 modest_maemo_security_options_view_save_settings (ModestSecurityOptionsView* self,
444 ModestAccountSettings *settings)
446 ModestServerAccountSettings *server_settings;
447 ModestSecurityOptionsViewPrivate *ppriv;
450 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
452 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
453 server_settings = modest_account_settings_get_store_settings (settings);
455 server_settings = modest_account_settings_get_transport_settings (settings);
458 server_port = modest_number_editor_get_value (MODEST_NUMBER_EDITOR (ppriv->port_view));
460 server_port = modest_serversecurity_picker_get_active_serversecurity_port (MODEST_SERVERSECURITY_PICKER (ppriv->security_view));
463 modest_server_account_settings_set_port (server_settings, server_port);
466 g_object_unref (server_settings);
470 modest_maemo_security_options_view_changed (ModestSecurityOptionsView* self,
471 ModestAccountSettings *settings)
473 ModestServerAccountSettings *server_settings;
474 ModestSecurityOptionsViewPrivate *ppriv;
477 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
479 /* If we're not showing the port number then it never changes */
483 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
484 server_settings = modest_account_settings_get_store_settings (settings);
486 server_settings = modest_account_settings_get_transport_settings (settings);
489 modest_number_editor_get_value (MODEST_NUMBER_EDITOR (ppriv->port_view));
492 g_object_unref (server_settings);
494 if (server_port != ppriv->initial_state.port)
501 modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj)
506 modest_maemo_security_options_view_finalize (GObject *obj)
508 G_OBJECT_CLASS (modest_maemo_security_options_view_parent_class)->finalize (obj);
513 modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass)
515 GObjectClass *gobject_class = (GObjectClass*) klass;
517 modest_maemo_security_options_view_parent_class = g_type_class_peek_parent (klass);
519 g_type_class_add_private (gobject_class, sizeof (ModestMaemoSecurityOptionsViewPrivate));
520 gobject_class->finalize = modest_maemo_security_options_view_finalize;
522 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->load_settings =
523 modest_maemo_security_options_view_load_settings;
524 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->save_settings =
525 modest_maemo_security_options_view_save_settings;
526 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->changed =
527 modest_maemo_security_options_view_changed;