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"
44 #include "modest-maemo-utils.h"
46 #include <glib/gi18n.h>
48 G_DEFINE_TYPE (ModestConnectionSpecificSmtpEditWindow, modest_connection_specific_smtp_edit_window, GTK_TYPE_DIALOG);
50 #define CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE(o) \
51 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW, ModestConnectionSpecificSmtpEditWindowPrivate))
53 typedef struct _ModestConnectionSpecificSmtpEditWindowPrivate ModestConnectionSpecificSmtpEditWindowPrivate;
55 struct _ModestConnectionSpecificSmtpEditWindowPrivate
57 GtkWidget *entry_outgoingserver;
58 GtkWidget *combo_outgoing_auth;
59 GtkWidget *entry_user_username;
60 GtkWidget *entry_user_password;
61 GtkWidget *combo_outgoing_security;
62 GtkWidget *entry_port;
65 GtkWidget *button_cancel;
68 gboolean range_error_occured;
72 modest_connection_specific_smtp_edit_window_get_property (GObject *object, guint property_id,
73 GValue *value, GParamSpec *pspec)
75 switch (property_id) {
77 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
82 modest_connection_specific_smtp_edit_window_set_property (GObject *object, guint property_id,
83 const GValue *value, GParamSpec *pspec)
85 switch (property_id) {
87 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
92 modest_connection_specific_smtp_edit_window_dispose (GObject *object)
94 if (G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->dispose)
95 G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->dispose (object);
99 modest_connection_specific_smtp_edit_window_finalize (GObject *object)
101 G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->finalize (object);
105 modest_connection_specific_smtp_edit_window_class_init (ModestConnectionSpecificSmtpEditWindowClass *klass)
107 GObjectClass *object_class = G_OBJECT_CLASS (klass);
109 g_type_class_add_private (klass, sizeof (ModestConnectionSpecificSmtpEditWindowPrivate));
111 object_class->get_property = modest_connection_specific_smtp_edit_window_get_property;
112 object_class->set_property = modest_connection_specific_smtp_edit_window_set_property;
113 object_class->dispose = modest_connection_specific_smtp_edit_window_dispose;
114 object_class->finalize = modest_connection_specific_smtp_edit_window_finalize;
119 MODEL_COL_SERVER_NAME = 1,
124 on_change(GtkWidget* widget, ModestConnectionSpecificSmtpEditWindow *self)
126 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
127 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
128 priv->is_dirty = TRUE;
132 on_value_changed(GtkWidget* widget, GValue* value, ModestConnectionSpecificSmtpEditWindow *self)
134 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
135 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
137 priv->range_error_occured = FALSE;
138 on_change(widget, self);
142 on_range_error (GtkWidget *widget, HildonNumberEditorErrorType type, gpointer user_data)
144 ModestConnectionSpecificSmtpEditWindow *self = user_data;
145 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
146 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
148 /* We want to prevent the closure of the dialog when a range error occured. The problem is that
149 * the hildon number editor already resets the value to the default value, so we have to
150 * remember that such an error occured. */
151 priv->range_error_occured = TRUE;
153 /* Show error message by not returning TRUE */
158 on_response (GtkDialog *dialog, int response_id, gpointer user_data)
160 const gchar *hostname;
161 ModestConnectionSpecificSmtpEditWindow *self = user_data;
162 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
163 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
165 hostname = gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver));
167 /* Don't close the dialog if a range error occured */
168 if(priv->range_error_occured)
170 priv->range_error_occured = FALSE;
171 g_signal_stop_emission_by_name (dialog, "response");
172 gtk_widget_grab_focus (priv->entry_port);
176 /* Don't close the dialog if a range error occured */
177 if(response_id == GTK_RESPONSE_OK) {
178 if (hostname && (hostname[0] != '\0') &&
179 (!modest_text_utils_validate_domain_name (hostname))) {
180 g_signal_stop_emission_by_name (dialog, "response");
181 hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_servername"));
182 gtk_widget_grab_focus (priv->entry_outgoingserver);
183 gtk_editable_select_region (GTK_EDITABLE (priv->entry_outgoingserver), 0, -1);
190 static void on_set_focus_child (GtkContainer *container, GtkWidget *widget, gpointer user_data)
192 ModestConnectionSpecificSmtpEditWindow *self = user_data;
193 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
194 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
196 /* Another child gained focus. Since the number editor already reset a
197 * possible range error to the default value, we allow closure of the
199 priv->range_error_occured = FALSE;
203 on_combo_security_changed (GtkComboBox *widget, gpointer user_data)
205 ModestConnectionSpecificSmtpEditWindow *self =
206 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (user_data);
207 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
208 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
210 on_change(GTK_WIDGET(widget), self);
212 const gint port_number =
213 modest_serversecurity_combo_box_get_active_serversecurity_port (
214 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security));
216 if(port_number != 0) {
217 hildon_number_editor_set_value (
218 HILDON_NUMBER_EDITOR (priv->entry_port), port_number);
223 modest_connection_specific_smtp_edit_window_init (ModestConnectionSpecificSmtpEditWindow *self)
225 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
226 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
228 GtkWidget *box = GTK_DIALOG(self)->vbox; /* gtk_vbox_new (FALSE, MODEST_MARGIN_HALF); */
229 gtk_box_set_spacing (GTK_BOX (box), MODEST_MARGIN_NONE);
230 gtk_container_set_border_width (GTK_CONTAINER (box), MODEST_MARGIN_HALF);
232 /* Create a size group to be used by all captions.
233 * Note that HildonCaption does not create a default size group if we do not specify one.
234 * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
235 GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
237 /* The outgoing server widgets: */
238 if (!priv->entry_outgoingserver)
239 priv->entry_outgoingserver = gtk_entry_new ();
240 /* Auto-capitalization is the default, so let's turn it off: */
241 hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
242 g_signal_connect(G_OBJECT(priv->entry_outgoingserver), "changed", G_CALLBACK(on_change), self);
244 GtkWidget *caption = hildon_caption_new (sizegroup,
245 _("mcen_li_emailsetup_smtp"), priv->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
246 gtk_widget_show (priv->entry_outgoingserver);
247 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
248 gtk_widget_show (caption);
250 /* The secure authentication widgets: */
251 if (!priv->combo_outgoing_auth)
252 priv->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
253 caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"),
254 priv->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
255 g_signal_connect (G_OBJECT (priv->combo_outgoing_auth), "changed", G_CALLBACK(on_change), self);
256 gtk_widget_show (priv->combo_outgoing_auth);
257 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
258 gtk_widget_show (caption);
260 /* The username widgets: */
261 priv->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
262 /* Auto-capitalization is the default, so let's turn it off: */
263 hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_username), HILDON_GTK_INPUT_MODE_FULL);
264 caption = hildon_caption_new (sizegroup, _("mail_fi_username"),
265 priv->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
266 g_signal_connect(G_OBJECT(priv->entry_user_username), "changed", G_CALLBACK(on_change), self);
267 gtk_widget_show (priv->entry_user_username);
268 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
269 gtk_widget_show (caption);
271 /* Prevent the use of some characters in the username,
272 * as required by our UI specification: */
273 modest_validating_entry_set_unallowed_characters_whitespace (
274 MODEST_VALIDATING_ENTRY (priv->entry_user_username));
276 /* Set max length as in the UI spec:
277 * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
278 gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_username), 64);
280 /* The password widgets: */
281 priv->entry_user_password = gtk_entry_new ();
282 /* Auto-capitalization is the default, so let's turn it off: */
283 hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_password),
284 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
285 gtk_entry_set_visibility (GTK_ENTRY (priv->entry_user_password), FALSE);
286 /* gtk_entry_set_invisible_char (GTK_ENTRY (priv->entry_user_password), '*'); */
287 caption = hildon_caption_new (sizegroup,
288 _("mail_fi_password"), priv->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
289 g_signal_connect(G_OBJECT(priv->entry_user_password), "changed", G_CALLBACK(on_change), self);
290 gtk_widget_show (priv->entry_user_password);
291 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
292 gtk_widget_show (caption);
294 /* The secure connection widgets: */
295 if (!priv->combo_outgoing_security)
296 priv->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
297 modest_serversecurity_combo_box_fill (
298 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), MODEST_PROTOCOL_TRANSPORT_SMTP);
299 modest_serversecurity_combo_box_set_active_serversecurity (
300 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), MODEST_PROTOCOL_CONNECTION_NORMAL);
301 caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"),
302 priv->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
303 gtk_widget_show (priv->combo_outgoing_security);
304 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
305 gtk_widget_show (caption);
307 /* The port number widgets: */
308 if (!priv->entry_port)
309 priv->entry_port = GTK_WIDGET (hildon_number_editor_new (1, 65535));
310 caption = hildon_caption_new (sizegroup,
311 _("mcen_fi_emailsetup_port"), priv->entry_port, NULL, HILDON_CAPTION_OPTIONAL);
312 gtk_widget_add_events(GTK_WIDGET(priv->entry_port), GDK_FOCUS_CHANGE_MASK);
313 g_signal_connect(G_OBJECT(priv->entry_port), "range-error", G_CALLBACK(on_range_error), self);
314 g_signal_connect(G_OBJECT(priv->entry_port), "notify::value", G_CALLBACK(on_value_changed), self);
315 gtk_widget_show (priv->entry_port);
316 gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
317 gtk_widget_show (caption);
319 /* Show a default port number when the security method changes, as per the UI spec: */
320 g_signal_connect (G_OBJECT (priv->combo_outgoing_security), "changed", (GCallback)on_combo_security_changed, self);
321 on_combo_security_changed (GTK_COMBO_BOX (priv->combo_outgoing_security), self);
323 /* Add the buttons: */
324 gtk_dialog_add_button (GTK_DIALOG (self), _("mcen_bd_dialog_ok"), GTK_RESPONSE_OK);
325 gtk_dialog_add_button (GTK_DIALOG (self), _("mcen_bd_dialog_cancel"), GTK_RESPONSE_CANCEL);
327 priv->is_dirty = FALSE;
328 priv->range_error_occured = FALSE;
329 g_signal_connect(G_OBJECT(self), "response", G_CALLBACK(on_response), self);
330 g_signal_connect(G_OBJECT(box), "set-focus-child", G_CALLBACK(on_set_focus_child), self);
332 gtk_widget_show (box);
335 /* When this window is shown, hibernation should not be possible,
336 * because there is no sensible way to save the state: */
337 modest_window_mgr_prevent_hibernation_while_window_is_shown (
338 modest_runtime_get_window_mgr (), GTK_WINDOW (self));
340 hildon_help_dialog_help_enable (GTK_DIALOG(self),
341 "applications_email_connectionspecificsmtpconf",
342 modest_maemo_utils_get_osso_context());
345 ModestConnectionSpecificSmtpEditWindow*
346 modest_connection_specific_smtp_edit_window_new (void)
348 return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW, NULL);
352 modest_connection_specific_smtp_edit_window_set_connection (
353 ModestConnectionSpecificSmtpEditWindow *window, const gchar* iap_id, const gchar* iap_name,
354 const ModestServerAccountData *data)
356 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
357 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
359 /* This causes a warning because of the %s in the translation, but not in the original string: */
360 gchar* title = g_strdup_printf (_("mcen_ti_connection_connection_name"), iap_name);
361 gtk_window_set_title (GTK_WINDOW (window), title);
366 gtk_entry_set_text (GTK_ENTRY (priv->entry_outgoingserver), data->hostname);
367 gtk_entry_set_text (GTK_ENTRY (priv->entry_user_username), data->username);
368 gtk_entry_set_text (GTK_ENTRY (priv->entry_user_password), data->password);
370 modest_serversecurity_combo_box_set_active_serversecurity (
371 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), data->security);
373 modest_secureauth_combo_box_set_active_secureauth (
374 MODEST_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth), data->secure_auth);
377 hildon_number_editor_set_value (
378 HILDON_NUMBER_EDITOR (priv->entry_port), data->port);
381 /* This will cause changed signals so we set dirty back to FALSE */
382 priv->is_dirty = FALSE;
387 * The result must be freed with modest_account_mgr_free_server_account_data(). */
388 ModestServerAccountData*
389 modest_connection_specific_smtp_edit_window_get_settings (
390 ModestConnectionSpecificSmtpEditWindow *window,
391 ModestAccountMgr *account_manager)
393 ModestConnectionSpecificSmtpEditWindowPrivate *priv = NULL;
394 ModestServerAccountData *result = NULL;
395 const gchar *outgoing_server = NULL;
397 priv = CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
398 outgoing_server = gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver));
400 /* If the outgoing server is NULL, we are removing the connection specific
402 if ((outgoing_server == NULL) || (outgoing_server[0] == '\0')) {
406 /* Use g_slice_new0(), because that's what modest_account_mgr_free_server_account_data()
407 * expects us to use. */
408 result = g_slice_new0 (ModestServerAccountData);
410 result->hostname = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver)));
411 result->username = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_user_username)));
412 result->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_user_password)));
414 result->security = modest_serversecurity_combo_box_get_active_serversecurity (
415 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security));
417 result->secure_auth = modest_secureauth_combo_box_get_active_secureauth (
418 MODEST_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth));
421 result->port = hildon_number_editor_get_value (
422 HILDON_NUMBER_EDITOR (priv->entry_port));
427 gboolean modest_connection_specific_smtp_edit_window_is_dirty(
428 ModestConnectionSpecificSmtpEditWindow *window)
430 ModestConnectionSpecificSmtpEditWindowPrivate *priv =
431 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
433 return priv->is_dirty;