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 static void modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj);
49 static void modest_maemo_security_options_view_finalize (GObject *obj);
50 static void modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass);
52 G_DEFINE_TYPE (ModestMaemoSecurityOptionsView,
53 modest_maemo_security_options_view,
54 MODEST_TYPE_SECURITY_OPTIONS_VIEW);
56 static void on_entry_changed (GtkEditable *editable, gpointer user_data);
58 /* Tracks changes in the incoming security picker */
60 on_security_changed (GtkWidget *widget,
61 ModestMaemoSecurityOptionsView *self)
63 ModestSecurityOptionsViewPrivate* ppriv;
64 ModestServersecurityPicker *picker;
65 ModestProtocolType proto_type;
66 ModestProtocolRegistry *proto_registry;
69 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
71 proto_registry = modest_runtime_get_protocol_registry ();
72 picker = MODEST_SERVERSECURITY_PICKER (ppriv->security_view);
73 proto_type = modest_serversecurity_picker_get_active_serversecurity (picker);
75 is_secure = modest_protocol_registry_protocol_type_has_tag (proto_registry, proto_type,
76 MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS);
78 if (MODEST_SECURITY_OPTIONS_VIEW (self)->type == MODEST_SECURITY_OPTIONS_INCOMING) {
79 /* Activate and dim checkbutton if it's secure */
80 hildon_check_button_set_active (HILDON_CHECK_BUTTON (ppriv->auth_view),
82 gtk_widget_set_sensitive (ppriv->auth_view, !is_secure);
89 modest_serversecurity_picker_get_active_serversecurity_port (MODEST_SERVERSECURITY_PICKER (ppriv->security_view));
92 modest_number_editor_set_value (MODEST_NUMBER_EDITOR (ppriv->port_view),
99 on_auth_changed (GtkWidget *widget,
100 ModestMaemoSecurityOptionsView *self)
102 ModestSecurityOptionsViewPrivate* ppriv;
103 ModestSecureauthPicker *picker;
104 ModestProtocolRegistry *protocol_registry;
105 ModestProtocolType auth_proto;
106 gboolean secureauth_used;
107 GtkWidget *user_caption, *pwd_caption;
109 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
110 protocol_registry = modest_runtime_get_protocol_registry ();
111 picker = MODEST_SECUREAUTH_PICKER (ppriv->auth_view);
113 auth_proto = modest_secureauth_picker_get_active_secureauth (picker);
114 secureauth_used = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
117 /* Get captions, well dimm the whole widget */
118 user_caption = gtk_widget_get_parent (ppriv->user_entry);
119 pwd_caption = gtk_widget_get_parent (ppriv->pwd_entry);
121 /* Enable / disable */
122 gtk_widget_set_sensitive (user_caption, secureauth_used);
123 gtk_widget_set_sensitive (pwd_caption, secureauth_used);
125 /* Check if mandatory data is missing */
126 on_entry_changed (GTK_EDITABLE (ppriv->user_entry), (gpointer) self);
130 create_incoming_security (ModestSecurityOptionsView* self,
131 GtkSizeGroup *title_size_group,
132 GtkSizeGroup *value_size_group)
134 ModestSecurityOptionsViewPrivate *ppriv;
135 GtkWidget *entry_caption = NULL;
137 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
139 /* Create widgets for incoming security */
140 ppriv->security_view = GTK_WIDGET (modest_serversecurity_picker_new (MODEST_EDITABLE_SIZE,
141 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
142 modest_serversecurity_picker_fill (MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
143 modest_protocol_registry_get_pop_type_id ());
144 modest_maemo_utils_set_hbutton_layout (title_size_group,
146 _("mcen_li_emailsetup_secure_connection"),
147 ppriv->security_view);
150 ppriv->port_view = GTK_WIDGET (modest_number_editor_new (PORT_MIN, PORT_MAX));
151 entry_caption = modest_maemo_utils_create_captioned (title_size_group,
153 _("mcen_fi_emailsetup_port"),
158 ppriv->auth_view = hildon_check_button_new (MODEST_EDITABLE_SIZE);
159 gtk_button_set_label (GTK_BUTTON (ppriv->auth_view), _("mcen_li_emailsetup_secure_authentication"));
160 gtk_button_set_alignment (GTK_BUTTON (ppriv->auth_view), 0.0, 0.5);
162 /* Track changes in UI */
163 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
164 G_CALLBACK (on_security_changed), self);
166 /* Pack into container */
167 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view,
168 FALSE, FALSE, MODEST_MARGIN_HALF);
170 gtk_box_pack_start (GTK_BOX (self), entry_caption,
171 FALSE, FALSE, MODEST_MARGIN_HALF);
172 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view,
173 FALSE, FALSE, MODEST_MARGIN_HALF);
177 gtk_widget_show (ppriv->port_view);
178 gtk_widget_show (entry_caption);
180 gtk_widget_show (ppriv->security_view);
181 gtk_widget_show (ppriv->auth_view);
185 on_entry_max (ModestValidatingEntry *self,
188 modest_platform_information_banner (GTK_WIDGET (self), NULL,
189 _CS("ckdg_ib_maximum_characters_reached"));
193 * TODO: call this whenever the auth picker changes. If we set it
194 * explicitely at the beggining to a value then there is no need to
195 * call this handler directly at the beginning
198 on_entry_changed (GtkEditable *editable,
201 ModestSecurityOptionsView* self;
202 ModestSecurityOptionsViewPrivate *ppriv;
203 ModestProtocolType auth_proto;
204 ModestSecureauthPicker *picker;
205 gboolean is_secure, missing;
206 ModestProtocolRegistry *protocol_registry;
208 self = MODEST_SECURITY_OPTIONS_VIEW (user_data);
209 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
210 protocol_registry = modest_runtime_get_protocol_registry ();
212 /* Outgoing username is mandatory if outgoing auth is secure */
213 picker = MODEST_SECUREAUTH_PICKER (ppriv->auth_view);
214 auth_proto = modest_secureauth_picker_get_active_secureauth (picker);
215 is_secure = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
219 !g_ascii_strcasecmp (hildon_entry_get_text (HILDON_ENTRY (ppriv->user_entry)), "")) {
225 /* Emit a signal to notify if mandatory data is missing */
226 g_signal_emit_by_name (G_OBJECT (self), "missing_mandatory_data",
231 create_outgoing_security (ModestSecurityOptionsView* self,
232 GtkSizeGroup *title_size_group,
233 GtkSizeGroup *value_size_group)
235 ModestSecurityOptionsViewPrivate *ppriv;
236 GtkWidget *user_caption = NULL;
237 GtkWidget *pwd_caption = NULL, *port_caption = NULL;
239 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
241 /* The secure connection widgets */
242 ppriv->security_view = GTK_WIDGET (modest_serversecurity_picker_new (MODEST_EDITABLE_SIZE,
243 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
244 modest_serversecurity_picker_fill (MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
245 MODEST_PROTOCOLS_TRANSPORT_SMTP);
246 modest_maemo_utils_set_hbutton_layout (title_size_group,
248 _("mcen_li_emailsetup_secure_connection"),
249 ppriv->security_view);
251 /* The secure authentication widgets */
252 ppriv->auth_view = GTK_WIDGET (modest_secureauth_picker_new (MODEST_EDITABLE_SIZE,
253 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
254 modest_maemo_utils_set_hbutton_layout (title_size_group,
256 _("mcen_li_emailsetup_secure_authentication"),
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 = modest_maemo_utils_create_captioned (title_size_group,
277 /* Prevent the use of some characters. Limit the max
279 modest_validating_entry_set_unallowed_characters_whitespace (
280 MODEST_VALIDATING_ENTRY (ppriv->user_entry));
281 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
282 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
285 /* Password widgets */
286 ppriv->pwd_entry = hildon_entry_new (MODEST_EDITABLE_SIZE);
288 /* Auto-capitalization is the default, so let's turn it off */
289 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
290 HILDON_GTK_INPUT_MODE_FULL |
291 HILDON_GTK_INPUT_MODE_INVISIBLE);
292 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
294 pwd_caption = modest_maemo_utils_create_captioned (title_size_group,
296 _("mail_fi_password"),
300 ppriv->port_view = GTK_WIDGET (modest_number_editor_new (PORT_MIN, PORT_MAX));
301 port_caption = modest_maemo_utils_create_captioned (title_size_group,
303 _("mcen_fi_emailsetup_port"),
308 /* Track changes in UI */
309 g_signal_connect (G_OBJECT (ppriv->security_view), "value-changed",
310 G_CALLBACK (on_security_changed), self);
312 g_signal_connect (G_OBJECT (ppriv->auth_view), "value-changed",
313 G_CALLBACK (on_auth_changed), self);
314 g_signal_connect (G_OBJECT (ppriv->user_entry), "changed",
315 G_CALLBACK (on_entry_changed), self);
318 /* Initialize widgets */
319 modest_serversecurity_picker_set_active_serversecurity (
320 MODEST_SERVERSECURITY_PICKER (ppriv->security_view),
321 MODEST_PROTOCOLS_CONNECTION_NONE);
322 modest_secureauth_picker_set_active_secureauth (
323 MODEST_SECUREAUTH_PICKER (ppriv->auth_view),
324 MODEST_PROTOCOLS_AUTH_NONE);
326 /* Pack into container */
328 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, MODEST_MARGIN_HALF);
329 gtk_box_pack_start (GTK_BOX (self), user_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
330 gtk_box_pack_start (GTK_BOX (self), pwd_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
331 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, MODEST_MARGIN_HALF);
332 gtk_box_pack_start (GTK_BOX (self), port_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
334 /* The order is different */
335 gtk_box_pack_start (GTK_BOX (self), ppriv->security_view, FALSE, FALSE, MODEST_MARGIN_HALF);
336 gtk_box_pack_start (GTK_BOX (self), ppriv->auth_view, FALSE, FALSE, MODEST_MARGIN_HALF);
341 gtk_widget_show (ppriv->pwd_entry);
342 gtk_widget_show (ppriv->user_entry);
343 gtk_widget_show (ppriv->port_view);
344 gtk_widget_show (pwd_caption);
345 gtk_widget_show (user_caption);
346 gtk_widget_show (port_caption);
348 gtk_widget_show (ppriv->security_view);
349 gtk_widget_show (ppriv->auth_view);
353 modest_maemo_security_options_view_new (ModestSecurityOptionsType type,
355 GtkSizeGroup *title_size_group,
356 GtkSizeGroup *value_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, title_size_group, value_size_group);
370 create_outgoing_security (self, title_size_group, value_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 modest_number_editor_set_value (MODEST_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 = modest_number_editor_get_value (MODEST_NUMBER_EDITOR (ppriv->port_view));
423 server_port = modest_serversecurity_picker_get_active_serversecurity_port (MODEST_SERVERSECURITY_PICKER (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 modest_number_editor_get_value (MODEST_NUMBER_EDITOR (ppriv->port_view));
455 g_object_unref (server_settings);
457 if (server_port != ppriv->initial_state.port)
464 modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj)
469 modest_maemo_security_options_view_finalize (GObject *obj)
471 G_OBJECT_CLASS (modest_maemo_security_options_view_parent_class)->finalize (obj);
476 modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass)
478 GObjectClass *gobject_class = (GObjectClass*) klass;
480 modest_maemo_security_options_view_parent_class = g_type_class_peek_parent (klass);
482 gobject_class->finalize = modest_maemo_security_options_view_finalize;
484 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->load_settings =
485 modest_maemo_security_options_view_load_settings;
486 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->save_settings =
487 modest_maemo_security_options_view_save_settings;
488 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->changed =
489 modest_maemo_security_options_view_changed;