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 <modest-account-mgr-helpers.h>
39 #include <gtk/gtkbutton.h>
40 #include <gtk/gtkhbox.h>
41 #include <gtk/gtkvbox.h>
42 #include <gtk/gtkstock.h>
43 #include "modest-text-utils.h"
45 #include <glib/gi18n.h>
47 G_DEFINE_TYPE (ModestConnectionSpecificSmtpEditWindow, modest_connection_specific_smtp_edit_window, GTK_TYPE_DIALOG);
49 #define CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE(o) \
50 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW, ModestConnectionSpecificSmtpEditWindowPrivate))
52 typedef struct _ModestConnectionSpecificSmtpEditWindowPrivate ModestConnectionSpecificSmtpEditWindowPrivate;
54 struct _ModestConnectionSpecificSmtpEditWindowPrivate
56 GtkWidget *entry_outgoingserver;
57 GtkWidget *combo_outgoing_auth;
58 GtkWidget *entry_user_username;
59 GtkWidget *entry_user_password;
60 GtkWidget *combo_outgoing_security;
61 GtkWidget *entry_port;
64 GtkWidget *button_cancel;
67 gboolean range_error_occured;
71 modest_connection_specific_smtp_edit_window_get_property (GObject *object, guint property_id,
72 GValue *value, GParamSpec *pspec)
74 switch (property_id) {
76 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
81 modest_connection_specific_smtp_edit_window_set_property (GObject *object, guint property_id,
82 const GValue *value, GParamSpec *pspec)
84 switch (property_id) {
86 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
91 modest_connection_specific_smtp_edit_window_dispose (GObject *object)
93 if (G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->dispose)
94 G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->dispose (object);
98 modest_connection_specific_smtp_edit_window_finalize (GObject *object)
100 G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->finalize (object);
104 modest_connection_specific_smtp_edit_window_class_init (ModestConnectionSpecificSmtpEditWindowClass *klass)
106 GObjectClass *object_class = G_OBJECT_CLASS (klass);
108 g_type_class_add_private (klass, sizeof (ModestConnectionSpecificSmtpEditWindowPrivate));
110 object_class->get_property = modest_connection_specific_smtp_edit_window_get_property;
111 object_class->set_property = modest_connection_specific_smtp_edit_window_set_property;
112 object_class->dispose = modest_connection_specific_smtp_edit_window_dispose;
113 object_class->finalize = modest_connection_specific_smtp_edit_window_finalize;
118 MODEL_COL_SERVER_NAME = 1,
123 on_change(GtkWidget* widget, ModestConnectionSpecificSmtpEditWindow *self)
125 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
126 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
127 priv->is_dirty = TRUE;
131 on_value_changed(GtkWidget* widget, GValue* value, ModestConnectionSpecificSmtpEditWindow *self)
133 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
134 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
136 priv->range_error_occured = FALSE;
137 on_change(widget, self);
141 on_range_error (GtkWidget *widget, HildonNumberEditorErrorType type, gpointer user_data)
143 ModestConnectionSpecificSmtpEditWindow *self = user_data;
144 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
145 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
147 /* We want to prevent the closure of the dialog when a range error occured. The problem is that
148 * the hildon number editor already resets the value to the default value, so we have to
149 * remember that such an error occured. */
150 priv->range_error_occured = TRUE;
152 /* Show error message by not returning TRUE */
157 on_response (GtkDialog *dialog, int response_id, gpointer user_data)
159 const gchar *hostname;
160 ModestConnectionSpecificSmtpEditWindow *self = user_data;
161 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
162 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
164 hostname = gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver));
166 /* Don't close the dialog if a range error occured */
167 if(priv->range_error_occured)
169 priv->range_error_occured = FALSE;
170 g_signal_stop_emission_by_name (dialog, "response");
171 gtk_widget_grab_focus (priv->entry_port);
175 /* Don't close the dialog if a range error occured */
176 if(response_id == GTK_RESPONSE_OK) {
177 if (!modest_text_utils_validate_domain_name (hostname)) {
178 g_signal_stop_emission_by_name (dialog, "response");
179 hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_servername"));
180 gtk_widget_grab_focus (priv->entry_outgoingserver);
181 gtk_editable_select_region (GTK_EDITABLE (priv->entry_outgoingserver), 0, -1);
188 static void on_set_focus_child (GtkContainer *container, GtkWidget *widget, gpointer user_data)
190 ModestConnectionSpecificSmtpEditWindow *self = user_data;
191 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
192 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
194 /* Another child gained focus. Since the number editor already reset a
195 * possible range error to the default value, we allow closure of the
197 priv->range_error_occured = FALSE;
201 on_combo_security_changed (GtkComboBox *widget, gpointer user_data)
203 ModestConnectionSpecificSmtpEditWindow *self =
204 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (user_data);
205 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
206 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
208 on_change(GTK_WIDGET(widget), self);
210 const gint port_number =
211 modest_serversecurity_combo_box_get_active_serversecurity_port (
212 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security));
214 if(port_number != 0) {
215 hildon_number_editor_set_value (
216 HILDON_NUMBER_EDITOR (priv->entry_port), port_number);
221 modest_connection_specific_smtp_edit_window_init (ModestConnectionSpecificSmtpEditWindow *self)
223 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
224 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
226 GtkWidget *box = GTK_DIALOG(self)->vbox; /* gtk_vbox_new (FALSE, MODEST_MARGIN_HALF); */
227 gtk_box_set_spacing (GTK_BOX (box), MODEST_MARGIN_NONE);
228 gtk_container_set_border_width (GTK_CONTAINER (box), MODEST_MARGIN_HALF);
230 /* Create a size group to be used by all captions.
231 * Note that HildonCaption does not create a default size group if we do not specify one.
232 * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
233 GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
235 /* The outgoing server widgets: */
236 if (!priv->entry_outgoingserver)
237 priv->entry_outgoingserver = gtk_entry_new ();
238 /* Auto-capitalization is the default, so let's turn it off: */
239 hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
240 g_signal_connect(G_OBJECT(priv->entry_outgoingserver), "changed", G_CALLBACK(on_change), self);
242 GtkWidget *caption = hildon_caption_new (sizegroup,
243 _("mcen_li_emailsetup_smtp"), priv->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
244 gtk_widget_show (priv->entry_outgoingserver);
245 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
246 gtk_widget_show (caption);
248 /* The secure authentication widgets: */
249 if (!priv->combo_outgoing_auth)
250 priv->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
251 caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"),
252 priv->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
253 g_signal_connect (G_OBJECT (priv->combo_outgoing_auth), "changed", G_CALLBACK(on_change), self);
254 gtk_widget_show (priv->combo_outgoing_auth);
255 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
256 gtk_widget_show (caption);
258 /* The username widgets: */
259 priv->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
260 /* Auto-capitalization is the default, so let's turn it off: */
261 hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_username), HILDON_GTK_INPUT_MODE_FULL);
262 caption = hildon_caption_new (sizegroup, _("mail_fi_username"),
263 priv->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
264 g_signal_connect(G_OBJECT(priv->entry_user_username), "changed", G_CALLBACK(on_change), self);
265 gtk_widget_show (priv->entry_user_username);
266 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
267 gtk_widget_show (caption);
269 /* Prevent the use of some characters in the username,
270 * as required by our UI specification: */
271 modest_validating_entry_set_unallowed_characters_whitespace (
272 MODEST_VALIDATING_ENTRY (priv->entry_user_username));
274 /* Set max length as in the UI spec:
275 * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
276 gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_username), 64);
278 /* The password widgets: */
279 priv->entry_user_password = gtk_entry_new ();
280 /* Auto-capitalization is the default, so let's turn it off: */
281 hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_password),
282 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
283 gtk_entry_set_visibility (GTK_ENTRY (priv->entry_user_password), FALSE);
284 /* gtk_entry_set_invisible_char (GTK_ENTRY (priv->entry_user_password), '*'); */
285 caption = hildon_caption_new (sizegroup,
286 _("mail_fi_password"), priv->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
287 g_signal_connect(G_OBJECT(priv->entry_user_password), "changed", G_CALLBACK(on_change), self);
288 gtk_widget_show (priv->entry_user_password);
289 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
290 gtk_widget_show (caption);
292 /* The secure connection widgets: */
293 if (!priv->combo_outgoing_security)
294 priv->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
295 modest_serversecurity_combo_box_fill (
296 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), MODEST_PROTOCOL_TRANSPORT_SMTP);
297 modest_serversecurity_combo_box_set_active_serversecurity (
298 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), MODEST_PROTOCOL_CONNECTION_NORMAL);
299 caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"),
300 priv->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
301 gtk_widget_show (priv->combo_outgoing_security);
302 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
303 gtk_widget_show (caption);
305 /* The port number widgets: */
306 if (!priv->entry_port)
307 priv->entry_port = GTK_WIDGET (hildon_number_editor_new (1, 65535));
308 caption = hildon_caption_new (sizegroup,
309 _("mcen_fi_emailsetup_port"), priv->entry_port, NULL, HILDON_CAPTION_OPTIONAL);
310 gtk_widget_add_events(GTK_WIDGET(priv->entry_port), GDK_FOCUS_CHANGE_MASK);
311 g_signal_connect(G_OBJECT(priv->entry_port), "range-error", G_CALLBACK(on_range_error), self);
312 g_signal_connect(G_OBJECT(priv->entry_port), "notify::value", G_CALLBACK(on_value_changed), self);
313 gtk_widget_show (priv->entry_port);
314 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
315 gtk_widget_show (caption);
317 /* Show a default port number when the security method changes, as per the UI spec: */
318 g_signal_connect (G_OBJECT (priv->combo_outgoing_security), "changed", (GCallback)on_combo_security_changed, self);
319 on_combo_security_changed (GTK_COMBO_BOX (priv->combo_outgoing_security), self);
321 /* Add the buttons: */
322 gtk_dialog_add_button (GTK_DIALOG (self), _("mcen_bd_dialog_ok"), GTK_RESPONSE_OK);
323 gtk_dialog_add_button (GTK_DIALOG (self), _("mcen_bd_dialog_cancel"), GTK_RESPONSE_CANCEL);
325 priv->is_dirty = FALSE;
326 priv->range_error_occured = FALSE;
327 g_signal_connect(G_OBJECT(self), "response", G_CALLBACK(on_response), self);
328 g_signal_connect(G_OBJECT(box), "set-focus-child", G_CALLBACK(on_set_focus_child), self);
330 gtk_widget_show (box);
333 /* When this window is shown, hibernation should not be possible,
334 * because there is no sensible way to save the state: */
335 modest_window_mgr_prevent_hibernation_while_window_is_shown (
336 modest_runtime_get_window_mgr (), GTK_WINDOW (self));
339 ModestConnectionSpecificSmtpEditWindow*
340 modest_connection_specific_smtp_edit_window_new (void)
342 return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW, NULL);
346 modest_connection_specific_smtp_edit_window_set_connection (
347 ModestConnectionSpecificSmtpEditWindow *window, const gchar* iap_id, const gchar* iap_name,
348 const ModestServerAccountData *data)
350 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
351 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
353 /* This causes a warning because of the %s in the translation, but not in the original string: */
354 gchar* title = g_strdup_printf (_("mcen_ti_connection_connection_name"), iap_name);
355 gtk_window_set_title (GTK_WINDOW (window), title);
360 gtk_entry_set_text (GTK_ENTRY (priv->entry_outgoingserver), data->hostname);
361 gtk_entry_set_text (GTK_ENTRY (priv->entry_user_username), data->username);
362 gtk_entry_set_text (GTK_ENTRY (priv->entry_user_password), data->password);
364 modest_serversecurity_combo_box_set_active_serversecurity (
365 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), data->security);
367 modest_secureauth_combo_box_set_active_secureauth (
368 MODEST_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth), data->secure_auth);
371 hildon_number_editor_set_value (
372 HILDON_NUMBER_EDITOR (priv->entry_port), data->port);
375 /* This will cause changed signals so we set dirty back to FALSE */
376 priv->is_dirty = FALSE;
381 * The result must be freed with modest_account_mgr_free_server_account_data(). */
382 ModestServerAccountData*
383 modest_connection_specific_smtp_edit_window_get_settings (
384 ModestConnectionSpecificSmtpEditWindow *window,
385 ModestAccountMgr *account_manager)
387 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
388 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
390 /* Use g_slice_new0(), because that's what modest_account_mgr_free_server_account_data()
391 * expects us to use. */
392 ModestServerAccountData *result = g_slice_new0 (ModestServerAccountData);
394 result->hostname = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver)));
395 result->username = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_user_username)));
396 result->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_user_password)));
398 result->security = modest_serversecurity_combo_box_get_active_serversecurity (
399 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security));
401 result->secure_auth = modest_secureauth_combo_box_get_active_secureauth (
402 MODEST_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth));
405 result->port = hildon_number_editor_get_value (
406 HILDON_NUMBER_EDITOR (priv->entry_port));
411 gboolean modest_connection_specific_smtp_edit_window_is_dirty(
412 ModestConnectionSpecificSmtpEditWindow *window)
414 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
415 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
417 return priv->is_dirty;