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-maemo-utils.h"
39 #include "modest-hildon-includes.h"
40 #include <modest-toolkit-factory.h>
41 #include <modest-toolkit-utils.h>
44 #define PORT_MAX 65535
46 typedef struct _ModestMaemoSecurityOptionsViewPrivate ModestMaemoSecurityOptionsViewPrivate;
47 struct _ModestMaemoSecurityOptionsViewPrivate {
48 gboolean missing_data;
51 #define MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE(o) \
52 (G_TYPE_INSTANCE_GET_PRIVATE((o), \
53 MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, \
54 ModestMaemoSecurityOptionsViewPrivate))
56 static void modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj);
57 static void modest_maemo_security_options_view_finalize (GObject *obj);
58 static void modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass);
60 G_DEFINE_TYPE (ModestMaemoSecurityOptionsView,
61 modest_maemo_security_options_view,
62 MODEST_TYPE_SECURITY_OPTIONS_VIEW);
64 static void on_entry_changed (GtkEditable *editable, gpointer user_data);
66 #ifdef MODEST_NUMBER_ENTRY_SUPPORT_VALID_CHANGED
67 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 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 proto_type = modest_serversecurity_selector_get_active_serversecurity (ppriv->security_view);
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 modest_togglable_set_active (ppriv->auth_view,
92 gtk_widget_set_sensitive (ppriv->auth_view, !is_secure);
99 modest_serversecurity_selector_get_active_serversecurity_port (ppriv->security_view);
102 modest_number_entry_set_value (ppriv->port_view,
109 on_auth_changed (GtkWidget *widget,
110 ModestMaemoSecurityOptionsView *self)
112 ModestSecurityOptionsViewPrivate* ppriv;
113 ModestProtocolRegistry *protocol_registry;
114 ModestProtocolType auth_proto;
115 gboolean secureauth_used;
116 GtkWidget *user_caption, *pwd_caption;
118 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
119 protocol_registry = modest_runtime_get_protocol_registry ();
121 auth_proto = modest_secureauth_selector_get_active_secureauth (ppriv->auth_view);
122 secureauth_used = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
125 /* Get captions, well dimm the whole widget */
126 user_caption = gtk_widget_get_parent (ppriv->user_entry);
127 pwd_caption = gtk_widget_get_parent (ppriv->pwd_entry);
129 /* Enable / disable */
130 gtk_widget_set_sensitive (user_caption, secureauth_used);
131 gtk_widget_set_sensitive (pwd_caption, secureauth_used);
133 /* Check if mandatory data is missing */
134 on_entry_changed (GTK_EDITABLE (ppriv->user_entry), (gpointer) self);
138 create_incoming_security (ModestSecurityOptionsView* self,
139 GtkSizeGroup *title_size_group,
140 GtkSizeGroup *value_size_group)
142 ModestSecurityOptionsViewPrivate *ppriv;
143 GtkWidget *entry_caption = NULL;
145 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
147 ppriv->auth_view = modest_toolkit_factory_create_check_button (modest_runtime_get_toolkit_factory (),
148 _("mcen_li_emailsetup_secure_authentication"));
149 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, 0);
151 /* Create widgets for incoming security */
152 ppriv->security_view = modest_toolkit_factory_create_serversecurity_selector (modest_runtime_get_toolkit_factory ());
153 modest_serversecurity_selector_fill (ppriv->security_view,
154 modest_protocol_registry_get_pop_type_id ());
155 if (GTK_IS_COMBO_BOX (ppriv->security_view)) {
156 GtkWidget *captioned;
158 captioned = modest_toolkit_utils_create_captioned (title_size_group, value_size_group,
159 _("mcen_li_emailsetup_secure_connection"), FALSE,
160 ppriv->security_view);
161 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
162 G_CALLBACK (on_security_changed), self);
163 gtk_box_pack_start (GTK_BOX (self), captioned, FALSE, FALSE, 0);
164 gtk_widget_show (captioned);
166 modest_toolkit_utils_set_hbutton_layout (title_size_group,
168 _("mcen_li_emailsetup_secure_connection"),
169 ppriv->security_view);
170 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
171 G_CALLBACK (on_security_changed), self);
172 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, 0);
175 /* Pack into container & show */
176 gtk_widget_show (ppriv->security_view);
177 gtk_widget_show (ppriv->auth_view);
180 ppriv->port_view = modest_toolkit_factory_create_number_entry (modest_runtime_get_toolkit_factory (),
183 modest_toolkit_utils_create_captioned_with_size_type (title_size_group,
185 _("mcen_fi_emailsetup_port"),
188 MODEST_EDITABLE_SIZE);
189 /* Pack & show widgets */
190 gtk_box_pack_start (GTK_BOX (self), entry_caption, FALSE, FALSE, 0);
191 gtk_widget_show (ppriv->port_view);
192 gtk_widget_show (entry_caption);
194 /* Track changes in UI */
195 #ifdef MODEST_NUMBER_ENTRY_SUPPORT_VALID_CHANGED
196 g_signal_connect (G_OBJECT (ppriv->port_view), "valid-changed",
197 G_CALLBACK (on_valid_changed), self);
203 on_entry_max (ModestValidatingEntry *self,
206 modest_platform_information_banner (GTK_WIDGET (self), NULL,
207 _CS("ckdg_ib_maximum_characters_reached"));
211 * TODO: call this whenever the auth picker changes. If we set it
212 * explicitely at the beggining to a value then there is no need to
213 * call this handler directly at the beginning
216 on_entry_changed (GtkEditable *editable,
219 ModestSecurityOptionsView* self;
220 ModestMaemoSecurityOptionsViewPrivate *priv;
221 ModestSecurityOptionsViewPrivate *ppriv;
222 gboolean is_secure = FALSE;
223 ModestProtocolRegistry *protocol_registry;
225 self = MODEST_SECURITY_OPTIONS_VIEW (user_data);
226 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
227 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
228 protocol_registry = modest_runtime_get_protocol_registry ();
230 /* Check if it's a secure protocol */
231 if (modest_is_secureauth_selector (ppriv->auth_view)) {
232 ModestProtocolType auth_proto;
233 auth_proto = modest_secureauth_selector_get_active_secureauth (ppriv->auth_view);
234 is_secure = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
236 } else if (modest_is_togglable (ppriv->auth_view)) {
237 is_secure = modest_togglable_get_active (ppriv->auth_view);
241 !g_strcmp0 (modest_entry_get_text (ppriv->user_entry), "")) {
242 priv->missing_data = TRUE;
244 priv->missing_data = FALSE;
247 if (!priv->missing_data &&
249 !modest_number_entry_is_valid (ppriv->port_view))
250 priv->missing_data = TRUE;
252 /* Emit a signal to notify if mandatory data is missing */
253 g_signal_emit_by_name (G_OBJECT (self), "missing_mandatory_data",
254 priv->missing_data, NULL);
257 #ifdef MODEST_NUMBER_ENTRY_SUPPORT_VALID_CHANGED
259 on_valid_changed (ModestNumberEditor *editor,
261 ModestSecurityOptionsView *self)
263 on_entry_changed (NULL, (gpointer) self);
268 create_outgoing_security (ModestSecurityOptionsView* self,
269 GtkSizeGroup *title_size_group,
270 GtkSizeGroup *value_size_group)
272 ModestSecurityOptionsViewPrivate *ppriv;
273 GtkWidget *user_caption = NULL, *security_caption = NULL, *auth_caption = NULL;
274 GtkWidget *pwd_caption = NULL, *port_caption = NULL;
276 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
278 /* The secure connection widgets */
279 ppriv->security_view = modest_toolkit_factory_create_serversecurity_selector (modest_runtime_get_toolkit_factory ());
280 modest_serversecurity_selector_fill (ppriv->security_view,
281 MODEST_PROTOCOLS_TRANSPORT_SMTP);
282 if (GTK_IS_COMBO_BOX (ppriv->security_view)) {
283 security_caption = modest_toolkit_utils_create_captioned (title_size_group, value_size_group,
284 _("mcen_li_emailsetup_secure_connection"), FALSE,
285 ppriv->security_view);
286 gtk_widget_show (security_caption);
288 modest_toolkit_utils_set_hbutton_layout (title_size_group,
290 _("mcen_li_emailsetup_secure_connection"),
291 ppriv->security_view);
292 security_caption = ppriv->security_view;
295 /* The secure authentication widgets */
296 ppriv->auth_view = modest_toolkit_factory_create_secureauth_selector (modest_runtime_get_toolkit_factory ());
297 if (GTK_IS_COMBO_BOX (ppriv->auth_view)) {
298 auth_caption = modest_toolkit_utils_create_captioned (title_size_group, value_size_group,
299 _("mcen_li_emailsetup_secure_authentication"), FALSE,
301 gtk_widget_show (auth_caption);
303 modest_toolkit_utils_set_hbutton_layout (title_size_group,
305 _("mcen_li_emailsetup_secure_authentication"),
307 auth_caption = ppriv->auth_view;
313 /* Username widgets */
314 ppriv->user_entry = GTK_WIDGET (modest_validating_entry_new ());
316 /* Auto-capitalization is the default, so let's turn it off: */
317 #ifdef MODEST_TOOLKIT_HILDON2
318 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->user_entry),
319 HILDON_GTK_INPUT_MODE_FULL);
322 user_label = g_strdup_printf("%s*", _("mail_fi_username"));
323 user_caption = modest_toolkit_utils_create_captioned_with_size_type (title_size_group,
328 MODEST_EDITABLE_SIZE);
331 /* Prevent the use of some characters. Limit the max
333 modest_validating_entry_set_unallowed_characters_whitespace (
334 MODEST_VALIDATING_ENTRY (ppriv->user_entry));
335 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
336 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
339 /* Password widgets */
340 ppriv->pwd_entry = modest_toolkit_factory_create_entry (modest_runtime_get_toolkit_factory ());
342 /* Auto-capitalization is the default, so let's turn it off */
343 #ifdef MODEST_TOOLKIT_HILDON2
344 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
345 HILDON_GTK_INPUT_MODE_FULL |
346 HILDON_GTK_INPUT_MODE_INVISIBLE);
348 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
351 modest_toolkit_utils_create_captioned_with_size_type (title_size_group,
353 _("mail_fi_password"),
356 MODEST_EDITABLE_SIZE);
358 ppriv->port_view = modest_toolkit_factory_create_number_entry (modest_runtime_get_toolkit_factory (),
361 modest_toolkit_utils_create_captioned_with_size_type (title_size_group,
363 _("mcen_fi_emailsetup_port"),
366 MODEST_EDITABLE_SIZE);
369 /* Track changes in UI */
370 if (GTK_IS_COMBO_BOX (ppriv->security_view)) {
371 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
372 G_CALLBACK (on_security_changed), self);
374 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
375 G_CALLBACK (on_security_changed), self);
378 if (GTK_IS_COMBO_BOX (ppriv->auth_view)) {
379 g_signal_connect (G_OBJECT (ppriv->auth_view), "changed",
380 G_CALLBACK (on_auth_changed), self);
382 g_signal_connect (G_OBJECT (ppriv->auth_view), "value-changed",
383 G_CALLBACK (on_auth_changed), self);
385 g_signal_connect (G_OBJECT (ppriv->user_entry), "changed",
386 G_CALLBACK (on_entry_changed), self);
387 #ifdef MODEST_NUMBER_ENTRY_SUPPORT_VALID_CHANGED
388 g_signal_connect (G_OBJECT (ppriv->port_view), "valid-changed",
389 G_CALLBACK (on_valid_changed), self);
393 /* Initialize widgets */
394 modest_serversecurity_selector_set_active_serversecurity (
395 ppriv->security_view,
396 MODEST_PROTOCOLS_CONNECTION_NONE);
397 modest_secureauth_selector_set_active_secureauth (
399 MODEST_PROTOCOLS_AUTH_NONE);
401 /* Pack into container */
403 gtk_box_pack_start (GTK_BOX (self), auth_caption, FALSE, FALSE, 0);
404 gtk_box_pack_start (GTK_BOX (self), user_caption, FALSE, FALSE, 0);
405 gtk_box_pack_start (GTK_BOX (self), pwd_caption, FALSE, FALSE, 0);
406 gtk_box_pack_start (GTK_BOX (self), security_caption, FALSE, FALSE, 0);
407 gtk_box_pack_start (GTK_BOX (self), port_caption, FALSE, FALSE, 0);
409 gtk_box_pack_start (GTK_BOX (self), auth_caption, FALSE, FALSE, 0);
410 gtk_box_pack_start (GTK_BOX (self), security_caption, FALSE, FALSE, 0);
415 gtk_widget_show (ppriv->pwd_entry);
416 gtk_widget_show (ppriv->user_entry);
417 gtk_widget_show (ppriv->port_view);
418 gtk_widget_show (pwd_caption);
419 gtk_widget_show (user_caption);
420 gtk_widget_show (port_caption);
422 gtk_widget_show (ppriv->security_view);
423 gtk_widget_show (ppriv->auth_view);
427 modest_maemo_security_options_view_new (ModestSecurityOptionsType type,
429 GtkSizeGroup *title_size_group,
430 GtkSizeGroup *value_size_group)
432 ModestSecurityOptionsView* self;
433 ModestSecurityOptionsViewPrivate *ppriv;
435 self = (ModestSecurityOptionsView *)
436 g_object_new (MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, NULL);
437 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
441 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
442 create_incoming_security (self, title_size_group, value_size_group);
444 create_outgoing_security (self, title_size_group, value_size_group);
446 return (GtkWidget *) self;
450 modest_security_options_view_has_missing_mandatory_data (ModestSecurityOptionsView* self)
452 ModestMaemoSecurityOptionsViewPrivate *priv;
454 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
456 return priv->missing_data;
460 modest_maemo_security_options_view_load_settings (ModestSecurityOptionsView* self,
461 ModestAccountSettings *settings)
463 ModestSecurityOptionsViewPrivate *ppriv;
464 ModestServerAccountSettings *server_settings;
467 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
469 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
470 server_settings = modest_account_settings_get_store_settings (settings);
472 server_settings = modest_account_settings_get_transport_settings (settings);
473 port_number = modest_server_account_settings_get_port (server_settings);
475 if (port_number == 0) {
476 /* Show the appropriate port number */
477 on_security_changed (ppriv->security_view,
478 MODEST_MAEMO_SECURITY_OPTIONS_VIEW (self));
479 } else if (ppriv->full) {
480 /* Keep the user-entered port-number, or the
481 * already-appropriate automatic port number */
482 modest_number_entry_set_value (ppriv->port_view,
486 g_object_unref (server_settings);
490 modest_maemo_security_options_view_save_settings (ModestSecurityOptionsView* self,
491 ModestAccountSettings *settings)
493 ModestServerAccountSettings *server_settings;
494 ModestSecurityOptionsViewPrivate *ppriv;
497 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
499 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
500 server_settings = modest_account_settings_get_store_settings (settings);
502 server_settings = modest_account_settings_get_transport_settings (settings);
505 server_port = modest_number_entry_get_value (ppriv->port_view);
507 server_port = modest_serversecurity_selector_get_active_serversecurity_port (ppriv->security_view);
510 modest_server_account_settings_set_port (server_settings, server_port);
513 g_object_unref (server_settings);
517 modest_maemo_security_options_view_changed (ModestSecurityOptionsView* self,
518 ModestAccountSettings *settings)
520 ModestServerAccountSettings *server_settings;
521 ModestSecurityOptionsViewPrivate *ppriv;
524 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
526 /* If we're not showing the port number then it never changes */
530 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
531 server_settings = modest_account_settings_get_store_settings (settings);
533 server_settings = modest_account_settings_get_transport_settings (settings);
536 modest_number_entry_get_value (ppriv->port_view);
539 g_object_unref (server_settings);
541 if (server_port != ppriv->initial_state.port)
548 modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj)
553 modest_maemo_security_options_view_finalize (GObject *obj)
555 G_OBJECT_CLASS (modest_maemo_security_options_view_parent_class)->finalize (obj);
560 modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass)
562 GObjectClass *gobject_class = (GObjectClass*) klass;
564 modest_maemo_security_options_view_parent_class = g_type_class_peek_parent (klass);
566 g_type_class_add_private (gobject_class, sizeof (ModestMaemoSecurityOptionsViewPrivate));
567 gobject_class->finalize = modest_maemo_security_options_view_finalize;
569 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->load_settings =
570 modest_maemo_security_options_view_load_settings;
571 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->save_settings =
572 modest_maemo_security_options_view_save_settings;
573 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->changed =
574 modest_maemo_security_options_view_changed;