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 #ifdef MODEST_HAVE_HILDON0_WIDGETS
41 #include <hildon-widgets/hildon-caption.h>
42 #include <hildon-widgets/hildon-number-editor.h>
44 #include <hildon/hildon-caption.h>
45 #include <hildon/hildon-number-editor.h>
46 #endif /*MODEST_HAVE_HILDON0_WIDGETS*/
49 #define PORT_MAX 65535
51 static void modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj);
52 static void modest_maemo_security_options_view_finalize (GObject *obj);
53 static void modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass);
55 G_DEFINE_TYPE (ModestMaemoSecurityOptionsView,
56 modest_maemo_security_options_view,
57 MODEST_TYPE_SECURITY_OPTIONS_VIEW);
59 static void on_entry_changed (GtkEditable *editable, gpointer user_data);
61 /* Tracks changes in the incoming security combo box */
63 on_security_changed (GtkWidget *widget,
64 ModestMaemoSecurityOptionsView *self)
66 ModestSecurityOptionsViewPrivate* ppriv;
67 ModestServersecurityComboBox *combo;
68 ModestProtocolType proto_type;
69 ModestProtocolRegistry *proto_registry;
72 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
74 proto_registry = modest_runtime_get_protocol_registry ();
75 combo = MODEST_SERVERSECURITY_COMBO_BOX (ppriv->security_view);
76 proto_type = modest_serversecurity_combo_box_get_active_serversecurity (combo);
78 is_secure = modest_protocol_registry_protocol_type_has_tag (proto_registry, proto_type,
79 MODEST_PROTOCOL_REGISTRY_SECURE_PROTOCOLS);
81 if (MODEST_SECURITY_OPTIONS_VIEW (self)->type == MODEST_SECURITY_OPTIONS_INCOMING) {
82 /* Activate and dim checkbutton if it's secure */
83 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ppriv->auth_view),
85 gtk_widget_set_sensitive (ppriv->auth_view, !is_secure);
92 modest_serversecurity_combo_box_get_active_serversecurity_port (MODEST_SERVERSECURITY_COMBO_BOX (ppriv->security_view));
95 hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (ppriv->port_view),
102 on_auth_changed (GtkWidget *widget,
103 ModestMaemoSecurityOptionsView *self)
105 ModestSecurityOptionsViewPrivate* ppriv;
106 ModestSecureauthComboBox *combo;
107 ModestProtocolRegistry *protocol_registry;
108 ModestProtocolType auth_proto;
109 gboolean secureauth_used;
110 GtkWidget *user_caption, *pwd_caption;
112 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
113 protocol_registry = modest_runtime_get_protocol_registry ();
114 combo = MODEST_SECUREAUTH_COMBO_BOX (ppriv->auth_view);
116 auth_proto = modest_secureauth_combo_box_get_active_secureauth (combo);
117 secureauth_used = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
120 /* Get captions, well dimm the whole widget */
121 user_caption = gtk_widget_get_parent (ppriv->user_entry);
122 pwd_caption = gtk_widget_get_parent (ppriv->pwd_entry);
124 /* Enable / disable */
125 gtk_widget_set_sensitive (user_caption, secureauth_used);
126 gtk_widget_set_sensitive (pwd_caption, secureauth_used);
128 /* Check if mandatory data is missing */
129 on_entry_changed (GTK_EDITABLE (ppriv->user_entry), (gpointer) self);
133 create_incoming_security (ModestSecurityOptionsView* self,
134 GtkSizeGroup *size_group)
136 ModestSecurityOptionsViewPrivate *ppriv;
137 GtkWidget *combo_caption, *check_caption, *entry_caption = NULL;
139 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
141 /* Create widgets for incoming security */
142 ppriv->security_view = GTK_WIDGET (modest_serversecurity_combo_box_new ());
143 combo_caption = hildon_caption_new (size_group, _("mcen_li_emailsetup_secure_connection"),
144 ppriv->security_view, NULL,
145 HILDON_CAPTION_OPTIONAL);
148 ppriv->port_view = GTK_WIDGET (hildon_number_editor_new (PORT_MIN, PORT_MAX));
149 entry_caption = hildon_caption_new (size_group, _("mcen_fi_emailsetup_port"),
150 ppriv->port_view, NULL,
151 HILDON_CAPTION_OPTIONAL);
154 ppriv->auth_view = gtk_check_button_new ();
156 hildon_caption_new (size_group, _("mcen_li_emailsetup_secure_authentication"),
157 ppriv->auth_view, NULL, HILDON_CAPTION_OPTIONAL);
159 /* Track changes in UI */
160 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
161 G_CALLBACK (on_security_changed), self);
163 /* Pack into container */
164 gtk_box_pack_start (GTK_BOX (self), combo_caption,
165 FALSE, FALSE, MODEST_MARGIN_HALF);
167 gtk_box_pack_start (GTK_BOX (self), entry_caption,
168 FALSE, FALSE, MODEST_MARGIN_HALF);
169 gtk_box_pack_start (GTK_BOX (self), check_caption,
170 FALSE, FALSE, MODEST_MARGIN_HALF);
174 gtk_widget_show (ppriv->port_view);
175 gtk_widget_show (entry_caption);
177 gtk_widget_show (ppriv->security_view);
178 gtk_widget_show (ppriv->auth_view);
179 gtk_widget_show (combo_caption);
180 gtk_widget_show (check_caption);
184 on_entry_max (ModestValidatingEntry *self,
187 modest_platform_information_banner (GTK_WIDGET (self), NULL,
188 _CS("ckdg_ib_maximum_characters_reached"));
192 * TODO: call this whenever the auth combo changes. If we set it
193 * explicitely at the beggining to a value then there is no need to
194 * call this handler directly at the beginning
197 on_entry_changed (GtkEditable *editable,
200 ModestSecurityOptionsView* self;
201 ModestSecurityOptionsViewPrivate *ppriv;
202 ModestProtocolType auth_proto;
203 ModestSecureauthComboBox *combo;
204 gboolean is_secure, missing;
205 ModestProtocolRegistry *protocol_registry;
207 self = MODEST_SECURITY_OPTIONS_VIEW (user_data);
208 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
209 protocol_registry = modest_runtime_get_protocol_registry ();
211 /* Outgoing username is mandatory if outgoing auth is secure */
212 combo = MODEST_SECUREAUTH_COMBO_BOX (ppriv->auth_view);
213 auth_proto = modest_secureauth_combo_box_get_active_secureauth (combo);
214 is_secure = modest_protocol_registry_protocol_type_is_secure (protocol_registry,
218 !g_ascii_strcasecmp (gtk_entry_get_text (GTK_ENTRY (ppriv->user_entry)), "")) {
224 /* Emit a signal to notify if mandatory data is missing */
225 g_signal_emit_by_name (G_OBJECT (self), "missing_mandatory_data",
230 create_outgoing_security (ModestSecurityOptionsView* self,
231 GtkSizeGroup *size_group)
233 ModestSecurityOptionsViewPrivate *ppriv;
234 GtkWidget *sec_caption, *auth_caption, *user_caption = NULL;
235 GtkWidget *pwd_caption = NULL, *port_caption = NULL;
237 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
239 /* The secure connection widgets */
240 ppriv->security_view = GTK_WIDGET (modest_serversecurity_combo_box_new ());
241 modest_serversecurity_combo_box_fill (MODEST_SERVERSECURITY_COMBO_BOX (ppriv->security_view),
242 MODEST_PROTOCOLS_TRANSPORT_SMTP);
243 sec_caption = hildon_caption_new (size_group, _("mcen_li_emailsetup_secure_connection"),
244 ppriv->security_view, NULL, HILDON_CAPTION_OPTIONAL);
246 /* The secure authentication widgets */
247 ppriv->auth_view = GTK_WIDGET (modest_secureauth_combo_box_new ());
248 auth_caption = hildon_caption_new (size_group, _("mcen_li_emailsetup_secure_authentication"),
249 ppriv->auth_view, NULL, HILDON_CAPTION_OPTIONAL);
254 /* Username widgets */
255 ppriv->user_entry = GTK_WIDGET (modest_validating_entry_new ());
257 /* Auto-capitalization is the default, so let's turn it off: */
258 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->user_entry),
259 HILDON_GTK_INPUT_MODE_FULL);
261 user_label = g_strdup_printf("%s*", _("mail_fi_username"));
262 user_caption = hildon_caption_new (size_group, user_label,
263 ppriv->user_entry, NULL,
264 HILDON_CAPTION_MANDATORY);
267 /* Prevent the use of some characters. Limit the max
269 modest_validating_entry_set_unallowed_characters_whitespace (
270 MODEST_VALIDATING_ENTRY (ppriv->user_entry));
271 gtk_entry_set_max_length (GTK_ENTRY (ppriv->user_entry), 64);
272 modest_validating_entry_set_max_func (MODEST_VALIDATING_ENTRY (ppriv->user_entry),
275 /* Password widgets */
276 ppriv->pwd_entry = gtk_entry_new ();
278 /* Auto-capitalization is the default, so let's turn it off */
279 hildon_gtk_entry_set_input_mode (GTK_ENTRY (ppriv->pwd_entry),
280 HILDON_GTK_INPUT_MODE_FULL |
281 HILDON_GTK_INPUT_MODE_INVISIBLE);
282 gtk_entry_set_visibility (GTK_ENTRY (ppriv->pwd_entry), FALSE);
284 pwd_caption = hildon_caption_new (size_group, _("mail_fi_password"),
285 ppriv->pwd_entry, NULL,
286 HILDON_CAPTION_OPTIONAL);
288 ppriv->port_view = GTK_WIDGET (hildon_number_editor_new (PORT_MIN, PORT_MAX));
289 port_caption = hildon_caption_new (size_group, _("mcen_fi_emailsetup_port"),
290 ppriv->port_view, NULL,
291 HILDON_CAPTION_OPTIONAL);
294 /* Track changes in UI */
295 g_signal_connect (G_OBJECT (ppriv->security_view), "changed",
296 G_CALLBACK (on_security_changed), self);
298 g_signal_connect (G_OBJECT (ppriv->auth_view), "changed",
299 G_CALLBACK (on_auth_changed), self);
300 g_signal_connect (G_OBJECT (ppriv->user_entry), "changed",
301 G_CALLBACK (on_entry_changed), self);
304 /* Initialize widgets */
305 modest_serversecurity_combo_box_set_active_serversecurity (
306 MODEST_SERVERSECURITY_COMBO_BOX (ppriv->security_view),
307 MODEST_PROTOCOLS_CONNECTION_NONE);
308 modest_secureauth_combo_box_set_active_secureauth (
309 MODEST_SECUREAUTH_COMBO_BOX (ppriv->auth_view),
310 MODEST_PROTOCOLS_AUTH_NONE);
312 /* Pack into container */
314 gtk_box_pack_start (GTK_BOX (self), auth_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
315 gtk_box_pack_start (GTK_BOX (self), user_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
316 gtk_box_pack_start (GTK_BOX (self), pwd_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
317 gtk_box_pack_start (GTK_BOX (self), sec_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
318 gtk_box_pack_start (GTK_BOX (self), port_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
320 /* The order is different */
321 gtk_box_pack_start (GTK_BOX (self), sec_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
322 gtk_box_pack_start (GTK_BOX (self), auth_caption, FALSE, FALSE, MODEST_MARGIN_HALF);
327 gtk_widget_show (ppriv->pwd_entry);
328 gtk_widget_show (ppriv->user_entry);
329 gtk_widget_show (ppriv->port_view);
330 gtk_widget_show (pwd_caption);
331 gtk_widget_show (user_caption);
332 gtk_widget_show (port_caption);
334 gtk_widget_show (ppriv->security_view);
335 gtk_widget_show (ppriv->auth_view);
336 gtk_widget_show (sec_caption);
337 gtk_widget_show (auth_caption);
341 modest_maemo_security_options_view_new (ModestSecurityOptionsType type,
343 GtkSizeGroup *size_group)
345 ModestSecurityOptionsView* self;
346 ModestSecurityOptionsViewPrivate *ppriv;
348 self = (ModestSecurityOptionsView *)
349 g_object_new (MODEST_TYPE_MAEMO_SECURITY_OPTIONS_VIEW, NULL);
350 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
354 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
355 create_incoming_security (self, size_group);
357 create_outgoing_security (self, size_group);
359 return (GtkWidget *) self;
363 modest_maemo_security_options_view_load_settings (ModestSecurityOptionsView* self,
364 ModestAccountSettings *settings)
366 ModestSecurityOptionsViewPrivate *ppriv;
367 ModestServerAccountSettings *server_settings;
370 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
372 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
373 server_settings = modest_account_settings_get_store_settings (settings);
375 server_settings = modest_account_settings_get_transport_settings (settings);
376 port_number = modest_server_account_settings_get_port (server_settings);
378 if (port_number == 0) {
379 /* Show the appropriate port number */
380 on_security_changed (ppriv->security_view,
381 MODEST_MAEMO_SECURITY_OPTIONS_VIEW (self));
382 } else if (ppriv->full) {
383 /* Keep the user-entered port-number, or the
384 * already-appropriate automatic port number */
385 hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (ppriv->port_view),
389 g_object_unref (server_settings);
393 modest_maemo_security_options_view_save_settings (ModestSecurityOptionsView* self,
394 ModestAccountSettings *settings)
396 ModestServerAccountSettings *server_settings;
397 ModestSecurityOptionsViewPrivate *ppriv;
400 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
402 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
403 server_settings = modest_account_settings_get_store_settings (settings);
405 server_settings = modest_account_settings_get_transport_settings (settings);
408 server_port = hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (ppriv->port_view));
410 server_port = modest_serversecurity_combo_box_get_active_serversecurity_port (MODEST_SERVERSECURITY_COMBO_BOX (ppriv->security_view));
413 modest_server_account_settings_set_port (server_settings, server_port);
416 g_object_unref (server_settings);
420 modest_maemo_security_options_view_changed (ModestSecurityOptionsView* self,
421 ModestAccountSettings *settings)
423 ModestServerAccountSettings *server_settings;
424 ModestSecurityOptionsViewPrivate *ppriv;
427 ppriv = MODEST_SECURITY_OPTIONS_VIEW_GET_PRIVATE (self);
429 /* If we're not showing the port number then it never changes */
433 if (self->type == MODEST_SECURITY_OPTIONS_INCOMING)
434 server_settings = modest_account_settings_get_store_settings (settings);
436 server_settings = modest_account_settings_get_transport_settings (settings);
439 hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (ppriv->port_view));
442 g_object_unref (server_settings);
444 if (server_port != ppriv->initial_state.port)
451 modest_maemo_security_options_view_init (ModestMaemoSecurityOptionsView *obj)
456 modest_maemo_security_options_view_finalize (GObject *obj)
458 G_OBJECT_CLASS (modest_maemo_security_options_view_parent_class)->finalize (obj);
463 modest_maemo_security_options_view_class_init (ModestMaemoSecurityOptionsViewClass *klass)
465 GObjectClass *gobject_class = (GObjectClass*) klass;
467 modest_maemo_security_options_view_parent_class = g_type_class_peek_parent (klass);
469 gobject_class->finalize = modest_maemo_security_options_view_finalize;
471 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->load_settings =
472 modest_maemo_security_options_view_load_settings;
473 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->save_settings =
474 modest_maemo_security_options_view_save_settings;
475 MODEST_SECURITY_OPTIONS_VIEW_CLASS (klass)->changed =
476 modest_maemo_security_options_view_changed;