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