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 "widgets/modest-serversecurity-combo-box.h"
39 #include "widgets/modest-secureauth-combo-box.h"
40 #include "maemo/easysetup/modest-easysetup-servertype-combo-box.h"
41 #include <hildon/hildon-caption.h>
42 #include <hildon/hildon-number-editor.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 /* Tracks changes in the incoming security combo box */
69 on_security_changed (GtkWidget *widget,
70 ModestMaemoSecurityOptionsView *self)
72 ModestSecurityOptionsViewPrivate* ppriv;
73 ModestServersecurityComboBox *combo;
74 ModestProtocolType proto_type;
75 ModestProtocolRegistry *proto_registry;
78 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
80 proto_registry = modest_runtime_get_protocol_registry ();
81 combo = MODEST_SERVERSECURITY_COMBO_BOX (ppriv->security_view);
82 proto_type = modest_serversecurity_combo_box_get_active_serversecurity (combo);
84 is_secure = modest_protocol_registry_protocol_type_has_tag (proto_registry, proto_type,
85 MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS);
87 if (MODEST_SECURITY_OPTIONS_VIEW (self)->type == MODEST_SECURITY_OPTIONS_INCOMING) {
88 /* Activate and dim checkbutton if it's secure */
89 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ppriv->auth_view),
91 gtk_widget_set_sensitive (ppriv->auth_view, !is_secure);
98 modest_serversecurity_combo_box_get_active_serversecurity_port (MODEST_SERVERSECURITY_COMBO_BOX (ppriv->security_view));
101 hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (ppriv->port_view),
108 on_auth_changed (GtkWidget *widget,
109 ModestMaemoSecurityOptionsView *self)
111 ModestSecurityOptionsViewPrivate* ppriv;
112 ModestSecureauthComboBox *combo;
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 ();
120 combo = MODEST_SECUREAUTH_COMBO_BOX (ppriv->auth_view);
122 auth_proto = modest_secureauth_combo_box_get_active_secureauth (combo);
123 secureauth_used = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
126 /* Get captions, well dimm the whole widget */
127 user_caption = gtk_widget_get_parent (ppriv->user_entry);
128 pwd_caption = gtk_widget_get_parent (ppriv->pwd_entry);
130 /* Enable / disable */
131 gtk_widget_set_sensitive (user_caption, secureauth_used);
132 gtk_widget_set_sensitive (pwd_caption, secureauth_used);
134 /* Check if mandatory data is missing */
135 on_entry_changed (GTK_EDITABLE (ppriv->user_entry), (gpointer) self);
139 create_incoming_security (ModestSecurityOptionsView* self,
140 GtkSizeGroup *size_group)
142 ModestSecurityOptionsViewPrivate *ppriv;
143 GtkWidget *combo_caption, *check_caption, *entry_caption = NULL;
145 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
147 /* Create widgets for incoming security */
148 ppriv->security_view = GTK_WIDGET (modest_serversecurity_combo_box_new ());
149 combo_caption = hildon_caption_new (size_group, _("mcen_li_emailsetup_secure_connection"),
150 ppriv->security_view, NULL,
151 HILDON_CAPTION_OPTIONAL);
154 ppriv->port_view = GTK_WIDGET (hildon_number_editor_new (PORT_MIN, PORT_MAX));
155 entry_caption = hildon_caption_new (size_group, _("mcen_fi_emailsetup_port"),
156 ppriv->port_view, NULL,
157 HILDON_CAPTION_OPTIONAL);
160 ppriv->auth_view = gtk_check_button_new ();
162 hildon_caption_new (size_group, _("mcen_li_emailsetup_secure_authentication"),
163 ppriv->auth_view, NULL, HILDON_CAPTION_OPTIONAL);
165 /* Track changes in UI */
166 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
167 G_CALLBACK (on_security_changed), self);
169 /* Pack into container */
170 gtk_box_pack_start (GTK_BOX (self), combo_caption,
171 FALSE, FALSE, MODEST_MARGIN_HALF);
173 gtk_box_pack_start (GTK_BOX (self), entry_caption,
174 FALSE, FALSE, MODEST_MARGIN_HALF);
175 gtk_box_pack_start (GTK_BOX (self), check_caption,
176 FALSE, FALSE, MODEST_MARGIN_HALF);
180 gtk_widget_show (ppriv->port_view);
181 gtk_widget_show (entry_caption);
183 gtk_widget_show (ppriv->security_view);
184 gtk_widget_show (ppriv->auth_view);
185 gtk_widget_show (combo_caption);
186 gtk_widget_show (check_caption);
190 on_entry_max (ModestValidatingEntry *self,
193 modest_platform_information_banner (GTK_WIDGET (self), NULL,
194 _CS("ckdg_ib_maximum_characters_reached"));
198 * TODO: call this whenever the auth combo changes. If we set it
199 * explicitely at the beggining to a value then there is no need to
200 * call this handler directly at the beginning
203 on_entry_changed (GtkEditable *editable,
206 ModestSecurityOptionsView* self;
207 ModestMaemoSecurityOptionsViewPrivate *priv;
208 ModestSecurityOptionsViewPrivate *ppriv;
209 ModestProtocolType auth_proto;
210 ModestSecureauthComboBox *combo;
212 ModestProtocolRegistry *protocol_registry;
214 self = MODEST_SECURITY_OPTIONS_VIEW (user_data);
215 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
216 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
217 protocol_registry = modest_runtime_get_protocol_registry ();
219 /* Outgoing username is mandatory if outgoing auth is secure */
220 combo = MODEST_SECUREAUTH_COMBO_BOX (ppriv->auth_view);
221 auth_proto = modest_secureauth_combo_box_get_active_secureauth (combo);
222 is_secure = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
226 !g_ascii_strcasecmp (gtk_entry_get_text (GTK_ENTRY (ppriv->user_entry)), "")) {
227 priv->missing_data = TRUE;
229 priv->missing_data = FALSE;
232 /* Emit a signal to notify if mandatory data is missing */
233 g_signal_emit_by_name (G_OBJECT (self), "missing_mandatory_data",
234 priv->missing_data, NULL);
238 create_outgoing_security (ModestSecurityOptionsView* self,
239 GtkSizeGroup *size_group)
241 ModestSecurityOptionsViewPrivate *ppriv;
242 GtkWidget *sec_caption, *auth_caption, *user_caption = NULL;
243 GtkWidget *pwd_caption = NULL, *port_caption = NULL;
245 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
247 /* The secure connection widgets */
248 ppriv->security_view = GTK_WIDGET (modest_serversecurity_combo_box_new ());
249 modest_serversecurity_combo_box_fill (MODEST_SERVERSECURITY_COMBO_BOX (ppriv->security_view),
250 MODEST_PROTOCOLS_TRANSPORT_SMTP);
251 sec_caption = hildon_caption_new (size_group, _("mcen_li_emailsetup_secure_connection"),
252 ppriv->security_view, NULL, HILDON_CAPTION_OPTIONAL);
254 /* The secure authentication widgets */
255 ppriv->auth_view = GTK_WIDGET (modest_secureauth_combo_box_new ());
256 auth_caption = hildon_caption_new (size_group, _("mcen_li_emailsetup_secure_authentication"),
257 ppriv->auth_view, NULL, HILDON_CAPTION_OPTIONAL);
262 /* Username widgets */
263 ppriv->user_entry = GTK_WIDGET (modest_validating_entry_new ());
265 /* Auto-capitalization is the default, so let's turn it off: */
266 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->user_entry),
267 HILDON_GTK_INPUT_MODE_FULL);
269 user_label = g_strdup_printf("%s*", _("mail_fi_username"));
270 user_caption = hildon_caption_new (size_group, user_label,
271 ppriv->user_entry, NULL,
272 HILDON_CAPTION_MANDATORY);
275 /* Prevent the use of some characters. Limit the max
277 modest_validating_entry_set_unallowed_characters_whitespace (
278 MODEST_VALIDATING_ENTRY (ppriv->user_entry));
279 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
280 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
283 /* Password widgets */
284 ppriv->pwd_entry = gtk_entry_new ();
286 /* Auto-capitalization is the default, so let's turn it off */
287 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
288 HILDON_GTK_INPUT_MODE_FULL |
289 HILDON_GTK_INPUT_MODE_INVISIBLE);
290 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
292 pwd_caption = hildon_caption_new (size_group, _("mail_fi_password"),
293 ppriv->pwd_entry, NULL,
294 HILDON_CAPTION_OPTIONAL);
296 ppriv->port_view = GTK_WIDGET (hildon_number_editor_new (PORT_MIN, PORT_MAX));
297 port_caption = hildon_caption_new (size_group, _("mcen_fi_emailsetup_port"),
298 ppriv->port_view, NULL,
299 HILDON_CAPTION_OPTIONAL);
302 /* Track changes in UI */
303 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
304 G_CALLBACK (on_security_changed), self);
306 g_signal_connect (G_OBJECT (ppriv->auth_view), "changed",
307 G_CALLBACK (on_auth_changed), self);
308 g_signal_connect (G_OBJECT (ppriv->user_entry), "changed",
309 G_CALLBACK (on_entry_changed), self);
312 /* Initialize widgets */
313 modest_serversecurity_combo_box_set_active_serversecurity (
314 MODEST_SERVERSECURITY_COMBO_BOX (ppriv->security_view),
315 MODEST_PROTOCOLS_CONNECTION_NONE);
316 modest_secureauth_combo_box_set_active_secureauth (
317 MODEST_SECUREAUTH_COMBO_BOX (ppriv->auth_view),
318 MODEST_PROTOCOLS_AUTH_NONE);
320 /* Pack into container */
322 gtk_box_pack_start (GTK_BOX (self), auth_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
323 gtk_box_pack_start (GTK_BOX (self), user_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
324 gtk_box_pack_start (GTK_BOX (self), pwd_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
325 gtk_box_pack_start (GTK_BOX (self), sec_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
326 gtk_box_pack_start (GTK_BOX (self), port_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
328 /* The order is different */
329 gtk_box_pack_start (GTK_BOX (self), sec_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
330 gtk_box_pack_start (GTK_BOX (self), auth_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
335 gtk_widget_show (ppriv->pwd_entry);
336 gtk_widget_show (ppriv->user_entry);
337 gtk_widget_show (ppriv->port_view);
338 gtk_widget_show (pwd_caption);
339 gtk_widget_show (user_caption);
340 gtk_widget_show (port_caption);
342 gtk_widget_show (ppriv->security_view);
343 gtk_widget_show (ppriv->auth_view);
344 gtk_widget_show (sec_caption);
345 gtk_widget_show (auth_caption);
349 modest_maemo_security_options_view_new (ModestSecurityOptionsType type,
351 GtkSizeGroup *size_group)
353 ModestSecurityOptionsView* self;
354 ModestSecurityOptionsViewPrivate *ppriv;
356 self = (ModestSecurityOptionsView *)
357 g_object_new (MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, NULL);
358 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
362 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
363 create_incoming_security (self, size_group);
365 create_outgoing_security (self, size_group);
367 return (GtkWidget *) self;
371 modest_maemo_security_options_view_load_settings (ModestSecurityOptionsView* self,
372 ModestAccountSettings *settings)
374 ModestSecurityOptionsViewPrivate *ppriv;
375 ModestServerAccountSettings *server_settings;
378 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
380 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
381 server_settings = modest_account_settings_get_store_settings (settings);
383 server_settings = modest_account_settings_get_transport_settings (settings);
384 port_number = modest_server_account_settings_get_port (server_settings);
386 if (port_number == 0) {
387 /* Show the appropriate port number */
388 on_security_changed (ppriv->security_view,
389 MODEST_MAEMO_SECURITY_OPTIONS_VIEW (self));
390 } else if (ppriv->full) {
391 /* Keep the user-entered port-number, or the
392 * already-appropriate automatic port number */
393 hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (ppriv->port_view),
397 g_object_unref (server_settings);
401 modest_maemo_security_options_view_save_settings (ModestSecurityOptionsView* self,
402 ModestAccountSettings *settings)
404 ModestServerAccountSettings *server_settings;
405 ModestSecurityOptionsViewPrivate *ppriv;
408 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
410 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
411 server_settings = modest_account_settings_get_store_settings (settings);
413 server_settings = modest_account_settings_get_transport_settings (settings);
416 server_port = hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (ppriv->port_view));
418 server_port = modest_serversecurity_combo_box_get_active_serversecurity_port (MODEST_SERVERSECURITY_COMBO_BOX (ppriv->security_view));
421 modest_server_account_settings_set_port (server_settings, server_port);
424 g_object_unref (server_settings);
428 modest_maemo_security_options_view_changed (ModestSecurityOptionsView* self,
429 ModestAccountSettings *settings)
431 ModestServerAccountSettings *server_settings;
432 ModestSecurityOptionsViewPrivate *ppriv;
435 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
437 /* If we're not showing the port number then it never changes */
441 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
442 server_settings = modest_account_settings_get_store_settings (settings);
444 server_settings = modest_account_settings_get_transport_settings (settings);
447 hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (ppriv->port_view));
450 g_object_unref (server_settings);
452 if (server_port != ppriv->initial_state.port)
459 modest_security_options_view_has_missing_mandatory_data (ModestSecurityOptionsView* self)
461 ModestMaemoSecurityOptionsViewPrivate *priv;
463 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
465 return priv->missing_data;
469 modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj)
474 modest_maemo_security_options_view_finalize (GObject *obj)
476 G_OBJECT_CLASS (modest_maemo_security_options_view_parent_class)->finalize (obj);
481 modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass)
483 GObjectClass *gobject_class = (GObjectClass*) klass;
485 modest_maemo_security_options_view_parent_class = g_type_class_peek_parent (klass);
487 g_type_class_add_private (gobject_class, sizeof (ModestMaemoSecurityOptionsViewPrivate));
488 gobject_class->finalize = modest_maemo_security_options_view_finalize;
490 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->load_settings =
491 modest_maemo_security_options_view_load_settings;
492 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->save_settings =
493 modest_maemo_security_options_view_save_settings;
494 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->changed =
495 modest_maemo_security_options_view_changed;