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