1 /* Copyright (c) 2006, 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-connection-specific-smtp-edit-window.h"
31 #include "widgets/modest-ui-constants.h"
32 #include "modest-hildon-includes.h"
33 #include "modest-runtime.h"
35 #include "widgets/modest-serversecurity-combo-box.h"
36 #include "widgets/modest-secureauth-combo-box.h"
37 #include "widgets/modest-validating-entry.h"
38 #include <gtk/gtkbutton.h>
39 #include <gtk/gtkhbox.h>
40 #include <gtk/gtkvbox.h>
41 #include <gtk/gtkstock.h>
42 #include "modest-text-utils.h"
43 #include "modest-maemo-utils.h"
45 #include <glib/gi18n.h>
47 #define PORT_RANGE_MIN 1
48 #define PORT_RANGE_MAX 65535
50 G_DEFINE_TYPE (ModestConnectionSpecificSmtpEditWindow, modest_connection_specific_smtp_edit_window, GTK_TYPE_DIALOG);
52 #define CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE(o) \
53 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW, ModestConnectionSpecificSmtpEditWindowPrivate))
55 typedef struct _ModestConnectionSpecificSmtpEditWindowPrivate ModestConnectionSpecificSmtpEditWindowPrivate;
57 struct _ModestConnectionSpecificSmtpEditWindowPrivate
59 GtkWidget *entry_outgoingserver;
60 GtkWidget *combo_outgoing_auth;
61 GtkWidget *entry_user_username;
62 GtkWidget *entry_user_password;
63 GtkWidget *combo_outgoing_security;
64 GtkWidget *entry_port;
67 GtkWidget *button_cancel;
70 gboolean range_error_occured;
71 guint range_error_banner_timeout;
75 modest_connection_specific_smtp_edit_window_get_property (GObject *object, guint property_id,
76 GValue *value, GParamSpec *pspec)
78 switch (property_id) {
80 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
85 modest_connection_specific_smtp_edit_window_set_property (GObject *object, guint property_id,
86 const GValue *value, GParamSpec *pspec)
88 switch (property_id) {
90 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
95 modest_connection_specific_smtp_edit_window_dispose (GObject *object)
99 if (G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->dispose)
100 G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->dispose (object);
104 modest_connection_specific_smtp_edit_window_finalize (GObject *object)
106 ModestConnectionSpecificSmtpEditWindow *self = (ModestConnectionSpecificSmtpEditWindow *) object;
107 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
108 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
110 if (priv->range_error_banner_timeout > 0) {
111 g_source_remove (priv->range_error_banner_timeout);
112 priv->range_error_banner_timeout = 0;
114 G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->finalize (object);
118 modest_connection_specific_smtp_edit_window_class_init (ModestConnectionSpecificSmtpEditWindowClass *klass)
120 GObjectClass *object_class = G_OBJECT_CLASS (klass);
122 g_type_class_add_private (klass, sizeof (ModestConnectionSpecificSmtpEditWindowPrivate));
124 object_class->get_property = modest_connection_specific_smtp_edit_window_get_property;
125 object_class->set_property = modest_connection_specific_smtp_edit_window_set_property;
126 object_class->dispose = modest_connection_specific_smtp_edit_window_dispose;
127 object_class->finalize = modest_connection_specific_smtp_edit_window_finalize;
132 MODEL_COL_SERVER_NAME = 1,
137 on_change(GtkWidget* widget, ModestConnectionSpecificSmtpEditWindow *self)
139 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
140 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
141 priv->is_dirty = TRUE;
145 on_value_changed(GtkWidget* widget, GValue* value, ModestConnectionSpecificSmtpEditWindow *self)
147 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
148 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
150 priv->range_error_occured = FALSE;
151 on_change(widget, self);
155 show_banner_handler (gpointer userdata)
157 ModestConnectionSpecificSmtpEditWindow *self = userdata;
158 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
159 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
162 msg = g_strdup_printf (dgettext("hildon-libs", "ckct_ib_set_a_value_within_range"), PORT_RANGE_MIN, PORT_RANGE_MAX);
164 hildon_banner_show_information (NULL, NULL, msg);
167 priv->range_error_banner_timeout = 0;
172 on_range_error (GtkWidget *widget, HildonNumberEditorErrorType type, gpointer user_data)
174 ModestConnectionSpecificSmtpEditWindow *self = user_data;
175 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
176 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
178 /* We want to prevent the closure of the dialog when a range error occured. The problem is that
179 * the hildon number editor already resets the value to the default value, so we have to
180 * remember that such an error occured. */
181 priv->range_error_occured = TRUE;
182 if (priv->range_error_banner_timeout == 0)
183 priv->range_error_banner_timeout = g_timeout_add (200, show_banner_handler, self);
185 /* Show error message by not returning TRUE */
190 on_response (GtkDialog *dialog, int response_id, gpointer user_data)
192 const gchar *hostname;
193 ModestConnectionSpecificSmtpEditWindow *self = user_data;
194 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
195 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
197 hostname = gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver));
199 if ((response_id == GTK_RESPONSE_CANCEL) &&
200 (priv->range_error_banner_timeout > 0)) {
201 g_source_remove (priv->range_error_banner_timeout);
202 priv->range_error_banner_timeout = 0;
205 /* Don't close the dialog if a range error occured */
206 if(response_id == GTK_RESPONSE_OK && priv->range_error_occured)
208 priv->range_error_occured = FALSE;
209 g_signal_stop_emission_by_name (dialog, "response");
210 gtk_widget_grab_focus (priv->entry_port);
214 /* Don't close the dialog if a range error occured */
215 if(response_id == GTK_RESPONSE_OK) {
216 if (hostname && (hostname[0] != '\0') &&
217 (!modest_text_utils_validate_domain_name (hostname))) {
218 g_signal_stop_emission_by_name (dialog, "response");
219 hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_servername"));
220 gtk_widget_grab_focus (priv->entry_outgoingserver);
221 gtk_editable_select_region (GTK_EDITABLE (priv->entry_outgoingserver), 0, -1);
228 static void on_set_focus_child (GtkContainer *container, GtkWidget *widget, gpointer user_data)
230 ModestConnectionSpecificSmtpEditWindow *self = user_data;
231 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
232 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
234 /* Another child gained focus. Since the number editor already reset a
235 * possible range error to the default value, we allow closure of the
237 priv->range_error_occured = FALSE;
241 on_combo_security_changed (GtkComboBox *widget, gpointer user_data)
243 ModestConnectionSpecificSmtpEditWindow *self =
244 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (user_data);
245 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
246 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
248 on_change(GTK_WIDGET(widget), self);
250 const gint port_number =
251 modest_serversecurity_combo_box_get_active_serversecurity_port (
252 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security));
254 if(port_number != 0) {
255 hildon_number_editor_set_value (
256 HILDON_NUMBER_EDITOR (priv->entry_port), port_number);
261 modest_connection_specific_smtp_edit_window_init (ModestConnectionSpecificSmtpEditWindow *self)
263 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
264 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
266 GtkWidget *box = GTK_DIALOG(self)->vbox; /* gtk_vbox_new (FALSE, MODEST_MARGIN_HALF); */
267 gtk_box_set_spacing (GTK_BOX (box), MODEST_MARGIN_NONE);
268 gtk_container_set_border_width (GTK_CONTAINER (box), MODEST_MARGIN_HALF);
270 /* Create a size group to be used by all captions.
271 * Note that HildonCaption does not create a default size group if we do not specify one.
272 * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
273 GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
275 /* The outgoing server widgets: */
276 if (!priv->entry_outgoingserver)
277 priv->entry_outgoingserver = gtk_entry_new ();
278 /* Auto-capitalization is the default, so let's turn it off: */
279 hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
280 g_signal_connect(G_OBJECT(priv->entry_outgoingserver), "changed", G_CALLBACK(on_change), self);
282 GtkWidget *caption = hildon_caption_new (sizegroup,
283 _("mcen_li_emailsetup_smtp"), priv->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
284 gtk_widget_show (priv->entry_outgoingserver);
285 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
286 gtk_widget_show (caption);
288 /* The secure authentication widgets: */
289 if (!priv->combo_outgoing_auth)
290 priv->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
291 caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"),
292 priv->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
293 g_signal_connect (G_OBJECT (priv->combo_outgoing_auth), "changed", G_CALLBACK(on_change), self);
294 gtk_widget_show (priv->combo_outgoing_auth);
295 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
296 gtk_widget_show (caption);
298 /* The username widgets: */
299 priv->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
300 /* Auto-capitalization is the default, so let's turn it off: */
301 hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_username), HILDON_GTK_INPUT_MODE_FULL);
302 caption = hildon_caption_new (sizegroup, _("mail_fi_username"),
303 priv->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
304 g_signal_connect(G_OBJECT(priv->entry_user_username), "changed", G_CALLBACK(on_change), self);
305 gtk_widget_show (priv->entry_user_username);
306 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
307 gtk_widget_show (caption);
309 /* Prevent the use of some characters in the username,
310 * as required by our UI specification: */
311 modest_validating_entry_set_unallowed_characters_whitespace (
312 MODEST_VALIDATING_ENTRY (priv->entry_user_username));
314 /* Set max length as in the UI spec:
315 * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
316 gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_username), 64);
318 /* The password widgets: */
319 priv->entry_user_password = gtk_entry_new ();
320 /* Auto-capitalization is the default, so let's turn it off: */
321 hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_password),
322 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
323 gtk_entry_set_visibility (GTK_ENTRY (priv->entry_user_password), FALSE);
324 /* gtk_entry_set_invisible_char (GTK_ENTRY (priv->entry_user_password), '*'); */
325 caption = hildon_caption_new (sizegroup,
326 _("mail_fi_password"), priv->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
327 g_signal_connect(G_OBJECT(priv->entry_user_password), "changed", G_CALLBACK(on_change), self);
328 gtk_widget_show (priv->entry_user_password);
329 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
330 gtk_widget_show (caption);
332 /* The secure connection widgets: */
333 if (!priv->combo_outgoing_security)
334 priv->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
335 modest_serversecurity_combo_box_fill (
336 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), MODEST_PROTOCOL_TRANSPORT_SMTP);
337 modest_serversecurity_combo_box_set_active_serversecurity (
338 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), MODEST_PROTOCOL_CONNECTION_NORMAL);
339 caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"),
340 priv->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
341 gtk_widget_show (priv->combo_outgoing_security);
342 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
343 gtk_widget_show (caption);
345 /* The port number widgets: */
346 if (!priv->entry_port)
347 priv->entry_port = GTK_WIDGET (hildon_number_editor_new (PORT_RANGE_MIN, PORT_RANGE_MAX));
348 caption = hildon_caption_new (sizegroup,
349 _("mcen_fi_emailsetup_port"), priv->entry_port, NULL, HILDON_CAPTION_OPTIONAL);
350 gtk_widget_add_events(GTK_WIDGET(priv->entry_port), GDK_FOCUS_CHANGE_MASK);
351 g_signal_connect(G_OBJECT(priv->entry_port), "range-error", G_CALLBACK(on_range_error), self);
352 g_signal_connect(G_OBJECT(priv->entry_port), "notify::value", G_CALLBACK(on_value_changed), self);
353 gtk_widget_show (priv->entry_port);
354 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
355 gtk_widget_show (caption);
357 /* Show a default port number when the security method changes, as per the UI spec: */
358 g_signal_connect (G_OBJECT (priv->combo_outgoing_security), "changed", (GCallback)on_combo_security_changed, self);
359 on_combo_security_changed (GTK_COMBO_BOX (priv->combo_outgoing_security), self);
361 /* Add the buttons: */
362 gtk_dialog_add_button (GTK_DIALOG (self), _("mcen_bd_dialog_ok"), GTK_RESPONSE_OK);
363 gtk_dialog_add_button (GTK_DIALOG (self), _("mcen_bd_dialog_cancel"), GTK_RESPONSE_CANCEL);
365 priv->is_dirty = FALSE;
366 priv->range_error_occured = FALSE;
367 g_signal_connect(G_OBJECT(self), "response", G_CALLBACK(on_response), self);
368 g_signal_connect(G_OBJECT(box), "set-focus-child", G_CALLBACK(on_set_focus_child), self);
370 priv->range_error_banner_timeout = 0;
372 gtk_widget_show (box);
375 /* When this window is shown, hibernation should not be possible,
376 * because there is no sensible way to save the state: */
377 modest_window_mgr_prevent_hibernation_while_window_is_shown (
378 modest_runtime_get_window_mgr (), GTK_WINDOW (self));
380 hildon_help_dialog_help_enable (GTK_DIALOG(self),
381 "applications_email_connectionspecificsmtpconf",
382 modest_maemo_utils_get_osso_context());
385 ModestConnectionSpecificSmtpEditWindow*
386 modest_connection_specific_smtp_edit_window_new (void)
388 return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW, NULL);
392 modest_connection_specific_smtp_edit_window_set_connection (
393 ModestConnectionSpecificSmtpEditWindow *window, const gchar* iap_id, const gchar* iap_name,
394 ModestServerAccountSettings *server_settings)
396 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
397 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
399 /* This causes a warning because of the %s in the translation, but not in the original string: */
400 gchar* title = g_strdup_printf (_("mcen_ti_connection_connection_name"), iap_name);
401 gtk_window_set_title (GTK_WINDOW (window), title);
406 gtk_entry_set_text (GTK_ENTRY (priv->entry_outgoingserver),
407 modest_server_account_settings_get_hostname (server_settings));
408 gtk_entry_set_text (GTK_ENTRY (priv->entry_user_username),
409 modest_server_account_settings_get_username (server_settings));
410 gtk_entry_set_text (GTK_ENTRY (priv->entry_user_password),
411 modest_server_account_settings_get_password (server_settings));
413 modest_serversecurity_combo_box_set_active_serversecurity (
414 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security),
415 modest_server_account_settings_get_security (server_settings));
417 modest_secureauth_combo_box_set_active_secureauth (
418 MODEST_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth),
419 modest_server_account_settings_get_auth_protocol (server_settings));
422 hildon_number_editor_set_value (
423 HILDON_NUMBER_EDITOR (priv->entry_port),
424 modest_server_account_settings_get_port (server_settings));
427 /* This will cause changed signals so we set dirty back to FALSE */
428 priv->is_dirty = FALSE;
429 if (priv->range_error_banner_timeout > 0) {
430 g_source_remove (priv->range_error_banner_timeout);
431 priv->range_error_banner_timeout = 0;
437 ModestServerAccountSettings*
438 modest_connection_specific_smtp_edit_window_get_settings (ModestConnectionSpecificSmtpEditWindow *window)
440 ModestConnectionSpecificSmtpEditWindowPrivate *priv = NULL;
441 ModestServerAccountSettings *server_settings = NULL;
442 const gchar *outgoing_server = NULL;
444 priv = CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
445 outgoing_server = gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver));
447 /* If the outgoing server is NULL, we are removing the connection specific
449 if ((outgoing_server == NULL) || (outgoing_server[0] == '\0')) {
453 server_settings = modest_server_account_settings_new ();
455 modest_server_account_settings_set_hostname (server_settings,
456 gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver)));
457 modest_server_account_settings_set_protocol (server_settings,
458 MODEST_PROTOCOL_TRANSPORT_SMTP);
459 modest_server_account_settings_set_username (server_settings,
460 gtk_entry_get_text (GTK_ENTRY (priv->entry_user_username)));
461 modest_server_account_settings_set_password (server_settings,
462 gtk_entry_get_text (GTK_ENTRY (priv->entry_user_password)));
464 modest_server_account_settings_set_security (server_settings,
465 modest_serversecurity_combo_box_get_active_serversecurity (
466 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security)));
467 modest_server_account_settings_set_auth_protocol (server_settings,
468 modest_secureauth_combo_box_get_active_secureauth (
469 MODEST_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth)));
472 modest_server_account_settings_set_port (server_settings,
473 hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (priv->entry_port)));
475 return server_settings;
478 gboolean modest_connection_specific_smtp_edit_window_is_dirty(
479 ModestConnectionSpecificSmtpEditWindow *window)
481 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
482 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
484 return priv->is_dirty;