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 "modest-hildon-includes.h"
45 #define PORT_MAX 65535
47 typedef struct _ModestMaemoSecurityOptionsViewPrivate ModestMaemoSecurityOptionsViewPrivate;
48 struct _ModestMaemoSecurityOptionsViewPrivate {
49 gboolean missing_data;
52 #define MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE(o) \
53 (G_TYPE_INSTANCE_GET_PRIVATE((o), \
54 MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, \
55 ModestMaemoSecurityOptionsViewPrivate))
57 static void modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj);
58 static void modest_maemo_security_options_view_finalize (GObject *obj);
59 static void modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass);
61 G_DEFINE_TYPE (ModestMaemoSecurityOptionsView,
62 modest_maemo_security_options_view,
63 MODEST_TYPE_SECURITY_OPTIONS_VIEW);
65 static void on_entry_changed (GtkEditable *editable, gpointer user_data);
67 static void on_valid_changed (ModestNumberEditor *editor, gboolean valid, ModestSecurityOptionsView *self);
69 /* Tracks changes in the incoming security picker */
71 on_security_changed (GtkWidget *widget,
72 ModestMaemoSecurityOptionsView *self)
74 ModestSecurityOptionsViewPrivate* ppriv;
75 ModestServersecurityPicker *picker;
76 ModestProtocolType proto_type;
77 ModestProtocolRegistry *proto_registry;
80 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
82 proto_registry = modest_runtime_get_protocol_registry ();
83 picker = MODEST_SERVERSECURITY_PICKER (ppriv->security_view);
84 proto_type = modest_serversecurity_picker_get_active_serversecurity (picker);
86 is_secure = modest_protocol_registry_protocol_type_has_tag (proto_registry, proto_type,
87 MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS);
89 if (MODEST_SECURITY_OPTIONS_VIEW (self)->type == MODEST_SECURITY_OPTIONS_INCOMING) {
90 /* Activate and dim checkbutton if it's secure */
91 modest_togglable_set_active (ppriv->auth_view,
93 gtk_widget_set_sensitive (ppriv->auth_view, !is_secure);
100 modest_serversecurity_picker_get_active_serversecurity_port (MODEST_SERVERSECURITY_PICKER (ppriv->security_view));
103 modest_number_editor_set_value (MODEST_NUMBER_EDITOR (ppriv->port_view),
110 on_auth_changed (GtkWidget *widget,
111 ModestMaemoSecurityOptionsView *self)
113 ModestSecurityOptionsViewPrivate* ppriv;
114 ModestSecureauthPicker *picker;
115 ModestProtocolRegistry *protocol_registry;
116 ModestProtocolType auth_proto;
117 gboolean secureauth_used;
118 GtkWidget *user_caption, *pwd_caption;
120 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
121 protocol_registry = modest_runtime_get_protocol_registry ();
122 picker = MODEST_SECUREAUTH_PICKER (ppriv->auth_view);
124 auth_proto = modest_secureauth_picker_get_active_secureauth (picker);
125 secureauth_used = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
128 /* Get captions, well dimm the whole widget */
129 user_caption = gtk_widget_get_parent (ppriv->user_entry);
130 pwd_caption = gtk_widget_get_parent (ppriv->pwd_entry);
132 /* Enable / disable */
133 gtk_widget_set_sensitive (user_caption, secureauth_used);
134 gtk_widget_set_sensitive (pwd_caption, secureauth_used);
136 /* Check if mandatory data is missing */
137 on_entry_changed (GTK_EDITABLE (ppriv->user_entry), (gpointer) self);
141 create_incoming_security (ModestSecurityOptionsView* self,
142 GtkSizeGroup *title_size_group,
143 GtkSizeGroup *value_size_group)
145 ModestSecurityOptionsViewPrivate *ppriv;
146 GtkWidget *entry_caption = NULL;
148 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
150 /* Create widgets for incoming security */
151 ppriv->security_view = GTK_WIDGET (modest_serversecurity_picker_new (MODEST_EDITABLE_SIZE,
152 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
153 modest_serversecurity_picker_fill (MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
154 modest_protocol_registry_get_pop_type_id ());
155 modest_maemo_utils_set_hbutton_layout (title_size_group,
157 _("mcen_li_emailsetup_secure_connection"),
158 ppriv->security_view);
160 ppriv->auth_view = modest_toolkit_factory_create_check_button (modest_runtime_get_toolkit_factory (),
161 _("mcen_li_emailsetup_secure_authentication"));
163 /* Track changes in UI */
164 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
165 G_CALLBACK (on_security_changed), self);
167 /* Pack into container & show */
168 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
169 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
170 gtk_widget_show (ppriv->security_view);
171 gtk_widget_show (ppriv->auth_view);
174 ppriv->port_view = GTK_WIDGET (modest_number_editor_new (PORT_MIN, PORT_MAX));
176 modest_maemo_utils_create_captioned_with_size_type (title_size_group,
178 _("mcen_fi_emailsetup_port"),
181 MODEST_EDITABLE_SIZE);
182 /* Pack & show widgets */
183 gtk_box_pack_start (GTK_BOX (self), entry_caption, FALSE, FALSE, 0);
184 gtk_widget_show (ppriv->port_view);
185 gtk_widget_show (entry_caption);
187 /* Track changes in UI */
188 g_signal_connect (G_OBJECT (ppriv->port_view), "valid-changed",
189 G_CALLBACK (on_valid_changed), self);
194 on_entry_max (ModestValidatingEntry *self,
197 modest_platform_information_banner (GTK_WIDGET (self), NULL,
198 _CS("ckdg_ib_maximum_characters_reached"));
202 * TODO: call this whenever the auth picker changes. If we set it
203 * explicitely at the beggining to a value then there is no need to
204 * call this handler directly at the beginning
207 on_entry_changed (GtkEditable *editable,
210 ModestSecurityOptionsView* self;
211 ModestMaemoSecurityOptionsViewPrivate *priv;
212 ModestSecurityOptionsViewPrivate *ppriv;
213 ModestSecureauthPicker *picker;
214 gboolean is_secure = FALSE;
215 ModestProtocolRegistry *protocol_registry;
217 self = MODEST_SECURITY_OPTIONS_VIEW (user_data);
218 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
219 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
220 protocol_registry = modest_runtime_get_protocol_registry ();
222 /* Check if it's a secure protocol */
223 if (MODEST_IS_SECUREAUTH_PICKER (ppriv->auth_view)) {
224 ModestProtocolType auth_proto;
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,
229 } else if (modest_is_togglable (ppriv->auth_view)) {
230 is_secure = modest_togglable_get_active (ppriv->auth_view);
234 !g_strcmp0 (hildon_entry_get_text (HILDON_ENTRY (ppriv->user_entry)), "")) {
235 priv->missing_data = TRUE;
237 priv->missing_data = FALSE;
240 if (!priv->missing_data &&
242 !modest_number_editor_is_valid (MODEST_NUMBER_EDITOR (ppriv->port_view)))
243 priv->missing_data = TRUE;
245 /* Emit a signal to notify if mandatory data is missing */
246 g_signal_emit_by_name (G_OBJECT (self), "missing_mandatory_data",
247 priv->missing_data, NULL);
251 on_valid_changed (ModestNumberEditor *editor,
253 ModestSecurityOptionsView *self)
255 on_entry_changed (NULL, (gpointer) self);
259 create_outgoing_security (ModestSecurityOptionsView* self,
260 GtkSizeGroup *title_size_group,
261 GtkSizeGroup *value_size_group)
263 ModestSecurityOptionsViewPrivate *ppriv;
264 GtkWidget *user_caption = NULL;
265 GtkWidget *pwd_caption = NULL, *port_caption = NULL;
267 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
269 /* The secure connection widgets */
270 ppriv->security_view = GTK_WIDGET (modest_serversecurity_picker_new (MODEST_EDITABLE_SIZE,
271 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
272 modest_serversecurity_picker_fill (MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
273 MODEST_PROTOCOLS_TRANSPORT_SMTP);
274 modest_maemo_utils_set_hbutton_layout (title_size_group,
276 _("mcen_li_emailsetup_secure_connection"),
277 ppriv->security_view);
279 /* The secure authentication widgets */
280 ppriv->auth_view = GTK_WIDGET (modest_secureauth_picker_new (MODEST_EDITABLE_SIZE,
281 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
282 modest_maemo_utils_set_hbutton_layout (title_size_group,
284 _("mcen_li_emailsetup_secure_authentication"),
290 /* Username widgets */
291 ppriv->user_entry = GTK_WIDGET (modest_validating_entry_new ());
293 /* Auto-capitalization is the default, so let's turn it off: */
294 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->user_entry),
295 HILDON_GTK_INPUT_MODE_FULL);
297 user_label = g_strdup_printf("%s*", _("mail_fi_username"));
298 user_caption = modest_maemo_utils_create_captioned_with_size_type (title_size_group,
303 MODEST_EDITABLE_SIZE);
306 /* Prevent the use of some characters. Limit the max
308 modest_validating_entry_set_unallowed_characters_whitespace (
309 MODEST_VALIDATING_ENTRY (ppriv->user_entry));
310 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
311 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
314 /* Password widgets */
315 ppriv->pwd_entry = hildon_entry_new (MODEST_EDITABLE_SIZE);
317 /* Auto-capitalization is the default, so let's turn it off */
318 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
319 HILDON_GTK_INPUT_MODE_FULL |
320 HILDON_GTK_INPUT_MODE_INVISIBLE);
321 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
324 modest_maemo_utils_create_captioned_with_size_type (title_size_group,
326 _("mail_fi_password"),
329 MODEST_EDITABLE_SIZE);
331 ppriv->port_view = GTK_WIDGET (modest_number_editor_new (PORT_MIN, PORT_MAX));
333 modest_maemo_utils_create_captioned_with_size_type (title_size_group,
335 _("mcen_fi_emailsetup_port"),
338 MODEST_EDITABLE_SIZE);
341 /* Track changes in UI */
342 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
343 G_CALLBACK (on_security_changed), self);
345 g_signal_connect (G_OBJECT (ppriv->auth_view), "value-changed",
346 G_CALLBACK (on_auth_changed), self);
347 g_signal_connect (G_OBJECT (ppriv->user_entry), "changed",
348 G_CALLBACK (on_entry_changed), self);
349 g_signal_connect (G_OBJECT (ppriv->port_view), "valid-changed",
350 G_CALLBACK (on_valid_changed), self);
353 /* Initialize widgets */
354 modest_serversecurity_picker_set_active_serversecurity (
355 MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
356 MODEST_PROTOCOLS_CONNECTION_NONE);
357 modest_secureauth_picker_set_active_secureauth (
358 MODEST_SECUREAUTH_PICKER (ppriv->auth_view),
359 MODEST_PROTOCOLS_AUTH_NONE);
361 /* Pack into container */
363 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
364 gtk_box_pack_start (GTK_BOX (self), user_caption, FALSE, FALSE, 0);
365 gtk_box_pack_start (GTK_BOX (self), pwd_caption, FALSE, FALSE, 0);
366 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
367 gtk_box_pack_start (GTK_BOX (self), port_caption, FALSE, FALSE, 0);
369 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
370 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
375 gtk_widget_show (ppriv->pwd_entry);
376 gtk_widget_show (ppriv->user_entry);
377 gtk_widget_show (ppriv->port_view);
378 gtk_widget_show (pwd_caption);
379 gtk_widget_show (user_caption);
380 gtk_widget_show (port_caption);
382 gtk_widget_show (ppriv->security_view);
383 gtk_widget_show (ppriv->auth_view);
387 modest_maemo_security_options_view_new (ModestSecurityOptionsType type,
389 GtkSizeGroup *title_size_group,
390 GtkSizeGroup *value_size_group)
392 ModestSecurityOptionsView* self;
393 ModestSecurityOptionsViewPrivate *ppriv;
395 self = (ModestSecurityOptionsView *)
396 g_object_new (MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, NULL);
397 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
401 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
402 create_incoming_security (self, title_size_group, value_size_group);
404 create_outgoing_security (self, title_size_group, value_size_group);
406 return (GtkWidget *) self;
410 modest_security_options_view_has_missing_mandatory_data (ModestSecurityOptionsView* self)
412 ModestMaemoSecurityOptionsViewPrivate *priv;
414 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
416 return priv->missing_data;
420 modest_maemo_security_options_view_load_settings (ModestSecurityOptionsView* self,
421 ModestAccountSettings *settings)
423 ModestSecurityOptionsViewPrivate *ppriv;
424 ModestServerAccountSettings *server_settings;
427 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
429 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
430 server_settings = modest_account_settings_get_store_settings (settings);
432 server_settings = modest_account_settings_get_transport_settings (settings);
433 port_number = modest_server_account_settings_get_port (server_settings);
435 if (port_number == 0) {
436 /* Show the appropriate port number */
437 on_security_changed (ppriv->security_view,
438 MODEST_MAEMO_SECURITY_OPTIONS_VIEW (self));
439 } else if (ppriv->full) {
440 /* Keep the user-entered port-number, or the
441 * already-appropriate automatic port number */
442 modest_number_editor_set_value (MODEST_NUMBER_EDITOR (ppriv->port_view),
446 g_object_unref (server_settings);
450 modest_maemo_security_options_view_save_settings (ModestSecurityOptionsView* self,
451 ModestAccountSettings *settings)
453 ModestServerAccountSettings *server_settings;
454 ModestSecurityOptionsViewPrivate *ppriv;
457 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
459 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
460 server_settings = modest_account_settings_get_store_settings (settings);
462 server_settings = modest_account_settings_get_transport_settings (settings);
465 server_port = modest_number_editor_get_value (MODEST_NUMBER_EDITOR (ppriv->port_view));
467 server_port = modest_serversecurity_picker_get_active_serversecurity_port (MODEST_SERVERSECURITY_PICKER (ppriv->security_view));
470 modest_server_account_settings_set_port (server_settings, server_port);
473 g_object_unref (server_settings);
477 modest_maemo_security_options_view_changed (ModestSecurityOptionsView* self,
478 ModestAccountSettings *settings)
480 ModestServerAccountSettings *server_settings;
481 ModestSecurityOptionsViewPrivate *ppriv;
484 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
486 /* If we're not showing the port number then it never changes */
490 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
491 server_settings = modest_account_settings_get_store_settings (settings);
493 server_settings = modest_account_settings_get_transport_settings (settings);
496 modest_number_editor_get_value (MODEST_NUMBER_EDITOR (ppriv->port_view));
499 g_object_unref (server_settings);
501 if (server_port != ppriv->initial_state.port)
508 modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj)
513 modest_maemo_security_options_view_finalize (GObject *obj)
515 G_OBJECT_CLASS (modest_maemo_security_options_view_parent_class)->finalize (obj);
520 modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass)
522 GObjectClass *gobject_class = (GObjectClass*) klass;
524 modest_maemo_security_options_view_parent_class = g_type_class_peek_parent (klass);
526 g_type_class_add_private (gobject_class, sizeof (ModestMaemoSecurityOptionsViewPrivate));
527 gobject_class->finalize = modest_maemo_security_options_view_finalize;
529 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->load_settings =
530 modest_maemo_security_options_view_load_settings;
531 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->save_settings =
532 modest_maemo_security_options_view_save_settings;
533 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->changed =
534 modest_maemo_security_options_view_changed;