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 #ifdef MODEST_HAVE_HILDON0_WIDGETS
42 #include <hildon-widgets/hildon-caption.h>
43 #include <hildon-widgets/hildon-number-editor.h>
45 #include <hildon/hildon-caption.h>
46 #include <hildon/hildon-number-editor.h>
47 #endif /*MODEST_HAVE_HILDON0_WIDGETS*/
50 #define PORT_MAX 65535
52 typedef struct _ModestMaemoSecurityOptionsViewPrivate ModestMaemoSecurityOptionsViewPrivate;
53 struct _ModestMaemoSecurityOptionsViewPrivate {
54 gboolean missing_data;
57 #define MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE(o) \
58 (G_TYPE_INSTANCE_GET_PRIVATE((o), \
59 MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, \
60 ModestMaemoSecurityOptionsViewPrivate))
62 static void modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj);
63 static void modest_maemo_security_options_view_finalize (GObject *obj);
64 static void modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass);
66 G_DEFINE_TYPE (ModestMaemoSecurityOptionsView,
67 modest_maemo_security_options_view,
68 MODEST_TYPE_SECURITY_OPTIONS_VIEW);
70 static void on_entry_changed (GtkEditable *editable, gpointer user_data);
72 /* Tracks changes in the incoming security combo box */
74 on_security_changed (GtkWidget *widget,
75 ModestMaemoSecurityOptionsView *self)
77 ModestSecurityOptionsViewPrivate* ppriv;
78 ModestServersecurityComboBox *combo;
79 ModestProtocolType proto_type;
80 ModestProtocolRegistry *proto_registry;
83 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
85 proto_registry = modest_runtime_get_protocol_registry ();
86 combo = MODEST_SERVERSECURITY_COMBO_BOX (ppriv->security_view);
87 proto_type = modest_serversecurity_combo_box_get_active_serversecurity (combo);
89 is_secure = modest_protocol_registry_protocol_type_has_tag (proto_registry, proto_type,
90 MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS);
92 if (MODEST_SECURITY_OPTIONS_VIEW (self)->type == MODEST_SECURITY_OPTIONS_INCOMING) {
93 /* Activate and dim checkbutton if it's secure */
94 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ppriv->auth_view),
96 gtk_widget_set_sensitive (ppriv->auth_view, !is_secure);
103 modest_serversecurity_combo_box_get_active_serversecurity_port (MODEST_SERVERSECURITY_COMBO_BOX (ppriv->security_view));
106 hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (ppriv->port_view),
113 on_auth_changed (GtkWidget *widget,
114 ModestMaemoSecurityOptionsView *self)
116 ModestSecurityOptionsViewPrivate* ppriv;
117 ModestSecureauthComboBox *combo;
118 ModestProtocolRegistry *protocol_registry;
119 ModestProtocolType auth_proto;
120 gboolean secureauth_used;
121 GtkWidget *user_caption, *pwd_caption;
123 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
124 protocol_registry = modest_runtime_get_protocol_registry ();
125 combo = MODEST_SECUREAUTH_COMBO_BOX (ppriv->auth_view);
127 auth_proto = modest_secureauth_combo_box_get_active_secureauth (combo);
128 secureauth_used = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
131 /* Get captions, well dimm the whole widget */
132 user_caption = gtk_widget_get_parent (ppriv->user_entry);
133 pwd_caption = gtk_widget_get_parent (ppriv->pwd_entry);
135 /* Enable / disable */
136 gtk_widget_set_sensitive (user_caption, secureauth_used);
137 gtk_widget_set_sensitive (pwd_caption, secureauth_used);
139 /* Check if mandatory data is missing */
140 on_entry_changed (GTK_EDITABLE (ppriv->user_entry), (gpointer) self);
144 create_incoming_security (ModestSecurityOptionsView* self,
145 GtkSizeGroup *size_group)
147 ModestSecurityOptionsViewPrivate *ppriv;
148 GtkWidget *combo_caption, *check_caption, *entry_caption = NULL;
150 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
152 /* Create widgets for incoming security */
153 ppriv->security_view = GTK_WIDGET (modest_serversecurity_combo_box_new ());
154 combo_caption = hildon_caption_new (size_group, _("mcen_li_emailsetup_secure_connection"),
155 ppriv->security_view, NULL,
156 HILDON_CAPTION_OPTIONAL);
159 ppriv->port_view = GTK_WIDGET (hildon_number_editor_new (PORT_MIN, PORT_MAX));
160 entry_caption = hildon_caption_new (size_group, _("mcen_fi_emailsetup_port"),
161 ppriv->port_view, NULL,
162 HILDON_CAPTION_OPTIONAL);
165 ppriv->auth_view = gtk_check_button_new ();
167 hildon_caption_new (size_group, _("mcen_li_emailsetup_secure_authentication"),
168 ppriv->auth_view, NULL, HILDON_CAPTION_OPTIONAL);
170 /* Track changes in UI */
171 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
172 G_CALLBACK (on_security_changed), self);
174 /* Pack into container */
175 gtk_box_pack_start (GTK_BOX (self), combo_caption,
176 FALSE, FALSE, MODEST_MARGIN_HALF);
178 gtk_box_pack_start (GTK_BOX (self), entry_caption,
179 FALSE, FALSE, MODEST_MARGIN_HALF);
180 gtk_box_pack_start (GTK_BOX (self), check_caption,
181 FALSE, FALSE, MODEST_MARGIN_HALF);
185 gtk_widget_show (ppriv->port_view);
186 gtk_widget_show (entry_caption);
188 gtk_widget_show (ppriv->security_view);
189 gtk_widget_show (ppriv->auth_view);
190 gtk_widget_show (combo_caption);
191 gtk_widget_show (check_caption);
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 combo 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 ModestSecureauthComboBox *combo;
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 combo = MODEST_SECUREAUTH_COMBO_BOX (ppriv->auth_view);
226 auth_proto = modest_secureauth_combo_box_get_active_secureauth (combo);
227 is_secure = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
231 !g_ascii_strcasecmp (gtk_entry_get_text (GTK_ENTRY (ppriv->user_entry)), "")) {
232 priv->missing_data = TRUE;
234 priv->missing_data = FALSE;
237 /* Emit a signal to notify if mandatory data is missing */
238 g_signal_emit_by_name (G_OBJECT (self), "missing_mandatory_data",
239 priv->missing_data, NULL);
243 create_outgoing_security (ModestSecurityOptionsView* self,
244 GtkSizeGroup *size_group)
246 ModestSecurityOptionsViewPrivate *ppriv;
247 GtkWidget *sec_caption, *auth_caption, *user_caption = NULL;
248 GtkWidget *pwd_caption = NULL, *port_caption = NULL;
250 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
252 /* The secure connection widgets */
253 ppriv->security_view = GTK_WIDGET (modest_serversecurity_combo_box_new ());
254 modest_serversecurity_combo_box_fill (MODEST_SERVERSECURITY_COMBO_BOX (ppriv->security_view),
255 MODEST_PROTOCOLS_TRANSPORT_SMTP);
256 sec_caption = hildon_caption_new (size_group, _("mcen_li_emailsetup_secure_connection"),
257 ppriv->security_view, NULL, HILDON_CAPTION_OPTIONAL);
259 /* The secure authentication widgets */
260 ppriv->auth_view = GTK_WIDGET (modest_secureauth_combo_box_new ());
261 auth_caption = hildon_caption_new (size_group, _("mcen_li_emailsetup_secure_authentication"),
262 ppriv->auth_view, NULL, HILDON_CAPTION_OPTIONAL);
267 /* Username widgets */
268 ppriv->user_entry = GTK_WIDGET (modest_validating_entry_new ());
270 /* Auto-capitalization is the default, so let's turn it off: */
271 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->user_entry),
272 HILDON_GTK_INPUT_MODE_FULL);
274 user_label = g_strdup_printf("%s*", _("mail_fi_username"));
275 user_caption = hildon_caption_new (size_group, user_label,
276 ppriv->user_entry, NULL,
277 HILDON_CAPTION_MANDATORY);
280 /* Prevent the use of some characters. Limit the max
282 modest_validating_entry_set_unallowed_characters_whitespace (
283 MODEST_VALIDATING_ENTRY (ppriv->user_entry));
284 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
285 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
288 /* Password widgets */
289 ppriv->pwd_entry = gtk_entry_new ();
291 /* Auto-capitalization is the default, so let's turn it off */
292 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
293 HILDON_GTK_INPUT_MODE_FULL |
294 HILDON_GTK_INPUT_MODE_INVISIBLE);
295 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
297 pwd_caption = hildon_caption_new (size_group, _("mail_fi_password"),
298 ppriv->pwd_entry, NULL,
299 HILDON_CAPTION_OPTIONAL);
301 ppriv->port_view = GTK_WIDGET (hildon_number_editor_new (PORT_MIN, PORT_MAX));
302 port_caption = hildon_caption_new (size_group, _("mcen_fi_emailsetup_port"),
303 ppriv->port_view, NULL,
304 HILDON_CAPTION_OPTIONAL);
307 /* Track changes in UI */
308 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
309 G_CALLBACK (on_security_changed), self);
311 g_signal_connect (G_OBJECT (ppriv->auth_view), "changed",
312 G_CALLBACK (on_auth_changed), self);
313 g_signal_connect (G_OBJECT (ppriv->user_entry), "changed",
314 G_CALLBACK (on_entry_changed), self);
317 /* Initialize widgets */
318 modest_serversecurity_combo_box_set_active_serversecurity (
319 MODEST_SERVERSECURITY_COMBO_BOX (ppriv->security_view),
320 MODEST_PROTOCOLS_CONNECTION_NONE);
321 modest_secureauth_combo_box_set_active_secureauth (
322 MODEST_SECUREAUTH_COMBO_BOX (ppriv->auth_view),
323 MODEST_PROTOCOLS_AUTH_NONE);
325 /* Pack into container */
327 gtk_box_pack_start (GTK_BOX (self), auth_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
328 gtk_box_pack_start (GTK_BOX (self), user_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
329 gtk_box_pack_start (GTK_BOX (self), pwd_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
330 gtk_box_pack_start (GTK_BOX (self), sec_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
331 gtk_box_pack_start (GTK_BOX (self), port_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
333 /* The order is different */
334 gtk_box_pack_start (GTK_BOX (self), sec_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
335 gtk_box_pack_start (GTK_BOX (self), auth_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
340 gtk_widget_show (ppriv->pwd_entry);
341 gtk_widget_show (ppriv->user_entry);
342 gtk_widget_show (ppriv->port_view);
343 gtk_widget_show (pwd_caption);
344 gtk_widget_show (user_caption);
345 gtk_widget_show (port_caption);
347 gtk_widget_show (ppriv->security_view);
348 gtk_widget_show (ppriv->auth_view);
349 gtk_widget_show (sec_caption);
350 gtk_widget_show (auth_caption);
354 modest_maemo_security_options_view_new (ModestSecurityOptionsType type,
356 GtkSizeGroup *size_group)
358 ModestSecurityOptionsView* self;
359 ModestSecurityOptionsViewPrivate *ppriv;
361 self = (ModestSecurityOptionsView *)
362 g_object_new (MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, NULL);
363 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
367 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
368 create_incoming_security (self, size_group);
370 create_outgoing_security (self, size_group);
372 return (GtkWidget *) self;
376 modest_maemo_security_options_view_load_settings (ModestSecurityOptionsView* self,
377 ModestAccountSettings *settings)
379 ModestSecurityOptionsViewPrivate *ppriv;
380 ModestServerAccountSettings *server_settings;
383 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
385 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
386 server_settings = modest_account_settings_get_store_settings (settings);
388 server_settings = modest_account_settings_get_transport_settings (settings);
389 port_number = modest_server_account_settings_get_port (server_settings);
391 if (port_number == 0) {
392 /* Show the appropriate port number */
393 on_security_changed (ppriv->security_view,
394 MODEST_MAEMO_SECURITY_OPTIONS_VIEW (self));
395 } else if (ppriv->full) {
396 /* Keep the user-entered port-number, or the
397 * already-appropriate automatic port number */
398 hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (ppriv->port_view),
402 g_object_unref (server_settings);
406 modest_maemo_security_options_view_save_settings (ModestSecurityOptionsView* self,
407 ModestAccountSettings *settings)
409 ModestServerAccountSettings *server_settings;
410 ModestSecurityOptionsViewPrivate *ppriv;
413 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
415 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
416 server_settings = modest_account_settings_get_store_settings (settings);
418 server_settings = modest_account_settings_get_transport_settings (settings);
421 server_port = hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (ppriv->port_view));
423 server_port = modest_serversecurity_combo_box_get_active_serversecurity_port (MODEST_SERVERSECURITY_COMBO_BOX (ppriv->security_view));
426 modest_server_account_settings_set_port (server_settings, server_port);
429 g_object_unref (server_settings);
433 modest_maemo_security_options_view_changed (ModestSecurityOptionsView* self,
434 ModestAccountSettings *settings)
436 ModestServerAccountSettings *server_settings;
437 ModestSecurityOptionsViewPrivate *ppriv;
440 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
442 /* If we're not showing the port number then it never changes */
446 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
447 server_settings = modest_account_settings_get_store_settings (settings);
449 server_settings = modest_account_settings_get_transport_settings (settings);
452 hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (ppriv->port_view));
455 g_object_unref (server_settings);
457 if (server_port != ppriv->initial_state.port)
464 modest_security_options_view_has_missing_mandatory_data (ModestSecurityOptionsView* self)
466 ModestMaemoSecurityOptionsViewPrivate *priv;
468 priv = MODEST_MAEMO_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
470 return priv->missing_data;
474 modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj)
479 modest_maemo_security_options_view_finalize (GObject *obj)
481 G_OBJECT_CLASS (modest_maemo_security_options_view_parent_class)->finalize (obj);
486 modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass)
488 GObjectClass *gobject_class = (GObjectClass*) klass;
490 modest_maemo_security_options_view_parent_class = g_type_class_peek_parent (klass);
492 g_type_class_add_private (gobject_class, sizeof (ModestMaemoSecurityOptionsViewPrivate));
493 gobject_class->finalize = modest_maemo_security_options_view_finalize;
495 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->load_settings =
496 modest_maemo_security_options_view_load_settings;
497 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->save_settings =
498 modest_maemo_security_options_view_save_settings;
499 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->changed =
500 modest_maemo_security_options_view_changed;