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