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