Avoid wait on taking screenshot. Now it shouldn't be required.
[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_mandatory_entry_changed (GtkWidget* widget, ModestConnectionSpecificSmtpEditWindow *self)
154 {
155         const gchar *text;
156         gboolean sensitive = TRUE;
157         ModestConnectionSpecificSmtpEditWindowPrivate *priv;
158         ModestProtocolType auth_proto;
159
160         priv = CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
161
162         /* Check all mandatory entries */
163         on_change (widget, self);
164
165         auth_proto = modest_secureauth_picker_get_active_secureauth (MODEST_SECUREAUTH_PICKER (priv->outgoing_auth_picker));
166         if (modest_protocol_registry_protocol_type_is_secure (modest_runtime_get_protocol_registry (),
167                                                               auth_proto)) {
168                 text = hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_username));
169                 if (!text || (strlen(text) == 0))
170                         sensitive = FALSE;
171         }
172
173         text = hildon_entry_get_text (HILDON_ENTRY (priv->entry_outgoingserver));
174         if (!text || (strlen(text) == 0))
175                 sensitive = FALSE;
176
177         gtk_dialog_set_response_sensitive (GTK_DIALOG (self), GTK_RESPONSE_OK, sensitive);
178 }
179
180
181 static void
182 on_value_changed(GtkWidget* widget, GValue* value, ModestConnectionSpecificSmtpEditWindow *self)
183 {
184         ModestConnectionSpecificSmtpEditWindowPrivate *priv =
185                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
186
187         priv->range_error_occured = FALSE;
188         on_change(widget, self);
189 }
190
191 static gboolean
192 on_range_error (GtkWidget *widget, ModestNumberEditorErrorType type, gpointer user_data)
193 {
194         gchar *msg;
195         ModestConnectionSpecificSmtpEditWindow *self = user_data;
196         ModestConnectionSpecificSmtpEditWindowPrivate *priv =
197                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
198
199         /* We want to prevent the closure of the dialog when a range error occured. The problem is that
200          * the hildon number editor already resets the value to the default value, so we have to
201          * remember that such an error occured. */
202         priv->range_error_occured = TRUE;
203
204         if (type == MODEST_NUMBER_EDITOR_ERROR_MAXIMUM_VALUE_EXCEED) {
205                 msg = g_strdup_printf (dgettext ("hildon-libs", "ckct_ib_maximum_value"), 65535);
206         } else if (type == MODEST_NUMBER_EDITOR_ERROR_MINIMUM_VALUE_EXCEED) {
207                 msg = g_strdup_printf (dgettext ("hildon-libs", "ckct_ib_minimum_value"), 1);
208         } else {
209                 msg = g_strdup_printf (_HL("ckct_ib_set_a_value_within_range"), PORT_RANGE_MIN, PORT_RANGE_MAX);
210         }
211         modest_platform_information_banner (widget, NULL, msg);
212         g_free (msg);
213
214         /* Show error message by not returning TRUE */
215         return TRUE;
216 }
217
218 static gboolean
219 on_delete_event (GtkWidget *widget,
220                  GdkEvent  *event,
221                  gpointer   user_data)
222 {
223         ModestConnectionSpecificSmtpEditWindow *self = user_data;
224         ModestConnectionSpecificSmtpEditWindowPrivate *priv =
225                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
226
227         /* If it's dirty, them the response method already asked the
228            user, because it's always executed before (see GtkDialog
229            code). If it's not then simply close */
230         return priv->is_dirty;
231 }
232
233 static void
234 on_response (GtkDialog *dialog, int response_id, gpointer user_data)
235 {
236         const gchar *hostname;
237         ModestConnectionSpecificSmtpEditWindow *self = user_data;
238         ModestConnectionSpecificSmtpEditWindowPrivate *priv =
239                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
240
241         hostname = hildon_entry_get_text (HILDON_ENTRY (priv->entry_outgoingserver));
242
243         /* Don't close the dialog if a range error occured */
244         if(response_id == GTK_RESPONSE_OK && priv->range_error_occured)
245         {
246                 priv->range_error_occured = FALSE;
247                 g_signal_stop_emission_by_name (dialog, "response");
248                 gtk_widget_grab_focus (priv->entry_port);
249                 return;
250         }
251
252         /* Don't close the dialog if a range error occured */
253         if (response_id == GTK_RESPONSE_OK) {
254                 if (hostname && (hostname[0] != '\0') &&
255                     (!modest_text_utils_validate_domain_name (hostname))) { 
256                         g_signal_stop_emission_by_name (dialog, "response");
257                         hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_servername"));
258                         gtk_widget_grab_focus (priv->entry_outgoingserver);
259                         gtk_editable_select_region (GTK_EDITABLE (priv->entry_outgoingserver), 0, -1);
260                         return;
261                 }
262         } else {
263                 /* Ask user if they want to discard changes */
264                 if (priv->is_dirty) {
265                         gint response;
266                         response = modest_platform_run_confirmation_dialog (GTK_WINDOW (user_data), 
267                                                                             _("imum_nc_wizard_confirm_lose_changes"));
268
269                         if (response != GTK_RESPONSE_OK)
270                                 g_signal_stop_emission_by_name (dialog, "response");
271                 }
272         }
273 }
274
275 static void 
276 on_set_focus_child (GtkContainer *container, GtkWidget *widget, gpointer user_data)
277 {
278         ModestConnectionSpecificSmtpEditWindow *self = user_data;
279         ModestConnectionSpecificSmtpEditWindowPrivate *priv =
280                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
281
282         /* Another child gained focus. Since the number editor already reset a
283          * possible range error to the default value, we allow closure of the
284          * dialog */
285         priv->range_error_occured = FALSE;
286 }
287
288 static void
289 security_picker_set_port (ModestConnectionSpecificSmtpEditWindowPrivate *priv)
290 {
291         const gint port_number = 
292                 modest_serversecurity_picker_get_active_serversecurity_port (
293                         MODEST_SERVERSECURITY_PICKER (priv->outgoing_security_picker));
294
295         if(port_number != 0) {
296                 modest_number_editor_set_value (
297                         MODEST_NUMBER_EDITOR (priv->entry_port), port_number);
298         }
299 }
300
301 static void
302 on_security_picker_changed (HildonPickerButton *widget, gpointer user_data)
303 {
304         ModestConnectionSpecificSmtpEditWindow *self = 
305                 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (user_data);
306         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
307                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
308
309         on_change(GTK_WIDGET(widget), self);
310
311         /* Enable/disable username and password fields */
312         security_picker_set_port (priv);
313 }
314
315 static void
316 auth_picker_set_sensitive (ModestConnectionSpecificSmtpEditWindowPrivate *priv)
317 {
318         ModestProtocolType auth_proto;
319
320         auth_proto =
321                 modest_secureauth_picker_get_active_secureauth (MODEST_SECUREAUTH_PICKER (priv->outgoing_auth_picker));
322
323         if (auth_proto == modest_protocol_registry_get_none_auth_type_id ()) {
324                 gtk_widget_set_sensitive (priv->entry_user_username, FALSE);
325                 gtk_widget_set_sensitive (priv->entry_user_password, FALSE);
326         } else {
327                 gtk_widget_set_sensitive (priv->entry_user_username, TRUE);
328                 gtk_widget_set_sensitive (priv->entry_user_password, TRUE);
329         }
330
331 }
332
333 static void
334 on_auth_picker_changed (HildonPickerButton *widget, gpointer user_data)
335 {
336         ModestConnectionSpecificSmtpEditWindow *self;
337         ModestConnectionSpecificSmtpEditWindowPrivate *priv;
338
339         self = MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (user_data);
340         priv = CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
341
342         on_change (GTK_WIDGET(widget), self);
343
344         /* Enable/disable username and password fields */
345         auth_picker_set_sensitive (priv);
346
347         /* Check missing mandatory data */
348         on_mandatory_entry_changed (priv->entry_user_username, self);
349 }
350
351 static void
352 modest_connection_specific_smtp_edit_window_init (ModestConnectionSpecificSmtpEditWindow *self)
353 {
354         ModestConnectionSpecificSmtpEditWindowPrivate *priv; 
355         GtkWidget *dialog_box;
356         GtkWidget *pannable, *vbox, *align;
357         gchar *server_label;
358
359         /* The title of this dialog is quite long, so make the window wide enough */
360         gtk_widget_set_size_request (GTK_WIDGET (self), 600, 320);
361
362         priv = CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
363         dialog_box = GTK_DIALOG(self)->vbox; /* gtk_vbox_new (FALSE, MODEST_MARGIN_HALF); */
364         gtk_box_set_spacing (GTK_BOX (dialog_box), MODEST_MARGIN_NONE);
365
366         vbox = gtk_vbox_new (FALSE, 0);
367         align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
368         gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, MODEST_MARGIN_DOUBLE, 0);
369         
370         /* Create a size group to be used by all captions.
371          * Note that HildonCaption does not create a default size group if we do not specify one.
372          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
373         GtkSizeGroup *title_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
374         GtkSizeGroup *value_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
375          
376         /* The outgoing server widgets: */
377         if (!priv->entry_outgoingserver)
378                 priv->entry_outgoingserver = hildon_entry_new (MODEST_EDITABLE_SIZE);
379         /* Auto-capitalization is the default, so let's turn it off: */
380         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
381         g_signal_connect(G_OBJECT(priv->entry_outgoingserver), "changed", G_CALLBACK(on_mandatory_entry_changed), self);
382
383         server_label = g_strconcat (_("mcen_li_emailsetup_smtp"), "*\n<small>(SMTP)</small>", NULL);
384         
385         GtkWidget *captioned = 
386           modest_maemo_utils_create_captioned (title_sizegroup, value_sizegroup,
387                                                server_label, TRUE,
388                                                priv->entry_outgoingserver);
389         g_free (server_label);
390         gtk_widget_show (priv->entry_outgoingserver);
391         gtk_box_pack_start (GTK_BOX (vbox), captioned, FALSE, FALSE, 0);
392         gtk_widget_show (captioned);
393         
394         /* The secure authentication widgets: */
395         if (!priv->outgoing_auth_picker) {
396                 priv->outgoing_auth_picker = 
397                         GTK_WIDGET (modest_secureauth_picker_new (MODEST_EDITABLE_SIZE,
398                                                                   HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
399         }
400         modest_maemo_utils_set_hbutton_layout (title_sizegroup, value_sizegroup,
401                                                _("mcen_li_emailsetup_secure_authentication"),
402                                                priv->outgoing_auth_picker);
403         gtk_widget_show (priv->outgoing_auth_picker);
404         gtk_box_pack_start (GTK_BOX (vbox), priv->outgoing_auth_picker, FALSE, FALSE, 0);
405         
406         /* The username widgets: */     
407         priv->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
408         /* Auto-capitalization is the default, so let's turn it off: */
409         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_username), HILDON_GTK_INPUT_MODE_FULL);
410         captioned = modest_maemo_utils_create_captioned (title_sizegroup, value_sizegroup,
411                                                          _("mail_fi_username"), FALSE,
412                                                          priv->entry_user_username);
413         g_signal_connect(G_OBJECT(priv->entry_user_username), "changed", G_CALLBACK(on_mandatory_entry_changed), self);
414         gtk_widget_show (priv->entry_user_username);
415         gtk_box_pack_start (GTK_BOX (vbox), captioned, FALSE, FALSE, 0);
416         gtk_widget_show (captioned);
417         
418         /* Prevent the use of some characters in the username, 
419          * as required by our UI specification: */
420         modest_validating_entry_set_unallowed_characters_whitespace (
421                 MODEST_VALIDATING_ENTRY (priv->entry_user_username));
422         
423         /* Set max length as in the UI spec:
424          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
425         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_username), 64);
426         
427         /* The password widgets: */     
428         priv->entry_user_password = hildon_entry_new (MODEST_EDITABLE_SIZE);
429         /* Auto-capitalization is the default, so let's turn it off: */
430         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_password), 
431                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
432         gtk_entry_set_visibility (GTK_ENTRY (priv->entry_user_password), FALSE);
433         /* gtk_entry_set_invisible_char (GTK_ENTRY (priv->entry_user_password), '*'); */
434         captioned = modest_maemo_utils_create_captioned (title_sizegroup, value_sizegroup,
435                                                          _("mail_fi_password"), FALSE, priv->entry_user_password);
436         g_signal_connect(G_OBJECT(priv->entry_user_password), "changed", G_CALLBACK(on_change), self);
437         gtk_widget_show (priv->entry_user_password);
438         gtk_box_pack_start (GTK_BOX (vbox), captioned, FALSE, FALSE, 0);
439         gtk_widget_show (captioned);
440         
441         /* The secure connection widgets: */    
442         if (!priv->outgoing_security_picker)
443                 priv->outgoing_security_picker = 
444                         GTK_WIDGET (modest_serversecurity_picker_new (MODEST_EDITABLE_SIZE,
445                                                                       HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
446         modest_serversecurity_picker_fill (
447                 MODEST_SERVERSECURITY_PICKER (priv->outgoing_security_picker), MODEST_PROTOCOLS_TRANSPORT_SMTP);
448         modest_serversecurity_picker_set_active_serversecurity (
449                 MODEST_SERVERSECURITY_PICKER (priv->outgoing_security_picker), MODEST_PROTOCOLS_CONNECTION_NONE);
450         modest_maemo_utils_set_hbutton_layout (title_sizegroup, value_sizegroup,
451                                                _("mcen_li_emailsetup_secure_connection"), 
452                                                priv->outgoing_security_picker);
453         gtk_widget_show (priv->outgoing_security_picker);
454         gtk_box_pack_start (GTK_BOX (vbox), priv->outgoing_security_picker, FALSE, FALSE, 0);
455         
456         /* The port number widgets: */
457         if (!priv->entry_port)
458                 priv->entry_port = GTK_WIDGET (modest_number_editor_new (PORT_RANGE_MIN, PORT_RANGE_MAX));
459         captioned = modest_maemo_utils_create_captioned (title_sizegroup, value_sizegroup,
460                                                          _("mcen_fi_emailsetup_port"), FALSE, priv->entry_port);
461         gtk_widget_add_events(GTK_WIDGET(priv->entry_port), GDK_FOCUS_CHANGE_MASK);
462         gtk_widget_show (priv->entry_port);
463         gtk_box_pack_start (GTK_BOX (vbox), captioned, FALSE, FALSE, 0);
464         gtk_widget_show (captioned);
465
466         /* Add the button. Disabled by default */
467         gtk_dialog_add_button (GTK_DIALOG (self), _HL("wdgt_bd_save"), GTK_RESPONSE_OK);
468         gtk_dialog_set_response_sensitive (GTK_DIALOG (self), GTK_RESPONSE_OK, FALSE);
469
470         priv->is_dirty = FALSE;
471         priv->range_error_occured = FALSE;
472         g_signal_connect(G_OBJECT(self), "response", G_CALLBACK(on_response), self);
473         g_signal_connect(G_OBJECT(self), "delete-event", G_CALLBACK(on_delete_event), self);
474         g_signal_connect(G_OBJECT(vbox), "set-focus-child", G_CALLBACK(on_set_focus_child), self);
475
476         priv->account_name = NULL;
477
478         pannable = hildon_pannable_area_new ();
479         g_object_set (G_OBJECT (pannable), "initial-hint", TRUE, NULL);
480         gtk_container_add (GTK_CONTAINER (align), vbox);
481         hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA (pannable), align);
482         gtk_box_pack_start (GTK_BOX (dialog_box), pannable, TRUE, TRUE, 0);
483
484         gtk_widget_show_all (dialog_box);
485         gtk_window_set_default_size (GTK_WINDOW (self), -1, MODEST_DIALOG_WINDOW_MAX_HEIGHT);
486
487         g_object_unref (title_sizegroup);
488         g_object_unref (value_sizegroup);
489
490         /* When this window is shown, hibernation should not be possible, 
491          * because there is no sensible way to save the state: */
492         modest_window_mgr_prevent_hibernation_while_window_is_shown (
493                 modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
494
495         /* Refresh view with current settings */
496         auth_picker_set_sensitive (priv);
497         security_picker_set_port (priv);
498
499         /* Connect signals to track changes */
500         g_signal_connect (G_OBJECT (priv->outgoing_security_picker),
501                           "value-changed",
502                           (GCallback) on_security_picker_changed,
503                           self);
504         g_signal_connect (G_OBJECT (priv->outgoing_auth_picker),
505                           "value-changed",
506                           (GCallback) on_auth_picker_changed,
507                           self);
508         g_signal_connect(G_OBJECT(priv->entry_port),
509                          "range-error",
510                          G_CALLBACK(on_range_error),
511                          self);
512         g_signal_connect(G_OBJECT(priv->entry_port),
513                          "notify::value",
514                          G_CALLBACK(on_value_changed),
515                          self);
516
517 }
518
519 ModestConnectionSpecificSmtpEditWindow*
520 modest_connection_specific_smtp_edit_window_new (void)
521 {
522         return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW, NULL);
523 }
524
525 void
526 modest_connection_specific_smtp_edit_window_set_connection (
527         ModestConnectionSpecificSmtpEditWindow *window, const gchar* iap_id, const gchar* iap_name,
528         ModestServerAccountSettings *server_settings)
529 {
530         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
531                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
532
533         /* This causes a warning because of the %s in the translation, but not in the original string: */
534         gchar* title = g_strdup_printf (_("mcen_ti_connection_connection_name"), iap_name);
535         gtk_window_set_title (GTK_WINDOW (window), title);
536         g_free (title);
537
538         if (server_settings) 
539         {
540                 /* Setting known values */
541                 if (priv->account_name)
542                         g_free (priv->account_name);
543                 priv->account_name = g_strdup (modest_server_account_settings_get_account_name (server_settings));
544                 hildon_entry_set_text (HILDON_ENTRY (priv->entry_outgoingserver), 
545                                        modest_server_account_settings_get_hostname (server_settings));
546                 hildon_entry_set_text (HILDON_ENTRY (priv->entry_user_username),
547                                        modest_server_account_settings_get_username (server_settings));  
548                 hildon_entry_set_text (HILDON_ENTRY (priv->entry_user_password), 
549                                        modest_server_account_settings_get_password (server_settings));
550         
551                 modest_serversecurity_picker_set_active_serversecurity (
552                 MODEST_SERVERSECURITY_PICKER (priv->outgoing_security_picker), 
553                 modest_server_account_settings_get_security_protocol (server_settings));
554         
555                 modest_secureauth_picker_set_active_secureauth (
556                 MODEST_SECUREAUTH_PICKER (priv->outgoing_auth_picker), 
557                 modest_server_account_settings_get_auth_protocol (server_settings));
558                 
559                 /* port: */
560                 modest_number_editor_set_value (
561                         MODEST_NUMBER_EDITOR (priv->entry_port), 
562                         modest_server_account_settings_get_port (server_settings));
563                 
564                 
565                 /* This will cause changed signals so we set dirty back to FALSE */
566                 priv->is_dirty = FALSE;
567         }
568 }
569
570 ModestServerAccountSettings*
571 modest_connection_specific_smtp_edit_window_get_settings (ModestConnectionSpecificSmtpEditWindow *window)
572 {
573         ModestConnectionSpecificSmtpEditWindowPrivate *priv = NULL;
574         ModestServerAccountSettings *server_settings = NULL;
575         const gchar *outgoing_server = NULL;
576
577         priv =  CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
578         outgoing_server = hildon_entry_get_text (HILDON_ENTRY (priv->entry_outgoingserver));
579
580         /* If the outgoing server is NULL, we are removing the connection specific
581          * settings */
582         if ((outgoing_server == NULL) || (outgoing_server[0] == '\0')) {
583                 return NULL;
584         }
585         
586         server_settings = modest_server_account_settings_new ();
587         
588         modest_server_account_settings_set_hostname (server_settings, 
589                                                      hildon_entry_get_text (HILDON_ENTRY (priv->entry_outgoingserver)));
590         modest_server_account_settings_set_protocol (server_settings,
591                                                      MODEST_PROTOCOLS_TRANSPORT_SMTP);
592         modest_server_account_settings_set_username (server_settings,
593                                                      hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_username)));
594         modest_server_account_settings_set_password (server_settings,
595                                                      hildon_entry_get_text (HILDON_ENTRY (priv->entry_user_password)));
596         
597         modest_server_account_settings_set_security_protocol (server_settings, 
598                                                      modest_serversecurity_picker_get_active_serversecurity (
599                                                      MODEST_SERVERSECURITY_PICKER (priv->outgoing_security_picker)));
600         modest_server_account_settings_set_auth_protocol (server_settings,
601                                                           modest_secureauth_picker_get_active_secureauth (
602                                                           MODEST_SECUREAUTH_PICKER (priv->outgoing_auth_picker)));
603         modest_server_account_settings_set_account_name (server_settings,
604                                                          priv->account_name);
605         
606         /* port: */
607         modest_server_account_settings_set_port (server_settings,
608                                                  modest_number_editor_get_value (MODEST_NUMBER_EDITOR (priv->entry_port)));
609                         
610         return server_settings;
611 }
612
613 gboolean 
614 modest_connection_specific_smtp_edit_window_is_dirty(ModestConnectionSpecificSmtpEditWindow *window)
615 {
616         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
617                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
618         
619         return priv->is_dirty;
620 }