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