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