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