This fixes NB#75543.
[modest] / src / maemo / 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-serversecurity-combo-box.h"
36 #include "widgets/modest-secureauth-combo-box.h"
37 #include "widgets/modest-validating-entry.h"
38 #include <modest-account-mgr-helpers.h>
39 #include <gtk/gtkbutton.h>
40 #include <gtk/gtkhbox.h>
41 #include <gtk/gtkvbox.h>
42 #include <gtk/gtkstock.h>
43 #include "modest-text-utils.h"
44 #include "modest-maemo-utils.h"
45
46 #include <glib/gi18n.h>
47
48 G_DEFINE_TYPE (ModestConnectionSpecificSmtpEditWindow, modest_connection_specific_smtp_edit_window, GTK_TYPE_DIALOG);
49
50 #define CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE(o) \
51         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW, ModestConnectionSpecificSmtpEditWindowPrivate))
52
53 typedef struct _ModestConnectionSpecificSmtpEditWindowPrivate ModestConnectionSpecificSmtpEditWindowPrivate;
54
55 struct _ModestConnectionSpecificSmtpEditWindowPrivate
56 {
57         GtkWidget *entry_outgoingserver;
58         GtkWidget *combo_outgoing_auth;
59         GtkWidget *entry_user_username;
60         GtkWidget *entry_user_password;
61         GtkWidget *combo_outgoing_security;
62         GtkWidget *entry_port;
63         
64         GtkWidget *button_ok;
65         GtkWidget *button_cancel;
66         
67         gboolean is_dirty;
68         gboolean range_error_occured;
69 };
70
71 static void
72 modest_connection_specific_smtp_edit_window_get_property (GObject *object, guint property_id,
73                                                                                                                         GValue *value, GParamSpec *pspec)
74 {
75         switch (property_id) {
76         default:
77                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
78         }
79 }
80
81 static void
82 modest_connection_specific_smtp_edit_window_set_property (GObject *object, guint property_id,
83                                                                                                                         const 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_dispose (GObject *object)
93 {
94         if (G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->dispose)
95                 G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->dispose (object);
96 }
97
98 static void
99 modest_connection_specific_smtp_edit_window_finalize (GObject *object)
100 {
101         G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->finalize (object);
102 }
103
104 static void
105 modest_connection_specific_smtp_edit_window_class_init (ModestConnectionSpecificSmtpEditWindowClass *klass)
106 {
107         GObjectClass *object_class = G_OBJECT_CLASS (klass);
108
109         g_type_class_add_private (klass, sizeof (ModestConnectionSpecificSmtpEditWindowPrivate));
110
111         object_class->get_property = modest_connection_specific_smtp_edit_window_get_property;
112         object_class->set_property = modest_connection_specific_smtp_edit_window_set_property;
113         object_class->dispose = modest_connection_specific_smtp_edit_window_dispose;
114         object_class->finalize = modest_connection_specific_smtp_edit_window_finalize;
115 }
116
117 enum MODEL_COLS {
118         MODEL_COL_NAME = 0,
119         MODEL_COL_SERVER_NAME = 1,
120         MODEL_COL_ID = 2
121 };
122
123 static void
124 on_change(GtkWidget* widget, ModestConnectionSpecificSmtpEditWindow *self)
125 {
126         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
127                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
128         priv->is_dirty = TRUE;
129 }
130
131 static void
132 on_value_changed(GtkWidget* widget, GValue* value, ModestConnectionSpecificSmtpEditWindow *self)
133 {
134         ModestConnectionSpecificSmtpEditWindowPrivate *priv =
135                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
136
137         priv->range_error_occured = FALSE;
138         on_change(widget, self);
139 }
140
141 static gboolean
142 on_range_error (GtkWidget *widget, HildonNumberEditorErrorType type, gpointer user_data)
143 {
144         ModestConnectionSpecificSmtpEditWindow *self = user_data;
145         ModestConnectionSpecificSmtpEditWindowPrivate *priv =
146                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
147
148         /* We want to prevent the closure of the dialog when a range error occured. The problem is that
149          * the hildon number editor already resets the value to the default value, so we have to
150          * remember that such an error occured. */
151         priv->range_error_occured = TRUE;
152
153         /* Show error message by not returning TRUE */
154         return FALSE;
155 }
156
157 static void
158 on_response (GtkDialog *dialog, int response_id, gpointer user_data)
159 {
160         const gchar *hostname;
161         ModestConnectionSpecificSmtpEditWindow *self = user_data;
162         ModestConnectionSpecificSmtpEditWindowPrivate *priv =
163                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
164
165         hostname = gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver));
166
167         /* Don't close the dialog if a range error occured */
168         if(priv->range_error_occured)
169         {
170                 priv->range_error_occured = FALSE;
171                 g_signal_stop_emission_by_name (dialog, "response");
172                 gtk_widget_grab_focus (priv->entry_port);
173                 return;
174         }
175
176         /* Don't close the dialog if a range error occured */
177         if(response_id == GTK_RESPONSE_OK) {
178                 if (hostname && (hostname[0] != '\0') &&
179                     (!modest_text_utils_validate_domain_name (hostname))) { 
180                         g_signal_stop_emission_by_name (dialog, "response");
181                         hildon_banner_show_information (NULL, NULL, _("mcen_ib_invalid_servername"));
182                         gtk_widget_grab_focus (priv->entry_outgoingserver);
183                         gtk_editable_select_region (GTK_EDITABLE (priv->entry_outgoingserver), 0, -1);
184                         return;
185                 }
186         }
187         
188 }
189
190 static void on_set_focus_child (GtkContainer *container, GtkWidget *widget, gpointer user_data)
191 {
192         ModestConnectionSpecificSmtpEditWindow *self = user_data;
193         ModestConnectionSpecificSmtpEditWindowPrivate *priv =
194                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
195
196         /* Another child gained focus. Since the number editor already reset a
197          * possible range error to the default value, we allow closure of the
198          * dialog */
199         priv->range_error_occured = FALSE;
200 }
201
202 static void
203 on_combo_security_changed (GtkComboBox *widget, gpointer user_data)
204 {
205         ModestConnectionSpecificSmtpEditWindow *self = 
206                 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (user_data);
207         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
208                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
209         
210         on_change(GTK_WIDGET(widget), self);
211         
212         const gint port_number = 
213                 modest_serversecurity_combo_box_get_active_serversecurity_port (
214                         MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security));
215
216         if(port_number != 0) {
217                 hildon_number_editor_set_value (
218                         HILDON_NUMBER_EDITOR (priv->entry_port), port_number);
219         }               
220 }
221
222 static void
223 modest_connection_specific_smtp_edit_window_init (ModestConnectionSpecificSmtpEditWindow *self)
224 {
225         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
226                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
227         
228         GtkWidget *box = GTK_DIALOG(self)->vbox; /* gtk_vbox_new (FALSE, MODEST_MARGIN_HALF); */
229         gtk_box_set_spacing (GTK_BOX (box), MODEST_MARGIN_NONE);
230         gtk_container_set_border_width (GTK_CONTAINER (box), MODEST_MARGIN_HALF);
231         
232         /* Create a size group to be used by all captions.
233          * Note that HildonCaption does not create a default size group if we do not specify one.
234          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
235         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
236          
237         /* The outgoing server widgets: */
238         if (!priv->entry_outgoingserver)
239                 priv->entry_outgoingserver = gtk_entry_new ();
240         /* Auto-capitalization is the default, so let's turn it off: */
241         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
242         g_signal_connect(G_OBJECT(priv->entry_outgoingserver), "changed", G_CALLBACK(on_change), self);
243         
244         GtkWidget *caption = hildon_caption_new (sizegroup, 
245                 _("mcen_li_emailsetup_smtp"), priv->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
246         gtk_widget_show (priv->entry_outgoingserver);
247         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
248         gtk_widget_show (caption);
249         
250         /* The secure authentication widgets: */
251         if (!priv->combo_outgoing_auth)
252                 priv->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
253         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
254                 priv->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
255         g_signal_connect (G_OBJECT (priv->combo_outgoing_auth), "changed", G_CALLBACK(on_change), self);
256         gtk_widget_show (priv->combo_outgoing_auth);
257         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
258         gtk_widget_show (caption);
259         
260         /* The username widgets: */     
261         priv->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
262         /* Auto-capitalization is the default, so let's turn it off: */
263         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_username), HILDON_GTK_INPUT_MODE_FULL);
264         caption = hildon_caption_new (sizegroup, _("mail_fi_username"), 
265                 priv->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
266         g_signal_connect(G_OBJECT(priv->entry_user_username), "changed", G_CALLBACK(on_change), self);
267         gtk_widget_show (priv->entry_user_username);
268         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
269         gtk_widget_show (caption);
270         
271         /* Prevent the use of some characters in the username, 
272          * as required by our UI specification: */
273         modest_validating_entry_set_unallowed_characters_whitespace (
274                 MODEST_VALIDATING_ENTRY (priv->entry_user_username));
275         
276         /* Set max length as in the UI spec:
277          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
278         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_username), 64);
279         
280         /* The password widgets: */     
281         priv->entry_user_password = gtk_entry_new ();
282         /* Auto-capitalization is the default, so let's turn it off: */
283         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_password), 
284                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
285         gtk_entry_set_visibility (GTK_ENTRY (priv->entry_user_password), FALSE);
286         /* gtk_entry_set_invisible_char (GTK_ENTRY (priv->entry_user_password), '*'); */
287         caption = hildon_caption_new (sizegroup, 
288                 _("mail_fi_password"), priv->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
289         g_signal_connect(G_OBJECT(priv->entry_user_password), "changed", G_CALLBACK(on_change), self);
290         gtk_widget_show (priv->entry_user_password);
291         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
292         gtk_widget_show (caption);
293         
294         /* The secure connection widgets: */    
295         if (!priv->combo_outgoing_security)
296                 priv->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
297         modest_serversecurity_combo_box_fill (
298                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), MODEST_PROTOCOL_TRANSPORT_SMTP);
299         modest_serversecurity_combo_box_set_active_serversecurity (
300                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), MODEST_PROTOCOL_CONNECTION_NORMAL);
301         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
302                 priv->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
303         gtk_widget_show (priv->combo_outgoing_security);
304         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
305         gtk_widget_show (caption);
306         
307         /* The port number widgets: */
308         if (!priv->entry_port)
309                 priv->entry_port = GTK_WIDGET (hildon_number_editor_new (1, 65535));
310         caption = hildon_caption_new (sizegroup, 
311                 _("mcen_fi_emailsetup_port"), priv->entry_port, NULL, HILDON_CAPTION_OPTIONAL);
312         gtk_widget_add_events(GTK_WIDGET(priv->entry_port), GDK_FOCUS_CHANGE_MASK);
313         g_signal_connect(G_OBJECT(priv->entry_port), "range-error", G_CALLBACK(on_range_error), self);
314         g_signal_connect(G_OBJECT(priv->entry_port), "notify::value", G_CALLBACK(on_value_changed), self);
315         gtk_widget_show (priv->entry_port);
316         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
317         gtk_widget_show (caption);
318         
319         /* Show a default port number when the security method changes, as per the UI spec: */
320         g_signal_connect (G_OBJECT (priv->combo_outgoing_security), "changed", (GCallback)on_combo_security_changed, self);
321         on_combo_security_changed (GTK_COMBO_BOX (priv->combo_outgoing_security), self);
322         
323         /* Add the buttons: */
324         gtk_dialog_add_button (GTK_DIALOG (self), _("mcen_bd_dialog_ok"), GTK_RESPONSE_OK);
325         gtk_dialog_add_button (GTK_DIALOG (self), _("mcen_bd_dialog_cancel"), GTK_RESPONSE_CANCEL);
326         
327         priv->is_dirty = FALSE;
328         priv->range_error_occured = FALSE;
329         g_signal_connect(G_OBJECT(self), "response", G_CALLBACK(on_response), self);
330         g_signal_connect(G_OBJECT(box), "set-focus-child", G_CALLBACK(on_set_focus_child), self);
331         
332         gtk_widget_show (box);
333         
334         
335         /* When this window is shown, hibernation should not be possible, 
336          * because there is no sensible way to save the state: */
337         modest_window_mgr_prevent_hibernation_while_window_is_shown (
338                 modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
339         
340         hildon_help_dialog_help_enable (GTK_DIALOG(self),
341                                         "applications_email_connectionspecificsmtpconf",
342                                         modest_maemo_utils_get_osso_context());
343 }
344
345 ModestConnectionSpecificSmtpEditWindow*
346 modest_connection_specific_smtp_edit_window_new (void)
347 {
348         return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW, NULL);
349 }
350
351 void
352 modest_connection_specific_smtp_edit_window_set_connection (
353         ModestConnectionSpecificSmtpEditWindow *window, const gchar* iap_id, const gchar* iap_name,
354         const ModestServerAccountData *data)
355 {
356         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
357                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
358
359         /* This causes a warning because of the %s in the translation, but not in the original string: */
360         gchar* title = g_strdup_printf (_("mcen_ti_connection_connection_name"), iap_name);
361         gtk_window_set_title (GTK_WINDOW (window), title);
362         g_free (title);
363         
364         if (data) 
365         {
366                 gtk_entry_set_text (GTK_ENTRY (priv->entry_outgoingserver), data->hostname);
367                 gtk_entry_set_text (GTK_ENTRY (priv->entry_user_username), data->username);     
368                 gtk_entry_set_text (GTK_ENTRY (priv->entry_user_password), data->password);
369         
370                 modest_serversecurity_combo_box_set_active_serversecurity (
371                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), data->security);
372         
373                 modest_secureauth_combo_box_set_active_secureauth (
374                 MODEST_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth), data->secure_auth);
375                 
376                 /* port: */
377                 hildon_number_editor_set_value (
378                         HILDON_NUMBER_EDITOR (priv->entry_port), data->port);
379                 
380                 
381                 /* This will cause changed signals so we set dirty back to FALSE */
382                 priv->is_dirty = FALSE;
383         }
384 }
385
386 /*
387  * The result must be freed with modest_account_mgr_free_server_account_data(). */
388 ModestServerAccountData*
389 modest_connection_specific_smtp_edit_window_get_settings (
390         ModestConnectionSpecificSmtpEditWindow *window, 
391         ModestAccountMgr *account_manager)
392 {
393         ModestConnectionSpecificSmtpEditWindowPrivate *priv = NULL;
394         ModestServerAccountData *result = NULL;
395         const gchar *outgoing_server = NULL;
396
397         priv =  CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
398         outgoing_server = gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver));
399
400         /* If the outgoing server is NULL, we are removing the connection specific
401          * settings */
402         if ((outgoing_server == NULL) || (outgoing_server[0] == '\0')) {
403                 return NULL;
404         }
405         
406         /* Use g_slice_new0(), because that's what modest_account_mgr_free_server_account_data() 
407          * expects us to use. */
408         result = g_slice_new0 (ModestServerAccountData);
409         
410         result->hostname = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver)));
411         result->username = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_user_username)));       
412         result->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_user_password)));
413         
414         result->security = modest_serversecurity_combo_box_get_active_serversecurity (
415                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security));
416         
417         result->secure_auth = modest_secureauth_combo_box_get_active_secureauth (
418                 MODEST_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth));
419                 
420         /* port: */
421         result->port = hildon_number_editor_get_value (
422                         HILDON_NUMBER_EDITOR (priv->entry_port));
423                         
424         return result;
425 }
426
427 gboolean modest_connection_specific_smtp_edit_window_is_dirty(
428         ModestConnectionSpecificSmtpEditWindow *window)
429 {
430         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
431                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
432         
433         return priv->is_dirty;
434 }