2007-06-30 Johannes Schmid <johannes.schmid@openismus.com>
[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_value_changed(GtkWidget* widget, GValue* value, ModestConnectionSpecificSmtpEditWindow *self)
130 {
131         on_change(widget, self);
132 }
133
134 static void
135 on_combo_security_changed (GtkComboBox *widget, gpointer user_data)
136 {
137         ModestConnectionSpecificSmtpEditWindow *self = 
138                 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (user_data);
139         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
140                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
141         
142         on_change(GTK_WIDGET(widget), self);
143         
144         const gint port_number = 
145                 modest_serversecurity_combo_box_get_active_serversecurity_port (
146                         MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security));
147
148         if(port_number != 0) {
149                 hildon_number_editor_set_value (
150                         HILDON_NUMBER_EDITOR (priv->entry_port), port_number);
151         }               
152 }
153
154 static void
155 modest_connection_specific_smtp_edit_window_init (ModestConnectionSpecificSmtpEditWindow *self)
156 {
157         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
158                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (self);
159         
160         GtkWidget *box = GTK_DIALOG(self)->vbox; /* gtk_vbox_new (FALSE, MODEST_MARGIN_HALF); */
161         gtk_box_set_spacing (GTK_BOX (box), MODEST_MARGIN_NONE);
162         gtk_container_set_border_width (GTK_CONTAINER (box), MODEST_MARGIN_HALF);
163         
164         /* Create a size group to be used by all captions.
165          * Note that HildonCaption does not create a default size group if we do not specify one.
166          * We use GTK_SIZE_GROUP_HORIZONTAL, so that the widths are the same. */
167         GtkSizeGroup *sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
168          
169         /* The outgoing server widgets: */
170         if (!priv->entry_outgoingserver)
171                 priv->entry_outgoingserver = gtk_entry_new ();
172         /* Auto-capitalization is the default, so let's turn it off: */
173         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_outgoingserver), HILDON_GTK_INPUT_MODE_FULL);
174         g_signal_connect(G_OBJECT(priv->entry_outgoingserver), "changed", G_CALLBACK(on_change), self);
175         
176         GtkWidget *caption = hildon_caption_new (sizegroup, 
177                 _("mcen_li_emailsetup_smtp"), priv->entry_outgoingserver, NULL, HILDON_CAPTION_OPTIONAL);
178         gtk_widget_show (priv->entry_outgoingserver);
179         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
180         gtk_widget_show (caption);
181         
182         /* The secure authentication widgets: */
183         if (!priv->combo_outgoing_auth)
184                 priv->combo_outgoing_auth = GTK_WIDGET (modest_secureauth_combo_box_new ());
185         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_authentication"), 
186                 priv->combo_outgoing_auth, NULL, HILDON_CAPTION_OPTIONAL);
187         g_signal_connect (G_OBJECT (priv->combo_outgoing_auth), "changed", G_CALLBACK(on_change), self);
188         gtk_widget_show (priv->combo_outgoing_auth);
189         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
190         gtk_widget_show (caption);
191         
192         /* The username widgets: */     
193         priv->entry_user_username = GTK_WIDGET (modest_validating_entry_new ());
194         /* Auto-capitalization is the default, so let's turn it off: */
195         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_username), HILDON_GTK_INPUT_MODE_FULL);
196         caption = hildon_caption_new (sizegroup, _("mail_fi_username"), 
197                 priv->entry_user_username, NULL, HILDON_CAPTION_MANDATORY);
198         g_signal_connect(G_OBJECT(priv->entry_user_username), "changed", G_CALLBACK(on_change), self);
199         gtk_widget_show (priv->entry_user_username);
200         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
201         gtk_widget_show (caption);
202         
203         /* Prevent the use of some characters in the username, 
204          * as required by our UI specification: */
205         modest_validating_entry_set_unallowed_characters_whitespace (
206                 MODEST_VALIDATING_ENTRY (priv->entry_user_username));
207         
208         /* Set max length as in the UI spec:
209          * TODO: The UI spec seems to want us to show a dialog if we hit the maximum. */
210         gtk_entry_set_max_length (GTK_ENTRY (priv->entry_user_username), 64);
211         
212         /* The password widgets: */     
213         priv->entry_user_password = gtk_entry_new ();
214         /* Auto-capitalization is the default, so let's turn it off: */
215         hildon_gtk_entry_set_input_mode (GTK_ENTRY (priv->entry_user_password), 
216                 HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
217         gtk_entry_set_visibility (GTK_ENTRY (priv->entry_user_password), FALSE);
218         /* gtk_entry_set_invisible_char (GTK_ENTRY (priv->entry_user_password), '*'); */
219         caption = hildon_caption_new (sizegroup, 
220                 _("mail_fi_password"), priv->entry_user_password, NULL, HILDON_CAPTION_OPTIONAL);
221         g_signal_connect(G_OBJECT(priv->entry_user_password), "changed", G_CALLBACK(on_change), self);
222         gtk_widget_show (priv->entry_user_password);
223         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
224         gtk_widget_show (caption);
225         
226         /* The secure connection widgets: */    
227         if (!priv->combo_outgoing_security)
228                 priv->combo_outgoing_security = GTK_WIDGET (modest_serversecurity_combo_box_new ());
229         modest_serversecurity_combo_box_fill (
230                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), MODEST_PROTOCOL_TRANSPORT_SMTP);
231         modest_serversecurity_combo_box_set_active_serversecurity (
232                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), MODEST_PROTOCOL_CONNECTION_NORMAL);
233         caption = hildon_caption_new (sizegroup, _("mcen_li_emailsetup_secure_connection"), 
234                 priv->combo_outgoing_security, NULL, HILDON_CAPTION_OPTIONAL);
235         gtk_widget_show (priv->combo_outgoing_security);
236         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
237         gtk_widget_show (caption);
238         
239         /* The port number widgets: */
240         if (!priv->entry_port)
241                 priv->entry_port = GTK_WIDGET (hildon_number_editor_new (0, 65535));
242         caption = hildon_caption_new (sizegroup, 
243                 _("mcen_li_emailsetup_smtp"), priv->entry_port, NULL, HILDON_CAPTION_OPTIONAL);
244         g_signal_connect(G_OBJECT(priv->entry_port), "notify::value", G_CALLBACK(on_value_changed), self);
245         gtk_widget_show (priv->entry_port);
246         gtk_box_pack_start (GTK_BOX (box), caption, FALSE, FALSE, MODEST_MARGIN_HALF);
247         gtk_widget_show (caption);
248         
249         /* Show a default port number when the security method changes, as per the UI spec: */
250         g_signal_connect (G_OBJECT (priv->combo_outgoing_security), "changed", (GCallback)on_combo_security_changed, self);
251         on_combo_security_changed (GTK_COMBO_BOX (priv->combo_outgoing_security), self);
252         
253         /* Add the buttons: */
254         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_OK, GTK_RESPONSE_OK);
255         gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
256         
257         priv->is_dirty = FALSE;
258         
259         gtk_widget_show (box);
260         
261         
262         /* When this window is shown, hibernation should not be possible, 
263          * because there is no sensible way to save the state: */
264     modest_window_mgr_prevent_hibernation_while_window_is_shown (
265         modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
266 }
267
268 ModestConnectionSpecificSmtpEditWindow*
269 modest_connection_specific_smtp_edit_window_new (void)
270 {
271         return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW, NULL);
272 }
273
274 void
275 modest_connection_specific_smtp_edit_window_set_connection (
276         ModestConnectionSpecificSmtpEditWindow *window, const gchar* iap_id, const gchar* iap_name,
277         const ModestServerAccountData *data)
278 {
279         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
280                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
281
282         /* This causes a warning because of the %s in the translation, but not in the original string: */
283         gchar* title = g_strdup_printf (_("mcen_ti_connection_connection_name"), iap_name);
284         gtk_window_set_title (GTK_WINDOW (window), title);
285         g_free (title);
286         
287         if (data) 
288         {
289                 gtk_entry_set_text (GTK_ENTRY (priv->entry_outgoingserver), data->hostname);
290                 gtk_entry_set_text (GTK_ENTRY (priv->entry_user_username), data->username);     
291                 gtk_entry_set_text (GTK_ENTRY (priv->entry_user_password), data->password);
292         
293                 modest_serversecurity_combo_box_set_active_serversecurity (
294                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security), data->security);
295         
296                 modest_secureauth_combo_box_set_active_secureauth (
297                 MODEST_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth), data->secure_auth);
298                 
299                 /* port: */
300                 hildon_number_editor_set_value (
301                         HILDON_NUMBER_EDITOR (priv->entry_port), data->port);
302                 
303                 
304                 /* This will cause changed signals so we set dirty back to FALSE */
305                 priv->is_dirty = FALSE;
306         }
307 }
308
309 /*
310  * The result must be freed with modest_account_mgr_free_server_account_data(). */
311 ModestServerAccountData*
312 modest_connection_specific_smtp_edit_window_get_settings (
313         ModestConnectionSpecificSmtpEditWindow *window, 
314         ModestAccountMgr *account_manager)
315 {
316         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
317                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
318         
319         /* Use g_slice_new0(), because that's what modest_account_mgr_free_server_account_data() 
320          * expects us to use. */
321         ModestServerAccountData *result = g_slice_new0 (ModestServerAccountData);
322         
323         result->hostname = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_outgoingserver)));
324         result->username = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_user_username)));       
325         result->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry_user_password)));
326         
327         result->security = modest_serversecurity_combo_box_get_active_serversecurity (
328                 MODEST_SERVERSECURITY_COMBO_BOX (priv->combo_outgoing_security));
329         
330         result->secure_auth = modest_secureauth_combo_box_get_active_secureauth (
331                 MODEST_SECUREAUTH_COMBO_BOX (priv->combo_outgoing_auth));
332                 
333         /* port: */
334         result->port = hildon_number_editor_get_value (
335                         HILDON_NUMBER_EDITOR (priv->entry_port));
336                         
337         return result;
338 }
339
340 gboolean modest_connection_specific_smtp_edit_window_is_dirty(
341         ModestConnectionSpecificSmtpEditWindow *window)
342 {
343         ModestConnectionSpecificSmtpEditWindowPrivate *priv = 
344                 CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW_GET_PRIVATE (window);
345         
346         return priv->is_dirty;
347 }