ae1a07dc49915b7afd8588e89dea09946bcaee6f
[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 };
67
68 static void
69 modest_connection_specific_smtp_edit_window_get_property (GObject *object, guint property_id,
70                                                                                                                         GValue *value, GParamSpec *pspec)
71 {
72         switch (property_id) {
73         default:
74                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
75         }
76 }
77
78 static void
79 modest_connection_specific_smtp_edit_window_set_property (GObject *object, guint property_id,
80                                                                                                                         const GValue *value, GParamSpec *pspec)
81 {
82         switch (property_id) {
83         default:
84                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
85         }
86 }
87
88 static void
89 modest_connection_specific_smtp_edit_window_dispose (GObject *object)
90 {
91         if (G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->dispose)
92                 G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->dispose (object);
93 }
94
95 static void
96 modest_connection_specific_smtp_edit_window_finalize (GObject *object)
97 {
98         G_OBJECT_CLASS (modest_connection_specific_smtp_edit_window_parent_class)->finalize (object);
99 }
100
101 static void
102 modest_connection_specific_smtp_edit_window_class_init (ModestConnectionSpecificSmtpEditWindowClass *klass)
103 {
104         GObjectClass *object_class = G_OBJECT_CLASS (klass);
105
106         g_type_class_add_private (klass, sizeof (ModestConnectionSpecificSmtpEditWindowPrivate));
107
108         object_class->get_property = modest_connection_specific_smtp_edit_window_get_property;
109         object_class->set_property = modest_connection_specific_smtp_edit_window_set_property;
110         object_class->dispose = modest_connection_specific_smtp_edit_window_dispose;
111         object_class->finalize = modest_connection_specific_smtp_edit_window_finalize;
112 }
113
114 enum MODEL_COLS {
115         MODEL_COL_NAME = 0,
116         MODEL_COL_SERVER_NAME = 1,
117         MODEL_COL_ID = 2
118 };
119
120 static void
121 on_change(GtkWidget* widget, ModestConnectionSpecificSmtpEditWindow *self)
122 {
123         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
124                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
125         priv->is_dirty = TRUE;
126 }
127
128 static void
129 on_combo_security_changed (GtkComboBox *widget, gpointer user_data)
130 {
131         ModestConnectionSpecificSmtpEditWindow *self = 
132                 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (user_data);
133         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
134                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
135         
136         on_change(GTK_WIDGET(widget), self);
137         
138         const gint port_number = 
139                 modest_serversecurity_combo_box_get_active_serversecurity_port (
140                         MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security));
141
142         if(port_number != 0) {
143                 hildon_number_editor_set_value (
144                         HILDON_NUMBER_EDITOR (priv->entry_port), port_number);
145         }               
146 }
147
148 static void
149 modest_connection_specific_smtp_edit_window_init (ModestConnectionSpecificSmtpEditWindow *self)
150 {
151         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
152                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
153         
154         GtkWidget *box = GTK_DIALOG(self)->vbox; /* gtk_vbox_new (FALSE, MODEST_MARGIN_HALF); */
155         gtk_box_set_spacing (GTK_BOX (box), MODEST_MARGIN_NONE);
156         gtk_container_set_border_width (GTK_CONTAINER (box), MODEST_MARGIN_HALF);
157         
158         /* Create a size group to be used by all captions.
159          * Note that HildonCaption does not create a default size group if we do not specify one.
160          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
161         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
162          
163         /* The outgoing server widgets: */
164         if (!priv->entry_outgoingserver)
165                 priv->entry_outgoingserver = gtk_entry_new ();
166         /* Auto-capitalization is the default, so let's turn it off: */
167         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
168         g_signal_connect(G_OBJECT(priv->entry_outgoingserver), "changed", G_CALLBACK(on_change), self);
169         
170         GtkWidget *caption = hildon_caption_new (sizegroup, 
171                 _("mcen_li_emailsetup_smtp"), priv->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
172         gtk_widget_show (priv->entry_outgoingserver);
173         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
174         gtk_widget_show (caption);
175         
176         /* The secure authentication widgets: */
177         if (!priv->combo_outgoing_auth)
178                 priv->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
179         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
180                 priv->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
181         g_signal_connect (G_OBJECT (priv->combo_outgoing_auth), "changed", G_CALLBACK(on_change), self);
182         gtk_widget_show (priv->combo_outgoing_auth);
183         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
184         gtk_widget_show (caption);
185         
186         /* The username widgets: */     
187         priv->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
188         /* Auto-capitalization is the default, so let's turn it off: */
189         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_username), HILDON_GTK_INPUT_MODE_FULL);
190         caption = hildon_caption_new (sizegroup, _("mail_fi_username"), 
191                 priv->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
192         g_signal_connect(G_OBJECT(priv->entry_user_username), "changed", G_CALLBACK(on_change), self);
193         gtk_widget_show (priv->entry_user_username);
194         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
195         gtk_widget_show (caption);
196         
197         /* Prevent the use of some characters in the username, 
198          * as required by our UI specification: */
199         modest_validating_entry_set_unallowed_characters_whitespace (
200                 MODEST_VALIDATING_ENTRY (priv->entry_user_username));
201         
202         /* Set max length as in the UI spec:
203          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
204         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_username), 64);
205         
206         /* The password widgets: */     
207         priv->entry_user_password = gtk_entry_new ();
208         /* Auto-capitalization is the default, so let's turn it off: */
209         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_password), 
210                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
211         gtk_entry_set_visibility (GTK_ENTRY (priv->entry_user_password), FALSE);
212         /* gtk_entry_set_invisible_char (GTK_ENTRY (priv->entry_user_password), '*'); */
213         caption = hildon_caption_new (sizegroup, 
214                 _("mail_fi_password"), priv->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
215         g_signal_connect(G_OBJECT(priv->entry_user_password), "changed", G_CALLBACK(on_change), self);
216         gtk_widget_show (priv->entry_user_password);
217         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
218         gtk_widget_show (caption);
219         
220         /* The secure connection widgets: */    
221         if (!priv->combo_outgoing_security)
222                 priv->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
223         modest_serversecurity_combo_box_fill (
224                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), MODEST_PROTOCOL_TRANSPORT_SMTP);
225         modest_serversecurity_combo_box_set_active_serversecurity (
226                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), MODEST_PROTOCOL_CONNECTION_NORMAL);
227         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
228                 priv->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
229         gtk_widget_show (priv->combo_outgoing_security);
230         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
231         gtk_widget_show (caption);
232         
233         /* The port number widgets: */
234         if (!priv->entry_port)
235                 priv->entry_port = GTK_WIDGET (hildon_number_editor_new (0, 65535));
236         caption = hildon_caption_new (sizegroup, 
237                 _("mcen_li_emailsetup_smtp"), priv->entry_port, NULL, HILDON_CAPTION_OPTIONAL);
238         /* FIXME: There is no changed signal for hildon_number_editor */
239         gtk_widget_show (priv->entry_port);
240         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
241         gtk_widget_show (caption);
242         
243         /* Show a default port number when the security method changes, as per the UI spec: */
244         g_signal_connect (G_OBJECT (priv->combo_outgoing_security), "changed", (GCallback)on_combo_security_changed, self);
245         on_combo_security_changed (GTK_COMBO_BOX (priv->combo_outgoing_security), self);
246         
247         /* Add the buttons: */
248         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_OK, GTK_RESPONSE_OK);
249         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
250         
251         priv->is_dirty = FALSE;
252         
253         gtk_widget_show (box);
254         
255         
256         /* When this window is shown, hibernation should not be possible, 
257          * because there is no sensible way to save the state: */
258     modest_window_mgr_prevent_hibernation_while_window_is_shown (
259         modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
260 }
261
262 ModestConnectionSpecificSmtpEditWindow*
263 modest_connection_specific_smtp_edit_window_new (void)
264 {
265         return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW, NULL);
266 }
267
268 void
269 modest_connection_specific_smtp_edit_window_set_connection (
270         ModestConnectionSpecificSmtpEditWindow *window, const gchar* iap_id, const gchar* iap_name,
271         const ModestServerAccountData *data)
272 {
273         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
274                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
275
276         /* This causes a warning because of the %s in the translation, but not in the original string: */
277         gchar* title = g_strdup_printf (_("mcen_ti_connection_connection_name"), iap_name);
278         gtk_window_set_title (GTK_WINDOW (window), title);
279         g_free (title);
280         
281         if (data) 
282         {
283                 gtk_entry_set_text (GTK_ENTRY (priv->entry_outgoingserver), data->hostname);
284                 gtk_entry_set_text (GTK_ENTRY (priv->entry_user_username), data->username);     
285                 gtk_entry_set_text (GTK_ENTRY (priv->entry_user_password), data->password);
286         
287                 modest_serversecurity_combo_box_set_active_serversecurity (
288                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), data->security);
289         
290                 modest_secureauth_combo_box_set_active_secureauth (
291                 MODEST_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth), data->secure_auth);
292                 
293                 /* port: */
294                 hildon_number_editor_set_value (
295                         HILDON_NUMBER_EDITOR (priv->entry_port), data->port);
296                 
297                 
298                 /* This will cause changed signals so we set dirty back to FALSE */
299                 priv->is_dirty = FALSE;
300         }
301 }
302
303 /*
304  * The result must be freed with modest_account_mgr_free_server_account_data(). */
305 ModestServerAccountData*
306 modest_connection_specific_smtp_edit_window_get_settings (
307         ModestConnectionSpecificSmtpEditWindow *window, 
308         ModestAccountMgr *account_manager)
309 {
310         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
311                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
312         
313         /* Use g_slice_new0(), because that's what modest_account_mgr_free_server_account_data() 
314          * expects us to use. */
315         ModestServerAccountData *result = g_slice_new0 (ModestServerAccountData);
316         
317         result->hostname = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver)));
318         result->username = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_user_username)));       
319         result->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_user_password)));
320         
321         result->security = modest_serversecurity_combo_box_get_active_serversecurity (
322                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security));
323         
324         result->secure_auth = modest_secureauth_combo_box_get_active_secureauth (
325                 MODEST_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth));
326                 
327         /* port: */
328         result->port = hildon_number_editor_get_value (
329                         HILDON_NUMBER_EDITOR (priv->entry_port));
330                         
331         return result;
332 }
333
334 gboolean modest_connection_specific_smtp_edit_window_is_dirty(
335         ModestConnectionSpecificSmtpEditWindow *window)
336 {
337         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
338                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
339         
340         return priv->is_dirty;
341 }