Set default height for smtp connection specific dialogs.
[modest] / src / hildon2 / modest-connection-specific-smtp-edit-window.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
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"
34
35 #include "modest-serversecurity-picker.h"
36 #include "modest-secureauth-picker.h"
37 #include "widgets/modest-validating-entry.h"
38 #include <hildon/hildon-pannable-area.h>
39 #include <hildon/hildon-entry.h>
40 #include <modest-ui-constants.h>
41 #include <gtk/gtkbutton.h>
42 #include <gtk/gtkhbox.h>
43 #include <gtk/gtkvbox.h>
44 #include <gtk/gtkstock.h>
45 #include "modest-text-utils.h"
46 #include "modest-maemo-utils.h"
47
48 #include <glib/gi18n.h>
49
50 #define PORT_RANGE_MIN 1
51 #define PORT_RANGE_MAX 65535
52
53 G_DEFINE_TYPE (ModestConnectionSpecificSmtpEditWindow, modest_connection_specific_smtp_edit_window, GTK_TYPE_DIALOG);
54
55 #define CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE(o) \
56         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW, ModestConnectionSpecificSmtpEditWindowPrivate))
57
58 static void on_response (GtkDialog *dialog,
59                          gint arg1,
60                          gpointer user_data);
61
62 typedef struct _ModestConnectionSpecificSmtpEditWindowPrivate ModestConnectionSpecificSmtpEditWindowPrivate;
63
64 struct _ModestConnectionSpecificSmtpEditWindowPrivate
65 {
66         GtkWidget *entry_outgoingserver;
67         GtkWidget *outgoing_auth_picker;
68         GtkWidget *entry_user_username;
69         GtkWidget *entry_user_password;
70         GtkWidget *outgoing_security_picker;
71         GtkWidget *entry_port;
72         
73         GtkWidget *button_ok;
74         GtkWidget *button_cancel;
75
76         gchar     *account_name;
77         
78         gboolean is_dirty;
79         gboolean range_error_occured;
80 };
81
82 static void
83 modest_connection_specific_smtp_edit_window_get_property (GObject *object, guint property_id,
84                                                                                                                         GValue *value, GParamSpec *pspec)
85 {
86         switch (property_id) {
87         default:
88                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
89         }
90 }
91
92 static void
93 modest_connection_specific_smtp_edit_window_set_property (GObject *object, guint property_id,
94                                                                                                                         const GValue *value, GParamSpec *pspec)
95 {
96         switch (property_id) {
97         default:
98                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
99         }
100 }
101
102 static void
103 modest_connection_specific_smtp_edit_window_dispose (GObject *object)
104 {
105
106         
107         if (G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->dispose)
108                 G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->dispose (object);
109 }
110
111 static void
112 modest_connection_specific_smtp_edit_window_finalize (GObject *object)
113 {
114         ModestConnectionSpecificSmtpEditWindow *self = (ModestConnectionSpecificSmtpEditWindow *) object;
115         ModestConnectionSpecificSmtpEditWindowPrivate *priv =
116                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
117
118         if (priv->account_name) {
119                 g_free (priv->account_name);
120                 priv->account_name = NULL;
121         }
122         G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->finalize (object);
123 }
124
125 static void
126 modest_connection_specific_smtp_edit_window_class_init (ModestConnectionSpecificSmtpEditWindowClass *klass)
127 {
128         GObjectClass *object_class = G_OBJECT_CLASS (klass);
129
130         g_type_class_add_private (klass, sizeof (ModestConnectionSpecificSmtpEditWindowPrivate));
131
132         object_class->get_property = modest_connection_specific_smtp_edit_window_get_property;
133         object_class->set_property = modest_connection_specific_smtp_edit_window_set_property;
134         object_class->dispose = modest_connection_specific_smtp_edit_window_dispose;
135         object_class->finalize = modest_connection_specific_smtp_edit_window_finalize;
136 }
137
138 enum MODEL_COLS {
139         MODEL_COL_NAME = 0,
140         MODEL_COL_SERVER_NAME = 1,
141         MODEL_COL_ID = 2
142 };
143
144 static void
145 on_change(GtkWidget* widget, ModestConnectionSpecificSmtpEditWindow *self)
146 {
147         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
148                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
149         priv->is_dirty = TRUE;
150 }
151
152 static void
153 on_value_changed(GtkWidget* widget, GValue* value, ModestConnectionSpecificSmtpEditWindow *self)
154 {
155         ModestConnectionSpecificSmtpEditWindowPrivate *priv =
156                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
157
158         priv->range_error_occured = FALSE;
159         on_change(widget, self);
160 }
161
162 static gboolean
163 on_range_error (GtkWidget *widget, ModestNumberEditorErrorType type, gpointer user_data)
164 {
165         gchar *msg;
166         ModestConnectionSpecificSmtpEditWindow *self = user_data;
167         ModestConnectionSpecificSmtpEditWindowPrivate *priv =
168                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
169
170         /* We want to prevent the closure of the dialog when a range error occured. The problem is that
171          * the hildon number editor already resets the value to the default value, so we have to
172          * remember that such an error occured. */
173         priv->range_error_occured = TRUE;
174
175         if (type == MODEST_NUMBER_EDITOR_ERROR_MAXIMUM_VALUE_EXCEED) {
176                 msg = g_strdup_printf (dgettext ("hildon-libs", "ckct_ib_maximum_value"), 65535);
177         } else if (type == MODEST_NUMBER_EDITOR_ERROR_MINIMUM_VALUE_EXCEED) {
178                 msg = g_strdup_printf (dgettext ("hildon-libs", "ckct_ib_minimum_value"), 1);
179         } else {
180                 msg = g_strdup_printf (_HL("ckct_ib_set_a_value_within_range"), PORT_RANGE_MIN, PORT_RANGE_MAX);
181         }
182         modest_platform_information_banner (widget, NULL, msg);
183         g_free (msg);
184
185         /* Show error message by not returning TRUE */
186         return TRUE;
187 }
188
189 static gboolean
190 on_delete_event (GtkWidget *widget,
191                  GdkEvent  *event,
192                  gpointer   user_data)
193 {
194         ModestConnectionSpecificSmtpEditWindow *self = user_data;
195         ModestConnectionSpecificSmtpEditWindowPrivate *priv =
196                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
197
198         /* If it's dirty, them the response method already asked the
199            user, because it's always executed before (see GtkDialog
200            code). If it's not then simply close */
201         return priv->is_dirty;
202 }
203
204 static void
205 on_response (GtkDialog *dialog, int response_id, gpointer user_data)
206 {
207         const gchar *hostname;
208         ModestConnectionSpecificSmtpEditWindow *self = user_data;
209         ModestConnectionSpecificSmtpEditWindowPrivate *priv =
210                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
211
212         hostname = hildon_entry_get_text (HILDON_ENTRY (priv->entry_outgoingserver));
213
214         /* Don't close the dialog if a range error occured */
215         if(response_id == GTK_RESPONSE_OK && priv->range_error_occured)
216         {
217                 priv->range_error_occured = FALSE;
218                 g_signal_stop_emission_by_name (dialog, "response");
219                 gtk_widget_grab_focus (priv->entry_port);
220                 return;
221         }
222
223         /* Don't close the dialog if a range error occured */
224         if (response_id == GTK_RESPONSE_OK) {
225                 if (hostname && (hostname[0] != '\0') &&
226                     (!modest_text_utils_validate_domain_name (hostname))) { 
227                         g_signal_stop_emission_by_name (dialog, "response");
228                         hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_servername"));
229                         gtk_widget_grab_focus (priv->entry_outgoingserver);
230                         gtk_editable_select_region (GTK_EDITABLE (priv->entry_outgoingserver), 0, -1);
231                         return;
232                 }
233         } else {
234                 /* Ask user if they want to discard changes */
235                 if (priv->is_dirty) {
236                         gint response;
237                         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (user_data), 
238                                                                             _("imum_nc_wizard_confirm_lose_changes"));
239
240                         if (response != GTK_RESPONSE_OK)
241                                 g_signal_stop_emission_by_name (dialog, "response");
242                 }
243         }
244 }
245
246 static void 
247 on_set_focus_child (GtkContainer *container, GtkWidget *widget, gpointer user_data)
248 {
249         ModestConnectionSpecificSmtpEditWindow *self = user_data;
250         ModestConnectionSpecificSmtpEditWindowPrivate *priv =
251                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
252
253         /* Another child gained focus. Since the number editor already reset a
254          * possible range error to the default value, we allow closure of the
255          * dialog */
256         priv->range_error_occured = FALSE;
257 }
258
259 static void
260 security_picker_set_port (ModestConnectionSpecificSmtpEditWindowPrivate *priv)
261 {
262         const gint port_number = 
263                 modest_serversecurity_picker_get_active_serversecurity_port (
264                         MODEST_SERVERSECURITY_PICKER (priv->outgoing_security_picker));
265
266         if(port_number != 0) {
267                 modest_number_editor_set_value (
268                         MODEST_NUMBER_EDITOR (priv->entry_port), port_number);
269         }
270 }
271
272 static void
273 on_security_picker_changed (HildonPickerButton *widget, gpointer user_data)
274 {
275         ModestConnectionSpecificSmtpEditWindow *self = 
276                 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (user_data);
277         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
278                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
279
280         on_change(GTK_WIDGET(widget), self);
281
282         /* Enable/disable username and password fields */
283         security_picker_set_port (priv);
284 }
285
286 static void
287 auth_picker_set_sensitive (ModestConnectionSpecificSmtpEditWindowPrivate *priv)
288 {
289         ModestProtocolType auth_proto;
290
291         auth_proto =
292                 modest_secureauth_picker_get_active_secureauth (MODEST_SECUREAUTH_PICKER (priv->outgoing_auth_picker));
293
294         if (auth_proto == modest_protocol_registry_get_none_auth_type_id ()) {
295                 gtk_widget_set_sensitive (priv->entry_user_username, FALSE);
296                 gtk_widget_set_sensitive (priv->entry_user_password, FALSE);
297         } else {
298                 gtk_widget_set_sensitive (priv->entry_user_username, TRUE);
299                 gtk_widget_set_sensitive (priv->entry_user_password, TRUE);
300         }
301
302 }
303
304 static void
305 on_auth_picker_changed (HildonPickerButton *widget, gpointer user_data)
306 {
307         ModestConnectionSpecificSmtpEditWindow *self =
308                 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (user_data);
309         ModestConnectionSpecificSmtpEditWindowPrivate *priv =
310                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
311
312         on_change (GTK_WIDGET(widget), self);
313
314         /* Enable/disable username and password fields */
315         auth_picker_set_sensitive (priv);
316 }
317
318 static void
319 modest_connection_specific_smtp_edit_window_init (ModestConnectionSpecificSmtpEditWindow *self)
320 {
321         ModestConnectionSpecificSmtpEditWindowPrivate *priv; 
322         GtkWidget *dialog_box;
323         GtkWidget *pannable, *vbox;
324         gchar *server_label;
325
326         /* The title of this dialog is quite long, so make the window wide enough */
327         gtk_widget_set_size_request (GTK_WIDGET (self), 600, 320);
328
329         priv = CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
330         dialog_box = GTK_DIALOG(self)->vbox; /* gtk_vbox_new (FALSE, MODEST_MARGIN_HALF); */
331         gtk_box_set_spacing (GTK_BOX (dialog_box), MODEST_MARGIN_NONE);
332         gtk_container_set_border_width (GTK_CONTAINER (dialog_box), MODEST_MARGIN_HALF);
333
334         vbox = gtk_vbox_new (FALSE, 0);
335         
336         /* Create a size group to be used by all captions.
337          * Note that HildonCaption does not create a default size group if we do not specify one.
338          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
339         GtkSizeGroup *title_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
340         GtkSizeGroup *value_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
341          
342         /* The outgoing server widgets: */
343         if (!priv->entry_outgoingserver)
344                 priv->entry_outgoingserver = hildon_entry_new (MODEST_EDITABLE_SIZE);
345         /* Auto-capitalization is the default, so let's turn it off: */
346         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
347         g_signal_connect(G_OBJECT(priv->entry_outgoingserver), "changed", G_CALLBACK(on_change), self);
348
349         server_label = g_strconcat (_("mcen_li_emailsetup_smtp"), "\n<small>(SMTP)</small>", NULL);
350         
351         GtkWidget *captioned = 
352           modest_maemo_utils_create_captioned (title_sizegroup, value_sizegroup,
353                                                server_label, TRUE,
354                                                priv->entry_outgoingserver);
355         g_free (server_label);
356         gtk_widget_show (priv->entry_outgoingserver);
357         gtk_box_pack_start (GTK_BOX (vbox), captioned, FALSE, FALSE, MODEST_MARGIN_HALF);
358         gtk_widget_show (captioned);
359         
360         /* The secure authentication widgets: */
361         if (!priv->outgoing_auth_picker) {
362                 priv->outgoing_auth_picker = 
363                         GTK_WIDGET (modest_secureauth_picker_new (MODEST_EDITABLE_SIZE,
364                                                                   HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
365         }
366         modest_maemo_utils_set_hbutton_layout (title_sizegroup, value_sizegroup,
367                                                _("mcen_li_emailsetup_secure_authentication"),
368                                                priv->outgoing_auth_picker);
369         gtk_widget_show (priv->outgoing_auth_picker);
370         gtk_box_pack_start (GTK_BOX (vbox), priv->outgoing_auth_picker, FALSE, FALSE, MODEST_MARGIN_HALF);
371         
372         /* The username widgets: */     
373         priv->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
374         /* Auto-capitalization is the default, so let's turn it off: */
375         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_username), HILDON_GTK_INPUT_MODE_FULL);
376         captioned = modest_maemo_utils_create_captioned (title_sizegroup, value_sizegroup,
377                                                          _("mail_fi_username"), FALSE,
378                                                          priv->entry_user_username);
379         g_signal_connect(G_OBJECT(priv->entry_user_username), "changed", G_CALLBACK(on_change), self);
380         gtk_widget_show (priv->entry_user_username);
381         gtk_box_pack_start (GTK_BOX (vbox), captioned, FALSE, FALSE, MODEST_MARGIN_HALF);
382         gtk_widget_show (captioned);
383         
384         /* Prevent the use of some characters in the username, 
385          * as required by our UI specification: */
386         modest_validating_entry_set_unallowed_characters_whitespace (
387                 MODEST_VALIDATING_ENTRY (priv->entry_user_username));
388         
389         /* Set max length as in the UI spec:
390          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
391         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_username), 64);
392         
393         /* The password widgets: */     
394         priv->entry_user_password = hildon_entry_new (MODEST_EDITABLE_SIZE);
395         /* Auto-capitalization is the default, so let's turn it off: */
396         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_password), 
397                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
398         gtk_entry_set_visibility (GTK_ENTRY (priv->entry_user_password), FALSE);
399         /* gtk_entry_set_invisible_char (GTK_ENTRY (priv->entry_user_password), '*'); */
400         captioned = modest_maemo_utils_create_captioned (title_sizegroup, value_sizegroup,
401                                                          _("mail_fi_password"), FALSE, priv->entry_user_password);
402         g_signal_connect(G_OBJECT(priv->entry_user_password), "changed", G_CALLBACK(on_change), self);
403         gtk_widget_show (priv->entry_user_password);
404         gtk_box_pack_start (GTK_BOX (vbox), captioned, FALSE, FALSE, MODEST_MARGIN_HALF);
405         gtk_widget_show (captioned);
406         
407         /* The secure connection widgets: */    
408         if (!priv->outgoing_security_picker)
409                 priv->outgoing_security_picker = 
410                         GTK_WIDGET (modest_serversecurity_picker_new (MODEST_EDITABLE_SIZE,
411                                                                       HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
412         modest_serversecurity_picker_fill (
413                 MODEST_SERVERSECURITY_PICKER (priv->outgoing_security_picker), MODEST_PROTOCOLS_TRANSPORT_SMTP);
414         modest_serversecurity_picker_set_active_serversecurity (
415                 MODEST_SERVERSECURITY_PICKER (priv->outgoing_security_picker), MODEST_PROTOCOLS_CONNECTION_NONE);
416         modest_maemo_utils_set_hbutton_layout (title_sizegroup, value_sizegroup,
417                                                _("mcen_li_emailsetup_secure_connection"), 
418                                                priv->outgoing_security_picker);
419         gtk_widget_show (priv->outgoing_security_picker);
420         gtk_box_pack_start (GTK_BOX (vbox), priv->outgoing_security_picker, FALSE, FALSE, MODEST_MARGIN_HALF);
421         
422         /* The port number widgets: */
423         if (!priv->entry_port)
424                 priv->entry_port = GTK_WIDGET (modest_number_editor_new (PORT_RANGE_MIN, PORT_RANGE_MAX));
425         captioned = modest_maemo_utils_create_captioned (title_sizegroup, value_sizegroup,
426                                                          _("mcen_fi_emailsetup_port"), FALSE, priv->entry_port);
427         gtk_widget_add_events(GTK_WIDGET(priv->entry_port), GDK_FOCUS_CHANGE_MASK);
428         gtk_widget_show (priv->entry_port);
429         gtk_box_pack_start (GTK_BOX (vbox), captioned, FALSE, FALSE, MODEST_MARGIN_HALF);
430         gtk_widget_show (captioned);
431
432         /* Add the buttons: */
433         gtk_dialog_add_button (GTK_DIALOG (self), _HL("wdgt_bd_save"), GTK_RESPONSE_OK);
434         
435         priv->is_dirty = FALSE;
436         priv->range_error_occured = FALSE;
437         g_signal_connect(G_OBJECT(self), "response", G_CALLBACK(on_response), self);
438         g_signal_connect(G_OBJECT(self), "delete-event", G_CALLBACK(on_delete_event), self);
439         g_signal_connect(G_OBJECT(vbox), "set-focus-child", G_CALLBACK(on_set_focus_child), self);
440
441         priv->account_name = NULL;
442
443         pannable = hildon_pannable_area_new ();
444         g_object_set (G_OBJECT (pannable), "initial-hint", TRUE, NULL);
445         hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA (pannable), vbox);
446         gtk_box_pack_start (GTK_BOX (dialog_box), pannable, TRUE, TRUE, 0);
447
448         gtk_widget_show_all (dialog_box);
449         gtk_window_set_default_size (GTK_WINDOW (self), -1, MODEST_DIALOG_WINDOW_MAX_HEIGHT);
450
451         g_object_unref (title_sizegroup);
452         g_object_unref (value_sizegroup);
453
454         /* When this window is shown, hibernation should not be possible, 
455          * because there is no sensible way to save the state: */
456         modest_window_mgr_prevent_hibernation_while_window_is_shown (
457                 modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
458
459         hildon_help_dialog_help_enable (GTK_DIALOG(self),
460                                         "applications_email_connectionspecificsmtpconf",
461                                         modest_maemo_utils_get_osso_context());
462
463         /* Refresh view with current settings */
464         auth_picker_set_sensitive (priv);
465         security_picker_set_port (priv);
466
467         /* Connect signals to track changes */
468         g_signal_connect (G_OBJECT (priv->outgoing_security_picker),
469                           "value-changed",
470                           (GCallback) on_security_picker_changed,
471                           self);
472         g_signal_connect (G_OBJECT (priv->outgoing_auth_picker),
473                           "value-changed",
474                           (GCallback) on_auth_picker_changed,
475                           self);
476         g_signal_connect(G_OBJECT(priv->entry_port),
477                          "range-error",
478                          G_CALLBACK(on_range_error),
479                          self);
480         g_signal_connect(G_OBJECT(priv->entry_port),
481                          "notify::value",
482                          G_CALLBACK(on_value_changed),
483                          self);
484
485 }
486
487 ModestConnectionSpecificSmtpEditWindow*
488 modest_connection_specific_smtp_edit_window_new (void)
489 {
490         return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW, NULL);
491 }
492
493 void
494 modest_connection_specific_smtp_edit_window_set_connection (
495         ModestConnectionSpecificSmtpEditWindow *window, const gchar* iap_id, const gchar* iap_name,
496         ModestServerAccountSettings *server_settings)
497 {
498         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
499                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
500
501         /* This causes a warning because of the %s in the translation, but not in the original string: */
502         gchar* title = g_strdup_printf (_("mcen_ti_connection_connection_name"), iap_name);
503         gtk_window_set_title (GTK_WINDOW (window), title);
504         g_free (title);
505
506         if (server_settings) 
507         {
508                 /* Setting known values */
509                 if (priv->account_name)
510                         g_free (priv->account_name);
511                 priv->account_name = g_strdup (modest_server_account_settings_get_account_name (server_settings));
512                 hildon_entry_set_text (HILDON_ENTRY (priv->entry_outgoingserver), 
513                                        modest_server_account_settings_get_hostname (server_settings));
514                 hildon_entry_set_text (HILDON_ENTRY (priv->entry_user_username),
515                                        modest_server_account_settings_get_username (server_settings));  
516                 hildon_entry_set_text (HILDON_ENTRY (priv->entry_user_password), 
517                                        modest_server_account_settings_get_password (server_settings));
518         
519                 modest_serversecurity_picker_set_active_serversecurity (
520                 MODEST_SERVERSECURITY_PICKER (priv->outgoing_security_picker), 
521                 modest_server_account_settings_get_security_protocol (server_settings));
522         
523                 modest_secureauth_picker_set_active_secureauth (
524                 MODEST_SECUREAUTH_PICKER (priv->outgoing_auth_picker), 
525                 modest_server_account_settings_get_auth_protocol (server_settings));
526                 
527                 /* port: */
528                 modest_number_editor_set_value (
529                         MODEST_NUMBER_EDITOR (priv->entry_port), 
530                         modest_server_account_settings_get_port (server_settings));
531                 
532                 
533                 /* This will cause changed signals so we set dirty back to FALSE */
534                 priv->is_dirty = FALSE;
535         }
536 }
537
538 ModestServerAccountSettings*
539 modest_connection_specific_smtp_edit_window_get_settings (ModestConnectionSpecificSmtpEditWindow *window)
540 {
541         ModestConnectionSpecificSmtpEditWindowPrivate *priv = NULL;
542         ModestServerAccountSettings *server_settings = NULL;
543         const gchar *outgoing_server = NULL;
544
545         priv =  CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
546         outgoing_server = hildon_entry_get_text (HILDON_ENTRY (priv->entry_outgoingserver));
547
548         /* If the outgoing server is NULL, we are removing the connection specific
549          * settings */
550         if ((outgoing_server == NULL) || (outgoing_server[0] == '\0')) {
551                 return NULL;
552         }
553         
554         server_settings = modest_server_account_settings_new ();
555         
556         modest_server_account_settings_set_hostname (server_settings, 
557                                                      hildon_entry_get_text (HILDON_ENTRY (priv->entry_outgoingserver)));
558         modest_server_account_settings_set_protocol (server_settings,
559                                                      MODEST_PROTOCOLS_TRANSPORT_SMTP);
560         modest_server_account_settings_set_username (server_settings,
561                                                      hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_username)));
562         modest_server_account_settings_set_password (server_settings,
563                                                      hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_password)));
564         
565         modest_server_account_settings_set_security_protocol (server_settings, 
566                                                      modest_serversecurity_picker_get_active_serversecurity (
567                                                      MODEST_SERVERSECURITY_PICKER (priv->outgoing_security_picker)));
568         modest_server_account_settings_set_auth_protocol (server_settings,
569                                                           modest_secureauth_picker_get_active_secureauth (
570                                                           MODEST_SECUREAUTH_PICKER (priv->outgoing_auth_picker)));
571         modest_server_account_settings_set_account_name (server_settings,
572                                                          priv->account_name);
573         
574         /* port: */
575         modest_server_account_settings_set_port (server_settings,
576                                                  modest_number_editor_get_value (MODEST_NUMBER_EDITOR (priv->entry_port)));
577                         
578         return server_settings;
579 }
580
581 gboolean 
582 modest_connection_specific_smtp_edit_window_is_dirty(ModestConnectionSpecificSmtpEditWindow *window)
583 {
584         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
585                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
586         
587         return priv->is_dirty;
588 }